format-patch: introduce --base=auto option
authorXiaolong Ye <xiaolong.ye@intel.com>
Tue, 26 Apr 2016 07:51:23 +0000 (15:51 +0800)
committerJunio C Hamano <gitster@pobox.com>
Tue, 26 Apr 2016 17:51:50 +0000 (10:51 -0700)
Introduce --base=auto to record the base commit info automatically, the
base_commit will be the merge base of tip commit of the upstream branch
and revision-range specified in cmdline.

Helped-by: Junio C Hamano <gitster@pobox.com>
Helped-by: Wu Fengguang <fengguang.wu@intel.com>
Signed-off-by: Xiaolong Ye <xiaolong.ye@intel.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/git-format-patch.txt
builtin/log.c
t/t4014-format-patch.sh
index 1d790f1af5f1ce719ff787f1df1a250323d9829d..bdeecd59e002b074f32960db2d19d745e21539c4 100644 (file)
@@ -575,6 +575,12 @@ You can also use `git format-patch --base=P -3 C` to generate patches
 for A, B and C, and the identifiers for P, X, Y, Z are appended at the
 end of the first message.
 
+If set `--base=auto` in cmdline, it will track base commit automatically,
+the base commit will be the merge base of tip commit of the remote-tracking
+branch and revision-range specified in cmdline.
+For a local branch, you need to track a remote branch by `git branch
+--set-upstream-to` before using this option.
+
 EXAMPLES
 --------
 
index c9ee333932daf10bf0f03716369b0b5bcec95ebe..9033bc7c54619bd6c59521b8f39f30e6b2333887 100644 (file)
@@ -1199,9 +1199,33 @@ static struct commit *get_base_commit(const char *base_commit,
        struct commit **rev;
        int i = 0, rev_nr = 0;
 
-       base = lookup_commit_reference_by_name(base_commit);
-       if (!base)
-               die(_("Unknown commit %s"), base_commit);
+       if (!strcmp(base_commit, "auto")) {
+               struct branch *curr_branch = branch_get(NULL);
+               const char *upstream = branch_get_upstream(curr_branch, NULL);
+               if (upstream) {
+                       struct commit_list *base_list;
+                       struct commit *commit;
+                       unsigned char sha1[20];
+
+                       if (get_sha1(upstream, sha1))
+                               die(_("Failed to resolve '%s' as a valid ref."), upstream);
+                       commit = lookup_commit_or_die(sha1, "upstream base");
+                       base_list = get_merge_bases_many(commit, total, list);
+                       /* There should be one and only one merge base. */
+                       if (!base_list || base_list->next)
+                               die(_("Could not find exact merge base."));
+                       base = base_list->item;
+                       free_commit_list(base_list);
+               } else {
+                       die(_("Failed to get upstream, if you want to record base commit automatically,\n"
+                             "please use git branch --set-upstream-to to track a remote branch.\n"
+                             "Or you could specify base commit by --base=<base-commit-id> manually."));
+               }
+       } else {
+               base = lookup_commit_reference_by_name(base_commit);
+               if (!base)
+                       die(_("Unknown commit %s"), base_commit);
+       }
 
        ALLOC_ARRAY(rev, total);
        for (i = 0; i < total; i++)
index edf07eefd4d7f5508547a835955adfdb8fb59373..1bfd868f6d2902dd501f7bca210fd00a7fc9174a 100755 (executable)
@@ -1507,4 +1507,43 @@ test_expect_success 'format-patch --base errors out when base commit is not ance
        test_cmp expected actual
 '
 
+test_expect_success 'format-patch --base=auto' '
+       git checkout -b upstream master &&
+       git checkout -b local upstream &&
+       git branch --set-upstream-to=upstream &&
+       test_commit N1 &&
+       test_commit N2 &&
+       git format-patch --stdout --base=auto -2 >patch &&
+       grep "^base-commit:" patch >actual &&
+       echo "base-commit: $(git rev-parse upstream)" >expected &&
+       test_cmp expected actual
+'
+
+test_expect_success 'format-patch errors out when history involves criss-cross' '
+       # setup criss-cross history
+       #
+       #   B---M1---D
+       #  / \ /
+       # A   X
+       #  \ / \
+       #   C---M2---E
+       #
+       git checkout master &&
+       test_commit A &&
+       git checkout -b xb master &&
+       test_commit B &&
+       git checkout -b xc master &&
+       test_commit C &&
+       git checkout -b xbc xb -- &&
+       git merge xc &&
+       git checkout -b xcb xc -- &&
+       git branch --set-upstream-to=xbc &&
+       git merge xb &&
+       git checkout xbc &&
+       test_commit D &&
+       git checkout xcb &&
+       test_commit E &&
+       test_must_fail  git format-patch --base=auto -1
+'
+
 test_done