Introduce --base=auto to record the base commit info automatically, the 
base_commit
will be the tip commit of the upstream branch. If upstream branch cannot be 
determined,
it will just record the parent's commit id and patch id.

Signed-off-by: Xiaolong Ye <xiaolong...@intel.com>
---
 builtin/log.c | 70 +++++++++++++++++++++++++++++++++++++++++++----------------
 1 file changed, 51 insertions(+), 19 deletions(-)

diff --git a/builtin/log.c b/builtin/log.c
index d3804b3..e8a3964 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -1187,6 +1187,8 @@ static int from_callback(const struct option *opt, const 
char *arg, int unset)
 
 struct base_tree_info {
        struct object_id base_commit;
+       struct object_id parent_commit;
+       struct object_id parent_patch_id;
        int nr_patch_id, alloc_patch_id;
        struct object_id *patch_id;
 };
@@ -1199,15 +1201,38 @@ static void prepare_bases(struct base_tree_info *bases,
        struct rev_info revs;
        struct diff_options diffopt;
        struct object_id *patch_id;
+       struct branch *curr_branch;
+       const char *upstream;
        unsigned char sha1[20];
        int pos = 0;
 
        if (!prerequisite_head)
                return;
-       base = lookup_commit_reference_by_name(base_commit);
-       if (!base)
-               die(_("Unknown commit %s"), base_commit);
-       oidcpy(&bases->base_commit, &base->object.oid);
+
+       diff_setup(&diffopt);
+       DIFF_OPT_SET(&diffopt, RECURSIVE);
+       diff_setup_done(&diffopt);
+
+       if (!strcmp(base_commit, "auto")) {
+               curr_branch = branch_get(NULL);
+               upstream = branch_get_upstream(curr_branch, NULL);
+               if (upstream) {
+                       if (get_sha1(upstream, sha1))
+                               die(_("Failed to resolve '%s' as a valid 
ref."), upstream);
+                       base = lookup_commit_or_die(sha1, "upstream base");
+                       oidcpy(&bases->base_commit, &base->object.oid);
+               } else {
+                       commit_patch_id(prerequisite_head, &diffopt, sha1);
+                       oidcpy(&bases->parent_commit, 
&prerequisite_head->object.oid);
+                       hashcpy(bases->parent_patch_id.hash, sha1);
+                       return;
+               }
+       } else {
+               base = lookup_commit_reference_by_name(base_commit);
+               if (!base)
+                       die(_("Unknown commit %s"), base_commit);
+               oidcpy(&bases->base_commit, &base->object.oid);
+       }
 
        if (base == prerequisite_head)
                return;
@@ -1223,10 +1248,6 @@ static void prepare_bases(struct base_tree_info *bases,
        prerequisite_head->object.flags |= 0;
        add_pending_object(&revs, &prerequisite_head->object, 
"prerequisite-head");
 
-       diff_setup(&diffopt);
-       DIFF_OPT_SET(&diffopt, RECURSIVE);
-       diff_setup_done(&diffopt);
-
        if (prepare_revision_walk(&revs))
                die(_("revision walk setup failed"));
        /*
@@ -1249,22 +1270,33 @@ static void print_bases(struct base_tree_info *bases)
        int i;
 
        /* Only do this once, either for the cover or for the first one */
-       if (is_null_oid(&bases->base_commit))
+       if (is_null_oid(&bases->base_commit) && 
is_null_oid(&bases->parent_commit))
                return;
 
-       printf("** base-commit-info **\n");
+       if (!is_null_oid(&bases->base_commit)) {
+               printf("** base-commit-info **\n");
+
+               /* Show the base commit */
+               printf("base-commit: %s\n", oid_to_hex(&bases->base_commit));
 
-       /* Show the base commit */
-       printf("base-commit: %s\n", oid_to_hex(&bases->base_commit));
+               /* Show the prerequisite patches */
+               for (i = 0; i < bases->nr_patch_id; i++)
+                       printf("prerequisite-patch-id: %s\n", 
oid_to_hex(&bases->patch_id[i]));
 
-       /* Show the prerequisite patches */
-       for (i = 0; i < bases->nr_patch_id; i++)
-               printf("prerequisite-patch-id: %s\n", 
oid_to_hex(&bases->patch_id[i]));
+               free(bases->patch_id);
+               bases->nr_patch_id = 0;
+               bases->alloc_patch_id = 0;
+               oidclr(&bases->base_commit);
+       } else if (!is_null_oid(&bases->parent_commit)) {
+               printf("** parent-commit-info **\n");
 
-       free(bases->patch_id);
-       bases->nr_patch_id = 0;
-       bases->alloc_patch_id = 0;
-       oidclr(&bases->base_commit);
+               /* Show the parent commit */
+               printf("parent-commit: %s\n", 
oid_to_hex(&bases->parent_commit));
+               /* Show the parent patch id */
+               printf("parent-patch-id: %s\n", 
oid_to_hex(&bases->parent_patch_id));
+
+               oidclr(&bases->parent_commit);
+       }
 }
 
 int cmd_format_patch(int argc, const char **argv, const char *prefix)
-- 
2.8.0.rc4.4.ga41a987

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to