merge-recursive.[ch] have three entry points:
* merge_trees()
* merge_recursive()
* merge_recursive_generic()
merge-ort*.[ch] only has equivalents for the first two. Add an
equivalent for the final entry point, so we can switch callers to
use it and remove merge-recursive.[ch].
While porting it over, finally fix the issue with the label for the
ancestor (used when merge.conflictStyle=diff3 as a conflict label).
merge-recursive.c has traditionally not allowed callers to set that
label, but I have found that problematic for years.
(Side note: This function was initially part of the merge-ort rewrite,
but reviewers questioned the ancestor label funnyness which I was
never really happy with anyway. It resulted in me jettisoning it and
hoping at the time that I would eventually be able to force the existing
callers to use some other API. That worked with `git stash`, as per
874cf2a604 (stash: apply stash using 'merge_ort_nonrecursive()',
2022-05-10), but this API is the most reasonable one for `git am` and
`git merge-recursive`, if we can just allow them some freedom over the
ancestor label.)
The merge_recursive_generic() function did not know whether it was being
invoked by `git stash`, `git merge-recursive`, or `git am`, and the
choice of meaningful ancestor label, when there is a unique ancestor,
varies for these different callers:
* git am: ancestor is a constructed "fake ancestor" that user knows
nothing about and has no access to. (And is different than
the normal thing we mean by a "virtual merge base" which is
the merging of merge bases.)
* git merge-recursive: ancestor might be a tree, but at least it
was one specified by the user (if they invoked
merge-recursive directly)
* git stash: ancestor was the commit serving as the stash base
Thus, using a label like "constructed merge base" (as
merge_recursive_generic() does) presupposes that `git am` is the only
caller; it is incorrect for other callers. This label has thrown me off
more than once. Allow the caller to override when there is a unique
merge base.
Signed-off-by: Elijah Newren <newren@gmail.com>
Reviewed-by: Taylor Blau <me@ttaylorr.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
131 lines
3.2 KiB
C
131 lines
3.2 KiB
C
#include "git-compat-util.h"
|
|
#include "gettext.h"
|
|
#include "hash.h"
|
|
#include "hex.h"
|
|
#include "lockfile.h"
|
|
#include "merge-ort.h"
|
|
#include "merge-ort-wrappers.h"
|
|
#include "read-cache-ll.h"
|
|
#include "repository.h"
|
|
#include "tag.h"
|
|
#include "tree.h"
|
|
|
|
#include "commit.h"
|
|
|
|
static int unclean(struct merge_options *opt, struct tree *head)
|
|
{
|
|
/* Sanity check on repo state; index must match head */
|
|
struct strbuf sb = STRBUF_INIT;
|
|
|
|
if (head && repo_index_has_changes(opt->repo, head, &sb)) {
|
|
error(_("Your local changes to the following files would be overwritten by merge:\n %s"),
|
|
sb.buf);
|
|
strbuf_release(&sb);
|
|
return -1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int merge_ort_nonrecursive(struct merge_options *opt,
|
|
struct tree *head,
|
|
struct tree *merge,
|
|
struct tree *merge_base)
|
|
{
|
|
struct merge_result result;
|
|
|
|
if (unclean(opt, head))
|
|
return -1;
|
|
|
|
if (oideq(&merge_base->object.oid, &merge->object.oid)) {
|
|
printf_ln(_("Already up to date."));
|
|
return 1;
|
|
}
|
|
|
|
memset(&result, 0, sizeof(result));
|
|
merge_incore_nonrecursive(opt, merge_base, head, merge, &result);
|
|
merge_switch_to_result(opt, head, &result, 1, 1);
|
|
|
|
return result.clean;
|
|
}
|
|
|
|
int merge_ort_recursive(struct merge_options *opt,
|
|
struct commit *side1,
|
|
struct commit *side2,
|
|
const struct commit_list *merge_bases,
|
|
struct commit **result)
|
|
{
|
|
struct tree *head = repo_get_commit_tree(opt->repo, side1);
|
|
struct merge_result tmp;
|
|
|
|
if (unclean(opt, head))
|
|
return -1;
|
|
|
|
memset(&tmp, 0, sizeof(tmp));
|
|
merge_incore_recursive(opt, merge_bases, side1, side2, &tmp);
|
|
merge_switch_to_result(opt, head, &tmp, 1, 1);
|
|
*result = NULL;
|
|
|
|
return tmp.clean;
|
|
}
|
|
|
|
static struct commit *get_ref(struct repository *repo,
|
|
const struct object_id *oid,
|
|
const char *name)
|
|
{
|
|
struct object *object;
|
|
|
|
object = deref_tag(repo, parse_object(repo, oid),
|
|
name, strlen(name));
|
|
if (!object)
|
|
return NULL;
|
|
if (object->type == OBJ_TREE)
|
|
return make_virtual_commit(repo, (struct tree*)object, name);
|
|
if (object->type != OBJ_COMMIT)
|
|
return NULL;
|
|
if (repo_parse_commit(repo, (struct commit *)object))
|
|
return NULL;
|
|
return (struct commit *)object;
|
|
}
|
|
|
|
int merge_ort_generic(struct merge_options *opt,
|
|
const struct object_id *head,
|
|
const struct object_id *merge,
|
|
int num_merge_bases,
|
|
const struct object_id *merge_bases,
|
|
struct commit **result)
|
|
{
|
|
int clean;
|
|
struct lock_file lock = LOCK_INIT;
|
|
struct commit *head_commit = get_ref(opt->repo, head, opt->branch1);
|
|
struct commit *next_commit = get_ref(opt->repo, merge, opt->branch2);
|
|
struct commit_list *ca = NULL;
|
|
|
|
if (merge_bases) {
|
|
int i;
|
|
for (i = 0; i < num_merge_bases; ++i) {
|
|
struct commit *base;
|
|
if (!(base = get_ref(opt->repo, &merge_bases[i],
|
|
oid_to_hex(&merge_bases[i]))))
|
|
return error(_("Could not parse object '%s'"),
|
|
oid_to_hex(&merge_bases[i]));
|
|
commit_list_insert(base, &ca);
|
|
}
|
|
}
|
|
|
|
repo_hold_locked_index(opt->repo, &lock, LOCK_DIE_ON_ERROR);
|
|
clean = merge_ort_recursive(opt, head_commit, next_commit, ca,
|
|
result);
|
|
free_commit_list(ca);
|
|
if (clean < 0) {
|
|
rollback_lock_file(&lock);
|
|
return clean;
|
|
}
|
|
|
|
if (write_locked_index(opt->repo->index, &lock,
|
|
COMMIT_LOCK | SKIP_IF_UNCHANGED))
|
|
return error(_("Unable to write index."));
|
|
|
|
return clean ? 0 : 1;
|
|
}
|