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
|
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"runtime/pprof"
"sort"
"github.com/google/codesearch/index"
)
var usageMessage = `usage: cindex [-list] [-reset] [path...]
Cindex prepares the trigram index for use by csearch. The index is the
file named by $CSEARCHINDEX, or else $HOME/.csearchindex.
The simplest invocation is
cindex path...
which adds the file or directory tree named by each path to the index.
For example:
cindex $HOME/src /usr/include
or, equivalently:
cindex $HOME/src
cindex /usr/include
If cindex is invoked with no paths, it reindexes the paths that have
already been added, in case the files have changed. Thus, 'cindex' by
itself is a useful command to run in a nightly cron job.
The -list flag causes cindex to list the paths it has indexed and exit.
By default cindex adds the named paths to the index but preserves
information about other paths that might already be indexed
(the ones printed by cindex -list). The -reset flag causes cindex to
delete the existing index before indexing the new paths.
With no path arguments, cindex -reset removes the index.
`
func usage() {
fmt.Fprintf(os.Stderr, usageMessage)
os.Exit(2)
}
var (
listFlag = flag.Bool("list", false, "list indexed paths and exit")
resetFlag = flag.Bool("reset", false, "discard existing index")
verboseFlag = flag.Bool("verbose", false, "print extra information")
cpuProfile = flag.String("cpuprofile", "", "write cpu profile to this file")
)
func main() {
flag.Usage = usage
flag.Parse()
args := flag.Args()
if *listFlag {
ix := index.Open(index.File())
for _, arg := range ix.Paths() {
fmt.Printf("%s\n", arg)
}
return
}
if *cpuProfile != "" {
f, err := os.Create(*cpuProfile)
if err != nil {
log.Fatal(err)
}
defer f.Close()
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
if *resetFlag && len(args) == 0 {
os.Remove(index.File())
return
}
if len(args) == 0 {
ix := index.Open(index.File())
for _, arg := range ix.Paths() {
args = append(args, arg)
}
}
// Translate paths to absolute paths so that we can
// generate the file list in sorted order.
for i, arg := range args {
a, err := filepath.Abs(arg)
if err != nil {
log.Printf("%s: %s", arg, err)
args[i] = ""
continue
}
args[i] = a
}
sort.Strings(args)
for len(args) > 0 && args[0] == "" {
args = args[1:]
}
master := index.File()
if _, err := os.Stat(master); err != nil {
// Does not exist.
*resetFlag = true
}
file := master
if !*resetFlag {
file += "~"
}
ix := index.Create(file)
ix.Verbose = *verboseFlag
ix.AddPaths(args)
for _, arg := range args {
log.Printf("index %s", arg)
filepath.Walk(arg, func(path string, info os.FileInfo, err error) error {
if _, elem := filepath.Split(path); elem != "" {
// Skip various temporary or "hidden" files or directories.
if elem[0] == '.' || elem[0] == '#' || elem[0] == '~' || elem[len(elem)-1] == '~' {
if info.IsDir() {
return filepath.SkipDir
}
return nil
}
}
if err != nil {
log.Printf("%s: %s", path, err)
return nil
}
if info != nil && info.Mode()&os.ModeType == 0 {
ix.AddFile(path)
}
return nil
})
}
log.Printf("flush index")
ix.Flush()
if !*resetFlag {
log.Printf("merge %s %s", master, file)
index.Merge(file+"~", master, file)
os.Remove(file)
os.Rename(file+"~", master)
}
log.Printf("done")
return
}
|