Skip to content

Commit 6a6c9fa

Browse files
authored
More idiomatic way to fetch HEAD refs (#1515)
1 parent 98d6ecb commit 6a6c9fa

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

src/info/head.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::info::utils::info_field::InfoField;
22
use anyhow::{Context, Result};
3-
use gix::{reference::Category, Reference, Repository};
3+
use gix::Repository;
44
use serde::Serialize;
55

66
#[derive(Serialize)]
@@ -49,18 +49,23 @@ impl HeadInfo {
4949
}
5050

5151
fn get_head_refs(repo: &Repository) -> Result<HeadRefs> {
52-
let head_oid = repo.head_id().context("Could not read HEAD")?;
53-
let refs_info = repo
54-
.references()?
55-
.all()?
56-
.filter_map(Result::ok)
57-
.filter_map(|reference: Reference<'_>| {
58-
(reference.target().try_id() == Some(&head_oid)
59-
&& reference.name().category() != Some(Category::Tag))
60-
.then(|| reference.name().shorten().to_string())
61-
})
62-
.collect();
63-
Ok(HeadRefs::new(head_oid.shorten()?.to_string(), refs_info))
52+
let head_id = repo.head_id().context("Failed to retrieve HEAD ID")?;
53+
54+
let mut ref_names = Vec::new();
55+
56+
if let Some(head_ref) = repo.head_ref()? {
57+
let head_ref_name = head_ref.name().shorten().to_string();
58+
ref_names.push(head_ref_name);
59+
60+
if let Some(Ok(remote_tracking_ref)) =
61+
repo.branch_remote_tracking_ref_name(head_ref.name(), gix::remote::Direction::Push)
62+
{
63+
let remote_tracking_ref_name = remote_tracking_ref.shorten().to_string();
64+
ref_names.push(remote_tracking_ref_name);
65+
}
66+
}
67+
68+
Ok(HeadRefs::new(head_id.shorten()?.to_string(), ref_names))
6469
}
6570

6671
#[typetag::serialize]

0 commit comments

Comments
 (0)