Skip to content

Commit 5d2a5fc

Browse files
Kevin Willforddscho
authored andcommitted
Fix reset when using the sparse-checkout feature.
When using the sparse checkout feature the git reset command will add entries to the index that will have the skip-worktree bit off but will leave the working directory empty. File data is lost because the index version of the files has been changed but there is nothing that is in the working directory. This will cause the next status call to show either deleted for files modified or deleting or nothing for files added. The added files should be shown as untracked and modified files should be shown as modified. To fix this when the reset is running if there is not a file in the working directory and if it will be missing with the new index entry or was not missing in the previous version, we create the previous index version of the file in the working directory so that status will report correctly and the files will be availble for the user to deal with. Signed-off-by: Kevin Willford <kewillf@microsoft.com>
1 parent a42b69a commit 5d2a5fc

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

builtin/reset.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "submodule-config.h"
2727
#include "strbuf.h"
2828
#include "quote.h"
29+
#include "dir.h"
2930

3031
#define REFRESH_INDEX_DELAY_WARNING_IN_MS (2 * 1000)
3132

@@ -127,12 +128,45 @@ static void update_index_from_diff(struct diff_queue_struct *q,
127128
struct diff_options *opt, void *data)
128129
{
129130
int i;
131+
int pos;
130132
int intent_to_add = *(int *)data;
131133

132134
for (i = 0; i < q->nr; i++) {
133135
struct diff_filespec *one = q->queue[i]->one;
136+
struct diff_filespec *two = q->queue[i]->two;
134137
int is_missing = !(one->mode && !is_null_oid(&one->oid));
138+
int was_missing = !two->mode && is_null_oid(&two->oid);
135139
struct cache_entry *ce;
140+
struct cache_entry *ceBefore;
141+
struct checkout state = CHECKOUT_INIT;
142+
143+
/*
144+
* When using the sparse-checkout feature the cache entries that are
145+
* added here will not have the skip-worktree bit set.
146+
* Without this code there is data that is lost because the files that
147+
* would normally be in the working directory are not there and show as
148+
* deleted for the next status or in the case of added files just disappear.
149+
* We need to create the previous version of the files in the working
150+
* directory so that they will have the right content and the next
151+
* status call will show modified or untracked files correctly.
152+
*/
153+
if (core_apply_sparse_checkout && !file_exists(two->path))
154+
{
155+
pos = cache_name_pos(two->path, strlen(two->path));
156+
if ((pos >= 0 && ce_skip_worktree(active_cache[pos])) && (is_missing || !was_missing))
157+
{
158+
state.force = 1;
159+
state.refresh_cache = 1;
160+
state.istate = &the_index;
161+
ceBefore = make_cache_entry(&the_index, two->mode, &two->oid, two->path,
162+
0, 0);
163+
if (!ceBefore)
164+
die(_("make_cache_entry failed for path '%s'"),
165+
two->path);
166+
167+
checkout_entry(ceBefore, &state, NULL);
168+
}
169+
}
136170

137171
if (is_missing && !intent_to_add) {
138172
remove_file_from_cache(one->path);

t/t7114-reset-sparse-checkout.sh

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/bin/sh
2+
3+
test_description='reset when using a sparse-checkout'
4+
5+
. ./test-lib.sh
6+
7+
# reset using a sparse-checkout file
8+
9+
test_expect_success 'setup' '
10+
test_tick &&
11+
echo "checkout file" >c &&
12+
echo "modify file" >m &&
13+
echo "delete file" >d &&
14+
git add . &&
15+
git commit -m "initial commit" &&
16+
echo "added file" >a &&
17+
echo "modification of a file" >m &&
18+
git rm d &&
19+
git add . &&
20+
git commit -m "second commit" &&
21+
git checkout -b endCommit
22+
'
23+
24+
test_expect_success 'reset when there is a sparse-checkout' '
25+
echo "/c" >.git/info/sparse-checkout &&
26+
test_config core.sparsecheckout true &&
27+
git checkout -b resetBranch &&
28+
test_path_is_missing m &&
29+
test_path_is_missing a &&
30+
test_path_is_missing d &&
31+
git reset HEAD~1 &&
32+
test "checkout file" = "$(cat c)" &&
33+
test "modification of a file" = "$(cat m)" &&
34+
test "added file" = "$(cat a)" &&
35+
test_path_is_missing d
36+
'
37+
38+
test_expect_success 'reset after deleting file without skip-worktree bit' '
39+
git checkout -f endCommit &&
40+
git clean -xdf &&
41+
echo "/c
42+
/m" >.git/info/sparse-checkout &&
43+
test_config core.sparsecheckout true &&
44+
git checkout -b resetAfterDelete &&
45+
test_path_is_file m &&
46+
test_path_is_missing a &&
47+
test_path_is_missing d &&
48+
rm -f m &&
49+
git reset HEAD~1 &&
50+
test "checkout file" = "$(cat c)" &&
51+
test "added file" = "$(cat a)" &&
52+
test_path_is_missing m &&
53+
test_path_is_missing d
54+
'
55+
56+
57+
58+
test_done

0 commit comments

Comments
 (0)