From be7ae8d20607ccd98afbe5dc353b2be2a40c3726 Mon Sep 17 00:00:00 2001 From: Peter Grayson Date: Sat, 7 Mar 2020 16:12:17 -0500 Subject: [PATCH] Use stg branch in bash and fish completions Instead of the bash completion script inspecting .git/refs/patches to find patches, it now runs `stg series`. The .git/refs/patches directory is an implementation detail and will likely change soon, so using the stg porcelain keeps the script at the right level of abstraction. Similarly, both the bash and fish completions are also updated to use `stg branch` to generate the list of StGit branches instead of searching .git/refs/heads for `.stgit` log branches. The log branches may be renamed in the future. Finally, a few additional changes are made to the bash completions to cleanup some shellcheck warnings. Signed-off-by: Peter Grayson --- stgit/completion/bash.py | 43 +++++++++++++++++++++++-------------------- stgit/completion/fish.py | 5 +++-- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/stgit/completion/bash.py b/stgit/completion/bash.py index 96b1076..a2b7910 100644 --- a/stgit/completion/bash.py +++ b/stgit/completion/bash.py @@ -142,19 +142,22 @@ def write(f, stuff, indent=0): def patch_list_fun(type): - return fun('_%s_patches' % type, 'local g=$(_gitdir)', - 'test "$g" && cat "$g/patches/$(_current_branch)/%s"' % type) + return fun('_%s_patches' % type, 'stg series --noprefix --%s' % type) def file_list_fun(name, cmd): - return fun('_%s_files' % name, 'local g=$(_gitdir)', - 'test "$g" && %s' % cmd) + return fun( + '_%s_files' % name, 'local g', 'g=$(_gitdir)', 'test "$g" && %s' % cmd, + ) def ref_list_fun(name, prefix): - return fun(name, 'local g=$(_gitdir)', - ("test \"$g\" && git show-ref | grep ' %s/' | sed 's,.* %s/,,'" - % (prefix, prefix))) + return fun( + name, + 'local g', + 'g=$(_gitdir)', + ("test \"$g\" && git show-ref | grep ' %s/' | sed 's,.* %s/,,'" % (prefix, prefix)) + ) def util(): @@ -162,22 +165,18 @@ def util(): fun_desc( '_gitdir', "The path to .git, or empty if we're not in a repository.", - 'echo "$(git rev-parse --git-dir 2>/dev/null)"', + 'git rev-parse --git-dir 2>/dev/null', ), fun_desc( '_current_branch', "Name of the current branch, or empty if there isn't one.", - 'local b=$(git symbolic-ref HEAD 2>/dev/null)', - 'echo ${b#refs/heads/}'), + 'local b', + 'b=$(git symbolic-ref HEAD 2>/dev/null)', + 'echo "${b#refs/heads/}"'), fun_desc( '_other_applied_patches', 'List of all applied patches except the current patch.', - 'local b=$(_current_branch)', - 'local g=$(_gitdir)', - ( - 'test "$g" && cat "$g/patches/$b/applied" | grep -v' - ' "^$(tail -n 1 $g/patches/$b/applied 2> /dev/null)$"' - ) + 'stg series --noprefix --applied | grep -v "^$(stg top)$"' ), fun( '_patch_range', @@ -198,8 +197,14 @@ def util(): ), fun( '_stg_branches', - 'local g=$(_gitdir)', - 'test "$g" && (cd $g/patches/ && echo *)', + ( + 'stg branch --list 2>/dev/null' + ' | grep ". s" | cut -f2 | cut -d" " -f1' + ) + ), + fun( + '_all_branches', + 'stg branch --list 2>/dev/null | cut -f2 | cut -d" " -f1' ), fun( '_mail_aliases', @@ -208,7 +213,6 @@ def util(): '| cut -d. -f 3' ) ), - ref_list_fun('_all_branches', 'refs/heads'), ref_list_fun('_tags', 'refs/tags'), ref_list_fun('_remotes', 'refs/remotes'), ] @@ -266,7 +270,6 @@ def command_fun(cmd, modname): def main_switch(commands): return fun( '_stg', - 'local i', 'local c=1', 'local command', '', diff --git a/stgit/completion/fish.py b/stgit/completion/fish.py index 30a97fe..2dd0d38 100644 --- a/stgit/completion/fish.py +++ b/stgit/completion/fish.py @@ -83,8 +83,9 @@ function __fish_stg_all_branches end function __fish_stg_stg_branches - command git for-each-ref --format='%(refname)' refs/heads/ 2>/dev/null \\ - | string replace -rf '^refs/heads/(.*).stgit$' '$1\\tStGit Branch' + command stg branch --list 2>/dev/null \\ + | string match -r ". s.\t\S+" \\ + | string replace -r ". s.\t" "" end function __fish_stg_applied_patches -- 2.11.4.GIT