diff --git a/CHANGELOG.md b/CHANGELOG.md index 340cfaf8..1830ae03 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ - Associate `poetry.lock` files with the `TOML` syntax. See #2049 - Associate `.mesh`, `.task`, `.rgen`, `.rint`, `.rahit`, `.rchit`, `.rmiss`, and `.rcall` with the `GLSL` syntax. See #2050 - Added support for `JQ` syntax, see #2072 +- Properly associate global git config files rooted in `$XDG_CONFIG_HOME/git/` or `$HOME/.config/git/`. See #2067 (@cyqsimon) ## Themes diff --git a/src/syntax_mapping.rs b/src/syntax_mapping.rs index 6c7d999c..ee4fdc40 100644 --- a/src/syntax_mapping.rs +++ b/src/syntax_mapping.rs @@ -122,8 +122,22 @@ impl<'a> SyntaxMapping<'a> { .insert("*.hook", MappingTarget::MapTo("INI")) .unwrap(); - if let Some(xdg_config_home) = std::env::var_os("XDG_CONFIG_HOME") { - let git_config_path = Path::new(&xdg_config_home).join("git"); + // Global git config files rooted in `$XDG_CONFIG_HOME/git/` or `$HOME/.config/git/` + // See e.g. https://git-scm.com/docs/git-config#FILES + if let Some(xdg_config_home) = + std::env::var_os("XDG_CONFIG_HOME").filter(|val| !val.is_empty()) + { + insert_git_config_global(&mut mapping, &xdg_config_home); + } + if let Some(default_config_home) = std::env::var_os("HOME") + .filter(|val| !val.is_empty()) + .map(|home| Path::new(&home).join(".config")) + { + insert_git_config_global(&mut mapping, &default_config_home); + } + + fn insert_git_config_global(mapping: &mut SyntaxMapping, config_home: impl AsRef) { + let git_config_path = config_home.as_ref().join("git"); mapping .insert( diff --git a/tests/examples/git/.config/git/config b/tests/examples/git/.config/git/config new file mode 100644 index 00000000..6b0bd1d6 --- /dev/null +++ b/tests/examples/git/.config/git/config @@ -0,0 +1,3 @@ +[user] + email = foo@bar.net + name = foobar diff --git a/tests/examples/git/.gitconfig b/tests/examples/git/.gitconfig new file mode 100644 index 00000000..6b0bd1d6 --- /dev/null +++ b/tests/examples/git/.gitconfig @@ -0,0 +1,3 @@ +[user] + email = foo@bar.net + name = foobar diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index 65da610a..6528ad99 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -1117,7 +1117,9 @@ Single Line ──────────────────────────────────────────────────────────────────────────────── ", ) - .stderr("\x1b[33m[bat warning]\x1b[0m: Style 'rule' is a subset of style 'grid', 'rule' will not be visible.\n"); + .stderr( + "\x1b[33m[bat warning]\x1b[0m: Style 'rule' is a subset of style 'grid', 'rule' will not be visible.\n", + ); } #[cfg(target_os = "linux")] @@ -1359,6 +1361,45 @@ fn ignored_suffix_arg() { .stderr(""); } +#[test] +fn all_global_git_config_locations_syntax_mapping_work() { + let fake_home = Path::new(EXAMPLES_DIR).join("git").canonicalize().unwrap(); + let expected = "\u{1b}[38;5;231m[\u{1b}[0m\u{1b}[38;5;149muser\u{1b}[0m\u{1b}[38;5;231m]\u{1b}[0m +\u{1b}[38;5;231m \u{1b}[0m\u{1b}[38;5;231memail\u{1b}[0m\u{1b}[38;5;231m \u{1b}[0m\u{1b}[38;5;203m=\u{1b}[0m\u{1b}[38;5;231m \u{1b}[0m\u{1b}[38;5;186mfoo@bar.net\u{1b}[0m +\u{1b}[38;5;231m \u{1b}[0m\u{1b}[38;5;231mname\u{1b}[0m\u{1b}[38;5;231m \u{1b}[0m\u{1b}[38;5;203m=\u{1b}[0m\u{1b}[38;5;231m \u{1b}[0m\u{1b}[38;5;186mfoobar\u{1b}[0m +"; + + bat() + .env("XDG_CONFIG_HOME", fake_home.join(".config").as_os_str()) + .arg("-f") + .arg("-p") + .arg("git/.config/git/config") + .assert() + .success() + .stdout(expected) + .stderr(""); + + bat() + .env("HOME", fake_home.as_os_str()) + .arg("-f") + .arg("-p") + .arg("git/.config/git/config") + .assert() + .success() + .stdout(expected) + .stderr(""); + + bat() + .env("HOME", fake_home.as_os_str()) + .arg("-f") + .arg("-p") + .arg("git/.gitconfig") + .assert() + .success() + .stdout(expected) + .stderr(""); +} + #[test] fn acknowledgements() { bat()