Skip to content

gptel-context: Allow exclusion of gitignored files #665

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 37 commits into
base: master
Choose a base branch
from

Conversation

benthamite
Copy link
Contributor

Continuing the discussion here, this PR allows the user to exclude, by setting the user option gptel-context-exclude-gitignored to t, gitignored files or directories from the context.

Tagging participants in previous discussion: @hartikainen, @collinarnett, @metachip, @sandinmyjoints.

@benthamite
Copy link
Contributor Author

(I just noticed that I forgot to set the default value of gptel-context-exclude-gitignored to nil, but now I wonder if we should leave it as t. I don’t have a clear view on this.)

@collinarnett
Copy link

I would set it to t by default. I tested out the new add directory functionality on a repository with node_modules folder and it froze my emacs adding thousands of files to the context window. Other utilities like RepoMix enable gitignore exclusion by default as well.

Copy link
Owner

@karthink karthink left a comment

Choose a reason for hiding this comment

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

Looks good. I've got one performance concern and one behavior suggestion.

@karthink
Copy link
Owner

Could you move all the git-related functions gptel-context--git-files, gptel-context--skip-file-p, gptel-context--message-git-skipped and gptel-context--build-git-cache into one place in the file? You can add a ;;; git tracking functions header over them for navigation.

Additionally, anywhere you run a file-name function like file-relative-name on a file, please check that the file argument can't be nil. I haven't analyzed all possible cases.

@karthink
Copy link
Owner

locate-dominating-file can be very slow on remote files, to the point of locking up Emacs for a while.

It might be worth using file-remote-p and skipping the gitignore check if it's a remote file.

…ames

* Calling files not matching patterns in .gitignore ‘git-tracked’ was
confusing because, in git terminology, ‘tracked’ has a technical
meaning. So we now use ‘git-unignored’ instead, which more clearly
conveys the idea of files not git-ignored.
I thought calling this function ‘gptel-context--is-git-ignored-p’
would make its purpose clear, but I now realize this is a misnomer:
the value returned depends also on the user option
gptel-context-exclude-git-ignored, so the function may return nil even
when the file is git-ignored.
@benthamite
Copy link
Contributor Author

@karthink, I believe I have now addressed all your requested changes. Let me know if you have further comments.

@vermiculus
Copy link

Late to the party here (and I wouldn't want to derail this feature; I was recently bitten by it), but was the use of project.el (via project-files) considered now that it's included in emacs? That would get you cross-VCS support for free.

@benthamite
Copy link
Contributor Author

Hi @vermiculus. I didn’t consider using project.el because I am not familiar with that package. Let me take a look and I’ll see if I can adapt it.

@benthamite
Copy link
Contributor Author

@vermiculus: I revised the PR to make it use project.el. The code is simpler now.

It would be great if folks could test this before it is merged: I’m not a programmer and my eyes may fail to catch problems.

Copy link

@vermiculus vermiculus left a comment

Choose a reason for hiding this comment

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

Took a pass here, though I'm just on my phone so sorry for formatting.

How does this perform functionally for you? Any noticeable delay with larger/smaller projects with more/fewer ignored build artifacts?

@pabl0
Copy link
Contributor

pabl0 commented Mar 27, 2025

I see some whitespace changes that apparently just change spaces to TABs, most likely since emacs has indent-tabs-mode on by default, and especially if you have aggressive-indent-mode on, it can cause unecessary changes in places you did not even meant to touch. (This is why I submitted #712.) In case you remove those changes, consider doing some squashing as well, this PR has currently 34 commits and I think some of them could be squashed together to make the git history cleaner.

@benthamite benthamite force-pushed the exclude-gitignored branch from a6faa40 to b83fbcd Compare April 7, 2025 14:12
@benthamite benthamite force-pushed the exclude-gitignored branch from b83fbcd to c197a74 Compare April 7, 2025 14:16
@benthamite
Copy link
Contributor Author

I think I'm basically done with the changes here. My only concern is that (member file gptel-context--project-files) may be a bit slow for projects with lots of files. I believe this issue arose after we started using project.el, and happens because (member file gptel-context--project-files) has to linearly search through the entire list of project files to check if a file is present. Claude suggests using a hash table, but I don’t know enough Elisp to implement this suggestion.

If and when @karthink agrees to merge this PR, I'm happy to do some squashing to clean up the commit history.

@benthamite
Copy link
Contributor Author

benthamite commented Apr 7, 2025

(If the performance issue is not addressed, I am now inclined to set the default value of gptel-context-restrict-to-project-files to nil.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants