This repository has no description
0

Configure Feed

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

1package main 2 3import ( 4 "context" 5 "crypto/sha1" 6 "encoding/json" 7 "fmt" 8 "io" 9 "net/url" 10 "os" 11 "path/filepath" 12 13 "github.com/bluesky-social/indigo/atproto/identity" 14 "github.com/bluesky-social/indigo/atproto/syntax" 15 indigoxrpc "github.com/bluesky-social/indigo/xrpc" 16 "github.com/sourcegraph/zoekt" 17 "tangled.org/core/api/tangled" 18) 19 20// 1 MB; match https://sourcegraph.sourcegraph.com/r/github.com/sourcegraph/sourcegraph/-/blob/cmd/searcher/internal/search/store.go?L32 21const MaxFileSize = 1 << 20 22 23func gitIndex(ctx context.Context, cfg *Config, dir identity.Directory, req indexRequest) error { 24 ctx, cancel := context.WithTimeout(ctx, cfg.IndexTimeout) 25 defer cancel() 26 27 repo, err := loadRepo(ctx, dir, req.Repo) 28 if err != nil { 29 return nil 30 } 31 repo.Branches = req.Branches 32 33 gitDir, err := tmpGitDir(repo.Did.String()) 34 if err != nil { 35 return err 36 } 37 defer os.RemoveAll(gitDir) // best-effort cleanup 38 39 if err := fetchRepo(ctx, gitDir, repo.CloneURL(), req.Branches); err != nil { 40 return err 41 } 42 43 if err := indexRepo(ctx, cfg, gitDir, *repo); err != nil { 44 return err 45 } 46 47 return nil 48} 49 50func loadRepo(ctx context.Context, dir identity.Directory, repoDID syntax.DID) (*Repo, error) { 51 ident, err := dir.LookupDID(ctx, repoDID) 52 if err != nil { 53 return nil, err 54 } 55 56 knot := ident.PDSEndpoint() 57 58 xrpcc := &indigoxrpc.Client{Host: knot} 59 out, err := tangled.RepoDescribeRepo(ctx, xrpcc, repoDID.String()) 60 if err != nil { 61 return nil, err 62 } 63 64 return &Repo{ 65 Did: repoDID, 66 Owner: syntax.DID(out.OwnerDid), 67 Slug: syntax.RecordKey(out.Rkey), 68 Knot: knot, 69 }, nil 70} 71 72func fetchRepo(ctx context.Context, gitDir, cloneUrl string, branches []zoekt.RepositoryBranch) error { 73 // Create a repo to fetch into 74 if err := executeCmd(ctx, 75 "git", 76 // use a random default branch. This is so that HEAD isn't a symref to a 77 // branch that is indexed. For example if you are indexing 78 // HEAD,master. Then HEAD would be pointing to master by default. 79 "-c", "init.defaultBranch=nonExistentBranchBB0FOFCH32", 80 "init", 81 // we don't need a working copy 82 "--bare", 83 gitDir, 84 ); err != nil { 85 return err 86 } 87 88 fetchArgs := []string{ 89 "-C", gitDir, 90 "-c", "protocol.version=2", 91 "fetch", "--depth=1", "--no-tags", 92 } 93 // Git's blob:limit filter excludes blobs whose size is >= the given limit, 94 // while zoekt indexes files up to and including FileLimit bytes. 95 fetchArgs = append(fetchArgs, fmt.Sprintf("--filter=blob:limit=%d", int64(MaxFileSize)+1)) 96 97 fetchArgs = append(fetchArgs, cloneUrl) 98 99 var commits []string 100 for _, b := range branches { 101 commits = append(commits, b.Version) 102 } 103 fetchArgs = append(fetchArgs, commits...) 104 105 if err := executeCmd(ctx, "git", fetchArgs...); err != nil { 106 return err 107 } 108 109 for _, b := range branches { 110 ref := b.Name 111 if ref != "HEAD" { 112 ref = "refs/heads/" + ref 113 } 114 if err := executeCmd(ctx, "git", "-C", gitDir, "update-ref", ref, b.Version); err != nil { 115 return fmt.Errorf("failed update-ref %s to %s: %w", ref, b.Version, err) 116 } 117 } 118 119 return nil 120} 121 122func indexRepo(ctx context.Context, cfg *Config, gitDir string, repo Repo) error { 123 executablePath, err := os.Executable() 124 if err != nil { 125 return err 126 } 127 128 repoJson, err := json.Marshal(repo) 129 if err != nil { 130 return err 131 } 132 133 args := []string{"index"} 134 args = append(args, "-index-dir", cfg.IndexDir) 135 args = append(args, "-appview-url", cfg.AppviewUrl) 136 args = append(args, gitDir, string(repoJson)) 137 if err := executeCmd(ctx, executablePath, args...); err != nil { 138 return err 139 } 140 return nil 141} 142 143func tmpGitDir(name string) (string, error) { 144 abs := url.QueryEscape(name) 145 if len(abs) > 200 { 146 h := sha1.New() 147 _, _ = io.WriteString(h, abs) 148 abs = abs[:200] + fmt.Sprintf("%x", h.Sum(nil))[:8] 149 } 150 dir := filepath.Join(os.TempDir(), abs+".git") 151 if _, err := os.Stat(dir); err == nil { 152 if err := os.RemoveAll(dir); err != nil { 153 return "", err 154 } 155 } 156 return dir, nil 157}