Skip to content
Myr

ignore: support `GIT_CONFIG_GLOBAL` and `GIT_CONFIG_SYSTEM` for `core.excludesFile`

Before adoption: this commit came in with the history the project adopted, so no landing records it.

`gitconfig_excludes_path()` resolves the global gitignore by reading `core.excludesFile` from `$HOME/.gitconfig` and `$XDG_CONFIG_HOME/git/config`, but git 2.32 (June 2021) introduced `GIT_CONFIG_GLOBAL` and `GIT_CONFIG_SYSTEM` environment variables that override these locations. The `ignore` crate doesn't check either of them, so users who point `GIT_CONFIG_GLOBAL` at a non-standard path (e.g. NixOS managing git config at `/etc/git/config`) have their global gitignore silently ignored by ripgrep while `git` itself respects it. This commit checks `GIT_CONFIG_GLOBAL` before the hardcoded paths, matching git's semantics where it replaces both `$HOME/.gitconfig` and the XDG config. It also adds system-level config (`GIT_CONFIG_SYSTEM`, falling back to `/etc/gitconfig`) as a lowest-priority source for `core.excludesFile`, which the crate previously didn't consult at all. Fixes #3275, Closes #3276

authored by Amaan Qureshi · committed by Andrew Gallant · parent b6849da · Browse files · 4 files+101 added−27 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+101 added−27 removed

  • tests:+35 added−0 removed
  • other:+66 added−27 removed

4 files+101 added−27 removed

modified:crates/ignore/src/gitignore.rs+61 added-27 removed
121 unmodified lines
122 ///123 /// The global config file path is specified by git's `core.excludesFile`124 /// config option.125 ///126 /// # Behavior127 ///128 /// This routine does its best to discover any global git exclude files.129 /// This will try to parse out the `excludesFile` config option in your130 /// global git configuration, if necessary.131 ///132 /// The specific things this routine tries (which are subject to change133 /// based on how git behaves) are:134 ///135 ///136 ///137 /// Git's config file location is `$HOME/.gitconfig`. If `$HOME/.gitconfig`138 /// does not exist or does not specify `core.excludesFile`, then139 /// `$XDG_CONFIG_HOME/git/ignore` is read. If `$XDG_CONFIG_HOME` is not140 /// set or is empty, then `$HOME/.config/git/ignore` is used instead.
437 unmodified lines
578/// Return the file path of the current environment's global gitignore file.579///580/// Note that the file path returned may not exist.581pub fn gitconfig_excludes_path() -> Option<PathBuf> {571 // git supports $HOME/.gitconfig and $XDG_CONFIG_HOME/git/config. Notably,572 // both can be active at the same time, where $HOME/.gitconfig takes573 // precedent. So if $HOME/.gitconfig defines a `core.excludesFile`, then574 // we're done.575 match gitconfig_home_contents().and_then(|x| parse_excludes_file(&x)) {576 Some(path) => return Some(path),577 None => {}582 // When GIT_CONFIG_GLOBAL is set, it replaces both $HOME/.gitconfig and583 // $XDG_CONFIG_HOME/git/config (per git 2.32+). Otherwise, git supports584 // $HOME/.gitconfig and $XDG_CONFIG_HOME/git/config simultaneously, where585 // $HOME/.gitconfig takes precedent.586 gitconfig_global_env_contents()587 .and_then(|x| parse_excludes_file(&x))588 .or_else(|| {589 gitconfig_home_contents().and_then(|x| parse_excludes_file(&x))590 })591 .or_else(|| {592 gitconfig_xdg_contents().and_then(|x| parse_excludes_file(&x))593 })594 // System-level config has the lowest priority for core.excludesFile.595 // GIT_CONFIG_SYSTEM overrides the default /etc/gitconfig path.596 .or_else(|| {597 gitconfig_system_contents().and_then(|x| parse_excludes_file(&x))598 })599 .or_else(excludes_file_default)600}601602/// Returns the file contents of git's global config file from the path603/// specified by the `GIT_CONFIG_GLOBAL` environment variable.604fn gitconfig_global_env_contents() -> Option<Vec<u8>> {605 let path = std::env::var_os("GIT_CONFIG_GLOBAL").map(PathBuf::from)?;606 if path.as_os_str().is_empty() {607 return None;608 }579 match gitconfig_xdg_contents().and_then(|x| parse_excludes_file(&x)) {580 Some(path) => return Some(path),581 None => {}582 }583 excludes_file_default()609 let mut file = BufReader::new(File::open(path).ok()?);610 let mut contents = vec![];611 file.read_to_end(&mut contents).ok().map(|_| contents)612}613614/// Returns the file contents of git's system-level config file.615///616/// Checks `GIT_CONFIG_SYSTEM` first, then falls back to `/etc/gitconfig`.617fn gitconfig_system_contents() -> Option<Vec<u8>> {618 let path = std::env::var_os("GIT_CONFIG_SYSTEM")619 .map(PathBuf::from)620 .filter(|x| !x.as_os_str().is_empty())621 .unwrap_or_else(|| PathBuf::from("/etc/gitconfig"));622 let mut file = BufReader::new(File::open(path).ok()?);623 let mut contents = vec![];624 file.read_to_end(&mut contents).ok().map(|_| contents)625}626627/// Returns the file contents of git's global config file, if one exists, in628/// the user's home directory.629fn gitconfig_home_contents() -> Option<Vec<u8>> {589 let home = match home_dir() {590 None => return None,591 Some(home) => home,592 };593 let mut file = match File::open(home.join(".gitconfig")) {594 Err(_) => return None,595 Ok(file) => BufReader::new(file),596 };630 let home = home_dir()?;631 let mut file = BufReader::new(File::open(home.join(".gitconfig")).ok()?);632 let mut contents = vec![];633 file.read_to_end(&mut contents).ok().map(|_| contents)634}635636/// Returns the file contents of git's global config file, if one exists, in637/// the user's XDG_CONFIG_HOME directory.638fn gitconfig_xdg_contents() -> Option<Vec<u8>> {639 let path = std::env::var_os("XDG_CONFIG_HOME")605 .and_then(|x| if x.is_empty() { None } else { Some(PathBuf::from(x)) })640 .map(PathBuf::from)641 .filter(|x| !x.as_os_str().is_empty())642 .or_else(|| home_dir().map(|p| p.join(".config")))607 .map(|x| x.join("git/config"));608 let mut file = match path.and_then(|p| File::open(p).ok()) {609 None => return None,610 Some(file) => BufReader::new(file),611 };643 .map(|x| x.join("git/config"))?;644 let mut file = BufReader::new(File::open(path).ok()?);645 let mut contents = vec![];646 file.read_to_end(&mut contents).ok().map(|_| contents)647}648
1 unmodified line
650///651/// Specifically, this respects XDG_CONFIG_HOME.652fn excludes_file_default() -> Option<PathBuf> {653 std::env::var_os("XDG_CONFIG_HOME")621 .and_then(|x| if x.is_empty() { None } else { Some(PathBuf::from(x)) })654 .map(PathBuf::from)655 .filter(|x| !x.as_os_str().is_empty())656 .or_else(|| home_dir().map(|p| p.join(".config")))657 .map(|x| x.join("git/ignore"))658}659
224 unmodified lines
modified:tests/regression.rs+25 added
1715 unmodified lines
1716 .arg("-rx")1717 .stdout();1718 eqnice!("xbxbx\n", got);1719});17201721// See: https://github.com/BurntSushi/ripgrep/issues/32751722//1723// This test doesn't seem to work on Windows for whatever reason. I haven't1724// investigated it yet, but I'm pretty sure it's just the test. I'm guessing1725// it's related to path separators. ---AG1726#[cfg(not(windows))]1727rgtest!(r3275_git_global_config_env, |dir: Dir, mut cmd: TestCommand| {1728 dir.create_dir("foo");1729 dir.create(".git", "");1730 dir.create("foo/foo1", "");1731 dir.create("foo/foo2", "");1732 dir.create("global-excludes-nonstandard", "foo2");1733 dir.create(1734 "global-config-nonstandard",1735 &format!(1736 "[core]\n\texcludesFile = {path}",1737 path = dir.path().join("global-excludes-nonstandard").display()1738 ),1739 );17401741 let config_path = dir.path().join("global-config-nonstandard");1742 cmd.env("GIT_CONFIG_GLOBAL", config_path);1743 eqnice!("foo/foo1\n", cmd.arg("--files").arg("foo").stdout());1744});
modified:tests/util.rs+10 added
272 unmodified lines
273 self.cmd.args(args);274 self275 }276277 /// Set an environment variable for this command.278 pub fn env(279 &mut self,280 key: impl AsRef<OsStr>,281 val: impl AsRef<OsStr>,282 ) -> &mut TestCommand {283 self.cmd.env(key, val);284 self285 }286287 /// Set the working directory for this command.288 ///289 /// The path given is interpreted relative to the directory that this290 /// command was created for.
234 unmodified lines
modified:CHANGELOG.md+5 added
10 unmodified lines
1112* [PERF #3293](https://github.com/BurntSushi/ripgrep/issues/3293):13 Improves directory traversal time on very large corpora.1415Feature enhancements:1617* [FEATURE #3275](https://github.com/BurntSushi/ripgrep/issues/3275):18 ripgrep now respects `GIT_CONFIG_GLOBAL` and `GIT_CONFIG_SYSTEM`.1920Bug fixes:2122* [BUG #3212](https://github.com/BurntSushi/ripgrep/pull/3212):23 Don't check for the existence of `.jj` when `--no-ignore` is used.
1841 unmodified lines