Skip to content

Use gitoxide in get_commit_info #2654

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions asyncgit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ git2-hooks = { path = "../git2-hooks", version = ">=0.4" }
gix = { version = "0.71.0", default-features = false, features = [
"max-performance",
"revision",
"mailmap"
] }
log = "0.4"
# git2 = { path = "../../extern/git2-rs", features = ["vendored-openssl"]}
Expand Down
10 changes: 10 additions & 0 deletions asyncgit/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@ pub enum Error {
#[error("gix::revision::walk error: {0}")]
GixRevisionWalk(#[from] gix::revision::walk::Error),

///
#[error("gix::objs::decode::Error error: {0}")]
GixObjsDecode(#[from] gix::objs::decode::Error),

///
#[error("gix::object::find::existing::with_conversion::Error error: {0}")]
GixObjectFindExistingWithConversionError(
#[from] gix::object::find::existing::with_conversion::Error,
),

///
#[error("amend error: config commit.gpgsign=true detected.\ngpg signing is not supported for amending non-last commits")]
SignAmendNonLastCommit,
Expand Down
62 changes: 53 additions & 9 deletions asyncgit/src/sync/commits_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,21 @@ impl From<Oid> for CommitId {
}
}

impl From<gix::ObjectId> for CommitId {
fn from(object_id: gix::ObjectId) -> Self {
#[allow(clippy::expect_used)]
let oid = Oid::from_bytes(object_id.as_bytes()).expect("`Oid::from_bytes(object_id.as_bytes())` is expected to never fail");

Self::new(oid)
}
}

impl From<CommitId> for gix::ObjectId {
fn from(id: CommitId) -> Self {
Self::from_bytes_or_panic(id.0.as_bytes())
}
}

///
#[derive(Debug, Clone)]
pub struct CommitInfo {
Expand Down Expand Up @@ -142,24 +157,35 @@ pub fn get_commit_info(
) -> Result<CommitInfo> {
scope_time!("get_commit_info");

let repo = repo(repo_path)?;
let mailmap = repo.mailmap()?;
let repo: gix::Repository =
gix::ThreadSafeRepository::discover_with_environment_overrides(repo_path.gitpath())
.map(Into::into)?;
let mailmap = repo.open_mailmap();

let commit = repo.find_commit(*commit_id)?;
let commit_ref = commit.decode()?;

let message = gix_get_message(&commit_ref, None);

let commit = repo.find_commit((*commit_id).into())?;
let author = get_author_of_commit(&commit, &mailmap);
let author = commit_ref.author();

let author = mailmap.try_resolve(author).map_or_else(
|| author.name.into(),
|signature| signature.name,
);

Ok(CommitInfo {
message: commit.message().unwrap_or("").into(),
author: author.name().unwrap_or("<unknown>").into(),
time: commit.time().seconds(),
id: CommitId(commit.id()),
message,
author: author.to_string(),
time: commit_ref.time().seconds,
id: commit.id().detach().into(),
})
}

/// if `message_limit` is set the message will be
/// limited to the first line and truncated to fit
pub fn get_message(
c: &Commit,
c: &git2::Commit,
message_limit: Option<usize>,
) -> String {
let msg = String::from_utf8_lossy(c.message_bytes());
Expand All @@ -174,6 +200,24 @@ pub fn get_message(
)
}

/// if `message_limit` is set the message will be
/// limited to the first line and truncated to fit
pub fn gix_get_message(
commit_ref: &gix::objs::CommitRef,
message_limit: Option<usize>,
) -> String {
let message = commit_ref.message.to_string();
let message = message.trim();

message_limit.map_or_else(
|| message.to_string(),
|limit| {
let message = message.lines().next().unwrap_or_default();
message.unicode_truncate(limit).0.to_string()
},
)
}

#[cfg(test)]
mod tests {
use super::get_commits_info;
Expand Down
5 changes: 1 addition & 4 deletions asyncgit/src/sync/logwalker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,7 @@ impl<'a> LogWalkerWithoutFilter<'a> {
let mut count = 0_usize;

while let Some(Ok(info)) = self.walk.next() {
let bytes = info.id.as_bytes();
let commit_id: CommitId = Oid::from_bytes(bytes)?.into();

out.push(commit_id);
out.push(info.id.into());

count += 1;

Expand Down
Loading