feat(dolt): add GetChangesSinceExport and CommitExists methods (bd-ejv83)
Add versioned storage methods for incremental export support: - GetChangesSinceExport: returns changes since a commit hash, with NeedsFullExport flag for invalid/GC'd commits - CommitExists: checks if a commit hash exists, supports short prefixes Also fixes dolt_diff syntax for embedded driver (from_ref, to_ref, table). Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
committed by
Steve Yegge
parent
6190dd9362
commit
938a17cda5
@@ -49,7 +49,8 @@ func (s *DoltStore) Diff(ctx context.Context, fromRef, toRef string) ([]*storage
|
||||
return nil, fmt.Errorf("invalid toRef: %w", err)
|
||||
}
|
||||
|
||||
// Query issue-level diffs directly
|
||||
// Query issue-level diffs using dolt_diff table function
|
||||
// Syntax: dolt_diff(from_ref, to_ref, 'table_name')
|
||||
// Note: refs are validated above
|
||||
// nolint:gosec // G201: refs validated by validateRef()
|
||||
query := fmt.Sprintf(`
|
||||
@@ -61,7 +62,7 @@ func (s *DoltStore) Diff(ctx context.Context, fromRef, toRef string) ([]*storage
|
||||
from_description, to_description,
|
||||
from_status, to_status,
|
||||
from_priority, to_priority
|
||||
FROM dolt_diff_issues('%s', '%s')
|
||||
FROM dolt_diff('%s', '%s', 'issues')
|
||||
`, fromRef, toRef)
|
||||
|
||||
rows, err := s.db.QueryContext(ctx, query)
|
||||
@@ -186,3 +187,82 @@ func (s *DoltStore) GetConflicts(ctx context.Context) ([]storage.Conflict, error
|
||||
}
|
||||
return conflicts, nil
|
||||
}
|
||||
|
||||
// ExportChanges represents the result of GetChangesSinceExport.
|
||||
type ExportChanges struct {
|
||||
Entries []*storage.DiffEntry // Changes since the export commit
|
||||
NeedsFullExport bool // True if fromCommit is invalid/GC'd
|
||||
}
|
||||
|
||||
// GetChangesSinceExport returns changes since a specific commit hash.
|
||||
// If the commit hash is invalid or has been garbage collected, it returns
|
||||
// NeedsFullExport=true to indicate a full export is required.
|
||||
func (s *DoltStore) GetChangesSinceExport(ctx context.Context, fromCommit string) (*ExportChanges, error) {
|
||||
// Empty commit means this is the first export
|
||||
if fromCommit == "" {
|
||||
return &ExportChanges{NeedsFullExport: true}, nil
|
||||
}
|
||||
|
||||
// Validate the ref format
|
||||
if err := validateRef(fromCommit); err != nil {
|
||||
return &ExportChanges{NeedsFullExport: true}, nil
|
||||
}
|
||||
|
||||
// Check if the commit exists
|
||||
exists, err := s.CommitExists(ctx, fromCommit)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to check commit existence: %w", err)
|
||||
}
|
||||
if !exists {
|
||||
return &ExportChanges{NeedsFullExport: true}, nil
|
||||
}
|
||||
|
||||
// Get current HEAD commit to check if we're already at fromCommit
|
||||
currentCommit, err := s.GetCurrentCommit(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get current commit: %w", err)
|
||||
}
|
||||
|
||||
// If fromCommit equals HEAD, there are no changes.
|
||||
// Note: This also avoids a nil pointer panic in the embedded Dolt driver
|
||||
// when querying dolt_diff with identical from/to refs.
|
||||
if fromCommit == currentCommit {
|
||||
return &ExportChanges{Entries: nil}, nil
|
||||
}
|
||||
|
||||
// Get the diff from that commit to HEAD
|
||||
entries, err := s.Diff(ctx, fromCommit, "HEAD")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get diff: %w", err)
|
||||
}
|
||||
|
||||
return &ExportChanges{Entries: entries}, nil
|
||||
}
|
||||
|
||||
// CommitExists checks whether a commit hash exists in the repository.
|
||||
// Returns false for empty strings, malformed input, or non-existent commits.
|
||||
func (s *DoltStore) CommitExists(ctx context.Context, commitHash string) (bool, error) {
|
||||
// Empty string is not a valid commit
|
||||
if commitHash == "" {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// Validate format to reject malformed input
|
||||
if err := validateRef(commitHash); err != nil {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// Query dolt_log to check if the commit exists.
|
||||
// Supports both full hashes and short prefixes (like git's short SHA).
|
||||
// The exact match handles full hashes; LIKE handles prefixes.
|
||||
var count int
|
||||
err := s.db.QueryRowContext(ctx, `
|
||||
SELECT COUNT(*) FROM dolt_log
|
||||
WHERE commit_hash = ? OR commit_hash LIKE ?
|
||||
`, commitHash, commitHash+"%").Scan(&count)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("failed to check commit existence: %w", err)
|
||||
}
|
||||
|
||||
return count > 0, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user