Skip to content

Commit 112a2e2

Browse files
fbenksteinjpgrayson
authored andcommitted
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 e232636 commit 112a2e2

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

src/cmd/rebase.rs

Lines changed: 29 additions & 9 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,36 @@ 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

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

110-
if !matches.get_flag("interactive") && target_commit.id == stack.base().id {
130+
if !interactive && target_commit.id == stack.base().id {
111131
print_info_message(
112132
matches,
113133
&format!(

t/t2200-rebase.sh

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,20 @@ 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+
git checkout master &&
50+
echo bar >>file2 &&
51+
git add file2 &&
52+
git commit -m c &&
53+
git remote add origin "file://$(pwd)" &&
54+
git fetch origin &&
55+
git checkout stack &&
56+
git branch --set-upstream-to=origin/master &&
57+
stg rebase 2>out &&
58+
grep "info: Rebasing to .*(master origin/master)" out &&
59+
test $(stg series --applied -c) = 1 &&
60+
grep bar file1 &&
61+
grep bar file2
4962
'
5063

5164
test_done

0 commit comments

Comments
 (0)