forked from tangled.org/core
Monorepo for Tangled — https://tangled.org
at master 2.1 kB view raw
1package xrpc 2 3import ( 4 "net/http" 5 "net/url" 6 "time" 7 8 "tangled.org/core/api/tangled" 9 "tangled.org/core/knotserver/git" 10 xrpcerr "tangled.org/core/xrpc/errors" 11) 12 13func (x *Xrpc) RepoBranch(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 name := r.URL.Query().Get("name") 22 if name == "" { 23 writeError(w, xrpcerr.NewXrpcError( 24 xrpcerr.WithTag("InvalidRequest"), 25 xrpcerr.WithMessage("missing name parameter"), 26 ), http.StatusBadRequest) 27 return 28 } 29 30 branchName, _ := url.PathUnescape(name) 31 32 gr, err := git.PlainOpen(repoPath) 33 if err != nil { 34 writeError(w, xrpcerr.RepoNotFoundError, http.StatusNoContent) 35 return 36 } 37 38 ref, err := gr.Branch(branchName) 39 if err != nil { 40 x.Logger.Error("getting branch", "error", err.Error()) 41 writeError(w, xrpcerr.NewXrpcError( 42 xrpcerr.WithTag("BranchNotFound"), 43 xrpcerr.WithMessage("branch not found"), 44 ), http.StatusNotFound) 45 return 46 } 47 48 commit, err := gr.Commit(ref.Hash()) 49 if err != nil { 50 x.Logger.Error("getting commit object", "error", err.Error()) 51 writeError(w, xrpcerr.NewXrpcError( 52 xrpcerr.WithTag("BranchNotFound"), 53 xrpcerr.WithMessage("failed to get commit object"), 54 ), http.StatusInternalServerError) 55 return 56 } 57 58 defaultBranch, err := gr.FindMainBranch() 59 isDefault := false 60 if err != nil { 61 x.Logger.Error("getting default branch", "error", err.Error()) 62 } else if defaultBranch == branchName { 63 isDefault = true 64 } 65 66 response := tangled.RepoBranch_Output{ 67 Name: ref.Name().Short(), 68 Hash: ref.Hash().String(), 69 ShortHash: &[]string{ref.Hash().String()[:7]}[0], 70 When: commit.Author.When.Format(time.RFC3339), 71 IsDefault: &isDefault, 72 } 73 74 if commit.Message != "" { 75 response.Message = &commit.Message 76 } 77 78 response.Author = &tangled.RepoBranch_Signature{ 79 Name: commit.Author.Name, 80 Email: commit.Author.Email, 81 When: commit.Author.When.Format(time.RFC3339), 82 } 83 84 writeJson(w, response) 85}