Skip to content

Commit

Permalink
add/rm: allow adding sparse entries when virtual
Browse files Browse the repository at this point in the history
Upstream, a20f704 (add: warn when asked to update SKIP_WORKTREE entries,
2021-04-08) modified how 'git add <pathspec>' works with cache entries
marked with the SKIP_WORKTREE bit. The intention is to prevent a user
from accidentally adding a path that is outside their sparse-checkout
definition but somehow matches an existing index entry.

A similar change for 'git rm' happened in d5f4b82 (rm: honor sparse
checkout patterns, 2021-04-08).

This breaks when using the virtual filesystem in VFS for Git. It is
rare, but we could be in a scenario where the user has staged a change
and then the file is projected away. If the user re-adds the file, then
this warning causes the command to fail with the advise message.

Disable this logic when core_virtualfilesystem is enabled.

Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
  • Loading branch information
derrickstolee authored and dscho committed Oct 8, 2024
1 parent 58aeae2 commit a30259c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
22 changes: 17 additions & 5 deletions builtin/add.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#define USE_THE_REPOSITORY_VARIABLE

/*
* "git add" builtin command
*
* Copyright (C) 2006 Linus Torvalds
*/
#include "builtin.h"
#include "environment.h"
#include "advice.h"
#include "config.h"
#include "lockfile.h"
Expand Down Expand Up @@ -46,6 +49,7 @@ static int chmod_pathspec(struct repository *repo,
int err;

if (!include_sparse &&
!core_virtualfilesystem &&
(ce_skip_worktree(ce) ||
!path_in_sparse_checkout(ce->name, repo->index)))
continue;
Expand Down Expand Up @@ -131,8 +135,9 @@ static int refresh(struct repository *repo, int verbose, const struct pathspec *
if (!seen[i]) {
const char *path = pathspec->items[i].original;

if (matches_skip_worktree(pathspec, i, &skip_worktree_seen) ||
!path_in_sparse_checkout(path, repo->index)) {
if (!core_virtualfilesystem &&
(matches_skip_worktree(pathspec, i, &skip_worktree_seen) ||
!path_in_sparse_checkout(path, repo->index))) {
string_list_append(&only_match_skip_worktree,
pathspec->items[i].original);
} else {
Expand All @@ -142,7 +147,11 @@ static int refresh(struct repository *repo, int verbose, const struct pathspec *
}
}

if (only_match_skip_worktree.nr) {
/*
* When using a virtual filesystem, we might re-add a path
* that is currently virtual and we want that to succeed.
*/
if (!core_virtualfilesystem && only_match_skip_worktree.nr) {
advise_on_updating_sparse_paths(&only_match_skip_worktree);
ret = 1;
}
Expand Down Expand Up @@ -527,7 +536,11 @@ int cmd_add(int argc,
if (seen[i])
continue;

if (!include_sparse &&
/*
* When using a virtual filesystem, we might re-add a path
* that is currently virtual and we want that to succeed.
*/
if (!include_sparse && !core_virtualfilesystem &&
matches_skip_worktree(&pathspec, i, &skip_worktree_seen)) {
string_list_append(&only_match_skip_worktree,
pathspec.items[i].original);
Expand All @@ -551,7 +564,6 @@ int cmd_add(int argc,
}
}


if (only_match_skip_worktree.nr) {
advise_on_updating_sparse_paths(&only_match_skip_worktree);
exit_status = 1;
Expand Down
9 changes: 7 additions & 2 deletions builtin/rm.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
#define USE_THE_REPOSITORY_VARIABLE
#include "builtin.h"
#include "environment.h"
#include "advice.h"
#include "config.h"
#include "lockfile.h"
Expand Down Expand Up @@ -314,7 +315,7 @@ int cmd_rm(int argc,
for (i = 0; i < the_repository->index->cache_nr; i++) {
const struct cache_entry *ce = the_repository->index->cache[i];

if (!include_sparse &&
if (!include_sparse && !core_virtualfilesystem &&
(ce_skip_worktree(ce) ||
!path_in_sparse_checkout(ce->name, the_repository->index)))
continue;
Expand Down Expand Up @@ -351,7 +352,11 @@ int cmd_rm(int argc,
*original ? original : ".");
}

if (only_match_skip_worktree.nr) {
/*
* When using a virtual filesystem, we might re-add a path
* that is currently virtual and we want that to succeed.
*/
if (!core_virtualfilesystem && only_match_skip_worktree.nr) {
advise_on_updating_sparse_paths(&only_match_skip_worktree);
ret = 1;
}
Expand Down
2 changes: 1 addition & 1 deletion read-cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -3968,7 +3968,7 @@ static void update_callback(struct diff_queue_struct *q,
struct diff_filepair *p = q->queue[i];
const char *path = p->one->path;

if (!data->include_sparse &&
if (!data->include_sparse && !core_virtualfilesystem &&
!path_in_sparse_checkout(path, data->index))
continue;

Expand Down

0 comments on commit a30259c

Please sign in to comment.