git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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.
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 9 of 10 in “builtin/mv: bail out when trying to move child and its parent”
  1. 0/2 builtin/mv: bail out when trying to move child and its parentPatrick Steinhardt, Apr 30, 2025
  2. 2/2 builtin/mv: convert assert(3p) into `BUG()`Patrick Steinhardt, Apr 30, 2025
  3. Junio C HamanoApr 30, 2025
  4. Junio C HamanoApr 30, 2025
  5. Patrick SteinhardtMay 2, 2025
  6. Johannes SchindelinMay 2, 2025
  7. Junio C HamanoMay 2, 2025
  8. 1/2 builtin/mv: bail out when trying to move child and its parentPatrick Steinhardt, Apr 30, 2025
  9. Junio C HamanoApr 30, 2025
  10. Patrick SteinhardtMay 2, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.