Skip to content

Commit c39b3ae

Browse files
feat(submit): add --dry-run flag
1 parent 5871499 commit c39b3ae

File tree

4 files changed

+47
-7
lines changed

4 files changed

+47
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
## [Unreleased] - ReleaseDate
1111

12+
### Added
13+
14+
- (#1129) Added a `--dry-run` option to `git submit` to report what would be submitted without actually doing so.
15+
1216
## [v0.8.0] - 2023-08-27
1317

1418
### Added

git-branchless-opts/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,11 @@ pub struct SubmitArgs {
396396
/// An optional message to include with the create or update operation.
397397
#[clap(short = 'm', long = "message")]
398398
pub message: Option<String>,
399+
400+
/// Don't push or create anything. Instead, report what would be pushed or
401+
/// created. (Still triggers a fetch.)
402+
#[clap(short = 'n', long = "dry-run")]
403+
pub dry_run: bool,
399404
}
400405

401406
/// Run a command on each commit in a given set and aggregate the results.

git-branchless-submit/src/lib.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ pub fn command_main(ctx: CommandContext, args: SubmitArgs) -> EyreExitOr<()> {
158158
resolve_revset_options,
159159
forge,
160160
message,
161+
dry_run,
161162
} = args;
162163
submit(
163164
&effects,
@@ -169,6 +170,7 @@ pub fn command_main(ctx: CommandContext, args: SubmitArgs) -> EyreExitOr<()> {
169170
strategy,
170171
forge,
171172
message,
173+
dry_run,
172174
)
173175
}
174176

@@ -182,6 +184,7 @@ fn submit(
182184
execution_strategy: Option<TestExecutionStrategy>,
183185
forge_kind: Option<ForgeKind>,
184186
message: Option<String>,
187+
dry_run: bool,
185188
) -> EyreExitOr<()> {
186189
let repo = Repo::from_current_dir()?;
187190
let conn = repo.get_db_conn()?;
@@ -329,6 +332,8 @@ fn submit(
329332
.collect();
330333
if unsubmitted_commits.is_empty() {
331334
Default::default()
335+
} else if create && dry_run {
336+
(unsubmitted_branches, Default::default())
332337
} else if create {
333338
let create_statuses =
334339
try_exit_code!(forge.create(unsubmitted_commits, &submit_options)?);
@@ -371,14 +376,17 @@ fn submit(
371376
.flat_map(|(_commit_oid, commit_status)| commit_status.local_branch_name.clone())
372377
.collect();
373378

374-
try_exit_code!(forge.update(commits_to_update, &submit_options)?);
379+
if !dry_run {
380+
try_exit_code!(forge.update(commits_to_update, &submit_options)?);
381+
}
375382
(updated_branch_names, skipped_branch_names)
376383
};
377384

378385
if !created_branches.is_empty() {
379386
writeln!(
380387
effects.get_output_stream(),
381-
"Created {}: {}",
388+
"{} {}: {}",
389+
if dry_run { "Would create" } else { "Created" },
382390
Pluralize {
383391
determiner: None,
384392
amount: created_branches.len(),
@@ -400,7 +408,8 @@ fn submit(
400408
if !updated_branch_names.is_empty() {
401409
writeln!(
402410
effects.get_output_stream(),
403-
"Pushed {}: {}",
411+
"{} {}: {}",
412+
if dry_run { "Would push" } else { "Pushed" },
404413
Pluralize {
405414
determiner: None,
406415
amount: updated_branch_names.len(),
@@ -422,7 +431,8 @@ fn submit(
422431
if !skipped_branch_names.is_empty() {
423432
writeln!(
424433
effects.get_output_stream(),
425-
"Skipped {} (already up-to-date): {}",
434+
"{} {} (already up-to-date): {}",
435+
if dry_run { "Would skip" } else { "Skipped" },
426436
Pluralize {
427437
determiner: None,
428438
amount: skipped_branch_names.len(),
@@ -444,7 +454,8 @@ fn submit(
444454
if !uncreated_branches.is_empty() {
445455
writeln!(
446456
effects.get_output_stream(),
447-
"Skipped {} (not yet on remote): {}",
457+
"{} {} (not yet on remote): {}",
458+
if dry_run { "Would skip" } else { "Skipped" },
448459
Pluralize {
449460
determiner: None,
450461
amount: uncreated_branches.len(),
@@ -465,8 +476,10 @@ fn submit(
465476
writeln!(
466477
effects.get_output_stream(),
467478
"\
468-
These branches were skipped because they were not already associated with a remote repository. To
469-
create and push them, retry this operation with the --create option."
479+
These branches {} skipped because they {} not already associated with a remote repository. To
480+
create and push them, retry this operation with the --create option.",
481+
if dry_run { "would be" } else { "were" },
482+
if dry_run { "are" } else { "were" },
470483
)?;
471484
}
472485

git-branchless-submit/tests/test_submit.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,24 @@ fn test_submit() -> eyre::Result<()> {
6464
"###);
6565
}
6666

67+
{
68+
let (stdout, stderr) = cloned_repo.run(&["submit", "--dry-run"])?;
69+
insta::assert_snapshot!(stderr, @"");
70+
insta::assert_snapshot!(stdout, @r###"
71+
Would skip 2 branches (not yet on remote): bar, qux
72+
These branches would be skipped because they are not already associated with a remote repository. To
73+
create and push them, retry this operation with the --create option.
74+
"###);
75+
}
76+
77+
{
78+
let (stdout, stderr) = cloned_repo.run(&["submit", "--create", "--dry-run"])?;
79+
insta::assert_snapshot!(stderr, @"");
80+
insta::assert_snapshot!(stdout, @r###"
81+
Would create 2 branches: bar, qux
82+
"###);
83+
}
84+
6785
{
6886
let (stdout, stderr) = cloned_repo.run(&["submit", "--create"])?;
6987
let stderr = redact_remotes(stderr);

0 commit comments

Comments
 (0)