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_type_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_type_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 
134 /*
135  * Three-way tree differencing
136  */
137 
138 git_merge_diff_list *git_merge_diff_list__alloc(git_repository *repo);
139 
140 int git_merge_diff_list__find_differences(
141 	git_merge_diff_list *merge_diff_list,
142 	git_iterator *ancestor_iterator,
143 	git_iterator *ours_iter,
144 	git_iterator *theirs_iter);
145 
146 int git_merge_diff_list__find_renames(git_repository *repo, git_merge_diff_list *merge_diff_list, const git_merge_options *opts);
147 
148 void git_merge_diff_list__free(git_merge_diff_list *diff_list);
149 
150 /* Merge metadata setup */
151 
152 int git_merge__setup(
153 	git_repository *repo,
154 	const git_annotated_commit *our_head,
155 	const git_annotated_commit *heads[],
156 	size_t heads_len);
157 
158 int git_merge__iterators(
159 	git_index **out,
160 	git_repository *repo,
161 	git_iterator *ancestor_iter,
162 	git_iterator *our_iter,
163 	git_iterator *their_iter,
164 	const git_merge_options *given_opts);
165 
166 int git_merge__check_result(git_repository *repo, git_index *index_new);
167 
168 int git_merge__append_conflicts_to_merge_msg(git_repository *repo, git_index *index);
169 
170 /* Merge files */
171 
git_merge_file__best_path(const char * ancestor,const char * ours,const char * theirs)172 GIT_INLINE(const char *) git_merge_file__best_path(
173 	const char *ancestor,
174 	const char *ours,
175 	const char *theirs)
176 {
177 	if (!ancestor) {
178 		if (ours && theirs && strcmp(ours, theirs) == 0)
179 			return ours;
180 
181 		return NULL;
182 	}
183 
184 	if (ours && strcmp(ancestor, ours) == 0)
185 		return theirs;
186 	else if(theirs && strcmp(ancestor, theirs) == 0)
187 		return ours;
188 
189 	return NULL;
190 }
191 
git_merge_file__best_mode(uint32_t ancestor,uint32_t ours,uint32_t theirs)192 GIT_INLINE(uint32_t) git_merge_file__best_mode(
193 	uint32_t ancestor, uint32_t ours, uint32_t theirs)
194 {
195 	/*
196 	 * If ancestor didn't exist and either ours or theirs is executable,
197 	 * assume executable.  Otherwise, if any mode changed from the ancestor,
198 	 * use that one.
199 	 */
200 	if (!ancestor) {
201 		if (ours == GIT_FILEMODE_BLOB_EXECUTABLE ||
202 			theirs == GIT_FILEMODE_BLOB_EXECUTABLE)
203 			return GIT_FILEMODE_BLOB_EXECUTABLE;
204 
205 		return GIT_FILEMODE_BLOB;
206 	} else if (ours && theirs) {
207 		if (ancestor == ours)
208 			return theirs;
209 
210 		return ours;
211 	}
212 
213 	return 0;
214 }
215 
216 #endif
217