this repo has no description
1package xrpc 2 3import ( 4 "encoding/json" 5 "net/http" 6 "net/url" 7 "time" 8 9 "tangled.sh/tangled.sh/core/api/tangled" 10 "tangled.sh/tangled.sh/core/knotserver/git" 11 xrpcerr "tangled.sh/tangled.sh/core/xrpc/errors" 12) 13 14func (x *Xrpc) RepoBranch(w http.ResponseWriter, r *http.Request) { 15 repo := r.URL.Query().Get("repo") 16 repoPath, err := x.parseRepoParam(repo) 17 if err != nil { 18 writeError(w, err.(xrpcerr.XrpcError), http.StatusBadRequest) 19 return 20 } 21 22 name := r.URL.Query().Get("name") 23 if name == "" { 24 writeError(w, xrpcerr.NewXrpcError( 25 xrpcerr.WithTag("InvalidRequest"), 26 xrpcerr.WithMessage("missing name parameter"), 27 ), http.StatusBadRequest) 28 return 29 } 30 31 branchName, _ := url.PathUnescape(name) 32 33 gr, err := git.PlainOpen(repoPath) 34 if err != nil { 35 writeError(w, xrpcerr.RepoNotFoundError, http.StatusNoContent) 36 return 37 } 38 39 ref, err := gr.Branch(branchName) 40 if err != nil { 41 x.Logger.Error("getting branch", "error", err.Error()) 42 writeError(w, xrpcerr.NewXrpcError( 43 xrpcerr.WithTag("BranchNotFound"), 44 xrpcerr.WithMessage("branch not found"), 45 ), http.StatusNotFound) 46 return 47 } 48 49 commit, err := gr.Commit(ref.Hash()) 50 if err != nil { 51 x.Logger.Error("getting commit object", "error", err.Error()) 52 writeError(w, xrpcerr.NewXrpcError( 53 xrpcerr.WithTag("BranchNotFound"), 54 xrpcerr.WithMessage("failed to get commit object"), 55 ), http.StatusInternalServerError) 56 return 57 } 58 59 defaultBranch, err := gr.FindMainBranch() 60 isDefault := false 61 if err != nil { 62 x.Logger.Error("getting default branch", "error", err.Error()) 63 } else if defaultBranch == branchName { 64 isDefault = true 65 } 66 67 response := tangled.RepoBranch_Output{ 68 Name: ref.Name().Short(), 69 Hash: ref.Hash().String(), 70 ShortHash: &[]string{ref.Hash().String()[:7]}[0], 71 When: commit.Author.When.Format(time.RFC3339), 72 IsDefault: &isDefault, 73 } 74 75 if commit.Message != "" { 76 response.Message = &commit.Message 77 } 78 79 response.Author = &tangled.RepoBranch_Signature{ 80 Name: commit.Author.Name, 81 Email: commit.Author.Email, 82 When: commit.Author.When.Format(time.RFC3339), 83 } 84 85 w.Header().Set("Content-Type", "application/json") 86 if err := json.NewEncoder(w).Encode(response); err != nil { 87 x.Logger.Error("failed to encode response", "error", err) 88 writeError(w, xrpcerr.NewXrpcError( 89 xrpcerr.WithTag("InternalServerError"), 90 xrpcerr.WithMessage("failed to encode response"), 91 ), http.StatusInternalServerError) 92 return 93 } 94}