Re: [PATCH] submodule absorbgitdirs: use relative <from> and <to> paths

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I think we might be better off reverting a79e56cb0a6 than trying to fix
forward. But before getting into that, thanks for reading the report and
sending a fix :)

Ævar Arnfjörð Bjarmason <avarab@xxxxxxxxx> writes:

Rearranging the thread slightly,

>> - If "absorbgitdirs" becomes consistent with other "git submodule"
>>   subcommands and prints relative paths to submodules, then this
>>   produces the wrong result.
> It's harder to narrowly fix that problem than to just have
> relocate_single_git_dir_into_superproject() display the same sorts of
> paths we do for most other "submodule" commands. I.e. the "<to>"
> should be relative to the "<from>" path, rather than relative to the
> eventual superproject.

As I noted here and in the initial report [1], the relative path is from
the original CWD to the submodule, not from 'old submodule gitdir' to
'new submodule gitdir', so I wouldn't really consider this consistent
with other "submodule" commands.

Besides that, I also don't find the output intuitive..

[1] https://lore.kernel.org/git/kl6lmt8qv9gc.fsf@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/

> diff --git a/submodule.c b/submodule.c
> index c47358097fd..a464c99a27f 100644
> --- a/submodule.c
> +++ b/submodule.c
> @@ -2271,9 +2271,12 @@ int validate_submodule_git_dir(char *git_dir, const char *submodule_name)
>  static void relocate_single_git_dir_into_superproject(const char *path)
>  {
>  	char *old_git_dir = NULL, *real_old_git_dir = NULL, *real_new_git_dir = NULL;
> +	char *rel_old_git_dir;
> +	const char *rel_new_git_dir;
>  	struct strbuf new_gitdir = STRBUF_INIT;
>  	const struct submodule *sub;
> -	size_t off = 0;
> +	const char *super_prefix = get_super_prefix();
> +	const char *sp = super_prefix ? super_prefix : "";
>  
>  	if (submodule_uses_worktrees(path))
>  		die(_("relocate_gitdir for submodule '%s' with "
> @@ -2285,6 +2288,7 @@ static void relocate_single_git_dir_into_superproject(const char *path)
>  		return;
>  
>  	real_old_git_dir = real_pathdup(old_git_dir, 1);
> +	rel_old_git_dir = xstrfmt("%s%s", sp, old_git_dir);

rel_old_git_dir is relative to the root of the superproject's worktree.

> @@ -2293,23 +2297,23 @@ static void relocate_single_git_dir_into_superproject(const char *path)
>  	submodule_name_to_gitdir(&new_gitdir, the_repository, sub->name);
>  	if (validate_submodule_git_dir(new_gitdir.buf, sub->name) < 0)
>  		die(_("refusing to move '%s' into an existing git dir"),
> -		    real_old_git_dir);
> +		    rel_old_git_dir);
>  	if (safe_create_leading_directories_const(new_gitdir.buf) < 0)
>  		die(_("could not create directory '%s'"), new_gitdir.buf);
> +
>  	real_new_git_dir = real_pathdup(new_gitdir.buf, 1);
> +	rel_new_git_dir = relative_path(real_new_git_dir, real_old_git_dir,
> +					&new_gitdir);

rel_new_git_dir is relative to rel_old_git_dir

>  
> -	while (real_old_git_dir[off] && real_new_git_dir[off] &&
> -	       real_old_git_dir[off] == real_new_git_dir[off])
> -		off++;
>  	fprintf(stderr, _("Migrating git directory of '%s%s' from '%s' to '%s'\n"),
> -		get_super_prefix_or_empty(), path,
> -		real_old_git_dir + off, real_new_git_dir + off);
> +		sp, path, rel_old_git_dir, rel_new_git_dir);

and the submodule is also relative to the root of the superproject's
worktree...

> diff --git a/t/t7412-submodule-absorbgitdirs.sh b/t/t7412-submodule-absorbgitdirs.sh
> index a5cd6db7ac2..0afa0fe3a83 100755
> --- a/t/t7412-submodule-absorbgitdirs.sh
> +++ b/t/t7412-submodule-absorbgitdirs.sh
> @@ -27,7 +27,7 @@ test_expect_success 'absorb the git dir' '
>  	git status >expect.1 &&
>  	git -C sub1 rev-parse HEAD >expect.2 &&
>  	cat >expect <<-\EOF &&
> -	Migrating git directory of '\''sub1'\'' from '\''sub1/.git'\'' to '\''.git/modules/sub1'\''
> +	Migrating git directory of '\''sub1'\'' from '\''sub1/.git'\'' to '\''../../.git/modules/sub1'\''
>  	EOF
>  	git submodule absorbgitdirs 2>actual &&
>  	test_cmp expect actual &&

So when we print the 3 relative paths here, they don't all share the
same base, which I find quite unintuitive to parse.

The 'obvious' solution to make this relative to the original CWD is to
plumb the relative path in --super-prefix (like the other "git
submodule" commands), but that won't give the right result in all cases
either, since we found a call site that gets --super-prefix from "git
read-tree" [2], in which case, --super-prefix is relative to the root of
the worktree and not the original CWD. I don't think we should pursue
this.

A more workable fix (and a possible future direction for removing
--super-prefix) would be to pass "--original-cwd" instead of
"--super-prefix", which would let the child process resolve the relative
path correctly. That's a big change though, and I don't think it's worth
doing right now, especially with 'remove --super-prefix' underway.

So for now, I'd strongly prefer we either eject or revert a79e56cb0a6.

[2] https://lore.kernel.org/git/cover-v3-0.9-00000000000-20221119T122853Z-avarab@xxxxxxxxx




[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux