Skip to content

Commit e0897fd

Browse files
committed
cli#2720 | Add patch flag to pull-request diff command
1 parent becc45a commit e0897fd

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

api/queries_pr.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,15 +267,19 @@ func (pr *PullRequest) DisplayableReviews() PullRequestReviews {
267267
return PullRequestReviews{Nodes: published, TotalCount: len(published)}
268268
}
269269

270-
func (c Client) PullRequestDiff(baseRepo ghrepo.Interface, prNumber int) (io.ReadCloser, error) {
270+
func (c Client) PullRequestDiff(baseRepo ghrepo.Interface, prNumber int, patch bool) (io.ReadCloser, error) {
271271
url := fmt.Sprintf("%srepos/%s/pulls/%d",
272272
ghinstance.RESTPrefix(baseRepo.RepoHost()), ghrepo.FullName(baseRepo), prNumber)
273273
req, err := http.NewRequest("GET", url, nil)
274274
if err != nil {
275275
return nil, err
276276
}
277277

278-
req.Header.Set("Accept", "application/vnd.github.v3.diff; charset=utf-8")
278+
if patch {
279+
req.Header.Set("Accept", "application/vnd.github.v3.patch")
280+
} else {
281+
req.Header.Set("Accept", "application/vnd.github.v3.diff; charset=utf-8")
282+
}
279283

280284
resp, err := c.http.Do(req)
281285
if err != nil {

pkg/cmd/pr/diff/diff.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type DiffOptions struct {
2525

2626
SelectorArg string
2727
UseColor string
28+
Patch bool
2829
}
2930

3031
func NewCmdDiff(f *cmdutil.Factory, runF func(*DiffOptions) error) *cobra.Command {
@@ -70,6 +71,7 @@ func NewCmdDiff(f *cmdutil.Factory, runF func(*DiffOptions) error) *cobra.Comman
7071
}
7172

7273
cmd.Flags().StringVar(&opts.UseColor, "color", "auto", "Use color in diff output: {always|never|auto}")
74+
cmd.Flags().BoolVar(&opts.Patch, "patch", false, "Display diff in patch format")
7375

7476
return cmd
7577
}
@@ -90,7 +92,7 @@ func diffRun(opts *DiffOptions) error {
9092
}
9193
apiClient := api.NewClientFromHTTP(httpClient)
9294

93-
diff, err := apiClient.PullRequestDiff(baseRepo, pr.Number)
95+
diff, err := apiClient.PullRequestDiff(baseRepo, pr.Number, opts.Patch)
9496
if err != nil {
9597
return fmt.Errorf("could not find pull request diff: %w", err)
9698
}

0 commit comments

Comments
 (0)