Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Bug#428793: git add completion suggests files not under VCS control



On Thu, Jun 14, 2007 at 09:40:01AM +0100, martin f krafft wrote:
> In a repository where *.pyc files are ignored, adding a .py file,
> which has already been put under git's control, to a commit causes
> the completer to suggest the .pyc instead.
> 
> git has two uses for add, one to put a file under git control and
> the other to add files to a commit when they have changes since the
> last add or commit.
> 
> Here is the output of git status:
> 
> # On branch master
> # Changed but not updated:
> #   (use "git add <file>..." to update what will be committed)
> #
> # modified:   src/NetconfDaemon.py
> #
> # Untracked files:
> #   (use "git add <file>..." to include in what will be committed)
> #
> # debian/
> # src/head
> # src/ifupdown_cfg.py
> # src/interfaces
> # src/mapping.sh
> no changes added to commit (use "git add" and/or "git commit -a")
> 
> 
> 
> the NetconfDaemon.py file is known to git but not part of the
> commit. The other files are unknown to git.
> 
> Now, when in the src/ directory, typing
> 
>   git add Netconf<type>
> 
> will yield NetconfDaemon.pyc.

"Cherry-picked" from Nikolai Weibull's repo mentioned in 23558.

Index: Completion/Unix/Command/_git
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/Unix/Command/_git,v
retrieving revision 1.3
diff -u -r1.3 _git
--- Completion/Unix/Command/_git	13 Jun 2007 16:40:07 -0000	1.3
+++ Completion/Unix/Command/_git	14 Jun 2007 13:53:40 -0000
@@ -624,10 +624,31 @@
 }
 
 _git-add () {
-  _arguments \
+  local curcontext=$curcontext state line
+  declare -A opt_args
+
+  _arguments -C -S \
     '-n[do not actually add files; only show which ones would be added]' \
     '-v[show files as they are added]' \
-    '*:file:_files -g "*(^e:__git_is_indexed:)"' && ret=0
+    '-f[allow adding otherwise ignored files]' \
+    '(-i --interactive)'{-i,--interactive}'[add contents interactively to the index]' \
+    '*:file:->files' && ret=0
+
+  case $state in
+    (files)
+      declare -a ignored_files_alternatives
+      if (( words[(I)-f] )); then
+        ignored_files_alternatives=(
+          'ignored-modified-files:ignored modified files:__git_modified_files --ignored'
+          'ignored-other-files:ignored other files:__git_other_files --ignored')
+      fi
+
+      _alternative \
+        'modified-files:modified files:__git_modified_files' \
+        'other-files:other files:__git_other_files' \
+        $ignored_files_alternatives && ret=0
+      ;;
+  esac
 }
 
 _git-am () {
@@ -1138,6 +1159,14 @@
   [[ -n "$PREFIX$SUFFIX" ]]
 }
 
+__git_command_successful () {
+  if (( ${#pipestatus:#0} > 0 )); then
+    _message 'not a git repository'
+    return 1
+  fi
+  return 0
+}
+
 __git_objects () {
   __git_guard $* "[[:xdigit:]]#" "object"
 }
@@ -1159,14 +1188,28 @@
 }
 
 __git_files () {
-  local expl files
+  local expl files ls_opts opts gitdir
 
-  files=("${(@f)$(git-ls-files 2>/dev/null)}")
-  if (( $? == 0 )); then
-    _wanted files expl 'index file' _multi_parts $@ - / files
-  else
-    _message 'not a git repository'
-  fi
+  zparseopts -D -E -a opts -- -cached -deleted -modified -others -ignored -unmerged -killed
+
+  gitdir=$(_call_program gitdir git rev-parse --git-dir 2>/dev/null)
+  __git_command_successful || return
+
+  ls_opts=("--exclude-per-directory=.gitignore")
+  [[ -f "$gitdir/info/exclude" ]] && ls_opts+="--exclude-from=$gitdir/info/exclude"
+
+  files=(${(ps:\0:)"$(_call_program files git ls-files -z $ls_opts $opts 2>/dev/null)"})
+  __git_command_successful || return
+
+  _wanted files expl 'index file' _multi_parts $@ - / files
+}
+
+__git_modified_files () {
+  __git_files $* --modified
+}
+
+__git_other_files () {
+  __git_files $* --others
 }
 
 __git_tree_files () {



Messages sorted by: Reverse Date, Date, Thread, Author