Skip to content

Commit ba359fd

Browse files
newrengitster
authored andcommitted
stash: fix stash application in sparse-checkouts
sparse-checkouts are built on the patterns in the $GIT_DIR/info/sparse-checkout file, where commands have modified behavior for paths that do not match those patterns. The differences in behavior, as far as the bugs concerned here, fall into three different categories (with git subcommands that fall into each category listed): * commands that only look at files matching the patterns: * status * diff * clean * update-index * commands that remove files from the working tree that do not match the patterns, and restore files that do match them: * read-tree * switch * checkout * reset (--hard) * commands that omit writing files to the working tree that do not match the patterns, unless those files are not clean: * merge * rebase * cherry-pick * revert There are some caveats above, e.g. a plain `git diff` ignores files outside the sparsity patterns but will show diffs for paths outside the sparsity patterns when revision arguments are passed. (Technically, diff is treating the sparse paths as matching HEAD.) So, there is some internal inconsistency among these commands. There are also additional commands that should behave differently in the face of sparse-checkouts, as the sparse-checkout documentation alludes to, but the above is sufficient for me to explain how `git stash` is affected. What is relevant here is that logically 'stash' should behave like a merge; it three-way merges the changes the user had in progress at stash creation time, the HEAD at the time the stash was created, and the current HEAD, in order to get the stashed changes applied to the current branch. However, this simplistic view doesn't quite work in practice, because stash tweaks it a bit due to two factors: (1) flags like --keep-index and --include-untracked (why we used two different verbs, 'keep' and 'include', is a rant for another day) modify what should be staged at the end and include more things that should be quasi-merged, (2) stash generally wants changes to NOT be staged. It only provides exceptions when (a) some of the changes had conflicts and thus we want to use stages to denote the clean merges and higher order stages to mark the conflicts, or (b) if there is a brand new file we don't want it to become untracked. stash has traditionally gotten this special behavior by first doing a merge, and then when it's clean, applying a pipeline of commands to modify the result. This series of commands for unstaging-non-newly-added-files came from the following commands: git diff-index --cached --name-only --diff-filter=A $CTREE >"$a" git read-tree --reset $CTREE git update-index --add --stdin <"$a" rm -f "$a" Looking back at the different types of special sparsity handling listed at the beginning of this message, you may note that we have at least one of each type covered here: merge, diff-index, and read-tree. The weird mix-and-match led to 3 different bugs: (1) If a path merged cleanly and it didn't match the sparsity patterns, the merge backend would know to avoid writing it to the working tree and keep the SKIP_WORKTREE bit, simply only updating it in the index. Unfortunately, the subsequent commands would essentially undo the changes in the index and thus simply toss the changes altogether since there was nothing left in the working tree. This means the stash is only partially applied. (2) If a path existed in the worktree before `git stash apply` despite having the SKIP_WORKTREE bit set, then the `git read-tree --reset` would print an error message of the form error: Entry 'modified' not uptodate. Cannot merge. and cause stash to abort early. (3) If there was a brand new file added by the stash, then the diff-index command would save that pathname to the temporary file, the read-tree --reset would remove it from the index, and the update-index command would barf due to no such file being present in the working copy; it would print a message of the form: error: NEWFILE: does not exist and --remove not passed fatal: Unable to process path NEWFILE and then cause stash to abort early. Basically, the whole idea of unstage-unless-brand-new requires special care when you are dealing with a sparse-checkout. Fix these problems by applying the following simple rule: When we unstage files, if they have the SKIP_WORKTREE bit set, clear that bit and write the file out to the working directory. (*) If there's already a file present in the way, rename it first. This fixes all three problems in t7012.13 and allows us to mark it as passing. Signed-off-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b34ab4a commit ba359fd

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

builtin/stash.c

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,12 +363,23 @@ static void unstage_changes_unless_new(struct object_id *orig_tree)
363363
* relevant trees, and the merge logic always stages whatever merges
364364
* cleanly. We want to unstage those changes, unless it corresponds
365365
* to a file that didn't exist as of orig_tree.
366+
*
367+
* However, if any SKIP_WORKTREE path is modified relative to
368+
* orig_tree, then we want to clear the SKIP_WORKTREE bit and write
369+
* it to the worktree before unstaging.
366370
*/
367371

372+
struct checkout state = CHECKOUT_INIT;
368373
struct diff_options diff_opts;
369374
struct lock_file lock = LOCK_INIT;
370375
int i;
371376

377+
/* If any entries have skip_worktree set, we'll have to check 'em out */
378+
state.force = 1;
379+
state.quiet = 1;
380+
state.refresh_cache = 1;
381+
state.istate = &the_index;
382+
372383
/*
373384
* Step 1: get a difference between orig_tree (which corresponding
374385
* to the index before a merge was run) and the current index
@@ -395,7 +406,42 @@ static void unstage_changes_unless_new(struct object_id *orig_tree)
395406
strlen(p->two->path));
396407

397408
/*
398-
* Step 2: "unstage" changes, as long as they are still tracked
409+
* Step 2: Place changes in the working tree
410+
*
411+
* Stash is about restoring changes *to the working tree*.
412+
* So if the merge successfully got a new version of some
413+
* path, but left it out of the working tree, then clear the
414+
* SKIP_WORKTREE bit and write it to the working tree.
415+
*/
416+
if (pos >= 0 && ce_skip_worktree(active_cache[pos])) {
417+
struct stat st;
418+
419+
ce = active_cache[pos];
420+
if (!lstat(ce->name, &st)) {
421+
/* Conflicting path present; relocate it */
422+
struct strbuf new_path = STRBUF_INIT;
423+
int fd;
424+
425+
strbuf_addf(&new_path,
426+
"%s.stash.XXXXXX", ce->name);
427+
fd = xmkstemp(new_path.buf);
428+
close(fd);
429+
printf(_("WARNING: Untracked file in way of "
430+
"tracked file! Renaming\n "
431+
" %s -> %s\n"
432+
" to make room.\n"),
433+
ce->name, new_path.buf);
434+
if (rename(ce->name, new_path.buf))
435+
die("Failed to move %s to %s\n",
436+
ce->name, new_path.buf);
437+
strbuf_release(&new_path);
438+
}
439+
checkout_entry(ce, &state, NULL, NULL);
440+
ce->ce_flags &= ~CE_SKIP_WORKTREE;
441+
}
442+
443+
/*
444+
* Step 3: "unstage" changes, as long as they are still tracked
399445
*/
400446
if (p->one->oid_valid) {
401447
/*
@@ -417,7 +463,7 @@ static void unstage_changes_unless_new(struct object_id *orig_tree)
417463
diff_flush(&diff_opts);
418464

419465
/*
420-
* Step 3: write the new index to disk
466+
* Step 4: write the new index to disk
421467
*/
422468
repo_hold_locked_index(the_repository, &lock, LOCK_DIE_ON_ERROR);
423469
if (write_locked_index(&the_index, &lock,

t/t7012-skip-worktree-writing.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ test_expect_success '--ignore-skip-worktree-entries leaves worktree alone' '
149149
--diff-filter=D -- keep-me.t
150150
'
151151

152-
test_expect_failure 'stash restore in sparse checkout' '
152+
test_expect_success 'stash restore in sparse checkout' '
153153
test_create_repo stash-restore &&
154154
(
155155
cd stash-restore &&

0 commit comments

Comments
 (0)