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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Feb 16, 2021 at 09:44 UTC dbf387d550d679369f3bbbcf2c25ed03f7ff851f
1 file changed +24 -19
attr.c
+24 -19
@@ -278,6 +278,9 @@ struct match_attr {
278
279 static const char blank[] = " \t\r\n";
280
281 +/* Flags usable in read_attr() and parse_attr_line() family of functions. */
282 +#define READ_ATTR_MACRO_OK (1<<0)
283 +
284 /*
285 * Parse a whitespace-delimited attribute state (i.e., "attr",
286 * "-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,
334 }
335
336 static struct match_attr *parse_attr_line(const char *line, const char *src,
334 - int lineno, int macro_ok)
337 + int lineno, unsigned flags)
338 {
339 int namelen;
340 int num_attr, i;
@@ -355,7 +358,7 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,
358
359 if (strlen(ATTRIBUTE_MACRO_PREFIX) < namelen &&
360 starts_with(name, ATTRIBUTE_MACRO_PREFIX)) {
358 - if (!macro_ok) {
361 + if (!(flags & READ_ATTR_MACRO_OK)) {
362 fprintf_ln(stderr, _("%s not allowed: %s:%d"),
363 name, src, lineno);
364 goto fail_return;
@@ -653,11 +656,11 @@ static void handle_attr_line(struct attr_stack *res,
656 const char *line,
657 const char *src,
658 int lineno,
656 - int macro_ok)
659 + unsigned flags)
660 {
661 struct match_attr *a;
662
660 - a = parse_attr_line(line, src, lineno, macro_ok);
663 + a = parse_attr_line(line, src, lineno, flags);
664 if (!a)
665 return;
666 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)
675
676 res = xcalloc(1, sizeof(*res));
677 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);
680 return res;
681 }
682
@@ -698,7 +702,7 @@ void git_attr_set_direction(enum git_attr_direction new_direction)
702 direction = new_direction;
703 }
704
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)
706 {
707 FILE *fp = fopen_or_warn(path, "r");
708 struct attr_stack *res;
@@ -712,7 +716,7 @@ static struct attr_stack *read_attr_from_file(const char *path, int macro_ok)
716 char *bufp = buf;
717 if (!lineno)
718 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);
720 }
721 fclose(fp);
722 return res;
@@ -720,7 +724,7 @@ static struct attr_stack *read_attr_from_file(const char *path, int macro_ok)
724
725 static struct attr_stack *read_attr_from_index(const struct index_state *istate,
726 const char *path,
723 - int macro_ok)
727 + unsigned flags)
728 {
729 struct attr_stack *res;
730 char *buf, *sp;
@@ -741,7 +745,7 @@ static struct attr_stack *read_attr_from_index(const struct index_state *istate,
745 ep = strchrnul(sp, '\n');
746 more = (*ep == '\n');
747 *ep = '\0';
744 - handle_attr_line(res, sp, path, ++lineno, macro_ok);
748 + handle_attr_line(res, sp, path, ++lineno, flags);
749 sp = ep + more;
750 }
751 free(buf);
@@ -749,19 +753,19 @@ static struct attr_stack *read_attr_from_index(const struct index_state *istate,
753 }
754
755 static struct attr_stack *read_attr(const struct index_state *istate,
752 - const char *path, int macro_ok)
756 + const char *path, unsigned flags)
757 {
758 struct attr_stack *res = NULL;
759
760 if (direction == GIT_ATTR_INDEX) {
757 - res = read_attr_from_index(istate, path, macro_ok);
761 + res = read_attr_from_index(istate, path, flags);
762 } else if (!is_bare_repository()) {
763 if (direction == GIT_ATTR_CHECKOUT) {
760 - res = read_attr_from_index(istate, path, macro_ok);
764 + res = read_attr_from_index(istate, path, flags);
765 if (!res)
762 - res = read_attr_from_file(path, macro_ok);
766 + res = read_attr_from_file(path, flags);
767 } else if (direction == GIT_ATTR_CHECKIN) {
764 - res = read_attr_from_file(path, macro_ok);
768 + res = read_attr_from_file(path, flags);
769 if (!res)
770 /*
771 * There is no checked out .gitattributes file
@@ -769,7 +773,7 @@ static struct attr_stack *read_attr(const struct index_state *istate,
773 * We allow operation in a sparsely checked out
774 * work tree, so read from it.
775 */
772 - res = read_attr_from_index(istate, path, macro_ok);
776 + res = read_attr_from_index(istate, path, flags);
777 }
778 }
779
@@ -844,6 +848,7 @@ static void bootstrap_attr_stack(const struct index_state *istate,
848 struct attr_stack **stack)
849 {
850 struct attr_stack *e;
851 + unsigned flags = READ_ATTR_MACRO_OK;
852
853 if (*stack)
854 return;
@@ -854,23 +859,23 @@ static void bootstrap_attr_stack(const struct index_state *istate,
859
860 /* system-wide frame */
861 if (git_attr_system()) {
857 - e = read_attr_from_file(git_etc_gitattributes(), 1);
862 + e = read_attr_from_file(git_etc_gitattributes(), flags);
863 push_stack(stack, e, NULL, 0);
864 }
865
866 /* home directory */
867 if (get_home_gitattributes()) {
863 - e = read_attr_from_file(get_home_gitattributes(), 1);
868 + e = read_attr_from_file(get_home_gitattributes(), flags);
869 push_stack(stack, e, NULL, 0);
870 }
871
872 /* root directory */
868 - e = read_attr(istate, GITATTRIBUTES_FILE, 1);
873 + e = read_attr(istate, GITATTRIBUTES_FILE, flags);
874 push_stack(stack, e, xstrdup(""), 0);
875
876 /* info frame */
877 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);
879 else
880 e = NULL;
881 if (!e)