From 184a4755c76699d126c14d012a06a6a19b6182ee Mon Sep 17 00:00:00 2001 From: cyqsimon <28627918+cyqsimon@users.noreply.github.com> Date: Wed, 9 Feb 2022 18:06:17 +0800 Subject: [PATCH 01/11] git global config - lookup $XDG_CONFIG_HOME faithfully --- src/syntax_mapping.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/syntax_mapping.rs b/src/syntax_mapping.rs index 6c7d999c7b..d0e206ff73 100644 --- a/src/syntax_mapping.rs +++ b/src/syntax_mapping.rs @@ -1,4 +1,4 @@ -use std::path::Path; +use std::path::{Path, PathBuf}; use crate::error::Result; use ignored_suffixes::IgnoredSuffixes; @@ -122,9 +122,18 @@ 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 + if let Some(git_config_path) = std::env::var_os("XDG_CONFIG_HOME") + .and_then(|val| if val.is_empty() { None } else { Some(val) }) + .map(PathBuf::from) + .or_else(|| std::env::var_os("HOME").map(|home| Path::new(&home).join(".config"))) + .map(|mut config_home| { + config_home.push("git"); + config_home + }) + { mapping .insert( &git_config_path.join("config").to_string_lossy(), From 021b1ee77e75fe0fee208a719b25b9d24fcae65e Mon Sep 17 00:00:00 2001 From: cyqsimon <28627918+cyqsimon@users.noreply.github.com> Date: Wed, 9 Feb 2022 18:14:26 +0800 Subject: [PATCH 02/11] Use `bool::then` --- src/syntax_mapping.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/syntax_mapping.rs b/src/syntax_mapping.rs index d0e206ff73..3671785a63 100644 --- a/src/syntax_mapping.rs +++ b/src/syntax_mapping.rs @@ -126,7 +126,7 @@ impl<'a> SyntaxMapping<'a> { // `$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 if let Some(git_config_path) = std::env::var_os("XDG_CONFIG_HOME") - .and_then(|val| if val.is_empty() { None } else { Some(val) }) + .and_then(|val| (!val.is_empty()).then(|| val)) .map(PathBuf::from) .or_else(|| std::env::var_os("HOME").map(|home| Path::new(&home).join(".config"))) .map(|mut config_home| { From 9d5fb8a9d3d48fe09e4bf07ea4727378fe4ebdf4 Mon Sep 17 00:00:00 2001 From: cyqsimon <28627918+cyqsimon@users.noreply.github.com> Date: Thu, 10 Feb 2022 13:14:27 +0800 Subject: [PATCH 03/11] Cover both `$XDG_CONFIG_HOME` & `$HOME/.config` --- src/syntax_mapping.rs | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/syntax_mapping.rs b/src/syntax_mapping.rs index 3671785a63..1cc8bde4e9 100644 --- a/src/syntax_mapping.rs +++ b/src/syntax_mapping.rs @@ -125,35 +125,41 @@ impl<'a> SyntaxMapping<'a> { // 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 - if let Some(git_config_path) = std::env::var_os("XDG_CONFIG_HOME") - .and_then(|val| (!val.is_empty()).then(|| val)) - .map(PathBuf::from) - .or_else(|| std::env::var_os("HOME").map(|home| Path::new(&home).join(".config"))) - .map(|mut config_home| { - config_home.push("git"); - config_home - }) + // we cover both cases regardless of the state of `$XDG_CONFIG_HOME` + 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")) + { + 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( &git_config_path.join("config").to_string_lossy(), MappingTarget::MapTo("Git Config"), ) - .ok(); + .unwrap(); mapping .insert( &git_config_path.join("ignore").to_string_lossy(), MappingTarget::MapTo("Git Ignore"), ) - .ok(); + .unwrap(); mapping .insert( &git_config_path.join("attributes").to_string_lossy(), MappingTarget::MapTo("Git Attributes"), ) - .ok(); + .unwrap(); } mapping From 6b8e12b125c48672ad9ce273ee80f4e6f7450103 Mon Sep 17 00:00:00 2001 From: cyqsimon <28627918+cyqsimon@users.noreply.github.com> Date: Thu, 10 Feb 2022 13:27:40 +0800 Subject: [PATCH 04/11] Remove unused import --- src/syntax_mapping.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/syntax_mapping.rs b/src/syntax_mapping.rs index 1cc8bde4e9..806432ccd3 100644 --- a/src/syntax_mapping.rs +++ b/src/syntax_mapping.rs @@ -1,4 +1,4 @@ -use std::path::{Path, PathBuf}; +use std::path::Path; use crate::error::Result; use ignored_suffixes::IgnoredSuffixes; From 0a7de4d10c2fb18f6c3b54a001f76ac5835090da Mon Sep 17 00:00:00 2001 From: cyqsimon <28627918+cyqsimon@users.noreply.github.com> Date: Fri, 18 Feb 2022 15:14:30 +0800 Subject: [PATCH 05/11] Global git config tests --- tests/examples/git/.config/git/config | 3 ++ tests/examples/git/.gitconfig | 3 ++ tests/integration_tests.rs | 43 ++++++++++++++++++++++++++- 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 tests/examples/git/.config/git/config create mode 100644 tests/examples/git/.gitconfig diff --git a/tests/examples/git/.config/git/config b/tests/examples/git/.config/git/config new file mode 100644 index 0000000000..6b0bd1d6b1 --- /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 0000000000..6b0bd1d6b1 --- /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 65da610a6c..4bfc507b6d 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;2;248;248;242m[\u{1b}[0m\u{1b}[38;2;166;226;46muser\u{1b}[0m\u{1b}[38;2;248;248;242m]\u{1b}[0m +\u{1b}[38;2;248;248;242m \u{1b}[0m\u{1b}[38;2;255;255;255memail\u{1b}[0m\u{1b}[38;2;248;248;242m \u{1b}[0m\u{1b}[38;2;249;38;114m=\u{1b}[0m\u{1b}[38;2;248;248;242m \u{1b}[0m\u{1b}[38;2;230;219;116mfoo@bar.net\u{1b}[0m +\u{1b}[38;2;248;248;242m \u{1b}[0m\u{1b}[38;2;255;255;255mname\u{1b}[0m\u{1b}[38;2;248;248;242m \u{1b}[0m\u{1b}[38;2;249;38;114m=\u{1b}[0m\u{1b}[38;2;248;248;242m \u{1b}[0m\u{1b}[38;2;230;219;116mfoobar\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() From b547e2c300a8bfbfe1aaa53e35815c4f80837fd4 Mon Sep 17 00:00:00 2001 From: cyqsimon <28627918+cyqsimon@users.noreply.github.com> Date: Fri, 18 Feb 2022 15:21:03 +0800 Subject: [PATCH 06/11] Added trailing newline --- tests/integration_tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index 4bfc507b6d..4ac952a879 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -1367,7 +1367,7 @@ fn all_global_git_config_locations_syntax_mapping_work() { let expected = "\ \u{1b}[38;2;248;248;242m[\u{1b}[0m\u{1b}[38;2;166;226;46muser\u{1b}[0m\u{1b}[38;2;248;248;242m]\u{1b}[0m \u{1b}[38;2;248;248;242m \u{1b}[0m\u{1b}[38;2;255;255;255memail\u{1b}[0m\u{1b}[38;2;248;248;242m \u{1b}[0m\u{1b}[38;2;249;38;114m=\u{1b}[0m\u{1b}[38;2;248;248;242m \u{1b}[0m\u{1b}[38;2;230;219;116mfoo@bar.net\u{1b}[0m -\u{1b}[38;2;248;248;242m \u{1b}[0m\u{1b}[38;2;255;255;255mname\u{1b}[0m\u{1b}[38;2;248;248;242m \u{1b}[0m\u{1b}[38;2;249;38;114m=\u{1b}[0m\u{1b}[38;2;248;248;242m \u{1b}[0m\u{1b}[38;2;230;219;116mfoobar\u{1b}[0m"; +\u{1b}[38;2;248;248;242m \u{1b}[0m\u{1b}[38;2;255;255;255mname\u{1b}[0m\u{1b}[38;2;248;248;242m \u{1b}[0m\u{1b}[38;2;249;38;114m=\u{1b}[0m\u{1b}[38;2;248;248;242m \u{1b}[0m\u{1b}[38;2;230;219;116mfoobar\u{1b}[0m\n"; bat() .env("XDG_CONFIG_HOME", fake_home.join(".config").as_os_str()) From c0ef64bb50a2c7abc1136120c2be3279f5c56f67 Mon Sep 17 00:00:00 2001 From: cyqsimon <28627918+cyqsimon@users.noreply.github.com> Date: Fri, 18 Feb 2022 19:53:24 +0800 Subject: [PATCH 07/11] Fix git config test --- tests/integration_tests.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index 4ac952a879..6528ad99c1 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -1364,10 +1364,10 @@ fn ignored_suffix_arg() { #[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;2;248;248;242m[\u{1b}[0m\u{1b}[38;2;166;226;46muser\u{1b}[0m\u{1b}[38;2;248;248;242m]\u{1b}[0m -\u{1b}[38;2;248;248;242m \u{1b}[0m\u{1b}[38;2;255;255;255memail\u{1b}[0m\u{1b}[38;2;248;248;242m \u{1b}[0m\u{1b}[38;2;249;38;114m=\u{1b}[0m\u{1b}[38;2;248;248;242m \u{1b}[0m\u{1b}[38;2;230;219;116mfoo@bar.net\u{1b}[0m -\u{1b}[38;2;248;248;242m \u{1b}[0m\u{1b}[38;2;255;255;255mname\u{1b}[0m\u{1b}[38;2;248;248;242m \u{1b}[0m\u{1b}[38;2;249;38;114m=\u{1b}[0m\u{1b}[38;2;248;248;242m \u{1b}[0m\u{1b}[38;2;230;219;116mfoobar\u{1b}[0m\n"; + 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()) From 43ba0ec7d34d1744066e33e0c923ad5889b7111c Mon Sep 17 00:00:00 2001 From: cyqsimon <28627918+cyqsimon@users.noreply.github.com> Date: Sun, 20 Feb 2022 11:57:59 +0800 Subject: [PATCH 08/11] Wrote to changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 56d28a3fa1..76acf03e5b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) ## Themes From 0fec8d0c7502866902a44999dae788fadfdfbdc1 Mon Sep 17 00:00:00 2001 From: cyqsimon <28627918+cyqsimon@users.noreply.github.com> Date: Sun, 20 Feb 2022 22:06:59 +0800 Subject: [PATCH 09/11] Revert change of `Result::ok` to `Result::unwrap` --- src/syntax_mapping.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/syntax_mapping.rs b/src/syntax_mapping.rs index 806432ccd3..a4f36a52bf 100644 --- a/src/syntax_mapping.rs +++ b/src/syntax_mapping.rs @@ -145,21 +145,21 @@ impl<'a> SyntaxMapping<'a> { &git_config_path.join("config").to_string_lossy(), MappingTarget::MapTo("Git Config"), ) - .unwrap(); + .ok(); mapping .insert( &git_config_path.join("ignore").to_string_lossy(), MappingTarget::MapTo("Git Ignore"), ) - .unwrap(); + .ok(); mapping .insert( &git_config_path.join("attributes").to_string_lossy(), MappingTarget::MapTo("Git Attributes"), ) - .unwrap(); + .ok(); } mapping From 92c8483b1b246d826c4b4fa182884fbe23d79aa6 Mon Sep 17 00:00:00 2001 From: cyqsimon <28627918+cyqsimon@users.noreply.github.com> Date: Fri, 25 Feb 2022 19:36:53 +0800 Subject: [PATCH 10/11] Apply suggestions from code review Co-authored-by: Martin Nordholts --- CHANGELOG.md | 2 +- src/syntax_mapping.rs | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 76acf03e5b..c8095cae97 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,7 +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) +- 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 a4f36a52bf..1784ce0718 100644 --- a/src/syntax_mapping.rs +++ b/src/syntax_mapping.rs @@ -122,10 +122,8 @@ impl<'a> SyntaxMapping<'a> { .insert("*.hook", MappingTarget::MapTo("INI")) .unwrap(); - // 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` + // 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").and_then(|val| (!val.is_empty()).then(|| val)) { From 255653040fc2d2d25887c88bae13dc715de89e41 Mon Sep 17 00:00:00 2001 From: cyqsimon <28627918+cyqsimon@users.noreply.github.com> Date: Fri, 25 Feb 2022 19:44:41 +0800 Subject: [PATCH 11/11] Guard against empty `$HOME` --- src/syntax_mapping.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/syntax_mapping.rs b/src/syntax_mapping.rs index 1784ce0718..ee4fdc40f8 100644 --- a/src/syntax_mapping.rs +++ b/src/syntax_mapping.rs @@ -125,12 +125,13 @@ impl<'a> SyntaxMapping<'a> { // 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").and_then(|val| (!val.is_empty()).then(|| val)) + 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").map(|home| Path::new(&home).join(".config")) + 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); }