Skip to content

Commit

Permalink
use git2::message_prettify to clean up the reworded message
Browse files Browse the repository at this point in the history
  • Loading branch information
claytonrcarter committed Mar 25, 2022
1 parent fa0ccd5 commit 440b6b7
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 20 deletions.
11 changes: 7 additions & 4 deletions src/commands/reword.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use eden_dag::DagAlgorithm;
use eyre::Context;
use tracing::{instrument, warn};

use crate::core::config::get_restack_preserve_timestamps;
use crate::core::config::{get_comment_char, get_restack_preserve_timestamps};
use crate::core::dag::{resolve_commits, CommitSet, Dag, ResolveCommitsResult};
use crate::core::effects::Effects;
use crate::core::eventlog::{EventLogDb, EventReplayer};
Expand Down Expand Up @@ -55,7 +55,7 @@ pub fn reword(
None => return Ok(1),
};

let messages = match build_messages(&messages, &commits)? {
let messages = match build_messages(&messages, &commits, get_comment_char(&repo)?)? {
BuildRewordMessageResult::Succeeded { messages } => messages,
BuildRewordMessageResult::IdenticalMessage => {
writeln!(
Expand Down Expand Up @@ -256,6 +256,7 @@ pub enum BuildRewordMessageResult {
fn build_messages(
messages: &[String],
commits: &[Commit],
comment_char: Option<u8>,
) -> eyre::Result<BuildRewordMessageResult> {
let message = messages.join("\n\n").trim().to_string();

Expand Down Expand Up @@ -301,9 +302,11 @@ fn build_messages(
message
};

// TODO process the message: remove comment lines, trim, etc
// clean up the commit message: remove comment lines, finish w/ a newline, etc
// TODO ask if this should be here or if it should be moved to the git module per project wiki
let message = git2::message_prettify(message, comment_char)?;

if message.is_empty() {
if message.trim().is_empty() {
return Ok(BuildRewordMessageResult::EmptyMessage);
}

Expand Down
12 changes: 12 additions & 0 deletions src/core/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ pub fn get_main_branch_name(repo: &Repo) -> eyre::Result<String> {
Ok(main_branch_name)
}

/// Get the default comment character.
#[instrument]
pub fn get_comment_char(repo: &Repo) -> eyre::Result<Option<u8>> {
let config = repo.get_readonly_config()?;
let comment_char: Option<String> = config.get("core.commentChar")?;
let comment_char = match comment_char {
Some(c) => Some(c.chars().next().unwrap() as u8),
None => git2::DEFAULT_COMMENT_CHAR,
};
Ok(comment_char)
}

/// Get the default init branch name.
#[instrument]
pub fn get_default_branch_name(repo: &Repo) -> eyre::Result<Option<String>> {
Expand Down
2 changes: 1 addition & 1 deletion src/core/rewrite/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -976,7 +976,7 @@ pub enum ExecuteRebasePlanResult {
/// The rebase operation succeeded.
Succeeded {
/// Mapping from old OID to new/rewritten OID. Will always be empty for on disk rebases.
// FIXME should this be an Option?
// TODO should this be an Option?
rewritten_oids: HashMap<NonZeroOid, MaybeZeroOid>,
},

Expand Down
62 changes: 47 additions & 15 deletions tests/command/test_reword.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn test_reword_head() -> eyre::Result<()> {
:
O 62fc20d2 (test1) create test1.txt
|
@ b38039bd (> master) foo
@ c1f5400a (> master) foo
"###);

Ok(())
Expand Down Expand Up @@ -59,9 +59,9 @@ fn test_reword_current_commit_not_head() -> eyre::Result<()> {
let (stdout, _stderr) = git.run(&["smartlog"])?;
insta::assert_snapshot!(stdout, @r###"
:
@ 5b4452da (test1) foo
@ a6f88684 (test1) foo
|
O 341c36ed (master) create test2.txt
O 5207ad50 (master) create test2.txt
"###);

Ok(())
Expand All @@ -87,10 +87,42 @@ fn test_reword_with_multiple_messages() -> eyre::Result<()> {

let (stdout, _stderr) = git.run(&["log", "-n", "1", "--format=%h%n%B"])?;
insta::assert_snapshot!(stdout, @r###"
961064b
34ae21e
foo
bar
"###);

Ok(())
}

#[test]
fn test_reword_removes_comment_lines_from_messages() -> eyre::Result<()> {
let git = make_git()?;

if !git.supports_committer_date_is_author_date()? {
return Ok(());
}
git.init_repo()?;
git.commit_file("test1", 1)?;

let (stdout, _stderr) = git.run(&["log", "-n", "1", "--format=%h%n%B"])?;
insta::assert_snapshot!(stdout, @r###"
62fc20d
create test1.txt
"###);

// try adding several messages that start w/ '#'
git.run(&["reword", "-m", "foo", "-m", "# bar", "-m", "#", "-m", "buz"])?;

// confirm the '#' messages aren't present
let (stdout, _stderr) = git.run(&["log", "-n", "1", "--format=%h%n%B"])?;
insta::assert_snapshot!(stdout, @r###"
2666020
foo
buz
"###);

Ok(())
Expand Down Expand Up @@ -121,9 +153,9 @@ fn test_reword_non_head_commit() -> eyre::Result<()> {
let (stdout, _stderr) = git.run(&["smartlog"])?;
insta::assert_snapshot!(stdout, @r###"
:
O 09b42f44 (test1) bar
O 8d4a670d (test1) bar
|
@ 8ab8b750 (> master) create test2.txt
@ 8f7f70ea (> master) create test2.txt
"###);

Ok(())
Expand Down Expand Up @@ -154,9 +186,9 @@ fn test_reword_multiple_commits_on_same_branch() -> eyre::Result<()> {
let (stdout, _stderr) = git.run(&["smartlog"])?;
insta::assert_snapshot!(stdout, @r###"
:
O 5b4452da (test1) foo
O a6f88684 (test1) foo
|
@ 12217a2f (> master) foo
@ e2308b39 (> master) foo
"###);

Ok(())
Expand Down Expand Up @@ -198,11 +230,11 @@ fn test_reword_tree() -> eyre::Result<()> {
:
O 96d1c37a (master) create test2.txt
|
o 4de8ed1a foo
o 929b68d8 foo
|\
| o 93c8ffd1 create test4.txt
| o a3679359 create test4.txt
|
@ 92c4833c create test5.txt
@ 38f9ce96 create test5.txt
"###);

Ok(())
Expand Down Expand Up @@ -252,13 +284,13 @@ fn test_reword_across_branches() -> eyre::Result<()> {
:
O 62fc20d2 (master) create test1.txt
|\
| o b38039bd foo
| o c1f5400a foo
| |
| o 0c46d151 create test3.txt
| o 1c9ad631 create test3.txt
|
o b376ed9e foo
o 3c442fc0 foo
|
@ a9fc71df create test5.txt
@ 8648fbd2 create test5.txt
"###);

Ok(())
Expand Down

0 comments on commit 440b6b7

Please sign in to comment.