Skip to main content

gen_models/
history.rs

1//! Doltlite-backed history primitives for the graph database.
2//!
3//! The following Dolt primitives are still unsupported at the Gen layer:
4//! `dolt_rebase`
5//! `dolt_revert`
6//! `dolt_tag`
7//! conflict-resolution helpers beyond read-only inspection through
8//! `HistoryStore::conflicts()`
9//!
10//! That means:
11//! Gen can detect merge/apply conflicts, but it does not yet expose a dedicated
12//! conflict-resolution workflow.
13//! Gen does not offer CLI flows for rebase, revert, or tags.
14//! Any new history behavior should extend this module first rather than issuing
15//! raw Dolt SQL from callers.
16
17use gen_core::{BranchName, CommitRef, DoltHashId};
18use rusqlite::Result as SqlResult;
19use thiserror::Error;
20
21use crate::db::GraphConnection;
22
23pub mod dolt;
24
25#[derive(Clone, Debug, Eq, PartialEq)]
26pub struct HistoryEntry {
27    pub commit_hash: DoltHashId,
28    pub parent_hash: Option<DoltHashId>,
29    pub committer: String,
30    pub email: String,
31    pub date: String,
32    pub message: String,
33    pub is_head: bool,
34}
35
36#[derive(Clone, Debug, Eq, PartialEq)]
37pub struct HistoryStatus {
38    pub table_name: String,
39    pub staged: bool,
40    pub status: String,
41}
42
43#[derive(Clone, Debug, Eq, PartialEq)]
44pub struct HistoryConflict {
45    pub table_name: String,
46    pub num_conflicts: i64,
47}
48
49#[derive(Debug, Error)]
50pub enum HistoryError {
51    #[error("Database error: {0}")]
52    Database(#[from] rusqlite::Error),
53    #[error("Commit reference '{0}' is ambiguous")]
54    AmbiguousReference(String),
55    #[error("No commit resolved for '{0}'")]
56    UnresolvedReference(String),
57}
58
59pub type HistoryResult<T> = Result<T, HistoryError>;
60
61pub trait HistoryStore {
62    fn current_head(&self) -> SqlResult<Option<DoltHashId>>;
63    fn current_branch(&self) -> SqlResult<Option<BranchName>>;
64    fn resolve_ref(&self, reference: &CommitRef) -> HistoryResult<Option<DoltHashId>>;
65    fn resolve_operation_hash(&self, reference: &CommitRef) -> HistoryResult<DoltHashId>;
66    fn log_for_ref(
67        &self,
68        reference: &CommitRef,
69        limit: Option<usize>,
70    ) -> HistoryResult<Vec<HistoryEntry>>;
71    fn log(&self, limit: Option<usize>) -> SqlResult<Vec<HistoryEntry>>;
72    fn status(&self) -> SqlResult<Vec<HistoryStatus>>;
73    fn conflicts(&self) -> SqlResult<Vec<HistoryConflict>>;
74    fn commit_exists(&self, commit_hash: &DoltHashId) -> SqlResult<bool>;
75    fn commit_parents(&self, commit_hash: &DoltHashId) -> SqlResult<Vec<DoltHashId>>;
76    fn is_ancestor(
77        &self,
78        ancestor_hash: &DoltHashId,
79        descendant_hash: &DoltHashId,
80    ) -> SqlResult<bool>;
81    fn commit_all(&self, message: &str) -> SqlResult<DoltHashId>;
82    fn checkout_branch(&self, branch_name: &BranchName) -> SqlResult<()>;
83    fn checkout_commit(&self, commit_hash: &DoltHashId) -> SqlResult<()>;
84    fn create_branch(
85        &self,
86        branch_name: &BranchName,
87        start_ref: Option<&CommitRef>,
88    ) -> SqlResult<()>;
89    fn delete_branch(&self, branch_name: &BranchName) -> SqlResult<()>;
90    fn merge_base(&self, source: &CommitRef, target: &CommitRef) -> SqlResult<DoltHashId>;
91    /// Merges a reference into the current branch.
92    ///
93    /// Callers must invoke this method inside [`GraphConnection::with_transaction`] for the
94    /// supported merge workflow: the transaction both rolls back failed merges and keeps
95    /// DoltLite conflict state available while conflicts are inspected and resolved.
96    fn merge(&self, reference: &CommitRef) -> SqlResult<DoltHashId>;
97    fn cherry_pick(&self, commit_hash: &DoltHashId) -> SqlResult<()>;
98    fn reset_hard(&self, target: &CommitRef) -> SqlResult<()>;
99    fn push(&self, remote_name: &str, branch_name: &BranchName) -> SqlResult<()>;
100    fn pull(&self, remote_name: &str, branch_name: &BranchName) -> SqlResult<()>;
101    fn fetch(&self, remote_name: &str, branch_name: Option<&BranchName>) -> SqlResult<()>;
102    fn graph(&self) -> &GraphConnection;
103}