Skip to content

Commit 13e7c3a

Browse files
committed
refactor!: Use git_object::commit::MessageRef::summary()… (#198)
…which removes `commit::summary()` and alters the signature of ``
1 parent cd3fc99 commit 13e7c3a

File tree

7 files changed

+21
-120
lines changed

7 files changed

+21
-120
lines changed

git-object/tests/immutable/commit/message.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,10 @@ fn title_with_windows_separator_and_empty_body() {
9595
pub mod summary {
9696
use std::borrow::Cow;
9797

98-
use git_object::bstr::{BStr, ByteSlice};
99-
use git_object::commit::MessageRef;
98+
use git_object::{
99+
bstr::{BStr, ByteSlice},
100+
commit::MessageRef,
101+
};
100102

101103
fn summary(input: &[u8]) -> Cow<'_, BStr> {
102104
MessageRef::from_bytes(input).summary()

git-repository/src/commit.rs

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,2 @@
1-
use std::borrow::Cow;
2-
3-
use bstr::{BStr, BString, ByteSlice, ByteVec};
4-
51
/// An empty array of a type usable with the `git::easy` API to help declaring no parents should be used
62
pub const NO_PARENT_IDS: [git_hash::ObjectId; 0] = [];
7-
8-
/// Produce a short commit summary for the given `message`.
9-
///
10-
/// This means the following
11-
///
12-
/// * Take the subject line which is delimited by two newlines (\n\n)
13-
/// * transform intermediate consecutive whitespace including \r into one space
14-
///
15-
/// The resulting summary will have folded whitespace before a newline into spaces and stopped that process
16-
/// once two consecutive newlines are encountered.
17-
pub fn summary(message: &BStr) -> Cow<'_, BStr> {
18-
let message = message.trim();
19-
match message.find_byte(b'\n') {
20-
Some(mut pos) => {
21-
let mut out = BString::default();
22-
let mut previous_pos = None;
23-
loop {
24-
if let Some(previous_pos) = previous_pos {
25-
if previous_pos + 1 == pos {
26-
let len_after_trim = out.trim_end().len();
27-
out.resize(len_after_trim, 0);
28-
break out.into();
29-
}
30-
}
31-
let message_to_newline = &message[previous_pos.map(|p| p + 1).unwrap_or(0)..pos];
32-
33-
if let Some(pos_before_whitespace) = message_to_newline.rfind_not_byteset(b"\t\n\x0C\r ") {
34-
out.extend_from_slice(&message_to_newline[..pos_before_whitespace + 1]);
35-
}
36-
out.push_byte(b' ');
37-
previous_pos = Some(pos);
38-
match message.get(pos + 1..).and_then(|i| i.find_byte(b'\n')) {
39-
Some(next_nl_pos) => pos += next_nl_pos + 1,
40-
None => {
41-
if let Some(slice) = message.get((pos + 1)..) {
42-
out.extend_from_slice(slice);
43-
}
44-
break out.into();
45-
}
46-
}
47-
}
48-
}
49-
None => message.as_bstr().into(),
50-
}
51-
}

git-repository/src/easy/ext/object.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,11 @@ pub trait ObjectAccessExt: easy::Access + Sized {
157157
log: LogChange {
158158
mode: RefLog::AndReference,
159159
force_create_reflog: false,
160-
message: crate::reference::log::message("commit", &commit),
160+
message: crate::reference::log::message(
161+
"commit",
162+
commit.message.as_ref(),
163+
commit.parents.len(),
164+
),
161165
},
162166
expected: match commit.parents.get(0).map(|p| Target::Peeled(*p)) {
163167
Some(previous) => {

git-repository/src/reference/log.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
use bstr::{BString, ByteSlice, ByteVec};
2-
use git_object::Commit;
1+
use bstr::{BStr, BString, ByteVec};
2+
use git_object::commit::MessageRef;
33

4-
use crate::commit;
5-
6-
/// Generate a message typical for git commit logs based on the given `operation`
7-
pub fn message(operation: &str, commit: &Commit) -> BString {
4+
/// Generate a message typical for git commit logs based on the given `operation`, commit `message` and `num_parents` of the commit.
5+
pub fn message(operation: &str, message: &BStr, num_parents: usize) -> BString {
86
let mut out = BString::from(operation);
9-
if let Some(commit_type) = commit_type_by_parents(commit.parents.len()) {
7+
if let Some(commit_type) = commit_type_by_parents(num_parents) {
108
out.push_str(b" (");
119
out.extend_from_slice(commit_type.as_bytes());
1210
out.push_byte(b')');
1311
}
1412
out.push_str(b": ");
15-
out.extend_from_slice(&commit::summary(commit.message.as_bstr()));
13+
out.extend_from_slice(&MessageRef::from_bytes(message).summary());
1614
out
1715
}
1816

git-repository/tests/commit/mod.rs

Lines changed: 0 additions & 45 deletions
This file was deleted.

git-repository/tests/reference/mod.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,17 @@ mod log {
33

44
#[test]
55
fn message() {
6-
let mut commit = git::objs::Commit {
7-
tree: git::hash::ObjectId::empty_tree(git_hash::Kind::Sha1),
8-
parents: Default::default(),
9-
author: Default::default(),
10-
committer: Default::default(),
11-
encoding: None,
12-
message: "the subject\n\nthe body".into(),
13-
extra_headers: vec![],
14-
};
156
assert_eq!(
16-
git::reference::log::message("commit", &commit),
7+
git::reference::log::message("commit", "the subject\n\nthe body".into(), 0),
178
"commit (initial): the subject"
189
);
19-
commit.parents.push(git::hash::ObjectId::null_sha1());
20-
assert_eq!(git::reference::log::message("other", &commit), "other: the subject");
10+
assert_eq!(
11+
git::reference::log::message("other", "the subject".into(), 1),
12+
"other: the subject"
13+
);
2114

22-
commit.parents.push(git::hash::ObjectId::null_sha1());
2315
assert_eq!(
24-
git::reference::log::message("rebase", &commit),
16+
git::reference::log::message("rebase", "the subject".into(), 2),
2517
"rebase (merge): the subject"
2618
);
2719
}

git-repository/tests/repo.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ fn basic_rw_repo() -> crate::Result<(Easy, tempfile::TempDir)> {
2424
easy_repo_rw("make_basic_repo.sh")
2525
}
2626

27-
mod commit;
2827
mod discover;
2928
mod easy;
3029
mod init;

0 commit comments

Comments
 (0)