Monorepo for Tangled tangled.org
4

Configure Feed

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

1package xrpc 2 3import ( 4 "cmp" 5 "errors" 6 "fmt" 7 "net/http" 8 "time" 9 10 "github.com/bluesky-social/indigo/atproto/atclient" 11 "github.com/bluesky-social/indigo/atproto/syntax" 12 "github.com/go-git/go-git/v5/plumbing/filemode" 13 "tangled.org/core/api/tangled" 14 "tangled.org/core/knotmirror/xrpc/gitea" 15) 16 17func (x *Xrpc) GetEntry(w http.ResponseWriter, r *http.Request) { 18 var ( 19 repoQuery = r.URL.Query().Get("repo") 20 ref = cmp.Or(r.URL.Query().Get("ref"), "HEAD") 21 path = r.URL.Query().Get("path") 22 ) 23 24 repo, err := syntax.ParseDID(repoQuery) 25 if err != nil { 26 writeJson(w, http.StatusBadRequest, atclient.ErrorBody{Name: "BadRequest", Message: fmt.Sprintf("repo parameter invalid: %s", repoQuery)}) 27 return 28 } 29 30 if path == "" { 31 writeJson(w, http.StatusBadRequest, atclient.ErrorBody{Name: "BadRequest", Message: "missing path parameter"}) 32 return 33 } 34 35 l := x.logger.With("method", "git.getEntry", "repo", repo, "ref", ref, "path", path) 36 l.Debug("request") 37 38 ctx := r.Context() 39 40 repoPath, err := x.makeRepoPath(ctx, repo) 41 if err != nil { 42 writeJson(w, http.StatusNotFound, atclient.ErrorBody{Name: "RepoNotFound", Message: fmt.Sprintf("unknown repository: %q", repo)}) 43 return 44 } 45 46 // resolve ref 47 commit, err := gitea.GetCommit(ctx, repoPath, ref) 48 if err != nil { 49 writeJson(w, http.StatusNotFound, atclient.ErrorBody{Name: "RefNotFound", Message: fmt.Sprintf("unknown ref: %q", ref)}) 50 return 51 } 52 ref = commit.Hash.String() 53 54 entry, err := gitea.GetEntryFromCommit(ctx, repoPath, commit, path) 55 if err != nil { 56 writeJson(w, http.StatusNotFound, atclient.ErrorBody{Name: "EntryNotFound", Message: fmt.Sprintf("entry %q not found", path)}) 57 return 58 } 59 size, err := gitea.GetBlobSize(ctx, repoPath, entry.Hash) 60 if err != nil { 61 l.Error("failed to read blob size", "err", err) 62 writeJson(w, http.StatusInternalServerError, atclient.ErrorBody{Name: "InternalServerError", Message: "failed to read blob size"}) 63 return 64 } 65 66 var outLastCommit *tangled.GitTempDefs_Commit 67 var outSubmodule *tangled.GitTempDefs_Submodule 68 69 lastCommit, err := gitea.GetCommitByPathWithID(ctx, commit.Hash, repoPath, path) 70 if err != nil { 71 l.Error("failed to find last commit", "err", err, "repoPath", repoPath) 72 } else { 73 outLastCommit = &tangled.GitTempDefs_Commit{ 74 Hash: refString(lastCommit.Hash.String()), 75 Tree: refString(lastCommit.TreeHash.String()), 76 Author: &tangled.GitTempDefs_Signature{ 77 Name: lastCommit.Author.Name, 78 Email: lastCommit.Author.Email, 79 When: lastCommit.Author.When.Format(time.RFC3339), 80 }, 81 Committer: &tangled.GitTempDefs_Signature{ 82 Name: lastCommit.Committer.Name, 83 Email: lastCommit.Committer.Email, 84 When: lastCommit.Committer.When.Format(time.RFC3339), 85 }, 86 Message: lastCommit.Message, 87 } 88 } 89 90 if entry.Mode == filemode.Submodule { 91 modules, err := gitea.GetSubmodules(ctx, repoPath, ref) 92 if err != nil && !(errors.Is(err, gitea.ErrMissingGitModules) || errors.Is(err, gitea.ErrInvalidGitModules)) { 93 writeJson(w, http.StatusInternalServerError, atclient.ErrorBody{Name: "InternalServerError", Message: "failed to read .gitmodules"}) 94 return 95 } 96 97 if modules != nil { 98 for _, submodule := range modules.Submodules { 99 if submodule.Path == path { 100 outSubmodule = &tangled.GitTempDefs_Submodule{ 101 Name: submodule.Name, 102 Url: submodule.URL, 103 Branch: refOptionalString(submodule.Branch), 104 } 105 break 106 } 107 } 108 } 109 } 110 111 writeJson(w, http.StatusOK, tangled.GitTempGetEntry_Output{ 112 Name: entry.Name, 113 Mode: entry.Mode.String(), 114 Oid: entry.Hash.String(), 115 Size: size, 116 LastCommit: outLastCommit, 117 Submodule: outSubmodule, 118 }) 119} 120 121func refString(s string) *string { return &s } 122func refOptionalString(s string) *string { 123 if s == "" { 124 return nil 125 } 126 return &s 127}