Re: bash completion of "addable" files for `git add`

2012-11-09 Thread Jeff King
On Fri, Nov 09, 2012 at 02:22:27PM +0100, Andreas Zeidler wrote:

> so 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

Please put cover letter material like this after the "---" divider, or
include a "-- >8 --" scissors line before your commit. Either helps "git
am" realize which content should go into the commit message.

> From cbac6caee7e788569562cb7138eb698cc46a1b8f Mon Sep 17 00:00:00 2001
> From: Andreas Zeidler 
> Date: Fri, 9 Nov 2012 13:05:43 +0100

Please omit these lines, as they are redundant with your email header.

> Subject: [PATCH] add bash completion for "addable" files

This one is useful, but it would be even better if it were the subject
of your email. :)

> +__git_addable ()
> +{
> + local dir="$(__gitdir)"
> + if [ -d "$dir" ]; then
> + git --git-dir="$dir" status --short --untracked=all |\
> + egrep '^.[UM?] ' | sed 's/^.. //'
> + return
> + fi
> +}

You would want to use the stable, scriptable "--porcelain" output, so
the completion is not broken by future changes to the "--short" format.
However, I do not think using "git status" is the best option. It
computes three things:

  1. The diff between HEAD and index.

  2. The diff between index and working tree.

  3. The list of untracked files.

But you only care about (2) and (3), so you are wasting time computing
(1).  I think the list you want could be generated with:

  git diff-files --name-only
  git ls-files --exclude-standard -o

and then you do not need to worry about grep or sed at all.

> @@ -810,6 +820,11 @@ _git_add ()
>   --ignore-errors --intent-to-add
>   "
>   return
> + ;;
> + *)
> + __gitcomp "$(__git_addable)"
> + return
> + ;;

I think you'd want to use __gitcomp_nl to handle filenames with spaces.

Speaking of which, the output of status (or of the commands I mentioned)
may have quoting applied to pathnames. I think that is not something we
handle very well right now in the completion, so it may not be worth
worrying about for this particular patch.

Other than those comments, I think the intent of your patch makes a lot
of sense.

-Peff
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


bash completion of "addable" files for `git add`

2012-11-09 Thread Andreas Zeidler
hi,

i'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.

so 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

best regards,


andi


>From cbac6caee7e788569562cb7138eb698cc46a1b8f Mon Sep 17 00:00:00 2001
From: Andreas Zeidler 
Date: Fri, 9 Nov 2012 13:05:43 +0100
Subject: [PATCH] add bash completion for "addable" files

this adds support for completing only modified, updated and untracked
files on `git add`, since almost always these are what you want to add
to the index.

the list of possible completions is determined using `git status` and
filtered using `egrep` and `sed`.

Signed-off-by: Andreas Zeidler 
---
 contrib/completion/git-completion.bash | 15 +++
 t/t9902-completion.sh  | 11 +++
 2 files changed, 26 insertions(+)

diff --git a/contrib/completion/git-completion.bash 
b/contrib/completion/git-completion.bash
index be800e0..4aa0981 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -799,6 +799,16 @@ _git_apply ()
COMPREPLY=()
 }
 
+__git_addable ()
+{
+   local dir="$(__gitdir)"
+   if [ -d "$dir" ]; then
+   git --git-dir="$dir" status --short --untracked=all |\
+   egrep '^.[UM?] ' | sed 's/^.. //'
+   return
+   fi
+}
+
 _git_add ()
 {
__git_has_doubledash && return
@@ -810,6 +820,11 @@ _git_add ()
--ignore-errors --intent-to-add
"
return
+   ;;
+   *)
+   __gitcomp "$(__git_addable)"
+   return
+   ;;
esac
COMPREPLY=()
 }
diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
index cbd0fb6..a7c81d3 100755
--- a/t/t9902-completion.sh
+++ b/t/t9902-completion.sh
@@ -288,4 +288,15 @@ test_expect_failure 'complete tree filename with 
metacharacters' '
EOF
 '
 
+test_expect_success 'add completes untracked and modified names' '
+   echo other content >file1 &&
+   echo content >file2 &&
+   echo more >file3 &&
+   git add file1 &&
+   test_completion_long "git add f" <<-\EOF
+   file2_
+   file3_
+   EOF
+'
+
 test_done
-- 
1.8.0.1.g43af610


-- 
pgp key at http://zitc.de/pgp - http://wwwkeys.de.pgp.net/
upgrade to plone 4! -- http://plone.org/4

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html