attr_file.h 5.38 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 "fileops.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
#define GIT_ATTR_FNMATCH_ALLOWNEG   (1U << 9)
#define GIT_ATTR_FNMATCH_ALLOWMACRO (1U << 10)
35
#define GIT_ATTR_FNMATCH_LEADINGDIR (1U << 11)
Russell Belfer committed
36
#define GIT_ATTR_FNMATCH_NOLEADINGDIR (1U << 12)
37 38

#define GIT_ATTR_FNMATCH__INCOMING \
Russell Belfer committed
39 40
	(GIT_ATTR_FNMATCH_ALLOWSPACE | GIT_ATTR_FNMATCH_ALLOWNEG | \
	 GIT_ATTR_FNMATCH_ALLOWMACRO | GIT_ATTR_FNMATCH_NOLEADINGDIR)
41

42
typedef enum {
43 44 45
	GIT_ATTR_FILE__IN_MEMORY   = 0,
	GIT_ATTR_FILE__FROM_FILE   = 1,
	GIT_ATTR_FILE__FROM_INDEX  = 2,
46

47
	GIT_ATTR_FILE_NUM_SOURCES  = 3
48 49
} git_attr_file_source;

50 51 52 53
extern const char *git_attr__true;
extern const char *git_attr__false;
extern const char *git_attr__unset;

54 55 56
typedef struct {
	char *pattern;
	size_t length;
57 58
	char *containing_dir;
	size_t containing_dir_length;
59
	unsigned int flags;
60 61
} git_attr_fnmatch;

62
typedef struct {
63 64
	git_attr_fnmatch match;
	git_vector assigns;		/* vector of <git_attr_assignment*> */
65
} git_attr_rule;
66 67

typedef struct {
68
	git_refcount unused;
69
	const char *name;
Linquize committed
70
	uint32_t name_hash;
71 72 73
} git_attr_name;

typedef struct {
74
	git_refcount rc;		/* for macros */
75
	char *name;
Linquize committed
76 77
	uint32_t name_hash;
	const char *value;
78 79
} git_attr_assignment;

80 81 82
typedef struct git_attr_file_entry git_attr_file_entry;

typedef struct {
83
	git_refcount rc;
84
	git_mutex lock;
85 86
	git_attr_file_entry *entry;
	git_attr_file_source source;
87 88
	git_vector rules;			/* vector of <rule*> or <fnmatch*> */
	git_pool pool;
89 90
	unsigned int nonexistent:1;
	int session_key;
91 92
	union {
		git_oid oid;
Vicent Marti committed
93
		git_futils_filestamp stamp;
94
	} cache_data;
95 96 97 98 99 100
} 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];
101
};
102 103

typedef struct {
104 105 106 107
	git_buf  full;
	char    *path;
	char    *basename;
	int      is_dir;
108 109
} git_attr_path;

110 111 112 113 114 115
/* 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;
116 117 118
	unsigned int init_setup:1,
		init_sysdir:1;
	git_buf sysdir;
119
	git_buf tmp;
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
} 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,
	uint32_t flags,
	const char *path,
	size_t num_attr,
	const char **names);

typedef int (*git_attr_file_parser)(
	git_repository *repo,
	git_attr_file *file,
	const char *data);

139 140 141 142
/*
 * git_attr_file API
 */

143 144
int git_attr_file__new(
	git_attr_file **out,
145 146
	git_attr_file_entry *entry,
	git_attr_file_source source);
147 148 149 150 151 152

void git_attr_file__free(git_attr_file *file);

int git_attr_file__load(
	git_attr_file **out,
	git_repository *repo,
153
	git_attr_session *attr_session,
154 155 156
	git_attr_file_entry *ce,
	git_attr_file_source source,
	git_attr_file_parser parser);
157

158
int git_attr_file__load_standalone(
159
	git_attr_file **out, const char *path);
160

161
int git_attr_file__out_of_date(
162
	git_repository *repo, git_attr_session *session, git_attr_file *file);
163

164
int git_attr_file__parse_buffer(
165
	git_repository *repo, git_attr_file *attrs, const char *data);
166

167 168
int git_attr_file__clear_rules(
	git_attr_file *file, bool need_lock);
169

170
int git_attr_file__lookup_one(
171
	git_attr_file *file,
172
	git_attr_path *path,
173 174 175 176 177 178
	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)) \
179
		if (git_attr_rule__match((rule), (path)))
180

181
uint32_t git_attr_file__name_hash(const char *name);
182 183 184 185 186 187


/*
 * other utilities
 */

188
extern int git_attr_fnmatch__parse(
189
	git_attr_fnmatch *spec,
190
	git_pool *pool,
191
	const char *source,
192 193
	const char **base);

194
extern bool git_attr_fnmatch__match(
195
	git_attr_fnmatch *rule,
196
	git_attr_path *path);
197

198 199
extern void git_attr_rule__free(git_attr_rule *rule);

200
extern bool git_attr_rule__match(
201
	git_attr_rule *rule,
202
	git_attr_path *path);
203 204 205 206

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

207 208
typedef enum { GIT_DIR_FLAG_TRUE = 1, GIT_DIR_FLAG_FALSE = 0, GIT_DIR_FLAG_UNKNOWN = -1 } git_dir_flag;

209
extern int git_attr_path__init(
210
	git_attr_path *info, const char *path, const char *base, git_dir_flag is_dir);
211

212 213
extern void git_attr_path__free(git_attr_path *info);

214 215
extern int git_attr_assignment__parse(
	git_repository *repo, /* needed to expand macros */
216
	git_pool *pool,
217 218 219
	git_vector *assigns,
	const char **scan);

220
#endif