Skip to main content

ralph_workflow/cli/handlers/
baseline.rs

1//! Baseline display handler.
2//!
3//! Handles the --show-baseline CLI flag to display the current
4//! start commit and review baseline state.
5
6use std::io;
7
8use crate::git_helpers::{get_current_head_oid, get_review_baseline_info, load_review_baseline};
9use crate::git_helpers::{load_start_point, ReviewBaseline};
10
11/// Handle the --show-baseline flag.
12///
13/// Displays information about the current start commit and review baseline.
14pub fn handle_show_baseline() -> io::Result<()> {
15    println!("╺━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
16    println!("RALPH BASELINE STATE\n");
17
18    // Show start commit state
19    println!("Start Commit (.agent/start_commit):");
20    match load_start_point() {
21        Ok(crate::git_helpers::StartPoint::Commit(oid)) => {
22            println!("  Commit: {}", oid);
23            print_commit_info(&oid.to_string());
24        }
25        Ok(crate::git_helpers::StartPoint::EmptyRepo) => {
26            println!("  State: Empty repository (no commits yet)");
27        }
28        Err(e) => {
29            println!("  Error: {e}");
30        }
31    }
32
33    println!();
34
35    // Show review baseline state
36    println!("Review Baseline (.agent/review_baseline.txt):");
37    match load_review_baseline() {
38        Ok(ReviewBaseline::Commit(oid)) => {
39            println!("  Commit: {}", oid);
40            print_commit_info(&oid.to_string());
41        }
42        Ok(ReviewBaseline::NotSet) => {
43            println!("  State: Not set (using start commit for diff)");
44        }
45        Err(e) => {
46            println!("  Error: {e}");
47        }
48    }
49
50    println!();
51
52    // Show baseline info (commits since baseline, stale status)
53    match get_review_baseline_info() {
54        Ok((baseline_oid, commits_since, is_stale)) => {
55            if let Some(oid) = baseline_oid {
56                println!("Baseline Analysis:");
57                println!("  Commits since baseline: {}", commits_since);
58                if is_stale {
59                    println!(
60                        "  Status: STALE (>10 commits behind)\n           \
61                          Consider running: ralph --reset-start-commit"
62                    );
63                } else {
64                    println!("  Status: Current (within 10 commits)");
65                }
66
67                // Show current HEAD for comparison
68                if let Ok(head) = get_current_head_oid() {
69                    println!();
70                    println!("Current HEAD: {}", head);
71                    if head != oid {
72                        println!(
73                            "  Difference: HEAD is {} commits ahead of baseline",
74                            commits_since
75                        );
76                    }
77                }
78            } else {
79                println!("Baseline Analysis:");
80                println!("  No review baseline set - using start commit");
81            }
82        }
83        Err(e) => {
84            println!("Could not analyze baseline: {e}");
85        }
86    }
87
88    println!("\n╺━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
89
90    Ok(())
91}
92
93/// Print information about a commit.
94fn print_commit_info(oid: &str) {
95    if let Ok(repo) = git2::Repository::discover(".") {
96        if let Ok(parsed_oid) = git2::Oid::from_str(oid) {
97            match repo.find_commit(parsed_oid) {
98                Ok(commit) => {
99                    // Get short ID
100                    let short_id = commit
101                        .as_object()
102                        .short_id()
103                        .ok()
104                        .and_then(|buf| buf.as_str().map(|s| s.to_string()))
105                        .unwrap_or_else(|| {
106                            let len = 8.min(oid.len());
107                            oid[..len].to_string()
108                        });
109
110                    println!("  Short ID: {}", short_id);
111
112                    // Get author info
113                    let author = commit.author();
114                    let name = author.name().unwrap_or("<unknown>");
115                    let when = author.when();
116                    println!("  Author: {}", name);
117                    println!("  Time: {} seconds since epoch", when.seconds());
118
119                    // Get commit summary
120                    let summary = commit.summary().unwrap_or("<no message>");
121                    // Truncate long summaries
122                    let summary = if summary.len() > 60 {
123                        format!("{}...", &summary[..57.min(summary.len())])
124                    } else {
125                        summary.to_string()
126                    };
127                    println!("  Summary: {}", summary);
128                }
129                Err(_) => {
130                    println!("  Warning: Commit not found in repository");
131                    println!("  The OID may reference a deleted commit or be from a different repository");
132                }
133            }
134        } else {
135            println!("  Warning: Invalid OID format");
136        }
137    }
138}