Skip to content

Commit 3d216ef

Browse files
Fix remote URL parsing for paths with spaces.
Parse `git remote -v` output without truncating whitespace-containing URLs and add a regression test to lock in behavior for local remotes with spaced paths. Made-with: Cursor
1 parent 0273a41 commit 3d216ef

2 files changed

Lines changed: 38 additions & 5 deletions

File tree

fixtures.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,27 @@ func GetRemotes(t *testing.T, repoPath string) map[string]string {
200200
}
201201

202202
for _, line := range strings.Split(lines, "\n") {
203+
line = strings.TrimSpace(line)
203204
if line == "" {
204205
continue
205206
}
206-
parts := strings.Fields(line)
207-
if len(parts) >= 2 {
208-
name := parts[0]
209-
url := parts[1]
210-
remotes[name] = url
207+
208+
name, remainder, ok := strings.Cut(line, "\t")
209+
if !ok {
210+
// Fallback for unusual formatting that does not use tabs.
211+
idx := strings.IndexAny(line, " \t")
212+
if idx == -1 {
213+
continue
214+
}
215+
name = line[:idx]
216+
remainder = strings.TrimSpace(line[idx+1:])
217+
}
218+
219+
remainder = strings.TrimSuffix(remainder, " (fetch)")
220+
remainder = strings.TrimSuffix(remainder, " (push)")
221+
222+
if name != "" && remainder != "" {
223+
remotes[name] = remainder
211224
}
212225
}
213226

fixtures_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,26 @@ func TestCreateTestRepo_WithRemotes(t *testing.T) {
7979
}
8080
}
8181

82+
func TestCreateTestRepo_WithRemotePathContainingSpaces(t *testing.T) {
83+
remotePath := testutil.CreateBareRemote(t, "origin with space")
84+
85+
repoPath := testutil.CreateTestRepo(t, testutil.RepoOptions{
86+
Name: "remote-space-repo",
87+
Remotes: map[string]string{
88+
"origin": remotePath,
89+
},
90+
})
91+
92+
remotes := testutil.GetRemotes(t, repoPath)
93+
originURL, exists := remotes["origin"]
94+
if !exists {
95+
t.Fatal("Expected 'origin' remote to be configured")
96+
}
97+
if originURL != remotePath {
98+
t.Fatalf("Expected origin URL %q, got %q", remotePath, originURL)
99+
}
100+
}
101+
82102
func TestCreateBareRemote(t *testing.T) {
83103
remotePath := testutil.CreateBareRemote(t, "test-remote")
84104

0 commit comments

Comments
 (0)