|
| 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; |
| 60 | + git_buf path = GIT_BUF_INIT; |
| 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++) { |
| 73 | + git_buf_clear(&path); |
| 74 | + cl_git_pass(git_buf_printf(&path, "%s/%s", |
| 75 | + fixture.worktree->path_repository, merge_files[i])); |
| 76 | + cl_assert(git_path_exists(path.ptr)); |
| 77 | + } |
| 78 | + |
| 79 | + git_buf_free(&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); |
| 84 | +} |
| 85 | + |
| 86 | +void test_worktree_merge__merge_conflict(void) |
| 87 | +{ |
| 88 | + git_buf path = GIT_BUF_INIT, buf = GIT_BUF_INIT; |
| 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 | + |
| 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); |
| 117 | + |
| 118 | + git_buf_free(&path); |
| 119 | + git_buf_free(&buf); |
| 120 | +} |
| 121 | + |
0 commit comments