]> git.proxmox.com Git - libgit2.git/blob - src/attr.c
Merge pull request #2883 from urkud/reget-reader-pointer
[libgit2.git] / src / attr.c
1 #include "common.h"
2 #include "repository.h"
3 #include "sysdir.h"
4 #include "config.h"
5 #include "attr_file.h"
6 #include "ignore.h"
7 #include "git2/oid.h"
8 #include <ctype.h>
9
10 GIT__USE_STRMAP;
11
12 const char *git_attr__true = "[internal]__TRUE__";
13 const char *git_attr__false = "[internal]__FALSE__";
14 const char *git_attr__unset = "[internal]__UNSET__";
15
16 git_attr_t git_attr_value(const char *attr)
17 {
18 if (attr == NULL || attr == git_attr__unset)
19 return GIT_ATTR_UNSPECIFIED_T;
20
21 if (attr == git_attr__true)
22 return GIT_ATTR_TRUE_T;
23
24 if (attr == git_attr__false)
25 return GIT_ATTR_FALSE_T;
26
27 return GIT_ATTR_VALUE_T;
28 }
29
30 static int collect_attr_files(
31 git_repository *repo,
32 git_attr_session *attr_session,
33 uint32_t flags,
34 const char *path,
35 git_vector *files);
36
37 static void release_attr_files(git_vector *files);
38
39 int git_attr_get(
40 const char **value,
41 git_repository *repo,
42 uint32_t flags,
43 const char *pathname,
44 const char *name)
45 {
46 int error;
47 git_attr_path path;
48 git_vector files = GIT_VECTOR_INIT;
49 size_t i, j;
50 git_attr_file *file;
51 git_attr_name attr;
52 git_attr_rule *rule;
53
54 assert(value && repo && name);
55
56 *value = NULL;
57
58 if (git_attr_path__init(&path, pathname, git_repository_workdir(repo)) < 0)
59 return -1;
60
61 if ((error = collect_attr_files(repo, NULL, flags, pathname, &files)) < 0)
62 goto cleanup;
63
64 memset(&attr, 0, sizeof(attr));
65 attr.name = name;
66 attr.name_hash = git_attr_file__name_hash(name);
67
68 git_vector_foreach(&files, i, file) {
69
70 git_attr_file__foreach_matching_rule(file, &path, j, rule) {
71 size_t pos;
72
73 if (!git_vector_bsearch(&pos, &rule->assigns, &attr)) {
74 *value = ((git_attr_assignment *)git_vector_get(
75 &rule->assigns, pos))->value;
76 goto cleanup;
77 }
78 }
79 }
80
81 cleanup:
82 release_attr_files(&files);
83 git_attr_path__free(&path);
84
85 return error;
86 }
87
88
89 typedef struct {
90 git_attr_name name;
91 git_attr_assignment *found;
92 } attr_get_many_info;
93
94 int git_attr_get_many_with_session(
95 const char **values,
96 git_repository *repo,
97 git_attr_session *attr_session,
98 uint32_t flags,
99 const char *pathname,
100 size_t num_attr,
101 const char **names)
102 {
103 int error;
104 git_attr_path path;
105 git_vector files = GIT_VECTOR_INIT;
106 size_t i, j, k;
107 git_attr_file *file;
108 git_attr_rule *rule;
109 attr_get_many_info *info = NULL;
110 size_t num_found = 0;
111
112 if (!num_attr)
113 return 0;
114
115 assert(values && repo && names);
116
117 if (git_attr_path__init(&path, pathname, git_repository_workdir(repo)) < 0)
118 return -1;
119
120 if ((error = collect_attr_files(repo, attr_session, flags, pathname, &files)) < 0)
121 goto cleanup;
122
123 info = git__calloc(num_attr, sizeof(attr_get_many_info));
124 GITERR_CHECK_ALLOC(info);
125
126 git_vector_foreach(&files, i, file) {
127
128 git_attr_file__foreach_matching_rule(file, &path, j, rule) {
129
130 for (k = 0; k < num_attr; k++) {
131 size_t pos;
132
133 if (info[k].found != NULL) /* already found assignment */
134 continue;
135
136 if (!info[k].name.name) {
137 info[k].name.name = names[k];
138 info[k].name.name_hash = git_attr_file__name_hash(names[k]);
139 }
140
141 if (!git_vector_bsearch(&pos, &rule->assigns, &info[k].name)) {
142 info[k].found = (git_attr_assignment *)
143 git_vector_get(&rule->assigns, pos);
144 values[k] = info[k].found->value;
145
146 if (++num_found == num_attr)
147 goto cleanup;
148 }
149 }
150 }
151 }
152
153 for (k = 0; k < num_attr; k++) {
154 if (!info[k].found)
155 values[k] = NULL;
156 }
157
158 cleanup:
159 release_attr_files(&files);
160 git_attr_path__free(&path);
161 git__free(info);
162
163 return error;
164 }
165
166 int git_attr_get_many(
167 const char **values,
168 git_repository *repo,
169 uint32_t flags,
170 const char *pathname,
171 size_t num_attr,
172 const char **names)
173 {
174 return git_attr_get_many_with_session(
175 values, repo, NULL, flags, pathname, num_attr, names);
176 }
177
178 int git_attr_foreach(
179 git_repository *repo,
180 uint32_t flags,
181 const char *pathname,
182 int (*callback)(const char *name, const char *value, void *payload),
183 void *payload)
184 {
185 int error;
186 git_attr_path path;
187 git_vector files = GIT_VECTOR_INIT;
188 size_t i, j, k;
189 git_attr_file *file;
190 git_attr_rule *rule;
191 git_attr_assignment *assign;
192 git_strmap *seen = NULL;
193
194 assert(repo && callback);
195
196 if (git_attr_path__init(&path, pathname, git_repository_workdir(repo)) < 0)
197 return -1;
198
199 if ((error = collect_attr_files(repo, NULL, flags, pathname, &files)) < 0 ||
200 (error = git_strmap_alloc(&seen)) < 0)
201 goto cleanup;
202
203 git_vector_foreach(&files, i, file) {
204
205 git_attr_file__foreach_matching_rule(file, &path, j, rule) {
206
207 git_vector_foreach(&rule->assigns, k, assign) {
208 /* skip if higher priority assignment was already seen */
209 if (git_strmap_exists(seen, assign->name))
210 continue;
211
212 git_strmap_insert(seen, assign->name, assign, error);
213 if (error < 0)
214 goto cleanup;
215
216 error = callback(assign->name, assign->value, payload);
217 if (error) {
218 giterr_set_after_callback(error);
219 goto cleanup;
220 }
221 }
222 }
223 }
224
225 cleanup:
226 git_strmap_free(seen);
227 release_attr_files(&files);
228 git_attr_path__free(&path);
229
230 return error;
231 }
232
233 static int preload_attr_file(
234 git_repository *repo,
235 git_attr_session *attr_session,
236 git_attr_file_source source,
237 const char *base,
238 const char *file)
239 {
240 int error;
241 git_attr_file *preload = NULL;
242
243 if (!file)
244 return 0;
245 if (!(error = git_attr_cache__get(
246 &preload, repo, attr_session, source, base, file, git_attr_file__parse_buffer)))
247 git_attr_file__free(preload);
248
249 return error;
250 }
251
252 static int system_attr_file(
253 git_buf *out,
254 git_attr_session *attr_session)
255 {
256 int error;
257
258 if (!attr_session) {
259 error = git_sysdir_find_system_file(out, GIT_ATTR_FILE_SYSTEM);
260
261 if (error == GIT_ENOTFOUND)
262 giterr_clear();
263
264 return error;
265 }
266
267 if (!attr_session->init_sysdir) {
268 error = git_sysdir_find_system_file(&attr_session->sysdir, GIT_ATTR_FILE_SYSTEM);
269
270 if (error == GIT_ENOTFOUND)
271 giterr_clear();
272 else if (error)
273 return error;
274
275 attr_session->init_sysdir = 1;
276 }
277
278 if (attr_session->sysdir.size == 0)
279 return GIT_ENOTFOUND;
280
281 /* We can safely provide a git_buf with no allocation (asize == 0) to
282 * a consumer. This allows them to treat this as a regular `git_buf`,
283 * but their call to `git_buf_free` will not attempt to free it.
284 */
285 out->ptr = attr_session->sysdir.ptr;
286 out->size = attr_session->sysdir.size;
287 out->asize = 0;
288 return 0;
289 }
290
291 static int attr_setup(git_repository *repo, git_attr_session *attr_session)
292 {
293 int error = 0;
294 const char *workdir = git_repository_workdir(repo);
295 git_index *idx = NULL;
296 git_buf sys = GIT_BUF_INIT;
297
298 if (attr_session && attr_session->init_setup)
299 return 0;
300
301 if ((error = git_attr_cache__init(repo)) < 0)
302 return error;
303
304 /* preload attribute files that could contain macros so the
305 * definitions will be available for later file parsing
306 */
307
308 error = system_attr_file(&sys, attr_session);
309
310 if (error == 0)
311 error = preload_attr_file(
312 repo, attr_session, GIT_ATTR_FILE__FROM_FILE, NULL, sys.ptr);
313 else if (error != GIT_ENOTFOUND)
314 return error;
315
316 git_buf_free(&sys);
317
318 if ((error = preload_attr_file(
319 repo, attr_session, GIT_ATTR_FILE__FROM_FILE,
320 NULL, git_repository_attr_cache(repo)->cfg_attr_file)) < 0)
321 return error;
322
323 if ((error = preload_attr_file(
324 repo, attr_session, GIT_ATTR_FILE__FROM_FILE,
325 git_repository_path(repo), GIT_ATTR_FILE_INREPO)) < 0)
326 return error;
327
328 if (workdir != NULL &&
329 (error = preload_attr_file(
330 repo, attr_session, GIT_ATTR_FILE__FROM_FILE, workdir, GIT_ATTR_FILE)) < 0)
331 return error;
332
333 if ((error = git_repository_index__weakptr(&idx, repo)) < 0 ||
334 (error = preload_attr_file(
335 repo, attr_session, GIT_ATTR_FILE__FROM_INDEX, NULL, GIT_ATTR_FILE)) < 0)
336 return error;
337
338 if (attr_session)
339 attr_session->init_setup = 1;
340
341 return error;
342 }
343
344 int git_attr_add_macro(
345 git_repository *repo,
346 const char *name,
347 const char *values)
348 {
349 int error;
350 git_attr_rule *macro = NULL;
351 git_pool *pool;
352
353 if ((error = git_attr_cache__init(repo)) < 0)
354 return error;
355
356 macro = git__calloc(1, sizeof(git_attr_rule));
357 GITERR_CHECK_ALLOC(macro);
358
359 pool = &git_repository_attr_cache(repo)->pool;
360
361 macro->match.pattern = git_pool_strdup(pool, name);
362 GITERR_CHECK_ALLOC(macro->match.pattern);
363
364 macro->match.length = strlen(macro->match.pattern);
365 macro->match.flags = GIT_ATTR_FNMATCH_MACRO;
366
367 error = git_attr_assignment__parse(repo, pool, &macro->assigns, &values);
368
369 if (!error)
370 error = git_attr_cache__insert_macro(repo, macro);
371
372 if (error < 0)
373 git_attr_rule__free(macro);
374
375 return error;
376 }
377
378 typedef struct {
379 git_repository *repo;
380 git_attr_session *attr_session;
381 uint32_t flags;
382 const char *workdir;
383 git_index *index;
384 git_vector *files;
385 } attr_walk_up_info;
386
387 static int attr_decide_sources(
388 uint32_t flags, bool has_wd, bool has_index, git_attr_file_source *srcs)
389 {
390 int count = 0;
391
392 switch (flags & 0x03) {
393 case GIT_ATTR_CHECK_FILE_THEN_INDEX:
394 if (has_wd)
395 srcs[count++] = GIT_ATTR_FILE__FROM_FILE;
396 if (has_index)
397 srcs[count++] = GIT_ATTR_FILE__FROM_INDEX;
398 break;
399 case GIT_ATTR_CHECK_INDEX_THEN_FILE:
400 if (has_index)
401 srcs[count++] = GIT_ATTR_FILE__FROM_INDEX;
402 if (has_wd)
403 srcs[count++] = GIT_ATTR_FILE__FROM_FILE;
404 break;
405 case GIT_ATTR_CHECK_INDEX_ONLY:
406 if (has_index)
407 srcs[count++] = GIT_ATTR_FILE__FROM_INDEX;
408 break;
409 }
410
411 return count;
412 }
413
414 static int push_attr_file(
415 git_repository *repo,
416 git_attr_session *attr_session,
417 git_vector *list,
418 git_attr_file_source source,
419 const char *base,
420 const char *filename)
421 {
422 int error = 0;
423 git_attr_file *file = NULL;
424
425 error = git_attr_cache__get(&file, repo, attr_session,
426 source, base, filename, git_attr_file__parse_buffer);
427
428 if (error < 0)
429 return error;
430
431 if (file != NULL) {
432 if ((error = git_vector_insert(list, file)) < 0)
433 git_attr_file__free(file);
434 }
435
436 return error;
437 }
438
439 static int push_one_attr(void *ref, const char *path)
440 {
441 int error = 0, n_src, i;
442 attr_walk_up_info *info = (attr_walk_up_info *)ref;
443 git_attr_file_source src[2];
444
445 n_src = attr_decide_sources(
446 info->flags, info->workdir != NULL, info->index != NULL, src);
447
448 for (i = 0; !error && i < n_src; ++i)
449 error = push_attr_file(info->repo, info->attr_session,
450 info->files, src[i], path, GIT_ATTR_FILE);
451
452 return error;
453 }
454
455 static void release_attr_files(git_vector *files)
456 {
457 size_t i;
458 git_attr_file *file;
459
460 git_vector_foreach(files, i, file) {
461 git_attr_file__free(file);
462 files->contents[i] = NULL;
463 }
464 git_vector_free(files);
465 }
466
467 static int collect_attr_files(
468 git_repository *repo,
469 git_attr_session *attr_session,
470 uint32_t flags,
471 const char *path,
472 git_vector *files)
473 {
474 int error = 0;
475 git_buf dir = GIT_BUF_INIT;
476 const char *workdir = git_repository_workdir(repo);
477 attr_walk_up_info info = { NULL };
478
479 if ((error = attr_setup(repo, attr_session)) < 0)
480 return error;
481
482 /* Resolve path in a non-bare repo */
483 if (workdir != NULL)
484 error = git_path_find_dir(&dir, path, workdir);
485 else
486 error = git_path_dirname_r(&dir, path);
487 if (error < 0)
488 goto cleanup;
489
490 /* in precendence order highest to lowest:
491 * - $GIT_DIR/info/attributes
492 * - path components with .gitattributes
493 * - config core.attributesfile
494 * - $GIT_PREFIX/etc/gitattributes
495 */
496
497 error = push_attr_file(
498 repo, attr_session, files, GIT_ATTR_FILE__FROM_FILE,
499 git_repository_path(repo), GIT_ATTR_FILE_INREPO);
500 if (error < 0)
501 goto cleanup;
502
503 info.repo = repo;
504 info.attr_session = attr_session;
505 info.flags = flags;
506 info.workdir = workdir;
507 if (git_repository_index__weakptr(&info.index, repo) < 0)
508 giterr_clear(); /* no error even if there is no index */
509 info.files = files;
510
511 if (!strcmp(dir.ptr, "."))
512 error = push_one_attr(&info, "");
513 else
514 error = git_path_walk_up(&dir, workdir, push_one_attr, &info);
515
516 if (error < 0)
517 goto cleanup;
518
519 if (git_repository_attr_cache(repo)->cfg_attr_file != NULL) {
520 error = push_attr_file(
521 repo, attr_session, files, GIT_ATTR_FILE__FROM_FILE,
522 NULL, git_repository_attr_cache(repo)->cfg_attr_file);
523 if (error < 0)
524 goto cleanup;
525 }
526
527 if ((flags & GIT_ATTR_CHECK_NO_SYSTEM) == 0) {
528 error = system_attr_file(&dir, attr_session);
529
530 if (!error)
531 error = push_attr_file(
532 repo, attr_session, files, GIT_ATTR_FILE__FROM_FILE,
533 NULL, dir.ptr);
534 else if (error == GIT_ENOTFOUND)
535 error = 0;
536 }
537
538 cleanup:
539 if (error < 0)
540 release_attr_files(files);
541 git_buf_free(&dir);
542
543 return error;
544 }