1185
return 0;
1186
}
1187
1188
+struct base_tree_info {
1189
+ struct object_id base_commit;
1190
+ int nr_patch_id, alloc_patch_id;
1191
+ struct object_id *patch_id;
1192
+};
1193
+
1194
+static struct commit *get_base_commit(const char *base_commit,
1195
+ struct commit **list,
1196
+ int total)
1197
+{
1198
+ struct commit *base = NULL;
1199
+ struct commit **rev;
1200
+ int i = 0, rev_nr = 0;
1201
+
1202
+ base = lookup_commit_reference_by_name(base_commit);
1203
+ if (!base)
1204
+ die(_("Unknown commit %s"), base_commit);
1205
+
1206
+ ALLOC_ARRAY(rev, total);
1207
+ for (i = 0; i < total; i++)
1208
+ rev[i] = list[i];
1209
+
1210
+ rev_nr = total;
1211
+ /*
1212
+ * Get merge base through pair-wise computations
1213
+ * and store it in rev[0].
1214
+ */
1215
+ while (rev_nr > 1) {
1216
+ for (i = 0; i < rev_nr / 2; i++) {
1217
+ struct commit_list *merge_base;
1218
+ merge_base = get_merge_bases(rev[2 * i], rev[2 * i + 1]);
1219
+ if (!merge_base || merge_base->next)
1220
+ die(_("Failed to find exact merge base"));
1221
+
1222
+ rev[i] = merge_base->item;
1223
+ }
1224
+
1225
+ if (rev_nr % 2)
1226
+ rev[i] = rev[2 * i];
1227
+ rev_nr = (rev_nr + 1) / 2;
1228
+ }
1229
+
1230
+ if (!in_merge_bases(base, rev[0]))
1231
+ die(_("base commit should be the ancestor of revision list"));
1232
+
1233
+ for (i = 0; i < total; i++) {
1234
+ if (base == list[i])
1235
+ die(_("base commit shouldn't be in revision list"));
1236
+ }
1237
+
1238
+ free(rev);
1239
+ return base;
1240
+}
1241
+
1242
+static void prepare_bases(struct base_tree_info *bases,
1243
+ struct commit *base,
1244
+ struct commit **list,
1245
+ int total)
1246
+{
1247
+ struct commit *commit;
1248
+ struct rev_info revs;
1249
+ struct diff_options diffopt;
1250
+ int i;
1251
+
1252
+ if (!base)
1253
+ return;
1254
+
1255
+ diff_setup(&diffopt);
1256
+ DIFF_OPT_SET(&diffopt, RECURSIVE);
1257
+ diff_setup_done(&diffopt);
1258
+
1259
+ oidcpy(&bases->base_commit, &base->object.oid);
1260
+
1261
+ init_revisions(&revs, NULL);
1262
+ revs.max_parents = 1;
1263
+ revs.topo_order = 1;
1264
+ for (i = 0; i < total; i++) {
1265
+ list[i]->object.flags &= ~UNINTERESTING;
1266
+ add_pending_object(&revs, &list[i]->object, "rev_list");
1267
+ list[i]->util = (void *)1;
1268
+ }
1269
+ base->object.flags |= UNINTERESTING;
1270
+ add_pending_object(&revs, &base->object, "base");
1271
+
1272
+ if (prepare_revision_walk(&revs))
1273
+ die(_("revision walk setup failed"));
1274
+ /*
1275
+ * Traverse the commits list, get prerequisite patch ids
1276
+ * and stuff them in bases structure.
1277
+ */
1278
+ while ((commit = get_revision(&revs)) != NULL) {
1279
+ unsigned char sha1[20];
1280
+ struct object_id *patch_id;
1281
+ if (commit->util)
1282
+ continue;
1283
+ if (commit_patch_id(commit, &diffopt, sha1))
1284
+ die(_("cannot get patch id"));
1285
+ ALLOC_GROW(bases->patch_id, bases->nr_patch_id + 1, bases->alloc_patch_id);
1286
+ patch_id = bases->patch_id + bases->nr_patch_id;
1287
+ hashcpy(patch_id->hash, sha1);
1288
+ bases->nr_patch_id++;
1289
+ }
1290
+}
1291
+
1292
+static void print_bases(struct base_tree_info *bases)
1293
+{
1294
+ int i;
1295
+
1296
+ /* Only do this once, either for the cover or for the first one */
1297
+ if (is_null_oid(&bases->base_commit))
1298
+ return;
1299
+
1300
+ /* Show the base commit */
1301
+ printf("base-commit: %s\n", oid_to_hex(&bases->base_commit));
1302
+
1303
+ /* Show the prerequisite patches */
1304
+ for (i = bases->nr_patch_id - 1; i >= 0; i--)
1305
+ printf("prerequisite-patch-id: %s\n", oid_to_hex(&bases->patch_id[i]));
1306
+
1307
+ free(bases->patch_id);
1308
+ bases->nr_patch_id = 0;
1309
+ bases->alloc_patch_id = 0;
1310
+ oidclr(&bases->base_commit);
1311
+}
1312
+
1313
int cmd_format_patch(int argc, const char **argv, const char *prefix)
1314
{
1315
struct commit *commit;
1334
int reroll_count = -1;
1335
char *branch_name = NULL;
1336
char *from = NULL;
1337
+ char *base_commit = NULL;
1338
+ struct base_tree_info bases;
1339
+
1340
const struct option builtin_format_patch_options[] = {
1341
{ OPTION_CALLBACK, 'n', "numbered", &numbered, NULL,
1342
N_("use [PATCH n/m] even with a single patch"),
1399
PARSE_OPT_OPTARG, thread_callback },
1400
OPT_STRING(0, "signature", &signature, N_("signature"),
1401
N_("add a signature")),
1402
+ OPT_STRING(0, "base", &base_commit, N_("base-commit"),
1403
+ N_("add prerequisite tree info to the patch series")),
1404
OPT_FILENAME(0, "signature-file", &signature_file,
1405
N_("add a signature from a file")),
1406
OPT__QUIET(&quiet, N_("don't print the patch filenames")),
1637
signature = strbuf_detach(&buf, NULL);
1638
}
1639
1640
+ memset(&bases, 0, sizeof(bases));
1641
+ if (base_commit) {
1642
+ struct commit *base = get_base_commit(base_commit, list, nr);
1643
+ reset_revision_walk();
1644
+ prepare_bases(&bases, base, list, nr);
1645
+ }
1646
+
1647
if (in_reply_to || thread || cover_letter)
1648
rev.ref_message_ids = xcalloc(1, sizeof(struct string_list));
1649
if (in_reply_to) {
1657
gen_message_id(&rev, "cover");
1658
make_cover_letter(&rev, use_stdout,
1659
origin, nr, list, branch_name, quiet);
1660
+ print_bases(&bases);
1661
total++;
1662
start_number--;
1663
}
1723
rev.mime_boundary);
1724
else
1725
print_signature();
1726
+ print_bases(&bases);
1727
}
1728
if (!use_stdout)
1729
fclose(stdout);