convert: make it safer to add conversion attributes

The places that need to pass an array of "struct git_attr_check" needed to be careful to pass a large enough array and know what index each element lied. Make it safer and easier to code these. Besides, the hard-coded sequence of initializing various attributes was too ugly after we gained more than a few attributes. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed May 9, 2011 at 11:23 UTC 83295964b3289e957d028960f14a2b71348c39ed
1 file changed +22 -26
convert.c
+22 -26
@@ -475,30 +475,6 @@ static int read_convert_config(const char *var, const char *value, void *cb)
475 return 0;
476 }
477
478 -static void setup_convert_check(struct git_attr_check *check)
479 -{
480 - static struct git_attr *attr_text;
481 - static struct git_attr *attr_crlf;
482 - static struct git_attr *attr_eol;
483 - static struct git_attr *attr_ident;
484 - static struct git_attr *attr_filter;
485 -
486 - if (!attr_text) {
487 - attr_text = git_attr("text");
488 - attr_crlf = git_attr("crlf");
489 - attr_eol = git_attr("eol");
490 - attr_ident = git_attr("ident");
491 - attr_filter = git_attr("filter");
492 - user_convert_tail = &user_convert;
493 - git_config(read_convert_config, NULL);
494 - }
495 - check[0].attr = attr_crlf;
496 - check[1].attr = attr_ident;
497 - check[2].attr = attr_filter;
498 - check[3].attr = attr_eol;
499 - check[4].attr = attr_text;
500 -}
501 -
478 static int count_ident(const char *cp, unsigned long size)
479 {
480 /*
@@ -727,10 +703,30 @@ static enum crlf_action input_crlf_action(enum crlf_action text_attr, enum eol e
703 return text_attr;
704 }
705
706 +static const char *conv_attr_name[] = {
707 + "crlf", "ident", "filter", "eol", "text",
708 +};
709 +#define NUM_CONV_ATTRS ARRAY_SIZE(conv_attr_name)
710 +
711 +static void setup_convert_check(struct git_attr_check *check)
712 +{
713 + int i;
714 + static struct git_attr_check ccheck[NUM_CONV_ATTRS];
715 +
716 + if (!ccheck[0].attr) {
717 + for (i = 0; i < NUM_CONV_ATTRS; i++)
718 + ccheck[i].attr = git_attr(conv_attr_name[i]);
719 + user_convert_tail = &user_convert;
720 + git_config(read_convert_config, NULL);
721 + }
722 + for (i = 0; i < NUM_CONV_ATTRS; i++)
723 + check[i].attr = ccheck[i].attr;
724 +}
725 +
726 int convert_to_git(const char *path, const char *src, size_t len,
727 struct strbuf *dst, enum safe_crlf checksafe)
728 {
733 - struct git_attr_check check[5];
729 + struct git_attr_check check[NUM_CONV_ATTRS];
730 enum crlf_action crlf_action = CRLF_GUESS;
731 enum eol eol_attr = EOL_UNSET;
732 int ident = 0, ret = 0;
@@ -767,7 +763,7 @@ static int convert_to_working_tree_internal(const char *path, const char *src,
763 size_t len, struct strbuf *dst,
764 int normalizing)
765 {
770 - struct git_attr_check check[5];
766 + struct git_attr_check check[NUM_CONV_ATTRS];
767 enum crlf_action crlf_action = CRLF_GUESS;
768 enum eol eol_attr = EOL_UNSET;
769 int ident = 0, ret = 0;