merge.h 5.72 KB
Newer Older
Edward Thomson committed
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
Edward Thomson committed
3 4 5 6 7 8 9
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */
#ifndef INCLUDE_merge_h__
#define INCLUDE_merge_h__

10 11
#include "common.h"

12
#include "vector.h"
Edward Thomson committed
13 14
#include "commit_list.h"
#include "pool.h"
15
#include "iterator.h"
Edward Thomson committed
16 17

#include "git2/types.h"
18 19
#include "git2/merge.h"
#include "git2/sys/merge.h"
Edward Thomson committed
20 21 22

#define GIT_MERGE_MSG_FILE		"MERGE_MSG"
#define GIT_MERGE_MODE_FILE		"MERGE_MODE"
23
#define GIT_MERGE_FILE_MODE		0666
Edward Thomson committed
24

25 26
#define GIT_MERGE_DEFAULT_RENAME_THRESHOLD	50
#define GIT_MERGE_DEFAULT_TARGET_LIMIT		1000
Edward Thomson committed
27

28 29 30 31 32 33 34 35 36 37 38 39 40

/** Internal merge flags. */
enum {
	/** The merge is for a virtual base in a recursive merge. */
	GIT_MERGE__VIRTUAL_BASE = (1 << 31),
};

enum {
	/** Accept the conflict file, staging it as the merge result. */
	GIT_MERGE_FILE_FAVOR__CONFLICTED = 4,
};


Edward Thomson committed
41 42 43 44
/** Types of changes when files are merged from branch to branch. */
typedef enum {
	/* No conflict - a change only occurs in one branch. */
	GIT_MERGE_DIFF_NONE = 0,
nulltoken committed
45

Edward Thomson committed
46 47
	/* Occurs when a file is modified in both branches. */
	GIT_MERGE_DIFF_BOTH_MODIFIED = (1 << 0),
nulltoken committed
48

Edward Thomson committed
49 50
	/* Occurs when a file is added in both branches. */
	GIT_MERGE_DIFF_BOTH_ADDED = (1 << 1),
nulltoken committed
51

Edward Thomson committed
52 53
	/* Occurs when a file is deleted in both branches. */
	GIT_MERGE_DIFF_BOTH_DELETED = (1 << 2),
nulltoken committed
54

Edward Thomson committed
55 56
	/* Occurs when a file is modified in one branch and deleted in the other. */
	GIT_MERGE_DIFF_MODIFIED_DELETED = (1 << 3),
nulltoken committed
57

Edward Thomson committed
58 59
	/* Occurs when a file is renamed in one branch and modified in the other. */
	GIT_MERGE_DIFF_RENAMED_MODIFIED = (1 << 4),
nulltoken committed
60

Edward Thomson committed
61 62
	/* Occurs when a file is renamed in one branch and deleted in the other. */
	GIT_MERGE_DIFF_RENAMED_DELETED = (1 << 5),
nulltoken committed
63

Edward Thomson committed
64 65 66 67
	/* Occurs when a file is renamed in one branch and a file with the same
	 * name is added in the other.  Eg, A->B and new file B.  Core git calls
	 * this a "rename/delete". */
	GIT_MERGE_DIFF_RENAMED_ADDED = (1 << 6),
nulltoken committed
68

Edward Thomson committed
69 70 71
	/* Occurs when both a file is renamed to the same name in the ours and
	 * theirs branches.  Eg, A->B and A->B in both.  Automergeable. */
	GIT_MERGE_DIFF_BOTH_RENAMED = (1 << 7),
nulltoken committed
72

Edward Thomson committed
73 74 75
	/* Occurs when a file is renamed to different names in the ours and theirs
	 * branches.  Eg, A->B and A->C. */
	GIT_MERGE_DIFF_BOTH_RENAMED_1_TO_2 = (1 << 8),
nulltoken committed
76

Edward Thomson committed
77 78 79
	/* Occurs when two files are renamed to the same name in the ours and
	 * theirs branches.  Eg, A->C and B->C. */
	GIT_MERGE_DIFF_BOTH_RENAMED_2_TO_1 = (1 << 9),
nulltoken committed
80

Edward Thomson committed
81 82 83
	/* Occurs when an item at a path in one branch is a directory, and an
	 * item at the same path in a different branch is a file. */
	GIT_MERGE_DIFF_DIRECTORY_FILE = (1 << 10),
nulltoken committed
84

Edward Thomson committed
85 86 87 88 89
	/* The child of a folder that is in a directory/file conflict. */
	GIT_MERGE_DIFF_DF_CHILD = (1 << 11),
} git_merge_diff_type_t;

typedef struct {
Linquize committed
90 91
	git_repository *repo;
	git_pool pool;
nulltoken committed
92

Linquize committed
93
	/* Vector of git_index_entry that represent the merged items that
Edward Thomson committed
94 95 96 97
	 * have been staged, either because only one side changed, or because
	 * the two changes were non-conflicting and mergeable.  These items
	 * will be written as staged entries in the main index.
	 */
Linquize committed
98
	git_vector staged;
nulltoken committed
99

Linquize committed
100
	/* Vector of git_merge_diff entries that represent the conflicts that
Edward Thomson committed
101 102 103
	 * have not been automerged.  These items will be written to high-stage
	 * entries in the main index.
	 */
Linquize committed
104
	git_vector conflicts;
nulltoken committed
105

Linquize committed
106
	/* Vector of git_merge_diff that have been automerged.  These items
Edward Thomson committed
107 108
	 * will be written to the REUC when the index is produced.
	 */
Linquize committed
109
	git_vector resolved;
Edward Thomson committed
110 111 112 113 114 115
} git_merge_diff_list;

/**
 * Description of changes to one file across three trees.
 */
typedef struct {
Linquize committed
116
	git_merge_diff_type_t type;
nulltoken committed
117

Linquize committed
118
	git_index_entry ancestor_entry;
nulltoken committed
119

Linquize committed
120 121
	git_index_entry our_entry;
	git_delta_t our_status;
nulltoken committed
122

Linquize committed
123 124
	git_index_entry their_entry;
	git_delta_t their_status;
125

Edward Thomson committed
126 127 128 129 130 131 132 133
} git_merge_diff;

int git_merge__bases_many(
	git_commit_list **out,
	git_revwalk *walk,
	git_commit_list_node *one,
	git_vector *twos);

Edward Thomson committed
134 135 136 137
/*
 * Three-way tree differencing
 */

Edward Thomson committed
138 139
git_merge_diff_list *git_merge_diff_list__alloc(git_repository *repo);

140 141 142 143 144
int git_merge_diff_list__find_differences(
	git_merge_diff_list *merge_diff_list,
	git_iterator *ancestor_iterator,
	git_iterator *ours_iter,
	git_iterator *theirs_iter);
Edward Thomson committed
145

146
int git_merge_diff_list__find_renames(git_repository *repo, git_merge_diff_list *merge_diff_list, const git_merge_options *opts);
Edward Thomson committed
147

Edward Thomson committed
148
void git_merge_diff_list__free(git_merge_diff_list *diff_list);
Edward Thomson committed
149

Edward Thomson committed
150 151 152 153
/* Merge metadata setup */

int git_merge__setup(
	git_repository *repo,
154 155
	const git_annotated_commit *our_head,
	const git_annotated_commit *heads[],
156
	size_t heads_len);
Edward Thomson committed
157

158 159 160 161 162 163 164 165
int git_merge__iterators(
	git_index **out,
	git_repository *repo,
	git_iterator *ancestor_iter,
	git_iterator *our_iter,
	git_iterator *their_iter,
	const git_merge_options *given_opts);

166
int git_merge__check_result(git_repository *repo, git_index *index_new);
167

168 169
int git_merge__append_conflicts_to_merge_msg(git_repository *repo, git_index *index);

170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
/* Merge files */

GIT_INLINE(const char *) git_merge_file__best_path(
	const char *ancestor,
	const char *ours,
	const char *theirs)
{
	if (!ancestor) {
		if (ours && theirs && strcmp(ours, theirs) == 0)
			return ours;

		return NULL;
	}

	if (ours && strcmp(ancestor, ours) == 0)
		return theirs;
	else if(theirs && strcmp(ancestor, theirs) == 0)
		return ours;

	return NULL;
}

GIT_INLINE(uint32_t) git_merge_file__best_mode(
	uint32_t ancestor, uint32_t ours, uint32_t theirs)
{
	/*
	 * If ancestor didn't exist and either ours or theirs is executable,
	 * assume executable.  Otherwise, if any mode changed from the ancestor,
	 * use that one.
	 */
	if (!ancestor) {
		if (ours == GIT_FILEMODE_BLOB_EXECUTABLE ||
			theirs == GIT_FILEMODE_BLOB_EXECUTABLE)
			return GIT_FILEMODE_BLOB_EXECUTABLE;

		return GIT_FILEMODE_BLOB;
	} else if (ours && theirs) {
		if (ancestor == ours)
			return theirs;

		return ours;
	}

	return 0;
}

Edward Thomson committed
216
#endif