attr_file.h 3.09 KB
Newer Older
1
/*
schu committed
2
 * Copyright (C) 2009-2012 the libgit2 contributors
3 4 5 6 7 8 9 10 11
 *
 * 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__

#include "git2/attr.h"
#include "vector.h"
12
#include "pool.h"
13
#include "buffer.h"
14

Russell Belfer committed
15 16 17 18
#define GIT_ATTR_FILE			".gitattributes"
#define GIT_ATTR_FILE_INREPO	"info/attributes"
#define GIT_ATTR_FILE_SYSTEM	"gitattributes"

19 20 21 22
#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)
23
#define GIT_ATTR_FNMATCH_IGNORE		(1U << 4)
24
#define GIT_ATTR_FNMATCH_HASWILD	(1U << 5)
25 26 27 28

typedef struct {
	char *pattern;
	size_t length;
29
	unsigned int flags;
30 31 32
} git_attr_fnmatch;

typedef struct {
33 34 35 36 37
	git_attr_fnmatch match;
	git_vector assigns;		/* vector of <git_attr_assignment*> */
} git_attr_rule;

typedef struct {
38
	git_refcount unused;
39
	const char *name;
40
    uint32_t name_hash;
41 42 43
} git_attr_name;

typedef struct {
44
	git_refcount rc;		/* for macros */
45
	char *name;
46
    uint32_t name_hash;
47 48 49 50
    const char *value;
} git_attr_assignment;

typedef struct {
51 52
	char *path;				/* cache the path this was loaded from */
	git_vector rules;		/* vector of <rule*> or <fnmatch*> */
53 54
	git_pool *pool;
	bool pool_is_allocated;
55 56 57
} git_attr_file;

typedef struct {
58
	git_buf     full;
59 60
	const char *path;
	const char *basename;
61
	int         is_dir;
62 63 64 65 66 67
} git_attr_path;

/*
 * git_attr_file API
 */

68
extern int git_attr_file__new(git_attr_file **attrs_ptr, git_pool *pool);
69 70
extern void git_attr_file__free(git_attr_file *file);

71
extern int git_attr_file__from_buffer(
72
	git_repository *repo, const char *buf, git_attr_file *file);
73
extern int git_attr_file__from_file(
74
	git_repository *repo, const char *path, git_attr_file *file);
75

76 77
extern int git_attr_file__set_path(
	git_repository *repo, const char *path, git_attr_file *file);
78 79 80 81 82 83 84 85 86 87

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)) \
88
		if (git_attr_rule__match((rule), (path)))
89

90
extern uint32_t git_attr_file__name_hash(const char *name);
91 92 93 94 95 96


/*
 * other utilities
 */

97 98
extern int git_attr_fnmatch__parse(
	git_attr_fnmatch *spec,
99
	git_pool *pool,
100
	const char *source,
101 102
	const char **base);

103
extern bool git_attr_fnmatch__match(
104 105 106
	git_attr_fnmatch *rule,
	const git_attr_path *path);

107 108
extern void git_attr_rule__free(git_attr_rule *rule);

109
extern bool git_attr_rule__match(
110 111 112 113 114 115 116
	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(
117
	git_attr_path *info, const char *path, const char *base);
118

119 120
extern void git_attr_path__free(git_attr_path *info);

121 122
extern int git_attr_assignment__parse(
	git_repository *repo, /* needed to expand macros */
123
	git_pool *pool,
124 125 126
	git_vector *assigns,
	const char **scan);

127
#endif