v0.3.0 - Override main author

This change allows the `-o <initials>` flag to override who the main
author of any commits in this repo will be going forward.

What this actually does, is that it modifies the Repository's config
file to change the `user.name` and `user.email` like this.

Running `git mob -o ae be` (given the corresponding authors have been
configured using `git add-coauthor`) is equivalent to doing:

    git config user.name "Alice Example"
    git config user.email "alice@example.com"
    git mob be # be being the initials of "Bob Example" <bob@example.com>
This commit is contained in:
Martin Frost 2020-06-19 13:25:09 +02:00 committed by Martin Frost
parent dc822cdcac
commit 5f88d5a029
4 changed files with 35 additions and 4 deletions

View File

@ -1,6 +1,6 @@
[package]
name = "git_mob"
version = "0.2.0"
version = "0.3.0"
authors = ["Martin Frost <martin@frost.ws>"]
edition = "2018"
description = "A CLI tool for social coding."

View File

@ -15,10 +15,11 @@ Just run `cargo install git_mob` and you should be all set.
## Examples
* Add Alice and Bob as a possible co-authors:
* Add Alice, Bob, and yourself as a possible co-authors:
git add-coauthor a "Alice" alice@example.com
git add-coauthor b "bob" Bob@example.com
git add-coauthor me "me" myself.i@example.com
* Set Alice as co-author, making your mob consist of you and Alice:
@ -28,6 +29,10 @@ Just run `cargo install git_mob` and you should be all set.
git mob a b
* Set Alice as the main author for any commits you make, since she is the one doing most of the thinking anyway, and add yourself as a mob member:
git mob -o a b me
* Edit Bob's name, since you accidentally capitalized his email instead of his name:
git edit-coauthor b --name "Bob" --email bob@example.com
@ -49,6 +54,7 @@ Just run `cargo install git_mob` and you should be all set.
* `git mob <co-author-initials>`
* `git add-coauthor <initials> "Co-author Name" <co-author-email-address>`
* `git -o <initials>` for overwriting the main author
* `git edit-coauthor [--name "Co-author Name"] [--email <co-author-email-address>]`
* `git delete-coauthor <initials>`
* `git mob -l`
@ -65,7 +71,6 @@ be implemented, and then there is also a severe lack of tests and documentation.
* `git mob-print`
* `git suggest-coauthors`
* `-o` for overwriting the main author
* `--installTemplate` and `--uninstallTemplate` for prepare-commit-msg

View File

@ -1,4 +1,4 @@
use git_mob::{Author, get_main_author, get_available_coauthors, with_gitmessage_template_path_or_exit};
use git_mob::{Author, get_main_author, get_available_coauthors, with_gitmessage_template_path_or_exit, set_main_author};
use structopt::StructOpt;
use std::process;
use std::fs;
@ -8,6 +8,9 @@ struct Opt {
/// Prints list of available co-authors
#[structopt(short,long)]
list: bool,
/// Overwrite the main author
#[structopt(short,long)]
overwrite: Option<String>,
/// A list of co-author initials
coauthors: Vec<String>,
}
@ -20,6 +23,10 @@ fn main() {
process::exit(0);
}
if let Some(initials) = opt.overwrite {
override_main_author(&initials);
}
write_coauthors_to_gitmessage_file(&opt.coauthors);
}
@ -29,6 +36,17 @@ fn list_coauthors() {
}
}
fn override_main_author(initials: &String) {
let all_authors = get_available_coauthors();
match all_authors.get(initials) {
Some(new_main_author) => set_main_author(&new_main_author),
None => {
eprintln!("Error: author with initials {} not found", initials);
process::exit(1);
}
}
}
fn write_coauthors_to_gitmessage_file(coauthor_initials: &[String]) {
let coauthors = select_coauthors(&coauthor_initials);
let mut content = String::from("\n\n");

View File

@ -34,6 +34,14 @@ pub fn get_main_author() -> Author {
}
}
pub fn set_main_author(author: &Author) {
with_git_repo_or_exit(|repo| {
let mut config = repo.config().unwrap();
config.set_str("user.name", &author.name).unwrap();
config.set_str("user.email", &author.email).unwrap();
});
}
pub fn get_available_coauthors() -> BTreeMap<String, Author> {
match parse_coauthors_file() {
Ok(coauthors) => coauthors,