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