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.NewXrpcError( 36 xrpcerr.WithTag("RepoNotFound"), 37 xrpcerr.WithMessage("repository not found"), 38 ), http.StatusNotFound) 39 return 40 } 41 42 ref, err := gr.Branch(branchName) 43 if err != nil { 44 x.Logger.Error("getting branch", "error", err.Error()) 45 writeError(w, xrpcerr.NewXrpcError( 46 xrpcerr.WithTag("BranchNotFound"), 47 xrpcerr.WithMessage("branch not found"), 48 ), http.StatusNotFound) 49 return 50 } 51 52 commit, err := gr.Commit(ref.Hash()) 53 if err != nil { 54 x.Logger.Error("getting commit object", "error", err.Error()) 55 writeError(w, xrpcerr.NewXrpcError( 56 xrpcerr.WithTag("BranchNotFound"), 57 xrpcerr.WithMessage("failed to get commit object"), 58 ), http.StatusInternalServerError) 59 return 60 } 61 62 defaultBranch, err := gr.FindMainBranch() 63 isDefault := false 64 if err != nil { 65 x.Logger.Error("getting default branch", "error", err.Error()) 66 } else if defaultBranch == branchName { 67 isDefault = true 68 } 69 70 response := tangled.RepoBranch_Output{ 71 Name: ref.Name().Short(), 72 Hash: ref.Hash().String(), 73 ShortHash: &[]string{ref.Hash().String()[:7]}[0], 74 When: commit.Author.When.Format(time.RFC3339), 75 IsDefault: &isDefault, 76 } 77 78 if commit.Message != "" { 79 response.Message = &commit.Message 80 } 81 82 response.Author = &tangled.RepoBranch_Signature{ 83 Name: commit.Author.Name, 84 Email: commit.Author.Email, 85 When: commit.Author.When.Format(time.RFC3339), 86 } 87 88 w.Header().Set("Content-Type", "application/json") 89 if err := json.NewEncoder(w).Encode(response); err != nil { 90 x.Logger.Error("failed to encode response", "error", err) 91 writeError(w, xrpcerr.NewXrpcError( 92 xrpcerr.WithTag("InternalServerError"), 93 xrpcerr.WithMessage("failed to encode response"), 94 ), http.StatusInternalServerError) 95 return 96 } 97}