* [StGIT PATCH] StGIT bash completion
@ 2006-11-12 21:18 Karl Hasselström
2006-11-13 5:04 ` Shawn Pearce
2006-11-16 14:21 ` Catalin Marinas
0 siblings, 2 replies; 6+ messages in thread
From: Karl Hasselström @ 2006-11-12 21:18 UTC (permalink / raw)
To: Catalin Marinas; +Cc: git, Shawn Pearce
From: Karl Hasselström <kha@treskal.com>
A minimal bash completion script for StGIT. It completes the
subcommand names, and options and patch names for some subcommands.
Signed-off-by: Karl Hasselström <kha@treskal.com>
---
I'm not good at all at bash programming -- this script was basically
put together by pattern-matching Shawns git completion code -- so
comments are even more welcome than usual.
contrib/stgit-completion.bash | 156 +++++++++++++++++++++++++++++++++++++++++
1 files changed, 156 insertions(+), 0 deletions(-)
diff --git a/contrib/stgit-completion.bash b/contrib/stgit-completion.bash
new file mode 100644
index 0000000..25549fb
--- /dev/null
+++ b/contrib/stgit-completion.bash
@@ -0,0 +1,156 @@
+# bash completion support for StGIT -*- shell-script -*-
+#
+# Copyright (C) 2006, Karl Hasselström <kha@treskal.com>
+# Based on git-completion.sh
+#
+# To use these routines:
+#
+# 1. Copy this file to somewhere (e.g. ~/.stgit-completion.bash).
+#
+# 2. Add the following line to your .bashrc:
+# . ~/.stgit-completion.bash
+
+# The path to .git, or empty if we're not in a repository.
+_gitdir ()
+{
+ echo "$(git rev-parse --git-dir 2>/dev/null)"
+}
+
+# Name of the current branch, or empty if there isn't one.
+_current_branch ()
+{
+ local b=$(git symbolic-ref HEAD 2>/dev/null)
+ echo ${b#refs/heads/}
+}
+
+# List of all applied patches.
+_applied_patches ()
+{
+ local g=$(_gitdir)
+ [ "$g" ] && cat "$g/patches/$(_current_branch)/applied"
+}
+
+# List of all unapplied patches.
+_unapplied_patches ()
+{
+ local g=$(_gitdir)
+ [ "$g" ] && cat "$g/patches/$(_current_branch)/unapplied"
+}
+
+# List of all patches.
+_all_patches ()
+{
+ local b=$(_current_branch)
+ local g=$(_gitdir)
+ [ "$g" ] && cat "$g/patches/$b/applied" "$g/patches/$b/unapplied"
+}
+
+# List of all patches except the current patch.
+_all_other_patches ()
+{
+ local b=$(_current_branch)
+ local g=$(_gitdir)
+ [ "$g" ] && cat "$g/patches/$b/applied" "$g/patches/$b/unapplied" \
+ | grep -v "^$(< $g/patches/$b/current)$"
+}
+
+# Generate completions for patches and patch ranges from the given
+# patch list function, and options from the given list.
+_complete_patch_range ()
+{
+ local patchlist="$1" options="$2"
+ local pfx cur="${COMP_WORDS[COMP_CWORD]}"
+ case "$cur" in
+ *..*)
+ pfx="${cur%..*}.."
+ cur="${cur#*..}"
+ COMPREPLY=($(compgen -P "$pfx" -W "$($patchlist)" -- "$cur"))
+ ;;
+ *)
+ COMPREPLY=($(compgen -W "$options $($patchlist)" -- "$cur"))
+ ;;
+ esac
+}
+
+# Generate completions for options from the given list.
+_complete_options ()
+{
+ local options="$1"
+ COMPREPLY=($(compgen -W "$options" -- "${COMP_WORDS[COMP_CWORD]}"))
+}
+
+_stg_delete ()
+{
+ _complete_patch_range _all_patches "--branch --help"
+}
+
+_stg_goto ()
+{
+ _complete_patch_range _all_other_patches "--help"
+}
+
+_stg_mail ()
+{
+ _complete_patch_range _all_patches \
+ "--all --to --cc --bcc --auto --noreply --version --prefix --template \
+ --cover --edit-cover --edit-patches --sleep --refid --smtp-user \
+ --smtp-password --branch --mbox --help"
+}
+
+_stg_new ()
+{
+ _complete_options "--message --showpatch --author --authname --authemail \
+ --authdate --commname --commemail --help"
+}
+
+_stg_pop ()
+{
+ _complete_patch_range _applied_patches "--all --number --keep --help"
+}
+
+_stg_push ()
+{
+ _complete_patch_range _unapplied_patches "--all --number --reverse \
+ --merged --undo --help"
+}
+
+_stg_status ()
+{
+ _complete_options "--modified --new --deleted --conflict --unknown \
+ --noexclude --reset --help"
+}
+
+_stg ()
+{
+ local i c=1 command
+
+ while [ $c -lt $COMP_CWORD ]; do
+ if [ $c == 1 ]; then
+ command="${COMP_WORDS[c]}"
+ fi
+ c=$((++c))
+ done
+
+ # Complete name of subcommand.
+ if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
+ COMPREPLY=($(compgen \
+ -W "--help --version \
+ $(stg help|grep '^ '|sed 's/ *\([^ ]\) .*/\1/')" \
+ -- "${COMP_WORDS[COMP_CWORD]}"))
+ return;
+ fi
+
+ # Complete arguments to subcommands.
+ case "$command" in
+ delete) _stg_delete ;;
+ goto) _stg_goto ;;
+ mail) _stg_mail ;;
+ new) _stg_new ;;
+ pop) _stg_pop ;;
+ push) _stg_push ;;
+ status) _stg_status ;;
+ *) COMPREPLY=() ;;
+ esac
+}
+
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [StGIT PATCH] StGIT bash completion
2006-11-12 21:18 [StGIT PATCH] StGIT bash completion Karl Hasselström
@ 2006-11-13 5:04 ` Shawn Pearce
2006-11-16 14:21 ` Catalin Marinas
1 sibling, 0 replies; 6+ messages in thread
From: Shawn Pearce @ 2006-11-13 5:04 UTC (permalink / raw)
To: Karl Hasselström; +Cc: Catalin Marinas, git
Karl Hasselstr?m <kha@treskal.com> wrote:
> From: Karl Hasselström <kha@treskal.com>
>
> A minimal bash completion script for StGIT. It completes the
> subcommand names, and options and patch names for some subcommands.
>
> Signed-off-by: Karl Hasselström <kha@treskal.com>
> ---
>
> I'm not good at all at bash programming -- this script was basically
> put together by pattern-matching Shawns git completion code -- so
> comments are even more welcome than usual.
I'm not an StGIT user, but this looks pretty good.
--
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [StGIT PATCH] StGIT bash completion
2006-11-12 21:18 [StGIT PATCH] StGIT bash completion Karl Hasselström
2006-11-13 5:04 ` Shawn Pearce
@ 2006-11-16 14:21 ` Catalin Marinas
2006-11-16 15:40 ` Karl Hasselström
1 sibling, 1 reply; 6+ messages in thread
From: Catalin Marinas @ 2006-11-16 14:21 UTC (permalink / raw)
To: Karl Hasselström; +Cc: git, Shawn Pearce
On 12/11/06, Karl Hasselström <kha@treskal.com> wrote:
> A minimal bash completion script for StGIT. It completes the
> subcommand names, and options and patch names for some subcommands.
Thanks for the patch. I modified it slightly to automatically generate
the options for other commands as well (by invoking "stg help
<command>" and it doesn't seem to be slow). I'll try to push it
tonight.
Thanks for the other patches as well. I included some but haven't
finished them yet.
--
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [StGIT PATCH] StGIT bash completion
2006-11-16 14:21 ` Catalin Marinas
@ 2006-11-16 15:40 ` Karl Hasselström
2006-11-16 16:12 ` Catalin Marinas
0 siblings, 1 reply; 6+ messages in thread
From: Karl Hasselström @ 2006-11-16 15:40 UTC (permalink / raw)
To: Catalin Marinas; +Cc: git, Shawn Pearce
On 2006-11-16 14:21:27 +0000, Catalin Marinas wrote:
> On 12/11/06, Karl Hasselström <kha@treskal.com> wrote:
>
> > A minimal bash completion script for StGIT. It completes the
> > subcommand names, and options and patch names for some
> > subcommands.
>
> Thanks for the patch. I modified it slightly to automatically
> generate the options for other commands as well (by invoking "stg
> help <command>" and it doesn't seem to be slow). I'll try to push it
> tonight.
Hmm. I'll have to try it, but I was half planning to hard-code the
list of subcommands instead of calling "stg help" since it causes a
tangible delay. On the machines I've tried, it easily takes 0.2
seconds to run "stg help" (with hot caches; with cold caches, it's
_really_ bad), and that's bad for interactive behavior.
There's also the point that some switches need (or can benefit from)
the tab completion machinery, and thus can't just be automatically
extracted and used. But autocompleting the switch names shouldn't
interfere with that, and hard-coding stuff that changes every now and
then is both morally wrong and a lot of work.
> Thanks for the other patches as well. I included some
Ah, thanks!
> but haven't finished them yet.
No problem. Thanks to the power of modern version control systems, I'm
able to use my own branch of StGIT in the meantime, on all the various
computers that I use. :-)
--
Karl Hasselström, kha@treskal.com
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [StGIT PATCH] StGIT bash completion
2006-11-16 15:40 ` Karl Hasselström
@ 2006-11-16 16:12 ` Catalin Marinas
2006-11-16 16:29 ` Karl Hasselström
0 siblings, 1 reply; 6+ messages in thread
From: Catalin Marinas @ 2006-11-16 16:12 UTC (permalink / raw)
To: Karl Hasselström; +Cc: git, Shawn Pearce
[-- Attachment #1: Type: text/plain, Size: 1245 bytes --]
On 16/11/06, Karl Hasselström <kha@treskal.com> wrote:
> On 2006-11-16 14:21:27 +0000, Catalin Marinas wrote:
> > Thanks for the patch. I modified it slightly to automatically
> > generate the options for other commands as well (by invoking "stg
> > help <command>" and it doesn't seem to be slow). I'll try to push it
> > tonight.
>
> Hmm. I'll have to try it, but I was half planning to hard-code the
> list of subcommands instead of calling "stg help" since it causes a
> tangible delay. On the machines I've tried, it easily takes 0.2
> seconds to run "stg help" (with hot caches; with cold caches, it's
> _really_ bad), and that's bad for interactive behavior.
Indeed, stg help takes over 200ms on my machine as well, with hot
caches but this is mainly because it imports all the stgit.commands.*
modules in order to read the short description. The 'stg help
<command>' takes around 90ms on my machine since it only imports one
module (I actually reduced it to 85ms by minimizing the imports even
further).
I could actually hard-code the commands only in the script. At the
moment I removed the _stg_* functions and added some common
_stg_all_patches() to avoid duplicating the code (see attached).
--
Catalin
[-- Attachment #2: stgit-completion.bash --]
[-- Type: application/octet-stream, Size: 4282 bytes --]
# bash completion support for StGIT -*- shell-script -*-
#
# Copyright (C) 2006, Karl Hasselström <kha@treskal.com>
# Based on git-completion.sh
#
# To use these routines:
#
# 1. Copy this file to somewhere (e.g. ~/.stgit-completion.bash).
#
# 2. Add the following line to your .bashrc:
# . ~/.stgit-completion.bash
stg_commands="
--help
--version
add
applied
assimilate
branch
delete
diff
clean
clone
commit
export
files
float
fold
goto
help
id
import
init
log
mail
new
patches
pick
pop
pull
push
refresh
rename
resolved
rm
series
show
status
top
unapplied
uncommit
"
# The path to .git, or empty if we're not in a repository.
_gitdir ()
{
echo "$(git rev-parse --git-dir 2>/dev/null)"
}
# Name of the current branch, or empty if there isn't one.
_current_branch ()
{
local b=$(git symbolic-ref HEAD 2>/dev/null)
echo ${b#refs/heads/}
}
# List of all applied patches.
_applied_patches ()
{
local g=$(_gitdir)
[ "$g" ] && cat "$g/patches/$(_current_branch)/applied"
}
# List of all unapplied patches.
_unapplied_patches ()
{
local g=$(_gitdir)
[ "$g" ] && cat "$g/patches/$(_current_branch)/unapplied"
}
# List of all patches.
_all_patches ()
{
local b=$(_current_branch)
local g=$(_gitdir)
[ "$g" ] && cat "$g/patches/$b/applied" "$g/patches/$b/unapplied"
}
# List of all patches except the current patch.
_all_other_patches ()
{
local b=$(_current_branch)
local g=$(_gitdir)
[ "$g" ] && cat "$g/patches/$b/applied" "$g/patches/$b/unapplied" \
| grep -v "^$(< $g/patches/$b/current)$"
}
# List the command options
_cmd_options ()
{
stg $1 --help | grep -e " --[A-Za-z]" | sed -e "s/.*\(--[A-Za-z]\+\).*/\1/"
}
# Generate completions for patches and patch ranges from the given
# patch list function, and options from the given list.
_complete_patch_range ()
{
local patchlist="$1" options="$2"
local pfx cur="${COMP_WORDS[COMP_CWORD]}"
case "$cur" in
*..*)
pfx="${cur%..*}.."
cur="${cur#*..}"
COMPREPLY=($(compgen -P "$pfx" -W "$($patchlist)" -- "$cur"))
;;
*)
COMPREPLY=($(compgen -W "$options $($patchlist)" -- "$cur"))
;;
esac
}
# Generate completions for options from the given list.
_complete_options ()
{
local options="$1"
COMPREPLY=($(compgen -W "$options" -- "${COMP_WORDS[COMP_CWORD]}"))
}
_stg_common ()
{
_complete_options "$(_cmd_options $1)"
}
_stg_all_patches ()
{
_complete_patch_range _all_patches "$(_cmd_options $1)"
}
_stg_other_patches ()
{
_complete_patch_range _all_other_patches "$(_cmd_options $1)"
}
_stg_applied_patches ()
{
_complete_patch_range _applied_patches "$(_cmd_options $1)"
}
_stg_unapplied_patches ()
{
_complete_patch_range _unapplied_patches "$(_cmd_options $1)"
}
_stg ()
{
local i c=1 command
while [ $c -lt $COMP_CWORD ]; do
if [ $c == 1 ]; then
command="${COMP_WORDS[c]}"
fi
c=$((++c))
done
# Complete name of subcommand.
if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
COMPREPLY=($(compgen \
-W "$stg_commands" \
-- "${COMP_WORDS[COMP_CWORD]}"))
return;
fi
# Complete arguments to subcommands.
case "$command" in
# repository commands
id) _stg_all_patches $command ;;
# stack commands
float) _stg_all_patches $command ;;
goto) _stg_other_patches $command ;;
pop) _stg_applied_patches $command ;;
push) _stg_unapplied_patches $command ;;
# patch commands
delete) _stg_all_patches $command ;;
export) _stg_applied_patches $command ;;
files) _stg_all_patches $command ;;
log) _stg_all_patches $command ;;
mail) _stg_applied_patches $command ;;
pick) _stg_unapplied_patches $command ;;
rename) _stg_all_patches $command ;;
show) _stg_all_patches $command ;;
# all the other commands
*) _stg_common $command ;;
esac
}
complete -o default -F _stg stg
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [StGIT PATCH] StGIT bash completion
2006-11-16 16:12 ` Catalin Marinas
@ 2006-11-16 16:29 ` Karl Hasselström
0 siblings, 0 replies; 6+ messages in thread
From: Karl Hasselström @ 2006-11-16 16:29 UTC (permalink / raw)
To: Catalin Marinas; +Cc: git
On 2006-11-16 16:12:43 +0000, Catalin Marinas wrote:
> Indeed, stg help takes over 200ms on my machine as well, with hot
> caches but this is mainly because it imports all the
> stgit.commands.* modules in order to read the short description. The
> 'stg help <command>' takes around 90ms on my machine since it only
> imports one module
Excellent. However, this suggests that an option should be added to
stg that makes it print just the subcommand names, without importing
anything, to get this kind of speed there as well. Call it
--generate-tab-completion or something, and don't print it in the help
output.
> (I actually reduced it to 85ms by minimizing the imports even
> further).
Goodie. For reference, I think git was able to list its subcommands in
about 20 ms. Just so you know what to aim for. :-)
> I could actually hard-code the commands only in the script. At the
> moment I removed the _stg_* functions and added some common
> _stg_all_patches() to avoid duplicating the code (see attached).
Seems reasonable.
Next time I do some StGIT hacking (not this weekend, unfortunately), I
was planning to build more tab-completion stuff. On top of my list are
fixing basic completion for all subcommands, and fixing some kind of
filename completion. But do feel free to do it first if you like. :-)
--
Karl Hasselström, kha@treskal.com
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2006-11-16 16:30 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-11-12 21:18 [StGIT PATCH] StGIT bash completion Karl Hasselström
2006-11-13 5:04 ` Shawn Pearce
2006-11-16 14:21 ` Catalin Marinas
2006-11-16 15:40 ` Karl Hasselström
2006-11-16 16:12 ` Catalin Marinas
2006-11-16 16:29 ` Karl Hasselström
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).