]> git.proxmox.com Git - libgit2.git/blob - tests/diff/externalmodifications.c
df62c33165e94008a919aea324f688ee7873c781
[libgit2.git] / tests / diff / externalmodifications.c
1 #include "clar_libgit2.h"
2 #include "../checkout/checkout_helpers.h"
3
4 #include "index.h"
5 #include "repository.h"
6
7 static git_repository *g_repo;
8
9 void test_diff_externalmodifications__initialize(void)
10 {
11 g_repo = cl_git_sandbox_init("testrepo2");
12 }
13
14 void test_diff_externalmodifications__cleanup(void)
15 {
16 cl_git_sandbox_cleanup();
17 g_repo = NULL;
18 }
19
20 void test_diff_externalmodifications__file_becomes_smaller(void)
21 {
22 git_index *index;
23 git_diff *diff;
24 git_patch* patch;
25 git_str path = GIT_STR_INIT;
26 char big_string[500001];
27
28 cl_git_pass(git_str_joinpath(&path, git_repository_workdir(g_repo), "README"));
29
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);
34
35 /* Get a diff */
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);
40
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");
45
46 /* Attempt to get a patch */
47 cl_git_fail(git_patch_from_diff(&patch, diff, 0));
48
49 git_index_free(index);
50 git_diff_free(diff);
51 git_str_dispose(&path);
52 }
53
54 void test_diff_externalmodifications__file_becomes_empty(void)
55 {
56 git_index *index;
57 git_diff *diff;
58 git_patch* patch;
59 git_str path = GIT_STR_INIT;
60
61 cl_git_pass(git_str_joinpath(&path, git_repository_workdir(g_repo), "README"));
62
63 /* Modify the file */
64 cl_git_mkfile(path.ptr, "hello");
65
66 /* Get a diff */
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);
71
72 /* Empty out the file after we've gotten the diff */
73 cl_git_mkfile(path.ptr, "");
74
75 /* Attempt to get a patch */
76 cl_git_fail(git_patch_from_diff(&patch, diff, 0));
77
78 git_index_free(index);
79 git_diff_free(diff);
80 git_str_dispose(&path);
81 }
82
83 void test_diff_externalmodifications__file_deleted(void)
84 {
85 git_index *index;
86 git_diff *diff;
87 git_patch* patch;
88 git_str path = GIT_STR_INIT;
89
90 cl_git_pass(git_str_joinpath(&path, git_repository_workdir(g_repo), "README"));
91
92 /* Get a diff */
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));
96
97 /* Delete the file */
98 cl_git_rmfile(path.ptr);
99
100 /* Attempt to get a patch */
101 cl_git_fail(git_patch_from_diff(&patch, diff, 0));
102
103 git_index_free(index);
104 git_diff_free(diff);
105 git_str_dispose(&path);
106 }
107
108 void test_diff_externalmodifications__empty_file_becomes_non_empty(void)
109 {
110 git_index *index;
111 git_diff *diff;
112 git_patch* patch;
113 git_str path = GIT_STR_INIT;
114
115 cl_git_pass(git_str_joinpath(&path, git_repository_workdir(g_repo), "README"));
116
117 /* Empty out the file */
118 cl_git_mkfile(path.ptr, "");
119
120 /* Get a diff */
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);
125
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));
129
130 git_index_free(index);
131 git_diff_free(diff);
132 git_str_dispose(&path);
133 }