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

[PATCH/RFC] completion: complete refs in multiple steps

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Jan 28, 2019, 09:41 UTC
Message-ID
<20190128094155.2424-1-pclouds@gmail.com>

This is in the same spirit of f22f682695 (completion: complete general config vars in two steps - 2018-05-27). Instead of considering all full refs as completion candidates, it completes one "path" component at a time, e.g.

    $ git switch-branch -d j<TAB>
    jch/            junio-gpg-pub
    $ git switch-branch -d jch/<TAB>
    Display all 154 possibilities? (y or n)
    jch/ab/            jch/fc/
    ....
    $ git switch-branch -d jch/nd/<TAB>
    jch/nd/attr-pathspec-fix
    jch/nd/attr-pathspec-in-tree-walk
    ...

For refs organized in multiple levels like this (and I've seen refs in 4 levels), especially when there a lot of refs, incremental completion this way makes it easier to get to what you want.

The cost of course is more complicated completion and also slower on systems with slow process creation. So maybe there will be a switch to turn this on or off?

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 contrib/completion/git-completion.bash | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 499e56f83d..d74ee79866 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -742,6 +742,17 @@ __git_refs ()
 	esac
 }
 
+__git_collapse_refs ()
+{
+	local regex="$(echo "$1" | sed 's/[^/]\+/[^\/]*/g')"
+	case "$regex" in
+		'') regex='[^\/]*';;
+		*/) regex="${regex}[^/]*";;
+	esac
+	regex="$(echo "$regex" | sed 's/\//\\\//g')"
+	sed -ne "s/\\($regex\\/\\?\\).*/\\1/p"
+}
+
 # Completes refs, short and long, local and remote, symbolic and pseudo.
 #
 # Usage: __git_complete_refs [<option>]...
@@ -769,7 +780,7 @@ __git_complete_refs ()
 		shift
 	done
 
-	__gitcomp_direct "$(__git_refs "$remote" "$track" "$pfx" "$cur_" "$sfx")"
+	__gitcomp_direct "$(__git_refs "$remote" "$track" "$pfx" "$cur_" "$sfx" | __git_collapse_refs "$cur_")"
 }
 
 # __git_refs2 requires 1 argument (to pass to __git_refs)
-- 
2.20.1.560.g70ca8b83ee
Next: SZEDER Gábor
Message 1 of 7 in “completion: complete refs in multiple steps”
  1. completion: complete refs in multiple stepsNguyễn Thái Ngọc Duy, Jan 28, 2019
  2. SZEDER GáborJan 28, 2019
  3. Jeff KingJan 28, 2019
  4. Jeff KingJan 28, 2019
  5. Duy NguyenJan 29, 2019
  6. Jeff KingJan 29, 2019
  7. SZEDER GáborJan 29, 2019

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.