MCPcopy Create free account
hub / github.com/cli/cli / NewCmdDiff

Function NewCmdDiff

pkg/cmd/pr/diff/diff.go:46–125  ·  view source on GitHub ↗
(f *cmdutil.Factory, runF func(*DiffOptions) error)

Source from the content-addressed store, hash-verified

44}
45
46func NewCmdDiff(f *cmdutil.Factory, runF func(*DiffOptions) error) *cobra.Command {
47 opts := &DiffOptions{
48 IO: f.IOStreams,
49 HttpClient: f.HttpClient,
50 Browser: f.Browser,
51 }
52
53 var colorFlag string
54
55 cmd := &cobra.Command{
56 Use: "diff [<number> | <url> | <branch>]",
57 Short: "View changes in a pull request",
58 Long: heredoc.Docf(`
59 View changes in a pull request.
60
61 Without an argument, the pull request that belongs to the current branch
62 is selected.
63
64 With %[1]s--web%[1]s flag, open the pull request diff in a web browser instead.
65
66 Use %[1]s--exclude%[1]s to filter out files matching a glob pattern. The pattern
67 uses forward slashes as path separators on all platforms. You can repeat
68 the flag to exclude multiple patterns.
69
70 By default, terminal escape sequences in the diff are neutralized, since
71 they could manipulate your terminal. Pass %[1]s--allow-escape-sequences%[1]s to
72 print the diff verbatim, for example when piping a patch to another program.
73 `, "`"),
74 Example: heredoc.Doc(`
75 # See diff for current branch
76 $ gh pr diff
77
78 # See diff for a specific PR
79 $ gh pr diff 123
80
81 # Exclude files from diff output
82 $ gh pr diff --exclude '*.yml' --exclude 'generated/*'
83
84 # Exclude matching files by name
85 $ gh pr diff --name-only --exclude '*.generated.*'
86 `),
87 Args: cobra.MaximumNArgs(1),
88 RunE: func(cmd *cobra.Command, args []string) error {
89 opts.Finder = shared.NewFinder(f)
90
91 if repoOverride, _ := cmd.Flags().GetString("repo"); repoOverride != "" && len(args) == 0 {
92 return cmdutil.FlagErrorf("argument required when using the `--repo` flag")
93 }
94
95 if len(args) > 0 {
96 opts.SelectorArg = args[0]
97 }
98
99 switch colorFlag {
100 case "always":
101 opts.UseColor = true
102 case "auto":
103 opts.UseColor = opts.IO.ColorEnabled()

Callers 1

Test_NewCmdDiffFunction · 0.85

Calls 6

NewFinderFunction · 0.92
FlagErrorfFunction · 0.92
StringEnumFlagFunction · 0.92
diffRunFunction · 0.85
ColorEnabledMethod · 0.80
ErrorfMethod · 0.65

Tested by 1

Test_NewCmdDiffFunction · 0.68