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