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 <predatoramigo@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Pratik Karki committed
Aug 8, 2018 at 21:21 UTC
8f5986d95a9154d2ecbc4dd16b893380875cc83d
1 file changed
+41
-3
builtin/rebase.c
+41
-3
@@ -622,6 +622,36 @@ static int parse_opt_interactive(const struct option *opt, const char *arg,
622
return 0;
623
}
624
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
+
655
int cmd_rebase(int argc, const char **argv, const char *prefix)
656
{
657
struct rebase_options options = {
@@ -1057,9 +1087,17 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1087
}
1088
1089
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 {
1101
options.upstream_name = argv[0];
1102
argc--;
1103
argv++;