@matheusbsbs / matheus-bagliano / commits / a5e92abde6
Fork 0

Fix funny types used in attribute value representation

It was bothering me a lot that I abused small integer values casted to (void *) to represent non string values in gitattributes. This corrects it by making the type of attribute values (const char *), and using the address of a few statically allocated character buffer to denote true/false. Unset attributes are represented as having NULLs as their values. Added in-header documentation to explain how git_checkattr() routine should be called. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Apr 18, 2007 at 16:16 UTC a5e92abde61d59a8612c5b87d0bae681e90f7fdb
6 files changed +43 -26
attr.c
+13 -7
@@ -1,7 +1,13 @@
1 #include "cache.h"
2 #include "attr.h"
3
4 -#define ATTR__UNKNOWN ((void *) -2)
4 +const char git_attr__true[] = "(builtin)true";
5 +const char git_attr__false[] = "\0(builtin)false";
6 +static const char git_attr__unknown[] = "(builtin)unknown";
7 +#define ATTR__TRUE git_attr__true
8 +#define ATTR__FALSE git_attr__false
9 +#define ATTR__UNSET NULL
10 +#define ATTR__UNKNOWN git_attr__unknown
11
12 /*
13 * The basic design decision here is that we are not going to have
@@ -102,7 +108,7 @@ struct git_attr *git_attr(const char *name, int len)
108 /* What does a matched pattern decide? */
109 struct attr_state {
110 struct git_attr *attr;
105 - void *setto;
111 + const char *setto;
112 };
113
114 struct match_attr {
@@ -262,14 +268,14 @@ static void free_attr_elem(struct attr_stack *e)
268 struct match_attr *a = e->attrs[i];
269 int j;
270 for (j = 0; j < a->num_attr; j++) {
265 - void *setto = a->state[j].setto;
271 + const char *setto = a->state[j].setto;
272 if (setto == ATTR__TRUE ||
273 setto == ATTR__FALSE ||
274 setto == ATTR__UNSET ||
275 setto == ATTR__UNKNOWN)
276 ;
277 else
272 - free(setto);
278 + free((char*) setto);
279 }
280 free(a);
281 }
@@ -478,8 +484,8 @@ static int fill_one(const char *what, struct match_attr *a, int rem)
484
485 for (i = 0; 0 < rem && i < a->num_attr; i++) {
486 struct git_attr *attr = a->state[i].attr;
481 - void **n = &(check[attr->attr_nr].value);
482 - void *v = a->state[i].setto;
487 + const char **n = &(check[attr->attr_nr].value);
488 + const char *v = a->state[i].setto;
489
490 if (*n == ATTR__UNKNOWN) {
491 debug_set(what, a->u.pattern, attr, v);
@@ -547,7 +553,7 @@ int git_checkattr(const char *path, int num, struct git_attr_check *check)
553 rem = macroexpand(stk, rem);
554
555 for (i = 0; i < num; i++) {
550 - void *value = check_all_attr[check[i].attr->attr_nr].value;
556 + const char *value = check_all_attr[check[i].attr->attr_nr].value;
557 if (value == ATTR__UNKNOWN)
558 value = ATTR__UNSET;
559 check[i].value = value;
attr.h
+15 -7
@@ -4,21 +4,29 @@
4 /* An attribute is a pointer to this opaque structure */
5 struct git_attr;
6
7 +/*
8 + * Given a string, return the gitattribute object that
9 + * corresponds to it.
10 + */
11 struct git_attr *git_attr(const char *, int);
12
13 /* Internal use */
10 -#define ATTR__TRUE ((void *) 1)
11 -#define ATTR__FALSE ((void *) 0)
12 -#define ATTR__UNSET ((void *) -1)
14 +extern const char git_attr__true[];
15 +extern const char git_attr__false[];
16
17 /* For public to check git_attr_check results */
15 -#define ATTR_TRUE(v) ((v) == ATTR__TRUE)
16 -#define ATTR_FALSE(v) ((v) == ATTR__FALSE)
17 -#define ATTR_UNSET(v) ((v) == ATTR__UNSET)
18 +#define ATTR_TRUE(v) ((v) == git_attr__true)
19 +#define ATTR_FALSE(v) ((v) == git_attr__false)
20 +#define ATTR_UNSET(v) ((v) == NULL)
21
22 +/*
23 + * Send one or more git_attr_check to git_checkattr(), and
24 + * each 'value' member tells what its value is.
25 + * Unset one is returned as NULL.
26 + */
27 struct git_attr_check {
28 struct git_attr *attr;
21 - void *value;
29 + const char *value;
30 };
31
32 int git_checkattr(const char *path, int, struct git_attr_check *);
builtin-check-attr.c
+2 -2
@@ -42,7 +42,7 @@ int cmd_check_attr(int argc, const char **argv, const char *prefix)
42 if (git_checkattr(argv[i], cnt, check))
43 die("git_checkattr died");
44 for (j = 0; j < cnt; j++) {
45 - void *value = check[j].value;
45 + const char *value = check[j].value;
46
47 if (ATTR_TRUE(value))
48 value = "set";
@@ -52,7 +52,7 @@ int cmd_check_attr(int argc, const char **argv, const char *prefix)
52 value = "unspecified";
53
54 write_name_quoted("", 0, argv[i], 1, stdout);
55 - printf(": %s: %s\n", argv[j+1], (char *) value);
55 + printf(": %s: %s\n", argv[j+1], value);
56 }
57 }
58 return 0;
convert.c
+1 -1
@@ -226,7 +226,7 @@ static int git_path_check_crlf(const char *path)
226 setup_crlf_check(&attr_crlf_check);
227
228 if (!git_checkattr(path, 1, &attr_crlf_check)) {
229 - void *value = attr_crlf_check.value;
229 + const char *value = attr_crlf_check.value;
230 if (ATTR_TRUE(value))
231 return 1;
232 else if (ATTR_FALSE(value))
diff.c
+2 -2
@@ -1069,7 +1069,7 @@ static int file_is_binary(struct diff_filespec *one)
1069
1070 setup_diff_attr_check(&attr_diff_check);
1071 if (!git_checkattr(one->path, 1, &attr_diff_check)) {
1072 - void *value = attr_diff_check.value;
1072 + const char *value = attr_diff_check.value;
1073 if (ATTR_TRUE(value))
1074 return 0;
1075 else if (ATTR_FALSE(value))
@@ -1078,7 +1078,7 @@ static int file_is_binary(struct diff_filespec *one)
1078 ;
1079 else
1080 die("unknown value %s given to 'diff' attribute",
1081 - (char *)value);
1081 + value);
1082 }
1083
1084 if (!one->data) {
merge-recursive.c
+10 -7
@@ -953,7 +953,7 @@ static void initialize_ll_merge(void)
953 git_config(read_merge_config);
954 }
955
956 -static const struct ll_merge_driver *find_ll_merge_driver(void *merge_attr)
956 +static const struct ll_merge_driver *find_ll_merge_driver(const char *merge_attr)
957 {
958 struct ll_merge_driver *fn;
959 const char *name;
@@ -986,7 +986,7 @@ static const struct ll_merge_driver *find_ll_merge_driver(void *merge_attr)
986 return &ll_merge_drv[LL_TEXT_MERGE];
987 }
988
989 -static void *git_path_check_merge(const char *path)
989 +static const char *git_path_check_merge(const char *path)
990 {
991 static struct git_attr_check attr_merge_check;
992
@@ -994,7 +994,7 @@ static void *git_path_check_merge(const char *path)
994 attr_merge_check.attr = git_attr("merge", 5);
995
996 if (git_checkattr(path, 1, &attr_merge_check))
997 - return ATTR__UNSET;
997 + return NULL;
998 return attr_merge_check.value;
999 }
1000
@@ -1008,7 +1008,7 @@ static int ll_merge(mmbuffer_t *result_buf,
1008 mmfile_t orig, src1, src2;
1009 char *name1, *name2;
1010 int merge_status;
1011 - void *merge_attr;
1011 + const char *ll_driver_name;
1012 const struct ll_merge_driver *driver;
1013
1014 name1 = xstrdup(mkpath("%s:%s", branch1, a->path));
@@ -1018,11 +1018,14 @@ static int ll_merge(mmbuffer_t *result_buf,
1018 fill_mm(a->sha1, &src1);
1019 fill_mm(b->sha1, &src2);
1020
1021 - merge_attr = git_path_check_merge(a->path);
1022 - driver = find_ll_merge_driver(merge_attr);
1021 + ll_driver_name = git_path_check_merge(a->path);
1022 + driver = find_ll_merge_driver(ll_driver_name);
1023
1024 if (index_only && driver->recursive) {
1025 - merge_attr = git_attr(driver->recursive, strlen(driver->recursive));
1025 + void *merge_attr;
1026 +
1027 + ll_driver_name = driver->recursive;
1028 + merge_attr = git_attr(ll_driver_name, strlen(ll_driver_name));
1029 driver = find_ll_merge_driver(merge_attr);
1030 }
1031 merge_status = driver->fn(driver, a->path,