From 8c701249d2257699c19822b528c101668abc55b9 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Thu, 12 Apr 2007 23:05:29 -0700 Subject: Teach 'diff' about 'diff' attribute. This makes paths that explicitly unset 'diff' attribute not to produce "textual" diffs from 'git-diff' family. Signed-off-by: Junio C Hamano --- diff.c | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) (limited to 'diff.c') diff --git a/diff.c b/diff.c index fbb79d70a9..e4efb657e8 100644 --- a/diff.c +++ b/diff.c @@ -8,6 +8,7 @@ #include "delta.h" #include "xdiff-interface.h" #include "color.h" +#include "attr.h" #ifdef NO_FAST_WORKING_DIRECTORY #define FAST_WORKING_DIRECTORY 0 @@ -1051,13 +1052,34 @@ static void emit_binary_diff(mmfile_t *one, mmfile_t *two) emit_binary_diff_body(two, one); } +static void setup_diff_attr_check(struct git_attr_check *check) +{ + static struct git_attr *attr_diff; + + if (!attr_diff) + attr_diff = git_attr("diff", 4); + check->attr = attr_diff; +} + #define FIRST_FEW_BYTES 8000 -static int mmfile_is_binary(mmfile_t *mf) +static int file_is_binary(struct diff_filespec *one) { - long sz = mf->size; + unsigned long sz; + struct git_attr_check attr_diff_check; + + setup_diff_attr_check(&attr_diff_check); + if (!git_checkattr(one->path, 1, &attr_diff_check) && + (0 == attr_diff_check.isset)) + return 1; + if (!one->data) { + if (!DIFF_FILE_VALID(one)) + return 0; + diff_populate_filespec(one, 0); + } + sz = one->size; if (FIRST_FEW_BYTES < sz) sz = FIRST_FEW_BYTES; - return !!memchr(mf->ptr, 0, sz); + return !!memchr(one->data, 0, sz); } static void builtin_diff(const char *name_a, @@ -1114,7 +1136,7 @@ static void builtin_diff(const char *name_a, if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0) die("unable to read files to diff"); - if (!o->text && (mmfile_is_binary(&mf1) || mmfile_is_binary(&mf2))) { + if (!o->text && (file_is_binary(one) || file_is_binary(two))) { /* Quite common confusing case */ if (mf1.size == mf2.size && !memcmp(mf1.ptr, mf2.ptr, mf1.size)) @@ -1190,7 +1212,7 @@ static void builtin_diffstat(const char *name_a, const char *name_b, if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0) die("unable to read files to diff"); - if (mmfile_is_binary(&mf1) || mmfile_is_binary(&mf2)) { + if (file_is_binary(one) || file_is_binary(two)) { data->is_binary = 1; data->added = mf2.size; data->deleted = mf1.size; @@ -1228,7 +1250,7 @@ static void builtin_checkdiff(const char *name_a, const char *name_b, if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0) die("unable to read files to diff"); - if (mmfile_is_binary(&mf2)) + if (file_is_binary(two)) return; else { /* Crazy xdl interfaces.. */ @@ -1805,8 +1827,8 @@ static void run_diff(struct diff_filepair *p, struct diff_options *o) if (o->binary) { mmfile_t mf; - if ((!fill_mmfile(&mf, one) && mmfile_is_binary(&mf)) || - (!fill_mmfile(&mf, two) && mmfile_is_binary(&mf))) + if ((!fill_mmfile(&mf, one) && file_is_binary(one)) || + (!fill_mmfile(&mf, two) && file_is_binary(two))) abbrev = 40; } len += snprintf(msg + len, sizeof(msg) - len, @@ -2701,7 +2723,7 @@ static int diff_get_patch_id(struct diff_options *options, unsigned char *sha1) return error("unable to read files to diff"); /* Maybe hash p->two? into the patch id? */ - if (mmfile_is_binary(&mf2)) + if (file_is_binary(p->two)) continue; len1 = remove_space(p->one->path, strlen(p->one->path)); -- cgit 1.2.3-korg From 40250af411f33afa0c39a5d461829b676453ce3b Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Sun, 15 Apr 2007 14:35:11 -0700 Subject: Fix 'diff' attribute semantics. This is in the same spirit as the previous one. Earlier 'diff' meant 'do the built-in binary heuristics and disable patch text generation based on it' while '!diff' meant 'do not guess, do not generate patch text'. There was no way to say 'do generate patch text even when the heuristics says it has NUL in it'. Signed-off-by: Junio C Hamano --- diff.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'diff.c') diff --git a/diff.c b/diff.c index e4efb657e8..dcea405ed3 100644 --- a/diff.c +++ b/diff.c @@ -1069,8 +1069,9 @@ static int file_is_binary(struct diff_filespec *one) setup_diff_attr_check(&attr_diff_check); if (!git_checkattr(one->path, 1, &attr_diff_check) && - (0 == attr_diff_check.isset)) - return 1; + (0 <= attr_diff_check.isset)) + return !attr_diff_check.isset; + if (!one->data) { if (!DIFF_FILE_VALID(one)) return 0; -- cgit 1.2.3-korg From 515106fa1335462393c08fa8712dddd767dc147a Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Mon, 16 Apr 2007 21:33:31 -0700 Subject: Allow more than true/false to attributes. This allows you to define three values (and possibly more) to each attribute: true, false, and unset. Typically the handlers that notice and act on attribute values treat "unset" attribute to mean "do your default thing" (e.g. crlf that is unset would trigger "guess from contents"), so being able to override a setting to an unset state is actually useful. - If you want to set the attribute value to true, have an entry in .gitattributes file that mentions the attribute name; e.g. *.o binary - If you want to set the attribute value explicitly to false, use '-'; e.g. *.a -diff - If you want to make the attribute value _unset_, perhaps to override an earlier entry, use '!'; e.g. *.a -diff c.i.a !diff This also allows string values to attributes, with the natural syntax: attrname=attrvalue but you cannot use it, as nobody takes notice and acts on it yet. Signed-off-by: Junio C Hamano --- attr.c | 194 ++++++++++++++++++++++++++++++++------------------- attr.h | 12 +++- builtin-check-attr.c | 14 ++-- convert.c | 16 ++++- diff.c | 15 +++- 5 files changed, 169 insertions(+), 82 deletions(-) (limited to 'diff.c') diff --git a/attr.c b/attr.c index 60fe48f3b8..b3496a6eb5 100644 --- a/attr.c +++ b/attr.c @@ -1,6 +1,8 @@ #include "cache.h" #include "attr.h" +#define ATTR__UNKNOWN ((void *) -2) + /* * The basic design decision here is that we are not going to have * insanely large number of attributes. @@ -83,6 +85,7 @@ struct git_attr *git_attr(const char *name, int len) check_all_attr = xrealloc(check_all_attr, sizeof(*check_all_attr) * attr_nr); check_all_attr[a->attr_nr].attr = a; + check_all_attr[a->attr_nr].value = ATTR__UNKNOWN; return a; } @@ -92,12 +95,14 @@ struct git_attr *git_attr(const char *name, int len) * (1) glob pattern. * (2) whitespace * (3) whitespace separated list of attribute names, each of which - * could be prefixed with '-' to mean "not set". + * could be prefixed with '-' to mean "set to false", '!' to mean + * "unset". */ +/* What does a matched pattern decide? */ struct attr_state { - int unset; struct git_attr *attr; + void *setto; }; struct match_attr { @@ -112,13 +117,63 @@ struct match_attr { static const char blank[] = " \t\r\n"; +static const char *parse_attr(const char *src, int lineno, const char *cp, + int *num_attr, struct match_attr *res) +{ + const char *ep, *equals; + int len; + + ep = cp + strcspn(cp, blank); + equals = strchr(cp, '='); + if (equals && ep < equals) + equals = NULL; + if (equals) + len = equals - cp; + else + len = ep - cp; + if (!res) { + if (*cp == '-' || *cp == '!') { + cp++; + len--; + } + if (invalid_attr_name(cp, len)) { + fprintf(stderr, + "%.*s is not a valid attribute name: %s:%d\n", + len, cp, src, lineno); + return NULL; + } + } else { + struct attr_state *e; + + e = &(res->state[*num_attr]); + if (*cp == '-' || *cp == '!') { + e->setto = (*cp == '-') ? ATTR__FALSE : ATTR__UNSET; + cp++; + len--; + } + else if (!equals) + e->setto = ATTR__TRUE; + else { + char *value; + int vallen = ep - equals; + value = xmalloc(vallen); + memcpy(value, equals+1, vallen-1); + value[vallen-1] = 0; + e->setto = value; + } + e->attr = git_attr(cp, len); + } + (*num_attr)++; + return ep + strspn(ep, blank); +} + static struct match_attr *parse_attr_line(const char *line, const char *src, int lineno, int macro_ok) { int namelen; int num_attr; const char *cp, *name; - struct match_attr *res = res; + struct match_attr *res = NULL; int pass; int is_macro; @@ -153,42 +208,16 @@ static struct match_attr *parse_attr_line(const char *line, const char *src, num_attr = 0; cp = name + namelen; cp = cp + strspn(cp, blank); - while (*cp) { - const char *ep; - ep = cp + strcspn(cp, blank); - if (!pass) { - if (*cp == '-') - cp++; - if (invalid_attr_name(cp, ep - cp)) { - fprintf(stderr, - "%.*s is not a valid attribute name: %s:%d\n", - (int)(ep - cp), cp, - src, lineno); - return NULL; - } - } else { - struct attr_state *e; - - e = &(res->state[num_attr]); - if (*cp == '-') { - e->unset = 1; - cp++; - } - e->attr = git_attr(cp, ep - cp); - } - num_attr++; - cp = ep + strspn(ep, blank); - } + while (*cp) + cp = parse_attr(src, lineno, cp, &num_attr, res); if (pass) break; - res = xcalloc(1, sizeof(*res) + sizeof(struct attr_state) * num_attr + (is_macro ? 0 : namelen + 1)); - if (is_macro) { + if (is_macro) res->u.attr = git_attr(name, namelen); - } else { res->u.pattern = (char*)&(res->state[num_attr]); memcpy(res->u.pattern, name, namelen); @@ -205,9 +234,9 @@ static struct match_attr *parse_attr_line(const char *line, const char *src, * come from many places. * * (1) .gitattribute file of the same directory; - * (2) .gitattribute file of the parent directory if (1) does not have any match; - * this goes recursively upwards, just like .gitignore - * (3) perhaps $GIT_DIR/info/attributes, as the final fallback. + * (2) .gitattribute file of the parent directory if (1) does not have + * any match; this goes recursively upwards, just like .gitignore. + * (3) $GIT_DIR/info/attributes, which overrides both of the above. * * In the same file, later entries override the earlier match, so in the * global list, we would have entries from info/attributes the earliest @@ -229,8 +258,21 @@ static void free_attr_elem(struct attr_stack *e) { int i; free(e->origin); - for (i = 0; i < e->num_matches; i++) - free(e->attrs[i]); + for (i = 0; i < e->num_matches; i++) { + struct match_attr *a = e->attrs[i]; + int j; + for (j = 0; j < a->num_attr; j++) { + void *setto = a->state[j].setto; + if (setto == ATTR__TRUE || + setto == ATTR__FALSE || + setto == ATTR__UNSET || + setto == ATTR__UNKNOWN) + ; + else + free(setto); + } + free(a); + } free(e); } @@ -288,10 +330,19 @@ static void debug_info(const char *what, struct attr_stack *elem) { fprintf(stderr, "%s: %s\n", what, elem->origin ? elem->origin : "()"); } -static void debug_set(const char *what, const char *match, struct git_attr *attr, int set) +static void debug_set(const char *what, const char *match, struct git_attr *attr, void *v) { - fprintf(stderr, "%s: %s => %d (%s)\n", - what, attr->name, set, match); + const char *value = v; + + if (ATTR_TRUE(value)) + value = "set"; + else if (ATTR_FALSE(value)) + value = "unset"; + else if (ATTR_UNSET(value)) + value = "unspecified"; + + fprintf(stderr, "%s: %s => %s (%s)\n", + what, attr->name, (char *) value, match); } #define debug_push(a) debug_info("push", (a)) #define debug_pop(a) debug_info("pop", (a)) @@ -420,56 +471,53 @@ static int path_matches(const char *pathname, int pathlen, return fnmatch(pattern, pathname + baselen, FNM_PATHNAME) == 0; } +static int fill_one(const char *what, struct match_attr *a, int rem) +{ + struct git_attr_check *check = check_all_attr; + int i; + + for (i = 0; 0 < rem && i < a->num_attr; i++) { + struct git_attr *attr = a->state[i].attr; + void **n = &(check[attr->attr_nr].value); + void *v = a->state[i].setto; + + if (*n == ATTR__UNKNOWN) { + debug_set(what, a->u.pattern, attr, v); + *n = v; + rem--; + } + } + return rem; +} + static int fill(const char *path, int pathlen, struct attr_stack *stk, int rem) { + int i; const char *base = stk->origin ? stk->origin : ""; - int i, j; - struct git_attr_check *check = check_all_attr; for (i = stk->num_matches - 1; 0 < rem && 0 <= i; i--) { struct match_attr *a = stk->attrs[i]; if (a->is_macro) continue; if (path_matches(path, pathlen, - a->u.pattern, base, strlen(base))) { - for (j = 0; 0 < rem && j < a->num_attr; j++) { - struct git_attr *attr = a->state[j].attr; - int set = !a->state[j].unset; - int *n = &(check[attr->attr_nr].isset); - - if (*n < 0) { - debug_set("fill", a->u.pattern, attr, set); - *n = set; - rem--; - } - } - } + a->u.pattern, base, strlen(base))) + rem = fill_one("fill", a, rem); } return rem; } static int macroexpand(struct attr_stack *stk, int rem) { - int i, j; + int i; struct git_attr_check *check = check_all_attr; for (i = stk->num_matches - 1; 0 < rem && 0 <= i; i--) { struct match_attr *a = stk->attrs[i]; if (!a->is_macro) continue; - if (check[a->u.attr->attr_nr].isset < 0) + if (check[a->u.attr->attr_nr].value != ATTR__TRUE) continue; - for (j = 0; 0 < rem && j < a->num_attr; j++) { - struct git_attr *attr = a->state[j].attr; - int set = !a->state[j].unset; - int *n = &(check[attr->attr_nr].isset); - - if (*n < 0) { - debug_set("expand", a->u.attr->name, attr, set); - *n = set; - rem--; - } - } + rem = fill_one("expand", a, rem); } return rem; } @@ -482,7 +530,7 @@ int git_checkattr(const char *path, int num, struct git_attr_check *check) bootstrap_attr_stack(); for (i = 0; i < attr_nr; i++) - check_all_attr[i].isset = -1; + check_all_attr[i].value = ATTR__UNKNOWN; pathlen = strlen(path); cp = strrchr(path, '/'); @@ -498,8 +546,12 @@ int git_checkattr(const char *path, int num, struct git_attr_check *check) for (stk = attr_stack; 0 < rem && stk; stk = stk->prev) rem = macroexpand(stk, rem); - for (i = 0; i < num; i++) - check[i].isset = check_all_attr[check[i].attr->attr_nr].isset; + for (i = 0; i < num; i++) { + void *value = check_all_attr[check[i].attr->attr_nr].value; + if (value == ATTR__UNKNOWN) + value = ATTR__UNSET; + check[i].value = value; + } return 0; } diff --git a/attr.h b/attr.h index 1e5ab40694..8ec2d3d35c 100644 --- a/attr.h +++ b/attr.h @@ -6,9 +6,19 @@ struct git_attr; struct git_attr *git_attr(const char *, int); +/* Internal use */ +#define ATTR__TRUE ((void *) 1) +#define ATTR__FALSE ((void *) 0) +#define ATTR__UNSET ((void *) -1) + +/* For public to check git_attr_check results */ +#define ATTR_TRUE(v) ((v) == ATTR__TRUE) +#define ATTR_FALSE(v) ((v) == ATTR__FALSE) +#define ATTR_UNSET(v) ((v) == ATTR__UNSET) + struct git_attr_check { struct git_attr *attr; - int isset; + void *value; }; int git_checkattr(const char *path, int, struct git_attr_check *); diff --git a/builtin-check-attr.c b/builtin-check-attr.c index 634be9ed2e..6983a73c1b 100644 --- a/builtin-check-attr.c +++ b/builtin-check-attr.c @@ -42,11 +42,17 @@ int cmd_check_attr(int argc, const char **argv, const char *prefix) if (git_checkattr(argv[i], cnt, check)) die("git_checkattr died"); for (j = 0; j < cnt; j++) { + void *value = check[j].value; + + if (ATTR_TRUE(value)) + value = "set"; + else if (ATTR_FALSE(value)) + value = "unset"; + else if (ATTR_UNSET(value)) + value = "unspecified"; + write_name_quoted("", 0, argv[i], 1, stdout); - printf(": %s: %s\n", argv[j+1], - (check[j].isset < 0) ? "unspecified" : - (check[j].isset == 0) ? "unset" : - "set"); + printf(": %s: %s\n", argv[j+1], (char *) value); } } return 0; diff --git a/convert.c b/convert.c index d0d4b81871..68bb70f12f 100644 --- a/convert.c +++ b/convert.c @@ -225,9 +225,19 @@ static int git_path_check_crlf(const char *path) setup_crlf_check(&attr_crlf_check); - if (git_checkattr(path, 1, &attr_crlf_check)) - return -1; - return attr_crlf_check.isset; + if (!git_checkattr(path, 1, &attr_crlf_check)) { + void *value = attr_crlf_check.value; + if (ATTR_TRUE(value)) + return 1; + else if (ATTR_FALSE(value)) + return 0; + else if (ATTR_UNSET(value)) + ; + else + die("unknown value %s given to 'crlf' attribute", + (char *)value); + } + return -1; } int convert_to_git(const char *path, char **bufp, unsigned long *sizep) diff --git a/diff.c b/diff.c index dcea405ed3..a32078ea01 100644 --- a/diff.c +++ b/diff.c @@ -1068,9 +1068,18 @@ static int file_is_binary(struct diff_filespec *one) struct git_attr_check attr_diff_check; setup_diff_attr_check(&attr_diff_check); - if (!git_checkattr(one->path, 1, &attr_diff_check) && - (0 <= attr_diff_check.isset)) - return !attr_diff_check.isset; + if (!git_checkattr(one->path, 1, &attr_diff_check)) { + void *value = attr_diff_check.value; + if (ATTR_TRUE(value)) + return 0; + else if (ATTR_FALSE(value)) + return 1; + else if (ATTR_UNSET(value)) + ; + else + die("unknown value %s given to 'diff' attribute", + (char *)value); + } if (!one->data) { if (!DIFF_FILE_VALID(one)) -- cgit 1.2.3-korg From a5e92abde61d59a8612c5b87d0bae681e90f7fdb Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 18 Apr 2007 16:16:37 -0700 Subject: 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 --- attr.c | 20 +++++++++++++------- attr.h | 22 +++++++++++++++------- builtin-check-attr.c | 4 ++-- convert.c | 2 +- diff.c | 4 ++-- merge-recursive.c | 17 ++++++++++------- 6 files changed, 43 insertions(+), 26 deletions(-) (limited to 'diff.c') diff --git a/attr.c b/attr.c index b3496a6eb5..285e689e5a 100644 --- a/attr.c +++ b/attr.c @@ -1,7 +1,13 @@ #include "cache.h" #include "attr.h" -#define ATTR__UNKNOWN ((void *) -2) +const char git_attr__true[] = "(builtin)true"; +const char git_attr__false[] = "\0(builtin)false"; +static const char git_attr__unknown[] = "(builtin)unknown"; +#define ATTR__TRUE git_attr__true +#define ATTR__FALSE git_attr__false +#define ATTR__UNSET NULL +#define ATTR__UNKNOWN git_attr__unknown /* * 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) /* What does a matched pattern decide? */ struct attr_state { struct git_attr *attr; - void *setto; + const char *setto; }; struct match_attr { @@ -262,14 +268,14 @@ static void free_attr_elem(struct attr_stack *e) struct match_attr *a = e->attrs[i]; int j; for (j = 0; j < a->num_attr; j++) { - void *setto = a->state[j].setto; + const char *setto = a->state[j].setto; if (setto == ATTR__TRUE || setto == ATTR__FALSE || setto == ATTR__UNSET || setto == ATTR__UNKNOWN) ; else - free(setto); + free((char*) setto); } free(a); } @@ -478,8 +484,8 @@ static int fill_one(const char *what, struct match_attr *a, int rem) for (i = 0; 0 < rem && i < a->num_attr; i++) { struct git_attr *attr = a->state[i].attr; - void **n = &(check[attr->attr_nr].value); - void *v = a->state[i].setto; + const char **n = &(check[attr->attr_nr].value); + const char *v = a->state[i].setto; if (*n == ATTR__UNKNOWN) { 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) rem = macroexpand(stk, rem); for (i = 0; i < num; i++) { - void *value = check_all_attr[check[i].attr->attr_nr].value; + const char *value = check_all_attr[check[i].attr->attr_nr].value; if (value == ATTR__UNKNOWN) value = ATTR__UNSET; check[i].value = value; diff --git a/attr.h b/attr.h index 8ec2d3d35c..f1c2038b09 100644 --- a/attr.h +++ b/attr.h @@ -4,21 +4,29 @@ /* An attribute is a pointer to this opaque structure */ struct git_attr; +/* + * Given a string, return the gitattribute object that + * corresponds to it. + */ struct git_attr *git_attr(const char *, int); /* Internal use */ -#define ATTR__TRUE ((void *) 1) -#define ATTR__FALSE ((void *) 0) -#define ATTR__UNSET ((void *) -1) +extern const char git_attr__true[]; +extern const char git_attr__false[]; /* For public to check git_attr_check results */ -#define ATTR_TRUE(v) ((v) == ATTR__TRUE) -#define ATTR_FALSE(v) ((v) == ATTR__FALSE) -#define ATTR_UNSET(v) ((v) == ATTR__UNSET) +#define ATTR_TRUE(v) ((v) == git_attr__true) +#define ATTR_FALSE(v) ((v) == git_attr__false) +#define ATTR_UNSET(v) ((v) == NULL) +/* + * Send one or more git_attr_check to git_checkattr(), and + * each 'value' member tells what its value is. + * Unset one is returned as NULL. + */ struct git_attr_check { struct git_attr *attr; - void *value; + const char *value; }; int git_checkattr(const char *path, int, struct git_attr_check *); diff --git a/builtin-check-attr.c b/builtin-check-attr.c index 6983a73c1b..9d77f76ff1 100644 --- a/builtin-check-attr.c +++ b/builtin-check-attr.c @@ -42,7 +42,7 @@ int cmd_check_attr(int argc, const char **argv, const char *prefix) if (git_checkattr(argv[i], cnt, check)) die("git_checkattr died"); for (j = 0; j < cnt; j++) { - void *value = check[j].value; + const char *value = check[j].value; if (ATTR_TRUE(value)) value = "set"; @@ -52,7 +52,7 @@ int cmd_check_attr(int argc, const char **argv, const char *prefix) value = "unspecified"; write_name_quoted("", 0, argv[i], 1, stdout); - printf(": %s: %s\n", argv[j+1], (char *) value); + printf(": %s: %s\n", argv[j+1], value); } } return 0; diff --git a/convert.c b/convert.c index 68bb70f12f..a5f60c7c6b 100644 --- a/convert.c +++ b/convert.c @@ -226,7 +226,7 @@ static int git_path_check_crlf(const char *path) setup_crlf_check(&attr_crlf_check); if (!git_checkattr(path, 1, &attr_crlf_check)) { - void *value = attr_crlf_check.value; + const char *value = attr_crlf_check.value; if (ATTR_TRUE(value)) return 1; else if (ATTR_FALSE(value)) diff --git a/diff.c b/diff.c index a32078ea01..5f501864e6 100644 --- a/diff.c +++ b/diff.c @@ -1069,7 +1069,7 @@ static int file_is_binary(struct diff_filespec *one) setup_diff_attr_check(&attr_diff_check); if (!git_checkattr(one->path, 1, &attr_diff_check)) { - void *value = attr_diff_check.value; + const char *value = attr_diff_check.value; if (ATTR_TRUE(value)) return 0; else if (ATTR_FALSE(value)) @@ -1078,7 +1078,7 @@ static int file_is_binary(struct diff_filespec *one) ; else die("unknown value %s given to 'diff' attribute", - (char *)value); + value); } if (!one->data) { diff --git a/merge-recursive.c b/merge-recursive.c index 7b5ca8e717..ec8438b463 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -953,7 +953,7 @@ static void initialize_ll_merge(void) git_config(read_merge_config); } -static const struct ll_merge_driver *find_ll_merge_driver(void *merge_attr) +static const struct ll_merge_driver *find_ll_merge_driver(const char *merge_attr) { struct ll_merge_driver *fn; const char *name; @@ -986,7 +986,7 @@ static const struct ll_merge_driver *find_ll_merge_driver(void *merge_attr) return &ll_merge_drv[LL_TEXT_MERGE]; } -static void *git_path_check_merge(const char *path) +static const char *git_path_check_merge(const char *path) { static struct git_attr_check attr_merge_check; @@ -994,7 +994,7 @@ static void *git_path_check_merge(const char *path) attr_merge_check.attr = git_attr("merge", 5); if (git_checkattr(path, 1, &attr_merge_check)) - return ATTR__UNSET; + return NULL; return attr_merge_check.value; } @@ -1008,7 +1008,7 @@ static int ll_merge(mmbuffer_t *result_buf, mmfile_t orig, src1, src2; char *name1, *name2; int merge_status; - void *merge_attr; + const char *ll_driver_name; const struct ll_merge_driver *driver; name1 = xstrdup(mkpath("%s:%s", branch1, a->path)); @@ -1018,11 +1018,14 @@ static int ll_merge(mmbuffer_t *result_buf, fill_mm(a->sha1, &src1); fill_mm(b->sha1, &src2); - merge_attr = git_path_check_merge(a->path); - driver = find_ll_merge_driver(merge_attr); + ll_driver_name = git_path_check_merge(a->path); + driver = find_ll_merge_driver(ll_driver_name); if (index_only && driver->recursive) { - merge_attr = git_attr(driver->recursive, strlen(driver->recursive)); + void *merge_attr; + + ll_driver_name = driver->recursive; + merge_attr = git_attr(ll_driver_name, strlen(ll_driver_name)); driver = find_ll_merge_driver(merge_attr); } merge_status = driver->fn(driver, a->path, -- cgit 1.2.3-korg From ac78e548049f4e86b38368d2c4b4dbb546c64ac6 Mon Sep 17 00:00:00 2001 From: Alex Riesen Date: Thu, 19 Apr 2007 02:05:03 +0200 Subject: Simplify calling of CR/LF conversion routines Signed-off-by: Alex Riesen Signed-off-by: Junio C Hamano --- builtin-apply.c | 18 ++++++--------- cache.h | 4 ++-- convert.c | 71 ++++++++++++++++++++++++++++----------------------------- diff.c | 4 ++-- entry.c | 7 ++---- sha1_file.c | 7 +++--- 6 files changed, 51 insertions(+), 60 deletions(-) (limited to 'diff.c') diff --git a/builtin-apply.c b/builtin-apply.c index fd92ef7174..ccd342c1c4 100644 --- a/builtin-apply.c +++ b/builtin-apply.c @@ -1475,8 +1475,8 @@ static int read_old_data(struct stat *st, const char *path, char **buf_p, unsign } close(fd); nsize = got; - nbuf = buf; - if (convert_to_git(path, &nbuf, &nsize)) { + nbuf = convert_to_git(path, buf, &nsize); + if (nbuf) { free(buf); *buf_p = nbuf; *alloc_p = nsize; @@ -2355,9 +2355,8 @@ static void add_index_file(const char *path, unsigned mode, void *buf, unsigned static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size) { - int fd, converted; + int fd; char *nbuf; - unsigned long nsize; if (has_symlinks && S_ISLNK(mode)) /* Although buf:size is counted string, it also is NUL @@ -2369,13 +2368,10 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf, if (fd < 0) return -1; - nsize = size; - nbuf = (char *) buf; - converted = convert_to_working_tree(path, &nbuf, &nsize); - if (converted) { + nbuf = convert_to_working_tree(path, buf, &size); + if (nbuf) buf = nbuf; - size = nsize; - } + while (size) { int written = xwrite(fd, buf, size); if (written < 0) @@ -2387,7 +2383,7 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf, } if (close(fd) < 0) die("closing file %s: %s", path, strerror(errno)); - if (converted) + if (nbuf) free(nbuf); return 0; } diff --git a/cache.h b/cache.h index 38ad00661d..8c804cb6ee 100644 --- a/cache.h +++ b/cache.h @@ -496,8 +496,8 @@ extern void trace_printf(const char *format, ...); extern void trace_argv_printf(const char **argv, int count, const char *format, ...); /* convert.c */ -extern int convert_to_git(const char *path, char **bufp, unsigned long *sizep); -extern int convert_to_working_tree(const char *path, char **bufp, unsigned long *sizep); +extern char *convert_to_git(const char *path, const char *src, unsigned long *sizep); +extern char *convert_to_working_tree(const char *path, const char *src, unsigned long *sizep); /* match-trees.c */ void shift_tree(const unsigned char *, const unsigned char *, unsigned char *, int); diff --git a/convert.c b/convert.c index da64253a16..742b895cfa 100644 --- a/convert.c +++ b/convert.c @@ -79,25 +79,24 @@ static int is_binary(unsigned long size, struct text_stat *stats) return 0; } -static int crlf_to_git(const char *path, char **bufp, unsigned long *sizep, int action) +static char *crlf_to_git(const char *path, const char *src, unsigned long *sizep, int action) { - char *buffer, *nbuf; + char *buffer, *dst; unsigned long size, nsize; struct text_stat stats; if ((action == CRLF_BINARY) || (action == CRLF_GUESS && !auto_crlf)) - return 0; + return NULL; size = *sizep; if (!size) - return 0; - buffer = *bufp; + return NULL; - gather_stats(buffer, size, &stats); + gather_stats(src, size, &stats); /* No CR? Nothing to convert, regardless. */ if (!stats.cr) - return 0; + return NULL; if (action == CRLF_GUESS) { /* @@ -106,13 +105,13 @@ static int crlf_to_git(const char *path, char **bufp, unsigned long *sizep, int * stuff? */ if (stats.cr != stats.crlf) - return 0; + return NULL; /* * And add some heuristics for binary vs text, of course... */ if (is_binary(size, &stats)) - return 0; + return NULL; } /* @@ -120,10 +119,10 @@ static int crlf_to_git(const char *path, char **bufp, unsigned long *sizep, int * to let the caller know that we switched buffers on it. */ nsize = size - stats.crlf; - nbuf = xmalloc(nsize); - *bufp = nbuf; + buffer = xmalloc(nsize); *sizep = nsize; + dst = buffer; if (action == CRLF_GUESS) { /* * If we guessed, we already know we rejected a file with @@ -131,54 +130,53 @@ static int crlf_to_git(const char *path, char **bufp, unsigned long *sizep, int * follow it. */ do { - unsigned char c = *buffer++; + unsigned char c = *src++; if (c != '\r') - *nbuf++ = c; + *dst++ = c; } while (--size); } else { do { - unsigned char c = *buffer++; + unsigned char c = *src++; if (! (c == '\r' && (1 < size && *buffer == '\n'))) - *nbuf++ = c; + *dst++ = c; } while (--size); } - return 1; + return buffer; } -static int crlf_to_worktree(const char *path, char **bufp, unsigned long *sizep, int action) +static char *crlf_to_worktree(const char *path, const char *src, unsigned long *sizep, int action) { - char *buffer, *nbuf; + char *buffer, *dst; unsigned long size, nsize; struct text_stat stats; unsigned char last; if ((action == CRLF_BINARY) || (action == CRLF_INPUT) || (action == CRLF_GUESS && auto_crlf <= 0)) - return 0; + return NULL; size = *sizep; if (!size) - return 0; - buffer = *bufp; + return NULL; - gather_stats(buffer, size, &stats); + gather_stats(src, size, &stats); /* No LF? Nothing to convert, regardless. */ if (!stats.lf) - return 0; + return NULL; /* Was it already in CRLF format? */ if (stats.lf == stats.crlf) - return 0; + return NULL; if (action == CRLF_GUESS) { /* If we have any bare CR characters, we're not going to touch it */ if (stats.cr != stats.crlf) - return 0; + return NULL; if (is_binary(size, &stats)) - return 0; + return NULL; } /* @@ -186,19 +184,20 @@ static int crlf_to_worktree(const char *path, char **bufp, unsigned long *sizep, * to let the caller know that we switched buffers on it. */ nsize = size + stats.lf - stats.crlf; - nbuf = xmalloc(nsize); - *bufp = nbuf; + buffer = xmalloc(nsize); *sizep = nsize; last = 0; + + dst = buffer; do { - unsigned char c = *buffer++; + unsigned char c = *src++; if (c == '\n' && last != '\r') - *nbuf++ = '\r'; - *nbuf++ = c; + *dst++ = '\r'; + *dst++ = c; last = c; } while (--size); - return 1; + return buffer; } static void setup_crlf_check(struct git_attr_check *check) @@ -231,12 +230,12 @@ static int git_path_check_crlf(const char *path) return CRLF_GUESS; } -int convert_to_git(const char *path, char **bufp, unsigned long *sizep) +char *convert_to_git(const char *path, const char *src, unsigned long *sizep) { - return crlf_to_git(path, bufp, sizep, git_path_check_crlf(path)); + return crlf_to_git(path, src, sizep, git_path_check_crlf(path)); } -int convert_to_working_tree(const char *path, char **bufp, unsigned long *sizep) +char *convert_to_working_tree(const char *path, const char *src, unsigned long *sizep) { - return crlf_to_worktree(path, bufp, sizep, git_path_check_crlf(path)); + return crlf_to_worktree(path, src, sizep, git_path_check_crlf(path)); } diff --git a/diff.c b/diff.c index 5f501864e6..1cb1230a99 100644 --- a/diff.c +++ b/diff.c @@ -1493,9 +1493,9 @@ int diff_populate_filespec(struct diff_filespec *s, int size_only) /* * Convert from working tree format to canonical git format */ - buf = s->data; size = s->size; - if (convert_to_git(s->path, &buf, &size)) { + buf = convert_to_git(s->path, s->data, &size); + if (buf) { munmap(s->data, s->size); s->should_munmap = 0; s->data = buf; diff --git a/entry.c b/entry.c index d72f811580..3771209f19 100644 --- a/entry.c +++ b/entry.c @@ -79,7 +79,6 @@ static int write_entry(struct cache_entry *ce, char *path, struct checkout *stat } switch (ntohl(ce->ce_mode) & S_IFMT) { char *buf; - unsigned long nsize; case S_IFREG: if (to_tempfile) { @@ -96,12 +95,10 @@ static int write_entry(struct cache_entry *ce, char *path, struct checkout *stat /* * Convert from git internal format to working tree format */ - buf = new; - nsize = size; - if (convert_to_working_tree(ce->name, &buf, &nsize)) { + buf = convert_to_working_tree(ce->name, new, &size); + if (buf) { free(new); new = buf; - size = nsize; } wrote = write_in_full(fd, new, size); diff --git a/sha1_file.c b/sha1_file.c index 4304fe9bbc..1978d5f14e 100644 --- a/sha1_file.c +++ b/sha1_file.c @@ -2277,10 +2277,9 @@ int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, */ if ((type == OBJ_BLOB) && S_ISREG(st->st_mode)) { unsigned long nsize = size; - char *nbuf = buf; - if (convert_to_git(path, &nbuf, &nsize)) { - if (size) - munmap(buf, size); + char *nbuf = convert_to_git(path, buf, &nsize); + if (nbuf) { + munmap(buf, size); size = nsize; buf = nbuf; re_allocated = 1; -- cgit 1.2.3-korg