From 3222cbef59c9d07a07ca6fa11132ec1ce66827b8 Mon Sep 17 00:00:00 2001 From: qwjyh Date: Sun, 9 Feb 2025 07:35:09 +0900 Subject: [PATCH 1/4] debug(CI): inspect git config --- tests/cli.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/cli.rs b/tests/cli.rs index 5041385..da2eaca 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -16,6 +16,13 @@ mod integrated_test { /// /// This function will return an error if it failed to get home directory. fn setup_gitconfig(path: &std::path::Path) -> Result<()> { + eprintln!("{:?}", git2::Config::find_global()); + eprintln!( + "{:?}", + git2::Config::open_default() + .expect("failed to get default") + .get_string("user.name") + ); let git_dir = path.join(".git"); if !git_dir.exists() { fs::DirBuilder::new().create(git_dir.clone())? From 1891e222c34b430c569b451e4da699b413dce95c Mon Sep 17 00:00:00 2001 From: qwjyh Date: Sun, 9 Feb 2025 08:08:29 +0900 Subject: [PATCH 2/4] fix(CI): add env var to allow setting git global config --- .github/workflows/rust.yml | 1 + tests/cli.rs | 58 +++++++++++++++++++++++++++----------- 2 files changed, 42 insertions(+), 17 deletions(-) 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/tests/cli.rs b/tests/cli.rs index da2eaca..dc6337b 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -4,30 +4,57 @@ mod integrated_test { io::{self, BufWriter, Write}, }; - use anyhow::{Ok, Result}; + use anyhow::{anyhow, Context, Ok, Result}; use assert_cmd::{assert::OutputAssertExt, Command}; use git2::Repository; - use log::trace; + use log::{debug, trace}; use predicates::{boolean::PredicateBooleanExt, prelude::predicate}; + const IS_GIT_CONFIG_WRITABLE: &str = "XDBM_ENABLE_OVERWRITE_GITCONFIG"; + /// Setup global gitconfig if it doesn't exist. /// /// # Errors /// /// This function will return an error if it failed to get home directory. fn setup_gitconfig(path: &std::path::Path) -> Result<()> { - eprintln!("{:?}", git2::Config::find_global()); - eprintln!( - "{:?}", - git2::Config::open_default() - .expect("failed to get default") - .get_string("user.name") - ); - let git_dir = path.join(".git"); - if !git_dir.exists() { - fs::DirBuilder::new().create(git_dir.clone())? - } - let f = match File::create_new(git_dir.join("config")) { + 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(()), }; @@ -41,9 +68,6 @@ mod integrated_test { .as_bytes(), )?; - eprintln!("{:?}", fs::read_dir(path)?.collect::>()); - eprintln!("{:?}", fs::read_dir(git_dir)?.collect::>()); - Ok(()) } From 60c35c93786b4975dbce7dfd700e25a9347beaa5 Mon Sep 17 00:00:00 2001 From: qwjyh Date: Sun, 9 Feb 2025 08:14:25 +0900 Subject: [PATCH 3/4] refactor(CI): remove unnecessary arg for setup_gitconfig --- tests/cli.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/tests/cli.rs b/tests/cli.rs index dc6337b..06e3890 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -16,8 +16,9 @@ mod integrated_test { /// /// # Errors /// - /// This function will return an error if it failed to get home directory. - fn setup_gitconfig(path: &std::path::Path) -> Result<()> { + /// This function will return an error if it failed to get git global config and environment + /// variable [XDBM_ENABLE_OVERWRITE_GITCONFIG](`IS_GIT_CONFIG_WRITABLE`) is not set. + fn setup_gitconfig() -> Result<()> { 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(()); @@ -74,7 +75,7 @@ mod integrated_test { #[test] fn single_device() -> Result<()> { let config_dir = assert_fs::TempDir::new()?; - setup_gitconfig(&config_dir)?; + setup_gitconfig()?; // init let mut cmd = Command::cargo_bin("xdbm")?; cmd.arg("-c") @@ -170,7 +171,7 @@ mod integrated_test { fn two_devices_with_same_name() -> Result<()> { // 1st device let config_dir_1 = assert_fs::TempDir::new()?; - setup_gitconfig(&config_dir_1)?; + setup_gitconfig()?; let mut cmd1 = Command::cargo_bin("xdbm")?; cmd1.arg("-c") .arg(config_dir_1.path()) @@ -203,7 +204,7 @@ mod integrated_test { // 2nd device let config_dir_2 = assert_fs::TempDir::new()?; - setup_gitconfig(&config_dir_2)?; + setup_gitconfig()?; let mut cmd2 = Command::cargo_bin("xdbm")?; cmd2.arg("-c") .arg(config_dir_2.path()) @@ -219,7 +220,7 @@ mod integrated_test { fn directory_without_parent() -> Result<()> { // 1st device let config_dir_1 = assert_fs::TempDir::new()?; - setup_gitconfig(&config_dir_1)?; + setup_gitconfig()?; let mut cmd1 = Command::cargo_bin("xdbm")?; cmd1.arg("-c") .arg(config_dir_1.path()) @@ -254,7 +255,7 @@ mod integrated_test { // // devices: first let config_dir_1 = assert_fs::TempDir::new()?; - setup_gitconfig(&config_dir_1)?; + setup_gitconfig()?; let mut cmd1 = Command::cargo_bin("xdbm")?; cmd1.arg("-c") .arg(config_dir_1.path()) @@ -288,7 +289,7 @@ mod integrated_test { // // devices: first, second let config_dir_2 = assert_fs::TempDir::new()?; - setup_gitconfig(&config_dir_2)?; + setup_gitconfig()?; let mut cmd2 = Command::cargo_bin("xdbm")?; cmd2.arg("-c") .arg(config_dir_2.path()) From df7b1dd819276df6203b41cd57f6ce37528fc2ff Mon Sep 17 00:00:00 2001 From: qwjyh Date: Sun, 9 Feb 2025 08:37:32 +0900 Subject: [PATCH 4/4] update changelog --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) 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