{"thread":{"id":"53636","subject":"Trailing slash conflicts with url.<base>.insteadOf","startedAt":"2020-06-08T13:26:22Z","lastAt":"2020-06-08T13:26:22Z","messageCount":1,"participants":["David Randall {Nice Guy IT}"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"399292","messageId":"CAKCYOsMTRm3Zb733OFWDEmBh+V_z1oU+KY568=b9U=tZqXaPfQ@mail.gmail.com","threadId":"53636","inReplyTo":null,"subject":"Trailing slash conflicts with url.<base>.insteadOf","fromName":"David Randall {Nice Guy IT}","fromEmail":"david@niceguyit.biz","sentAt":"2020-06-08T13:26:07Z","receivedAt":"2020-06-08T13:26:22Z","isPatch":false,"sender":{"key":"david@niceguyit.biz","avatar":null},"body":"Hey everyone,\n\nI ran into an edge case with the url.<base>.insteadOf option and\ntrailing slashes.\n\nI can clone a git repo with a trailing slash.\n    $ git clone https://github.com/NiceGuyIT/git-test-parent/\n    Cloning into 'git-test-parent'...\n    remote: Enumerating objects: 4, done.\n    remote: Counting objects: 100% (4/4), done.\n    remote: Compressing objects: 100% (3/3), done.\n    remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0\n    Receiving objects: 100% (4/4), done.\n\nCloning with SSH and a trailing slash fails. This is expected.\n    $ git clone ssh://git@github.com/NiceGuyIT/git-test-parent/\n    Cloning into 'git-test-parent'...\n    ERROR: Repository not found.\n    fatal: Could not read from remote repository.\n\n    Please make sure you have the correct access rights\n    and the repository exists.\n\nInside a repo, I can add a submodule with a trailing slash using HTTPS.\n    $ cd git-test-parent\n    $ git submodule add https://github.com/NiceGuyIT/git-test-child/\n    Cloning into\n'/home/user/Projects/git-test/git-test-parent/git-test-child'...\n    remote: Enumerating objects: 4, done.\n    remote: Counting objects: 100% (4/4), done.\n    remote: Compressing objects: 100% (3/3), done.\n    remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0\n    Receiving objects: 100% (4/4), done.\n    $ cat .gitmodules\n    [submodule \"git-test-child\"]\n            path = git-test-child\n            url = https://github.com/NiceGuyIT/git-test-child/\n\nAdding a submodule with a trailing slash using SSH faills. Again, this\nis expected.\n    $ cd git-test-parent\n    $ git submodule add ssh://git@github.com/NiceGuyIT/git-test-child.git/\n    Cloning into\n'/home/user/Projects/git-test/git-test-parent/git-test-child'...\n    ERROR: Repository not found.\n    fatal: Could not read from remote repository.\n\n    Please make sure you have the correct access rights\n    and the repository exists.\n    fatal: clone of\n'ssh://git@github.com/NiceGuyIT/git-test-child.git/' into submodule\npath '/home/user/Projects/git-test/git-test-parent/git-test-child'\nfailed\n\nThe edge case comes from Golang's FAQ[1] why it uses HTTPS to clone a\nrepo. Specifically, how to\nauthenticate against private repos when using `go get`. I'm using the\n2nd option which uses git's\n`url.<base>.insteadOf` configuration to replace HTTPS URLs with SSH\nurls. I added this to my\n`~/.gitconfig`.\n    [url \"ssh://git@github.com/\"]\n        insteadOf = https://github.com/\n\nWith this option configured, adding a submodule via HTTPS with a\ntrailing slash fails. The fact this\nfails is not that surprising.\n    $ git submodule add https://github.com/NiceGuyIT/git-test-child/\n    Cloning into\n'/home/user/Projects/git-test/git-test-parent/git-test-child'...\n    ERROR: Repository not found.\n    fatal: Could not read from remote repository.\n\n    Please make sure you have the correct access rights\n    and the repository exists.\n    fatal: clone of 'https://github.com/NiceGuyIT/git-test-child/'\ninto submodule path\n'/home/user/Projects/git-test/git-test-parent/git-test-child' failed\n\nThe problem comes from working with other git repos where they do not\nhave this option configured\nand add submodules with a trailing slash. As shown above, the trailing\nslash is included in\n`.gitmodules`. `git clone --recursive` will fail if submodules were\nadded with trailing slashes and\nyou have `insteadOf` configured to replace HTTPS with SSH. I ran into\nthis problem[2] when trying to\n`git submodule update --init --recursive` and having it fail on some\nrepos but not all repos.\n\nI'm using git version 2.26.2 on openSUSE Leap 15.1. I don't have the\ncapacity to test a newer\nversion or compile the latest version. I've added a submodule with a\ntrailing slash to\nthe above project. To reproduce this, add the `insteadOf` option\nmentioned above to your\n`~/.gitconfig` and perform a recursive clone. The parent repo will be\ncloned but not the submodules.\n    git clone --recursive https://github.com/NiceGuyIT/git-test-parent\n\nIs it possible to remove trailing slashes on HTTPS URLs before doing\nthe URL rewrite?\n\nThank you for providing great software!\n\nDavid\n\n\n[1]: https://golang.org/doc/faq#git_https\n[2]: https://github.com/purpleidea/mgmt/issues/602#issuecomment-640063867\n"}]}