|
| 1 | +#include "clar_libgit2.h" |
| 2 | +#include "apply_helpers.h" |
| 3 | + |
| 4 | +static git_repository *repo; |
| 5 | + |
| 6 | +#define TEST_REPO_PATH "merge-recursive" |
| 7 | + |
| 8 | +void test_apply_check__initialize(void) |
| 9 | +{ |
| 10 | + git_oid oid; |
| 11 | + git_commit *commit; |
| 12 | + |
| 13 | + repo = cl_git_sandbox_init(TEST_REPO_PATH); |
| 14 | + |
| 15 | + git_oid_fromstr(&oid, "539bd011c4822c560c1d17cab095006b7a10f707"); |
| 16 | + cl_git_pass(git_commit_lookup(&commit, repo, &oid)); |
| 17 | + cl_git_pass(git_reset(repo, (git_object *)commit, GIT_RESET_HARD, NULL)); |
| 18 | + git_commit_free(commit); |
| 19 | +} |
| 20 | + |
| 21 | +void test_apply_check__cleanup(void) |
| 22 | +{ |
| 23 | + cl_git_sandbox_cleanup(); |
| 24 | +} |
| 25 | + |
| 26 | +void test_apply_check__generate_diff(void) |
| 27 | +{ |
| 28 | + git_oid a_oid, b_oid; |
| 29 | + git_commit *a_commit, *b_commit; |
| 30 | + git_tree *a_tree, *b_tree; |
| 31 | + git_diff *diff; |
| 32 | + git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT; |
| 33 | + git_apply_options opts = GIT_APPLY_OPTIONS_INIT; |
| 34 | + |
| 35 | + cl_git_pass(git_oid_fromstr(&a_oid, "539bd011c4822c560c1d17cab095006b7a10f707")); |
| 36 | + cl_git_pass(git_oid_fromstr(&b_oid, "7c7bf85e978f1d18c0566f702d2cb7766b9c8d4f")); |
| 37 | + cl_git_pass(git_commit_lookup(&a_commit, repo, &a_oid)); |
| 38 | + cl_git_pass(git_commit_lookup(&b_commit, repo, &b_oid)); |
| 39 | + |
| 40 | + cl_git_pass(git_commit_tree(&a_tree, a_commit)); |
| 41 | + cl_git_pass(git_commit_tree(&b_tree, b_commit)); |
| 42 | + |
| 43 | + opts.flags |= GIT_APPLY_CHECK; |
| 44 | + cl_git_pass(git_diff_tree_to_tree(&diff, repo, a_tree, b_tree, &diff_opts)); |
| 45 | + cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_BOTH, &opts)); |
| 46 | + |
| 47 | + validate_index_unchanged(repo); |
| 48 | + validate_workdir_unchanged(repo); |
| 49 | + |
| 50 | + git_diff_free(diff); |
| 51 | + git_tree_free(a_tree); |
| 52 | + git_tree_free(b_tree); |
| 53 | + git_commit_free(a_commit); |
| 54 | + git_commit_free(b_commit); |
| 55 | +} |
| 56 | + |
| 57 | +void test_apply_check__parsed_diff(void) |
| 58 | +{ |
| 59 | + git_diff *diff; |
| 60 | + git_apply_options opts = GIT_APPLY_OPTIONS_INIT; |
| 61 | + |
| 62 | + opts.flags |= GIT_APPLY_CHECK; |
| 63 | + cl_git_pass(git_diff_from_buffer(&diff, |
| 64 | + DIFF_MODIFY_TWO_FILES, strlen(DIFF_MODIFY_TWO_FILES))); |
| 65 | + cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_INDEX, &opts)); |
| 66 | + |
| 67 | + validate_index_unchanged(repo); |
| 68 | + validate_workdir_unchanged(repo); |
| 69 | + |
| 70 | + git_diff_free(diff); |
| 71 | +} |
| 72 | + |
| 73 | +void test_apply_check__binary(void) |
| 74 | +{ |
| 75 | + git_diff *diff; |
| 76 | + git_apply_options opts = GIT_APPLY_OPTIONS_INIT; |
| 77 | + |
| 78 | + opts.flags |= GIT_APPLY_CHECK; |
| 79 | + cl_git_pass(git_diff_from_buffer(&diff, |
| 80 | + DIFF_MODIFY_TWO_FILES_BINARY, |
| 81 | + strlen(DIFF_MODIFY_TWO_FILES_BINARY))); |
| 82 | + cl_git_pass(git_apply(repo, diff, GIT_APPLY_LOCATION_INDEX, &opts)); |
| 83 | + |
| 84 | + validate_index_unchanged(repo); |
| 85 | + validate_workdir_unchanged(repo); |
| 86 | + |
| 87 | + git_diff_free(diff); |
| 88 | +} |
| 89 | + |
| 90 | +void test_apply_check__does_not_apply(void) |
| 91 | +{ |
| 92 | + git_diff *diff; |
| 93 | + git_index *index; |
| 94 | + git_apply_options opts = GIT_APPLY_OPTIONS_INIT; |
| 95 | + |
| 96 | + const char *diff_file = DIFF_MODIFY_TWO_FILES; |
| 97 | + |
| 98 | + struct merge_index_entry index_expected[] = { |
| 99 | + { 0100644, "f51658077d85f2264fa179b4d0848268cb3475c3", 0, "asparagus.txt" }, |
| 100 | + { 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" }, |
| 101 | + { 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" }, |
| 102 | + { 0100644, "c4e6cca3ec6ae0148ed231f97257df8c311e015f", 0, "gravy.txt" }, |
| 103 | + { 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" }, |
| 104 | + }; |
| 105 | + size_t index_expected_cnt = sizeof(index_expected) / |
| 106 | + sizeof(struct merge_index_entry); |
| 107 | + |
| 108 | + /* mutate the index */ |
| 109 | + cl_git_pass(git_repository_index(&index, repo)); |
| 110 | + cl_git_pass(git_index_remove(index, "veal.txt", 0)); |
| 111 | + cl_git_pass(git_index_write(index)); |
| 112 | + git_index_free(index); |
| 113 | + |
| 114 | + opts.flags |= GIT_APPLY_CHECK; |
| 115 | + cl_git_pass(git_diff_from_buffer(&diff, diff_file, strlen(diff_file))); |
| 116 | + cl_git_fail_with(GIT_EAPPLYFAIL, git_apply(repo, diff, GIT_APPLY_LOCATION_INDEX, &opts)); |
| 117 | + |
| 118 | + validate_apply_index(repo, index_expected, index_expected_cnt); |
| 119 | + |
| 120 | + git_diff_free(diff); |
| 121 | +} |
0 commit comments