A vibe coded tangled fork which supports pijul.
1package xrpc
2
3import (
4 "context"
5 "fmt"
6 "net/http"
7 "net/url"
8 "time"
9
10 "github.com/bluesky-social/indigo/atproto/atclient"
11 "github.com/bluesky-social/indigo/atproto/syntax"
12 "tangled.org/core/api/tangled"
13 "tangled.org/core/knotserver/git"
14)
15
16// TODO: maybe rename to `sh.tangled.repo.temp.getCommit`?
17// then, we should ensure the given `ref` is valid
18func (x *Xrpc) GetBranch(w http.ResponseWriter, r *http.Request) {
19 var (
20 repoQuery = r.URL.Query().Get("repo")
21 nameQuery = r.URL.Query().Get("name")
22 )
23
24 repo, err := syntax.ParseATURI(repoQuery)
25 if err != nil || repo.RecordKey() == "" {
26 writeJson(w, http.StatusBadRequest, atclient.ErrorBody{Name: "BadRequest", Message: fmt.Sprintf("repo parameter invalid: %s", repoQuery)})
27 return
28 }
29
30 if nameQuery == "" {
31 writeJson(w, http.StatusBadRequest, atclient.ErrorBody{Name: "BadRequest", Message: "missing name parameter"})
32 return
33 }
34 branchName, _ := url.PathUnescape(nameQuery)
35
36 l := x.logger.With("repo", repo, "branch", branchName)
37
38 out, err := x.getBranch(r.Context(), repo, branchName)
39 if err != nil {
40 // TODO: better error return
41 l.Error("failed to get branch", "err", err)
42 writeJson(w, http.StatusInternalServerError, atclient.ErrorBody{Name: "InternalServerError", Message: "failed to get branch"})
43 return
44 }
45 writeJson(w, http.StatusOK, out)
46}
47
48func (x *Xrpc) getBranch(ctx context.Context, repo syntax.ATURI, branchName string) (*tangled.GitTempGetBranch_Output, error) {
49 repoPath, err := x.makeRepoPath(ctx, repo)
50 if err != nil {
51 return nil, fmt.Errorf("failed to resolve repo at-uri: %w", err)
52 }
53
54 gr, err := git.PlainOpen(repoPath)
55 if err != nil {
56 return nil, fmt.Errorf("failed to open git repo: %w", err)
57 }
58
59 ref, err := gr.Branch(branchName)
60 if err != nil {
61 return nil, fmt.Errorf("getting branch '%s': %w", branchName, err)
62 }
63
64 commit, err := gr.Commit(ref.Hash())
65 if err != nil {
66 return nil, fmt.Errorf("getting commit '%s': %w", ref.Hash(), err)
67 }
68
69 out := tangled.GitTempGetBranch_Output{
70 Name: ref.Name().Short(),
71 Hash: ref.Hash().String(),
72 When: commit.Author.When.Format(time.RFC3339),
73 Author: &tangled.GitTempDefs_Signature{
74 Name: commit.Author.Name,
75 Email: commit.Author.Email,
76 When: commit.Author.When.Format(time.RFC3339),
77 },
78 }
79
80 if commit.Message != "" {
81 out.Message = &commit.Message
82 }
83
84 return &out, nil
85}