this repo has no description
1package xrpc
2
3import (
4 "encoding/json"
5 "net/http"
6 "time"
7
8 "tangled.sh/tangled.sh/core/api/tangled"
9 "tangled.sh/tangled.sh/core/knotserver/git"
10 xrpcerr "tangled.sh/tangled.sh/core/xrpc/errors"
11)
12
13func (x *Xrpc) RepoGetDefaultBranch(w http.ResponseWriter, r *http.Request) {
14 repo := r.URL.Query().Get("repo")
15 repoPath, err := x.parseRepoParam(repo)
16 if err != nil {
17 writeError(w, err.(xrpcerr.XrpcError), http.StatusBadRequest)
18 return
19 }
20
21 gr, err := git.PlainOpen(repoPath)
22
23 branch, err := gr.FindMainBranch()
24 if err != nil {
25 x.Logger.Error("getting default branch", "error", err.Error())
26 writeError(w, xrpcerr.NewXrpcError(
27 xrpcerr.WithTag("InvalidRequest"),
28 xrpcerr.WithMessage("failed to get default branch"),
29 ), http.StatusInternalServerError)
30 return
31 }
32
33 response := tangled.RepoGetDefaultBranch_Output{
34 Name: branch,
35 Hash: "",
36 When: time.UnixMicro(0).Format(time.RFC3339),
37 }
38
39 w.Header().Set("Content-Type", "application/json")
40 if err := json.NewEncoder(w).Encode(response); err != nil {
41 x.Logger.Error("failed to encode response", "error", err)
42 writeError(w, xrpcerr.NewXrpcError(
43 xrpcerr.WithTag("InternalServerError"),
44 xrpcerr.WithMessage("failed to encode response"),
45 ), http.StatusInternalServerError)
46 return
47 }
48}