Skip to content

Commit 85a41fc

Browse files
author
Vicent Marti
committed
Merge pull request libgit2#2183 from ethomson/merge_refactor
Refactor the `git_merge` API
2 parents f57cc63 + 58c2b1c commit 85a41fc

File tree

29 files changed

+1092
-999
lines changed

29 files changed

+1092
-999
lines changed

include/git2/merge.h

Lines changed: 221 additions & 108 deletions
Large diffs are not rendered by default.

include/git2/revert.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ typedef struct {
2626
/** For merge commits, the "mainline" is treated as the parent. */
2727
unsigned int mainline;
2828

29-
git_merge_tree_opts merge_tree_opts;
29+
git_merge_options merge_opts;
3030
git_checkout_options checkout_opts;
3131
} git_revert_options;
3232

3333
#define GIT_REVERT_OPTIONS_VERSION 1
34-
#define GIT_REVERT_OPTIONS_INIT {GIT_REVERT_OPTIONS_VERSION, 0, GIT_MERGE_TREE_OPTS_INIT, GIT_CHECKOUT_OPTIONS_INIT}
34+
#define GIT_REVERT_OPTIONS_INIT {GIT_REVERT_OPTIONS_VERSION, 0, GIT_MERGE_OPTIONS_INIT, GIT_CHECKOUT_OPTIONS_INIT}
3535

3636
/**
3737
* Initializes a `git_revert_options` with default values. Equivalent to
@@ -66,7 +66,7 @@ int git_revert_commit(
6666
git_commit *revert_commit,
6767
git_commit *our_commit,
6868
unsigned int mainline,
69-
const git_merge_tree_opts *merge_tree_opts);
69+
const git_merge_options *merge_options);
7070

7171
/**
7272
* Reverts the given commit, producing changes in the working directory.

src/checkout.c

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1681,29 +1681,20 @@ static int checkout_write_merge(
16811681
{
16821682
git_buf our_label = GIT_BUF_INIT, their_label = GIT_BUF_INIT,
16831683
path_suffixed = GIT_BUF_INIT, path_workdir = GIT_BUF_INIT;
1684-
git_merge_file_options merge_file_opts = GIT_MERGE_FILE_OPTIONS_INIT;
1685-
git_merge_file_input ancestor = GIT_MERGE_FILE_INPUT_INIT,
1686-
ours = GIT_MERGE_FILE_INPUT_INIT,
1687-
theirs = GIT_MERGE_FILE_INPUT_INIT;
1688-
git_merge_file_result result = GIT_MERGE_FILE_RESULT_INIT;
1684+
git_merge_file_options opts = GIT_MERGE_FILE_OPTIONS_INIT;
1685+
git_merge_file_result result = {0};
16891686
git_filebuf output = GIT_FILEBUF_INIT;
16901687
int error = 0;
16911688

16921689
if (data->opts.checkout_strategy & GIT_CHECKOUT_CONFLICT_STYLE_DIFF3)
1693-
merge_file_opts.style = GIT_MERGE_FILE_STYLE_DIFF3;
1694-
1695-
if ((conflict->ancestor &&
1696-
(error = git_merge_file_input_from_index_entry(
1697-
&ancestor, data->repo, conflict->ancestor)) < 0) ||
1698-
(error = git_merge_file_input_from_index_entry(
1699-
&ours, data->repo, conflict->ours)) < 0 ||
1700-
(error = git_merge_file_input_from_index_entry(
1701-
&theirs, data->repo, conflict->theirs)) < 0)
1702-
goto done;
1690+
opts.flags |= GIT_MERGE_FILE_STYLE_DIFF3;
17031691

1704-
ancestor.label = data->opts.ancestor_label ? data->opts.ancestor_label : "ancestor";
1705-
ours.label = data->opts.our_label ? data->opts.our_label : "ours";
1706-
theirs.label = data->opts.their_label ? data->opts.their_label : "theirs";
1692+
opts.ancestor_label = data->opts.ancestor_label ?
1693+
data->opts.ancestor_label : "ancestor";
1694+
opts.our_label = data->opts.our_label ?
1695+
data->opts.our_label : "ours";
1696+
opts.their_label = data->opts.their_label ?
1697+
data->opts.their_label : "theirs";
17071698

17081699
/* If all the paths are identical, decorate the diff3 file with the branch
17091700
* names. Otherwise, append branch_name:path.
@@ -1712,16 +1703,17 @@ static int checkout_write_merge(
17121703
strcmp(conflict->ours->path, conflict->theirs->path) != 0) {
17131704

17141705
if ((error = conflict_entry_name(
1715-
&our_label, ours.label, conflict->ours->path)) < 0 ||
1706+
&our_label, opts.our_label, conflict->ours->path)) < 0 ||
17161707
(error = conflict_entry_name(
1717-
&their_label, theirs.label, conflict->theirs->path)) < 0)
1708+
&their_label, opts.their_label, conflict->theirs->path)) < 0)
17181709
goto done;
17191710

1720-
ours.label = git_buf_cstr(&our_label);
1721-
theirs.label = git_buf_cstr(&their_label);
1711+
opts.our_label = git_buf_cstr(&our_label);
1712+
opts.their_label = git_buf_cstr(&their_label);
17221713
}
17231714

1724-
if ((error = git_merge_files(&result, &ancestor, &ours, &theirs, &merge_file_opts)) < 0)
1715+
if ((error = git_merge_file_from_index(&result, data->repo,
1716+
conflict->ancestor, conflict->ours, conflict->theirs, &opts)) < 0)
17251717
goto done;
17261718

17271719
if (result.path == NULL || result.mode == 0) {
@@ -1739,17 +1731,14 @@ static int checkout_write_merge(
17391731

17401732
if ((error = git_futils_mkpath2file(path_workdir.ptr, 0755)) < 0 ||
17411733
(error = git_filebuf_open(&output, path_workdir.ptr, GIT_FILEBUF_DO_NOT_BUFFER, result.mode)) < 0 ||
1742-
(error = git_filebuf_write(&output, result.data, result.len)) < 0 ||
1734+
(error = git_filebuf_write(&output, result.ptr, result.len)) < 0 ||
17431735
(error = git_filebuf_commit(&output)) < 0)
17441736
goto done;
17451737

17461738
done:
17471739
git_buf_free(&our_label);
17481740
git_buf_free(&their_label);
17491741

1750-
git_merge_file_input_free(&ancestor);
1751-
git_merge_file_input_free(&ours);
1752-
git_merge_file_input_free(&theirs);
17531742
git_merge_file_result_free(&result);
17541743
git_buf_free(&path_workdir);
17551744
git_buf_free(&path_suffixed);

src/index.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ static void index_entry_free(git_index_entry *entry)
300300
git__free(entry);
301301
}
302302

303-
static unsigned int index_create_mode(unsigned int mode)
303+
unsigned int git_index__create_mode(unsigned int mode)
304304
{
305305
if (S_ISLNK(mode))
306306
return S_IFLNK;
@@ -320,9 +320,9 @@ static unsigned int index_merge_mode(
320320

321321
if (index->distrust_filemode && S_ISREG(mode))
322322
return (existing && S_ISREG(existing->mode)) ?
323-
existing->mode : index_create_mode(0666);
323+
existing->mode : git_index__create_mode(0666);
324324

325-
return index_create_mode(mode);
325+
return git_index__create_mode(mode);
326326
}
327327

328328
void git_index__set_ignore_case(git_index *index, bool ignore_case)
@@ -619,7 +619,7 @@ void git_index_entry__init_from_stat(
619619
entry->dev = st->st_rdev;
620620
entry->ino = st->st_ino;
621621
entry->mode = (!trust_mode && S_ISREG(st->st_mode)) ?
622-
index_create_mode(0666) : index_create_mode(st->st_mode);
622+
git_index__create_mode(0666) : git_index__create_mode(st->st_mode);
623623
entry->uid = st->st_uid;
624624
entry->gid = st->st_gid;
625625
entry->file_size = st->st_size;

src/index.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,6 @@ extern int git_index__find(
6060

6161
extern void git_index__set_ignore_case(git_index *index, bool ignore_case);
6262

63+
extern unsigned int git_index__create_mode(unsigned int mode);
64+
6365
#endif

0 commit comments

Comments
 (0)