forked from tangled.org/core
Something went wrong. Try again.
26 kB · 1069 lines
Go
at master
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070package knotserver
import ( "compress/gzip" "context" "crypto/sha256" "encoding/json" "errors" "fmt" "log" "net/http" "net/url" "path/filepath" "strconv" "strings" "sync" "time"
securejoin "github.com/cyphar/filepath-securejoin" "github.com/gliderlabs/ssh" "github.com/go-chi/chi/v5" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/object" "tangled.sh/tangled.sh/core/knotserver/db" "tangled.sh/tangled.sh/core/knotserver/git" "tangled.sh/tangled.sh/core/types")
func (h *Handle) Index(w http.ResponseWriter, r *http.Request) { w.Write([]byte("This is a knot server. More info at https://tangled.sh"))}
func (h *Handle) Capabilities(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json")
capabilities := map[string]any{ "pull_requests": map[string]any{ "format_patch": true, "patch_submissions": true, "branch_submissions": true, "fork_submissions": true, }, "xrpc": true, }
jsonData, err := json.Marshal(capabilities) if err != nil { http.Error(w, "Failed to serialize JSON", http.StatusInternalServerError) return }
w.Write(jsonData)}
func (h *Handle) RepoIndex(w http.ResponseWriter, r *http.Request) { path, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, didPath(r)) l := h.l.With("path", path, "handler", "RepoIndex") ref := chi.URLParam(r, "ref") ref, _ = url.PathUnescape(ref)
gr, err := git.Open(path, ref) if err != nil { plain, err2 := git.PlainOpen(path) if err2 != nil { l.Error("opening repo", "error", err2.Error()) notFound(w) return } branches, _ := plain.Branches()
log.Println(err)
if errors.Is(err, plumbing.ErrReferenceNotFound) { resp := types.RepoIndexResponse{ IsEmpty: true, Branches: branches, } writeJSON(w, resp) return } else { l.Error("opening repo", "error", err.Error()) notFound(w) return } }
var ( commits []*object.Commit total int branches []types.Branch files []types.NiceTree tags []object.Tag )
var wg sync.WaitGroup errorsCh := make(chan error, 5)
wg.Add(1) go func() { defer wg.Done() cs, err := gr.Commits(0, 60) if err != nil { errorsCh <- fmt.Errorf("commits: %w", err) return } commits = cs }()
wg.Add(1) go func() { defer wg.Done() t, err := gr.TotalCommits() if err != nil { errorsCh <- fmt.Errorf("calculating total: %w", err) return } total = t }()
wg.Add(1) go func() { defer wg.Done() bs, err := gr.Branches() if err != nil { errorsCh <- fmt.Errorf("fetching branches: %w", err) return } branches = bs }()
wg.Add(1) go func() { defer wg.Done() ts, err := gr.Tags() if err != nil { errorsCh <- fmt.Errorf("fetching tags: %w", err) return } tags = ts }()
wg.Add(1) go func() { defer wg.Done() fs, err := gr.FileTree(r.Context(), "") if err != nil { errorsCh <- fmt.Errorf("fetching filetree: %w", err) return } files = fs }()
wg.Wait() close(errorsCh)
// show any errors for err := range errorsCh { l.Error("loading repo", "error", err.Error()) writeError(w, err.Error(), http.StatusInternalServerError) return }
rtags := []*types.TagReference{} for _, tag := range tags { var target *object.Tag if tag.Target != plumbing.ZeroHash { target = &tag } tr := types.TagReference{ Tag: target, }
tr.Reference = types.Reference{ Name: tag.Name, Hash: tag.Hash.String(), }
if tag.Message != "" { tr.Message = tag.Message }
rtags = append(rtags, &tr) }
var readmeContent string var readmeFile string for _, readme := range h.c.Repo.Readme { content, _ := gr.FileContent(readme) if len(content) > 0 { readmeContent = string(content) readmeFile = readme } }
if ref == "" { mainBranch, err := gr.FindMainBranch() if err != nil { writeError(w, err.Error(), http.StatusInternalServerError) l.Error("finding main branch", "error", err.Error()) return } ref = mainBranch }
resp := types.RepoIndexResponse{ IsEmpty: false, Ref: ref, Commits: commits, Description: getDescription(path), Readme: readmeContent, ReadmeFileName: readmeFile, Files: files, Branches: branches, Tags: rtags, TotalCommits: total, }
writeJSON(w, resp)}
func (h *Handle) RepoTree(w http.ResponseWriter, r *http.Request) { treePath := chi.URLParam(r, "*") ref := chi.URLParam(r, "ref") ref, _ = url.PathUnescape(ref)
l := h.l.With("handler", "RepoTree", "ref", ref, "treePath", treePath)
path, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, didPath(r)) gr, err := git.Open(path, ref) if err != nil { notFound(w) return }
files, err := gr.FileTree(r.Context(), treePath) if err != nil { writeError(w, err.Error(), http.StatusInternalServerError) l.Error("file tree", "error", err.Error()) return }
resp := types.RepoTreeResponse{ Ref: ref, Parent: treePath, Description: getDescription(path), DotDot: filepath.Dir(treePath), Files: files, }
writeJSON(w, resp)}
func (h *Handle) BlobRaw(w http.ResponseWriter, r *http.Request) { treePath := chi.URLParam(r, "*") ref := chi.URLParam(r, "ref") ref, _ = url.PathUnescape(ref)
l := h.l.With("handler", "BlobRaw", "ref", ref, "treePath", treePath)
path, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, didPath(r)) gr, err := git.Open(path, ref) if err != nil { notFound(w) return }
contents, err := gr.RawContent(treePath) if err != nil { writeError(w, err.Error(), http.StatusBadRequest) l.Error("file content", "error", err.Error()) return }
mimeType := http.DetectContentType(contents)
// exception for svg if filepath.Ext(treePath) == ".svg" { mimeType = "image/svg+xml" }
contentHash := sha256.Sum256(contents) eTag := fmt.Sprintf("\"%x\"", contentHash)
// allow image, video, and text/plain files to be served directly switch { case strings.HasPrefix(mimeType, "image/"), strings.HasPrefix(mimeType, "video/"): if clientETag := r.Header.Get("If-None-Match"); clientETag == eTag { w.WriteHeader(http.StatusNotModified) return } w.Header().Set("ETag", eTag)
case strings.HasPrefix(mimeType, "text/plain"): w.Header().Set("Cache-Control", "public, no-cache")
default: l.Error("attempted to serve disallowed file type", "mimetype", mimeType) writeError(w, "only image, video, and text files can be accessed directly", http.StatusForbidden) return }
w.Header().Set("Content-Type", mimeType) w.Write(contents)}
func (h *Handle) Blob(w http.ResponseWriter, r *http.Request) { treePath := chi.URLParam(r, "*") ref := chi.URLParam(r, "ref") ref, _ = url.PathUnescape(ref)
l := h.l.With("handler", "Blob", "ref", ref, "treePath", treePath)
path, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, didPath(r)) gr, err := git.Open(path, ref) if err != nil { notFound(w) return }
var isBinaryFile bool = false contents, err := gr.FileContent(treePath) if errors.Is(err, git.ErrBinaryFile) { isBinaryFile = true } else if errors.Is(err, object.ErrFileNotFound) { notFound(w) return } else if err != nil { writeError(w, err.Error(), http.StatusInternalServerError) return }
bytes := []byte(contents) // safe := string(sanitize(bytes)) sizeHint := len(bytes)
resp := types.RepoBlobResponse{ Ref: ref, Contents: string(bytes), Path: treePath, IsBinary: isBinaryFile, SizeHint: uint64(sizeHint), }
h.showFile(resp, w, l)}
func (h *Handle) Archive(w http.ResponseWriter, r *http.Request) { name := chi.URLParam(r, "name") file := chi.URLParam(r, "file")
l := h.l.With("handler", "Archive", "name", name, "file", file)
// TODO: extend this to add more files compression (e.g.: xz) if !strings.HasSuffix(file, ".tar.gz") { notFound(w) return }
ref := strings.TrimSuffix(file, ".tar.gz")
unescapedRef, err := url.PathUnescape(ref) if err != nil { notFound(w) return }
safeRefFilename := strings.ReplaceAll(plumbing.ReferenceName(unescapedRef).Short(), "/", "-")
// This allows the browser to use a proper name for the file when // downloading filename := fmt.Sprintf("%s-%s.tar.gz", name, safeRefFilename) setContentDisposition(w, filename) setGZipMIME(w)
path, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, didPath(r)) gr, err := git.Open(path, unescapedRef) if err != nil { notFound(w) return }
gw := gzip.NewWriter(w) defer gw.Close()
prefix := fmt.Sprintf("%s-%s", name, safeRefFilename) err = gr.WriteTar(gw, prefix) if err != nil { // once we start writing to the body we can't report error anymore // so we are only left with printing the error. l.Error("writing tar file", "error", err.Error()) return }
err = gw.Flush() if err != nil { // once we start writing to the body we can't report error anymore // so we are only left with printing the error. l.Error("flushing?", "error", err.Error()) return }}
func (h *Handle) Log(w http.ResponseWriter, r *http.Request) { ref := chi.URLParam(r, "ref") ref, _ = url.PathUnescape(ref)
path, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, didPath(r))
l := h.l.With("handler", "Log", "ref", ref, "path", path)
gr, err := git.Open(path, ref) if err != nil { notFound(w) return }
// Get page parameters page := 1 pageSize := 30
if pageParam := r.URL.Query().Get("page"); pageParam != "" { if p, err := strconv.Atoi(pageParam); err == nil && p > 0 { page = p } }
if pageSizeParam := r.URL.Query().Get("per_page"); pageSizeParam != "" { if ps, err := strconv.Atoi(pageSizeParam); err == nil && ps > 0 { pageSize = ps } }
// convert to offset/limit offset := (page - 1) * pageSize limit := pageSize
commits, err := gr.Commits(offset, limit) if err != nil { writeError(w, err.Error(), http.StatusInternalServerError) l.Error("fetching commits", "error", err.Error()) return }
total := len(commits)
resp := types.RepoLogResponse{ Commits: commits, Ref: ref, Description: getDescription(path), Log: true, Total: total, Page: page, PerPage: pageSize, }
writeJSON(w, resp)}
func (h *Handle) Diff(w http.ResponseWriter, r *http.Request) { ref := chi.URLParam(r, "ref") ref, _ = url.PathUnescape(ref)
l := h.l.With("handler", "Diff", "ref", ref)
path, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, didPath(r)) gr, err := git.Open(path, ref) if err != nil { notFound(w) return }
diff, err := gr.Diff() if err != nil { writeError(w, err.Error(), http.StatusInternalServerError) l.Error("getting diff", "error", err.Error()) return }
resp := types.RepoCommitResponse{ Ref: ref, Diff: diff, }
writeJSON(w, resp)}
func (h *Handle) Tags(w http.ResponseWriter, r *http.Request) { path, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, didPath(r)) l := h.l.With("handler", "Refs")
gr, err := git.Open(path, "") if err != nil { notFound(w) return }
tags, err := gr.Tags() if err != nil { // Non-fatal, we *should* have at least one branch to show. l.Warn("getting tags", "error", err.Error()) }
rtags := []*types.TagReference{} for _, tag := range tags { var target *object.Tag if tag.Target != plumbing.ZeroHash { target = &tag } tr := types.TagReference{ Tag: target, }
tr.Reference = types.Reference{ Name: tag.Name, Hash: tag.Hash.String(), }
if tag.Message != "" { tr.Message = tag.Message }
rtags = append(rtags, &tr) }
resp := types.RepoTagsResponse{ Tags: rtags, }
writeJSON(w, resp)}
func (h *Handle) Branches(w http.ResponseWriter, r *http.Request) { path, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, didPath(r))
gr, err := git.PlainOpen(path) if err != nil { notFound(w) return }
branches, _ := gr.Branches()
resp := types.RepoBranchesResponse{ Branches: branches, }
writeJSON(w, resp)}
func (h *Handle) Branch(w http.ResponseWriter, r *http.Request) { path, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, didPath(r)) branchName := chi.URLParam(r, "branch") branchName, _ = url.PathUnescape(branchName)
l := h.l.With("handler", "Branch")
gr, err := git.PlainOpen(path) if err != nil { notFound(w) return }
ref, err := gr.Branch(branchName) if err != nil { l.Error("getting branch", "error", err.Error()) writeError(w, err.Error(), http.StatusInternalServerError) return }
commit, err := gr.Commit(ref.Hash()) if err != nil { l.Error("getting commit object", "error", err.Error()) writeError(w, err.Error(), http.StatusInternalServerError) return }
defaultBranch, err := gr.FindMainBranch() isDefault := false if err != nil { l.Error("getting default branch", "error", err.Error()) // do not quit though } else if defaultBranch == branchName { isDefault = true }
resp := types.RepoBranchResponse{ Branch: types.Branch{ Reference: types.Reference{ Name: ref.Name().Short(), Hash: ref.Hash().String(), }, Commit: commit, IsDefault: isDefault, }, }
writeJSON(w, resp)}
func (h *Handle) Keys(w http.ResponseWriter, r *http.Request) { l := h.l.With("handler", "Keys")
switch r.Method { case http.MethodGet: keys, err := h.db.GetAllPublicKeys() if err != nil { writeError(w, err.Error(), http.StatusInternalServerError) l.Error("getting public keys", "error", err.Error()) return }
data := make([]map[string]any, 0) for _, key := range keys { j := key.JSON() data = append(data, j) } writeJSON(w, data) return
case http.MethodPut: pk := db.PublicKey{} if err := json.NewDecoder(r.Body).Decode(&pk); err != nil { writeError(w, "invalid request body", http.StatusBadRequest) return }
_, _, _, _, err := ssh.ParseAuthorizedKey([]byte(pk.Key)) if err != nil { writeError(w, "invalid pubkey", http.StatusBadRequest) }
if err := h.db.AddPublicKey(pk); err != nil { writeError(w, err.Error(), http.StatusInternalServerError) l.Error("adding public key", "error", err.Error()) return }
w.WriteHeader(http.StatusNoContent) return }}
// func (h *Handle) RepoForkAheadBehind(w http.ResponseWriter, r *http.Request) {// l := h.l.With("handler", "RepoForkSync")//// data := struct {// Did string `json:"did"`// Source string `json:"source"`// Name string `json:"name,omitempty"`// HiddenRef string `json:"hiddenref"`// }{}//// if err := json.NewDecoder(r.Body).Decode(&data); err != nil {// writeError(w, "invalid request body", http.StatusBadRequest)// return// }//// did := data.Did// source := data.Source//// if did == "" || source == "" {// l.Error("invalid request body, empty did or name")// w.WriteHeader(http.StatusBadRequest)// return// }//// var name string// if data.Name != "" {// name = data.Name// } else {// name = filepath.Base(source)// }//// branch := chi.URLParam(r, "branch")// branch, _ = url.PathUnescape(branch)//// relativeRepoPath := filepath.Join(did, name)// repoPath, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, relativeRepoPath)//// gr, err := git.PlainOpen(repoPath)// if err != nil {// log.Println(err)// notFound(w)// return// }//// forkCommit, err := gr.ResolveRevision(branch)// if err != nil {// l.Error("error resolving ref revision", "msg", err.Error())// writeError(w, fmt.Sprintf("error resolving revision %s", branch), http.StatusBadRequest)// return// }//// sourceCommit, err := gr.ResolveRevision(data.HiddenRef)// if err != nil {// l.Error("error resolving hidden ref revision", "msg", err.Error())// writeError(w, fmt.Sprintf("error resolving revision %s", data.HiddenRef), http.StatusBadRequest)// return// }//// status := types.UpToDate// if forkCommit.Hash.String() != sourceCommit.Hash.String() {// isAncestor, err := forkCommit.IsAncestor(sourceCommit)// if err != nil {// log.Printf("error resolving whether %s is ancestor of %s: %s", branch, data.HiddenRef, err)// return// }//// if isAncestor {// status = types.FastForwardable// } else {// status = types.Conflict// }// }//// w.Header().Set("Content-Type", "application/json")// json.NewEncoder(w).Encode(types.AncestorCheckResponse{Status: status})// }
func (h *Handle) RepoLanguages(w http.ResponseWriter, r *http.Request) { repoPath, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, didPath(r)) ref := chi.URLParam(r, "ref") ref, _ = url.PathUnescape(ref)
l := h.l.With("handler", "RepoLanguages")
gr, err := git.Open(repoPath, ref) if err != nil { l.Error("opening repo", "error", err.Error()) notFound(w) return }
ctx, cancel := context.WithTimeout(r.Context(), 1*time.Second) defer cancel()
sizes, err := gr.AnalyzeLanguages(ctx) if err != nil { l.Error("failed to analyze languages", "error", err.Error()) writeError(w, err.Error(), http.StatusNoContent) return }
resp := types.RepoLanguageResponse{Languages: sizes}
writeJSON(w, resp)}
// func (h *Handle) RepoForkSync(w http.ResponseWriter, r *http.Request) {// l := h.l.With("handler", "RepoForkSync")//// data := struct {// Did string `json:"did"`// Source string `json:"source"`// Name string `json:"name,omitempty"`// }{}//// if err := json.NewDecoder(r.Body).Decode(&data); err != nil {// writeError(w, "invalid request body", http.StatusBadRequest)// return// }//// did := data.Did// source := data.Source//// if did == "" || source == "" {// l.Error("invalid request body, empty did or name")// w.WriteHeader(http.StatusBadRequest)// return// }//// var name string// if data.Name != "" {// name = data.Name// } else {// name = filepath.Base(source)// }//// branch := chi.URLParam(r, "branch")// branch, _ = url.PathUnescape(branch)//// relativeRepoPath := filepath.Join(did, name)// repoPath, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, relativeRepoPath)//// gr, err := git.Open(repoPath, branch)// if err != nil {// log.Println(err)// notFound(w)// return// }//// err = gr.Sync()// if err != nil {// l.Error("error syncing repo fork", "error", err.Error())// writeError(w, err.Error(), http.StatusInternalServerError)// return// }//// w.WriteHeader(http.StatusNoContent)// }
// func (h *Handle) RepoFork(w http.ResponseWriter, r *http.Request) {// l := h.l.With("handler", "RepoFork")//// data := struct {// Did string `json:"did"`// Source string `json:"source"`// Name string `json:"name,omitempty"`// }{}//// if err := json.NewDecoder(r.Body).Decode(&data); err != nil {// writeError(w, "invalid request body", http.StatusBadRequest)// return// }//// did := data.Did// source := data.Source//// if did == "" || source == "" {// l.Error("invalid request body, empty did or name")// w.WriteHeader(http.StatusBadRequest)// return// }//// var name string// if data.Name != "" {// name = data.Name// } else {// name = filepath.Base(source)// }//// relativeRepoPath := filepath.Join(did, name)// repoPath, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, relativeRepoPath)//// err := git.Fork(repoPath, source)// if err != nil {// l.Error("forking repo", "error", err.Error())// writeError(w, err.Error(), http.StatusInternalServerError)// return// }//// // add perms for this user to access the repo// err = h.e.AddRepo(did, rbac.ThisServer, relativeRepoPath)// if err != nil {// l.Error("adding repo permissions", "error", err.Error())// writeError(w, err.Error(), http.StatusInternalServerError)// return// }//// hook.SetupRepo(// hook.Config(// hook.WithScanPath(h.c.Repo.ScanPath),// hook.WithInternalApi(h.c.Server.InternalListenAddr),// ),// repoPath,// )//// w.WriteHeader(http.StatusNoContent)// }
// func (h *Handle) RemoveRepo(w http.ResponseWriter, r *http.Request) {// l := h.l.With("handler", "RemoveRepo")//// data := struct {// Did string `json:"did"`// Name string `json:"name"`// }{}//// if err := json.NewDecoder(r.Body).Decode(&data); err != nil {// writeError(w, "invalid request body", http.StatusBadRequest)// return// }//// did := data.Did// name := data.Name//// if did == "" || name == "" {// l.Error("invalid request body, empty did or name")// w.WriteHeader(http.StatusBadRequest)// return// }//// relativeRepoPath := filepath.Join(did, name)// repoPath, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, relativeRepoPath)// err := os.RemoveAll(repoPath)// if err != nil {// l.Error("removing repo", "error", err.Error())// writeError(w, err.Error(), http.StatusInternalServerError)// return// }//// w.WriteHeader(http.StatusNoContent)//// }
// func (h *Handle) Merge(w http.ResponseWriter, r *http.Request) {// path, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, didPath(r))//// data := types.MergeRequest{}//// if err := json.NewDecoder(r.Body).Decode(&data); err != nil {// writeError(w, err.Error(), http.StatusBadRequest)// h.l.Error("git: failed to unmarshal json patch", "handler", "Merge", "error", err)// return// }//// mo := &git.MergeOptions{// AuthorName: data.AuthorName,// AuthorEmail: data.AuthorEmail,// CommitBody: data.CommitBody,// CommitMessage: data.CommitMessage,// }//// patch := data.Patch// branch := data.Branch// gr, err := git.Open(path, branch)// if err != nil {// notFound(w)// return// }//// mo.FormatPatch = patchutil.IsFormatPatch(patch)//// if err := gr.MergeWithOptions([]byte(patch), branch, mo); err != nil {// var mergeErr *git.ErrMerge// if errors.As(err, &mergeErr) {// conflicts := make([]types.ConflictInfo, len(mergeErr.Conflicts))// for i, conflict := range mergeErr.Conflicts {// conflicts[i] = types.ConflictInfo{// Filename: conflict.Filename,// Reason: conflict.Reason,// }// }// response := types.MergeCheckResponse{// IsConflicted: true,// Conflicts: conflicts,// Message: mergeErr.Message,// }// writeConflict(w, response)// h.l.Error("git: merge conflict", "handler", "Merge", "error", mergeErr)// } else {// writeError(w, err.Error(), http.StatusBadRequest)// h.l.Error("git: failed to merge", "handler", "Merge", "error", err.Error())// }// return// }//// w.WriteHeader(http.StatusOK)// }
// func (h *Handle) MergeCheck(w http.ResponseWriter, r *http.Request) {// path, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, didPath(r))//// var data struct {// Patch string `json:"patch"`// Branch string `json:"branch"`// }//// if err := json.NewDecoder(r.Body).Decode(&data); err != nil {// writeError(w, err.Error(), http.StatusBadRequest)// h.l.Error("git: failed to unmarshal json patch", "handler", "MergeCheck", "error", err)// return// }//// patch := data.Patch// branch := data.Branch// gr, err := git.Open(path, branch)// if err != nil {// notFound(w)// return// }//// err = gr.MergeCheck([]byte(patch), branch)// if err == nil {// response := types.MergeCheckResponse{// IsConflicted: false,// }// writeJSON(w, response)// return// }//// var mergeErr *git.ErrMerge// if errors.As(err, &mergeErr) {// conflicts := make([]types.ConflictInfo, len(mergeErr.Conflicts))// for i, conflict := range mergeErr.Conflicts {// conflicts[i] = types.ConflictInfo{// Filename: conflict.Filename,// Reason: conflict.Reason,// }// }// response := types.MergeCheckResponse{// IsConflicted: true,// Conflicts: conflicts,// Message: mergeErr.Message,// }// writeConflict(w, response)// h.l.Error("git: merge conflict", "handler", "MergeCheck", "error", mergeErr.Error())// return// }// writeError(w, err.Error(), http.StatusInternalServerError)// h.l.Error("git: failed to check merge", "handler", "MergeCheck", "error", err.Error())// }
func (h *Handle) Compare(w http.ResponseWriter, r *http.Request) { rev1 := chi.URLParam(r, "rev1") rev1, _ = url.PathUnescape(rev1)
rev2 := chi.URLParam(r, "rev2") rev2, _ = url.PathUnescape(rev2)
l := h.l.With("handler", "Compare", "r1", rev1, "r2", rev2)
path, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, didPath(r)) gr, err := git.PlainOpen(path) if err != nil { notFound(w) return }
commit1, err := gr.ResolveRevision(rev1) if err != nil { l.Error("error resolving revision 1", "msg", err.Error()) writeError(w, fmt.Sprintf("error resolving revision %s", rev1), http.StatusBadRequest) return }
commit2, err := gr.ResolveRevision(rev2) if err != nil { l.Error("error resolving revision 2", "msg", err.Error()) writeError(w, fmt.Sprintf("error resolving revision %s", rev2), http.StatusBadRequest) return }
rawPatch, formatPatch, err := gr.FormatPatch(commit1, commit2) if err != nil { l.Error("error comparing revisions", "msg", err.Error()) writeError(w, "error comparing revisions", http.StatusBadRequest) return }
writeJSON(w, types.RepoFormatPatchResponse{ Rev1: commit1.Hash.String(), Rev2: commit2.Hash.String(), FormatPatch: formatPatch, Patch: rawPatch, })}
func (h *Handle) DefaultBranch(w http.ResponseWriter, r *http.Request) { l := h.l.With("handler", "DefaultBranch") path, _ := securejoin.SecureJoin(h.c.Repo.ScanPath, didPath(r))
gr, err := git.Open(path, "") if err != nil { notFound(w) return }
branch, err := gr.FindMainBranch() if err != nil { writeError(w, err.Error(), http.StatusInternalServerError) l.Error("getting default branch", "error", err.Error()) return }
writeJSON(w, types.RepoDefaultBranchResponse{ Branch: branch, })}