Skip to content

Commit

Permalink
refactor!: Use git_object::commit::MessageRef::summary()… (#198)
Browse files Browse the repository at this point in the history
…which removes `commit::summary()` and alters the signature of
``
  • Loading branch information
Byron committed Sep 17, 2021
1 parent cd3fc99 commit 13e7c3a
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 120 deletions.
6 changes: 4 additions & 2 deletions git-object/tests/immutable/commit/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,10 @@ fn title_with_windows_separator_and_empty_body() {
pub mod summary {
use std::borrow::Cow;

use git_object::bstr::{BStr, ByteSlice};
use git_object::commit::MessageRef;
use git_object::{
bstr::{BStr, ByteSlice},
commit::MessageRef,
};

fn summary(input: &[u8]) -> Cow<'_, BStr> {
MessageRef::from_bytes(input).summary()
Expand Down
49 changes: 0 additions & 49 deletions git-repository/src/commit.rs
Original file line number Diff line number Diff line change
@@ -1,51 +1,2 @@
use std::borrow::Cow;

use bstr::{BStr, BString, ByteSlice, ByteVec};

/// An empty array of a type usable with the `git::easy` API to help declaring no parents should be used
pub const NO_PARENT_IDS: [git_hash::ObjectId; 0] = [];

/// Produce a short commit summary for the given `message`.
///
/// This means the following
///
/// * Take the subject line which is delimited by two newlines (\n\n)
/// * transform intermediate consecutive whitespace including \r into one space
///
/// The resulting summary will have folded whitespace before a newline into spaces and stopped that process
/// once two consecutive newlines are encountered.
pub fn summary(message: &BStr) -> Cow<'_, BStr> {
let message = message.trim();
match message.find_byte(b'\n') {
Some(mut pos) => {
let mut out = BString::default();
let mut previous_pos = None;
loop {
if let Some(previous_pos) = previous_pos {
if previous_pos + 1 == pos {
let len_after_trim = out.trim_end().len();
out.resize(len_after_trim, 0);
break out.into();
}
}
let message_to_newline = &message[previous_pos.map(|p| p + 1).unwrap_or(0)..pos];

if let Some(pos_before_whitespace) = message_to_newline.rfind_not_byteset(b"\t\n\x0C\r ") {
out.extend_from_slice(&message_to_newline[..pos_before_whitespace + 1]);
}
out.push_byte(b' ');
previous_pos = Some(pos);
match message.get(pos + 1..).and_then(|i| i.find_byte(b'\n')) {
Some(next_nl_pos) => pos += next_nl_pos + 1,
None => {
if let Some(slice) = message.get((pos + 1)..) {
out.extend_from_slice(slice);
}
break out.into();
}
}
}
}
None => message.as_bstr().into(),
}
}
6 changes: 5 additions & 1 deletion git-repository/src/easy/ext/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,11 @@ pub trait ObjectAccessExt: easy::Access + Sized {
log: LogChange {
mode: RefLog::AndReference,
force_create_reflog: false,
message: crate::reference::log::message("commit", &commit),
message: crate::reference::log::message(
"commit",
commit.message.as_ref(),
commit.parents.len(),
),
},
expected: match commit.parents.get(0).map(|p| Target::Peeled(*p)) {
Some(previous) => {
Expand Down
14 changes: 6 additions & 8 deletions git-repository/src/reference/log.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
use bstr::{BString, ByteSlice, ByteVec};
use git_object::Commit;
use bstr::{BStr, BString, ByteVec};
use git_object::commit::MessageRef;

use crate::commit;

/// Generate a message typical for git commit logs based on the given `operation`
pub fn message(operation: &str, commit: &Commit) -> BString {
/// Generate a message typical for git commit logs based on the given `operation`, commit `message` and `num_parents` of the commit.
pub fn message(operation: &str, message: &BStr, num_parents: usize) -> BString {
let mut out = BString::from(operation);
if let Some(commit_type) = commit_type_by_parents(commit.parents.len()) {
if let Some(commit_type) = commit_type_by_parents(num_parents) {
out.push_str(b" (");
out.extend_from_slice(commit_type.as_bytes());
out.push_byte(b')');
}
out.push_str(b": ");
out.extend_from_slice(&commit::summary(commit.message.as_bstr()));
out.extend_from_slice(&MessageRef::from_bytes(message).summary());
out
}

Expand Down
45 changes: 0 additions & 45 deletions git-repository/tests/commit/mod.rs

This file was deleted.

20 changes: 6 additions & 14 deletions git-repository/tests/reference/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,17 @@ mod log {

#[test]
fn message() {
let mut commit = git::objs::Commit {
tree: git::hash::ObjectId::empty_tree(git_hash::Kind::Sha1),
parents: Default::default(),
author: Default::default(),
committer: Default::default(),
encoding: None,
message: "the subject\n\nthe body".into(),
extra_headers: vec![],
};
assert_eq!(
git::reference::log::message("commit", &commit),
git::reference::log::message("commit", "the subject\n\nthe body".into(), 0),
"commit (initial): the subject"
);
commit.parents.push(git::hash::ObjectId::null_sha1());
assert_eq!(git::reference::log::message("other", &commit), "other: the subject");
assert_eq!(
git::reference::log::message("other", "the subject".into(), 1),
"other: the subject"
);

commit.parents.push(git::hash::ObjectId::null_sha1());
assert_eq!(
git::reference::log::message("rebase", &commit),
git::reference::log::message("rebase", "the subject".into(), 2),
"rebase (merge): the subject"
);
}
Expand Down
1 change: 0 additions & 1 deletion git-repository/tests/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ fn basic_rw_repo() -> crate::Result<(Easy, tempfile::TempDir)> {
easy_repo_rw("make_basic_repo.sh")
}

mod commit;
mod discover;
mod easy;
mod init;
Expand Down

0 comments on commit 13e7c3a

Please sign in to comment.