Extract `with_git_repo_or_exit`

This function will come in handy when doing other actions with a git
repo, like when modifying a repository's config.
This commit is contained in:
Martin Frost 2020-06-19 13:23:56 +02:00 committed by Martin Frost
parent 747e19828c
commit 15886667da
1 changed files with 9 additions and 3 deletions

View File

@ -57,11 +57,10 @@ fn parse_coauthors_file() -> Result<BTreeMap<String, Author>, Box<dyn Error>> {
}
}
pub fn with_gitmessage_template_path_or_exit<F: FnOnce(std::path::PathBuf)>(f: F) {
fn with_git_repo_or_exit<F: FnOnce(Repository)>(f: F) {
match Repository::open_from_env() {
Ok(repo) => {
let path = repo.path().join(".gitmessage");
f(path);
f(repo);
},
Err(_e) => {
eprintln!("Not in a git repository");
@ -70,6 +69,13 @@ pub fn with_gitmessage_template_path_or_exit<F: FnOnce(std::path::PathBuf)>(f: F
}
}
pub fn with_gitmessage_template_path_or_exit<F: FnOnce(std::path::PathBuf)>(f: F) {
with_git_repo_or_exit(|repo| {
let path = repo.path().join(".gitmessage");
f(path);
})
}
pub fn write_coauthors_file(authors: BTreeMap<String, Author>) {
let mut wrapper_tree = BTreeMap::new();
wrapper_tree.insert("coauthors", authors);