Skip to content

Commit fd46b9a

Browse files
authored
Use gitoxide for get_tags (#2664)
1 parent fdd5a19 commit fd46b9a

File tree

3 files changed

+54
-54
lines changed

3 files changed

+54
-54
lines changed

asyncgit/src/error.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub enum GixError {
1717

1818
///
1919
#[error("gix::object::find::existing::with_conversion::Error error: {0}")]
20-
ObjectFindExistingWithConversionError(
20+
ObjectFindExistingWithConversion(
2121
#[from] gix::object::find::existing::with_conversion::Error,
2222
),
2323

@@ -39,6 +39,14 @@ pub enum GixError {
3939
#[error("gix::reference::head_tree_id::Error error: {0}")]
4040
ReferenceHeadTreeId(#[from] gix::reference::head_tree_id::Error),
4141

42+
///
43+
#[error("gix::reference::iter::Error error: {0}")]
44+
ReferenceIter(#[from] gix::reference::iter::Error),
45+
46+
///
47+
#[error("gix::reference::iter::init::Error error: {0}")]
48+
ReferenceIterInit(#[from] gix::reference::iter::init::Error),
49+
4250
///
4351
#[error("gix::revision::walk error: {0}")]
4452
RevisionWalk(#[from] gix::revision::walk::Error),
@@ -251,6 +259,18 @@ impl From<gix::reference::head_tree_id::Error> for Error {
251259
}
252260
}
253261

262+
impl From<gix::reference::iter::Error> for Error {
263+
fn from(error: gix::reference::iter::Error) -> Self {
264+
Self::Gix(GixError::from(error))
265+
}
266+
}
267+
268+
impl From<gix::reference::iter::init::Error> for Error {
269+
fn from(error: gix::reference::iter::init::Error) -> Self {
270+
Self::Gix(GixError::from(error))
271+
}
272+
}
273+
254274
impl From<gix::revision::walk::Error> for Error {
255275
fn from(error: gix::revision::walk::Error) -> Self {
256276
Self::Gix(GixError::from(error))

asyncgit/src/sync/commits_info.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,15 @@ impl From<gix::ObjectId> for CommitId {
9696
}
9797
}
9898

99+
impl From<gix::Commit<'_>> for CommitId {
100+
fn from(commit: gix::Commit<'_>) -> Self {
101+
#[allow(clippy::expect_used)]
102+
let oid = Oid::from_bytes(commit.id().as_bytes()).expect("`Oid::from_bytes(commit.id().as_bytes())` is expected to never fail");
103+
104+
Self::new(oid)
105+
}
106+
}
107+
99108
impl From<CommitId> for gix::ObjectId {
100109
fn from(id: CommitId) -> Self {
101110
Self::from_bytes_or_panic(id.0.as_bytes())

asyncgit/src/sync/tags.rs

Lines changed: 24 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
use super::{get_commits_info, CommitId, RepoPath};
2-
use crate::{
3-
error::Result,
4-
sync::{repository::repo, utils::bytes2string},
5-
};
2+
use crate::{error::Result, sync::repository::repo};
63
use scopetime::scope_time;
7-
use std::{
8-
collections::{BTreeMap, HashMap, HashSet},
9-
ops::Not,
10-
};
4+
use std::collections::{BTreeMap, HashMap, HashSet};
115

126
///
137
#[derive(Clone, Hash, PartialEq, Eq, Debug)]
@@ -64,52 +58,29 @@ pub fn get_tags(repo_path: &RepoPath) -> Result<Tags> {
6458
}
6559
};
6660

67-
let repo = repo(repo_path)?;
68-
69-
repo.tag_foreach(|id, name| {
70-
if let Ok(name) =
71-
// skip the `refs/tags/` part
72-
String::from_utf8(name[10..name.len()].into())
73-
{
74-
//NOTE: find_tag (using underlying git_tag_lookup) only
75-
// works on annotated tags lightweight tags `id` already
76-
// points to the target commit
77-
// see https://github.com/libgit2/libgit2/issues/5586
78-
let commit = repo
79-
.find_tag(id)
80-
.and_then(|tag| tag.target())
81-
.and_then(|target| target.peel_to_commit())
82-
.map_or_else(
83-
|_| {
84-
if repo.find_commit(id).is_ok() {
85-
Some(CommitId::new(id))
86-
} else {
87-
None
88-
}
89-
},
90-
|commit| Some(CommitId::new(commit.id())),
91-
);
92-
93-
let annotation = repo
94-
.find_tag(id)
95-
.ok()
96-
.as_ref()
97-
.and_then(git2::Tag::message_bytes)
98-
.and_then(|msg| {
99-
msg.is_empty()
100-
.not()
101-
.then(|| bytes2string(msg).ok())
102-
.flatten()
103-
});
104-
105-
if let Some(commit) = commit {
106-
adder(commit, Tag { name, annotation });
107-
}
108-
109-
return true;
61+
let gix_repo: gix::Repository =
62+
gix::ThreadSafeRepository::discover_with_environment_overrides(repo_path.gitpath())
63+
.map(Into::into)?;
64+
let platform = gix_repo.references()?;
65+
for mut reference in (platform.tags()?).flatten() {
66+
let commit = reference.peel_to_commit();
67+
let tag = reference.peel_to_tag();
68+
69+
if let Ok(commit) = commit {
70+
let tag_ref = tag.as_ref().map(gix::Tag::decode);
71+
72+
let name = match tag_ref {
73+
Ok(Ok(tag)) => tag.name.to_string(),
74+
_ => reference.name().shorten().to_string(),
75+
};
76+
let annotation = match tag_ref {
77+
Ok(Ok(tag)) => Some(tag.message.to_string()),
78+
_ => None,
79+
};
80+
81+
adder(commit.into(), Tag { name, annotation });
11082
}
111-
false
112-
})?;
83+
}
11384

11485
Ok(res)
11586
}

0 commit comments

Comments
 (0)