]>
git.proxmox.com Git - libgit2.git/blob - tests/worktree/merge.c
1 #include "clar_libgit2.h"
3 #include "worktree_helpers.h"
4 #include "merge/merge_helpers.h"
6 #define COMMON_REPO "testrepo"
7 #define WORKTREE_REPO "testrepo-worktree"
9 #define MASTER_BRANCH "refs/heads/master"
10 #define CONFLICT_BRANCH "refs/heads/merge-conflict"
12 #define CONFLICT_BRANCH_FILE_TXT \
18 ">>>>>>> merge-conflict\n" \
20 static worktree_fixture fixture =
21 WORKTREE_FIXTURE_INIT(COMMON_REPO
, WORKTREE_REPO
);
23 static const char *merge_files
[] = {
30 void test_worktree_merge__initialize(void)
32 setup_fixture_worktree(&fixture
);
35 void test_worktree_merge__cleanup(void)
37 cleanup_fixture_worktree(&fixture
);
40 void test_worktree_merge__merge_head(void)
42 git_reference
*theirs_ref
, *ref
;
43 git_annotated_commit
*theirs
;
45 cl_git_pass(git_reference_lookup(&theirs_ref
, fixture
.worktree
, CONFLICT_BRANCH
));
46 cl_git_pass(git_annotated_commit_from_ref(&theirs
, fixture
.worktree
, theirs_ref
));
47 cl_git_pass(git_merge(fixture
.worktree
, (const git_annotated_commit
**)&theirs
, 1, NULL
, NULL
));
49 cl_git_pass(git_reference_lookup(&ref
, fixture
.worktree
, GIT_MERGE_HEAD_FILE
));
51 git_reference_free(ref
);
52 git_reference_free(theirs_ref
);
53 git_annotated_commit_free(theirs
);
56 void test_worktree_merge__merge_setup(void)
58 git_reference
*ours_ref
, *theirs_ref
;
59 git_annotated_commit
*ours
, *theirs
;
60 git_buf path
= GIT_BUF_INIT
;
63 cl_git_pass(git_reference_lookup(&ours_ref
, fixture
.worktree
, MASTER_BRANCH
));
64 cl_git_pass(git_annotated_commit_from_ref(&ours
, fixture
.worktree
, ours_ref
));
66 cl_git_pass(git_reference_lookup(&theirs_ref
, fixture
.worktree
, CONFLICT_BRANCH
));
67 cl_git_pass(git_annotated_commit_from_ref(&theirs
, fixture
.worktree
, theirs_ref
));
69 cl_git_pass(git_merge__setup(fixture
.worktree
,
70 ours
, (const git_annotated_commit
**)&theirs
, 1));
72 for (i
= 0; i
< ARRAY_SIZE(merge_files
); i
++) {
73 cl_git_pass(git_buf_joinpath(&path
,
74 fixture
.worktree
->gitdir
,
76 cl_assert(git_path_exists(path
.ptr
));
79 git_buf_dispose(&path
);
80 git_reference_free(ours_ref
);
81 git_reference_free(theirs_ref
);
82 git_annotated_commit_free(ours
);
83 git_annotated_commit_free(theirs
);
86 void test_worktree_merge__merge_conflict(void)
88 git_buf path
= GIT_BUF_INIT
, buf
= GIT_BUF_INIT
;
89 git_reference
*theirs_ref
;
90 git_annotated_commit
*theirs
;
92 const git_index_entry
*entry
;
93 size_t i
, conflicts
= 0;
95 cl_git_pass(git_reference_lookup(&theirs_ref
, fixture
.worktree
, CONFLICT_BRANCH
));
96 cl_git_pass(git_annotated_commit_from_ref(&theirs
, fixture
.worktree
, theirs_ref
));
98 cl_git_pass(git_merge(fixture
.worktree
,
99 (const git_annotated_commit
**)&theirs
, 1, NULL
, NULL
));
101 cl_git_pass(git_repository_index(&index
, fixture
.worktree
));
102 for (i
= 0; i
< git_index_entrycount(index
); i
++) {
103 cl_assert(entry
= git_index_get_byindex(index
, i
));
105 if (git_index_entry_is_conflict(entry
))
108 cl_assert_equal_sz(conflicts
, 3);
110 git_reference_free(theirs_ref
);
111 git_annotated_commit_free(theirs
);
112 git_index_free(index
);
114 cl_git_pass(git_buf_joinpath(&path
, fixture
.worktree
->workdir
, "branch_file.txt"));
115 cl_git_pass(git_futils_readbuffer(&buf
, path
.ptr
));
116 cl_assert_equal_s(buf
.ptr
, CONFLICT_BRANCH_FILE_TXT
);
118 git_buf_dispose(&path
);
119 git_buf_dispose(&buf
);