mirror of
https://github.com/git/git.git
synced 2026-01-11 13:23:12 +09:00
A common pattern to check N attributes for many paths is to (1) prepare an array A of N attr_check_item items; (2) call git_attr() to intern the N attribute names and fill A; (3) repeatedly call git_check_attrs() for path with N and A; A look-up for these N attributes for a single path P scans the entire attr_stack, starting from the .git/info/attributes file and then .gitattributes file in the directory the path P is in, going upwards to find .gitattributes file found in parent directories. An earlier commit 06a604e6 (attr: avoid heavy work when we know the specified attr is not defined, 2014-12-28) tried to optimize out this scanning for one trivial special case: when the attribute being sought is known not to exist, we do not have to scan for it. While this may be a cheap and effective heuristic, it would not work well when N is (much) more than 1. What we would want is a more customized way to skip irrelevant entries in the attribute stack, and the definition of irrelevance is tied to the set of attributes passed to git_check_attrs() call, i.e. the set of attributes being sought. The data necessary for this optimization needs to live alongside the set of attributes, but a simple array of git_attr_check_elem simply does not have any place for that. Introduce "struct attr_check" that contains N, the number of attributes being sought, and A, the array that holds N attr_check_item items, and a function git_check_attr() that takes a path P and this structure as its parameters. This structure can later be extended to hold extra data necessary for optimization. Also, to make it easier to write the first two steps in common cases, introduce git_attr_check_initl() helper function, which takes a NULL-terminated list of attribute names and initialize this structure. Signed-off-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Stefan Beller <sbeller@google.com> Signed-off-by: Brandon Williams <bmwill@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
75 lines
2.1 KiB
C
75 lines
2.1 KiB
C
#ifndef ATTR_H
|
|
#define ATTR_H
|
|
|
|
/* An attribute is a pointer to this opaque structure */
|
|
struct git_attr;
|
|
|
|
/*
|
|
* Given a string, return the gitattribute object that
|
|
* corresponds to it.
|
|
*/
|
|
struct git_attr *git_attr(const char *);
|
|
|
|
/* Internal use */
|
|
extern const char git_attr__true[];
|
|
extern const char git_attr__false[];
|
|
|
|
/* For public to check git_attr_check results */
|
|
#define ATTR_TRUE(v) ((v) == git_attr__true)
|
|
#define ATTR_FALSE(v) ((v) == git_attr__false)
|
|
#define ATTR_UNSET(v) ((v) == NULL)
|
|
|
|
/*
|
|
* Send one or more git_attr_check to git_check_attrs(), and
|
|
* each 'value' member tells what its value is.
|
|
* Unset one is returned as NULL.
|
|
*/
|
|
struct attr_check_item {
|
|
const struct git_attr *attr;
|
|
const char *value;
|
|
};
|
|
|
|
struct attr_check {
|
|
int nr;
|
|
int alloc;
|
|
struct attr_check_item *items;
|
|
};
|
|
|
|
extern struct attr_check *attr_check_alloc(void);
|
|
extern struct attr_check *attr_check_initl(const char *, ...);
|
|
|
|
extern struct attr_check_item *attr_check_append(struct attr_check *check,
|
|
const struct git_attr *attr);
|
|
|
|
extern void attr_check_reset(struct attr_check *check);
|
|
extern void attr_check_clear(struct attr_check *check);
|
|
extern void attr_check_free(struct attr_check *check);
|
|
|
|
/*
|
|
* Return the name of the attribute represented by the argument. The
|
|
* return value is a pointer to a null-delimited string that is part
|
|
* of the internal data structure; it should not be modified or freed.
|
|
*/
|
|
extern const char *git_attr_name(const struct git_attr *);
|
|
|
|
int git_check_attrs(const char *path, int, struct attr_check_item *);
|
|
extern int git_check_attr(const char *path, struct attr_check *check);
|
|
|
|
/*
|
|
* Retrieve all attributes that apply to the specified path. *num
|
|
* will be set to the number of attributes on the path; **check will
|
|
* be set to point at a newly-allocated array of git_attr_check
|
|
* objects describing the attributes and their values. *check must be
|
|
* free()ed by the caller.
|
|
*/
|
|
int git_all_attrs(const char *path, int *num, struct attr_check_item **check);
|
|
|
|
enum git_attr_direction {
|
|
GIT_ATTR_CHECKIN,
|
|
GIT_ATTR_CHECKOUT,
|
|
GIT_ATTR_INDEX
|
|
};
|
|
void git_attr_set_direction(enum git_attr_direction, struct index_state *);
|
|
|
|
#endif /* ATTR_H */
|