|
Packit Service |
20376f |
/*
|
|
Packit Service |
20376f |
* Copyright (C) the libgit2 contributors. All rights reserved.
|
|
Packit Service |
20376f |
*
|
|
Packit Service |
20376f |
* This file is part of libgit2, distributed under the GNU GPL v2 with
|
|
Packit Service |
20376f |
* a Linking Exception. For full terms see the included COPYING file.
|
|
Packit Service |
20376f |
*/
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
#include "common.h"
|
|
Packit Service |
20376f |
#include "repository.h"
|
|
Packit Service |
20376f |
#include "posix.h"
|
|
Packit Service |
20376f |
#include "fileops.h"
|
|
Packit Service |
20376f |
#include "index.h"
|
|
Packit Service |
20376f |
#include "diff_xdiff.h"
|
|
Packit Service |
20376f |
#include "merge.h"
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
#include "git2/repository.h"
|
|
Packit Service |
20376f |
#include "git2/object.h"
|
|
Packit Service |
20376f |
#include "git2/index.h"
|
|
Packit Service |
20376f |
#include "git2/merge.h"
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
#include "xdiff/xdiff.h"
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
/* only examine the first 8000 bytes for binaryness.
|
|
Packit Service |
20376f |
* https://github.com/git/git/blob/77bd3ea9f54f1584147b594abc04c26ca516d987/xdiff-interface.c#L197
|
|
Packit Service |
20376f |
*/
|
|
Packit Service |
20376f |
#define GIT_MERGE_FILE_BINARY_SIZE 8000
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
#define GIT_MERGE_FILE_SIDE_EXISTS(X) ((X)->mode != 0)
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
int git_merge_file__input_from_index(
|
|
Packit Service |
20376f |
git_merge_file_input *input_out,
|
|
Packit Service |
20376f |
git_odb_object **odb_object_out,
|
|
Packit Service |
20376f |
git_odb *odb,
|
|
Packit Service |
20376f |
const git_index_entry *entry)
|
|
Packit Service |
20376f |
{
|
|
Packit Service |
20376f |
int error = 0;
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
assert(input_out && odb_object_out && odb && entry);
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
if ((error = git_odb_read(odb_object_out, odb, &entry->id)) < 0)
|
|
Packit Service |
20376f |
goto done;
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
input_out->path = entry->path;
|
|
Packit Service |
20376f |
input_out->mode = entry->mode;
|
|
Packit Service |
20376f |
input_out->ptr = (char *)git_odb_object_data(*odb_object_out);
|
|
Packit Service |
20376f |
input_out->size = git_odb_object_size(*odb_object_out);
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
done:
|
|
Packit Service |
20376f |
return error;
|
|
Packit Service |
20376f |
}
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
static void merge_file_normalize_opts(
|
|
Packit Service |
20376f |
git_merge_file_options *out,
|
|
Packit Service |
20376f |
const git_merge_file_options *given_opts)
|
|
Packit Service |
20376f |
{
|
|
Packit Service |
20376f |
if (given_opts)
|
|
Packit Service |
20376f |
memcpy(out, given_opts, sizeof(git_merge_file_options));
|
|
Packit Service |
20376f |
else {
|
|
Packit Service |
20376f |
git_merge_file_options default_opts = GIT_MERGE_FILE_OPTIONS_INIT;
|
|
Packit Service |
20376f |
memcpy(out, &default_opts, sizeof(git_merge_file_options));
|
|
Packit Service |
20376f |
}
|
|
Packit Service |
20376f |
}
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
static int merge_file__xdiff(
|
|
Packit Service |
20376f |
git_merge_file_result *out,
|
|
Packit Service |
20376f |
const git_merge_file_input *ancestor,
|
|
Packit Service |
20376f |
const git_merge_file_input *ours,
|
|
Packit Service |
20376f |
const git_merge_file_input *theirs,
|
|
Packit Service |
20376f |
const git_merge_file_options *given_opts)
|
|
Packit Service |
20376f |
{
|
|
Packit Service |
20376f |
xmparam_t xmparam;
|
|
Packit Service |
20376f |
mmfile_t ancestor_mmfile = {0}, our_mmfile = {0}, their_mmfile = {0};
|
|
Packit Service |
20376f |
mmbuffer_t mmbuffer;
|
|
Packit Service |
20376f |
git_merge_file_options options = GIT_MERGE_FILE_OPTIONS_INIT;
|
|
Packit Service |
20376f |
const char *path;
|
|
Packit Service |
20376f |
int xdl_result;
|
|
Packit Service |
20376f |
int error = 0;
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
memset(out, 0x0, sizeof(git_merge_file_result));
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
merge_file_normalize_opts(&options, given_opts);
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
memset(&xmparam, 0x0, sizeof(xmparam_t));
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
if (ancestor) {
|
|
Packit Service |
20376f |
xmparam.ancestor = (options.ancestor_label) ?
|
|
Packit Service |
20376f |
options.ancestor_label : ancestor->path;
|
|
Packit Service |
20376f |
ancestor_mmfile.ptr = (char *)ancestor->ptr;
|
|
Packit Service |
20376f |
ancestor_mmfile.size = ancestor->size;
|
|
Packit Service |
20376f |
}
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
xmparam.file1 = (options.our_label) ?
|
|
Packit Service |
20376f |
options.our_label : ours->path;
|
|
Packit Service |
20376f |
our_mmfile.ptr = (char *)ours->ptr;
|
|
Packit Service |
20376f |
our_mmfile.size = ours->size;
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
xmparam.file2 = (options.their_label) ?
|
|
Packit Service |
20376f |
options.their_label : theirs->path;
|
|
Packit Service |
20376f |
their_mmfile.ptr = (char *)theirs->ptr;
|
|
Packit Service |
20376f |
their_mmfile.size = theirs->size;
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
if (options.favor == GIT_MERGE_FILE_FAVOR_OURS)
|
|
Packit Service |
20376f |
xmparam.favor = XDL_MERGE_FAVOR_OURS;
|
|
Packit Service |
20376f |
else if (options.favor == GIT_MERGE_FILE_FAVOR_THEIRS)
|
|
Packit Service |
20376f |
xmparam.favor = XDL_MERGE_FAVOR_THEIRS;
|
|
Packit Service |
20376f |
else if (options.favor == GIT_MERGE_FILE_FAVOR_UNION)
|
|
Packit Service |
20376f |
xmparam.favor = XDL_MERGE_FAVOR_UNION;
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
xmparam.level = (options.flags & GIT_MERGE_FILE_SIMPLIFY_ALNUM) ?
|
|
Packit Service |
20376f |
XDL_MERGE_ZEALOUS_ALNUM : XDL_MERGE_ZEALOUS;
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
if (options.flags & GIT_MERGE_FILE_STYLE_DIFF3)
|
|
Packit Service |
20376f |
xmparam.style = XDL_MERGE_DIFF3;
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
if (options.flags & GIT_MERGE_FILE_IGNORE_WHITESPACE)
|
|
Packit Service |
20376f |
xmparam.xpp.flags |= XDF_IGNORE_WHITESPACE;
|
|
Packit Service |
20376f |
if (options.flags & GIT_MERGE_FILE_IGNORE_WHITESPACE_CHANGE)
|
|
Packit Service |
20376f |
xmparam.xpp.flags |= XDF_IGNORE_WHITESPACE_CHANGE;
|
|
Packit Service |
20376f |
if (options.flags & GIT_MERGE_FILE_IGNORE_WHITESPACE_EOL)
|
|
Packit Service |
20376f |
xmparam.xpp.flags |= XDF_IGNORE_WHITESPACE_AT_EOL;
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
if (options.flags & GIT_MERGE_FILE_DIFF_PATIENCE)
|
|
Packit Service |
20376f |
xmparam.xpp.flags |= XDF_PATIENCE_DIFF;
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
if (options.flags & GIT_MERGE_FILE_DIFF_MINIMAL)
|
|
Packit Service |
20376f |
xmparam.xpp.flags |= XDF_NEED_MINIMAL;
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
if ((xdl_result = xdl_merge(&ancestor_mmfile, &our_mmfile,
|
|
Packit Service |
20376f |
&their_mmfile, &xmparam, &mmbuffer)) < 0) {
|
|
Packit Service |
20376f |
giterr_set(GITERR_MERGE, "failed to merge files");
|
|
Packit Service |
20376f |
error = -1;
|
|
Packit Service |
20376f |
goto done;
|
|
Packit Service |
20376f |
}
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
path = git_merge_file__best_path(
|
|
Packit Service |
20376f |
ancestor ? ancestor->path : NULL,
|
|
Packit Service |
20376f |
ours->path,
|
|
Packit Service |
20376f |
theirs->path);
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
if (path != NULL && (out->path = git__strdup(path)) == NULL) {
|
|
Packit Service |
20376f |
error = -1;
|
|
Packit Service |
20376f |
goto done;
|
|
Packit Service |
20376f |
}
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
out->automergeable = (xdl_result == 0);
|
|
Packit Service |
20376f |
out->ptr = (const char *)mmbuffer.ptr;
|
|
Packit Service |
20376f |
out->len = mmbuffer.size;
|
|
Packit Service |
20376f |
out->mode = git_merge_file__best_mode(
|
|
Packit Service |
20376f |
ancestor ? ancestor->mode : 0,
|
|
Packit Service |
20376f |
ours->mode,
|
|
Packit Service |
20376f |
theirs->mode);
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
done:
|
|
Packit Service |
20376f |
if (error < 0)
|
|
Packit Service |
20376f |
git_merge_file_result_free(out);
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
return error;
|
|
Packit Service |
20376f |
}
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
static bool merge_file__is_binary(const git_merge_file_input *file)
|
|
Packit Service |
20376f |
{
|
|
Packit Service |
20376f |
size_t len = file ? file->size : 0;
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
if (len > GIT_XDIFF_MAX_SIZE)
|
|
Packit Service |
20376f |
return true;
|
|
Packit Service |
20376f |
if (len > GIT_MERGE_FILE_BINARY_SIZE)
|
|
Packit Service |
20376f |
len = GIT_MERGE_FILE_BINARY_SIZE;
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
return len ? (memchr(file->ptr, 0, len) != NULL) : false;
|
|
Packit Service |
20376f |
}
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
static int merge_file__binary(
|
|
Packit Service |
20376f |
git_merge_file_result *out,
|
|
Packit Service |
20376f |
const git_merge_file_input *ours,
|
|
Packit Service |
20376f |
const git_merge_file_input *theirs,
|
|
Packit Service |
20376f |
const git_merge_file_options *given_opts)
|
|
Packit Service |
20376f |
{
|
|
Packit Service |
20376f |
const git_merge_file_input *favored = NULL;
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
memset(out, 0x0, sizeof(git_merge_file_result));
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
if (given_opts && given_opts->favor == GIT_MERGE_FILE_FAVOR_OURS)
|
|
Packit Service |
20376f |
favored = ours;
|
|
Packit Service |
20376f |
else if (given_opts && given_opts->favor == GIT_MERGE_FILE_FAVOR_THEIRS)
|
|
Packit Service |
20376f |
favored = theirs;
|
|
Packit Service |
20376f |
else
|
|
Packit Service |
20376f |
goto done;
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
if ((out->path = git__strdup(favored->path)) == NULL ||
|
|
Packit Service |
20376f |
(out->ptr = git__malloc(favored->size)) == NULL)
|
|
Packit Service |
20376f |
goto done;
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
memcpy((char *)out->ptr, favored->ptr, favored->size);
|
|
Packit Service |
20376f |
out->len = favored->size;
|
|
Packit Service |
20376f |
out->mode = favored->mode;
|
|
Packit Service |
20376f |
out->automergeable = 1;
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
done:
|
|
Packit Service |
20376f |
return 0;
|
|
Packit Service |
20376f |
}
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
static int merge_file__from_inputs(
|
|
Packit Service |
20376f |
git_merge_file_result *out,
|
|
Packit Service |
20376f |
const git_merge_file_input *ancestor,
|
|
Packit Service |
20376f |
const git_merge_file_input *ours,
|
|
Packit Service |
20376f |
const git_merge_file_input *theirs,
|
|
Packit Service |
20376f |
const git_merge_file_options *given_opts)
|
|
Packit Service |
20376f |
{
|
|
Packit Service |
20376f |
if (merge_file__is_binary(ancestor) ||
|
|
Packit Service |
20376f |
merge_file__is_binary(ours) ||
|
|
Packit Service |
20376f |
merge_file__is_binary(theirs))
|
|
Packit Service |
20376f |
return merge_file__binary(out, ours, theirs, given_opts);
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
return merge_file__xdiff(out, ancestor, ours, theirs, given_opts);
|
|
Packit Service |
20376f |
}
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
static git_merge_file_input *git_merge_file__normalize_inputs(
|
|
Packit Service |
20376f |
git_merge_file_input *out,
|
|
Packit Service |
20376f |
const git_merge_file_input *given)
|
|
Packit Service |
20376f |
{
|
|
Packit Service |
20376f |
memcpy(out, given, sizeof(git_merge_file_input));
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
if (!out->path)
|
|
Packit Service |
20376f |
out->path = "file.txt";
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
if (!out->mode)
|
|
Packit Service |
20376f |
out->mode = 0100644;
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
return out;
|
|
Packit Service |
20376f |
}
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
int git_merge_file(
|
|
Packit Service |
20376f |
git_merge_file_result *out,
|
|
Packit Service |
20376f |
const git_merge_file_input *ancestor,
|
|
Packit Service |
20376f |
const git_merge_file_input *ours,
|
|
Packit Service |
20376f |
const git_merge_file_input *theirs,
|
|
Packit Service |
20376f |
const git_merge_file_options *options)
|
|
Packit Service |
20376f |
{
|
|
Packit Service |
20376f |
git_merge_file_input inputs[3] = { {0} };
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
assert(out && ours && theirs);
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
memset(out, 0x0, sizeof(git_merge_file_result));
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
if (ancestor)
|
|
Packit Service |
20376f |
ancestor = git_merge_file__normalize_inputs(&inputs[0], ancestor);
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
ours = git_merge_file__normalize_inputs(&inputs[1], ours);
|
|
Packit Service |
20376f |
theirs = git_merge_file__normalize_inputs(&inputs[2], theirs);
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
return merge_file__from_inputs(out, ancestor, ours, theirs, options);
|
|
Packit Service |
20376f |
}
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
int git_merge_file_from_index(
|
|
Packit Service |
20376f |
git_merge_file_result *out,
|
|
Packit Service |
20376f |
git_repository *repo,
|
|
Packit Service |
20376f |
const git_index_entry *ancestor,
|
|
Packit Service |
20376f |
const git_index_entry *ours,
|
|
Packit Service |
20376f |
const git_index_entry *theirs,
|
|
Packit Service |
20376f |
const git_merge_file_options *options)
|
|
Packit Service |
20376f |
{
|
|
Packit Service |
20376f |
git_merge_file_input *ancestor_ptr = NULL,
|
|
Packit Service |
20376f |
ancestor_input = {0}, our_input = {0}, their_input = {0};
|
|
Packit Service |
20376f |
git_odb *odb = NULL;
|
|
Packit Service |
20376f |
git_odb_object *odb_object[3] = { 0 };
|
|
Packit Service |
20376f |
int error = 0;
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
assert(out && repo && ours && theirs);
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
memset(out, 0x0, sizeof(git_merge_file_result));
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
if ((error = git_repository_odb(&odb, repo)) < 0)
|
|
Packit Service |
20376f |
goto done;
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
if (ancestor) {
|
|
Packit Service |
20376f |
if ((error = git_merge_file__input_from_index(
|
|
Packit Service |
20376f |
&ancestor_input, &odb_object[0], odb, ancestor)) < 0)
|
|
Packit Service |
20376f |
goto done;
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
ancestor_ptr = &ancestor_input;
|
|
Packit Service |
20376f |
}
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
if ((error = git_merge_file__input_from_index(
|
|
Packit Service |
20376f |
&our_input, &odb_object[1], odb, ours)) < 0 ||
|
|
Packit Service |
20376f |
(error = git_merge_file__input_from_index(
|
|
Packit Service |
20376f |
&their_input, &odb_object[2], odb, theirs)) < 0)
|
|
Packit Service |
20376f |
goto done;
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
error = merge_file__from_inputs(out,
|
|
Packit Service |
20376f |
ancestor_ptr, &our_input, &their_input, options);
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
done:
|
|
Packit Service |
20376f |
git_odb_object_free(odb_object[0]);
|
|
Packit Service |
20376f |
git_odb_object_free(odb_object[1]);
|
|
Packit Service |
20376f |
git_odb_object_free(odb_object[2]);
|
|
Packit Service |
20376f |
git_odb_free(odb);
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
return error;
|
|
Packit Service |
20376f |
}
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
void git_merge_file_result_free(git_merge_file_result *result)
|
|
Packit Service |
20376f |
{
|
|
Packit Service |
20376f |
if (result == NULL)
|
|
Packit Service |
20376f |
return;
|
|
Packit Service |
20376f |
|
|
Packit Service |
20376f |
git__free((char *)result->path);
|
|
Packit Service |
20376f |
git__free((char *)result->ptr);
|
|
Packit Service |
20376f |
}
|