-
-
Notifications
You must be signed in to change notification settings - Fork 641
Use gitoxide
for get_tags
#2664
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,7 @@ | ||
use super::{get_commits_info, CommitId, RepoPath}; | ||
use crate::{ | ||
error::Result, | ||
sync::{repository::repo, utils::bytes2string}, | ||
}; | ||
use crate::{error::Result, sync::repository::repo}; | ||
use scopetime::scope_time; | ||
use std::{ | ||
collections::{BTreeMap, HashMap, HashSet}, | ||
ops::Not, | ||
}; | ||
use std::collections::{BTreeMap, HashMap, HashSet}; | ||
|
||
/// | ||
#[derive(Clone, Hash, PartialEq, Eq, Debug)] | ||
|
@@ -64,52 +58,29 @@ pub fn get_tags(repo_path: &RepoPath) -> Result<Tags> { | |
} | ||
}; | ||
|
||
let repo = repo(repo_path)?; | ||
|
||
repo.tag_foreach(|id, name| { | ||
if let Ok(name) = | ||
// skip the `refs/tags/` part | ||
String::from_utf8(name[10..name.len()].into()) | ||
{ | ||
//NOTE: find_tag (using underlying git_tag_lookup) only | ||
// works on annotated tags lightweight tags `id` already | ||
// points to the target commit | ||
// see https://github.com/libgit2/libgit2/issues/5586 | ||
let commit = repo | ||
.find_tag(id) | ||
.and_then(|tag| tag.target()) | ||
.and_then(|target| target.peel_to_commit()) | ||
.map_or_else( | ||
|_| { | ||
if repo.find_commit(id).is_ok() { | ||
Some(CommitId::new(id)) | ||
} else { | ||
None | ||
} | ||
}, | ||
|commit| Some(CommitId::new(commit.id())), | ||
); | ||
|
||
let annotation = repo | ||
.find_tag(id) | ||
.ok() | ||
.as_ref() | ||
.and_then(git2::Tag::message_bytes) | ||
.and_then(|msg| { | ||
msg.is_empty() | ||
.not() | ||
.then(|| bytes2string(msg).ok()) | ||
.flatten() | ||
}); | ||
|
||
if let Some(commit) = commit { | ||
adder(commit, Tag { name, annotation }); | ||
} | ||
|
||
return true; | ||
let gix_repo: gix::Repository = | ||
gix::ThreadSafeRepository::discover_with_environment_overrides(repo_path.gitpath()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When seeing this I think A PR is very welcome, it should not be more than a refactor.. The key here is really just if std::env::var_os("GIT_DIR").is_some() {
return Self::open_with_environment_overrides(directory.as_ref(), trust_map).map_err(Error::Open);
} which could also be part of the |
||
.map(Into::into)?; | ||
let platform = gix_repo.references()?; | ||
for mut reference in (platform.tags()?).flatten() { | ||
let commit = reference.peel_to_commit(); | ||
let tag = reference.peel_to_tag(); | ||
Comment on lines
+66
to
+67
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be extra-awesome, I'd invert the logic here and But if you want nice code, this is probably it, and I don't think this is a performance issue either. |
||
|
||
if let Ok(commit) = commit { | ||
let tag_ref = tag.as_ref().map(gix::Tag::decode); | ||
|
||
let name = match tag_ref { | ||
Ok(Ok(tag)) => tag.name.to_string(), | ||
_ => reference.name().shorten().to_string(), | ||
}; | ||
let annotation = match tag_ref { | ||
Ok(Ok(tag)) => Some(tag.message.to_string()), | ||
_ => None, | ||
}; | ||
|
||
adder(commit.into(), Tag { name, annotation }); | ||
} | ||
false | ||
})?; | ||
} | ||
|
||
Ok(res) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Nit) I often struggle with good
.expect("messages")
as they shouldn't be redundant, or else I could use the.unwrap()
I want to avoid. Here I'd probably write thatgit2 commits are always 20 bytes long
as the reason I expect this to always work.Funnily enough, while writing it I was wondering… are they? Is there SHA256 and would that work then? Answer: certainly once
gix
has SHA256 compiled in, but it felt interesting how a good.expect()
makes you think.