[RFC PATCH 09/19] completion: platform-specific helper function to get physical path

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Two upcoming optimizations will replace invocations of git commands in
command substitutions with bash builtins examining the path of the
current working directory.  Since git works with physical paths, we
can't use logical path provided in $PWD, but have to resort to the
'$(pwd -P)' command substitution to get the physical path of the
current directory.

However, on platforms not supporting symbolic links, such as MinGW,
the path in $PWD is bound to be the physical path.  So on those
platforms we could avoid the command substitution and use $PWD
directly.  Great for MinGW, because the overhead of forking a subshell
is relatively large there.

So add a platform-specific helper function to get the physical path of
the current directory: on MinGW it's defined such that it gets the
physical path from $PWD, while on other platforms from '$(pwd -P)'.
The path is stored in a variable whose name is passed as argument, so
no command substitution is needed when invoking this function.

Signed-off-by: SZEDER Gábor <szeder@xxxxxxxxxx>
---

I'm not sure how to check for MinGW; I just looked through the output from
'set', and saw OSTYPE=msys there.

 contrib/completion/git-completion.bash | 14 ++++++++++++++
 t/t9903-bash-prompt.sh                 | 21 +++++++++++++++++++++
 2 files changed, 35 insertions(+)

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 5c8d4aea..bd7d39e3 100755
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -70,6 +70,20 @@ case "$COMP_WORDBREAKS" in
 *)   COMP_WORDBREAKS="$COMP_WORDBREAKS:"
 esac
 
+# __git_pwd_p() stores the physical path of the current working directory
+# in the variable whose name is given as argument
+if [ ${OSTYPE-} = "msys" ]; then
+__git_pwd_p ()
+{
+	eval $1="$PWD"
+}
+else
+__git_pwd_p ()
+{
+	eval $1=\"$(pwd -P)\"
+}
+fi
+
 # __gitdir accepts 0 or 1 arguments (i.e., location)
 # Prints the path to the .git directory, and stores it in $__git_dir as well.
 __gitdir ()
diff --git a/t/t9903-bash-prompt.sh b/t/t9903-bash-prompt.sh
index 496e04ad..3d722b25 100755
--- a/t/t9903-bash-prompt.sh
+++ b/t/t9903-bash-prompt.sh
@@ -32,6 +32,27 @@ test_expect_success 'setup for prompt tests' '
 	git checkout master
 '
 
+test_expect_success 'getting pwd -P' '
+	echo "$TRASH_DIRECTORY" > expected &&
+	(
+		__git_pwd_p p &&
+		echo "$p" > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success SYMLINKS 'getting pwd -P - avoids symlinks' '
+	echo "$TRASH_DIRECTORY/otherrepo" > expected &&
+	ln -s otherrepo link &&
+	test_when_finished "rm -f link" &&
+	(
+		cd link &&
+		__git_pwd_p p &&
+		echo "$p" > "$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
 test_expect_success 'gitdir - from command line (through $__git_dir)' '
 	echo "$TRASH_DIRECTORY/otherrepo/.git" > expected &&
 	(
-- 
1.7.10.1.541.gb1be298

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]