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_git_merge_h__
8 #define INCLUDE_git_merge_h__
16 #include "annotated_commit.h"
20 * @brief Git merge routines
21 * @defgroup git_merge Git merge routines
28 * The file inputs to `git_merge_file`. Callers should populate the
29 * `git_merge_file_input` structure with descriptions of the files in
30 * each side of the conflict for use in producing the merge file.
35 /** Pointer to the contents of the file. */
38 /** Size of the contents pointed to in `ptr`. */
41 /** File name of the conflicted file, or `NULL` to not merge the path. */
44 /** File mode of the conflicted file, or `0` to not merge the mode. */
46 } git_merge_file_input
;
48 #define GIT_MERGE_FILE_INPUT_VERSION 1
49 #define GIT_MERGE_FILE_INPUT_INIT {GIT_MERGE_FILE_INPUT_VERSION}
52 * Initializes a `git_merge_file_input` with default values. Equivalent to
53 * creating an instance with GIT_MERGE_FILE_INPUT_INIT.
55 * @param opts the `git_merge_file_input` instance to initialize.
56 * @param version the version of the struct; you should pass
57 * `GIT_MERGE_FILE_INPUT_VERSION` here.
58 * @return Zero on success; -1 on failure.
60 GIT_EXTERN(int) git_merge_file_input_init(
61 git_merge_file_input
*opts
,
62 unsigned int version
);
65 * Flags for `git_merge` options. A combination of these flags can be
66 * passed in via the `flags` value in the `git_merge_options`.
70 * Detect renames that occur between the common ancestor and the "ours"
71 * side or the common ancestor and the "theirs" side. This will enable
72 * the ability to merge between a modified and renamed file.
74 GIT_MERGE_FIND_RENAMES
= (1 << 0),
77 * If a conflict occurs, exit immediately instead of attempting to
78 * continue resolving conflicts. The merge operation will fail with
79 * GIT_EMERGECONFLICT and no index will be returned.
81 GIT_MERGE_FAIL_ON_CONFLICT
= (1 << 1),
84 * Do not write the REUC extension on the generated index
86 GIT_MERGE_SKIP_REUC
= (1 << 2),
89 * If the commits being merged have multiple merge bases, do not build
90 * a recursive merge base (by merging the multiple merge bases),
91 * instead simply use the first base. This flag provides a similar
92 * merge base to `git-merge-resolve`.
94 GIT_MERGE_NO_RECURSIVE
= (1 << 3),
97 * Treat this merge as if it is to produce the virtual base
98 * of a recursive merge. This will ensure that there are
99 * no conflicts, any conflicting regions will keep conflict
100 * markers in the merge result.
102 GIT_MERGE_VIRTUAL_BASE
= (1 << 4)
106 * Merge file favor options for `git_merge_options` instruct the file-level
107 * merging functionality how to deal with conflicting regions of the files.
111 * When a region of a file is changed in both branches, a conflict
112 * will be recorded in the index so that `git_checkout` can produce
113 * a merge file with conflict markers in the working directory.
114 * This is the default.
116 GIT_MERGE_FILE_FAVOR_NORMAL
= 0,
119 * When a region of a file is changed in both branches, the file
120 * created in the index will contain the "ours" side of any conflicting
121 * region. The index will not record a conflict.
123 GIT_MERGE_FILE_FAVOR_OURS
= 1,
126 * When a region of a file is changed in both branches, the file
127 * created in the index will contain the "theirs" side of any conflicting
128 * region. The index will not record a conflict.
130 GIT_MERGE_FILE_FAVOR_THEIRS
= 2,
133 * When a region of a file is changed in both branches, the file
134 * created in the index will contain each unique line from each side,
135 * which has the result of combining both files. The index will not
138 GIT_MERGE_FILE_FAVOR_UNION
= 3
139 } git_merge_file_favor_t
;
146 GIT_MERGE_FILE_DEFAULT
= 0,
148 /** Create standard conflicted merge files */
149 GIT_MERGE_FILE_STYLE_MERGE
= (1 << 0),
151 /** Create diff3-style files */
152 GIT_MERGE_FILE_STYLE_DIFF3
= (1 << 1),
154 /** Condense non-alphanumeric regions for simplified diff file */
155 GIT_MERGE_FILE_SIMPLIFY_ALNUM
= (1 << 2),
157 /** Ignore all whitespace */
158 GIT_MERGE_FILE_IGNORE_WHITESPACE
= (1 << 3),
160 /** Ignore changes in amount of whitespace */
161 GIT_MERGE_FILE_IGNORE_WHITESPACE_CHANGE
= (1 << 4),
163 /** Ignore whitespace at end of line */
164 GIT_MERGE_FILE_IGNORE_WHITESPACE_EOL
= (1 << 5),
166 /** Use the "patience diff" algorithm */
167 GIT_MERGE_FILE_DIFF_PATIENCE
= (1 << 6),
169 /** Take extra time to find minimal diff */
170 GIT_MERGE_FILE_DIFF_MINIMAL
= (1 << 7),
172 /** Create zdiff3 ("zealous diff3")-style files */
173 GIT_MERGE_FILE_STYLE_ZDIFF3
= (1 << 8),
176 * Do not produce file conflicts when common regions have
177 * changed; keep the conflict markers in the file and accept
178 * that as the merge result.
180 GIT_MERGE_FILE_ACCEPT_CONFLICTS
= (1 << 9)
181 } git_merge_file_flag_t
;
183 #define GIT_MERGE_CONFLICT_MARKER_SIZE 7
186 * Options for merging a file
189 unsigned int version
;
192 * Label for the ancestor file side of the conflict which will be prepended
193 * to labels in diff3-format merge files.
195 const char *ancestor_label
;
198 * Label for our file side of the conflict which will be prepended
199 * to labels in merge files.
201 const char *our_label
;
204 * Label for their file side of the conflict which will be prepended
205 * to labels in merge files.
207 const char *their_label
;
209 /** The file to favor in region conflicts. */
210 git_merge_file_favor_t favor
;
212 /** see `git_merge_file_flag_t` above */
215 /** The size of conflict markers (eg, "<<<<<<<"). Default is
216 * GIT_MERGE_CONFLICT_MARKER_SIZE. */
217 unsigned short marker_size
;
218 } git_merge_file_options
;
220 #define GIT_MERGE_FILE_OPTIONS_VERSION 1
221 #define GIT_MERGE_FILE_OPTIONS_INIT {GIT_MERGE_FILE_OPTIONS_VERSION}
224 * Initialize git_merge_file_options structure
226 * Initializes a `git_merge_file_options` with default values. Equivalent to
227 * creating an instance with `GIT_MERGE_FILE_OPTIONS_INIT`.
229 * @param opts The `git_merge_file_options` struct to initialize.
230 * @param version The struct version; pass `GIT_MERGE_FILE_OPTIONS_VERSION`.
231 * @return Zero on success; -1 on failure.
233 GIT_EXTERN(int) git_merge_file_options_init(git_merge_file_options
*opts
, unsigned int version
);
236 * Information about file-level merging
240 * True if the output was automerged, false if the output contains
243 unsigned int automergeable
;
246 * The path that the resultant merge file should use, or NULL if a
247 * filename conflict would occur.
251 /** The mode that the resultant merge file should use. */
254 /** The contents of the merge. */
257 /** The length of the merge contents. */
259 } git_merge_file_result
;
265 unsigned int version
;
267 /** See `git_merge_flag_t` above */
271 * Similarity to consider a file renamed (default 50). If
272 * `GIT_MERGE_FIND_RENAMES` is enabled, added files will be compared
273 * with deleted files to determine their similarity. Files that are
274 * more similar than the rename threshold (percentage-wise) will be
275 * treated as a rename.
277 unsigned int rename_threshold
;
280 * Maximum similarity sources to examine for renames (default 200).
281 * If the number of rename candidates (add / delete pairs) is greater
282 * than this value, inexact rename detection is aborted.
284 * This setting overrides the `merge.renameLimit` configuration value.
286 unsigned int target_limit
;
288 /** Pluggable similarity metric; pass NULL to use internal metric */
289 git_diff_similarity_metric
*metric
;
292 * Maximum number of times to merge common ancestors to build a
293 * virtual merge base when faced with criss-cross merges. When this
294 * limit is reached, the next ancestor will simply be used instead of
295 * attempting to merge it. The default is unlimited.
297 unsigned int recursion_limit
;
300 * Default merge driver to be used when both sides of a merge have
301 * changed. The default is the `text` driver.
303 const char *default_driver
;
306 * Flags for handling conflicting content, to be used with the standard
307 * (`text`) merge driver.
309 git_merge_file_favor_t file_favor
;
311 /** see `git_merge_file_flag_t` above */
315 #define GIT_MERGE_OPTIONS_VERSION 1
316 #define GIT_MERGE_OPTIONS_INIT { \
317 GIT_MERGE_OPTIONS_VERSION, GIT_MERGE_FIND_RENAMES }
320 * Initialize git_merge_options structure
322 * Initializes a `git_merge_options` with default values. Equivalent to
323 * creating an instance with `GIT_MERGE_OPTIONS_INIT`.
325 * @param opts The `git_merge_options` struct to initialize.
326 * @param version The struct version; pass `GIT_MERGE_OPTIONS_VERSION`.
327 * @return Zero on success; -1 on failure.
329 GIT_EXTERN(int) git_merge_options_init(git_merge_options
*opts
, unsigned int version
);
332 * The results of `git_merge_analysis` indicate the merge opportunities.
335 /** No merge is possible. (Unused.) */
336 GIT_MERGE_ANALYSIS_NONE
= 0,
339 * A "normal" merge; both HEAD and the given merge input have diverged
340 * from their common ancestor. The divergent commits must be merged.
342 GIT_MERGE_ANALYSIS_NORMAL
= (1 << 0),
345 * All given merge inputs are reachable from HEAD, meaning the
346 * repository is up-to-date and no merge needs to be performed.
348 GIT_MERGE_ANALYSIS_UP_TO_DATE
= (1 << 1),
351 * The given merge input is a fast-forward from HEAD and no merge
352 * needs to be performed. Instead, the client can check out the
355 GIT_MERGE_ANALYSIS_FASTFORWARD
= (1 << 2),
358 * The HEAD of the current repository is "unborn" and does not point to
359 * a valid commit. No merge can be performed, but the caller may wish
360 * to simply set HEAD to the target commit(s).
362 GIT_MERGE_ANALYSIS_UNBORN
= (1 << 3)
363 } git_merge_analysis_t
;
366 * The user's stated preference for merges.
370 * No configuration was found that suggests a preferred behavior for
373 GIT_MERGE_PREFERENCE_NONE
= 0,
376 * There is a `merge.ff=false` configuration setting, suggesting that
377 * the user does not want to allow a fast-forward merge.
379 GIT_MERGE_PREFERENCE_NO_FASTFORWARD
= (1 << 0),
382 * There is a `merge.ff=only` configuration setting, suggesting that
383 * the user only wants fast-forward merges.
385 GIT_MERGE_PREFERENCE_FASTFORWARD_ONLY
= (1 << 1)
386 } git_merge_preference_t
;
389 * Analyzes the given branch(es) and determines the opportunities for
390 * merging them into the HEAD of the repository.
392 * @param analysis_out analysis enumeration that the result is written into
393 * @param preference_out One of the `git_merge_preference_t` flag.
394 * @param repo the repository to merge
395 * @param their_heads the heads to merge into
396 * @param their_heads_len the number of heads to merge
397 * @return 0 on success or error code
399 GIT_EXTERN(int) git_merge_analysis(
400 git_merge_analysis_t
*analysis_out
,
401 git_merge_preference_t
*preference_out
,
402 git_repository
*repo
,
403 const git_annotated_commit
**their_heads
,
404 size_t their_heads_len
);
407 * Analyzes the given branch(es) and determines the opportunities for
408 * merging them into a reference.
410 * @param analysis_out analysis enumeration that the result is written into
411 * @param preference_out One of the `git_merge_preference_t` flag.
412 * @param repo the repository to merge
413 * @param our_ref the reference to perform the analysis from
414 * @param their_heads the heads to merge into
415 * @param their_heads_len the number of heads to merge
416 * @return 0 on success or error code
418 GIT_EXTERN(int) git_merge_analysis_for_ref(
419 git_merge_analysis_t
*analysis_out
,
420 git_merge_preference_t
*preference_out
,
421 git_repository
*repo
,
422 git_reference
*our_ref
,
423 const git_annotated_commit
**their_heads
,
424 size_t their_heads_len
);
427 * Find a merge base between two commits
429 * @param out the OID of a merge base between 'one' and 'two'
430 * @param repo the repository where the commits exist
431 * @param one one of the commits
432 * @param two the other commit
433 * @return 0 on success, GIT_ENOTFOUND if not found or error code
435 GIT_EXTERN(int) git_merge_base(
437 git_repository
*repo
,
442 * Find merge bases between two commits
444 * @param out array in which to store the resulting ids
445 * @param repo the repository where the commits exist
446 * @param one one of the commits
447 * @param two the other commit
448 * @return 0 on success, GIT_ENOTFOUND if not found or error code
450 GIT_EXTERN(int) git_merge_bases(
452 git_repository
*repo
,
457 * Find a merge base given a list of commits
459 * @param out the OID of a merge base considering all the commits
460 * @param repo the repository where the commits exist
461 * @param length The number of commits in the provided `input_array`
462 * @param input_array oids of the commits
463 * @return Zero on success; GIT_ENOTFOUND or -1 on failure.
465 GIT_EXTERN(int) git_merge_base_many(
467 git_repository
*repo
,
469 const git_oid input_array
[]);
472 * Find all merge bases given a list of commits
474 * @param out array in which to store the resulting ids
475 * @param repo the repository where the commits exist
476 * @param length The number of commits in the provided `input_array`
477 * @param input_array oids of the commits
478 * @return Zero on success; GIT_ENOTFOUND or -1 on failure.
480 GIT_EXTERN(int) git_merge_bases_many(
482 git_repository
*repo
,
484 const git_oid input_array
[]);
487 * Find a merge base in preparation for an octopus merge
489 * @param out the OID of a merge base considering all the commits
490 * @param repo the repository where the commits exist
491 * @param length The number of commits in the provided `input_array`
492 * @param input_array oids of the commits
493 * @return Zero on success; GIT_ENOTFOUND or -1 on failure.
495 GIT_EXTERN(int) git_merge_base_octopus(
497 git_repository
*repo
,
499 const git_oid input_array
[]);
502 * Merge two files as they exist in the in-memory data structures, using
503 * the given common ancestor as the baseline, producing a
504 * `git_merge_file_result` that reflects the merge result. The
505 * `git_merge_file_result` must be freed with `git_merge_file_result_free`.
507 * Note that this function does not reference a repository and any
508 * configuration must be passed as `git_merge_file_options`.
510 * @param out The git_merge_file_result to be filled in
511 * @param ancestor The contents of the ancestor file
512 * @param ours The contents of the file in "our" side
513 * @param theirs The contents of the file in "their" side
514 * @param opts The merge file options or `NULL` for defaults
515 * @return 0 on success or error code
517 GIT_EXTERN(int) git_merge_file(
518 git_merge_file_result
*out
,
519 const git_merge_file_input
*ancestor
,
520 const git_merge_file_input
*ours
,
521 const git_merge_file_input
*theirs
,
522 const git_merge_file_options
*opts
);
525 * Merge two files as they exist in the index, using the given common
526 * ancestor as the baseline, producing a `git_merge_file_result` that
527 * reflects the merge result. The `git_merge_file_result` must be freed with
528 * `git_merge_file_result_free`.
530 * @param out The git_merge_file_result to be filled in
531 * @param repo The repository
532 * @param ancestor The index entry for the ancestor file (stage level 1)
533 * @param ours The index entry for our file (stage level 2)
534 * @param theirs The index entry for their file (stage level 3)
535 * @param opts The merge file options or NULL
536 * @return 0 on success or error code
538 GIT_EXTERN(int) git_merge_file_from_index(
539 git_merge_file_result
*out
,
540 git_repository
*repo
,
541 const git_index_entry
*ancestor
,
542 const git_index_entry
*ours
,
543 const git_index_entry
*theirs
,
544 const git_merge_file_options
*opts
);
547 * Frees a `git_merge_file_result`.
549 * @param result The result to free or `NULL`
551 GIT_EXTERN(void) git_merge_file_result_free(git_merge_file_result
*result
);
554 * Merge two trees, producing a `git_index` that reflects the result of
555 * the merge. The index may be written as-is to the working directory
556 * or checked out. If the index is to be converted to a tree, the caller
557 * should resolve any conflicts that arose as part of the merge.
559 * The returned index must be freed explicitly with `git_index_free`.
561 * @param out pointer to store the index result in
562 * @param repo repository that contains the given trees
563 * @param ancestor_tree the common ancestor between the trees (or null if none)
564 * @param our_tree the tree that reflects the destination tree
565 * @param their_tree the tree to merge in to `our_tree`
566 * @param opts the merge tree options (or null for defaults)
567 * @return 0 on success or error code
569 GIT_EXTERN(int) git_merge_trees(
571 git_repository
*repo
,
572 const git_tree
*ancestor_tree
,
573 const git_tree
*our_tree
,
574 const git_tree
*their_tree
,
575 const git_merge_options
*opts
);
578 * Merge two commits, producing a `git_index` that reflects the result of
579 * the merge. The index may be written as-is to the working directory
580 * or checked out. If the index is to be converted to a tree, the caller
581 * should resolve any conflicts that arose as part of the merge.
583 * The returned index must be freed explicitly with `git_index_free`.
585 * @param out pointer to store the index result in
586 * @param repo repository that contains the given trees
587 * @param our_commit the commit that reflects the destination tree
588 * @param their_commit the commit to merge in to `our_commit`
589 * @param opts the merge tree options (or null for defaults)
590 * @return 0 on success or error code
592 GIT_EXTERN(int) git_merge_commits(
594 git_repository
*repo
,
595 const git_commit
*our_commit
,
596 const git_commit
*their_commit
,
597 const git_merge_options
*opts
);
600 * Merges the given commit(s) into HEAD, writing the results into the working
601 * directory. Any changes are staged for commit and any conflicts are written
602 * to the index. Callers should inspect the repository's index after this
603 * completes, resolve any conflicts and prepare a commit.
605 * For compatibility with git, the repository is put into a merging
606 * state. Once the commit is done (or if the uses wishes to abort),
607 * you should clear this state by calling
608 * `git_repository_state_cleanup()`.
610 * @param repo the repository to merge
611 * @param their_heads the heads to merge into
612 * @param their_heads_len the number of heads to merge
613 * @param merge_opts merge options
614 * @param checkout_opts checkout options
615 * @return 0 on success or error code
617 GIT_EXTERN(int) git_merge(
618 git_repository
*repo
,
619 const git_annotated_commit
**their_heads
,
620 size_t their_heads_len
,
621 const git_merge_options
*merge_opts
,
622 const git_checkout_options
*checkout_opts
);