v0.3.3 - Ensure commit.template is set

We currently ensure this by simply overriding any existing template.

This is of course sub-optimal, but the previous recommendation, to
globally set `commit.template` to a relative path would otherwise make
`git commit` crash when run in a repo without first creating that
template file, e.g. by running `git mob ...` or `git solo` in the
project directory before committing anything.

This way, we don't have to globally set `commit.template`, and git-mob
will still work.
This commit is contained in:
Martin Frost 2020-10-24 18:08:13 +02:00
parent 20997a0605
commit 4575605991
6 changed files with 28 additions and 8 deletions

2
Cargo.lock generated
View File

@ -157,7 +157,7 @@ dependencies = [
[[package]] [[package]]
name = "git_mob" name = "git_mob"
version = "0.3.2" version = "0.3.3"
dependencies = [ dependencies = [
"dirs", "dirs",
"git2", "git2",

View File

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

View File

@ -12,9 +12,16 @@ It is essentially a Rust clone of the [git-mob NPM package](https://www.npmjs.co
Just run `cargo install git_mob` and you should be all set. Just run `cargo install git_mob` and you should be all set.
If it does not seem to work, try to run `git config --global commit.template ## A note on commit template and git-mob
.git/.gitmessage` to ensure that you have configured your your gitmessage
template to where `git-mob` is looking for it. `git-mob` _will_ currently override any existing `commit.template` setting
in any project where it is run. It does this in order to ensure that `git
commit` will pick up your current mob.
The future plan is to do something a bit smarter, like first detecting if the
repo already has a `commit.template` setting, and in that case, modify the
existing template by adding `Co-authored-by:` trailers to it, or something
similar.
## Examples ## Examples

View File

@ -1,5 +1,5 @@
use git_mob::{ use git_mob::{
get_available_coauthors, get_main_author, set_main_author, ensure_commit_template_is_set, get_available_coauthors, get_main_author, set_main_author,
with_gitmessage_template_path_or_exit, Author, with_gitmessage_template_path_or_exit, Author,
}; };
use std::fs; use std::fs;
@ -31,6 +31,7 @@ fn main() {
} }
write_coauthors_to_gitmessage_file(&opt.coauthors); write_coauthors_to_gitmessage_file(&opt.coauthors);
ensure_commit_template_is_set();
} }
fn list_coauthors() { fn list_coauthors() {

View File

@ -1,4 +1,6 @@
use git_mob::{get_main_author, with_gitmessage_template_path_or_exit}; use git_mob::{
ensure_commit_template_is_set, get_main_author, with_gitmessage_template_path_or_exit,
};
use std::fs::File; use std::fs::File;
fn main() { fn main() {
@ -7,5 +9,6 @@ fn main() {
with_gitmessage_template_path_or_exit(|path| { with_gitmessage_template_path_or_exit(|path| {
let _template = File::create(path); let _template = File::create(path);
}) });
ensure_commit_template_is_set();
} }

View File

@ -42,6 +42,15 @@ pub fn set_main_author(author: &Author) {
}); });
} }
pub fn ensure_commit_template_is_set() {
with_git_repo_or_exit(|repo| {
let mut config = repo.config().unwrap();
config
.set_str("commit.template", &".git/.gitmessage")
.unwrap();
})
}
pub fn get_available_coauthors() -> BTreeMap<String, Author> { pub fn get_available_coauthors() -> BTreeMap<String, Author> {
match parse_coauthors_file() { match parse_coauthors_file() {
Ok(coauthors) => coauthors, Ok(coauthors) => coauthors,