use rustic_git::{Repository, Result};
use std::{env, fs};
fn main() -> Result<()> {
let test_path = env::temp_dir().join("rustic_git_branch_example");
if test_path.exists() {
fs::remove_dir_all(&test_path).unwrap();
}
let repo = Repository::init(&test_path, false)?;
println!("Created repository at: {}", test_path.display());
fs::write(test_path.join("README.md"), "# Branch Operations Demo\n").unwrap();
repo.add(&["README.md"])?;
repo.commit("Initial commit")?;
println!("Created initial commit");
let branches = repo.branches()?;
println!("\n=== Initial Branches ===");
for branch in branches.iter() {
println!(" {}", branch);
}
if let Some(current) = repo.current_branch()? {
println!(
"\nCurrent branch: {} ({})",
current.name,
current.commit_hash.short()
);
}
println!("\n=== Creating Branches ===");
let feature_branch = repo.create_branch("feature/new-api", None)?;
println!("Created branch: {}", feature_branch.name);
let bugfix_branch = repo.create_branch("bugfix/issue-123", Some("HEAD"))?;
println!("Created branch: {}", bugfix_branch.name);
let branches = repo.branches()?;
println!("\n=== After Creating Branches ===");
for branch in branches.local() {
println!(" {} (local)", branch);
}
println!("\n=== Creating and Checking Out Branch ===");
let dev_branch = repo.checkout_new("develop", None)?;
println!("Created and checked out: {}", dev_branch.name);
fs::write(test_path.join("feature.txt"), "New feature code\n").unwrap();
repo.add(&["feature.txt"])?;
repo.commit("Add new feature")?;
println!("Made commit on develop branch");
if let Some(current) = repo.current_branch()? {
println!(
"Now on branch: {} ({})",
current.name,
current.commit_hash.short()
);
}
let main_branch = branches.find("master").unwrap().clone();
repo.checkout(&main_branch)?;
println!("\nSwitched back to master branch");
let final_branches = repo.branches()?;
println!("\n=== Final Branch List ===");
println!("Total branches: {}", final_branches.len());
println!("Local branches: {}", final_branches.local_count());
for branch in final_branches.iter() {
let marker = if branch.is_current { "*" } else { " " };
let branch_type = if branch.is_local() { "local" } else { "remote" };
println!(
" {}{} ({}) {}",
marker,
branch.name,
branch_type,
branch.commit_hash.short()
);
if let Some(upstream) = &branch.upstream {
println!(" └── tracks: {}", upstream);
}
}
println!("\n=== Branch Search Examples ===");
if let Some(branch) = final_branches.find("develop") {
println!("Found branch by name: {}", branch.name);
}
if let Some(branch) = final_branches.find_by_short_name("new-api") {
println!("Found branch by short name: {}", branch.name);
}
println!("\n=== Branch Filtering ===");
println!("Local branches:");
for branch in final_branches.local() {
println!(" - {}", branch.name);
}
if final_branches.remote_count() > 0 {
println!("Remote branches:");
for branch in final_branches.remote() {
println!(" - {}", branch.name);
}
}
println!("\n=== Branch Deletion ===");
let bugfix = final_branches.find("bugfix/issue-123").unwrap().clone();
repo.delete_branch(&bugfix, false)?;
println!("Deleted branch: {}", bugfix.name);
let final_branches = repo.branches()?;
println!("\nFinal branch count: {}", final_branches.len());
fs::remove_dir_all(&test_path).unwrap();
println!("\nCleaned up test repository");
Ok(())
}