fork of https://github.com/sourcegraph/zoekt
1// Copyright 2016 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package main
16
17import (
18 "flag"
19 "log"
20 "os"
21 "path/filepath"
22 "runtime/pprof"
23 "strings"
24
25 "github.com/dustin/go-humanize"
26 "github.com/sourcegraph/zoekt/cmd"
27 "github.com/sourcegraph/zoekt/internal/ctags"
28 "github.com/sourcegraph/zoekt/internal/gitindex"
29 "github.com/sourcegraph/zoekt/internal/profiler"
30 "go.uber.org/automaxprocs/maxprocs"
31)
32
33func run() int {
34 allowMissing := flag.Bool("allow_missing_branches", false, "allow missing branches.")
35 submodules := flag.Bool("submodules", true, "if set to false, do not recurse into submodules")
36 branchesStr := flag.String("branches", "HEAD", "git branches to index.")
37 branchPrefix := flag.String("prefix", "refs/heads/", "prefix for branch names")
38
39 incremental := flag.Bool("incremental", true, "only index changed repositories")
40 repoCacheDir := flag.String("repo_cache", "", "directory holding bare git repos, named by URL. "+
41 "this is used to find repositories for submodules. "+
42 "It also affects name if the indexed repository is under this directory.")
43 isDelta := flag.Bool("delta", false, "whether we should use delta build")
44 deltaShardNumberFallbackThreshold := flag.Uint64("delta_threshold", 0, "upper limit on the number of preexisting shards that can exist before attempting a delta build (0 to disable fallback behavior)")
45 languageMap := flag.String("language_map", "", "a mapping between a language and its ctags processor (a:0,b:3).")
46
47 cpuProfile := flag.String("cpu_profile", "", "write cpu profile to `file`")
48
49 flag.Parse()
50
51 // Tune GOMAXPROCS to match Linux container CPU quota.
52 _, _ = maxprocs.Set()
53
54 if *cpuProfile != "" {
55 f, err := os.Create(*cpuProfile)
56 if err != nil {
57 log.Fatal("could not create CPU profile: ", err)
58 }
59 defer f.Close() // error handling omitted for example
60 if err := pprof.StartCPUProfile(f); err != nil {
61 log.Fatal("could not start CPU profile: ", err)
62 }
63 defer pprof.StopCPUProfile()
64 }
65
66 if *repoCacheDir != "" {
67 dir, err := filepath.Abs(*repoCacheDir)
68 if err != nil {
69 log.Fatalf("Abs: %v", err)
70 }
71 *repoCacheDir = dir
72 }
73
74 opts := cmd.OptionsFromFlags()
75 opts.IsDelta = *isDelta
76
77 var branches []string
78 if *branchesStr != "" {
79 branches = strings.Split(*branchesStr, ",")
80 }
81
82 gitRepos := map[string]string{}
83 for _, repoDir := range flag.Args() {
84 repoDir, err := filepath.Abs(repoDir)
85 if err != nil {
86 log.Fatal(err)
87 }
88 repoDir = filepath.Clean(repoDir)
89
90 name := strings.TrimSuffix(repoDir, "/.git")
91 if *repoCacheDir != "" && strings.HasPrefix(name, *repoCacheDir) {
92 name = strings.TrimPrefix(name, *repoCacheDir+"/")
93 name = strings.TrimSuffix(name, ".git")
94 } else {
95 name = strings.TrimSuffix(filepath.Base(name), ".git")
96 }
97 gitRepos[repoDir] = name
98 }
99
100 opts.LanguageMap = make(ctags.LanguageMap)
101 for _, mapping := range strings.Split(*languageMap, ",") {
102 m := strings.Split(mapping, ":")
103 if len(m) != 2 {
104 continue
105 }
106 opts.LanguageMap[m[0]] = ctags.StringToParser(m[1])
107 }
108
109 if heapProfileTrigger := os.Getenv("ZOEKT_HEAP_PROFILE_TRIGGER"); heapProfileTrigger != "" {
110 trigger, err := humanize.ParseBytes(heapProfileTrigger)
111 if err != nil {
112 log.Printf("invalid value for ZOEKT_HEAP_PROFILE_TRIGGER: %v", err)
113 } else {
114 opts.HeapProfileTriggerBytes = trigger
115 }
116 }
117
118 profiler.Init("zoekt-git-index")
119 exitStatus := 0
120 for dir, name := range gitRepos {
121 opts.RepositoryDescription.Name = name
122 gitOpts := gitindex.Options{
123 BranchPrefix: *branchPrefix,
124 Incremental: *incremental,
125 Submodules: *submodules,
126 RepoCacheDir: *repoCacheDir,
127 AllowMissingBranch: *allowMissing,
128 BuildOptions: *opts,
129 Branches: branches,
130 RepoDir: dir,
131 DeltaShardNumberFallbackThreshold: *deltaShardNumberFallbackThreshold,
132 }
133
134 if _, err := gitindex.IndexGitRepo(gitOpts); err != nil {
135 log.Printf("indexGitRepo(%s, delta=%t): %v", dir, gitOpts.BuildOptions.IsDelta, err)
136 exitStatus = 1
137 }
138 }
139
140 return exitStatus
141}
142
143func main() {
144 exitStatus := run()
145 os.Exit(exitStatus)
146}