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

[PATCH 2/2] Use $(git rev-parse --show-toplevel) in cd_to_toplevel().

From
SDSteven Drake <sdrake@xnet.co.nz>
Date
Jan 11, 2010, 22:34 UTC
Message-ID
<alpine.LNX.2.00.1001121133520.10158@vqena.qenxr.bet.am>
In-Reply-To
<alpine.LNX.2.00.1001102011420.2560@vqena.qenxr.bet.am>

rev-parse --show-toplevel gives the absolute (aka "physical") path of the toplevel directory and is more portable as 'cd -P' is not supported by all shell implementations.

This is also closer to what setup_work_tree() does.
Signed-off-by: Steven Drake <sdrake@xnet.co.nz>
---
 git-sh-setup.sh |   19 +++++--------------
 1 files changed, 5 insertions(+), 14 deletions(-)
diff --git a/git-sh-setup.sh b/git-sh-setup.sh
index dfcb807..d56426d 100755
--- a/git-sh-setup.sh
+++ b/git-sh-setup.sh
@@ -120,20 +120,11 @@ is_bare_repository () {
 }
 
 cd_to_toplevel () {
-	cdup=$(git rev-parse --show-cdup)
-	if test ! -z "$cdup"
-	then
-		# The "-P" option says to follow "physical" directory
-		# structure instead of following symbolic links.  When cdup is
-		# "../", this means following the ".." entry in the current
-		# directory instead textually removing a symlink path element
-		# from the PWD shell variable.  The "-P" behavior is more
-		# consistent with the C-style chdir used by most of Git.
-		cd -P "$cdup" || {
-			echo >&2 "Cannot chdir to $cdup, the toplevel of the working tree"
-			exit 1
-		}
-	fi
+	cdup=$(git rev-parse --show-toplevel) &&
+	cd "$cdup" || {
+		echo >&2 "Cannot chdir to $cdup, the toplevel of the working tree"
+		exit 1
+	}
 }
 
 require_work_tree () {
-- 
1.6.4
Previous: Steven Drake
Message 3 of 3 in “Add 'git rev-parse --show-toplevel' option.”
  1. 1/2 Add 'git rev-parse --show-toplevel' option.Steven Drake, Jan 10, 2010
  2. 1/2 Add 'git rev-parse --show-toplevel' option.Steven Drake, Jan 11, 2010
  3. 2/2 Use $(git rev-parse --show-toplevel) in cd_to_toplevel().Steven Drake, Jan 11, 2010

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.