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

Function NewCmdDiff

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

Source from the content-addressed store, hash-verified

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