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

[PATCH 3/3] completion: handle git -C option

From
Peter Wu <peter@lekensteyn.nl>
Date
Oct 28, 2015, 17:21 UTC
Message-ID
<1446052885-11173-4-git-send-email-peter@lekensteyn.nl>
In-Reply-To
<1446052885-11173-1-git-send-email-peter@lekensteyn.nl>

Avoid the "fatal: bad config file line 5 in config" message and properly complete git commands having the "-C" option.

Besides the trivial command parsing, __gitdir is rewritten to apply any directory changes requested via `git -C otherdir ...`.

Signed-off-by: Peter Wu <peter@lekensteyn.nl>
---
 contrib/completion/git-completion.bash | 45 +++++++++++++++++++++++-----------
 1 file changed, 31 insertions(+), 14 deletions(-)
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index fdf0f16..1646f61 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -34,24 +34,39 @@ case "$COMP_WORDBREAKS" in
 esac
 
 # __gitdir accepts 0 or 1 arguments (i.e., location)
-# returns location of .git repo
+# outputs location of .git repo if it exists, nothing otherwise.
 __gitdir ()
 {
-	if [ -z "${1-}" ]; then
-		if [ -n "${__git_dir-}" ]; then
-			echo "$__git_dir"
-		elif [ -n "${GIT_DIR-}" ]; then
-			test -d "${GIT_DIR-}" || return 1
-			echo "$GIT_DIR"
-		elif [ -d .git ]; then
-			echo .git
+	local gitdir=${1:-}
+
+	if [ -z "$gitdir" ]; then
+		# Try the first matching --git-dir or GIT_DIR, print nothing if these
+		# directories are invalid.
+		for gitdir in "${__git_dir-}" "${GIT_DIR-}"; do
+			[ -n "$gitdir" ] || continue
+			if [[ "$gitdir" != /* ]]; then
+				gitdir="${__git_cd:-.}/$gitdir"
+			fi
+			if [ -d "$gitdir" ]; then
+				echo "$gitdir"
+			fi
+			return
+		done
+
+		if [ -d "${__git_cd:-.}/.git" ]; then
+			echo "${__git_cd:-.}/.git"
 		else
-			git rev-parse --git-dir 2>/dev/null
+			git -C "$__git_cd" rev-parse --git-dir 2>/dev/null
 		fi
-	elif [ -d "$1/.git" ]; then
-		echo "$1/.git"
 	else
-		echo "$1"
+		if [[ "$gitdir" != /* ]]; then
+			gitdir="${__git_cd:-.}/$gitdir"
+		fi
+		if [ -d "$gitdir/.git" ]; then
+			echo "$gitdir/.git"
+		elif [ -d "$gitdir" ]; then
+			echo "$gitdir"
+		fi
 	fi
 }
 
@@ -2566,11 +2581,12 @@ _git_whatchanged ()
 
 __git_main ()
 {
-	local i c=1 command command_word_index __git_dir __git_options
+	local i c=1 command command_word_index __git_dir __git_options __git_cd=.
 
 	while [ $c -lt $cword ]; do
 		i="${words[c]}"
 		case "$i" in
+		-C)          ((c++)) ; __git_cd="${words[c]}" ;;
 		--git-dir=*) __git_dir="${i#--git-dir=}" ;;
 		--git-dir)   ((c++)) ; __git_dir="${words[c]}" ;;
 		--bare)      __git_dir="." ;;
@@ -2583,6 +2599,7 @@ __git_main ()
 	done
 
 	__git_options=(
+		-C "$__git_cd"
 		--git-dir="$(__gitdir)"
 	)
 
-- 
2.6.1
Previous: Peter Wu
Message 4 of 4 in “bash-completion fixes for global git options handling”
  1. 0/3 bash-completion fixes for global git options handlingPeter Wu, Oct 28, 2015
  2. 1/3 completion: ignore git options for subcommand completionPeter Wu, Oct 28, 2015
  3. 2/3 completion: pass --git-dir to more commandsPeter Wu, Oct 28, 2015
  4. 3/3 completion: handle git -C optionPeter Wu, Oct 28, 2015

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.