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