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

[PATCH 1/2] completion: refactor existence checks for special refs

From
Stan Hu <stanhu@gmail.com>
Date
Nov 30, 2023, 20:24 UTC
Message-ID
<20231130202404.89791-2-stanhu@gmail.com>
In-Reply-To
<20231130202404.89791-1-stanhu@gmail.com>

In preparation for the reftable backend, this commit introduces a '__git_ref_exists' function that continues to use 'test -f' to determine whether a given ref exists in the local filesystem.

Each caller of '__git_ref_exists' must call '__git_find_repo_path` first. '_git_restore' already used 'git rev-parse HEAD', but to use '__git_ref_exists' insert a '__git_find_repo_path' at the start.

Reviewed-by: Patrick Steinhardt <ps@pks.im>
Reviewed-by: Christian Couder <christian.couder@gmail.com>
Signed-off-by: Stan Hu <stanhu@gmail.com>
---
 contrib/completion/git-completion.bash | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 13a39ebd2e..9fbdc13f9a 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -122,6 +122,15 @@ __git ()
 		${__git_dir:+--git-dir="$__git_dir"} "$@" 2>/dev/null
 }
 
+# Runs git in $__git_repo_path to determine whether a ref exists.
+# 1: The ref to search
+__git_ref_exists ()
+{
+	local ref=$1
+
+	[ -f "$__git_repo_path/$ref" ]
+}
+
 # Removes backslash escaping, single quotes and double quotes from a word,
 # stores the result in the variable $dequoted_word.
 # 1: The word to dequote.
@@ -1625,7 +1634,7 @@ __git_cherry_pick_inprogress_options=$__git_sequencer_inprogress_options
 _git_cherry_pick ()
 {
 	__git_find_repo_path
-	if [ -f "$__git_repo_path"/CHERRY_PICK_HEAD ]; then
+	if __git_ref_exists CHERRY_PICK_HEAD; then
 		__gitcomp "$__git_cherry_pick_inprogress_options"
 		return
 	fi
@@ -2067,7 +2076,7 @@ _git_log ()
 	__git_find_repo_path
 
 	local merge=""
-	if [ -f "$__git_repo_path/MERGE_HEAD" ]; then
+	if __git_ref_exists MERGE_HEAD; then
 		merge="--merge"
 	fi
 	case "$prev,$cur" in
@@ -2934,6 +2943,7 @@ _git_reset ()
 
 _git_restore ()
 {
+	__git_find_repo_path
 	case "$prev" in
 	-s)
 		__git_complete_refs
@@ -2952,7 +2962,7 @@ _git_restore ()
 		__gitcomp_builtin restore
 		;;
 	*)
-		if __git rev-parse --verify --quiet HEAD >/dev/null; then
+		if __git_ref_exists HEAD; then
 			__git_complete_index_file "--modified"
 		fi
 	esac
@@ -2963,7 +2973,7 @@ __git_revert_inprogress_options=$__git_sequencer_inprogress_options
 _git_revert ()
 {
 	__git_find_repo_path
-	if [ -f "$__git_repo_path"/REVERT_HEAD ]; then
+	if __git_ref_exists REVERT_HEAD; then
 		__gitcomp "$__git_revert_inprogress_options"
 		return
 	fi
@@ -3592,7 +3602,7 @@ __gitk_main ()
 	__git_find_repo_path
 
 	local merge=""
-	if [ -f "$__git_repo_path/MERGE_HEAD" ]; then
+	if __git_ref_exists MERGE_HEAD; then
 		merge="--merge"
 	fi
 	case "$cur" in
-- 
2.42.0
Previous: Stan HuNext: Stan Hu
Message 2 of 15 in “completion: refactor and support reftables backend”
  1. 0/2 completion: refactor and support reftables backendStan Hu, Nov 30, 2023
  2. 1/2 completion: refactor existence checks for special refsStan Hu, Nov 30, 2023
  3. 2/2 completion: stop checking for reference existence via `test -f`Stan Hu, Nov 30, 2023
  4. Patrick SteinhardtDec 1, 2023
  5. Junio C HamanoDec 3, 2023
  6. Patrick SteinhardtDec 1, 2023
  7. 0/2 completion: refactor and support reftables backendStan Hu, Dec 7, 2023
  8. 1/2 completion: refactor existence checks for pseudorefsStan Hu, Dec 7, 2023
  9. Patrick SteinhardtDec 8, 2023
  10. 2/2 completion: support pseudoref existence checks for reftablesStan Hu, Dec 7, 2023
  11. 0/2 completion: refactor and support reftables backendStan Hu, Dec 19, 2023
  12. 1/2 completion: refactor existence checks for pseudorefsStan Hu, Dec 19, 2023
  13. SZEDER GáborJan 13, 2024
  14. 2/2 completion: support pseudoref existence checks for reftablesStan Hu, Dec 19, 2023
  15. Junio C HamanoDec 20, 2023

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.