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: add support for format-patch

authored by

Anirudh Oppiliappan and committed by
Akshay
f0f962e3 77809623

+56 -49
+15
appview/db/pulls.go
··· 10 10 11 11 "github.com/bluekeyes/go-gitdiff/gitdiff" 12 12 "github.com/bluesky-social/indigo/atproto/syntax" 13 + "tangled.sh/tangled.sh/core/patchutil" 13 14 "tangled.sh/tangled.sh/core/types" 14 15 ) 15 16 ··· 189 188 nd.Stat.FilesChanged = len(diffs) 190 189 191 190 return nd 191 + } 192 + 193 + func (s PullSubmission) IsFormatPatch() bool { 194 + return patchutil.IsFormatPatch(s.Patch) 195 + } 196 + 197 + func (s PullSubmission) AsFormatPatch() []patchutil.FormatPatch { 198 + patches, err := patchutil.ExtractPatches(s.Patch) 199 + if err != nil { 200 + log.Println("error extracting patches from submission:", err) 201 + return []patchutil.FormatPatch{} 202 + } 203 + 204 + return patches 192 205 } 193 206 194 207 func NewPull(tx *sql.Tx, pull *Pull) error {
+29 -41
appview/state/pull.go
··· 10 10 "net/http" 11 11 "net/url" 12 12 "strconv" 13 - "strings" 14 13 "time" 15 14 16 15 "github.com/go-chi/chi/v5" ··· 17 18 "tangled.sh/tangled.sh/core/appview/auth" 18 19 "tangled.sh/tangled.sh/core/appview/db" 19 20 "tangled.sh/tangled.sh/core/appview/pages" 21 + "tangled.sh/tangled.sh/core/patchutil" 20 22 "tangled.sh/tangled.sh/core/types" 21 23 22 24 comatproto "github.com/bluesky-social/indigo/api/atproto" ··· 254 254 255 255 latestSubmission := pull.Submissions[pull.LastRoundNumber()] 256 256 if latestSubmission.SourceRev != result.Branch.Hash { 257 + fmt.Println(latestSubmission.SourceRev, result.Branch.Hash) 257 258 return pages.ShouldResubmit 258 259 } 259 260 ··· 636 635 return 637 636 } 638 637 639 - diffTreeResponse, err := ksClient.Compare(f.OwnerDid(), f.RepoName, targetBranch, sourceBranch) 638 + comparison, err := ksClient.Compare(f.OwnerDid(), f.RepoName, targetBranch, sourceBranch) 640 639 if err != nil { 641 640 log.Println("failed to compare", err) 642 641 s.pages.Notice(w, "pull", err.Error()) 643 642 return 644 643 } 645 644 646 - sourceRev := diffTreeResponse.DiffTree.Rev2 647 - patch := diffTreeResponse.DiffTree.Patch 645 + sourceRev := comparison.Rev2 646 + patch := comparison.Patch 648 647 649 - if !isPatchValid(patch) { 648 + if !patchutil.IsPatchValid(patch) { 650 649 s.pages.Notice(w, "pull", "Invalid patch format. Please provide a valid diff.") 651 650 return 652 651 } ··· 655 654 } 656 655 657 656 func (s *State) handlePatchBasedPull(w http.ResponseWriter, r *http.Request, f *FullyResolvedRepo, user *auth.User, title, body, targetBranch, patch string) { 658 - if !isPatchValid(patch) { 657 + if !patchutil.IsPatchValid(patch) { 659 658 s.pages.Notice(w, "pull", "Invalid patch format. Please provide a valid diff.") 660 659 return 661 660 } ··· 715 714 // hiddenRef: hidden/feature-1/main (on repo-fork) 716 715 // targetBranch: main (on repo-1) 717 716 // sourceBranch: feature-1 (on repo-fork) 718 - diffTreeResponse, err := us.Compare(user.Did, fork.Name, hiddenRef, sourceBranch) 717 + comparison, err := us.Compare(user.Did, fork.Name, hiddenRef, sourceBranch) 719 718 if err != nil { 720 719 log.Println("failed to compare across branches", err) 721 720 s.pages.Notice(w, "pull", err.Error()) 722 721 return 723 722 } 724 723 725 - sourceRev := diffTreeResponse.DiffTree.Rev2 726 - patch := diffTreeResponse.DiffTree.Patch 724 + sourceRev := comparison.Rev2 725 + patch := comparison.Patch 727 726 728 - if !isPatchValid(patch) { 727 + if patchutil.IsPatchValid(patch) { 729 728 s.pages.Notice(w, "pull", "Invalid patch format. Please provide a valid diff.") 730 729 return 731 730 } ··· 743 742 }, &tangled.RepoPull_Source{Branch: sourceBranch, Repo: &fork.AtUri}) 744 743 } 745 744 746 - func (s *State) createPullRequest(w http.ResponseWriter, r *http.Request, f *FullyResolvedRepo, user *auth.User, title, body, targetBranch, patch, sourceRev string, pullSource *db.PullSource, recordPullSource *tangled.RepoPull_Source) { 745 + func (s *State) createPullRequest( 746 + w http.ResponseWriter, 747 + r *http.Request, 748 + f *FullyResolvedRepo, 749 + user *auth.User, 750 + title, body, targetBranch string, 751 + patch string, 752 + sourceRev string, 753 + pullSource *db.PullSource, 754 + recordPullSource *tangled.RepoPull_Source, 755 + ) { 747 756 tx, err := s.db.BeginTx(r.Context(), nil) 748 757 if err != nil { 749 758 log.Println("failed to start tx") ··· 1123 1112 return 1124 1113 } 1125 1114 1126 - diffTreeResponse, err := ksClient.Compare(f.OwnerDid(), f.RepoName, pull.TargetBranch, pull.PullSource.Branch) 1115 + comparison, err := ksClient.Compare(f.OwnerDid(), f.RepoName, pull.TargetBranch, pull.PullSource.Branch) 1127 1116 if err != nil { 1128 1117 log.Printf("compare request failed: %s", err) 1129 1118 s.pages.Notice(w, "resubmit-error", err.Error()) 1130 1119 return 1131 1120 } 1132 1121 1133 - sourceRev := diffTreeResponse.DiffTree.Rev2 1134 - patch := diffTreeResponse.DiffTree.Patch 1122 + sourceRev := comparison.Rev2 1123 + patch := comparison.Patch 1135 1124 1136 1125 if err = validateResubmittedPatch(pull, patch); err != nil { 1137 1126 s.pages.Notice(w, "resubmit-error", err.Error()) ··· 1260 1249 } 1261 1250 1262 1251 hiddenRef := url.QueryEscape(fmt.Sprintf("hidden/%s/%s", pull.PullSource.Branch, pull.TargetBranch)) 1263 - diffTreeResponse, err := ksClient.Compare(forkRepo.Did, forkRepo.Name, hiddenRef, pull.PullSource.Branch) 1252 + comparison, err := ksClient.Compare(forkRepo.Did, forkRepo.Name, hiddenRef, pull.PullSource.Branch) 1264 1253 if err != nil { 1265 1254 log.Printf("failed to compare branches: %s", err) 1266 1255 s.pages.Notice(w, "resubmit-error", err.Error()) 1267 1256 return 1268 1257 } 1269 1258 1270 - sourceRev := diffTreeResponse.DiffTree.Rev2 1271 - patch := diffTreeResponse.DiffTree.Patch 1259 + sourceRev := comparison.Rev2 1260 + patch := comparison.Patch 1272 1261 1273 1262 if err = validateResubmittedPatch(pull, patch); err != nil { 1274 1263 s.pages.Notice(w, "resubmit-error", err.Error()) ··· 1349 1338 return fmt.Errorf("Patch is identical to previous submission.") 1350 1339 } 1351 1340 1352 - if !isPatchValid(patch) { 1341 + if patchutil.IsPatchValid(patch) { 1353 1342 return fmt.Errorf("Invalid patch format. Please provide a valid diff.") 1354 1343 } 1355 1344 ··· 1526 1515 1527 1516 s.pages.HxLocation(w, fmt.Sprintf("/%s/pulls/%d", f.OwnerSlashRepo(), pull.PullId)) 1528 1517 return 1529 - } 1530 - 1531 - // Very basic validation to check if it looks like a diff/patch 1532 - // A valid patch usually starts with diff or --- lines 1533 - func isPatchValid(patch string) bool { 1534 - // Basic validation to check if it looks like a diff/patch 1535 - // A valid patch usually starts with diff or --- lines 1536 - if len(patch) == 0 { 1537 - return false 1538 - } 1539 - 1540 - lines := strings.Split(patch, "\n") 1541 - if len(lines) < 2 { 1542 - return false 1543 - } 1544 - 1545 - // Check for common patch format markers 1546 - firstLine := strings.TrimSpace(lines[0]) 1547 - return strings.HasPrefix(firstLine, "diff ") || 1548 - strings.HasPrefix(firstLine, "--- ") || 1549 - strings.HasPrefix(firstLine, "Index: ") || 1550 - strings.HasPrefix(firstLine, "+++ ") || 1551 - strings.HasPrefix(firstLine, "@@ ") 1552 1518 }
+6 -6
appview/state/signer.go
··· 378 378 return &capabilities, nil 379 379 } 380 380 381 - func (us *UnsignedClient) Compare(ownerDid, repoName, rev1, rev2 string) (*types.RepoDiffTreeResponse, error) { 381 + func (us *UnsignedClient) Compare(ownerDid, repoName, rev1, rev2 string) (*types.RepoFormatPatchResponse, error) { 382 382 const ( 383 383 Method = "GET" 384 384 ) ··· 409 409 } 410 410 defer compareResp.Body.Close() 411 411 412 - var diffTreeResponse types.RepoDiffTreeResponse 413 - err = json.Unmarshal(respBody, &diffTreeResponse) 412 + var formatPatchResponse types.RepoFormatPatchResponse 413 + err = json.Unmarshal(respBody, &formatPatchResponse) 414 414 if err != nil { 415 - log.Println("failed to unmarshal diff tree response", err) 416 - return nil, fmt.Errorf("Failed to compare branches.") 415 + log.Println("failed to unmarshal format-patch response", err) 416 + return nil, fmt.Errorf("failed to compare branches.") 417 417 } 418 418 419 - return &diffTreeResponse, nil 419 + return &formatPatchResponse, nil 420 420 }
+6 -2
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" 5 6 ) 6 7 7 8 type RepoIndexResponse struct { ··· 33 32 Diff *NiceDiff `json:"diff,omitempty"` 34 33 } 35 34 36 - type RepoDiffTreeResponse struct { 37 - DiffTree *DiffTree `json:"difftree,omitempty"` 35 + type RepoFormatPatchResponse struct { 36 + Rev1 string `json:"rev1,omitempty"` 37 + Rev2 string `json:"rev2,omitempty"` 38 + FormatPatch []patchutil.FormatPatch `json:"format_patch,omitempty"` 39 + Patch string `json:"patch,omitempty"` 38 40 } 39 41 40 42 type RepoTreeResponse struct {