Skip to content

Commit 8f5986d

Browse files
prertikgitster
authored andcommitted
builtin rebase: optionally auto-detect the upstream
The `git rebase` command, when called without the `<upstream>` command-line argument, automatically looks for the upstream branch configured for the current branch. With this commit, the builtin rebase learned that trick, too. Signed-off-by: Pratik Karki <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9dba809 commit 8f5986d

File tree

1 file changed

+41
-3
lines changed

1 file changed

+41
-3
lines changed

builtin/rebase.c

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,36 @@ static int parse_opt_interactive(const struct option *opt, const char *arg,
622622
return 0;
623623
}
624624

625+
static void NORETURN error_on_missing_default_upstream(void)
626+
{
627+
struct branch *current_branch = branch_get(NULL);
628+
629+
printf(_("%s\n"
630+
"Please specify which branch you want to rebase against.\n"
631+
"See git-rebase(1) for details.\n"
632+
"\n"
633+
" git rebase '<branch>'\n"
634+
"\n"),
635+
current_branch ? _("There is no tracking information for "
636+
"the current branch.") :
637+
_("You are not currently on a branch."));
638+
639+
if (current_branch) {
640+
const char *remote = current_branch->remote_name;
641+
642+
if (!remote)
643+
remote = _("<remote>");
644+
645+
printf(_("If you wish to set tracking information for this "
646+
"branch you can do so with:\n"
647+
"\n"
648+
" git branch --set-upstream-to=%s/<branch> %s\n"
649+
"\n"),
650+
remote, current_branch->name);
651+
}
652+
exit(1);
653+
}
654+
625655
int cmd_rebase(int argc, const char **argv, const char *prefix)
626656
{
627657
struct rebase_options options = {
@@ -1057,9 +1087,17 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
10571087
}
10581088

10591089
if (!options.root) {
1060-
if (argc < 1)
1061-
die("TODO: handle @{upstream}");
1062-
else {
1090+
if (argc < 1) {
1091+
struct branch *branch;
1092+
1093+
branch = branch_get(NULL);
1094+
options.upstream_name = branch_get_upstream(branch,
1095+
NULL);
1096+
if (!options.upstream_name)
1097+
error_on_missing_default_upstream();
1098+
if (fork_point < 0)
1099+
fork_point = 1;
1100+
} else {
10631101
options.upstream_name = argv[0];
10641102
argc--;
10651103
argv++;

0 commit comments

Comments
 (0)