Skip to content

Commit 4a1c14f

Browse files
derrickstoleedscho
authored andcommitted
Merge pull request git-for-windows#494: reset: fix mixed reset when using virtual filesystem
This replaces git-for-windows#493 (can't reopen a PR after a force-push...). I updated this commit with a more firm version of the fix. This hopefully answers Victoria's excellent concerns with the previous approach. I did not manage to get an automated test for this, but I did carefully verify this manually with a few commits in a VFS for Git enlistment (with different files every time). I updated the commit message with more details about why this works. --- This fork contains changes specific to monorepo scenarios. If you are an external contributor, then please detail your reason for submitting to this fork: * [X] This change only applies to the virtualization hook and VFS for Git. Resolves git-for-windows#490.
2 parents b3b0d1c + 48de480 commit 4a1c14f

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

builtin/reset.c

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#include "dir.h"
2929
#include "strbuf.h"
3030
#include "quote.h"
31+
#include "dir.h"
32+
#include "entry.h"
3133

3234
#define REFRESH_INDEX_DELAY_WARNING_IN_MS (2 * 1000)
3335

@@ -141,9 +143,47 @@ static void update_index_from_diff(struct diff_queue_struct *q,
141143

142144
for (i = 0; i < q->nr; i++) {
143145
int pos;
146+
int respect_skip_worktree = 1;
144147
struct diff_filespec *one = q->queue[i]->one;
148+
struct diff_filespec *two = q->queue[i]->two;
145149
int is_in_reset_tree = one->mode && !is_null_oid(&one->oid);
150+
int is_missing = !(one->mode && !is_null_oid(&one->oid));
151+
int was_missing = !two->mode && is_null_oid(&two->oid);
146152
struct cache_entry *ce;
153+
struct cache_entry *ceBefore;
154+
struct checkout state = CHECKOUT_INIT;
155+
156+
/*
157+
* When using the virtual filesystem feature, the cache entries that are
158+
* added here will not have the skip-worktree bit set.
159+
*
160+
* Without this code there is data that is lost because the files that
161+
* would normally be in the working directory are not there and show as
162+
* deleted for the next status or in the case of added files just disappear.
163+
* We need to create the previous version of the files in the working
164+
* directory so that they will have the right content and the next
165+
* status call will show modified or untracked files correctly.
166+
*/
167+
if (core_virtualfilesystem && !file_exists(two->path))
168+
{
169+
pos = cache_name_pos(two->path, strlen(two->path));
170+
if ((pos >= 0 && ce_skip_worktree(active_cache[pos])) &&
171+
(is_missing || !was_missing))
172+
{
173+
state.force = 1;
174+
state.refresh_cache = 1;
175+
state.istate = &the_index;
176+
ceBefore = make_cache_entry(&the_index, two->mode,
177+
&two->oid, two->path,
178+
0, 0);
179+
if (!ceBefore)
180+
die(_("make_cache_entry failed for path '%s'"),
181+
two->path);
182+
183+
checkout_entry(ceBefore, &state, NULL, NULL);
184+
respect_skip_worktree = 0;
185+
}
186+
}
147187

148188
if (!is_in_reset_tree && !intent_to_add) {
149189
remove_file_from_cache(one->path);
@@ -162,8 +202,14 @@ static void update_index_from_diff(struct diff_queue_struct *q,
162202
* to properly construct the reset sparse directory.
163203
*/
164204
pos = cache_name_pos(one->path, strlen(one->path));
165-
if ((pos >= 0 && ce_skip_worktree(active_cache[pos])) ||
166-
(pos < 0 && !path_in_sparse_checkout(one->path, &the_index)))
205+
206+
/*
207+
* Do not add the SKIP_WORKTREE bit back if we populated the
208+
* file on purpose in a virtual filesystem scenario.
209+
*/
210+
if (respect_skip_worktree &&
211+
((pos >= 0 && ce_skip_worktree(active_cache[pos])) ||
212+
(pos < 0 && !path_in_sparse_checkout(one->path, &the_index))))
167213
ce->ce_flags |= CE_SKIP_WORKTREE;
168214

169215
if (!ce)

0 commit comments

Comments
 (0)