attr_file.h 3.58 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
#include "git2/oid.h"
11 12
#include "git2/attr.h"
#include "vector.h"
13
#include "pool.h"
14
#include "buffer.h"
15
#include "fileops.h"
16

Russell Belfer committed
17 18 19
#define GIT_ATTR_FILE			".gitattributes"
#define GIT_ATTR_FILE_INREPO	"info/attributes"
#define GIT_ATTR_FILE_SYSTEM	"gitattributes"
20
#define GIT_ATTR_FILE_XDG		"attributes"
Russell Belfer committed
21

22 23 24 25
#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)
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

32 33 34 35
extern const char *git_attr__true;
extern const char *git_attr__false;
extern const char *git_attr__unset;

36 37 38
typedef struct {
	char *pattern;
	size_t length;
39
	unsigned int flags;
40 41 42
} git_attr_fnmatch;

typedef struct {
43 44 45 46 47
	git_attr_fnmatch match;
	git_vector assigns;		/* vector of <git_attr_assignment*> */
} git_attr_rule;

typedef struct {
48
	git_refcount unused;
49
	const char *name;
Linquize committed
50
	uint32_t name_hash;
51 52 53
} git_attr_name;

typedef struct {
54
	git_refcount rc;		/* for macros */
55
	char *name;
Linquize committed
56 57
	uint32_t name_hash;
	const char *value;
58 59 60
} git_attr_assignment;

typedef struct {
61
	char *key;				/* cache "source#path" this was loaded from */
62
	git_vector rules;		/* vector of <rule*> or <fnmatch*> */
63 64
	git_pool *pool;
	bool pool_is_allocated;
65 66
	union {
		git_oid oid;
Vicent Marti committed
67
		git_futils_filestamp stamp;
68
	} cache_data;
69 70 71
} git_attr_file;

typedef struct {
72 73 74 75
	git_buf  full;
	char    *path;
	char    *basename;
	int      is_dir;
76 77
} git_attr_path;

78 79 80 81 82
typedef enum {
	GIT_ATTR_FILE_FROM_FILE = 0,
	GIT_ATTR_FILE_FROM_INDEX = 1
} git_attr_file_source;

83 84 85 86
/*
 * git_attr_file API
 */

87 88 89 90 91 92
extern int git_attr_file__new(
	git_attr_file **attrs_ptr, git_attr_file_source src, const char *path, git_pool *pool);

extern int git_attr_file__new_and_load(
	git_attr_file **attrs_ptr, const char *path);

93 94
extern void git_attr_file__free(git_attr_file *file);

95 96
extern void git_attr_file__clear_rules(git_attr_file *file);

97
extern int git_attr_file__parse_buffer(
98
	git_repository *repo, void *parsedata, const char *buf, git_attr_file *file);
99 100 101 102 103 104 105 106 107 108

extern int git_attr_file__lookup_one(
	git_attr_file *file,
	const git_attr_path *path,
	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)) \
109
		if (git_attr_rule__match((rule), (path)))
110

111
extern uint32_t git_attr_file__name_hash(const char *name);
112 113 114 115 116 117


/*
 * other utilities
 */

118 119
extern int git_attr_fnmatch__parse(
	git_attr_fnmatch *spec,
120
	git_pool *pool,
121
	const char *source,
122 123
	const char **base);

124
extern bool git_attr_fnmatch__match(
125 126 127
	git_attr_fnmatch *rule,
	const git_attr_path *path);

128 129
extern void git_attr_rule__free(git_attr_rule *rule);

130
extern bool git_attr_rule__match(
131 132 133 134 135 136 137
	git_attr_rule *rule,
	const git_attr_path *path);

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

extern int git_attr_path__init(
138
	git_attr_path *info, const char *path, const char *base);
139

140 141
extern void git_attr_path__free(git_attr_path *info);

142 143
extern int git_attr_assignment__parse(
	git_repository *repo, /* needed to expand macros */
144
	git_pool *pool,
145 146 147
	git_vector *assigns,
	const char **scan);

148
#endif