fork of https://github.com/sourcegraph/zoekt
0

Configure Feed

Select the types of activity you want to include in your feed.

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 gitindex 16 17import ( 18 "fmt" 19 "io" 20 "log" 21 "net/url" 22 "path" 23 "path/filepath" 24 "strings" 25 26 "github.com/go-git/go-git/v5/plumbing" 27 "github.com/go-git/go-git/v5/plumbing/filemode" 28 "github.com/go-git/go-git/v5/plumbing/object" 29 30 git "github.com/go-git/go-git/v5" 31) 32 33// repoWalker walks a tree, recursing into submodules. 34type repoWalker struct { 35 repo *git.Repository 36 37 repoURL *url.URL 38 tree map[fileKey]BlobLocation 39 40 // Path => SubmoduleEntry 41 submodules map[string]*SubmoduleEntry 42 43 // Path => commit SHA1 44 subRepoVersions map[string]plumbing.Hash 45 repoCache *RepoCache 46} 47 48// subURL returns the URL for a submodule. 49func (w *repoWalker) subURL(relURL string) (*url.URL, error) { 50 if w.repoURL == nil { 51 return nil, fmt.Errorf("no URL for base repo") 52 } 53 if strings.HasPrefix(relURL, "../") { 54 u := *w.repoURL 55 u.Path = path.Join(u.Path, relURL) 56 return &u, nil 57 } 58 59 return url.Parse(relURL) 60} 61 62// newRepoWalker creates a new repoWalker. 63func newRepoWalker(r *git.Repository, repoURL string, repoCache *RepoCache) *repoWalker { 64 u, _ := url.Parse(repoURL) 65 return &repoWalker{ 66 repo: r, 67 repoURL: u, 68 tree: map[fileKey]BlobLocation{}, 69 repoCache: repoCache, 70 subRepoVersions: map[string]plumbing.Hash{}, 71 } 72} 73 74// parseModuleMap initializes rw.submodules. 75func (rw *repoWalker) parseModuleMap(t *object.Tree) error { 76 if rw.repoCache == nil { 77 return nil 78 } 79 modEntry, _ := t.File(".gitmodules") 80 if modEntry != nil { 81 c, err := blobContents(&modEntry.Blob) 82 if err != nil { 83 return fmt.Errorf("blobContents: %w", err) 84 } 85 mods, err := ParseGitModules(c) 86 if err != nil { 87 return fmt.Errorf("ParseGitModules: %w", err) 88 } 89 rw.submodules = map[string]*SubmoduleEntry{} 90 for _, entry := range mods { 91 rw.submodules[entry.Path] = entry 92 } 93 } 94 return nil 95} 96 97// TreeToFiles fetches the blob SHA1s for a tree. If repoCache is 98// non-nil, recurse into submodules. In addition, it returns a mapping 99// that indicates in which repo each SHA1 can be found. 100func TreeToFiles(r *git.Repository, t *object.Tree, 101 repoURL string, repoCache *RepoCache) (map[fileKey]BlobLocation, map[string]plumbing.Hash, error) { 102 rw := newRepoWalker(r, repoURL, repoCache) 103 104 if err := rw.parseModuleMap(t); err != nil { 105 return nil, nil, fmt.Errorf("parseModuleMap: %w", err) 106 } 107 108 tw := object.NewTreeWalker(t, true, make(map[plumbing.Hash]bool)) 109 defer tw.Close() 110 for { 111 name, entry, err := tw.Next() 112 if err == io.EOF { 113 break 114 } 115 if err := rw.handleEntry(name, &entry); err != nil { 116 return nil, nil, fmt.Errorf("handleEntry: %w", err) 117 } 118 } 119 return rw.tree, rw.subRepoVersions, nil 120} 121 122func (r *repoWalker) tryHandleSubmodule(p string, id *plumbing.Hash) error { 123 if err := r.handleSubmodule(p, id); err != nil { 124 log.Printf("submodule %s: ignoring error %v", p, err) 125 } 126 return nil 127} 128 129func (r *repoWalker) handleSubmodule(p string, id *plumbing.Hash) error { 130 submod := r.submodules[p] 131 if submod == nil { 132 return fmt.Errorf("no entry for submodule path %q", r.repoURL) 133 } 134 135 subURL, err := r.subURL(submod.URL) 136 if err != nil { 137 return err 138 } 139 140 subRepo, err := r.repoCache.Open(subURL) 141 if err != nil { 142 return err 143 } 144 145 obj, err := subRepo.CommitObject(*id) 146 if err != nil { 147 return err 148 } 149 tree, err := subRepo.TreeObject(obj.TreeHash) 150 if err != nil { 151 return err 152 } 153 154 r.subRepoVersions[p] = *id 155 156 subTree, subVersions, err := TreeToFiles(subRepo, tree, subURL.String(), r.repoCache) 157 if err != nil { 158 return err 159 } 160 for k, repo := range subTree { 161 r.tree[fileKey{ 162 SubRepoPath: filepath.Join(p, k.SubRepoPath), 163 Path: k.Path, 164 ID: k.ID, 165 }] = repo 166 } 167 for k, v := range subVersions { 168 r.subRepoVersions[filepath.Join(p, k)] = v 169 } 170 return nil 171} 172 173func (r *repoWalker) handleEntry(p string, e *object.TreeEntry) error { 174 if e.Mode == filemode.Submodule && r.repoCache != nil { 175 if err := r.tryHandleSubmodule(p, &e.Hash); err != nil { 176 return fmt.Errorf("submodule %s: %v", p, err) 177 } 178 } 179 180 switch e.Mode { 181 case filemode.Regular, filemode.Executable, filemode.Symlink: 182 default: 183 return nil 184 } 185 186 r.tree[fileKey{ 187 Path: p, 188 ID: e.Hash, 189 }] = BlobLocation{ 190 Repo: r.repo, 191 URL: r.repoURL, 192 } 193 return nil 194} 195 196// fileKey describes a blob at a location in the final tree. We also 197// record the subrepository from where it came. 198type fileKey struct { 199 SubRepoPath string 200 Path string 201 ID plumbing.Hash 202} 203 204func (k *fileKey) FullPath() string { 205 return filepath.Join(k.SubRepoPath, k.Path) 206} 207 208// BlobLocation holds data where a blob can be found. 209type BlobLocation struct { 210 Repo *git.Repository 211 URL *url.URL 212} 213 214func (l *BlobLocation) Blob(id *plumbing.Hash) ([]byte, error) { 215 blob, err := l.Repo.BlobObject(*id) 216 if err != nil { 217 return nil, err 218 } 219 return blobContents(blob) 220}