Skip to content

Commit 88dd99e

Browse files
committed
Use gitoxide in get_commit_info
1 parent b54d4cd commit 88dd99e

File tree

4 files changed

+68
-9
lines changed

4 files changed

+68
-9
lines changed

Cargo.lock

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

asyncgit/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ git2-hooks = { path = "../git2-hooks", version = ">=0.4" }
2727
gix = { version = "0.71.0", default-features = false, features = [
2828
"max-performance",
2929
"revision",
30+
"mailmap"
3031
] }
3132
log = "0.4"
3233
# git2 = { path = "../../extern/git2-rs", features = ["vendored-openssl"]}

asyncgit/src/error.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,16 @@ pub enum Error {
113113
#[error("gix::revision::walk error: {0}")]
114114
GixRevisionWalk(#[from] gix::revision::walk::Error),
115115

116+
///
117+
#[error("gix::objs::decode::Error error: {0}")]
118+
GixObjsDecode(#[from] gix::objs::decode::Error),
119+
120+
///
121+
#[error("gix::object::find::existing::with_conversion::Error error: {0}")]
122+
GixObjectFindExistingWithConversionError(
123+
#[from] gix::object::find::existing::with_conversion::Error,
124+
),
125+
116126
///
117127
#[error("amend error: config commit.gpgsign=true detected.\ngpg signing is not supported for amending non-last commits")]
118128
SignAmendNonLastCommit,

asyncgit/src/sync/commits_info.rs

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ impl From<gix::ObjectId> for CommitId {
9393
}
9494
}
9595

96+
impl From<CommitId> for gix::ObjectId {
97+
fn from(id: CommitId) -> Self {
98+
Self::from_bytes_or_panic(id.0.as_bytes())
99+
}
100+
}
101+
96102
///
97103
#[derive(Debug, Clone)]
98104
pub struct CommitInfo {
@@ -151,24 +157,35 @@ pub fn get_commit_info(
151157
) -> Result<CommitInfo> {
152158
scope_time!("get_commit_info");
153159

154-
let repo = repo(repo_path)?;
155-
let mailmap = repo.mailmap()?;
160+
let repo: gix::Repository =
161+
gix::ThreadSafeRepository::discover_with_environment_overrides(repo_path.gitpath())
162+
.map(Into::into)?;
163+
let mailmap = repo.open_mailmap();
164+
165+
let commit = repo.find_commit(*commit_id)?;
166+
let commit_ref = commit.decode()?;
167+
168+
let message = gix_get_message(&commit_ref, None);
156169

157-
let commit = repo.find_commit((*commit_id).into())?;
158-
let author = get_author_of_commit(&commit, &mailmap);
170+
let author = commit_ref.author();
171+
172+
let author = mailmap.try_resolve(author).map_or_else(
173+
|| author.name.into(),
174+
|signature| signature.name,
175+
);
159176

160177
Ok(CommitInfo {
161-
message: commit.message().unwrap_or("").into(),
162-
author: author.name().unwrap_or("<unknown>").into(),
163-
time: commit.time().seconds(),
164-
id: CommitId(commit.id()),
178+
message,
179+
author: author.to_string(),
180+
time: commit_ref.time().seconds,
181+
id: commit.id().detach().into(),
165182
})
166183
}
167184

168185
/// if `message_limit` is set the message will be
169186
/// limited to the first line and truncated to fit
170187
pub fn get_message(
171-
c: &Commit,
188+
c: &git2::Commit,
172189
message_limit: Option<usize>,
173190
) -> String {
174191
let msg = String::from_utf8_lossy(c.message_bytes());
@@ -183,6 +200,24 @@ pub fn get_message(
183200
)
184201
}
185202

203+
/// if `message_limit` is set the message will be
204+
/// limited to the first line and truncated to fit
205+
pub fn gix_get_message(
206+
commit_ref: &gix::objs::CommitRef,
207+
message_limit: Option<usize>,
208+
) -> String {
209+
let message = commit_ref.message.to_string();
210+
let message = message.trim();
211+
212+
message_limit.map_or_else(
213+
|| message.to_string(),
214+
|limit| {
215+
let message = message.lines().next().unwrap_or_default();
216+
message.unicode_truncate(limit).0.to_string()
217+
},
218+
)
219+
}
220+
186221
#[cfg(test)]
187222
mod tests {
188223
use super::get_commits_info;

0 commit comments

Comments
 (0)