attr_file.h 5.8 KB
Newer Older
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
3 4 5 6 7 8 9
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */
#ifndef INCLUDE_attr_file_h__
#define INCLUDE_attr_file_h__

10 11
#include "common.h"

12
#include "git2/oid.h"
13 14
#include "git2/attr.h"
#include "vector.h"
15
#include "pool.h"
16
#include "buffer.h"
17
#include "futils.h"
18

Russell Belfer committed
19
#define GIT_ATTR_FILE			".gitattributes"
20
#define GIT_ATTR_FILE_INREPO	"attributes"
Russell Belfer committed
21
#define GIT_ATTR_FILE_SYSTEM	"gitattributes"
22
#define GIT_ATTR_FILE_XDG		"attributes"
Russell Belfer committed
23

24 25 26 27
#define GIT_ATTR_FNMATCH_NEGATIVE	(1U << 0)
#define GIT_ATTR_FNMATCH_DIRECTORY	(1U << 1)
#define GIT_ATTR_FNMATCH_FULLPATH	(1U << 2)
#define GIT_ATTR_FNMATCH_MACRO		(1U << 3)
28
#define GIT_ATTR_FNMATCH_IGNORE		(1U << 4)
29
#define GIT_ATTR_FNMATCH_HASWILD	(1U << 5)
30
#define GIT_ATTR_FNMATCH_ALLOWSPACE	(1U << 6)
31
#define GIT_ATTR_FNMATCH_ICASE		(1U << 7)
32
#define GIT_ATTR_FNMATCH_MATCH_ALL	(1U << 8)
33 34 35 36
#define GIT_ATTR_FNMATCH_ALLOWNEG   (1U << 9)
#define GIT_ATTR_FNMATCH_ALLOWMACRO (1U << 10)

#define GIT_ATTR_FNMATCH__INCOMING \
37
	(GIT_ATTR_FNMATCH_ALLOWSPACE | GIT_ATTR_FNMATCH_ALLOWNEG | GIT_ATTR_FNMATCH_ALLOWMACRO)
38

39
typedef enum {
40 41 42
	GIT_ATTR_FILE_SOURCE_MEMORY = 0,
	GIT_ATTR_FILE_SOURCE_FILE   = 1,
	GIT_ATTR_FILE_SOURCE_INDEX  = 2,
43
	GIT_ATTR_FILE_SOURCE_COMMIT = 3,
44

45 46
	GIT_ATTR_FILE_NUM_SOURCES   = 4
} git_attr_file_source_t;
47

48
typedef struct {
49
	/* The source location for the attribute file. */
50
	git_attr_file_source_t type;
51 52 53 54 55 56 57

	/*
	 * The filename of the attribute file to read (relative to the
	 * given base path).
	 */
	const char *base;
	const char *filename;
58 59 60 61 62 63

	/*
	 * The commit ID when the given source type is a commit (or NULL
	 * for the repository's HEAD commit.)
	 */
	git_oid *commit_id;
64 65
} git_attr_file_source;

66 67 68 69
extern const char *git_attr__true;
extern const char *git_attr__false;
extern const char *git_attr__unset;

70 71 72
typedef struct {
	char *pattern;
	size_t length;
73 74
	char *containing_dir;
	size_t containing_dir_length;
75
	unsigned int flags;
76 77
} git_attr_fnmatch;

78
typedef struct {
79 80
	git_attr_fnmatch match;
	git_vector assigns;		/* vector of <git_attr_assignment*> */
81
} git_attr_rule;
82 83

typedef struct {
84
	git_refcount unused;
85
	const char *name;
Linquize committed
86
	uint32_t name_hash;
87 88 89
} git_attr_name;

typedef struct {
90
	git_refcount rc;		/* for macros */
91
	char *name;
Linquize committed
92 93
	uint32_t name_hash;
	const char *value;
94 95
} git_attr_assignment;

96 97 98
typedef struct git_attr_file_entry git_attr_file_entry;

typedef struct {
99
	git_refcount rc;
100
	git_mutex lock;
101
	git_attr_file_entry *entry;
102
	git_attr_file_source source;
103 104
	git_vector rules;			/* vector of <rule*> or <fnmatch*> */
	git_pool pool;
105 106
	unsigned int nonexistent:1;
	int session_key;
107 108
	union {
		git_oid oid;
Vicent Marti committed
109
		git_futils_filestamp stamp;
110
	} cache_data;
111 112 113 114 115 116
} git_attr_file;

struct git_attr_file_entry {
	git_attr_file *file[GIT_ATTR_FILE_NUM_SOURCES];
	const char *path; /* points into fullpath */
	char fullpath[GIT_FLEX_ARRAY];
117
};
118 119

typedef struct {
120 121 122 123
	git_buf  full;
	char    *path;
	char    *basename;
	int      is_dir;
124 125
} git_attr_path;

126 127 128 129 130 131
/* A git_attr_session can provide an "instance" of reading, to prevent cache
 * invalidation during a single operation instance (like checkout).
 */

typedef struct {
	int key;
132 133 134
	unsigned int init_setup:1,
		init_sysdir:1;
	git_buf sysdir;
135
	git_buf tmp;
136 137 138 139 140 141 142 143 144
} git_attr_session;

extern int git_attr_session__init(git_attr_session *attr_session, git_repository *repo);
extern void git_attr_session__free(git_attr_session *session);

extern int git_attr_get_many_with_session(
	const char **values_out,
	git_repository *repo,
	git_attr_session *attr_session,
145
	git_attr_options *opts,
146 147 148 149 150 151 152
	const char *path,
	size_t num_attr,
	const char **names);

typedef int (*git_attr_file_parser)(
	git_repository *repo,
	git_attr_file *file,
153 154
	const char *data,
	bool allow_macros);
155

156 157 158 159
/*
 * git_attr_file API
 */

160 161
int git_attr_file__new(
	git_attr_file **out,
162
	git_attr_file_entry *entry,
163
	git_attr_file_source *source);
164 165 166 167 168 169

void git_attr_file__free(git_attr_file *file);

int git_attr_file__load(
	git_attr_file **out,
	git_repository *repo,
170
	git_attr_session *attr_session,
171
	git_attr_file_entry *ce,
172
	git_attr_file_source *source,
173 174
	git_attr_file_parser parser,
	bool allow_macros);
175

176
int git_attr_file__load_standalone(
177
	git_attr_file **out, const char *path);
178

179
int git_attr_file__out_of_date(
180
	git_repository *repo, git_attr_session *session, git_attr_file *file, git_attr_file_source *source);
181

182
int git_attr_file__parse_buffer(
183
	git_repository *repo, git_attr_file *attrs, const char *data, bool allow_macros);
184

185 186
int git_attr_file__clear_rules(
	git_attr_file *file, bool need_lock);
187

188
int git_attr_file__lookup_one(
189
	git_attr_file *file,
190
	git_attr_path *path,
191 192 193 194 195 196
	const char *attr,
	const char **value);

/* loop over rules in file from bottom to top */
#define git_attr_file__foreach_matching_rule(file, path, iter, rule)	\
	git_vector_rforeach(&(file)->rules, (iter), (rule)) \
197
		if (git_attr_rule__match((rule), (path)))
198

199
uint32_t git_attr_file__name_hash(const char *name);
200 201 202 203 204 205


/*
 * other utilities
 */

206
extern int git_attr_fnmatch__parse(
207
	git_attr_fnmatch *spec,
208
	git_pool *pool,
209
	const char *source,
210 211
	const char **base);

212
extern bool git_attr_fnmatch__match(
213
	git_attr_fnmatch *rule,
214
	git_attr_path *path);
215

216 217
extern void git_attr_rule__free(git_attr_rule *rule);

218
extern bool git_attr_rule__match(
219
	git_attr_rule *rule,
220
	git_attr_path *path);
221 222 223 224

extern git_attr_assignment *git_attr_rule__lookup_assignment(
	git_attr_rule *rule, const char *name);

225 226
typedef enum { GIT_DIR_FLAG_TRUE = 1, GIT_DIR_FLAG_FALSE = 0, GIT_DIR_FLAG_UNKNOWN = -1 } git_dir_flag;

227
extern int git_attr_path__init(
228 229 230 231 232
	git_attr_path *out,
	git_repository *repo,
	const char *path,
	const char *base,
	git_dir_flag is_dir);
233 234
extern void git_attr_path__free(git_attr_path *info);

235 236
extern int git_attr_assignment__parse(
	git_repository *repo, /* needed to expand macros */
237
	git_pool *pool,
238 239 240
	git_vector *assigns,
	const char **scan);

241
#endif