{"thread":{"id":"43335","subject":"Re: [StGIT PATCH] StGIT bash completion","startedAt":"2006-11-12T21:18:13Z","lastAt":"2006-11-16T16:29:58Z","messageCount":6,"participants":["Karl Hasselström","Catalin Marinas","Shawn Pearce"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"297993","messageId":"20061112211813.19959.73406.stgit@localhost","threadId":"43335","inReplyTo":null,"subject":"[StGIT PATCH] StGIT bash completion","fromName":"Karl Hasselström","fromEmail":"kha@treskal.com","sentAt":"2006-11-12T21:18:13Z","receivedAt":"2006-11-12T21:18:13Z","isPatch":true,"sender":{"key":"kha@treskal.com","avatar":"https://gravatar.com/avatar/f0120c734b5279b345075a28521e1ac66acb20c9913ffe9bf6ae97e53f7f3f13?d=mp&s=160"},"body":"From: Karl Hasselström <kha@treskal.com>\n\nA minimal bash completion script for StGIT. It completes the\nsubcommand names, and options and patch names for some subcommands.\n\nSigned-off-by: Karl Hasselström <kha@treskal.com>\n---\n\nI'm not good at all at bash programming -- this script was basically\nput together by pattern-matching Shawns git completion code -- so\ncomments are even more welcome than usual.\n\n contrib/stgit-completion.bash |  156 +++++++++++++++++++++++++++++++++++++++++\n 1 files changed, 156 insertions(+), 0 deletions(-)\n\ndiff --git a/contrib/stgit-completion.bash b/contrib/stgit-completion.bash\nnew file mode 100644\nindex 0000000..25549fb\n--- /dev/null\n+++ b/contrib/stgit-completion.bash\n@@ -0,0 +1,156 @@\n+# bash completion support for StGIT                        -*- shell-script -*-\n+#\n+# Copyright (C) 2006, Karl Hasselström <kha@treskal.com>\n+# Based on git-completion.sh\n+#\n+# To use these routines:\n+#\n+#    1. Copy this file to somewhere (e.g. ~/.stgit-completion.bash).\n+#\n+#    2. Add the following line to your .bashrc:\n+#         . ~/.stgit-completion.bash\n+\n+# The path to .git, or empty if we're not in a repository.\n+_gitdir ()\n+{\n+    echo \"$(git rev-parse --git-dir 2>/dev/null)\"\n+}\n+\n+# Name of the current branch, or empty if there isn't one.\n+_current_branch ()\n+{\n+    local b=$(git symbolic-ref HEAD 2>/dev/null)\n+    echo ${b#refs/heads/}\n+}\n+\n+# List of all applied patches.\n+_applied_patches ()\n+{\n+    local g=$(_gitdir)\n+    [ \"$g\" ] && cat \"$g/patches/$(_current_branch)/applied\"\n+}\n+\n+# List of all unapplied patches.\n+_unapplied_patches ()\n+{\n+    local g=$(_gitdir)\n+    [ \"$g\" ] && cat \"$g/patches/$(_current_branch)/unapplied\"\n+}\n+\n+# List of all patches.\n+_all_patches ()\n+{\n+    local b=$(_current_branch)\n+    local g=$(_gitdir)\n+    [ \"$g\" ] && cat \"$g/patches/$b/applied\" \"$g/patches/$b/unapplied\"\n+}\n+\n+# List of all patches except the current patch.\n+_all_other_patches ()\n+{\n+    local b=$(_current_branch)\n+    local g=$(_gitdir)\n+    [ \"$g\" ] && cat \"$g/patches/$b/applied\" \"$g/patches/$b/unapplied\" \\\n+        | grep -v \"^$(< $g/patches/$b/current)$\"\n+}\n+\n+# Generate completions for patches and patch ranges from the given\n+# patch list function, and options from the given list.\n+_complete_patch_range ()\n+{\n+    local patchlist=\"$1\" options=\"$2\"\n+    local pfx cur=\"${COMP_WORDS[COMP_CWORD]}\"\n+    case \"$cur\" in\n+        *..*)\n+            pfx=\"${cur%..*}..\"\n+            cur=\"${cur#*..}\"\n+            COMPREPLY=($(compgen -P \"$pfx\" -W \"$($patchlist)\" -- \"$cur\"))\n+            ;;\n+        *)\n+            COMPREPLY=($(compgen -W \"$options $($patchlist)\" -- \"$cur\"))\n+            ;;\n+    esac\n+}\n+\n+# Generate completions for options from the given list.\n+_complete_options ()\n+{\n+    local options=\"$1\"\n+    COMPREPLY=($(compgen -W \"$options\" -- \"${COMP_WORDS[COMP_CWORD]}\"))\n+}\n+\n+_stg_delete ()\n+{\n+    _complete_patch_range _all_patches \"--branch --help\"\n+}\n+\n+_stg_goto ()\n+{\n+    _complete_patch_range _all_other_patches \"--help\"\n+}\n+\n+_stg_mail ()\n+{\n+    _complete_patch_range _all_patches \\\n+        \"--all --to --cc --bcc --auto --noreply --version --prefix --template \\\n+         --cover --edit-cover --edit-patches --sleep --refid --smtp-user \\\n+         --smtp-password --branch --mbox --help\"\n+}\n+\n+_stg_new ()\n+{\n+    _complete_options \"--message --showpatch --author --authname --authemail \\\n+                       --authdate --commname --commemail --help\"\n+}\n+\n+_stg_pop ()\n+{\n+    _complete_patch_range _applied_patches \"--all --number --keep --help\"\n+}\n+\n+_stg_push ()\n+{\n+    _complete_patch_range _unapplied_patches \"--all --number --reverse \\\n+                                              --merged --undo --help\"\n+}\n+\n+_stg_status ()\n+{\n+    _complete_options \"--modified --new --deleted --conflict --unknown \\\n+                       --noexclude --reset --help\"\n+}\n+\n+_stg ()\n+{\n+    local i c=1 command\n+\n+    while [ $c -lt $COMP_CWORD ]; do\n+        if [ $c == 1 ]; then\n+            command=\"${COMP_WORDS[c]}\"\n+        fi\n+        c=$((++c))\n+    done\n+\n+    # Complete name of subcommand.\n+    if [ $c -eq $COMP_CWORD -a -z \"$command\" ]; then\n+        COMPREPLY=($(compgen \\\n+            -W \"--help --version \\\n+                $(stg help|grep '^ '|sed 's/ *\\([^ ]\\) .*/\\1/')\" \\\n+            -- \"${COMP_WORDS[COMP_CWORD]}\"))\n+        return;\n+    fi\n+\n+    # Complete arguments to subcommands.\n+    case \"$command\" in\n+        delete) _stg_delete ;;\n+        goto)   _stg_goto ;;\n+        mail)   _stg_mail ;;\n+        new)    _stg_new ;;\n+        pop)    _stg_pop ;;\n+        push)   _stg_push ;;\n+        status) _stg_status ;;\n+        *)      COMPREPLY=() ;;\n+    esac\n+}\n+\n"},{"id":"298119","messageId":"20061113050453.GA31488@spearce.org","threadId":"43335","inReplyTo":"20061112211813.19959.73406.stgit@localhost","subject":"Re: [StGIT PATCH] StGIT bash completion","fromName":"Shawn Pearce","fromEmail":"spearce@spearce.org","sentAt":"2006-11-13T05:04:53Z","receivedAt":"2006-11-13T05:04:53Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"Karl Hasselstr?m <kha@treskal.com> wrote:\n> From: Karl Hasselström <kha@treskal.com>\n> \n> A minimal bash completion script for StGIT. It completes the\n> subcommand names, and options and patch names for some subcommands.\n> \n> Signed-off-by: Karl Hasselström <kha@treskal.com>\n> ---\n> \n> I'm not good at all at bash programming -- this script was basically\n> put together by pattern-matching Shawns git completion code -- so\n> comments are even more welcome than usual.\n\nI'm not an StGIT user, but this looks pretty good.\n\n-- \n"},{"id":"296826","messageId":"b0943d9e0611160621g60a0cac2r5205e37ed7d9fe9f@mail.gmail.com","threadId":"43335","inReplyTo":"20061112211813.19959.73406.stgit@localhost","subject":"Re: [StGIT PATCH] StGIT bash completion","fromName":"Catalin Marinas","fromEmail":"catalin.marinas@gmail.com","sentAt":"2006-11-16T14:21:27Z","receivedAt":"2006-11-16T14:21:27Z","isPatch":true,"sender":{"key":"catalin.marinas@gmail.com","avatar":null},"body":"On 12/11/06, Karl Hasselström <kha@treskal.com> wrote:\n> A minimal bash completion script for StGIT. It completes the\n> subcommand names, and options and patch names for some subcommands.\n\nThanks for the patch. I modified it slightly to automatically generate\nthe options for other commands as well (by invoking \"stg help\n<command>\" and it doesn't seem to be slow). I'll try to push it\ntonight.\n\nThanks for the other patches as well. I included some but haven't\nfinished them yet.\n\n-- \n"},{"id":"297833","messageId":"20061116154002.GA20729@diana.vm.bytemark.co.uk","threadId":"43335","inReplyTo":"b0943d9e0611160621g60a0cac2r5205e37ed7d9fe9f@mail.gmail.com","subject":"Re: [StGIT PATCH] StGIT bash completion","fromName":"Karl Hasselström","fromEmail":"kha@treskal.com","sentAt":"2006-11-16T15:40:02Z","receivedAt":"2006-11-16T15:40:02Z","isPatch":true,"sender":{"key":"kha@treskal.com","avatar":"https://gravatar.com/avatar/f0120c734b5279b345075a28521e1ac66acb20c9913ffe9bf6ae97e53f7f3f13?d=mp&s=160"},"body":"On 2006-11-16 14:21:27 +0000, Catalin Marinas wrote:\n\n> On 12/11/06, Karl Hasselström <kha@treskal.com> wrote:\n>\n> > A minimal bash completion script for StGIT. It completes the\n> > subcommand names, and options and patch names for some\n> > subcommands.\n>\n> Thanks for the patch. I modified it slightly to automatically\n> generate the options for other commands as well (by invoking \"stg\n> help <command>\" and it doesn't seem to be slow). I'll try to push it\n> tonight.\n\nHmm. I'll have to try it, but I was half planning to hard-code the\nlist of subcommands instead of calling \"stg help\" since it causes a\ntangible delay. On the machines I've tried, it easily takes 0.2\nseconds to run \"stg help\" (with hot caches; with cold caches, it's\n_really_ bad), and that's bad for interactive behavior.\n\nThere's also the point that some switches need (or can benefit from)\nthe tab completion machinery, and thus can't just be automatically\nextracted and used. But autocompleting the switch names shouldn't\ninterfere with that, and hard-coding stuff that changes every now and\nthen is both morally wrong and a lot of work.\n\n> Thanks for the other patches as well. I included some\n\nAh, thanks!\n\n>  but haven't finished them yet.\n\nNo problem. Thanks to the power of modern version control systems, I'm\nable to use my own branch of StGIT in the meantime, on all the various\ncomputers that I use. :-)\n\n-- \nKarl Hasselström, kha@treskal.com\n"},{"id":"297446","messageId":"b0943d9e0611160812s6ffec19fm4bf079e08e5fce01@mail.gmail.com","threadId":"43335","inReplyTo":"20061116154002.GA20729@diana.vm.bytemark.co.uk","subject":"Re: [StGIT PATCH] StGIT bash completion","fromName":"Catalin Marinas","fromEmail":"catalin.marinas@gmail.com","sentAt":"2006-11-16T16:12:43Z","receivedAt":"2006-11-16T16:12:43Z","isPatch":true,"sender":{"key":"catalin.marinas@gmail.com","avatar":null},"body":"On 16/11/06, Karl Hasselström <kha@treskal.com> wrote:\n> On 2006-11-16 14:21:27 +0000, Catalin Marinas wrote:\n> > Thanks for the patch. I modified it slightly to automatically\n> > generate the options for other commands as well (by invoking \"stg\n> > help <command>\" and it doesn't seem to be slow). I'll try to push it\n> > tonight.\n>\n> Hmm. I'll have to try it, but I was half planning to hard-code the\n> list of subcommands instead of calling \"stg help\" since it causes a\n> tangible delay. On the machines I've tried, it easily takes 0.2\n> seconds to run \"stg help\" (with hot caches; with cold caches, it's\n> _really_ bad), and that's bad for interactive behavior.\n\nIndeed, stg help takes over 200ms on my machine as well, with hot\ncaches but this is mainly because it imports all the stgit.commands.*\nmodules in order to read the short description. The 'stg help\n<command>' takes around 90ms on my machine since it only imports one\nmodule (I actually reduced it to 85ms by minimizing the imports even\nfurther).\n\nI could actually hard-code the commands only in the script. At the\nmoment I removed the _stg_* functions and added some common\n_stg_all_patches() to avoid duplicating the code (see attached).\n\n-- \nCatalin\n"},{"id":"294394","messageId":"20061116162958.GB20729@diana.vm.bytemark.co.uk","threadId":"43335","inReplyTo":"b0943d9e0611160812s6ffec19fm4bf079e08e5fce01@mail.gmail.com","subject":"Re: [StGIT PATCH] StGIT bash completion","fromName":"Karl Hasselström","fromEmail":"kha@treskal.com","sentAt":"2006-11-16T16:29:58Z","receivedAt":"2006-11-16T16:29:58Z","isPatch":true,"sender":{"key":"kha@treskal.com","avatar":"https://gravatar.com/avatar/f0120c734b5279b345075a28521e1ac66acb20c9913ffe9bf6ae97e53f7f3f13?d=mp&s=160"},"body":"On 2006-11-16 16:12:43 +0000, Catalin Marinas wrote:\n\n> Indeed, stg help takes over 200ms on my machine as well, with hot\n> caches but this is mainly because it imports all the\n> stgit.commands.* modules in order to read the short description. The\n> 'stg help <command>' takes around 90ms on my machine since it only\n> imports one module\n\nExcellent. However, this suggests that an option should be added to\nstg that makes it print just the subcommand names, without importing\nanything, to get this kind of speed there as well. Call it\n--generate-tab-completion or something, and don't print it in the help\noutput.\n\n> (I actually reduced it to 85ms by minimizing the imports even\n> further).\n\nGoodie. For reference, I think git was able to list its subcommands in\nabout 20 ms. Just so you know what to aim for. :-)\n\n> I could actually hard-code the commands only in the script. At the\n> moment I removed the _stg_* functions and added some common\n> _stg_all_patches() to avoid duplicating the code (see attached).\n\nSeems reasonable.\n\nNext time I do some StGIT hacking (not this weekend, unfortunately), I\nwas planning to build more tab-completion stuff. On top of my list are\nfixing basic completion for all subcommands, and fixing some kind of\nfilename completion. But do feel free to do it first if you like. :-)\n\n-- \nKarl Hasselström, kha@treskal.com\n"}]}