use crate::errors::{GitError, Result, RonaError};
use regex::Regex;
use std::process::Output;
pub mod branch;
pub mod commit;
pub mod files;
pub mod remote;
pub mod repository;
pub mod staging;
pub mod status;
use colored::Colorize;
pub use branch::{
format_branch_name, get_all_branches, get_current_branch, git_branch_only, git_create_branch,
git_merge, git_pull, git_rebase, git_switch, sanitize_branch_name,
};
pub use commit::{
COMMIT_MESSAGE_FILE_PATH, COMMIT_TYPES, generate_commit_message, get_current_commit_nb,
git_commit,
};
pub use files::{add_to_git_exclude, create_needed_files};
pub use remote::git_push;
pub use repository::{find_git_root, get_top_level_path};
pub use staging::{
git_add_files, git_add_with_exclude_patterns, git_restore_files, git_unstage_files,
};
pub use status::{
StatusEntry, get_all_staged_file_paths, get_restorable_files, get_stageable_files,
get_staged_files, get_status_files,
};
#[doc(hidden)]
#[tracing::instrument(skip(output))]
pub fn handle_output(method_name: &str, output: &Output) -> Result<()> {
use crate::errors::pretty_print_error;
if output.status.success() {
tracing::debug!("{method_name} successful!");
if !output.stdout.is_empty() {
println!("{}", String::from_utf8_lossy(&output.stdout).trim());
}
Ok(())
} else {
let error_message = String::from_utf8_lossy(&output.stderr);
println!("\n{}", format!("Git {method_name} failed:").red().bold());
pretty_print_error(&error_message);
Err(RonaError::Git(GitError::CommandFailed {
command: method_name.to_string(),
output: error_message.trim().to_string(),
}))
}
}
#[doc(hidden)]
pub fn extract_filenames(message: &str, pattern: &str) -> Result<Vec<String>> {
let regex = Regex::new(pattern).map_err(|e| {
RonaError::Git(GitError::InvalidStatus {
output: format!("Failed to compile regex pattern: {e}"),
})
})?;
let mut result = Vec::new();
for line in message.lines() {
if regex.is_match(line)
&& let Some(captures) = regex.captures(line)
{
if let Some(new_name) = captures.get(2) {
result.push(new_name.as_str().to_string());
} else if let Some(file_name) = captures.get(1) {
result.push(file_name.as_str().to_string());
}
}
}
Ok(result)
}