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