jiri grep should support GNU pattern --
TO-391
Change-Id: I8250df8cd5ba1ec144cd5283ba7b5bd13f188964
diff --git a/cmd/jiri/grep.go b/cmd/jiri/grep.go
index 2f37422..2b4f350 100644
--- a/cmd/jiri/grep.go
+++ b/cmd/jiri/grep.go
@@ -6,6 +6,7 @@
import (
"fmt"
+ "os"
"path/filepath"
"fuchsia.googlesource.com/jiri"
@@ -21,7 +22,7 @@
Long: `
Run git grep across all projects.
`,
- ArgsName: "<query>",
+ ArgsName: "<query> [--] [<pathspec>...]",
}
var grepFlags struct {
@@ -49,9 +50,32 @@
}
func doGrep(jirix *jiri.X, args []string) ([]string, error) {
- if grepFlags.e != "" && len(args) > 0 {
+ var pathSpecs []string
+ lenArgs := len(args)
+ if lenArgs > 0 {
+ for i, a := range os.Args {
+ if a == "--" {
+ pathSpecs = os.Args[i+1:]
+ break
+ }
+ }
+ // we will not find -- if user uses something like jiri grep -- a b,
+ // as flag.Parse() removes '--' in that case, so set args length
+ lenArgs = len(args) - len(pathSpecs)
+ for i, a := range args {
+
+ if a == "--" {
+ args = args[0:i]
+ // reset length
+ lenArgs = len(args)
+ break
+ }
+ }
+ }
+
+ if grepFlags.e != "" && lenArgs > 0 {
return nil, jirix.UsageErrorf("No additional argument allowed with flag -e")
- } else if grepFlags.e == "" && len(args) != 1 {
+ } else if grepFlags.e == "" && lenArgs != 1 {
return nil, jirix.UsageErrorf("grep requires one argument")
}
@@ -68,7 +92,7 @@
flags = append(flags, "--color=always")
}
query := ""
- if len(args) == 1 {
+ if lenArgs == 1 {
query = args[0]
}
for _, project := range projects {
@@ -77,7 +101,7 @@
return nil, err
}
git := gitutil.New(jirix, gitutil.RootDirOpt(project.Path))
- lines, err := git.Grep(query, flags...)
+ lines, err := git.Grep(query, pathSpecs, flags...)
if err != nil {
continue
}
diff --git a/gitutil/git.go b/gitutil/git.go
index 88f23d0..ad51338 100644
--- a/gitutil/git.go
+++ b/gitutil/git.go
@@ -585,11 +585,15 @@
// Grep searches for matching text and returns a list of lines from
// `git grep`.
-func (g *Git) Grep(query string, flags ...string) ([]string, error) {
+func (g *Git) Grep(query string, pathSpecs []string, flags ...string) ([]string, error) {
args := append([]string{"grep"}, flags...)
if query != "" {
args = append(args, query)
}
+ if len(pathSpecs) != 0 {
+ args = append(args, "--")
+ args = append(args, pathSpecs...)
+ }
// TODO(ianloic): handle patterns that start with "-"
// TODO(ianloic): handle different pattern types (-i, -P, -E, etc)
// TODO(ianloic): handle different response types (--full-name, -v, --name-only, etc)