use git2::{Cred, FetchOptions, RemoteCallbacks, Repository, build::CheckoutBuilder}; use std::env; use std::fs; use std::io; use std::io::Write; use std::path::Path; use std::process::Command; pub fn dir_size(path: &Path) -> std::io::Result { let mut size = 0; if path.is_dir() { for entry in fs::read_dir(path)? { let entry = entry?; let metadata = entry.metadata()?; if metadata.is_file() { size += metadata.len(); } else if metadata.is_dir() { size += dir_size(&entry.path())?; } } } Ok(size) } pub fn get_editor() -> String { env::var("VISUAL") .or_else(|_| env::var("EDITOR")) .unwrap_or_else(|_| "nano".to_string()) } pub fn open_in_editor(editor: &str, file: &str) -> Result<(), String> { let status = Command::new(editor) .arg(file) .status() .map_err(|e| format!("failed to execute editor: {}", e))?; if !status.success() { return Err(format!("editor exited with non-zero status: {}", status)); } Ok(()) } pub fn pull_repo(path: &Path) -> Result<(), git2::Error> { let repo = Repository::open(path)?; let head = repo.head()?; let branch = head .shorthand() .ok_or_else(|| git2::Error::from_str("Could not determine current branch"))?; let mut callbacks = RemoteCallbacks::new(); callbacks.credentials(|_url, username_from_url, _allowed| { Cred::ssh_key_from_agent(username_from_url.unwrap()) }); let mut fetch_options = FetchOptions::new(); fetch_options.remote_callbacks(callbacks); let mut remote = repo.find_remote("origin")?; remote.fetch(&[branch], Some(&mut fetch_options), None)?; let fetch_head = repo.find_reference("FETCH_HEAD")?; let fetch_commit = repo.reference_to_annotated_commit(&fetch_head)?; let (analysis, _pref) = repo.merge_analysis(&[&fetch_commit])?; if analysis.is_fast_forward() { println!("Fast-forwarding..."); let refname = format!("refs/heads/{}", branch); let mut reference = repo.find_reference(&refname)?; reference.set_target(fetch_commit.id(), "Fast-Forward")?; repo.set_head(&refname)?; repo.checkout_head(Some(CheckoutBuilder::default().force()))?; } else if analysis.is_up_to_date() { println!("Already up to date."); } else { println!("Non fast-forward merge required (manual merge needed)."); } Ok(()) } pub fn yn_prompt(prompt: &str) -> bool { print!("{} [y/n]: ", prompt); io::stdout().flush().unwrap(); // Read input from user let mut input = String::new(); io::stdin().read_line(&mut input).unwrap(); // Normalize input let input = input.trim().to_lowercase(); match input.as_str() { "y" | "yes" | "" => true, _ => false, } }