Skip to content

Commit dbf387d

Browse files
peffgitster
authored andcommitted
attr: convert "macro_ok" into a flags field
The attribute code can have a rather deep callstack, through which we have to pass the "macro_ok" flag. In anticipation of adding other flags, let's convert this to a generic bit-field. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 00611d8 commit dbf387d

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

attr.c

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,9 @@ struct match_attr {
278278

279279
static const char blank[] = " \t\r\n";
280280

281+
/* Flags usable in read_attr() and parse_attr_line() family of functions. */
282+
#define READ_ATTR_MACRO_OK (1<<0)
283+
281284
/*
282285
* Parse a whitespace-delimited attribute state (i.e., "attr",
283286
* "-attr", "!attr", or "attr=value") from the string starting at src.
@@ -331,7 +334,7 @@ static const char *parse_attr(const char *src, int lineno, const char *cp,
331334
}
332335

333336
static struct match_attr *parse_attr_line(const char *line, const char *src,
334-
int lineno, int macro_ok)
337+
int lineno, unsigned flags)
335338
{
336339
int namelen;
337340
int num_attr, i;
@@ -355,7 +358,7 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,
355358

356359
if (strlen(ATTRIBUTE_MACRO_PREFIX) < namelen &&
357360
starts_with(name, ATTRIBUTE_MACRO_PREFIX)) {
358-
if (!macro_ok) {
361+
if (!(flags & READ_ATTR_MACRO_OK)) {
359362
fprintf_ln(stderr, _("%s not allowed: %s:%d"),
360363
name, src, lineno);
361364
goto fail_return;
@@ -653,11 +656,11 @@ static void handle_attr_line(struct attr_stack *res,
653656
const char *line,
654657
const char *src,
655658
int lineno,
656-
int macro_ok)
659+
unsigned flags)
657660
{
658661
struct match_attr *a;
659662

660-
a = parse_attr_line(line, src, lineno, macro_ok);
663+
a = parse_attr_line(line, src, lineno, flags);
661664
if (!a)
662665
return;
663666
ALLOC_GROW(res->attrs, res->num_matches + 1, res->alloc);
@@ -672,7 +675,8 @@ static struct attr_stack *read_attr_from_array(const char **list)
672675

673676
res = xcalloc(1, sizeof(*res));
674677
while ((line = *(list++)) != NULL)
675-
handle_attr_line(res, line, "[builtin]", ++lineno, 1);
678+
handle_attr_line(res, line, "[builtin]", ++lineno,
679+
READ_ATTR_MACRO_OK);
676680
return res;
677681
}
678682

@@ -698,7 +702,7 @@ void git_attr_set_direction(enum git_attr_direction new_direction)
698702
direction = new_direction;
699703
}
700704

701-
static struct attr_stack *read_attr_from_file(const char *path, int macro_ok)
705+
static struct attr_stack *read_attr_from_file(const char *path, unsigned flags)
702706
{
703707
FILE *fp = fopen_or_warn(path, "r");
704708
struct attr_stack *res;
@@ -712,15 +716,15 @@ static struct attr_stack *read_attr_from_file(const char *path, int macro_ok)
712716
char *bufp = buf;
713717
if (!lineno)
714718
skip_utf8_bom(&bufp, strlen(bufp));
715-
handle_attr_line(res, bufp, path, ++lineno, macro_ok);
719+
handle_attr_line(res, bufp, path, ++lineno, flags);
716720
}
717721
fclose(fp);
718722
return res;
719723
}
720724

721725
static struct attr_stack *read_attr_from_index(const struct index_state *istate,
722726
const char *path,
723-
int macro_ok)
727+
unsigned flags)
724728
{
725729
struct attr_stack *res;
726730
char *buf, *sp;
@@ -741,35 +745,35 @@ static struct attr_stack *read_attr_from_index(const struct index_state *istate,
741745
ep = strchrnul(sp, '\n');
742746
more = (*ep == '\n');
743747
*ep = '\0';
744-
handle_attr_line(res, sp, path, ++lineno, macro_ok);
748+
handle_attr_line(res, sp, path, ++lineno, flags);
745749
sp = ep + more;
746750
}
747751
free(buf);
748752
return res;
749753
}
750754

751755
static struct attr_stack *read_attr(const struct index_state *istate,
752-
const char *path, int macro_ok)
756+
const char *path, unsigned flags)
753757
{
754758
struct attr_stack *res = NULL;
755759

756760
if (direction == GIT_ATTR_INDEX) {
757-
res = read_attr_from_index(istate, path, macro_ok);
761+
res = read_attr_from_index(istate, path, flags);
758762
} else if (!is_bare_repository()) {
759763
if (direction == GIT_ATTR_CHECKOUT) {
760-
res = read_attr_from_index(istate, path, macro_ok);
764+
res = read_attr_from_index(istate, path, flags);
761765
if (!res)
762-
res = read_attr_from_file(path, macro_ok);
766+
res = read_attr_from_file(path, flags);
763767
} else if (direction == GIT_ATTR_CHECKIN) {
764-
res = read_attr_from_file(path, macro_ok);
768+
res = read_attr_from_file(path, flags);
765769
if (!res)
766770
/*
767771
* There is no checked out .gitattributes file
768772
* there, but we might have it in the index.
769773
* We allow operation in a sparsely checked out
770774
* work tree, so read from it.
771775
*/
772-
res = read_attr_from_index(istate, path, macro_ok);
776+
res = read_attr_from_index(istate, path, flags);
773777
}
774778
}
775779

@@ -844,6 +848,7 @@ static void bootstrap_attr_stack(const struct index_state *istate,
844848
struct attr_stack **stack)
845849
{
846850
struct attr_stack *e;
851+
unsigned flags = READ_ATTR_MACRO_OK;
847852

848853
if (*stack)
849854
return;
@@ -854,23 +859,23 @@ static void bootstrap_attr_stack(const struct index_state *istate,
854859

855860
/* system-wide frame */
856861
if (git_attr_system()) {
857-
e = read_attr_from_file(git_etc_gitattributes(), 1);
862+
e = read_attr_from_file(git_etc_gitattributes(), flags);
858863
push_stack(stack, e, NULL, 0);
859864
}
860865

861866
/* home directory */
862867
if (get_home_gitattributes()) {
863-
e = read_attr_from_file(get_home_gitattributes(), 1);
868+
e = read_attr_from_file(get_home_gitattributes(), flags);
864869
push_stack(stack, e, NULL, 0);
865870
}
866871

867872
/* root directory */
868-
e = read_attr(istate, GITATTRIBUTES_FILE, 1);
873+
e = read_attr(istate, GITATTRIBUTES_FILE, flags);
869874
push_stack(stack, e, xstrdup(""), 0);
870875

871876
/* info frame */
872877
if (startup_info->have_repository)
873-
e = read_attr_from_file(git_path_info_attributes(), 1);
878+
e = read_attr_from_file(git_path_info_attributes(), flags);
874879
else
875880
e = NULL;
876881
if (!e)

0 commit comments

Comments
 (0)