|
|
@ -24,7 +24,7 @@ static const char git_attr__unknown[] = "(builtin)unknown"; |
|
|
|
#define ATTR__UNKNOWN git_attr__unknown |
|
|
|
#define ATTR__UNKNOWN git_attr__unknown |
|
|
|
|
|
|
|
|
|
|
|
struct git_attr { |
|
|
|
struct git_attr { |
|
|
|
int attr_nr; /* unique attribute number */ |
|
|
|
unsigned int attr_nr; /* unique attribute number */ |
|
|
|
char name[FLEX_ARRAY]; /* attribute name */ |
|
|
|
char name[FLEX_ARRAY]; /* attribute name */ |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
@ -206,7 +206,7 @@ static void report_invalid_attr(const char *name, size_t len, |
|
|
|
* dictionary. If no entry is found, create a new attribute and store it in |
|
|
|
* dictionary. If no entry is found, create a new attribute and store it in |
|
|
|
* the dictionary. |
|
|
|
* the dictionary. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
static const struct git_attr *git_attr_internal(const char *name, int namelen) |
|
|
|
static const struct git_attr *git_attr_internal(const char *name, size_t namelen) |
|
|
|
{ |
|
|
|
{ |
|
|
|
struct git_attr *a; |
|
|
|
struct git_attr *a; |
|
|
|
|
|
|
|
|
|
|
@ -222,8 +222,8 @@ static const struct git_attr *git_attr_internal(const char *name, int namelen) |
|
|
|
a->attr_nr = hashmap_get_size(&g_attr_hashmap.map); |
|
|
|
a->attr_nr = hashmap_get_size(&g_attr_hashmap.map); |
|
|
|
|
|
|
|
|
|
|
|
attr_hashmap_add(&g_attr_hashmap, a->name, namelen, a); |
|
|
|
attr_hashmap_add(&g_attr_hashmap, a->name, namelen, a); |
|
|
|
assert(a->attr_nr == |
|
|
|
if (a->attr_nr != hashmap_get_size(&g_attr_hashmap.map) - 1) |
|
|
|
(hashmap_get_size(&g_attr_hashmap.map) - 1)); |
|
|
|
die(_("unable to add additional attribute")); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
hashmap_unlock(&g_attr_hashmap); |
|
|
|
hashmap_unlock(&g_attr_hashmap); |
|
|
@ -268,7 +268,7 @@ struct match_attr { |
|
|
|
const struct git_attr *attr; |
|
|
|
const struct git_attr *attr; |
|
|
|
} u; |
|
|
|
} u; |
|
|
|
char is_macro; |
|
|
|
char is_macro; |
|
|
|
unsigned num_attr; |
|
|
|
size_t num_attr; |
|
|
|
struct attr_state state[FLEX_ARRAY]; |
|
|
|
struct attr_state state[FLEX_ARRAY]; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
@ -289,7 +289,7 @@ static const char *parse_attr(const char *src, int lineno, const char *cp, |
|
|
|
struct attr_state *e) |
|
|
|
struct attr_state *e) |
|
|
|
{ |
|
|
|
{ |
|
|
|
const char *ep, *equals; |
|
|
|
const char *ep, *equals; |
|
|
|
int len; |
|
|
|
size_t len; |
|
|
|
|
|
|
|
|
|
|
|
ep = cp + strcspn(cp, blank); |
|
|
|
ep = cp + strcspn(cp, blank); |
|
|
|
equals = strchr(cp, '='); |
|
|
|
equals = strchr(cp, '='); |
|
|
@ -333,8 +333,7 @@ static const char *parse_attr(const char *src, int lineno, const char *cp, |
|
|
|
static struct match_attr *parse_attr_line(const char *line, const char *src, |
|
|
|
static struct match_attr *parse_attr_line(const char *line, const char *src, |
|
|
|
int lineno, unsigned flags) |
|
|
|
int lineno, unsigned flags) |
|
|
|
{ |
|
|
|
{ |
|
|
|
int namelen; |
|
|
|
size_t namelen, num_attr, i; |
|
|
|
int num_attr, i; |
|
|
|
|
|
|
|
const char *cp, *name, *states; |
|
|
|
const char *cp, *name, *states; |
|
|
|
struct match_attr *res = NULL; |
|
|
|
struct match_attr *res = NULL; |
|
|
|
int is_macro; |
|
|
|
int is_macro; |
|
|
@ -345,6 +344,11 @@ static struct match_attr *parse_attr_line(const char *line, const char *src, |
|
|
|
return NULL; |
|
|
|
return NULL; |
|
|
|
name = cp; |
|
|
|
name = cp; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (strlen(line) >= ATTR_MAX_LINE_LENGTH) { |
|
|
|
|
|
|
|
warning(_("ignoring overly long attributes line %d"), lineno); |
|
|
|
|
|
|
|
return NULL; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (*cp == '"' && !unquote_c_style(&pattern, name, &states)) { |
|
|
|
if (*cp == '"' && !unquote_c_style(&pattern, name, &states)) { |
|
|
|
name = pattern.buf; |
|
|
|
name = pattern.buf; |
|
|
|
namelen = pattern.len; |
|
|
|
namelen = pattern.len; |
|
|
@ -381,10 +385,9 @@ static struct match_attr *parse_attr_line(const char *line, const char *src, |
|
|
|
goto fail_return; |
|
|
|
goto fail_return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
res = xcalloc(1, |
|
|
|
res = xcalloc(1, st_add3(sizeof(*res), |
|
|
|
sizeof(*res) + |
|
|
|
st_mult(sizeof(struct attr_state), num_attr), |
|
|
|
sizeof(struct attr_state) * num_attr + |
|
|
|
is_macro ? 0 : namelen + 1)); |
|
|
|
(is_macro ? 0 : namelen + 1)); |
|
|
|
|
|
|
|
if (is_macro) { |
|
|
|
if (is_macro) { |
|
|
|
res->u.attr = git_attr_internal(name, namelen); |
|
|
|
res->u.attr = git_attr_internal(name, namelen); |
|
|
|
} else { |
|
|
|
} else { |
|
|
@ -447,11 +450,12 @@ struct attr_stack { |
|
|
|
|
|
|
|
|
|
|
|
static void attr_stack_free(struct attr_stack *e) |
|
|
|
static void attr_stack_free(struct attr_stack *e) |
|
|
|
{ |
|
|
|
{ |
|
|
|
int i; |
|
|
|
unsigned i; |
|
|
|
free(e->origin); |
|
|
|
free(e->origin); |
|
|
|
for (i = 0; i < e->num_matches; i++) { |
|
|
|
for (i = 0; i < e->num_matches; i++) { |
|
|
|
struct match_attr *a = e->attrs[i]; |
|
|
|
struct match_attr *a = e->attrs[i]; |
|
|
|
int j; |
|
|
|
size_t j; |
|
|
|
|
|
|
|
|
|
|
|
for (j = 0; j < a->num_attr; j++) { |
|
|
|
for (j = 0; j < a->num_attr; j++) { |
|
|
|
const char *setto = a->state[j].setto; |
|
|
|
const char *setto = a->state[j].setto; |
|
|
|
if (setto == ATTR__TRUE || |
|
|
|
if (setto == ATTR__TRUE || |
|
|
@ -660,8 +664,8 @@ static void handle_attr_line(struct attr_stack *res, |
|
|
|
a = parse_attr_line(line, src, lineno, flags); |
|
|
|
a = parse_attr_line(line, src, lineno, flags); |
|
|
|
if (!a) |
|
|
|
if (!a) |
|
|
|
return; |
|
|
|
return; |
|
|
|
ALLOC_GROW(res->attrs, res->num_matches + 1, res->alloc); |
|
|
|
ALLOC_GROW_BY(res->attrs, res->num_matches, 1, res->alloc); |
|
|
|
res->attrs[res->num_matches++] = a; |
|
|
|
res->attrs[res->num_matches - 1] = a; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static struct attr_stack *read_attr_from_array(const char **list) |
|
|
|
static struct attr_stack *read_attr_from_array(const char **list) |
|
|
@ -701,11 +705,12 @@ void git_attr_set_direction(enum git_attr_direction new_direction) |
|
|
|
|
|
|
|
|
|
|
|
static struct attr_stack *read_attr_from_file(const char *path, unsigned flags) |
|
|
|
static struct attr_stack *read_attr_from_file(const char *path, unsigned flags) |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
struct strbuf buf = STRBUF_INIT; |
|
|
|
int fd; |
|
|
|
int fd; |
|
|
|
FILE *fp; |
|
|
|
FILE *fp; |
|
|
|
struct attr_stack *res; |
|
|
|
struct attr_stack *res; |
|
|
|
char buf[2048]; |
|
|
|
|
|
|
|
int lineno = 0; |
|
|
|
int lineno = 0; |
|
|
|
|
|
|
|
struct stat st; |
|
|
|
|
|
|
|
|
|
|
|
if (flags & READ_ATTR_NOFOLLOW) |
|
|
|
if (flags & READ_ATTR_NOFOLLOW) |
|
|
|
fd = open_nofollow(path, O_RDONLY); |
|
|
|
fd = open_nofollow(path, O_RDONLY); |
|
|
@ -717,15 +722,26 @@ static struct attr_stack *read_attr_from_file(const char *path, unsigned flags) |
|
|
|
return NULL; |
|
|
|
return NULL; |
|
|
|
} |
|
|
|
} |
|
|
|
fp = xfdopen(fd, "r"); |
|
|
|
fp = xfdopen(fd, "r"); |
|
|
|
|
|
|
|
if (fstat(fd, &st)) { |
|
|
|
|
|
|
|
warning_errno(_("cannot fstat gitattributes file '%s'"), path); |
|
|
|
|
|
|
|
fclose(fp); |
|
|
|
|
|
|
|
return NULL; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (st.st_size >= ATTR_MAX_FILE_SIZE) { |
|
|
|
|
|
|
|
warning(_("ignoring overly large gitattributes file '%s'"), path); |
|
|
|
|
|
|
|
fclose(fp); |
|
|
|
|
|
|
|
return NULL; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
CALLOC_ARRAY(res, 1); |
|
|
|
CALLOC_ARRAY(res, 1); |
|
|
|
while (fgets(buf, sizeof(buf), fp)) { |
|
|
|
while (strbuf_getline(&buf, fp) != EOF) { |
|
|
|
char *bufp = buf; |
|
|
|
if (!lineno && starts_with(buf.buf, utf8_bom)) |
|
|
|
if (!lineno) |
|
|
|
strbuf_remove(&buf, 0, strlen(utf8_bom)); |
|
|
|
skip_utf8_bom(&bufp, strlen(bufp)); |
|
|
|
handle_attr_line(res, buf.buf, path, ++lineno, flags); |
|
|
|
handle_attr_line(res, bufp, path, ++lineno, flags); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fclose(fp); |
|
|
|
fclose(fp); |
|
|
|
|
|
|
|
strbuf_release(&buf); |
|
|
|
return res; |
|
|
|
return res; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -736,6 +752,7 @@ static struct attr_stack *read_attr_from_index(struct index_state *istate, |
|
|
|
struct attr_stack *res; |
|
|
|
struct attr_stack *res; |
|
|
|
char *buf, *sp; |
|
|
|
char *buf, *sp; |
|
|
|
int lineno = 0; |
|
|
|
int lineno = 0; |
|
|
|
|
|
|
|
size_t size; |
|
|
|
|
|
|
|
|
|
|
|
if (!istate) |
|
|
|
if (!istate) |
|
|
|
return NULL; |
|
|
|
return NULL; |
|
|
@ -754,9 +771,13 @@ static struct attr_stack *read_attr_from_index(struct index_state *istate, |
|
|
|
if (!path_in_cone_mode_sparse_checkout(path, istate)) |
|
|
|
if (!path_in_cone_mode_sparse_checkout(path, istate)) |
|
|
|
return NULL; |
|
|
|
return NULL; |
|
|
|
|
|
|
|
|
|
|
|
buf = read_blob_data_from_index(istate, path, NULL); |
|
|
|
buf = read_blob_data_from_index(istate, path, &size); |
|
|
|
if (!buf) |
|
|
|
if (!buf) |
|
|
|
return NULL; |
|
|
|
return NULL; |
|
|
|
|
|
|
|
if (size >= ATTR_MAX_FILE_SIZE) { |
|
|
|
|
|
|
|
warning(_("ignoring overly large gitattributes blob '%s'"), path); |
|
|
|
|
|
|
|
return NULL; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
CALLOC_ARRAY(res, 1); |
|
|
|
CALLOC_ARRAY(res, 1); |
|
|
|
for (sp = buf; *sp; ) { |
|
|
|
for (sp = buf; *sp; ) { |
|
|
@ -999,12 +1020,12 @@ static int macroexpand_one(struct all_attrs_item *all_attrs, int nr, int rem); |
|
|
|
static int fill_one(struct all_attrs_item *all_attrs, |
|
|
|
static int fill_one(struct all_attrs_item *all_attrs, |
|
|
|
const struct match_attr *a, int rem) |
|
|
|
const struct match_attr *a, int rem) |
|
|
|
{ |
|
|
|
{ |
|
|
|
int i; |
|
|
|
size_t i; |
|
|
|
|
|
|
|
|
|
|
|
for (i = a->num_attr - 1; rem > 0 && i >= 0; i--) { |
|
|
|
for (i = a->num_attr; rem > 0 && i > 0; i--) { |
|
|
|
const struct git_attr *attr = a->state[i].attr; |
|
|
|
const struct git_attr *attr = a->state[i - 1].attr; |
|
|
|
const char **n = &(all_attrs[attr->attr_nr].value); |
|
|
|
const char **n = &(all_attrs[attr->attr_nr].value); |
|
|
|
const char *v = a->state[i].setto; |
|
|
|
const char *v = a->state[i - 1].setto; |
|
|
|
|
|
|
|
|
|
|
|
if (*n == ATTR__UNKNOWN) { |
|
|
|
if (*n == ATTR__UNKNOWN) { |
|
|
|
*n = v; |
|
|
|
*n = v; |
|
|
@ -1020,11 +1041,11 @@ static int fill(const char *path, int pathlen, int basename_offset, |
|
|
|
struct all_attrs_item *all_attrs, int rem) |
|
|
|
struct all_attrs_item *all_attrs, int rem) |
|
|
|
{ |
|
|
|
{ |
|
|
|
for (; rem > 0 && stack; stack = stack->prev) { |
|
|
|
for (; rem > 0 && stack; stack = stack->prev) { |
|
|
|
int i; |
|
|
|
unsigned i; |
|
|
|
const char *base = stack->origin ? stack->origin : ""; |
|
|
|
const char *base = stack->origin ? stack->origin : ""; |
|
|
|
|
|
|
|
|
|
|
|
for (i = stack->num_matches - 1; 0 < rem && 0 <= i; i--) { |
|
|
|
for (i = stack->num_matches; 0 < rem && 0 < i; i--) { |
|
|
|
const struct match_attr *a = stack->attrs[i]; |
|
|
|
const struct match_attr *a = stack->attrs[i - 1]; |
|
|
|
if (a->is_macro) |
|
|
|
if (a->is_macro) |
|
|
|
continue; |
|
|
|
continue; |
|
|
|
if (path_matches(path, pathlen, basename_offset, |
|
|
|
if (path_matches(path, pathlen, basename_offset, |
|
|
@ -1055,11 +1076,11 @@ static void determine_macros(struct all_attrs_item *all_attrs, |
|
|
|
const struct attr_stack *stack) |
|
|
|
const struct attr_stack *stack) |
|
|
|
{ |
|
|
|
{ |
|
|
|
for (; stack; stack = stack->prev) { |
|
|
|
for (; stack; stack = stack->prev) { |
|
|
|
int i; |
|
|
|
unsigned i; |
|
|
|
for (i = stack->num_matches - 1; i >= 0; i--) { |
|
|
|
for (i = stack->num_matches; i > 0; i--) { |
|
|
|
const struct match_attr *ma = stack->attrs[i]; |
|
|
|
const struct match_attr *ma = stack->attrs[i - 1]; |
|
|
|
if (ma->is_macro) { |
|
|
|
if (ma->is_macro) { |
|
|
|
int n = ma->u.attr->attr_nr; |
|
|
|
unsigned int n = ma->u.attr->attr_nr; |
|
|
|
if (!all_attrs[n].macro) { |
|
|
|
if (!all_attrs[n].macro) { |
|
|
|
all_attrs[n].macro = ma; |
|
|
|
all_attrs[n].macro = ma; |
|
|
|
} |
|
|
|
} |
|
|
@ -1111,7 +1132,7 @@ void git_check_attr(struct index_state *istate, |
|
|
|
collect_some_attrs(istate, path, check); |
|
|
|
collect_some_attrs(istate, path, check); |
|
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < check->nr; i++) { |
|
|
|
for (i = 0; i < check->nr; i++) { |
|
|
|
size_t n = check->items[i].attr->attr_nr; |
|
|
|
unsigned int n = check->items[i].attr->attr_nr; |
|
|
|
const char *value = check->all_attrs[n].value; |
|
|
|
const char *value = check->all_attrs[n].value; |
|
|
|
if (value == ATTR__UNKNOWN) |
|
|
|
if (value == ATTR__UNKNOWN) |
|
|
|
value = ATTR__UNSET; |
|
|
|
value = ATTR__UNSET; |
|
|
|