mattn / jvgrep

grep for japanese vimmer
https://mattn.kaoriya.net/
140 stars 19 forks source link

"jvgrep 'hub\.' **/*" doesn't match in github/hub repository? #47

Closed tyru closed 8 years ago

tyru commented 8 years ago

In github/hub repository (current HEAD is https://github.com/github/hub/commit/aa4611dd9add76b1facda9906fcdf8078a2e89e6) jvgrep 'hub\.' **/* doesn't match the following line.

./github/project.go:101:                userProtocol, _ = git.Config("hub.protocol")

jvgrep -R 'hub\.' . is also the same result.

tyru commented 8 years ago

I also noticed jvgrep {pattern} **/* and jvgrep -R {pattern} . outputs differ. Is this intentional?

diff -u <(jvgrep -r --exclude "" '\bhub\.\w+' **/*) <(jvgrep -R -r --exclude "" '\bhub\.\w+' .)

--- /dev/fd/63  2016-03-19 03:24:29.117780487 +0900
+++ /dev/fd/62  2016-03-19 03:24:29.117780487 +0900
@@ -1,3 +1,12 @@
+.gitignore:6:man/hub.1
+.gitignore:7:man/hub.1.html
+.gitignore:8:man/hub.1.txt
+CONTRIBUTING.md:33:    `git clone https://github.com/github/hub.git && cd hub`
+Makefile:28:HELP_ALL = man/hub.1 $(HELP_CMD) $(HELP_EXT)
+Makefile:63:man/hub.1.ronn:
+README.md:47:$ git clone https://github.com/github/hub.git
+README.md:75:* [hub bash completion](https://github.com/github/hub/blob/master/etc/hub.bash_completion.sh)
+README.md:76:* [hub zsh completion](https://github.com/github/hub/blob/master/etc/hub.zsh_completion)
 commands/help.go:68:           err := displayManPage("hub.1", args)
 commands/updater.go:25:        hubAutoUpdateConfig = "hub.autoUpdate"
 etc/README.md:12:if [ -f /path/to/hub.bash_completion ]; then

You know, **/* for :vimgrep matches current directory's file like README.md in above example. (if you follow the :vimgrep specification)

mattn commented 8 years ago

Seems to be a bug of isLiteralRegexp. @koron could you please handle this?

mattn commented 8 years ago

@koron how about this?

diff --git a/jvgrep.go b/jvgrep.go
index 6044eb8..66e74ab 100644
--- a/jvgrep.go
+++ b/jvgrep.go
@@ -1050,7 +1050,7 @@ func isLiteralRegexp(expr string, flags syntax.Flags) bool {
    if err != nil {
        return false
    }
-   if re.Op == syntax.OpLiteral && re.Flags&syntax.FoldCase == 0 {
+   if re.Op&syntax.OpLiteral == 0 && re.Flags&syntax.FoldCase == 0 {
        return true
    }
    return false
mattn commented 8 years ago

いったん isLiteralRegexp は無効化しました。テスト追加後に復活させましょう。

mattn commented 8 years ago

これでいい気がする。

diff --git a/jvgrep.go b/jvgrep.go
index ef253a4..c24f6de 100644
--- a/jvgrep.go
+++ b/jvgrep.go
@@ -790,7 +790,7 @@ func main() {
        if ignorecase {
            instr = "(?i:" + instr + ")"
        }
-       if isLiteralRegexp(instr, syntax.Perl) {
+       if isLiteralRegexp(instr) {
            if verbose {
                println("pattern treated as literal:", instr)
            }
@@ -806,7 +806,7 @@ func main() {
        if ignorecase {
            instr = "(?i:" + instr + ")"
        }
-       if isLiteralRegexp(instr, syntax.POSIX) {
+       if isLiteralRegexp(instr) {
            if verbose {
                println("pattern treated as literal:", instr)
            }
@@ -1045,17 +1045,6 @@ func main() {
 }

 // isLiteralRegexp checks regexp is a simple literal or not.
-func isLiteralRegexp(expr string, flags syntax.Flags) bool {
-   return false
-   /*
-       FIXME
-       re, err := syntax.Parse(expr, flags)
-       if err != nil {
-           return false
-       }
-       if re.Op&syntax.OpLiteral == 0 && re.Flags&syntax.FoldCase == 0 {
-           return true
-       }
-       return false
-   */
+func isLiteralRegexp(expr string) bool {
+   return regexp.QuoteMeta(expr) == expr
 }
tyru commented 8 years ago

FYI, now I can confirm ./github/project.go:101 is included in the result output at current HEAD. Thanks :)

$ jvgrep -r 'hub\.' **/* | grep project.go
github/project.go:10:   "github.com/github/hub/git"
github/project.go:11:   "github.com/github/hub/utils"
github/project.go:101:          userProtocol, _ = git.Config("hub.protocol")
github/project.go:146:  if host == "ssh.github.com" {
github/project.go:147:          host = "github.com"
mattn commented 8 years ago

Fixed

https://github.com/mattn/jvgrep/commit/2ada5131329e03628de6d63e4737c4269755d5a6 https://github.com/mattn/jvgrep/commit/ff97e7a7200e22a8ae9c410ad1ea130cceb4f4dd

tyru commented 8 years ago

Thanks!