From 7cceca5cccdcf1f0f9caa80b82d26fcff65e6fdf Mon Sep 17 00:00:00 2001 From: Steven Drake Date: Tue, 12 Jan 2010 11:33:48 +1300 Subject: Add 'git rev-parse --show-toplevel' option. Shows the absolute path of the top-level working directory. Signed-off-by: Steven Drake Signed-off-by: Junio C Hamano --- Documentation/git-rev-parse.txt | 3 +++ builtin-rev-parse.c | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/Documentation/git-rev-parse.txt b/Documentation/git-rev-parse.txt index 82045a2522..dc829b333d 100644 --- a/Documentation/git-rev-parse.txt +++ b/Documentation/git-rev-parse.txt @@ -112,6 +112,9 @@ OPTIONS --remotes:: Show tag refs found in `$GIT_DIR/refs/remotes`. +--show-toplevel:: + Show the absolute path of the top-level directory. + --show-prefix:: When the command is invoked from a subdirectory, show the path of the current directory relative to the top-level diff --git a/builtin-rev-parse.c b/builtin-rev-parse.c index 37d0233521..cbe5b428ad 100644 --- a/builtin-rev-parse.c +++ b/builtin-rev-parse.c @@ -581,6 +581,12 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix) for_each_remote_ref(show_reference, NULL); continue; } + if (!strcmp(arg, "--show-toplevel")) { + const char *work_tree = get_git_work_tree(); + if (work_tree) + puts(work_tree); + continue; + } if (!strcmp(arg, "--show-prefix")) { if (prefix) puts(prefix); -- cgit v1.2.3 From 91dc602de9579fe8dc29814819904e2b5a4e92e1 Mon Sep 17 00:00:00 2001 From: Steven Drake Date: Tue, 12 Jan 2010 11:34:34 +1300 Subject: Use $(git rev-parse --show-toplevel) in cd_to_toplevel(). 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 Signed-off-by: Junio C Hamano --- git-sh-setup.sh | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/git-sh-setup.sh b/git-sh-setup.sh index dfcb8078f5..d56426dd39 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 () { -- cgit v1.2.3