3

I have my dotfiles setup and one thing I do have is a .gitconfig. The problem is on my OSX machines on want the helper to be osxkeychain, but for my linux machines I want cache --timeout 3600. Is there a way to do OS detection in the gitconfig file? Maybe something like:

[credential os=apple]
    helper = osxkeychain
[credential os=linux]
    helper = cache --timeout 3600
jrock2004
  • 3,229
  • 5
  • 40
  • 73

2 Answers2

2

There isn't a way to do OS configuration in Git configuration. However, there are two possible approaches.

One is to to take advantage of the fact that there are two per-user config files, ~/.gitconfig and ~/.config/git/config (although the latter may differ if you have $XDG_CONFIG_HOME set). I store my common configurations in the former, and any custom, per-machine configuration (which is not managed by Git) in the latter, such as credential helpers and signing subkeys. It is possible to have a script in your dotfiles that copies an appropriate file into the latter if you do manage it with Git.

The other option is to specify the credential helper as a shell command, which of course has all the power of the shell. So you could do something like this:

[credential]
    helper = "!f() { if [ \"$(uname -s)\" = Linux ]; then git credential-cache --timeout 3600 \"$@\"; else git credential-osxkeychain \"$@\"; fi; };f"

That will invoke the proper helper depending on the OS you're running on.

bk2204
  • 64,793
  • 6
  • 84
  • 100
1

You can define a conditional include in your global .gitconfig file, since Git 2.13.

But it only applies for now on the path of your repository.

So if you can set your repository in two different path on your two different machines, you could associate a global setting which would include the relevant helper, based on those paths.

; include for all repositories inside /path/to/group/osx
[includeIf "gitdir:/path/to/group/osx"]
    helper = osxkeychain
; include for all repositories inside /path/to/group/linux
[includeIf "gitdir:/path/to/group/linux"]
    helper = cache --timeout 3600

The alternative would be to set that setting per branch (since Git 2.23), but that does not map well with the concept of OS (branch "linux", branch "OSx": it does not makes much sense)

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250