]>
git.proxmox.com Git - libgit2.git/blob - tests/diff/externalmodifications.c
df62c33165e94008a919aea324f688ee7873c781
1 #include "clar_libgit2.h"
2 #include "../checkout/checkout_helpers.h"
5 #include "repository.h"
7 static git_repository
*g_repo
;
9 void test_diff_externalmodifications__initialize(void)
11 g_repo
= cl_git_sandbox_init("testrepo2");
14 void test_diff_externalmodifications__cleanup(void)
16 cl_git_sandbox_cleanup();
20 void test_diff_externalmodifications__file_becomes_smaller(void)
25 git_str path
= GIT_STR_INIT
;
26 char big_string
[500001];
28 cl_git_pass(git_str_joinpath(&path
, git_repository_workdir(g_repo
), "README"));
30 /* Modify the file with a large string */
31 memset(big_string
, '\n', sizeof(big_string
) - 1);
32 big_string
[sizeof(big_string
) - 1] = '\0';
33 cl_git_mkfile(path
.ptr
, big_string
);
36 cl_git_pass(git_repository_index(&index
, g_repo
));
37 cl_git_pass(git_diff_index_to_workdir(&diff
, g_repo
, index
, NULL
));
38 cl_assert_equal_i(1, git_diff_num_deltas(diff
));
39 cl_assert_equal_i(500000, git_diff_get_delta(diff
, 0)->new_file
.size
);
41 /* Simulate file modification after we've gotten the diff.
42 * Write a shorter string to ensure that we don't mmap 500KB from
43 * the previous revision, which would most likely crash. */
44 cl_git_mkfile(path
.ptr
, "hello");
46 /* Attempt to get a patch */
47 cl_git_fail(git_patch_from_diff(&patch
, diff
, 0));
49 git_index_free(index
);
51 git_str_dispose(&path
);
54 void test_diff_externalmodifications__file_becomes_empty(void)
59 git_str path
= GIT_STR_INIT
;
61 cl_git_pass(git_str_joinpath(&path
, git_repository_workdir(g_repo
), "README"));
64 cl_git_mkfile(path
.ptr
, "hello");
67 cl_git_pass(git_repository_index(&index
, g_repo
));
68 cl_git_pass(git_diff_index_to_workdir(&diff
, g_repo
, index
, NULL
));
69 cl_assert_equal_i(1, git_diff_num_deltas(diff
));
70 cl_assert_equal_i(5, git_diff_get_delta(diff
, 0)->new_file
.size
);
72 /* Empty out the file after we've gotten the diff */
73 cl_git_mkfile(path
.ptr
, "");
75 /* Attempt to get a patch */
76 cl_git_fail(git_patch_from_diff(&patch
, diff
, 0));
78 git_index_free(index
);
80 git_str_dispose(&path
);
83 void test_diff_externalmodifications__file_deleted(void)
88 git_str path
= GIT_STR_INIT
;
90 cl_git_pass(git_str_joinpath(&path
, git_repository_workdir(g_repo
), "README"));
93 cl_git_pass(git_repository_index(&index
, g_repo
));
94 cl_git_pass(git_diff_index_to_workdir(&diff
, g_repo
, index
, NULL
));
95 cl_assert_equal_i(0, git_diff_num_deltas(diff
));
98 cl_git_rmfile(path
.ptr
);
100 /* Attempt to get a patch */
101 cl_git_fail(git_patch_from_diff(&patch
, diff
, 0));
103 git_index_free(index
);
105 git_str_dispose(&path
);
108 void test_diff_externalmodifications__empty_file_becomes_non_empty(void)
113 git_str path
= GIT_STR_INIT
;
115 cl_git_pass(git_str_joinpath(&path
, git_repository_workdir(g_repo
), "README"));
117 /* Empty out the file */
118 cl_git_mkfile(path
.ptr
, "");
121 cl_git_pass(git_repository_index(&index
, g_repo
));
122 cl_git_pass(git_diff_index_to_workdir(&diff
, g_repo
, index
, NULL
));
123 cl_assert_equal_i(1, git_diff_num_deltas(diff
));
124 cl_assert_equal_i(0, git_diff_get_delta(diff
, 0)->new_file
.size
);
126 /* Simulate file modification after we've gotten the diff */
127 cl_git_mkfile(path
.ptr
, "hello");
128 cl_git_fail(git_patch_from_diff(&patch
, diff
, 0));
130 git_index_free(index
);
132 git_str_dispose(&path
);