···
19
-
securejoin "github.com/cyphar/filepath-securejoin"
20
-
"github.com/gliderlabs/ssh"
21
-
"github.com/go-chi/chi/v5"
22
-
"github.com/go-git/go-git/v5/plumbing"
23
-
"github.com/go-git/go-git/v5/plumbing/object"
24
-
"tangled.sh/tangled.sh/core/knotserver/db"
25
-
"tangled.sh/tangled.sh/core/knotserver/git"
26
-
"tangled.sh/tangled.sh/core/types"
29
-
func (h *Handle) Index(w http.ResponseWriter, r *http.Request) {
30
-
w.Write([]byte("This is a knot server. More info at https://tangled.sh"))
33
-
func (h *Handle) Capabilities(w http.ResponseWriter, r *http.Request) {
34
-
w.Header().Set("Content-Type", "application/json")
36
-
capabilities := map[string]any{
37
-
"pull_requests": map[string]any{
38
-
"format_patch": true,
39
-
"patch_submissions": true,
40
-
"branch_submissions": true,
41
-
"fork_submissions": true,
46
-
jsonData, err := json.Marshal(capabilities)
48
-
http.Error(w, "Failed to serialize JSON", http.StatusInternalServerError)
55
-
func (h *Handle) RepoIndex(w http.ResponseWriter, r *http.Request) {
56
-
path, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, didPath(r))
57
-
l := h.l.With("path", path, "handler", "RepoIndex")
58
-
ref := chi.URLParam(r, "ref")
59
-
ref, _ = url.PathUnescape(ref)
61
-
gr, err := git.Open(path, ref)
63
-
plain, err2 := git.PlainOpen(path)
65
-
l.Error("opening repo", "error", err2.Error())
69
-
branches, _ := plain.Branches()
73
-
if errors.Is(err, plumbing.ErrReferenceNotFound) {
74
-
resp := types.RepoIndexResponse{
81
-
l.Error("opening repo", "error", err.Error())
88
-
commits []*object.Commit
90
-
branches []types.Branch
91
-
files []types.NiceTree
95
-
var wg sync.WaitGroup
96
-
errorsCh := make(chan error, 5)
101
-
cs, err := gr.Commits(0, 60)
103
-
errorsCh <- fmt.Errorf("commits: %w", err)
112
-
t, err := gr.TotalCommits()
114
-
errorsCh <- fmt.Errorf("calculating total: %w", err)
123
-
bs, err := gr.Branches()
125
-
errorsCh <- fmt.Errorf("fetching branches: %w", err)
134
-
ts, err := gr.Tags()
136
-
errorsCh <- fmt.Errorf("fetching tags: %w", err)
145
-
fs, err := gr.FileTree(r.Context(), "")
147
-
errorsCh <- fmt.Errorf("fetching filetree: %w", err)
157
-
for err := range errorsCh {
158
-
l.Error("loading repo", "error", err.Error())
159
-
writeError(w, err.Error(), http.StatusInternalServerError)
163
-
rtags := []*types.TagReference{}
164
-
for _, tag := range tags {
165
-
var target *object.Tag
166
-
if tag.Target != plumbing.ZeroHash {
169
-
tr := types.TagReference{
173
-
tr.Reference = types.Reference{
175
-
Hash: tag.Hash.String(),
178
-
if tag.Message != "" {
179
-
tr.Message = tag.Message
182
-
rtags = append(rtags, &tr)
185
-
var readmeContent string
186
-
var readmeFile string
187
-
for _, readme := range h.c.Repo.Readme {
188
-
content, _ := gr.FileContent(readme)
189
-
if len(content) > 0 {
190
-
readmeContent = string(content)
191
-
readmeFile = readme
196
-
mainBranch, err := gr.FindMainBranch()
198
-
writeError(w, err.Error(), http.StatusInternalServerError)
199
-
l.Error("finding main branch", "error", err.Error())
205
-
resp := types.RepoIndexResponse{
209
-
Description: getDescription(path),
210
-
Readme: readmeContent,
211
-
ReadmeFileName: readmeFile,
213
-
Branches: branches,
215
-
TotalCommits: total,
221
-
func (h *Handle) RepoTree(w http.ResponseWriter, r *http.Request) {
222
-
treePath := chi.URLParam(r, "*")
223
-
ref := chi.URLParam(r, "ref")
224
-
ref, _ = url.PathUnescape(ref)
226
-
l := h.l.With("handler", "RepoTree", "ref", ref, "treePath", treePath)
228
-
path, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, didPath(r))
229
-
gr, err := git.Open(path, ref)
235
-
files, err := gr.FileTree(r.Context(), treePath)
237
-
writeError(w, err.Error(), http.StatusInternalServerError)
238
-
l.Error("file tree", "error", err.Error())
242
-
resp := types.RepoTreeResponse{
245
-
Description: getDescription(path),
246
-
DotDot: filepath.Dir(treePath),
253
-
func (h *Handle) BlobRaw(w http.ResponseWriter, r *http.Request) {
254
-
treePath := chi.URLParam(r, "*")
255
-
ref := chi.URLParam(r, "ref")
256
-
ref, _ = url.PathUnescape(ref)
258
-
l := h.l.With("handler", "BlobRaw", "ref", ref, "treePath", treePath)
260
-
path, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, didPath(r))
261
-
gr, err := git.Open(path, ref)
267
-
contents, err := gr.RawContent(treePath)
269
-
writeError(w, err.Error(), http.StatusBadRequest)
270
-
l.Error("file content", "error", err.Error())
274
-
mimeType := http.DetectContentType(contents)
276
-
// exception for svg
277
-
if filepath.Ext(treePath) == ".svg" {
278
-
mimeType = "image/svg+xml"
281
-
contentHash := sha256.Sum256(contents)
282
-
eTag := fmt.Sprintf("\"%x\"", contentHash)
284
-
// allow image, video, and text/plain files to be served directly
286
-
case strings.HasPrefix(mimeType, "image/"), strings.HasPrefix(mimeType, "video/"):
287
-
if clientETag := r.Header.Get("If-None-Match"); clientETag == eTag {
288
-
w.WriteHeader(http.StatusNotModified)
291
-
w.Header().Set("ETag", eTag)
293
-
case strings.HasPrefix(mimeType, "text/plain"):
294
-
w.Header().Set("Cache-Control", "public, no-cache")
297
-
l.Error("attempted to serve disallowed file type", "mimetype", mimeType)
298
-
writeError(w, "only image, video, and text files can be accessed directly", http.StatusForbidden)
302
-
w.Header().Set("Content-Type", mimeType)
306
-
func (h *Handle) Blob(w http.ResponseWriter, r *http.Request) {
307
-
treePath := chi.URLParam(r, "*")
308
-
ref := chi.URLParam(r, "ref")
309
-
ref, _ = url.PathUnescape(ref)
311
-
l := h.l.With("handler", "Blob", "ref", ref, "treePath", treePath)
313
-
path, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, didPath(r))
314
-
gr, err := git.Open(path, ref)
320
-
var isBinaryFile bool = false
321
-
contents, err := gr.FileContent(treePath)
322
-
if errors.Is(err, git.ErrBinaryFile) {
323
-
isBinaryFile = true
324
-
} else if errors.Is(err, object.ErrFileNotFound) {
327
-
} else if err != nil {
328
-
writeError(w, err.Error(), http.StatusInternalServerError)
332
-
bytes := []byte(contents)
333
-
// safe := string(sanitize(bytes))
334
-
sizeHint := len(bytes)
336
-
resp := types.RepoBlobResponse{
338
-
Contents: string(bytes),
340
-
IsBinary: isBinaryFile,
341
-
SizeHint: uint64(sizeHint),
344
-
h.showFile(resp, w, l)
347
-
func (h *Handle) Archive(w http.ResponseWriter, r *http.Request) {
348
-
name := chi.URLParam(r, "name")
349
-
file := chi.URLParam(r, "file")
351
-
l := h.l.With("handler", "Archive", "name", name, "file", file)
353
-
// TODO: extend this to add more files compression (e.g.: xz)
354
-
if !strings.HasSuffix(file, ".tar.gz") {
359
-
ref := strings.TrimSuffix(file, ".tar.gz")
361
-
unescapedRef, err := url.PathUnescape(ref)
367
-
safeRefFilename := strings.ReplaceAll(plumbing.ReferenceName(unescapedRef).Short(), "/", "-")
369
-
// This allows the browser to use a proper name for the file when
371
-
filename := fmt.Sprintf("%s-%s.tar.gz", name, safeRefFilename)
372
-
setContentDisposition(w, filename)
375
-
path, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, didPath(r))
376
-
gr, err := git.Open(path, unescapedRef)
382
-
gw := gzip.NewWriter(w)
385
-
prefix := fmt.Sprintf("%s-%s", name, safeRefFilename)
386
-
err = gr.WriteTar(gw, prefix)
388
-
// once we start writing to the body we can't report error anymore
389
-
// so we are only left with printing the error.
390
-
l.Error("writing tar file", "error", err.Error())
396
-
// once we start writing to the body we can't report error anymore
397
-
// so we are only left with printing the error.
398
-
l.Error("flushing?", "error", err.Error())
403
-
func (h *Handle) Log(w http.ResponseWriter, r *http.Request) {
404
-
ref := chi.URLParam(r, "ref")
405
-
ref, _ = url.PathUnescape(ref)
407
-
path, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, didPath(r))
409
-
l := h.l.With("handler", "Log", "ref", ref, "path", path)
411
-
gr, err := git.Open(path, ref)
417
-
// Get page parameters
421
-
if pageParam := r.URL.Query().Get("page"); pageParam != "" {
422
-
if p, err := strconv.Atoi(pageParam); err == nil && p > 0 {
427
-
if pageSizeParam := r.URL.Query().Get("per_page"); pageSizeParam != "" {
428
-
if ps, err := strconv.Atoi(pageSizeParam); err == nil && ps > 0 {
433
-
// convert to offset/limit
434
-
offset := (page - 1) * pageSize
437
-
commits, err := gr.Commits(offset, limit)
439
-
writeError(w, err.Error(), http.StatusInternalServerError)
440
-
l.Error("fetching commits", "error", err.Error())
444
-
total := len(commits)
446
-
resp := types.RepoLogResponse{
449
-
Description: getDescription(path),
459
-
func (h *Handle) Diff(w http.ResponseWriter, r *http.Request) {
460
-
ref := chi.URLParam(r, "ref")
461
-
ref, _ = url.PathUnescape(ref)
463
-
l := h.l.With("handler", "Diff", "ref", ref)
465
-
path, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, didPath(r))
466
-
gr, err := git.Open(path, ref)
472
-
diff, err := gr.Diff()
474
-
writeError(w, err.Error(), http.StatusInternalServerError)
475
-
l.Error("getting diff", "error", err.Error())
479
-
resp := types.RepoCommitResponse{
487
-
func (h *Handle) Tags(w http.ResponseWriter, r *http.Request) {
488
-
path, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, didPath(r))
489
-
l := h.l.With("handler", "Refs")
491
-
gr, err := git.Open(path, "")
497
-
tags, err := gr.Tags()
499
-
// Non-fatal, we *should* have at least one branch to show.
500
-
l.Warn("getting tags", "error", err.Error())
503
-
rtags := []*types.TagReference{}
504
-
for _, tag := range tags {
505
-
var target *object.Tag
506
-
if tag.Target != plumbing.ZeroHash {
509
-
tr := types.TagReference{
513
-
tr.Reference = types.Reference{
515
-
Hash: tag.Hash.String(),
518
-
if tag.Message != "" {
519
-
tr.Message = tag.Message
522
-
rtags = append(rtags, &tr)
525
-
resp := types.RepoTagsResponse{
532
-
func (h *Handle) Branches(w http.ResponseWriter, r *http.Request) {
533
-
path, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, didPath(r))
535
-
gr, err := git.PlainOpen(path)
541
-
branches, _ := gr.Branches()
543
-
resp := types.RepoBranchesResponse{
544
-
Branches: branches,
550
-
func (h *Handle) Branch(w http.ResponseWriter, r *http.Request) {
551
-
path, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, didPath(r))
552
-
branchName := chi.URLParam(r, "branch")
553
-
branchName, _ = url.PathUnescape(branchName)
555
-
l := h.l.With("handler", "Branch")
557
-
gr, err := git.PlainOpen(path)
563
-
ref, err := gr.Branch(branchName)
565
-
l.Error("getting branch", "error", err.Error())
566
-
writeError(w, err.Error(), http.StatusInternalServerError)
570
-
commit, err := gr.Commit(ref.Hash())
572
-
l.Error("getting commit object", "error", err.Error())
573
-
writeError(w, err.Error(), http.StatusInternalServerError)
577
-
defaultBranch, err := gr.FindMainBranch()
580
-
l.Error("getting default branch", "error", err.Error())
581
-
// do not quit though
582
-
} else if defaultBranch == branchName {
586
-
resp := types.RepoBranchResponse{
587
-
Branch: types.Branch{
588
-
Reference: types.Reference{
589
-
Name: ref.Name().Short(),
590
-
Hash: ref.Hash().String(),
593
-
IsDefault: isDefault,
600
-
func (h *Handle) Keys(w http.ResponseWriter, r *http.Request) {
601
-
l := h.l.With("handler", "Keys")
604
-
case http.MethodGet:
605
-
keys, err := h.db.GetAllPublicKeys()
607
-
writeError(w, err.Error(), http.StatusInternalServerError)
608
-
l.Error("getting public keys", "error", err.Error())
612
-
data := make([]map[string]any, 0)
613
-
for _, key := range keys {
615
-
data = append(data, j)
620
-
case http.MethodPut:
621
-
pk := db.PublicKey{}
622
-
if err := json.NewDecoder(r.Body).Decode(&pk); err != nil {
623
-
writeError(w, "invalid request body", http.StatusBadRequest)
627
-
_, _, _, _, err := ssh.ParseAuthorizedKey([]byte(pk.Key))
629
-
writeError(w, "invalid pubkey", http.StatusBadRequest)
632
-
if err := h.db.AddPublicKey(pk); err != nil {
633
-
writeError(w, err.Error(), http.StatusInternalServerError)
634
-
l.Error("adding public key", "error", err.Error())
638
-
w.WriteHeader(http.StatusNoContent)
643
-
// func (h *Handle) RepoForkAheadBehind(w http.ResponseWriter, r *http.Request) {
644
-
// l := h.l.With("handler", "RepoForkSync")
646
-
// data := struct {
647
-
// Did string `json:"did"`
648
-
// Source string `json:"source"`
649
-
// Name string `json:"name,omitempty"`
650
-
// HiddenRef string `json:"hiddenref"`
653
-
// if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
654
-
// writeError(w, "invalid request body", http.StatusBadRequest)
659
-
// source := data.Source
661
-
// if did == "" || source == "" {
662
-
// l.Error("invalid request body, empty did or name")
663
-
// w.WriteHeader(http.StatusBadRequest)
668
-
// if data.Name != "" {
669
-
// name = data.Name
671
-
// name = filepath.Base(source)
674
-
// branch := chi.URLParam(r, "branch")
675
-
// branch, _ = url.PathUnescape(branch)
677
-
// relativeRepoPath := filepath.Join(did, name)
678
-
// repoPath, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, relativeRepoPath)
680
-
// gr, err := git.PlainOpen(repoPath)
682
-
// log.Println(err)
687
-
// forkCommit, err := gr.ResolveRevision(branch)
689
-
// l.Error("error resolving ref revision", "msg", err.Error())
690
-
// writeError(w, fmt.Sprintf("error resolving revision %s", branch), http.StatusBadRequest)
694
-
// sourceCommit, err := gr.ResolveRevision(data.HiddenRef)
696
-
// l.Error("error resolving hidden ref revision", "msg", err.Error())
697
-
// writeError(w, fmt.Sprintf("error resolving revision %s", data.HiddenRef), http.StatusBadRequest)
701
-
// status := types.UpToDate
702
-
// if forkCommit.Hash.String() != sourceCommit.Hash.String() {
703
-
// isAncestor, err := forkCommit.IsAncestor(sourceCommit)
705
-
// log.Printf("error resolving whether %s is ancestor of %s: %s", branch, data.HiddenRef, err)
710
-
// status = types.FastForwardable
712
-
// status = types.Conflict
716
-
// w.Header().Set("Content-Type", "application/json")
717
-
// json.NewEncoder(w).Encode(types.AncestorCheckResponse{Status: status})
720
-
func (h *Handle) RepoLanguages(w http.ResponseWriter, r *http.Request) {
721
-
repoPath, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, didPath(r))
722
-
ref := chi.URLParam(r, "ref")
723
-
ref, _ = url.PathUnescape(ref)
725
-
l := h.l.With("handler", "RepoLanguages")
727
-
gr, err := git.Open(repoPath, ref)
729
-
l.Error("opening repo", "error", err.Error())
734
-
ctx, cancel := context.WithTimeout(r.Context(), 1*time.Second)
737
-
sizes, err := gr.AnalyzeLanguages(ctx)
739
-
l.Error("failed to analyze languages", "error", err.Error())
740
-
writeError(w, err.Error(), http.StatusNoContent)
744
-
resp := types.RepoLanguageResponse{Languages: sizes}
749
-
// func (h *Handle) RepoForkSync(w http.ResponseWriter, r *http.Request) {
750
-
// l := h.l.With("handler", "RepoForkSync")
752
-
// data := struct {
753
-
// Did string `json:"did"`
754
-
// Source string `json:"source"`
755
-
// Name string `json:"name,omitempty"`
758
-
// if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
759
-
// writeError(w, "invalid request body", http.StatusBadRequest)
764
-
// source := data.Source
766
-
// if did == "" || source == "" {
767
-
// l.Error("invalid request body, empty did or name")
768
-
// w.WriteHeader(http.StatusBadRequest)
773
-
// if data.Name != "" {
774
-
// name = data.Name
776
-
// name = filepath.Base(source)
779
-
// branch := chi.URLParam(r, "branch")
780
-
// branch, _ = url.PathUnescape(branch)
782
-
// relativeRepoPath := filepath.Join(did, name)
783
-
// repoPath, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, relativeRepoPath)
785
-
// gr, err := git.Open(repoPath, branch)
787
-
// log.Println(err)
794
-
// l.Error("error syncing repo fork", "error", err.Error())
795
-
// writeError(w, err.Error(), http.StatusInternalServerError)
799
-
// w.WriteHeader(http.StatusNoContent)
802
-
// func (h *Handle) RepoFork(w http.ResponseWriter, r *http.Request) {
803
-
// l := h.l.With("handler", "RepoFork")
805
-
// data := struct {
806
-
// Did string `json:"did"`
807
-
// Source string `json:"source"`
808
-
// Name string `json:"name,omitempty"`
811
-
// if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
812
-
// writeError(w, "invalid request body", http.StatusBadRequest)
817
-
// source := data.Source
819
-
// if did == "" || source == "" {
820
-
// l.Error("invalid request body, empty did or name")
821
-
// w.WriteHeader(http.StatusBadRequest)
826
-
// if data.Name != "" {
827
-
// name = data.Name
829
-
// name = filepath.Base(source)
832
-
// relativeRepoPath := filepath.Join(did, name)
833
-
// repoPath, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, relativeRepoPath)
835
-
// err := git.Fork(repoPath, source)
837
-
// l.Error("forking repo", "error", err.Error())
838
-
// writeError(w, err.Error(), http.StatusInternalServerError)
842
-
// // add perms for this user to access the repo
843
-
// err = h.e.AddRepo(did, rbac.ThisServer, relativeRepoPath)
845
-
// l.Error("adding repo permissions", "error", err.Error())
846
-
// writeError(w, err.Error(), http.StatusInternalServerError)
852
-
// hook.WithScanPath(h.c.Repo.ScanPath),
853
-
// hook.WithInternalApi(h.c.Server.InternalListenAddr),
858
-
// w.WriteHeader(http.StatusNoContent)
861
-
// func (h *Handle) RemoveRepo(w http.ResponseWriter, r *http.Request) {
862
-
// l := h.l.With("handler", "RemoveRepo")
864
-
// data := struct {
865
-
// Did string `json:"did"`
866
-
// Name string `json:"name"`
869
-
// if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
870
-
// writeError(w, "invalid request body", http.StatusBadRequest)
875
-
// name := data.Name
877
-
// if did == "" || name == "" {
878
-
// l.Error("invalid request body, empty did or name")
879
-
// w.WriteHeader(http.StatusBadRequest)
883
-
// relativeRepoPath := filepath.Join(did, name)
884
-
// repoPath, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, relativeRepoPath)
885
-
// err := os.RemoveAll(repoPath)
887
-
// l.Error("removing repo", "error", err.Error())
888
-
// writeError(w, err.Error(), http.StatusInternalServerError)
892
-
// w.WriteHeader(http.StatusNoContent)
896
-
// func (h *Handle) Merge(w http.ResponseWriter, r *http.Request) {
897
-
// path, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, didPath(r))
899
-
// data := types.MergeRequest{}
901
-
// if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
902
-
// writeError(w, err.Error(), http.StatusBadRequest)
903
-
// h.l.Error("git: failed to unmarshal json patch", "handler", "Merge", "error", err)
907
-
// mo := &git.MergeOptions{
908
-
// AuthorName: data.AuthorName,
909
-
// AuthorEmail: data.AuthorEmail,
910
-
// CommitBody: data.CommitBody,
911
-
// CommitMessage: data.CommitMessage,
914
-
// patch := data.Patch
915
-
// branch := data.Branch
916
-
// gr, err := git.Open(path, branch)
922
-
// mo.FormatPatch = patchutil.IsFormatPatch(patch)
924
-
// if err := gr.MergeWithOptions([]byte(patch), branch, mo); err != nil {
925
-
// var mergeErr *git.ErrMerge
926
-
// if errors.As(err, &mergeErr) {
927
-
// conflicts := make([]types.ConflictInfo, len(mergeErr.Conflicts))
928
-
// for i, conflict := range mergeErr.Conflicts {
929
-
// conflicts[i] = types.ConflictInfo{
930
-
// Filename: conflict.Filename,
931
-
// Reason: conflict.Reason,
934
-
// response := types.MergeCheckResponse{
935
-
// IsConflicted: true,
936
-
// Conflicts: conflicts,
937
-
// Message: mergeErr.Message,
939
-
// writeConflict(w, response)
940
-
// h.l.Error("git: merge conflict", "handler", "Merge", "error", mergeErr)
942
-
// writeError(w, err.Error(), http.StatusBadRequest)
943
-
// h.l.Error("git: failed to merge", "handler", "Merge", "error", err.Error())
948
-
// w.WriteHeader(http.StatusOK)
951
-
// func (h *Handle) MergeCheck(w http.ResponseWriter, r *http.Request) {
952
-
// path, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, didPath(r))
954
-
// var data struct {
955
-
// Patch string `json:"patch"`
956
-
// Branch string `json:"branch"`
959
-
// if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
960
-
// writeError(w, err.Error(), http.StatusBadRequest)
961
-
// h.l.Error("git: failed to unmarshal json patch", "handler", "MergeCheck", "error", err)
965
-
// patch := data.Patch
966
-
// branch := data.Branch
967
-
// gr, err := git.Open(path, branch)
973
-
// err = gr.MergeCheck([]byte(patch), branch)
975
-
// response := types.MergeCheckResponse{
976
-
// IsConflicted: false,
978
-
// writeJSON(w, response)
982
-
// var mergeErr *git.ErrMerge
983
-
// if errors.As(err, &mergeErr) {
984
-
// conflicts := make([]types.ConflictInfo, len(mergeErr.Conflicts))
985
-
// for i, conflict := range mergeErr.Conflicts {
986
-
// conflicts[i] = types.ConflictInfo{
987
-
// Filename: conflict.Filename,
988
-
// Reason: conflict.Reason,
991
-
// response := types.MergeCheckResponse{
992
-
// IsConflicted: true,
993
-
// Conflicts: conflicts,
994
-
// Message: mergeErr.Message,
996
-
// writeConflict(w, response)
997
-
// h.l.Error("git: merge conflict", "handler", "MergeCheck", "error", mergeErr.Error())
1000
-
// writeError(w, err.Error(), http.StatusInternalServerError)
1001
-
// h.l.Error("git: failed to check merge", "handler", "MergeCheck", "error", err.Error())
1004
-
func (h *Handle) Compare(w http.ResponseWriter, r *http.Request) {
1005
-
rev1 := chi.URLParam(r, "rev1")
1006
-
rev1, _ = url.PathUnescape(rev1)
1008
-
rev2 := chi.URLParam(r, "rev2")
1009
-
rev2, _ = url.PathUnescape(rev2)
1011
-
l := h.l.With("handler", "Compare", "r1", rev1, "r2", rev2)
1013
-
path, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, didPath(r))
1014
-
gr, err := git.PlainOpen(path)
1020
-
commit1, err := gr.ResolveRevision(rev1)
1022
-
l.Error("error resolving revision 1", "msg", err.Error())
1023
-
writeError(w, fmt.Sprintf("error resolving revision %s", rev1), http.StatusBadRequest)
1027
-
commit2, err := gr.ResolveRevision(rev2)
1029
-
l.Error("error resolving revision 2", "msg", err.Error())
1030
-
writeError(w, fmt.Sprintf("error resolving revision %s", rev2), http.StatusBadRequest)
1034
-
rawPatch, formatPatch, err := gr.FormatPatch(commit1, commit2)
1036
-
l.Error("error comparing revisions", "msg", err.Error())
1037
-
writeError(w, "error comparing revisions", http.StatusBadRequest)
1041
-
writeJSON(w, types.RepoFormatPatchResponse{
1042
-
Rev1: commit1.Hash.String(),
1043
-
Rev2: commit2.Hash.String(),
1044
-
FormatPatch: formatPatch,
1049
-
func (h *Handle) DefaultBranch(w http.ResponseWriter, r *http.Request) {
1050
-
l := h.l.With("handler", "DefaultBranch")
1051
-
path, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, didPath(r))
1053
-
gr, err := git.Open(path, "")
1059
-
branch, err := gr.FindMainBranch()
1061
-
writeError(w, err.Error(), http.StatusInternalServerError)
1062
-
l.Error("getting default branch", "error", err.Error())
1066
-
writeJSON(w, types.RepoDefaultBranchResponse{