{"thread":{"id":"32047","subject":"bash completion of \"addable\" files for `git add`","startedAt":"2012-11-09T13:22:27Z","lastAt":"2012-11-09T16:27:50Z","messageCount":2,"participants":["Andreas Zeidler","Jeff King"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"202694","messageId":"5E69B894-C392-4DD5-A4F1-723DA1A3D059@zitc.de","threadId":"32047","inReplyTo":null,"subject":"bash completion of \"addable\" files for `git add`","fromName":"Andreas Zeidler","fromEmail":"az@zitc.de","sentAt":"2012-11-09T13:22:27Z","receivedAt":"2012-11-09T13:22:27Z","isPatch":false,"sender":{"key":"az@zitc.de","avatar":null},"body":"hi,\n\ni've always been annoyed by having to type — or copy & paste — paths when i wanted to simply add some files to the index.  i know about the `add -i` interface, and that improves things a little, but having bash completion for so many other things it still seemed to much hassle.\n\nso here's a patch adding bash completion on `git add` for modified, updated and untracked files.  i've also set up a pull request — before i found `Documentation/SubmittingPatches`.  fwiw, it's at https://github.com/git/git/pull/29\n\nbest regards,\n\n\nandi\n\n\nFrom cbac6caee7e788569562cb7138eb698cc46a1b8f Mon Sep 17 00:00:00 2001\nFrom: Andreas Zeidler <az@zitc.de>\nDate: Fri, 9 Nov 2012 13:05:43 +0100\nSubject: [PATCH] add bash completion for \"addable\" files\n\nthis adds support for completing only modified, updated and untracked\nfiles on `git add`, since almost always these are what you want to add\nto the index.\n\nthe list of possible completions is determined using `git status` and\nfiltered using `egrep` and `sed`.\n\nSigned-off-by: Andreas Zeidler <az@zitc.de>\n---\n contrib/completion/git-completion.bash | 15 +++++++++++++++\n t/t9902-completion.sh                  | 11 +++++++++++\n 2 files changed, 26 insertions(+)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex be800e0..4aa0981 100644\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -799,6 +799,16 @@ _git_apply ()\n \tCOMPREPLY=()\n }\n \n+__git_addable ()\n+{\n+\tlocal dir=\"$(__gitdir)\"\n+\tif [ -d \"$dir\" ]; then\n+\t\tgit --git-dir=\"$dir\" status --short --untracked=all |\\\n+\t\t\tegrep '^.[UM?] ' | sed 's/^.. //'\n+\t\treturn\n+\tfi\n+}\n+\n _git_add ()\n {\n \t__git_has_doubledash && return\n@@ -810,6 +820,11 @@ _git_add ()\n \t\t\t--ignore-errors --intent-to-add\n \t\t\t\"\n \t\treturn\n+\t\t;;\n+\t*)\n+\t\t__gitcomp \"$(__git_addable)\"\n+\t\treturn\n+\t\t;;\n \tesac\n \tCOMPREPLY=()\n }\ndiff --git a/t/t9902-completion.sh b/t/t9902-completion.sh\nindex cbd0fb6..a7c81d3 100755\n--- a/t/t9902-completion.sh\n+++ b/t/t9902-completion.sh\n@@ -288,4 +288,15 @@ test_expect_failure 'complete tree filename with metacharacters' '\n \tEOF\n '\n \n+test_expect_success 'add completes untracked and modified names' '\n+\techo other content >file1 &&\n+\techo content >file2 &&\n+\techo more >file3 &&\n+\tgit add file1 &&\n+\ttest_completion_long \"git add f\" <<-\\EOF\n+\tfile2_\n+\tfile3_\n+\tEOF\n+'\n+\n test_done\n-- \n1.8.0.1.g43af610\n\n\n-- \npgp key at http://zitc.de/pgp - http://wwwkeys.de.pgp.net/\nupgrade to plone 4! -- http://plone.org/4\n"},{"id":"202704","messageId":"20121109162750.GC19725@sigill.intra.peff.net","threadId":"32047","inReplyTo":"5E69B894-C392-4DD5-A4F1-723DA1A3D059@zitc.de","subject":"Re: bash completion of \"addable\" files for `git add`","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2012-11-09T16:27:50Z","receivedAt":"2012-11-09T16:27:50Z","isPatch":false,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Fri, Nov 09, 2012 at 02:22:27PM +0100, Andreas Zeidler wrote:\n\n> so here's a patch adding bash completion on `git add` for modified,\n> updated and untracked files.  i've also set up a pull request — before\n> i found `Documentation/SubmittingPatches`.  fwiw, it's at\n> https://github.com/git/git/pull/29\n\nPlease put cover letter material like this after the \"---\" divider, or\ninclude a \"-- >8 --\" scissors line before your commit. Either helps \"git\nam\" realize which content should go into the commit message.\n\n> From cbac6caee7e788569562cb7138eb698cc46a1b8f Mon Sep 17 00:00:00 2001\n> From: Andreas Zeidler <az@zitc.de>\n> Date: Fri, 9 Nov 2012 13:05:43 +0100\n\nPlease omit these lines, as they are redundant with your email header.\n\n> Subject: [PATCH] add bash completion for \"addable\" files\n\nThis one is useful, but it would be even better if it were the subject\nof your email. :)\n\n> +__git_addable ()\n> +{\n> +\tlocal dir=\"$(__gitdir)\"\n> +\tif [ -d \"$dir\" ]; then\n> +\t\tgit --git-dir=\"$dir\" status --short --untracked=all |\\\n> +\t\t\tegrep '^.[UM?] ' | sed 's/^.. //'\n> +\t\treturn\n> +\tfi\n> +}\n\nYou would want to use the stable, scriptable \"--porcelain\" output, so\nthe completion is not broken by future changes to the \"--short\" format.\nHowever, I do not think using \"git status\" is the best option. It\ncomputes three things:\n\n  1. The diff between HEAD and index.\n\n  2. The diff between index and working tree.\n\n  3. The list of untracked files.\n\nBut you only care about (2) and (3), so you are wasting time computing\n(1).  I think the list you want could be generated with:\n\n  git diff-files --name-only\n  git ls-files --exclude-standard -o\n\nand then you do not need to worry about grep or sed at all.\n\n> @@ -810,6 +820,11 @@ _git_add ()\n>  \t\t\t--ignore-errors --intent-to-add\n>  \t\t\t\"\n>  \t\treturn\n> +\t\t;;\n> +\t*)\n> +\t\t__gitcomp \"$(__git_addable)\"\n> +\t\treturn\n> +\t\t;;\n\nI think you'd want to use __gitcomp_nl to handle filenames with spaces.\n\nSpeaking of which, the output of status (or of the commands I mentioned)\nmay have quoting applied to pathnames. I think that is not something we\nhandle very well right now in the completion, so it may not be worth\nworrying about for this particular patch.\n\nOther than those comments, I think the intent of your patch makes a lot\nof sense.\n\n-Peff\n"}]}