Skip to content

Commit a3fb158

Browse files
committed
fix(submit): do not force-push current branch when all branches are up-to-date
1 parent 0e15576 commit a3fb158

File tree

2 files changed

+69
-15
lines changed

2 files changed

+69
-15
lines changed

git-branchless/src/commands/submit.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -184,16 +184,18 @@ These remotes are available: {}",
184184
pushed_branches.extend(branches_to_push_names.iter());
185185
skipped_branches.extend(branches_to_skip_names.iter());
186186

187-
let mut args = vec!["push", "--force-with-lease", remote_name];
188-
args.extend(branches_to_push_names.iter());
189-
let exit_code = git_run_info.run(&effects, Some(event_tx_id), &args)?;
190-
if !exit_code.is_success() {
191-
writeln!(
192-
effects.get_output_stream(),
193-
"Failed to push branches: {}",
194-
branches_to_push_names.into_iter().join(", ")
195-
)?;
196-
return Ok(exit_code);
187+
if !pushed_branches.is_empty() {
188+
let mut args = vec!["push", "--force-with-lease", remote_name];
189+
args.extend(branches_to_push_names.iter());
190+
let exit_code = git_run_info.run(&effects, Some(event_tx_id), &args)?;
191+
if !exit_code.is_success() {
192+
writeln!(
193+
effects.get_output_stream(),
194+
"Failed to push branches: {}",
195+
branches_to_push_names.into_iter().join(", ")
196+
)?;
197+
return Ok(exit_code);
198+
}
197199
}
198200
progress.notify_progress_inc(branches.len());
199201
}

git-branchless/tests/command/test_submit.rs

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,9 @@ fn test_submit() -> eyre::Result<()> {
113113
{
114114
let (stdout, stderr) = cloned_repo.run(&["submit", "--create"])?;
115115
let stderr = redact_remotes(stderr);
116-
insta::assert_snapshot!(stderr, @r###"
117-
branchless: processing 1 update: remote branch origin/qux
118-
Everything up-to-date
119-
"###);
116+
insta::assert_snapshot!(stderr, @"");
120117
insta::assert_snapshot!(stdout, @r###"
121118
branchless: running command: <git-executable> fetch origin
122-
branchless: running command: <git-executable> push --force-with-lease origin
123119
Skipped 2 branches (already up-to-date): bar, qux
124120
"###);
125121
}
@@ -240,3 +236,59 @@ fn test_submit_existing_branch() -> eyre::Result<()> {
240236

241237
Ok(())
242238
}
239+
240+
#[test]
241+
fn test_submit_up_to_date_branch() -> eyre::Result<()> {
242+
let GitWrapperWithRemoteRepo {
243+
temp_dir: _guard,
244+
original_repo,
245+
cloned_repo,
246+
} = make_git_with_remote_repo()?;
247+
248+
if original_repo.get_version()? < MIN_VERSION {
249+
return Ok(());
250+
}
251+
252+
{
253+
original_repo.init_repo()?;
254+
original_repo.commit_file("test1", 1)?;
255+
original_repo.commit_file("test2", 2)?;
256+
original_repo.clone_repo_into(&cloned_repo, &[])?;
257+
cloned_repo.init_repo_with_options(&GitInitOptions {
258+
make_initial_commit: false,
259+
..Default::default()
260+
})?;
261+
}
262+
263+
cloned_repo.run(&["checkout", "-b", "feature"])?;
264+
cloned_repo.commit_file("test3", 3)?;
265+
266+
{
267+
let (stdout, stderr) = cloned_repo.run(&["submit", "--create", "feature"])?;
268+
let stderr = redact_remotes(stderr);
269+
insta::assert_snapshot!(stderr, @r###"
270+
branchless: processing 1 update: branch feature
271+
To: file://<remote>
272+
* [new branch] feature -> feature
273+
branchless: processing 1 update: remote branch origin/feature
274+
"###);
275+
insta::assert_snapshot!(stdout, @r###"
276+
branchless: running command: <git-executable> push --set-upstream origin feature
277+
branch 'feature' set up to track 'origin/feature'.
278+
Created 1 branch: feature
279+
"###);
280+
}
281+
282+
cloned_repo.detach_head()?;
283+
{
284+
let (stdout, stderr) = cloned_repo.run(&["submit", "feature"])?;
285+
let stderr = redact_remotes(stderr);
286+
insta::assert_snapshot!(stderr, @"");
287+
insta::assert_snapshot!(stdout, @r###"
288+
branchless: running command: <git-executable> fetch origin
289+
Skipped 1 branch (already up-to-date): feature
290+
"###);
291+
}
292+
293+
Ok(())
294+
}

0 commit comments

Comments
 (0)