Re: [PATCH 1/2] builtin/mv: bail out when trying to move child and its parent
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Apr 30, 2025, 22:21 UTC
- Message-ID
- <xmqqmsbxz3wp.fsf@gitster.g>
- In-Reply-To
- <20250430-pks-mv-parent-child-conflict-v1-1-11a87c55ffb9@pks.im>
Patrick Steinhardt <ps@pks.im> writes:
Show 8 quoted lines
> @@ -368,8 +398,7 @@ int cmd_mv(int argc, > strvec_push(&sources, path); > strvec_push(&destinations, prefixed_path); > > - memset(modes + argc + j, 0, sizeof(enum update_mode)); > - modes[argc + j] |= ce_skip_worktree(ce) ? SPARSE : INDEX; > + modes[argc + j] = MOVE_VIA_PARENT_DIR | (ce_skip_worktree(ce) ? SPARSE : INDEX); > submodule_gitfiles[argc + j] = NULL;
OK, this is the part we both missed during the earlier round.
> + if (hashmap_get_entry(&moved_dirs, &needle, ent, NULL))
> + die(_("cannot move both '%s' and its parent directory '%s'"),
> + sources.v[i], pathbuf.buf);OK.
> -test_expect_failure 'nonsense mv triggers assertion failure and partially updated index' ' > +test_expect_success 'moving file and its parent directory at the same time fails' '
The new title is much more descriptive.
Show 9 quoted lines
> - test_must_fail git mv a/a.txt a b &&
> - git status --porcelain >actual &&
> - grep "^A[ ]*a/a.txt$" actual
> + cat >expect <<-EOF &&
> + fatal: cannot move both ${SQ}a/a.txt${SQ} and its parent directory ${SQ}a${SQ}
> + EOF
> + test_must_fail git mv a/a.txt a b 2>err &&
> + test_cmp expect err
> +'Shouldn't we make sure that after failing "git mv" the paths and the index entries stay as expected?
Show 13 quoted lines
> +test_expect_success 'moving nested directory and its parent directory at the same time fails' '
> + test_when_finished git reset --hard HEAD &&
> + git reset --hard HEAD &&
> + mkdir -p a/b/c &&
> + >a/b/c/file.txt &&
> + git add a &&
> + mkdir target &&
> + cat >expect <<-EOF &&
> + fatal: cannot move both ${SQ}a/b/c${SQ} and its parent directory ${SQ}a${SQ}
> + EOF
> + test_must_fail git mv a/b/c a target 2>err &&
> + test_cmp expect err
> 'Ditto.
By the way I think "git mv a a/b" in the same scenario already notices a problematic request, so it probably won't hit this codepath but we shoudl already be covered.
Thanks.