diff --git a/svn.go b/svn.go index 077c914..eb1a15f 100644 --- a/svn.go +++ b/svn.go @@ -378,11 +378,14 @@ func detectRemoteFromInfoCommand(infoOut string) (string, error) { if urlIndex == -1 { return "", fmt.Errorf("Remote not specified in svn info") } - urlEndIndex := strings.Index(string(sBytes[urlIndex:]), "\n") + urlEndIndex := strings.Index(string(sBytes[urlIndex:]), "\r\n") if urlEndIndex == -1 { - urlEndIndex = strings.Index(string(sBytes[urlIndex:]), "\r") + urlEndIndex = strings.Index(string(sBytes[urlIndex:]), "\n") if urlEndIndex == -1 { - return "", fmt.Errorf("unable to parse remote URL for svn info") + urlEndIndex = strings.Index(string(sBytes[urlIndex:]), "\r") + if urlEndIndex == -1 { + return "", fmt.Errorf("unable to parse remote URL for svn info") + } } } diff --git a/svn_test.go b/svn_test.go index ab32417..8dcf510 100644 --- a/svn_test.go +++ b/svn_test.go @@ -300,3 +300,58 @@ func TestSvnInit(t *testing.T) { t.Errorf("Svn Init returns wrong version: %s", v) } } + +func TestDetectRemoteFromInfoCommand(t *testing.T) { + tests := []struct { + name string + input string + expected string + hasError bool + }{ + { + name: "Unix line ending (LF)", + input: "Path: /svn/repo\nURL: https://example.com/repo\nRepository Root: https://example.com\n", + expected: "https://example.com/repo", + hasError: false, + }, + { + name: "Windows line ending (CRLF)", + input: "Path: /svn/repo\r\nURL: https://example.com/repo\r\nRepository Root: https://example.com\r\n", + expected: "https://example.com/repo", + hasError: false, + }, + { + name: "Old Mac line ending (CR)", + input: "Path: /svn/repo\rURL: https://example.com/repo\rRepository Root: https://example.com\r", + expected: "https://example.com/repo", + hasError: false, + }, + { + name: "URL not found", + input: "Path: /svn/repo\nRepository Root: https://example.com\n", + expected: "", + hasError: true, + }, + { + name: "URL with no line ending", + input: "Path: /svn/repo\nURL: https://example.com/repo", + expected: "", + hasError: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result, err := detectRemoteFromInfoCommand(tt.input) + if tt.hasError && err == nil { + t.Errorf("expected error but got none") + } + if !tt.hasError && err != nil { + t.Errorf("unexpected error: %v", err) + } + if result != tt.expected { + t.Errorf("expected %q, got %q", tt.expected, result) + } + }) + } +}