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// Package gitindex provides functions for indexing Git repositories.
16package gitindex
17
18import (
19 "bytes"
20 "cmp"
21 "context"
22 "errors"
23 "fmt"
24 "io"
25 "log"
26 "math"
27 "net/url"
28 "os"
29 "path/filepath"
30 "regexp"
31 "sort"
32 "strconv"
33 "strings"
34
35 "github.com/go-git/go-billy/v5/osfs"
36 "github.com/go-git/go-git/v5/config"
37 "github.com/go-git/go-git/v5/plumbing"
38 "github.com/go-git/go-git/v5/plumbing/cache"
39 "github.com/go-git/go-git/v5/plumbing/object"
40 "github.com/go-git/go-git/v5/storage/filesystem"
41
42 "github.com/sourcegraph/zoekt"
43 "github.com/sourcegraph/zoekt/ignore"
44 "github.com/sourcegraph/zoekt/index"
45
46 git "github.com/go-git/go-git/v5"
47)
48
49// FindGitRepos finds directories holding git repositories below the
50// given directory. It will find both bare and the ".git" dirs in
51// non-bare repositories. It returns the full path including the dir
52// passed in.
53func FindGitRepos(dir string) ([]string, error) {
54 arg, err := filepath.Abs(dir)
55 if err != nil {
56 return nil, err
57 }
58 var dirs []string
59 if err := filepath.Walk(arg, func(name string, fi os.FileInfo, err error) error {
60 // Best-effort, ignore filepath.Walk failing
61 if err != nil {
62 return nil
63 }
64
65 if fi, err := os.Lstat(filepath.Join(name, ".git")); err == nil && fi.IsDir() {
66 dirs = append(dirs, filepath.Join(name, ".git"))
67 return filepath.SkipDir
68 }
69
70 if !strings.HasSuffix(name, ".git") || !fi.IsDir() {
71 return nil
72 }
73
74 fi, err = os.Lstat(filepath.Join(name, "objects"))
75 if err != nil || !fi.IsDir() {
76 return nil
77 }
78
79 dirs = append(dirs, name)
80 return filepath.SkipDir
81 }); err != nil {
82 return nil, err
83 }
84
85 return dirs, nil
86}
87
88// setTemplates fills in URL templates for known git hosting
89// sites.
90func setTemplates(repo *zoekt.Repository, u *url.URL, typ string) error {
91 if u.Scheme == "ssh+git" {
92 u.Scheme = "https"
93 u.User = nil
94 }
95
96 // helper to generate u.JoinPath as a template
97 varVersion := ".Version"
98 varPath := ".Path"
99 urlJoinPath := func(elem ...string) string {
100 elem = append([]string{u.String()}, elem...)
101 var parts []string
102 for _, e := range elem {
103 if e == varVersion || e == varPath {
104 parts = append(parts, e)
105 } else {
106 parts = append(parts, strconv.Quote(e))
107 }
108 }
109 return fmt.Sprintf("{{URLJoinPath %s}}", strings.Join(parts, " "))
110 }
111
112 repo.URL = u.String()
113 switch typ {
114 case "gitiles":
115 // eg. https://gerrit.googlesource.com/gitiles/+/master/tools/run_dev.sh#20
116 repo.CommitURLTemplate = urlJoinPath("+", varVersion)
117 repo.FileURLTemplate = urlJoinPath("+", varVersion, varPath)
118 repo.LineFragmentTemplate = "#{{.LineNumber}}"
119 case "github":
120 // eg. https://github.com/hanwen/go-fuse/blob/notify/genversion.sh#L10
121 repo.CommitURLTemplate = urlJoinPath("commit", varVersion)
122 repo.FileURLTemplate = urlJoinPath("blob", varVersion, varPath)
123 repo.LineFragmentTemplate = "#L{{.LineNumber}}"
124 case "cgit":
125 // http://git.savannah.gnu.org/cgit/lilypond.git/tree/elisp/lilypond-mode.el?h=dev/philh&id=b2ca0fefe3018477aaca23b6f672c7199ba5238e#n100
126 repo.CommitURLTemplate = urlJoinPath("commit") + "/?id={{.Version}}"
127 repo.FileURLTemplate = urlJoinPath("tree", varPath) + "/?id={{.Version}}"
128 repo.LineFragmentTemplate = "#n{{.LineNumber}}"
129 case "gitweb":
130 // https://gerrit.libreoffice.org/gitweb?p=online.git;a=blob;f=Makefile.am;h=cfcfd7c36fbae10e269653dc57a9b68c92d4c10b;hb=848145503bf7b98ce4a4aa0a858a0d71dd0dbb26#l10
131 repo.FileURLTemplate = u.String() + ";a=blob;f={{.Path}};hb={{.Version}}"
132 repo.CommitURLTemplate = u.String() + ";a=commit;h={{.Version}}"
133 repo.LineFragmentTemplate = "#l{{.LineNumber}}"
134 case "source.bazel.build":
135 // https://source.bazel.build/bazel/+/57bc201346e61c62a921c1cbf32ad24f185c10c9
136 // https://source.bazel.build/bazel/+/57bc201346e61c62a921c1cbf32ad24f185c10c9:tools/cpp/BUILD.empty;l=10
137 repo.CommitURLTemplate = u.String() + "/%2B/{{.Version}}"
138 repo.FileURLTemplate = u.String() + "/%2B/{{.Version}}:{{.Path}}"
139 repo.LineFragmentTemplate = ";l={{.LineNumber}}"
140 case "bitbucket-server":
141 // https://<bitbucketserver-host>/projects/<project>/repos/<repo>/commits/5be7ca73b898bf17a08e607918accfdeafe1e0bc
142 // https://<bitbucketserver-host>/projects/<project>/repos/<repo>/browse/<file>?at=5be7ca73b898bf17a08e607918accfdeafe1e0bc
143 repo.CommitURLTemplate = urlJoinPath("commits", varVersion)
144 repo.FileURLTemplate = urlJoinPath(varPath) + "?at={{.Version}}"
145 repo.LineFragmentTemplate = "#{{.LineNumber}}"
146 case "gitlab":
147 // https://gitlab.com/gitlab-org/omnibus-gitlab/-/commit/b152c864303dae0e55377a1e2c53c9592380ffed
148 // https://gitlab.com/gitlab-org/omnibus-gitlab/-/blob/aad04155b3f6fc50ede88aedaee7fc624d481149/files/gitlab-config-template/gitlab.rb.template
149 repo.CommitURLTemplate = urlJoinPath("-/commit", varVersion)
150 repo.FileURLTemplate = urlJoinPath("-/blob", varVersion, varPath)
151 repo.LineFragmentTemplate = "#L{{.LineNumber}}"
152 case "gitea":
153 repo.CommitURLTemplate = urlJoinPath("commit", varVersion)
154 // NOTE The `display=source` query parameter is required to disable file rendering.
155 // Since line numbers are disabled in rendered files, you wouldn't be able to jump to
156 // a line without `display=source`. This is supported since gitea 1.17.0.
157 // When /src/{{.Version}} is used it will redirect to /src/commit/{{.Version}},
158 // but the query parameters are obmitted.
159 repo.FileURLTemplate = urlJoinPath("src/commit", varVersion, varPath) + "?display=source"
160 repo.LineFragmentTemplate = "#L{{.LineNumber}}"
161 default:
162 return fmt.Errorf("URL scheme type %q unknown", typ)
163 }
164 return nil
165}
166
167// getCommit returns a tree object for the given reference.
168func getCommit(repo *git.Repository, prefix, ref string) (*object.Commit, error) {
169 sha1, err := repo.ResolveRevision(plumbing.Revision(ref))
170 // ref might be a branch name (e.g. "master") add branch prefix and try again.
171 if err != nil {
172 sha1, err = repo.ResolveRevision(plumbing.Revision(filepath.Join(prefix, ref)))
173 }
174 if err != nil {
175 return nil, err
176 }
177
178 commitObj, err := repo.CommitObject(*sha1)
179 if err != nil {
180 return nil, err
181 }
182 return commitObj, nil
183}
184
185func configLookupRemoteURL(cfg *config.Config, key string) string {
186 rc := cfg.Remotes[key]
187 if rc == nil || len(rc.URLs) == 0 {
188 return ""
189 }
190 return rc.URLs[0]
191}
192
193var sshRelativeURLRegexp = regexp.MustCompile(`^([^@]+)@([^:]+):(.*)$`)
194
195func setTemplatesFromConfig(desc *zoekt.Repository, repoDir string) error {
196 repo, err := git.PlainOpen(repoDir)
197 if err != nil {
198 return err
199 }
200
201 cfg, err := repo.Config()
202 if err != nil {
203 return err
204 }
205
206 sec := cfg.Raw.Section("zoekt")
207
208 webURLStr := sec.Options.Get("web-url")
209 webURLType := sec.Options.Get("web-url-type")
210
211 if webURLType != "" && webURLStr != "" {
212 webURL, err := url.Parse(webURLStr)
213 if err != nil {
214 return err
215 }
216 if err := setTemplates(desc, webURL, webURLType); err != nil {
217 return err
218 }
219 } else if webURLStr != "" {
220 desc.URL = webURLStr
221 }
222
223 name := sec.Options.Get("name")
224 if name != "" {
225 desc.Name = name
226 } else {
227 remoteURL := configLookupRemoteURL(cfg, "origin")
228 if remoteURL == "" {
229 return nil
230 }
231 if sm := sshRelativeURLRegexp.FindStringSubmatch(remoteURL); sm != nil {
232 user := sm[1]
233 host := sm[2]
234 path := sm[3]
235
236 remoteURL = fmt.Sprintf("ssh+git://%s@%s/%s", user, host, path)
237 }
238
239 u, err := url.Parse(remoteURL)
240 if err != nil {
241 return err
242 }
243 if err := SetTemplatesFromOrigin(desc, u); err != nil {
244 return err
245 }
246 }
247
248 id, _ := strconv.ParseUint(sec.Options.Get("repoid"), 10, 32)
249 desc.ID = uint32(id)
250
251 desc.TenantID, _ = strconv.Atoi(sec.Options.Get("tenantID"))
252
253 if desc.RawConfig == nil {
254 desc.RawConfig = map[string]string{}
255 }
256 for _, o := range sec.Options {
257 desc.RawConfig[o.Key] = o.Value
258 }
259
260 // Ranking info.
261
262 // Github:
263 traction := 0
264 for _, s := range []string{"github-stars", "github-forks", "github-watchers", "github-subscribers"} {
265 f, err := strconv.Atoi(sec.Options.Get(s))
266 if err == nil {
267 traction += f
268 }
269 }
270
271 if strings.Contains(desc.Name, "googlesource.com/") && traction == 0 {
272 // Pretend everything on googlesource.com has 1000
273 // github stars.
274 traction = 1000
275 }
276
277 if traction > 0 {
278 l := math.Log(float64(traction))
279 desc.Rank = uint16((1.0 - 1.0/math.Pow(1+l, 0.6)) * 10000)
280 }
281
282 return nil
283}
284
285// This attempts to get a repo URL similar to the main repository template processing as in setTemplatesFromConfig()
286func normalizeSubmoduleRemoteURL(cfg *config.Config) (string, error) {
287 sec := cfg.Raw.Section("zoekt")
288 remoteURL := sec.Options.Get("web-url")
289 if remoteURL == "" {
290 // fall back to "origin" remote
291 remoteURL = configLookupRemoteURL(cfg, "origin")
292 if remoteURL == "" {
293 return "", nil
294 }
295 }
296
297 if sm := sshRelativeURLRegexp.FindStringSubmatch(remoteURL); sm != nil {
298 user := sm[1]
299 host := sm[2]
300 path := sm[3]
301
302 remoteURL = fmt.Sprintf("ssh+git://%s@%s/%s", user, host, path)
303 }
304
305 u, err := url.Parse(remoteURL)
306 if err != nil {
307 return "", fmt.Errorf("unable to parse remote URL %q: %w", remoteURL, err)
308 }
309
310 if u.Scheme == "ssh+git" {
311 u.Scheme = "https"
312 u.User = nil
313 }
314
315 // Assume we cannot build templates for this URL, leave it empty
316 if u.Scheme == "" {
317 return "", nil
318 }
319
320 return u.String(), nil
321}
322
323// SetTemplatesFromOrigin fills in templates based on the origin URL.
324func SetTemplatesFromOrigin(desc *zoekt.Repository, u *url.URL) error {
325 desc.Name = filepath.Join(u.Host, strings.TrimSuffix(u.Path, ".git"))
326
327 if strings.HasSuffix(u.Host, ".googlesource.com") {
328 return setTemplates(desc, u, "gitiles")
329 } else if u.Host == "github.com" {
330 u.Path = strings.TrimSuffix(u.Path, ".git")
331 return setTemplates(desc, u, "github")
332 } else {
333 return fmt.Errorf("unknown git hosting site %q", u)
334 }
335}
336
337// The Options structs controls details of the indexing process.
338type Options struct {
339 // The repository to be indexed.
340 RepoDir string
341
342 // If set, follow submodule links. This requires RepoCacheDir to be set.
343 Submodules bool
344
345 // If set, skip indexing if the existing index shard is newer
346 // than the refs in the repository.
347 Incremental bool
348
349 // Don't error out if some branch is missing
350 AllowMissingBranch bool
351
352 // Specifies the root of a Repository cache. Needed for submodule indexing.
353 RepoCacheDir string
354
355 // Indexing options.
356 BuildOptions index.Options
357
358 // Prefix of the branch to index, e.g. `remotes/origin`.
359 BranchPrefix string
360
361 // List of branch names to index, e.g. []string{"HEAD", "stable"}
362 Branches []string
363
364 // DeltaShardNumberFallbackThreshold defines an upper limit (inclusive) on the number of preexisting shards
365 // that can exist before attempting another delta build. If the number of preexisting shards exceeds this threshold,
366 // then a normal build will be performed instead.
367 //
368 // If DeltaShardNumberFallbackThreshold is 0, then this fallback behavior is disabled:
369 // a delta build will always be performed regardless of the number of preexisting shards.
370 DeltaShardNumberFallbackThreshold uint64
371}
372
373func expandBranches(repo *git.Repository, bs []string, prefix string) ([]string, error) {
374 var result []string
375 for _, b := range bs {
376 // Sourcegraph: We disable resolving refs. We want to return the exact ref
377 // requested so we can match it up.
378 if b == "HEAD" && false {
379 ref, err := repo.Head()
380 if err != nil {
381 return nil, err
382 }
383
384 result = append(result, strings.TrimPrefix(ref.Name().String(), prefix))
385 continue
386 }
387
388 if strings.Contains(b, "*") {
389 iter, err := repo.Branches()
390 if err != nil {
391 return nil, err
392 }
393
394 defer iter.Close()
395 for {
396 ref, err := iter.Next()
397 if err == io.EOF {
398 break
399 }
400 if err != nil {
401 return nil, err
402 }
403
404 name := ref.Name().Short()
405 if matched, err := filepath.Match(b, name); err != nil {
406 return nil, err
407 } else if !matched {
408 continue
409 }
410
411 result = append(result, strings.TrimPrefix(name, prefix))
412 }
413 continue
414 }
415
416 result = append(result, b)
417 }
418
419 return result, nil
420}
421
422// IndexGitRepo indexes the git repository as specified by the options.
423// The returned bool indicates whether the index was updated as a result. This
424// can be informative if doing incremental indexing.
425func IndexGitRepo(opts Options) (bool, error) {
426 return indexGitRepo(opts, gitIndexConfig{})
427}
428
429// indexGitRepo indexes the git repository as specified by the options and the provided gitIndexConfig.
430// The returned bool indicates whether the index was updated as a result. This
431// can be informative if doing incremental indexing.
432func indexGitRepo(opts Options, config gitIndexConfig) (bool, error) {
433 prepareDeltaBuild := prepareDeltaBuild
434 if config.prepareDeltaBuild != nil {
435 prepareDeltaBuild = config.prepareDeltaBuild
436 }
437
438 prepareNormalBuild := prepareNormalBuild
439 if config.prepareNormalBuild != nil {
440 prepareNormalBuild = config.prepareNormalBuild
441 }
442
443 // Set max thresholds, since we use them in this function.
444 opts.BuildOptions.SetDefaults()
445 if opts.RepoDir == "" {
446 return false, fmt.Errorf("gitindex: must set RepoDir")
447 }
448
449 opts.BuildOptions.RepositoryDescription.Source = opts.RepoDir
450
451 var repo *git.Repository
452 legacyRepoOpen := cmp.Or(os.Getenv("ZOEKT_DISABLE_GOGIT_OPTIMIZATION"), "false")
453 if b, err := strconv.ParseBool(legacyRepoOpen); b || err != nil {
454 repo, err = git.PlainOpen(opts.RepoDir)
455 if err != nil {
456 return false, fmt.Errorf("git.PlainOpen: %w", err)
457 }
458 } else {
459 var repoCloser io.Closer
460 repo, repoCloser, err = openRepo(opts.RepoDir)
461 if err != nil {
462 return false, fmt.Errorf("openRepo: %w", err)
463 }
464 defer repoCloser.Close()
465 }
466
467 if err := setTemplatesFromConfig(&opts.BuildOptions.RepositoryDescription, opts.RepoDir); err != nil {
468 log.Printf("setTemplatesFromConfig(%s): %s", opts.RepoDir, err)
469 }
470
471 branches, err := expandBranches(repo, opts.Branches, opts.BranchPrefix)
472 if err != nil {
473 return false, fmt.Errorf("expandBranches: %w", err)
474 }
475 for _, b := range branches {
476 commit, err := getCommit(repo, opts.BranchPrefix, b)
477 if err != nil {
478 if opts.AllowMissingBranch && err.Error() == "reference not found" {
479 continue
480 }
481
482 return false, fmt.Errorf("getCommit(%q, %q): %w", opts.BranchPrefix, b, err)
483 }
484
485 opts.BuildOptions.RepositoryDescription.Branches = append(opts.BuildOptions.RepositoryDescription.Branches, zoekt.RepositoryBranch{
486 Name: b,
487 Version: commit.Hash.String(),
488 })
489
490 if when := commit.Committer.When; when.After(opts.BuildOptions.RepositoryDescription.LatestCommitDate) {
491 opts.BuildOptions.RepositoryDescription.LatestCommitDate = when
492 }
493 }
494
495 if opts.Incremental && opts.BuildOptions.IncrementalSkipIndexing() {
496 return false, nil
497 }
498
499 // branch => (path, sha1) => repo.
500 var repos map[fileKey]BlobLocation
501
502 // Branch => Repo => SHA1
503 var branchVersions map[string]map[string]plumbing.Hash
504
505 // set of file paths that have been changed or deleted since
506 // the last indexed commit
507 //
508 // These only have an effect on delta builds
509 var changedOrRemovedFiles []string
510
511 if opts.BuildOptions.IsDelta {
512 repos, branchVersions, changedOrRemovedFiles, err = prepareDeltaBuild(opts, repo)
513 if err != nil {
514 log.Printf("delta build: falling back to normal build since delta build failed, repository=%q, err=%s", opts.BuildOptions.RepositoryDescription.Name, err)
515 opts.BuildOptions.IsDelta = false
516 }
517 }
518
519 if !opts.BuildOptions.IsDelta {
520 repos, branchVersions, err = prepareNormalBuild(opts, repo)
521 if err != nil {
522 return false, fmt.Errorf("preparing normal build: %w", err)
523 }
524 }
525
526 reposByPath := map[string]BlobLocation{}
527 for key, info := range repos {
528 reposByPath[key.SubRepoPath] = info
529 }
530
531 opts.BuildOptions.SubRepositories = map[string]*zoekt.Repository{}
532 for path, info := range reposByPath {
533 tpl := opts.BuildOptions.RepositoryDescription
534 if path != "" {
535 tpl = zoekt.Repository{URL: info.URL.String()}
536 if info.URL.String() != "" {
537 if err := SetTemplatesFromOrigin(&tpl, info.URL); err != nil {
538 log.Printf("setTemplatesFromOrigin(%s, %s): %s", path, info.URL, err)
539 }
540 }
541 if tpl.Name == "" {
542 tpl.Name = path
543 }
544 }
545 opts.BuildOptions.SubRepositories[path] = &tpl
546 }
547
548 for _, br := range opts.BuildOptions.RepositoryDescription.Branches {
549 for path, repo := range opts.BuildOptions.SubRepositories {
550 id := branchVersions[br.Name][path]
551 repo.Branches = append(repo.Branches, zoekt.RepositoryBranch{
552 Name: br.Name,
553 Version: id.String(),
554 })
555 }
556 }
557
558 builder, err := index.NewBuilder(opts.BuildOptions)
559 if err != nil {
560 return false, fmt.Errorf("build.NewBuilder: %w", err)
561 }
562
563 // Preparing the build can consume substantial memory, so check usage before starting to index.
564 builder.CheckMemoryUsage()
565
566 // we don't need to check error, since we either already have an error, or
567 // we returning the first call to builder.Finish.
568 defer builder.Finish() // nolint:errcheck
569
570 for _, f := range changedOrRemovedFiles {
571 builder.MarkFileAsChangedOrRemoved(f)
572 }
573
574 var names []string
575 fileKeys := map[string][]fileKey{}
576 totalFiles := 0
577
578 for key := range repos {
579 n := key.FullPath()
580 fileKeys[n] = append(fileKeys[n], key)
581 names = append(names, n)
582 totalFiles++
583 }
584
585 sort.Strings(names)
586 names = uniq(names)
587
588 // Separate main-repo keys from submodule keys, collecting blob SHAs
589 // for the main repo so we can stream them via git cat-file --batch.
590 // ZOEKT_DISABLE_CATFILE_BATCH=true falls back to the go-git path for
591 // all files, useful as a kill switch if the cat-file path causes issues.
592 catfileBatchDisabled := cmp.Or(os.Getenv("ZOEKT_DISABLE_CATFILE_BATCH"), "false")
593 useCatfileBatch := true
594 if disabled, _ := strconv.ParseBool(catfileBatchDisabled); disabled {
595 useCatfileBatch = false
596 log.Printf("cat-file batch disabled via ZOEKT_DISABLE_CATFILE_BATCH, using go-git")
597 }
598
599 mainRepoKeys := make([]fileKey, 0, totalFiles)
600 mainRepoIDs := make([]plumbing.Hash, 0, totalFiles)
601 var submoduleKeys []fileKey
602
603 for _, name := range names {
604 for _, key := range fileKeys[name] {
605 if useCatfileBatch && key.SubRepoPath == "" {
606 mainRepoKeys = append(mainRepoKeys, key)
607 mainRepoIDs = append(mainRepoIDs, key.ID)
608 } else {
609 submoduleKeys = append(submoduleKeys, key)
610 }
611 }
612 }
613
614 log.Printf("attempting to index %d total files (%d via cat-file, %d via go-git)", totalFiles, len(mainRepoIDs), len(submoduleKeys))
615
616 // Stream main-repo blobs via pipelined cat-file --batch --buffer.
617 // Large blobs are skipped without reading content into memory.
618 if len(mainRepoIDs) > 0 {
619 cr, err := newCatfileReader(opts.RepoDir, mainRepoIDs)
620 if err != nil {
621 return false, fmt.Errorf("newCatfileReader: %w", err)
622 }
623
624 if err := indexCatfileBlobs(cr, mainRepoKeys, repos, opts, builder); err != nil {
625 return false, err
626 }
627 }
628
629 // Index submodule blobs via go-git.
630 for idx, key := range submoduleKeys {
631 doc, err := createDocument(key, repos, opts.BuildOptions)
632 if err != nil {
633 return false, err
634 }
635
636 if err := builder.Add(doc); err != nil {
637 return false, fmt.Errorf("error adding document with name %s: %w", key.FullPath(), err)
638 }
639
640 if idx%10_000 == 0 {
641 builder.CheckMemoryUsage()
642 }
643 }
644
645 return true, builder.Finish()
646}
647
648// indexCatfileBlobs streams main-repo blobs from the catfileReader into the
649// builder. Large blobs are skipped without reading content into memory.
650// keys must correspond 1:1 (in order) with the ids passed to newCatfileReader.
651// The reader is always closed when this function returns.
652func indexCatfileBlobs(cr *catfileReader, keys []fileKey, repos map[fileKey]BlobLocation, opts Options, builder *index.Builder) error {
653 defer cr.Close()
654
655 for idx, key := range keys {
656 size, missing, err := cr.Next()
657 if err != nil {
658 return fmt.Errorf("cat-file next for %s: %w", key.FullPath(), err)
659 }
660
661 branches := repos[key].Branches
662 var doc index.Document
663
664 if missing {
665 // Unexpected for local repos — may indicate corruption, shallow
666 // clone, or a race with git gc. Log a warning and skip.
667 log.Printf("warning: blob %s missing for %s", key.ID, key.FullPath())
668 doc = skippedDoc(key, branches, index.SkipReasonMissing)
669 } else {
670 keyFullPath := key.FullPath()
671 if size > opts.BuildOptions.SizeMax && !opts.BuildOptions.IgnoreSizeMax(keyFullPath) {
672 // Skip without reading content into memory.
673 doc = skippedDoc(key, branches, index.SkipReasonTooLarge)
674 } else {
675 // Pre-allocate and read the full blob content in one call.
676 // io.ReadFull is preferred over io.LimitedReader here as it
677 // avoids the intermediate allocation and the size is known.
678 content := make([]byte, size)
679 if _, err := io.ReadFull(cr, content); err != nil {
680 return fmt.Errorf("read blob %s: %w", keyFullPath, err)
681 }
682 doc = index.Document{
683 SubRepositoryPath: key.SubRepoPath,
684 Name: keyFullPath,
685 Content: content,
686 Branches: branches,
687 }
688 }
689 }
690
691 if err := builder.Add(doc); err != nil {
692 return fmt.Errorf("error adding document with name %s: %w", key.FullPath(), err)
693 }
694
695 if idx%10_000 == 0 {
696 builder.CheckMemoryUsage()
697 }
698 }
699
700 return nil
701}
702
703// openRepo opens a git repository in a way that's optimized for indexing.
704//
705// It copies the relevant logic from git.PlainOpen, and tweaks certain filesystem options.
706func openRepo(repoDir string) (*git.Repository, io.Closer, error) {
707 fs := osfs.New(repoDir)
708 wt := fs
709
710 // Check if the root directory exists.
711 if _, err := fs.Stat(""); err != nil {
712 if os.IsNotExist(err) {
713 return nil, nil, git.ErrRepositoryNotExists
714 }
715 return nil, nil, err
716 }
717
718 // If there's a .git directory, use that as the new root.
719 if fi, err := fs.Stat(git.GitDirName); err == nil && fi.IsDir() {
720 if fs, err = fs.Chroot(git.GitDirName); err != nil {
721 return nil, nil, fmt.Errorf("fs.Chroot: %w", err)
722 }
723 }
724
725 s := filesystem.NewStorageWithOptions(fs, cache.NewObjectLRUDefault(), filesystem.Options{
726 // Cache the packfile handles, preventing the packfile from being opened then closed on every object access
727 KeepDescriptors: true,
728 })
729
730 // Because we're keeping descriptors open, we need to close the storage object when we're done.
731 repo, err := git.Open(s, wt)
732 return repo, s, err
733}
734
735func newIgnoreMatcher(tree *object.Tree) (*ignore.Matcher, error) {
736 ignoreFile, err := tree.File(ignore.IgnoreFile)
737 if err == object.ErrFileNotFound {
738 return &ignore.Matcher{}, nil
739 }
740 if err != nil {
741 return nil, err
742 }
743 content, err := ignoreFile.Contents()
744 if err != nil {
745 return nil, err
746 }
747 return ignore.ParseIgnoreFile(strings.NewReader(content))
748}
749
750// prepareDeltaBuildFunc is a function that calculates the necessary metadata for preparing
751// a build.Builder instance for generating a delta build.
752type prepareDeltaBuildFunc func(options Options, repository *git.Repository) (repos map[fileKey]BlobLocation, branchVersions map[string]map[string]plumbing.Hash, changedOrDeletedPaths []string, err error)
753
754// prepareNormalBuildFunc is a function that calculates the necessary metadata for preparing
755// a build.Builder instance for generating a normal build.
756type prepareNormalBuildFunc func(options Options, repository *git.Repository) (repos map[fileKey]BlobLocation, branchVersions map[string]map[string]plumbing.Hash, err error)
757
758type gitIndexConfig struct {
759 // prepareDeltaBuild, if not nil, is the function that is used to calculate the metadata that will be used to
760 // prepare the build.Builder instance for generating a delta build.
761 //
762 // If prepareDeltaBuild is nil, gitindex.prepareDeltaBuild will be used instead.
763 prepareDeltaBuild prepareDeltaBuildFunc
764
765 // prepareNormalBuild, if not nil, is the function that is used to calculate the metadata that will be used to
766 // prepare the build.Builder instance for generating a normal build.
767 //
768 // If prepareNormalBuild is nil, gitindex.prepareNormalBuild will be used instead.
769 prepareNormalBuild prepareNormalBuildFunc
770}
771
772func prepareDeltaBuild(options Options, repository *git.Repository) (repos map[fileKey]BlobLocation, branchVersions map[string]map[string]plumbing.Hash, changedOrDeletedPaths []string, err error) {
773 if options.Submodules {
774 return nil, nil, nil, fmt.Errorf("delta builds currently don't support submodule indexing")
775 }
776
777 // discover what commits we indexed during our last build
778 existingRepository, _, ok, err := options.BuildOptions.FindRepositoryMetadata()
779 if err != nil {
780 return nil, nil, nil, fmt.Errorf("failed to get repository metadata: %w", err)
781 }
782
783 if !ok {
784 return nil, nil, nil, fmt.Errorf("no existing shards found for repository")
785 }
786
787 if options.DeltaShardNumberFallbackThreshold > 0 {
788 // HACK: For our interim compaction strategy, we force a full normal index once
789 // the number of shards on disk for this repository exceeds the provided threshold.
790 //
791 // This strategy obviously isn't optimal (as an example: we currently can't differentiate
792 // between "normal" and "delta" shards, so repositories like the gigarepo that generate a large number of shards per
793 // build would be disproportionately affected by this), but it'll allow us to continue experimenting on real workloads
794 // while we create a better compaction strategy).
795
796 oldShards := options.BuildOptions.FindAllShards()
797 if uint64(len(oldShards)) > options.DeltaShardNumberFallbackThreshold {
798 return nil, nil, nil, fmt.Errorf("number of existing shards (%d) > requested shard threshold (%d)", len(oldShards), options.DeltaShardNumberFallbackThreshold)
799 }
800 }
801
802 // Check to see if the set of branch names is consistent with what we last indexed.
803 // If it isn't consistent, that we can't proceed with a delta build (and the caller should fall back to a
804 // normal one).
805
806 if !index.BranchNamesEqual(existingRepository.Branches, options.BuildOptions.RepositoryDescription.Branches) {
807 var existingBranchNames []string
808 for _, b := range existingRepository.Branches {
809 existingBranchNames = append(existingBranchNames, b.Name)
810 }
811
812 var optionsBranchNames []string
813 for _, b := range options.BuildOptions.RepositoryDescription.Branches {
814 optionsBranchNames = append(optionsBranchNames, b.Name)
815 }
816
817 existingBranchList := strings.Join(existingBranchNames, ", ")
818 optionsBranchList := strings.Join(optionsBranchNames, ", ")
819
820 return nil, nil, nil, fmt.Errorf("requested branch set in build options (%q) != branch set found on disk (%q) - branch set must be the same for delta shards", optionsBranchList, existingBranchList)
821 }
822
823 // Check if the build options hash does not match the repository metadata's hash
824 // If it does not index then one or more index options has changed and will require a normal build instead of a delta build
825 if options.BuildOptions.GetHash() != existingRepository.IndexOptions {
826 return nil, nil, nil, fmt.Errorf("one or more index options previously stored for repository %s (ID: %d) does not match the index options for this requested build; These index option updates are incompatible with delta build. new index options: %+v", existingRepository.Name, existingRepository.ID, options.BuildOptions.HashOptions())
827 }
828
829 // branch => (path, sha1) => repo.
830 repos = map[fileKey]BlobLocation{}
831
832 branches, err := expandBranches(repository, options.Branches, options.BranchPrefix)
833 if err != nil {
834 return nil, nil, nil, fmt.Errorf("expandBranches: %w", err)
835 }
836
837 // branch name -> git worktree at most current commit
838 branchToCurrentTree := make(map[string]*object.Tree, len(branches))
839
840 for _, b := range branches {
841 commit, err := getCommit(repository, options.BranchPrefix, b)
842 if err != nil {
843 return nil, nil, nil, fmt.Errorf("getting last current commit for branch %q: %w", b, err)
844 }
845
846 tree, err := commit.Tree()
847 if err != nil {
848 return nil, nil, nil, fmt.Errorf("getting current git tree for branch %q: %w", b, err)
849 }
850
851 branchToCurrentTree[b] = tree
852 }
853
854 rawURL := options.BuildOptions.RepositoryDescription.URL
855 u, err := url.Parse(rawURL)
856 if err != nil {
857 return nil, nil, nil, fmt.Errorf("parsing repository URL %q: %w", rawURL, err)
858 }
859
860 // TODO: Support repository submodules for delta builds
861
862 // loop over all branches, calculate the diff between our
863 // last indexed commit and the current commit, and add files mentioned in the diff
864 for _, branch := range existingRepository.Branches {
865 lastIndexedCommit, err := getCommit(repository, "", branch.Version)
866 if err != nil {
867 return nil, nil, nil, fmt.Errorf("getting last indexed commit for branch %q: %w", branch.Name, err)
868 }
869
870 lastIndexedTree, err := lastIndexedCommit.Tree()
871 if err != nil {
872 return nil, nil, nil, fmt.Errorf("getting lasted indexed git tree for branch %q: %w", branch.Name, err)
873 }
874
875 changes, err := object.DiffTreeWithOptions(context.Background(), lastIndexedTree, branchToCurrentTree[branch.Name], &object.DiffTreeOptions{DetectRenames: false})
876 if err != nil {
877 return nil, nil, nil, fmt.Errorf("generating changeset for branch %q: %w", branch.Name, err)
878 }
879
880 for i, c := range changes {
881 oldFile, newFile, err := c.Files()
882 if err != nil {
883 return nil, nil, nil, fmt.Errorf("change #%d: getting files before and after change: %w", i, err)
884 }
885
886 if newFile != nil {
887 // note: newFile.Name could be a path that isn't relative to the repository root - using the
888 // change's Name field is the only way that @ggilmore saw to get the full path relative to the root
889 newFileRelativeRootPath := c.To.Name
890
891 // TODO@ggilmore: HACK - remove once ignore files are supported in delta builds
892 if newFileRelativeRootPath == ignore.IgnoreFile {
893 return nil, nil, nil, fmt.Errorf("%q file is not yet supported in delta builds", ignore.IgnoreFile)
894 }
895
896 // either file is added or renamed, so we need to add the new version to the build
897 file := fileKey{Path: newFileRelativeRootPath, ID: newFile.Hash}
898 if existing, ok := repos[file]; ok {
899 existing.Branches = append(existing.Branches, branch.Name)
900 repos[file] = existing
901 } else {
902 repos[file] = BlobLocation{
903 GitRepo: repository,
904 URL: u,
905 Branches: []string{branch.Name},
906 }
907 }
908 }
909
910 if oldFile == nil {
911 // file added - nothing more to do
912 continue
913 }
914
915 // Note: oldFile.Name could be a path that isn't relative to the repository root - using the
916 // change's "Name" field is the only way that ggilmore saw to get the full path relative to the root
917 oldFileRelativeRootPath := c.From.Name
918
919 if oldFileRelativeRootPath == ignore.IgnoreFile {
920 return nil, nil, nil, fmt.Errorf("%q file is not yet supported in delta builds", ignore.IgnoreFile)
921 }
922
923 // The file is either modified or deleted. So, we need to add ALL versions
924 // of the old file (across all branches) to the build.
925 for b, currentTree := range branchToCurrentTree {
926 f, err := currentTree.File(oldFileRelativeRootPath)
927 if err != nil {
928 // the file doesn't exist in this branch
929 if errors.Is(err, object.ErrFileNotFound) {
930 continue
931 }
932
933 return nil, nil, nil, fmt.Errorf("getting hash for file %q in branch %q: %w", oldFile.Name, b, err)
934 }
935
936 file := fileKey{Path: oldFileRelativeRootPath, ID: f.ID()}
937 if existing, ok := repos[file]; ok {
938 existing.Branches = append(existing.Branches, b)
939 repos[file] = existing
940 } else {
941 repos[file] = BlobLocation{
942 GitRepo: repository,
943 URL: u,
944 Branches: []string{b},
945 }
946 }
947 }
948
949 changedOrDeletedPaths = append(changedOrDeletedPaths, oldFileRelativeRootPath)
950 }
951 }
952
953 // we need to de-duplicate the branch map before returning it - it's possible for the same
954 // branch to have been added multiple times if a file has been modified across multiple commits
955 for _, info := range repos {
956 sort.Strings(info.Branches)
957 info.Branches = uniq(info.Branches)
958 }
959
960 // we also need to de-duplicate the list of changed or deleted file paths, it's also possible to have duplicates
961 // for the same reasoning as above
962 sort.Strings(changedOrDeletedPaths)
963 changedOrDeletedPaths = uniq(changedOrDeletedPaths)
964
965 return repos, nil, changedOrDeletedPaths, nil
966}
967
968func prepareNormalBuild(options Options, repository *git.Repository) (repos map[fileKey]BlobLocation, branchVersions map[string]map[string]plumbing.Hash, err error) {
969 var repoCache *RepoCache
970 if options.Submodules && options.RepoCacheDir != "" {
971 repoCache = NewRepoCache(options.RepoCacheDir)
972 }
973 return prepareNormalBuildRecurse(options, repository, repoCache, false)
974}
975
976func prepareNormalBuildRecurse(options Options, repository *git.Repository, repoCache *RepoCache, isSubrepo bool) (repos map[fileKey]BlobLocation, branchVersions map[string]map[string]plumbing.Hash, err error) {
977 // Branch => Repo => SHA1
978 branchVersions = map[string]map[string]plumbing.Hash{}
979
980 branches, err := expandBranches(repository, options.Branches, options.BranchPrefix)
981 if err != nil {
982 return nil, nil, fmt.Errorf("expandBranches: %w", err)
983 }
984
985 repoURL := options.BuildOptions.RepositoryDescription.URL
986
987 if isSubrepo {
988 cfg, err := repository.Config()
989 if err != nil {
990 return nil, nil, fmt.Errorf("unable to get repository config: %w", err)
991 }
992
993 u, err := normalizeSubmoduleRemoteURL(cfg)
994 if err != nil {
995 return nil, nil, fmt.Errorf("failed to identify subrepository URL: %w", err)
996 }
997 repoURL = u
998 }
999
1000 rw := NewRepoWalker(repository, repoURL, repoCache)
1001 for _, b := range branches {
1002 commit, err := getCommit(repository, options.BranchPrefix, b)
1003 if err != nil {
1004 if options.AllowMissingBranch && err.Error() == "reference not found" {
1005 continue
1006 }
1007
1008 return nil, nil, fmt.Errorf("getCommit: %w", err)
1009 }
1010
1011 tree, err := commit.Tree()
1012 if err != nil {
1013 return nil, nil, fmt.Errorf("commit.Tree: %w", err)
1014 }
1015
1016 ig, err := newIgnoreMatcher(tree)
1017 if err != nil {
1018 return nil, nil, fmt.Errorf("newIgnoreMatcher: %w", err)
1019 }
1020
1021 subVersions, err := rw.CollectFiles(tree, b, ig)
1022 if err != nil {
1023 return nil, nil, fmt.Errorf("CollectFiles: %w", err)
1024 }
1025
1026 branchVersions[b] = subVersions
1027 }
1028
1029 // Index submodules using go-git if we didn't do so using the repo cache
1030 if options.Submodules && options.RepoCacheDir == "" {
1031 worktree, err := repository.Worktree()
1032 if err != nil {
1033 return nil, nil, fmt.Errorf("failed to get repository worktree: %w", err)
1034 }
1035
1036 submodules, err := worktree.Submodules()
1037 if err != nil {
1038 return nil, nil, fmt.Errorf("failed to get submodules: %w", err)
1039 }
1040
1041 for _, submodule := range submodules {
1042 subRepository, err := submodule.Repository()
1043 if err != nil {
1044 log.Printf("failed to open submodule repository: %s, %s", submodule.Config().Name, err)
1045 continue
1046 }
1047
1048 sw, subVersions, err := prepareNormalBuildRecurse(options, subRepository, repoCache, true)
1049 if err != nil {
1050 log.Printf("failed to index submodule repository: %s, %s", submodule.Config().Name, err)
1051 continue
1052 }
1053
1054 log.Printf("adding subrepository files from: %s", submodule.Config().Name)
1055
1056 for k, repo := range sw {
1057 rw.Files[fileKey{
1058 SubRepoPath: filepath.Join(submodule.Config().Path, k.SubRepoPath),
1059 Path: k.Path,
1060 ID: k.ID,
1061 }] = repo
1062 }
1063
1064 for k, v := range subVersions {
1065 branchVersions[filepath.Join(submodule.Config().Path, k)] = v
1066 }
1067 }
1068 }
1069
1070 return rw.Files, branchVersions, nil
1071}
1072
1073func createDocument(key fileKey,
1074 repos map[fileKey]BlobLocation,
1075 opts index.Options,
1076) (index.Document, error) {
1077 repo := repos[key]
1078 blob, err := repo.GitRepo.BlobObject(key.ID)
1079 branches := repos[key].Branches
1080
1081 // We filter out large documents when fetching the repo. So if an object is too large, it will not be found.
1082 if errors.Is(err, plumbing.ErrObjectNotFound) {
1083 return skippedDoc(key, branches, index.SkipReasonTooLarge), nil
1084 }
1085
1086 if err != nil {
1087 return index.Document{}, err
1088 }
1089
1090 keyFullPath := key.FullPath()
1091 if blob.Size > int64(opts.SizeMax) && !opts.IgnoreSizeMax(keyFullPath) {
1092 return skippedDoc(key, branches, index.SkipReasonTooLarge), nil
1093 }
1094
1095 contents, err := blobContents(blob)
1096 if err != nil {
1097 return index.Document{}, err
1098 }
1099
1100 return index.Document{
1101 SubRepositoryPath: key.SubRepoPath,
1102 Name: keyFullPath,
1103 Content: contents,
1104 Branches: branches,
1105 }, nil
1106}
1107
1108// skippedDoc creates a Document placeholder for a blob that was not indexed.
1109func skippedDoc(key fileKey, branches []string, reason index.SkipReason) index.Document {
1110 return index.Document{
1111 SkipReason: reason,
1112 Name: key.FullPath(),
1113 Branches: branches,
1114 SubRepositoryPath: key.SubRepoPath,
1115 }
1116}
1117
1118func blobContents(blob *object.Blob) ([]byte, error) {
1119 r, err := blob.Reader()
1120 if err != nil {
1121 return nil, err
1122 }
1123 defer r.Close()
1124
1125 var buf bytes.Buffer
1126 buf.Grow(int(blob.Size))
1127 _, err = buf.ReadFrom(r)
1128 if err != nil {
1129 return nil, err
1130 }
1131 return buf.Bytes(), nil
1132}
1133
1134func uniq(ss []string) []string {
1135 result := ss[:0]
1136 var last string
1137 for i, s := range ss {
1138 if i == 0 || s != last {
1139 result = append(result, s)
1140 }
1141 last = s
1142 }
1143 return result
1144}