diff --git a/pkg/cmd/browse/browse.go b/pkg/cmd/browse/browse.go index 8bd64940d..c3b592fc5 100644 --- a/pkg/cmd/browse/browse.go +++ b/pkg/cmd/browse/browse.go @@ -192,6 +192,10 @@ func parseSection(baseRepo ghrepo.Interface, opts *BrowseOptions) (string, error } func parseFile(opts BrowseOptions, f string) (p string, start int, end int, err error) { + if f == "" { + return + } + parts := strings.SplitN(f, ":", 3) if len(parts) > 2 { err = fmt.Errorf("invalid file argument: %q", f) diff --git a/pkg/cmd/browse/browse_test.go b/pkg/cmd/browse/browse_test.go index 489ad4e09..087feed49 100644 --- a/pkg/cmd/browse/browse_test.go +++ b/pkg/cmd/browse/browse_test.go @@ -228,6 +228,35 @@ func Test_runBrowse(t *testing.T) { baseRepo: ghrepo.New("bchadwic", "LedZeppelinIV"), expectedURL: "https://github.com/bchadwic/LedZeppelinIV/tree/trunk/main.go", }, + { + name: "branch flag within dir", + opts: BrowseOptions{ + Branch: "feature-123", + PathFromRepoRoot: func() string { return "pkg/dir" }, + }, + baseRepo: ghrepo.New("bstnc", "yeepers"), + expectedURL: "https://github.com/bstnc/yeepers/tree/feature-123/", + }, + { + name: "branch flag within dir with .", + opts: BrowseOptions{ + Branch: "feature-123", + SelectorArg: ".", + PathFromRepoRoot: func() string { return "pkg/dir" }, + }, + baseRepo: ghrepo.New("bstnc", "yeepers"), + expectedURL: "https://github.com/bstnc/yeepers/tree/feature-123/pkg/dir", + }, + { + name: "branch flag within dir with dir", + opts: BrowseOptions{ + Branch: "feature-123", + SelectorArg: "inner/more", + PathFromRepoRoot: func() string { return "pkg/dir" }, + }, + baseRepo: ghrepo.New("bstnc", "yeepers"), + expectedURL: "https://github.com/bstnc/yeepers/tree/feature-123/pkg/dir/inner/more", + }, { name: "file with line number", opts: BrowseOptions{ @@ -466,6 +495,11 @@ func Test_parsePathFromFileArg(t *testing.T) { fileArg: filepath.Join("..", "..", "..") + s + "", expectedPath: "", }, + { + name: "empty fileArg", + fileArg: "", + expectedPath: "", + }, } for _, tt := range tests { path, _, _, _ := parseFile(BrowseOptions{