Multiple fixes

This commit is contained in:
Andy Dixon
2026-07-21 14:27:58 +01:00
parent b9b8635c33
commit 411e033a8e
5 changed files with 447 additions and 36 deletions
+67
View File
@@ -124,3 +124,70 @@ func TestRewriteHistoryFailsWhenNoRefsAreRewritten(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}
}
func TestApplyChangesUpdatesDatesAndRemovesCoAuthorTrailers(t *testing.T) {
repo := t.TempDir()
run := func(args ...string) string {
t.Helper()
cmd := exec.Command("git", append([]string{"-C", repo}, args...)...)
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("git %v failed: %v\n%s", args, err, string(out))
}
return string(out)
}
run("init")
run("config", "user.name", "Original User")
run("config", "user.email", "original@example.com")
if err := os.WriteFile(filepath.Join(repo, "a.txt"), []byte("one\n"), 0o644); err != nil {
t.Fatal(err)
}
run("add", "a.txt")
run("commit", "-m", "subject", "-m", "Context: Co-authored-by: stays because it is prose.\n\nCo-authored-by: One <one@example.com>\nco-authored by: Two <two@example.com>")
app := NewApp()
if _, err := app.SetRepository(repo); err != nil {
t.Fatal(err)
}
history, err := app.LoadHistory()
if err != nil {
t.Fatal(err)
}
history[0].AuthorDate = "2020-06-15 09:30:45 +0530"
history[0].CommitterDate = "Mon, 15 Jun 2020 04:00:45 +0000"
result, err := app.ApplyChanges(ApplyRequest{Commits: history, RemoveCoAuthors: true})
if err != nil {
t.Fatal(err)
}
if result.RemovedCoAuthors != 2 {
t.Fatalf("expected two removed co-author lines, got %d", result.RemovedCoAuthors)
}
log := run("log", "-1", "--pretty=format:%aI%x1f%cI%x1f%B")
fields := strings.SplitN(log, "\x1f", 3)
if len(fields) != 3 || !gitDatesEqual(fields[0], "2020-06-15T09:30:45+05:30") || !gitDatesEqual(fields[1], "2020-06-15T04:00:45Z") {
t.Fatalf("expected exact rewritten dates, got: %s", log)
}
if strings.Contains(strings.ToLower(log), "co-authored-by: one") || strings.Contains(strings.ToLower(log), "co-authored by: two") {
t.Fatalf("expected trailer lines to be removed, got: %s", log)
}
if !strings.Contains(log, "Context: Co-authored-by: stays because it is prose.") {
t.Fatalf("expected non-trailer prose to remain, got: %s", log)
}
}
func TestRemoveCoAuthorLinesOnlyMatchesWholeTrailerLines(t *testing.T) {
message := "subject\r\n\r\nCo-authored-by: Ada <ada@example.com>\r\n CO-AUTHORED BY: Grace <grace@example.com>\r\nNote: Co-authored-by: this is prose"
cleaned, removed := removeCoAuthorLines(message)
if removed != 2 {
t.Fatalf("expected two removed lines, got %d", removed)
}
if strings.Contains(cleaned, "Ada") || strings.Contains(cleaned, "Grace") {
t.Fatalf("expected co-author trailers to be removed: %q", cleaned)
}
if !strings.Contains(cleaned, "Note: Co-authored-by: this is prose") {
t.Fatalf("expected ordinary prose to remain: %q", cleaned)
}
}