]> git.proxmox.com Git - libgit2.git/blob - src/attr_file.h
Revert PR #1462 and provide alternative fix
[libgit2.git] / src / attr_file.h
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 #ifndef INCLUDE_attr_file_h__
8 #define INCLUDE_attr_file_h__
9
10 #include "git2/oid.h"
11 #include "git2/attr.h"
12 #include "vector.h"
13 #include "pool.h"
14 #include "buffer.h"
15 #include "fileops.h"
16
17 #define GIT_ATTR_FILE ".gitattributes"
18 #define GIT_ATTR_FILE_INREPO "info/attributes"
19 #define GIT_ATTR_FILE_SYSTEM "gitattributes"
20 #define GIT_ATTR_FILE_XDG "attributes"
21
22 #define GIT_ATTR_FNMATCH_NEGATIVE (1U << 0)
23 #define GIT_ATTR_FNMATCH_DIRECTORY (1U << 1)
24 #define GIT_ATTR_FNMATCH_FULLPATH (1U << 2)
25 #define GIT_ATTR_FNMATCH_MACRO (1U << 3)
26 #define GIT_ATTR_FNMATCH_IGNORE (1U << 4)
27 #define GIT_ATTR_FNMATCH_HASWILD (1U << 5)
28 #define GIT_ATTR_FNMATCH_ALLOWSPACE (1U << 6)
29 #define GIT_ATTR_FNMATCH_ICASE (1U << 7)
30 #define GIT_ATTR_FNMATCH_MATCH_ALL (1U << 8)
31 #define GIT_ATTR_FNMATCH_ALLOWNEG (1U << 9)
32 #define GIT_ATTR_FNMATCH_ALLOWMACRO (1U << 10)
33
34 #define GIT_ATTR_FNMATCH__INCOMING \
35 (GIT_ATTR_FNMATCH_ALLOWSPACE | \
36 GIT_ATTR_FNMATCH_ALLOWNEG | GIT_ATTR_FNMATCH_ALLOWMACRO)
37
38 extern const char *git_attr__true;
39 extern const char *git_attr__false;
40 extern const char *git_attr__unset;
41
42 typedef struct {
43 char *pattern;
44 size_t length;
45 unsigned int flags;
46 } git_attr_fnmatch;
47
48 typedef struct {
49 git_attr_fnmatch match;
50 git_vector assigns; /* vector of <git_attr_assignment*> */
51 } git_attr_rule;
52
53 typedef struct {
54 git_refcount unused;
55 const char *name;
56 uint32_t name_hash;
57 } git_attr_name;
58
59 typedef struct {
60 git_refcount rc; /* for macros */
61 char *name;
62 uint32_t name_hash;
63 const char *value;
64 } git_attr_assignment;
65
66 typedef struct {
67 char *key; /* cache "source#path" this was loaded from */
68 git_vector rules; /* vector of <rule*> or <fnmatch*> */
69 git_pool *pool;
70 bool pool_is_allocated;
71 union {
72 git_oid oid;
73 git_futils_filestamp stamp;
74 } cache_data;
75 } git_attr_file;
76
77 typedef struct {
78 git_buf full;
79 char *path;
80 char *basename;
81 int is_dir;
82 } git_attr_path;
83
84 typedef enum {
85 GIT_ATTR_FILE_FROM_FILE = 0,
86 GIT_ATTR_FILE_FROM_INDEX = 1
87 } git_attr_file_source;
88
89 /*
90 * git_attr_file API
91 */
92
93 extern int git_attr_file__new(
94 git_attr_file **attrs_ptr, git_attr_file_source src, const char *path, git_pool *pool);
95
96 extern int git_attr_file__new_and_load(
97 git_attr_file **attrs_ptr, const char *path);
98
99 extern void git_attr_file__free(git_attr_file *file);
100
101 extern void git_attr_file__clear_rules(git_attr_file *file);
102
103 extern int git_attr_file__parse_buffer(
104 git_repository *repo, void *parsedata, const char *buf, git_attr_file *file);
105
106 extern int git_attr_file__lookup_one(
107 git_attr_file *file,
108 const git_attr_path *path,
109 const char *attr,
110 const char **value);
111
112 /* loop over rules in file from bottom to top */
113 #define git_attr_file__foreach_matching_rule(file, path, iter, rule) \
114 git_vector_rforeach(&(file)->rules, (iter), (rule)) \
115 if (git_attr_rule__match((rule), (path)))
116
117 extern uint32_t git_attr_file__name_hash(const char *name);
118
119
120 /*
121 * other utilities
122 */
123
124 extern int git_attr_fnmatch__parse(
125 git_attr_fnmatch *spec,
126 git_pool *pool,
127 const char *source,
128 const char **base);
129
130 extern bool git_attr_fnmatch__match(
131 git_attr_fnmatch *rule,
132 const git_attr_path *path);
133
134 extern void git_attr_rule__free(git_attr_rule *rule);
135
136 extern bool git_attr_rule__match(
137 git_attr_rule *rule,
138 const git_attr_path *path);
139
140 extern git_attr_assignment *git_attr_rule__lookup_assignment(
141 git_attr_rule *rule, const char *name);
142
143 extern int git_attr_path__init(
144 git_attr_path *info, const char *path, const char *base);
145
146 extern void git_attr_path__free(git_attr_path *info);
147
148 extern int git_attr_assignment__parse(
149 git_repository *repo, /* needed to expand macros */
150 git_pool *pool,
151 git_vector *assigns,
152 const char **scan);
153
154 #endif