git-sh-setup: Use "cd" option, not /bin/pwd, for symlinked work tree

In cd_to_toplevel, instead of 'cd $(unset PWD; /bin/pwd)/$path'
use 'cd -P $path'.  The "-P" option yields a desirable similarity to
C chdir.

While the "-P" option may be slightly less commonly supported than
/bin/pwd, it is more concise, better tested, and less error prone.
I've already added the 'unset PWD' to fix the /bin/pwd solution on
BSD; there may be more edge cases out there.

This still passes all the same test cases in t5521-pull-symlink.sh and
t2300-cd-to-toplevel.sh, even before updating them to use 'pwd -P'.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
maint
Marcel M. Cary 2009-02-06 19:24:28 -08:00 committed by Junio C Hamano
parent ba743d1b0c
commit 2c3c395e84
2 changed files with 10 additions and 23 deletions

View File

@ -85,27 +85,14 @@ cd_to_toplevel () {
cdup=$(git rev-parse --show-cdup) cdup=$(git rev-parse --show-cdup)
if test ! -z "$cdup" if test ! -z "$cdup"
then then
case "$cdup" in # The "-P" option says to follow "physical" directory
/*) # structure instead of following symbolic links. When cdup is
# Not quite the same as if we did "cd -P '$cdup'" when # "../", this means following the ".." entry in the current
# $cdup contains ".." after symlink path components. # directory instead textually removing a symlink path element
# Don't fix that case at least until Git switches to # from the PWD shell variable. The "-P" behavior is more
# "cd -P" across the board. # consistent with the C-style chdir used by most of Git.
phys="$cdup" cd -P "$cdup" || {
;; echo >&2 "Cannot chdir to $cdup, the toplevel of the working tree"
..|../*|*/..|*/../*)
# Interpret $cdup relative to the physical, not logical, cwd.
# Probably /bin/pwd is more portable than passing -P to cd or pwd.
phys="$(unset PWD; /bin/pwd)/$cdup"
;;
*)
# There's no "..", so no need to make things absolute.
phys="$cdup"
;;
esac

cd "$phys" || {
echo >&2 "Cannot chdir to $phys, the toplevel of the working tree"
exit 1 exit 1
} }
fi fi

View File

@ -10,12 +10,12 @@ test_cd_to_toplevel () {
cd '"'$1'"' && cd '"'$1'"' &&
. git-sh-setup && . git-sh-setup &&
cd_to_toplevel && cd_to_toplevel &&
[ "$(unset PWD; /bin/pwd)" = "$TOPLEVEL" ] [ "$(pwd -P)" = "$TOPLEVEL" ]
) )
' '
} }


TOPLEVEL="$(unset PWD; /bin/pwd)/repo" TOPLEVEL="$(pwd -P)/repo"
mkdir -p repo/sub/dir mkdir -p repo/sub/dir
mv .git repo/ mv .git repo/
SUBDIRECTORY_OK=1 SUBDIRECTORY_OK=1