Skip to content

Commit

Permalink
feat: support calling stg rebase without arguments
Browse files Browse the repository at this point in the history
When calling "git rebase" without a target commit argument then it will
try to look up remote tracking branch and rebase onto that. This change
makes "stg rebase" behave the same way. This slightly changes the
behavior of `stg rebase --interactive`: previously when called without
argument it would rebase onto the stack base, now it will rebase onto
the upstream branch.

Signed-off-by: Frank Benkstein <[email protected]>
  • Loading branch information
fbenkstein committed Dec 24, 2024
1 parent 8901396 commit a245639
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
27 changes: 23 additions & 4 deletions src/cmd/rebase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -99,15 +98,35 @@ 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::<SingleRevisionSpec>("committish") {
target_rev_spec.resolve(&repo, Some(&stack))?.commit
} else {
} 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=<remote>/<branch> {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!(
Expand Down
15 changes: 14 additions & 1 deletion t/t2200-rebase.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,20 @@ test_expect_success 'Rebase to same base message' '

test_expect_success 'Rebase without argument' '
test_must_fail stg rebase 2>out &&
grep -q "error: the following required arguments were not provided" 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

0 comments on commit a245639

Please sign in to comment.