Remove trailing slash before stripping .git from the end

Fixed a problem that ghq get -u may clone to another directory
1. ghq get https://git.kernel.org/pub/scm/git/git.git/
   -> GHQ_ROOT/git.kernel.org/pub/scm/git/git.git
2. ghq list | ghq get -u
   -> GHQ_ROOT/git.kernel.org/pub/scm/git/git
This commit is contained in:
Yoichi Nakayama 2020-06-29 23:51:09 +09:00
parent f0bdf0c4bd
commit 0b8805b2f0
2 changed files with 12 additions and 2 deletions

View file

@ -122,8 +122,8 @@ func (g *getter) getRemoteRepository(remote RemoteRepository) error {
}
func detectLocalRepoRoot(remotePath, repoPath string) string {
remotePath = strings.TrimSuffix(remotePath, ".git")
repoPath = strings.TrimSuffix(repoPath, ".git")
remotePath = strings.TrimSuffix(strings.TrimSuffix(remotePath, "/"), ".git")
repoPath = strings.TrimSuffix(strings.TrimSuffix(repoPath, "/"), ".git")
pathParts := strings.Split(repoPath, "/")
pathParts = pathParts[1:]
for i := 0; i < len(pathParts); i++ {

View file

@ -45,6 +45,16 @@ func TestDetectLocalRepoRoot(t *testing.T) {
remotePath: "/path/to/repo.git",
repoPath: "/path/to/repo.git",
expect: "/path/to/repo",
}, {
name: "trailing slash",
remotePath: "/path/to/repo/",
repoPath: "/path/to/repo/",
expect: "/path/to/repo",
}, {
name: ".git/ at the end",
remotePath: "/path/to/repo.git/",
repoPath: "/path/to/repo.git/",
expect: "/path/to/repo",
}}
for _, tc := range testCases {