Skip to content

Commit dd0edc8

Browse files
Omit current directory when browse --branch is used with no file argument (cli#4676)
1 parent 3a4d947 commit dd0edc8

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

pkg/cmd/browse/browse.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,10 @@ func parseSection(baseRepo ghrepo.Interface, opts *BrowseOptions) (string, error
192192
}
193193

194194
func parseFile(opts BrowseOptions, f string) (p string, start int, end int, err error) {
195+
if f == "" {
196+
return
197+
}
198+
195199
parts := strings.SplitN(f, ":", 3)
196200
if len(parts) > 2 {
197201
err = fmt.Errorf("invalid file argument: %q", f)

pkg/cmd/browse/browse_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,35 @@ func Test_runBrowse(t *testing.T) {
228228
baseRepo: ghrepo.New("bchadwic", "LedZeppelinIV"),
229229
expectedURL: "https://github.com/bchadwic/LedZeppelinIV/tree/trunk/main.go",
230230
},
231+
{
232+
name: "branch flag within dir",
233+
opts: BrowseOptions{
234+
Branch: "feature-123",
235+
PathFromRepoRoot: func() string { return "pkg/dir" },
236+
},
237+
baseRepo: ghrepo.New("bstnc", "yeepers"),
238+
expectedURL: "https://github.com/bstnc/yeepers/tree/feature-123/",
239+
},
240+
{
241+
name: "branch flag within dir with .",
242+
opts: BrowseOptions{
243+
Branch: "feature-123",
244+
SelectorArg: ".",
245+
PathFromRepoRoot: func() string { return "pkg/dir" },
246+
},
247+
baseRepo: ghrepo.New("bstnc", "yeepers"),
248+
expectedURL: "https://github.com/bstnc/yeepers/tree/feature-123/pkg/dir",
249+
},
250+
{
251+
name: "branch flag within dir with dir",
252+
opts: BrowseOptions{
253+
Branch: "feature-123",
254+
SelectorArg: "inner/more",
255+
PathFromRepoRoot: func() string { return "pkg/dir" },
256+
},
257+
baseRepo: ghrepo.New("bstnc", "yeepers"),
258+
expectedURL: "https://github.com/bstnc/yeepers/tree/feature-123/pkg/dir/inner/more",
259+
},
231260
{
232261
name: "file with line number",
233262
opts: BrowseOptions{
@@ -466,6 +495,11 @@ func Test_parsePathFromFileArg(t *testing.T) {
466495
fileArg: filepath.Join("..", "..", "..") + s + "",
467496
expectedPath: "",
468497
},
498+
{
499+
name: "empty fileArg",
500+
fileArg: "",
501+
expectedPath: "",
502+
},
469503
}
470504
for _, tt := range tests {
471505
path, _, _, _ := parseFile(BrowseOptions{

0 commit comments

Comments
 (0)