caelum0x 49b183818c Unify gitgram + aitimemachine into the monorepo (de-submodule) and merge vendored engines
Convert gitgram/ and aitimemachine/ from embedded git repos (which showed as
opaque submodule gitlinks on GitHub) into fully-tracked first-party content.

AITM now merges its 7 vendored repos as first-party engines:
- memory: beads (Go task DAG) + context7 (docs SDK), unified into saved_contexts
- git: vendored C git (bundled binary) + gitnexus (in-process code intelligence)
- agentic: a2a (@a2a-js/sdk) + pal (AITM-managed Python subprocess)

Includes the full session's product build: Gitgram web/mobile/backend features,
AITM website/CLI/MCP, the unified memory layer, engine build tooling
(scripts/build-engines.sh, Dockerfile, CI), docs (README, ENGINES.md, ARCHITECTURE.md),
and extensive test suites.

Build artifacts, node_modules, external/, and gemini-cli are gitignored.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 14:14:09 +03:00

88 lines
2.8 KiB
Go

package versioncontrolops
import (
"context"
"fmt"
)
// Flatten squashes all Dolt commit history into a single commit using
// the Tim Sehn recipe:
// 1. Create a temp branch from current state
// 2. Checkout temp branch
// 3. Soft-reset to the initial (oldest) commit, collapsing all history
// 4. Stage all + commit as a single snapshot
// 5. Checkout main
// 6. Hard-reset main to the flattened branch
// 7. Delete temp branch
//
// Callers should run DoltGC afterward to reclaim disk space from orphaned history.
//
// conn must be a single database connection (not a pooled *sql.DB) since the
// stored procedures rely on session-scoped state (current branch, working set).
func Flatten(ctx context.Context, conn DBConn) error {
// Find the initial commit hash (oldest ancestor).
var initialHash string
if err := conn.QueryRowContext(ctx,
"SELECT commit_hash FROM dolt_log ORDER BY date ASC LIMIT 1",
).Scan(&initialHash); err != nil {
return fmt.Errorf("find initial commit: %w", err)
}
// Count commits to check if flatten is needed.
var commitCount int
if err := conn.QueryRowContext(ctx,
"SELECT COUNT(*) FROM dolt_log",
).Scan(&commitCount); err != nil {
return fmt.Errorf("count commits: %w", err)
}
if commitCount <= 1 {
return nil // already flat
}
execSQL := func(name, query string, args ...interface{}) error {
if _, err := conn.ExecContext(ctx, query, args...); err != nil {
return fmt.Errorf("flatten step %q: %w", name, err)
}
return nil
}
steps := []struct {
name string
query string
args []interface{}
}{
{"create temp branch", "CALL DOLT_BRANCH('flatten-tmp')", nil},
{"checkout temp branch", "CALL DOLT_CHECKOUT('flatten-tmp')", nil},
{"soft reset to initial", "CALL DOLT_RESET('--soft', ?)", []interface{}{initialHash}},
{"commit flattened snapshot", "CALL DOLT_COMMIT('-Am', 'flatten: squash all history into single commit')", nil},
{"checkout main", "CALL DOLT_CHECKOUT('main')", nil},
{"reset main to flattened", "CALL DOLT_RESET('--hard', 'flatten-tmp')", nil},
{"delete temp branch", "CALL DOLT_BRANCH('-D', 'flatten-tmp')", nil},
}
for _, s := range steps {
if err := execSQL(s.name, s.query, s.args...); err != nil {
return err
}
}
return nil
}
// FlattenDryRun returns the commit count and initial hash without modifying anything.
func FlattenDryRun(ctx context.Context, conn DBConn) (commitCount int, initialHash string, err error) {
if err = conn.QueryRowContext(ctx,
"SELECT COUNT(*) FROM dolt_log",
).Scan(&commitCount); err != nil {
err = fmt.Errorf("count commits: %w", err)
return
}
if err = conn.QueryRowContext(ctx,
"SELECT commit_hash FROM dolt_log ORDER BY date ASC LIMIT 1",
).Scan(&initialHash); err != nil {
err = fmt.Errorf("find initial commit: %w", err)
return
}
return
}