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/internal/profiler"
27 "go.uber.org/automaxprocs/maxprocs"
28
29 "github.com/sourcegraph/zoekt/cmd"
30 "github.com/sourcegraph/zoekt/ctags"
31 "github.com/sourcegraph/zoekt/gitindex"
32)
33
34func run() int {
35 allowMissing := flag.Bool("allow_missing_branches", false, "allow missing branches.")
36 submodules := flag.Bool("submodules", true, "if set to false, do not recurse into submodules")
37 branchesStr := flag.String("branches", "HEAD", "git branches to index.")
38 branchPrefix := flag.String("prefix", "refs/heads/", "prefix for branch names")
39
40 incremental := flag.Bool("incremental", true, "only index changed repositories")
41 repoCacheDir := flag.String("repo_cache", "", "directory holding bare git repos, named by URL. "+
42 "this is used to find repositories for submodules. "+
43 "It also affects name if the indexed repository is under this directory.")
44 isDelta := flag.Bool("delta", false, "whether we should use delta build")
45 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)")
46 offlineRanking := flag.String("offline_ranking", "", "the name of the file that contains the ranking info.")
47 offlineRankingVersion := flag.String("offline_ranking_version", "", "a version string identifying the contents in offline_ranking.")
48 languageMap := flag.String("language_map", "", "a mapping between a language and its ctags processor (a:0,b:3).")
49
50 cpuProfile := flag.String("cpuprofile", "", "write cpu profile to `file`")
51
52 flag.Parse()
53
54 // Tune GOMAXPROCS to match Linux container CPU quota.
55 _, _ = maxprocs.Set()
56
57 if *cpuProfile != "" {
58 f, err := os.Create(*cpuProfile)
59 if err != nil {
60 log.Fatal("could not create CPU profile: ", err)
61 }
62 defer f.Close() // error handling omitted for example
63 if err := pprof.StartCPUProfile(f); err != nil {
64 log.Fatal("could not start CPU profile: ", err)
65 }
66 defer pprof.StopCPUProfile()
67 }
68
69 if *repoCacheDir != "" {
70 dir, err := filepath.Abs(*repoCacheDir)
71 if err != nil {
72 log.Fatalf("Abs: %v", err)
73 }
74 *repoCacheDir = dir
75 }
76
77 opts := cmd.OptionsFromFlags()
78 opts.IsDelta = *isDelta
79 opts.DocumentRanksPath = *offlineRanking
80 opts.DocumentRanksVersion = *offlineRankingVersion
81
82 var branches []string
83 if *branchesStr != "" {
84 branches = strings.Split(*branchesStr, ",")
85 }
86
87 gitRepos := map[string]string{}
88 for _, repoDir := range flag.Args() {
89 repoDir, err := filepath.Abs(repoDir)
90 if err != nil {
91 log.Fatal(err)
92 }
93 repoDir = filepath.Clean(repoDir)
94
95 name := strings.TrimSuffix(repoDir, "/.git")
96 if *repoCacheDir != "" && strings.HasPrefix(name, *repoCacheDir) {
97 name = strings.TrimPrefix(name, *repoCacheDir+"/")
98 name = strings.TrimSuffix(name, ".git")
99 } else {
100 name = strings.TrimSuffix(filepath.Base(name), ".git")
101 }
102 gitRepos[repoDir] = name
103 }
104
105 opts.LanguageMap = make(ctags.LanguageMap)
106 for _, mapping := range strings.Split(*languageMap, ",") {
107 m := strings.Split(mapping, ":")
108 if len(m) != 2 {
109 continue
110 }
111 opts.LanguageMap[m[0]] = ctags.StringToParser(m[1])
112 }
113
114 if heapProfileTrigger := os.Getenv("ZOEKT_HEAP_PROFILE_TRIGGER"); heapProfileTrigger != "" {
115 trigger, err := humanize.ParseBytes(heapProfileTrigger)
116 if err != nil {
117 log.Printf("invalid value for ZOEKT_HEAP_PROFILE_TRIGGER: %v", err)
118 } else {
119 opts.HeapProfileTriggerBytes = trigger
120 }
121 }
122
123 profiler.Init("zoekt-git-index")
124 exitStatus := 0
125 for dir, name := range gitRepos {
126 opts.RepositoryDescription.Name = name
127 gitOpts := gitindex.Options{
128 BranchPrefix: *branchPrefix,
129 Incremental: *incremental,
130 Submodules: *submodules,
131 RepoCacheDir: *repoCacheDir,
132 AllowMissingBranch: *allowMissing,
133 BuildOptions: *opts,
134 Branches: branches,
135 RepoDir: dir,
136 DeltaShardNumberFallbackThreshold: *deltaShardNumberFallbackThreshold,
137 }
138
139 if _, err := gitindex.IndexGitRepo(gitOpts); err != nil {
140 log.Printf("indexGitRepo(%s, delta=%t): %v", dir, gitOpts.BuildOptions.IsDelta, err)
141 exitStatus = 1
142 }
143 }
144
145 return exitStatus
146}
147
148func main() {
149 exitStatus := run()
150 os.Exit(exitStatus)
151}