Skip to content

Commit

Permalink
feat(4248): add support for line range w/ browse
Browse files Browse the repository at this point in the history
Allow using a range of lines when browsing files. For example:

`gh browse test.go:10-20`

Closes cli#4248
  • Loading branch information
despreston committed Aug 31, 2021
1 parent 4fa984a commit dafd0bf
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
22 changes: 19 additions & 3 deletions pkg/cmd/browse/browse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}

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 arr[0] + "#L" + arr[1], nil

return out, nil
}

return arr[0], nil
}

Expand Down
17 changes: 17 additions & 0 deletions pkg/cmd/browse/browse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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{
Expand Down

0 comments on commit dafd0bf

Please sign in to comment.