diff --git a/pkg/cmd/browse/browse.go b/pkg/cmd/browse/browse.go index 514477efc..6677e5d45 100644 --- a/pkg/cmd/browse/browse.go +++ b/pkg/cmd/browse/browse.go @@ -169,12 +169,28 @@ func parseFileArg(fileArg string) (string, error) { if len(arr) > 2 { return "", fmt.Errorf("invalid use of colon\nUse 'gh browse --help' for more information about browse\n") } + if len(arr) > 1 { - if !isNumber(arr[1]) { - return "", fmt.Errorf("invalid line number after colon\nUse 'gh browse --help' for more information about browse\n") + out := arr[0] + "#L" + lineRange := strings.Split(arr[1], "-") + + if len(lineRange) > 0 { + if !isNumber(lineRange[0]) { + return "", fmt.Errorf("invalid line number after colon\nUse 'gh browse --help' for more information about browse\n") + } + out += lineRange[0] } - return arr[0] + "#L" + arr[1], nil + + if len(lineRange) > 1 { + if !isNumber(lineRange[1]) { + return "", fmt.Errorf("invalid line range after colon\nUse 'gh browse --help' for more information about browse\n") + } + out += "-L" + lineRange[1] + } + + return out, nil } + return arr[0], nil } diff --git a/pkg/cmd/browse/browse_test.go b/pkg/cmd/browse/browse_test.go index d30a95024..e89863b89 100644 --- a/pkg/cmd/browse/browse_test.go +++ b/pkg/cmd/browse/browse_test.go @@ -215,6 +215,15 @@ func Test_runBrowse(t *testing.T) { defaultBranch: "trunk", expectedURL: "https://github.com/ravocean/angur/tree/trunk/path/to/file.txt#L32", }, + { + name: "file with line range", + opts: BrowseOptions{ + SelectorArg: "path/to/file.txt:32-40", + }, + baseRepo: ghrepo.New("ravocean", "angur"), + defaultBranch: "trunk", + expectedURL: "https://github.com/ravocean/angur/tree/trunk/path/to/file.txt#L32-L40", + }, { name: "file with invalid line number", opts: BrowseOptions{ @@ -223,6 +232,14 @@ func Test_runBrowse(t *testing.T) { baseRepo: ghrepo.New("ttran112", "ttrain211"), wantsErr: true, }, + { + name: "file with invalid line range", + opts: BrowseOptions{ + SelectorArg: "path/to/file.txt:32-abc", + }, + baseRepo: ghrepo.New("ttran112", "ttrain211"), + wantsErr: true, + }, { name: "branch with issue number", opts: BrowseOptions{