]> git.proxmox.com Git - libgit2.git/blame - tests/libgit2/stress/diff.c
Merge https://salsa.debian.org/debian/libgit2 into proxmox/bullseye
[libgit2.git] / tests / libgit2 / stress / diff.c
CommitLineData
e8242022
RB
1#include "clar_libgit2.h"
2#include "../diff/diff_helpers.h"
3
4static git_repository *g_repo = NULL;
5
6void test_stress_diff__initialize(void)
7{
8}
9
10void test_stress_diff__cleanup(void)
11{
12 cl_git_sandbox_cleanup();
13}
14
15#define ANOTHER_POEM \
16"OH, glorious are the guarded heights\nWhere guardian souls abide—\nSelf-exiled from our gross delights—\nAbove, beyond, outside:\nAn ampler arc their spirit swings—\nCommands a juster view—\nWe have their word for all these things,\nNo doubt their words are true.\n\nYet we, the bond slaves of our day,\nWhom dirt and danger press—\nCo-heirs of insolence, delay,\nAnd leagued unfaithfulness—\nSuch is our need must seek indeed\nAnd, having found, engage\nThe men who merely do the work\nFor which they draw the wage.\n\nFrom forge and farm and mine and bench,\nDeck, altar, outpost lone—\nMill, school, battalion, counter, trench,\nRail, senate, sheepfold, throne—\nCreation's cry goes up on high\nFrom age to cheated age:\n\"Send us the men who do the work\n\"For which they draw the wage!\"\n"
17
b6ac07b5 18static void test_with_many(int expected_new)
e8242022
RB
19{
20 git_index *index;
21 git_tree *tree, *new_tree;
3ff1d123 22 git_diff *diff = NULL;
e8242022
RB
23 diff_expects exp;
24 git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT;
25 git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT;
26
27 cl_git_pass(git_repository_index(&index, g_repo));
28 cl_git_pass(
29 git_revparse_single((git_object **)&tree, g_repo, "HEAD^{tree}"));
30
31 cl_git_pass(p_rename("renames/ikeepsix.txt", "renames/ikeepsix2.txt"));
32 cl_git_pass(git_index_remove_bypath(index, "ikeepsix.txt"));
33 cl_git_pass(git_index_add_bypath(index, "ikeepsix2.txt"));
34 cl_git_pass(git_index_write(index));
35
36 cl_git_pass(git_diff_tree_to_index(&diff, g_repo, tree, index, &diffopts));
37
38 memset(&exp, 0, sizeof(exp));
39 cl_git_pass(git_diff_foreach(
8147b1af 40 diff, diff_file_cb, NULL, NULL, NULL, &exp));
e8242022
RB
41 cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]);
42 cl_assert_equal_i(expected_new + 1, exp.file_status[GIT_DELTA_ADDED]);
43 cl_assert_equal_i(expected_new + 2, exp.files);
44
45 opts.flags = GIT_DIFF_FIND_ALL;
46 cl_git_pass(git_diff_find_similar(diff, &opts));
47
48 memset(&exp, 0, sizeof(exp));
49 cl_git_pass(git_diff_foreach(
8147b1af 50 diff, diff_file_cb, NULL, NULL, NULL, &exp));
e8242022
RB
51 cl_assert_equal_i(1, exp.file_status[GIT_DELTA_RENAMED]);
52 cl_assert_equal_i(expected_new, exp.file_status[GIT_DELTA_ADDED]);
53 cl_assert_equal_i(expected_new + 1, exp.files);
54
3ff1d123 55 git_diff_free(diff);
e8242022 56
155fa234
RB
57 cl_repo_commit_from_index(NULL, g_repo, NULL, 1372350000, "yoyoyo");
58 cl_git_pass(git_revparse_single(
59 (git_object **)&new_tree, g_repo, "HEAD^{tree}"));
e8242022
RB
60
61 cl_git_pass(git_diff_tree_to_tree(
62 &diff, g_repo, tree, new_tree, &diffopts));
63
64 memset(&exp, 0, sizeof(exp));
65 cl_git_pass(git_diff_foreach(
8147b1af 66 diff, diff_file_cb, NULL, NULL, NULL, &exp));
e8242022
RB
67 cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]);
68 cl_assert_equal_i(expected_new + 1, exp.file_status[GIT_DELTA_ADDED]);
69 cl_assert_equal_i(expected_new + 2, exp.files);
70
71 opts.flags = GIT_DIFF_FIND_ALL;
72 cl_git_pass(git_diff_find_similar(diff, &opts));
73
74 memset(&exp, 0, sizeof(exp));
75 cl_git_pass(git_diff_foreach(
8147b1af 76 diff, diff_file_cb, NULL, NULL, NULL, &exp));
e8242022
RB
77 cl_assert_equal_i(1, exp.file_status[GIT_DELTA_RENAMED]);
78 cl_assert_equal_i(expected_new, exp.file_status[GIT_DELTA_ADDED]);
79 cl_assert_equal_i(expected_new + 1, exp.files);
80
3ff1d123 81 git_diff_free(diff);
e8242022
RB
82
83 git_tree_free(new_tree);
84 git_tree_free(tree);
85 git_index_free(index);
86}
87
88void test_stress_diff__rename_big_files(void)
89{
90 git_index *index;
91 char tmp[64];
92 int i, j;
e579e0f7 93 git_str b = GIT_STR_INIT;
e8242022
RB
94
95 g_repo = cl_git_sandbox_init("renames");
96
97 cl_git_pass(git_repository_index(&index, g_repo));
98
99 for (i = 0; i < 100; i += 1) {
c7dd0a56 100 p_snprintf(tmp, sizeof(tmp), "renames/newfile%03d", i);
e8242022 101 for (j = i * 256; j > 0; --j)
e579e0f7 102 git_str_printf(&b, "more content %d\n", i);
e8242022
RB
103 cl_git_mkfile(tmp, b.ptr);
104 }
105
106 for (i = 0; i < 100; i += 1) {
c7dd0a56 107 p_snprintf(tmp, sizeof(tmp), "renames/newfile%03d", i);
e8242022
RB
108 cl_git_pass(git_index_add_bypath(index, tmp + strlen("renames/")));
109 }
110
e579e0f7 111 git_str_dispose(&b);
e8242022
RB
112 git_index_free(index);
113
114 test_with_many(100);
115}
116
117void test_stress_diff__rename_many_files(void)
118{
119 git_index *index;
120 char tmp[64];
121 int i;
e579e0f7 122 git_str b = GIT_STR_INIT;
e8242022
RB
123
124 g_repo = cl_git_sandbox_init("renames");
125
126 cl_git_pass(git_repository_index(&index, g_repo));
127
e579e0f7 128 git_str_printf(&b, "%08d\n" ANOTHER_POEM "%08d\n" ANOTHER_POEM ANOTHER_POEM, 0, 0);
e8242022
RB
129
130 for (i = 0; i < 2500; i += 1) {
c7dd0a56
JG
131 p_snprintf(tmp, sizeof(tmp), "renames/newfile%03d", i);
132 p_snprintf(b.ptr, 9, "%08d", i);
e8242022
RB
133 b.ptr[8] = '\n';
134 cl_git_mkfile(tmp, b.ptr);
135 }
e579e0f7 136 git_str_dispose(&b);
e8242022
RB
137
138 for (i = 0; i < 2500; i += 1) {
c7dd0a56 139 p_snprintf(tmp, sizeof(tmp), "renames/newfile%03d", i);
e8242022
RB
140 cl_git_pass(git_index_add_bypath(index, tmp + strlen("renames/")));
141 }
142
143 git_index_free(index);
144
145 test_with_many(2500);
146}