branch_operations/
branch_operations.rs

1use rustic_git::{Repository, Result};
2use std::{env, fs};
3
4fn main() -> Result<()> {
5    let test_path = env::temp_dir().join("rustic_git_branch_example");
6
7    // Clean up if exists
8    if test_path.exists() {
9        fs::remove_dir_all(&test_path).unwrap();
10    }
11
12    // Create a test repository
13    let repo = Repository::init(&test_path, false)?;
14    println!("Created repository at: {}", test_path.display());
15
16    // Create initial commit so we have a valid HEAD
17    fs::write(test_path.join("README.md"), "# Branch Operations Demo\n").unwrap();
18    repo.add(&["README.md"])?;
19    repo.commit("Initial commit")?;
20    println!("Created initial commit");
21
22    // List all branches
23    let branches = repo.branches()?;
24    println!("\n=== Initial Branches ===");
25    for branch in branches.iter() {
26        println!("  {}", branch);
27    }
28
29    // Get current branch
30    if let Some(current) = repo.current_branch()? {
31        println!(
32            "\nCurrent branch: {} ({})",
33            current.name,
34            current.commit_hash.short()
35        );
36    }
37
38    // Create new branches
39    println!("\n=== Creating Branches ===");
40    let feature_branch = repo.create_branch("feature/new-api", None)?;
41    println!("Created branch: {}", feature_branch.name);
42
43    let bugfix_branch = repo.create_branch("bugfix/issue-123", Some("HEAD"))?;
44    println!("Created branch: {}", bugfix_branch.name);
45
46    // List branches again
47    let branches = repo.branches()?;
48    println!("\n=== After Creating Branches ===");
49    for branch in branches.local() {
50        println!("  {} (local)", branch);
51    }
52
53    // Create and checkout a new branch
54    println!("\n=== Creating and Checking Out Branch ===");
55    let dev_branch = repo.checkout_new("develop", None)?;
56    println!("Created and checked out: {}", dev_branch.name);
57
58    // Make a commit on the new branch
59    fs::write(test_path.join("feature.txt"), "New feature code\n").unwrap();
60    repo.add(&["feature.txt"])?;
61    repo.commit("Add new feature")?;
62    println!("Made commit on develop branch");
63
64    // Show current branch after checkout
65    if let Some(current) = repo.current_branch()? {
66        println!(
67            "Now on branch: {} ({})",
68            current.name,
69            current.commit_hash.short()
70        );
71    }
72
73    // Switch back to master branch
74    let main_branch = branches.find("master").unwrap().clone();
75    repo.checkout(&main_branch)?;
76    println!("\nSwitched back to master branch");
77
78    // List all branches with details
79    let final_branches = repo.branches()?;
80    println!("\n=== Final Branch List ===");
81    println!("Total branches: {}", final_branches.len());
82    println!("Local branches: {}", final_branches.local_count());
83
84    for branch in final_branches.iter() {
85        let marker = if branch.is_current { "*" } else { " " };
86        let branch_type = if branch.is_local() { "local" } else { "remote" };
87        println!(
88            "  {}{} ({}) {}",
89            marker,
90            branch.name,
91            branch_type,
92            branch.commit_hash.short()
93        );
94
95        if let Some(upstream) = &branch.upstream {
96            println!("    └── tracks: {}", upstream);
97        }
98    }
99
100    // Demonstrate branch searching
101    println!("\n=== Branch Search Examples ===");
102
103    if let Some(branch) = final_branches.find("develop") {
104        println!("Found branch by name: {}", branch.name);
105    }
106
107    if let Some(branch) = final_branches.find_by_short_name("new-api") {
108        println!("Found branch by short name: {}", branch.name);
109    }
110
111    // Demonstrate branch filtering
112    println!("\n=== Branch Filtering ===");
113
114    println!("Local branches:");
115    for branch in final_branches.local() {
116        println!("  - {}", branch.name);
117    }
118
119    if final_branches.remote_count() > 0 {
120        println!("Remote branches:");
121        for branch in final_branches.remote() {
122            println!("  - {}", branch.name);
123        }
124    }
125
126    // Delete a branch (switch away first if it's current)
127    println!("\n=== Branch Deletion ===");
128    let bugfix = final_branches.find("bugfix/issue-123").unwrap().clone();
129    repo.delete_branch(&bugfix, false)?;
130    println!("Deleted branch: {}", bugfix.name);
131
132    // Show final state
133    let final_branches = repo.branches()?;
134    println!("\nFinal branch count: {}", final_branches.len());
135
136    // Clean up
137    fs::remove_dir_all(&test_path).unwrap();
138    println!("\nCleaned up test repository");
139
140    Ok(())
141}