Skip to content

Conversation

@MaestroLockedIn
Copy link

Issue: Add() should not add ignored files by default #1679

This PR adds support for force-adding ignored files using the --force option, mirroring standard Git’s behavior (git add -f).

Changes

Updated AddWithOptions():

Supports Force: true to bypass .gitignore patterns.

Modified doAdd() and doAddDirectory() to correctly include ignored files when forced.

Added comprehensive tests:

TestAddWithForceIgnoredFile

Minor cleanup and improved .gitignore pattern handling.

johndoe12312 and others added 4 commits October 7, 2025 22:14
This adds support for  (force add) to include ignored files
in the worktree index. Also improves AddWithOptions and related tests
to cover ignored file handling and force scenarios.
Copy link
Contributor

@onee-only onee-only left a comment

Choose a reason for hiding this comment

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

Hello @MaestroLockedIn! I'm not the maintainer of the project, but I was also interested in the issue. I've left some suggestions. Please have a look and share your thoughts.

func (w *Worktree) Add(path string) (plumbing.Hash, error) {
// TODO(mcuadros): deprecate in favor of AddWithOption in v6.
return w.doAdd(path, make([]gitignore.Pattern, 0), false)
// TODO(mcuadros): deprecate in favor of AddWithOption in v7.
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we don't have to delay this to v7. Currently v6 isn't released and API changes can be made. See #910.

Copy link
Member

Choose a reason for hiding this comment

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

I think we don't have to delay this to v7.

I'd concur with this, ideally we would tackle it as part of v6. I'm planning to raise an RFC on the subject of API consolidation which would affect this (and the general "WithOption" funcs), just haven't got around to it yet. In the mean time, we could leave the comment unchanged:

Suggested change
// TODO(mcuadros): deprecate in favor of AddWithOption in v7.
// TODO(mcuadros): deprecate in favor of AddWithOption in v6.

dirPatterns, _ := gitignore.ReadPatterns(w.Filesystem, nil)
patterns = append(patterns, dirPatterns...)
} else {
dirPatterns, _ := gitignore.ReadPatterns(w.Filesystem, []string{dir})
Copy link
Contributor

Choose a reason for hiding this comment

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

As far as I know, gitignore.ReadPatterns reads the directories recursively starting from the specified directory. So I think the case below won't work as expected:

  1. root directory contains .gitignore with content: foo.
  2. user adds foo/bar.txt

In this case, since it isn't aware of the root's .gitignore, it will add foo/bar.txt.
Could you please test the code against this case?

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.

4 participants