Skip to main content

ralph_workflow/git_helpers/review_baseline/
baseline_persistence.rs

1// Review baseline persistence, parsing, and commit-distance calculation.
2
3/// Path to the review baseline file.
4///
5/// Stored in `.agent/review_baseline.txt`, this file contains the OID (SHA)
6/// for the baseline commit used for per-review-cycle diffs.
7pub const REVIEW_BASELINE_FILE: &str = ".agent/review_baseline.txt";
8
9/// Sentinel value for "baseline not set".
10///
11/// This is written to the baseline file when a baseline cannot be determined
12/// (e.g., empty repository / unborn HEAD) or when explicitly cleared.
13pub const BASELINE_NOT_SET: &str = "__BASELINE_NOT_SET__";
14
15#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16pub enum ReviewBaseline {
17    /// A concrete commit OID to diff from.
18    Commit(git2::Oid),
19    /// No baseline set; fall back to start_commit.
20    NotSet,
21}
22
23/// Load the review baseline from the working directory.
24pub fn load_review_baseline() -> io::Result<ReviewBaseline> {
25    let repo = git2::Repository::discover(".").map_err(|e| to_io_error(&e))?;
26    let repo_root = repo
27        .workdir()
28        .ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, "No workdir for repository"))?;
29    let workspace = WorkspaceFs::new(repo_root.to_path_buf());
30    load_review_baseline_with_workspace(&workspace)
31}
32
33/// Load the review baseline using the workspace abstraction.
34pub fn load_review_baseline_with_workspace(
35    workspace: &dyn Workspace,
36) -> io::Result<ReviewBaseline> {
37    let path = Path::new(REVIEW_BASELINE_FILE);
38    if !workspace.exists(path) {
39        return Ok(ReviewBaseline::NotSet);
40    }
41
42    let content = workspace.read(path)?;
43    let raw = content.trim();
44
45    if raw.is_empty() || raw == BASELINE_NOT_SET {
46        return Ok(ReviewBaseline::NotSet);
47    }
48
49    let oid = git2::Oid::from_str(raw).map_err(|_| {
50        io::Error::new(
51            io::ErrorKind::InvalidData,
52            format!(
53                "Invalid baseline OID in {}: '{}'",
54                REVIEW_BASELINE_FILE, raw
55            ),
56        )
57    })?;
58
59    Ok(ReviewBaseline::Commit(oid))
60}
61
62/// Update the review baseline to the current HEAD.
63pub fn update_review_baseline() -> io::Result<()> {
64    let repo = git2::Repository::discover(".").map_err(|e| to_io_error(&e))?;
65    let repo_root = repo
66        .workdir()
67        .ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, "No workdir for repository"))?;
68    let workspace = WorkspaceFs::new(repo_root.to_path_buf());
69    update_review_baseline_with_workspace(&workspace)
70}
71
72/// Update the review baseline using the workspace abstraction.
73pub fn update_review_baseline_with_workspace(workspace: &dyn Workspace) -> io::Result<()> {
74    let path = Path::new(REVIEW_BASELINE_FILE);
75    match get_current_head_oid() {
76        Ok(oid) => workspace.write(path, oid.trim()),
77        Err(e) if e.kind() == io::ErrorKind::NotFound => workspace.write(path, BASELINE_NOT_SET),
78        Err(e) => Err(e),
79    }
80}
81
82/// Get review baseline info: (baseline_oid, commits_since, is_stale).
83///
84/// If no baseline is set, returns `(None, 0, false)`.
85pub fn get_review_baseline_info() -> io::Result<(Option<String>, usize, bool)> {
86    let repo = git2::Repository::discover(".").map_err(|e| to_io_error(&e))?;
87    match load_review_baseline()? {
88        ReviewBaseline::Commit(oid) => {
89            let oid_str = oid.to_string();
90            let commits_since = count_commits_since(&repo, &oid_str)?;
91            let is_stale = commits_since > 10;
92            Ok((Some(oid_str), commits_since, is_stale))
93        }
94        ReviewBaseline::NotSet => Ok((None, 0, false)),
95    }
96}
97
98fn count_commits_since(repo: &git2::Repository, baseline_oid: &str) -> io::Result<usize> {
99    let baseline = git2::Oid::from_str(baseline_oid).map_err(|_| {
100        io::Error::new(
101            io::ErrorKind::InvalidInput,
102            format!("Invalid baseline OID: {}", baseline_oid),
103        )
104    })?;
105
106    let head_oid = match repo.head() {
107        Ok(head) => head.peel_to_commit().map_err(|e| to_io_error(&e))?.id(),
108        Err(ref e) if e.code() == git2::ErrorCode::UnbornBranch => return Ok(0),
109        Err(e) => return Err(to_io_error(&e)),
110    };
111
112    // Prefer libgit2 graph calculation when possible.
113    if let Ok((ahead, _behind)) = repo.graph_ahead_behind(head_oid, baseline) {
114        return Ok(ahead);
115    }
116
117    // Fallback: count commits reachable from HEAD excluding those reachable from baseline.
118    let mut walk = repo.revwalk().map_err(|e| to_io_error(&e))?;
119    walk.push(head_oid).map_err(|e| to_io_error(&e))?;
120    walk.hide(baseline).map_err(|e| to_io_error(&e))?;
121    Ok(walk.count())
122}
123
124fn to_io_error(err: &git2::Error) -> io::Error {
125    io::Error::other(err.to_string())
126}