-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsearcher.go
206 lines (186 loc) · 4.87 KB
/
searcher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package main
import (
//"github.com/howeyc/fsnotify"
"flag"
"fmt"
"github.com/rliebling/codesearch/index"
"github.com/rliebling/codesearch/regexp"
"github.com/rliebling/terminal"
"io"
"log"
"os"
"os/exec"
std_regexp "regexp"
"strings"
"syscall"
)
type fileset map[string]bool
var (
fileFilterFlag = flag.String("f", "", "search only files with names matching this regexp")
fileExclusionFlag = flag.String("F", "", "search excluding files with names matching this regexp")
iFlag = flag.Bool("i", false, "case-insensitive search")
nameOnlyFlag = flag.Bool("l", false, "only print filenames that match")
colorFlag = flag.Bool("color", true, "show results with coloring")
terminalFlag = flag.Bool("terminal", true, "treat as if going to human reader")
indexFlag = flag.Bool("index", false, "create index")
watchFlag = flag.Bool("watch", false, "watch for changes")
indexFilename = flag.String("file", ".cindex", "index filename")
verboseFlag = flag.Bool("verbose", false, "print extra information")
cpuProfile = flag.String("cpuprofile", "", "write cpu profile to this file")
)
func main() {
flag.Parse()
if len(os.Args) < 2 {
flag.PrintDefaults()
return
}
if *watchFlag {
createIndex(".")
watch(".")
} else if *indexFlag {
createIndex(".")
} else {
search(flag.Args()...)
}
}
func search(args ...string) {
var stdout io.WriteCloser
var err error
is_terminal := *terminalFlag && terminal.IsTerminal(syscall.Stdout)
if !is_terminal {
*colorFlag = false
}
if *colorFlag && strings.HasPrefix(os.Getenv("OS"), "Windows") {
cmd := exec.Command("ruby", "-rubygems", "-rwin32console", "-e", "puts STDIN.readlines")
cmd.Stdout = os.Stdout
stdout, err = cmd.StdinPipe()
cmd.Start()
defer cmd.Wait()
defer stdout.Close()
} else {
stdout = os.Stdout
}
pat := "(?m)" + args[0]
if *iFlag {
pat = "(?i)" + pat
}
re, err := regexp.Compile(pat)
if err != nil {
log.Fatal(err)
}
g := Grepper{}
if is_terminal {
if *colorFlag {
g.MatchCallback = func(name string) {
fmt.Fprintf(stdout, "\033[1;31m%s\033[0m\n", name)
}
if !*nameOnlyFlag {
std_re, _ := std_regexp.Compile(pat)
g.LineCallback = func(name, line string, line_number int) {
// nuke EOL and wrap with coloring
eol := len(line)
if line[eol-1:eol] == "\n" {
eol = eol - 1
}
result := std_re.ReplaceAllString(line[:eol], "\033[1;37m\033[1;41m$0\033[0m")
fmt.Fprintf(stdout, "%d|\t%s\n", line_number, result)
}
}
} else {
g.MatchCallback = func(name string) {
fmt.Fprintf(stdout, "%s\n", name)
}
if !*nameOnlyFlag {
g.LineCallback = func(name, line string, line_number int) {
fmt.Fprintf(stdout, "%d|\t%s\n", line_number, line[:len(line)-1])
}
}
}
} else {
if *nameOnlyFlag {
g.MatchCallback = func(name string) {
fmt.Fprintf(stdout, "%s\n", name)
}
} else {
g.LineCallback = func(name, line string, line_number int) {
fmt.Fprintf(stdout, "%s:%d: %s\n", name, line_number, line[:len(line)-1])
}
}
}
g.Regexp = re
var fre, fexclusion_re *regexp.Regexp
if *fileFilterFlag != "" {
fre, err = regexp.Compile(*fileFilterFlag)
if err != nil {
log.Fatal(err)
}
}
if *fileExclusionFlag != "" {
fexclusion_re, err = regexp.Compile(*fileExclusionFlag)
if err != nil {
log.Fatal(err)
}
}
q := index.RegexpQuery(re.Syntax)
if *verboseFlag {
log.Printf("query: %s\n", q)
}
*indexFilename = findIndexFile(*indexFilename)
if !exists(*indexFilename) {
log.Fatalf("Could not find %s", *indexFilename)
}
ix := index.Open(*indexFilename)
ix.Verbose = *verboseFlag
var post []uint32
post = ix.PostingQuery(q)
if *verboseFlag {
log.Printf("post query identified %d possible files\n", len(post))
}
if fre != nil || fexclusion_re != nil {
fnames := make([]uint32, 0, len(post))
for _, fileid := range post {
name := ix.Name(fileid)
if fre != nil && fre.MatchString(name, true, true) < 0 {
continue
}
if fexclusion_re != nil && fexclusion_re.MatchString(name, true, true) >= 0 {
continue
}
fnames = append(fnames, fileid)
}
if *verboseFlag {
log.Printf("filename regexp matched %d files\n", len(fnames))
}
post = fnames
}
for _, fileid := range post {
name := ix.Name(fileid)
g.File(name)
}
//matches = g.Match
}
func findIndexFile(indexFileName string) string {
workingDirectory, _ := os.Getwd()
searchDepth := strings.Count(workingDirectory, string(os.PathSeparator))
searchPath := indexFileName
for depth := 0; depth < searchDepth; depth++ {
if exists(searchPath) {
if *verboseFlag {
log.Printf("Found .cindex in %s", searchPath)
}
return searchPath
}
searchPath = "../" + searchPath
}
return indexFileName
}
func exists(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
}
if os.IsNotExist(err) {
return false
}
return false
}