Skip to main content

Branch

Struct Branch 

Source
pub struct Branch {
    pub name: String,
    pub branch_type: BranchType,
    pub is_current: bool,
    pub commit_hash: Hash,
    pub upstream: Option<String>,
}

Fields§

§name: String§branch_type: BranchType§is_current: bool§commit_hash: Hash§upstream: Option<String>

Implementations§

Source§

impl Branch

Source

pub fn is_local(&self) -> bool

Check if this is a local branch

Examples found in repository?
examples/branch_operations.rs (line 86)
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}
Source

pub fn is_remote(&self) -> bool

Check if this is a remote-tracking branch

Source

pub fn short_name(&self) -> &str

Get the short name of the branch (without remote prefix for remote branches)

Trait Implementations§

Source§

impl Clone for Branch

Source§

fn clone(&self) -> Branch

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Branch

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Branch

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Branch

Source§

fn eq(&self, other: &Branch) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Branch

Source§

impl StructuralPartialEq for Branch

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.