X Tutup
Skip to content

Commit 4a6aa0e

Browse files
committed
pr diff: respect global NO_COLOR/CLICOLOR_FORCE settings
In the absence of an explicit `--color` setting, or when `--color=auto` is passed, the pr diff command should fall back to respecting the global colorization setting as inferred from the environment.
1 parent c987c57 commit 4a6aa0e

File tree

2 files changed

+136
-138
lines changed

2 files changed

+136
-138
lines changed

pkg/cmd/pr/diff/diff.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type DiffOptions struct {
2626
Finder shared.PRFinder
2727

2828
SelectorArg string
29-
UseColor string
29+
UseColor bool
3030
Patch bool
3131
}
3232

@@ -36,6 +36,8 @@ func NewCmdDiff(f *cmdutil.Factory, runF func(*DiffOptions) error) *cobra.Comman
3636
HttpClient: f.HttpClient,
3737
}
3838

39+
var colorFlag string
40+
3941
cmd := &cobra.Command{
4042
Use: "diff [<number> | <url> | <branch>]",
4143
Short: "View changes in a pull request",
@@ -50,19 +52,22 @@ func NewCmdDiff(f *cmdutil.Factory, runF func(*DiffOptions) error) *cobra.Comman
5052
opts.Finder = shared.NewFinder(f)
5153

5254
if repoOverride, _ := cmd.Flags().GetString("repo"); repoOverride != "" && len(args) == 0 {
53-
return cmdutil.FlagErrorf("argument required when using the --repo flag")
55+
return cmdutil.FlagErrorf("argument required when using the `--repo` flag")
5456
}
5557

5658
if len(args) > 0 {
5759
opts.SelectorArg = args[0]
5860
}
5961

60-
if !validColorFlag(opts.UseColor) {
61-
return cmdutil.FlagErrorf("did not understand color: %q. Expected one of always, never, or auto", opts.UseColor)
62-
}
63-
64-
if opts.UseColor == "auto" && !opts.IO.IsStdoutTTY() {
65-
opts.UseColor = "never"
62+
switch colorFlag {
63+
case "always":
64+
opts.UseColor = true
65+
case "auto":
66+
opts.UseColor = opts.IO.ColorEnabled()
67+
case "never":
68+
opts.UseColor = false
69+
default:
70+
return cmdutil.FlagErrorf("the value for `--color` must be one of \"auto\", \"always\", or \"never\"")
6671
}
6772

6873
if runF != nil {
@@ -72,7 +77,7 @@ func NewCmdDiff(f *cmdutil.Factory, runF func(*DiffOptions) error) *cobra.Comman
7277
},
7378
}
7479

75-
cmd.Flags().StringVar(&opts.UseColor, "color", "auto", "Use color in diff output: {always|never|auto}")
80+
cmd.Flags().StringVar(&colorFlag, "color", "auto", "Use color in diff output: {always|never|auto}")
7681
cmd.Flags().BoolVar(&opts.Patch, "patch", false, "Display diff in patch format")
7782

7883
return cmd
@@ -105,7 +110,7 @@ func diffRun(opts *DiffOptions) error {
105110
}
106111
defer opts.IO.StopPager()
107112

108-
if opts.UseColor == "never" {
113+
if !opts.UseColor {
109114
_, err = io.Copy(opts.IO.Out, diff)
110115
if errors.Is(err, syscall.EPIPE) {
111116
return nil
@@ -183,7 +188,3 @@ func isAdditionLine(dl string) bool {
183188
func isRemovalLine(dl string) bool {
184189
return strings.HasPrefix(dl, "-")
185190
}
186-
187-
func validColorFlag(c string) bool {
188-
return c == "auto" || c == "always" || c == "never"
189-
}

0 commit comments

Comments
 (0)
X Tutup