Trailing slash conflicts with url.<base>.insteadOf
From: David Randall {Nice Guy IT} <hidden>
Date: 2020-06-08 13:26:22
Hey everyone,
I ran into an edge case with the url.<base>.insteadOf option and
trailing slashes.
I can clone a git repo with a trailing slash.
$ git clone https://github.com/NiceGuyIT/git-test-parent/
Cloning into 'git-test-parent'...
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (4/4), done.
Cloning with SSH and a trailing slash fails. This is expected.
$ git clone ssh://git@github.com/NiceGuyIT/git-test-parent/
Cloning into 'git-test-parent'...
ERROR: Repository not found.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Inside a repo, I can add a submodule with a trailing slash using HTTPS.
$ cd git-test-parent
$ git submodule add https://github.com/NiceGuyIT/git-test-child/
Cloning into
'/home/user/Projects/git-test/git-test-parent/git-test-child'...
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (4/4), done.
$ cat .gitmodules
[submodule "git-test-child"]
path = git-test-child
url = https://github.com/NiceGuyIT/git-test-child/
Adding a submodule with a trailing slash using SSH faills. Again, this
is expected.
$ cd git-test-parent
$ git submodule add ssh://git@github.com/NiceGuyIT/git-test-child.git/
Cloning into
'/home/user/Projects/git-test/git-test-parent/git-test-child'...
ERROR: Repository not found.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
fatal: clone of
'ssh://git@github.com/NiceGuyIT/git-test-child.git/' into submodule
path '/home/user/Projects/git-test/git-test-parent/git-test-child'
failed
The edge case comes from Golang's FAQ[1] why it uses HTTPS to clone a
repo. Specifically, how to
authenticate against private repos when using `go get`. I'm using the
2nd option which uses git's
`url.<base>.insteadOf` configuration to replace HTTPS URLs with SSH
urls. I added this to my
`~/.gitconfig`.
[url "ssh://git@github.com/"]
insteadOf = https://github.com/
With this option configured, adding a submodule via HTTPS with a
trailing slash fails. The fact this
fails is not that surprising.
$ git submodule add https://github.com/NiceGuyIT/git-test-child/
Cloning into
'/home/user/Projects/git-test/git-test-parent/git-test-child'...
ERROR: Repository not found.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
fatal: clone of 'https://github.com/NiceGuyIT/git-test-child/'
into submodule path
'/home/user/Projects/git-test/git-test-parent/git-test-child' failed
The problem comes from working with other git repos where they do not
have this option configured
and add submodules with a trailing slash. As shown above, the trailing
slash is included in
`.gitmodules`. `git clone --recursive` will fail if submodules were
added with trailing slashes and
you have `insteadOf` configured to replace HTTPS with SSH. I ran into
this problem[2] when trying to
`git submodule update --init --recursive` and having it fail on some
repos but not all repos.
I'm using git version 2.26.2 on openSUSE Leap 15.1. I don't have the
capacity to test a newer
version or compile the latest version. I've added a submodule with a
trailing slash to
the above project. To reproduce this, add the `insteadOf` option
mentioned above to your
`~/.gitconfig` and perform a recursive clone. The parent repo will be
cloned but not the submodules.
git clone --recursive https://github.com/NiceGuyIT/git-test-parent
Is it possible to remove trailing slashes on HTTPS URLs before doing
the URL rewrite?
Thank you for providing great software!
David
[1]: https://golang.org/doc/faq#git_https
[2]: https://github.com/purpleidea/mgmt/issues/602#issuecomment-640063867