Monorepo for Tangled tangled.org
6

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 60 var outLastCommit *tangled.GitTempDefs_Commit 61 var outSubmodule *tangled.GitTempDefs_Submodule 62 63 lastCommit, err := gitea.GetCommitByPathWithID(ctx, commit.Hash, repoPath, path) 64 if err != nil { 65 l.Error("failed to find last commit", "err", err, "repoPath", repoPath) 66 } else { 67 outLastCommit = &tangled.GitTempDefs_Commit{ 68 Hash: refString(lastCommit.Hash.String()), 69 Tree: refString(lastCommit.TreeHash.String()), 70 Author: &tangled.GitTempDefs_Signature{ 71 Name: lastCommit.Author.Name, 72 Email: lastCommit.Author.Email, 73 When: lastCommit.Author.When.Format(time.RFC3339), 74 }, 75 Committer: &tangled.GitTempDefs_Signature{ 76 Name: lastCommit.Committer.Name, 77 Email: lastCommit.Committer.Email, 78 When: lastCommit.Committer.When.Format(time.RFC3339), 79 }, 80 Message: lastCommit.Message, 81 } 82 } 83 84 if entry.Mode == filemode.Submodule { 85 modules, err := gitea.GetSubmodules(ctx, repoPath, ref) 86 if err != nil && !(errors.Is(err, gitea.ErrMissingGitModules) || errors.Is(err, gitea.ErrInvalidGitModules)) { 87 writeJson(w, http.StatusInternalServerError, atclient.ErrorBody{Name: "InternalServerError", Message: "failed to read .gitmodules"}) 88 return 89 } 90 91 if modules != nil { 92 for _, submodule := range modules.Submodules { 93 if submodule.Path == path { 94 outSubmodule = &tangled.GitTempDefs_Submodule{ 95 Name: submodule.Name, 96 Url: submodule.URL, 97 Branch: refOptionalString(submodule.Branch), 98 } 99 break 100 } 101 } 102 } 103 } 104 105 writeJson(w, http.StatusOK, tangled.GitTempGetEntry_Output{ 106 Name: entry.Name, 107 Mode: entry.Mode.String(), 108 Oid: entry.Hash.String(), 109 LastCommit: outLastCommit, 110 Submodule: outSubmodule, 111 }) 112} 113 114func refString(s string) *string { return &s } 115func refOptionalString(s string) *string { 116 if s == "" { 117 return nil 118 } 119 return &s 120}