this repo has no description
1package xrpc 2 3import ( 4 "encoding/json" 5 "fmt" 6 "net/http" 7 8 "tangled.sh/tangled.sh/core/knotserver/git" 9 "tangled.sh/tangled.sh/core/types" 10 xrpcerr "tangled.sh/tangled.sh/core/xrpc/errors" 11) 12 13func (x *Xrpc) RepoCompare(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 rev1 := r.URL.Query().Get("rev1") 22 if rev1 == "" { 23 writeError(w, xrpcerr.NewXrpcError( 24 xrpcerr.WithTag("InvalidRequest"), 25 xrpcerr.WithMessage("missing rev1 parameter"), 26 ), http.StatusBadRequest) 27 return 28 } 29 30 rev2 := r.URL.Query().Get("rev2") 31 if rev2 == "" { 32 writeError(w, xrpcerr.NewXrpcError( 33 xrpcerr.WithTag("InvalidRequest"), 34 xrpcerr.WithMessage("missing rev2 parameter"), 35 ), http.StatusBadRequest) 36 return 37 } 38 39 gr, err := git.PlainOpen(repoPath) 40 if err != nil { 41 writeError(w, xrpcerr.RepoNotFoundError, http.StatusNoContent) 42 return 43 } 44 45 commit1, err := gr.ResolveRevision(rev1) 46 if err != nil { 47 x.Logger.Error("error resolving revision 1", "msg", err.Error()) 48 writeError(w, xrpcerr.NewXrpcError( 49 xrpcerr.WithTag("RevisionNotFound"), 50 xrpcerr.WithMessage(fmt.Sprintf("error resolving revision %s", rev1)), 51 ), http.StatusBadRequest) 52 return 53 } 54 55 commit2, err := gr.ResolveRevision(rev2) 56 if err != nil { 57 x.Logger.Error("error resolving revision 2", "msg", err.Error()) 58 writeError(w, xrpcerr.NewXrpcError( 59 xrpcerr.WithTag("RevisionNotFound"), 60 xrpcerr.WithMessage(fmt.Sprintf("error resolving revision %s", rev2)), 61 ), http.StatusBadRequest) 62 return 63 } 64 65 rawPatch, formatPatch, err := gr.FormatPatch(commit1, commit2) 66 if err != nil { 67 x.Logger.Error("error comparing revisions", "msg", err.Error()) 68 writeError(w, xrpcerr.NewXrpcError( 69 xrpcerr.WithTag("CompareError"), 70 xrpcerr.WithMessage("error comparing revisions"), 71 ), http.StatusBadRequest) 72 return 73 } 74 75 resp := types.RepoFormatPatchResponse{ 76 Rev1: commit1.Hash.String(), 77 Rev2: commit2.Hash.String(), 78 FormatPatch: formatPatch, 79 Patch: rawPatch, 80 } 81 82 w.Header().Set("Content-Type", "application/json") 83 if err := json.NewEncoder(w).Encode(resp); err != nil { 84 x.Logger.Error("failed to encode response", "error", err) 85 writeError(w, xrpcerr.NewXrpcError( 86 xrpcerr.WithTag("InternalServerError"), 87 xrpcerr.WithMessage("failed to encode response"), 88 ), http.StatusInternalServerError) 89 return 90 } 91}