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, 102) (map[fileKey]BlobLocation, map[string]plumbing.Hash, error) { 103 rw := newRepoWalker(r, repoURL, repoCache) 104 105 if err := rw.parseModuleMap(t); err != nil { 106 return nil, nil, fmt.Errorf("parseModuleMap: %w", err) 107 } 108 109 tw := object.NewTreeWalker(t, true, make(map[plumbing.Hash]bool)) 110 defer tw.Close() 111 for { 112 name, entry, err := tw.Next() 113 if err == io.EOF { 114 break 115 } 116 if err := rw.handleEntry(name, &entry); err != nil { 117 return nil, nil, fmt.Errorf("handleEntry: %w", err) 118 } 119 } 120 return rw.tree, rw.subRepoVersions, nil 121} 122 123func (r *repoWalker) tryHandleSubmodule(p string, id *plumbing.Hash) error { 124 if err := r.handleSubmodule(p, id); err != nil { 125 log.Printf("submodule %s: ignoring error %v", p, err) 126 } 127 return nil 128} 129 130func (r *repoWalker) handleSubmodule(p string, id *plumbing.Hash) error { 131 submod := r.submodules[p] 132 if submod == nil { 133 return fmt.Errorf("no entry for submodule path %q", r.repoURL) 134 } 135 136 subURL, err := r.subURL(submod.URL) 137 if err != nil { 138 return err 139 } 140 141 subRepo, err := r.repoCache.Open(subURL) 142 if err != nil { 143 return err 144 } 145 146 obj, err := subRepo.CommitObject(*id) 147 if err != nil { 148 return err 149 } 150 tree, err := subRepo.TreeObject(obj.TreeHash) 151 if err != nil { 152 return err 153 } 154 155 r.subRepoVersions[p] = *id 156 157 subTree, subVersions, err := TreeToFiles(subRepo, tree, subURL.String(), r.repoCache) 158 if err != nil { 159 return err 160 } 161 for k, repo := range subTree { 162 r.tree[fileKey{ 163 SubRepoPath: filepath.Join(p, k.SubRepoPath), 164 Path: k.Path, 165 ID: k.ID, 166 }] = repo 167 } 168 for k, v := range subVersions { 169 r.subRepoVersions[filepath.Join(p, k)] = v 170 } 171 return nil 172} 173 174func (r *repoWalker) handleEntry(p string, e *object.TreeEntry) error { 175 if e.Mode == filemode.Submodule && r.repoCache != nil { 176 if err := r.tryHandleSubmodule(p, &e.Hash); err != nil { 177 return fmt.Errorf("submodule %s: %v", p, err) 178 } 179 } 180 181 switch e.Mode { 182 case filemode.Regular, filemode.Executable, filemode.Symlink: 183 default: 184 return nil 185 } 186 187 r.tree[fileKey{ 188 Path: p, 189 ID: e.Hash, 190 }] = BlobLocation{ 191 Repo: r.repo, 192 URL: r.repoURL, 193 } 194 return nil 195} 196 197// fileKey describes a blob at a location in the final tree. We also 198// record the subrepository from where it came. 199type fileKey struct { 200 SubRepoPath string 201 Path string 202 ID plumbing.Hash 203} 204 205func (k *fileKey) FullPath() string { 206 return filepath.Join(k.SubRepoPath, k.Path) 207} 208 209// BlobLocation holds data where a blob can be found. 210type BlobLocation struct { 211 Repo *git.Repository 212 URL *url.URL 213} 214 215func (l *BlobLocation) Blob(id *plumbing.Hash) ([]byte, error) { 216 blob, err := l.Repo.BlobObject(*id) 217 if err != nil { 218 return nil, err 219 } 220 return blobContents(blob) 221}