Skip to content

Commit b7fc2e2

Browse files
committed
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. Note that this introduces a slight discrepancy with "stg rebase --interactive": in this case it will still use the stack base instead of the remote tracking branch. Signed-off-by: Frank Benkstein <[email protected]>
1 parent 5c35432 commit b7fc2e2

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
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!(

0 commit comments

Comments
 (0)