2 * Copyright (C) the libgit2 contributors. All rights reserved.
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.
7 #ifndef INCLUDE_merge_h__
8 #define INCLUDE_merge_h__
13 #include "commit_list.h"
17 #include "git2/types.h"
18 #include "git2/merge.h"
19 #include "git2/sys/merge.h"
21 #define GIT_MERGE_MSG_FILE "MERGE_MSG"
22 #define GIT_MERGE_MODE_FILE "MERGE_MODE"
23 #define GIT_MERGE_FILE_MODE 0666
25 #define GIT_MERGE_DEFAULT_RENAME_THRESHOLD 50
26 #define GIT_MERGE_DEFAULT_TARGET_LIMIT 1000
29 /** Internal merge flags. */
31 /** The merge is for a virtual base in a recursive merge. */
32 GIT_MERGE__VIRTUAL_BASE
= (1 << 31),
36 /** Accept the conflict file, staging it as the merge result. */
37 GIT_MERGE_FILE_FAVOR__CONFLICTED
= 4,
41 /** Types of changes when files are merged from branch to branch. */
43 /* No conflict - a change only occurs in one branch. */
44 GIT_MERGE_DIFF_NONE
= 0,
46 /* Occurs when a file is modified in both branches. */
47 GIT_MERGE_DIFF_BOTH_MODIFIED
= (1 << 0),
49 /* Occurs when a file is added in both branches. */
50 GIT_MERGE_DIFF_BOTH_ADDED
= (1 << 1),
52 /* Occurs when a file is deleted in both branches. */
53 GIT_MERGE_DIFF_BOTH_DELETED
= (1 << 2),
55 /* Occurs when a file is modified in one branch and deleted in the other. */
56 GIT_MERGE_DIFF_MODIFIED_DELETED
= (1 << 3),
58 /* Occurs when a file is renamed in one branch and modified in the other. */
59 GIT_MERGE_DIFF_RENAMED_MODIFIED
= (1 << 4),
61 /* Occurs when a file is renamed in one branch and deleted in the other. */
62 GIT_MERGE_DIFF_RENAMED_DELETED
= (1 << 5),
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),
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),
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),
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),
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),
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
;
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.
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.
104 git_vector conflicts
;
106 /* Vector of git_merge_diff that have been automerged. These items
107 * will be written to the REUC when the index is produced.
110 } git_merge_diff_list
;
113 * Description of changes to one file across three trees.
116 git_merge_diff_type_t type
;
118 git_index_entry ancestor_entry
;
120 git_index_entry our_entry
;
121 git_delta_t our_status
;
123 git_index_entry their_entry
;
124 git_delta_t their_status
;
128 int git_merge__bases_many(
129 git_commit_list
**out
,
131 git_commit_list_node
*one
,
135 * Three-way tree differencing
138 git_merge_diff_list
*git_merge_diff_list__alloc(git_repository
*repo
);
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
);
146 int git_merge_diff_list__find_renames(git_repository
*repo
, git_merge_diff_list
*merge_diff_list
, const git_merge_options
*opts
);
148 void git_merge_diff_list__free(git_merge_diff_list
*diff_list
);
150 /* Merge metadata setup */
152 int git_merge__setup(
153 git_repository
*repo
,
154 const git_annotated_commit
*our_head
,
155 const git_annotated_commit
*heads
[],
158 int git_merge__iterators(
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
);
166 int git_merge__check_result(git_repository
*repo
, git_index
*index_new
);
168 int git_merge__append_conflicts_to_merge_msg(git_repository
*repo
, git_index
*index
);
172 GIT_INLINE(const char *) git_merge_file__best_path(
173 const char *ancestor
,
178 if (ours
&& theirs
&& strcmp(ours
, theirs
) == 0)
184 if (ours
&& strcmp(ancestor
, ours
) == 0)
186 else if(theirs
&& strcmp(ancestor
, theirs
) == 0)
192 GIT_INLINE(uint32_t) git_merge_file__best_mode(
193 uint32_t ancestor
, uint32_t ours
, uint32_t theirs
)
196 * If ancestor didn't exist and either ours or theirs is executable,
197 * assume executable. Otherwise, if any mode changed from the ancestor,
201 if (ours
== GIT_FILEMODE_BLOB_EXECUTABLE
||
202 theirs
== GIT_FILEMODE_BLOB_EXECUTABLE
)
203 return GIT_FILEMODE_BLOB_EXECUTABLE
;
205 return GIT_FILEMODE_BLOB
;
206 } else if (ours
&& theirs
) {
207 if (ancestor
== ours
)