Skip to content

Commit f61761d

Browse files
committed
Collect branches at current head-commit with gitoxide
1 parent 2c6016e commit f61761d

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

src/info/head_refs.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
use git2::Oid;
1+
use git_repository as git;
22
use serde::ser::SerializeStruct;
33
use serde::Serialize;
44

55
pub struct HeadRefs {
6-
commit: Oid,
6+
commit: git::hash::ObjectId,
77
refs: Vec<String>,
88
}
99

1010
impl HeadRefs {
11-
pub fn new(commit: Oid, refs: Vec<String>) -> HeadRefs {
11+
pub fn new(commit: git::hash::ObjectId, refs: Vec<String>) -> HeadRefs {
1212
HeadRefs { commit, refs }
1313
}
1414
}

src/info/repo.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ impl<'a> Repo<'a> {
203203
let current_time = commit.time()?.seconds();
204204
if current_time > most_recent {
205205
most_recent = current_time;
206-
version_name = tag.name().strip_prefix().to_string();
206+
version_name = tag.name().shorten().to_string();
207207
}
208208
}
209209
}
@@ -301,20 +301,24 @@ impl<'a> Repo<'a> {
301301
}
302302

303303
pub fn get_head_refs(&self) -> Result<HeadRefs> {
304-
let head = self.git2_repo.head()?;
305-
let head_oid = head.target().with_context(|| "Could not read HEAD")?;
306-
let refs = self.git2_repo.references()?;
307-
let refs_info = refs
308-
.filter_map(|reference| match reference {
309-
Ok(reference) => match (reference.target(), reference.shorthand()) {
310-
(Some(oid), Some(shorthand)) if oid == head_oid && !reference.is_tag() => {
311-
Some(String::from(shorthand))
312-
}
313-
_ => None,
314-
},
315-
Err(_) => None,
304+
let head_oid = self
305+
.repo
306+
.head()
307+
.with_context(|| "Could not read HEAD")?
308+
.peel_to_commit_in_place()?
309+
.id;
310+
let refs_info = self
311+
.repo
312+
.references()?
313+
.all()?
314+
.peeled()
315+
.filter_map(Result::ok)
316+
.filter_map(|reference: git::Reference<'_>| {
317+
(reference.id() == head_oid
318+
&& reference.name().category() != Some(git::reference::Category::Tag))
319+
.then(|| reference.name().shorten().to_string())
316320
})
317-
.collect::<Vec<String>>();
321+
.collect();
318322
Ok(HeadRefs::new(head_oid, refs_info))
319323
}
320324

0 commit comments

Comments
 (0)