Skip to content

Use gitoxide in get_commits_info and get_commit_info #2643

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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 @@ -22,6 +22,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
123 changes: 83 additions & 40 deletions asyncgit/src/sync/commits_info.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
use std::fmt::Display;

use super::RepoPath;
use crate::{
error::Result,
sync::{commit_details::get_author_of_commit, repository::repo},
};
use git2::{Commit, Error, Oid};
use crate::{error::Result, sync::repository::repo};
use git2::Oid;
use scopetime::scope_time;
use unicode_truncate::UnicodeTruncateStr;

Expand Down Expand Up @@ -84,6 +81,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 All @@ -105,34 +117,36 @@ pub fn get_commits_info(
) -> Result<Vec<CommitInfo>> {
scope_time!("get_commits_info");

let repo = repo(repo_path)?;
let mailmap = repo.mailmap()?;

let commits = ids
.iter()
.map(|id| repo.find_commit((*id).into()))
.collect::<std::result::Result<Vec<Commit>, Error>>()?
.into_iter();

let res = commits
.map(|c: Commit| {
let message = get_message(&c, Some(message_length_limit));
let author = get_author_of_commit(&c, &mailmap)
.name()
.map_or_else(
|| String::from("<unknown>"),
String::from,
);
CommitInfo {
let repo: gix::Repository =
gix::ThreadSafeRepository::discover_with_environment_overrides(repo_path.gitpath())
.map(Into::into)?;
let mailmap = repo.open_mailmap();

ids.iter()
.map(|id| -> Result<_> {
let commit = repo.find_commit(*id)?;
let commit_ref = commit.decode()?;

let message = gix_get_message(
&commit_ref,
Some(message_length_limit),
);

let author = commit_ref.author();

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

Ok(CommitInfo {
message,
author,
time: c.time().seconds(),
id: CommitId(c.id()),
}
author: author.to_string(),
time: commit_ref.time().seconds,
id: *id,
})
})
.collect::<Vec<_>>();

Ok(res)
.collect()
}

///
Expand All @@ -142,24 +156,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).into())?;
let author = get_author_of_commit(&commit, &mailmap);
let commit = repo.find_commit(*commit_id)?;
let commit_ref = commit.decode()?;

let message = gix_get_message(&commit_ref, None);

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 +199,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