blob: e4a20e81a3aea90b64e65b1f33a38efa595d9a2a [file] [log] [blame]
Junio C Hamano525ab632007-12-24 00:36:00 -08001/*
2 * Low level 3-way in-core file merge.
3 */
4
5#ifndef LL_MERGE_H
6#define LL_MERGE_H
7
Elijah Newrenef3ca952018-08-15 10:54:05 -07008#include "xdiff/xdiff.h"
9
Heba Walyd3d71722019-11-17 21:04:43 +000010/**
11 *
12 * Calling sequence:
13 * ----------------
14 *
15 * - Prepare a `struct ll_merge_options` to record options.
16 * If you have no special requests, skip this and pass `NULL`
17 * as the `opts` parameter to use the default options.
18 *
19 * - Allocate an mmbuffer_t variable for the result.
20 *
21 * - Allocate and fill variables with the file's original content
22 * and two modified versions (using `read_mmfile`, for example).
23 *
24 * - Call `ll_merge()`.
25 *
26 * - Read the merged content from `result_buf.ptr` and `result_buf.size`.
27 *
28 * - Release buffers when finished. A simple
29 * `free(ancestor.ptr); free(ours.ptr); free(theirs.ptr);
30 * free(result_buf.ptr);` will do.
31 *
32 * If the modifications do not merge cleanly, `ll_merge` will return a
33 * nonzero value and `result_buf` will generally include a description of
34 * the conflict bracketed by markers such as the traditional `<<<<<<<`
35 * and `>>>>>>>`.
36 *
37 * The `ancestor_label`, `our_label`, and `their_label` parameters are
38 * used to label the different sides of a conflict if the merge driver
39 * supports this.
40 */
41
42
Nguyễn Thái Ngọc Duy32eaa462018-09-21 17:57:27 +020043struct index_state;
44
Heba Walyd3d71722019-11-17 21:04:43 +000045/**
46 * This describes the set of options the calling program wants to affect
47 * the operation of a low-level (single file) merge.
48 */
Jonathan Nieder712516b2010-08-26 00:49:53 -050049struct ll_merge_options {
Heba Walyd3d71722019-11-17 21:04:43 +000050
51 /**
52 * Behave as though this were part of a merge between common ancestors in
53 * a recursive merge (merges of binary files may need to be handled
54 * differently in such cases, for example). If a helper program is
55 * specified by the `[merge "<driver>"] recursive` configuration, it will
56 * be used.
57 */
Jonathan Nieder712516b2010-08-26 00:49:53 -050058 unsigned virtual_ancestor : 1;
Heba Walyd3d71722019-11-17 21:04:43 +000059
60 /**
61 * Resolve local conflicts automatically in favor of one side or the other
62 * (as in 'git merge-file' `--ours`/`--theirs`/`--union`). Can be `0`,
63 * `XDL_MERGE_FAVOR_OURS`, `XDL_MERGE_FAVOR_THEIRS`,
64 * or `XDL_MERGE_FAVOR_UNION`.
65 */
66 unsigned variant : 2;
67
68 /**
69 * Resmudge and clean the "base", "theirs" and "ours" files before merging.
70 * Use this when the merge is likely to have overlapped with a change in
71 * smudge/clean or end-of-line normalization rules.
72 */
Jonathan Nieder712516b2010-08-26 00:49:53 -050073 unsigned renormalize : 1;
Heba Walyd3d71722019-11-17 21:04:43 +000074
75 /**
76 * Increase the length of conflict markers so that nested conflicts
77  * can be differentiated.
78 */
Elijah Newrenb2a79422018-11-07 20:40:24 -080079 unsigned extra_marker_size;
Heba Walyd3d71722019-11-17 21:04:43 +000080
81 /* Extra xpparam_t flags as defined in xdiff/xdiff.h. */
Justin Frankel58a1ece2010-08-26 00:50:45 -050082 long xdl_opts;
Jonathan Nieder712516b2010-08-26 00:49:53 -050083};
Jonathan Nieder73cf7f72010-08-05 06:17:38 -050084
Elijah Newren35f69672022-02-02 02:37:30 +000085enum ll_merge_result {
86 LL_MERGE_ERROR = -1,
87 LL_MERGE_OK = 0,
88 LL_MERGE_CONFLICT,
89 LL_MERGE_BINARY_CONFLICT,
90};
91
Heba Walyd3d71722019-11-17 21:04:43 +000092/**
93 * Perform a three-way single-file merge in core. This is a thin wrapper
94 * around `xdl_merge` that takes the path and any merge backend specified in
95 * `.gitattributes` or `.git/info/attributes` into account.
96 * Returns 0 for a clean merge.
97 */
Elijah Newren35f69672022-02-02 02:37:30 +000098enum ll_merge_result ll_merge(mmbuffer_t *result_buf,
Junio C Hamano525ab632007-12-24 00:36:00 -080099 const char *path,
Jonathan Niederf01de622010-03-20 19:38:58 -0500100 mmfile_t *ancestor, const char *ancestor_label,
Junio C Hamano525ab632007-12-24 00:36:00 -0800101 mmfile_t *ours, const char *our_label,
102 mmfile_t *theirs, const char *their_label,
Nguyễn Thái Ngọc Duy32eaa462018-09-21 17:57:27 +0200103 struct index_state *istate,
Jonathan Nieder712516b2010-08-26 00:49:53 -0500104 const struct ll_merge_options *opts);
Junio C Hamano525ab632007-12-24 00:36:00 -0800105
Nguyễn Thái Ngọc Duy32eaa462018-09-21 17:57:27 +0200106int ll_merge_marker_size(struct index_state *istate, const char *path);
brian m. carlson2c65d902019-09-02 22:39:44 +0000107void reset_merge_attributes(void);
Junio C Hamano85885672010-01-16 23:28:46 -0800108
Junio C Hamano525ab632007-12-24 00:36:00 -0800109#endif