@matheusbsbs / matheus-bagliano / commits / 2c3c395e84
Fork 0

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>

Marcel M. Cary committed Feb 6, 2009 at 19:24 UTC 2c3c395e84409c278bd7b050877c36d04b952056
2 files changed +10 -23
git-sh-setup.sh
+8 -21
@@ -85,27 +85,14 @@ cd_to_toplevel () {
85 cdup=$(git rev-parse --show-cdup)
86 if test ! -z "$cdup"
87 then
88 - case "$cdup" in
89 - /*)
90 - # Not quite the same as if we did "cd -P '$cdup'" when
91 - # $cdup contains ".." after symlink path components.
92 - # Don't fix that case at least until Git switches to
93 - # "cd -P" across the board.
94 - phys="$cdup"
95 - ;;
96 - ..|../*|*/..|*/../*)
97 - # Interpret $cdup relative to the physical, not logical, cwd.
98 - # Probably /bin/pwd is more portable than passing -P to cd or pwd.
99 - phys="$(unset PWD; /bin/pwd)/$cdup"
100 - ;;
101 - *)
102 - # There's no "..", so no need to make things absolute.
103 - phys="$cdup"
104 - ;;
105 - esac
106 -
107 - cd "$phys" || {
108 - echo >&2 "Cannot chdir to $phys, the toplevel of the working tree"
88 + # The "-P" option says to follow "physical" directory
89 + # structure instead of following symbolic links. When cdup is
90 + # "../", this means following the ".." entry in the current
91 + # directory instead textually removing a symlink path element
92 + # from the PWD shell variable. The "-P" behavior is more
93 + # consistent with the C-style chdir used by most of Git.
94 + cd -P "$cdup" || {
95 + echo >&2 "Cannot chdir to $cdup, the toplevel of the working tree"
96 exit 1
97 }
98 fi
t/t2300-cd-to-toplevel.sh
+2 -2
@@ -10,12 +10,12 @@ test_cd_to_toplevel () {
10 cd '"'$1'"' &&
11 . git-sh-setup &&
12 cd_to_toplevel &&
13 - [ "$(unset PWD; /bin/pwd)" = "$TOPLEVEL" ]
13 + [ "$(pwd -P)" = "$TOPLEVEL" ]
14 )
15 '
16 }
17
18 -TOPLEVEL="$(unset PWD; /bin/pwd)/repo"
18 +TOPLEVEL="$(pwd -P)/repo"
19 mkdir -p repo/sub/dir
20 mv .git repo/
21 SUBDIRECTORY_OK=1