]> git.proxmox.com Git - libgit2.git/blame - src/merge.h
New upstream version 1.3.0+dfsg.1
[libgit2.git] / src / merge.h
CommitLineData
632d8b23 1/*
359fc2d2 2 * Copyright (C) the libgit2 contributors. All rights reserved.
632d8b23
ET
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7#ifndef INCLUDE_merge_h__
8#define INCLUDE_merge_h__
9
eae0bfdc
PP
10#include "common.h"
11
42e50b5e 12#include "vector.h"
bec65a5e
ET
13#include "commit_list.h"
14#include "pool.h"
9ebb5a3f 15#include "iterator.h"
bec65a5e 16
bec65a5e 17#include "git2/types.h"
3f04219f
ET
18#include "git2/merge.h"
19#include "git2/sys/merge.h"
632d8b23
ET
20
21#define GIT_MERGE_MSG_FILE "MERGE_MSG"
22#define GIT_MERGE_MODE_FILE "MERGE_MODE"
1d3a8aeb 23#define GIT_MERGE_FILE_MODE 0666
632d8b23 24
fa78782f
ET
25#define GIT_MERGE_DEFAULT_RENAME_THRESHOLD 50
26#define GIT_MERGE_DEFAULT_TARGET_LIMIT 1000
bec65a5e 27
3f04219f
ET
28
29/** Internal merge flags. */
30enum {
31 /** The merge is for a virtual base in a recursive merge. */
32 GIT_MERGE__VIRTUAL_BASE = (1 << 31),
33};
34
35enum {
36 /** Accept the conflict file, staging it as the merge result. */
37 GIT_MERGE_FILE_FAVOR__CONFLICTED = 4,
38};
39
40
bec65a5e
ET
41/** Types of changes when files are merged from branch to branch. */
42typedef enum {
43 /* No conflict - a change only occurs in one branch. */
44 GIT_MERGE_DIFF_NONE = 0,
1fed6b07 45
bec65a5e
ET
46 /* Occurs when a file is modified in both branches. */
47 GIT_MERGE_DIFF_BOTH_MODIFIED = (1 << 0),
1fed6b07 48
bec65a5e
ET
49 /* Occurs when a file is added in both branches. */
50 GIT_MERGE_DIFF_BOTH_ADDED = (1 << 1),
1fed6b07 51
bec65a5e
ET
52 /* Occurs when a file is deleted in both branches. */
53 GIT_MERGE_DIFF_BOTH_DELETED = (1 << 2),
1fed6b07 54
bec65a5e
ET
55 /* Occurs when a file is modified in one branch and deleted in the other. */
56 GIT_MERGE_DIFF_MODIFIED_DELETED = (1 << 3),
1fed6b07 57
bec65a5e
ET
58 /* Occurs when a file is renamed in one branch and modified in the other. */
59 GIT_MERGE_DIFF_RENAMED_MODIFIED = (1 << 4),
1fed6b07 60
bec65a5e
ET
61 /* Occurs when a file is renamed in one branch and deleted in the other. */
62 GIT_MERGE_DIFF_RENAMED_DELETED = (1 << 5),
1fed6b07 63
bec65a5e
ET
64 /* Occurs when a file is renamed in one branch and a file with the same
65 * name is added in the other. Eg, A->B and new file B. Core git calls
66 * this a "rename/delete". */
67 GIT_MERGE_DIFF_RENAMED_ADDED = (1 << 6),
1fed6b07 68
bec65a5e
ET
69 /* Occurs when both a file is renamed to the same name in the ours and
70 * theirs branches. Eg, A->B and A->B in both. Automergeable. */
71 GIT_MERGE_DIFF_BOTH_RENAMED = (1 << 7),
1fed6b07 72
bec65a5e
ET
73 /* Occurs when a file is renamed to different names in the ours and theirs
74 * branches. Eg, A->B and A->C. */
75 GIT_MERGE_DIFF_BOTH_RENAMED_1_TO_2 = (1 << 8),
1fed6b07 76
bec65a5e
ET
77 /* Occurs when two files are renamed to the same name in the ours and
78 * theirs branches. Eg, A->C and B->C. */
79 GIT_MERGE_DIFF_BOTH_RENAMED_2_TO_1 = (1 << 9),
1fed6b07 80
bec65a5e
ET
81 /* Occurs when an item at a path in one branch is a directory, and an
82 * item at the same path in a different branch is a file. */
83 GIT_MERGE_DIFF_DIRECTORY_FILE = (1 << 10),
1fed6b07 84
bec65a5e
ET
85 /* The child of a folder that is in a directory/file conflict. */
86 GIT_MERGE_DIFF_DF_CHILD = (1 << 11),
22a2d3d5 87} git_merge_diff_t;
bec65a5e 88
bec65a5e 89typedef struct {
0cb16fe9
L
90 git_repository *repo;
91 git_pool pool;
1fed6b07 92
0cb16fe9 93 /* Vector of git_index_entry that represent the merged items that
bec65a5e
ET
94 * have been staged, either because only one side changed, or because
95 * the two changes were non-conflicting and mergeable. These items
96 * will be written as staged entries in the main index.
97 */
0cb16fe9 98 git_vector staged;
1fed6b07 99
0cb16fe9 100 /* Vector of git_merge_diff entries that represent the conflicts that
bec65a5e
ET
101 * have not been automerged. These items will be written to high-stage
102 * entries in the main index.
103 */
0cb16fe9 104 git_vector conflicts;
1fed6b07 105
0cb16fe9 106 /* Vector of git_merge_diff that have been automerged. These items
bec65a5e
ET
107 * will be written to the REUC when the index is produced.
108 */
0cb16fe9 109 git_vector resolved;
bec65a5e
ET
110} git_merge_diff_list;
111
112/**
113 * Description of changes to one file across three trees.
114 */
115typedef struct {
22a2d3d5 116 git_merge_diff_t type;
1fed6b07 117
0cb16fe9 118 git_index_entry ancestor_entry;
1fed6b07 119
0cb16fe9
L
120 git_index_entry our_entry;
121 git_delta_t our_status;
1fed6b07 122
0cb16fe9
L
123 git_index_entry their_entry;
124 git_delta_t their_status;
6b92c99b 125
bec65a5e
ET
126} git_merge_diff;
127
128int git_merge__bases_many(
129 git_commit_list **out,
130 git_revwalk *walk,
131 git_commit_list_node *one,
c25aa7cd
PP
132 git_vector *twos,
133 uint32_t minimum_generation);
bec65a5e 134
9c06b250
ET
135/*
136 * Three-way tree differencing
137 */
138
bec65a5e
ET
139git_merge_diff_list *git_merge_diff_list__alloc(git_repository *repo);
140
9ebb5a3f
ET
141int git_merge_diff_list__find_differences(
142 git_merge_diff_list *merge_diff_list,
143 git_iterator *ancestor_iterator,
144 git_iterator *ours_iter,
145 git_iterator *theirs_iter);
bec65a5e 146
5aa2ac6d 147int git_merge_diff_list__find_renames(git_repository *repo, git_merge_diff_list *merge_diff_list, const git_merge_options *opts);
632d8b23 148
bec65a5e 149void git_merge_diff_list__free(git_merge_diff_list *diff_list);
632d8b23 150
9c06b250
ET
151/* Merge metadata setup */
152
153int git_merge__setup(
154 git_repository *repo,
18b00406
ET
155 const git_annotated_commit *our_head,
156 const git_annotated_commit *heads[],
1c0b6a38 157 size_t heads_len);
9c06b250 158
9ebb5a3f
ET
159int git_merge__iterators(
160 git_index **out,
161 git_repository *repo,
162 git_iterator *ancestor_iter,
163 git_iterator *our_iter,
164 git_iterator *their_iter,
165 const git_merge_options *given_opts);
166
967f5a76 167int git_merge__check_result(git_repository *repo, git_index *index_new);
300d192f 168
399f2b62
JG
169int git_merge__append_conflicts_to_merge_msg(git_repository *repo, git_index *index);
170
3f04219f
ET
171/* Merge files */
172
173GIT_INLINE(const char *) git_merge_file__best_path(
174 const char *ancestor,
175 const char *ours,
176 const char *theirs)
177{
178 if (!ancestor) {
179 if (ours && theirs && strcmp(ours, theirs) == 0)
180 return ours;
181
182 return NULL;
183 }
184
185 if (ours && strcmp(ancestor, ours) == 0)
186 return theirs;
187 else if(theirs && strcmp(ancestor, theirs) == 0)
188 return ours;
189
190 return NULL;
191}
192
193GIT_INLINE(uint32_t) git_merge_file__best_mode(
194 uint32_t ancestor, uint32_t ours, uint32_t theirs)
195{
196 /*
197 * If ancestor didn't exist and either ours or theirs is executable,
198 * assume executable. Otherwise, if any mode changed from the ancestor,
199 * use that one.
200 */
201 if (!ancestor) {
202 if (ours == GIT_FILEMODE_BLOB_EXECUTABLE ||
203 theirs == GIT_FILEMODE_BLOB_EXECUTABLE)
204 return GIT_FILEMODE_BLOB_EXECUTABLE;
205
206 return GIT_FILEMODE_BLOB;
207 } else if (ours && theirs) {
208 if (ancestor == ours)
209 return theirs;
210
211 return ours;
212 }
213
214 return 0;
215}
216
632d8b23 217#endif