Skip to content

feat(pr diff): add --exclude flag to filter files from diff output#12655

Open
yuvrajangadsingh wants to merge 4 commits intocli:trunkfrom
yuvrajangadsingh:feature/pr-diff-exclude
Open

feat(pr diff): add --exclude flag to filter files from diff output#12655
yuvrajangadsingh wants to merge 4 commits intocli:trunkfrom
yuvrajangadsingh:feature/pr-diff-exclude

Conversation

@yuvrajangadsingh
Copy link
Contributor

Adds a --exclude (-e) flag to gh pr diff that lets you filter out files matching glob patterns from the diff output.

Example usage:

# Exclude all YAML files
gh pr diff --exclude '*.yml'

# Exclude multiple patterns
gh pr diff --exclude '*.yml' --exclude 'vendor/*'

# Works with --name-only too
gh pr diff --name-only --exclude '*.generated.*'

Patterns are matched against both the full file path and the basename, so --exclude '*.yml' will match dir/file.yml.

Uses Go's filepath.Match for glob support — no new dependencies.

Closes #8739

@yuvrajangadsingh yuvrajangadsingh marked this pull request as ready for review February 17, 2026 07:12
@yuvrajangadsingh yuvrajangadsingh requested a review from a team as a code owner February 17, 2026 07:12
@cliAutomation cliAutomation added the external pull request originating outside of the CLI core team label Feb 17, 2026
@yuvrajangadsingh yuvrajangadsingh force-pushed the feature/pr-diff-exclude branch 2 times, most recently from 186d5d5 to bc15e7d Compare February 20, 2026 08:13
@yuvrajangadsingh
Copy link
Contributor Author

@BagToad @babakks hey, just wanted to check if this is something the team would be interested in — adds an --exclude flag to gh pr diff so you can filter out noisy files (like lockfiles, snapshots, generated code). pretty common ask based on the issues i've seen. let me know if there's anything to adjust!

@yuvrajangadsingh yuvrajangadsingh force-pushed the feature/pr-diff-exclude branch 2 times, most recently from aa530d3 to e147705 Compare February 24, 2026 07:09
@yuvrajangadsingh
Copy link
Contributor Author

gentle bump on this one — would love to get some eyes on it when someone has a chance. happy to make changes if anything needs adjusting.

@yuvrajangadsingh yuvrajangadsingh force-pushed the feature/pr-diff-exclude branch from e147705 to be8446c Compare March 1, 2026 10:05
Copy link
Member

@BagToad BagToad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice implementation — the exclude logic is clean and well-tested.

One change needed: the diff header regex is duplicated. changedFilesNames (line ~310) already has an inline regex that's nearly identical to your new diffHeaderRegexp:

// existing (changedFilesNames):
pattern := regexp.MustCompile(`(?:^|\n)diff\s--git.*\s(["']?)b/(.*)`)

// new (extractFileName):
var diffHeaderRegexp = regexp.MustCompile(`diff\s--git.*\s(["']?)b/(.*)`)

Could you share the compiled regex between both and ideally refactor changedFilesNames to use your splitDiffSections/extractFileName helpers? That would reduce duplication and make the code easier to maintain.

If this is too painful, let me know — it's kind of a nit so I'm happy to tackle it in a follow-up if needed instead.

@BagToad
Copy link
Member

BagToad commented Mar 4, 2026

Also note, we need to fix the linter checks.

@yuvrajangadsingh yuvrajangadsingh force-pushed the feature/pr-diff-exclude branch from be8446c to fc3b9ef Compare March 4, 2026 21:49
@yuvrajangadsingh
Copy link
Contributor Author

@BagToad fixed both — shared the compiled diffHeaderRegexp between changedFilesNames and extractFileName (removed the inline duplicate), and fixed the gofmt spacing. rebased on trunk too.

@yuvrajangadsingh yuvrajangadsingh requested a review from BagToad March 4, 2026 21:53
@yuvrajangadsingh yuvrajangadsingh force-pushed the feature/pr-diff-exclude branch from fc3b9ef to 40ac081 Compare March 5, 2026 15:30
Copy link
Member

@babakks babakks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, @yuvrajangadsingh! 🙏

Just commented some thoughts, but the main one is the last (about using path instead of filepath).

Comment on lines 288 to +306
// This is kind of a gnarly regex. We're looking lines of the format:
// diff --git a/9114-triage b/9114-triage
// diff --git "a/hello-\360\237\230\200-world" "b/hello-\360\237\230\200-world"
//
// From these lines we would look to extract:
// 9114-triage
// "hello-\360\237\230\200-world"
//
// Note that the b/ is removed but in the second case the preceeding quote remains.
// This is important for how git handles filenames that would be quoted with core.quotePath.
// https://git-scm.com/docs/git-config#Documentation/git-config.txt-corequotePath
//
// Thus we capture the quote if it exists, and everything that follows the b/
// We then concatenate those two capture groups together which for the examples above would be:
// `` + 9114-triage
// `"`` + hello-\360\237\230\200-world"
//
// Where I'm using the `` to indicate a string to avoid confusion with the " character.
pattern := regexp.MustCompile(`(?:^|\n)diff\s--git.*\s(["]?)b/(.*)`)
matches := pattern.FindAllStringSubmatch(string(diff), -1)
matches := diffHeaderRegexp.FindAllStringSubmatch(string(diff), -1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought: we should really think about using a reliable third-party for this stuff. 🤷

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯 agree. Felt this pain with gh agent as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed, would be nice to have. out of scope for this PR though

Copy link
Member

@BagToad BagToad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please re-request review when @babakks comments addressed 🙏 thank you

@yuvrajangadsingh
Copy link
Contributor Author

@babakks @BagToad addressed all feedback in 43c845e:

  • added usage examples to --help
  • switched filepath.Match to path.Match for OS-consistent behavior
  • renamed patterns to excludePatterns
  • simplified splitDiffSections with strings.Split

ready for another look when you get a chance.

@yuvrajangadsingh yuvrajangadsingh requested a review from BagToad March 6, 2026 08:11
@yuvrajangadsingh yuvrajangadsingh force-pushed the feature/pr-diff-exclude branch from 16fd9ad to f2ac428 Compare March 6, 2026 08:13
Add a new --exclude (-e) flag to gh pr diff that allows users to exclude
files matching glob patterns from the diff output. This is useful for
filtering out auto-generated files, vendor directories, or other noise
when reviewing pull requests.

Supports standard glob patterns and can be specified multiple times.
Patterns match against both the full path and basename.

Closes cli#8739
- add usage examples to --help output
- use path.Match instead of filepath.Match for OS-consistent behavior
- rename patterns to excludePatterns for clarity
- simplify splitDiffSections using strings.Split
@yuvrajangadsingh yuvrajangadsingh force-pushed the feature/pr-diff-exclude branch from f2ac428 to a331ef7 Compare March 6, 2026 17:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external pull request originating outside of the CLI core team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add File Exclusion Option to gh pr diff Command

4 participants