[WIP] Force add a file that is in an ignored subdirectory #290
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
A common setup I use for my Git repositories is to ignore entire directories which contain many files that I do not want to commit, e.g. subdirectories with data or figure files. Ignoring these directories prevents me from accidentally committing them and makes the output of
git status
less cluttered. However, I often want to commit a few files from these ignored subdirectories, e.g. a small data file with summary statistics or the final version of a figure I want to share. This is possible with Git, e.g.Problem
I am unable to force add a file in an ignored subdirectory with
git2r::add()
. Using the example above,git2r::add("data/summary.txt", force = TRUE)
does not force add the file, nor does it throw a warning or error. It behaves the same as ifforce = FALSE
.What's currently in this PR
I haven't been able to figure out how to implement this myself, but to assist in developing a solution, I've added unit tests that currently fail but should pass if the behavior is implemented.
Researching potential solutions
PR #148 was the original request for force adding files, and commit 7ccb202 implemented the functionality. However, the focus was on ignored files, and not ignored subdirectories.
I have been trying to determine if this is a fundamental limitation of libgit2. The libgit2 docs for git_index_add_all discuss force adding specific files with the
GIT_INDEX_ADD_FORCE
flag, but do not discuss this specific use case. Looking at the Python API for libgit2, pygit2, it appears that they do not support force adding at all (source code, search results of online documentation), so that's not informative. The Lua API, luagit2, also appears to not handle ignored files, instead recommending that this be implemented manually: "This forces the file to be added to the index, not looking at gitignore rules. Those rules can be evaluated through the git_status APIs (in status.h) before calling this."This issue is potentially related: libgit2/libgit2#3535 (comment) At the very least it demonstrates an example where Git and libgit2 differ in their interpretation of the
.gitingore
file.Would it be possible to implement this functionality in git2r either by modifying the call to libgit2 or instead adding a custom check via a call to
git2r::status()
?