| 1 |
use crate::{git::GitSignature, prelude::*}; |
| 2 |
use gix::Reference; |
| 3 |
|
| 4 |
pub struct Branch<'repo> { |
| 5 |
pub name: String, |
| 6 |
pub last_commit: Commit<'repo>, |
| 7 |
pub author: GitSignature, |
| 8 |
} |
| 9 |
|
| 10 |
impl<'repo> Branch<'repo> { |
| 11 |
pub fn from_gix(mut reference: Reference<'repo>) -> Result<Branch<'repo>, Error> { |
| 12 |
let name = reference.name().shorten().to_string(); |
| 13 |
let last_commit = Commit::from_gix(reference.peel_to_commit()?)?; |
| 14 |
let author = last_commit.author.clone(); |
| 15 |
|
| 16 |
Ok(Branch { |
| 17 |
name, |
| 18 |
last_commit, |
| 19 |
author, |
| 20 |
}) |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
impl Repository { |
| 25 |
pub fn head_branch_name(&self) -> Result<String, Error> { |
| 26 |
let gix_repo = self.gix(); |
| 27 |
let head = gix_repo.head()?; |
| 28 |
let branch_name = match head.referent_name() { |
| 29 |
Some(b) => b.shorten().to_string(), |
| 30 |
None => head.name().shorten().to_string(), |
| 31 |
}; |
| 32 |
Ok(branch_name) |
| 33 |
} |
| 34 |
|
| 35 |
pub fn branches<'repo>( |
| 36 |
&self, |
| 37 |
gix_repo: &'repo gix::Repository, |
| 38 |
offset: usize, |
| 39 |
limit: usize, |
| 40 |
) -> Result<(Vec<Branch<'repo>>, bool), Error> { |
| 41 |
let references = gix_repo.references()?; |
| 42 |
let gix_branches = references.local_branches()?; |
| 43 |
|
| 44 |
let mut branches = Vec::new(); |
| 45 |
let mut has_next = false; |
| 46 |
|
| 47 |
for (i, branch) in gix_branches.skip(offset).enumerate() { |
| 48 |
if i >= limit { |
| 49 |
has_next = true; |
| 50 |
break; |
| 51 |
} |
| 52 |
branches.push(Branch::from_gix(branch?)?); |
| 53 |
} |
| 54 |
Ok((branches, has_next)) |
| 55 |
} |
| 56 |
|
| 57 |
pub fn branch_names(&self) -> Result<Vec<String>, Error> { |
| 58 |
let gix_repo = self.gix(); |
| 59 |
let references = gix_repo.references()?; |
| 60 |
references |
| 61 |
.local_branches()? |
| 62 |
.map(|b| Ok(b?.name().shorten().to_string())) |
| 63 |
.collect() |
| 64 |
} |
| 65 |
} |