Search code examples
gitgit-config

Script to set multiple insteadOf cases in gitconfig


I need a script that will ensure the following content on the .gitconfig file:

[url "git@<IP addr>:"]
    insteadOf = https://<IP addr>/
    insteadOf = https://<domain name>/
    insteadOf = git@<domain name>:

This is meant to ensure that the user will always clone with SSH and use the IP address instead of the HTTP address (both in IP or domain form) and instead of ssh using the domain name.

However, using the following code:

git config --global url.git@<IP addr>:.insteadOf https://<IP addr>/ --replace-all
git config --global url.git@<IP addr>:.insteadOf https://<domain name>/ --add
git config --global url.git@<IP addr>:.insteadOf git@<domain name>: --add

This works fine, but once the script is run again, it reprints the lines in the .gitconfig file, which over time will make the file too large and git speed too slow. I've tried all combinations of flags --replace-all --add and so on. They either stack up the file or issue a warning which I also cannot tolerate due to project requirements.

Is there any syntax to add the three lines at once and do nothing if they're already there?


Solution

  • It looks to me like a bug in the options parsing for intermixed-options-and-args style,

    git config --global --replace-all url.git@<IP addr>:.insteadOf https://<IP addr>/
    git config --global --add         url.git@<IP addr>:.insteadOf https://<domain name>/
    git config --global --add         url.git@<IP addr>:.insteadOf git@<domain name>:
    

    works.