Pergunta

To keep all of my config files in sync I have a Github repository with all of my settings. While this works fairly well in most cases, some apps have different versions on different machines.

So now I am wondering... is there any way to set Git global config setting like this one (which causes a warning on my laptop) without causing errors on different machines with older Git versions?

One option might be wrapping the git push command to add the flags when supported but that seems like a hack.

Foi útil?

Solução

Separate configs for separate machines

Create different config files for different machines and at each machine symlink one of them as the real config. The symlink itself will not be tracked, add it in .gitignore.

The symlinking could be done in a post-checkout hook. You need to identify the current host (e.g. uname -n) and have a file that contains appropriate settings for the hook.

The downside of this solution is that is is not dynamic. Once a new version of Git is installed on a host, you have to check that the appropriate config works with it.

What is impossible

If you want something that checks whether a value is supported for an option and use another value as fallback, you’d have to parse manpages and rely on their contents. Git has nothing similar implemented. You cannot check if a value is supported, you cannot write a config file that dynamically choses the value itself.

Dynamically generated config

However, it is possible to generate the config dynamically. In a similar way to how you replace an ordinary file with a symlink to chose between several static files, you can replace it with a named pipe connected to a daemon that generates the config.

In the daemon you can check git --version and based on that choose the config options’ values. Although this is a clever solution, fully exploiting the capabilities of Unix file system, it is quite a hacky one. You have to keep the daemon running and if it stops, Git will not be able to read the config and will hang.

Outras dicas

Hidden directory with a specially made git porcelain script that you do check in to every repo you work on. ( Or a separate utility repo you could then check out with a manifest and the repo command line utility.) Your script would call push with whatever flags you need based on git version, independent of the machine you're on.

Not a comprehensive solution, but one trick I use is to put 1.7.12+ options (like push.default=simple) in ~/.config/git/config, which older gits don't read.

I set everything applicable to all git versions in ~/.gitconfig.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top