From 0b8805b2f00b006afb98c7d15f6f67f1b7194054 Mon Sep 17 00:00:00 2001 From: Yoichi Nakayama Date: Mon, 29 Jun 2020 23:51:09 +0900 Subject: [PATCH] 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 --- getter.go | 4 ++-- getter_test.go | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/getter.go b/getter.go index d5e77de..c61ffa3 100644 --- a/getter.go +++ b/getter.go @@ -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++ { diff --git a/getter_test.go b/getter_test.go index 0edd0f6..6ba5a0f 100644 --- a/getter_test.go +++ b/getter_test.go @@ -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 {