]> git.proxmox.com Git - libgit2.git/blob - src/attr.c
repository: use `git_repository_item_path`
[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), GIT_DIR_FLAG_UNKNOWN) < 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), GIT_DIR_FLAG_UNKNOWN) < 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), GIT_DIR_FLAG_UNKNOWN) < 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 git_buf_attach_notowned(
286 out, attr_session->sysdir.ptr, attr_session->sysdir.size);
287 return 0;
288 }
289
290 static int attr_setup(git_repository *repo, git_attr_session *attr_session)
291 {
292 int error = 0;
293 const char *workdir = git_repository_workdir(repo);
294 git_index *idx = NULL;
295 git_buf path = GIT_BUF_INIT;
296
297 if (attr_session && attr_session->init_setup)
298 return 0;
299
300 if ((error = git_attr_cache__init(repo)) < 0)
301 return error;
302
303 /* preload attribute files that could contain macros so the
304 * definitions will be available for later file parsing
305 */
306
307 error = system_attr_file(&path, attr_session);
308
309 if (error == 0)
310 error = preload_attr_file(
311 repo, attr_session, GIT_ATTR_FILE__FROM_FILE, NULL, path.ptr);
312
313 if (error != GIT_ENOTFOUND)
314 goto out;
315
316 if ((error = preload_attr_file(
317 repo, attr_session, GIT_ATTR_FILE__FROM_FILE,
318 NULL, git_repository_attr_cache(repo)->cfg_attr_file)) < 0)
319 goto out;
320
321 if ((error = git_repository_item_path(&path,
322 repo, GIT_REPOSITORY_ITEM_INFO)) < 0)
323 goto out;
324
325 if ((error = preload_attr_file(
326 repo, attr_session, GIT_ATTR_FILE__FROM_FILE,
327 path.ptr, GIT_ATTR_FILE_INREPO)) < 0)
328 goto out;
329
330 if (workdir != NULL &&
331 (error = preload_attr_file(
332 repo, attr_session, GIT_ATTR_FILE__FROM_FILE, workdir, GIT_ATTR_FILE)) < 0)
333 goto out;
334
335 if ((error = git_repository_index__weakptr(&idx, repo)) < 0 ||
336 (error = preload_attr_file(
337 repo, attr_session, GIT_ATTR_FILE__FROM_INDEX, NULL, GIT_ATTR_FILE)) < 0)
338 goto out;
339
340 if (attr_session)
341 attr_session->init_setup = 1;
342
343 out:
344 git_buf_free(&path);
345
346 return error;
347 }
348
349 int git_attr_add_macro(
350 git_repository *repo,
351 const char *name,
352 const char *values)
353 {
354 int error;
355 git_attr_rule *macro = NULL;
356 git_pool *pool;
357
358 if ((error = git_attr_cache__init(repo)) < 0)
359 return error;
360
361 macro = git__calloc(1, sizeof(git_attr_rule));
362 GITERR_CHECK_ALLOC(macro);
363
364 pool = &git_repository_attr_cache(repo)->pool;
365
366 macro->match.pattern = git_pool_strdup(pool, name);
367 GITERR_CHECK_ALLOC(macro->match.pattern);
368
369 macro->match.length = strlen(macro->match.pattern);
370 macro->match.flags = GIT_ATTR_FNMATCH_MACRO;
371
372 error = git_attr_assignment__parse(repo, pool, &macro->assigns, &values);
373
374 if (!error)
375 error = git_attr_cache__insert_macro(repo, macro);
376
377 if (error < 0)
378 git_attr_rule__free(macro);
379
380 return error;
381 }
382
383 typedef struct {
384 git_repository *repo;
385 git_attr_session *attr_session;
386 uint32_t flags;
387 const char *workdir;
388 git_index *index;
389 git_vector *files;
390 } attr_walk_up_info;
391
392 static int attr_decide_sources(
393 uint32_t flags, bool has_wd, bool has_index, git_attr_file_source *srcs)
394 {
395 int count = 0;
396
397 switch (flags & 0x03) {
398 case GIT_ATTR_CHECK_FILE_THEN_INDEX:
399 if (has_wd)
400 srcs[count++] = GIT_ATTR_FILE__FROM_FILE;
401 if (has_index)
402 srcs[count++] = GIT_ATTR_FILE__FROM_INDEX;
403 break;
404 case GIT_ATTR_CHECK_INDEX_THEN_FILE:
405 if (has_index)
406 srcs[count++] = GIT_ATTR_FILE__FROM_INDEX;
407 if (has_wd)
408 srcs[count++] = GIT_ATTR_FILE__FROM_FILE;
409 break;
410 case GIT_ATTR_CHECK_INDEX_ONLY:
411 if (has_index)
412 srcs[count++] = GIT_ATTR_FILE__FROM_INDEX;
413 break;
414 }
415
416 return count;
417 }
418
419 static int push_attr_file(
420 git_repository *repo,
421 git_attr_session *attr_session,
422 git_vector *list,
423 git_attr_file_source source,
424 const char *base,
425 const char *filename)
426 {
427 int error = 0;
428 git_attr_file *file = NULL;
429
430 error = git_attr_cache__get(&file, repo, attr_session,
431 source, base, filename, git_attr_file__parse_buffer);
432
433 if (error < 0)
434 return error;
435
436 if (file != NULL) {
437 if ((error = git_vector_insert(list, file)) < 0)
438 git_attr_file__free(file);
439 }
440
441 return error;
442 }
443
444 static int push_one_attr(void *ref, const char *path)
445 {
446 int error = 0, n_src, i;
447 attr_walk_up_info *info = (attr_walk_up_info *)ref;
448 git_attr_file_source src[2];
449
450 n_src = attr_decide_sources(
451 info->flags, info->workdir != NULL, info->index != NULL, src);
452
453 for (i = 0; !error && i < n_src; ++i)
454 error = push_attr_file(info->repo, info->attr_session,
455 info->files, src[i], path, GIT_ATTR_FILE);
456
457 return error;
458 }
459
460 static void release_attr_files(git_vector *files)
461 {
462 size_t i;
463 git_attr_file *file;
464
465 git_vector_foreach(files, i, file) {
466 git_attr_file__free(file);
467 files->contents[i] = NULL;
468 }
469 git_vector_free(files);
470 }
471
472 static int collect_attr_files(
473 git_repository *repo,
474 git_attr_session *attr_session,
475 uint32_t flags,
476 const char *path,
477 git_vector *files)
478 {
479 int error = 0;
480 git_buf dir = GIT_BUF_INIT, attrfile = GIT_BUF_INIT;
481 const char *workdir = git_repository_workdir(repo);
482 attr_walk_up_info info = { NULL };
483
484 if ((error = attr_setup(repo, attr_session)) < 0)
485 return error;
486
487 /* Resolve path in a non-bare repo */
488 if (workdir != NULL)
489 error = git_path_find_dir(&dir, path, workdir);
490 else
491 error = git_path_dirname_r(&dir, path);
492 if (error < 0)
493 goto cleanup;
494
495 /* in precendence order highest to lowest:
496 * - $GIT_DIR/info/attributes
497 * - path components with .gitattributes
498 * - config core.attributesfile
499 * - $GIT_PREFIX/etc/gitattributes
500 */
501
502 error = git_repository_item_path(&attrfile, repo, GIT_REPOSITORY_ITEM_INFO);
503 if (error < 0)
504 goto cleanup;
505
506 error = push_attr_file(
507 repo, attr_session, files, GIT_ATTR_FILE__FROM_FILE,
508 attrfile.ptr, GIT_ATTR_FILE_INREPO);
509 if (error < 0)
510 goto cleanup;
511
512 info.repo = repo;
513 info.attr_session = attr_session;
514 info.flags = flags;
515 info.workdir = workdir;
516 if (git_repository_index__weakptr(&info.index, repo) < 0)
517 giterr_clear(); /* no error even if there is no index */
518 info.files = files;
519
520 if (!strcmp(dir.ptr, "."))
521 error = push_one_attr(&info, "");
522 else
523 error = git_path_walk_up(&dir, workdir, push_one_attr, &info);
524
525 if (error < 0)
526 goto cleanup;
527
528 if (git_repository_attr_cache(repo)->cfg_attr_file != NULL) {
529 error = push_attr_file(
530 repo, attr_session, files, GIT_ATTR_FILE__FROM_FILE,
531 NULL, git_repository_attr_cache(repo)->cfg_attr_file);
532 if (error < 0)
533 goto cleanup;
534 }
535
536 if ((flags & GIT_ATTR_CHECK_NO_SYSTEM) == 0) {
537 error = system_attr_file(&dir, attr_session);
538
539 if (!error)
540 error = push_attr_file(
541 repo, attr_session, files, GIT_ATTR_FILE__FROM_FILE,
542 NULL, dir.ptr);
543 else if (error == GIT_ENOTFOUND)
544 error = 0;
545 }
546
547 cleanup:
548 if (error < 0)
549 release_attr_files(files);
550 git_buf_free(&attrfile);
551 git_buf_free(&dir);
552
553 return error;
554 }