{"thread":{"id":"1999","subject":"StGit command completions - and Git ones?","startedAt":"2005-10-02T11:03:44Z","lastAt":"2005-10-04T09:57:50Z","messageCount":3,"participants":["Blaisorblade","Catalin Marinas"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"9608","messageId":"200510021303.44960.blaisorblade@yahoo.it","threadId":"1999","inReplyTo":null,"subject":"StGit command completions - and Git ones?","fromName":"Blaisorblade","fromEmail":"blaisorblade@yahoo.it","sentAt":"2005-10-02T11:03:44Z","receivedAt":"2005-10-02T11:03:44Z","isPatch":false,"sender":{"key":"blaisorblade@yahoo.it","avatar":null},"body":"I noticed in your TODO you talk about adding command line completions. I've \nbeen doing it for my needs, and the thing is still very incomplete, even if \nit's not a \"XXX hack hack hack\", and I wanted to send this first draft.\n\nNote: I've not tried to add any completion which isn't StGit specific:\na) I didn't need them\n\nb) they should be implemented for git and cogito first\n\nc) not everything is clear. For instance, I think that SHA1 completion is \npretty useless (I never type a SHA1), while only branch and tag completion \nis, but maybe other disagree: there's support for it in Cogito, in fact, and \na shortened SHA1 is used as kernel revision.\n\n-- \nInform me of my mistakes, so I can keep imitating Homer Simpson's \"Doh!\".\nPaolo Giarrusso, aka Blaisorblade (Skype ID \"PaoloGiarrusso\", ICQ 215621894)\nhttp://www.user-mode-linux.org/~blaisorblade\n\n\n_stg () \n{\n\tlocal cur cmd cmds \n\tcur=${COMP_WORDS[COMP_CWORD]}\n\tCOMPREPLY=()\n\tif [ $COMP_CWORD -eq 1 ]; then\n\t\tcmds=$(stg help|tail +4|awk '{print $1}')\n\t\tCOMPREPLY=( $(compgen -W \"${cmds}\" -- $cur) )\n\telse\n\t\tlocal cmd=${COMP_WORDS[1]}\n\t\tlocal prev=${COMP_WORDS[COMP_CWORD-1]}\n\t\t#if [ $COMP_CWORD -eq 2 -o \"$prev\" = \"-t\" -o \"$prev\" = \"--to\" -o \"$prev\" = \"-r\" ]; then\n\t\tlocal patches\n\t\t#Add -b support - pass \"-b branch\" to unapplied and applied.\n\t\tcase $cmd in\n\t\t\tpush)\n\t\t\tpatches=$(stg unapplied)\n\t\t\t;;\n\t\t\tpop|mail)\n\t\t\tpatches=$(stg applied)\n\t\t\t;;\n\t\t\tdiff|id)\n\t\t\tpatches=$((stg applied; stg unapplied)|while read i; do echo $i/; done)\n\t\t\t;;\n\t\tesac\n\t\tcase $cmd in\n\t\t\tpush)\n\t\t\tif [ \"$prev\" = \"-t\" -o \"$prev\" = \"--to\" ]; then\n\t\t\t\tif [ \"${cur#*:}\" != \"${cur}\" ]; then\n\t\t\t\t\tCOMPREPLY=( $(compgen -W \"${patches}\" -- ${cur#*:}) )\n\t\t\t\telse\n\t\t\t\t\tCOMPREPLY=( $(compgen -W \"${patches}\" -- $cur) )\n\t\t\t\tfi\n\t\t\telse\n\t\t\t\tcmds=\"-a --all -n --number -t --to --reverse --undo -h --help\"\n\t\t\t\tCOMPREPLY=( $(compgen -W \"${cmds} ${patches}\" -- $cur) )\n\t\t\tfi\n\t\t\t;;\n\n\t\t\tpop)\n\t\t\tif [ \"$prev\" = \"-t\" -o \"$prev\" = \"--to\" ]; then\n\t\t\t\tCOMPREPLY=( $(compgen -W \"${patches}\" -- $cur) )\n\t\t\telse\n\t\t\t\tcmds=\"-a --all -n --number -t --to -h --help\"\n\t\t\t\tCOMPREPLY=( $(compgen -W \"${cmds}\" -- $cur) )\n\t\t\tfi\n\t\t\t;;\n\n\t\t\tmail)\n\t\t\tif [ \"$prev\" = \"-r\" -o \"$prev\" = \"--range\" ]; then\n\t\t\t\tif [ \"${cur#*:}\" != \"${cur}\" ]; then\n\t\t\t\t\tCOMPREPLY=( $(compgen -W \"${patches}\" -- ${cur#*:}) )\n\t\t\t\telse\n\t\t\t\t\tCOMPREPLY=( $(compgen -W \"${patches}\" -- $cur) )\n\t\t\t\tfi\n\t\t\telse\n\t\t\t\tcmds=\"-a --all -r --range --to --cc --bcc -v --version \\\n\t\t\t\t-t --template -f --first -s --sleep --refid -u --smtp-user \\\n\t\t\t\t-p --smtp-password -b --branch -h --help\"\n\n\t\t\t\tCOMPREPLY=( $(compgen -o bashdefault -W \"${cmds} ${patches}\" -- $cur) )\n\t\t\tfi\n\t\t\t;;\n\t\t\tdiff)\n\t\t\tif [ \"$prev\" = \"-r\" ]; then\n\t\t\t\tif [ \"${cur#*:}\" != \"${cur}\" ]; then\n\t\t\t\t\tCOMPREPLY=( $(compgen -W \"${patches}\" -- ${cur#*:}) )\n\t\t\t\telse\n\t\t\t\t\tCOMPREPLY=( $(compgen -W \"${patches}\" -- $cur) )\n\t\t\t\tfi\n\t\t\telse\n\t\t\t\tcmds=\"-r -s --stat -h --help\"\n\n\t\t\t\tCOMPREPLY=( $(compgen -W \"${cmds}\" -- $cur) )\n\t\t\tfi\n\t\t\t;;\n\t\t\tid)\n\t\t\t\tcmds=\"-b --branch -h --help\"\n\t\t\t\tCOMPREPLY=( $(compgen -W \"${cmds} ${patches}\" -- $cur) )\n\t\t\t;;\n\t\t\t*)\n\t\t\tCOMPREPLY=( $(compgen -f -- $cur) )\n\t\t\t;;\n\t\tesac\n\t\t#else\n\t\t\t#COMPREPLY=( $(compgen -f -- $cur) )\n\t\t#fi\n\tfi\n}\n\ncomplete -o default -F _stg stg\n\n# vi: set ft=sh:\n"},{"id":"9659","messageId":"b0943d9e0510040023h287b9d44w@mail.gmail.com","threadId":"1999","inReplyTo":"200510021303.44960.blaisorblade@yahoo.it","subject":"Re: StGit command completions - and Git ones?","fromName":"Catalin Marinas","fromEmail":"catalin.marinas@gmail.com","sentAt":"2005-10-04T07:23:11Z","receivedAt":"2005-10-04T07:23:11Z","isPatch":false,"sender":{"key":"catalin.marinas@gmail.com","avatar":null},"body":"On 02/10/05, Blaisorblade <blaisorblade@yahoo.it> wrote:\n> I noticed in your TODO you talk about adding command line completions. I've\n> been doing it for my needs, and the thing is still very incomplete, even if\n> it's not a \"XXX hack hack hack\", and I wanted to send this first draft.\n\nThanks, I will have a look at it.\n\nFYI, the tla-contrib package has a script which automatically\ngenerates the bash completion script from the help messages (well,\nonly for tla, but can be adapted).\n\n--\nCatalin\n"},{"id":"9665","messageId":"200510041157.50747.blaisorblade@yahoo.it","threadId":"1999","inReplyTo":"b0943d9e0510040023h287b9d44w@mail.gmail.com","subject":"Re: StGit command completions - and Git ones?","fromName":"Blaisorblade","fromEmail":"blaisorblade@yahoo.it","sentAt":"2005-10-04T09:57:50Z","receivedAt":"2005-10-04T09:57:50Z","isPatch":false,"sender":{"key":"blaisorblade@yahoo.it","avatar":null},"body":"On Tuesday 04 October 2005 09:23, Catalin Marinas wrote:\n> On 02/10/05, Blaisorblade <blaisorblade@yahoo.it> wrote:\n> > I noticed in your TODO you talk about adding command line completions.\n> > I've been doing it for my needs, and the thing is still very incomplete,\n> > even if it's not a \"XXX hack hack hack\", and I wanted to send this first\n> > draft.\n>\n> Thanks, I will have a look at it.\nIf you haven't yet, here's today's version.\n> FYI, the tla-contrib package has a script which automatically\n> generates the bash completion script from the help messages (well,\n> only for tla, but can be adapted).\nYes, I wanted to do something like that to speed things up...\n\nif possible, I'd also like to generate option lists from help messages but \nthat doesn't seem so obvious (not much thought on this, though).\n\nFinally, I'd like to reimplement directly stg applied and unapplied, since \nthey reduce to trivial \"cat\"...\n-- \nInform me of my mistakes, so I can keep imitating Homer Simpson's \"Doh!\".\nPaolo Giarrusso, aka Blaisorblade (Skype ID \"PaoloGiarrusso\", ICQ 215621894)\nhttp://www.user-mode-linux.org/~blaisorblade\n\n\n#Stacked git bash completion.\n\n#TODO:\n# My opinion about bash completion is that they're excessively slow, especially\n# when the system is under load.\n#\n# So:\n# - save the list of stg commands in a file, created at install moment; on an\n# idle Athlon 64 laptop at 800MHz, stg help takes 0.22 seconds of CPU time,\n# without disk I/O.\n#\n# - read .git/patches/$branch/{applied,unapplied} directly instead of invoking\n# stg.\n#\n\n#XXX: must test for bash version, done in generic bash-completion and the\n#generic value can be seen from here, if we are included by the loop at the end\n#of /etc/bash_completion, i.e. if we're installed in /etc/bash-completion.d.\n#\n#Gentoo should be fixed to allow this.\n\nbashdefault=\"-o bashdefault\"\ndefault=\"-o default\"\n\n#XXX: not StGit specific, valid for git too.\n__git_refs()\n{\n    for i in $(echo .git/refs/heads/*); do\n\techo ${i#.git/refs/heads/}\n    done\n    for i in $(echo .git/refs/tags/*); do\n\techo ${i#.git/refs/tags/}\n    done\n    echo HEAD\n}\n\n__stg_unapplied()\n{\n    stg unapplied 2>/dev/null $@\n}\n\n__stg_applied()\n{\n    stg applied 2>/dev/null $@\n}\n\n__stg_all_patches()\n{\n    __stg_applied $@; __stg_unapplied $@\n}\n\n#XXX: Find a better name for this.\n#\n__stg_all_patch_ranges()\n{\n    __stg_all_patches $@|while read i; do echo $i/; done\n}\n\n__stg_top()\n{\n    stg top 2>/dev/null $@\n}\n\n__stg_branches()\n{\n    #for i in $(compgen -f .git/patches/); do\n    for i in $(echo .git/patches/*); do\n\techo ${i#.git/patches/}\n    done\n}\n\n_stg ()\n{\n    local cur cmd cmds opts\n    cur=${COMP_WORDS[COMP_CWORD]}\n    COMPREPLY=()\n    if [ $COMP_CWORD -eq 1 ]; then\n\tcmds=$(stg help|tail +4|awk '{print $1}')\n\tCOMPREPLY=( $(compgen -W \"${cmds}\" -- $cur) )\n    else\n\tlocal cmd=${COMP_WORDS[1]}\n\tlocal prev=${COMP_WORDS[COMP_CWORD-1]}\n\tlocal o_help=\"-h --help\"\n\tlocal o_branch=\"-b --branch\"\n\t#XXX: Add -b support - pass \"-b branch\" to unapplied and applied.\n\t#This can be done by calling __stg_unapplied directly below\n\t#instead of setting patches here.\n\t#But: how to look for -b? I'm scared about looping over opts\n\t#(I don't like completions when they take so much time).\n\tcase $cmd in\n\t    push)\n\t    if [ \"$prev\" = \"-t\" -o \"$prev\" = \"--to\" ]; then\n\t\tif [ \"${cur#*:}\" != \"${cur}\" ]; then\n\t\t    # Complete the 2nd range component, after ':'.\n\t\t    COMPREPLY=( $(compgen -W \"$(__stg_unapplied)\" -- ${cur#*:}) )\n\t\telse\n\t\t    COMPREPLY=( $(compgen -W \"$(__stg_unapplied)\" -- $cur) )\n\t\tfi\n\t    else\n\t\topts=\"-a --all -n --number -t --to --reverse --undo $o_help\"\n\t\tCOMPREPLY=( $(compgen -W \"${opts} $(__stg_unapplied)\" -- $cur) )\n\t    fi\n\t    ;;\n\n\t    pop)\n\t    if [ \"$prev\" = \"-t\" -o \"$prev\" = \"--to\" ]; then\n\t\tCOMPREPLY=( $(compgen -W \"$(__stg_applied)\" -- $cur) )\n\t    else\n\t\topts=\"-a --all -n --number -t --to $o_help\"\n\t\tCOMPREPLY=( $(compgen -W \"${opts}\" -- $cur) )\n\t    fi\n\t    ;;\n\n\t    mail)\n\t    if [ \"$prev\" = \"-r\" -o \"$prev\" = \"--range\" ]; then\n\t\tif [ \"${cur#*:}\" != \"${cur}\" ]; then\n\t\t    COMPREPLY=( $(compgen -W \"$(__stg_applied)\" -- ${cur#*:}) )\n\t\telse\n\t\t    COMPREPLY=( $(compgen -W \"$(__stg_applied)\" -- $cur) )\n\t\tfi\n\t    else\n\t\topts=\"-a --all -r --range --to --cc --bcc -v --version \\\n\t\t-t --template -f --first -s --sleep --refid -u --smtp-user \\\n\t\t-p --smtp-password $o_branch $o_help\"\n\n\t\tCOMPREPLY=( $(compgen $bashdefault -W \"${opts} \\\n\t\t$(__stg_applied)\" -- $cur) )\n\t    fi\n\t    ;;\n\t    diff)\n\t    if [ \"$prev\" = \"-r\" ]; then\n\t\tif [ \"${cur#*:}\" != \"${cur}\" ]; then\n\t\t    COMPREPLY=( $(compgen -W \"$(__stg_all_patch_ranges)\" -- \\\n\t\t    ${cur#*:}) )\n\t\telse\n\t\t    COMPREPLY=( $(compgen -W \"$(__stg_all_patch_ranges)\" -- \\\n\t\t    $cur) )\n\t\tfi\n\t    else\n\t\topts=\"-r -s --stat $o_help\"\n\n\t\tCOMPREPLY=( $(compgen -W \"${opts}\" -- $cur) )\n\t    fi\n\t    ;;\n\t    id)\n\t    if [ \"$prev\" = \"-b\" -o \"$prev\" = \"--branch\" ]; then\n\t\tCOMPREPLY=( $(compgen -W \"$(__stg_branches)\" -- $cur) )\n\t    else\n\t\topts=\"$o_branch $o_help\"\n\t\t#there's a lot of possible id's to complete\n\t\tCOMPREPLY=( $(compgen -W \"${opts} $(__stg_all_patch_ranges) \\\n\t\t$(__git_refs)\" -- $cur) )\n\t    fi\n\t    ;;\n\t    rename)\n\t    if [ \"$prev\" = \"-b\" -o \"$prev\" = \"--branch\" ]; then\n\t\tCOMPREPLY=( $(compgen -W \"$(__stg_branches)\" -- $cur) )\n\t    else\n\t\tCOMPREPLY=( $(compgen -W \"$(__stg_all_patches)\" -- $cur) )\n\t    fi\n\t    ;;\n\t    delete)\n\t    opts=\"${o_help}\"\n\t    COMPREPLY=( $(compgen -W \"${opts} $(__stg_unapplied; __stg_top)\" \\\n\t    -- $cur) )\n\t    ;;\n\t    series|unapplied|applied)\n\t    if [ \"$prev\" = \"-b\" -o \"$prev\" = \"--branch\" ]; then\n\t\tCOMPREPLY=( $(compgen -W \"$(__stg_branches)\" -- $cur) )\n\t    else\n\t\topts=\"$o_branch $o_help\"\n\t\t[ \"$cmd\" = \"series\" ] && \\\n\t\t    opts=\"$opts -e --empty\"\n\t\tCOMPREPLY=( $(compgen -W \"${opts}\" -- $cur) )\n\t    fi\n\t    ;;\n\t    refresh)\n\t    opts=\"-f --force -e --edit -s --showpatch -m --message \\\n\t    -a --author --authname --authemail --authdate --commname\n\t    --commemail $o_help\"\n\n\t    COMPREPLY=( $(compgen $bashdefault -W \"${opts}\" -- $cur) )\n\t    ;;\n\t    *)\n\t    COMPREPLY=( $(compgen $bashdefault -W \"${o_help}\" -f -- $cur) )\n\t    ;;\n\tesac\n    fi\n}\n\ncomplete $default -F _stg stg\n\n# vi: set ft=sh sw=4:\n"}]}