Skip to content

Commit 6498777

Browse files
temp(split): add --before flag to insert extracted changes before split commit
1 parent 3fe59dc commit 6498777

File tree

4 files changed

+73
-1
lines changed

4 files changed

+73
-1
lines changed

git-branchless-opts/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,10 @@ pub enum Command {
662662
#[clap(value_parser, required = true)]
663663
files: Vec<String>,
664664

665+
/// Insert the extracted commit before (as a parent of) the split commit.
666+
#[clap(action, short = 'b', long)]
667+
before: bool,
668+
665669
/// Restack any descendents onto the split commit, not the extracted commit.
666670
#[clap(action, short = 'd', long)]
667671
detach: bool,

git-branchless/src/commands/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ fn command_main(ctx: CommandContext, opts: Opts) -> EyreExitOr<()> {
181181
},
182182

183183
Command::Split {
184+
before,
184185
detach,
185186
discard,
186187
files,
@@ -192,6 +193,7 @@ fn command_main(ctx: CommandContext, opts: Opts) -> EyreExitOr<()> {
192193
revset,
193194
&resolve_revset_options,
194195
files,
196+
before,
195197
detach,
196198
discard,
197199
&move_options,

git-branchless/src/commands/split.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ pub fn split(
4141
revset: Revset,
4242
resolve_revset_options: &ResolveRevsetOptions,
4343
files_to_extract: Vec<String>,
44+
before: bool,
4445
detach: bool,
4546
discard: bool,
4647
move_options: &MoveOptions,
@@ -266,13 +267,18 @@ pub fn split(
266267
reuse_parent_tree_if_possible: true,
267268
},
268269
)?;
270+
let parents = if before {
271+
split_commit.get_parents()
272+
} else {
273+
vec![split_commit]
274+
};
269275
let extracted_commit_oid = repo.create_commit(
270276
None,
271277
&commit_to_split.get_author(),
272278
&commit_to_split.get_committer(),
273279
format!("temp(split): {message}").as_str(),
274280
&extracted_tree,
275-
vec![&split_commit],
281+
parents.iter().collect(),
276282
)?;
277283

278284
// see git-branchless/src/commands/amend.rs:172

git-branchless/tests/test_split.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,66 @@ fn test_split_discard() -> eyre::Result<()> {
554554

555555
Ok(())
556556
}
557+
#[test]
558+
fn test_split_insert_before() -> eyre::Result<()> {
559+
let git = make_git()?;
560+
git.init_repo()?;
561+
git.detach_head()?;
562+
563+
git.write_file_txt("test1", "contents1")?;
564+
git.write_file_txt("test2", "contents2")?;
565+
git.write_file_txt("test3", "contents3")?;
566+
git.run(&["add", "."])?;
567+
git.run(&["commit", "-m", "first commit"])?;
568+
569+
git.commit_file("test3", 1)?;
570+
571+
{
572+
let (stdout, _stderr) = git.branchless("smartlog", &[])?;
573+
insta::assert_snapshot!(stdout, @r###"
574+
O f777ecc (master) create initial.txt
575+
|
576+
o e48cdc5 first commit
577+
|
578+
@ 3d220e0 create test3.txt
579+
"###);
580+
}
581+
582+
{
583+
let (stdout, _stderr) = git.branchless("split", &["HEAD~", "test2.txt", "--before"])?;
584+
insta::assert_snapshot!(&stdout, @r###"
585+
Attempting rebase in-memory...
586+
[1/1] Committed as: f88fbe5 create test3.txt
587+
branchless: processing 1 rewritten commit
588+
branchless: running command: <git-executable> checkout f88fbe5901493ffe1c669cdb8aa5f056dc0bb605
589+
In-memory rebase succeeded.
590+
O f777ecc (master) create initial.txt
591+
|
592+
o 01523cc temp(split): test2.txt (+1)
593+
|
594+
o 2932db7 first commit
595+
|
596+
@ f88fbe5 create test3.txt
597+
"###);
598+
}
599+
600+
{
601+
let (stdout, _stderr) = git.run(&["show", "--pretty=format:", "--stat", "HEAD~"])?;
602+
insta::assert_snapshot!(&stdout, @"
603+
test1.txt | 1 +
604+
test3.txt | 1 +
605+
2 files changed, 2 insertions(+)
606+
");
607+
608+
let (stdout, _stderr) = git.run(&["show", "--pretty=format:", "--stat", "HEAD~2"])?;
609+
insta::assert_snapshot!(&stdout, @"
610+
test2.txt | 1 +
611+
1 file changed, 1 insertion(+)
612+
");
613+
}
614+
615+
Ok(())
616+
}
557617

558618
#[test]
559619
fn test_split_undo_works() -> eyre::Result<()> {

0 commit comments

Comments
 (0)