Blame src/merge.h

Packit ae9e2a
/*
Packit ae9e2a
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit ae9e2a
 *
Packit ae9e2a
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit ae9e2a
 * a Linking Exception. For full terms see the included COPYING file.
Packit ae9e2a
 */
Packit ae9e2a
#ifndef INCLUDE_merge_h__
Packit ae9e2a
#define INCLUDE_merge_h__
Packit ae9e2a
Packit ae9e2a
#include "vector.h"
Packit ae9e2a
#include "commit_list.h"
Packit ae9e2a
#include "pool.h"
Packit ae9e2a
#include "iterator.h"
Packit ae9e2a
Packit ae9e2a
#include "git2/types.h"
Packit ae9e2a
#include "git2/merge.h"
Packit ae9e2a
#include "git2/sys/merge.h"
Packit ae9e2a
Packit ae9e2a
#define GIT_MERGE_MSG_FILE		"MERGE_MSG"
Packit ae9e2a
#define GIT_MERGE_MODE_FILE		"MERGE_MODE"
Packit ae9e2a
#define GIT_MERGE_FILE_MODE		0666
Packit ae9e2a
Packit ae9e2a
#define GIT_MERGE_DEFAULT_RENAME_THRESHOLD	50
Packit ae9e2a
#define GIT_MERGE_DEFAULT_TARGET_LIMIT		1000
Packit ae9e2a
Packit ae9e2a
Packit ae9e2a
/** Internal merge flags. */
Packit ae9e2a
enum {
Packit ae9e2a
	/** The merge is for a virtual base in a recursive merge. */
Packit ae9e2a
	GIT_MERGE__VIRTUAL_BASE = (1 << 31),
Packit ae9e2a
};
Packit ae9e2a
Packit ae9e2a
enum {
Packit ae9e2a
	/** Accept the conflict file, staging it as the merge result. */
Packit ae9e2a
	GIT_MERGE_FILE_FAVOR__CONFLICTED = 4,
Packit ae9e2a
};
Packit ae9e2a
Packit ae9e2a
Packit ae9e2a
/** Types of changes when files are merged from branch to branch. */
Packit ae9e2a
typedef enum {
Packit ae9e2a
	/* No conflict - a change only occurs in one branch. */
Packit ae9e2a
	GIT_MERGE_DIFF_NONE = 0,
Packit ae9e2a
Packit ae9e2a
	/* Occurs when a file is modified in both branches. */
Packit ae9e2a
	GIT_MERGE_DIFF_BOTH_MODIFIED = (1 << 0),
Packit ae9e2a
Packit ae9e2a
	/* Occurs when a file is added in both branches. */
Packit ae9e2a
	GIT_MERGE_DIFF_BOTH_ADDED = (1 << 1),
Packit ae9e2a
Packit ae9e2a
	/* Occurs when a file is deleted in both branches. */
Packit ae9e2a
	GIT_MERGE_DIFF_BOTH_DELETED = (1 << 2),
Packit ae9e2a
Packit ae9e2a
	/* Occurs when a file is modified in one branch and deleted in the other. */
Packit ae9e2a
	GIT_MERGE_DIFF_MODIFIED_DELETED = (1 << 3),
Packit ae9e2a
Packit ae9e2a
	/* Occurs when a file is renamed in one branch and modified in the other. */
Packit ae9e2a
	GIT_MERGE_DIFF_RENAMED_MODIFIED = (1 << 4),
Packit ae9e2a
Packit ae9e2a
	/* Occurs when a file is renamed in one branch and deleted in the other. */
Packit ae9e2a
	GIT_MERGE_DIFF_RENAMED_DELETED = (1 << 5),
Packit ae9e2a
Packit ae9e2a
	/* Occurs when a file is renamed in one branch and a file with the same
Packit ae9e2a
	 * name is added in the other.  Eg, A->B and new file B.  Core git calls
Packit ae9e2a
	 * this a "rename/delete". */
Packit ae9e2a
	GIT_MERGE_DIFF_RENAMED_ADDED = (1 << 6),
Packit ae9e2a
Packit ae9e2a
	/* Occurs when both a file is renamed to the same name in the ours and
Packit ae9e2a
	 * theirs branches.  Eg, A->B and A->B in both.  Automergeable. */
Packit ae9e2a
	GIT_MERGE_DIFF_BOTH_RENAMED = (1 << 7),
Packit ae9e2a
Packit ae9e2a
	/* Occurs when a file is renamed to different names in the ours and theirs
Packit ae9e2a
	 * branches.  Eg, A->B and A->C. */
Packit ae9e2a
	GIT_MERGE_DIFF_BOTH_RENAMED_1_TO_2 = (1 << 8),
Packit ae9e2a
Packit ae9e2a
	/* Occurs when two files are renamed to the same name in the ours and
Packit ae9e2a
	 * theirs branches.  Eg, A->C and B->C. */
Packit ae9e2a
	GIT_MERGE_DIFF_BOTH_RENAMED_2_TO_1 = (1 << 9),
Packit ae9e2a
Packit ae9e2a
	/* Occurs when an item at a path in one branch is a directory, and an
Packit ae9e2a
	 * item at the same path in a different branch is a file. */
Packit ae9e2a
	GIT_MERGE_DIFF_DIRECTORY_FILE = (1 << 10),
Packit ae9e2a
Packit ae9e2a
	/* The child of a folder that is in a directory/file conflict. */
Packit ae9e2a
	GIT_MERGE_DIFF_DF_CHILD = (1 << 11),
Packit ae9e2a
} git_merge_diff_type_t;
Packit ae9e2a
Packit ae9e2a
typedef struct {
Packit ae9e2a
	git_repository *repo;
Packit ae9e2a
	git_pool pool;
Packit ae9e2a
Packit ae9e2a
	/* Vector of git_index_entry that represent the merged items that
Packit ae9e2a
	 * have been staged, either because only one side changed, or because
Packit ae9e2a
	 * the two changes were non-conflicting and mergeable.  These items
Packit ae9e2a
	 * will be written as staged entries in the main index.
Packit ae9e2a
	 */
Packit ae9e2a
	git_vector staged;
Packit ae9e2a
Packit ae9e2a
	/* Vector of git_merge_diff entries that represent the conflicts that
Packit ae9e2a
	 * have not been automerged.  These items will be written to high-stage
Packit ae9e2a
	 * entries in the main index.
Packit ae9e2a
	 */
Packit ae9e2a
	git_vector conflicts;
Packit ae9e2a
Packit ae9e2a
	/* Vector of git_merge_diff that have been automerged.  These items
Packit ae9e2a
	 * will be written to the REUC when the index is produced.
Packit ae9e2a
	 */
Packit ae9e2a
	git_vector resolved;
Packit ae9e2a
} git_merge_diff_list;
Packit ae9e2a
Packit ae9e2a
/**
Packit ae9e2a
 * Description of changes to one file across three trees.
Packit ae9e2a
 */
Packit ae9e2a
typedef struct {
Packit ae9e2a
	git_merge_diff_type_t type;
Packit ae9e2a
Packit ae9e2a
	git_index_entry ancestor_entry;
Packit ae9e2a
Packit ae9e2a
	git_index_entry our_entry;
Packit ae9e2a
	git_delta_t our_status;
Packit ae9e2a
Packit ae9e2a
	git_index_entry their_entry;
Packit ae9e2a
	git_delta_t their_status;
Packit ae9e2a
Packit ae9e2a
} git_merge_diff;
Packit ae9e2a
Packit ae9e2a
int git_merge__bases_many(
Packit ae9e2a
	git_commit_list **out,
Packit ae9e2a
	git_revwalk *walk,
Packit ae9e2a
	git_commit_list_node *one,
Packit ae9e2a
	git_vector *twos);
Packit ae9e2a
Packit ae9e2a
/*
Packit ae9e2a
 * Three-way tree differencing
Packit ae9e2a
 */
Packit ae9e2a
Packit ae9e2a
git_merge_diff_list *git_merge_diff_list__alloc(git_repository *repo);
Packit ae9e2a
Packit ae9e2a
int git_merge_diff_list__find_differences(
Packit ae9e2a
	git_merge_diff_list *merge_diff_list,
Packit ae9e2a
	git_iterator *ancestor_iterator,
Packit ae9e2a
	git_iterator *ours_iter,
Packit ae9e2a
	git_iterator *theirs_iter);
Packit ae9e2a
Packit ae9e2a
int git_merge_diff_list__find_renames(git_repository *repo, git_merge_diff_list *merge_diff_list, const git_merge_options *opts);
Packit ae9e2a
Packit ae9e2a
void git_merge_diff_list__free(git_merge_diff_list *diff_list);
Packit ae9e2a
Packit ae9e2a
/* Merge metadata setup */
Packit ae9e2a
Packit ae9e2a
int git_merge__setup(
Packit ae9e2a
	git_repository *repo,
Packit ae9e2a
	const git_annotated_commit *our_head,
Packit ae9e2a
	const git_annotated_commit *heads[],
Packit ae9e2a
	size_t heads_len);
Packit ae9e2a
Packit ae9e2a
int git_merge__iterators(
Packit ae9e2a
	git_index **out,
Packit ae9e2a
	git_repository *repo,
Packit ae9e2a
	git_iterator *ancestor_iter,
Packit ae9e2a
	git_iterator *our_iter,
Packit ae9e2a
	git_iterator *their_iter,
Packit ae9e2a
	const git_merge_options *given_opts);
Packit ae9e2a
Packit ae9e2a
int git_merge__check_result(git_repository *repo, git_index *index_new);
Packit ae9e2a
Packit ae9e2a
int git_merge__append_conflicts_to_merge_msg(git_repository *repo, git_index *index);
Packit ae9e2a
Packit ae9e2a
/* Merge files */
Packit ae9e2a
Packit ae9e2a
GIT_INLINE(const char *) git_merge_file__best_path(
Packit ae9e2a
	const char *ancestor,
Packit ae9e2a
	const char *ours,
Packit ae9e2a
	const char *theirs)
Packit ae9e2a
{
Packit ae9e2a
	if (!ancestor) {
Packit ae9e2a
		if (ours && theirs && strcmp(ours, theirs) == 0)
Packit ae9e2a
			return ours;
Packit ae9e2a
Packit ae9e2a
		return NULL;
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	if (ours && strcmp(ancestor, ours) == 0)
Packit ae9e2a
		return theirs;
Packit ae9e2a
	else if(theirs && strcmp(ancestor, theirs) == 0)
Packit ae9e2a
		return ours;
Packit ae9e2a
Packit ae9e2a
	return NULL;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
GIT_INLINE(uint32_t) git_merge_file__best_mode(
Packit ae9e2a
	uint32_t ancestor, uint32_t ours, uint32_t theirs)
Packit ae9e2a
{
Packit ae9e2a
	/*
Packit ae9e2a
	 * If ancestor didn't exist and either ours or theirs is executable,
Packit ae9e2a
	 * assume executable.  Otherwise, if any mode changed from the ancestor,
Packit ae9e2a
	 * use that one.
Packit ae9e2a
	 */
Packit ae9e2a
	if (!ancestor) {
Packit ae9e2a
		if (ours == GIT_FILEMODE_BLOB_EXECUTABLE ||
Packit ae9e2a
			theirs == GIT_FILEMODE_BLOB_EXECUTABLE)
Packit ae9e2a
			return GIT_FILEMODE_BLOB_EXECUTABLE;
Packit ae9e2a
Packit ae9e2a
		return GIT_FILEMODE_BLOB;
Packit ae9e2a
	} else if (ours && theirs) {
Packit ae9e2a
		if (ancestor == ours)
Packit ae9e2a
			return theirs;
Packit ae9e2a
Packit ae9e2a
		return ours;
Packit ae9e2a
	}
Packit ae9e2a
Packit ae9e2a
	return 0;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
#endif