Skip to content

Conversation

@tt
Copy link
Contributor

@tt tt commented Jan 18, 2026

On my local machine, I set diff.noPrefix to true in my global Git configuration.

This can interfere with software that uses git2. For instance, running cargo test in this repository causes the diff::tests::format_email_simple test to fail as it's expecting lines with the "a/" and "b/" prefixes:

git2-rs/src/diff.rs

Lines 1694 to 1695 in 62e83e2

"--- a/file1.txt\n",
"+++ b/file1.txt\n",

There's a couple of workarounds available:

  1. I can prevent the global configuration from loading by using git2::opts::set_search_path and pointing it at a non-existent path. The downside is that it is itself a global option.

  2. I can (probably) use git2::DiffOptions to undo my local environment. This is a bit verbose but a great solution in many cases to ensure predictability

I'm proposing exposing the git_repository_set_config function to allow setting configuration for a particular repository instance. This gives the user full control of which configuration to load.

@rustbot rustbot added the S-waiting-on-review Status: Waiting on review label Jan 18, 2026
///
/// This configuration file will be used for all configuration queries involving this
/// repository.
pub fn set_config(&self, config: &Config) -> Result<(), Error> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is useful for complete config isolation, but for your specific (the diff.noPrefix issue), shouldn't repo.config()?.set_bool("diff.noPrefix", false) help with that? That would override the global setting without removing all other inherited config.

Before merging, could you clarify why that didn't work? If it is a separate bug we might want to fix it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works but it's very specific to running the software on my machine.

Rather than relying on Git's defaults, I have to unset everything that may be different in a particular environment like diff.context, diff.mnemonicPrefix etc.

So, yes, it works but I would prefer the ability to not load any configuration.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that makes sense. I was just wondering if you missed those API or there were bugs. Thanks for the clarification!

@weihanglo weihanglo added this pull request to the merge queue Jan 18, 2026
Merged via the queue into rust-lang:master with commit 03ec581 Jan 18, 2026
7 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Waiting on review label Jan 18, 2026
@tt tt deleted the expose-git-repository-set-config-function branch January 18, 2026 19:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants