+4
-1
knotserver/handler.go
+4
-1
knotserver/handler.go
···
126
126
r.Use(h.VerifySignature)
127
127
r.Put("/new", h.NewRepo)
128
128
r.Delete("/", h.RemoveRepo)
129
-
r.Post("/fork", h.RepoFork)
129
+
r.Route("/fork", func(r chi.Router) {
130
+
r.Post("/", h.RepoFork)
131
+
r.Post("/sync/{branch}", h.RepoForkSync)
132
+
})
130
133
})
131
134
132
135
r.Route("/member", func(r chi.Router) {
+53
knotserver/routes.go
+53
knotserver/routes.go
···
631
631
w.WriteHeader(http.StatusNoContent)
632
632
}
633
633
634
+
func (h *Handle) RepoForkSync(w http.ResponseWriter, r *http.Request) {
635
+
l := h.l.With("handler", "RepoForkSync")
636
+
637
+
data := struct {
638
+
Did string `json:"did"`
639
+
Source string `json:"source"`
640
+
Name string `json:"name,omitempty"`
641
+
}{}
642
+
643
+
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
644
+
writeError(w, "invalid request body", http.StatusBadRequest)
645
+
return
646
+
}
647
+
648
+
did := data.Did
649
+
source := data.Source
650
+
651
+
if did == "" || source == "" {
652
+
l.Error("invalid request body, empty did or name")
653
+
w.WriteHeader(http.StatusBadRequest)
654
+
return
655
+
}
656
+
657
+
var name string
658
+
if data.Name != "" {
659
+
name = data.Name
660
+
} else {
661
+
name = filepath.Base(source)
662
+
}
663
+
664
+
branch := chi.URLParam(r, "branch")
665
+
branch, _ = url.PathUnescape(branch)
666
+
667
+
relativeRepoPath := filepath.Join(did, name)
668
+
repoPath, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, relativeRepoPath)
669
+
670
+
gr, err := git.PlainOpen(repoPath)
671
+
if err != nil {
672
+
log.Println(err)
673
+
notFound(w)
674
+
return
675
+
}
676
+
677
+
err = gr.Sync(branch)
678
+
if err != nil {
679
+
l.Error("syncing repo fork", "error", err.Error())
680
+
writeError(w, err.Error(), http.StatusInternalServerError)
681
+
return
682
+
}
683
+
684
+
w.WriteHeader(http.StatusNoContent)
685
+
}
686
+
634
687
func (h *Handle) RepoFork(w http.ResponseWriter, r *http.Request) {
635
688
l := h.l.With("handler", "RepoFork")
636
689