format-patch: introduce --base=auto option

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>
This commit is contained in:
Xiaolong Ye
2016-04-26 15:51:23 +08:00
committed by Junio C Hamano
parent fa2ab86d18
commit 3de665175f
3 changed files with 72 additions and 3 deletions

View 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++)