format-patch: add '--base' option to record base tree info
[gitweb.git] / builtin / log.c
index 0d738d6ddc0d6335e15392dd90e4118e2cb58b88..c9ee333932daf10bf0f03716369b0b5bcec95ebe 100644 (file)
@@ -1185,6 +1185,131 @@ static int from_callback(const struct option *opt, const char *arg, int unset)
        return 0;
 }
 
+struct base_tree_info {
+       struct object_id base_commit;
+       int nr_patch_id, alloc_patch_id;
+       struct object_id *patch_id;
+};
+
+static struct commit *get_base_commit(const char *base_commit,
+                                     struct commit **list,
+                                     int total)
+{
+       struct commit *base = NULL;
+       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);
+
+       ALLOC_ARRAY(rev, total);
+       for (i = 0; i < total; i++)
+               rev[i] = list[i];
+
+       rev_nr = total;
+       /*
+        * Get merge base through pair-wise computations
+        * and store it in rev[0].
+        */
+       while (rev_nr > 1) {
+               for (i = 0; i < rev_nr / 2; i++) {
+                       struct commit_list *merge_base;
+                       merge_base = get_merge_bases(rev[2 * i], rev[2 * i + 1]);
+                       if (!merge_base || merge_base->next)
+                               die(_("Failed to find exact merge base"));
+
+                       rev[i] = merge_base->item;
+               }
+
+               if (rev_nr % 2)
+                       rev[i] = rev[2 * i];
+               rev_nr = (rev_nr + 1) / 2;
+       }
+
+       if (!in_merge_bases(base, rev[0]))
+               die(_("base commit should be the ancestor of revision list"));
+
+       for (i = 0; i < total; i++) {
+               if (base == list[i])
+                       die(_("base commit shouldn't be in revision list"));
+       }
+
+       free(rev);
+       return base;
+}
+
+static void prepare_bases(struct base_tree_info *bases,
+                         struct commit *base,
+                         struct commit **list,
+                         int total)
+{
+       struct commit *commit;
+       struct rev_info revs;
+       struct diff_options diffopt;
+       int i;
+
+       if (!base)
+               return;
+
+       diff_setup(&diffopt);
+       DIFF_OPT_SET(&diffopt, RECURSIVE);
+       diff_setup_done(&diffopt);
+
+       oidcpy(&bases->base_commit, &base->object.oid);
+
+       init_revisions(&revs, NULL);
+       revs.max_parents = 1;
+       revs.topo_order = 1;
+       for (i = 0; i < total; i++) {
+               list[i]->object.flags &= ~UNINTERESTING;
+               add_pending_object(&revs, &list[i]->object, "rev_list");
+               list[i]->util = (void *)1;
+       }
+       base->object.flags |= UNINTERESTING;
+       add_pending_object(&revs, &base->object, "base");
+
+       if (prepare_revision_walk(&revs))
+               die(_("revision walk setup failed"));
+       /*
+        * Traverse the commits list, get prerequisite patch ids
+        * and stuff them in bases structure.
+        */
+       while ((commit = get_revision(&revs)) != NULL) {
+               unsigned char sha1[20];
+               struct object_id *patch_id;
+               if (commit->util)
+                       continue;
+               if (commit_patch_id(commit, &diffopt, sha1))
+                       die(_("cannot get patch id"));
+               ALLOC_GROW(bases->patch_id, bases->nr_patch_id + 1, bases->alloc_patch_id);
+               patch_id = bases->patch_id + bases->nr_patch_id;
+               hashcpy(patch_id->hash, sha1);
+               bases->nr_patch_id++;
+       }
+}
+
+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))
+               return;
+
+       /* Show the base commit */
+       printf("base-commit: %s\n", oid_to_hex(&bases->base_commit));
+
+       /* Show the prerequisite patches */
+       for (i = bases->nr_patch_id - 1; i >= 0; 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);
+}
+
 int cmd_format_patch(int argc, const char **argv, const char *prefix)
 {
        struct commit *commit;
@@ -1209,6 +1334,9 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
        int reroll_count = -1;
        char *branch_name = NULL;
        char *from = NULL;
+       char *base_commit = NULL;
+       struct base_tree_info bases;
+
        const struct option builtin_format_patch_options[] = {
                { OPTION_CALLBACK, 'n', "numbered", &numbered, NULL,
                            N_("use [PATCH n/m] even with a single patch"),
@@ -1271,6 +1399,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
                            PARSE_OPT_OPTARG, thread_callback },
                OPT_STRING(0, "signature", &signature, N_("signature"),
                            N_("add a signature")),
+               OPT_STRING(0, "base", &base_commit, N_("base-commit"),
+                          N_("add prerequisite tree info to the patch series")),
                OPT_FILENAME(0, "signature-file", &signature_file,
                                N_("add a signature from a file")),
                OPT__QUIET(&quiet, N_("don't print the patch filenames")),
@@ -1507,6 +1637,13 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
                signature = strbuf_detach(&buf, NULL);
        }
 
+       memset(&bases, 0, sizeof(bases));
+       if (base_commit) {
+               struct commit *base = get_base_commit(base_commit, list, nr);
+               reset_revision_walk();
+               prepare_bases(&bases, base, list, nr);
+       }
+
        if (in_reply_to || thread || cover_letter)
                rev.ref_message_ids = xcalloc(1, sizeof(struct string_list));
        if (in_reply_to) {
@@ -1520,6 +1657,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
                        gen_message_id(&rev, "cover");
                make_cover_letter(&rev, use_stdout,
                                  origin, nr, list, branch_name, quiet);
+               print_bases(&bases);
                total++;
                start_number--;
        }
@@ -1585,6 +1723,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
                                       rev.mime_boundary);
                        else
                                print_signature();
+                       print_bases(&bases);
                }
                if (!use_stdout)
                        fclose(stdout);