Skip to content

Commit 30e7cc7

Browse files
authored
Merge pull request #176 from arielf212/issue-129
issue #129 - Adds new -m option for adding commit message body to generated fixups
2 parents 98ff3ac + 770be92 commit 30e7cc7

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

Documentation/git-absorb.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ OPTIONS
9090
--base <base>::
9191
Use this commit as the base of the absorb stack
9292

93+
-m <MESSAGE>::
94+
--message <MESSAGE>::
95+
A simple commit message body that will be used for **all** generated fixup commits.
96+
9397
--gen-completions <SHELL>::
9498
Generate completions
9599
[possible values: bash, fish, nushell, zsh, powershell, elvish]

src/lib.rs

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub struct Config<'a> {
1919
pub rebase_options: &'a Vec<&'a str>,
2020
pub whole_file: bool,
2121
pub one_fixup_per_commit: bool,
22+
pub message: Option<&'a str>,
2223
}
2324

2425
pub fn run(logger: &slog::Logger, config: &Config) -> Result<()> {
@@ -319,11 +320,17 @@ fn run_with_repo(logger: &slog::Logger, config: &Config, repo: &git2::Repository
319320
.stats()?;
320321
if !config.dry_run {
321322
head_tree = new_head_tree;
323+
let mut message = format!("fixup! {}\n", dest_commit_locator);
324+
if let Some(m) = config.message.filter(|m| !m.is_empty()) {
325+
message.push('\n');
326+
message.push_str(m);
327+
message.push('\n');
328+
};
322329
head_commit = repo.find_commit(repo.commit(
323330
Some("HEAD"),
324331
&signature,
325332
&signature,
326-
&format!("fixup! {}\n", dest_commit_locator),
333+
&message,
327334
&head_tree,
328335
&[&head_commit],
329336
)?)?;
@@ -978,6 +985,59 @@ mod tests {
978985
assert_eq!(actual_msg, expected_msg);
979986
}
980987

988+
#[test]
989+
fn fixup_message_option_left_out_sets_only_summary() {
990+
let ctx = repo_utils::prepare_and_stage();
991+
992+
// run 'git-absorb'
993+
let drain = slog::Discard;
994+
let logger = slog::Logger::root(drain, o!());
995+
run_with_repo(&logger, &DEFAULT_CONFIG, &ctx.repo).unwrap();
996+
assert!(nothing_left_in_index(&ctx.repo).unwrap());
997+
998+
let mut revwalk = ctx.repo.revwalk().unwrap();
999+
revwalk.push_head().unwrap();
1000+
1001+
let oids: Vec<git2::Oid> = revwalk.by_ref().collect::<Result<Vec<_>, _>>().unwrap();
1002+
assert_eq!(oids.len(), 3);
1003+
1004+
let fixup_commit = ctx.repo.find_commit(oids[0]).unwrap();
1005+
let fixed_up_commit = ctx.repo.find_commit(*oids.last().unwrap()).unwrap();
1006+
let actual_msg = fixup_commit.message().unwrap();
1007+
let expected_msg = fixed_up_commit.message().unwrap();
1008+
let expected_msg = format!("fixup! {}\n", expected_msg);
1009+
assert_eq!(actual_msg, expected_msg);
1010+
}
1011+
1012+
#[test]
1013+
fn fixup_message_option_provided_sets_message() {
1014+
let ctx = repo_utils::prepare_and_stage();
1015+
1016+
// run 'git-absorb'
1017+
let drain = slog::Discard;
1018+
let logger = slog::Logger::root(drain, o!());
1019+
let fixup_message_body = "git-absorb is my favorite git tool!";
1020+
let config = Config {
1021+
message: Some(fixup_message_body),
1022+
..DEFAULT_CONFIG
1023+
};
1024+
run_with_repo(&logger, &config, &ctx.repo).unwrap();
1025+
assert!(nothing_left_in_index(&ctx.repo).unwrap());
1026+
1027+
let mut revwalk = ctx.repo.revwalk().unwrap();
1028+
revwalk.push_head().unwrap();
1029+
1030+
let oids: Vec<git2::Oid> = revwalk.by_ref().collect::<Result<Vec<_>, _>>().unwrap();
1031+
assert_eq!(oids.len(), 3);
1032+
1033+
let fixup_commit = ctx.repo.find_commit(oids[0]).unwrap();
1034+
let fixed_up_commit = ctx.repo.find_commit(*oids.last().unwrap()).unwrap();
1035+
let actual_msg = fixup_commit.message().unwrap();
1036+
let expected_msg = fixed_up_commit.message().unwrap();
1037+
let expected_msg = format!("fixup! {}\n\n{}\n", expected_msg, fixup_message_body);
1038+
assert_eq!(actual_msg, expected_msg);
1039+
}
1040+
9811041
const DEFAULT_CONFIG: Config = Config {
9821042
dry_run: false,
9831043
force_author: false,
@@ -987,5 +1047,6 @@ mod tests {
9871047
rebase_options: &Vec::new(),
9881048
whole_file: false,
9891049
one_fixup_per_commit: false,
1050+
message: None,
9901051
};
9911052
}

src/main.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ struct Cli {
4444
/// Only generate one fixup per commit
4545
#[clap(long, short = 'F')]
4646
one_fixup_per_commit: bool,
47+
/// Commit message body that is given to all fixup commits
48+
#[clap(long, short)]
49+
message: Option<String>,
4750
}
4851

4952
fn main() {
@@ -59,6 +62,7 @@ fn main() {
5962
gen_completions,
6063
whole_file,
6164
one_fixup_per_commit,
65+
message,
6266
} = Cli::parse();
6367

6468
if let Some(shell) = gen_completions {
@@ -109,6 +113,7 @@ fn main() {
109113
rebase_options: &rebase_options,
110114
whole_file,
111115
one_fixup_per_commit,
116+
message: message.as_deref(),
112117
},
113118
) {
114119
crit!(logger, "absorb failed"; "err" => e.to_string());

0 commit comments

Comments
 (0)