diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 7cee19b..c307ec5 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -8,6 +8,7 @@ on: env: CARGO_TERM_COLOR: always + XDBM_ENABLE_OVERWRITE_GITCONFIG: true jobs: build-and-lint: diff --git a/CHANGELOG.md b/CHANGELOG.md index 77119fe..4a1a66f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## [Unreleased] + +### Fixed +- Git local config is now looked up. (#20) +- Git global config will not be polluted in test by default. (#20) + ## [0.3.0] - 2024-12-02 ### Added diff --git a/src/main.rs b/src/main.rs index 23dadcd..32cf7d6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -156,7 +156,7 @@ fn add_and_commit(repo: &Repository, path: &Path, message: &str) -> Result Result<()> { - let f = match File::create_new( - home_dir() - .context("Failed to find home directory")? - .join(".gitconfig"), - ) { + let config = git2::Config::open_default().expect("failed to get default"); + if config.get_string("user.name").is_ok() && config.get_string("user.email").is_ok() { + return Ok(()); + }; + + match std::env::var_os(IS_GIT_CONFIG_WRITABLE) { + Some(_) => { + debug!( + "global git config not found & env var `{}` found", + IS_GIT_CONFIG_WRITABLE + ); + } + None => { + eprintln!("Failed to get git global config"); + eprintln!( + "Set env var `{}` to set automatically (mainly for CI)", + IS_GIT_CONFIG_WRITABLE + ); + return Err(anyhow!("failed to get git global config")); + } + }; + + let config_file = git2::Config::find_global().map_or_else( + |e| { + trace!("global git config file not found: {e:?}"); + Ok(dirs::home_dir() + .context("Failed to get home dir")? + .join(".gitconfig")) + }, + Ok, + )?; + let f = match File::options() + .create(true) + .truncate(true) + .write(true) + .open(config_file) + { io::Result::Ok(f) => f, io::Result::Err(_err) => return Ok(()), }; @@ -47,8 +81,10 @@ mod integrated_test { cmd.arg("-c") .arg(config_dir.path()) .arg("init") - .arg("testdev"); + .arg("testdev") + .arg("-vvvv"); cmd.assert().success().stdout(predicate::str::contains("")); + eprintln!("{:?}", fs::read_dir(config_dir.path())?.collect::>()); assert_eq!( std::fs::read_to_string(config_dir.path().join("devname"))?, "testdev\n" @@ -253,6 +289,7 @@ mod integrated_test { // // devices: first, second let config_dir_2 = assert_fs::TempDir::new()?; + setup_gitconfig()?; let mut cmd2 = Command::cargo_bin("xdbm")?; cmd2.arg("-c") .arg(config_dir_2.path())