on master help sign in
Recognize files in `$XDG_CONFIG_HOME/git/` and `$HOME/.config/git/` better (#2067)

* git global config - lookup $XDG_CONFIG_HOME faithfully * Use `bool::then` * Cover both `$XDG_CONFIG_HOME` & `$HOME/.config` * Remove unused import * Global git config tests * Added trailing newline * Fix git config test * Wrote to changelog * Revert change of `Result::ok` to `Result::unwrap` * Apply suggestions from code review Co-authored-by: Martin Nordholts <enselic@gmail.com> * Guard against empty `$HOME` Co-authored-by: Martin Nordholts <enselic@gmail.com>

commit14ddda0a8b9727208e68f74615534f1b9ca20f42 authorcyqsimon <28627918+cyqsimon@users.noreply.github.com> dateSun 27 Feb 2022, 00:01 (5 years ago) parent36093dd
5 files changed+65−3
CHANGELOG.md+1 src/syntax_mapping.rs+16−2 tests/examples/git/.config/git/config+3 tests/examples/git/.gitconfig+3 tests/integration_tests.rs+42−1
CHANGELOG.md+1
@@ -18,6 +18,7 @@
1818- Associate `poetry.lock` files with the `TOML` syntax. See #2049
1919- Associate `.mesh`, `.task`, `.rgen`, `.rint`, `.rahit`, `.rchit`, `.rmiss`, and `.rcall` with the `GLSL` syntax. See #2050
2020- Added support for `JQ` syntax, see #2072
21+- Properly associate global git config files rooted in `$XDG_CONFIG_HOME/git/` or `$HOME/.config/git/`. See #2067 (@cyqsimon)
2122
2223## Themes
2324
src/syntax_mapping.rs+16−2
@@ -122,8 +122,22 @@ impl<'a> SyntaxMapping<'a> {
122122 .insert("*.hook", MappingTarget::MapTo("INI"))
123123 .unwrap();
124124
125− if let Some(xdg_config_home) = std::env::var_os("XDG_CONFIG_HOME") {
126− let git_config_path = Path::new(&xdg_config_home).join("git");
125+ // Global git config files rooted in `$XDG_CONFIG_HOME/git/` or `$HOME/.config/git/`
126+ // See e.g. https://git-scm.com/docs/git-config#FILES
127+ if let Some(xdg_config_home) =
128+ std::env::var_os("XDG_CONFIG_HOME").filter(|val| !val.is_empty())
129+ {
130+ insert_git_config_global(&mut mapping, &xdg_config_home);
131+ }
132+ if let Some(default_config_home) = std::env::var_os("HOME")
133+ .filter(|val| !val.is_empty())
134+ .map(|home| Path::new(&home).join(".config"))
135+ {
136+ insert_git_config_global(&mut mapping, &default_config_home);
137+ }
138+
139+ fn insert_git_config_global(mapping: &mut SyntaxMapping, config_home: impl AsRef<Path>) {
140+ let git_config_path = config_home.as_ref().join("git");
127141
128142 mapping
129143 .insert(
tests/examples/git/.config/git/confignew file+3
@@ -0,0 +1,3 @@
1+[user]
2+ email = foo@bar.net
3+ name = foobar
tests/examples/git/.gitconfignew file+3
@@ -0,0 +1,3 @@
1+[user]
2+ email = foo@bar.net
3+ name = foobar
tests/integration_tests.rs+42−1
@@ -1117,7 +1117,9 @@ Single Line
11171117────────────────────────────────────────────────────────────────────────────────
11181118",
11191119 )
1120− .stderr("\x1b[33m[bat warning]\x1b[0m: Style 'rule' is a subset of style 'grid', 'rule' will not be visible.\n");
1120+ .stderr(
1121+ "\x1b[33m[bat warning]\x1b[0m: Style 'rule' is a subset of style 'grid', 'rule' will not be visible.\n",
1122+ );
11211123}
11221124
11231125#[cfg(target_os = "linux")]
@@ -1359,6 +1361,45 @@ fn ignored_suffix_arg() {
13591361 .stderr("");
13601362}
13611363
1364+#[test]
1365+fn all_global_git_config_locations_syntax_mapping_work() {
1366+ let fake_home = Path::new(EXAMPLES_DIR).join("git").canonicalize().unwrap();
1367+ 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
1368+\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
1369+\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
1370+";
1371+
1372+ bat()
1373+ .env("XDG_CONFIG_HOME", fake_home.join(".config").as_os_str())
1374+ .arg("-f")
1375+ .arg("-p")
1376+ .arg("git/.config/git/config")
1377+ .assert()
1378+ .success()
1379+ .stdout(expected)
1380+ .stderr("");
1381+
1382+ bat()
1383+ .env("HOME", fake_home.as_os_str())
1384+ .arg("-f")
1385+ .arg("-p")
1386+ .arg("git/.config/git/config")
1387+ .assert()
1388+ .success()
1389+ .stdout(expected)
1390+ .stderr("");
1391+
1392+ bat()
1393+ .env("HOME", fake_home.as_os_str())
1394+ .arg("-f")
1395+ .arg("-p")
1396+ .arg("git/.gitconfig")
1397+ .assert()
1398+ .success()
1399+ .stdout(expected)
1400+ .stderr("");
1401+}
1402+
13621403#[test]
13631404fn acknowledgements() {
13641405 bat()