Search code examples
gitgogithubgit-configgo-get

Git config with condifional does not work with url rewrites


I have two different work folders in my computer one is in ~/Documents/work/companyA other is in ~/work/companyB. I wrote conditional gitconfig that is like

[includeIf "gitdir:~/Documents/work/companyA/"]
    path = ~/Documents/work/companyA/.gitconfig

[includeIf "gitdir:~/Documents/work/companyB/"]
    path = ~/Documents/work/companyB/.gitconfig

and the content of both of these files are like

[user]
        name = companyAuser
        email = [email protected]

[url "ssh://git@github-companyA/"]
        insteadOf = https://github.com/

I know this file is being read and git@github-companyA is valid because when I run

git config user.email
ssh -T git@github-companyA

in a directory inside ~/Documents/work/companyA/ both of these commands work correctly.

i am using this config to "go get" from a private repository. Also when i put the URL rewrite command to ~/.gitconfig it works correct. How can i fix this


Solution

  • I misread your question: it is not a git question, it is actually a "go get + git" question.

    The locations used by go get are located under the GOMODCACHE directory, not under your working directory.

    Here is an illustration using strace on a go get github.com/spf13/cast command (I simulated a private repo by setting GOPRIVATE=github.com/spf13/cast) :

    ...
    [pid 711376] chdir("/home/LeGEC/pkg/mod/cache/vcs/60c669232b830322cfc3d9e570d86c2297d36fdbc38de8ad322b59a3dff47a24") = 0
    [pid 711376] execve("/usr/bin/git", ["git", "init", "--bare"], 0xc00046a000 /* 94 vars */) = 0
    ...
    [pid 711377] execve("/usr/bin/git", ["git", "remote", "add", "origin", "--", "https://github.com/spf13/cast"], 0xc00046a300 /* 94 vars */) = 0
    ...
    

    Possible workarounds:

    • if your can target more specific urls (for example: github.com/companyA/ instead of all of github.com/), you can set replacement urls globally, e.g:
    # in your ~/.gitconfig:
    [url "ssh://git@github-companyA/companyA/"]
      insteadOf = https://github.com/companyA/
    
    • use one of the environment variables to specify a gitconfig file, for the duration of one of your go get commands; for example:
    GIT_CONFIG_GLOBAL=~/Documents/work/companyA/.gitconfig  go get ...
    
    • set GOMODCACHE to a location under ~/Documents/work/companyA/ when you work on a repo for companyA
      note: this would lead to having several copies of your go cache on your disk