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.NewXrpcError(
42 xrpcerr.WithTag("RepoNotFound"),
43 xrpcerr.WithMessage("repository not found"),
44 ), http.StatusNotFound)
45 return
46 }
47
48 commit1, err := gr.ResolveRevision(rev1)
49 if err != nil {
50 x.Logger.Error("error resolving revision 1", "msg", err.Error())
51 writeError(w, xrpcerr.NewXrpcError(
52 xrpcerr.WithTag("RevisionNotFound"),
53 xrpcerr.WithMessage(fmt.Sprintf("error resolving revision %s", rev1)),
54 ), http.StatusBadRequest)
55 return
56 }
57
58 commit2, err := gr.ResolveRevision(rev2)
59 if err != nil {
60 x.Logger.Error("error resolving revision 2", "msg", err.Error())
61 writeError(w, xrpcerr.NewXrpcError(
62 xrpcerr.WithTag("RevisionNotFound"),
63 xrpcerr.WithMessage(fmt.Sprintf("error resolving revision %s", rev2)),
64 ), http.StatusBadRequest)
65 return
66 }
67
68 rawPatch, formatPatch, err := gr.FormatPatch(commit1, commit2)
69 if err != nil {
70 x.Logger.Error("error comparing revisions", "msg", err.Error())
71 writeError(w, xrpcerr.NewXrpcError(
72 xrpcerr.WithTag("CompareError"),
73 xrpcerr.WithMessage("error comparing revisions"),
74 ), http.StatusBadRequest)
75 return
76 }
77
78 resp := types.RepoFormatPatchResponse{
79 Rev1: commit1.Hash.String(),
80 Rev2: commit2.Hash.String(),
81 FormatPatch: formatPatch,
82 Patch: rawPatch,
83 }
84
85 w.Header().Set("Content-Type", "application/json")
86 if err := json.NewEncoder(w).Encode(resp); 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}