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.

appview,knotserver: produce combined patch in comparisons

this is calculated by the knotserver in sh.tangled.repo.compare and
cached by the appview in pull submissions, this cannot be calculated on
the appview side with just the format-patch because this calculation
requires a git-index.

Signed-off-by: oppiliappan <me@oppi.li>

authored by

oppiliappan and committed by
Tangled
898d826c ce1f2b90

+108 -76
+9
appview/db/db.go
··· 1097 1097 }) 1098 1098 conn.ExecContext(ctx, "pragma foreign_keys = on;") 1099 1099 1100 + // knots may report the combined patch for a comparison, we can store that on the appview side 1101 + // (but not on the pds record), because calculating the combined patch requires a git index 1102 + runMigration(conn, logger, "add-combined-column-submissions", func(tx *sql.Tx) error { 1103 + _, err := tx.Exec(` 1104 + alter table pull_submissions add column combined text; 1105 + `) 1106 + return err 1107 + }) 1108 + 1100 1109 return &DB{ 1101 1110 db, 1102 1111 logger,
+21 -17
appview/db/pulls.go
··· 90 90 pull.ID = int(id) 91 91 92 92 _, err = tx.Exec(` 93 - insert into pull_submissions (pull_at, round_number, patch, source_rev) 94 - values (?, ?, ?, ?) 95 - `, pull.PullAt(), 0, pull.Submissions[0].Patch, pull.Submissions[0].SourceRev) 93 + insert into pull_submissions (pull_at, round_number, patch, combined, source_rev) 94 + values (?, ?, ?, ?, ?) 95 + `, pull.PullAt(), 0, pull.Submissions[0].Patch, pull.Submissions[0].Combined, pull.Submissions[0].SourceRev) 96 96 return err 97 97 } 98 98 ··· 313 313 pull_at, 314 314 round_number, 315 315 patch, 316 + combined, 316 317 created, 317 318 source_rev 318 319 from ··· 333 332 334 333 for rows.Next() { 335 334 var submission models.PullSubmission 336 - var createdAt string 337 - var sourceRev sql.NullString 335 + var submissionCreatedStr string 336 + var submissionSourceRev, submissionCombined sql.NullString 338 337 err := rows.Scan( 339 338 &submission.ID, 340 339 &submission.PullAt, 341 340 &submission.RoundNumber, 342 341 &submission.Patch, 343 - &createdAt, 344 - &sourceRev, 342 + &submissionCombined, 343 + &submissionCreatedStr, 344 + &submissionSourceRev, 345 345 ) 346 346 if err != nil { 347 347 return nil, err 348 348 } 349 349 350 - createdTime, err := time.Parse(time.RFC3339, createdAt) 351 - if err != nil { 352 - return nil, err 350 + if t, err := time.Parse(time.RFC3339, submissionCreatedStr); err == nil { 351 + submission.Created = t 353 352 } 354 - submission.Created = createdTime 355 353 356 - if sourceRev.Valid { 357 - submission.SourceRev = sourceRev.String 354 + if submissionSourceRev.Valid { 355 + submission.SourceRev = submissionSourceRev.String 356 + } 357 + 358 + if submissionCombined.Valid { 359 + submission.Combined = submissionCombined.String 358 360 } 359 361 360 362 submissionMap[submission.ID] = &submission ··· 594 590 return err 595 591 } 596 592 597 - func ResubmitPull(e Execer, pullAt syntax.ATURI, newRoundNumber int, newPatch string, newSourceRev string) error { 593 + func ResubmitPull(e Execer, pullAt syntax.ATURI, newRoundNumber int, newPatch string, combinedPatch string, newSourceRev string) error { 598 594 _, err := e.Exec(` 599 - insert into pull_submissions (pull_at, round_number, patch, source_rev) 600 - values (?, ?, ?, ?) 601 - `, pullAt, newRoundNumber, newPatch, newSourceRev) 595 + insert into pull_submissions (pull_at, round_number, patch, combined, source_rev) 596 + values (?, ?, ?, ?, ?) 597 + `, pullAt, newRoundNumber, newPatch, combinedPatch, newSourceRev) 602 598 603 599 return err 604 600 }
+19 -8
appview/models/pull.go
··· 125 125 // content 126 126 RoundNumber int 127 127 Patch string 128 + Combined string 128 129 Comments []PullComment 129 130 SourceRev string // include the rev that was used to create this submission: only for branch/fork PRs 130 131 ··· 151 150 Created time.Time 152 151 } 153 152 153 + func (p *Pull) LastRoundNumber() int { 154 + return len(p.Submissions) - 1 155 + } 156 + 157 + func (p *Pull) LatestSubmission() *PullSubmission { 158 + return p.Submissions[p.LastRoundNumber()] 159 + } 160 + 154 161 func (p *Pull) LatestPatch() string { 155 - latestSubmission := p.Submissions[p.LastRoundNumber()] 156 - return latestSubmission.Patch 162 + return p.LatestSubmission().Patch 157 163 } 158 164 159 165 func (p *Pull) LatestSha() string { 160 - latestSubmission := p.Submissions[p.LastRoundNumber()] 161 - return latestSubmission.SourceRev 166 + return p.LatestSubmission().SourceRev 162 167 } 163 168 164 169 func (p *Pull) PullAt() syntax.ATURI { 165 170 return syntax.ATURI(fmt.Sprintf("at://%s/%s/%s", p.OwnerDid, tangled.RepoPullNSID, p.Rkey)) 166 - } 167 - 168 - func (p *Pull) LastRoundNumber() int { 169 - return len(p.Submissions) - 1 170 171 } 171 172 172 173 func (p *Pull) IsPatchBased() bool { ··· 255 252 } 256 253 257 254 return participants 255 + } 256 + 257 + func (s PullSubmission) CombinedPatch() string { 258 + if s.Combined == "" { 259 + return s.Patch 260 + } 261 + 262 + return s.Combined 258 263 } 259 264 260 265 type Stack []*Pull
+26 -41
appview/pulls/pulls.go
··· 145 145 stack, _ := r.Context().Value("stack").(models.Stack) 146 146 abandonedPulls, _ := r.Context().Value("abandonedPulls").([]*models.Pull) 147 147 148 - totalIdents := 1 149 - for _, submission := range pull.Submissions { 150 - totalIdents += len(submission.Comments) 151 - } 152 - 153 - identsToResolve := make([]string, totalIdents) 154 - 155 - // populate idents 156 - identsToResolve[0] = pull.OwnerDid 157 - idx := 1 158 - for _, submission := range pull.Submissions { 159 - for _, comment := range submission.Comments { 160 - identsToResolve[idx] = comment.OwnerDid 161 - idx += 1 162 - } 163 - } 164 - 165 148 mergeCheckResponse := s.mergeCheck(r, f, pull, stack) 166 149 branchDeleteStatus := s.branchDeleteStatus(r, f, pull) 167 150 resubmitResult := pages.Unknown ··· 442 459 return 443 460 } 444 461 445 - patch := pull.Submissions[roundIdInt].Patch 462 + patch := pull.Submissions[roundIdInt].CombinedPatch() 446 463 diff := patchutil.AsNiceDiff(patch, pull.TargetBranch) 447 464 448 465 s.pages.RepoPullPatchPage(w, pages.RepoPullPatchParams{ ··· 493 510 return 494 511 } 495 512 496 - currentPatch, err := patchutil.AsDiff(pull.Submissions[roundIdInt].Patch) 513 + currentPatch, err := patchutil.AsDiff(pull.Submissions[roundIdInt].CombinedPatch()) 497 514 if err != nil { 498 515 log.Println("failed to interdiff; current patch malformed") 499 516 s.pages.Notice(w, fmt.Sprintf("interdiff-error-%d", roundIdInt), "Failed to calculate interdiff; current patch is invalid.") 500 517 return 501 518 } 502 519 503 - previousPatch, err := patchutil.AsDiff(pull.Submissions[roundIdInt-1].Patch) 520 + previousPatch, err := patchutil.AsDiff(pull.Submissions[roundIdInt-1].CombinedPatch()) 504 521 if err != nil { 505 522 log.Println("failed to interdiff; previous patch malformed") 506 523 s.pages.Notice(w, fmt.Sprintf("interdiff-error-%d", roundIdInt), "Failed to calculate interdiff; previous patch is invalid.") ··· 702 719 703 720 createdAt := time.Now().Format(time.RFC3339) 704 721 705 - pullAt, err := db.GetPullAt(s.db, f.RepoAt(), pull.PullId) 706 - if err != nil { 707 - log.Println("failed to get pull at", err) 708 - s.pages.Notice(w, "pull-comment", "Failed to create comment.") 709 - return 710 - } 711 - 712 722 client, err := s.oauth.AuthorizedClient(r) 713 723 if err != nil { 714 724 log.Println("failed to get authorized client", err) ··· 714 738 Rkey: tid.TID(), 715 739 Record: &lexutil.LexiconTypeDecoder{ 716 740 Val: &tangled.RepoPullComment{ 717 - Pull: string(pullAt), 741 + Pull: pull.PullAt().String(), 718 742 Body: body, 719 743 CreatedAt: createdAt, 720 744 }, ··· 962 986 } 963 987 964 988 sourceRev := comparison.Rev2 965 - patch := comparison.Patch 989 + patch := comparison.FormatPatchRaw 990 + combined := comparison.CombinedPatchRaw 966 991 967 992 if !patchutil.IsPatchValid(patch) { 968 993 s.pages.Notice(w, "pull", "Invalid patch format. Please provide a valid diff.") ··· 978 1001 Sha: comparison.Rev2, 979 1002 } 980 1003 981 - s.createPullRequest(w, r, f, user, title, body, targetBranch, patch, sourceRev, pullSource, recordPullSource, isStacked) 1004 + s.createPullRequest(w, r, f, user, title, body, targetBranch, patch, combined, sourceRev, pullSource, recordPullSource, isStacked) 982 1005 } 983 1006 984 1007 func (s *Pulls) handlePatchBasedPull(w http.ResponseWriter, r *http.Request, f *reporesolver.ResolvedRepo, user *oauth.User, title, body, targetBranch, patch string, isStacked bool) { ··· 987 1010 return 988 1011 } 989 1012 990 - s.createPullRequest(w, r, f, user, title, body, targetBranch, patch, "", nil, nil, isStacked) 1013 + s.createPullRequest(w, r, f, user, title, body, targetBranch, patch, "", "", nil, nil, isStacked) 991 1014 } 992 1015 993 1016 func (s *Pulls) handleForkBasedPull(w http.ResponseWriter, r *http.Request, f *reporesolver.ResolvedRepo, user *oauth.User, forkRepo string, title, body, targetBranch, sourceBranch string, isStacked bool) { ··· 1070 1093 } 1071 1094 1072 1095 sourceRev := comparison.Rev2 1073 - patch := comparison.Patch 1096 + patch := comparison.FormatPatchRaw 1097 + combined := comparison.CombinedPatchRaw 1074 1098 1075 1099 if !patchutil.IsPatchValid(patch) { 1076 1100 s.pages.Notice(w, "pull", "Invalid patch format. Please provide a valid diff.") ··· 1091 1113 Sha: sourceRev, 1092 1114 } 1093 1115 1094 - s.createPullRequest(w, r, f, user, title, body, targetBranch, patch, sourceRev, pullSource, recordPullSource, isStacked) 1116 + s.createPullRequest(w, r, f, user, title, body, targetBranch, patch, combined, sourceRev, pullSource, recordPullSource, isStacked) 1095 1117 } 1096 1118 1097 1119 func (s *Pulls) createPullRequest( ··· 1101 1123 user *oauth.User, 1102 1124 title, body, targetBranch string, 1103 1125 patch string, 1126 + combined string, 1104 1127 sourceRev string, 1105 1128 pullSource *models.PullSource, 1106 1129 recordPullSource *tangled.RepoPull_Source, ··· 1161 1182 rkey := tid.TID() 1162 1183 initialSubmission := models.PullSubmission{ 1163 1184 Patch: patch, 1185 + Combined: combined, 1164 1186 SourceRev: sourceRev, 1165 1187 } 1166 1188 pull := &models.Pull{ ··· 1591 1611 1592 1612 patch := r.FormValue("patch") 1593 1613 1594 - s.resubmitPullHelper(w, r, f, user, pull, patch, "") 1614 + s.resubmitPullHelper(w, r, f, user, pull, patch, "", "") 1595 1615 } 1596 1616 1597 1617 func (s *Pulls) resubmitBranch(w http.ResponseWriter, r *http.Request) { ··· 1652 1672 } 1653 1673 1654 1674 sourceRev := comparison.Rev2 1655 - patch := comparison.Patch 1675 + patch := comparison.FormatPatchRaw 1676 + combined := comparison.CombinedPatchRaw 1656 1677 1657 - s.resubmitPullHelper(w, r, f, user, pull, patch, sourceRev) 1678 + s.resubmitPullHelper(w, r, f, user, pull, patch, combined, sourceRev) 1658 1679 } 1659 1680 1660 1681 func (s *Pulls) resubmitFork(w http.ResponseWriter, r *http.Request) { ··· 1748 1767 comparison := forkComparison 1749 1768 1750 1769 sourceRev := comparison.Rev2 1751 - patch := comparison.Patch 1770 + patch := comparison.FormatPatchRaw 1771 + combined := comparison.CombinedPatchRaw 1752 1772 1753 - s.resubmitPullHelper(w, r, f, user, pull, patch, sourceRev) 1773 + s.resubmitPullHelper(w, r, f, user, pull, patch, combined, sourceRev) 1754 1774 } 1755 1775 1756 1776 // validate a resubmission against a pull request ··· 1778 1796 user *oauth.User, 1779 1797 pull *models.Pull, 1780 1798 patch string, 1799 + combined string, 1781 1800 sourceRev string, 1782 1801 ) { 1783 1802 if pull.IsStacked() { ··· 1812 1829 newRoundNumber := len(pull.Submissions) 1813 1830 newPatch := patch 1814 1831 newSourceRev := sourceRev 1815 - err = db.ResubmitPull(tx, pullAt, newRoundNumber, newPatch, newSourceRev) 1832 + combinedPatch := combined 1833 + err = db.ResubmitPull(tx, pullAt, newRoundNumber, newPatch, combinedPatch, newSourceRev) 1816 1834 if err != nil { 1817 1835 log.Println("failed to create pull request", err) 1818 1836 s.pages.Notice(w, "resubmit-error", "Failed to create pull request. Try again later.") ··· 2008 2024 pullAt := op.PullAt() 2009 2025 newRoundNumber := len(op.Submissions) 2010 2026 newPatch := np.LatestPatch() 2027 + combinedPatch := np.LatestSubmission().Combined 2011 2028 newSourceRev := np.LatestSha() 2012 - err := db.ResubmitPull(tx, pullAt, newRoundNumber, newPatch, newSourceRev) 2013 - 2029 + err := db.ResubmitPull(tx, pullAt, newRoundNumber, newPatch, combinedPatch, newSourceRev) 2014 2030 if err != nil { 2015 2031 log.Println("failed to update pull", err, op.PullId) 2016 2032 s.pages.Notice(w, "pull-resubmit-error", "Failed to resubmit pull request. Try again later.") ··· 2358 2374 initialSubmission := models.PullSubmission{ 2359 2375 Patch: fp.Raw, 2360 2376 SourceRev: fp.SHA, 2377 + Combined: fp.Raw, 2361 2378 } 2362 2379 pull := models.Pull{ 2363 2380 Title: title,
+6 -1
appview/repo/repo.go
··· 2565 2565 return 2566 2566 } 2567 2567 2568 - diff := patchutil.AsNiceDiff(formatPatch.Patch, base) 2568 + var diff types.NiceDiff 2569 + if formatPatch.CombinedPatchRaw != "" { 2570 + diff = patchutil.AsNiceDiff(formatPatch.CombinedPatchRaw, base) 2571 + } else { 2572 + diff = patchutil.AsNiceDiff(formatPatch.FormatPatchRaw, base) 2573 + } 2569 2574 2570 2575 repoinfo := f.RepoInfo(user) 2571 2576
+20 -4
knotserver/xrpc/repo_compare.go
··· 4 4 "fmt" 5 5 "net/http" 6 6 7 + "github.com/bluekeyes/go-gitdiff/gitdiff" 7 8 "tangled.org/core/knotserver/git" 8 9 "tangled.org/core/types" 9 10 xrpcerr "tangled.org/core/xrpc/errors" ··· 72 71 return 73 72 } 74 73 74 + var combinedPatch []*gitdiff.File 75 + var combinedPatchRaw string 76 + // we need the combined patch 77 + if len(formatPatch) >= 2 { 78 + diffTree, err := gr.DiffTree(commit1, commit2) 79 + if err != nil { 80 + x.Logger.Error("error comparing revisions", "msg", err.Error()) 81 + } else { 82 + combinedPatch = diffTree.Diff 83 + combinedPatchRaw = diffTree.Patch 84 + } 85 + } 86 + 75 87 response := types.RepoFormatPatchResponse{ 76 - Rev1: commit1.Hash.String(), 77 - Rev2: commit2.Hash.String(), 78 - FormatPatch: formatPatch, 79 - Patch: rawPatch, 88 + Rev1: commit1.Hash.String(), 89 + Rev2: commit2.Hash.String(), 90 + FormatPatch: formatPatch, 91 + FormatPatchRaw: rawPatch, 92 + CombinedPatch: combinedPatch, 93 + CombinedPatchRaw: combinedPatchRaw, 80 94 } 81 95 82 96 writeJson(w, response)
+7 -5
types/repo.go
··· 1 1 package types 2 2 3 3 import ( 4 + "github.com/bluekeyes/go-gitdiff/gitdiff" 4 5 "github.com/go-git/go-git/v5/plumbing/object" 5 6 ) 6 7 ··· 34 33 } 35 34 36 35 type RepoFormatPatchResponse struct { 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"` // deprecated 41 - Patch string `json:"patch,omitempty"` 36 + Rev1 string `json:"rev1,omitempty"` 37 + Rev2 string `json:"rev2,omitempty"` 38 + FormatPatch []FormatPatch `json:"format_patch,omitempty"` 39 + FormatPatchRaw string `json:"patch,omitempty"` 40 + CombinedPatch []*gitdiff.File `json:"combined_patch,omitempty"` 41 + CombinedPatchRaw string `json:"combined_patch_raw,omitempty"` 42 42 } 43 43 44 44 type RepoTreeResponse struct {