diff --git a/src/cmd/rebase.rs b/src/cmd/rebase.rs index 1f87f624..82a3a213 100644 --- a/src/cmd/rebase.rs +++ b/src/cmd/rebase.rs @@ -49,8 +49,7 @@ fn make() -> clap::Command { .arg( Arg::new("committish") .help("New base commit for the stack") - .value_parser(clap::value_parser!(SingleRevisionSpec)) - .required_unless_present("interactive"), + .value_parser(clap::value_parser!(SingleRevisionSpec)), ) .arg( Arg::new("interactive") @@ -99,15 +98,36 @@ fn run(matches: &ArgMatches) -> Result<()> { let branch_name = stack.get_branch_name().to_string(); let allow_push_conflicts = argset::resolve_allow_push_conflicts(&config, matches); let committer_date_is_author_date = matches.get_flag("committer-date-is-author-date"); + let interactive = matches.get_flag("interactive"); - let target_commit = - if let Some(target_rev_spec) = matches.get_one::("committish") { - target_rev_spec.resolve(&repo, Some(&stack))?.commit - } else { - stack.base().clone() - }; + let target_commit = if let Some(target_rev_spec) = + matches.get_one::("committish") + { + target_rev_spec.resolve(&repo, Some(&stack))?.commit + } else if let Some(remote_ref) = repo + .branch_remote_tracking_ref_name(stack.get_branch_refname(), gix::remote::Direction::Fetch) + .transpose()? + { + let id = repo.rev_parse_single(remote_ref.as_bstr())?; + id.object()?.into_commit().into() + } else if interactive { + stack.base().clone() + } else { + print_info_message( + matches, + &format!( + "if you wish to set tracking information for this branch you can do so with: + + git branch --set-upstream-to=/ {branch_name} +" + ), + ); + return Err(anyhow!( + "there is no tracking information for the current branch" + )); + }; - if !matches.get_flag("interactive") && target_commit.id == stack.base().id { + if !interactive && target_commit.id == stack.base().id { print_info_message( matches, &format!( diff --git a/t/t2200-rebase.sh b/t/t2200-rebase.sh index 53d0ed0f..7161794a 100755 --- a/t/t2200-rebase.sh +++ b/t/t2200-rebase.sh @@ -43,4 +43,22 @@ test_expect_success 'Rebase to same base message' ' grep "info: Already based on .*(master)" out ' +test_expect_success 'Rebase without argument' ' + test_must_fail stg rebase 2>out && + grep -q "error: there is no tracking information for the current branch" out && + git checkout master && + echo bar >>file2 && + git add file2 && + git commit -m c && + git remote add origin "file://$(pwd)" && + git fetch origin && + git checkout stack && + git branch --set-upstream-to=origin/master && + stg rebase 2>out && + grep "info: Rebasing to .*(master origin/master)" out && + test $(stg series --applied -c) = 1 && + grep bar file1 && + grep bar file2 +' + test_done diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh index 90034dfb..621cd9bf 100644 --- a/t/test-lib-functions.sh +++ b/t/test-lib-functions.sh @@ -1024,7 +1024,7 @@ test_must_fail_acceptable () { fi case "$1" in - git|__git*|scalar|test-tool|fake_tool|test_terminal) + git|__git*|scalar|test-tool|fake_tool|test_terminal|stg) return 0 ;; *)