]> git.proxmox.com Git - libgit2.git/blob - src/attr_file.h
libgit2 v0.21.0
[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 #define GIT_ATTR_FNMATCH_LEADINGDIR (1U << 11)
34 #define GIT_ATTR_FNMATCH_NOLEADINGDIR (1U << 12)
35
36 #define GIT_ATTR_FNMATCH__INCOMING \
37 (GIT_ATTR_FNMATCH_ALLOWSPACE | GIT_ATTR_FNMATCH_ALLOWNEG | \
38 GIT_ATTR_FNMATCH_ALLOWMACRO | GIT_ATTR_FNMATCH_NOLEADINGDIR)
39
40 typedef enum {
41 GIT_ATTR_FILE__IN_MEMORY = 0,
42 GIT_ATTR_FILE__FROM_FILE = 1,
43 GIT_ATTR_FILE__FROM_INDEX = 2,
44
45 GIT_ATTR_FILE_NUM_SOURCES = 3
46 } git_attr_file_source;
47
48 extern const char *git_attr__true;
49 extern const char *git_attr__false;
50 extern const char *git_attr__unset;
51
52 typedef struct {
53 char *pattern;
54 size_t length;
55 unsigned int flags;
56 } git_attr_fnmatch;
57
58 typedef struct {
59 git_attr_fnmatch match;
60 git_vector assigns; /* vector of <git_attr_assignment*> */
61 } git_attr_rule;
62
63 typedef struct {
64 git_refcount unused;
65 const char *name;
66 uint32_t name_hash;
67 } git_attr_name;
68
69 typedef struct {
70 git_refcount rc; /* for macros */
71 char *name;
72 uint32_t name_hash;
73 const char *value;
74 } git_attr_assignment;
75
76 typedef struct git_attr_file_entry git_attr_file_entry;
77
78 typedef struct {
79 git_refcount rc;
80 git_mutex lock;
81 git_attr_file_entry *entry;
82 git_attr_file_source source;
83 git_vector rules; /* vector of <rule*> or <fnmatch*> */
84 git_pool pool;
85 union {
86 git_oid oid;
87 git_futils_filestamp stamp;
88 } cache_data;
89 } git_attr_file;
90
91 struct git_attr_file_entry {
92 git_attr_file *file[GIT_ATTR_FILE_NUM_SOURCES];
93 const char *path; /* points into fullpath */
94 char fullpath[GIT_FLEX_ARRAY];
95 };
96
97 typedef int (*git_attr_file_parser)(
98 git_repository *repo,
99 git_attr_file *file,
100 const char *data);
101
102 typedef struct {
103 git_buf full;
104 char *path;
105 char *basename;
106 int is_dir;
107 } git_attr_path;
108
109 /*
110 * git_attr_file API
111 */
112
113 int git_attr_file__new(
114 git_attr_file **out,
115 git_attr_file_entry *entry,
116 git_attr_file_source source);
117
118 void git_attr_file__free(git_attr_file *file);
119
120 int git_attr_file__load(
121 git_attr_file **out,
122 git_repository *repo,
123 git_attr_file_entry *ce,
124 git_attr_file_source source,
125 git_attr_file_parser parser);
126
127 int git_attr_file__load_standalone(
128 git_attr_file **out, const char *path);
129
130 int git_attr_file__out_of_date(
131 git_repository *repo, git_attr_file *file);
132
133 int git_attr_file__parse_buffer(
134 git_repository *repo, git_attr_file *attrs, const char *data);
135
136 int git_attr_file__clear_rules(
137 git_attr_file *file, bool need_lock);
138
139 int git_attr_file__lookup_one(
140 git_attr_file *file,
141 git_attr_path *path,
142 const char *attr,
143 const char **value);
144
145 /* loop over rules in file from bottom to top */
146 #define git_attr_file__foreach_matching_rule(file, path, iter, rule) \
147 git_vector_rforeach(&(file)->rules, (iter), (rule)) \
148 if (git_attr_rule__match((rule), (path)))
149
150 uint32_t git_attr_file__name_hash(const char *name);
151
152
153 /*
154 * other utilities
155 */
156
157 extern int git_attr_fnmatch__parse(
158 git_attr_fnmatch *spec,
159 git_pool *pool,
160 const char *source,
161 const char **base);
162
163 extern bool git_attr_fnmatch__match(
164 git_attr_fnmatch *rule,
165 git_attr_path *path);
166
167 extern void git_attr_rule__free(git_attr_rule *rule);
168
169 extern bool git_attr_rule__match(
170 git_attr_rule *rule,
171 git_attr_path *path);
172
173 extern git_attr_assignment *git_attr_rule__lookup_assignment(
174 git_attr_rule *rule, const char *name);
175
176 extern int git_attr_path__init(
177 git_attr_path *info, const char *path, const char *base);
178
179 extern void git_attr_path__free(git_attr_path *info);
180
181 extern int git_attr_assignment__parse(
182 git_repository *repo, /* needed to expand macros */
183 git_pool *pool,
184 git_vector *assigns,
185 const char **scan);
186
187 #endif