Skip to content

Commit 8241322

Browse files
committed
feat: support calling stg rebase without arguments
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]>
1 parent 8901396 commit 8241322

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

src/cmd/rebase.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ fn make() -> clap::Command {
4949
.arg(
5050
Arg::new("committish")
5151
.help("New base commit for the stack")
52-
.value_parser(clap::value_parser!(SingleRevisionSpec))
53-
.required_unless_present("interactive"),
52+
.value_parser(clap::value_parser!(SingleRevisionSpec)),
5453
)
5554
.arg(
5655
Arg::new("interactive")
@@ -99,15 +98,35 @@ fn run(matches: &ArgMatches) -> Result<()> {
9998
let branch_name = stack.get_branch_name().to_string();
10099
let allow_push_conflicts = argset::resolve_allow_push_conflicts(&config, matches);
101100
let committer_date_is_author_date = matches.get_flag("committer-date-is-author-date");
101+
let interactive = matches.get_flag("interactive");
102102

103103
let target_commit =
104104
if let Some(target_rev_spec) = matches.get_one::<SingleRevisionSpec>("committish") {
105105
target_rev_spec.resolve(&repo, Some(&stack))?.commit
106-
} else {
106+
} else if let Some(remote_ref) = repo
107+
.branch_remote_ref_name(stack.get_branch_refname(), gix::remote::Direction::Fetch)
108+
.transpose()?
109+
{
110+
let id = repo.rev_parse_single(remote_ref.as_bstr())?;
111+
id.object()?.into_commit().into()
112+
} else if interactive {
107113
stack.base().clone()
114+
} else {
115+
print_info_message(
116+
matches,
117+
&format!(
118+
"if you wish to set tracking information for this branch you can do so with:
119+
120+
git branch --set-upstream-to=<remote>/<branch> {branch_name}
121+
"
122+
),
123+
);
124+
return Err(anyhow!(
125+
"there is no tracking information for the current branch"
126+
));
108127
};
109128

110-
if !matches.get_flag("interactive") && target_commit.id == stack.base().id {
129+
if !interactive && target_commit.id == stack.base().id {
111130
print_info_message(
112131
matches,
113132
&format!(

t/t2200-rebase.sh

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,22 @@ test_expect_success 'Rebase to same base message' '
4545

4646
test_expect_success 'Rebase without argument' '
4747
test_must_fail stg rebase 2>out &&
48-
grep -q "error: the following required arguments were not provided" out
48+
grep -q "error: there is no tracking information for the current branch" out &&
49+
sed "s/^/out: /" out &&
50+
git checkout master &&
51+
echo bar >>file2 &&
52+
git add file2 &&
53+
git commit -m c &&
54+
git remote add origin "file://$(pwd)" &&
55+
git fetch origin &&
56+
git checkout stack &&
57+
git branch --set-upstream-to=origin/master &&
58+
stg rebase 2>out &&
59+
sed "s/^/out2: /" out &&
60+
grep "info: Rebasing to .*(master origin/master)" out &&
61+
test $(stg series --applied -c) = 1 &&
62+
grep bar file1 &&
63+
grep bar file2
4964
'
5065

5166
test_done

0 commit comments

Comments
 (0)