Subject: Re: [PATCH 1/2] builtin/mv: bail out when trying to move child and its parent

From: Junio C Hamano <[email-masked]>
Date: Wed, 30 Apr 2025 15:21:42 -0700

Patrick Steinhardt <[email-masked]> writes:

> @@ -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.

> -	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?

> +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.