Wrap gitmessage template path in closure

Since the only thing the `repo` argument to `with_repo_or_exit` was used
for, was to extract the gitmessage template path from it, we can just do
that directly instead.
This commit is contained in:
Martin Frost 2020-05-24 13:48:51 +02:00
parent 67f3f6b9b0
commit 747e19828c
3 changed files with 9 additions and 15 deletions

View File

@ -1,5 +1,4 @@
use git_mob::{Author, get_main_author, get_available_coauthors, gitmessage_template_file_path, with_repo_or_exit};
use git2::Repository;
use git_mob::{Author, get_main_author, get_available_coauthors, with_gitmessage_template_path_or_exit};
use structopt::StructOpt;
use std::process;
use std::fs;
@ -37,9 +36,7 @@ fn write_coauthors_to_gitmessage_file(coauthor_initials: &[String]) {
content.push_str(&format!("Co-authored-by: {}\n", &author.to_string()));
}
with_repo_or_exit(|repo: Repository| {
let path = gitmessage_template_file_path(repo);
with_gitmessage_template_path_or_exit(|path| {
match fs::write(path, content) {
Ok(_) => {
println!("{}", get_main_author());

View File

@ -1,13 +1,11 @@
use git2::Repository;
use std::fs::File;
use git_mob::{get_main_author, gitmessage_template_file_path, with_repo_or_exit};
use git_mob::{get_main_author, with_gitmessage_template_path_or_exit};
fn main() {
let main_author = get_main_author();
println!("{}", main_author);
with_repo_or_exit(|repo: Repository| {
let path = gitmessage_template_file_path(repo);
with_gitmessage_template_path_or_exit(|path| {
let _template = File::create(path);
})
}

View File

@ -57,13 +57,12 @@ fn parse_coauthors_file() -> Result<BTreeMap<String, Author>, Box<dyn Error>> {
}
}
pub fn gitmessage_template_file_path(repo: Repository) -> std::path::PathBuf {
repo.path().join(".gitmessage")
}
pub fn with_repo_or_exit<F: FnOnce(Repository)>(f: F) {
pub fn with_gitmessage_template_path_or_exit<F: FnOnce(std::path::PathBuf)>(f: F) {
match Repository::open_from_env() {
Ok(repo) => f(repo),
Ok(repo) => {
let path = repo.path().join(".gitmessage");
f(path);
},
Err(_e) => {
eprintln!("Not in a git repository");
process::exit(1);