zsh-workers
 help / color / mirror / code / Atom feed
From: Clint Adams <clint@zsh.org>
To: zsh-workers@sunsite.dk
Subject: PATCH: _git grep
Date: Sun, 24 Jun 2007 11:13:55 -0400	[thread overview]
Message-ID: <20070624151355.GA2293@scowler.net> (raw)

git grep from Nikolai Weibull's repo.

Index: Completion/Unix/Command/_git
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/Unix/Command/_git,v
retrieving revision 1.5
diff -u -r1.5 _git
--- Completion/Unix/Command/_git	24 Jun 2007 14:58:16 -0000	1.5
+++ Completion/Unix/Command/_git	24 Jun 2007 15:11:50 -0000
@@ -829,10 +829,80 @@
     '::my revision:__git_revisions' && ret=0
 }
 
-# TODO: something better
 _git-grep () {
-  service=grep _grep
-  ret=0
+  local -a pattern_operators
+
+  if (( words[(I)-e] == CURRENT - 2 )); then
+    pattern_operators=(
+      '--and[both patterns must match]'
+      '--or[either pattern must match]'
+      '--not[the following pattern must not match]')
+  fi
+
+  local curcontext=$curcontext state line
+  declare -A opt_args
+
+  _arguments -A '--*' \
+    '--cached[grep blobs registered in index file instead of working tree]' \
+    '(-a --text)'{-a,--text}'[process binary files as if they were text]' \
+    '(-i --ignore-case)'{-i,--ignore-case}'[ignore case when matching]' \
+    '(-w --word-regexp)'{-w,--word-regexp}'[match only whole words]' \
+    '(-v --invert-match)'{-v,--invert-match}'[select non-matching lines]' \
+    '(   -H)-h[supress output of filenames]' \
+    '(-h   )-H[show filenames]' \
+    '--full-name[output paths relative to the project top directory]' \
+    '(-E --extended-regexp -G --basic-regexp)'{-E,--extended-regexp}'[use POSIX extended regexes]' \
+    '(-E --extended-regexp -G --basic-regexp)'{-G,--basic-regexp}'[use POSIX basic regexes]' \
+    '-n[prefix the line number to matching lines]' \
+    '(-l --files-with-matches -L --files-without-match)'{-l,--files-with-match}'[show only names of matching files]' \
+    '(-l --files-with-matches -L --files-without-match)'{-L,--files-without-match}'[show only names of non-matching files]' \
+    '(-c --count)'{-c,--count}'[show number of matching lines in files]' \
+    '-A[show trailing context]: :_guard "[[\:digit\:]]#" lines' \
+    '-B[show leading context]: :_guard "[[\:digit\:]]#" lines' \
+    '-C[show context]: :_guard "[[\:digit\:]]#" lines' \
+    '(1)*-f[read patterns from given file]:pattern file:_files' \
+    '(1)*-e[use the given pattern for matching]:pattern' \
+    $pattern_operators \
+    '--all-match[all patterns must match]' \
+    ':pattern:' \
+    '*::tree-or-file:->files' && ret=0
+  
+  case $state in
+    (files)
+      integer first_tree last_tree start end
+
+      (( start = words[(I)(-f|-e)] > 0 ? 1 : 2 ))
+      (( end = $#line - 1 ))
+
+      for (( i = start; i <= end; i++ )); do
+        [[ line[i] == '--' ]] && break
+        git cat-file -e "${(Q)line[i]}^{tree}" 2>/dev/null || break
+        if (( first_tree == 0 )); then
+          (( first_tree = last_tree = i ))
+        else
+          (( last_tree = i ))
+        fi
+      done
+
+      if (( last_tree == 0 || last_tree == end )); then
+        if (( first_tree == 0 )); then
+          _alternative \
+            'tree:tree:__git_trees' \
+            "file:file:__git_cached_files" && ret=0
+        else
+          _alternative \
+            'tree:tree:__git_trees' \
+            "tree file:tree-files:__git_tree_files $line[first_tree,last_tree]" && ret=0
+        fi
+      else
+        if (( first_tree == 0 )); then
+          __git_cached_files
+        else
+          __git_tree_files $line[first_tree,last_tree]
+        fi
+      fi
+      ;;
+  esac
 }
 
 # TODO: this isn't strictly right, but close enough


                 reply	other threads:[~2007-06-24 15:14 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20070624151355.GA2293@scowler.net \
    --to=clint@zsh.org \
    --cc=zsh-workers@sunsite.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).