From d0bfd026a8241d544c339944976927b388d61a5e Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Thu, 12 Apr 2007 01:07:32 -0700 Subject: Add basic infrastructure to assign attributes to paths This adds the basic infrastructure to assign attributes to paths, in a way similar to what the exclusion mechanism does based on $GIT_DIR/info/exclude and .gitignore files. An attribute is just a simple string that does not contain any whitespace. They can be specified in $GIT_DIR/info/attributes file, and .gitattributes file in each directory. Each line in these files defines a pattern matching rule. Similar to the exclusion mechanism, a later match overrides an earlier match in the same file, and entries from .gitattributes file in the same directory takes precedence over the ones from parent directories. Lines in $GIT_DIR/info/attributes file are used as the lowest precedence default rules. A line is either a comment (an empty line, or a line that begins with a '#'), or a rule, which is a whitespace separated list of tokens. The first token on the line is a shell glob pattern. The rest are names of attributes, each of which can optionally be prefixed with '!'. Such a line means "if a path matches this glob, this attribute is set (or unset -- if the attribute name is prefixed with '!'). For glob matching, the same "if the pattern does not have a slash in it, the basename of the path is matched with fnmatch(3) against the pattern, otherwise, the path is matched with the pattern with FNM_PATHNAME" rule as the exclusion mechanism is used. This does not define what an attribute means. Tying an attribute to various effects it has on git operation for paths that have it will be specified separately. Signed-off-by: Junio C Hamano diff --git a/.gitignore b/.gitignore index 9229e91..d96f4f0 100644 --- a/.gitignore +++ b/.gitignore @@ -16,6 +16,7 @@ git-blame git-branch git-bundle git-cat-file +git-check-attr git-check-ref-format git-checkout git-checkout-index diff --git a/Makefile b/Makefile index b8e6030..ac89d1b 100644 --- a/Makefile +++ b/Makefile @@ -283,7 +283,7 @@ LIB_H = \ diff.h object.h pack.h pkt-line.h quote.h refs.h list-objects.h sideband.h \ run-command.h strbuf.h tag.h tree.h git-compat-util.h revision.h \ tree-walk.h log-tree.h dir.h path-list.h unpack-trees.h builtin.h \ - utf8.h reflog-walk.h patch-ids.h + utf8.h reflog-walk.h attr.h DIFF_OBJS = \ diff.o diff-lib.o diffcore-break.o diffcore-order.o \ @@ -305,7 +305,7 @@ LIB_OBJS = \ write_or_die.o trace.o list-objects.o grep.o match-trees.o \ alloc.o merge-file.o path-list.o help.o unpack-trees.o $(DIFF_OBJS) \ color.o wt-status.o archive-zip.o archive-tar.o shallow.o utf8.o \ - convert.o + convert.o attr.o BUILTIN_OBJS = \ builtin-add.o \ @@ -316,6 +316,7 @@ BUILTIN_OBJS = \ builtin-branch.o \ builtin-bundle.o \ builtin-cat-file.o \ + builtin-check-attr.o \ builtin-checkout-index.o \ builtin-check-ref-format.o \ builtin-commit-tree.o \ diff --git a/attr.c b/attr.c new file mode 100644 index 0000000..7435d92 --- /dev/null +++ b/attr.c @@ -0,0 +1,380 @@ +#include "cache.h" +#include "attr.h" + +/* + * The basic design decision here is that we are not going to have + * insanely large number of attributes. + * + * This is a randomly chosen prime. + */ +#define HASHSIZE 257 + +#ifndef DEBUG_ATTR +#define DEBUG_ATTR 0 +#endif + +struct git_attr { + struct git_attr *next; + unsigned h; + char name[FLEX_ARRAY]; +}; + +static struct git_attr *(git_attr_hash[HASHSIZE]); + +static unsigned hash_name(const char *name, int namelen) +{ + unsigned val = 0; + unsigned char c; + + while (namelen--) { + c = *name++; + val = ((val << 7) | (val >> 22)) ^ c; + } + return val; +} + +struct git_attr *git_attr(const char *name, int len) +{ + unsigned hval = hash_name(name, len); + unsigned pos = hval % HASHSIZE; + struct git_attr *a; + + for (a = git_attr_hash[pos]; a; a = a->next) { + if (a->h == hval && + !memcmp(a->name, name, len) && !a->name[len]) + return a; + } + + a = xmalloc(sizeof(*a) + len + 1); + memcpy(a->name, name, len); + a->name[len] = 0; + a->h = hval; + a->next = git_attr_hash[pos]; + git_attr_hash[pos] = a; + return a; +} + +/* + * .gitattributes file is one line per record, each of which is + * + * (1) glob pattern. + * (2) whitespace + * (3) whitespace separated list of attribute names, each of which + * could be prefixed with '!' to mean "not set". + */ + +struct attr_state { + int unset; + struct git_attr *attr; +}; + +struct match_attr { + char *pattern; + unsigned num_attr; + struct attr_state state[FLEX_ARRAY]; +}; + +static const char blank[] = " \t\r\n"; + +static struct match_attr *parse_attr_line(const char *line) +{ + int namelen; + int num_attr; + const char *cp, *name; + struct match_attr *res = res; + int pass; + + cp = line + strspn(line, blank); + if (!*cp || *cp == '#') + return NULL; + name = cp; + namelen = strcspn(name, blank); + + for (pass = 0; pass < 2; pass++) { + /* pass 0 counts and allocates, pass 1 fills */ + num_attr = 0; + cp = name + namelen; + cp = cp + strspn(cp, blank); + while (*cp) { + const char *ep; + ep = cp + strcspn(cp, blank); + if (pass) { + 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); + } + if (pass) + break; + res = xcalloc(1, + sizeof(*res) + + sizeof(struct attr_state) * num_attr + + namelen + 1); + res->pattern = (char*)&(res->state[num_attr]); + memcpy(res->pattern, name, namelen); + res->pattern[namelen] = 0; + res->num_attr = num_attr; + } + return res; +} + +/* + * Like info/exclude and .gitignore, the attribute information can + * 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. + * + * In the same file, later entries override the earlier match, so in the + * global list, we would have entries from info/attributes the earliest + * (reading the file from top to bottom), .gitattribute of the root + * directory (again, reading the file from top to bottom) down to the + * current directory, and then scan the list backwards to find the first match. + * This is exactly the same as what excluded() does in dir.c to deal with + * .gitignore + */ + +static struct attr_stack { + struct attr_stack *prev; + char *origin; + unsigned num_matches; + struct match_attr **attrs; +} *attr_stack; + +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]); + free(e); +} + +static const char *builtin_attr[] = { + NULL, +}; + +static struct attr_stack *read_attr_from_array(const char **list) +{ + struct attr_stack *res; + const char *line; + + res = xcalloc(1, sizeof(*res)); + while ((line = *(list++)) != NULL) { + struct match_attr *a = parse_attr_line(line); + if (!a) + continue; + res->attrs = xrealloc(res->attrs, res->num_matches + 1); + res->attrs[res->num_matches++] = a; + } + return res; +} + +static struct attr_stack *read_attr_from_file(const char *path) +{ + FILE *fp; + struct attr_stack *res; + char buf[2048]; + + res = xcalloc(1, sizeof(*res)); + fp = fopen(path, "r"); + if (!fp) + return res; + + while (fgets(buf, sizeof(buf), fp)) { + struct match_attr *a = parse_attr_line(buf); + if (!a) + continue; + res->attrs = xrealloc(res->attrs, res->num_matches + 1); + res->attrs[res->num_matches++] = a; + } + fclose(fp); + return res; +} + +#if DEBUG_ATTR +static void debug_info(const char *what, struct attr_stack *elem) +{ + fprintf(stderr, "%s: %s\n", what, elem->origin ? elem->origin : "()"); +} +#define debug_push(a) debug_info("push", (a)) +#define debug_pop(a) debug_info("pop", (a)) +#else +#define debug_push(a) do { ; } while (0) +#define debug_pop(a) do { ; } while (0) +#endif + +static void prepare_attr_stack(const char *path, int dirlen) +{ + struct attr_stack *elem, *info; + int len; + char pathbuf[PATH_MAX]; + + /* + * At the bottom of the attribute stack is the built-in + * set of attribute definitions. Then, contents from + * .gitattribute files from directories closer to the + * root to the ones in deeper directories are pushed + * to the stack. Finally, at the very top of the stack + * we always keep the contents of $GIT_DIR/info/attributes. + * + * When checking, we use entries from near the top of the + * stack, preferring $GIT_DIR/info/attributes, then + * .gitattributes in deeper directories to shallower ones, + * and finally use the built-in set as the default. + */ + if (!attr_stack) { + elem = read_attr_from_array(builtin_attr); + elem->origin = NULL; + elem->prev = attr_stack; + attr_stack = elem; + + elem = read_attr_from_file(GITATTRIBUTES_FILE); + elem->origin = strdup(""); + elem->prev = attr_stack; + attr_stack = elem; + debug_push(elem); + + elem = read_attr_from_file(git_path(INFOATTRIBUTES_FILE)); + elem->origin = NULL; + elem->prev = attr_stack; + attr_stack = elem; + } + + /* + * Pop the "info" one that is always at the top of the stack. + */ + info = attr_stack; + attr_stack = info->prev; + + /* + * Pop the ones from directories that are not the prefix of + * the path we are checking. + */ + while (attr_stack && attr_stack->origin) { + int namelen = strlen(attr_stack->origin); + + elem = attr_stack; + if (namelen <= dirlen && + !strncmp(elem->origin, path, namelen)) + break; + + debug_pop(elem); + attr_stack = elem->prev; + free_attr_elem(elem); + } + + /* + * Read from parent directories and push them down + */ + while (1) { + char *cp; + + len = strlen(attr_stack->origin); + if (dirlen <= len) + break; + memcpy(pathbuf, path, dirlen); + memcpy(pathbuf + dirlen, "/", 2); + cp = strchr(pathbuf + len + 1, '/'); + strcpy(cp + 1, GITATTRIBUTES_FILE); + elem = read_attr_from_file(pathbuf); + *cp = '\0'; + elem->origin = strdup(pathbuf); + elem->prev = attr_stack; + attr_stack = elem; + debug_push(elem); + } + + /* + * Finally push the "info" one at the top of the stack. + */ + info->prev = attr_stack; + attr_stack = info; +} + +static int path_matches(const char *pathname, int pathlen, + const char *pattern, + const char *base, int baselen) +{ + if (!strchr(pattern, '/')) { + /* match basename */ + const char *basename = strrchr(pathname, '/'); + basename = basename ? basename + 1 : pathname; + return (fnmatch(pattern, basename, 0) == 0); + } + /* + * match with FNM_PATHNAME; the pattern has base implicitly + * in front of it. + */ + if (*pattern == '/') + pattern++; + if (pathlen < baselen || + (baselen && pathname[baselen - 1] != '/') || + strncmp(pathname, base, baselen)) + return 0; + return fnmatch(pattern, pathname + baselen, FNM_PATHNAME) == 0; +} + +/* + * I do not like this at all. Only because we allow individual + * attribute to be set or unset incrementally by individual + * lines in .gitattribute files, we need to do this triple + * loop which looks quite wasteful. + */ +static int fill(const char *path, int pathlen, + struct attr_stack *stk, struct git_attr_check *check, + int num, int rem) +{ + int i, j, k; + const char *base = stk->origin ? stk->origin : ""; + + for (i = stk->num_matches - 1; 0 < rem && 0 <= i; i--) { + struct match_attr *a = stk->attrs[i]; + if (path_matches(path, pathlen, + a->pattern, base, strlen(base))) { + for (j = 0; j < a->num_attr; j++) { + struct git_attr *attr = a->state[j].attr; + int set = !a->state[j].unset; + for (k = 0; k < num; k++) { + if (0 <= check[k].isset || + check[k].attr != attr) + continue; + check[k].isset = set; + rem--; + } + } + } + } + return rem; +} + +int git_checkattr(const char *path, int num, struct git_attr_check *check) +{ + struct attr_stack *stk; + const char *cp; + int dirlen, pathlen, i, rem; + + for (i = 0; i < num; i++) + check[i].isset = -1; + + pathlen = strlen(path); + cp = strrchr(path, '/'); + if (!cp) + dirlen = 0; + else + dirlen = cp - path; + prepare_attr_stack(path, dirlen); + rem = num; + for (stk = attr_stack; 0 < rem && stk; stk = stk->prev) + rem = fill(path, pathlen, stk, check, num, rem); + return 0; +} diff --git a/attr.h b/attr.h new file mode 100644 index 0000000..1e5ab40 --- /dev/null +++ b/attr.h @@ -0,0 +1,16 @@ +#ifndef ATTR_H +#define ATTR_H + +/* An attribute is a pointer to this opaque structure */ +struct git_attr; + +struct git_attr *git_attr(const char *, int); + +struct git_attr_check { + struct git_attr *attr; + int isset; +}; + +int git_checkattr(const char *path, int, struct git_attr_check *); + +#endif /* ATTR_H */ diff --git a/builtin-check-attr.c b/builtin-check-attr.c new file mode 100644 index 0000000..47b0721 --- /dev/null +++ b/builtin-check-attr.c @@ -0,0 +1,49 @@ +#include "builtin.h" +#include "attr.h" +#include "quote.h" + +static const char check_attr_usage[] = +"git-check-attr attr... [--] pathname..."; + +int cmd_check_attr(int argc, const char **argv, const char *prefix) +{ + struct git_attr_check *check; + int cnt, i, doubledash; + + doubledash = -1; + for (i = 1; doubledash < 0 && i < argc; i++) { + if (!strcmp(argv[i], "--")) + doubledash = i; + } + + /* If there is no double dash, we handle only one attribute */ + if (doubledash < 0) { + cnt = 1; + doubledash = 1; + } else + cnt = doubledash - 1; + doubledash++; + + if (cnt <= 0 || argc < doubledash) + usage(check_attr_usage); + check = xcalloc(cnt, sizeof(*check)); + for (i = 0; i < cnt; i++) { + const char *name; + name = argv[i + 1]; + check[i].attr = git_attr(name, strlen(name)); + } + + for (i = doubledash; i < argc; i++) { + int j; + if (git_checkattr(argv[i], cnt, check)) + die("git_checkattr died"); + for (j = 0; j < cnt; j++) { + 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"); + } + } + return 0; +} diff --git a/builtin.h b/builtin.h index af203e9..d3f3a74 100644 --- a/builtin.h +++ b/builtin.h @@ -22,6 +22,7 @@ extern int cmd_branch(int argc, const char **argv, const char *prefix); extern int cmd_bundle(int argc, const char **argv, const char *prefix); extern int cmd_cat_file(int argc, const char **argv, const char *prefix); extern int cmd_checkout_index(int argc, const char **argv, const char *prefix); +extern int cmd_check_attr(int argc, const char **argv, const char *prefix); extern int cmd_check_ref_format(int argc, const char **argv, const char *prefix); extern int cmd_cherry(int argc, const char **argv, const char *prefix); extern int cmd_cherry_pick(int argc, const char **argv, const char *prefix); diff --git a/cache.h b/cache.h index b1bd9e4..63af43f 100644 --- a/cache.h +++ b/cache.h @@ -151,6 +151,8 @@ enum object_type { #define CONFIG_ENVIRONMENT "GIT_CONFIG" #define CONFIG_LOCAL_ENVIRONMENT "GIT_CONFIG_LOCAL" #define EXEC_PATH_ENVIRONMENT "GIT_EXEC_PATH" +#define GITATTRIBUTES_FILE ".gitattributes" +#define INFOATTRIBUTES_FILE "info/attributes" extern int is_bare_repository_cfg; extern int is_bare_repository(void); diff --git a/git.c b/git.c index 7def319..f200907 100644 --- a/git.c +++ b/git.c @@ -234,6 +234,7 @@ static void handle_internal_command(int argc, const char **argv, char **envp) { "cat-file", cmd_cat_file, RUN_SETUP }, { "checkout-index", cmd_checkout_index, RUN_SETUP }, { "check-ref-format", cmd_check_ref_format }, + { "check-attr", cmd_check_attr, RUN_SETUP | NOT_BARE }, { "cherry", cmd_cherry, RUN_SETUP }, { "cherry-pick", cmd_cherry_pick, RUN_SETUP | NOT_BARE }, { "commit-tree", cmd_commit_tree, RUN_SETUP }, -- cgit v0.10.2-6-g49f6 From 35ebfd6a0cd71795c4fa510b99e55ad89fb654f1 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Thu, 12 Apr 2007 22:30:05 -0700 Subject: Define 'crlf' attribute. This defines the semantics of 'crlf' attribute as an example. When a path has this attribute unset (i.e. '!crlf'), autocrlf line-end conversion is not applied. Eventually we would want to let users to build a pipeline of processing to munge blob data to filesystem format (and in the other direction) based on combination of attributes, and at that point the mechanism in convert_to_{git,working_tree}() that looks at 'crlf' attribute needs to be enhanced. Perhaps the existing 'crlf' would become the first step in the input chain, and the last step in the output chain. Signed-off-by: Junio C Hamano diff --git a/attr.c b/attr.c index 7435d92..ed4db01 100644 --- a/attr.c +++ b/attr.c @@ -378,3 +378,21 @@ int git_checkattr(const char *path, int num, struct git_attr_check *check) rem = fill(path, pathlen, stk, check, num, rem); return 0; } + +static void setup_binary_check(struct git_attr_check *check) +{ + static struct git_attr *attr_binary; + + if (!attr_binary) + attr_binary = git_attr("binary", 6); + check->attr = attr_binary; +} + +int git_path_is_binary(const char *path) +{ + struct git_attr_check attr_binary_check; + + setup_binary_check(&attr_binary_check); + return (!git_checkattr(path, 1, &attr_binary_check) && + (0 < attr_binary_check.isset)); +} diff --git a/convert.c b/convert.c index 898bfe3..20c744a 100644 --- a/convert.c +++ b/convert.c @@ -1,4 +1,6 @@ #include "cache.h" +#include "attr.h" + /* * convert.c - convert a file when checking it out and checking it in. * @@ -72,17 +74,12 @@ static int is_binary(unsigned long size, struct text_stat *stats) return 0; } -int convert_to_git(const char *path, char **bufp, unsigned long *sizep) +static int autocrlf_to_git(const char *path, char **bufp, unsigned long *sizep) { char *buffer, *nbuf; unsigned long size, nsize; struct text_stat stats; - /* - * FIXME! Other pluggable conversions should go here, - * based on filename patterns. Right now we just do the - * stupid auto-CRLF one. - */ if (!auto_crlf) return 0; @@ -128,7 +125,7 @@ int convert_to_git(const char *path, char **bufp, unsigned long *sizep) return 1; } -int convert_to_working_tree(const char *path, char **bufp, unsigned long *sizep) +static int autocrlf_to_working_tree(const char *path, char **bufp, unsigned long *sizep) { char *buffer, *nbuf; unsigned long size, nsize; @@ -184,3 +181,41 @@ int convert_to_working_tree(const char *path, char **bufp, unsigned long *sizep) return 1; } + +static void setup_crlf_check(struct git_attr_check *check) +{ + static struct git_attr *attr_crlf; + + if (!attr_crlf) + attr_crlf = git_attr("crlf", 4); + check->attr = attr_crlf; +} + +static int git_path_is_binary(const char *path) +{ + struct git_attr_check attr_crlf_check; + + setup_crlf_check(&attr_crlf_check); + + /* + * If crlf is not mentioned, default to autocrlf; + * disable autocrlf only when crlf attribute is explicitly + * unset. + */ + return (!git_checkattr(path, 1, &attr_crlf_check) && + (0 == attr_crlf_check.isset)); +} + +int convert_to_git(const char *path, char **bufp, unsigned long *sizep) +{ + if (git_path_is_binary(path)) + return 0; + return autocrlf_to_git(path, bufp, sizep); +} + +int convert_to_working_tree(const char *path, char **bufp, unsigned long *sizep) +{ + if (git_path_is_binary(path)) + return 0; + return autocrlf_to_working_tree(path, bufp, sizep); +} diff --git a/t/t0020-crlf.sh b/t/t0020-crlf.sh index 723b29a..600dcd3 100755 --- a/t/t0020-crlf.sh +++ b/t/t0020-crlf.sh @@ -214,4 +214,28 @@ test_expect_success 'apply patch --index (autocrlf=true)' ' } ' +test_expect_success '.gitattributes says two is binary' ' + + echo "two !crlf" >.gitattributes && + rm -f tmp one dir/two && + git repo-config core.autocrlf true && + git read-tree --reset -u HEAD && + + if remove_cr dir/two >/dev/null + then + echo "Huh?" + false + else + : happy + fi && + + if remove_cr one >/dev/null + then + : happy + else + echo "Huh?" + false + fi +' + test_done -- cgit v0.10.2-6-g49f6 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 --git a/attr.c b/attr.c index ed4db01..7435d92 100644 --- a/attr.c +++ b/attr.c @@ -378,21 +378,3 @@ int git_checkattr(const char *path, int num, struct git_attr_check *check) rem = fill(path, pathlen, stk, check, num, rem); return 0; } - -static void setup_binary_check(struct git_attr_check *check) -{ - static struct git_attr *attr_binary; - - if (!attr_binary) - attr_binary = git_attr("binary", 6); - check->attr = attr_binary; -} - -int git_path_is_binary(const char *path) -{ - struct git_attr_check attr_binary_check; - - setup_binary_check(&attr_binary_check); - return (!git_checkattr(path, 1, &attr_binary_check) && - (0 < attr_binary_check.isset)); -} diff --git a/diff.c b/diff.c index fbb79d7..e4efb65 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 v0.10.2-6-g49f6 From 201ac8efc79668353281583629aa15ac7f36e843 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Sun, 15 Apr 2007 13:35:45 -0700 Subject: Fix 'crlf' attribute semantics. Earlier we said 'crlf lets the path go through core.autocrlf process while !crlf disables it altogether'. This fixes the semantics to: - Lack of 'crlf' attribute makes core.autocrlf to apply (i.e. we guess based on the contents and if platform expresses its desire to have CRLF line endings via core.autocrlf, we do so). - Setting 'crlf' attribute to true forces CRLF line endings in working tree files, even if blob does not look like text (e.g. contains NUL or other bytes we consider binary). - Setting 'crlf' attribute to false disables conversion. Signed-off-by: Junio C Hamano diff --git a/convert.c b/convert.c index 20c744a..d0d4b81 100644 --- a/convert.c +++ b/convert.c @@ -74,13 +74,13 @@ static int is_binary(unsigned long size, struct text_stat *stats) return 0; } -static int autocrlf_to_git(const char *path, char **bufp, unsigned long *sizep) +static int crlf_to_git(const char *path, char **bufp, unsigned long *sizep, int guess) { char *buffer, *nbuf; unsigned long size, nsize; struct text_stat stats; - if (!auto_crlf) + if (guess && !auto_crlf) return 0; size = *sizep; @@ -94,19 +94,21 @@ static int autocrlf_to_git(const char *path, char **bufp, unsigned long *sizep) if (!stats.cr) return 0; - /* - * We're currently not going to even try to convert stuff - * that has bare CR characters. Does anybody do that crazy - * stuff? - */ - if (stats.cr != stats.crlf) - return 0; - - /* - * And add some heuristics for binary vs text, of course... - */ - if (is_binary(size, &stats)) - return 0; + if (guess) { + /* + * We're currently not going to even try to convert stuff + * that has bare CR characters. Does anybody do that crazy + * stuff? + */ + if (stats.cr != stats.crlf) + return 0; + + /* + * And add some heuristics for binary vs text, of course... + */ + if (is_binary(size, &stats)) + return 0; + } /* * Ok, allocate a new buffer, fill it in, and return true @@ -116,28 +118,42 @@ static int autocrlf_to_git(const char *path, char **bufp, unsigned long *sizep) nbuf = xmalloc(nsize); *bufp = nbuf; *sizep = nsize; - do { - unsigned char c = *buffer++; - if (c != '\r') - *nbuf++ = c; - } while (--size); + + if (guess) { + do { + unsigned char c = *buffer++; + if (c != '\r') + *nbuf++ = c; + } while (--size); + } else { + do { + unsigned char c = *buffer++; + if (! (c == '\r' && (1 < size && *buffer == '\n'))) + *nbuf++ = c; + } while (--size); + } return 1; } -static int autocrlf_to_working_tree(const char *path, char **bufp, unsigned long *sizep) +static int autocrlf_to_git(const char *path, char **bufp, unsigned long *sizep) +{ + return crlf_to_git(path, bufp, sizep, 1); +} + +static int forcecrlf_to_git(const char *path, char **bufp, unsigned long *sizep) +{ + return crlf_to_git(path, bufp, sizep, 0); +} + +static int crlf_to_working_tree(const char *path, char **bufp, unsigned long *sizep, int guess) { char *buffer, *nbuf; unsigned long size, nsize; struct text_stat stats; unsigned char last; - /* - * FIXME! Other pluggable conversions should go here, - * based on filename patterns. Right now we just do the - * stupid auto-CRLF one. - */ - if (auto_crlf <= 0) + if (guess && auto_crlf <= 0) return 0; size = *sizep; @@ -155,12 +171,14 @@ static int autocrlf_to_working_tree(const char *path, char **bufp, unsigned long if (stats.lf == stats.crlf) return 0; - /* If we have any bare CR characters, we're not going to touch it */ - if (stats.cr != stats.crlf) - return 0; + if (guess) { + /* If we have any bare CR characters, we're not going to touch it */ + if (stats.cr != stats.crlf) + return 0; - if (is_binary(size, &stats)) - return 0; + if (is_binary(size, &stats)) + return 0; + } /* * Ok, allocate a new buffer, fill it in, and return true @@ -182,6 +200,16 @@ static int autocrlf_to_working_tree(const char *path, char **bufp, unsigned long return 1; } +static int autocrlf_to_working_tree(const char *path, char **bufp, unsigned long *sizep) +{ + return crlf_to_working_tree(path, bufp, sizep, 1); +} + +static int forcecrlf_to_working_tree(const char *path, char **bufp, unsigned long *sizep) +{ + return crlf_to_working_tree(path, bufp, sizep, 0); +} + static void setup_crlf_check(struct git_attr_check *check) { static struct git_attr *attr_crlf; @@ -191,31 +219,37 @@ static void setup_crlf_check(struct git_attr_check *check) check->attr = attr_crlf; } -static int git_path_is_binary(const char *path) +static int git_path_check_crlf(const char *path) { struct git_attr_check attr_crlf_check; setup_crlf_check(&attr_crlf_check); - /* - * If crlf is not mentioned, default to autocrlf; - * disable autocrlf only when crlf attribute is explicitly - * unset. - */ - return (!git_checkattr(path, 1, &attr_crlf_check) && - (0 == attr_crlf_check.isset)); + if (git_checkattr(path, 1, &attr_crlf_check)) + return -1; + return attr_crlf_check.isset; } int convert_to_git(const char *path, char **bufp, unsigned long *sizep) { - if (git_path_is_binary(path)) + switch (git_path_check_crlf(path)) { + case 0: return 0; - return autocrlf_to_git(path, bufp, sizep); + case 1: + return forcecrlf_to_git(path, bufp, sizep); + default: + return autocrlf_to_git(path, bufp, sizep); + } } int convert_to_working_tree(const char *path, char **bufp, unsigned long *sizep) { - if (git_path_is_binary(path)) + switch (git_path_check_crlf(path)) { + case 0: return 0; - return autocrlf_to_working_tree(path, bufp, sizep); + case 1: + return forcecrlf_to_working_tree(path, bufp, sizep); + default: + return autocrlf_to_working_tree(path, bufp, sizep); + } } -- cgit v0.10.2-6-g49f6 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 --git a/diff.c b/diff.c index e4efb65..dcea405 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 v0.10.2-6-g49f6 From 6d4da3dea0ba01bb17a9001efd3ab2bbce531b6f Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Sun, 15 Apr 2007 13:39:32 -0700 Subject: Makefile: add patch-ids.h back in. I lost it by mistake while shuffling the gitattributes series which originally was on top of the subproject topic onto the master branch. Signed-off-by: Junio C Hamano diff --git a/Makefile b/Makefile index ac89d1b..4a399dd 100644 --- a/Makefile +++ b/Makefile @@ -283,7 +283,7 @@ LIB_H = \ diff.h object.h pack.h pkt-line.h quote.h refs.h list-objects.h sideband.h \ run-command.h strbuf.h tag.h tree.h git-compat-util.h revision.h \ tree-walk.h log-tree.h dir.h path-list.h unpack-trees.h builtin.h \ - utf8.h reflog-walk.h attr.h + utf8.h reflog-walk.h patch-ids.h attr.h DIFF_OBJS = \ diff.o diff-lib.o diffcore-break.o diffcore-order.o \ -- cgit v0.10.2-6-g49f6 From f48fd68887a03756658a46486a5dd1301c5a655f Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Sat, 14 Apr 2007 08:54:37 -0700 Subject: attribute macro support This adds "attribute macros" (for lack of better name). So far, we have low-level attributes such as crlf and diff, which are defined in operational terms --- setting or unsetting them on a particular path directly affects what is done to the path. For example, in order to decline diffs or crlf conversions on a binary blob, no diffs on PostScript files, and treat all other files normally, you would have something like these: * diff crlf *.ps !diff proprietary.o !diff !crlf That is fine as the operation goes, but gets unwieldy rather rapidly, when we start adding more low-level attributes that are defined in operational terms. A near-term example of such an attribute would be 'merge-3way' which would control if git should attempt the usual 3-way file-level merge internally, or leave merging to a specialized external program of user's choice. When it is added, we do _not_ want to force the users to update the above to: * diff crlf merge-3way *.ps !diff proprietary.o !diff !crlf !merge-3way The way this patch solves this issue is to realize that the attributes the user is assigning to paths are not defined in terms of operations but in terms of what they are. All of the three low-level attributes usually make sense for most of the files that sane SCM users have git operate on (these files are typically called "text'). Only a few cases, such as binary blob, need exception to decline the "usual treatment given to text files" -- and people mark them as "binary". So this allows the $GIT_DIR/info/alternates and .gitattributes at the toplevel of the project to also specify attributes that assigns other attributes. The syntax is '[attr]' followed by an attribute name followed by a list of attribute names: [attr] binary !diff !crlf !merge-3way When "binary" attribute is set to a path, if the path has not got diff/crlf/merge-3way attribute set or unset by other rules, this rule unsets the three low-level attributes. It is expected that the user level .gitattributes will be expressed mostly in terms of attributes based on what the files are, and the above sample would become like this: (built-in attribute configuration) [attr] binary !diff !crlf !merge-3way * diff crlf merge-3way (project specific .gitattributes) proprietary.o binary (user preference $GIT_DIR/info/attributes) *.ps !diff There are a few caveats. * As described above, you can define these macros only in $GIT_DIR/info/attributes and toplevel .gitattributes. * There is no attempt to detect circular definition of macro attributes, and definitions are evaluated from bottom to top as usual to fill in other attributes that have not yet got values. The following would work as expected: [attr] text diff crlf [attr] ps text !diff *.ps ps while this would most likely not (I haven't tried): [attr] ps text !diff [attr] text diff crlf *.ps ps * When a macro says "[attr] A B !C", saying that a path does not have attribute A does not let you tell anything about attributes B or C. That is, given this: [attr] text diff crlf [attr] ps text !diff *.txt !ps path hello.txt, which would match "*.txt" pattern, would have "ps" attribute set to zero, but that does not make text attribute of hello.txt set to false (nor diff attribute set to true). Signed-off-by: Junio C Hamano diff --git a/attr.c b/attr.c index 7435d92..a306144 100644 --- a/attr.c +++ b/attr.c @@ -16,9 +16,12 @@ struct git_attr { struct git_attr *next; unsigned h; + int attr_nr; char name[FLEX_ARRAY]; }; +static int attr_nr; +static struct git_attr_check *check_all_attr; static struct git_attr *(git_attr_hash[HASHSIZE]); static unsigned hash_name(const char *name, int namelen) @@ -50,7 +53,12 @@ struct git_attr *git_attr(const char *name, int len) a->name[len] = 0; a->h = hval; a->next = git_attr_hash[pos]; + a->attr_nr = attr_nr++; git_attr_hash[pos] = a; + + check_all_attr = xrealloc(check_all_attr, + sizeof(*check_all_attr) * attr_nr); + check_all_attr[a->attr_nr].attr = a; return a; } @@ -69,26 +77,46 @@ struct attr_state { }; struct match_attr { - char *pattern; + union { + char *pattern; + struct git_attr *attr; + } u; + char is_macro; unsigned num_attr; struct attr_state state[FLEX_ARRAY]; }; static const char blank[] = " \t\r\n"; -static struct match_attr *parse_attr_line(const char *line) +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; int pass; + int is_macro; cp = line + strspn(line, blank); if (!*cp || *cp == '#') return NULL; name = cp; namelen = strcspn(name, blank); + if (strlen(ATTRIBUTE_MACRO_PREFIX) < namelen && + !prefixcmp(name, ATTRIBUTE_MACRO_PREFIX)) { + if (!macro_ok) { + fprintf(stderr, "%s not allowed: %s:%d\n", + name, src, lineno); + return NULL; + } + is_macro = 1; + name += strlen(ATTRIBUTE_MACRO_PREFIX); + name += strspn(name, blank); + namelen = strcspn(name, blank); + } + else + is_macro = 0; for (pass = 0; pass < 2; pass++) { /* pass 0 counts and allocates, pass 1 fills */ @@ -113,13 +141,19 @@ static struct match_attr *parse_attr_line(const char *line) } if (pass) break; + res = xcalloc(1, sizeof(*res) + sizeof(struct attr_state) * num_attr + - namelen + 1); - res->pattern = (char*)&(res->state[num_attr]); - memcpy(res->pattern, name, namelen); - res->pattern[namelen] = 0; + (is_macro ? 0 : namelen + 1)); + 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); + res->u.pattern[namelen] = 0; + } + res->is_macro = is_macro; res->num_attr = num_attr; } return res; @@ -167,10 +201,13 @@ static struct attr_stack *read_attr_from_array(const char **list) { struct attr_stack *res; const char *line; + int lineno = 0; res = xcalloc(1, sizeof(*res)); while ((line = *(list++)) != NULL) { - struct match_attr *a = parse_attr_line(line); + struct match_attr *a; + + a = parse_attr_line(line, "[builtin]", ++lineno, 1); if (!a) continue; res->attrs = xrealloc(res->attrs, res->num_matches + 1); @@ -179,11 +216,12 @@ static struct attr_stack *read_attr_from_array(const char **list) return res; } -static struct attr_stack *read_attr_from_file(const char *path) +static struct attr_stack *read_attr_from_file(const char *path, int macro_ok) { FILE *fp; struct attr_stack *res; char buf[2048]; + int lineno = 0; res = xcalloc(1, sizeof(*res)); fp = fopen(path, "r"); @@ -191,7 +229,9 @@ static struct attr_stack *read_attr_from_file(const char *path) return res; while (fgets(buf, sizeof(buf), fp)) { - struct match_attr *a = parse_attr_line(buf); + struct match_attr *a; + + a = parse_attr_line(buf, path, ++lineno, macro_ok); if (!a) continue; res->attrs = xrealloc(res->attrs, res->num_matches + 1); @@ -206,13 +246,42 @@ 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) +{ + fprintf(stderr, "%s: %s => %d (%s)\n", + what, attr->name, set, match); +} #define debug_push(a) debug_info("push", (a)) #define debug_pop(a) debug_info("pop", (a)) #else #define debug_push(a) do { ; } while (0) #define debug_pop(a) do { ; } while (0) +#define debug_set(a,b,c,d) do { ; } while (0) #endif +static void bootstrap_attr_stack(void) +{ + if (!attr_stack) { + struct attr_stack *elem; + + elem = read_attr_from_array(builtin_attr); + elem->origin = NULL; + elem->prev = attr_stack; + attr_stack = elem; + + elem = read_attr_from_file(GITATTRIBUTES_FILE, 1); + elem->origin = strdup(""); + elem->prev = attr_stack; + attr_stack = elem; + debug_push(elem); + + elem = read_attr_from_file(git_path(INFOATTRIBUTES_FILE), 1); + elem->origin = NULL; + elem->prev = attr_stack; + attr_stack = elem; + } +} + static void prepare_attr_stack(const char *path, int dirlen) { struct attr_stack *elem, *info; @@ -232,23 +301,8 @@ static void prepare_attr_stack(const char *path, int dirlen) * .gitattributes in deeper directories to shallower ones, * and finally use the built-in set as the default. */ - if (!attr_stack) { - elem = read_attr_from_array(builtin_attr); - elem->origin = NULL; - elem->prev = attr_stack; - attr_stack = elem; - - elem = read_attr_from_file(GITATTRIBUTES_FILE); - elem->origin = strdup(""); - elem->prev = attr_stack; - attr_stack = elem; - debug_push(elem); - - elem = read_attr_from_file(git_path(INFOATTRIBUTES_FILE)); - elem->origin = NULL; - elem->prev = attr_stack; - attr_stack = elem; - } + if (!attr_stack) + bootstrap_attr_stack(); /* * Pop the "info" one that is always at the top of the stack. @@ -286,7 +340,7 @@ static void prepare_attr_stack(const char *path, int dirlen) memcpy(pathbuf + dirlen, "/", 2); cp = strchr(pathbuf + len + 1, '/'); strcpy(cp + 1, GITATTRIBUTES_FILE); - elem = read_attr_from_file(pathbuf); + elem = read_attr_from_file(pathbuf, 0); *cp = '\0'; elem->origin = strdup(pathbuf); elem->prev = attr_stack; @@ -324,31 +378,26 @@ static int path_matches(const char *pathname, int pathlen, return fnmatch(pattern, pathname + baselen, FNM_PATHNAME) == 0; } -/* - * I do not like this at all. Only because we allow individual - * attribute to be set or unset incrementally by individual - * lines in .gitattribute files, we need to do this triple - * loop which looks quite wasteful. - */ -static int fill(const char *path, int pathlen, - struct attr_stack *stk, struct git_attr_check *check, - int num, int rem) +static int fill(const char *path, int pathlen, struct attr_stack *stk, int rem) { - int i, j, k; 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->pattern, base, strlen(base))) { - for (j = 0; j < a->num_attr; j++) { + 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; - for (k = 0; k < num; k++) { - if (0 <= check[k].isset || - check[k].attr != attr) - continue; - check[k].isset = set; + int *n = &(check[attr->attr_nr].isset); + + if (*n < 0) { + debug_set("fill", a->u.pattern, attr, set); + *n = set; rem--; } } @@ -357,14 +406,41 @@ static int fill(const char *path, int pathlen, return rem; } +static int macroexpand(struct attr_stack *stk, int rem) +{ + 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 (check[a->u.attr->attr_nr].isset < 0) + 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--; + } + } + } + return rem; +} + int git_checkattr(const char *path, int num, struct git_attr_check *check) { struct attr_stack *stk; const char *cp; int dirlen, pathlen, i, rem; - for (i = 0; i < num; i++) - check[i].isset = -1; + bootstrap_attr_stack(); + for (i = 0; i < attr_nr; i++) + check_all_attr[i].isset = -1; pathlen = strlen(path); cp = strrchr(path, '/'); @@ -373,8 +449,15 @@ int git_checkattr(const char *path, int num, struct git_attr_check *check) else dirlen = cp - path; prepare_attr_stack(path, dirlen); - rem = num; + rem = attr_nr; + for (stk = attr_stack; 0 < rem && stk; stk = stk->prev) + rem = fill(path, pathlen, stk, rem); + for (stk = attr_stack; 0 < rem && stk; stk = stk->prev) - rem = fill(path, pathlen, stk, check, num, rem); + rem = macroexpand(stk, rem); + + for (i = 0; i < num; i++) + check[i].isset = check_all_attr[check[i].attr->attr_nr].isset; + return 0; } diff --git a/cache.h b/cache.h index 63af43f..38ad006 100644 --- a/cache.h +++ b/cache.h @@ -153,6 +153,7 @@ enum object_type { #define EXEC_PATH_ENVIRONMENT "GIT_EXEC_PATH" #define GITATTRIBUTES_FILE ".gitattributes" #define INFOATTRIBUTES_FILE "info/attributes" +#define ATTRIBUTE_MACRO_PREFIX "[attr]" extern int is_bare_repository_cfg; extern int is_bare_repository(void); -- cgit v0.10.2-6-g49f6 From fc2d07b05fb691f98b3a55c1499fae6fb25a7d31 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Sat, 14 Apr 2007 08:56:35 -0700 Subject: Define a built-in attribute macro "binary". For binary files we would want to disable textual diff generation and automatic crlf conversion. Signed-off-by: Junio C Hamano diff --git a/attr.c b/attr.c index a306144..410bca6 100644 --- a/attr.c +++ b/attr.c @@ -194,6 +194,7 @@ static void free_attr_elem(struct attr_stack *e) } static const char *builtin_attr[] = { + "[attr]binary !diff !crlf", NULL, }; -- cgit v0.10.2-6-g49f6 From e4aee10a2eaf0937d86d046f85ee569a75cae9ac Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Sun, 15 Apr 2007 14:56:09 -0700 Subject: Change attribute negation marker from '!' to '-'. At the same time, we do not want to allow arbitrary strings for attribute names, as we are likely to want to extend the syntax later. Allow only alnum, dash, underscore and dot for now. Signed-off-by: Junio C Hamano diff --git a/attr.c b/attr.c index 410bca6..60fe48f 100644 --- a/attr.c +++ b/attr.c @@ -36,6 +36,27 @@ static unsigned hash_name(const char *name, int namelen) return val; } +static int invalid_attr_name(const char *name, int namelen) +{ + /* + * Attribute name cannot begin with '-' and from + * [-A-Za-z0-9_.]. We'd specifically exclude '=' for now, + * as we might later want to allow non-binary value for + * attributes, e.g. "*.svg merge=special-merge-program-for-svg" + */ + if (*name == '-') + return -1; + while (namelen--) { + char ch = *name++; + if (! (ch == '-' || ch == '.' || ch == '_' || + ('0' <= ch && ch <= '9') || + ('a' <= ch && ch <= 'z') || + ('A' <= ch && ch <= 'Z')) ) + return -1; + } + return 0; +} + struct git_attr *git_attr(const char *name, int len) { unsigned hval = hash_name(name, len); @@ -48,6 +69,9 @@ struct git_attr *git_attr(const char *name, int len) return a; } + if (invalid_attr_name(name, len)) + return NULL; + a = xmalloc(sizeof(*a) + len + 1); memcpy(a->name, name, len); a->name[len] = 0; @@ -68,7 +92,7 @@ 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 "not set". */ struct attr_state { @@ -114,6 +138,12 @@ static struct match_attr *parse_attr_line(const char *line, const char *src, name += strlen(ATTRIBUTE_MACRO_PREFIX); name += strspn(name, blank); namelen = strcspn(name, blank); + if (invalid_attr_name(name, namelen)) { + fprintf(stderr, + "%.*s is not a valid attribute name: %s:%d\n", + namelen, name, src, lineno); + return NULL; + } } else is_macro = 0; @@ -126,11 +156,21 @@ static struct match_attr *parse_attr_line(const char *line, const char *src, while (*cp) { const char *ep; ep = cp + strcspn(cp, blank); - if (pass) { + 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 == '!') { + if (*cp == '-') { e->unset = 1; cp++; } @@ -146,8 +186,9 @@ static struct match_attr *parse_attr_line(const char *line, const char *src, 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); @@ -194,7 +235,7 @@ static void free_attr_elem(struct attr_stack *e) } static const char *builtin_attr[] = { - "[attr]binary !diff !crlf", + "[attr]binary -diff -crlf", NULL, }; diff --git a/builtin-check-attr.c b/builtin-check-attr.c index 47b0721..634be9e 100644 --- a/builtin-check-attr.c +++ b/builtin-check-attr.c @@ -29,8 +29,12 @@ int cmd_check_attr(int argc, const char **argv, const char *prefix) check = xcalloc(cnt, sizeof(*check)); for (i = 0; i < cnt; i++) { const char *name; + struct git_attr *a; name = argv[i + 1]; - check[i].attr = git_attr(name, strlen(name)); + a = git_attr(name, strlen(name)); + if (!a) + return error("%s: not a valid attribute name", name); + check[i].attr = a; } for (i = doubledash; i < argc; i++) { diff --git a/t/t0020-crlf.sh b/t/t0020-crlf.sh index 600dcd3..cf84f0a 100755 --- a/t/t0020-crlf.sh +++ b/t/t0020-crlf.sh @@ -216,7 +216,7 @@ test_expect_success 'apply patch --index (autocrlf=true)' ' test_expect_success '.gitattributes says two is binary' ' - echo "two !crlf" >.gitattributes && + echo "two -crlf" >.gitattributes && rm -f tmp one dir/two && git repo-config core.autocrlf true && git read-tree --reset -u HEAD && -- cgit v0.10.2-6-g49f6 From b568a503def81f49704ba94f5a822d523022102a Mon Sep 17 00:00:00 2001 From: James Bowes Date: Sat, 14 Apr 2007 21:27:20 -0400 Subject: Document git-check-attr Signed-off-by: James Bowes Signed-off-by: Junio C Hamano diff --git a/Documentation/cmd-list.perl b/Documentation/cmd-list.perl index 0381590..443802a 100755 --- a/Documentation/cmd-list.perl +++ b/Documentation/cmd-list.perl @@ -84,6 +84,7 @@ git-bundle mainporcelain git-cat-file plumbinginterrogators git-checkout-index plumbingmanipulators git-checkout mainporcelain +git-check-attr purehelpers git-check-ref-format purehelpers git-cherry ancillaryinterrogators git-cherry-pick mainporcelain diff --git a/Documentation/git-check-attr.txt b/Documentation/git-check-attr.txt new file mode 100644 index 0000000..ceb5195 --- /dev/null +++ b/Documentation/git-check-attr.txt @@ -0,0 +1,37 @@ +git-check-attr(1) +================= + +NAME +---- +git-check-attr - Display gitattributes information. + + +SYNOPSIS +-------- +'git-check-attr' attr... [--] pathname... + +DESCRIPTION +----------- +For every pathname, this command will list if each attr is 'unspecified', +'set', or 'unset' as a gitattribute on that pathname. + +OPTIONS +------- +\--:: + Interpret all preceding arguments as attributes, and all following + arguments as path names. If not supplied, only the first argument will + be treated as an attribute. + + +Author +------ +Written by Junio C Hamano + +Documentation +-------------- +Documentation by James Bowes. + +GIT +--- +Part of the gitlink:git[7] suite + -- cgit v0.10.2-6-g49f6 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 diff --git a/attr.c b/attr.c index 60fe48f..b3496a6 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 1e5ab40..8ec2d3d 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 634be9e..6983a73 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 d0d4b81..68bb70f 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 dcea405..a32078e 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 v0.10.2-6-g49f6 From 3e5261a24071ca23c3514c0ebd5ee55f1e79d9cc Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Mon, 16 Apr 2007 21:58:01 -0700 Subject: merge-recursive: separate out xdl_merge() interface. This just moves code around to make the actual call to xdl_merge() into a separate function. Signed-off-by: Junio C Hamano diff --git a/merge-recursive.c b/merge-recursive.c index 3096594..4eb62cf 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -659,6 +659,39 @@ static void fill_mm(const unsigned char *sha1, mmfile_t *mm) mm->size = size; } +static int ll_merge(mmbuffer_t *result_buf, + struct diff_filespec *o, + struct diff_filespec *a, + struct diff_filespec *b, + const char *branch1, + const char *branch2) +{ + mmfile_t orig, src1, src2; + xpparam_t xpp; + char *name1, *name2; + int merge_status; + + name1 = xstrdup(mkpath("%s:%s", branch1, a->path)); + name2 = xstrdup(mkpath("%s:%s", branch2, b->path)); + + fill_mm(o->sha1, &orig); + fill_mm(a->sha1, &src1); + fill_mm(b->sha1, &src2); + + memset(&xpp, 0, sizeof(xpp)); + merge_status = xdl_merge(&orig, + &src1, name1, + &src2, name2, + &xpp, XDL_MERGE_ZEALOUS, + result_buf); + free(name1); + free(name2); + free(orig.ptr); + free(src1.ptr); + free(src2.ptr); + return merge_status; +} + static struct merge_file_info merge_file(struct diff_filespec *o, struct diff_filespec *a, struct diff_filespec *b, const char *branch1, const char *branch2) @@ -687,30 +720,11 @@ static struct merge_file_info merge_file(struct diff_filespec *o, else if (sha_eq(b->sha1, o->sha1)) hashcpy(result.sha, a->sha1); else if (S_ISREG(a->mode)) { - mmfile_t orig, src1, src2; mmbuffer_t result_buf; - xpparam_t xpp; - char *name1, *name2; int merge_status; - name1 = xstrdup(mkpath("%s:%s", branch1, a->path)); - name2 = xstrdup(mkpath("%s:%s", branch2, b->path)); - - fill_mm(o->sha1, &orig); - fill_mm(a->sha1, &src1); - fill_mm(b->sha1, &src2); - - memset(&xpp, 0, sizeof(xpp)); - merge_status = xdl_merge(&orig, - &src1, name1, - &src2, name2, - &xpp, XDL_MERGE_ZEALOUS, - &result_buf); - free(name1); - free(name2); - free(orig.ptr); - free(src1.ptr); - free(src2.ptr); + merge_status = ll_merge(&result_buf, o, a, b, + branch1, branch2); if ((merge_status < 0) || !result_buf.ptr) die("Failed to execute internal merge"); -- cgit v0.10.2-6-g49f6 From a129d96f4144215711e379565af97f6a82197f4f Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Mon, 16 Apr 2007 22:59:18 -0700 Subject: Allow specifying specialized merge-backend per path. This allows 'merge' attribute to control how the file-level three-way merge is done per path. - If you set 'merge' to true, leave it unspecified, or set it to "text", we use the built-in 3-way xdl-merge. - If you set 'merge' to false, or set it to "binary, the "binary" merge is done. The merge result is the blob from 'our' tree, but this still leaves the path conflicted, so that the mess can be sorted out by the user. This is obviously meant to be useful for binary files. - 'merge=union' (this is the first example of a string valued attribute, introduced in the previous one) uses the "union" merge. The "union" merge takes lines in conflicted hunks from both sides, which is useful for line-oriented files such as .gitignore. Instead fo setting merge to 'true' or 'false' by using 'merge' or '-merge', setting it explicitly to "text" or "binary" will become useful once we start allowing custom per-path backends to be added, and allow them to be activated for the default (i.e. 'merge' attribute specified to 'true' or 'false') case, using some other mechanisms. Setting merge attribute to "text" or "binary" will be a way to explicitly request to override such a custom default for selected paths. Currently there is no way to specify random programs but it should be trivial for motivated contributors to add later. There is one caveat, though. ll_merge() is called for both internal ancestor merge and the outer "final" merge. I think an interactive custom per-path merge backend should refrain from going interactive when performing an internal merge (you can tell it by checking call_depth) and instead just call either ll_xdl_merge() if the content is text, or call ll_binary_merge() otherwise. Signed-off-by: Junio C Hamano diff --git a/merge-recursive.c b/merge-recursive.c index 4eb62cf..3b34401 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -15,6 +15,7 @@ #include "unpack-trees.h" #include "path-list.h" #include "xdiff-interface.h" +#include "attr.h" static int subtree_merge; @@ -659,6 +660,127 @@ static void fill_mm(const unsigned char *sha1, mmfile_t *mm) mm->size = size; } +/* Low-level merge functions */ +typedef int (*ll_merge_fn)(mmfile_t *orig, + mmfile_t *src1, const char *name1, + mmfile_t *src2, const char *name2, + mmbuffer_t *result); + +static int ll_xdl_merge(mmfile_t *orig, + mmfile_t *src1, const char *name1, + mmfile_t *src2, const char *name2, + mmbuffer_t *result) +{ + xpparam_t xpp; + + memset(&xpp, 0, sizeof(xpp)); + return xdl_merge(orig, + src1, name1, + src2, name2, + &xpp, XDL_MERGE_ZEALOUS, + result); +} + +static int ll_union_merge(mmfile_t *orig, + mmfile_t *src1, const char *name1, + mmfile_t *src2, const char *name2, + mmbuffer_t *result) +{ + char *src, *dst; + long size; + const int marker_size = 7; + + int status = ll_xdl_merge(orig, src1, NULL, src2, NULL, result); + if (status <= 0) + return status; + size = result->size; + src = dst = result->ptr; + while (size) { + char ch; + if ((marker_size < size) && + (*src == '<' || *src == '=' || *src == '>')) { + int i; + ch = *src; + for (i = 0; i < marker_size; i++) + if (src[i] != ch) + goto not_a_marker; + if (src[marker_size] != '\n') + goto not_a_marker; + src += marker_size + 1; + size -= marker_size + 1; + continue; + } + not_a_marker: + do { + ch = *src++; + *dst++ = ch; + size--; + } while (ch != '\n' && size); + } + result->size = dst - result->ptr; + return 0; +} + +static int ll_binary_merge(mmfile_t *orig, + mmfile_t *src1, const char *name1, + mmfile_t *src2, const char *name2, + mmbuffer_t *result) +{ + /* + * The tentative merge result is "ours" for the final round, + * or common ancestor for an internal merge. Still return + * "conflicted merge" status. + */ + mmfile_t *stolen = index_only ? orig : src1; + + result->ptr = stolen->ptr; + result->size = stolen->size; + stolen->ptr = NULL; + return 1; +} + +static struct { + const char *name; + ll_merge_fn fn; +} ll_merge_fns[] = { + { "text", ll_xdl_merge }, + { "binary", ll_binary_merge }, + { "union", ll_union_merge }, + { NULL, NULL }, +}; + +static ll_merge_fn find_ll_merge_fn(void *merge_attr) +{ + const char *name; + int i; + + if (ATTR_TRUE(merge_attr) || ATTR_UNSET(merge_attr)) + return ll_xdl_merge; + else if (ATTR_FALSE(merge_attr)) + return ll_binary_merge; + + /* Otherwise merge_attr may name the merge function */ + name = merge_attr; + for (i = 0; ll_merge_fns[i].name; i++) + if (!strcmp(ll_merge_fns[i].name, name)) + return ll_merge_fns[i].fn; + + /* default to the 3-way */ + return ll_xdl_merge; +} + +static void *git_path_check_merge(const char *path) +{ + static struct git_attr_check attr_merge_check; + + if (!attr_merge_check.attr) + attr_merge_check.attr = git_attr("merge", 5); + + if (git_checkattr(path, 1, &attr_merge_check)) + return ATTR__UNSET; + return attr_merge_check.value; +} + static int ll_merge(mmbuffer_t *result_buf, struct diff_filespec *o, struct diff_filespec *a, @@ -667,9 +789,10 @@ static int ll_merge(mmbuffer_t *result_buf, const char *branch2) { mmfile_t orig, src1, src2; - xpparam_t xpp; char *name1, *name2; int merge_status; + void *merge_attr; + ll_merge_fn fn; name1 = xstrdup(mkpath("%s:%s", branch1, a->path)); name2 = xstrdup(mkpath("%s:%s", branch2, b->path)); @@ -678,12 +801,11 @@ static int ll_merge(mmbuffer_t *result_buf, fill_mm(a->sha1, &src1); fill_mm(b->sha1, &src2); - memset(&xpp, 0, sizeof(xpp)); - merge_status = xdl_merge(&orig, - &src1, name1, - &src2, name2, - &xpp, XDL_MERGE_ZEALOUS, - result_buf); + merge_attr = git_path_check_merge(a->path); + fn = find_ll_merge_fn(merge_attr); + + merge_status = fn(&orig, &src1, name1, &src2, name2, result_buf); + free(name1); free(name2); free(orig.ptr); -- cgit v0.10.2-6-g49f6 From 47579efc009c6f7afaf31be107eb92395a4f10db Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Tue, 17 Apr 2007 00:05:00 -0700 Subject: Add a demonstration/test of customized merge. This demonstrates how the new low-level per-path merge backends, union and ours, work, and shows how they are controlled by the gitattribute mechanism. Signed-off-by: Junio C Hamano diff --git a/t/t6026-merge-attr.sh b/t/t6026-merge-attr.sh new file mode 100755 index 0000000..5daa223 --- /dev/null +++ b/t/t6026-merge-attr.sh @@ -0,0 +1,72 @@ +#!/bin/sh +# +# Copyright (c) 2007 Junio C Hamano +# + +test_description='per path merge controlled by merge attribute' + +. ./test-lib.sh + +test_expect_success setup ' + + for f in text binary union + do + echo Initial >$f && git add $f || break + done && + test_tick && + git commit -m Initial && + + git branch side && + for f in text binary union + do + echo Master >>$f && git add $f || break + done && + test_tick && + git commit -m Master && + + git checkout side && + for f in text binary union + do + echo Side >>$f && git add $f || break + done && + test_tick && + git commit -m Side + +' + +test_expect_success merge ' + + { + echo "binary -merge" + echo "union merge=union" + } >.gitattributes && + + if git merge master + then + echo Gaah, should have conflicted + false + else + echo Ok, conflicted. + fi +' + +test_expect_success 'check merge result in index' ' + + git ls-files -u | grep binary && + git ls-files -u | grep text && + ! (git ls-files -u | grep union) + +' + +test_expect_success 'check merge result in working tree' ' + + git cat-file -p HEAD:binary >binary-orig && + grep "<<<<<<<" text && + cmp binary-orig binary && + ! grep "<<<<<<<" union && + grep Master union && + grep Side union + +' + +test_done -- cgit v0.10.2-6-g49f6 From f3ef6b6bbe9bfd3d09130f7e26b87dbe11b93c5b Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Tue, 17 Apr 2007 22:51:45 -0700 Subject: Custom low-level merge driver support. This allows users to specify custom low-level merge driver per path, using the attributes mechanism. Just like you can specify one of built-in "text", "binary", "union" low-level merge drivers by saying: * merge=text .gitignore merge=union *.jpg merge=binary pick a name of your favorite merge driver, and assign it as the value of the 'merge' attribute. A custom low-level merge driver is defined via the config mechanism. This patch introduces 'merge.driver', a multi-valued configuration. Its value is the name (i.e. the one you use as the value of 'merge' attribute) followed by a command line specification. The command line can contain %O, %A, and %B to be interpolated with the names of temporary files that hold the common ancestor version, the version from your branch, and the version from the other branch, and the resulting command is spawned. The low-level merge driver is expected to update the temporary file for your branch (i.e. %A) with the result and exit with status 0 for a clean merge, and non-zero status for a conflicted merge. A new test in t6026 demonstrates a sample usage. Signed-off-by: Junio C Hamano diff --git a/merge-recursive.c b/merge-recursive.c index 3b34401..8ec18ad 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -15,6 +15,7 @@ #include "unpack-trees.h" #include "path-list.h" #include "xdiff-interface.h" +#include "interpolate.h" #include "attr.h" static int subtree_merge; @@ -661,12 +662,14 @@ static void fill_mm(const unsigned char *sha1, mmfile_t *mm) } /* Low-level merge functions */ -typedef int (*ll_merge_fn)(mmfile_t *orig, +typedef int (*ll_merge_fn)(const char *cmd, + mmfile_t *orig, mmfile_t *src1, const char *name1, mmfile_t *src2, const char *name2, mmbuffer_t *result); -static int ll_xdl_merge(mmfile_t *orig, +static int ll_xdl_merge(const char *cmd__unused, + mmfile_t *orig, mmfile_t *src1, const char *name1, mmfile_t *src2, const char *name2, mmbuffer_t *result) @@ -681,7 +684,8 @@ static int ll_xdl_merge(mmfile_t *orig, result); } -static int ll_union_merge(mmfile_t *orig, +static int ll_union_merge(const char *cmd__unused, + mmfile_t *orig, mmfile_t *src1, const char *name1, mmfile_t *src2, const char *name2, mmbuffer_t *result) @@ -690,7 +694,8 @@ static int ll_union_merge(mmfile_t *orig, long size; const int marker_size = 7; - int status = ll_xdl_merge(orig, src1, NULL, src2, NULL, result); + int status = ll_xdl_merge(cmd__unused, orig, + src1, NULL, src2, NULL, result); if (status <= 0) return status; size = result->size; @@ -721,7 +726,8 @@ static int ll_union_merge(mmfile_t *orig, return 0; } -static int ll_binary_merge(mmfile_t *orig, +static int ll_binary_merge(const char *cmd__unused, + mmfile_t *orig, mmfile_t *src1, const char *name1, mmfile_t *src2, const char *name2, mmbuffer_t *result) @@ -743,24 +749,169 @@ static struct { const char *name; ll_merge_fn fn; } ll_merge_fns[] = { - { "text", ll_xdl_merge }, { "binary", ll_binary_merge }, + { "text", ll_xdl_merge }, { "union", ll_union_merge }, { NULL, NULL }, }; -static ll_merge_fn find_ll_merge_fn(void *merge_attr) +static void create_temp(mmfile_t *src, char *path) { + int fd; + + strcpy(path, ".merge_file_XXXXXX"); + fd = mkstemp(path); + if (fd < 0) + die("unable to create temp-file"); + if (write_in_full(fd, src->ptr, src->size) != src->size) + die("unable to write temp-file"); + close(fd); +} + +static int ll_ext_merge(const char *cmd, + mmfile_t *orig, + mmfile_t *src1, const char *name1, + mmfile_t *src2, const char *name2, + mmbuffer_t *result) +{ + char temp[3][50]; + char cmdbuf[2048]; + struct interp table[] = { + { "%O" }, + { "%A" }, + { "%B" }, + }; + struct child_process child; + const char *args[20]; + int status, fd, i; + struct stat st; + + result->ptr = NULL; + result->size = 0; + create_temp(orig, temp[0]); + create_temp(src1, temp[1]); + create_temp(src2, temp[2]); + + interp_set_entry(table, 0, temp[0]); + interp_set_entry(table, 1, temp[1]); + interp_set_entry(table, 2, temp[2]); + + interpolate(cmdbuf, sizeof(cmdbuf), cmd, table, 3); + + memset(&child, 0, sizeof(child)); + child.argv = args; + args[0] = "sh"; + args[1] = "-c"; + args[2] = cmdbuf; + args[3] = NULL; + + status = run_command(&child); + if (status < -ERR_RUN_COMMAND_FORK) + ; /* failure in run-command */ + else + status = -status; + fd = open(temp[1], O_RDONLY); + if (fd < 0) + goto bad; + if (fstat(fd, &st)) + goto close_bad; + result->size = st.st_size; + result->ptr = xmalloc(result->size + 1); + if (read_in_full(fd, result->ptr, result->size) != result->size) { + free(result->ptr); + result->ptr = NULL; + result->size = 0; + } + close_bad: + close(fd); + bad: + for (i = 0; i < 3; i++) + unlink(temp[i]); + return status; +} + +/* + * merge.default and merge.driver configuration items + */ +static struct user_merge_fn { + struct user_merge_fn *next; + const char *name; + char *cmdline; + char b_[1]; +} *ll_user_merge_fns, **ll_user_merge_fns_tail; + +static int read_merge_config(const char *var, const char *value) +{ + struct user_merge_fn *fn; + int blen, nlen; + + if (strcmp(var, "merge.driver")) + return 0; + if (!value) + return error("%s: lacks value", var); + /* + * merge.driver is a multi-valued configuration, whose value is + * of form: + * + * name command-line + * + * The command-line will be interpolated with the following + * tokens and is given to the shell: + * + * %O - temporary file name for the merge base. + * %A - temporary file name for our version. + * %B - temporary file name for the other branches' version. + * + * The external merge driver should write the results in the file + * named by %A, and signal that it has done with exit status 0. + */ + for (nlen = -1, blen = 0; value[blen]; blen++) + if (nlen < 0 && isspace(value[blen])) + nlen = blen; + if (nlen < 0) + return error("%s '%s': lacks command line", var, value); + fn = xcalloc(1, sizeof(struct user_merge_fn) + blen + 1); + memcpy(fn->b_, value, blen + 1); + fn->name = fn->b_; + fn->b_[nlen] = 0; + fn->cmdline = fn->b_ + nlen + 1; + fn->next = *ll_user_merge_fns_tail; + *ll_user_merge_fns_tail = fn; + return 0; +} + +static void initialize_ll_merge(void) +{ + if (ll_user_merge_fns_tail) + return; + ll_user_merge_fns_tail = &ll_user_merge_fns; + git_config(read_merge_config); +} + +static ll_merge_fn find_ll_merge_fn(void *merge_attr, const char **cmdline) +{ + struct user_merge_fn *fn; const char *name; int i; - if (ATTR_TRUE(merge_attr) || ATTR_UNSET(merge_attr)) + initialize_ll_merge(); + + if (ATTR_TRUE(merge_attr)) return ll_xdl_merge; else if (ATTR_FALSE(merge_attr)) return ll_binary_merge; + else if (ATTR_UNSET(merge_attr)) + return ll_xdl_merge; + else + name = merge_attr; + + for (fn = ll_user_merge_fns; fn; fn = fn->next) { + if (!strcmp(fn->name, name)) { + *cmdline = fn->cmdline; + return ll_ext_merge; + } + } - /* Otherwise merge_attr may name the merge function */ - name = merge_attr; for (i = 0; ll_merge_fns[i].name; i++) if (!strcmp(ll_merge_fns[i].name, name)) return ll_merge_fns[i].fn; @@ -793,6 +944,7 @@ static int ll_merge(mmbuffer_t *result_buf, int merge_status; void *merge_attr; ll_merge_fn fn; + const char *driver = NULL; name1 = xstrdup(mkpath("%s:%s", branch1, a->path)); name2 = xstrdup(mkpath("%s:%s", branch2, b->path)); @@ -802,9 +954,10 @@ static int ll_merge(mmbuffer_t *result_buf, fill_mm(b->sha1, &src2); merge_attr = git_path_check_merge(a->path); - fn = find_ll_merge_fn(merge_attr); + fn = find_ll_merge_fn(merge_attr, &driver); - merge_status = fn(&orig, &src1, name1, &src2, name2, result_buf); + merge_status = fn(driver, &orig, + &src1, name1, &src2, name2, result_buf); free(name1); free(name2); diff --git a/t/t6026-merge-attr.sh b/t/t6026-merge-attr.sh index 5daa223..1732b60 100755 --- a/t/t6026-merge-attr.sh +++ b/t/t6026-merge-attr.sh @@ -30,8 +30,9 @@ test_expect_success setup ' echo Side >>$f && git add $f || break done && test_tick && - git commit -m Side + git commit -m Side && + git tag anchor ' test_expect_success merge ' @@ -69,4 +70,72 @@ test_expect_success 'check merge result in working tree' ' ' +cat >./custom-merge <<\EOF +#!/bin/sh + +orig="$1" ours="$2" theirs="$3" exit="$4" +( + echo "orig is $orig" + echo "ours is $ours" + echo "theirs is $theirs" + echo "=== orig ===" + cat "$orig" + echo "=== ours ===" + cat "$ours" + echo "=== theirs ===" + cat "$theirs" +) >"$ours+" +cat "$ours+" >"$ours" +rm -f "$ours+" +exit "$exit" +EOF +chmod +x ./custom-merge + +test_expect_success 'custom merge backend' ' + + echo "* merge=union" >.gitattributes && + echo "text merge=custom" >>.gitattributes && + + git reset --hard anchor && + git config --replace-all \ + merge.driver "custom ./custom-merge %O %A %B 0" && + + git merge master && + + cmp binary union && + sed -e 1,3d text >check-1 && + o=$(git-unpack-file master^:text) && + a=$(git-unpack-file side^:text) && + b=$(git-unpack-file master:text) && + sh -c "./custom-merge $o $a $b 0" && + sed -e 1,3d $a >check-2 && + cmp check-1 check-2 && + rm -f $o $a $b +' + +test_expect_success 'custom merge backend' ' + + git reset --hard anchor && + git config --replace-all \ + merge.driver "custom ./custom-merge %O %A %B 1" && + + if git merge master + then + echo "Eh? should have conflicted" + false + else + echo "Ok, conflicted" + fi && + + cmp binary union && + sed -e 1,3d text >check-1 && + o=$(git-unpack-file master^:text) && + a=$(git-unpack-file anchor:text) && + b=$(git-unpack-file master:text) && + sh -c "./custom-merge $o $a $b 0" && + sed -e 1,3d $a >check-2 && + cmp check-1 check-2 && + rm -f $o $a $b +' + test_done -- cgit v0.10.2-6-g49f6 From be89cb239e8ec02e23015675cc8b2d60992a6cfc Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 18 Apr 2007 01:47:21 -0700 Subject: Allow the default low-level merge driver to be configured. When no 'merge' attribute is given to a path, merge-recursive uses the built-in xdl-merge as the low-level merge driver. A new configuration item 'merge.default' can name a low-level merge driver of user's choice to be used instead. Signed-off-by: Junio C Hamano diff --git a/merge-recursive.c b/merge-recursive.c index 8ec18ad..5983000 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -839,12 +839,18 @@ static struct user_merge_fn { char *cmdline; char b_[1]; } *ll_user_merge_fns, **ll_user_merge_fns_tail; +static const char *default_ll_merge; static int read_merge_config(const char *var, const char *value) { struct user_merge_fn *fn; int blen, nlen; + if (!strcmp(var, "merge.default")) { + default_ll_merge = strdup(value); + return 0; + } + if (strcmp(var, "merge.driver")) return 0; if (!value) @@ -900,8 +906,12 @@ static ll_merge_fn find_ll_merge_fn(void *merge_attr, const char **cmdline) return ll_xdl_merge; else if (ATTR_FALSE(merge_attr)) return ll_binary_merge; - else if (ATTR_UNSET(merge_attr)) - return ll_xdl_merge; + else if (ATTR_UNSET(merge_attr)) { + if (!default_ll_merge) + return ll_xdl_merge; + else + name = default_ll_merge; + } else name = merge_attr; -- cgit v0.10.2-6-g49f6 From 153920da5b62024c0aceef23252b82ad18e5fe22 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 18 Apr 2007 11:27:32 -0700 Subject: Custom low-level merge driver: change the configuration scheme. This changes the configuration syntax for defining a low-level merge driver to be: [merge "<>"] driver = "<>" name = "<>" which is much nicer to read and is extensible. Credit goes to Martin Waitz and Linus. In addition, when we use an external low-level merge driver, it is reported as an extra output from merge-recursive, using the value of merge.<.name variable. The demonstration in t6026 has also been updated. Signed-off-by: Junio C Hamano diff --git a/merge-recursive.c b/merge-recursive.c index 5983000..0f5c28e 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -661,14 +661,31 @@ static void fill_mm(const unsigned char *sha1, mmfile_t *mm) mm->size = size; } -/* Low-level merge functions */ -typedef int (*ll_merge_fn)(const char *cmd, +/* + * Customizable low-level merge drivers support. + */ + +struct ll_merge_driver; +typedef int (*ll_merge_fn)(const struct ll_merge_driver *, + const char *path, mmfile_t *orig, mmfile_t *src1, const char *name1, mmfile_t *src2, const char *name2, mmbuffer_t *result); -static int ll_xdl_merge(const char *cmd__unused, +struct ll_merge_driver { + const char *name; + const char *description; + ll_merge_fn fn; + struct ll_merge_driver *next; + char *cmdline; +}; + +/* + * Built-in low-levels + */ +static int ll_xdl_merge(const struct ll_merge_driver *drv_unused, + const char *path_unused, mmfile_t *orig, mmfile_t *src1, const char *name1, mmfile_t *src2, const char *name2, @@ -684,7 +701,8 @@ static int ll_xdl_merge(const char *cmd__unused, result); } -static int ll_union_merge(const char *cmd__unused, +static int ll_union_merge(const struct ll_merge_driver *drv_unused, + const char *path_unused, mmfile_t *orig, mmfile_t *src1, const char *name1, mmfile_t *src2, const char *name2, @@ -694,8 +712,8 @@ static int ll_union_merge(const char *cmd__unused, long size; const int marker_size = 7; - int status = ll_xdl_merge(cmd__unused, orig, - src1, NULL, src2, NULL, result); + int status = ll_xdl_merge(drv_unused, path_unused, + orig, src1, NULL, src2, NULL, result); if (status <= 0) return status; size = result->size; @@ -726,7 +744,8 @@ static int ll_union_merge(const char *cmd__unused, return 0; } -static int ll_binary_merge(const char *cmd__unused, +static int ll_binary_merge(const struct ll_merge_driver *drv_unused, + const char *path_unused, mmfile_t *orig, mmfile_t *src1, const char *name1, mmfile_t *src2, const char *name2, @@ -745,14 +764,13 @@ static int ll_binary_merge(const char *cmd__unused, return 1; } -static struct { - const char *name; - ll_merge_fn fn; -} ll_merge_fns[] = { - { "binary", ll_binary_merge }, - { "text", ll_xdl_merge }, - { "union", ll_union_merge }, - { NULL, NULL }, +#define LL_BINARY_MERGE 0 +#define LL_TEXT_MERGE 1 +#define LL_UNION_MERGE 2 +static struct ll_merge_driver ll_merge_drv[] = { + { "binary", "built-in binary merge", ll_binary_merge }, + { "text", "built-in 3-way text merge", ll_xdl_merge }, + { "union", "built-in union merge", ll_union_merge }, }; static void create_temp(mmfile_t *src, char *path) @@ -768,7 +786,11 @@ static void create_temp(mmfile_t *src, char *path) close(fd); } -static int ll_ext_merge(const char *cmd, +/* + * User defined low-level merge driver support. + */ +static int ll_ext_merge(const struct ll_merge_driver *fn, + const char *path, mmfile_t *orig, mmfile_t *src1, const char *name1, mmfile_t *src2, const char *name2, @@ -796,7 +818,10 @@ static int ll_ext_merge(const char *cmd, interp_set_entry(table, 1, temp[1]); interp_set_entry(table, 2, temp[2]); - interpolate(cmdbuf, sizeof(cmdbuf), cmd, table, 3); + output(1, "merging %s using %s", path, + fn->description ? fn->description : fn->name); + + interpolate(cmdbuf, sizeof(cmdbuf), fn->cmdline, table, 3); memset(&child, 0, sizeof(child)); child.argv = args; @@ -833,101 +858,124 @@ static int ll_ext_merge(const char *cmd, /* * merge.default and merge.driver configuration items */ -static struct user_merge_fn { - struct user_merge_fn *next; - const char *name; - char *cmdline; - char b_[1]; -} *ll_user_merge_fns, **ll_user_merge_fns_tail; +static struct ll_merge_driver *ll_user_merge, **ll_user_merge_tail; static const char *default_ll_merge; static int read_merge_config(const char *var, const char *value) { - struct user_merge_fn *fn; - int blen, nlen; + struct ll_merge_driver *fn; + const char *ep, *name; + int namelen; if (!strcmp(var, "merge.default")) { - default_ll_merge = strdup(value); + if (value) + default_ll_merge = strdup(value); return 0; } - if (strcmp(var, "merge.driver")) + /* + * We are not interested in anything but "merge..variable"; + * especially, we do not want to look at variables such as + * "merge.summary", "merge.tool", and "merge.verbosity". + */ + if (prefixcmp(var, "merge.") || (ep = strrchr(var, '.')) == var + 6) return 0; - if (!value) - return error("%s: lacks value", var); + /* - * merge.driver is a multi-valued configuration, whose value is - * of form: - * - * name command-line - * - * The command-line will be interpolated with the following - * tokens and is given to the shell: - * - * %O - temporary file name for the merge base. - * %A - temporary file name for our version. - * %B - temporary file name for the other branches' version. - * - * The external merge driver should write the results in the file - * named by %A, and signal that it has done with exit status 0. + * Find existing one as we might be processing merge..var2 + * after seeing merge..var1. */ - for (nlen = -1, blen = 0; value[blen]; blen++) - if (nlen < 0 && isspace(value[blen])) - nlen = blen; - if (nlen < 0) - return error("%s '%s': lacks command line", var, value); - fn = xcalloc(1, sizeof(struct user_merge_fn) + blen + 1); - memcpy(fn->b_, value, blen + 1); - fn->name = fn->b_; - fn->b_[nlen] = 0; - fn->cmdline = fn->b_ + nlen + 1; - fn->next = *ll_user_merge_fns_tail; - *ll_user_merge_fns_tail = fn; + name = var + 6; + namelen = ep - name; + for (fn = ll_user_merge; fn; fn = fn->next) + if (!strncmp(fn->name, name, namelen) && !fn->name[namelen]) + break; + if (!fn) { + char *namebuf; + fn = xcalloc(1, sizeof(struct ll_merge_driver)); + namebuf = xmalloc(namelen + 1); + memcpy(namebuf, name, namelen); + namebuf[namelen] = 0; + fn->name = namebuf; + fn->fn = ll_ext_merge; + fn->next = *ll_user_merge_tail; + *ll_user_merge_tail = fn; + } + + ep++; + + if (!strcmp("name", ep)) { + if (!value) + return error("%s: lacks value", var); + fn->description = strdup(value); + return 0; + } + + if (!strcmp("driver", ep)) { + if (!value) + return error("%s: lacks value", var); + /* + * merge..driver specifies the command line: + * + * command-line + * + * The command-line will be interpolated with the following + * tokens and is given to the shell: + * + * %O - temporary file name for the merge base. + * %A - temporary file name for our version. + * %B - temporary file name for the other branches' version. + * + * The external merge driver should write the results in the + * file named by %A, and signal that it has done with zero exit + * status. + */ + fn->cmdline = strdup(value); + return 0; + } + return 0; } static void initialize_ll_merge(void) { - if (ll_user_merge_fns_tail) + if (ll_user_merge_tail) return; - ll_user_merge_fns_tail = &ll_user_merge_fns; + ll_user_merge_tail = &ll_user_merge; git_config(read_merge_config); } -static ll_merge_fn find_ll_merge_fn(void *merge_attr, const char **cmdline) +static const struct ll_merge_driver *find_ll_merge_driver(void *merge_attr) { - struct user_merge_fn *fn; + struct ll_merge_driver *fn; const char *name; int i; initialize_ll_merge(); if (ATTR_TRUE(merge_attr)) - return ll_xdl_merge; + return &ll_merge_drv[LL_TEXT_MERGE]; else if (ATTR_FALSE(merge_attr)) - return ll_binary_merge; + return &ll_merge_drv[LL_BINARY_MERGE]; else if (ATTR_UNSET(merge_attr)) { if (!default_ll_merge) - return ll_xdl_merge; + return &ll_merge_drv[LL_TEXT_MERGE]; else name = default_ll_merge; } else name = merge_attr; - for (fn = ll_user_merge_fns; fn; fn = fn->next) { - if (!strcmp(fn->name, name)) { - *cmdline = fn->cmdline; - return ll_ext_merge; - } - } + for (fn = ll_user_merge; fn; fn = fn->next) + if (!strcmp(fn->name, name)) + return fn; - for (i = 0; ll_merge_fns[i].name; i++) - if (!strcmp(ll_merge_fns[i].name, name)) - return ll_merge_fns[i].fn; + for (i = 0; i < ARRAY_SIZE(ll_merge_drv); i++) + if (!strcmp(ll_merge_drv[i].name, name)) + return &ll_merge_drv[i]; /* default to the 3-way */ - return ll_xdl_merge; + return &ll_merge_drv[LL_TEXT_MERGE]; } static void *git_path_check_merge(const char *path) @@ -953,8 +1001,7 @@ static int ll_merge(mmbuffer_t *result_buf, char *name1, *name2; int merge_status; void *merge_attr; - ll_merge_fn fn; - const char *driver = NULL; + const struct ll_merge_driver *driver; name1 = xstrdup(mkpath("%s:%s", branch1, a->path)); name2 = xstrdup(mkpath("%s:%s", branch2, b->path)); @@ -964,10 +1011,11 @@ static int ll_merge(mmbuffer_t *result_buf, fill_mm(b->sha1, &src2); merge_attr = git_path_check_merge(a->path); - fn = find_ll_merge_fn(merge_attr, &driver); + driver = find_ll_merge_driver(merge_attr); - merge_status = fn(driver, &orig, - &src1, name1, &src2, name2, result_buf); + merge_status = driver->fn(driver, a->path, + &orig, &src1, name1, &src2, name2, + result_buf); free(name1); free(name2); diff --git a/t/t6026-merge-attr.sh b/t/t6026-merge-attr.sh index 1732b60..56fc341 100755 --- a/t/t6026-merge-attr.sh +++ b/t/t6026-merge-attr.sh @@ -98,7 +98,9 @@ test_expect_success 'custom merge backend' ' git reset --hard anchor && git config --replace-all \ - merge.driver "custom ./custom-merge %O %A %B 0" && + merge.custom.driver "./custom-merge %O %A %B 0" && + git config --replace-all \ + merge.custom.name "custom merge driver for testing" && git merge master && @@ -117,7 +119,9 @@ test_expect_success 'custom merge backend' ' git reset --hard anchor && git config --replace-all \ - merge.driver "custom ./custom-merge %O %A %B 1" && + merge.custom.driver "./custom-merge %O %A %B 1" && + git config --replace-all \ + merge.custom.name "custom merge driver for testing" && if git merge master then -- cgit v0.10.2-6-g49f6 From 3086486d326b00ce308208e62e0e0de831f3563b Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 18 Apr 2007 12:18:25 -0700 Subject: Allow low-level driver to specify different behaviour during internal merge. This allows [merge "drivername"] to have a variable "recursive" that names a different low-level merge driver to be used when merging common ancestors to come up with a virtual ancestor. Signed-off-by: Junio C Hamano diff --git a/merge-recursive.c b/merge-recursive.c index 0f5c28e..7b5ca8e 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -677,6 +677,7 @@ struct ll_merge_driver { const char *name; const char *description; ll_merge_fn fn; + const char *recursive; struct ll_merge_driver *next; char *cmdline; }; @@ -934,6 +935,13 @@ static int read_merge_config(const char *var, const char *value) return 0; } + if (!strcmp("recursive", ep)) { + if (!value) + return error("%s: lacks value", var); + fn->recursive = strdup(value); + return 0; + } + return 0; } @@ -1013,6 +1021,10 @@ static int ll_merge(mmbuffer_t *result_buf, merge_attr = git_path_check_merge(a->path); driver = find_ll_merge_driver(merge_attr); + if (index_only && driver->recursive) { + merge_attr = git_attr(driver->recursive, strlen(driver->recursive)); + driver = find_ll_merge_driver(merge_attr); + } merge_status = driver->fn(driver, a->path, &orig, &src1, name1, &src2, name2, result_buf); -- cgit v0.10.2-6-g49f6 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 diff --git a/attr.c b/attr.c index b3496a6..285e689 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 8ec2d3d..f1c2038 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 6983a73..9d77f76 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 68bb70f..a5f60c7 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 a32078e..5f50186 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 7b5ca8e..ec8438b 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 v0.10.2-6-g49f6 From 15ba3af2d5056313fa19ceb0cb7f7cb3cdd54f16 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 18 Apr 2007 19:05:57 -0700 Subject: Counto-fix in merge-recursive When the configuration has variables unrelated to low-level merge drivers (e.g. merge.summary), the code failed to ignore them but did something totally senseless. Signed-off-by: Junio C Hamano diff --git a/merge-recursive.c b/merge-recursive.c index ec8438b..65c018b 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -809,6 +809,9 @@ static int ll_ext_merge(const struct ll_merge_driver *fn, int status, fd, i; struct stat st; + if (fn->cmdline == NULL) + die("custom merge driver %s lacks command line.", fn->name); + result->ptr = NULL; result->size = 0; create_temp(orig, temp[0]); @@ -879,7 +882,7 @@ static int read_merge_config(const char *var, const char *value) * especially, we do not want to look at variables such as * "merge.summary", "merge.tool", and "merge.verbosity". */ - if (prefixcmp(var, "merge.") || (ep = strrchr(var, '.')) == var + 6) + if (prefixcmp(var, "merge.") || (ep = strrchr(var, '.')) == var + 5) return 0; /* -- cgit v0.10.2-6-g49f6 From d56dbd67097a84dac1dbdf28c1a254f63f93724a Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 18 Apr 2007 19:22:57 -0700 Subject: Simplify code to find recursive merge driver. There is no need to intern the string to git_attr, as we are already dealing with the name of the driver there. Signed-off-by: Junio C Hamano diff --git a/merge-recursive.c b/merge-recursive.c index 65c018b..96e461c 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -1024,13 +1024,8 @@ static int ll_merge(mmbuffer_t *result_buf, ll_driver_name = git_path_check_merge(a->path); driver = find_ll_merge_driver(ll_driver_name); - if (index_only && 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); - } + if (index_only && driver->recursive) + driver = find_ll_merge_driver(driver->recursive); merge_status = driver->fn(driver, a->path, &orig, &src1, name1, &src2, name2, result_buf); -- cgit v0.10.2-6-g49f6 From 4392da4d5d7585a9defa6869517cb354f7460f35 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Thu, 19 Apr 2007 20:47:04 -0700 Subject: Documentation: support manual section (5) - file formats. Signed-off-by: Junio C Hamano diff --git a/Documentation/Makefile b/Documentation/Makefile index a637d8d..f4c6a80 100644 --- a/Documentation/Makefile +++ b/Documentation/Makefile @@ -2,9 +2,10 @@ MAN1_TXT= \ $(filter-out $(addsuffix .txt, $(ARTICLES) $(SP_ARTICLES)), \ $(wildcard git-*.txt)) \ gitk.txt +MAN5_TXT= MAN7_TXT=git.txt -DOC_HTML=$(patsubst %.txt,%.html,$(MAN1_TXT) $(MAN7_TXT)) +DOC_HTML=$(patsubst %.txt,%.html,$(MAN1_TXT) $(MAN5_TXT) $(MAN7_TXT)) ARTICLES = tutorial ARTICLES += tutorial-2 @@ -23,12 +24,14 @@ SP_ARTICLES = howto/revert-branch-rebase user-manual DOC_HTML += $(patsubst %,%.html,$(ARTICLES) $(SP_ARTICLES)) DOC_MAN1=$(patsubst %.txt,%.1,$(MAN1_TXT)) +DOC_MAN5=$(patsubst %.txt,%.5,$(MAN1_TXT)) DOC_MAN7=$(patsubst %.txt,%.7,$(MAN7_TXT)) prefix?=$(HOME) bindir?=$(prefix)/bin mandir?=$(prefix)/man man1dir=$(mandir)/man1 +man5dir=$(mandir)/man5 man7dir=$(mandir)/man7 # DESTDIR= @@ -53,15 +56,19 @@ all: html man html: $(DOC_HTML) -$(DOC_HTML) $(DOC_MAN1) $(DOC_MAN7): asciidoc.conf +$(DOC_HTML) $(DOC_MAN1) $(DOC_MAN5) $(DOC_MAN7): asciidoc.conf -man: man1 man7 +man: man1 man5 man7 man1: $(DOC_MAN1) +man5: $(DOC_MAN5) man7: $(DOC_MAN7) install: man - $(INSTALL) -d -m755 $(DESTDIR)$(man1dir) $(DESTDIR)$(man7dir) + $(INSTALL) -d -m755 $(DESTDIR)$(man1dir) + $(INSTALL) -d -m755 $(DESTDIR)$(man5dir) + $(INSTALL) -d -m755 $(DESTDIR)$(man7dir) $(INSTALL) -m644 $(DOC_MAN1) $(DESTDIR)$(man1dir) + : $(INSTALL) -m644 $(DOC_MAN5) $(DESTDIR)$(man5dir) $(INSTALL) -m644 $(DOC_MAN7) $(DESTDIR)$(man7dir) @@ -99,7 +106,7 @@ cmd-list.made: cmd-list.perl $(MAN1_TXT) git.7 git.html: git.txt core-intro.txt clean: - rm -f *.xml *.xml+ *.html *.html+ *.1 *.7 howto-index.txt howto/*.html doc.dep + rm -f *.xml *.xml+ *.html *.html+ *.1 *.5 *.7 howto-index.txt howto/*.html doc.dep rm -f $(cmds_txt) *.made %.html : %.txt @@ -109,7 +116,7 @@ clean: sed -e 's/@@GIT_VERSION@@/$(GIT_VERSION)/g' >$@+ mv $@+ $@ -%.1 %.7 : %.xml +%.1 %.5 %.7 : %.xml xmlto -m callouts.xsl man $< %.xml : %.txt diff --git a/Makefile b/Makefile index 4a399dd..e14cc10 100644 --- a/Makefile +++ b/Makefile @@ -1030,9 +1030,10 @@ dist-doc: gzip -n -9 -f $(htmldocs).tar : rm -fr .doc-tmp-dir - mkdir .doc-tmp-dir .doc-tmp-dir/man1 .doc-tmp-dir/man7 + mkdir -p .doc-tmp-dir/man1 .doc-tmp-dir/man5 .doc-tmp-dir/man7 $(MAKE) -C Documentation DESTDIR=./ \ man1dir=../.doc-tmp-dir/man1 \ + man5dir=../.doc-tmp-dir/man5 \ man7dir=../.doc-tmp-dir/man7 \ install cd .doc-tmp-dir && $(TAR) cf ../$(manpages).tar . -- cgit v0.10.2-6-g49f6 From 163b95919428cd7d782af91296e0b886683f2daa Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Thu, 19 Apr 2007 22:37:19 -0700 Subject: Update 'crlf' attribute semantics. This updates the semantics of 'crlf' so that .gitattributes file can say "this is text, even though it may look funny". Setting the `crlf` attribute on a path is meant to mark the path as a "text" file. 'core.autocrlf' conversion takes place without guessing the content type by inspection. Unsetting the `crlf` attribute on a path is meant to mark the path as a "binary" file. The path never goes through line endings conversion upon checkin/checkout. Unspecified `crlf` attribute tells git to apply the `core.autocrlf` conversion when the file content looks like text. Setting the `crlf` attribut to string value "input" is similar to setting the attribute to `true`, but also forces git to act as if `core.autocrlf` is set to `input` for the path. Signed-off-by: Junio C Hamano diff --git a/convert.c b/convert.c index a5f60c7..da64253 100644 --- a/convert.c +++ b/convert.c @@ -10,6 +10,11 @@ * translation when the "auto_crlf" option is set. */ +#define CRLF_GUESS (-1) +#define CRLF_BINARY 0 +#define CRLF_TEXT 1 +#define CRLF_INPUT 2 + struct text_stat { /* CR, LF and CRLF counts */ unsigned cr, lf, crlf; @@ -74,13 +79,13 @@ 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 guess) +static int crlf_to_git(const char *path, char **bufp, unsigned long *sizep, int action) { char *buffer, *nbuf; unsigned long size, nsize; struct text_stat stats; - if (guess && !auto_crlf) + if ((action == CRLF_BINARY) || (action == CRLF_GUESS && !auto_crlf)) return 0; size = *sizep; @@ -94,7 +99,7 @@ static int crlf_to_git(const char *path, char **bufp, unsigned long *sizep, int if (!stats.cr) return 0; - if (guess) { + if (action == CRLF_GUESS) { /* * We're currently not going to even try to convert stuff * that has bare CR characters. Does anybody do that crazy @@ -119,7 +124,12 @@ static int crlf_to_git(const char *path, char **bufp, unsigned long *sizep, int *bufp = nbuf; *sizep = nsize; - if (guess) { + if (action == CRLF_GUESS) { + /* + * If we guessed, we already know we rejected a file with + * lone CR, and we can strip a CR without looking at what + * follow it. + */ do { unsigned char c = *buffer++; if (c != '\r') @@ -136,24 +146,15 @@ static int crlf_to_git(const char *path, char **bufp, unsigned long *sizep, int return 1; } -static int autocrlf_to_git(const char *path, char **bufp, unsigned long *sizep) -{ - return crlf_to_git(path, bufp, sizep, 1); -} - -static int forcecrlf_to_git(const char *path, char **bufp, unsigned long *sizep) -{ - return crlf_to_git(path, bufp, sizep, 0); -} - -static int crlf_to_working_tree(const char *path, char **bufp, unsigned long *sizep, int guess) +static int crlf_to_worktree(const char *path, char **bufp, unsigned long *sizep, int action) { char *buffer, *nbuf; unsigned long size, nsize; struct text_stat stats; unsigned char last; - if (guess && auto_crlf <= 0) + if ((action == CRLF_BINARY) || (action == CRLF_INPUT) || + (action == CRLF_GUESS && auto_crlf <= 0)) return 0; size = *sizep; @@ -171,7 +172,7 @@ static int crlf_to_working_tree(const char *path, char **bufp, unsigned long *si if (stats.lf == stats.crlf) return 0; - if (guess) { + 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; @@ -200,16 +201,6 @@ static int crlf_to_working_tree(const char *path, char **bufp, unsigned long *si return 1; } -static int autocrlf_to_working_tree(const char *path, char **bufp, unsigned long *sizep) -{ - return crlf_to_working_tree(path, bufp, sizep, 1); -} - -static int forcecrlf_to_working_tree(const char *path, char **bufp, unsigned long *sizep) -{ - return crlf_to_working_tree(path, bufp, sizep, 0); -} - static void setup_crlf_check(struct git_attr_check *check) { static struct git_attr *attr_crlf; @@ -228,38 +219,24 @@ static int git_path_check_crlf(const char *path) if (!git_checkattr(path, 1, &attr_crlf_check)) { const char *value = attr_crlf_check.value; if (ATTR_TRUE(value)) - return 1; + return CRLF_TEXT; else if (ATTR_FALSE(value)) - return 0; + return CRLF_BINARY; else if (ATTR_UNSET(value)) ; - else - die("unknown value %s given to 'crlf' attribute", - (char *)value); + else if (!strcmp(value, "input")) + return CRLF_INPUT; + /* fallthru */ } - return -1; + return CRLF_GUESS; } int convert_to_git(const char *path, char **bufp, unsigned long *sizep) { - switch (git_path_check_crlf(path)) { - case 0: - return 0; - case 1: - return forcecrlf_to_git(path, bufp, sizep); - default: - return autocrlf_to_git(path, bufp, sizep); - } + return crlf_to_git(path, bufp, sizep, git_path_check_crlf(path)); } int convert_to_working_tree(const char *path, char **bufp, unsigned long *sizep) { - switch (git_path_check_crlf(path)) { - case 0: - return 0; - case 1: - return forcecrlf_to_working_tree(path, bufp, sizep); - default: - return autocrlf_to_working_tree(path, bufp, sizep); - } + return crlf_to_worktree(path, bufp, sizep, git_path_check_crlf(path)); } diff --git a/t/t0020-crlf.sh b/t/t0020-crlf.sh index cf84f0a..fe1dfd0 100755 --- a/t/t0020-crlf.sh +++ b/t/t0020-crlf.sh @@ -4,6 +4,10 @@ test_description='CRLF conversion' . ./test-lib.sh +q_to_nul () { + tr Q '\0' +} + append_cr () { sed -e 's/$/Q/' | tr Q '\015' } @@ -20,6 +24,7 @@ test_expect_success setup ' for w in Hello world how are you; do echo $w; done >one && mkdir dir && for w in I am very very fine thank you; do echo $w; done >dir/two && + for w in Oh here is NULQin text here; do echo $w; done | q_to_nul >three && git add . && git commit -m initial && @@ -27,6 +32,7 @@ test_expect_success setup ' one=`git rev-parse HEAD:one` && dir=`git rev-parse HEAD:dir` && two=`git rev-parse HEAD:dir/two` && + three=`git rev-parse HEAD:three` && for w in Some extra lines here; do echo $w; done >>one && git diff >patch.file && @@ -38,7 +44,7 @@ test_expect_success setup ' test_expect_success 'update with autocrlf=input' ' - rm -f tmp one dir/two && + rm -f tmp one dir/two three && git read-tree --reset -u HEAD && git repo-config core.autocrlf input && @@ -62,7 +68,7 @@ test_expect_success 'update with autocrlf=input' ' test_expect_success 'update with autocrlf=true' ' - rm -f tmp one dir/two && + rm -f tmp one dir/two three && git read-tree --reset -u HEAD && git repo-config core.autocrlf true && @@ -86,7 +92,7 @@ test_expect_success 'update with autocrlf=true' ' test_expect_success 'checkout with autocrlf=true' ' - rm -f tmp one dir/two && + rm -f tmp one dir/two three && git repo-config core.autocrlf true && git read-tree --reset -u HEAD && @@ -110,7 +116,7 @@ test_expect_success 'checkout with autocrlf=true' ' test_expect_success 'checkout with autocrlf=input' ' - rm -f tmp one dir/two && + rm -f tmp one dir/two three && git repo-config core.autocrlf input && git read-tree --reset -u HEAD && @@ -136,7 +142,7 @@ test_expect_success 'checkout with autocrlf=input' ' test_expect_success 'apply patch (autocrlf=input)' ' - rm -f tmp one dir/two && + rm -f tmp one dir/two three && git repo-config core.autocrlf input && git read-tree --reset -u HEAD && @@ -149,7 +155,7 @@ test_expect_success 'apply patch (autocrlf=input)' ' test_expect_success 'apply patch --cached (autocrlf=input)' ' - rm -f tmp one dir/two && + rm -f tmp one dir/two three && git repo-config core.autocrlf input && git read-tree --reset -u HEAD && @@ -162,7 +168,7 @@ test_expect_success 'apply patch --cached (autocrlf=input)' ' test_expect_success 'apply patch --index (autocrlf=input)' ' - rm -f tmp one dir/two && + rm -f tmp one dir/two three && git repo-config core.autocrlf input && git read-tree --reset -u HEAD && @@ -176,7 +182,7 @@ test_expect_success 'apply patch --index (autocrlf=input)' ' test_expect_success 'apply patch (autocrlf=true)' ' - rm -f tmp one dir/two && + rm -f tmp one dir/two three && git repo-config core.autocrlf true && git read-tree --reset -u HEAD && @@ -189,7 +195,7 @@ test_expect_success 'apply patch (autocrlf=true)' ' test_expect_success 'apply patch --cached (autocrlf=true)' ' - rm -f tmp one dir/two && + rm -f tmp one dir/two three && git repo-config core.autocrlf true && git read-tree --reset -u HEAD && @@ -202,7 +208,7 @@ test_expect_success 'apply patch --cached (autocrlf=true)' ' test_expect_success 'apply patch --index (autocrlf=true)' ' - rm -f tmp one dir/two && + rm -f tmp one dir/two three && git repo-config core.autocrlf true && git read-tree --reset -u HEAD && @@ -216,8 +222,8 @@ test_expect_success 'apply patch --index (autocrlf=true)' ' test_expect_success '.gitattributes says two is binary' ' + rm -f tmp one dir/two three && echo "two -crlf" >.gitattributes && - rm -f tmp one dir/two && git repo-config core.autocrlf true && git read-tree --reset -u HEAD && @@ -235,6 +241,52 @@ test_expect_success '.gitattributes says two is binary' ' else echo "Huh?" false + fi && + + if remove_cr three >/dev/null + then + echo "Huh?" + false + else + : happy + fi +' + +test_expect_success '.gitattributes says two is input' ' + + rm -f tmp one dir/two three && + echo "two crlf=input" >.gitattributes && + git read-tree --reset -u HEAD && + + if remove_cr dir/two >/dev/null + then + echo "Huh?" + false + else + : happy + fi +' + +test_expect_success '.gitattributes says two and three are text' ' + + rm -f tmp one dir/two three && + echo "t* crlf" >.gitattributes && + git read-tree --reset -u HEAD && + + if remove_cr dir/two >/dev/null + then + : happy + else + echo "Huh?" + false + fi && + + if remove_cr three >/dev/null + then + : happy + else + echo "Huh?" + false fi ' -- cgit v0.10.2-6-g49f6 From 88e7fdf2cb436e068434241b0519577293055c19 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Thu, 19 Apr 2007 20:48:03 -0700 Subject: Document gitattributes(5) Signed-off-by: Junio C Hamano diff --git a/Documentation/Makefile b/Documentation/Makefile index f4c6a80..8d3617d 100644 --- a/Documentation/Makefile +++ b/Documentation/Makefile @@ -2,7 +2,7 @@ MAN1_TXT= \ $(filter-out $(addsuffix .txt, $(ARTICLES) $(SP_ARTICLES)), \ $(wildcard git-*.txt)) \ gitk.txt -MAN5_TXT= +MAN5_TXT=gitattributes.txt MAN7_TXT=git.txt DOC_HTML=$(patsubst %.txt,%.html,$(MAN1_TXT) $(MAN5_TXT) $(MAN7_TXT)) @@ -68,7 +68,7 @@ install: man $(INSTALL) -d -m755 $(DESTDIR)$(man5dir) $(INSTALL) -d -m755 $(DESTDIR)$(man7dir) $(INSTALL) -m644 $(DOC_MAN1) $(DESTDIR)$(man1dir) - : $(INSTALL) -m644 $(DOC_MAN5) $(DESTDIR)$(man5dir) + $(INSTALL) -m644 $(DOC_MAN5) $(DESTDIR)$(man5dir) $(INSTALL) -m644 $(DOC_MAN7) $(DESTDIR)$(man7dir) diff --git a/Documentation/config.txt b/Documentation/config.txt index 7e41ca6..a130846 100644 --- a/Documentation/config.txt +++ b/Documentation/config.txt @@ -499,6 +499,19 @@ merge.verbosity:: conflicts, 2 outputs conflicts and file changes. Level 5 and above outputs debugging information. The default is level 2. +merge..name:: + Defines a human readable name for a custom low-level + merge driver. See gitlink:gitattributes[5] for details. + +merge..driver:: + Defines the command that implements a custom low-level + merge driver. See gitlink:gitattributes[5] for details. + +merge..recursive:: + Names a low-level merge driver to be used when + performing an internal merge between common ancestors. + See gitlink:gitattributes[5] for details. + pack.window:: The size of the window used by gitlink:git-pack-objects[1] when no window size is given on the command line. Defaults to 10. diff --git a/Documentation/gitattributes.txt b/Documentation/gitattributes.txt new file mode 100644 index 0000000..ece58ab --- /dev/null +++ b/Documentation/gitattributes.txt @@ -0,0 +1,285 @@ +gitattributes(5) +================ + +NAME +---- +gitattributes - defining attributes per path + +SYNOPSIS +-------- +.gitattributes + + +DESCRIPTION +----------- + +A `gitattributes` file is a simple text file that gives +`attributes` to pathnames. + +Each line in `gitattributes` file is of form: + + glob attr1 attr2 ... + +That is, a glob pattern followed by an attributes list, +separated by whitespaces. When the glob pattern matches the +path in question, the attributes listed on the line are given to +the path. + +Each attribute can be in one of these states for a given path: + +Set:: + + The path has the attribute with special value "true"; + this is specified by listing only the name of the + attribute in the attribute list. + +Unset:: + + The path has the attribute with special value "false"; + this is specified by listing the name of the attribute + prefixed with a dash `-` in the attribute list. + +Set to a value:: + + The path has the attribute with specified string value; + this is specified by listing the name of the attribute + followed by an equal sign `=` and its value in the + attribute list. + +Unspecified:: + + No glob pattern matches the path, and nothing says if + the path has or does not have the attribute. + +When more than one glob pattern matches the path, a later line +overrides an earlier line. + +When deciding what attributes are assigned to a path, git +consults `$GIT_DIR/info/attributes` file (which has the highest +precedence), `.gitattributes` file in the same directory as the +path in question, and its parent directories (the further the +directory that contains `.gitattributes` is from the path in +question, the lower its precedence). + +Sometimes you would need to override an setting of an attribute +for a path to `unspecified` state. This can be done by listing +the name of the attribute prefixed with an exclamation point `!`. + + +EFFECTS +------- + +Certain operations by git can be influenced by assigning +particular attributes to a path. Currently, three operations +are attributes-aware. + +Checking-out and checking-in +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +The attribute `crlf` affects how the contents stored in the +repository are copied to the working tree files when commands +such as `git checkout` and `git merge` run. It also affects how +git stores the contents you prepare in the working tree in the +repository upon `git add` and `git commit`. + +Set:: + + Setting the `crlf` attribute on a path is meant to mark + the path as a "text" file. 'core.autocrlf' conversion + takes place without guessing the content type by + inspection. + +Unset:: + + Unsetting the `crlf` attribute on a path is meant to + mark the path as a "binary" file. The path never goes + through line endings conversion upon checkin/checkout. + +Unspecified:: + + Unspecified `crlf` attribute tells git to apply the + `core.autocrlf` conversion when the file content looks + like text. + +Set to string value "input":: + + This is similar to setting the attribute to `true`, but + also forces git to act as if `core.autocrlf` is set to + `input` for the path. + +Any other value set to `crlf` attribute is ignored and git acts +as if the attribute is left unspecified. + + +The `core.autocrlf` conversion +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +If the configuration variable `core.autocrlf` is false, no +conversion is done. + +When `core.autocrlf` is true, it means that the platform wants +CRLF line endings for files in the working tree, and you want to +convert them back to the normal LF line endings when checking +in to the repository. + +When `core.autocrlf` is set to "input", line endings are +converted to LF upon checkin, but there is no conversion done +upon checkout. + + +Generating diff text +~~~~~~~~~~~~~~~~~~~~ + +The attribute `diff` affects if `git diff` generates textual +patch for the path or just says `Binary files differ`. + +Set:: + + A path to which the `diff` attribute is set is treated + as text, even when they contain byte values that + normally never appear in text files, such as NUL. + +Unset:: + + A path to which the `diff` attribute is unset will + generate `Binary files differ`. + +Unspecified:: + + A path to which the `diff` attribute is unspecified + first gets its contents inspected, and if it looks like + text, it is treated as text. Otherwise it would + generate `Binary files differ`. + +Any other value set to `diff` attribute is ignored and git acts +as if the attribute is left unspecified. + + +Performing a three-way merge +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +The attribute `merge` affects how three versions of a file is +merged when a file-level merge is necessary during `git merge`, +and other programs such as `git revert` and `git cherry-pick`. + +Set:: + + Built-in 3-way merge driver is used to merge the + contents in a way similar to `merge` command of `RCS` + suite. This is suitable for ordinary text files. + +Unset:: + + Take the version from the current branch as the + tentative merge result, and declare that the merge has + conflicts. This is suitable for binary files that does + not have a well-defined merge semantics. + +Unspecified:: + + By default, this uses the same built-in 3-way merge + driver as is the case the `merge` attribute is set. + However, `merge.default` configuration variable can name + different merge driver to be used for paths to which the + `merge` attribute is unspecified. + +Any other string value:: + + 3-way merge is performed using the specified custom + merge driver. The built-in 3-way merge driver can be + explicitly specified by asking for "text" driver; the + built-in "take the current branch" driver can be + requested by "binary". + + +Defining a custom merge driver +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +The definition of a merge driver is done in `gitconfig` not +`gitattributes` file, so strictly speaking this manual page is a +wrong place to talk about it. However... + +To define a custom merge driver `filfre`, add a section to your +`$GIT_DIR/config` file (or `$HOME/.gitconfig` file) like this: + +---------------------------------------------------------------- +[merge "filfre"] + name = feel-free merge driver + driver = filfre %O %A %B + recursive = binary +---------------------------------------------------------------- + +The `merge.*.name` variable gives the driver a human-readable +name. + +The `merge.*.driver` variable's value is used to construct a +command to run to merge ancestor's version (`%O`), current +version (`%A`) and the other branches' version (`%B`). These +three tokens are replaced with the names of temporary files that +hold the contents of these versions when the command line is +built. + +The merge driver is expected to leave the result of the merge in +the file named with `%A` by overwriting it, and exit with zero +status if it managed to merge them cleanly, or non-zero if there +were conflicts. + +The `merge.*.recursive` variable specifies what other merge +driver to use when the merge driver is called for an internal +merge between common ancestors, when there are more than one. +When left unspecified, the driver itself is used for both +internal merge and the final merge. + + +EXAMPLE +------- + +If you have these three `gitattributes` file: + +---------------------------------------------------------------- +(in $GIT_DIR/info/attributes) + +a* foo !bar -baz + +(in .gitattributes) +abc foo bar baz + +(in t/.gitattributes) +ab* merge=filfre +abc -foo -bar +*.c frotz +---------------------------------------------------------------- + +the attributes given to path `t/abc` are computed as follows: + +1. By examining `t/.gitattributes` (which is in the same + diretory as the path in question), git finds that the first + line matches. `merge` attribute is set. It also finds that + the second line matches, and attributes `foo` and `bar` + are unset. + +2. Then it examines `.gitattributes` (which is in the parent + directory), and finds that the first line matches, but + `t/.gitattributes` file already decided how `merge`, `foo` + and `bar` attributes should be given to this path, so it + leaves `foo` and `bar` unset. Attribute `baz` is set. + +3. Finally it examines `$GIT_DIR/info/gitattributes`. This file + is used to override the in-tree settings. The first line is + a match, and `foo` is set, `bar` is reverted to unspecified + state, and `baz` is unset. + +As the result, the attributes assignement to `t/abc` becomes: + +---------------------------------------------------------------- +foo set to true +bar unspecified +baz set to false +merge set to string value "filfre" +frotz unspecified +---------------------------------------------------------------- + + +GIT +--- +Part of the gitlink:git[7] suite -- cgit v0.10.2-6-g49f6 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 diff --git a/builtin-apply.c b/builtin-apply.c index fd92ef7..ccd342c 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 38ad006..8c804cb 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 da64253..742b895 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 5f50186..1cb1230 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 d72f811..3771209 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 4304fe9..1978d5f 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 v0.10.2-6-g49f6 From e87b1c943a50af9ab51df20b3419cbffa4e75484 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Sat, 21 Apr 2007 00:05:31 -0700 Subject: Fix bogus linked-list management for user defined merge drivers. ll_user_merge_tail is supposed to point at the pointer to be updated to point at a newly created item. Signed-off-by: Junio C Hamano diff --git a/merge-recursive.c b/merge-recursive.c index 96e461c..3d39589 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -902,8 +902,9 @@ static int read_merge_config(const char *var, const char *value) namebuf[namelen] = 0; fn->name = namebuf; fn->fn = ll_ext_merge; - fn->next = *ll_user_merge_tail; + fn->next = NULL; *ll_user_merge_tail = fn; + ll_user_merge_tail = &(fn->next); } ep++; -- cgit v0.10.2-6-g49f6 From 6073ee85719be6d959e74aa667024fcbec44a588 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Fri, 20 Apr 2007 23:44:02 -0700 Subject: convert.c: restructure the attribute checking part. This separates the checkattr() call and interpretation of the returned value specific to the 'crlf' attribute into separate routines, so that we can run a single call to checkattr() to check for more than one attributes, and then interprete what the returned settings mean separately. Signed-off-by: Junio C Hamano diff --git a/convert.c b/convert.c index 742b895..37239ac 100644 --- a/convert.c +++ b/convert.c @@ -200,7 +200,7 @@ static char *crlf_to_worktree(const char *path, const char *src, unsigned long * return buffer; } -static void setup_crlf_check(struct git_attr_check *check) +static void setup_convert_check(struct git_attr_check *check) { static struct git_attr *attr_crlf; @@ -209,33 +209,41 @@ static void setup_crlf_check(struct git_attr_check *check) check->attr = attr_crlf; } -static int git_path_check_crlf(const char *path) +static int git_path_check_crlf(const char *path, struct git_attr_check *check) { - struct git_attr_check attr_crlf_check; - - setup_crlf_check(&attr_crlf_check); - - if (!git_checkattr(path, 1, &attr_crlf_check)) { - const char *value = attr_crlf_check.value; - if (ATTR_TRUE(value)) - return CRLF_TEXT; - else if (ATTR_FALSE(value)) - return CRLF_BINARY; - else if (ATTR_UNSET(value)) - ; - else if (!strcmp(value, "input")) - return CRLF_INPUT; - /* fallthru */ - } + const char *value = check->value; + + if (ATTR_TRUE(value)) + return CRLF_TEXT; + else if (ATTR_FALSE(value)) + return CRLF_BINARY; + else if (ATTR_UNSET(value)) + ; + else if (!strcmp(value, "input")) + return CRLF_INPUT; return CRLF_GUESS; } char *convert_to_git(const char *path, const char *src, unsigned long *sizep) { - return crlf_to_git(path, src, sizep, git_path_check_crlf(path)); + struct git_attr_check check[1]; + int crlf = CRLF_GUESS; + + setup_convert_check(check); + if (!git_checkattr(path, 1, check)) { + crlf = git_path_check_crlf(path, check); + } + return crlf_to_git(path, src, sizep, crlf); } char *convert_to_working_tree(const char *path, const char *src, unsigned long *sizep) { - return crlf_to_worktree(path, src, sizep, git_path_check_crlf(path)); + struct git_attr_check check[1]; + int crlf = CRLF_GUESS; + + setup_convert_check(check); + if (!git_checkattr(path, 1, check)) { + crlf = git_path_check_crlf(path, check); + } + return crlf_to_worktree(path, src, sizep, crlf); } -- cgit v0.10.2-6-g49f6 From 5e635e396020cc08bc21a3e67c20c5294d6d13fd Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Sat, 21 Apr 2007 03:11:10 -0700 Subject: lockfile: record the primary process. The usual process flow is the main process opens and holds the lock to the index, does its thing, perhaps spawning children during the course, and then writes the resulting index out by releaseing the lock. However, the lockfile interface uses atexit(3) to clean it up, without regard to who actually created the lock. This typically leads to a confusing behaviour of lock being released too early when the child exits, and then the parent process when it calls commit_lockfile() finds that it cannot unlock it. This fixes the problem by recording who created and holds the lock, and upon atexit(3) handler, child simply ignores the lockfile the parent created. Signed-off-by: Junio C Hamano diff --git a/cache.h b/cache.h index 8c804cb..faddaf6 100644 --- a/cache.h +++ b/cache.h @@ -209,6 +209,7 @@ extern int refresh_cache(unsigned int flags); struct lock_file { struct lock_file *next; + pid_t owner; char on_list; char filename[PATH_MAX]; }; diff --git a/lockfile.c b/lockfile.c index bed6b21..23db35a 100644 --- a/lockfile.c +++ b/lockfile.c @@ -8,8 +8,11 @@ static const char *alternate_index_output; static void remove_lock_file(void) { + pid_t me = getpid(); + while (lock_file_list) { - if (lock_file_list->filename[0]) + if (lock_file_list->owner == me && + lock_file_list->filename[0]) unlink(lock_file_list->filename); lock_file_list = lock_file_list->next; } @@ -28,6 +31,7 @@ static int lock_file(struct lock_file *lk, const char *path) sprintf(lk->filename, "%s.lock", path); fd = open(lk->filename, O_RDWR | O_CREAT | O_EXCL, 0666); if (0 <= fd) { + lk->owner = getpid(); if (!lk->on_list) { lk->next = lock_file_list; lock_file_list = lk; -- cgit v0.10.2-6-g49f6