X Tutup
Skip to content

Commit 4bbbf46

Browse files
authored
Fix browse last commit when using the repo override flag (cli#4845)
1 parent 2d0b946 commit 4bbbf46

File tree

3 files changed

+68
-3
lines changed

3 files changed

+68
-3
lines changed

api/queries_repo.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,26 @@ func ForkRepo(client *Client, repo ghrepo.Interface, org string) (*Repository, e
524524
}, nil
525525
}
526526

527+
func LastCommit(client *Client, repo ghrepo.Interface) (*Commit, error) {
528+
var responseData struct {
529+
Repository struct {
530+
DefaultBranchRef struct {
531+
Target struct {
532+
Commit `graphql:"... on Commit"`
533+
}
534+
}
535+
} `graphql:"repository(owner: $owner, name: $repo)"`
536+
}
537+
variables := map[string]interface{}{
538+
"owner": githubv4.String(repo.RepoOwner()), "repo": githubv4.String(repo.RepoName()),
539+
}
540+
gql := graphQLClient(client.http, repo.RepoHost())
541+
if err := gql.QueryNamed(context.Background(), "LastCommit", &responseData, variables); err != nil {
542+
return nil, err
543+
}
544+
return &responseData.Repository.DefaultBranchRef.Target.Commit, nil
545+
}
546+
527547
// RepoFindForks finds forks of the repo that are affiliated with the viewer
528548
func RepoFindForks(client *Client, repo ghrepo.Interface, limit int) ([]*Repository, error) {
529549
result := struct {

pkg/cmd/browse/browse.go

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type BrowseOptions struct {
2929
HttpClient func() (*http.Client, error)
3030
IO *iostreams.IOStreams
3131
PathFromRepoRoot func() string
32+
GitClient gitClient
3233

3334
SelectorArg string
3435

@@ -46,6 +47,7 @@ func NewCmdBrowse(f *cmdutil.Factory, runF func(*BrowseOptions) error) *cobra.Co
4647
HttpClient: f.HttpClient,
4748
IO: f.IOStreams,
4849
PathFromRepoRoot: git.PathFromRepoRoot,
50+
GitClient: &localGitClient{},
4951
}
5052

5153
cmd := &cobra.Command{
@@ -97,6 +99,9 @@ func NewCmdBrowse(f *cmdutil.Factory, runF func(*BrowseOptions) error) *cobra.Co
9799
); err != nil {
98100
return err
99101
}
102+
if cmd.Flags().Changed("repo") {
103+
opts.GitClient = &remoteGitClient{opts.BaseRepo, opts.HttpClient}
104+
}
100105

101106
if runF != nil {
102107
return runF(opts)
@@ -123,10 +128,11 @@ func runBrowse(opts *BrowseOptions) error {
123128
}
124129

125130
if opts.CommitFlag {
126-
commit, err := git.LastCommit()
127-
if err == nil {
128-
opts.Branch = commit.Sha
131+
commit, err := opts.GitClient.LastCommit()
132+
if err != nil {
133+
return err
129134
}
135+
opts.Branch = commit.Sha
130136
}
131137

132138
section, err := parseSection(baseRepo, opts)
@@ -245,3 +251,33 @@ func isNumber(arg string) bool {
245251
_, err := strconv.Atoi(arg)
246252
return err == nil
247253
}
254+
255+
// gitClient is used to implement functions that can be performed on both local and remote git repositories
256+
type gitClient interface {
257+
LastCommit() (*git.Commit, error)
258+
}
259+
260+
type localGitClient struct{}
261+
262+
type remoteGitClient struct {
263+
repo func() (ghrepo.Interface, error)
264+
httpClient func() (*http.Client, error)
265+
}
266+
267+
func (gc *localGitClient) LastCommit() (*git.Commit, error) { return git.LastCommit() }
268+
269+
func (gc *remoteGitClient) LastCommit() (*git.Commit, error) {
270+
httpClient, err := gc.httpClient()
271+
if err != nil {
272+
return nil, err
273+
}
274+
repo, err := gc.repo()
275+
if err != nil {
276+
return nil, err
277+
}
278+
commit, err := api.LastCommit(api.NewClientFromHTTP(httpClient), repo)
279+
if err != nil {
280+
return nil, err
281+
}
282+
return &git.Commit{Sha: commit.OID}, nil
283+
}

pkg/cmd/browse/browse_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ func TestNewCmdBrowse(t *testing.T) {
143143
assert.Equal(t, tt.wants.WikiFlag, opts.WikiFlag)
144144
assert.Equal(t, tt.wants.NoBrowserFlag, opts.NoBrowserFlag)
145145
assert.Equal(t, tt.wants.SettingsFlag, opts.SettingsFlag)
146+
assert.Equal(t, tt.wants.CommitFlag, opts.CommitFlag)
146147
})
147148
}
148149
}
@@ -156,6 +157,12 @@ func setGitDir(t *testing.T, dir string) {
156157
})
157158
}
158159

160+
type testGitClient struct{}
161+
162+
func (gc *testGitClient) LastCommit() (*git.Commit, error) {
163+
return &git.Commit{Sha: "6f1a2405cace1633d89a79c74c65f22fe78f9659"}, nil
164+
}
165+
159166
func Test_runBrowse(t *testing.T) {
160167
s := string(os.PathSeparator)
161168
setGitDir(t, "../../../git/fixtures/simple.git")
@@ -354,6 +361,7 @@ func Test_runBrowse(t *testing.T) {
354361
name: "open last commit",
355362
opts: BrowseOptions{
356363
CommitFlag: true,
364+
GitClient: &testGitClient{},
357365
},
358366
baseRepo: ghrepo.New("vilmibm", "gh-user-status"),
359367
wantsErr: false,
@@ -364,6 +372,7 @@ func Test_runBrowse(t *testing.T) {
364372
opts: BrowseOptions{
365373
CommitFlag: true,
366374
SelectorArg: "main.go",
375+
GitClient: &testGitClient{},
367376
},
368377
baseRepo: ghrepo.New("vilmibm", "gh-user-status"),
369378
wantsErr: false,

0 commit comments

Comments
 (0)
X Tutup