···20842084 return20852085 }2086208620872087- forks, err := db.GetForksByDid(s.db, user.Did)20882088- if err != nil {20892089- s.pages.Notice(w, "compare-error", "Failed to produce comparison. Try again later.")20902090- log.Println("failed to get forks", err)20912091- return20872087+ var forks []db.Repo20882088+ if user != nil {20892089+ var err error20902090+ forks, err = db.GetForksByDid(s.db, user.Did)20912091+ if err != nil {20922092+ s.pages.Notice(w, "compare-error", "Failed to produce comparison. Try again later.")20932093+ log.Println("failed to get forks", err)20942094+ return20952095+ }20922096 }2093209720942098 s.pages.RepoCompare(w, pages.RepoCompareParams{
+3-3
knotserver/git/diff.go
···127127128128// FormatPatch generates a git-format-patch output between two commits,129129// and returns the raw format-patch series, a parsed FormatPatch and an error.130130-func (g *GitRepo) formatSinglePatch(base, commit2 plumbing.Hash, extraArgs ...string) (string, *patchutil.FormatPatch, error) {130130+func (g *GitRepo) formatSinglePatch(base, commit2 plumbing.Hash, extraArgs ...string) (string, *types.FormatPatch, error) {131131 var stdout bytes.Buffer132132133133 args := []string{···222222 return commits, nil223223}224224225225-func (g *GitRepo) FormatPatch(base, commit2 *object.Commit) (string, []patchutil.FormatPatch, error) {225225+func (g *GitRepo) FormatPatch(base, commit2 *object.Commit) (string, []types.FormatPatch, error) {226226 // get list of commits between commir2 and base227227 commits, err := g.commitsBetween(commit2, base)228228 if err != nil {···233233 slices.Reverse(commits)234234235235 var allPatchesContent strings.Builder236236- var allPatches []patchutil.FormatPatch236236+ var allPatches []types.FormatPatch237237238238 for _, commit := range commits {239239 changeId := ""
+69-16
patchutil/patchutil.go
···2233import (44 "fmt"55+ "log"56 "os"67 "os/exec"78 "regexp"···109 "strings"11101211 "github.com/bluekeyes/go-gitdiff/gitdiff"1212+ "tangled.sh/tangled.sh/core/types"1313)14141515-type FormatPatch struct {1616- Files []*gitdiff.File1717- *gitdiff.PatchHeader1818- Raw string1919-}2020-2121-func (f FormatPatch) ChangeId() (string, error) {2222- if vals, ok := f.RawHeaders["Change-Id"]; ok && len(vals) == 1 {2323- return vals[0], nil2424- }2525- return "", fmt.Errorf("no change-id found")2626-}2727-2828-func ExtractPatches(formatPatch string) ([]FormatPatch, error) {1515+func ExtractPatches(formatPatch string) ([]types.FormatPatch, error) {2916 patches := splitFormatPatch(formatPatch)30173131- result := []FormatPatch{}1818+ result := []types.FormatPatch{}32193320 for _, patch := range patches {3421 files, headerStr, err := gitdiff.Parse(strings.NewReader(patch))···2940 return nil, fmt.Errorf("failed to parse patch header: %w", err)3041 }31423232- result = append(result, FormatPatch{4343+ result = append(result, types.FormatPatch{3344 Files: files,3445 PatchHeader: header,3546 Raw: patch,···251262 slices.SortFunc(patch, func(a, b *gitdiff.File) int {252263 return strings.Compare(bestName(a), bestName(b))253264 })265265+}266266+267267+func AsDiff(patch string) ([]*gitdiff.File, error) {268268+ // if format-patch; then extract each patch269269+ var diffs []*gitdiff.File270270+ if IsFormatPatch(patch) {271271+ patches, err := ExtractPatches(patch)272272+ if err != nil {273273+ return nil, err274274+ }275275+ var ps [][]*gitdiff.File276276+ for _, p := range patches {277277+ ps = append(ps, p.Files)278278+ }279279+280280+ diffs = CombineDiff(ps...)281281+ } else {282282+ d, _, err := gitdiff.Parse(strings.NewReader(patch))283283+ if err != nil {284284+ return nil, err285285+ }286286+ diffs = d287287+ }288288+289289+ return diffs, nil290290+}291291+292292+func AsNiceDiff(patch, targetBranch string) types.NiceDiff {293293+ diffs, err := AsDiff(patch)294294+ if err != nil {295295+ log.Println(err)296296+ }297297+298298+ nd := types.NiceDiff{}299299+ nd.Commit.Parent = targetBranch300300+301301+ for _, d := range diffs {302302+ ndiff := types.Diff{}303303+ ndiff.Name.New = d.NewName304304+ ndiff.Name.Old = d.OldName305305+ ndiff.IsBinary = d.IsBinary306306+ ndiff.IsNew = d.IsNew307307+ ndiff.IsDelete = d.IsDelete308308+ ndiff.IsCopy = d.IsCopy309309+ ndiff.IsRename = d.IsRename310310+311311+ for _, tf := range d.TextFragments {312312+ ndiff.TextFragments = append(ndiff.TextFragments, *tf)313313+ for _, l := range tf.Lines {314314+ switch l.Op {315315+ case gitdiff.OpAdd:316316+ nd.Stat.Insertions += 1317317+ case gitdiff.OpDelete:318318+ nd.Stat.Deletions += 1319319+ }320320+ }321321+ }322322+323323+ nd.Diff = append(nd.Diff, ndiff)324324+ }325325+326326+ nd.Stat.FilesChanged = len(diffs)327327+328328+ return nd254329}
+20
types/patch.go
···11+package types22+33+import (44+ "fmt"55+66+ "github.com/bluekeyes/go-gitdiff/gitdiff"77+)88+99+type FormatPatch struct {1010+ Files []*gitdiff.File1111+ *gitdiff.PatchHeader1212+ Raw string1313+}1414+1515+func (f FormatPatch) ChangeId() (string, error) {1616+ if vals, ok := f.RawHeaders["Change-Id"]; ok && len(vals) == 1 {1717+ return vals[0], nil1818+ }1919+ return "", fmt.Errorf("no change-id found")2020+}