]>
Commit | Line | Data |
---|---|---|
4321595d PS |
1 | #include "clar_libgit2.h" |
2 | ||
3 | #include "worktree_helpers.h" | |
4 | #include "merge/merge_helpers.h" | |
5 | ||
6 | #define COMMON_REPO "testrepo" | |
7 | #define WORKTREE_REPO "testrepo-worktree" | |
8 | ||
9 | #define MASTER_BRANCH "refs/heads/master" | |
10 | #define CONFLICT_BRANCH "refs/heads/merge-conflict" | |
11 | ||
12 | #define CONFLICT_BRANCH_FILE_TXT \ | |
13 | "<<<<<<< HEAD\n" \ | |
14 | "hi\n" \ | |
15 | "bye!\n" \ | |
16 | "=======\n" \ | |
17 | "conflict\n" \ | |
18 | ">>>>>>> merge-conflict\n" \ | |
19 | ||
20 | static worktree_fixture fixture = | |
21 | WORKTREE_FIXTURE_INIT(COMMON_REPO, WORKTREE_REPO); | |
22 | ||
23 | static const char *merge_files[] = { | |
24 | GIT_MERGE_HEAD_FILE, | |
25 | GIT_ORIG_HEAD_FILE, | |
26 | GIT_MERGE_MODE_FILE, | |
27 | GIT_MERGE_MSG_FILE, | |
28 | }; | |
29 | ||
30 | void test_worktree_merge__initialize(void) | |
31 | { | |
32 | setup_fixture_worktree(&fixture); | |
33 | } | |
34 | ||
35 | void test_worktree_merge__cleanup(void) | |
36 | { | |
37 | cleanup_fixture_worktree(&fixture); | |
38 | } | |
39 | ||
40 | void test_worktree_merge__merge_head(void) | |
41 | { | |
42 | git_reference *theirs_ref, *ref; | |
43 | git_annotated_commit *theirs; | |
44 | ||
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)); | |
48 | ||
49 | cl_git_pass(git_reference_lookup(&ref, fixture.worktree, GIT_MERGE_HEAD_FILE)); | |
50 | ||
51 | git_reference_free(ref); | |
52 | git_reference_free(theirs_ref); | |
53 | git_annotated_commit_free(theirs); | |
54 | } | |
55 | ||
56 | void test_worktree_merge__merge_setup(void) | |
57 | { | |
58 | git_reference *ours_ref, *theirs_ref; | |
59 | git_annotated_commit *ours, *theirs; | |
e579e0f7 | 60 | git_str path = GIT_STR_INIT; |
4321595d PS |
61 | unsigned i; |
62 | ||
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)); | |
65 | ||
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)); | |
68 | ||
69 | cl_git_pass(git_merge__setup(fixture.worktree, | |
70 | ours, (const git_annotated_commit **)&theirs, 1)); | |
71 | ||
72 | for (i = 0; i < ARRAY_SIZE(merge_files); i++) { | |
e579e0f7 | 73 | cl_git_pass(git_str_joinpath(&path, |
c25aa7cd PP |
74 | fixture.worktree->gitdir, |
75 | merge_files[i])); | |
e579e0f7 | 76 | cl_assert(git_fs_path_exists(path.ptr)); |
4321595d PS |
77 | } |
78 | ||
e579e0f7 | 79 | git_str_dispose(&path); |
4321595d PS |
80 | git_reference_free(ours_ref); |
81 | git_reference_free(theirs_ref); | |
82 | git_annotated_commit_free(ours); | |
83 | git_annotated_commit_free(theirs); | |
84 | } | |
85 | ||
86 | void test_worktree_merge__merge_conflict(void) | |
87 | { | |
e579e0f7 | 88 | git_str path = GIT_STR_INIT, buf = GIT_STR_INIT; |
4321595d PS |
89 | git_reference *theirs_ref; |
90 | git_annotated_commit *theirs; | |
91 | git_index *index; | |
92 | const git_index_entry *entry; | |
93 | size_t i, conflicts = 0; | |
94 | ||
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)); | |
97 | ||
98 | cl_git_pass(git_merge(fixture.worktree, | |
99 | (const git_annotated_commit **)&theirs, 1, NULL, NULL)); | |
100 | ||
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)); | |
104 | ||
105 | if (git_index_entry_is_conflict(entry)) | |
106 | conflicts++; | |
107 | } | |
108 | cl_assert_equal_sz(conflicts, 3); | |
109 | ||
110 | git_reference_free(theirs_ref); | |
111 | git_annotated_commit_free(theirs); | |
112 | git_index_free(index); | |
113 | ||
e579e0f7 | 114 | cl_git_pass(git_str_joinpath(&path, fixture.worktree->workdir, "branch_file.txt")); |
4321595d PS |
115 | cl_git_pass(git_futils_readbuffer(&buf, path.ptr)); |
116 | cl_assert_equal_s(buf.ptr, CONFLICT_BRANCH_FILE_TXT); | |
117 | ||
e579e0f7 MB |
118 | git_str_dispose(&path); |
119 | git_str_dispose(&buf); | |
4321595d PS |
120 | } |
121 |