Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recognize files in $XDG_CONFIG_HOME/git/ and $HOME/.config/git/ better #2067

Merged
merged 11 commits into from
Feb 26, 2022
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,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
- `Git`: Associate global config files in all valid locations. See #2067 (@cyqsimon)
cyqsimon marked this conversation as resolved.
Show resolved Hide resolved

## Themes

Expand Down
19 changes: 17 additions & 2 deletions src/syntax_mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,23 @@ 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
// `$XDG_CONFIG_HOME/git`, or `$HOME/.config/git` if `$XDG_CONFIG_HOME` is not set or empty
// see https://git-scm.com/docs/git-config#FILES
// we cover both cases regardless of the state of `$XDG_CONFIG_HOME`
cyqsimon marked this conversation as resolved.
Show resolved Hide resolved
if let Some(xdg_config_home) =
std::env::var_os("XDG_CONFIG_HOME").and_then(|val| (!val.is_empty()).then(|| val))
{
insert_git_config_global(&mut mapping, &xdg_config_home);
}
if let Some(default_config_home) =
std::env::var_os("HOME").map(|home| Path::new(&home).join(".config"))
cyqsimon marked this conversation as resolved.
Show resolved Hide resolved
{
insert_git_config_global(&mut mapping, &default_config_home);
}

fn insert_git_config_global(mapping: &mut SyntaxMapping, config_home: impl AsRef<Path>) {
let git_config_path = config_home.as_ref().join("git");

mapping
.insert(
Expand Down
3 changes: 3 additions & 0 deletions tests/examples/git/.config/git/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[user]
email = foo@bar.net
name = foobar
3 changes: 3 additions & 0 deletions tests/examples/git/.gitconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[user]
email = foo@bar.net
name = foobar
43 changes: 42 additions & 1 deletion tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
);
cyqsimon marked this conversation as resolved.
Show resolved Hide resolved
}

#[cfg(target_os = "linux")]
Expand Down Expand Up @@ -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()
Expand Down