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
15// Command zoekt-git-index indexes a single git repository. It works directly with git
16// repositories and supports git-specific features like branches and submodules.
17package main
18
19import (
20 "flag"
21 "log"
22 "os"
23 "path/filepath"
24 "runtime/pprof"
25 "strings"
26
27 "github.com/dustin/go-humanize"
28 "go.uber.org/automaxprocs/maxprocs"
29
30 "github.com/sourcegraph/zoekt/cmd"
31 "github.com/sourcegraph/zoekt/internal/ctags"
32 "github.com/sourcegraph/zoekt/internal/gitindex"
33 "github.com/sourcegraph/zoekt/internal/profiler"
34)
35
36func run() int {
37 allowMissing := flag.Bool("allow_missing_branches", false, "allow missing branches.")
38 submodules := flag.Bool("submodules", true, "if set to false, do not recurse into submodules")
39 branchesStr := flag.String("branches", "HEAD", "git branches to index.")
40 branchPrefix := flag.String("prefix", "refs/heads/", "prefix for branch names")
41
42 incremental := flag.Bool("incremental", true, "only index changed repositories")
43 repoCacheDir := flag.String("repo_cache", "", "directory holding bare git repos, named by URL. "+
44 "this is used to find repositories for submodules. "+
45 "It also affects name if the indexed repository is under this directory.")
46 isDelta := flag.Bool("delta", false, "whether we should use delta build")
47 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)")
48 languageMap := flag.String("language_map", "", "a mapping between a language and its ctags processor (a:0,b:3).")
49
50 cpuProfile := flag.String("cpu_profile", "", "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
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 if heapProfileTrigger := os.Getenv("ZOEKT_HEAP_PROFILE_TRIGGER"); heapProfileTrigger != "" {
113 trigger, err := humanize.ParseBytes(heapProfileTrigger)
114 if err != nil {
115 log.Printf("invalid value for ZOEKT_HEAP_PROFILE_TRIGGER: %v", err)
116 } else {
117 opts.HeapProfileTriggerBytes = trigger
118 }
119 }
120
121 profiler.Init("zoekt-git-index")
122 exitStatus := 0
123 for dir, name := range gitRepos {
124 opts.RepositoryDescription.Name = name
125 gitOpts := gitindex.Options{
126 BranchPrefix: *branchPrefix,
127 Incremental: *incremental,
128 Submodules: *submodules,
129 RepoCacheDir: *repoCacheDir,
130 AllowMissingBranch: *allowMissing,
131 BuildOptions: *opts,
132 Branches: branches,
133 RepoDir: dir,
134 DeltaShardNumberFallbackThreshold: *deltaShardNumberFallbackThreshold,
135 }
136
137 if _, err := gitindex.IndexGitRepo(gitOpts); err != nil {
138 log.Printf("indexGitRepo(%s, delta=%t): %v", dir, gitOpts.BuildOptions.IsDelta, err)
139 exitStatus = 1
140 }
141 }
142
143 return exitStatus
144}
145
146func main() {
147 exitStatus := run()
148 os.Exit(exitStatus)
149}