Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@gsquared94
Copy link
Collaborator

TLDR

This PR enhances the GitIgnoreParser to correctly process .gitignore files located in subdirectories. Previously, only the root .gitignore was considered. Now, rules defined in nested .gitignore files are properly applied relative to their location within the project structure.

Dive Deeper

The primary motivation for this change is to align the tool's behavior with standard Git functionality, where .gitignore files can be placed in any directory to specify ignore patterns for that part of the tree.

The key modifications are within packages/core/src/utils/gitIgnoreParser.ts:

  • A new findAndLoadGitignoreFiles method recursively scans the project directory for .gitignore files.

  • The logic in loadPatterns has been updated to transform patterns from these nested files. For instance, a pattern like build/ in a .gitignore located at src/component/ will be correctly interpreted to ignore src/component/build/ and not the root build/ directory.

  • The parser now correctly handles pattern precedence, ensuring that rules in nested .gitignore files are
    applied correctly alongside the root .gitignore and .git/info/exclude.

Comprehensive unit tests have been added in packages/core/src/utils/gitIgnoreParser.test.ts to validate the new functionality, including tests for nested patterns and precedence rules.

Reviewer Test Plan

  1. Setup test project:
 # Create a root directory for the test project and enter it
 mkdir test_gitignore_project && cd test_gitignore_project
 
 # Initialize a Git repository
 git init
 
 # Create a nested directory structure
 mkdir -p dir1/dir2/dir3
 mkdir -p dir4
 
 # Create various files to test against the ignore rules
 touch root_file.txt
 touch root_file.log
 touch dir1/file_in_dir1.txt
 touch dir1/file_in_dir1.log
 touch dir1/file_to_keep.tmp
 touch dir1/file_to_ignore.tmp
 touch dir1/dir2/file_in_dir2.txt
 touch dir1/dir2/dir3/file_in_dir3.txt
 touch dir4/file_in_dir4.log
 
 # Create .gitignore files at different levels
 
 # Root .gitignore: ignores all .log files
 echo "*.log" > .gitignore
 
 # Nested .gitignore in dir1: ignores .tmp files, but negates the rule for a specific file
 echo -e "*.tmp\n!file_to_keep.tmp" > dir1/.gitignore
 
 # Nested .gitignore in dir1/dir2: ignores everything inside, but un-ignores dir3
 echo -e "*\n!/dir3/" > dir1/dir2/.gitignore
 
 # Add all files to staging to see the effect of the .gitignore rules
 git add .
 
 # Check the git status to see which files are tracked and which are ignored
 echo "\n--- Git Status ---"
 git status --short
  1. Should see this output:
A  .gitignore
A  dir1/.gitignore
A  dir1/file_in_dir1.txt
A  dir1/file_to_keep.tmp
A  root_file.txt
  1. Now run this command in Gemini built from this branch:
image

Testing Matrix

🍏 🪟 🐧
npm run
npx
Docker
Podman - -
Seatbelt - -

Linked issues / bugs

Fixes #3072

@gsquared94 gsquared94 requested a review from a team as a code owner September 3, 2025 13:28
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Summary of Changes

Hello @gsquared94, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the GitIgnoreParser to accurately interpret and apply ignore rules from .gitignore files found throughout the project's directory structure, not just the root. This change ensures that the tool's behavior mirrors Git's native handling of ignore patterns, leading to more precise and expected file exclusion across complex repositories.

Highlights

  • Nested Gitignore Support: The GitIgnoreParser now correctly processes .gitignore files located in subdirectories, aligning with standard Git behavior.
  • Pattern Transformation: Patterns defined in nested .gitignore files are now properly transformed and applied relative to their location within the project structure.
  • Precedence Handling: The parser correctly handles pattern precedence, ensuring rules in nested .gitignore files are applied alongside root .gitignore and .git/info/exclude.
  • Recursive File Discovery: A new method, findAndLoadGitignoreFiles, has been introduced to recursively scan the project directory for all .gitignore files.
  • Enhanced Test Coverage: Comprehensive unit tests have been added to validate the new functionality, including tests for nested patterns and precedence rules.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gsquared94 gsquared94 enabled auto-merge September 3, 2025 13:28
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request is a significant improvement, adding support for nested .gitignore files, which brings the tool's behavior more in line with standard Git functionality. The recursive approach to finding and parsing .gitignore files, along with the logic to transform patterns relative to their location, is well-implemented. The tests are thorough and cover important cases for nested patterns and precedence rules. I have identified one high-severity issue related to a hardcoded exclusion of node_modules, which should be addressed to ensure the parser's behavior is fully consistent with Git.

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@gemini-cli gemini-cli bot added kind/enhancement priority/p1 Important and should be addressed in the near term. labels Sep 3, 2025
@gsquared94 gsquared94 added this pull request to the merge queue Sep 3, 2025
Merged via the queue into google-gemini:main with commit abddd2b Sep 3, 2025
18 checks passed
@gsquared94 gsquared94 deleted the fix-3072 branch September 3, 2025 14:33
thacio added a commit to thacio/auditaria that referenced this pull request Sep 3, 2025
acoliver referenced this pull request in vybestack/llxprt-code Sep 19, 2025
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
(cherry picked from commit abddd2b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

priority/p1 Important and should be addressed in the near term.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

only top level .gitignore file is processed

3 participants