deps/libgit2/src/libgit2/attr.c

filter: Statementstmt 62.4% 201 / 322 Branchbran 43.6% 96 / 220 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 54.8% 297 / 542 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« deps/libgit2/src/libgit2/diff_tform.c ↑ Summary xs/Diff/File.xs »
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7  
8 #include "attr.h"
9  
10 #include "repository.h"
11 #include "sysdir.h"
12 #include "config.h"
13 #include "attr_file.h"
14 #include "ignore.h"
15 #include "git2/oid.h"
16 #include <ctype.h>
17  
18 const char *git_attr__true = "[internal]__TRUE__";
19 const char *git_attr__false = "[internal]__FALSE__";
20 const char *git_attr__unset = "[internal]__UNSET__";
21  
22 2537 git_attr_value_t git_attr_value(const char *attr)
23 {
24 2537 ▶ if (attr == NULL || attr == git_attr__unset)
Branch: 2 branches
BranchTrueFalseTotal
if (attr == NULL || attr == git_attr__unset) 269 2268 2537
if (attr == NULL || attr == git_attr__unset) 0 269 269
25 2268 return GIT_ATTR_VALUE_UNSPECIFIED;
26  
27 269 ▶ if (attr == git_attr__true)
Branch: 1 branch
BranchTrueFalseTotal
if (attr == git_attr__true) 269 0 269
28 269 return GIT_ATTR_VALUE_TRUE;
29  
30 0 ▶ if (attr == git_attr__false)
Branch: 1 branch
BranchTrueFalseTotal
if (attr == git_attr__false) 0 0 0
31 0 return GIT_ATTR_VALUE_FALSE;
32  
33 0 return GIT_ATTR_VALUE_STRING;
34 }
35  
36 static int collect_attr_files(
37 git_repository *repo,
38 git_attr_session *attr_session,
39 git_attr_options *opts,
40 const char *path,
41 git_vector *files);
42  
43 static void release_attr_files(git_vector *files);
44  
45 4 int git_attr_get_ext(
46 const char **value,
47 git_repository *repo,
48 git_attr_options *opts,
49 const char *pathname,
50 const char *name)
51 {
52 int error;
53 git_attr_path path;
54 4 git_vector files = GIT_VECTOR_INIT;
55 size_t i, j;
56 git_attr_file *file;
57 git_attr_name attr;
58 git_attr_rule *rule;
59 4 git_dir_flag dir_flag = GIT_DIR_FLAG_UNKNOWN;
60  
61 4 ▶ GIT_ASSERT_ARG(value);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(value); 0 4 4
62 4 ▶ GIT_ASSERT_ARG(repo);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(repo); 0 4 4
63 4 ▶ GIT_ASSERT_ARG(name);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(name); 0 4 4
64 4 ▶ GIT_ERROR_CHECK_VERSION(opts, GIT_ATTR_OPTIONS_VERSION, "git_attr_options");
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_VERSION(opts, GIT_ATTR_OPTIONS_VERSION, "git_attr_options"); 0 4 4
65  
66 4 *value = NULL;
67  
68 4 ▶ if (git_repository_is_bare(repo))
Branch: 1 branch
BranchTrueFalseTotal
if (git_repository_is_bare(repo)) 0 4 4
69 0 dir_flag = GIT_DIR_FLAG_FALSE;
70  
71 4 ▶ if (git_attr_path__init(&path, pathname, git_repository_workdir(repo), dir_flag) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_attr_path__init(&path, pathname, git_repository_workdir(repo), dir_flag) < 0) 0 4 4
72 0 return -1;
73  
74 4 ▶ if ((error = collect_attr_files(repo, NULL, opts, pathname, &files)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = collect_attr_files(repo, NULL, opts, pathname, &files)) < 0) 0 4 4
75 0 goto cleanup;
76  
77 4 memset(&attr, 0, sizeof(attr));
78 4 attr.name = name;
79 4 attr.name_hash = git_attr_file__name_hash(name);
80  
81 12 ▶ git_vector_foreach(&files, i, file) {
Branch: 1 branch
BranchTrueFalseTotal
git_vector_foreach(&files, i, file) { 8 4 12
82  
83 8 ▶ git_attr_file__foreach_matching_rule(file, &path, j, rule) {
Branch: 2 branches
BranchTrueFalseTotal
git_attr_file__foreach_matching_rule(file, &path, j, rule) { 0 0 0
git_attr_file__foreach_matching_rule(file, &path, j, rule) { 0 8 8
84 size_t pos;
85  
86 0 ▶ if (!git_vector_bsearch(&pos, &rule->assigns, &attr)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!git_vector_bsearch(&pos, &rule->assigns, &attr)) { 0 0 0
87 0 *value = ((git_attr_assignment *)git_vector_get(
88 0 &rule->assigns, pos))->value;
89 0 goto cleanup;
90 }
91 }
92 }
93  
94 4 cleanup:
95 4 release_attr_files(&files);
96 4 git_attr_path__free(&path);
97  
98 4 return error;
99 }
100  
101 4 int git_attr_get(
102 const char **value,
103 git_repository *repo,
104 uint32_t flags,
105 const char *pathname,
106 const char *name)
107 {
108 4 git_attr_options opts = GIT_ATTR_OPTIONS_INIT;
109  
110 4 opts.flags = flags;
111  
112 4 return git_attr_get_ext(value, repo, &opts, pathname, name);
113 }
114  
115  
116 typedef struct {
117 git_attr_name name;
118 git_attr_assignment *found;
119 } attr_get_many_info;
120  
121 645 int git_attr_get_many_with_session(
122 const char **values,
123 git_repository *repo,
124 git_attr_session *attr_session,
125 git_attr_options *opts,
126 const char *pathname,
127 size_t num_attr,
128 const char **names)
129 {
130 int error;
131 git_attr_path path;
132 645 git_vector files = GIT_VECTOR_INIT;
133 size_t i, j, k;
134 git_attr_file *file;
135 git_attr_rule *rule;
136 645 attr_get_many_info *info = NULL;
137 645 size_t num_found = 0;
138 645 git_dir_flag dir_flag = GIT_DIR_FLAG_UNKNOWN;
139  
140 645 ▶ if (!num_attr)
Branch: 1 branch
BranchTrueFalseTotal
if (!num_attr) 0 645 645
141 0 return 0;
142  
143 645 ▶ GIT_ASSERT_ARG(values);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(values); 0 645 645
144 645 ▶ GIT_ASSERT_ARG(repo);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(repo); 0 645 645
145 645 ▶ GIT_ASSERT_ARG(pathname);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(pathname); 0 645 645
146 645 ▶ GIT_ASSERT_ARG(names);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(names); 0 645 645
147 645 ▶ GIT_ERROR_CHECK_VERSION(opts, GIT_ATTR_OPTIONS_VERSION, "git_attr_options");
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_VERSION(opts, GIT_ATTR_OPTIONS_VERSION, "git_attr_options"); 0 645 645
148  
149 645 ▶ if (git_repository_is_bare(repo))
Branch: 1 branch
BranchTrueFalseTotal
if (git_repository_is_bare(repo)) 0 645 645
150 0 dir_flag = GIT_DIR_FLAG_FALSE;
151  
152 645 ▶ if (git_attr_path__init(&path, pathname, git_repository_workdir(repo), dir_flag) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_attr_path__init(&path, pathname, git_repository_workdir(repo), dir_flag) < 0) 0 645 645
153 0 return -1;
154  
155 645 ▶ if ((error = collect_attr_files(repo, attr_session, opts, pathname, &files)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = collect_attr_files(repo, attr_session, opts, pathname, &files)) < 0) 0 645 645
156 0 goto cleanup;
157  
158 645 info = git__calloc(num_attr, sizeof(attr_get_many_info));
159 645 ▶ GIT_ERROR_CHECK_ALLOC(info);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(info); 0 645 645
160  
161 2059 ▶ git_vector_foreach(&files, i, file) {
Branch: 1 branch
BranchTrueFalseTotal
git_vector_foreach(&files, i, file) { 1414 645 2059
162  
163 1414 ▶ git_attr_file__foreach_matching_rule(file, &path, j, rule) {
Branch: 2 branches
BranchTrueFalseTotal
git_attr_file__foreach_matching_rule(file, &path, j, rule) { 0 0 0
git_attr_file__foreach_matching_rule(file, &path, j, rule) { 0 1414 1414
164  
165 0 ▶ for (k = 0; k < num_attr; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < num_attr; k++) { 0 0 0
166 size_t pos;
167  
168 0 ▶ if (info[k].found != NULL) /* already found assignment */
Branch: 1 branch
BranchTrueFalseTotal
if (info[k].found != NULL) /* already found assignment */ 0 0 0
169 0 continue;
170  
171 0 ▶ if (!info[k].name.name) {
Branch: 1 branch
BranchTrueFalseTotal
if (!info[k].name.name) { 0 0 0
172 0 info[k].name.name = names[k];
173 0 info[k].name.name_hash = git_attr_file__name_hash(names[k]);
174 }
175  
176 0 ▶ if (!git_vector_bsearch(&pos, &rule->assigns, &info[k].name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!git_vector_bsearch(&pos, &rule->assigns, &info[k].name)) { 0 0 0
177 0 info[k].found = (git_attr_assignment *)
178 0 git_vector_get(&rule->assigns, pos);
179 0 values[k] = info[k].found->value;
180  
181 0 ▶ if (++num_found == num_attr)
Branch: 1 branch
BranchTrueFalseTotal
if (++num_found == num_attr) 0 0 0
182 0 goto cleanup;
183 }
184 }
185 }
186 }
187  
188 1828 ▶ for (k = 0; k < num_attr; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < num_attr; k++) { 1183 645 1828
189 1183 ▶ if (!info[k].found)
Branch: 1 branch
BranchTrueFalseTotal
if (!info[k].found) 1183 0 1183
190 1183 values[k] = NULL;
191 }
192  
193 645 cleanup:
194 645 release_attr_files(&files);
195 645 git_attr_path__free(&path);
196 645 git__free(info);
197  
198 645 return error;
199 }
200  
201 0 int git_attr_get_many(
202 const char **values,
203 git_repository *repo,
204 uint32_t flags,
205 const char *pathname,
206 size_t num_attr,
207 const char **names)
208 {
209 0 git_attr_options opts = GIT_ATTR_OPTIONS_INIT;
210  
211 0 opts.flags = flags;
212  
213 0 return git_attr_get_many_with_session(
214 values, repo, NULL, &opts, pathname, num_attr, names);
215 }
216  
217 0 int git_attr_get_many_ext(
218 const char **values,
219 git_repository *repo,
220 git_attr_options *opts,
221 const char *pathname,
222 size_t num_attr,
223 const char **names)
224 {
225 0 return git_attr_get_many_with_session(
226 values, repo, NULL, opts, pathname, num_attr, names);
227 }
228  
229 0 int git_attr_foreach(
230 git_repository *repo,
231 uint32_t flags,
232 const char *pathname,
233 int (*callback)(const char *name, const char *value, void *payload),
234 void *payload)
235 {
236 0 git_attr_options opts = GIT_ATTR_OPTIONS_INIT;
237  
238 0 opts.flags = flags;
239  
240 0 return git_attr_foreach_ext(repo, &opts, pathname, callback, payload);
241 }
242  
243 0 int git_attr_foreach_ext(
244 git_repository *repo,
245 git_attr_options *opts,
246 const char *pathname,
247 int (*callback)(const char *name, const char *value, void *payload),
248 void *payload)
249 {
250 int error;
251 git_attr_path path;
252 0 git_vector files = GIT_VECTOR_INIT;
253 size_t i, j, k;
254 git_attr_file *file;
255 git_attr_rule *rule;
256 git_attr_assignment *assign;
257 0 git_strmap *seen = NULL;
258 0 git_dir_flag dir_flag = GIT_DIR_FLAG_UNKNOWN;
259  
260 0 ▶ GIT_ASSERT_ARG(repo);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(repo); 0 0 0
261 0 ▶ GIT_ASSERT_ARG(callback);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(callback); 0 0 0
262 0 ▶ GIT_ERROR_CHECK_VERSION(opts, GIT_ATTR_OPTIONS_VERSION, "git_attr_options");
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_VERSION(opts, GIT_ATTR_OPTIONS_VERSION, "git_attr_options"); 0 0 0
263  
264 0 ▶ if (git_repository_is_bare(repo))
Branch: 1 branch
BranchTrueFalseTotal
if (git_repository_is_bare(repo)) 0 0 0
265 0 dir_flag = GIT_DIR_FLAG_FALSE;
266  
267 0 ▶ if (git_attr_path__init(&path, pathname, git_repository_workdir(repo), dir_flag) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_attr_path__init(&path, pathname, git_repository_workdir(repo), dir_flag) < 0) 0 0 0
268 0 return -1;
269  
270 0 if ((error = collect_attr_files(repo, NULL, opts, pathname, &files)) < 0 ||
271 0 (error = git_strmap_new(&seen)) < 0)
272 0 goto cleanup;
273  
274 0 ▶ git_vector_foreach(&files, i, file) {
Branch: 1 branch
BranchTrueFalseTotal
git_vector_foreach(&files, i, file) { 0 0 0
275  
276 0 ▶ git_attr_file__foreach_matching_rule(file, &path, j, rule) {
Branch: 2 branches
BranchTrueFalseTotal
git_attr_file__foreach_matching_rule(file, &path, j, rule) { 0 0 0
git_attr_file__foreach_matching_rule(file, &path, j, rule) { 0 0 0
277  
278 0 ▶ git_vector_foreach(&rule->assigns, k, assign) {
Branch: 1 branch
BranchTrueFalseTotal
git_vector_foreach(&rule->assigns, k, assign) { 0 0 0
279 /* skip if higher priority assignment was already seen */
280 0 ▶ if (git_strmap_exists(seen, assign->name))
Branch: 1 branch
BranchTrueFalseTotal
if (git_strmap_exists(seen, assign->name)) 0 0 0
281 0 continue;
282  
283 0 ▶ if ((error = git_strmap_set(seen, assign->name, assign)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_strmap_set(seen, assign->name, assign)) < 0) 0 0 0
284 0 goto cleanup;
285  
286 0 error = callback(assign->name, assign->value, payload);
287 0 ▶ if (error) {
Branch: 1 branch
BranchTrueFalseTotal
if (error) { 0 0 0
288 0 git_error_set_after_callback(error);
289 0 goto cleanup;
290 }
291 }
292 }
293 }
294  
295 0 cleanup:
296 0 git_strmap_free(seen);
297 0 release_attr_files(&files);
298 0 git_attr_path__free(&path);
299  
300 0 return error;
301 }
302  
303 1605 static int preload_attr_source(
304 git_repository *repo,
305 git_attr_session *attr_session,
306 git_attr_file_source *source)
307 {
308 int error;
309 1605 git_attr_file *preload = NULL;
310  
311 1605 ▶ if (!source)
Branch: 1 branch
BranchTrueFalseTotal
if (!source) 0 1605 1605
312 0 return 0;
313  
314 1605 error = git_attr_cache__get(&preload, repo, attr_session, source,
315 git_attr_file__parse_buffer, true);
316  
317 1605 ▶ if (!error)
Branch: 1 branch
BranchTrueFalseTotal
if (!error) 1605 0 1605
318 1605 git_attr_file__free(preload);
319  
320 1605 return error;
321 }
322  
323 1605 GIT_INLINE(int) preload_attr_file(
324 git_repository *repo,
325 git_attr_session *attr_session,
326 const char *base,
327 const char *filename)
328 {
329 1605 git_attr_file_source source = { GIT_ATTR_FILE_SOURCE_FILE };
330  
331 1605 ▶ if (!filename)
Branch: 1 branch
BranchTrueFalseTotal
if (!filename) 535 1070 1605
332 535 return 0;
333  
334 1070 source.base = base;
335 1070 source.filename = filename;
336  
337 1070 return preload_attr_source(repo, attr_session, &source);
338 }
339  
340 1184 static int system_attr_file(
341 git_str *out,
342 git_attr_session *attr_session)
343 {
344 int error;
345  
346 1184 ▶ if (!attr_session) {
Branch: 1 branch
BranchTrueFalseTotal
if (!attr_session) { 962 222 1184
347 962 error = git_sysdir_find_system_file(out, GIT_ATTR_FILE_SYSTEM);
348  
349 962 ▶ if (error == GIT_ENOTFOUND)
Branch: 1 branch
BranchTrueFalseTotal
if (error == GIT_ENOTFOUND) 962 0 962
350 962 git_error_clear();
351  
352 962 return error;
353 }
354  
355 222 ▶ if (!attr_session->init_sysdir) {
Branch: 1 branch
BranchTrueFalseTotal
if (!attr_session->init_sysdir) { 54 168 222
356 54 error = git_sysdir_find_system_file(&attr_session->sysdir, GIT_ATTR_FILE_SYSTEM);
357  
358 54 ▶ if (error == GIT_ENOTFOUND)
Branch: 1 branch
BranchTrueFalseTotal
if (error == GIT_ENOTFOUND) 54 0 54
359 54 git_error_clear();
360 0 ▶ else if (error)
Branch: 1 branch
BranchTrueFalseTotal
else if (error) 0 0 0
361 0 return error;
362  
363 54 attr_session->init_sysdir = 1;
364 }
365  
366 222 ▶ if (attr_session->sysdir.size == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (attr_session->sysdir.size == 0) 222 0 222
367 222 return GIT_ENOTFOUND;
368  
369 /* We can safely provide a git_str with no allocation (asize == 0) to
370 * a consumer. This allows them to treat this as a regular `git_str`,
371 * but their call to `git_str_dispose` will not attempt to free it.
372 */
373 0 git_str_attach_notowned(
374 0 out, attr_session->sysdir.ptr, attr_session->sysdir.size);
375 0 return 0;
376 }
377  
378 649 static int attr_setup(
379 git_repository *repo,
380 git_attr_session *attr_session,
381 git_attr_options *opts)
382 {
383 649 git_str system = GIT_STR_INIT, info = GIT_STR_INIT;
384 649 git_attr_file_source index_source = { GIT_ATTR_FILE_SOURCE_INDEX, NULL, GIT_ATTR_FILE, NULL };
385 649 git_attr_file_source head_source = { GIT_ATTR_FILE_SOURCE_HEAD, NULL, GIT_ATTR_FILE, NULL };
386 649 git_attr_file_source commit_source = { GIT_ATTR_FILE_SOURCE_COMMIT, NULL, GIT_ATTR_FILE, NULL };
387 649 git_index *idx = NULL;
388 const char *workdir;
389 649 int error = 0;
390  
391 649 ▶ if (attr_session && attr_session->init_setup)
Branch: 2 branches
BranchTrueFalseTotal
if (attr_session && attr_session->init_setup) 168 481 649
if (attr_session && attr_session->init_setup) 114 54 168
392 114 return 0;
393  
394 535 ▶ if ((error = git_attr_cache__init(repo)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_attr_cache__init(repo)) < 0) 0 535 535
395 0 return error;
396  
397 /*
398 * Preload attribute files that could contain macros so the
399 * definitions will be available for later file parsing.
400 */
401  
402 535 if ((error = system_attr_file(&system, attr_session)) < 0 ||
403 0 (error = preload_attr_file(repo, attr_session, NULL, system.ptr)) < 0) {
404 535 ▶ if (error != GIT_ENOTFOUND)
Branch: 1 branch
BranchTrueFalseTotal
if (error != GIT_ENOTFOUND) 0 535 535
405 0 goto out;
406  
407 535 error = 0;
408 }
409  
410 535 ▶ if ((error = preload_attr_file(repo, attr_session, NULL,
Branch: 1 branch
BranchTrueFalseTotal
if ((error = preload_attr_file(repo, attr_session, NULL, 0 535 535
411 535 git_repository_attr_cache(repo)->cfg_attr_file)) < 0)
412 0 goto out;
413  
414 1070 if ((error = git_repository__item_path(&info, repo, GIT_REPOSITORY_ITEM_INFO)) < 0 ||
415 535 (error = preload_attr_file(repo, attr_session, info.ptr, GIT_ATTR_FILE_INREPO)) < 0) {
416 0 ▶ if (error != GIT_ENOTFOUND)
Branch: 1 branch
BranchTrueFalseTotal
if (error != GIT_ENOTFOUND) 0 0 0
417 0 goto out;
418  
419 0 error = 0;
420 }
421  
422 1070 if ((workdir = git_repository_workdir(repo)) != NULL &&
423 535 (error = preload_attr_file(repo, attr_session, workdir, GIT_ATTR_FILE)) < 0)
424 0 goto out;
425  
426 1070 if ((error = git_repository_index__weakptr(&idx, repo)) < 0 ||
427 535 (error = preload_attr_source(repo, attr_session, &index_source)) < 0)
428 0 goto out;
429  
430 535 ▶ if ((opts && (opts->flags & GIT_ATTR_CHECK_INCLUDE_HEAD) != 0) &&
Branch: 1 branch
BranchTrueFalseTotal
if ((opts && (opts->flags & GIT_ATTR_CHECK_INCLUDE_HEAD) != 0) && 511 24 535
431 0 (error = preload_attr_source(repo, attr_session, &head_source)) < 0)
432 0 goto out;
433  
434 535 ▶ if ((opts && (opts->flags & GIT_ATTR_CHECK_INCLUDE_COMMIT) != 0)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((opts && (opts->flags & GIT_ATTR_CHECK_INCLUDE_COMMIT) != 0)) { 511 24 535
if ((opts && (opts->flags & GIT_ATTR_CHECK_INCLUDE_COMMIT) != 0)) { 0 511 511
435 #ifndef GIT_DEPRECATE_HARD
436 0 ▶ if (opts->commit_id)
Branch: 1 branch
BranchTrueFalseTotal
if (opts->commit_id) 0 0 0
437 0 commit_source.commit_id = opts->commit_id;
438 else
439 #endif
440 0 commit_source.commit_id = &opts->attr_commit_id;
441  
442 0 ▶ if ((error = preload_attr_source(repo, attr_session, &commit_source)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = preload_attr_source(repo, attr_session, &commit_source)) < 0) 0 0 0
443 0 goto out;
444 }
445  
446 535 ▶ if (attr_session)
Branch: 1 branch
BranchTrueFalseTotal
if (attr_session) 481 54 535
447 54 attr_session->init_setup = 1;
448  
449 481 out:
450 535 git_str_dispose(&system);
451 535 git_str_dispose(&info);
452  
453 535 return error;
454 }
455  
456 16 int git_attr_add_macro(
457 git_repository *repo,
458 const char *name,
459 const char *values)
460 {
461 int error;
462 16 git_attr_rule *macro = NULL;
463 git_pool *pool;
464  
465 16 ▶ GIT_ASSERT_ARG(repo);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(repo); 0 16 16
466 16 ▶ GIT_ASSERT_ARG(name);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT_ARG(name); 0 16 16
467  
468 16 ▶ if ((error = git_attr_cache__init(repo)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_attr_cache__init(repo)) < 0) 0 16 16
469 0 return error;
470  
471 16 macro = git__calloc(1, sizeof(git_attr_rule));
472 16 ▶ GIT_ERROR_CHECK_ALLOC(macro);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(macro); 0 16 16
473  
474 16 pool = &git_repository_attr_cache(repo)->pool;
475  
476 16 macro->match.pattern = git_pool_strdup(pool, name);
477 16 ▶ GIT_ERROR_CHECK_ALLOC(macro->match.pattern);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(macro->match.pattern); 0 16 16
478  
479 16 macro->match.length = strlen(macro->match.pattern);
480 16 macro->match.flags = GIT_ATTR_FNMATCH_MACRO;
481  
482 16 error = git_attr_assignment__parse(repo, pool, &macro->assigns, &values);
483  
484 16 ▶ if (!error)
Branch: 1 branch
BranchTrueFalseTotal
if (!error) 16 0 16
485 16 error = git_attr_cache__insert_macro(repo, macro);
486  
487 16 ▶ if (error < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (error < 0) 0 16 16
488 0 git_attr_rule__free(macro);
489  
490 16 return error;
491 }
492  
493 typedef struct {
494 git_repository *repo;
495 git_attr_session *attr_session;
496 git_attr_options *opts;
497 const char *workdir;
498 git_index *index;
499 git_vector *files;
500 } attr_walk_up_info;
501  
502 773 static int attr_decide_sources(
503 uint32_t flags,
504 bool has_wd,
505 bool has_index,
506 git_attr_file_source_t *srcs)
507 {
508 773 int count = 0;
509  
510 773 switch (flags & 0x03) {
511 773 case GIT_ATTR_CHECK_FILE_THEN_INDEX:
512 773 ▶ if (has_wd)
Branch: 1 branch
BranchTrueFalseTotal
if (has_wd) 773 0 773
513 773 srcs[count++] = GIT_ATTR_FILE_SOURCE_FILE;
514 773 ▶ if (has_index)
Branch: 1 branch
BranchTrueFalseTotal
if (has_index) 773 0 773
515 773 srcs[count++] = GIT_ATTR_FILE_SOURCE_INDEX;
516 773 break;
517 0 case GIT_ATTR_CHECK_INDEX_THEN_FILE:
518 0 ▶ if (has_index)
Branch: 1 branch
BranchTrueFalseTotal
if (has_index) 0 0 0
519 0 srcs[count++] = GIT_ATTR_FILE_SOURCE_INDEX;
520 0 ▶ if (has_wd)
Branch: 1 branch
BranchTrueFalseTotal
if (has_wd) 0 0 0
521 0 srcs[count++] = GIT_ATTR_FILE_SOURCE_FILE;
522 0 break;
523 0 case GIT_ATTR_CHECK_INDEX_ONLY:
524 0 ▶ if (has_index)
Branch: 1 branch
BranchTrueFalseTotal
if (has_index) 0 0 0
525 0 srcs[count++] = GIT_ATTR_FILE_SOURCE_INDEX;
526 0 break;
527 }
528  
529 773 ▶ if ((flags & GIT_ATTR_CHECK_INCLUDE_HEAD) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((flags & GIT_ATTR_CHECK_INCLUDE_HEAD) != 0) 0 773 773
530 0 srcs[count++] = GIT_ATTR_FILE_SOURCE_HEAD;
531  
532 773 ▶ if ((flags & GIT_ATTR_CHECK_INCLUDE_COMMIT) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((flags & GIT_ATTR_CHECK_INCLUDE_COMMIT) != 0) 0 773 773
533 0 srcs[count++] = GIT_ATTR_FILE_SOURCE_COMMIT;
534  
535 773 return count;
536 }
537  
538 2195 static int push_attr_source(
539 git_repository *repo,
540 git_attr_session *attr_session,
541 git_vector *list,
542 git_attr_file_source *source,
543 bool allow_macros)
544 {
545 2195 int error = 0;
546 2195 git_attr_file *file = NULL;
547  
548 2195 error = git_attr_cache__get(&file, repo, attr_session,
549 source,
550 git_attr_file__parse_buffer,
551 allow_macros);
552  
553 2195 ▶ if (error < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (error < 0) 0 2195 2195
554 0 return error;
555  
556 2195 ▶ if (file != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (file != NULL) { 1422 773 2195
557 1422 ▶ if ((error = git_vector_insert(list, file)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_vector_insert(list, file)) < 0) 0 1422 1422
558 0 git_attr_file__free(file);
559 }
560  
561 2195 return error;
562 }
563  
564 649 GIT_INLINE(int) push_attr_file(
565 git_repository *repo,
566 git_attr_session *attr_session,
567 git_vector *list,
568 const char *base,
569 const char *filename)
570 {
571 649 git_attr_file_source source = { GIT_ATTR_FILE_SOURCE_FILE, base, filename };
572 649 return push_attr_source(repo, attr_session, list, &source, true);
573 }
574  
575 773 static int push_one_attr(void *ref, const char *path)
576 {
577 773 attr_walk_up_info *info = (attr_walk_up_info *)ref;
578 git_attr_file_source_t src[GIT_ATTR_FILE_NUM_SOURCES];
579 773 int error = 0, n_src, i;
580 bool allow_macros;
581  
582 773 n_src = attr_decide_sources(info->opts ? info->opts->flags : 0,
583 773 info->workdir != NULL,
584 773 ▶ info->index != NULL,
Branch: 1 branch
BranchTrueFalseTotal
info->index != NULL, 639 134 773
585 src);
586  
587 773 ▶ allow_macros = info->workdir ? !strcmp(info->workdir, path) : false;
Branch: 2 branches
BranchTrueFalseTotal
allow_macros = info->workdir ? !strcmp(info->workdir, path) : false; 773 0 773
allow_macros = info->workdir ? !strcmp(info->workdir, path) : false; 649 124 773
588  
589 2319 ▶ for (i = 0; !error && i < n_src; ++i) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; !error && i < n_src; ++i) { 2319 0 2319
for (i = 0; !error && i < n_src; ++i) { 1546 773 2319
590 1546 git_attr_file_source source = { src[i], path, GIT_ATTR_FILE };
591  
592 1546 ▶ if (src[i] == GIT_ATTR_FILE_SOURCE_COMMIT && info->opts) {
Branch: 2 branches
BranchTrueFalseTotal
if (src[i] == GIT_ATTR_FILE_SOURCE_COMMIT && info->opts) { 0 1546 1546
if (src[i] == GIT_ATTR_FILE_SOURCE_COMMIT && info->opts) { 0 0 0
593 #ifndef GIT_DEPRECATE_HARD
594 0 ▶ if (info->opts->commit_id)
Branch: 1 branch
BranchTrueFalseTotal
if (info->opts->commit_id) 0 0 0
595 0 source.commit_id = info->opts->commit_id;
596 else
597 #endif
598 0 source.commit_id = &info->opts->attr_commit_id;
599 }
600  
601 1546 error = push_attr_source(info->repo, info->attr_session, info->files,
602 &source, allow_macros);
603 }
604  
605 773 return error;
606 }
607  
608 649 static void release_attr_files(git_vector *files)
609 {
610 size_t i;
611 git_attr_file *file;
612  
613 2071 ▶ git_vector_foreach(files, i, file) {
Branch: 1 branch
BranchTrueFalseTotal
git_vector_foreach(files, i, file) { 1422 649 2071
614 1422 git_attr_file__free(file);
615 1422 files->contents[i] = NULL;
616 }
617 649 git_vector_free(files);
618 649 }
619  
620 649 static int collect_attr_files(
621 git_repository *repo,
622 git_attr_session *attr_session,
623 git_attr_options *opts,
624 const char *path,
625 git_vector *files)
626 {
627 649 int error = 0;
628 649 git_str dir = GIT_STR_INIT, attrfile = GIT_STR_INIT;
629 649 const char *workdir = git_repository_workdir(repo);
630 649 attr_walk_up_info info = { NULL };
631  
632 649 ▶ GIT_ASSERT(!git_fs_path_is_absolute(path));
Branch: 1 branch
BranchTrueFalseTotal
GIT_ASSERT(!git_fs_path_is_absolute(path)); 0 649 649
633  
634 649 ▶ if ((error = attr_setup(repo, attr_session, opts)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = attr_setup(repo, attr_session, opts)) < 0) 0 649 649
635 0 return error;
636  
637 /* Resolve path in a non-bare repo */
638 649 ▶ if (workdir != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (workdir != NULL) { 649 0 649
639 649 ▶ if (!(error = git_repository_workdir_path(&dir, repo, path)))
Branch: 1 branch
BranchTrueFalseTotal
if (!(error = git_repository_workdir_path(&dir, repo, path))) 649 0 649
640 649 error = git_fs_path_find_dir(&dir);
641 }
642 else {
643 0 error = git_fs_path_dirname_r(&dir, path);
644 }
645  
646 649 ▶ if (error < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (error < 0) 0 649 649
647 0 goto cleanup;
648  
649 /* in precedence order highest to lowest:
650 * - $GIT_DIR/info/attributes
651 * - path components with .gitattributes
652 * - config core.attributesfile
653 * - $GIT_PREFIX/etc/gitattributes
654 */
655  
656 1298 if ((error = git_repository__item_path(&attrfile, repo, GIT_REPOSITORY_ITEM_INFO)) < 0 ||
657 649 (error = push_attr_file(repo, attr_session, files, attrfile.ptr, GIT_ATTR_FILE_INREPO)) < 0) {
658 0 ▶ if (error != GIT_ENOTFOUND)
Branch: 1 branch
BranchTrueFalseTotal
if (error != GIT_ENOTFOUND) 0 0 0
659 0 goto cleanup;
660 }
661  
662 649 info.repo = repo;
663 649 info.attr_session = attr_session;
664 649 info.opts = opts;
665 649 info.workdir = workdir;
666 649 ▶ if (git_repository_index__weakptr(&info.index, repo) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_repository_index__weakptr(&info.index, repo) < 0) 0 649 649
667 0 git_error_clear(); /* no error even if there is no index */
668 649 info.files = files;
669  
670 649 ▶ if (!strcmp(dir.ptr, "."))
Branch: 1 branch
BranchTrueFalseTotal
if (!strcmp(dir.ptr, ".")) 0 649 649
671 0 error = push_one_attr(&info, "");
672 else
673 649 error = git_fs_path_walk_up(&dir, workdir, push_one_attr, &info);
674  
675 649 ▶ if (error < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (error < 0) 0 649 649
676 0 goto cleanup;
677  
678 649 ▶ if (git_repository_attr_cache(repo)->cfg_attr_file != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (git_repository_attr_cache(repo)->cfg_attr_file != NULL) { 0 649 649
679 0 error = push_attr_file(repo, attr_session, files, NULL, git_repository_attr_cache(repo)->cfg_attr_file);
680 0 ▶ if (error < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (error < 0) 0 0 0
681 0 goto cleanup;
682 }
683  
684 649 ▶ if (!opts || (opts->flags & GIT_ATTR_CHECK_NO_SYSTEM) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (!opts || (opts->flags & GIT_ATTR_CHECK_NO_SYSTEM) == 0) { 549 100 649
if (!opts || (opts->flags & GIT_ATTR_CHECK_NO_SYSTEM) == 0) { 0 549 549
685 649 error = system_attr_file(&dir, attr_session);
686  
687 649 ▶ if (!error)
Branch: 1 branch
BranchTrueFalseTotal
if (!error) 0 649 649
688 0 error = push_attr_file(repo, attr_session, files, NULL, dir.ptr);
689 649 ▶ else if (error == GIT_ENOTFOUND)
Branch: 1 branch
BranchTrueFalseTotal
else if (error == GIT_ENOTFOUND) 0 649 649
690 649 error = 0;
691 }
692  
693 0 cleanup:
694 649 ▶ if (error < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (error < 0) 0 649 649
695 0 release_attr_files(files);
696 649 git_str_dispose(&attrfile);
697 649 git_str_dispose(&dir);
698  
699 649 return error;
700 }
« deps/libgit2/src/libgit2/diff_tform.c ↑ Summary xs/Diff/File.xs »