|
14 | 14 | static git_repository *g_repo; |
15 | 15 | static git_index *g_index; |
16 | 16 |
|
| 17 | +static git_buf expected_fixture = GIT_BUF_INIT; |
| 18 | + |
17 | 19 | void test_index_crlf__initialize(void) |
18 | 20 | { |
19 | | - g_repo = cl_git_sandbox_init("crlf"); |
| 21 | + g_repo = cl_git_sandbox_init_new("crlf"); |
20 | 22 | cl_git_pass(git_repository_index(&g_index, g_repo)); |
21 | 23 | } |
22 | 24 |
|
23 | 25 | void test_index_crlf__cleanup(void) |
24 | 26 | { |
25 | 27 | git_index_free(g_index); |
26 | 28 | cl_git_sandbox_cleanup(); |
| 29 | + |
| 30 | + if (expected_fixture.size) { |
| 31 | + cl_fixture_cleanup(expected_fixture.ptr); |
| 32 | + git_buf_free(&expected_fixture); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +struct compare_data |
| 37 | +{ |
| 38 | + const char *dirname; |
| 39 | + const char *safecrlf; |
| 40 | + const char *autocrlf; |
| 41 | + const char *attrs; |
| 42 | +}; |
| 43 | + |
| 44 | +static int add_and_check_file(void *payload, git_buf *actual_path) |
| 45 | +{ |
| 46 | + git_buf expected_path_oid = GIT_BUF_INIT; |
| 47 | + git_buf expected_path_fail = GIT_BUF_INIT; |
| 48 | + git_buf expected_contents = GIT_BUF_INIT; |
| 49 | + struct compare_data *cd = payload; |
| 50 | + bool failed = true; |
| 51 | + int cmp_git, cmp_gitattributes; |
| 52 | + char *basename; |
| 53 | + int add_bypath_ret; |
| 54 | + const git_index_entry *entry; |
| 55 | + git_oid oid; |
| 56 | + git_error_state error = { 0 }; |
| 57 | + |
| 58 | + basename = git_path_basename(actual_path->ptr); |
| 59 | + cmp_git = strcmp(basename, ".git"); |
| 60 | + cmp_gitattributes = strcmp(basename, ".gitattributes"); |
| 61 | + |
| 62 | + if (cmp_git == 0 || cmp_gitattributes == 0) { |
| 63 | + failed = false; |
| 64 | + goto done; |
| 65 | + } |
| 66 | + |
| 67 | + giterr_clear(); |
| 68 | + add_bypath_ret = git_index_add_bypath(g_index, basename); |
| 69 | + giterr_state_capture(&error, add_bypath_ret); |
| 70 | + |
| 71 | + entry = git_index_get_bypath(g_index, basename, 0); |
| 72 | + if (!add_bypath_ret && !entry) |
| 73 | + goto done; |
| 74 | + |
| 75 | + cl_git_pass(git_buf_joinpath(&expected_path_oid, cd->dirname, basename)); |
| 76 | + cl_git_pass(git_buf_joinpath(&expected_path_fail, cd->dirname, basename)); |
| 77 | + git_buf_puts(&expected_path_oid, ".obj"); |
| 78 | + git_buf_puts(&expected_path_fail, ".fail"); |
| 79 | + |
| 80 | + if (git_path_isfile(expected_path_oid.ptr)) { |
| 81 | + if (add_bypath_ret) |
| 82 | + goto done; |
| 83 | + |
| 84 | + if (git_futils_readbuffer(&expected_contents, expected_path_oid.ptr) < 0) |
| 85 | + goto done; |
| 86 | + |
| 87 | + if (git_oid_fromstr(&oid, expected_contents.ptr)) |
| 88 | + goto done; |
| 89 | + if (!git_oid_equal(&oid, &entry->id)) |
| 90 | + goto done; |
| 91 | + } |
| 92 | + |
| 93 | + if (git_path_isfile(expected_path_fail.ptr)) { |
| 94 | + if (!add_bypath_ret) |
| 95 | + goto done; |
| 96 | + |
| 97 | + if (git_futils_readbuffer(&expected_contents, expected_path_fail.ptr) < 0) |
| 98 | + goto done; |
| 99 | + |
| 100 | + git_buf_rtrim(&expected_contents); |
| 101 | + |
| 102 | + if (error.error_msg.klass != GITERR_FILTER || strstr(error.error_msg.message, expected_contents.ptr) == NULL) |
| 103 | + goto done; |
| 104 | + } |
| 105 | + |
| 106 | + failed = false; |
| 107 | + |
| 108 | +done: |
| 109 | + if (failed) { |
| 110 | + git_buf details = GIT_BUF_INIT; |
| 111 | + git_buf_printf(&details, "filename=%s, safecrlf=%s, autocrlf=%s, attrs={%s}", |
| 112 | + git_path_basename(actual_path->ptr), cd->safecrlf, cd->autocrlf, cd->attrs); |
| 113 | + clar__fail(__FILE__, __LINE__, |
| 114 | + "adding file did not work as expected", details.ptr, 0); |
| 115 | + git_buf_free(&details); |
| 116 | + } |
| 117 | + |
| 118 | + git__free(basename); |
| 119 | + git_buf_free(&expected_contents); |
| 120 | + git_buf_free(&expected_path_oid); |
| 121 | + git_buf_free(&expected_path_fail); |
| 122 | + giterr_state_free(&error); |
| 123 | + |
| 124 | + return 0; |
| 125 | +} |
| 126 | + |
| 127 | +static void test_add_index(const char *safecrlf, const char *autocrlf, const char *attrs) |
| 128 | +{ |
| 129 | + git_buf attrbuf = GIT_BUF_INIT; |
| 130 | + git_buf expected_dirname = GIT_BUF_INIT; |
| 131 | + git_buf sandboxname = GIT_BUF_INIT; |
| 132 | + git_buf reponame = GIT_BUF_INIT; |
| 133 | + struct compare_data compare_data = { NULL, safecrlf, autocrlf, attrs }; |
| 134 | + const char *c; |
| 135 | + |
| 136 | + git_buf_puts(&reponame, "crlf"); |
| 137 | + |
| 138 | + git_buf_puts(&sandboxname, "safecrlf_"); |
| 139 | + git_buf_puts(&sandboxname, safecrlf); |
| 140 | + |
| 141 | + git_buf_puts(&sandboxname, ",autocrlf_"); |
| 142 | + git_buf_puts(&sandboxname, autocrlf); |
| 143 | + |
| 144 | + if (*attrs) { |
| 145 | + git_buf_puts(&sandboxname, ","); |
| 146 | + |
| 147 | + for (c = attrs; *c; c++) { |
| 148 | + if (*c == ' ') |
| 149 | + git_buf_putc(&sandboxname, ','); |
| 150 | + else if (*c == '=') |
| 151 | + git_buf_putc(&sandboxname, '_'); |
| 152 | + else |
| 153 | + git_buf_putc(&sandboxname, *c); |
| 154 | + } |
| 155 | + |
| 156 | + git_buf_printf(&attrbuf, "* %s\n", attrs); |
| 157 | + cl_git_mkfile("crlf/.gitattributes", attrbuf.ptr); |
| 158 | + } |
| 159 | + |
| 160 | + cl_repo_set_string(g_repo, "core.safecrlf", safecrlf); |
| 161 | + cl_repo_set_string(g_repo, "core.autocrlf", autocrlf); |
| 162 | + |
| 163 | + cl_git_pass(git_index_clear(g_index)); |
| 164 | + |
| 165 | + git_buf_joinpath(&expected_dirname, "crlf_data", "checkin_results"); |
| 166 | + git_buf_joinpath(&expected_fixture, expected_dirname.ptr, sandboxname.ptr); |
| 167 | + cl_fixture_sandbox(expected_fixture.ptr); |
| 168 | + |
| 169 | + compare_data.dirname = sandboxname.ptr; |
| 170 | + cl_git_pass(git_path_direach(&reponame, 0, add_and_check_file, &compare_data)); |
| 171 | + |
| 172 | + cl_fixture_cleanup(expected_fixture.ptr); |
| 173 | + git_buf_free(&expected_fixture); |
| 174 | + |
| 175 | + git_buf_free(&attrbuf); |
| 176 | + git_buf_free(&expected_fixture); |
| 177 | + git_buf_free(&expected_dirname); |
| 178 | + git_buf_free(&sandboxname); |
| 179 | + git_buf_free(&reponame); |
| 180 | +} |
| 181 | + |
| 182 | +static void set_up_workingdir(const char *name) |
| 183 | +{ |
| 184 | + git_vector contents = GIT_VECTOR_INIT; |
| 185 | + size_t i; |
| 186 | + const char *fn; |
| 187 | + |
| 188 | + git_path_dirload(&contents, name, 0, 0); |
| 189 | + git_vector_foreach(&contents, i, fn) { |
| 190 | + char *basename = git_path_basename(fn); |
| 191 | + bool skip = strncasecmp(basename, ".git", 4) == 0 && strlen(basename) == 4; |
| 192 | + |
| 193 | + git__free(basename); |
| 194 | + |
| 195 | + if (skip) |
| 196 | + continue; |
| 197 | + p_unlink(fn); |
| 198 | + } |
| 199 | + git_vector_free_deep(&contents); |
| 200 | + |
| 201 | + /* copy input files */ |
| 202 | + git_path_dirload(&contents, cl_fixture("crlf_data/checkin_input_files"), 0, 0); |
| 203 | + git_vector_foreach(&contents, i, fn) { |
| 204 | + char *basename = git_path_basename(fn); |
| 205 | + git_buf dest_filename = GIT_BUF_INIT; |
| 206 | + git_buf_joinpath(&dest_filename, name, basename); |
| 207 | + git__free(basename); |
| 208 | + cl_git_pass(git_futils_cp(fn, dest_filename.ptr, 0644)); |
| 209 | + git_buf_free(&dest_filename); |
| 210 | + } |
| 211 | + git_vector_free_deep(&contents); |
| 212 | +} |
| 213 | + |
| 214 | +void test_index_crlf__matches_core_git(void) |
| 215 | +{ |
| 216 | + const char *safecrlf[] = { "true", "false", "warn", NULL }; |
| 217 | + const char *autocrlf[] = { "true", "false", "input", NULL }; |
| 218 | + const char *attrs[] = { "", "-crlf", "-text", "eol=crlf", "eol=lf", |
| 219 | + "text", "text eol=crlf", "text eol=lf", |
| 220 | + "text=auto", "text=auto eol=crlf", "text=auto eol=lf", |
| 221 | + NULL }; |
| 222 | + const char **a, **b, **c; |
| 223 | + |
| 224 | + for (a = safecrlf; *a; a++) { |
| 225 | + for (b = autocrlf; *b; b++) { |
| 226 | + for (c = attrs; *c; c++) { |
| 227 | + set_up_workingdir("crlf"); |
| 228 | + test_add_index(*a, *b, *c); |
| 229 | + } |
| 230 | + } |
| 231 | + } |
27 | 232 | } |
28 | 233 |
|
29 | 234 | void test_index_crlf__autocrlf_false_no_attrs(void) |
|
0 commit comments