Monorepo for Tangled
tangled.org
1package repo
2
3import (
4 "fmt"
5 "net/http"
6 "net/url"
7 "path"
8 "strings"
9 "time"
10
11 "tangled.org/core/api/tangled"
12 "tangled.org/core/appview/db"
13 "tangled.org/core/appview/pages"
14 "tangled.org/core/appview/pages/markup"
15 "tangled.org/core/appview/reporesolver"
16 xrpcclient "tangled.org/core/appview/xrpcclient"
17 "tangled.org/core/types"
18
19 indigoxrpc "github.com/bluesky-social/indigo/xrpc"
20 "github.com/go-chi/chi/v5"
21 "github.com/go-git/go-git/v5/plumbing"
22)
23
24func (rp *Repo) Tree(w http.ResponseWriter, r *http.Request) {
25 l := rp.logger.With("handler", "RepoTree")
26 f, err := rp.repoResolver.Resolve(r)
27 if err != nil {
28 l.Error("failed to fully resolve repo", "err", err)
29 return
30 }
31 ref := chi.URLParam(r, "ref")
32 ref, _ = url.PathUnescape(ref)
33 // if the tree path has a trailing slash, let's strip it
34 // so we don't 404
35 treePath := chi.URLParam(r, "*")
36 treePath, _ = url.PathUnescape(treePath)
37 treePath = strings.TrimSuffix(treePath, "/")
38
39 xrpcc := &indigoxrpc.Client{Host: rp.config.KnotMirror.Url}
40 xrpcResp, err := tangled.GitTempGetTree(r.Context(), xrpcc, treePath, ref, f.RepoDid)
41 if xrpcerr := xrpcclient.HandleXrpcErr(err); xrpcerr != nil {
42 l.Error("failed to call XRPC repo.tree", "xrpcerr", xrpcerr, "err", err)
43 rp.pages.Error503(w)
44 return
45 }
46 var readmeFile *tangled.GitTempGetTree_TreeEntry
47 // Convert XRPC response to internal types.RepoTreeResponse
48 files := make([]types.NiceTree, len(xrpcResp.Files))
49 for i, xrpcFile := range xrpcResp.Files {
50 file := types.NiceTree{
51 Name: xrpcFile.Name,
52 Mode: xrpcFile.Mode,
53 Size: int64(xrpcFile.Size),
54 }
55 // Convert last commit info if present
56 if xrpcFile.Last_commit != nil {
57 commitWhen, _ := time.Parse(time.RFC3339, xrpcFile.Last_commit.When)
58 file.LastCommit = &types.LastCommitInfo{
59 Hash: plumbing.NewHash(xrpcFile.Last_commit.Hash),
60 Message: xrpcFile.Last_commit.Message,
61 When: commitWhen,
62 }
63 }
64 files[i] = file
65 if markup.IsReadmeFile(xrpcFile.Name, xrpcFile.Mode) {
66 readmeFile = xrpcFile
67 }
68 }
69 result := types.RepoTreeResponse{
70 Ref: xrpcResp.Ref,
71 Files: files,
72 }
73 if xrpcResp.Parent != nil {
74 result.Parent = *xrpcResp.Parent
75 }
76 if xrpcResp.Dotdot != nil {
77 result.DotDot = *xrpcResp.Dotdot
78 }
79 if readmeFile != nil {
80 bytes, err := tangled.GitTempGetBlob(r.Context(), xrpcc, path.Join(treePath, readmeFile.Name), ref, f.RepoDid)
81 if xrpcerr := xrpcclient.HandleXrpcErr(err); xrpcerr != nil {
82 l.Error("failed to call XRPC git.getBlob", "xrpcerr", xrpcerr, "err", err)
83 rp.pages.Error503(w)
84 return
85 }
86 result.ReadmeFileName = readmeFile.Name
87 result.Readme = string(bytes)
88 }
89 ownerSlashRepo := reporesolver.GetBaseRepoPath(r, f)
90 // redirects tree paths trying to access a blob; in this case the result.Files is unpopulated,
91 // so we can safely redirect to the "parent" (which is the same file).
92 if len(result.Files) == 0 && result.Parent == treePath {
93 redirectTo := fmt.Sprintf("/%s/blob/%s/%s", ownerSlashRepo, url.PathEscape(ref), result.Parent)
94 http.Redirect(w, r, redirectTo, http.StatusFound)
95 return
96 }
97 user := rp.oauth.GetMultiAccountUser(r)
98 var breadcrumbs [][]string
99 breadcrumbs = append(breadcrumbs, []string{f.Name, fmt.Sprintf("/%s/tree/%s", ownerSlashRepo, url.PathEscape(ref))})
100 if treePath != "" {
101 for idx, elem := range strings.Split(treePath, "/") {
102 breadcrumbs = append(breadcrumbs, []string{elem, fmt.Sprintf("%s/%s", breadcrumbs[idx][1], url.PathEscape(elem))})
103 }
104 }
105 sortFiles(result.Files)
106
107 // Get email to DID mapping for commit author
108 var emails []string
109 if xrpcResp.LastCommit != nil && xrpcResp.LastCommit.Author != nil {
110 emails = append(emails, xrpcResp.LastCommit.Author.Email)
111 }
112 emailToDidMap, err := db.GetEmailToDid(rp.db, emails, true)
113 if err != nil {
114 l.Error("failed to get email to did mapping", "err", err)
115 emailToDidMap = make(map[string]string)
116 }
117
118 var lastCommitInfo *types.LastCommitInfo
119 if xrpcResp.LastCommit != nil {
120 when, _ := time.Parse(time.RFC3339, xrpcResp.LastCommit.When)
121 lastCommitInfo = &types.LastCommitInfo{
122 Hash: plumbing.NewHash(xrpcResp.LastCommit.Hash),
123 Message: xrpcResp.LastCommit.Message,
124 When: when,
125 }
126 if xrpcResp.LastCommit.Author != nil {
127 lastCommitInfo.Author.Name = xrpcResp.LastCommit.Author.Name
128 lastCommitInfo.Author.Email = xrpcResp.LastCommit.Author.Email
129 lastCommitInfo.Author.When, _ = time.Parse(time.RFC3339, xrpcResp.LastCommit.Author.When)
130 }
131 }
132
133 rp.pages.RepoTree(w, pages.RepoTreeParams{
134 LoggedInUser: user,
135 BreadCrumbs: breadcrumbs,
136 Path: treePath,
137 RepoInfo: rp.repoResolver.GetRepoInfo(r, user),
138 EmailToDid: emailToDidMap,
139 LastCommitInfo: lastCommitInfo,
140 RepoTreeResponse: result,
141 })
142}