back interdiff of round #2 and #1

feat: add commit footers for pull merge #109

closed
opened by dvjn.dev targeting master from dvjn.dev/core: push-rwutqnoxqxlr
files
appview
knotclient
state
knotserver
patchutil
types
REVERTED
types/merge.go
··· 13 } 14 15 type MergeRequest struct { 16 - Patch string `json:"patch"` 17 - AuthorName string `json:"authorName,omitempty"` 18 - AuthorEmail string `json:"authorEmail,omitempty"` 19 - CommitBody string `json:"commitBody,omitempty"` 20 - CommitMessage string `json:"commitMessage,omitempty"` 21 - CommitFooters map[string]string `json:"commitFooters,omitempty"` 22 - Branch string `json:"branch"` 23 }
··· 13 } 14 15 type MergeRequest struct { 16 + Patch string `json:"patch"` 17 + AuthorName string `json:"authorName,omitempty"` 18 + AuthorEmail string `json:"authorEmail,omitempty"` 19 + CommitBody string `json:"commitBody,omitempty"` 20 + CommitMessage string `json:"commitMessage,omitempty"` 21 + Branch string `json:"branch"` 22 }
REVERTED
appview/knotclient/signer.go
··· 201 202 func (s *SignedClient) Merge( 203 patch []byte, 204 - ownerDid, targetRepo, branch, commitMessage, commitBody, authorName, authorEmail string, footers map[string]string, 205 ) (*http.Response, error) { 206 const ( 207 Method = "POST" ··· 215 AuthorName: authorName, 216 AuthorEmail: authorEmail, 217 Patch: string(patch), 218 - CommitFooters: footers, 219 } 220 221 body, _ := json.Marshal(mr)
··· 201 202 func (s *SignedClient) Merge( 203 patch []byte, 204 + ownerDid, targetRepo, branch, commitMessage, commitBody, authorName, authorEmail string, 205 ) (*http.Response, error) { 206 const ( 207 Method = "POST" ··· 215 AuthorName: authorName, 216 AuthorEmail: authorEmail, 217 Patch: string(patch), 218 } 219 220 body, _ := json.Marshal(mr)
ERROR
appview/state/pull.go

Failed to calculate interdiff for this file.

REVERTED
knotserver/git/merge.go
··· 32 AuthorName string 33 AuthorEmail string 34 FormatPatch bool 35 - CommitFooters map[string]string 36 } 37 38 func (e ErrMerge) Error() string { ··· 89 var stderr bytes.Buffer 90 var cmd *exec.Cmd 91 92 - footers := "" 93 - for k, v := range opts.CommitFooters { 94 - footers += fmt.Sprintf("\n%s: %s", k, v) 95 - } 96 - 97 if checkOnly { 98 cmd = exec.Command("git", "-C", tmpDir, "apply", "--check", "-v", patchFile) 99 } else { 100 // if patch is a format-patch, apply using 'git am' 101 if opts.FormatPatch { 102 - patch, err := os.ReadFile(patchFile) 103 - if err != nil { 104 - return fmt.Errorf("failed to read patch file: %w", err) 105 - } 106 - 107 - re := regexp.MustCompile(`\n(Subject: )([\s\S]*?)(---)\n`) 108 - patch = re.ReplaceAll(patch, []byte("\n${1}${2}\n"+footers+"\n${3}\n")) 109 - 110 - err = os.WriteFile(patchFile, patch, 0o644) 111 - if err != nil { 112 - return fmt.Errorf("failed to write patch file: %w", err) 113 - } 114 - 115 amCmd := exec.Command("git", "-C", tmpDir, "am", patchFile) 116 amCmd.Stderr = &stderr 117 if err := amCmd.Run(); err != nil { ··· 158 commitArgs = append(commitArgs, "-m", opts.CommitBody) 159 } 160 161 - commitArgs = append(commitArgs, "-m", footers) 162 - 163 cmd = exec.Command("git", commitArgs...) 164 } else { 165 // If no commit message specified, use git-am which automatically creates a commit
··· 32 AuthorName string 33 AuthorEmail string 34 FormatPatch bool 35 } 36 37 func (e ErrMerge) Error() string { ··· 88 var stderr bytes.Buffer 89 var cmd *exec.Cmd 90 91 if checkOnly { 92 cmd = exec.Command("git", "-C", tmpDir, "apply", "--check", "-v", patchFile) 93 } else { 94 // if patch is a format-patch, apply using 'git am' 95 if opts.FormatPatch { 96 amCmd := exec.Command("git", "-C", tmpDir, "am", patchFile) 97 amCmd.Stderr = &stderr 98 if err := amCmd.Run(); err != nil { ··· 139 commitArgs = append(commitArgs, "-m", opts.CommitBody) 140 } 141 142 cmd = exec.Command("git", commitArgs...) 143 } else { 144 // If no commit message specified, use git-am which automatically creates a commit
REVERTED
knotserver/routes.go
··· 732 AuthorEmail: data.AuthorEmail, 733 CommitBody: data.CommitBody, 734 CommitMessage: data.CommitMessage, 735 - CommitFooters: data.CommitFooters, 736 } 737 738 patch := data.Patch
··· 732 AuthorEmail: data.AuthorEmail, 733 CommitBody: data.CommitBody, 734 CommitMessage: data.CommitMessage, 735 } 736 737 patch := data.Patch
NEW
patchutil/patchutil.go
··· 194 195 return string(output), nil 196 }
··· 194 195 return string(output), nil 196 } 197 + 198 + func AddCommitFooters(patch string, footers string) string { 199 + re := regexp.MustCompile(`\n(Subject: )([\s\S]*?)(---\n|\ndiff --git )`) 200 + return re.ReplaceAllString(patch, "\n${1}${2}\n"+footers+"\n${3}") 201 + }
NEW
patchutil/patchutil_test.go
··· 322 }) 323 } 324 }
··· 322 }) 323 } 324 } 325 + 326 + func TestAddCommmitFooters(t *testing.T) { 327 + tests := []struct { 328 + name string 329 + input string 330 + footers string 331 + expected string 332 + }{ 333 + { 334 + name: "Single patch", 335 + input: `From 3c5035488318164b81f60fe3adcd6c9199d76331 Mon Sep 17 00:00:00 2001 336 + From: Author <author@example.com> 337 + Date: Wed, 16 Apr 2025 11:01:00 +0300 338 + Subject: [PATCH] Example patch 339 + 340 + more message body 341 + 342 + --- 343 + file.txt | 2 +- 344 + 1 file changed, 1 insertions(+), 1 deletions(-) 345 + 346 + diff --git a/file.txt b/file.txt 347 + index 123456..789012 100644 348 + --- a/file.txt 349 + +++ b/file.txt 350 + @@ -1 +1 @@ 351 + -old content 352 + +new content 353 + -- 354 + 2.48.1`, 355 + footers: `Pull-Id: Author <author@example.com> 356 + Merge-Request: 123`, 357 + expected: `From 3c5035488318164b81f60fe3adcd6c9199d76331 Mon Sep 17 00:00:00 2001 358 + From: Author <author@example.com> 359 + Date: Wed, 16 Apr 2025 11:01:00 +0300 360 + Subject: [PATCH] Example patch 361 + 362 + more message body 363 + 364 + 365 + Pull-Id: Author <author@example.com> 366 + Merge-Request: 123 367 + --- 368 + file.txt | 2 +- 369 + 1 file changed, 1 insertions(+), 1 deletions(-) 370 + 371 + diff --git a/file.txt b/file.txt 372 + index 123456..789012 100644 373 + --- a/file.txt 374 + +++ b/file.txt 375 + @@ -1 +1 @@ 376 + -old content 377 + +new content 378 + -- 379 + 2.48.1`, 380 + }, 381 + { 382 + name: "Two patches with --no-stat", 383 + input: `From 3c5035488318164b81f60fe3adcd6c9199d76331 Mon Sep 17 00:00:00 2001 384 + From: Author <author@example.com> 385 + Date: Wed, 16 Apr 2025 11:01:00 +0300 386 + Subject: [PATCH 1/2] First patch 387 + 388 + more message body 389 + 390 + --- 391 + file.txt | 2 +- 392 + 1 file changed, 1 insertions(+), 1 deletions(-) 393 + 394 + diff --git a/file1.txt b/file1.txt 395 + index 123456..789012 100644 396 + --- a/file1.txt 397 + +++ b/file1.txt 398 + @@ -1 +1 @@ 399 + -old content 400 + +new content 401 + -- 402 + 2.48.1 403 + From a9529f3b3a653329a5268f0f4067225480207e3c Mon Sep 17 00:00:00 2001 404 + From: Author <author@example.com> 405 + Date: Wed, 16 Apr 2025 11:03:11 +0300 406 + Subject: [PATCH 2/2] Second patch 407 + 408 + more message body 409 + 410 + --- 411 + file.txt | 2 +- 412 + 1 file changed, 1 insertions(+), 1 deletions(-) 413 + 414 + diff --git a/file2.txt b/file2.txt 415 + index abcdef..ghijkl 100644 416 + --- a/file2.txt 417 + +++ b/file2.txt 418 + @@ -1 +1 @@ 419 + -foo bar 420 + +baz qux 421 + -- 422 + 2.48.1`, 423 + footers: `Pull-Id: Author <author@example.com> 424 + Merge-Request: 123`, 425 + expected: `From 3c5035488318164b81f60fe3adcd6c9199d76331 Mon Sep 17 00:00:00 2001 426 + From: Author <author@example.com> 427 + Date: Wed, 16 Apr 2025 11:01:00 +0300 428 + Subject: [PATCH 1/2] First patch 429 + 430 + more message body 431 + 432 + 433 + Pull-Id: Author <author@example.com> 434 + Merge-Request: 123 435 + --- 436 + file.txt | 2 +- 437 + 1 file changed, 1 insertions(+), 1 deletions(-) 438 + 439 + diff --git a/file1.txt b/file1.txt 440 + index 123456..789012 100644 441 + --- a/file1.txt 442 + +++ b/file1.txt 443 + @@ -1 +1 @@ 444 + -old content 445 + +new content 446 + -- 447 + 2.48.1 448 + From a9529f3b3a653329a5268f0f4067225480207e3c Mon Sep 17 00:00:00 2001 449 + From: Author <author@example.com> 450 + Date: Wed, 16 Apr 2025 11:03:11 +0300 451 + Subject: [PATCH 2/2] Second patch 452 + 453 + more message body 454 + 455 + 456 + Pull-Id: Author <author@example.com> 457 + Merge-Request: 123 458 + --- 459 + file.txt | 2 +- 460 + 1 file changed, 1 insertions(+), 1 deletions(-) 461 + 462 + diff --git a/file2.txt b/file2.txt 463 + index abcdef..ghijkl 100644 464 + --- a/file2.txt 465 + +++ b/file2.txt 466 + @@ -1 +1 @@ 467 + -foo bar 468 + +baz qux 469 + -- 470 + 2.48.1`, 471 + }, 472 + { 473 + name: "Single patch with no stat", 474 + input: `From 3c5035488318164b81f60fe3adcd6c9199d76331 Mon Sep 17 00:00:00 2001 475 + From: Author <author@example.com> 476 + Date: Wed, 16 Apr 2025 11:01:00 +0300 477 + Subject: [PATCH] Example patch 478 + 479 + diff --git a/file.txt b/file.txt 480 + index 123456..789012 100644 481 + --- a/file.txt 482 + +++ b/file.txt 483 + @@ -1 +1 @@ 484 + -old content 485 + +new content 486 + -- 487 + 2.48.1`, 488 + footers: `Pull-Id: Author <author@example.com> 489 + Merge-Request: 123`, 490 + expected: `From 3c5035488318164b81f60fe3adcd6c9199d76331 Mon Sep 17 00:00:00 2001 491 + From: Author <author@example.com> 492 + Date: Wed, 16 Apr 2025 11:01:00 +0300 493 + Subject: [PATCH] Example patch 494 + 495 + Pull-Id: Author <author@example.com> 496 + Merge-Request: 123 497 + 498 + diff --git a/file.txt b/file.txt 499 + index 123456..789012 100644 500 + --- a/file.txt 501 + +++ b/file.txt 502 + @@ -1 +1 @@ 503 + -old content 504 + +new content 505 + -- 506 + 2.48.1`, 507 + }, 508 + { 509 + name: "Two patches with no stat", 510 + input: `From 3c5035488318164b81f60fe3adcd6c9199d76331 Mon Sep 17 00:00:00 2001 511 + From: Author <author@example.com> 512 + Date: Wed, 16 Apr 2025 11:01:00 +0300 513 + Subject: [PATCH 1/2] First patch 514 + 515 + with long message body 516 + 517 + diff --git a/file1.txt b/file1.txt 518 + index 123456..789012 100644 519 + --- a/file1.txt 520 + +++ b/file1.txt 521 + @@ -1 +1 @@ 522 + -old content 523 + +new content 524 + -- 525 + 2.48.1 526 + From a9529f3b3a653329a5268f0f4067225480207e3c Mon Sep 17 00:00:00 2001 527 + From: Author <author@example.com> 528 + Date: Wed, 16 Apr 2025 11:03:11 +0300 529 + Subject: [PATCH 2/2] Second patch 530 + 531 + diff --git a/file2.txt b/file2.txt 532 + index abcdef..ghijkl 100644 533 + --- a/file2.txt 534 + +++ b/file2.txt 535 + @@ -1 +1 @@ 536 + -foo bar 537 + +baz qux 538 + -- 539 + 2.48.1`, 540 + footers: `Pull-Id: Author <author@example.com> 541 + Merge-Request: 123`, 542 + expected: `From 3c5035488318164b81f60fe3adcd6c9199d76331 Mon Sep 17 00:00:00 2001 543 + From: Author <author@example.com> 544 + Date: Wed, 16 Apr 2025 11:01:00 +0300 545 + Subject: [PATCH 1/2] First patch 546 + 547 + with long message body 548 + 549 + Pull-Id: Author <author@example.com> 550 + Merge-Request: 123 551 + 552 + diff --git a/file1.txt b/file1.txt 553 + index 123456..789012 100644 554 + --- a/file1.txt 555 + +++ b/file1.txt 556 + @@ -1 +1 @@ 557 + -old content 558 + +new content 559 + -- 560 + 2.48.1 561 + From a9529f3b3a653329a5268f0f4067225480207e3c Mon Sep 17 00:00:00 2001 562 + From: Author <author@example.com> 563 + Date: Wed, 16 Apr 2025 11:03:11 +0300 564 + Subject: [PATCH 2/2] Second patch 565 + 566 + Pull-Id: Author <author@example.com> 567 + Merge-Request: 123 568 + 569 + diff --git a/file2.txt b/file2.txt 570 + index abcdef..ghijkl 100644 571 + --- a/file2.txt 572 + +++ b/file2.txt 573 + @@ -1 +1 @@ 574 + -foo bar 575 + +baz qux 576 + -- 577 + 2.48.1`, 578 + }, 579 + } 580 + 581 + for _, tt := range tests { 582 + t.Run(tt.name, func(t *testing.T) { 583 + result := AddCommitFooters(tt.input, tt.footers) 584 + if result != tt.expected { 585 + t.Errorf("Got:\n========\n%v\n========\nExpected:\n========\n%v\n========\n", result, tt.expected) 586 + } 587 + }) 588 + } 589 + }