]> git.proxmox.com Git - libgit2.git/blob - src/merge.h
New upstream version 1.3.0+dfsg.1
[libgit2.git] / src / merge.h
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
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
10 #include "common.h"
11
12 #include "vector.h"
13 #include "commit_list.h"
14 #include "pool.h"
15 #include "iterator.h"
16
17 #include "git2/types.h"
18 #include "git2/merge.h"
19 #include "git2/sys/merge.h"
20
21 #define GIT_MERGE_MSG_FILE "MERGE_MSG"
22 #define GIT_MERGE_MODE_FILE "MERGE_MODE"
23 #define GIT_MERGE_FILE_MODE 0666
24
25 #define GIT_MERGE_DEFAULT_RENAME_THRESHOLD 50
26 #define GIT_MERGE_DEFAULT_TARGET_LIMIT 1000
27
28
29 /** Internal merge flags. */
30 enum {
31 /** The merge is for a virtual base in a recursive merge. */
32 GIT_MERGE__VIRTUAL_BASE = (1 << 31),
33 };
34
35 enum {
36 /** Accept the conflict file, staging it as the merge result. */
37 GIT_MERGE_FILE_FAVOR__CONFLICTED = 4,
38 };
39
40
41 /** Types of changes when files are merged from branch to branch. */
42 typedef enum {
43 /* No conflict - a change only occurs in one branch. */
44 GIT_MERGE_DIFF_NONE = 0,
45
46 /* Occurs when a file is modified in both branches. */
47 GIT_MERGE_DIFF_BOTH_MODIFIED = (1 << 0),
48
49 /* Occurs when a file is added in both branches. */
50 GIT_MERGE_DIFF_BOTH_ADDED = (1 << 1),
51
52 /* Occurs when a file is deleted in both branches. */
53 GIT_MERGE_DIFF_BOTH_DELETED = (1 << 2),
54
55 /* Occurs when a file is modified in one branch and deleted in the other. */
56 GIT_MERGE_DIFF_MODIFIED_DELETED = (1 << 3),
57
58 /* Occurs when a file is renamed in one branch and modified in the other. */
59 GIT_MERGE_DIFF_RENAMED_MODIFIED = (1 << 4),
60
61 /* Occurs when a file is renamed in one branch and deleted in the other. */
62 GIT_MERGE_DIFF_RENAMED_DELETED = (1 << 5),
63
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),
68
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),
72
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),
76
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),
80
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),
84
85 /* The child of a folder that is in a directory/file conflict. */
86 GIT_MERGE_DIFF_DF_CHILD = (1 << 11),
87 } git_merge_diff_t;
88
89 typedef struct {
90 git_repository *repo;
91 git_pool pool;
92
93 /* Vector of git_index_entry that represent the merged items that
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 */
98 git_vector staged;
99
100 /* Vector of git_merge_diff entries that represent the conflicts that
101 * have not been automerged. These items will be written to high-stage
102 * entries in the main index.
103 */
104 git_vector conflicts;
105
106 /* Vector of git_merge_diff that have been automerged. These items
107 * will be written to the REUC when the index is produced.
108 */
109 git_vector resolved;
110 } git_merge_diff_list;
111
112 /**
113 * Description of changes to one file across three trees.
114 */
115 typedef struct {
116 git_merge_diff_t type;
117
118 git_index_entry ancestor_entry;
119
120 git_index_entry our_entry;
121 git_delta_t our_status;
122
123 git_index_entry their_entry;
124 git_delta_t their_status;
125
126 } git_merge_diff;
127
128 int git_merge__bases_many(
129 git_commit_list **out,
130 git_revwalk *walk,
131 git_commit_list_node *one,
132 git_vector *twos,
133 uint32_t minimum_generation);
134
135 /*
136 * Three-way tree differencing
137 */
138
139 git_merge_diff_list *git_merge_diff_list__alloc(git_repository *repo);
140
141 int 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);
146
147 int git_merge_diff_list__find_renames(git_repository *repo, git_merge_diff_list *merge_diff_list, const git_merge_options *opts);
148
149 void git_merge_diff_list__free(git_merge_diff_list *diff_list);
150
151 /* Merge metadata setup */
152
153 int git_merge__setup(
154 git_repository *repo,
155 const git_annotated_commit *our_head,
156 const git_annotated_commit *heads[],
157 size_t heads_len);
158
159 int 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
167 int git_merge__check_result(git_repository *repo, git_index *index_new);
168
169 int git_merge__append_conflicts_to_merge_msg(git_repository *repo, git_index *index);
170
171 /* Merge files */
172
173 GIT_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
193 GIT_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
217 #endif