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