use crate::cli::output::Output;
use crate::errors::{CascadeError, Result};
use chrono;
use dialoguer::{theme::ColorfulTheme, Confirm, Select};
use git2::{Oid, Repository, Signature};
use std::path::{Path, PathBuf};
use tracing::{debug, info, warn};
#[derive(Debug, Clone)]
pub struct RepositoryInfo {
pub path: PathBuf,
pub head_branch: Option<String>,
pub head_commit: Option<String>,
pub is_dirty: bool,
pub untracked_files: Vec<String>,
}
#[derive(Debug, Clone)]
struct ForceBackupInfo {
pub backup_branch_name: String,
pub remote_commit_id: String,
#[allow(dead_code)] pub commits_that_would_be_lost: usize,
}
#[derive(Debug, Clone)]
struct BranchDeletionSafety {
pub unpushed_commits: Vec<String>,
pub remote_tracking_branch: Option<String>,
pub is_merged_to_main: bool,
pub main_branch_name: String,
}
#[derive(Debug, Clone)]
struct CheckoutSafety {
#[allow(dead_code)] pub has_uncommitted_changes: bool,
pub modified_files: Vec<String>,
pub staged_files: Vec<String>,
pub untracked_files: Vec<String>,
#[allow(dead_code)] pub stash_created: Option<String>,
#[allow(dead_code)] pub current_branch: Option<String>,
}
#[derive(Debug, Clone)]
pub struct GitSslConfig {
pub accept_invalid_certs: bool,
pub ca_bundle_path: Option<String>,
}
#[derive(Debug, Clone)]
pub struct GitStatusSummary {
staged_files: usize,
unstaged_files: usize,
untracked_files: usize,
}
impl GitStatusSummary {
pub fn is_clean(&self) -> bool {
self.staged_files == 0 && self.unstaged_files == 0 && self.untracked_files == 0
}
pub fn has_staged_changes(&self) -> bool {
self.staged_files > 0
}
pub fn has_unstaged_changes(&self) -> bool {
self.unstaged_files > 0
}
pub fn has_untracked_files(&self) -> bool {
self.untracked_files > 0
}
pub fn staged_count(&self) -> usize {
self.staged_files
}
pub fn unstaged_count(&self) -> usize {
self.unstaged_files
}
pub fn untracked_count(&self) -> usize {
self.untracked_files
}
}
pub struct GitRepository {
repo: Repository,
path: PathBuf,
ssl_config: Option<GitSslConfig>,
bitbucket_credentials: Option<BitbucketCredentials>,
}
#[derive(Debug, Clone)]
struct BitbucketCredentials {
username: Option<String>,
token: Option<String>,
}
impl GitRepository {
pub fn open(path: &Path) -> Result<Self> {
let repo = Repository::discover(path)
.map_err(|e| CascadeError::config(format!("Not a git repository: {e}")))?;
let workdir = repo
.workdir()
.ok_or_else(|| CascadeError::config("Repository has no working directory"))?
.to_path_buf();
let ssl_config = Self::load_ssl_config_from_cascade(&workdir);
let bitbucket_credentials = Self::load_bitbucket_credentials_from_cascade(&workdir);
Ok(Self {
repo,
path: workdir,
ssl_config,
bitbucket_credentials,
})
}
fn load_ssl_config_from_cascade(repo_path: &Path) -> Option<GitSslConfig> {
let config_dir = crate::config::get_repo_config_dir(repo_path).ok()?;
let config_path = config_dir.join("config.json");
let settings = crate::config::Settings::load_from_file(&config_path).ok()?;
if settings.bitbucket.accept_invalid_certs.is_some()
|| settings.bitbucket.ca_bundle_path.is_some()
{
Some(GitSslConfig {
accept_invalid_certs: settings.bitbucket.accept_invalid_certs.unwrap_or(false),
ca_bundle_path: settings.bitbucket.ca_bundle_path,
})
} else {
None
}
}
fn load_bitbucket_credentials_from_cascade(repo_path: &Path) -> Option<BitbucketCredentials> {
let config_dir = crate::config::get_repo_config_dir(repo_path).ok()?;
let config_path = config_dir.join("config.json");
let settings = crate::config::Settings::load_from_file(&config_path).ok()?;
if settings.bitbucket.username.is_some() || settings.bitbucket.token.is_some() {
Some(BitbucketCredentials {
username: settings.bitbucket.username.clone(),
token: settings.bitbucket.token.clone(),
})
} else {
None
}
}
pub fn get_info(&self) -> Result<RepositoryInfo> {
let head_branch = self.get_current_branch().ok();
let head_commit = self.get_head_commit_hash().ok();
let is_dirty = self.is_dirty()?;
let untracked_files = self.get_untracked_files()?;
Ok(RepositoryInfo {
path: self.path.clone(),
head_branch,
head_commit,
is_dirty,
untracked_files,
})
}
pub fn get_current_branch(&self) -> Result<String> {
let head = self
.repo
.head()
.map_err(|e| CascadeError::branch(format!("Could not get HEAD: {e}")))?;
if let Some(name) = head.shorthand() {
Ok(name.to_string())
} else {
let commit = head
.peel_to_commit()
.map_err(|e| CascadeError::branch(format!("Could not get HEAD commit: {e}")))?;
Ok(format!("HEAD@{}", commit.id()))
}
}
pub fn get_head_commit_hash(&self) -> Result<String> {
let head = self
.repo
.head()
.map_err(|e| CascadeError::branch(format!("Could not get HEAD: {e}")))?;
let commit = head
.peel_to_commit()
.map_err(|e| CascadeError::branch(format!("Could not get HEAD commit: {e}")))?;
Ok(commit.id().to_string())
}
pub fn is_dirty(&self) -> Result<bool> {
let statuses = self.repo.statuses(None).map_err(CascadeError::Git)?;
for status in statuses.iter() {
let flags = status.status();
if let Some(path) = status.path() {
if path.starts_with(".cascade/") || path == ".cascade" {
continue;
}
}
if flags.intersects(
git2::Status::INDEX_MODIFIED
| git2::Status::INDEX_NEW
| git2::Status::INDEX_DELETED
| git2::Status::WT_MODIFIED
| git2::Status::WT_NEW
| git2::Status::WT_DELETED,
) {
return Ok(true);
}
}
Ok(false)
}
pub fn get_untracked_files(&self) -> Result<Vec<String>> {
let statuses = self.repo.statuses(None).map_err(CascadeError::Git)?;
let mut untracked = Vec::new();
for status in statuses.iter() {
if status.status().contains(git2::Status::WT_NEW) {
if let Some(path) = status.path() {
untracked.push(path.to_string());
}
}
}
Ok(untracked)
}
pub fn create_branch(&self, name: &str, target: Option<&str>) -> Result<()> {
let target_commit = if let Some(target) = target {
let target_obj = self.repo.revparse_single(target).map_err(|e| {
CascadeError::branch(format!("Could not find target '{target}': {e}"))
})?;
target_obj.peel_to_commit().map_err(|e| {
CascadeError::branch(format!("Target '{target}' is not a commit: {e}"))
})?
} else {
let head = self
.repo
.head()
.map_err(|e| CascadeError::branch(format!("Could not get HEAD: {e}")))?;
head.peel_to_commit()
.map_err(|e| CascadeError::branch(format!("Could not get HEAD commit: {e}")))?
};
self.repo
.branch(name, &target_commit, false)
.map_err(|e| CascadeError::branch(format!("Could not create branch '{name}': {e}")))?;
Ok(())
}
pub fn update_branch_to_commit(&self, branch_name: &str, commit_id: &str) -> Result<()> {
let commit_oid = Oid::from_str(commit_id).map_err(|e| {
CascadeError::branch(format!("Invalid commit ID '{}': {}", commit_id, e))
})?;
let commit = self.repo.find_commit(commit_oid).map_err(|e| {
CascadeError::branch(format!("Commit '{}' not found: {}", commit_id, e))
})?;
if self
.repo
.find_branch(branch_name, git2::BranchType::Local)
.is_ok()
{
let refname = format!("refs/heads/{}", branch_name);
self.repo
.reference(
&refname,
commit_oid,
true,
"update branch to rebased commit",
)
.map_err(|e| {
CascadeError::branch(format!(
"Failed to update branch '{}': {}",
branch_name, e
))
})?;
} else {
self.repo.branch(branch_name, &commit, false).map_err(|e| {
CascadeError::branch(format!("Failed to create branch '{}': {}", branch_name, e))
})?;
}
Ok(())
}
pub fn force_push_single_branch(&self, branch_name: &str) -> Result<()> {
self.force_push_single_branch_with_options(branch_name, false, false)
}
pub fn force_push_single_branch_auto(&self, branch_name: &str) -> Result<()> {
self.force_push_single_branch_with_options(branch_name, true, false)
}
pub fn force_push_single_branch_auto_no_fetch(&self, branch_name: &str) -> Result<()> {
self.force_push_single_branch_with_options(branch_name, true, true)
}
fn force_push_single_branch_with_options(
&self,
branch_name: &str,
auto_confirm: bool,
skip_fetch: bool,
) -> Result<()> {
if self.get_branch_commit_hash(branch_name).is_err() {
return Err(CascadeError::branch(format!(
"Cannot push '{}': branch does not exist locally",
branch_name
)));
}
if !skip_fetch {
self.fetch_with_retry()?;
}
let safety_result = if auto_confirm {
self.check_force_push_safety_auto_no_fetch(branch_name)?
} else {
self.check_force_push_safety_enhanced(branch_name)?
};
if let Some(backup_info) = safety_result {
self.create_backup_branch(branch_name, &backup_info.remote_commit_id)?;
Output::sub_item(format!(
"Created backup branch: {}",
backup_info.backup_branch_name
));
}
self.ensure_index_closed()?;
let marker_path = self.git_dir().join(".cascade-internal-push");
std::fs::write(&marker_path, "1")
.map_err(|e| CascadeError::branch(format!("Failed to create push marker: {}", e)))?;
let output = std::process::Command::new("git")
.args(["push", "--force", "origin", branch_name])
.current_dir(&self.path)
.output()
.map_err(|e| {
let _ = std::fs::remove_file(&marker_path);
CascadeError::branch(format!("Failed to execute git push: {}", e))
})?;
let _ = std::fs::remove_file(&marker_path);
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
let stdout = String::from_utf8_lossy(&output.stdout);
let full_error = if !stdout.is_empty() {
format!("{}\n{}", stderr.trim(), stdout.trim())
} else {
stderr.trim().to_string()
};
return Err(CascadeError::branch(format!(
"Force push failed for '{}':\n{}",
branch_name, full_error
)));
}
Ok(())
}
pub fn checkout_branch(&self, name: &str) -> Result<()> {
self.checkout_branch_with_options(name, false, true)
}
pub fn checkout_branch_silent(&self, name: &str) -> Result<()> {
self.checkout_branch_with_options(name, false, false)
}
pub fn checkout_branch_unsafe(&self, name: &str) -> Result<()> {
self.checkout_branch_with_options(name, true, false)
}
fn checkout_branch_with_options(
&self,
name: &str,
force_unsafe: bool,
show_output: bool,
) -> Result<()> {
debug!("Attempting to checkout branch: {}", name);
if !force_unsafe {
let safety_result = self.check_checkout_safety(name)?;
if let Some(safety_info) = safety_result {
self.handle_checkout_confirmation(name, &safety_info)?;
}
}
let branch = self
.repo
.find_branch(name, git2::BranchType::Local)
.map_err(|e| CascadeError::branch(format!("Could not find branch '{name}': {e}")))?;
let branch_ref = branch.get();
let tree = branch_ref.peel_to_tree().map_err(|e| {
CascadeError::branch(format!("Could not get tree for branch '{name}': {e}"))
})?;
let old_head = self.repo.head().ok();
self.repo
.set_head(&format!("refs/heads/{name}"))
.map_err(|e| CascadeError::branch(format!("Could not update HEAD to '{name}': {e}")))?;
let mut checkout_builder = git2::build::CheckoutBuilder::new();
checkout_builder.force(); checkout_builder.remove_untracked(false);
if let Err(e) = self
.repo
.checkout_tree(tree.as_object(), Some(&mut checkout_builder))
{
if let Some(old) = old_head {
if let Some(old_name) = old.name() {
let _ = self.repo.set_head(old_name);
}
}
return Err(CascadeError::branch(format!(
"Could not checkout branch '{name}': {e}"
)));
}
if show_output {
Output::success(format!("Switched to branch '{name}'"));
}
Ok(())
}
pub fn checkout_commit(&self, commit_hash: &str) -> Result<()> {
self.checkout_commit_with_options(commit_hash, false)
}
pub fn checkout_commit_unsafe(&self, commit_hash: &str) -> Result<()> {
self.checkout_commit_with_options(commit_hash, true)
}
fn checkout_commit_with_options(&self, commit_hash: &str, force_unsafe: bool) -> Result<()> {
debug!("Attempting to checkout commit: {}", commit_hash);
if !force_unsafe {
let safety_result = self.check_checkout_safety(&format!("commit:{commit_hash}"))?;
if let Some(safety_info) = safety_result {
self.handle_checkout_confirmation(&format!("commit {commit_hash}"), &safety_info)?;
}
}
let oid = Oid::from_str(commit_hash).map_err(CascadeError::Git)?;
let commit = self.repo.find_commit(oid).map_err(|e| {
CascadeError::branch(format!("Could not find commit '{commit_hash}': {e}"))
})?;
let tree = commit.tree().map_err(|e| {
CascadeError::branch(format!(
"Could not get tree for commit '{commit_hash}': {e}"
))
})?;
self.repo
.checkout_tree(tree.as_object(), None)
.map_err(|e| {
CascadeError::branch(format!("Could not checkout commit '{commit_hash}': {e}"))
})?;
self.repo.set_head_detached(oid).map_err(|e| {
CascadeError::branch(format!(
"Could not update HEAD to commit '{commit_hash}': {e}"
))
})?;
Output::success(format!(
"Checked out commit '{commit_hash}' (detached HEAD)"
));
Ok(())
}
pub fn branch_exists(&self, name: &str) -> bool {
self.repo.find_branch(name, git2::BranchType::Local).is_ok()
}
pub fn branch_exists_or_fetch(&self, name: &str) -> Result<bool> {
if self.repo.find_branch(name, git2::BranchType::Local).is_ok() {
return Ok(true);
}
crate::cli::output::Output::info(format!(
"Branch '{name}' not found locally, trying to fetch from remote..."
));
use std::process::Command;
let fetch_result = Command::new("git")
.args(["fetch", "origin", &format!("{name}:{name}")])
.current_dir(&self.path)
.output();
match fetch_result {
Ok(output) => {
if output.status.success() {
println!("✅ Successfully fetched '{name}' from origin");
return Ok(self.repo.find_branch(name, git2::BranchType::Local).is_ok());
} else {
let stderr = String::from_utf8_lossy(&output.stderr);
tracing::debug!("Failed to fetch branch '{name}': {stderr}");
}
}
Err(e) => {
tracing::debug!("Git fetch command failed: {e}");
}
}
if name.contains('/') {
crate::cli::output::Output::info("Trying alternative fetch patterns...");
let fetch_all_result = Command::new("git")
.args(["fetch", "origin"])
.current_dir(&self.path)
.output();
if let Ok(output) = fetch_all_result {
if output.status.success() {
let checkout_result = Command::new("git")
.args(["checkout", "-b", name, &format!("origin/{name}")])
.current_dir(&self.path)
.output();
if let Ok(checkout_output) = checkout_result {
if checkout_output.status.success() {
println!(
"✅ Successfully created local branch '{name}' from origin/{name}"
);
return Ok(true);
}
}
}
}
}
Ok(false)
}
pub fn get_branch_commit_hash(&self, branch_name: &str) -> Result<String> {
let branch = self
.repo
.find_branch(branch_name, git2::BranchType::Local)
.map_err(|e| {
CascadeError::branch(format!("Could not find branch '{branch_name}': {e}"))
})?;
let commit = branch.get().peel_to_commit().map_err(|e| {
CascadeError::branch(format!(
"Could not get commit for branch '{branch_name}': {e}"
))
})?;
Ok(commit.id().to_string())
}
pub fn list_branches(&self) -> Result<Vec<String>> {
let branches = self
.repo
.branches(Some(git2::BranchType::Local))
.map_err(CascadeError::Git)?;
let mut branch_names = Vec::new();
for branch in branches {
let (branch, _) = branch.map_err(CascadeError::Git)?;
if let Some(name) = branch.name().map_err(CascadeError::Git)? {
branch_names.push(name.to_string());
}
}
Ok(branch_names)
}
pub fn get_upstream_branch(&self, branch_name: &str) -> Result<Option<String>> {
let config = self.repo.config().map_err(CascadeError::Git)?;
let remote_key = format!("branch.{branch_name}.remote");
let merge_key = format!("branch.{branch_name}.merge");
if let (Ok(remote), Ok(merge_ref)) = (
config.get_string(&remote_key),
config.get_string(&merge_key),
) {
if let Some(branch_part) = merge_ref.strip_prefix("refs/heads/") {
return Ok(Some(format!("{remote}/{branch_part}")));
}
}
let potential_upstream = format!("origin/{branch_name}");
if self
.repo
.find_reference(&format!("refs/remotes/{potential_upstream}"))
.is_ok()
{
return Ok(Some(potential_upstream));
}
Ok(None)
}
pub fn get_ahead_behind_counts(
&self,
local_branch: &str,
upstream_branch: &str,
) -> Result<(usize, usize)> {
let local_ref = self
.repo
.find_reference(&format!("refs/heads/{local_branch}"))
.map_err(|_| {
CascadeError::config(format!("Local branch '{local_branch}' not found"))
})?;
let local_commit = local_ref.peel_to_commit().map_err(CascadeError::Git)?;
let upstream_ref = self
.repo
.find_reference(&format!("refs/remotes/{upstream_branch}"))
.map_err(|_| {
CascadeError::config(format!("Upstream branch '{upstream_branch}' not found"))
})?;
let upstream_commit = upstream_ref.peel_to_commit().map_err(CascadeError::Git)?;
let (ahead, behind) = self
.repo
.graph_ahead_behind(local_commit.id(), upstream_commit.id())
.map_err(CascadeError::Git)?;
Ok((ahead, behind))
}
pub fn set_upstream(&self, branch_name: &str, remote: &str, remote_branch: &str) -> Result<()> {
let mut config = self.repo.config().map_err(CascadeError::Git)?;
let remote_key = format!("branch.{branch_name}.remote");
config
.set_str(&remote_key, remote)
.map_err(CascadeError::Git)?;
let merge_key = format!("branch.{branch_name}.merge");
let merge_value = format!("refs/heads/{remote_branch}");
config
.set_str(&merge_key, &merge_value)
.map_err(CascadeError::Git)?;
Ok(())
}
pub fn commit(&self, message: &str) -> Result<String> {
self.validate_git_user_config()?;
let signature = self.get_signature()?;
let tree_id = self.get_index_tree()?;
let tree = self.repo.find_tree(tree_id).map_err(CascadeError::Git)?;
let head = self.repo.head().map_err(CascadeError::Git)?;
let parent_commit = head.peel_to_commit().map_err(CascadeError::Git)?;
let commit_id = self
.repo
.commit(
Some("HEAD"),
&signature,
&signature,
message,
&tree,
&[&parent_commit],
)
.map_err(CascadeError::Git)?;
Output::success(format!("Created commit: {commit_id} - {message}"));
Ok(commit_id.to_string())
}
pub fn commit_staged_changes(&self, default_message: &str) -> Result<Option<String>> {
let staged_files = self.get_staged_files()?;
if staged_files.is_empty() {
tracing::debug!("No staged changes to commit");
return Ok(None);
}
tracing::debug!("Committing {} staged files", staged_files.len());
let commit_hash = self.commit(default_message)?;
Ok(Some(commit_hash))
}
pub fn stage_all(&self) -> Result<()> {
let mut index = self.repo.index().map_err(CascadeError::Git)?;
index
.add_all(["*"].iter(), git2::IndexAddOption::DEFAULT, None)
.map_err(CascadeError::Git)?;
index.write().map_err(CascadeError::Git)?;
drop(index);
tracing::debug!("Staged all changes");
Ok(())
}
fn ensure_index_closed(&self) -> Result<()> {
let mut index = self.repo.index().map_err(CascadeError::Git)?;
index.write().map_err(CascadeError::Git)?;
drop(index);
std::thread::sleep(std::time::Duration::from_millis(10));
Ok(())
}
pub fn stage_files(&self, file_paths: &[&str]) -> Result<()> {
if file_paths.is_empty() {
tracing::debug!("No files to stage");
return Ok(());
}
let mut index = self.repo.index().map_err(CascadeError::Git)?;
for file_path in file_paths {
index
.add_path(std::path::Path::new(file_path))
.map_err(CascadeError::Git)?;
}
index.write().map_err(CascadeError::Git)?;
drop(index);
tracing::debug!(
"Staged {} specific files: {:?}",
file_paths.len(),
file_paths
);
Ok(())
}
pub fn stage_conflict_resolved_files(&self) -> Result<()> {
let conflicted_files = self.get_conflicted_files()?;
if conflicted_files.is_empty() {
tracing::debug!("No conflicted files to stage");
return Ok(());
}
let file_paths: Vec<&str> = conflicted_files.iter().map(|s| s.as_str()).collect();
self.stage_files(&file_paths)?;
tracing::debug!("Staged {} conflict-resolved files", conflicted_files.len());
Ok(())
}
pub fn cleanup_state(&self) -> Result<()> {
let state = self.repo.state();
if state == git2::RepositoryState::Clean {
return Ok(());
}
tracing::debug!("Cleaning up repository state: {:?}", state);
self.repo.cleanup_state().map_err(|e| {
CascadeError::branch(format!(
"Failed to clean up repository state ({:?}): {}",
state, e
))
})
}
pub fn path(&self) -> &Path {
&self.path
}
pub fn git_dir(&self) -> &Path {
self.repo.path()
}
pub fn common_dir(&self) -> &Path {
self.repo.commondir()
}
pub fn commit_exists(&self, commit_hash: &str) -> Result<bool> {
match Oid::from_str(commit_hash) {
Ok(oid) => match self.repo.find_commit(oid) {
Ok(_) => Ok(true),
Err(_) => Ok(false),
},
Err(_) => Ok(false),
}
}
pub fn is_commit_based_on(&self, commit_hash: &str, expected_base: &str) -> Result<bool> {
let commit_oid = Oid::from_str(commit_hash).map_err(|e| {
CascadeError::branch(format!("Invalid commit hash '{}': {}", commit_hash, e))
})?;
let commit = self.repo.find_commit(commit_oid).map_err(|e| {
CascadeError::branch(format!("Commit '{}' not found: {}", commit_hash, e))
})?;
if commit.parent_count() == 0 {
return Ok(false);
}
let parent = commit.parent(0).map_err(|e| {
CascadeError::branch(format!(
"Could not get parent of commit '{}': {}",
commit_hash, e
))
})?;
let parent_hash = parent.id().to_string();
let expected_base_oid = if let Ok(oid) = Oid::from_str(expected_base) {
oid
} else {
let branch_ref = format!("refs/heads/{}", expected_base);
let reference = self.repo.find_reference(&branch_ref).map_err(|e| {
CascadeError::branch(format!("Could not find base '{}': {}", expected_base, e))
})?;
reference.target().ok_or_else(|| {
CascadeError::branch(format!("Base '{}' has no target commit", expected_base))
})?
};
let expected_base_hash = expected_base_oid.to_string();
tracing::debug!(
"Checking if commit {} is based on {}: parent={}, expected={}",
&commit_hash[..8],
expected_base,
&parent_hash[..8],
&expected_base_hash[..8]
);
Ok(parent_hash == expected_base_hash)
}
pub fn is_descendant_of(&self, descendant: &str, ancestor: &str) -> Result<bool> {
let descendant_oid = Oid::from_str(descendant).map_err(|e| {
CascadeError::branch(format!(
"Invalid commit hash '{}' for descendant check: {}",
descendant, e
))
})?;
let ancestor_oid = Oid::from_str(ancestor).map_err(|e| {
CascadeError::branch(format!(
"Invalid commit hash '{}' for descendant check: {}",
ancestor, e
))
})?;
self.repo
.graph_descendant_of(descendant_oid, ancestor_oid)
.map_err(CascadeError::Git)
}
pub fn get_head_commit(&self) -> Result<git2::Commit<'_>> {
let head = self
.repo
.head()
.map_err(|e| CascadeError::branch(format!("Could not get HEAD: {e}")))?;
head.peel_to_commit()
.map_err(|e| CascadeError::branch(format!("Could not get HEAD commit: {e}")))
}
pub fn get_commit(&self, commit_hash: &str) -> Result<git2::Commit<'_>> {
let oid = Oid::from_str(commit_hash).map_err(CascadeError::Git)?;
self.repo.find_commit(oid).map_err(CascadeError::Git)
}
pub fn get_branch_head(&self, branch_name: &str) -> Result<String> {
let branch = self
.repo
.find_branch(branch_name, git2::BranchType::Local)
.map_err(|e| {
CascadeError::branch(format!("Could not find branch '{branch_name}': {e}"))
})?;
let commit = branch.get().peel_to_commit().map_err(|e| {
CascadeError::branch(format!(
"Could not get commit for branch '{branch_name}': {e}"
))
})?;
Ok(commit.id().to_string())
}
pub fn get_remote_branch_head(&self, branch_name: &str) -> Result<String> {
let refname = format!("refs/remotes/origin/{branch_name}");
let reference = self.repo.find_reference(&refname).map_err(|e| {
CascadeError::branch(format!("Remote branch '{branch_name}' not found: {e}"))
})?;
let target = reference.target().ok_or_else(|| {
CascadeError::branch(format!(
"Remote branch '{branch_name}' does not have a target commit"
))
})?;
Ok(target.to_string())
}
pub fn validate_git_user_config(&self) -> Result<()> {
if let Ok(config) = self.repo.config() {
let name_result = config.get_string("user.name");
let email_result = config.get_string("user.email");
if let (Ok(name), Ok(email)) = (name_result, email_result) {
if !name.trim().is_empty() && !email.trim().is_empty() {
tracing::debug!("Git user config validated: {} <{}>", name, email);
return Ok(());
}
}
}
let is_ci = std::env::var("CI").is_ok();
if is_ci {
tracing::debug!("CI environment - skipping git user config validation");
return Ok(());
}
Output::warning("Git user configuration missing or incomplete");
Output::info("This can cause cherry-pick and commit operations to fail");
Output::info("Please configure git user information:");
Output::bullet("git config user.name \"Your Name\"".to_string());
Output::bullet("git config user.email \"your.email@example.com\"".to_string());
Output::info("Or set globally with the --global flag");
Ok(())
}
pub fn get_user_info(&self) -> (Option<String>, Option<String>) {
let config = match self.repo.config() {
Ok(c) => c,
Err(_) => return (None, None),
};
let name = config.get_string("user.name").ok();
let email = config.get_string("user.email").ok();
(name, email)
}
fn get_signature(&self) -> Result<Signature<'_>> {
if let Ok(config) = self.repo.config() {
let name_result = config.get_string("user.name");
let email_result = config.get_string("user.email");
if let (Ok(name), Ok(email)) = (name_result, email_result) {
if !name.trim().is_empty() && !email.trim().is_empty() {
tracing::debug!("Using git config: {} <{}>", name, email);
return Signature::now(&name, &email).map_err(CascadeError::Git);
}
} else {
tracing::debug!("Git user config incomplete or missing");
}
}
let is_ci = std::env::var("CI").is_ok();
if is_ci {
tracing::debug!("CI environment detected, using fallback signature");
return Signature::now("Cascade CLI", "cascade@example.com").map_err(CascadeError::Git);
}
tracing::warn!("Git user configuration missing - this can cause commit operations to fail");
match Signature::now("Cascade CLI", "cascade@example.com") {
Ok(sig) => {
Output::warning("Git user not configured - using fallback signature");
Output::info("For better git history, run:");
Output::bullet("git config user.name \"Your Name\"".to_string());
Output::bullet("git config user.email \"your.email@example.com\"".to_string());
Output::info("Or set it globally with --global flag");
Ok(sig)
}
Err(e) => {
Err(CascadeError::branch(format!(
"Cannot create git signature: {e}. Please configure git user with:\n git config user.name \"Your Name\"\n git config user.email \"your.email@example.com\""
)))
}
}
}
fn configure_remote_callbacks(&self) -> Result<git2::RemoteCallbacks<'_>> {
self.configure_remote_callbacks_with_fallback(false)
}
fn should_retry_with_default_credentials(&self, error: &git2::Error) -> bool {
match error.class() {
git2::ErrorClass::Http => {
match error.code() {
git2::ErrorCode::Auth => true,
_ => {
let error_string = error.to_string();
error_string.contains("too many redirects")
|| error_string.contains("authentication replays")
|| error_string.contains("authentication required")
}
}
}
git2::ErrorClass::Net => {
let error_string = error.to_string();
error_string.contains("authentication")
|| error_string.contains("unauthorized")
|| error_string.contains("forbidden")
}
_ => false,
}
}
fn should_fallback_to_git_cli(&self, error: &git2::Error) -> bool {
match error.class() {
git2::ErrorClass::Ssl => true,
git2::ErrorClass::Http if error.code() == git2::ErrorCode::Certificate => true,
git2::ErrorClass::Ssh => {
let error_string = error.to_string();
error_string.contains("no callback set")
|| error_string.contains("authentication required")
}
git2::ErrorClass::Net => {
let error_string = error.to_string();
error_string.contains("TLS stream")
|| error_string.contains("SSL")
|| error_string.contains("proxy")
|| error_string.contains("firewall")
}
git2::ErrorClass::Http => {
let error_string = error.to_string();
error_string.contains("TLS stream")
|| error_string.contains("SSL")
|| error_string.contains("proxy")
}
_ => false,
}
}
fn configure_remote_callbacks_with_fallback(
&self,
use_default_first: bool,
) -> Result<git2::RemoteCallbacks<'_>> {
let mut callbacks = git2::RemoteCallbacks::new();
let bitbucket_credentials = self.bitbucket_credentials.clone();
callbacks.credentials(move |url, username_from_url, allowed_types| {
tracing::debug!(
"Authentication requested for URL: {}, username: {:?}, allowed_types: {:?}",
url,
username_from_url,
allowed_types
);
if allowed_types.contains(git2::CredentialType::SSH_KEY) {
if let Some(username) = username_from_url {
tracing::debug!("Trying SSH key authentication for user: {}", username);
return git2::Cred::ssh_key_from_agent(username);
}
}
if allowed_types.contains(git2::CredentialType::USER_PASS_PLAINTEXT) {
if use_default_first {
tracing::debug!("Corporate network mode: trying DefaultCredentials first");
return git2::Cred::default();
}
if url.contains("bitbucket") {
if let Some(creds) = &bitbucket_credentials {
if let (Some(username), Some(token)) = (&creds.username, &creds.token) {
tracing::debug!("Trying Bitbucket username + token authentication");
return git2::Cred::userpass_plaintext(username, token);
}
if let Some(token) = &creds.token {
tracing::debug!("Trying Bitbucket token-as-username authentication");
return git2::Cred::userpass_plaintext(token, "");
}
if let Some(username) = &creds.username {
tracing::debug!("Trying Bitbucket username authentication (will use credential helper)");
return git2::Cred::username(username);
}
}
}
tracing::debug!("Trying default credential helper for HTTPS authentication");
return git2::Cred::default();
}
tracing::debug!("Using default credential fallback");
git2::Cred::default()
});
let mut ssl_configured = false;
if let Some(ssl_config) = &self.ssl_config {
if ssl_config.accept_invalid_certs {
Output::warning(
"SSL certificate verification DISABLED via Cascade config - this is insecure!",
);
callbacks.certificate_check(|_cert, _host| {
tracing::debug!("⚠️ Accepting invalid certificate for host: {}", _host);
Ok(git2::CertificateCheckStatus::CertificateOk)
});
ssl_configured = true;
} else if let Some(ca_path) = &ssl_config.ca_bundle_path {
Output::info(format!(
"Using custom CA bundle from Cascade config: {ca_path}"
));
callbacks.certificate_check(|_cert, host| {
tracing::debug!("Using custom CA bundle for host: {}", host);
Ok(git2::CertificateCheckStatus::CertificateOk)
});
ssl_configured = true;
}
}
if !ssl_configured {
if let Ok(config) = self.repo.config() {
let ssl_verify = config.get_bool("http.sslVerify").unwrap_or(true);
if !ssl_verify {
Output::warning(
"SSL certificate verification DISABLED via git config - this is insecure!",
);
callbacks.certificate_check(|_cert, host| {
tracing::debug!("⚠️ Bypassing SSL verification for host: {}", host);
Ok(git2::CertificateCheckStatus::CertificateOk)
});
ssl_configured = true;
} else if let Ok(ca_path) = config.get_string("http.sslCAInfo") {
Output::info(format!("Using custom CA bundle from git config: {ca_path}"));
callbacks.certificate_check(|_cert, host| {
tracing::debug!("Using git config CA bundle for host: {}", host);
Ok(git2::CertificateCheckStatus::CertificateOk)
});
ssl_configured = true;
}
}
}
if !ssl_configured {
tracing::debug!(
"Using system certificate store for SSL verification (default behavior)"
);
if cfg!(target_os = "macos") {
tracing::debug!("macOS detected - using default certificate validation");
} else {
callbacks.certificate_check(|_cert, host| {
tracing::debug!("System certificate validation for host: {}", host);
Ok(git2::CertificateCheckStatus::CertificatePassthrough)
});
}
}
Ok(callbacks)
}
fn get_index_tree(&self) -> Result<Oid> {
let mut index = self.repo.index().map_err(CascadeError::Git)?;
index.write_tree().map_err(CascadeError::Git)
}
pub fn get_status(&self) -> Result<git2::Statuses<'_>> {
self.repo.statuses(None).map_err(CascadeError::Git)
}
pub fn get_status_summary(&self) -> Result<GitStatusSummary> {
let statuses = self.get_status()?;
let mut staged_files = 0;
let mut unstaged_files = 0;
let mut untracked_files = 0;
for status in statuses.iter() {
let flags = status.status();
if flags.intersects(
git2::Status::INDEX_MODIFIED
| git2::Status::INDEX_NEW
| git2::Status::INDEX_DELETED
| git2::Status::INDEX_RENAMED
| git2::Status::INDEX_TYPECHANGE,
) {
staged_files += 1;
}
if flags.intersects(
git2::Status::WT_MODIFIED
| git2::Status::WT_DELETED
| git2::Status::WT_TYPECHANGE
| git2::Status::WT_RENAMED,
) {
unstaged_files += 1;
}
if flags.intersects(git2::Status::WT_NEW) {
untracked_files += 1;
}
}
Ok(GitStatusSummary {
staged_files,
unstaged_files,
untracked_files,
})
}
pub fn get_current_commit_hash(&self) -> Result<String> {
self.get_head_commit_hash()
}
pub fn get_commit_count_between(&self, from_commit: &str, to_commit: &str) -> Result<usize> {
let from_oid = git2::Oid::from_str(from_commit).map_err(CascadeError::Git)?;
let to_oid = git2::Oid::from_str(to_commit).map_err(CascadeError::Git)?;
let mut revwalk = self.repo.revwalk().map_err(CascadeError::Git)?;
revwalk.push(to_oid).map_err(CascadeError::Git)?;
revwalk.hide(from_oid).map_err(CascadeError::Git)?;
Ok(revwalk.count())
}
pub fn get_remote_url(&self, name: &str) -> Result<String> {
let remote = self.repo.find_remote(name).map_err(CascadeError::Git)?;
Ok(remote.url().unwrap_or("unknown").to_string())
}
pub fn cherry_pick(&self, commit_hash: &str) -> Result<String> {
tracing::debug!("Cherry-picking commit {}", commit_hash);
self.validate_git_user_config()?;
let oid = Oid::from_str(commit_hash).map_err(CascadeError::Git)?;
let commit = self.repo.find_commit(oid).map_err(CascadeError::Git)?;
let commit_tree = commit.tree().map_err(CascadeError::Git)?;
let parent_commit = if commit.parent_count() > 0 {
commit.parent(0).map_err(CascadeError::Git)?
} else {
let empty_tree_oid = self.repo.treebuilder(None)?.write()?;
let empty_tree = self.repo.find_tree(empty_tree_oid)?;
let sig = self.get_signature()?;
return self
.repo
.commit(
Some("HEAD"),
&sig,
&sig,
commit.message().unwrap_or("Cherry-picked commit"),
&empty_tree,
&[],
)
.map(|oid| oid.to_string())
.map_err(CascadeError::Git);
};
let parent_tree = parent_commit.tree().map_err(CascadeError::Git)?;
let head_commit = self.get_head_commit()?;
let head_tree = head_commit.tree().map_err(CascadeError::Git)?;
let mut index = self
.repo
.merge_trees(&parent_tree, &head_tree, &commit_tree, None)
.map_err(CascadeError::Git)?;
if index.has_conflicts() {
debug!("Cherry-pick has conflicts - writing conflicted state to disk for resolution");
let mut repo_index = self.repo.index().map_err(CascadeError::Git)?;
repo_index.clear().map_err(CascadeError::Git)?;
repo_index
.read_tree(&head_tree)
.map_err(CascadeError::Git)?;
repo_index
.add_all(["*"].iter(), git2::IndexAddOption::DEFAULT, None)
.map_err(CascadeError::Git)?;
drop(repo_index);
self.ensure_index_closed()?;
let cherry_pick_output = std::process::Command::new("git")
.args(["cherry-pick", commit_hash])
.current_dir(self.path())
.output()
.map_err(CascadeError::Io)?;
if !cherry_pick_output.status.success() {
debug!("Git CLI cherry-pick failed as expected (has conflicts)");
}
self.repo
.index()
.and_then(|mut idx| idx.read(true).map(|_| ()))
.map_err(CascadeError::Git)?;
debug!("Conflicted state written and index reloaded - auto-resolve can now process conflicts");
return Err(CascadeError::branch(format!(
"Cherry-pick of {commit_hash} has conflicts that need manual resolution"
)));
}
let merged_tree_oid = index.write_tree_to(&self.repo).map_err(CascadeError::Git)?;
let merged_tree = self
.repo
.find_tree(merged_tree_oid)
.map_err(CascadeError::Git)?;
let signature = self.get_signature()?;
let message = commit.message().unwrap_or("Cherry-picked commit");
let new_commit_oid = self
.repo
.commit(
Some("HEAD"),
&signature,
&signature,
message,
&merged_tree,
&[&head_commit],
)
.map_err(CascadeError::Git)?;
let new_commit = self
.repo
.find_commit(new_commit_oid)
.map_err(CascadeError::Git)?;
let new_tree = new_commit.tree().map_err(CascadeError::Git)?;
self.repo
.checkout_tree(
new_tree.as_object(),
Some(git2::build::CheckoutBuilder::new().force()),
)
.map_err(CascadeError::Git)?;
tracing::debug!("Cherry-picked {} -> {}", commit_hash, new_commit_oid);
Ok(new_commit_oid.to_string())
}
pub fn has_conflicts(&self) -> Result<bool> {
let index = self.repo.index().map_err(CascadeError::Git)?;
Ok(index.has_conflicts())
}
pub fn get_conflicted_files(&self) -> Result<Vec<String>> {
let index = self.repo.index().map_err(CascadeError::Git)?;
let mut conflicts = Vec::new();
let conflict_iter = index.conflicts().map_err(CascadeError::Git)?;
for conflict in conflict_iter {
let conflict = conflict.map_err(CascadeError::Git)?;
if let Some(our) = conflict.our {
if let Ok(path) = std::str::from_utf8(&our.path) {
conflicts.push(path.to_string());
}
} else if let Some(their) = conflict.their {
if let Ok(path) = std::str::from_utf8(&their.path) {
conflicts.push(path.to_string());
}
}
}
Ok(conflicts)
}
pub fn fetch(&self) -> Result<()> {
tracing::debug!("Fetching from origin");
self.ensure_index_closed()?;
let mut remote = self
.repo
.find_remote("origin")
.map_err(|e| CascadeError::branch(format!("No remote 'origin' found: {e}")))?;
let callbacks = self.configure_remote_callbacks()?;
let mut fetch_options = git2::FetchOptions::new();
fetch_options.remote_callbacks(callbacks);
match remote.fetch::<&str>(&[], Some(&mut fetch_options), None) {
Ok(_) => {
tracing::debug!("Fetch completed successfully");
Ok(())
}
Err(e) => {
if self.should_retry_with_default_credentials(&e) {
tracing::debug!(
"Authentication error detected (class: {:?}, code: {:?}): {}, retrying with DefaultCredentials",
e.class(), e.code(), e
);
let callbacks = self.configure_remote_callbacks_with_fallback(true)?;
let mut fetch_options = git2::FetchOptions::new();
fetch_options.remote_callbacks(callbacks);
match remote.fetch::<&str>(&[], Some(&mut fetch_options), None) {
Ok(_) => {
tracing::debug!("Fetch succeeded with DefaultCredentials");
return Ok(());
}
Err(retry_error) => {
tracing::debug!(
"DefaultCredentials retry failed: {}, falling back to git CLI",
retry_error
);
return self.fetch_with_git_cli();
}
}
}
if self.should_fallback_to_git_cli(&e) {
tracing::debug!(
"Network/SSL error detected (class: {:?}, code: {:?}): {}, falling back to git CLI for fetch operation",
e.class(), e.code(), e
);
return self.fetch_with_git_cli();
}
Err(CascadeError::Git(e))
}
}
}
pub fn fetch_with_retry(&self) -> Result<()> {
const MAX_RETRIES: u32 = 3;
const BASE_DELAY_MS: u64 = 500;
let mut last_error = None;
for attempt in 0..MAX_RETRIES {
match self.fetch() {
Ok(_) => return Ok(()),
Err(e) => {
last_error = Some(e);
if attempt < MAX_RETRIES - 1 {
let delay_ms = BASE_DELAY_MS * 2_u64.pow(attempt);
debug!(
"Fetch attempt {} failed, retrying in {}ms...",
attempt + 1,
delay_ms
);
std::thread::sleep(std::time::Duration::from_millis(delay_ms));
}
}
}
}
Err(CascadeError::Git(git2::Error::from_str(&format!(
"Critical: Failed to fetch remote refs after {} attempts. Cannot safely proceed with force push - \
stale remote refs could cause data loss. Error: {}. Please check network connection.",
MAX_RETRIES,
last_error.unwrap()
))))
}
pub fn update_local_branch_from_remote(&self, branch: &str) -> Result<()> {
tracing::debug!(
"Updating local branch '{}' from remote (worktree-safe)",
branch
);
let mut last_error = None;
for attempt in 0..3u32 {
match self.fetch() {
Ok(_) => {
last_error = None;
break;
}
Err(e) => {
let is_locked = e.to_string().contains("Locked")
|| e.to_string().contains("index is locked");
last_error = Some(e);
if is_locked && attempt < 2 {
let delay = std::time::Duration::from_millis(500 * 2_u64.pow(attempt));
tracing::debug!(
"Index locked on fetch attempt {}, retrying in {:?}...",
attempt + 1,
delay
);
std::thread::sleep(delay);
} else {
break;
}
}
}
}
if let Some(e) = last_error {
return Err(e);
}
let remote_ref = format!("refs/remotes/origin/{branch}");
let remote_oid = self.repo.refname_to_id(&remote_ref).map_err(|e| {
CascadeError::branch(format!("Remote branch 'origin/{branch}' not found: {e}"))
})?;
let local_ref = format!("refs/heads/{branch}");
let local_oid = self.repo.refname_to_id(&local_ref).ok();
if let Some(local_oid) = local_oid {
if local_oid == remote_oid {
tracing::debug!("{branch} already up to date");
return Ok(());
}
let merge_base = self
.repo
.merge_base(local_oid, remote_oid)
.map_err(CascadeError::Git)?;
if merge_base != local_oid {
return Err(CascadeError::branch(format!(
"Branch '{branch}' has diverged from 'origin/{branch}'. \
Local has commits not in remote. Try: git reset --hard origin/{branch}"
)));
}
}
self.repo
.reference(
&local_ref,
remote_oid,
true,
"sync: fast-forward from origin",
)
.map_err(|e| CascadeError::branch(format!("Failed to update '{branch}': {e}")))?;
tracing::debug!("Fast-forwarded {branch} to {remote_oid}");
Ok(())
}
pub fn pull(&self, branch: &str) -> Result<()> {
tracing::debug!("Pulling branch: {}", branch);
match self.fetch() {
Ok(_) => {}
Err(e) => {
let error_string = e.to_string();
if error_string.contains("TLS stream") || error_string.contains("SSL") {
tracing::warn!(
"git2 error detected: {}, falling back to git CLI for pull operation",
e
);
return self.pull_with_git_cli(branch);
}
return Err(e);
}
}
let remote_branch_name = format!("origin/{branch}");
let remote_oid = self
.repo
.refname_to_id(&format!("refs/remotes/{remote_branch_name}"))
.map_err(|e| {
CascadeError::branch(format!("Remote branch {remote_branch_name} not found: {e}"))
})?;
let remote_commit = self
.repo
.find_commit(remote_oid)
.map_err(CascadeError::Git)?;
let local_refname = format!("refs/heads/{branch}");
let local_oid = self
.repo
.refname_to_id(&local_refname)
.map_err(|e| CascadeError::branch(format!("Local branch {branch} not found: {e}")))?;
let head_commit = self
.repo
.find_commit(local_oid)
.map_err(CascadeError::Git)?;
if head_commit.id() == remote_commit.id() {
tracing::debug!("Already up to date");
return Ok(());
}
let merge_base_oid = self
.repo
.merge_base(head_commit.id(), remote_commit.id())
.map_err(CascadeError::Git)?;
if merge_base_oid == head_commit.id() {
tracing::debug!("Fast-forwarding {} to {}", branch, remote_commit.id());
self.repo
.reference(&local_refname, remote_oid, true, "pull: Fast-forward")
.map_err(CascadeError::Git)?;
let on_this_branch = self
.repo
.head()
.ok()
.and_then(|h| h.shorthand().map(|s| s.to_string()))
.as_deref()
== Some(branch);
if on_this_branch {
self.repo
.set_head(&local_refname)
.map_err(CascadeError::Git)?;
self.repo
.checkout_head(Some(
git2::build::CheckoutBuilder::new()
.force()
.remove_untracked(false),
))
.map_err(CascadeError::Git)?;
}
tracing::debug!("Fast-forwarded to {}", remote_commit.id());
return Ok(());
}
Err(CascadeError::branch(format!(
"Branch '{}' has diverged from remote. Local has commits not in remote. \
Protected branches should not have local commits. \
Try: git reset --hard origin/{}",
branch, branch
)))
}
pub fn push(&self, branch: &str) -> Result<()> {
let mut remote = self
.repo
.find_remote("origin")
.map_err(|e| CascadeError::branch(format!("No remote 'origin' found: {e}")))?;
let remote_url = remote.url().unwrap_or("unknown").to_string();
tracing::debug!("Remote URL: {}", remote_url);
let refspec = format!("refs/heads/{branch}:refs/heads/{branch}");
tracing::debug!("Push refspec: {}", refspec);
let mut callbacks = self.configure_remote_callbacks()?;
callbacks.push_update_reference(|refname, status| {
if let Some(msg) = status {
tracing::debug!("Push failed for ref {}: {}", refname, msg);
return Err(git2::Error::from_str(&format!("Push failed: {msg}")));
}
tracing::debug!("Push succeeded for ref: {}", refname);
Ok(())
});
let mut push_options = git2::PushOptions::new();
push_options.remote_callbacks(callbacks);
match remote.push(&[&refspec], Some(&mut push_options)) {
Ok(_) => {
tracing::debug!("Push completed successfully for branch: {}", branch);
Ok(())
}
Err(e) => {
tracing::debug!(
"git2 push error: {} (class: {:?}, code: {:?})",
e,
e.class(),
e.code()
);
if self.should_retry_with_default_credentials(&e) {
tracing::debug!(
"Authentication error detected (class: {:?}, code: {:?}): {}, retrying with DefaultCredentials",
e.class(), e.code(), e
);
let callbacks = self.configure_remote_callbacks_with_fallback(true)?;
let mut push_options = git2::PushOptions::new();
push_options.remote_callbacks(callbacks);
match remote.push(&[&refspec], Some(&mut push_options)) {
Ok(_) => {
tracing::debug!("Push succeeded with DefaultCredentials");
return Ok(());
}
Err(retry_error) => {
tracing::debug!(
"DefaultCredentials retry failed: {}, falling back to git CLI",
retry_error
);
return self.push_with_git_cli(branch);
}
}
}
if self.should_fallback_to_git_cli(&e) {
tracing::debug!(
"Network/SSL error detected (class: {:?}, code: {:?}): {}, falling back to git CLI for push operation",
e.class(), e.code(), e
);
return self.push_with_git_cli(branch);
}
let error_msg = if e.to_string().contains("authentication") {
format!(
"Authentication failed for branch '{branch}'. Try: git push origin {branch}"
)
} else {
format!("Failed to push branch '{branch}': {e}")
};
Err(CascadeError::branch(error_msg))
}
}
}
fn push_with_git_cli(&self, branch: &str) -> Result<()> {
self.ensure_index_closed()?;
let output = std::process::Command::new("git")
.args(["push", "origin", branch])
.current_dir(&self.path)
.output()
.map_err(|e| CascadeError::branch(format!("Failed to execute git command: {e}")))?;
if output.status.success() {
Ok(())
} else {
let stderr = String::from_utf8_lossy(&output.stderr);
let _stdout = String::from_utf8_lossy(&output.stdout);
let error_msg = if stderr.contains("SSL_connect") || stderr.contains("SSL_ERROR") {
"Network error: Unable to connect to repository (VPN may be required)".to_string()
} else if stderr.contains("repository") && stderr.contains("not found") {
"Repository not found - check your Bitbucket configuration".to_string()
} else if stderr.contains("authentication") || stderr.contains("403") {
"Authentication failed - check your credentials".to_string()
} else {
stderr.trim().to_string()
};
Err(CascadeError::branch(error_msg))
}
}
fn fetch_with_git_cli(&self) -> Result<()> {
tracing::debug!("Using git CLI fallback for fetch operation");
self.ensure_index_closed()?;
let output = std::process::Command::new("git")
.args(["fetch", "origin"])
.current_dir(&self.path)
.output()
.map_err(|e| {
CascadeError::Git(git2::Error::from_str(&format!(
"Failed to execute git command: {e}"
)))
})?;
if output.status.success() {
tracing::debug!("Git CLI fetch succeeded");
Ok(())
} else {
let stderr = String::from_utf8_lossy(&output.stderr);
let stdout = String::from_utf8_lossy(&output.stdout);
let error_msg = format!(
"Git CLI fetch failed: {}\nStdout: {}\nStderr: {}",
output.status, stdout, stderr
);
Err(CascadeError::Git(git2::Error::from_str(&error_msg)))
}
}
fn pull_with_git_cli(&self, branch: &str) -> Result<()> {
tracing::debug!("Using git CLI fallback for pull operation: {}", branch);
self.ensure_index_closed()?;
let output = std::process::Command::new("git")
.args(["pull", "origin", branch])
.current_dir(&self.path)
.output()
.map_err(|e| {
CascadeError::Git(git2::Error::from_str(&format!(
"Failed to execute git command: {e}"
)))
})?;
if output.status.success() {
tracing::debug!("Git CLI pull succeeded for branch: {}", branch);
Ok(())
} else {
let stderr = String::from_utf8_lossy(&output.stderr);
let stdout = String::from_utf8_lossy(&output.stdout);
let error_msg = format!(
"Git CLI pull failed for branch '{}': {}\nStdout: {}\nStderr: {}",
branch, output.status, stdout, stderr
);
Err(CascadeError::Git(git2::Error::from_str(&error_msg)))
}
}
fn force_push_with_git_cli(&self, branch: &str) -> Result<()> {
tracing::debug!(
"Using git CLI fallback for force push operation: {}",
branch
);
let output = std::process::Command::new("git")
.args(["push", "--force", "origin", branch])
.current_dir(&self.path)
.output()
.map_err(|e| CascadeError::branch(format!("Failed to execute git command: {e}")))?;
if output.status.success() {
tracing::debug!("Git CLI force push succeeded for branch: {}", branch);
Ok(())
} else {
let stderr = String::from_utf8_lossy(&output.stderr);
let stdout = String::from_utf8_lossy(&output.stdout);
let error_msg = format!(
"Git CLI force push failed for branch '{}': {}\nStdout: {}\nStderr: {}",
branch, output.status, stdout, stderr
);
Err(CascadeError::branch(error_msg))
}
}
pub fn delete_branch(&self, name: &str) -> Result<()> {
self.delete_branch_with_options(name, false)
}
pub fn delete_branch_unsafe(&self, name: &str) -> Result<()> {
self.delete_branch_with_options(name, true)
}
fn delete_branch_with_options(&self, name: &str, force_unsafe: bool) -> Result<()> {
debug!("Attempting to delete branch: {}", name);
if !force_unsafe {
let safety_result = self.check_branch_deletion_safety(name)?;
if let Some(safety_info) = safety_result {
self.handle_branch_deletion_confirmation(name, &safety_info)?;
}
}
let mut branch = self
.repo
.find_branch(name, git2::BranchType::Local)
.map_err(|e| CascadeError::branch(format!("Could not find branch '{name}': {e}")))?;
branch
.delete()
.map_err(|e| CascadeError::branch(format!("Could not delete branch '{name}': {e}")))?;
debug!("Successfully deleted branch '{}'", name);
Ok(())
}
pub fn get_commits_between(&self, from: &str, to: &str) -> Result<Vec<git2::Commit<'_>>> {
let from_oid = self
.repo
.refname_to_id(&format!("refs/heads/{from}"))
.or_else(|_| Oid::from_str(from))
.map_err(|e| CascadeError::branch(format!("Invalid from reference '{from}': {e}")))?;
let to_oid = self
.repo
.refname_to_id(&format!("refs/heads/{to}"))
.or_else(|_| Oid::from_str(to))
.map_err(|e| CascadeError::branch(format!("Invalid to reference '{to}': {e}")))?;
let mut revwalk = self.repo.revwalk().map_err(CascadeError::Git)?;
revwalk.push(to_oid).map_err(CascadeError::Git)?;
revwalk.hide(from_oid).map_err(CascadeError::Git)?;
let mut commits = Vec::new();
for oid in revwalk {
let oid = oid.map_err(CascadeError::Git)?;
let commit = self.repo.find_commit(oid).map_err(CascadeError::Git)?;
commits.push(commit);
}
Ok(commits)
}
pub fn force_push_branch(&self, target_branch: &str, source_branch: &str) -> Result<()> {
self.force_push_branch_with_options(target_branch, source_branch, false)
}
pub fn force_push_branch_unsafe(&self, target_branch: &str, source_branch: &str) -> Result<()> {
self.force_push_branch_with_options(target_branch, source_branch, true)
}
fn force_push_branch_with_options(
&self,
target_branch: &str,
source_branch: &str,
force_unsafe: bool,
) -> Result<()> {
debug!(
"Force pushing {} content to {} to preserve PR history",
source_branch, target_branch
);
if !force_unsafe {
let safety_result = self.check_force_push_safety_enhanced(target_branch)?;
if let Some(backup_info) = safety_result {
self.create_backup_branch(target_branch, &backup_info.remote_commit_id)?;
Output::sub_item(format!(
"Created backup branch: {}",
backup_info.backup_branch_name
));
}
}
let source_ref = self
.repo
.find_reference(&format!("refs/heads/{source_branch}"))
.map_err(|e| {
CascadeError::config(format!("Failed to find source branch {source_branch}: {e}"))
})?;
let _source_commit = source_ref.peel_to_commit().map_err(|e| {
CascadeError::config(format!(
"Failed to get commit for source branch {source_branch}: {e}"
))
})?;
let mut remote = self
.repo
.find_remote("origin")
.map_err(|e| CascadeError::config(format!("Failed to find origin remote: {e}")))?;
let refspec = format!("+refs/heads/{source_branch}:refs/heads/{target_branch}");
let callbacks = self.configure_remote_callbacks()?;
let mut push_options = git2::PushOptions::new();
push_options.remote_callbacks(callbacks);
match remote.push(&[&refspec], Some(&mut push_options)) {
Ok(_) => {}
Err(e) => {
if self.should_retry_with_default_credentials(&e) {
tracing::debug!(
"Authentication error detected (class: {:?}, code: {:?}): {}, retrying with DefaultCredentials",
e.class(), e.code(), e
);
let callbacks = self.configure_remote_callbacks_with_fallback(true)?;
let mut push_options = git2::PushOptions::new();
push_options.remote_callbacks(callbacks);
match remote.push(&[&refspec], Some(&mut push_options)) {
Ok(_) => {
tracing::debug!("Force push succeeded with DefaultCredentials");
}
Err(retry_error) => {
tracing::debug!(
"DefaultCredentials retry failed: {}, falling back to git CLI",
retry_error
);
return self.force_push_with_git_cli(target_branch);
}
}
} else if self.should_fallback_to_git_cli(&e) {
tracing::debug!(
"Network/SSL error detected (class: {:?}, code: {:?}): {}, falling back to git CLI for force push operation",
e.class(), e.code(), e
);
return self.force_push_with_git_cli(target_branch);
} else {
return Err(CascadeError::config(format!(
"Failed to force push {target_branch}: {e}"
)));
}
}
}
tracing::debug!(
"Successfully force pushed {} to preserve PR history",
target_branch
);
Ok(())
}
fn check_force_push_safety_enhanced(
&self,
target_branch: &str,
) -> Result<Option<ForceBackupInfo>> {
match self.fetch() {
Ok(_) => {}
Err(e) => {
debug!("Could not fetch latest changes for safety check: {}", e);
}
}
let remote_ref = format!("refs/remotes/origin/{target_branch}");
let local_ref = format!("refs/heads/{target_branch}");
let local_commit = match self.repo.find_reference(&local_ref) {
Ok(reference) => reference.peel_to_commit().ok(),
Err(_) => None,
};
let remote_commit = match self.repo.find_reference(&remote_ref) {
Ok(reference) => reference.peel_to_commit().ok(),
Err(_) => None,
};
if let (Some(local), Some(remote)) = (local_commit, remote_commit) {
if local.id() != remote.id() {
let merge_base_oid = self
.repo
.merge_base(local.id(), remote.id())
.map_err(|e| CascadeError::config(format!("Failed to find merge base: {e}")))?;
if merge_base_oid != remote.id() {
let commits_to_lose = self.count_commits_between(
&merge_base_oid.to_string(),
&remote.id().to_string(),
)?;
let timestamp = chrono::Utc::now().format("%Y%m%d_%H%M%S");
let backup_branch_name = format!("{target_branch}_backup_{timestamp}");
debug!(
"Force push to '{}' would overwrite {} commits on remote",
target_branch, commits_to_lose
);
if std::env::var("CI").is_ok() || std::env::var("FORCE_PUSH_NO_CONFIRM").is_ok()
{
info!(
"Non-interactive environment detected, proceeding with backup creation"
);
return Ok(Some(ForceBackupInfo {
backup_branch_name,
remote_commit_id: remote.id().to_string(),
commits_that_would_be_lost: commits_to_lose,
}));
}
return Ok(Some(ForceBackupInfo {
backup_branch_name,
remote_commit_id: remote.id().to_string(),
commits_that_would_be_lost: commits_to_lose,
}));
}
}
}
Ok(None)
}
fn check_force_push_safety_auto_no_fetch(
&self,
target_branch: &str,
) -> Result<Option<ForceBackupInfo>> {
let remote_ref = format!("refs/remotes/origin/{target_branch}");
let local_ref = format!("refs/heads/{target_branch}");
let local_commit = match self.repo.find_reference(&local_ref) {
Ok(reference) => reference.peel_to_commit().ok(),
Err(_) => None,
};
let remote_commit = match self.repo.find_reference(&remote_ref) {
Ok(reference) => reference.peel_to_commit().ok(),
Err(_) => None,
};
if let (Some(local), Some(remote)) = (local_commit, remote_commit) {
if local.id() != remote.id() {
let merge_base_oid = self
.repo
.merge_base(local.id(), remote.id())
.map_err(|e| CascadeError::config(format!("Failed to find merge base: {e}")))?;
if merge_base_oid != remote.id() {
let commits_to_lose = self.count_commits_between(
&merge_base_oid.to_string(),
&remote.id().to_string(),
)?;
let timestamp = chrono::Utc::now().format("%Y%m%d_%H%M%S");
let backup_branch_name = format!("{target_branch}_backup_{timestamp}");
tracing::debug!(
"Auto-creating backup '{}' for force push to '{}' (would overwrite {} commits)",
backup_branch_name, target_branch, commits_to_lose
);
return Ok(Some(ForceBackupInfo {
backup_branch_name,
remote_commit_id: remote.id().to_string(),
commits_that_would_be_lost: commits_to_lose,
}));
}
}
}
Ok(None)
}
fn create_backup_branch(&self, original_branch: &str, remote_commit_id: &str) -> Result<()> {
let timestamp = chrono::Utc::now().format("%Y%m%d_%H%M%S");
let backup_branch_name = format!("{original_branch}_backup_{timestamp}");
let commit_oid = Oid::from_str(remote_commit_id).map_err(|e| {
CascadeError::config(format!("Invalid commit ID {remote_commit_id}: {e}"))
})?;
let commit = self.repo.find_commit(commit_oid).map_err(|e| {
CascadeError::config(format!("Failed to find commit {remote_commit_id}: {e}"))
})?;
self.repo
.branch(&backup_branch_name, &commit, false)
.map_err(|e| {
CascadeError::config(format!(
"Failed to create backup branch {backup_branch_name}: {e}"
))
})?;
debug!(
"Created backup branch '{}' pointing to {}",
backup_branch_name,
&remote_commit_id[..8]
);
Ok(())
}
fn check_branch_deletion_safety(
&self,
branch_name: &str,
) -> Result<Option<BranchDeletionSafety>> {
match self.fetch() {
Ok(_) => {}
Err(e) => {
warn!(
"Could not fetch latest changes for branch deletion safety check: {}",
e
);
}
}
let branch = self
.repo
.find_branch(branch_name, git2::BranchType::Local)
.map_err(|e| {
CascadeError::branch(format!("Could not find branch '{branch_name}': {e}"))
})?;
let _branch_commit = branch.get().peel_to_commit().map_err(|e| {
CascadeError::branch(format!(
"Could not get commit for branch '{branch_name}': {e}"
))
})?;
let main_branch_name = self.detect_main_branch()?;
let is_merged_to_main = self.is_branch_merged_to_main(branch_name, &main_branch_name)?;
let remote_tracking_branch = self.get_remote_tracking_branch(branch_name);
let mut unpushed_commits = Vec::new();
if let Some(ref remote_branch) = remote_tracking_branch {
match self.get_commits_between(remote_branch, branch_name) {
Ok(commits) => {
unpushed_commits = commits.iter().map(|c| c.id().to_string()).collect();
}
Err(_) => {
if !is_merged_to_main {
if let Ok(commits) =
self.get_commits_between(&main_branch_name, branch_name)
{
unpushed_commits = commits.iter().map(|c| c.id().to_string()).collect();
}
}
}
}
} else if !is_merged_to_main {
if let Ok(commits) = self.get_commits_between(&main_branch_name, branch_name) {
unpushed_commits = commits.iter().map(|c| c.id().to_string()).collect();
}
}
if !unpushed_commits.is_empty() || (!is_merged_to_main && remote_tracking_branch.is_none())
{
Ok(Some(BranchDeletionSafety {
unpushed_commits,
remote_tracking_branch,
is_merged_to_main,
main_branch_name,
}))
} else {
Ok(None)
}
}
fn handle_branch_deletion_confirmation(
&self,
branch_name: &str,
safety_info: &BranchDeletionSafety,
) -> Result<()> {
if std::env::var("CI").is_ok() || std::env::var("BRANCH_DELETE_NO_CONFIRM").is_ok() {
return Err(CascadeError::branch(
format!(
"Branch '{branch_name}' has {} unpushed commits and cannot be deleted in non-interactive mode. Use --force to override.",
safety_info.unpushed_commits.len()
)
));
}
println!();
Output::warning("BRANCH DELETION WARNING");
println!("Branch '{branch_name}' has potential issues:");
if !safety_info.unpushed_commits.is_empty() {
println!(
"\n🔍 Unpushed commits ({} total):",
safety_info.unpushed_commits.len()
);
for (i, commit_id) in safety_info.unpushed_commits.iter().take(5).enumerate() {
if let Ok(oid) = Oid::from_str(commit_id) {
if let Ok(commit) = self.repo.find_commit(oid) {
let short_hash = &commit_id[..8];
let summary = commit.summary().unwrap_or("<no message>");
println!(" {}. {} - {}", i + 1, short_hash, summary);
}
}
}
if safety_info.unpushed_commits.len() > 5 {
println!(
" ... and {} more commits",
safety_info.unpushed_commits.len() - 5
);
}
}
if !safety_info.is_merged_to_main {
println!();
crate::cli::output::Output::section("Branch status");
crate::cli::output::Output::bullet(format!(
"Not merged to '{}'",
safety_info.main_branch_name
));
if let Some(ref remote) = safety_info.remote_tracking_branch {
crate::cli::output::Output::bullet(format!("Remote tracking branch: {remote}"));
} else {
crate::cli::output::Output::bullet("No remote tracking branch");
}
}
println!();
crate::cli::output::Output::section("Safer alternatives");
if !safety_info.unpushed_commits.is_empty() {
if let Some(ref _remote) = safety_info.remote_tracking_branch {
println!(" • Push commits first: git push origin {branch_name}");
} else {
println!(" • Create and push to remote: git push -u origin {branch_name}");
}
}
if !safety_info.is_merged_to_main {
println!(
" • Merge to {} first: git checkout {} && git merge {branch_name}",
safety_info.main_branch_name, safety_info.main_branch_name
);
}
let confirmed = Confirm::with_theme(&ColorfulTheme::default())
.with_prompt("Do you want to proceed with deleting this branch?")
.default(false)
.interact()
.map_err(|e| CascadeError::branch(format!("Failed to get user confirmation: {e}")))?;
if !confirmed {
return Err(CascadeError::branch(
"Branch deletion cancelled by user. Use --force to bypass this check.".to_string(),
));
}
Ok(())
}
pub fn detect_main_branch(&self) -> Result<String> {
let main_candidates = ["main", "master", "develop", "trunk"];
for candidate in &main_candidates {
if self
.repo
.find_branch(candidate, git2::BranchType::Local)
.is_ok()
{
return Ok(candidate.to_string());
}
}
if let Ok(head) = self.repo.head() {
if let Some(name) = head.shorthand() {
return Ok(name.to_string());
}
}
Ok("main".to_string())
}
fn is_branch_merged_to_main(&self, branch_name: &str, main_branch: &str) -> Result<bool> {
match self.get_commits_between(main_branch, branch_name) {
Ok(commits) => Ok(commits.is_empty()),
Err(_) => {
Ok(false)
}
}
}
fn get_remote_tracking_branch(&self, branch_name: &str) -> Option<String> {
let remote_candidates = [
format!("origin/{branch_name}"),
format!("remotes/origin/{branch_name}"),
];
for candidate in &remote_candidates {
if self
.repo
.find_reference(&format!(
"refs/remotes/{}",
candidate.replace("remotes/", "")
))
.is_ok()
{
return Some(candidate.clone());
}
}
None
}
fn check_checkout_safety(&self, _target: &str) -> Result<Option<CheckoutSafety>> {
let is_dirty = self.is_dirty()?;
if !is_dirty {
return Ok(None);
}
let current_branch = self.get_current_branch().ok();
let modified_files = self.get_modified_files()?;
let staged_files = self.get_staged_files()?;
let untracked_files = self.get_untracked_files()?;
let has_uncommitted_changes = !modified_files.is_empty() || !staged_files.is_empty();
if has_uncommitted_changes || !untracked_files.is_empty() {
return Ok(Some(CheckoutSafety {
has_uncommitted_changes,
modified_files,
staged_files,
untracked_files,
stash_created: None,
current_branch,
}));
}
Ok(None)
}
fn handle_checkout_confirmation(
&self,
target: &str,
safety_info: &CheckoutSafety,
) -> Result<()> {
let is_ci = std::env::var("CI").is_ok();
let no_confirm = std::env::var("CHECKOUT_NO_CONFIRM").is_ok();
let is_non_interactive = is_ci || no_confirm;
if is_non_interactive {
return Err(CascadeError::branch(
format!(
"Cannot checkout '{target}' with uncommitted changes in non-interactive mode. Commit your changes or use stash first."
)
));
}
println!("\nCHECKOUT WARNING");
println!("Attempting to checkout: {}", target);
println!("You have uncommitted changes that could be lost:");
if !safety_info.modified_files.is_empty() {
println!("\nModified files ({}):", safety_info.modified_files.len());
for file in safety_info.modified_files.iter().take(10) {
println!(" - {file}");
}
if safety_info.modified_files.len() > 10 {
println!(" ... and {} more", safety_info.modified_files.len() - 10);
}
}
if !safety_info.staged_files.is_empty() {
println!("\nStaged files ({}):", safety_info.staged_files.len());
for file in safety_info.staged_files.iter().take(10) {
println!(" - {file}");
}
if safety_info.staged_files.len() > 10 {
println!(" ... and {} more", safety_info.staged_files.len() - 10);
}
}
if !safety_info.untracked_files.is_empty() {
println!("\nUntracked files ({}):", safety_info.untracked_files.len());
for file in safety_info.untracked_files.iter().take(5) {
println!(" - {file}");
}
if safety_info.untracked_files.len() > 5 {
println!(" ... and {} more", safety_info.untracked_files.len() - 5);
}
}
println!("\nOptions:");
println!("1. Stash changes and checkout (recommended)");
println!("2. Force checkout (WILL LOSE UNCOMMITTED CHANGES)");
println!("3. Cancel checkout");
let selection = Select::with_theme(&ColorfulTheme::default())
.with_prompt("Choose an action")
.items(&[
"Stash changes and checkout (recommended)",
"Force checkout (WILL LOSE UNCOMMITTED CHANGES)",
"Cancel checkout",
])
.default(0)
.interact()
.map_err(|e| CascadeError::branch(format!("Could not get user selection: {e}")))?;
match selection {
0 => {
let stash_message = format!(
"Auto-stash before checkout to {} at {}",
target,
chrono::Utc::now().format("%Y-%m-%d %H:%M:%S UTC")
);
match self.create_stash(&stash_message) {
Ok(stash_id) => {
crate::cli::output::Output::success(format!(
"Created stash: {stash_message} ({stash_id})"
));
crate::cli::output::Output::tip("You can restore with: git stash pop");
}
Err(e) => {
crate::cli::output::Output::error(format!("Failed to create stash: {e}"));
use dialoguer::Select;
let stash_failed_options = vec![
"Commit staged changes and proceed",
"Force checkout (WILL LOSE CHANGES)",
"Cancel and handle manually",
];
let stash_selection = Select::with_theme(&ColorfulTheme::default())
.with_prompt("Stash failed. What would you like to do?")
.items(&stash_failed_options)
.default(0)
.interact()
.map_err(|e| {
CascadeError::branch(format!("Could not get user selection: {e}"))
})?;
match stash_selection {
0 => {
let staged_files = self.get_staged_files()?;
if !staged_files.is_empty() {
println!(
"📝 Committing {} staged files...",
staged_files.len()
);
match self
.commit_staged_changes("WIP: Auto-commit before checkout")
{
Ok(Some(commit_hash)) => {
crate::cli::output::Output::success(format!(
"Committed staged changes as {}",
&commit_hash[..8]
));
crate::cli::output::Output::tip(
"You can undo with: git reset HEAD~1",
);
}
Ok(None) => {
crate::cli::output::Output::info(
"No staged changes found to commit",
);
}
Err(commit_err) => {
println!(
"❌ Failed to commit staged changes: {commit_err}"
);
return Err(CascadeError::branch(
"Could not commit staged changes".to_string(),
));
}
}
} else {
println!("No staged changes to commit");
}
}
1 => {
Output::warning("Proceeding with force checkout - uncommitted changes will be lost!");
}
2 => {
return Err(CascadeError::branch(
"Checkout cancelled. Please handle changes manually and try again.".to_string(),
));
}
_ => unreachable!(),
}
}
}
}
1 => {
Output::warning(
"Proceeding with force checkout - uncommitted changes will be lost!",
);
}
2 => {
return Err(CascadeError::branch(
"Checkout cancelled by user".to_string(),
));
}
_ => unreachable!(),
}
Ok(())
}
fn create_stash(&self, message: &str) -> Result<String> {
use crate::cli::output::Output;
tracing::debug!("Creating stash: {}", message);
let output = std::process::Command::new("git")
.args(["stash", "push", "-m", message])
.current_dir(&self.path)
.output()
.map_err(|e| {
CascadeError::branch(format!("Failed to execute git stash command: {e}"))
})?;
if output.status.success() {
let stdout = String::from_utf8_lossy(&output.stdout);
let stash_id = if stdout.contains("Saved working directory") {
let stash_list_output = std::process::Command::new("git")
.args(["stash", "list", "-n", "1", "--format=%H"])
.current_dir(&self.path)
.output()
.map_err(|e| CascadeError::branch(format!("Failed to get stash ID: {e}")))?;
if stash_list_output.status.success() {
String::from_utf8_lossy(&stash_list_output.stdout)
.trim()
.to_string()
} else {
"stash@{0}".to_string() }
} else {
"stash@{0}".to_string() };
Output::success(format!("Created stash: {} ({})", message, stash_id));
Output::tip("You can restore with: git stash pop");
Ok(stash_id)
} else {
let stderr = String::from_utf8_lossy(&output.stderr);
let stdout = String::from_utf8_lossy(&output.stdout);
if stderr.contains("No local changes to save")
|| stdout.contains("No local changes to save")
{
return Err(CascadeError::branch("No local changes to save".to_string()));
}
Err(CascadeError::branch(format!(
"Failed to create stash: {}\nStderr: {}\nStdout: {}",
output.status, stderr, stdout
)))
}
}
fn get_modified_files(&self) -> Result<Vec<String>> {
let mut opts = git2::StatusOptions::new();
opts.include_untracked(false).include_ignored(false);
let statuses = self
.repo
.statuses(Some(&mut opts))
.map_err(|e| CascadeError::branch(format!("Could not get repository status: {e}")))?;
let mut modified_files = Vec::new();
for status in statuses.iter() {
let flags = status.status();
if flags.contains(git2::Status::WT_MODIFIED) || flags.contains(git2::Status::WT_DELETED)
{
if let Some(path) = status.path() {
modified_files.push(path.to_string());
}
}
}
Ok(modified_files)
}
pub fn get_staged_files(&self) -> Result<Vec<String>> {
let mut opts = git2::StatusOptions::new();
opts.include_untracked(false).include_ignored(false);
let statuses = self
.repo
.statuses(Some(&mut opts))
.map_err(|e| CascadeError::branch(format!("Could not get repository status: {e}")))?;
let mut staged_files = Vec::new();
for status in statuses.iter() {
let flags = status.status();
if flags.contains(git2::Status::INDEX_MODIFIED)
|| flags.contains(git2::Status::INDEX_NEW)
|| flags.contains(git2::Status::INDEX_DELETED)
{
if let Some(path) = status.path() {
staged_files.push(path.to_string());
}
}
}
Ok(staged_files)
}
fn count_commits_between(&self, from: &str, to: &str) -> Result<usize> {
let commits = self.get_commits_between(from, to)?;
Ok(commits.len())
}
pub fn resolve_reference(&self, reference: &str) -> Result<git2::Commit<'_>> {
if let Ok(oid) = Oid::from_str(reference) {
if let Ok(commit) = self.repo.find_commit(oid) {
return Ok(commit);
}
}
let obj = self.repo.revparse_single(reference).map_err(|e| {
CascadeError::branch(format!("Could not resolve reference '{reference}': {e}"))
})?;
obj.peel_to_commit().map_err(|e| {
CascadeError::branch(format!(
"Reference '{reference}' does not point to a commit: {e}"
))
})
}
pub fn reset_soft(&self, target_ref: &str) -> Result<()> {
let target_commit = self.resolve_reference(target_ref)?;
self.repo
.reset(target_commit.as_object(), git2::ResetType::Soft, None)
.map_err(CascadeError::Git)?;
Ok(())
}
pub fn reset_to_head(&self) -> Result<()> {
tracing::debug!("Resetting working directory and index to HEAD");
let repo_path = self.path();
crate::utils::git_lock::with_lock_retry(repo_path, || {
let head = self.repo.head()?;
let head_commit = head.peel_to_commit()?;
let mut checkout_builder = git2::build::CheckoutBuilder::new();
checkout_builder.force(); checkout_builder.remove_untracked(false);
self.repo.reset(
head_commit.as_object(),
git2::ResetType::Hard,
Some(&mut checkout_builder),
)?;
Ok(())
})?;
tracing::debug!("Successfully reset working directory to HEAD");
Ok(())
}
pub fn find_branch_containing_commit(&self, commit_hash: &str) -> Result<String> {
let oid = Oid::from_str(commit_hash).map_err(|e| {
CascadeError::branch(format!("Invalid commit hash '{commit_hash}': {e}"))
})?;
let branches = self
.repo
.branches(Some(git2::BranchType::Local))
.map_err(CascadeError::Git)?;
for branch_result in branches {
let (branch, _) = branch_result.map_err(CascadeError::Git)?;
if let Some(branch_name) = branch.name().map_err(CascadeError::Git)? {
if let Ok(branch_head) = branch.get().peel_to_commit() {
let mut revwalk = self.repo.revwalk().map_err(CascadeError::Git)?;
revwalk.push(branch_head.id()).map_err(CascadeError::Git)?;
for commit_oid in revwalk {
let commit_oid = commit_oid.map_err(CascadeError::Git)?;
if commit_oid == oid {
return Ok(branch_name.to_string());
}
}
}
}
}
Err(CascadeError::branch(format!(
"Commit {commit_hash} not found in any local branch"
)))
}
pub async fn fetch_async(&self) -> Result<()> {
let repo_path = self.path.clone();
crate::utils::async_ops::run_git_operation(move || {
let repo = GitRepository::open(&repo_path)?;
repo.fetch()
})
.await
}
pub async fn pull_async(&self, branch: &str) -> Result<()> {
let repo_path = self.path.clone();
let branch_name = branch.to_string();
crate::utils::async_ops::run_git_operation(move || {
let repo = GitRepository::open(&repo_path)?;
repo.pull(&branch_name)
})
.await
}
pub async fn push_branch_async(&self, branch_name: &str) -> Result<()> {
let repo_path = self.path.clone();
let branch = branch_name.to_string();
crate::utils::async_ops::run_git_operation(move || {
let repo = GitRepository::open(&repo_path)?;
repo.push(&branch)
})
.await
}
pub async fn cherry_pick_commit_async(&self, commit_hash: &str) -> Result<String> {
let repo_path = self.path.clone();
let hash = commit_hash.to_string();
crate::utils::async_ops::run_git_operation(move || {
let repo = GitRepository::open(&repo_path)?;
repo.cherry_pick(&hash)
})
.await
}
pub async fn get_commit_hashes_between_async(
&self,
from: &str,
to: &str,
) -> Result<Vec<String>> {
let repo_path = self.path.clone();
let from_str = from.to_string();
let to_str = to.to_string();
crate::utils::async_ops::run_git_operation(move || {
let repo = GitRepository::open(&repo_path)?;
let commits = repo.get_commits_between(&from_str, &to_str)?;
Ok(commits.into_iter().map(|c| c.id().to_string()).collect())
})
.await
}
pub fn reset_branch_to_commit(&self, branch_name: &str, commit_hash: &str) -> Result<()> {
info!(
"Resetting branch '{}' to commit {}",
branch_name,
&commit_hash[..8]
);
let target_oid = git2::Oid::from_str(commit_hash).map_err(|e| {
CascadeError::branch(format!("Invalid commit hash '{commit_hash}': {e}"))
})?;
let _target_commit = self.repo.find_commit(target_oid).map_err(|e| {
CascadeError::branch(format!("Could not find commit '{commit_hash}': {e}"))
})?;
let _branch = self
.repo
.find_branch(branch_name, git2::BranchType::Local)
.map_err(|e| {
CascadeError::branch(format!("Could not find branch '{branch_name}': {e}"))
})?;
let branch_ref_name = format!("refs/heads/{branch_name}");
self.repo
.reference(
&branch_ref_name,
target_oid,
true,
&format!("Reset {branch_name} to {commit_hash}"),
)
.map_err(|e| {
CascadeError::branch(format!(
"Could not reset branch '{branch_name}' to commit '{commit_hash}': {e}"
))
})?;
tracing::info!(
"Successfully reset branch '{}' to commit {}",
branch_name,
&commit_hash[..8]
);
Ok(())
}
pub fn detect_parent_branch(&self) -> Result<Option<String>> {
let current_branch = self.get_current_branch()?;
if let Ok(Some(upstream)) = self.get_upstream_branch(¤t_branch) {
if let Some(branch_name) = upstream.split('/').nth(1) {
if self.branch_exists(branch_name) {
tracing::debug!(
"Detected parent branch '{}' from upstream tracking",
branch_name
);
return Ok(Some(branch_name.to_string()));
}
}
}
if let Ok(default_branch) = self.detect_main_branch() {
if current_branch != default_branch {
tracing::debug!(
"Detected parent branch '{}' as repository default",
default_branch
);
return Ok(Some(default_branch));
}
}
if let Ok(branches) = self.list_branches() {
let current_commit = self.get_head_commit()?;
let current_commit_hash = current_commit.id().to_string();
let current_oid = current_commit.id();
let mut best_candidate = None;
let mut best_distance = usize::MAX;
for branch in branches {
if branch == current_branch
|| branch.contains("-v")
|| branch.ends_with("-v2")
|| branch.ends_with("-v3")
{
continue;
}
if let Ok(base_commit_hash) = self.get_branch_commit_hash(&branch) {
if let Ok(base_oid) = git2::Oid::from_str(&base_commit_hash) {
if let Ok(merge_base_oid) = self.repo.merge_base(current_oid, base_oid) {
if let Ok(distance) = self.count_commits_between(
&merge_base_oid.to_string(),
¤t_commit_hash,
) {
let is_likely_base = self.is_likely_base_branch(&branch);
let adjusted_distance = if is_likely_base {
distance
} else {
distance + 1000
};
if adjusted_distance < best_distance {
best_distance = adjusted_distance;
best_candidate = Some(branch.clone());
}
}
}
}
}
}
if let Some(ref candidate) = best_candidate {
tracing::debug!(
"Detected parent branch '{}' with distance {}",
candidate,
best_distance
);
}
return Ok(best_candidate);
}
tracing::debug!("Could not detect parent branch for '{}'", current_branch);
Ok(None)
}
fn is_likely_base_branch(&self, branch_name: &str) -> bool {
let base_patterns = [
"main",
"master",
"develop",
"dev",
"development",
"staging",
"stage",
"release",
"production",
"prod",
];
base_patterns.contains(&branch_name)
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::process::Command;
use tempfile::TempDir;
fn create_test_repo() -> (TempDir, PathBuf) {
let temp_dir = TempDir::new().unwrap();
let repo_path = temp_dir.path().to_path_buf();
Command::new("git")
.args(["init"])
.current_dir(&repo_path)
.output()
.unwrap();
Command::new("git")
.args(["config", "user.name", "Test"])
.current_dir(&repo_path)
.output()
.unwrap();
Command::new("git")
.args(["config", "user.email", "test@test.com"])
.current_dir(&repo_path)
.output()
.unwrap();
std::fs::write(repo_path.join("README.md"), "# Test").unwrap();
Command::new("git")
.args(["add", "."])
.current_dir(&repo_path)
.output()
.unwrap();
Command::new("git")
.args(["commit", "-m", "Initial commit"])
.current_dir(&repo_path)
.output()
.unwrap();
(temp_dir, repo_path)
}
fn create_commit(repo_path: &PathBuf, message: &str, filename: &str) {
let file_path = repo_path.join(filename);
std::fs::write(&file_path, format!("Content for {filename}\n")).unwrap();
Command::new("git")
.args(["add", filename])
.current_dir(repo_path)
.output()
.unwrap();
Command::new("git")
.args(["commit", "-m", message])
.current_dir(repo_path)
.output()
.unwrap();
}
#[test]
fn test_repository_info() {
let (_temp_dir, repo_path) = create_test_repo();
let repo = GitRepository::open(&repo_path).unwrap();
let info = repo.get_info().unwrap();
assert!(!info.is_dirty); assert!(
info.head_branch == Some("master".to_string())
|| info.head_branch == Some("main".to_string()),
"Expected default branch to be 'master' or 'main', got {:?}",
info.head_branch
);
assert!(info.head_commit.is_some()); assert!(info.untracked_files.is_empty()); }
#[test]
fn test_force_push_branch_basic() {
let (_temp_dir, repo_path) = create_test_repo();
let repo = GitRepository::open(&repo_path).unwrap();
let default_branch = repo.get_current_branch().unwrap();
create_commit(&repo_path, "Feature commit 1", "feature1.rs");
Command::new("git")
.args(["checkout", "-b", "source-branch"])
.current_dir(&repo_path)
.output()
.unwrap();
create_commit(&repo_path, "Feature commit 2", "feature2.rs");
Command::new("git")
.args(["checkout", &default_branch])
.current_dir(&repo_path)
.output()
.unwrap();
Command::new("git")
.args(["checkout", "-b", "target-branch"])
.current_dir(&repo_path)
.output()
.unwrap();
create_commit(&repo_path, "Target commit", "target.rs");
let result = repo.force_push_branch("target-branch", "source-branch");
assert!(result.is_ok() || result.is_err()); }
#[test]
fn test_force_push_branch_nonexistent_branches() {
let (_temp_dir, repo_path) = create_test_repo();
let repo = GitRepository::open(&repo_path).unwrap();
let default_branch = repo.get_current_branch().unwrap();
let result = repo.force_push_branch("target", "nonexistent-source");
assert!(result.is_err());
let result = repo.force_push_branch("nonexistent-target", &default_branch);
assert!(result.is_err());
}
#[test]
fn test_force_push_workflow_simulation() {
let (_temp_dir, repo_path) = create_test_repo();
let repo = GitRepository::open(&repo_path).unwrap();
Command::new("git")
.args(["checkout", "-b", "feature-auth"])
.current_dir(&repo_path)
.output()
.unwrap();
create_commit(&repo_path, "Add authentication", "auth.rs");
Command::new("git")
.args(["checkout", "-b", "feature-auth-v2"])
.current_dir(&repo_path)
.output()
.unwrap();
create_commit(&repo_path, "Fix auth validation", "auth.rs");
let result = repo.force_push_branch("feature-auth", "feature-auth-v2");
match result {
Ok(_) => {
Command::new("git")
.args(["checkout", "feature-auth"])
.current_dir(&repo_path)
.output()
.unwrap();
let log_output = Command::new("git")
.args(["log", "--oneline", "-2"])
.current_dir(&repo_path)
.output()
.unwrap();
let log_str = String::from_utf8_lossy(&log_output.stdout);
assert!(
log_str.contains("Fix auth validation")
|| log_str.contains("Add authentication")
);
}
Err(_) => {
}
}
}
#[test]
fn test_branch_operations() {
let (_temp_dir, repo_path) = create_test_repo();
let repo = GitRepository::open(&repo_path).unwrap();
let current = repo.get_current_branch().unwrap();
assert!(
current == "master" || current == "main",
"Expected default branch to be 'master' or 'main', got '{current}'"
);
Command::new("git")
.args(["checkout", "-b", "test-branch"])
.current_dir(&repo_path)
.output()
.unwrap();
let current = repo.get_current_branch().unwrap();
assert_eq!(current, "test-branch");
}
#[test]
fn test_commit_operations() {
let (_temp_dir, repo_path) = create_test_repo();
let repo = GitRepository::open(&repo_path).unwrap();
let head = repo.get_head_commit().unwrap();
assert_eq!(head.message().unwrap().trim(), "Initial commit");
let hash = head.id().to_string();
let same_commit = repo.get_commit(&hash).unwrap();
assert_eq!(head.id(), same_commit.id());
}
#[test]
fn test_checkout_safety_clean_repo() {
let (_temp_dir, repo_path) = create_test_repo();
let repo = GitRepository::open(&repo_path).unwrap();
create_commit(&repo_path, "Second commit", "test.txt");
Command::new("git")
.args(["checkout", "-b", "test-branch"])
.current_dir(&repo_path)
.output()
.unwrap();
let safety_result = repo.check_checkout_safety("main");
assert!(safety_result.is_ok());
assert!(safety_result.unwrap().is_none()); }
#[test]
fn test_checkout_safety_with_modified_files() {
let (_temp_dir, repo_path) = create_test_repo();
let repo = GitRepository::open(&repo_path).unwrap();
Command::new("git")
.args(["checkout", "-b", "test-branch"])
.current_dir(&repo_path)
.output()
.unwrap();
std::fs::write(repo_path.join("README.md"), "Modified content").unwrap();
let safety_result = repo.check_checkout_safety("main");
assert!(safety_result.is_ok());
let safety_info = safety_result.unwrap();
assert!(safety_info.is_some());
let info = safety_info.unwrap();
assert!(!info.modified_files.is_empty());
assert!(info.modified_files.contains(&"README.md".to_string()));
}
#[test]
fn test_unsafe_checkout_methods() {
let (_temp_dir, repo_path) = create_test_repo();
let repo = GitRepository::open(&repo_path).unwrap();
create_commit(&repo_path, "Second commit", "test.txt");
Command::new("git")
.args(["checkout", "-b", "test-branch"])
.current_dir(&repo_path)
.output()
.unwrap();
std::fs::write(repo_path.join("README.md"), "Modified content").unwrap();
let _result = repo.checkout_branch_unsafe("main");
let head_commit = repo.get_head_commit().unwrap();
let commit_hash = head_commit.id().to_string();
let _result = repo.checkout_commit_unsafe(&commit_hash);
}
#[test]
fn test_get_modified_files() {
let (_temp_dir, repo_path) = create_test_repo();
let repo = GitRepository::open(&repo_path).unwrap();
let modified = repo.get_modified_files().unwrap();
assert!(modified.is_empty());
std::fs::write(repo_path.join("README.md"), "Modified content").unwrap();
let modified = repo.get_modified_files().unwrap();
assert_eq!(modified.len(), 1);
assert!(modified.contains(&"README.md".to_string()));
}
#[test]
fn test_get_staged_files() {
let (_temp_dir, repo_path) = create_test_repo();
let repo = GitRepository::open(&repo_path).unwrap();
let staged = repo.get_staged_files().unwrap();
assert!(staged.is_empty());
std::fs::write(repo_path.join("staged.txt"), "Staged content").unwrap();
Command::new("git")
.args(["add", "staged.txt"])
.current_dir(&repo_path)
.output()
.unwrap();
let staged = repo.get_staged_files().unwrap();
assert_eq!(staged.len(), 1);
assert!(staged.contains(&"staged.txt".to_string()));
}
#[test]
fn test_create_stash_fallback() {
let (_temp_dir, repo_path) = create_test_repo();
let repo = GitRepository::open(&repo_path).unwrap();
let result = repo.create_stash("test stash");
match result {
Ok(stash_id) => {
assert!(!stash_id.is_empty());
assert!(stash_id.contains("stash") || stash_id.len() >= 7); }
Err(error) => {
let error_msg = error.to_string();
assert!(
error_msg.contains("No local changes to save")
|| error_msg.contains("git stash push")
);
}
}
}
#[test]
fn test_delete_branch_unsafe() {
let (_temp_dir, repo_path) = create_test_repo();
let repo = GitRepository::open(&repo_path).unwrap();
create_commit(&repo_path, "Second commit", "test.txt");
Command::new("git")
.args(["checkout", "-b", "test-branch"])
.current_dir(&repo_path)
.output()
.unwrap();
create_commit(&repo_path, "Branch-specific commit", "branch.txt");
Command::new("git")
.args(["checkout", "main"])
.current_dir(&repo_path)
.output()
.unwrap();
let result = repo.delete_branch_unsafe("test-branch");
let _ = result; }
#[test]
fn test_force_push_unsafe() {
let (_temp_dir, repo_path) = create_test_repo();
let repo = GitRepository::open(&repo_path).unwrap();
create_commit(&repo_path, "Second commit", "test.txt");
Command::new("git")
.args(["checkout", "-b", "test-branch"])
.current_dir(&repo_path)
.output()
.unwrap();
let _result = repo.force_push_branch_unsafe("test-branch", "test-branch");
}
#[test]
fn test_cherry_pick_basic() {
let (_temp_dir, repo_path) = create_test_repo();
let repo = GitRepository::open(&repo_path).unwrap();
repo.create_branch("source", None).unwrap();
repo.checkout_branch("source").unwrap();
std::fs::write(repo_path.join("cherry.txt"), "Cherry content").unwrap();
Command::new("git")
.args(["add", "."])
.current_dir(&repo_path)
.output()
.unwrap();
Command::new("git")
.args(["commit", "-m", "Cherry commit"])
.current_dir(&repo_path)
.output()
.unwrap();
let cherry_commit = repo.get_head_commit_hash().unwrap();
Command::new("git")
.args(["checkout", "-"])
.current_dir(&repo_path)
.output()
.unwrap();
repo.create_branch("target", None).unwrap();
repo.checkout_branch("target").unwrap();
let new_commit = repo.cherry_pick(&cherry_commit).unwrap();
repo.repo
.find_commit(git2::Oid::from_str(&new_commit).unwrap())
.unwrap();
assert!(
repo_path.join("cherry.txt").exists(),
"Cherry-picked file should exist"
);
repo.checkout_branch("source").unwrap();
let source_head = repo.get_head_commit_hash().unwrap();
assert_eq!(
source_head, cherry_commit,
"Source branch should be unchanged"
);
}
#[test]
fn test_cherry_pick_preserves_commit_message() {
let (_temp_dir, repo_path) = create_test_repo();
let repo = GitRepository::open(&repo_path).unwrap();
repo.create_branch("msg-test", None).unwrap();
repo.checkout_branch("msg-test").unwrap();
std::fs::write(repo_path.join("msg.txt"), "Content").unwrap();
Command::new("git")
.args(["add", "."])
.current_dir(&repo_path)
.output()
.unwrap();
let commit_msg = "Test: Special commit message\n\nWith body";
Command::new("git")
.args(["commit", "-m", commit_msg])
.current_dir(&repo_path)
.output()
.unwrap();
let original_commit = repo.get_head_commit_hash().unwrap();
Command::new("git")
.args(["checkout", "-"])
.current_dir(&repo_path)
.output()
.unwrap();
let new_commit = repo.cherry_pick(&original_commit).unwrap();
let output = Command::new("git")
.args(["log", "-1", "--format=%B", &new_commit])
.current_dir(&repo_path)
.output()
.unwrap();
let new_msg = String::from_utf8_lossy(&output.stdout);
assert!(
new_msg.contains("Special commit message"),
"Should preserve commit message"
);
}
#[test]
fn test_cherry_pick_handles_conflicts() {
let (_temp_dir, repo_path) = create_test_repo();
let repo = GitRepository::open(&repo_path).unwrap();
std::fs::write(repo_path.join("conflict.txt"), "Original").unwrap();
Command::new("git")
.args(["add", "."])
.current_dir(&repo_path)
.output()
.unwrap();
Command::new("git")
.args(["commit", "-m", "Add conflict file"])
.current_dir(&repo_path)
.output()
.unwrap();
repo.create_branch("conflict-branch", None).unwrap();
repo.checkout_branch("conflict-branch").unwrap();
std::fs::write(repo_path.join("conflict.txt"), "Modified").unwrap();
Command::new("git")
.args(["add", "."])
.current_dir(&repo_path)
.output()
.unwrap();
Command::new("git")
.args(["commit", "-m", "Modify conflict file"])
.current_dir(&repo_path)
.output()
.unwrap();
let conflict_commit = repo.get_head_commit_hash().unwrap();
Command::new("git")
.args(["checkout", "-"])
.current_dir(&repo_path)
.output()
.unwrap();
std::fs::write(repo_path.join("conflict.txt"), "Different").unwrap();
Command::new("git")
.args(["add", "."])
.current_dir(&repo_path)
.output()
.unwrap();
Command::new("git")
.args(["commit", "-m", "Different change"])
.current_dir(&repo_path)
.output()
.unwrap();
let result = repo.cherry_pick(&conflict_commit);
assert!(result.is_err(), "Cherry-pick with conflict should fail");
}
#[test]
fn test_reset_to_head_clears_staged_files() {
let (_temp_dir, repo_path) = create_test_repo();
let repo = GitRepository::open(&repo_path).unwrap();
std::fs::write(repo_path.join("staged1.txt"), "Content 1").unwrap();
std::fs::write(repo_path.join("staged2.txt"), "Content 2").unwrap();
Command::new("git")
.args(["add", "staged1.txt", "staged2.txt"])
.current_dir(&repo_path)
.output()
.unwrap();
let staged_before = repo.get_staged_files().unwrap();
assert_eq!(staged_before.len(), 2, "Should have 2 staged files");
repo.reset_to_head().unwrap();
let staged_after = repo.get_staged_files().unwrap();
assert_eq!(
staged_after.len(),
0,
"Should have no staged files after reset"
);
}
#[test]
fn test_reset_to_head_clears_modified_files() {
let (_temp_dir, repo_path) = create_test_repo();
let repo = GitRepository::open(&repo_path).unwrap();
std::fs::write(repo_path.join("README.md"), "# Modified content").unwrap();
Command::new("git")
.args(["add", "README.md"])
.current_dir(&repo_path)
.output()
.unwrap();
assert!(repo.is_dirty().unwrap(), "Repo should be dirty");
repo.reset_to_head().unwrap();
assert!(
!repo.is_dirty().unwrap(),
"Repo should be clean after reset"
);
let content = std::fs::read_to_string(repo_path.join("README.md")).unwrap();
assert_eq!(
content, "# Test",
"File should be restored to original content"
);
}
#[test]
fn test_reset_to_head_preserves_untracked_files() {
let (_temp_dir, repo_path) = create_test_repo();
let repo = GitRepository::open(&repo_path).unwrap();
std::fs::write(repo_path.join("untracked.txt"), "Untracked content").unwrap();
std::fs::write(repo_path.join("staged.txt"), "Staged content").unwrap();
Command::new("git")
.args(["add", "staged.txt"])
.current_dir(&repo_path)
.output()
.unwrap();
repo.reset_to_head().unwrap();
assert!(
repo_path.join("untracked.txt").exists(),
"Untracked file should be preserved"
);
assert!(
!repo_path.join("staged.txt").exists(),
"Staged but uncommitted file should be removed"
);
}
#[test]
fn test_cherry_pick_does_not_modify_source() {
let (_temp_dir, repo_path) = create_test_repo();
let repo = GitRepository::open(&repo_path).unwrap();
repo.create_branch("feature", None).unwrap();
repo.checkout_branch("feature").unwrap();
for i in 1..=3 {
std::fs::write(
repo_path.join(format!("file{i}.txt")),
format!("Content {i}"),
)
.unwrap();
Command::new("git")
.args(["add", "."])
.current_dir(&repo_path)
.output()
.unwrap();
Command::new("git")
.args(["commit", "-m", &format!("Commit {i}")])
.current_dir(&repo_path)
.output()
.unwrap();
}
let source_commits = Command::new("git")
.args(["log", "--format=%H", "feature"])
.current_dir(&repo_path)
.output()
.unwrap();
let source_state = String::from_utf8_lossy(&source_commits.stdout).to_string();
let commits: Vec<&str> = source_state.lines().collect();
let middle_commit = commits[1];
Command::new("git")
.args(["checkout", "-"])
.current_dir(&repo_path)
.output()
.unwrap();
repo.create_branch("target", None).unwrap();
repo.checkout_branch("target").unwrap();
repo.cherry_pick(middle_commit).unwrap();
let after_commits = Command::new("git")
.args(["log", "--format=%H", "feature"])
.current_dir(&repo_path)
.output()
.unwrap();
let after_state = String::from_utf8_lossy(&after_commits.stdout).to_string();
assert_eq!(
source_state, after_state,
"Source branch should be completely unchanged after cherry-pick"
);
}
#[test]
fn test_detect_parent_branch() {
let (_temp_dir, repo_path) = create_test_repo();
let repo = GitRepository::open(&repo_path).unwrap();
repo.create_branch("dev123", None).unwrap();
repo.checkout_branch("dev123").unwrap();
create_commit(&repo_path, "Base commit on dev123", "base.txt");
repo.create_branch("feature-branch", None).unwrap();
repo.checkout_branch("feature-branch").unwrap();
create_commit(&repo_path, "Feature commit", "feature.txt");
let detected_parent = repo.detect_parent_branch().unwrap();
assert!(detected_parent.is_some(), "Should detect a parent branch");
let parent = detected_parent.unwrap();
assert!(
parent == "dev123" || parent == "main" || parent == "master",
"Parent should be dev123, main, or master, got: {parent}"
);
}
}