threads / patch / 30995

patchsubmodules: don't stumble over symbolic links when cloning recursively

Subject: [PATCH] submodules: don't stumble over symbolic links when cloning recursively

## tl;dr

6 messages between Jul 11, 2012 and Jul 12, 2012. Diffs are folded; open one to read it.

replies: 5people: 2as markdown or json

Jens Lehmann· Jul 11, 2012, 18:11 UTC · lore

Since 69c305178 (submodules: refactor computation of relative gitdir path) cloning a submodule recursively fails for recursive submodules when a symbolic link is part of the path to the work tree of the superproject.

This happens when module_clone() tries to find the relative paths between work tree and git dir. When a symbolic link in current $PWD points to a directory in a different level determining the number of "../" needed to traverse to the superprojects work tree leads to a wrong result.

As there is no portable way to say "pwd -P" use cd_to_toplevel to remove the link from the pwd, which fixes this problem.

A test for this problem has been added to t7406.
Reported-by: Bob Halley <halley@play-bow.org>
Signed-off-by: Jens Lehmann <Jens.Lehmann@web.de>
---
Thanks to Bob for providing a very detailed bug report and test case!
 git-submodule.sh            |  4 ++--
 t/t7406-submodule-update.sh | 13 +++++++++++++
 2 files changed, 15 insertions(+), 2 deletions(-)
Show changes to 2 files +15 −2

git-submodule.sh, t/t7406-submodule-update.sh

diff --git a/git-submodule.sh b/git-submodule.sh
index 5629d87..f73e32e 100755
--- a/git-submodule.sh
+++ b/git-submodule.sh
@@ -186,8 +186,8 @@ module_clone()
 		die "$(eval_gettext "Clone of '\$url' into submodule path '\$sm_path' failed")"
 	fi

-	a=$(cd "$gitdir" && pwd)/
-	b=$(cd "$sm_path" && pwd)/
+	a=$(cd_to_toplevel && cd "$gitdir" && pwd)/
+	b=$(cd_to_toplevel && cd "$sm_path" && pwd)/
 	# normalize Windows-style absolute paths to POSIX-style absolute paths
 	case $a in [a-zA-Z]:/*) a=/${a%%:*}${a#*:} ;; esac
 	case $b in [a-zA-Z]:/*) b=/${b%%:*}${b#*:} ;; esac
diff --git a/t/t7406-submodule-update.sh b/t/t7406-submodule-update.sh
index dcb195b..05521de 100755
--- a/t/t7406-submodule-update.sh
+++ b/t/t7406-submodule-update.sh
@@ -636,4 +636,17 @@ test_expect_success 'submodule update properly revives a moved submodule' '
 	)
 '

+test_expect_success 'submodule update can handle symbolic links in pwd' '
+	mkdir -p linked/dir &&
+	ln -s linked/dir linkto &&
+	(
+		cd linkto &&
+		git clone "$TRASH_DIRECTORY"/super_update_r2 super &&
+		(
+			cd super &&
+			git submodule update --init --recursive
+		)
+	)
+'
+
 test_done
-- 
1.7.11.1.166.gb8ff004
Johannes Sixt· Jul 11, 2012, 19:10 UTC · re: Jens Lehmann · lore

Re: [PATCH] submodules: don't stumble over symbolic links when cloning recursively

Am 11.07.2012 20:11, schrieb Jens Lehmann:
Show 11 quoted lines
> Since 69c305178 (submodules: refactor computation of relative gitdir path)
> cloning a submodule recursively fails for recursive submodules when a
> symbolic link is part of the path to the work tree of the superproject.
> 
> This happens when module_clone() tries to find the relative paths between
> work tree and git dir. When a symbolic link in current $PWD points to a
> directory in a different level determining the number of "../" needed to
> traverse to the superprojects work tree leads to a wrong result.
> 
> As there is no portable way to say "pwd -P" use cd_to_toplevel to remove
> the link from the pwd, which fixes this problem.
...
> -	a=$(cd "$gitdir" && pwd)/
> -	b=$(cd "$sm_path" && pwd)/
> +	a=$(cd_to_toplevel && cd "$gitdir" && pwd)/
> +	b=$(cd_to_toplevel && cd "$sm_path" && pwd)/

But if you cd out, how can it be correct not to cd in again if $gitdir and/or $sm_path are relative?

And if $gitdir and/or $sm_path are absolute, how can the earlier cd_to_toplevel make a difference?

> +test_expect_success 'submodule update can handle symbolic links in pwd' '
Please add a SYMLINKS prerequisite.
Show 11 quoted lines
> +	mkdir -p linked/dir &&
> +	ln -s linked/dir linkto &&
> +	(
> +		cd linkto &&
> +		git clone "$TRASH_DIRECTORY"/super_update_r2 super &&
> +		(
> +			cd super &&
> +			git submodule update --init --recursive
> +		)
> +	)
> +'
-- Hannes
Jens Lehmann· Jul 11, 2012, 20:06 UTC · re: Johannes Sixt · lore

Re: [PATCH] submodules: don't stumble over symbolic links when cloning recursively

Am 11.07.2012 21:10, schrieb Johannes Sixt:
Show 20 quoted lines
> Am 11.07.2012 20:11, schrieb Jens Lehmann:
>> Since 69c305178 (submodules: refactor computation of relative gitdir path)
>> cloning a submodule recursively fails for recursive submodules when a
>> symbolic link is part of the path to the work tree of the superproject.
>>
>> This happens when module_clone() tries to find the relative paths between
>> work tree and git dir. When a symbolic link in current $PWD points to a
>> directory in a different level determining the number of "../" needed to
>> traverse to the superprojects work tree leads to a wrong result.
>>
>> As there is no portable way to say "pwd -P" use cd_to_toplevel to remove
>> the link from the pwd, which fixes this problem.
> ...
>> -	a=$(cd "$gitdir" && pwd)/
>> -	b=$(cd "$sm_path" && pwd)/
>> +	a=$(cd_to_toplevel && cd "$gitdir" && pwd)/
>> +	b=$(cd_to_toplevel && cd "$sm_path" && pwd)/
> 
> But if you cd out, how can it be correct not to cd in again if $gitdir
> and/or $sm_path are relative?

I'm not sure what you mean by "cd out", but the two "cd_to_toplevel" make sure that when $gitdir or $sm_path are relative the symbolic link gets removed from the output of pwd. So it's rather "cd into the path where the symlink is resolved".

> And if $gitdir and/or $sm_path are absolute, how can the earlier
> cd_to_toplevel make a difference?

Then it doesn't, but $sm_path is always relative while $gitdir is sometimes (in the superproject it returns ".git"). Just drop either of the "cd_to_toplevel" and run the test ;-)

But it looks like the commit message could use some tuning ...
>> +test_expect_success 'submodule update can handle symbolic links in pwd' '
> 
> Please add a SYMLINKS prerequisite.
Oops. Thanks, will add that.
Will wait some time for other comments before preparing v2.
Johannes Sixt· Jul 11, 2012, 20:39 UTC · re: Jens Lehmann · lore

Re: [PATCH] submodules: don't stumble over symbolic links when cloning recursively

Am 11.07.2012 22:06, schrieb Jens Lehmann:
Show 26 quoted lines
> Am 11.07.2012 21:10, schrieb Johannes Sixt:
>> Am 11.07.2012 20:11, schrieb Jens Lehmann:
>>> Since 69c305178 (submodules: refactor computation of relative gitdir path)
>>> cloning a submodule recursively fails for recursive submodules when a
>>> symbolic link is part of the path to the work tree of the superproject.
>>>
>>> This happens when module_clone() tries to find the relative paths between
>>> work tree and git dir. When a symbolic link in current $PWD points to a
>>> directory in a different level determining the number of "../" needed to
>>> traverse to the superprojects work tree leads to a wrong result.
>>>
>>> As there is no portable way to say "pwd -P" use cd_to_toplevel to remove
>>> the link from the pwd, which fixes this problem.
>> ...
>>> -	a=$(cd "$gitdir" && pwd)/
>>> -	b=$(cd "$sm_path" && pwd)/
>>> +	a=$(cd_to_toplevel && cd "$gitdir" && pwd)/
>>> +	b=$(cd_to_toplevel && cd "$sm_path" && pwd)/
>>
>> But if you cd out, how can it be correct not to cd in again if $gitdir
>> and/or $sm_path are relative?
> 
> I'm not sure what you mean by "cd out", but the two "cd_to_toplevel"
> make sure that when $gitdir or $sm_path are relative the symbolic link
> gets removed from the output of pwd. So it's rather "cd into the path
> where the symlink is resolved".

At this point we can be in a subdirectory of the worktree. With cd_to_toplevel we move up in the directory hierarchy ("cd out"). Then a relative $gitdir or $sm_path now points to the wrong directory. No?

-- Hannes
Jens Lehmann· Jul 11, 2012, 21:08 UTC · re: Johannes Sixt · lore

Re: [PATCH] submodules: don't stumble over symbolic links when cloning recursively

Am 11.07.2012 22:39, schrieb Johannes Sixt:
Show 31 quoted lines
> Am 11.07.2012 22:06, schrieb Jens Lehmann:
>> Am 11.07.2012 21:10, schrieb Johannes Sixt:
>>> Am 11.07.2012 20:11, schrieb Jens Lehmann:
>>>> Since 69c305178 (submodules: refactor computation of relative gitdir path)
>>>> cloning a submodule recursively fails for recursive submodules when a
>>>> symbolic link is part of the path to the work tree of the superproject.
>>>>
>>>> This happens when module_clone() tries to find the relative paths between
>>>> work tree and git dir. When a symbolic link in current $PWD points to a
>>>> directory in a different level determining the number of "../" needed to
>>>> traverse to the superprojects work tree leads to a wrong result.
>>>>
>>>> As there is no portable way to say "pwd -P" use cd_to_toplevel to remove
>>>> the link from the pwd, which fixes this problem.
>>> ...
>>>> -	a=$(cd "$gitdir" && pwd)/
>>>> -	b=$(cd "$sm_path" && pwd)/
>>>> +	a=$(cd_to_toplevel && cd "$gitdir" && pwd)/
>>>> +	b=$(cd_to_toplevel && cd "$sm_path" && pwd)/
>>>
>>> But if you cd out, how can it be correct not to cd in again if $gitdir
>>> and/or $sm_path are relative?
>>
>> I'm not sure what you mean by "cd out", but the two "cd_to_toplevel"
>> make sure that when $gitdir or $sm_path are relative the symbolic link
>> gets removed from the output of pwd. So it's rather "cd into the path
>> where the symlink is resolved".
> 
> At this point we can be in a subdirectory of the worktree. With
> cd_to_toplevel we move up in the directory hierarchy ("cd out"). Then a
> relative $gitdir or $sm_path now points to the wrong directory. No?

Nope, "git submodule" will refuse to run in anything but the root of the worktree. So we already are at the toplevel and use "cd_to_toplevel" only to resolve any symlinks present in $PWD. Looks like a comment explaining that above those lines would be a good idea ... will add one.

Johannes Sixt· Jul 12, 2012, 17:12 UTC · re: Jens Lehmann · lore

Re: [PATCH] submodules: don't stumble over symbolic links when cloning recursively

Am 11.07.2012 23:08, schrieb Jens Lehmann:
Show 9 quoted lines
> Am 11.07.2012 22:39, schrieb Johannes Sixt:
>> At this point we can be in a subdirectory of the worktree. With
>> cd_to_toplevel we move up in the directory hierarchy ("cd out"). Then a
>> relative $gitdir or $sm_path now points to the wrong directory. No?
> 
> Nope, "git submodule" will refuse to run in anything but the root of
> the worktree. So we already are at the toplevel and use "cd_to_toplevel"
> only to resolve any symlinks present in $PWD. Looks like a comment
> explaining that above those lines would be a good idea ... will add one.

Ah, OK. I missed the lack of SUBDIRECTORY_OK=Yes, but noticed a few cd_to_toplevel sprinkled throughout the script, so I thought it was OK to be in a subdirectory.

-- Hannes

← back to recent threads