Skip to content

Commit f5a1d54

Browse files
authored
Merge pull request #120 from Masterminds/fix-102
Trim windows line endings in svn handling
2 parents 645e870 + 053463c commit f5a1d54

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

svn.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -378,11 +378,14 @@ func detectRemoteFromInfoCommand(infoOut string) (string, error) {
378378
if urlIndex == -1 {
379379
return "", fmt.Errorf("Remote not specified in svn info")
380380
}
381-
urlEndIndex := strings.Index(string(sBytes[urlIndex:]), "\n")
381+
urlEndIndex := strings.Index(string(sBytes[urlIndex:]), "\r\n")
382382
if urlEndIndex == -1 {
383-
urlEndIndex = strings.Index(string(sBytes[urlIndex:]), "\r")
383+
urlEndIndex = strings.Index(string(sBytes[urlIndex:]), "\n")
384384
if urlEndIndex == -1 {
385-
return "", fmt.Errorf("unable to parse remote URL for svn info")
385+
urlEndIndex = strings.Index(string(sBytes[urlIndex:]), "\r")
386+
if urlEndIndex == -1 {
387+
return "", fmt.Errorf("unable to parse remote URL for svn info")
388+
}
386389
}
387390
}
388391

svn_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,3 +300,58 @@ func TestSvnInit(t *testing.T) {
300300
t.Errorf("Svn Init returns wrong version: %s", v)
301301
}
302302
}
303+
304+
func TestDetectRemoteFromInfoCommand(t *testing.T) {
305+
tests := []struct {
306+
name string
307+
input string
308+
expected string
309+
hasError bool
310+
}{
311+
{
312+
name: "Unix line ending (LF)",
313+
input: "Path: /svn/repo\nURL: https://example.com/repo\nRepository Root: https://example.com\n",
314+
expected: "https://example.com/repo",
315+
hasError: false,
316+
},
317+
{
318+
name: "Windows line ending (CRLF)",
319+
input: "Path: /svn/repo\r\nURL: https://example.com/repo\r\nRepository Root: https://example.com\r\n",
320+
expected: "https://example.com/repo",
321+
hasError: false,
322+
},
323+
{
324+
name: "Old Mac line ending (CR)",
325+
input: "Path: /svn/repo\rURL: https://example.com/repo\rRepository Root: https://example.com\r",
326+
expected: "https://example.com/repo",
327+
hasError: false,
328+
},
329+
{
330+
name: "URL not found",
331+
input: "Path: /svn/repo\nRepository Root: https://example.com\n",
332+
expected: "",
333+
hasError: true,
334+
},
335+
{
336+
name: "URL with no line ending",
337+
input: "Path: /svn/repo\nURL: https://example.com/repo",
338+
expected: "",
339+
hasError: true,
340+
},
341+
}
342+
343+
for _, tt := range tests {
344+
t.Run(tt.name, func(t *testing.T) {
345+
result, err := detectRemoteFromInfoCommand(tt.input)
346+
if tt.hasError && err == nil {
347+
t.Errorf("expected error but got none")
348+
}
349+
if !tt.hasError && err != nil {
350+
t.Errorf("unexpected error: %v", err)
351+
}
352+
if result != tt.expected {
353+
t.Errorf("expected %q, got %q", tt.expected, result)
354+
}
355+
})
356+
}
357+
}

0 commit comments

Comments
 (0)