fix(compact): replay cherry-picks in commit_order, not commit date (#6644)
bd compact --days N replayed the preserved commits in commit-date order. Dolt does not require dates to be monotonic along the parent chain, so a child dated before its parent was cherry-picked first and conflicted on any shared row (#6516). Log now also surfaces dolt_log.commit_order (CommitInfo.CommitOrder), and both compact paths (embedded/server and proxied-server) share a new planCompaction helper that sorts the recent commits by commit_order ascending. Old/recent partition, initialHash and boundaryHash selection are unchanged (still date-based). Adds unit tests for the planner and an embedded-Dolt regression test that reproduces the issue (child dated 20 min before its parent). Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
parent
ced977b21d
commit
fb43d97220
7 changed files with 185 additions and 48 deletions
|
|
@ -91,30 +91,9 @@ Examples:
|
|||
|
||||
cutoff := time.Now().AddDate(0, 0, -compactDoltDays)
|
||||
|
||||
var oldCommits int
|
||||
var recentHashes []string
|
||||
var initialHash, boundaryHash string
|
||||
|
||||
for _, entry := range logEntries {
|
||||
if entry.Date.Before(cutoff) {
|
||||
oldCommits++
|
||||
boundaryHash = entry.Hash
|
||||
} else {
|
||||
recentHashes = append(recentHashes, entry.Hash)
|
||||
}
|
||||
}
|
||||
initialHash = logEntries[totalCommits-1].Hash
|
||||
boundaryHash = ""
|
||||
for _, entry := range logEntries {
|
||||
if entry.Date.Before(cutoff) {
|
||||
boundaryHash = entry.Hash
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
for i, j := 0, len(recentHashes)-1; i < j; i, j = i+1, j-1 {
|
||||
recentHashes[i], recentHashes[j] = recentHashes[j], recentHashes[i]
|
||||
}
|
||||
plan := planCompaction(logEntries, cutoff)
|
||||
oldCommits, recentHashes := plan.oldCommits, plan.recentHashes
|
||||
initialHash, boundaryHash := plan.initialHash, plan.boundaryHash
|
||||
|
||||
recentCommits := len(recentHashes)
|
||||
|
||||
|
|
|
|||
|
|
@ -6,9 +6,14 @@ import (
|
|||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/steveyegge/beads/internal/configfile"
|
||||
"github.com/steveyegge/beads/internal/storage/embeddeddolt"
|
||||
)
|
||||
|
||||
// bdCompact runs "bd compact" with the given args and returns stdout.
|
||||
|
|
@ -190,3 +195,62 @@ func TestEmbeddedCompactConcurrent(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestEmbeddedCompactMisdatedChild reproduces #6516: two commits editing the
|
||||
// same row where the child is dated before its parent. Compact must replay the
|
||||
// preserved commits in graph order (dolt_log.commit_order), not date order,
|
||||
// or the cherry-pick of the child conflicts.
|
||||
func TestEmbeddedCompactMisdatedChild(t *testing.T) {
|
||||
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
|
||||
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests")
|
||||
}
|
||||
t.Parallel()
|
||||
|
||||
bd := buildEmbeddedBD(t)
|
||||
dir, beadsDir, _ := bdInit(t, bd, "--prefix", "md")
|
||||
issue := bdCreate(t, bd, dir, "Misdated bead", "--type", "task")
|
||||
bdCreate(t, bd, dir, "Filler bead", "--type", "task")
|
||||
|
||||
cfg, _ := configfile.Load(beadsDir)
|
||||
database := ""
|
||||
if cfg != nil {
|
||||
database = cfg.GetDoltDatabase()
|
||||
}
|
||||
db, cleanup, err := embeddeddolt.OpenSQL(t.Context(), filepath.Join(beadsDir, "embeddeddolt"), database, "main")
|
||||
if err != nil {
|
||||
t.Fatalf("OpenSQL: %v", err)
|
||||
}
|
||||
conn, err := db.Conn(t.Context())
|
||||
if err != nil {
|
||||
t.Fatalf("db.Conn: %v", err)
|
||||
}
|
||||
// Both commits are dated in the future so --days 0 preserves them; the
|
||||
// child (edit B) is dated 20 minutes before its parent (edit A).
|
||||
now := time.Now()
|
||||
edits := []struct {
|
||||
title string
|
||||
date time.Time
|
||||
}{
|
||||
{"edit A", now.Add(40 * time.Minute)},
|
||||
{"edit B", now.Add(20 * time.Minute)},
|
||||
}
|
||||
for _, e := range edits {
|
||||
if _, err := conn.ExecContext(t.Context(), "UPDATE issues SET title = ? WHERE id = ?", e.title, issue.ID); err != nil {
|
||||
t.Fatalf("update title %q: %v", e.title, err)
|
||||
}
|
||||
if _, err := conn.ExecContext(t.Context(), "CALL DOLT_COMMIT('-Am', ?, '--date', ?)",
|
||||
"repro: "+e.title, e.date.Format(time.RFC3339)); err != nil {
|
||||
t.Fatalf("dolt commit %q: %v", e.title, err)
|
||||
}
|
||||
}
|
||||
conn.Close()
|
||||
if err := cleanup(); err != nil {
|
||||
t.Fatalf("cleanup: %v", err)
|
||||
}
|
||||
|
||||
bdCompact(t, bd, dir, "--force", "--days", "0")
|
||||
|
||||
if got := bdShow(t, bd, dir, issue.ID).Title; got != "edit B" {
|
||||
t.Errorf("title after compact = %q, want %q", got, "edit B")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
53
cmd/bd/compact_plan.go
Normal file
53
cmd/bd/compact_plan.go
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
)
|
||||
|
||||
// compactPlan is the old/recent partition of a Dolt commit history used by
|
||||
// `bd compact --days N`.
|
||||
type compactPlan struct {
|
||||
// oldCommits is the number of commits dated before the cutoff.
|
||||
oldCommits int
|
||||
// recentHashes are the commits to preserve, in ascending graph
|
||||
// (commit_order) order so a cherry-pick replay applies every parent before
|
||||
// its children.
|
||||
recentHashes []string
|
||||
// initialHash and boundaryHash are the soft-reset target and the temp
|
||||
// branch point for the squashed base.
|
||||
initialHash, boundaryHash string
|
||||
}
|
||||
|
||||
// planCompaction partitions logEntries (as returned by Log: newest commit date
|
||||
// first, non-empty) around cutoff. Old/recent membership, initialHash and
|
||||
// boundaryHash are selected by commit date as before. The replay order of the
|
||||
// recent commits is by Dolt's commit_order, not by date: commit dates are not
|
||||
// required to be monotonic along the parent chain, so a child dated before its
|
||||
// parent would otherwise be cherry-picked first and conflict (#6516).
|
||||
func planCompaction(logEntries []storage.CommitInfo, cutoff time.Time) compactPlan {
|
||||
var plan compactPlan
|
||||
recent := make([]storage.CommitInfo, 0, len(logEntries))
|
||||
for _, entry := range logEntries {
|
||||
if entry.Date.Before(cutoff) {
|
||||
plan.oldCommits++
|
||||
if plan.boundaryHash == "" {
|
||||
plan.boundaryHash = entry.Hash
|
||||
}
|
||||
} else {
|
||||
recent = append(recent, entry)
|
||||
}
|
||||
}
|
||||
plan.initialHash = logEntries[len(logEntries)-1].Hash
|
||||
|
||||
sort.SliceStable(recent, func(i, j int) bool {
|
||||
return recent[i].CommitOrder < recent[j].CommitOrder
|
||||
})
|
||||
plan.recentHashes = make([]string, 0, len(recent))
|
||||
for _, entry := range recent {
|
||||
plan.recentHashes = append(plan.recentHashes, entry.Hash)
|
||||
}
|
||||
return plan
|
||||
}
|
||||
51
cmd/bd/compact_plan_test.go
Normal file
51
cmd/bd/compact_plan_test.go
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
)
|
||||
|
||||
// TestPlanCompactionReplaysInCommitOrder covers #6516: a child dated before its
|
||||
// parent must still be replayed after it. Entries are in Log order (date DESC).
|
||||
func TestPlanCompactionReplaysInCommitOrder(t *testing.T) {
|
||||
now := time.Now()
|
||||
cutoff := now.Add(-time.Hour)
|
||||
entries := []storage.CommitInfo{
|
||||
{Hash: "parent", Date: now.Add(40 * time.Minute), CommitOrder: 4},
|
||||
{Hash: "child", Date: now.Add(20 * time.Minute), CommitOrder: 5}, // dated before its parent
|
||||
{Hash: "mid", Date: now.Add(-30 * time.Minute), CommitOrder: 3},
|
||||
{Hash: "old2", Date: now.Add(-2 * time.Hour), CommitOrder: 2},
|
||||
{Hash: "old1", Date: now.Add(-3 * time.Hour), CommitOrder: 1},
|
||||
}
|
||||
// Log order is date DESC: parent(+40m), child(+20m), mid, old2, old1.
|
||||
plan := planCompaction(entries, cutoff)
|
||||
|
||||
want := []string{"mid", "parent", "child"}
|
||||
if !reflect.DeepEqual(plan.recentHashes, want) {
|
||||
t.Errorf("recentHashes = %v, want %v (ascending commit_order)", plan.recentHashes, want)
|
||||
}
|
||||
if plan.oldCommits != 2 {
|
||||
t.Errorf("oldCommits = %d, want 2", plan.oldCommits)
|
||||
}
|
||||
// Partition selection stays date-based.
|
||||
if plan.boundaryHash != "old2" {
|
||||
t.Errorf("boundaryHash = %q, want old2", plan.boundaryHash)
|
||||
}
|
||||
if plan.initialHash != "old1" {
|
||||
t.Errorf("initialHash = %q, want old1", plan.initialHash)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlanCompactionNoRecent(t *testing.T) {
|
||||
now := time.Now()
|
||||
plan := planCompaction([]storage.CommitInfo{
|
||||
{Hash: "b", Date: now.Add(-time.Hour), CommitOrder: 2},
|
||||
{Hash: "a", Date: now.Add(-2 * time.Hour), CommitOrder: 1},
|
||||
}, now)
|
||||
if len(plan.recentHashes) != 0 || plan.oldCommits != 2 || plan.boundaryHash != "b" || plan.initialHash != "a" {
|
||||
t.Errorf("unexpected plan: %+v", plan)
|
||||
}
|
||||
}
|
||||
|
|
@ -52,27 +52,10 @@ func runCompactProxiedServer(ctx context.Context) error {
|
|||
|
||||
cutoff := time.Now().AddDate(0, 0, -compactDoltDays)
|
||||
|
||||
var oldCommits int
|
||||
var recentHashes []string
|
||||
for _, entry := range logEntries {
|
||||
if entry.Date.Before(cutoff) {
|
||||
oldCommits++
|
||||
} else {
|
||||
recentHashes = append(recentHashes, entry.Hash)
|
||||
}
|
||||
}
|
||||
initialHash := logEntries[totalCommits-1].Hash
|
||||
boundaryHash := ""
|
||||
for _, entry := range logEntries {
|
||||
if entry.Date.Before(cutoff) {
|
||||
boundaryHash = entry.Hash
|
||||
break
|
||||
}
|
||||
}
|
||||
plan := planCompaction(logEntries, cutoff)
|
||||
oldCommits, recentHashes := plan.oldCommits, plan.recentHashes
|
||||
initialHash, boundaryHash := plan.initialHash, plan.boundaryHash
|
||||
|
||||
for i, j := 0, len(recentHashes)-1; i < j; i, j = i+1, j-1 {
|
||||
recentHashes[i], recentHashes[j] = recentHashes[j], recentHashes[i]
|
||||
}
|
||||
recentCommits := len(recentHashes)
|
||||
|
||||
if compactDoltDryRun {
|
||||
|
|
|
|||
|
|
@ -12,6 +12,11 @@ type CommitInfo struct {
|
|||
Email string
|
||||
Date time.Time
|
||||
Message string
|
||||
// CommitOrder is Dolt's ancestry-derived position of the commit in the
|
||||
// history graph (dolt_log.commit_order): a parent always has a lower value
|
||||
// than its children, regardless of commit dates, which are not required to
|
||||
// be monotonic along the parent chain.
|
||||
CommitOrder int64
|
||||
}
|
||||
|
||||
// StatusEntry represents a changed table in the working set.
|
||||
|
|
|
|||
|
|
@ -43,16 +43,18 @@ func Status(ctx context.Context, db DBConn) (*storage.Status, error) {
|
|||
return status, rows.Err()
|
||||
}
|
||||
|
||||
// Log returns recent commit history up to limit entries.
|
||||
// Log returns recent commit history up to limit entries, newest commit date
|
||||
// first. Each entry also carries Dolt's commit_order so callers that need graph
|
||||
// (ancestry) order can sort by CommitOrder instead of trusting commit dates.
|
||||
// If limit is 0 or negative, all entries are returned.
|
||||
func Log(ctx context.Context, db DBConn, limit int) ([]storage.CommitInfo, error) {
|
||||
var query string
|
||||
var args []interface{}
|
||||
if limit > 0 {
|
||||
query = "SELECT commit_hash, committer, email, date, message FROM dolt_log ORDER BY date DESC LIMIT ?"
|
||||
query = "SELECT commit_hash, committer, email, date, message, commit_order FROM dolt_log ORDER BY date DESC LIMIT ?"
|
||||
args = []interface{}{limit}
|
||||
} else {
|
||||
query = "SELECT commit_hash, committer, email, date, message FROM dolt_log ORDER BY date DESC"
|
||||
query = "SELECT commit_hash, committer, email, date, message, commit_order FROM dolt_log ORDER BY date DESC"
|
||||
}
|
||||
rows, err := db.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
|
|
@ -63,7 +65,7 @@ func Log(ctx context.Context, db DBConn, limit int) ([]storage.CommitInfo, error
|
|||
var commits []storage.CommitInfo
|
||||
for rows.Next() {
|
||||
var c storage.CommitInfo
|
||||
if err := rows.Scan(&c.Hash, &c.Author, &c.Email, &c.Date, &c.Message); err != nil {
|
||||
if err := rows.Scan(&c.Hash, &c.Author, &c.Email, &c.Date, &c.Message, &c.CommitOrder); err != nil {
|
||||
return nil, fmt.Errorf("scan commit: %w", err)
|
||||
}
|
||||
commits = append(commits, c)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue