Mirror of @tangled.org/core. Running on a Raspberry Pi Zero 2 (Please be gentle).
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

patchutil: move AsDiff and AsNiceDiff to patchutil

Also move FormatPatch to types to avoid an import cycle.

authored by

Anirudh Oppiliappan and committed by
Tangled
74b9d52b df75814b

+114 -104
+2 -69
appview/db/pulls.go
··· 9 9 "strings" 10 10 "time" 11 11 12 - "github.com/bluekeyes/go-gitdiff/gitdiff" 13 12 "github.com/bluesky-social/indigo/atproto/syntax" 14 13 "tangled.sh/tangled.sh/core/api/tangled" 15 14 "tangled.sh/tangled.sh/core/patchutil" ··· 202 203 return p.StackId != "" 203 204 } 204 205 205 - func (s PullSubmission) AsDiff(targetBranch string) ([]*gitdiff.File, error) { 206 - patch := s.Patch 207 - 208 - // if format-patch; then extract each patch 209 - var diffs []*gitdiff.File 210 - if patchutil.IsFormatPatch(patch) { 211 - patches, err := patchutil.ExtractPatches(patch) 212 - if err != nil { 213 - return nil, err 214 - } 215 - var ps [][]*gitdiff.File 216 - for _, p := range patches { 217 - ps = append(ps, p.Files) 218 - } 219 - 220 - diffs = patchutil.CombineDiff(ps...) 221 - } else { 222 - d, _, err := gitdiff.Parse(strings.NewReader(patch)) 223 - if err != nil { 224 - return nil, err 225 - } 226 - diffs = d 227 - } 228 - 229 - return diffs, nil 230 - } 231 - 232 - func (s PullSubmission) AsNiceDiff(targetBranch string) types.NiceDiff { 233 - diffs, err := s.AsDiff(targetBranch) 234 - if err != nil { 235 - log.Println(err) 236 - } 237 - 238 - nd := types.NiceDiff{} 239 - nd.Commit.Parent = targetBranch 240 - 241 - for _, d := range diffs { 242 - ndiff := types.Diff{} 243 - ndiff.Name.New = d.NewName 244 - ndiff.Name.Old = d.OldName 245 - ndiff.IsBinary = d.IsBinary 246 - ndiff.IsNew = d.IsNew 247 - ndiff.IsDelete = d.IsDelete 248 - ndiff.IsCopy = d.IsCopy 249 - ndiff.IsRename = d.IsRename 250 - 251 - for _, tf := range d.TextFragments { 252 - ndiff.TextFragments = append(ndiff.TextFragments, *tf) 253 - for _, l := range tf.Lines { 254 - switch l.Op { 255 - case gitdiff.OpAdd: 256 - nd.Stat.Insertions += 1 257 - case gitdiff.OpDelete: 258 - nd.Stat.Deletions += 1 259 - } 260 - } 261 - } 262 - 263 - nd.Diff = append(nd.Diff, ndiff) 264 - } 265 - 266 - nd.Stat.FilesChanged = len(diffs) 267 - 268 - return nd 269 - } 270 - 271 206 func (s PullSubmission) IsFormatPatch() bool { 272 207 return patchutil.IsFormatPatch(s.Patch) 273 208 } 274 209 275 - func (s PullSubmission) AsFormatPatch() []patchutil.FormatPatch { 210 + func (s PullSubmission) AsFormatPatch() []types.FormatPatch { 276 211 patches, err := patchutil.ExtractPatches(s.Patch) 277 212 if err != nil { 278 213 log.Println("error extracting patches from submission:", err) 279 - return []patchutil.FormatPatch{} 214 + return []types.FormatPatch{} 280 215 } 281 216 282 217 return patches
+6 -5
appview/state/pull.go
··· 305 305 } 306 306 } 307 307 308 - diff := pull.Submissions[roundIdInt].AsNiceDiff(pull.TargetBranch) 308 + patch := pull.Submissions[roundIdInt].Patch 309 + diff := patchutil.AsNiceDiff(patch, pull.TargetBranch) 309 310 310 311 s.pages.RepoPullPatchPage(w, pages.RepoPullPatchParams{ 311 312 LoggedInUser: user, ··· 362 361 } 363 362 } 364 363 365 - currentPatch, err := pull.Submissions[roundIdInt].AsDiff(pull.TargetBranch) 364 + currentPatch, err := patchutil.AsDiff(pull.Submissions[roundIdInt].Patch) 366 365 if err != nil { 367 366 log.Println("failed to interdiff; current patch malformed") 368 367 s.pages.Notice(w, fmt.Sprintf("interdiff-error-%d", roundIdInt), "Failed to calculate interdiff; current patch is invalid.") 369 368 return 370 369 } 371 370 372 - previousPatch, err := pull.Submissions[roundIdInt-1].AsDiff(pull.TargetBranch) 371 + previousPatch, err := patchutil.AsDiff(pull.Submissions[roundIdInt-1].Patch) 373 372 if err != nil { 374 373 log.Println("failed to interdiff; previous patch malformed") 375 374 s.pages.Notice(w, fmt.Sprintf("interdiff-error-%d", roundIdInt), "Failed to calculate interdiff; previous patch is invalid.") ··· 1144 1143 return 1145 1144 } 1146 1145 1147 - branches := result.Branches 1146 + branches := result.Branches 1148 1147 sort.Slice(branches, func(i int, j int) bool { 1149 1148 return branches[i].Commit.Committer.When.After(branches[j].Commit.Committer.When) 1150 1149 }) ··· 1234 1233 1235 1234 s.pages.PullCompareForkBranchesFragment(w, pages.PullCompareForkBranchesParams{ 1236 1235 RepoInfo: f.RepoInfo(s, user), 1237 - SourceBranches: sourceResult.Branches, 1236 + SourceBranches: sourceBranches, 1238 1237 TargetBranches: targetResult.Branches, 1239 1238 }) 1240 1239 }
+9 -5
appview/state/repo.go
··· 2084 2084 return 2085 2085 } 2086 2086 2087 - forks, err := db.GetForksByDid(s.db, user.Did) 2088 - if err != nil { 2089 - s.pages.Notice(w, "compare-error", "Failed to produce comparison. Try again later.") 2090 - log.Println("failed to get forks", err) 2091 - return 2087 + var forks []db.Repo 2088 + if user != nil { 2089 + var err error 2090 + forks, err = db.GetForksByDid(s.db, user.Did) 2091 + if err != nil { 2092 + s.pages.Notice(w, "compare-error", "Failed to produce comparison. Try again later.") 2093 + log.Println("failed to get forks", err) 2094 + return 2095 + } 2092 2096 } 2093 2097 2094 2098 s.pages.RepoCompare(w, pages.RepoCompareParams{
+3 -3
knotserver/git/diff.go
··· 127 127 128 128 // FormatPatch generates a git-format-patch output between two commits, 129 129 // and returns the raw format-patch series, a parsed FormatPatch and an error. 130 - func (g *GitRepo) formatSinglePatch(base, commit2 plumbing.Hash, extraArgs ...string) (string, *patchutil.FormatPatch, error) { 130 + func (g *GitRepo) formatSinglePatch(base, commit2 plumbing.Hash, extraArgs ...string) (string, *types.FormatPatch, error) { 131 131 var stdout bytes.Buffer 132 132 133 133 args := []string{ ··· 222 222 return commits, nil 223 223 } 224 224 225 - func (g *GitRepo) FormatPatch(base, commit2 *object.Commit) (string, []patchutil.FormatPatch, error) { 225 + func (g *GitRepo) FormatPatch(base, commit2 *object.Commit) (string, []types.FormatPatch, error) { 226 226 // get list of commits between commir2 and base 227 227 commits, err := g.commitsBetween(commit2, base) 228 228 if err != nil { ··· 233 233 slices.Reverse(commits) 234 234 235 235 var allPatchesContent strings.Builder 236 - var allPatches []patchutil.FormatPatch 236 + var allPatches []types.FormatPatch 237 237 238 238 for _, commit := range commits { 239 239 changeId := ""
+69 -16
patchutil/patchutil.go
··· 2 2 3 3 import ( 4 4 "fmt" 5 + "log" 5 6 "os" 6 7 "os/exec" 7 8 "regexp" ··· 10 9 "strings" 11 10 12 11 "github.com/bluekeyes/go-gitdiff/gitdiff" 12 + "tangled.sh/tangled.sh/core/types" 13 13 ) 14 14 15 - type FormatPatch struct { 16 - Files []*gitdiff.File 17 - *gitdiff.PatchHeader 18 - Raw string 19 - } 20 - 21 - func (f FormatPatch) ChangeId() (string, error) { 22 - if vals, ok := f.RawHeaders["Change-Id"]; ok && len(vals) == 1 { 23 - return vals[0], nil 24 - } 25 - return "", fmt.Errorf("no change-id found") 26 - } 27 - 28 - func ExtractPatches(formatPatch string) ([]FormatPatch, error) { 15 + func ExtractPatches(formatPatch string) ([]types.FormatPatch, error) { 29 16 patches := splitFormatPatch(formatPatch) 30 17 31 - result := []FormatPatch{} 18 + result := []types.FormatPatch{} 32 19 33 20 for _, patch := range patches { 34 21 files, headerStr, err := gitdiff.Parse(strings.NewReader(patch)) ··· 29 40 return nil, fmt.Errorf("failed to parse patch header: %w", err) 30 41 } 31 42 32 - result = append(result, FormatPatch{ 43 + result = append(result, types.FormatPatch{ 33 44 Files: files, 34 45 PatchHeader: header, 35 46 Raw: patch, ··· 251 262 slices.SortFunc(patch, func(a, b *gitdiff.File) int { 252 263 return strings.Compare(bestName(a), bestName(b)) 253 264 }) 265 + } 266 + 267 + func AsDiff(patch string) ([]*gitdiff.File, error) { 268 + // if format-patch; then extract each patch 269 + var diffs []*gitdiff.File 270 + if IsFormatPatch(patch) { 271 + patches, err := ExtractPatches(patch) 272 + if err != nil { 273 + return nil, err 274 + } 275 + var ps [][]*gitdiff.File 276 + for _, p := range patches { 277 + ps = append(ps, p.Files) 278 + } 279 + 280 + diffs = CombineDiff(ps...) 281 + } else { 282 + d, _, err := gitdiff.Parse(strings.NewReader(patch)) 283 + if err != nil { 284 + return nil, err 285 + } 286 + diffs = d 287 + } 288 + 289 + return diffs, nil 290 + } 291 + 292 + func AsNiceDiff(patch, targetBranch string) types.NiceDiff { 293 + diffs, err := AsDiff(patch) 294 + if err != nil { 295 + log.Println(err) 296 + } 297 + 298 + nd := types.NiceDiff{} 299 + nd.Commit.Parent = targetBranch 300 + 301 + for _, d := range diffs { 302 + ndiff := types.Diff{} 303 + ndiff.Name.New = d.NewName 304 + ndiff.Name.Old = d.OldName 305 + ndiff.IsBinary = d.IsBinary 306 + ndiff.IsNew = d.IsNew 307 + ndiff.IsDelete = d.IsDelete 308 + ndiff.IsCopy = d.IsCopy 309 + ndiff.IsRename = d.IsRename 310 + 311 + for _, tf := range d.TextFragments { 312 + ndiff.TextFragments = append(ndiff.TextFragments, *tf) 313 + for _, l := range tf.Lines { 314 + switch l.Op { 315 + case gitdiff.OpAdd: 316 + nd.Stat.Insertions += 1 317 + case gitdiff.OpDelete: 318 + nd.Stat.Deletions += 1 319 + } 320 + } 321 + } 322 + 323 + nd.Diff = append(nd.Diff, ndiff) 324 + } 325 + 326 + nd.Stat.FilesChanged = len(diffs) 327 + 328 + return nd 254 329 }
+20
types/patch.go
··· 1 + package types 2 + 3 + import ( 4 + "fmt" 5 + 6 + "github.com/bluekeyes/go-gitdiff/gitdiff" 7 + ) 8 + 9 + type FormatPatch struct { 10 + Files []*gitdiff.File 11 + *gitdiff.PatchHeader 12 + Raw string 13 + } 14 + 15 + func (f FormatPatch) ChangeId() (string, error) { 16 + if vals, ok := f.RawHeaders["Change-Id"]; ok && len(vals) == 1 { 17 + return vals[0], nil 18 + } 19 + return "", fmt.Errorf("no change-id found") 20 + }
+5 -6
types/repo.go
··· 2 2 3 3 import ( 4 4 "github.com/go-git/go-git/v5/plumbing/object" 5 - "tangled.sh/tangled.sh/core/patchutil" 6 5 ) 7 6 8 7 type RepoIndexResponse struct { ··· 33 34 } 34 35 35 36 type RepoFormatPatchResponse struct { 36 - Rev1 string `json:"rev1,omitempty"` 37 - Rev2 string `json:"rev2,omitempty"` 38 - FormatPatch []patchutil.FormatPatch `json:"format_patch,omitempty"` 39 - MergeBase string `json:"merge_base,omitempty"` 40 - Patch string `json:"patch,omitempty"` 37 + Rev1 string `json:"rev1,omitempty"` 38 + Rev2 string `json:"rev2,omitempty"` 39 + FormatPatch []FormatPatch `json:"format_patch,omitempty"` 40 + MergeBase string `json:"merge_base,omitempty"` 41 + Patch string `json:"patch,omitempty"` 41 42 } 42 43 43 44 type RepoTreeResponse struct {