Skip to main content

omni_dev/
git.rs

1//! Git operations and repository management.
2
3use anyhow::{Context, Result};
4use git2::Repository;
5
6pub mod amendment;
7pub mod commit;
8pub mod remote;
9pub mod repository;
10
11pub use amendment::AmendmentHandler;
12pub use commit::{CommitAnalysis, CommitAnalysisForAI, CommitInfo, CommitInfoForAI};
13pub use remote::RemoteInfo;
14pub use repository::GitRepository;
15
16/// Number of hex characters to show in abbreviated commit hashes.
17pub const SHORT_HASH_LEN: usize = 8;
18
19/// Length of a full SHA-1 commit hash in hex characters.
20pub const FULL_HASH_LEN: usize = 40;
21
22/// Checks if the current directory is a git repository.
23pub fn check_git_repo() -> Result<()> {
24    Repository::open(".").context("Not in a git repository")?;
25    Ok(())
26}
27
28/// Checks if the working directory is clean.
29pub fn check_working_directory_clean() -> Result<()> {
30    let repo = Repository::open(".").context("Failed to open git repository")?;
31
32    let statuses = repo
33        .statuses(None)
34        .context("Failed to get repository status")?;
35
36    if !statuses.is_empty() {
37        anyhow::bail!(
38            "Working directory is not clean. Please commit or stash changes before amending commit messages."
39        );
40    }
41
42    Ok(())
43}