branch_operations/
branch_operations.rs1use 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 if test_path.exists() {
9 fs::remove_dir_all(&test_path).unwrap();
10 }
11
12 let repo = Repository::init(&test_path, false)?;
14 println!("Created repository at: {}", test_path.display());
15
16 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 let branches = repo.branches()?;
24 println!("\n=== Initial Branches ===");
25 for branch in branches.iter() {
26 println!(" {}", branch);
27 }
28
29 if let Some(current) = repo.current_branch()? {
31 println!(
32 "\nCurrent branch: {} ({})",
33 current.name,
34 current.commit_hash.short()
35 );
36 }
37
38 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 let branches = repo.branches()?;
48 println!("\n=== After Creating Branches ===");
49 for branch in branches.local() {
50 println!(" {} (local)", branch);
51 }
52
53 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 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 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 let main_branch = branches.find("master").unwrap().clone();
75 repo.checkout(&main_branch)?;
76 println!("\nSwitched back to master branch");
77
78 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 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 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 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 let final_branches = repo.branches()?;
134 println!("\nFinal branch count: {}", final_branches.len());
135
136 fs::remove_dir_all(&test_path).unwrap();
138 println!("\nCleaned up test repository");
139
140 Ok(())
141}