Skip to content

Commit 71471b2

Browse files
vdyegitster
authored andcommitted
reset: preserve skip-worktree bit in mixed reset
Change `update_index_from_diff` to set `skip-worktree` when applicable for new index entries. When `git reset --mixed <tree-ish>` is run, entries in the index with differences between the pre-reset HEAD and reset <tree-ish> are identified and handled with `update_index_from_diff`. For each file, a new cache entry in inserted into the index, created from the <tree-ish> side of the reset (without changing the working tree). However, the newly-created entry must have `skip-worktree` explicitly set in either of the following scenarios: 1. the file is in the current index and has `skip-worktree` set 2. the file is not in the current index but is outside of a defined sparse checkout definition Not setting the `skip-worktree` bit leads to likely-undesirable results for a user. It causes `skip-worktree` settings to disappear on the "diff"-containing files (but *only* the diff-containing files), leading to those files now showing modifications in `git status`. For example, when running `git reset --mixed` in a sparse checkout, some file entries outside of sparse checkout could show up as deleted, despite the user never deleting anything (and not wanting them on-disk anyway). Additionally, add a test to `t7102` to ensure `skip-worktree` is preserved in a basic `git reset --mixed` scenario and update a failure-documenting test from 19a0acc (t1092: test interesting sparse-checkout scenarios, 2021-01-23) with new expected behavior. Helped-by: Junio C Hamano <[email protected]> Signed-off-by: Victoria Dye <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1f86b7c commit 71471b2

File tree

3 files changed

+39
-14
lines changed

3 files changed

+39
-14
lines changed

builtin/reset.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "cache-tree.h"
2626
#include "submodule.h"
2727
#include "submodule-config.h"
28+
#include "dir.h"
2829

2930
#define REFRESH_INDEX_DELAY_WARNING_IN_MS (2 * 1000)
3031

@@ -130,6 +131,7 @@ static void update_index_from_diff(struct diff_queue_struct *q,
130131
int intent_to_add = *(int *)data;
131132

132133
for (i = 0; i < q->nr; i++) {
134+
int pos;
133135
struct diff_filespec *one = q->queue[i]->one;
134136
int is_in_reset_tree = one->mode && !is_null_oid(&one->oid);
135137
struct cache_entry *ce;
@@ -141,6 +143,18 @@ static void update_index_from_diff(struct diff_queue_struct *q,
141143

142144
ce = make_cache_entry(&the_index, one->mode, &one->oid, one->path,
143145
0, 0);
146+
147+
/*
148+
* If the file 1) corresponds to an existing index entry with
149+
* skip-worktree set, or 2) does not exist in the index but is
150+
* outside the sparse checkout definition, add a skip-worktree bit
151+
* to the new index entry.
152+
*/
153+
pos = cache_name_pos(one->path, strlen(one->path));
154+
if ((pos >= 0 && ce_skip_worktree(active_cache[pos])) ||
155+
(pos < 0 && !path_in_sparse_checkout(one->path, &the_index)))
156+
ce->ce_flags |= CE_SKIP_WORKTREE;
157+
144158
if (!ce)
145159
die(_("make_cache_entry failed for path '%s'"),
146160
one->path);

t/t1092-sparse-checkout-compatibility.sh

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -459,26 +459,20 @@ test_expect_failure 'blame with pathspec outside sparse definition' '
459459
test_all_match git blame deep/deeper2/deepest/a
460460
'
461461

462-
# NEEDSWORK: a sparse-checkout behaves differently from a full checkout
463-
# in this scenario, but it shouldn't.
464-
test_expect_failure 'checkout and reset (mixed)' '
462+
test_expect_success 'checkout and reset (mixed)' '
465463
init_repos &&
466464
467465
test_all_match git checkout -b reset-test update-deep &&
468466
test_all_match git reset deepest &&
469-
test_all_match git reset update-folder1 &&
470-
test_all_match git reset update-folder2
471-
'
472-
473-
# NEEDSWORK: a sparse-checkout behaves differently from a full checkout
474-
# in this scenario, but it shouldn't.
475-
test_expect_success 'checkout and reset (mixed) [sparse]' '
476-
init_repos &&
477467
478-
test_sparse_match git checkout -b reset-test update-deep &&
479-
test_sparse_match git reset deepest &&
468+
# Because skip-worktree is preserved, resetting to update-folder1
469+
# will show worktree changes for folder1/a in full-checkout, but not
470+
# in sparse-checkout or sparse-index.
471+
git -C full-checkout reset update-folder1 >full-checkout-out &&
480472
test_sparse_match git reset update-folder1 &&
481-
test_sparse_match git reset update-folder2
473+
grep "M folder1/a" full-checkout-out &&
474+
! grep "M folder1/a" sparse-checkout-out &&
475+
run_on_sparse test_path_is_missing folder1
482476
'
483477

484478
test_expect_success 'merge, cherry-pick, and rebase' '

t/t7102-reset.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,23 @@ test_expect_success '--mixed refreshes the index' '
472472
test_cmp expect output
473473
'
474474

475+
test_expect_success '--mixed preserves skip-worktree' '
476+
echo 123 >>file2 &&
477+
git add file2 &&
478+
git update-index --skip-worktree file2 &&
479+
git reset --mixed HEAD >output &&
480+
test_must_be_empty output &&
481+
482+
cat >expect <<-\EOF &&
483+
Unstaged changes after reset:
484+
M file2
485+
EOF
486+
git update-index --no-skip-worktree file2 &&
487+
git add file2 &&
488+
git reset --mixed HEAD >output &&
489+
test_cmp expect output
490+
'
491+
475492
test_expect_success 'resetting specific path that is unmerged' '
476493
git rm --cached file2 &&
477494
F1=$(git rev-parse HEAD:file1) &&

0 commit comments

Comments
 (0)