Skip to content

Commit 8329596

Browse files
committed
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 <[email protected]>
1 parent c61dcff commit 8329596

File tree

1 file changed

+22
-26
lines changed

1 file changed

+22
-26
lines changed

convert.c

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -475,30 +475,6 @@ static int read_convert_config(const char *var, const char *value, void *cb)
475475
return 0;
476476
}
477477

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-
502478
static int count_ident(const char *cp, unsigned long size)
503479
{
504480
/*
@@ -727,10 +703,30 @@ static enum crlf_action input_crlf_action(enum crlf_action text_attr, enum eol e
727703
return text_attr;
728704
}
729705

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+
730726
int convert_to_git(const char *path, const char *src, size_t len,
731727
struct strbuf *dst, enum safe_crlf checksafe)
732728
{
733-
struct git_attr_check check[5];
729+
struct git_attr_check check[NUM_CONV_ATTRS];
734730
enum crlf_action crlf_action = CRLF_GUESS;
735731
enum eol eol_attr = EOL_UNSET;
736732
int ident = 0, ret = 0;
@@ -767,7 +763,7 @@ static int convert_to_working_tree_internal(const char *path, const char *src,
767763
size_t len, struct strbuf *dst,
768764
int normalizing)
769765
{
770-
struct git_attr_check check[5];
766+
struct git_attr_check check[NUM_CONV_ATTRS];
771767
enum crlf_action crlf_action = CRLF_GUESS;
772768
enum eol eol_attr = EOL_UNSET;
773769
int ident = 0, ret = 0;

0 commit comments

Comments
 (0)