Skip to content

Commit dabba59

Browse files
jherlandgitster
authored andcommitted
notes-merge: Don't remove .git/NOTES_MERGE_WORKTREE; it may be the user's cwd
When a manual notes merge is committed or aborted, we need to remove the temporary worktree at .git/NOTES_MERGE_WORKTREE. However, removing the entire directory is not good if the user ran the 'git notes merge --commit/--abort' from within that directory. On Windows, the directory removal would simply fail, while on POSIX systems, users would suddenly find themselves in an invalid current directory. Therefore, instead of deleting the entire directory, we delete everything _within_ the directory, and leave the (empty) directory in place. This would cause a subsequent notes merge to abort, complaining about a previous - unfinished - notes merge (due to the presence of .git/NOTES_MERGE_WORKTREE), so we also need to adjust this check to only trigger when .git/NOTES_MERGE_WORKTREE is non-empty. Finally, adjust the t3310 manual notes merge testcases to correctly handle the existence of an empty .git/NOTES_MERGE_WORKTREE directory. Inspired-by: Junio C Hamano <[email protected]> Signed-off-by: Johan Herland <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a0be62c commit dabba59

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

notes-merge.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,8 @@ static void check_notes_merge_worktree(struct notes_merge_options *o)
267267
* Must establish NOTES_MERGE_WORKTREE.
268268
* Abort if NOTES_MERGE_WORKTREE already exists
269269
*/
270-
if (file_exists(git_path(NOTES_MERGE_WORKTREE))) {
270+
if (file_exists(git_path(NOTES_MERGE_WORKTREE)) &&
271+
!is_empty_dir(git_path(NOTES_MERGE_WORKTREE))) {
271272
if (advice_resolve_conflict)
272273
die("You have not concluded your previous "
273274
"notes merge (%s exists).\nPlease, use "
@@ -756,14 +757,18 @@ int notes_merge_commit(struct notes_merge_options *o,
756757

757758
int notes_merge_abort(struct notes_merge_options *o)
758759
{
759-
/* Remove .git/NOTES_MERGE_WORKTREE directory and all files within */
760+
/*
761+
* Remove all files within .git/NOTES_MERGE_WORKTREE. We do not remove
762+
* the .git/NOTES_MERGE_WORKTREE directory itself, since it might be
763+
* the current working directory of the user.
764+
*/
760765
struct strbuf buf = STRBUF_INIT;
761766
int ret;
762767

763768
strbuf_addstr(&buf, git_path(NOTES_MERGE_WORKTREE));
764769
if (o->verbosity >= 3)
765-
printf("Removing notes merge worktree at %s\n", buf.buf);
766-
ret = remove_dir_recursively(&buf, 0);
770+
printf("Removing notes merge worktree at %s/*\n", buf.buf);
771+
ret = remove_dir_recursively(&buf, REMOVE_DIR_KEEP_TOPLEVEL);
767772
strbuf_release(&buf);
768773
return ret;
769774
}

t/t3310-notes-merge-manual-resolve.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ y and z notes on 4th commit
324324
EOF
325325
git notes merge --commit &&
326326
# No .git/NOTES_MERGE_* files left
327-
test_must_fail ls .git/NOTES_MERGE_* >output 2>/dev/null &&
327+
test_might_fail ls .git/NOTES_MERGE_* >output 2>/dev/null &&
328328
test_cmp /dev/null output &&
329329
# Merge commit has pre-merge y and pre-merge z as parents
330330
test "$(git rev-parse refs/notes/m^1)" = "$(cat pre_merge_y)" &&
@@ -386,7 +386,7 @@ test_expect_success 'redo merge of z into m (== y) with default ("manual") resol
386386
test_expect_success 'abort notes merge' '
387387
git notes merge --abort &&
388388
# No .git/NOTES_MERGE_* files left
389-
test_must_fail ls .git/NOTES_MERGE_* >output 2>/dev/null &&
389+
test_might_fail ls .git/NOTES_MERGE_* >output 2>/dev/null &&
390390
test_cmp /dev/null output &&
391391
# m has not moved (still == y)
392392
test "$(git rev-parse refs/notes/m)" = "$(cat pre_merge_y)" &&
@@ -453,7 +453,7 @@ EOF
453453
# Finalize merge
454454
git notes merge --commit &&
455455
# No .git/NOTES_MERGE_* files left
456-
test_must_fail ls .git/NOTES_MERGE_* >output 2>/dev/null &&
456+
test_might_fail ls .git/NOTES_MERGE_* >output 2>/dev/null &&
457457
test_cmp /dev/null output &&
458458
# Merge commit has pre-merge y and pre-merge z as parents
459459
test "$(git rev-parse refs/notes/m^1)" = "$(cat pre_merge_y)" &&
@@ -542,7 +542,7 @@ EOF
542542
test_expect_success 'resolve situation by aborting the notes merge' '
543543
git notes merge --abort &&
544544
# No .git/NOTES_MERGE_* files left
545-
test_must_fail ls .git/NOTES_MERGE_* >output 2>/dev/null &&
545+
test_might_fail ls .git/NOTES_MERGE_* >output 2>/dev/null &&
546546
test_cmp /dev/null output &&
547547
# m has not moved (still == w)
548548
test "$(git rev-parse refs/notes/m)" = "$(git rev-parse refs/notes/w)" &&

0 commit comments

Comments
 (0)