pub struct BranchList { /* private fields */ }
Implementations§
Source§impl BranchList
impl BranchList
Sourcepub fn iter(&self) -> impl Iterator<Item = &Branch>
pub fn iter(&self) -> impl Iterator<Item = &Branch>
Get an iterator over all branches
Examples found in repository?
examples/branch_operations.rs (line 25)
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}
Sourcepub fn local(&self) -> impl Iterator<Item = &Branch>
pub fn local(&self) -> impl Iterator<Item = &Branch>
Get an iterator over local branches
Examples found in repository?
examples/branch_operations.rs (line 49)
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}
Sourcepub fn remote(&self) -> impl Iterator<Item = &Branch>
pub fn remote(&self) -> impl Iterator<Item = &Branch>
Get an iterator over remote-tracking branches
Examples found in repository?
examples/branch_operations.rs (line 121)
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}
Sourcepub fn find(&self, name: &str) -> Option<&Branch>
pub fn find(&self, name: &str) -> Option<&Branch>
Find a branch by name
Examples found in repository?
examples/merge_operations.rs (line 69)
45fn demonstrate_fast_forward_merge(repo: &Repository, temp_dir: &std::path::Path) -> Result<()> {
46 println!("--- Demonstrating Fast-Forward Merge ---\n");
47
48 // Create initial commit
49 println!("1. Creating initial commit on master...");
50 let file1_path = temp_dir.join("README.md");
51 fs::write(&file1_path, "# Project\n\nInitial content")?;
52 repo.add(&["README.md"])?;
53 let initial_commit = repo.commit("Initial commit")?;
54 println!(" Created commit: {}", initial_commit);
55
56 // Create feature branch and add commits
57 println!("\n2. Creating feature branch and adding commits...");
58 repo.checkout_new("feature/fast-forward", None)?;
59
60 let file2_path = temp_dir.join("feature.txt");
61 fs::write(&file2_path, "New feature implementation")?;
62 repo.add(&["feature.txt"])?;
63 let feature_commit = repo.commit("Add new feature")?;
64 println!(" Feature commit: {}", feature_commit);
65
66 // Switch back to master
67 println!("\n3. Switching back to master...");
68 let branches = repo.branches()?;
69 let master_branch = branches.find("master").unwrap();
70 repo.checkout(master_branch)?;
71 println!(" Switched to master");
72
73 // Perform fast-forward merge
74 println!("\n4. Performing fast-forward merge...");
75 let merge_status = repo.merge("feature/fast-forward")?;
76
77 match merge_status {
78 MergeStatus::FastForward(hash) => {
79 println!(" ✓ Fast-forward merge completed!");
80 println!(" New HEAD: {}", hash);
81 println!(" Both files are now present on master");
82 }
83 _ => println!(" Unexpected merge result: {:?}", merge_status),
84 }
85
86 println!(" Files in repository:");
87 for file in ["README.md", "feature.txt"] {
88 if temp_dir.join(file).exists() {
89 println!(" ✓ {}", file);
90 }
91 }
92
93 Ok(())
94}
95
96fn demonstrate_no_fast_forward_merge(repo: &Repository, temp_dir: &std::path::Path) -> Result<()> {
97 println!("\n--- Demonstrating No-Fast-Forward Merge ---\n");
98
99 // Add a commit to master to prevent fast-forward
100 println!("1. Adding commit to master...");
101 let readme_path = temp_dir.join("README.md");
102 fs::write(
103 &readme_path,
104 "# Project\n\nInitial content\n\n## Updates\nAdded documentation",
105 )?;
106 repo.add(&["README.md"])?;
107 let master_commit = repo.commit("Update documentation")?;
108 println!(" Master commit: {}", master_commit);
109
110 // Create another feature branch
111 println!("\n2. Creating another feature branch...");
112 repo.checkout_new("feature/no-ff", None)?;
113
114 let config_path = temp_dir.join("config.yaml");
115 fs::write(&config_path, "app:\n name: example\n version: 1.0")?;
116 repo.add(&["config.yaml"])?;
117 let config_commit = repo.commit("Add configuration file")?;
118 println!(" Config commit: {}", config_commit);
119
120 // Switch back to master
121 println!("\n3. Switching back to master...");
122 let branches = repo.branches()?;
123 let master_branch = branches.find("master").unwrap();
124 repo.checkout(master_branch)?;
125
126 // Perform no-fast-forward merge
127 println!("\n4. Performing no-fast-forward merge...");
128 let options = MergeOptions::new()
129 .with_fast_forward(FastForwardMode::Never)
130 .with_message("Merge feature/no-ff into master".to_string());
131
132 let merge_status = repo.merge_with_options("feature/no-ff", options)?;
133
134 match merge_status {
135 MergeStatus::Success(hash) => {
136 println!(" ✓ Merge commit created!");
137 println!(" Merge commit: {}", hash);
138 println!(" Created explicit merge commit preserving branch history");
139 }
140 _ => println!(" Unexpected merge result: {:?}", merge_status),
141 }
142
143 // Show the commit history
144 println!("\n5. Recent commit history:");
145 let commits = repo.recent_commits(3)?;
146 for (i, commit) in commits.iter().enumerate() {
147 println!(
148 " {}: {} - {}",
149 i + 1,
150 commit.hash.short(),
151 commit.message.subject
152 );
153 }
154
155 Ok(())
156}
157
158fn demonstrate_merge_conflicts(repo: &Repository, temp_dir: &std::path::Path) -> Result<()> {
159 println!("\n--- Demonstrating Merge Conflicts ---\n");
160
161 // Create conflicting branch
162 println!("1. Creating branch with conflicting changes...");
163 repo.checkout_new("feature/conflict", None)?;
164
165 // Modify the same file differently
166 let readme_path = temp_dir.join("README.md");
167 fs::write(
168 &readme_path,
169 "# Project\n\nFeature branch changes\n\n## Updates\nAdded documentation",
170 )?;
171 repo.add(&["README.md"])?;
172 let feature_commit = repo.commit("Update README from feature branch")?;
173 println!(" Feature commit: {}", feature_commit);
174
175 // Switch back to master and make conflicting change
176 println!("\n2. Making conflicting change on master...");
177 let branches = repo.branches()?;
178 let master_branch = branches.find("master").unwrap();
179 repo.checkout(master_branch)?;
180
181 fs::write(
182 &readme_path,
183 "# Project\n\nMaster branch changes\n\n## Updates\nAdded documentation",
184 )?;
185 repo.add(&["README.md"])?;
186 let master_conflict_commit = repo.commit("Update README from master")?;
187 println!(" Master commit: {}", master_conflict_commit);
188
189 // Attempt merge (will have conflicts)
190 println!("\n3. Attempting merge (will have conflicts)...");
191 let merge_status = repo.merge("feature/conflict")?;
192
193 match merge_status {
194 MergeStatus::Conflicts(files) => {
195 println!(" ⚠️ Merge conflicts detected!");
196 println!(" Conflicted files:");
197 for file in &files {
198 println!(" - {}", file.display());
199 }
200
201 // Check merge in progress
202 if repo.merge_in_progress()? {
203 println!(" ✓ Merge in progress status detected");
204 }
205
206 // Show conflict markers in file
207 println!("\n4. Conflict markers in README.md:");
208 let content = fs::read_to_string(&readme_path)?;
209 for (i, line) in content.lines().enumerate() {
210 if line.starts_with("<<<<<<< ")
211 || line.starts_with("======= ")
212 || line.starts_with(">>>>>>> ")
213 {
214 println!(" {}: {} <-- conflict marker", i + 1, line);
215 } else {
216 println!(" {}: {}", i + 1, line);
217 }
218 }
219
220 // Abort the merge
221 println!("\n5. Aborting merge...");
222 repo.abort_merge()?;
223 println!(" ✓ Merge aborted successfully");
224
225 // Verify merge is no longer in progress
226 if !repo.merge_in_progress()? {
227 println!(" ✓ Repository is back to clean state");
228 }
229 }
230 _ => println!(" Unexpected merge result: {:?}", merge_status),
231 }
232
233 Ok(())
234}
235
236fn demonstrate_merge_status_and_abort(repo: &Repository, temp_dir: &std::path::Path) -> Result<()> {
237 println!("\n--- Demonstrating Merge Status and Options ---\n");
238
239 // Create a simple feature branch
240 println!("1. Creating simple feature branch...");
241 repo.checkout_new("feature/simple", None)?;
242
243 let simple_path = temp_dir.join("simple.txt");
244 fs::write(&simple_path, "Simple feature content")?;
245 repo.add(&["simple.txt"])?;
246 repo.commit("Add simple feature")?;
247
248 // Switch back to master
249 let branches = repo.branches()?;
250 let master_branch = branches.find("master").unwrap();
251 repo.checkout(master_branch)?;
252
253 // Test merge with different options
254 println!("\n2. Testing merge with custom options...");
255 let options = MergeOptions::new()
256 .with_fast_forward(FastForwardMode::Auto)
257 .with_message("Integrate simple feature".to_string());
258
259 let merge_status = repo.merge_with_options("feature/simple", options)?;
260
261 match merge_status {
262 MergeStatus::FastForward(hash) => {
263 println!(" ✓ Fast-forward merge completed: {}", hash);
264 }
265 MergeStatus::Success(hash) => {
266 println!(" ✓ Merge commit created: {}", hash);
267 }
268 MergeStatus::UpToDate => {
269 println!(" ✓ Already up to date");
270 }
271 MergeStatus::Conflicts(_) => {
272 println!(" ⚠️ Unexpected conflicts");
273 }
274 }
275
276 // Show final repository state
277 println!("\n3. Final repository state:");
278 let status = repo.status()?;
279 println!(
280 " Working directory clean: {}",
281 status.staged_files().count() == 0 && status.unstaged_files().count() == 0
282 );
283
284 let commits = repo.recent_commits(5)?;
285 println!(" Recent commits:");
286 for (i, commit) in commits.iter().enumerate() {
287 println!(
288 " {}: {} - {}",
289 i + 1,
290 commit.hash.short(),
291 commit.message.subject
292 );
293 }
294
295 Ok(())
296}
More examples
examples/branch_operations.rs (line 74)
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}
Sourcepub fn find_by_short_name(&self, short_name: &str) -> Option<&Branch>
pub fn find_by_short_name(&self, short_name: &str) -> Option<&Branch>
Find a branch by short name (useful for remote branches)
Examples found in repository?
examples/branch_operations.rs (line 107)
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}
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Get the count of branches
Examples found in repository?
examples/branch_operations.rs (line 81)
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}
Sourcepub fn local_count(&self) -> usize
pub fn local_count(&self) -> usize
Get count of local branches
Examples found in repository?
examples/branch_operations.rs (line 82)
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}
Sourcepub fn remote_count(&self) -> usize
pub fn remote_count(&self) -> usize
Get count of remote-tracking branches
Examples found in repository?
examples/branch_operations.rs (line 119)
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}
Trait Implementations§
Source§impl Clone for BranchList
impl Clone for BranchList
Source§fn clone(&self) -> BranchList
fn clone(&self) -> BranchList
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl Debug for BranchList
impl Debug for BranchList
Source§impl Display for BranchList
impl Display for BranchList
Source§impl PartialEq for BranchList
impl PartialEq for BranchList
impl StructuralPartialEq for BranchList
Auto Trait Implementations§
impl Freeze for BranchList
impl RefUnwindSafe for BranchList
impl Send for BranchList
impl Sync for BranchList
impl Unpin for BranchList
impl UnwindSafe for BranchList
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more