{"thread":{"id":"44273","subject":"[PATCH 00/28] Revamping the attr subsystem!","startedAt":"2016-10-11T00:21:28Z","lastAt":"2016-10-11T21:53:19Z","messageCount":44,"participants":["Stefan Beller","Brandon Williams","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":28},"messages":[{"id":"303821","messageId":"20161011002115.23312-1-sbeller@google.com","threadId":"44273","inReplyTo":null,"subject":"[PATCH 00/28] Revamping the attr subsystem!","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-10-11T00:20:47Z","receivedAt":"2016-10-11T00:21:28Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"This is a series that\n* replaces jc/attr-more. I did merge one fixup! commit at the appropriate place,\n  as well as resolving a minor merge conflict when rebasing to the latest master\n* revamps the API of the attr subsystem, such that it can be made thread safe\n  in a later step easily, because the expected changes are only in attr.c\n  \nI think this is a start to the minimal set of changes such that we can rebase\nsb/pathspec-label and sb/submodule-default-paths on top of these eventually.\n\nThanks,\nStefan\n\nJunio C Hamano (24):\n  commit.c: use strchrnul() to scan for one line\n  attr.c: use strchrnul() to scan for one line\n  attr.c: update a stale comment on \"struct match_attr\"\n  attr.c: explain the lack of attr-name syntax check in parse_attr()\n  attr.c: complete a sentence in a comment\n  attr.c: mark where #if DEBUG ends more clearly\n  attr.c: simplify macroexpand_one()\n  attr.c: tighten constness around \"git_attr\" structure\n  attr.c: plug small leak in parse_attr_line()\n  attr: rename function and struct related to checking attributes\n  attr: (re)introduce git_check_attr() and struct git_attr_check\n  attr: convert git_all_attrs() to use \"struct git_attr_check\"\n  attr: convert git_check_attrs() callers to use the new API\n  attr: retire git_check_attrs() API\n  attr: add counted string version of git_check_attr()\n  attr: add counted string version of git_attr()\n  attr: expose validity check for attribute names\n  attr.c: add push_stack() helper\n  attr.c: pass struct git_attr_check down the callchain\n  attr.c: rename a local variable check\n  attr.c: correct ugly hack for git_all_attrs()\n  attr.c: introduce empty_attr_check_elems()\n  attr.c: always pass check[] to collect_some_attrs()\n  attr.c: outline the future plans by heavily commenting\n\nNguyễn Thái Ngọc Duy (1):\n  attr: support quoting pathname patterns in C style\n\nStefan Beller (3):\n  attr: make git_attr_counted static\n  attr: make git_check_attr_counted static\n  attr: convert to new threadsafe API\n\n Documentation/gitattributes.txt               |   8 +-\n Documentation/technical/api-gitattributes.txt | 114 +++++--\n archive.c                                     |  27 +-\n attr.c                                        | 413 ++++++++++++++++++--------\n attr.h                                        |  69 +++--\n builtin/check-attr.c                          |  59 ++--\n builtin/pack-objects.c                        |  22 +-\n commit.c                                      |   3 +-\n convert.c                                     |  44 ++-\n ll-merge.c                                    |  38 +--\n t/t0003-attributes.sh                         |  26 ++\n userdiff.c                                    |  22 +-\n ws.c                                          |  22 +-\n 13 files changed, 561 insertions(+), 306 deletions(-)\n\n-- \n2.10.1.382.ga23ca1b.dirty\n\n"},{"id":"303822","messageId":"20161011002115.23312-2-sbeller@google.com","threadId":"44273","inReplyTo":"20161011002115.23312-1-sbeller@google.com","subject":"[PATCH 01/28] commit.c: use strchrnul() to scan for one line","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-10-11T00:20:48Z","receivedAt":"2016-10-11T00:21:35Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"From: Junio C Hamano <gitster@pobox.com>\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Stefan Beller <sbeller@google.com>\n---\n commit.c | 3 +--\n 1 file changed, 1 insertion(+), 2 deletions(-)\n\ndiff --git a/commit.c b/commit.c\nindex 856fd4a..41b2fdd 100644\n--- a/commit.c\n+++ b/commit.c\n@@ -415,8 +415,7 @@ int find_commit_subject(const char *commit_buffer, const char **subject)\n \t\tp++;\n \tif (*p) {\n \t\tp = skip_blank_lines(p + 2);\n-\t\tfor (eol = p; *eol && *eol != '\\n'; eol++)\n-\t\t\t; /* do nothing */\n+\t\teol = strchrnul(p, '\\n');\n \t} else\n \t\teol = p;\n \n-- \n2.10.1.382.ga23ca1b.dirty\n\n"},{"id":"303823","messageId":"20161011002115.23312-3-sbeller@google.com","threadId":"44273","inReplyTo":"20161011002115.23312-1-sbeller@google.com","subject":"[PATCH 02/28] attr.c: use strchrnul() to scan for one line","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-10-11T00:20:49Z","receivedAt":"2016-10-11T00:21:38Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"From: Junio C Hamano <gitster@pobox.com>\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Stefan Beller <sbeller@google.com>\n---\n attr.c | 4 ++--\n 1 file changed, 2 insertions(+), 2 deletions(-)\n\ndiff --git a/attr.c b/attr.c\nindex eec5d7d..45aec1b 100644\n--- a/attr.c\n+++ b/attr.c\n@@ -402,8 +402,8 @@ static struct attr_stack *read_attr_from_index(const char *path, int macro_ok)\n \tfor (sp = buf; *sp; ) {\n \t\tchar *ep;\n \t\tint more;\n-\t\tfor (ep = sp; *ep && *ep != '\\n'; ep++)\n-\t\t\t;\n+\n+\t\tep = strchrnul(sp, '\\n');\n \t\tmore = (*ep == '\\n');\n \t\t*ep = '\\0';\n \t\thandle_attr_line(res, sp, path, ++lineno, macro_ok);\n-- \n2.10.1.382.ga23ca1b.dirty\n\n"},{"id":"303824","messageId":"20161011002115.23312-4-sbeller@google.com","threadId":"44273","inReplyTo":"20161011002115.23312-1-sbeller@google.com","subject":"[PATCH 03/28] attr.c: update a stale comment on \"struct match_attr\"","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-10-11T00:20:50Z","receivedAt":"2016-10-11T00:21:40Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"From: Junio C Hamano <gitster@pobox.com>\n\nWhen 82dce998 (attr: more matching optimizations from .gitignore,\n2012-10-15) changed a pointer to a string \"*pattern\" into an\nembedded \"struct pattern\" in struct match_attr, it forgot to update\nthe comment that describes the structure.\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Stefan Beller <sbeller@google.com>\n---\n attr.c | 5 ++---\n 1 file changed, 2 insertions(+), 3 deletions(-)\n\ndiff --git a/attr.c b/attr.c\nindex 45aec1b..4ae7801 100644\n--- a/attr.c\n+++ b/attr.c\n@@ -131,9 +131,8 @@ struct pattern {\n  * If is_macro is true, then u.attr is a pointer to the git_attr being\n  * defined.\n  *\n- * If is_macro is false, then u.pattern points at the filename pattern\n- * to which the rule applies.  (The memory pointed to is part of the\n- * memory block allocated for the match_attr instance.)\n+ * If is_macro is false, then u.pat is the filename pattern to which the\n+ * rule applies.\n  *\n  * In either case, num_attr is the number of attributes affected by\n  * this rule, and state is an array listing them.  The attributes are\n-- \n2.10.1.382.ga23ca1b.dirty\n\n"},{"id":"303825","messageId":"20161011002115.23312-8-sbeller@google.com","threadId":"44273","inReplyTo":"20161011002115.23312-1-sbeller@google.com","subject":"[PATCH 07/28] attr.c: simplify macroexpand_one()","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-10-11T00:20:54Z","receivedAt":"2016-10-11T00:21:43Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"From: Junio C Hamano <gitster@pobox.com>\n\nThe double-loop wants to do an early return immediately when one\nmatching macro is found.  Eliminate the extra variable 'a' used for\nthat purpose and rewrite the \"assign the found item to 'a' to make\nit non-NULL and force the loop(s) to terminate\" with a direct return\nfrom there.\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Stefan Beller <sbeller@google.com>\n---\n attr.c | 11 ++++-------\n 1 file changed, 4 insertions(+), 7 deletions(-)\n\ndiff --git a/attr.c b/attr.c\nindex 95416d3..7bfeef3 100644\n--- a/attr.c\n+++ b/attr.c\n@@ -701,24 +701,21 @@ static int fill(const char *path, int pathlen, int basename_offset,\n static int macroexpand_one(int nr, int rem)\n {\n \tstruct attr_stack *stk;\n-\tstruct match_attr *a = NULL;\n \tint i;\n \n \tif (check_all_attr[nr].value != ATTR__TRUE ||\n \t    !check_all_attr[nr].attr->maybe_macro)\n \t\treturn rem;\n \n-\tfor (stk = attr_stack; !a && stk; stk = stk->prev)\n-\t\tfor (i = stk->num_matches - 1; !a && 0 <= i; i--) {\n+\tfor (stk = attr_stack; stk; stk = stk->prev) {\n+\t\tfor (i = stk->num_matches - 1; 0 <= i; i--) {\n \t\t\tstruct match_attr *ma = stk->attrs[i];\n \t\t\tif (!ma->is_macro)\n \t\t\t\tcontinue;\n \t\t\tif (ma->u.attr->attr_nr == nr)\n-\t\t\t\ta = ma;\n+\t\t\t\treturn fill_one(\"expand\", ma, rem);\n \t\t}\n-\n-\tif (a)\n-\t\trem = fill_one(\"expand\", a, rem);\n+\t}\n \n \treturn rem;\n }\n-- \n2.10.1.382.ga23ca1b.dirty\n\n"},{"id":"303826","messageId":"20161011002115.23312-6-sbeller@google.com","threadId":"44273","inReplyTo":"20161011002115.23312-1-sbeller@google.com","subject":"[PATCH 05/28] attr.c: complete a sentence in a comment","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-10-11T00:20:52Z","receivedAt":"2016-10-11T00:21:44Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"From: Junio C Hamano <gitster@pobox.com>\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Stefan Beller <sbeller@google.com>\n---\n attr.c | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)\n\ndiff --git a/attr.c b/attr.c\nindex 05db667..a7f2c3f 100644\n--- a/attr.c\n+++ b/attr.c\n@@ -300,7 +300,7 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,\n  * directory (again, reading the file from top to bottom) down to the\n  * current directory, and then scan the list backwards to find the first match.\n  * This is exactly the same as what is_excluded() does in dir.c to deal with\n- * .gitignore\n+ * .gitignore file and info/excludes file as a fallback.\n  */\n \n static struct attr_stack {\n-- \n2.10.1.382.ga23ca1b.dirty\n\n"},{"id":"303827","messageId":"20161011002115.23312-5-sbeller@google.com","threadId":"44273","inReplyTo":"20161011002115.23312-1-sbeller@google.com","subject":"[PATCH 04/28] attr.c: explain the lack of attr-name syntax check in parse_attr()","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-10-11T00:20:51Z","receivedAt":"2016-10-11T00:21:47Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"From: Junio C Hamano <gitster@pobox.com>\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Stefan Beller <sbeller@google.com>\n---\n attr.c | 6 ++++++\n 1 file changed, 6 insertions(+)\n\ndiff --git a/attr.c b/attr.c\nindex 4ae7801..05db667 100644\n--- a/attr.c\n+++ b/attr.c\n@@ -183,6 +183,12 @@ static const char *parse_attr(const char *src, int lineno, const char *cp,\n \t\t\treturn NULL;\n \t\t}\n \t} else {\n+\t\t/*\n+\t\t * As this function is always called twice, once with\n+\t\t * e == NULL in the first pass and then e != NULL in\n+\t\t * the second pass, no need for invalid_attr_name()\n+\t\t * check here.\n+\t\t */\n \t\tif (*cp == '-' || *cp == '!') {\n \t\t\te->setto = (*cp == '-') ? ATTR__FALSE : ATTR__UNSET;\n \t\t\tcp++;\n-- \n2.10.1.382.ga23ca1b.dirty\n\n"},{"id":"303828","messageId":"20161011002115.23312-7-sbeller@google.com","threadId":"44273","inReplyTo":"20161011002115.23312-1-sbeller@google.com","subject":"[PATCH 06/28] attr.c: mark where #if DEBUG ends more clearly","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-10-11T00:20:53Z","receivedAt":"2016-10-11T00:21:49Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"From: Junio C Hamano <gitster@pobox.com>\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Stefan Beller <sbeller@google.com>\n---\n attr.c | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)\n\ndiff --git a/attr.c b/attr.c\nindex a7f2c3f..95416d3 100644\n--- a/attr.c\n+++ b/attr.c\n@@ -469,7 +469,7 @@ static void debug_set(const char *what, const char *match, struct git_attr *attr\n #define debug_push(a) do { ; } while (0)\n #define debug_pop(a) do { ; } while (0)\n #define debug_set(a,b,c,d) do { ; } while (0)\n-#endif\n+#endif /* DEBUG_ATTR */\n \n static void drop_attr_stack(void)\n {\n-- \n2.10.1.382.ga23ca1b.dirty\n\n"},{"id":"303829","messageId":"20161011002115.23312-10-sbeller@google.com","threadId":"44273","inReplyTo":"20161011002115.23312-1-sbeller@google.com","subject":"[PATCH 09/28] attr.c: plug small leak in parse_attr_line()","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-10-11T00:20:56Z","receivedAt":"2016-10-11T00:21:51Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"From: Junio C Hamano <gitster@pobox.com>\n\nIf any error is noticed after the match_attr structure is allocated,\nwe shouldn't just return NULL from this function.\n\nAdd a fail_return label that frees the allocated structure and\nreturns NULL, and consistently jump there when we want to return\nNULL after cleaning up.\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Stefan Beller <sbeller@google.com>\n---\n attr.c | 12 ++++++++----\n 1 file changed, 8 insertions(+), 4 deletions(-)\n\ndiff --git a/attr.c b/attr.c\nindex 5c35d42..1877f7a 100644\n--- a/attr.c\n+++ b/attr.c\n@@ -223,7 +223,7 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,\n \t\tif (!macro_ok) {\n \t\t\tfprintf(stderr, \"%s not allowed: %s:%d\\n\",\n \t\t\t\tname, src, lineno);\n-\t\t\treturn NULL;\n+\t\t\tgoto fail_return;\n \t\t}\n \t\tis_macro = 1;\n \t\tname += strlen(ATTRIBUTE_MACRO_PREFIX);\n@@ -233,7 +233,7 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,\n \t\t\tfprintf(stderr,\n \t\t\t\t\"%.*s is not a valid attribute name: %s:%d\\n\",\n \t\t\t\tnamelen, name, src, lineno);\n-\t\t\treturn NULL;\n+\t\t\tgoto fail_return;\n \t\t}\n \t}\n \telse\n@@ -246,7 +246,7 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,\n \tfor (cp = states, num_attr = 0; *cp; num_attr++) {\n \t\tcp = parse_attr(src, lineno, cp, NULL);\n \t\tif (!cp)\n-\t\t\treturn NULL;\n+\t\t\tgoto fail_return;\n \t}\n \n \tres = xcalloc(1,\n@@ -267,7 +267,7 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,\n \t\tif (res->u.pat.flags & EXC_FLAG_NEGATIVE) {\n \t\t\twarning(_(\"Negative patterns are ignored in git attributes\\n\"\n \t\t\t\t  \"Use '\\\\!' for literal leading exclamation.\"));\n-\t\t\treturn NULL;\n+\t\t\tgoto fail_return;\n \t\t}\n \t}\n \tres->is_macro = is_macro;\n@@ -283,6 +283,10 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,\n \t}\n \n \treturn res;\n+\n+fail_return:\n+\tfree(res);\n+\treturn NULL;\n }\n \n /*\n-- \n2.10.1.382.ga23ca1b.dirty\n\n"},{"id":"303830","messageId":"20161011002115.23312-9-sbeller@google.com","threadId":"44273","inReplyTo":"20161011002115.23312-1-sbeller@google.com","subject":"[PATCH 08/28] attr.c: tighten constness around \"git_attr\" structure","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-10-11T00:20:55Z","receivedAt":"2016-10-11T00:21:54Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"From: Junio C Hamano <gitster@pobox.com>\n\nIt holds an interned string, and git_attr_name() is a way to peek\ninto it.  Make sure the involved pointer types are pointer-to-const.\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Stefan Beller <sbeller@google.com>\n---\n attr.c | 2 +-\n attr.h | 4 ++--\n 2 files changed, 3 insertions(+), 3 deletions(-)\n\ndiff --git a/attr.c b/attr.c\nindex 7bfeef3..5c35d42 100644\n--- a/attr.c\n+++ b/attr.c\n@@ -43,7 +43,7 @@ static int cannot_trust_maybe_real;\n static struct git_attr_check *check_all_attr;\n static struct git_attr *(git_attr_hash[HASHSIZE]);\n \n-char *git_attr_name(struct git_attr *attr)\n+const char *git_attr_name(const struct git_attr *attr)\n {\n \treturn attr->name;\n }\ndiff --git a/attr.h b/attr.h\nindex 8b08d33..00d7a66 100644\n--- a/attr.h\n+++ b/attr.h\n@@ -25,7 +25,7 @@ extern const char git_attr__false[];\n  * Unset one is returned as NULL.\n  */\n struct git_attr_check {\n-\tstruct git_attr *attr;\n+\tconst struct git_attr *attr;\n \tconst char *value;\n };\n \n@@ -34,7 +34,7 @@ struct git_attr_check {\n  * return value is a pointer to a null-delimited string that is part\n  * of the internal data structure; it should not be modified or freed.\n  */\n-char *git_attr_name(struct git_attr *);\n+extern const char *git_attr_name(const struct git_attr *);\n \n int git_check_attr(const char *path, int, struct git_attr_check *);\n \n-- \n2.10.1.382.ga23ca1b.dirty\n\n"},{"id":"303831","messageId":"20161011002115.23312-11-sbeller@google.com","threadId":"44273","inReplyTo":"20161011002115.23312-1-sbeller@google.com","subject":"[PATCH 10/28] attr: rename function and struct related to checking attributes","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-10-11T00:20:57Z","receivedAt":"2016-10-11T00:21:57Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"From: Junio C Hamano <gitster@pobox.com>\n\nThe traditional API to check attributes is to prepare an N-element\narray of \"struct git_attr_check\" and pass N and the array to the\nfunction \"git_check_attr()\" as arguments.\n\nIn preparation to revamp the API to pass a single structure, in\nwhich these N elements are held, rename the type used for these\nindividual array elements to \"struct git_attr_check_elem\" and rename\nthe function to \"git_check_attrs()\".\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Stefan Beller <sbeller@google.com>\n---\n archive.c              |  6 +++---\n attr.c                 | 12 ++++++------\n attr.h                 |  8 ++++----\n builtin/check-attr.c   | 19 ++++++++++---------\n builtin/pack-objects.c |  6 +++---\n convert.c              | 12 ++++++------\n ll-merge.c             | 10 +++++-----\n userdiff.c             |  4 ++--\n ws.c                   |  6 +++---\n 9 files changed, 42 insertions(+), 41 deletions(-)\n\ndiff --git a/archive.c b/archive.c\nindex dde1ab4..2dc8d6c 100644\n--- a/archive.c\n+++ b/archive.c\n@@ -87,7 +87,7 @@ void *sha1_file_to_archive(const struct archiver_args *args,\n \treturn buffer;\n }\n \n-static void setup_archive_check(struct git_attr_check *check)\n+static void setup_archive_check(struct git_attr_check_elem *check)\n {\n \tstatic struct git_attr *attr_export_ignore;\n \tstatic struct git_attr *attr_export_subst;\n@@ -123,7 +123,7 @@ static int write_archive_entry(const unsigned char *sha1, const char *base,\n \tstruct archiver_context *c = context;\n \tstruct archiver_args *args = c->args;\n \twrite_archive_entry_fn_t write_entry = c->write_entry;\n-\tstruct git_attr_check check[2];\n+\tstruct git_attr_check_elem check[2];\n \tconst char *path_without_prefix;\n \tint err;\n \n@@ -138,7 +138,7 @@ static int write_archive_entry(const unsigned char *sha1, const char *base,\n \tpath_without_prefix = path.buf + args->baselen;\n \n \tsetup_archive_check(check);\n-\tif (!git_check_attr(path_without_prefix, ARRAY_SIZE(check), check)) {\n+\tif (!git_check_attrs(path_without_prefix, ARRAY_SIZE(check), check)) {\n \t\tif (ATTR_TRUE(check[0].value))\n \t\t\treturn 0;\n \t\targs->convert = ATTR_TRUE(check[1].value);\ndiff --git a/attr.c b/attr.c\nindex 1877f7a..c99e23a 100644\n--- a/attr.c\n+++ b/attr.c\n@@ -40,7 +40,7 @@ struct git_attr {\n static int attr_nr;\n static int cannot_trust_maybe_real;\n \n-static struct git_attr_check *check_all_attr;\n+static struct git_attr_check_elem *check_all_attr;\n static struct git_attr *(git_attr_hash[HASHSIZE]);\n \n const char *git_attr_name(const struct git_attr *attr)\n@@ -665,7 +665,7 @@ static int macroexpand_one(int attr_nr, int rem);\n \n static int fill_one(const char *what, struct match_attr *a, int rem)\n {\n-\tstruct git_attr_check *check = check_all_attr;\n+\tstruct git_attr_check_elem *check = check_all_attr;\n \tint i;\n \n \tfor (i = a->num_attr - 1; 0 < rem && 0 <= i; i--) {\n@@ -730,7 +730,7 @@ static int macroexpand_one(int nr, int rem)\n  * collected. Otherwise all attributes are collected.\n  */\n static void collect_some_attrs(const char *path, int num,\n-\t\t\t       struct git_attr_check *check)\n+\t\t\t       struct git_attr_check_elem *check)\n \n {\n \tstruct attr_stack *stk;\n@@ -758,7 +758,7 @@ static void collect_some_attrs(const char *path, int num,\n \t\trem = 0;\n \t\tfor (i = 0; i < num; i++) {\n \t\t\tif (!check[i].attr->maybe_real) {\n-\t\t\t\tstruct git_attr_check *c;\n+\t\t\t\tstruct git_attr_check_elem *c;\n \t\t\t\tc = check_all_attr + check[i].attr->attr_nr;\n \t\t\t\tc->value = ATTR__UNSET;\n \t\t\t\trem++;\n@@ -773,7 +773,7 @@ static void collect_some_attrs(const char *path, int num,\n \t\trem = fill(path, pathlen, basename_offset, stk, rem);\n }\n \n-int git_check_attr(const char *path, int num, struct git_attr_check *check)\n+int git_check_attrs(const char *path, int num, struct git_attr_check_elem *check)\n {\n \tint i;\n \n@@ -789,7 +789,7 @@ int git_check_attr(const char *path, int num, struct git_attr_check *check)\n \treturn 0;\n }\n \n-int git_all_attrs(const char *path, int *num, struct git_attr_check **check)\n+int git_all_attrs(const char *path, int *num, struct git_attr_check_elem **check)\n {\n \tint i, count, j;\n \ndiff --git a/attr.h b/attr.h\nindex 00d7a66..dd3c4a3 100644\n--- a/attr.h\n+++ b/attr.h\n@@ -20,11 +20,11 @@ extern const char git_attr__false[];\n #define ATTR_UNSET(v) ((v) == NULL)\n \n /*\n- * Send one or more git_attr_check to git_check_attr(), and\n+ * Send one or more git_attr_check to git_check_attrs(), and\n  * each 'value' member tells what its value is.\n  * Unset one is returned as NULL.\n  */\n-struct git_attr_check {\n+struct git_attr_check_elem {\n \tconst struct git_attr *attr;\n \tconst char *value;\n };\n@@ -36,7 +36,7 @@ struct git_attr_check {\n  */\n extern const char *git_attr_name(const struct git_attr *);\n \n-int git_check_attr(const char *path, int, struct git_attr_check *);\n+int git_check_attrs(const char *path, int, struct git_attr_check_elem *);\n \n /*\n  * Retrieve all attributes that apply to the specified path.  *num\n@@ -45,7 +45,7 @@ int git_check_attr(const char *path, int, struct git_attr_check *);\n  * objects describing the attributes and their values.  *check must be\n  * free()ed by the caller.\n  */\n-int git_all_attrs(const char *path, int *num, struct git_attr_check **check);\n+int git_all_attrs(const char *path, int *num, struct git_attr_check_elem **check);\n \n enum git_attr_direction {\n \tGIT_ATTR_CHECKIN,\ndiff --git a/builtin/check-attr.c b/builtin/check-attr.c\nindex 53a5a18..97e3837 100644\n--- a/builtin/check-attr.c\n+++ b/builtin/check-attr.c\n@@ -24,8 +24,8 @@ static const struct option check_attr_options[] = {\n \tOPT_END()\n };\n \n-static void output_attr(int cnt, struct git_attr_check *check,\n-\tconst char *file)\n+static void output_attr(int cnt, struct git_attr_check_elem *check,\n+\t\t\tconst char *file)\n {\n \tint j;\n \tfor (j = 0; j < cnt; j++) {\n@@ -51,14 +51,15 @@ static void output_attr(int cnt, struct git_attr_check *check,\n \t}\n }\n \n-static void check_attr(const char *prefix, int cnt,\n-\tstruct git_attr_check *check, const char *file)\n+static void check_attr(const char *prefix,\n+\t\t       int cnt, struct git_attr_check_elem *check,\n+\t\t       const char *file)\n {\n \tchar *full_path =\n \t\tprefix_path(prefix, prefix ? strlen(prefix) : 0, file);\n \tif (check != NULL) {\n-\t\tif (git_check_attr(full_path, cnt, check))\n-\t\t\tdie(\"git_check_attr died\");\n+\t\tif (git_check_attrs(full_path, cnt, check))\n+\t\t\tdie(\"git_check_attrs died\");\n \t\toutput_attr(cnt, check, file);\n \t} else {\n \t\tif (git_all_attrs(full_path, &cnt, &check))\n@@ -69,8 +70,8 @@ static void check_attr(const char *prefix, int cnt,\n \tfree(full_path);\n }\n \n-static void check_attr_stdin_paths(const char *prefix, int cnt,\n-\tstruct git_attr_check *check)\n+static void check_attr_stdin_paths(const char *prefix,\n+\t\t\t\t   int cnt, struct git_attr_check_elem *check)\n {\n \tstruct strbuf buf = STRBUF_INIT;\n \tstruct strbuf unquoted = STRBUF_INIT;\n@@ -99,7 +100,7 @@ static NORETURN void error_with_usage(const char *msg)\n \n int cmd_check_attr(int argc, const char **argv, const char *prefix)\n {\n-\tstruct git_attr_check *check;\n+\tstruct git_attr_check_elem *check;\n \tint cnt, i, doubledash, filei;\n \n \tif (!is_bare_repository())\ndiff --git a/builtin/pack-objects.c b/builtin/pack-objects.c\nindex 166e52c..9df2b08 100644\n--- a/builtin/pack-objects.c\n+++ b/builtin/pack-objects.c\n@@ -895,7 +895,7 @@ static void write_pack_file(void)\n \t\t\twritten, nr_result);\n }\n \n-static void setup_delta_attr_check(struct git_attr_check *check)\n+static void setup_delta_attr_check(struct git_attr_check_elem *check)\n {\n \tstatic struct git_attr *attr_delta;\n \n@@ -907,10 +907,10 @@ static void setup_delta_attr_check(struct git_attr_check *check)\n \n static int no_try_delta(const char *path)\n {\n-\tstruct git_attr_check check[1];\n+\tstruct git_attr_check_elem check[1];\n \n \tsetup_delta_attr_check(check);\n-\tif (git_check_attr(path, ARRAY_SIZE(check), check))\n+\tif (git_check_attrs(path, ARRAY_SIZE(check), check))\n \t\treturn 0;\n \tif (ATTR_FALSE(check->value))\n \t\treturn 1;\ndiff --git a/convert.c b/convert.c\nindex 077f5e6..c95ae71 100644\n--- a/convert.c\n+++ b/convert.c\n@@ -718,7 +718,7 @@ static int ident_to_worktree(const char *path, const char *src, size_t len,\n \treturn 1;\n }\n \n-static enum crlf_action git_path_check_crlf(struct git_attr_check *check)\n+static enum crlf_action git_path_check_crlf(struct git_attr_check_elem *check)\n {\n \tconst char *value = check->value;\n \n@@ -735,7 +735,7 @@ static enum crlf_action git_path_check_crlf(struct git_attr_check *check)\n \treturn CRLF_UNDEFINED;\n }\n \n-static enum eol git_path_check_eol(struct git_attr_check *check)\n+static enum eol git_path_check_eol(struct git_attr_check_elem *check)\n {\n \tconst char *value = check->value;\n \n@@ -748,7 +748,7 @@ static enum eol git_path_check_eol(struct git_attr_check *check)\n \treturn EOL_UNSET;\n }\n \n-static struct convert_driver *git_path_check_convert(struct git_attr_check *check)\n+static struct convert_driver *git_path_check_convert(struct git_attr_check_elem *check)\n {\n \tconst char *value = check->value;\n \tstruct convert_driver *drv;\n@@ -761,7 +761,7 @@ static struct convert_driver *git_path_check_convert(struct git_attr_check *chec\n \treturn NULL;\n }\n \n-static int git_path_check_ident(struct git_attr_check *check)\n+static int git_path_check_ident(struct git_attr_check_elem *check)\n {\n \tconst char *value = check->value;\n \n@@ -783,7 +783,7 @@ static const char *conv_attr_name[] = {\n static void convert_attrs(struct conv_attrs *ca, const char *path)\n {\n \tint i;\n-\tstatic struct git_attr_check ccheck[NUM_CONV_ATTRS];\n+\tstatic struct git_attr_check_elem ccheck[NUM_CONV_ATTRS];\n \n \tif (!ccheck[0].attr) {\n \t\tfor (i = 0; i < NUM_CONV_ATTRS; i++)\n@@ -792,7 +792,7 @@ static void convert_attrs(struct conv_attrs *ca, const char *path)\n \t\tgit_config(read_convert_config, NULL);\n \t}\n \n-\tif (!git_check_attr(path, NUM_CONV_ATTRS, ccheck)) {\n+\tif (!git_check_attrs(path, NUM_CONV_ATTRS, ccheck)) {\n \t\tca->crlf_action = git_path_check_crlf(ccheck + 4);\n \t\tif (ca->crlf_action == CRLF_UNDEFINED)\n \t\t\tca->crlf_action = git_path_check_crlf(ccheck + 0);\ndiff --git a/ll-merge.c b/ll-merge.c\nindex ad8be42..eb2c37e 100644\n--- a/ll-merge.c\n+++ b/ll-merge.c\n@@ -336,13 +336,13 @@ static const struct ll_merge_driver *find_ll_merge_driver(const char *merge_attr\n \treturn &ll_merge_drv[LL_TEXT_MERGE];\n }\n \n-static int git_path_check_merge(const char *path, struct git_attr_check check[2])\n+static int git_path_check_merge(const char *path, struct git_attr_check_elem check[2])\n {\n \tif (!check[0].attr) {\n \t\tcheck[0].attr = git_attr(\"merge\");\n \t\tcheck[1].attr = git_attr(\"conflict-marker-size\");\n \t}\n-\treturn git_check_attr(path, 2, check);\n+\treturn git_check_attrs(path, 2, check);\n }\n \n static void normalize_file(mmfile_t *mm, const char *path)\n@@ -362,7 +362,7 @@ int ll_merge(mmbuffer_t *result_buf,\n \t     mmfile_t *theirs, const char *their_label,\n \t     const struct ll_merge_options *opts)\n {\n-\tstatic struct git_attr_check check[2];\n+\tstatic struct git_attr_check_elem check[2];\n \tstatic const struct ll_merge_options default_opts;\n \tconst char *ll_driver_name = NULL;\n \tint marker_size = DEFAULT_CONFLICT_MARKER_SIZE;\n@@ -398,12 +398,12 @@ int ll_merge(mmbuffer_t *result_buf,\n \n int ll_merge_marker_size(const char *path)\n {\n-\tstatic struct git_attr_check check;\n+\tstatic struct git_attr_check_elem check;\n \tint marker_size = DEFAULT_CONFLICT_MARKER_SIZE;\n \n \tif (!check.attr)\n \t\tcheck.attr = git_attr(\"conflict-marker-size\");\n-\tif (!git_check_attr(path, 1, &check) && check.value) {\n+\tif (!git_check_attrs(path, 1, &check) && check.value) {\n \t\tmarker_size = atoi(check.value);\n \t\tif (marker_size <= 0)\n \t\t\tmarker_size = DEFAULT_CONFLICT_MARKER_SIZE;\ndiff --git a/userdiff.c b/userdiff.c\nindex 2125d6d..4de3289 100644\n--- a/userdiff.c\n+++ b/userdiff.c\n@@ -263,7 +263,7 @@ struct userdiff_driver *userdiff_find_by_name(const char *name) {\n struct userdiff_driver *userdiff_find_by_path(const char *path)\n {\n \tstatic struct git_attr *attr;\n-\tstruct git_attr_check check;\n+\tstruct git_attr_check_elem check;\n \n \tif (!attr)\n \t\tattr = git_attr(\"diff\");\n@@ -271,7 +271,7 @@ struct userdiff_driver *userdiff_find_by_path(const char *path)\n \n \tif (!path)\n \t\treturn NULL;\n-\tif (git_check_attr(path, 1, &check))\n+\tif (git_check_attrs(path, 1, &check))\n \t\treturn NULL;\n \n \tif (ATTR_TRUE(check.value))\ndiff --git a/ws.c b/ws.c\nindex ea4b2b1..7350905 100644\n--- a/ws.c\n+++ b/ws.c\n@@ -71,7 +71,7 @@ unsigned parse_whitespace_rule(const char *string)\n \treturn rule;\n }\n \n-static void setup_whitespace_attr_check(struct git_attr_check *check)\n+static void setup_whitespace_attr_check(struct git_attr_check_elem *check)\n {\n \tstatic struct git_attr *attr_whitespace;\n \n@@ -82,10 +82,10 @@ static void setup_whitespace_attr_check(struct git_attr_check *check)\n \n unsigned whitespace_rule(const char *pathname)\n {\n-\tstruct git_attr_check attr_whitespace_rule;\n+\tstruct git_attr_check_elem attr_whitespace_rule;\n \n \tsetup_whitespace_attr_check(&attr_whitespace_rule);\n-\tif (!git_check_attr(pathname, 1, &attr_whitespace_rule)) {\n+\tif (!git_check_attrs(pathname, 1, &attr_whitespace_rule)) {\n \t\tconst char *value;\n \n \t\tvalue = attr_whitespace_rule.value;\n-- \n2.10.1.382.ga23ca1b.dirty\n\n"},{"id":"303832","messageId":"20161011002115.23312-15-sbeller@google.com","threadId":"44273","inReplyTo":"20161011002115.23312-1-sbeller@google.com","subject":"[PATCH 14/28] attr: retire git_check_attrs() API","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-10-11T00:21:01Z","receivedAt":"2016-10-11T00:21:58Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"From: Junio C Hamano <gitster@pobox.com>\n\nSince nobody uses the old API, make it file-scope static, and update\nthe documentation to describe the new API.\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Stefan Beller <sbeller@google.com>\n---\n Documentation/technical/api-gitattributes.txt | 82 ++++++++++++++++++---------\n attr.c                                        |  3 +-\n attr.h                                        |  2 -\n 3 files changed, 56 insertions(+), 31 deletions(-)\n\ndiff --git a/Documentation/technical/api-gitattributes.txt b/Documentation/technical/api-gitattributes.txt\nindex 2602668..92fc32a 100644\n--- a/Documentation/technical/api-gitattributes.txt\n+++ b/Documentation/technical/api-gitattributes.txt\n@@ -16,10 +16,15 @@ Data Structure\n \tof no interest to the calling programs.  The name of the\n \tattribute can be retrieved by calling `git_attr_name()`.\n \n+`struct git_attr_check_elem`::\n+\n+\tThis structure represents one attribute and its value.\n+\n `struct git_attr_check`::\n \n-\tThis structure represents a set of attributes to check in a call\n-\tto `git_check_attr()` function, and receives the results.\n+\tThis structure represents a collection of `git_attr_check_elem`.\n+\tIt is passed to `git_check_attr()` function, specifying the\n+\tattributes to check, and receives their values.\n \n \n Attribute Values\n@@ -48,49 +53,51 @@ value of the attribute for the path.\n Querying Specific Attributes\n ----------------------------\n \n-* Prepare an array of `struct git_attr_check` to define the list of\n-  attributes you would want to check.  To populate this array, you would\n-  need to define necessary attributes by calling `git_attr()` function.\n+* Prepare `struct git_attr_check` using git_attr_check_initl()\n+  function, enumerating the names of attributes whose values you are\n+  interested in, terminated with a NULL pointer.  Alternatively, an\n+  empty `struct git_attr_check` can be prepared by calling\n+  `git_attr_check_alloc()` function and then attributes you want to\n+  ask about can be added to it with `git_attr_check_append()`\n+  function.\n \n * Call `git_check_attr()` to check the attributes for the path.\n \n-* Inspect `git_attr_check` structure to see how each of the attribute in\n-  the array is defined for the path.\n+* Inspect `git_attr_check` structure to see how each of the\n+  attribute in the array is defined for the path.\n \n \n Example\n -------\n \n-To see how attributes \"crlf\" and \"indent\" are set for different paths.\n+To see how attributes \"crlf\" and \"ident\" are set for different paths.\n \n-. Prepare an array of `struct git_attr_check` with two elements (because\n-  we are checking two attributes).  Initialize their `attr` member with\n-  pointers to `struct git_attr` obtained by calling `git_attr()`:\n+. Prepare a `struct git_attr_check` with two elements (because\n+  we are checking two attributes):\n \n ------------\n-static struct git_attr_check check[2];\n+static struct git_attr_check *check;\n static void setup_check(void)\n {\n-\tif (check[0].attr)\n+\tif (check)\n \t\treturn; /* already done */\n-\tcheck[0].attr = git_attr(\"crlf\");\n-\tcheck[1].attr = git_attr(\"ident\");\n+\tcheck = git_attr_check_initl(\"crlf\", \"ident\", NULL);\n }\n ------------\n \n-. Call `git_check_attr()` with the prepared array of `struct git_attr_check`:\n+. Call `git_check_attr()` with the prepared `struct git_attr_check`:\n \n ------------\n \tconst char *path;\n \n \tsetup_check();\n-\tgit_check_attr(path, ARRAY_SIZE(check), check);\n+\tgit_check_attr(path, check);\n ------------\n \n-. Act on `.value` member of the result, left in `check[]`:\n+. Act on `.value` member of the result, left in `check->check[]`:\n \n ------------\n-\tconst char *value = check[0].value;\n+\tconst char *value = check->check[0].value;\n \n \tif (ATTR_TRUE(value)) {\n \t\tThe attribute is Set, by listing only the name of the\n@@ -109,20 +116,39 @@ static void setup_check(void)\n \t}\n ------------\n \n+To see how attributes in argv[] are set for different paths, only\n+the first step in the above would be different.\n+\n+------------\n+static struct git_attr_check *check;\n+static void setup_check(const char **argv)\n+{\n+\tcheck = git_attr_check_alloc();\n+\twhile (*argv) {\n+\t\tstruct git_attr *attr = git_attr(*argv);\n+\t\tgit_attr_check_append(check, attr);\n+\t\targv++;\n+\t}\n+}\n+------------\n+\n \n Querying All Attributes\n -----------------------\n \n To get the values of all attributes associated with a file:\n \n-* Call `git_all_attrs()`, which returns an array of `git_attr_check`\n-  structures.\n+* Prepare an empty `git_attr_check` structure by calling\n+  `git_attr_check_alloc()`.\n+\n+* Call `git_all_attrs()`, which populates the `git_attr_check`\n+  with the attributes attached to the path.\n \n-* Iterate over the `git_attr_check` array to examine the attribute\n-  names and values.  The name of the attribute described by a\n-  `git_attr_check` object can be retrieved via\n-  `git_attr_name(check[i].attr)`.  (Please note that no items will be\n-  returned for unset attributes, so `ATTR_UNSET()` will return false\n-  for all returned `git_array_check` objects.)\n+* Iterate over the `git_attr_check.check[]` array to examine\n+  the attribute names and values.  The name of the attribute\n+  described by a  `git_attr_check.check[]` object can be retrieved via\n+  `git_attr_name(check->check[i].attr)`.  (Please note that no items\n+  will be returned for unset attributes, so `ATTR_UNSET()` will return\n+  false for all returned `git_array_check` objects.)\n \n-* Free the `git_array_check` array.\n+* Free the `git_array_check` by calling `git_attr_check_free()`.\ndiff --git a/attr.c b/attr.c\nindex 76f0d6b..d427798 100644\n--- a/attr.c\n+++ b/attr.c\n@@ -778,7 +778,8 @@ static void collect_some_attrs(const char *path, int num,\n \t\trem = fill(path, pathlen, basename_offset, stk, rem);\n }\n \n-int git_check_attrs(const char *path, int num, struct git_attr_check_elem *check)\n+static int git_check_attrs(const char *path, int num,\n+\t\t\t   struct git_attr_check_elem *check)\n {\n \tint i;\n \ndiff --git a/attr.h b/attr.h\nindex 0d94077..506db0c 100644\n--- a/attr.h\n+++ b/attr.h\n@@ -52,8 +52,6 @@ extern void git_attr_check_free(struct git_attr_check *);\n  */\n extern const char *git_attr_name(const struct git_attr *);\n \n-int git_check_attrs(const char *path, int, struct git_attr_check_elem *);\n-\n /*\n  * Retrieve all attributes that apply to the specified path.\n  * check holds the attributes and their values.\n-- \n2.10.1.382.ga23ca1b.dirty\n\n"},{"id":"303833","messageId":"20161011002115.23312-14-sbeller@google.com","threadId":"44273","inReplyTo":"20161011002115.23312-1-sbeller@google.com","subject":"[PATCH 13/28] attr: convert git_check_attrs() callers to use the new API","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-10-11T00:21:00Z","receivedAt":"2016-10-11T00:22:03Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"From: Junio C Hamano <gitster@pobox.com>\n\nThe remaining callers are all simple \"I have N attributes I am\ninterested in.  I'll ask about them with various paths one by one\".\n\nAfter this step, no caller to git_check_attrs() remains.  After\nremoving it, we can extend \"struct git_attr_check\" struct with data\nthat can be used in optimizing the query for the specific N\nattributes it contains.\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Stefan Beller <sbeller@google.com>\n---\n builtin/pack-objects.c | 19 +++++--------------\n convert.c              | 18 +++++++-----------\n ll-merge.c             | 33 ++++++++++++++-------------------\n userdiff.c             | 19 ++++++++-----------\n ws.c                   | 19 ++++++-------------\n 5 files changed, 40 insertions(+), 68 deletions(-)\n\ndiff --git a/builtin/pack-objects.c b/builtin/pack-objects.c\nindex 9df2b08..df4e6b6 100644\n--- a/builtin/pack-objects.c\n+++ b/builtin/pack-objects.c\n@@ -895,24 +895,15 @@ static void write_pack_file(void)\n \t\t\twritten, nr_result);\n }\n \n-static void setup_delta_attr_check(struct git_attr_check_elem *check)\n-{\n-\tstatic struct git_attr *attr_delta;\n-\n-\tif (!attr_delta)\n-\t\tattr_delta = git_attr(\"delta\");\n-\n-\tcheck[0].attr = attr_delta;\n-}\n-\n static int no_try_delta(const char *path)\n {\n-\tstruct git_attr_check_elem check[1];\n+\tstatic struct git_attr_check *check;\n \n-\tsetup_delta_attr_check(check);\n-\tif (git_check_attrs(path, ARRAY_SIZE(check), check))\n+\tif (!check)\n+\t\tcheck = git_attr_check_initl(\"delta\", NULL);\n+\tif (git_check_attr(path, check))\n \t\treturn 0;\n-\tif (ATTR_FALSE(check->value))\n+\tif (ATTR_FALSE(check->check[0].value))\n \t\treturn 1;\n \treturn 0;\n }\ndiff --git a/convert.c b/convert.c\nindex c95ae71..bb2435a 100644\n--- a/convert.c\n+++ b/convert.c\n@@ -775,24 +775,20 @@ struct conv_attrs {\n \tint ident;\n };\n \n-static const char *conv_attr_name[] = {\n-\t\"crlf\", \"ident\", \"filter\", \"eol\", \"text\",\n-};\n-#define NUM_CONV_ATTRS ARRAY_SIZE(conv_attr_name)\n-\n static void convert_attrs(struct conv_attrs *ca, const char *path)\n {\n-\tint i;\n-\tstatic struct git_attr_check_elem ccheck[NUM_CONV_ATTRS];\n+\tstatic struct git_attr_check *check;\n \n-\tif (!ccheck[0].attr) {\n-\t\tfor (i = 0; i < NUM_CONV_ATTRS; i++)\n-\t\t\tccheck[i].attr = git_attr(conv_attr_name[i]);\n+\tif (!check) {\n+\t\tcheck = git_attr_check_initl(\"crlf\", \"ident\",\n+\t\t\t\t\t     \"filter\", \"eol\", \"text\",\n+\t\t\t\t\t     NULL);\n \t\tuser_convert_tail = &user_convert;\n \t\tgit_config(read_convert_config, NULL);\n \t}\n \n-\tif (!git_check_attrs(path, NUM_CONV_ATTRS, ccheck)) {\n+\tif (!git_check_attr(path, check)) {\n+\t\tstruct git_attr_check_elem *ccheck = check->check;\n \t\tca->crlf_action = git_path_check_crlf(ccheck + 4);\n \t\tif (ca->crlf_action == CRLF_UNDEFINED)\n \t\t\tca->crlf_action = git_path_check_crlf(ccheck + 0);\ndiff --git a/ll-merge.c b/ll-merge.c\nindex eb2c37e..bc6479c 100644\n--- a/ll-merge.c\n+++ b/ll-merge.c\n@@ -336,15 +336,6 @@ static const struct ll_merge_driver *find_ll_merge_driver(const char *merge_attr\n \treturn &ll_merge_drv[LL_TEXT_MERGE];\n }\n \n-static int git_path_check_merge(const char *path, struct git_attr_check_elem check[2])\n-{\n-\tif (!check[0].attr) {\n-\t\tcheck[0].attr = git_attr(\"merge\");\n-\t\tcheck[1].attr = git_attr(\"conflict-marker-size\");\n-\t}\n-\treturn git_check_attrs(path, 2, check);\n-}\n-\n static void normalize_file(mmfile_t *mm, const char *path)\n {\n \tstruct strbuf strbuf = STRBUF_INIT;\n@@ -362,7 +353,7 @@ int ll_merge(mmbuffer_t *result_buf,\n \t     mmfile_t *theirs, const char *their_label,\n \t     const struct ll_merge_options *opts)\n {\n-\tstatic struct git_attr_check_elem check[2];\n+\tstatic struct git_attr_check *check;\n \tstatic const struct ll_merge_options default_opts;\n \tconst char *ll_driver_name = NULL;\n \tint marker_size = DEFAULT_CONFLICT_MARKER_SIZE;\n@@ -376,10 +367,14 @@ int ll_merge(mmbuffer_t *result_buf,\n \t\tnormalize_file(ours, path);\n \t\tnormalize_file(theirs, path);\n \t}\n-\tif (!git_path_check_merge(path, check)) {\n-\t\tll_driver_name = check[0].value;\n-\t\tif (check[1].value) {\n-\t\t\tmarker_size = atoi(check[1].value);\n+\n+\tif (!check)\n+\t\tcheck = git_attr_check_initl(\"merge\", \"conflict-marker-size\", NULL);\n+\n+\tif (!git_check_attr(path, check)) {\n+\t\tll_driver_name = check->check[0].value;\n+\t\tif (check->check[1].value) {\n+\t\t\tmarker_size = atoi(check->check[1].value);\n \t\t\tif (marker_size <= 0)\n \t\t\t\tmarker_size = DEFAULT_CONFLICT_MARKER_SIZE;\n \t\t}\n@@ -398,13 +393,13 @@ int ll_merge(mmbuffer_t *result_buf,\n \n int ll_merge_marker_size(const char *path)\n {\n-\tstatic struct git_attr_check_elem check;\n+\tstatic struct git_attr_check *check;\n \tint marker_size = DEFAULT_CONFLICT_MARKER_SIZE;\n \n-\tif (!check.attr)\n-\t\tcheck.attr = git_attr(\"conflict-marker-size\");\n-\tif (!git_check_attrs(path, 1, &check) && check.value) {\n-\t\tmarker_size = atoi(check.value);\n+\tif (!check)\n+\t\tcheck = git_attr_check_initl(\"conflict-marker-size\", NULL);\n+\tif (!git_check_attr(path, check) && check->check[0].value) {\n+\t\tmarker_size = atoi(check->check[0].value);\n \t\tif (marker_size <= 0)\n \t\t\tmarker_size = DEFAULT_CONFLICT_MARKER_SIZE;\n \t}\ndiff --git a/userdiff.c b/userdiff.c\nindex 4de3289..46dfd32 100644\n--- a/userdiff.c\n+++ b/userdiff.c\n@@ -262,25 +262,22 @@ struct userdiff_driver *userdiff_find_by_name(const char *name) {\n \n struct userdiff_driver *userdiff_find_by_path(const char *path)\n {\n-\tstatic struct git_attr *attr;\n-\tstruct git_attr_check_elem check;\n-\n-\tif (!attr)\n-\t\tattr = git_attr(\"diff\");\n-\tcheck.attr = attr;\n+\tstatic struct git_attr_check *check;\n \n+\tif (!check)\n+\t\tcheck = git_attr_check_initl(\"diff\", NULL);\n \tif (!path)\n \t\treturn NULL;\n-\tif (git_check_attrs(path, 1, &check))\n+\tif (git_check_attr(path, check))\n \t\treturn NULL;\n \n-\tif (ATTR_TRUE(check.value))\n+\tif (ATTR_TRUE(check->check[0].value))\n \t\treturn &driver_true;\n-\tif (ATTR_FALSE(check.value))\n+\tif (ATTR_FALSE(check->check[0].value))\n \t\treturn &driver_false;\n-\tif (ATTR_UNSET(check.value))\n+\tif (ATTR_UNSET(check->check[0].value))\n \t\treturn NULL;\n-\treturn userdiff_find_by_name(check.value);\n+\treturn userdiff_find_by_name(check->check[0].value);\n }\n \n struct userdiff_driver *userdiff_get_textconv(struct userdiff_driver *driver)\ndiff --git a/ws.c b/ws.c\nindex 7350905..bb3270c 100644\n--- a/ws.c\n+++ b/ws.c\n@@ -71,24 +71,17 @@ unsigned parse_whitespace_rule(const char *string)\n \treturn rule;\n }\n \n-static void setup_whitespace_attr_check(struct git_attr_check_elem *check)\n-{\n-\tstatic struct git_attr *attr_whitespace;\n-\n-\tif (!attr_whitespace)\n-\t\tattr_whitespace = git_attr(\"whitespace\");\n-\tcheck[0].attr = attr_whitespace;\n-}\n-\n unsigned whitespace_rule(const char *pathname)\n {\n-\tstruct git_attr_check_elem attr_whitespace_rule;\n+\tstatic struct git_attr_check *attr_whitespace_rule;\n+\n+\tif (!attr_whitespace_rule)\n+\t\tattr_whitespace_rule = git_attr_check_initl(\"whitespace\", NULL);\n \n-\tsetup_whitespace_attr_check(&attr_whitespace_rule);\n-\tif (!git_check_attrs(pathname, 1, &attr_whitespace_rule)) {\n+\tif (!git_check_attr(pathname, attr_whitespace_rule)) {\n \t\tconst char *value;\n \n-\t\tvalue = attr_whitespace_rule.value;\n+\t\tvalue = attr_whitespace_rule->check[0].value;\n \t\tif (ATTR_TRUE(value)) {\n \t\t\t/* true (whitespace) */\n \t\t\tunsigned all_rule = ws_tab_width(whitespace_rule_cfg);\n-- \n2.10.1.382.ga23ca1b.dirty\n\n"},{"id":"303834","messageId":"20161011002115.23312-17-sbeller@google.com","threadId":"44273","inReplyTo":"20161011002115.23312-1-sbeller@google.com","subject":"[PATCH 16/28] attr: add counted string version of git_attr()","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-10-11T00:21:03Z","receivedAt":"2016-10-11T00:22:06Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"From: Junio C Hamano <gitster@pobox.com>\n\nOften a potential caller has <name, namelen> pair that\nrepresents the name it wants to create an attribute out of.\n\nWhen name[namelen] is not NUL, the caller has to xmemdupz()\nonly to call git_attr().\n\nAdd git_attr_counted() that takes such a counted string instead of\n\"const char *name\".\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Stefan Beller <sbeller@google.com>\n---\n attr.c | 8 ++++----\n attr.h | 5 ++++-\n 2 files changed, 8 insertions(+), 5 deletions(-)\n\ndiff --git a/attr.c b/attr.c\nindex 9bec243..90dbacd 100644\n--- a/attr.c\n+++ b/attr.c\n@@ -78,7 +78,7 @@ static int invalid_attr_name(const char *name, int namelen)\n \treturn 0;\n }\n \n-static struct git_attr *git_attr_internal(const char *name, int len)\n+struct git_attr *git_attr_counted(const char *name, size_t len)\n {\n \tunsigned hval = hash_name(name, len);\n \tunsigned pos = hval % HASHSIZE;\n@@ -109,7 +109,7 @@ static struct git_attr *git_attr_internal(const char *name, int len)\n \n struct git_attr *git_attr(const char *name)\n {\n-\treturn git_attr_internal(name, strlen(name));\n+\treturn git_attr_counted(name, strlen(name));\n }\n \n /* What does a matched pattern decide? */\n@@ -199,7 +199,7 @@ static const char *parse_attr(const char *src, int lineno, const char *cp,\n \t\telse {\n \t\t\te->setto = xmemdupz(equals + 1, ep - equals - 1);\n \t\t}\n-\t\te->attr = git_attr_internal(cp, len);\n+\t\te->attr = git_attr_counted(cp, len);\n \t}\n \treturn ep + strspn(ep, blank);\n }\n@@ -254,7 +254,7 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,\n \t\t      sizeof(struct attr_state) * num_attr +\n \t\t      (is_macro ? 0 : namelen + 1));\n \tif (is_macro) {\n-\t\tres->u.attr = git_attr_internal(name, namelen);\n+\t\tres->u.attr = git_attr_counted(name, namelen);\n \t\tres->u.attr->maybe_macro = 1;\n \t} else {\n \t\tchar *p = (char *)&(res->state[num_attr]);\ndiff --git a/attr.h b/attr.h\nindex c84f164..bcedf92 100644\n--- a/attr.h\n+++ b/attr.h\n@@ -8,7 +8,10 @@ struct git_attr;\n  * Given a string, return the gitattribute object that\n  * corresponds to it.\n  */\n-struct git_attr *git_attr(const char *);\n+extern struct git_attr *git_attr(const char *);\n+\n+/* The same, but with counted string */\n+extern struct git_attr *git_attr_counted(const char *, size_t);\n \n /* Internal use */\n extern const char git_attr__true[];\n-- \n2.10.1.382.ga23ca1b.dirty\n\n"},{"id":"303835","messageId":"20161011002115.23312-18-sbeller@google.com","threadId":"44273","inReplyTo":"20161011002115.23312-1-sbeller@google.com","subject":"[PATCH 17/28] attr: expose validity check for attribute names","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-10-11T00:21:04Z","receivedAt":"2016-10-11T00:22:07Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"From: Junio C Hamano <gitster@pobox.com>\n\nExport attr_name_valid() function, and a helper function that\nreturns the message to be given when a given <name, len> pair\nis not a good name for an attribute.\n\nWe could later update the message to exactly spell out what the\nrules for a good attribute name are, etc.\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Stefan Beller <sbeller@google.com>\n---\n attr.c | 39 +++++++++++++++++++++++++--------------\n attr.h |  3 +++\n 2 files changed, 28 insertions(+), 14 deletions(-)\n\ndiff --git a/attr.c b/attr.c\nindex 90dbacd..6e3bd25 100644\n--- a/attr.c\n+++ b/attr.c\n@@ -59,23 +59,38 @@ static unsigned hash_name(const char *name, int namelen)\n \treturn val;\n }\n \n-static int invalid_attr_name(const char *name, int namelen)\n+int attr_name_valid(const char *name, size_t namelen)\n {\n \t/*\n \t * Attribute name cannot begin with '-' and must consist of\n \t * characters from [-A-Za-z0-9_.].\n \t */\n \tif (namelen <= 0 || *name == '-')\n-\t\treturn -1;\n+\t\treturn 0;\n \twhile (namelen--) {\n \t\tchar ch = *name++;\n \t\tif (! (ch == '-' || ch == '.' || ch == '_' ||\n \t\t       ('0' <= ch && ch <= '9') ||\n \t\t       ('a' <= ch && ch <= 'z') ||\n \t\t       ('A' <= ch && ch <= 'Z')) )\n-\t\t\treturn -1;\n+\t\t\treturn 0;\n \t}\n-\treturn 0;\n+\treturn -1;\n+}\n+\n+void invalid_attr_name_message(struct strbuf *err, const char *name, int len)\n+{\n+\tstrbuf_addf(err, _(\"%.*s is not a valid attribute name\"),\n+\t\t    len, name);\n+}\n+\n+static void report_invalid_attr(const char *name, size_t len,\n+\t\t\t\tconst char *src, int lineno)\n+{\n+\tstruct strbuf err = STRBUF_INIT;\n+\tinvalid_attr_name_message(&err, name, len);\n+\tfprintf(stderr, \"%s: %s:%d\\n\", err.buf, src, lineno);\n+\tstrbuf_release(&err);\n }\n \n struct git_attr *git_attr_counted(const char *name, size_t len)\n@@ -90,7 +105,7 @@ struct git_attr *git_attr_counted(const char *name, size_t len)\n \t\t\treturn a;\n \t}\n \n-\tif (invalid_attr_name(name, len))\n+\tif (!attr_name_valid(name, len))\n \t\treturn NULL;\n \n \tFLEX_ALLOC_MEM(a, name, name, len);\n@@ -176,17 +191,15 @@ static const char *parse_attr(const char *src, int lineno, const char *cp,\n \t\t\tcp++;\n \t\t\tlen--;\n \t\t}\n-\t\tif (invalid_attr_name(cp, len)) {\n-\t\t\tfprintf(stderr,\n-\t\t\t\t\"%.*s is not a valid attribute name: %s:%d\\n\",\n-\t\t\t\tlen, cp, src, lineno);\n+\t\tif (!attr_name_valid(cp, len)) {\n+\t\t\treport_invalid_attr(cp, len, src, lineno);\n \t\t\treturn NULL;\n \t\t}\n \t} else {\n \t\t/*\n \t\t * As this function is always called twice, once with\n \t\t * e == NULL in the first pass and then e != NULL in\n-\t\t * the second pass, no need for invalid_attr_name()\n+\t\t * the second pass, no need for attr_name_valid()\n \t\t * check here.\n \t\t */\n \t\tif (*cp == '-' || *cp == '!') {\n@@ -229,10 +242,8 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,\n \t\tname += strlen(ATTRIBUTE_MACRO_PREFIX);\n \t\tname += strspn(name, blank);\n \t\tnamelen = strcspn(name, blank);\n-\t\tif (invalid_attr_name(name, namelen)) {\n-\t\t\tfprintf(stderr,\n-\t\t\t\t\"%.*s is not a valid attribute name: %s:%d\\n\",\n-\t\t\t\tnamelen, name, src, lineno);\n+\t\tif (!attr_name_valid(name, namelen)) {\n+\t\t\treport_invalid_attr(name, namelen, src, lineno);\n \t\t\tgoto fail_return;\n \t\t}\n \t}\ndiff --git a/attr.h b/attr.h\nindex bcedf92..40abc16 100644\n--- a/attr.h\n+++ b/attr.h\n@@ -13,6 +13,9 @@ extern struct git_attr *git_attr(const char *);\n /* The same, but with counted string */\n extern struct git_attr *git_attr_counted(const char *, size_t);\n \n+extern int attr_name_valid(const char *name, size_t namelen);\n+extern void invalid_attr_name_message(struct strbuf *, const char *, int);\n+\n /* Internal use */\n extern const char git_attr__true[];\n extern const char git_attr__false[];\n-- \n2.10.1.382.ga23ca1b.dirty\n\n"},{"id":"303836","messageId":"20161011002115.23312-16-sbeller@google.com","threadId":"44273","inReplyTo":"20161011002115.23312-1-sbeller@google.com","subject":"[PATCH 15/28] attr: add counted string version of git_check_attr()","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-10-11T00:21:02Z","receivedAt":"2016-10-11T00:22:08Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"From: Junio C Hamano <gitster@pobox.com>\n\nOften a potential caller has <path, pathlen> pair that\nrepresents the path it wants to ask attributes for; when\npath[pathlen] is not NUL, the caller has to xmemdupz()\nonly to call git_check_attr().\n\nAdd git_check_attr_counted() that takes such a counted\nstring instead of \"const char *path\".\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Stefan Beller <sbeller@google.com>\n---\n attr.c | 23 ++++++++++++++---------\n attr.h |  1 +\n 2 files changed, 15 insertions(+), 9 deletions(-)\n\ndiff --git a/attr.c b/attr.c\nindex d427798..9bec243 100644\n--- a/attr.c\n+++ b/attr.c\n@@ -734,20 +734,19 @@ static int attr_check_is_dynamic(const struct git_attr_check *check)\n  * check_all_attr. If num is non-zero, only attributes in check[] are\n  * collected. Otherwise all attributes are collected.\n  */\n-static void collect_some_attrs(const char *path, int num,\n+static void collect_some_attrs(const char *path, int pathlen, int num,\n \t\t\t       struct git_attr_check_elem *check)\n \n {\n \tstruct attr_stack *stk;\n-\tint i, pathlen, rem, dirlen;\n+\tint i, rem, dirlen;\n \tconst char *cp, *last_slash = NULL;\n \tint basename_offset;\n \n-\tfor (cp = path; *cp; cp++) {\n+\tfor (cp = path; cp < path + pathlen; cp++) {\n \t\tif (*cp == '/' && cp[1])\n \t\t\tlast_slash = cp;\n \t}\n-\tpathlen = cp - path;\n \tif (last_slash) {\n \t\tbasename_offset = last_slash + 1 - path;\n \t\tdirlen = last_slash - path;\n@@ -778,12 +777,12 @@ static void collect_some_attrs(const char *path, int num,\n \t\trem = fill(path, pathlen, basename_offset, stk, rem);\n }\n \n-static int git_check_attrs(const char *path, int num,\n+static int git_check_attrs(const char *path, int pathlen, int num,\n \t\t\t   struct git_attr_check_elem *check)\n {\n \tint i;\n \n-\tcollect_some_attrs(path, num, check);\n+\tcollect_some_attrs(path, pathlen, num, check);\n \n \tfor (i = 0; i < num; i++) {\n \t\tconst char *value = check_all_attr[check[i].attr->attr_nr].value;\n@@ -800,7 +799,7 @@ void git_all_attrs(const char *path, struct git_attr_check *check)\n \tint i;\n \n \tgit_attr_check_clear(check);\n-\tcollect_some_attrs(path, 0, NULL);\n+\tcollect_some_attrs(path, strlen(path), 0, NULL);\n \n \tfor (i = 0; i < attr_nr; i++) {\n \t\tconst char *name = check_all_attr[i].attr->name;\n@@ -825,10 +824,16 @@ void git_attr_set_direction(enum git_attr_direction new, struct index_state *ist\n \tuse_index = istate;\n }\n \n-int git_check_attr(const char *path, struct git_attr_check *check)\n+int git_check_attr_counted(const char *path, int pathlen,\n+\t\t\t   struct git_attr_check *check)\n {\n \tcheck->finalized = 1;\n-\treturn git_check_attrs(path, check->check_nr, check->check);\n+\treturn git_check_attrs(path, pathlen, check->check_nr, check->check);\n+}\n+\n+int git_check_attr(const char *path, struct git_attr_check *check)\n+{\n+\treturn git_check_attr_counted(path, strlen(path), check);\n }\n \n struct git_attr_check *git_attr_check_initl(const char *one, ...)\ndiff --git a/attr.h b/attr.h\nindex 506db0c..c84f164 100644\n--- a/attr.h\n+++ b/attr.h\n@@ -38,6 +38,7 @@ struct git_attr_check {\n \n extern struct git_attr_check *git_attr_check_initl(const char *, ...);\n extern int git_check_attr(const char *path, struct git_attr_check *);\n+extern int git_check_attr_counted(const char *, int, struct git_attr_check *);\n \n extern struct git_attr_check *git_attr_check_alloc(void);\n extern struct git_attr_check_elem *git_attr_check_append(struct git_attr_check *, const struct git_attr *);\n-- \n2.10.1.382.ga23ca1b.dirty\n\n"},{"id":"303837","messageId":"20161011002115.23312-20-sbeller@google.com","threadId":"44273","inReplyTo":"20161011002115.23312-1-sbeller@google.com","subject":"[PATCH 19/28] attr.c: add push_stack() helper","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-10-11T00:21:06Z","receivedAt":"2016-10-11T00:22:11Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"From: Junio C Hamano <gitster@pobox.com>\n\nThere are too many repetitious \"I have this new attr_stack element;\npush it at the top of the stack\" sequence.  The new helper function\npush_stack() gives us a way to express what is going on at these\nplaces, and as a side effect, halves the number of times we mention\nthe attr_stack global variable.\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Stefan Beller <sbeller@google.com>\n---\n attr.c | 71 +++++++++++++++++++++++++++++++-----------------------------------\n 1 file changed, 33 insertions(+), 38 deletions(-)\n\ndiff --git a/attr.c b/attr.c\nindex 6a14c9a..f9ebdd1 100644\n--- a/attr.c\n+++ b/attr.c\n@@ -521,6 +521,18 @@ static int git_attr_system(void)\n \n static GIT_PATH_FUNC(git_path_info_attributes, INFOATTRIBUTES_FILE)\n \n+static void push_stack(struct attr_stack **attr_stack_p,\n+\t\t       struct attr_stack *elem, char *origin, size_t originlen)\n+{\n+\tif (elem) {\n+\t\telem->origin = origin;\n+\t\tif (origin)\n+\t\t\telem->originlen = originlen;\n+\t\telem->prev = *attr_stack_p;\n+\t\t*attr_stack_p = elem;\n+\t}\n+}\n+\n static void bootstrap_attr_stack(void)\n {\n \tstruct attr_stack *elem;\n@@ -528,52 +540,35 @@ static void bootstrap_attr_stack(void)\n \tif (attr_stack)\n \t\treturn;\n \n-\telem = read_attr_from_array(builtin_attr);\n-\telem->origin = NULL;\n-\telem->prev = attr_stack;\n-\tattr_stack = elem;\n-\n-\tif (git_attr_system()) {\n-\t\telem = read_attr_from_file(git_etc_gitattributes(), 1);\n-\t\tif (elem) {\n-\t\t\telem->origin = NULL;\n-\t\t\telem->prev = attr_stack;\n-\t\t\tattr_stack = elem;\n-\t\t}\n-\t}\n+\tpush_stack(&attr_stack, read_attr_from_array(builtin_attr), NULL, 0);\n+\n+\tif (git_attr_system())\n+\t\tpush_stack(&attr_stack,\n+\t\t\t   read_attr_from_file(git_etc_gitattributes(), 1),\n+\t\t\t   NULL, 0);\n \n \tif (!git_attributes_file)\n \t\tgit_attributes_file = xdg_config_home(\"attributes\");\n-\tif (git_attributes_file) {\n-\t\telem = read_attr_from_file(git_attributes_file, 1);\n-\t\tif (elem) {\n-\t\t\telem->origin = NULL;\n-\t\t\telem->prev = attr_stack;\n-\t\t\tattr_stack = elem;\n-\t\t}\n-\t}\n+\tif (git_attributes_file)\n+\t\tpush_stack(&attr_stack,\n+\t\t\t   read_attr_from_file(git_attributes_file, 1),\n+\t\t\t   NULL, 0);\n \n \tif (!is_bare_repository() || direction == GIT_ATTR_INDEX) {\n \t\telem = read_attr(GITATTRIBUTES_FILE, 1);\n-\t\telem->origin = xstrdup(\"\");\n-\t\telem->originlen = 0;\n-\t\telem->prev = attr_stack;\n-\t\tattr_stack = elem;\n+\t\tpush_stack(&attr_stack, elem, xstrdup(\"\"), 0);\n \t\tdebug_push(elem);\n \t}\n \n \telem = read_attr_from_file(git_path_info_attributes(), 1);\n \tif (!elem)\n \t\telem = xcalloc(1, sizeof(*elem));\n-\telem->origin = NULL;\n-\telem->prev = attr_stack;\n-\tattr_stack = elem;\n+\tpush_stack(&attr_stack, elem, NULL, 0);\n }\n \n static void prepare_attr_stack(const char *path, int dirlen)\n {\n \tstruct attr_stack *elem, *info;\n-\tint len;\n \tconst char *cp;\n \n \t/*\n@@ -633,20 +628,21 @@ static void prepare_attr_stack(const char *path, int dirlen)\n \n \t\tassert(attr_stack->origin);\n \t\twhile (1) {\n-\t\t\tlen = strlen(attr_stack->origin);\n+\t\t\tsize_t len = strlen(attr_stack->origin);\n+\t\t\tchar *origin;\n+\n \t\t\tif (dirlen <= len)\n \t\t\t\tbreak;\n \t\t\tcp = memchr(path + len + 1, '/', dirlen - len - 1);\n \t\t\tif (!cp)\n \t\t\t\tcp = path + dirlen;\n-\t\t\tstrbuf_add(&pathbuf, path, cp - path);\n-\t\t\tstrbuf_addch(&pathbuf, '/');\n-\t\t\tstrbuf_addstr(&pathbuf, GITATTRIBUTES_FILE);\n+\t\t\tstrbuf_addf(&pathbuf,\n+\t\t\t\t    \"%.*s/%s\", (int)(cp - path), path,\n+\t\t\t\t    GITATTRIBUTES_FILE);\n \t\t\telem = read_attr(pathbuf.buf, 0);\n \t\t\tstrbuf_setlen(&pathbuf, cp - path);\n-\t\t\telem->origin = strbuf_detach(&pathbuf, &elem->originlen);\n-\t\t\telem->prev = attr_stack;\n-\t\t\tattr_stack = elem;\n+\t\t\torigin = strbuf_detach(&pathbuf, &len);\n+\t\t\tpush_stack(&attr_stack, elem, origin, len);\n \t\t\tdebug_push(elem);\n \t\t}\n \n@@ -656,8 +652,7 @@ static void prepare_attr_stack(const char *path, int dirlen)\n \t/*\n \t * Finally push the \"info\" one at the top of the stack.\n \t */\n-\tinfo->prev = attr_stack;\n-\tattr_stack = info;\n+\tpush_stack(&attr_stack, info, NULL, 0);\n }\n \n static int path_matches(const char *pathname, int pathlen,\n-- \n2.10.1.382.ga23ca1b.dirty\n\n"},{"id":"303838","messageId":"20161011002115.23312-19-sbeller@google.com","threadId":"44273","inReplyTo":"20161011002115.23312-1-sbeller@google.com","subject":"[PATCH 18/28] attr: support quoting pathname patterns in C style","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-10-11T00:21:05Z","receivedAt":"2016-10-11T00:22:13Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"From: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n\nFull pattern must be quoted. So 'pat\"t\"ern attr' will give exactly\n'pat\"t\"ern', not 'pattern'. Also clarify that leading whitespaces are\nnot part of the pattern and document comment syntax.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Stefan Beller <sbeller@google.com>\n---\n Documentation/gitattributes.txt |  8 +++++---\n attr.c                          | 15 +++++++++++++--\n t/t0003-attributes.sh           | 26 ++++++++++++++++++++++++++\n 3 files changed, 44 insertions(+), 5 deletions(-)\n\ndiff --git a/Documentation/gitattributes.txt b/Documentation/gitattributes.txt\nindex 7aff940..8a061af 100644\n--- a/Documentation/gitattributes.txt\n+++ b/Documentation/gitattributes.txt\n@@ -21,9 +21,11 @@ Each line in `gitattributes` file is of form:\n \tpattern\tattr1 attr2 ...\n \n That is, a pattern followed by an attributes list,\n-separated by whitespaces.  When the pattern matches the\n-path in question, the attributes listed on the line are given to\n-the path.\n+separated by whitespaces. Leading and trailing whitespaces are\n+ignored. Lines that begin with '#' are ignored. Patterns\n+that begin with a double quote are quoted in C style.\n+When the pattern matches the path in question, the attributes\n+listed on the line are given to the path.\n \n Each attribute can be in one of these states for a given path:\n \ndiff --git a/attr.c b/attr.c\nindex 6e3bd25..6a14c9a 100644\n--- a/attr.c\n+++ b/attr.c\n@@ -13,6 +13,7 @@\n #include \"attr.h\"\n #include \"dir.h\"\n #include \"utf8.h\"\n+#include \"quote.h\"\n \n const char git_attr__true[] = \"(builtin)true\";\n const char git_attr__false[] = \"\\0(builtin)false\";\n@@ -225,12 +226,21 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,\n \tconst char *cp, *name, *states;\n \tstruct match_attr *res = NULL;\n \tint is_macro;\n+\tstruct strbuf pattern = STRBUF_INIT;\n \n \tcp = line + strspn(line, blank);\n \tif (!*cp || *cp == '#')\n \t\treturn NULL;\n \tname = cp;\n-\tnamelen = strcspn(name, blank);\n+\n+\tif (*cp == '\"' && !unquote_c_style(&pattern, name, &states)) {\n+\t\tname = pattern.buf;\n+\t\tnamelen = pattern.len;\n+\t} else {\n+\t\tnamelen = strcspn(name, blank);\n+\t\tstates = name + namelen;\n+\t}\n+\n \tif (strlen(ATTRIBUTE_MACRO_PREFIX) < namelen &&\n \t    starts_with(name, ATTRIBUTE_MACRO_PREFIX)) {\n \t\tif (!macro_ok) {\n@@ -250,7 +260,6 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,\n \telse\n \t\tis_macro = 0;\n \n-\tstates = name + namelen;\n \tstates += strspn(states, blank);\n \n \t/* First pass to count the attr_states */\n@@ -293,9 +302,11 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,\n \t\t\tcannot_trust_maybe_real = 1;\n \t}\n \n+\tstrbuf_release(&pattern);\n \treturn res;\n \n fail_return:\n+\tstrbuf_release(&pattern);\n \tfree(res);\n \treturn NULL;\n }\ndiff --git a/t/t0003-attributes.sh b/t/t0003-attributes.sh\nindex f0fbb42..f19ae4f 100755\n--- a/t/t0003-attributes.sh\n+++ b/t/t0003-attributes.sh\n@@ -13,10 +13,31 @@ attr_check () {\n \ttest_line_count = 0 err\n }\n \n+attr_check_quote () {\n+\n+\tpath=\"$1\"\n+\tquoted_path=\"$2\"\n+\texpect=\"$3\"\n+\n+\tgit check-attr test -- \"$path\" >actual &&\n+\techo \"\\\"$quoted_path\\\": test: $expect\" >expect &&\n+\ttest_cmp expect actual\n+\n+}\n+\n+test_expect_success 'open-quoted pathname' '\n+\techo \"\\\"a test=a\" >.gitattributes &&\n+\ttest_must_fail attr_check a a\n+'\n+\n+\n test_expect_success 'setup' '\n \tmkdir -p a/b/d a/c b &&\n \t(\n \t\techo \"[attr]notest !test\"\n+\t\techo \"\\\" d \\\"\ttest=d\"\n+\t\techo \" e\ttest=e\"\n+\t\techo \" e\\\"\ttest=e\"\n \t\techo \"f\ttest=f\"\n \t\techo \"a/i test=a/i\"\n \t\techo \"onoff test -test\"\n@@ -69,6 +90,11 @@ test_expect_success 'command line checks' '\n '\n \n test_expect_success 'attribute test' '\n+\n+\tattr_check \" d \" d &&\n+\tattr_check e e &&\n+\tattr_check_quote e\\\" e\\\\\\\" e &&\n+\n \tattr_check f f &&\n \tattr_check a/f f &&\n \tattr_check a/c/f f &&\n-- \n2.10.1.382.ga23ca1b.dirty\n\n"},{"id":"303839","messageId":"20161011002115.23312-22-sbeller@google.com","threadId":"44273","inReplyTo":"20161011002115.23312-1-sbeller@google.com","subject":"[PATCH 21/28] attr.c: rename a local variable check","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-10-11T00:21:08Z","receivedAt":"2016-10-11T00:22:14Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"From: Junio C Hamano <gitster@pobox.com>\n\nThroughout this series, we are trying to use \"check\" to name an\ninstance of \"git_attr_check\" structure; let's rename a \"check\" that\nrefers to an array whose elements are git_attr_check_elem to avoid\nconfusion.\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Stefan Beller <sbeller@google.com>\n---\n attr.c | 4 ++--\n 1 file changed, 2 insertions(+), 2 deletions(-)\n\ndiff --git a/attr.c b/attr.c\nindex c1d5222..abf23d8 100644\n--- a/attr.c\n+++ b/attr.c\n@@ -682,12 +682,12 @@ static int macroexpand_one(int attr_nr, int rem);\n \n static int fill_one(const char *what, struct match_attr *a, int rem)\n {\n-\tstruct git_attr_check_elem *check = check_all_attr;\n+\tstruct git_attr_check_elem *celem = check_all_attr;\n \tint i;\n \n \tfor (i = a->num_attr - 1; 0 < rem && 0 <= i; i--) {\n \t\tstruct git_attr *attr = a->state[i].attr;\n-\t\tconst char **n = &(check[attr->attr_nr].value);\n+\t\tconst char **n = &(celem[attr->attr_nr].value);\n \t\tconst char *v = a->state[i].setto;\n \n \t\tif (*n == ATTR__UNKNOWN) {\n-- \n2.10.1.382.ga23ca1b.dirty\n\n"},{"id":"303840","messageId":"20161011002115.23312-26-sbeller@google.com","threadId":"44273","inReplyTo":"20161011002115.23312-1-sbeller@google.com","subject":"[PATCH 25/28] attr.c: outline the future plans by heavily commenting","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-10-11T00:21:12Z","receivedAt":"2016-10-11T00:22:17Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"From: Junio C Hamano <gitster@pobox.com>\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Stefan Beller <sbeller@google.com>\n---\n attr.c | 40 +++++++++++++++++++++++++++++++++++++++-\n 1 file changed, 39 insertions(+), 1 deletion(-)\n\ndiff --git a/attr.c b/attr.c\nindex a08783a..e34b4c4 100644\n--- a/attr.c\n+++ b/attr.c\n@@ -30,6 +30,11 @@ static const char git_attr__unknown[] = \"(builtin)unknown\";\n #define DEBUG_ATTR 0\n #endif\n \n+/*\n+ * NEEDSWORK: the global dictionary of the interned attributes\n+ * must stay a singleton even after we become thread-ready.\n+ * Access to these must be surrounded with mutex when it happens.\n+ */\n struct git_attr {\n \tstruct git_attr *next;\n \tunsigned h;\n@@ -39,10 +44,19 @@ struct git_attr {\n \tchar name[FLEX_ARRAY];\n };\n static int attr_nr;\n+static struct git_attr *(git_attr_hash[HASHSIZE]);\n+\n+/*\n+ * NEEDSWORK: maybe-real, maybe-macro are not property of\n+ * an attribute, as it depends on what .gitattributes are\n+ * read.  Once we introduce per git_attr_check attr_stack\n+ * and check_all_attr, the optimization based on them will\n+ * become unnecessary and can go away.  So is this variable.\n+ */\n static int cannot_trust_maybe_real;\n \n+/* NEEDSWORK: This will become per git_attr_check */\n static struct git_attr_check_elem *check_all_attr;\n-static struct git_attr *(git_attr_hash[HASHSIZE]);\n \n const char *git_attr_name(const struct git_attr *attr)\n {\n@@ -117,6 +131,11 @@ struct git_attr *git_attr_counted(const char *name, size_t len)\n \ta->maybe_real = 0;\n \tgit_attr_hash[pos] = a;\n \n+\t/*\n+\t * NEEDSWORK: per git_attr_check check_all_attr\n+\t * will be initialized a lot more lazily, not\n+\t * like this, and not here.\n+\t */\n \tREALLOC_ARRAY(check_all_attr, attr_nr);\n \tcheck_all_attr[a->attr_nr].attr = a;\n \tcheck_all_attr[a->attr_nr].value = ATTR__UNKNOWN;\n@@ -329,6 +348,7 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,\n  * .gitignore file and info/excludes file as a fallback.\n  */\n \n+/* NEEDSWORK: This will become per git_attr_check */\n static struct attr_stack {\n \tstruct attr_stack *prev;\n \tchar *origin;\n@@ -393,6 +413,24 @@ static struct attr_stack *read_attr_from_array(const char **list)\n \treturn res;\n }\n \n+/*\n+ * NEEDSWORK: these two are tricky.  The callers assume there is a\n+ * single, system-wide global state \"where we read attributes from?\"\n+ * and when the state is flipped by calling git_attr_set_direction(),\n+ * attr_stack is discarded so that subsequent attr_check will lazily\n+ * read from the right place.  And they do not know or care who called\n+ * by them uses the attribute subsystem, hence have no knowledge of\n+ * existing git_attr_check instances or future ones that will be\n+ * created).\n+ *\n+ * Probably we need a thread_local that holds these two variables,\n+ * and a list of git_attr_check instances (which need to be maintained\n+ * by hooking into git_attr_check_alloc(), git_attr_check_initl(), and\n+ * git_attr_check_clear().  Then git_attr_set_direction() updates the\n+ * fields in that thread_local for these two variables, iterate over\n+ * all the active git_attr_check instances and discard the attr_stack\n+ * they hold.  Yuck, but it sounds doable.\n+ */\n static enum git_attr_direction direction;\n static struct index_state *use_index;\n \n-- \n2.10.1.382.ga23ca1b.dirty\n\n"},{"id":"303841","messageId":"20161011002115.23312-21-sbeller@google.com","threadId":"44273","inReplyTo":"20161011002115.23312-1-sbeller@google.com","subject":"[PATCH 20/28] attr.c: pass struct git_attr_check down the callchain","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-10-11T00:21:07Z","receivedAt":"2016-10-11T00:22:18Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"From: Junio C Hamano <gitster@pobox.com>\n\nThe callchain that starts from git_check_attrs() down to\ncollect_some_attrs() used to take an array of git_attr_check_elem\nas their parameters.  Pass the enclosing git_attr_check instance\ninstead, so that they will have access to new fields we will add to\nthe data structure.\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Stefan Beller <sbeller@google.com>\n---\n attr.c | 36 ++++++++++++++++++++++++------------\n 1 file changed, 24 insertions(+), 12 deletions(-)\n\ndiff --git a/attr.c b/attr.c\nindex f9ebdd1..c1d5222 100644\n--- a/attr.c\n+++ b/attr.c\n@@ -751,14 +751,25 @@ static int attr_check_is_dynamic(const struct git_attr_check *check)\n  * check_all_attr. If num is non-zero, only attributes in check[] are\n  * collected. Otherwise all attributes are collected.\n  */\n-static void collect_some_attrs(const char *path, int pathlen, int num,\n-\t\t\t       struct git_attr_check_elem *check)\n+static void collect_some_attrs(const char *path, int pathlen,\n+\t\t\t       struct git_attr_check *check)\n \n {\n \tstruct attr_stack *stk;\n \tint i, rem, dirlen;\n \tconst char *cp, *last_slash = NULL;\n \tint basename_offset;\n+\tint num;\n+\tstruct git_attr_check_elem *celem;\n+\n+\tif (!check) {\n+\t\t/* Yuck - ugly git_all_attrs() hack! */\n+\t\tcelem = NULL;\n+\t\tnum = 0;\n+\t} else {\n+\t\tcelem = check->check;\n+\t\tnum = check->check_nr;\n+\t}\n \n \tfor (cp = path; cp < path + pathlen; cp++) {\n \t\tif (*cp == '/' && cp[1])\n@@ -778,9 +789,9 @@ static void collect_some_attrs(const char *path, int pathlen, int num,\n \tif (num && !cannot_trust_maybe_real) {\n \t\trem = 0;\n \t\tfor (i = 0; i < num; i++) {\n-\t\t\tif (!check[i].attr->maybe_real) {\n+\t\t\tif (!celem[i].attr->maybe_real) {\n \t\t\t\tstruct git_attr_check_elem *c;\n-\t\t\t\tc = check_all_attr + check[i].attr->attr_nr;\n+\t\t\t\tc = check_all_attr + celem[i].attr->attr_nr;\n \t\t\t\tc->value = ATTR__UNSET;\n \t\t\t\trem++;\n \t\t\t}\n@@ -794,18 +805,19 @@ static void collect_some_attrs(const char *path, int pathlen, int num,\n \t\trem = fill(path, pathlen, basename_offset, stk, rem);\n }\n \n-static int git_check_attrs(const char *path, int pathlen, int num,\n-\t\t\t   struct git_attr_check_elem *check)\n+static int git_check_attrs(const char *path, int pathlen,\n+\t\t\t   struct git_attr_check *check)\n {\n \tint i;\n+\tstruct git_attr_check_elem *celem = check->check;\n \n-\tcollect_some_attrs(path, pathlen, num, check);\n+\tcollect_some_attrs(path, pathlen, check);\n \n-\tfor (i = 0; i < num; i++) {\n-\t\tconst char *value = check_all_attr[check[i].attr->attr_nr].value;\n+\tfor (i = 0; i < check->check_nr; i++) {\n+\t\tconst char *value = check_all_attr[celem[i].attr->attr_nr].value;\n \t\tif (value == ATTR__UNKNOWN)\n \t\t\tvalue = ATTR__UNSET;\n-\t\tcheck[i].value = value;\n+\t\tcelem[i].value = value;\n \t}\n \n \treturn 0;\n@@ -816,7 +828,7 @@ void git_all_attrs(const char *path, struct git_attr_check *check)\n \tint i;\n \n \tgit_attr_check_clear(check);\n-\tcollect_some_attrs(path, strlen(path), 0, NULL);\n+\tcollect_some_attrs(path, strlen(path), NULL);\n \n \tfor (i = 0; i < attr_nr; i++) {\n \t\tconst char *name = check_all_attr[i].attr->name;\n@@ -845,7 +857,7 @@ int git_check_attr_counted(const char *path, int pathlen,\n \t\t\t   struct git_attr_check *check)\n {\n \tcheck->finalized = 1;\n-\treturn git_check_attrs(path, pathlen, check->check_nr, check->check);\n+\treturn git_check_attrs(path, pathlen, check);\n }\n \n int git_check_attr(const char *path, struct git_attr_check *check)\n-- \n2.10.1.382.ga23ca1b.dirty\n\n"},{"id":"303842","messageId":"20161011002115.23312-27-sbeller@google.com","threadId":"44273","inReplyTo":"20161011002115.23312-1-sbeller@google.com","subject":"[PATCH 26/28] attr: make git_attr_counted static","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-10-11T00:21:13Z","receivedAt":"2016-10-11T00:22:20Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"It's not used outside the attr code, so let's keep it private.\n\nChange-Id: I0d15e0f2ea944b31d68b9cf1a4edecac0ca2170d\nSigned-off-by: Stefan Beller <sbeller@google.com>\n---\n attr.c | 2 +-\n attr.h | 3 ---\n 2 files changed, 1 insertion(+), 4 deletions(-)\n\ndiff --git a/attr.c b/attr.c\nindex e34b4c4..ea6d5c1 100644\n--- a/attr.c\n+++ b/attr.c\n@@ -108,7 +108,7 @@ static void report_invalid_attr(const char *name, size_t len,\n \tstrbuf_release(&err);\n }\n \n-struct git_attr *git_attr_counted(const char *name, size_t len)\n+static struct git_attr *git_attr_counted(const char *name, size_t len)\n {\n \tunsigned hval = hash_name(name, len);\n \tunsigned pos = hval % HASHSIZE;\ndiff --git a/attr.h b/attr.h\nindex 40abc16..f85a95f 100644\n--- a/attr.h\n+++ b/attr.h\n@@ -10,9 +10,6 @@ struct git_attr;\n  */\n extern struct git_attr *git_attr(const char *);\n \n-/* The same, but with counted string */\n-extern struct git_attr *git_attr_counted(const char *, size_t);\n-\n extern int attr_name_valid(const char *name, size_t namelen);\n extern void invalid_attr_name_message(struct strbuf *, const char *, int);\n \n-- \n2.10.1.382.ga23ca1b.dirty\n\n"},{"id":"303843","messageId":"20161011002115.23312-28-sbeller@google.com","threadId":"44273","inReplyTo":"20161011002115.23312-1-sbeller@google.com","subject":"[PATCH 27/28] attr: make git_check_attr_counted static","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-10-11T00:21:14Z","receivedAt":"2016-10-11T00:22:23Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"It's not used outside the attr code, so let's keep it private.\n\nSigned-off-by: Stefan Beller <sbeller@google.com>\n---\n attr.c | 4 ++--\n attr.h | 1 -\n 2 files changed, 2 insertions(+), 3 deletions(-)\n\ndiff --git a/attr.c b/attr.c\nindex ea6d5c1..fea0f10 100644\n--- a/attr.c\n+++ b/attr.c\n@@ -892,8 +892,8 @@ void git_attr_set_direction(enum git_attr_direction new, struct index_state *ist\n \tuse_index = istate;\n }\n \n-int git_check_attr_counted(const char *path, int pathlen,\n-\t\t\t   struct git_attr_check *check)\n+static int git_check_attr_counted(const char *path, int pathlen,\n+\t\t\t\t  struct git_attr_check *check)\n {\n \tcheck->finalized = 1;\n \treturn git_check_attrs(path, pathlen, check);\ndiff --git a/attr.h b/attr.h\nindex f85a95f..163fcd6 100644\n--- a/attr.h\n+++ b/attr.h\n@@ -41,7 +41,6 @@ struct git_attr_check {\n \n extern struct git_attr_check *git_attr_check_initl(const char *, ...);\n extern int git_check_attr(const char *path, struct git_attr_check *);\n-extern int git_check_attr_counted(const char *, int, struct git_attr_check *);\n \n extern struct git_attr_check *git_attr_check_alloc(void);\n extern struct git_attr_check_elem *git_attr_check_append(struct git_attr_check *, const struct git_attr *);\n-- \n2.10.1.382.ga23ca1b.dirty\n\n"},{"id":"303844","messageId":"20161011002115.23312-24-sbeller@google.com","threadId":"44273","inReplyTo":"20161011002115.23312-1-sbeller@google.com","subject":"[PATCH 23/28] attr.c: introduce empty_attr_check_elems()","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-10-11T00:21:10Z","receivedAt":"2016-10-11T00:22:26Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"From: Junio C Hamano <gitster@pobox.com>\n\nOne codepath needs to just empty the git_attr_check_elem array in\nthe git_attr_check structure, without releasing the entire resource.\nIntroduce a helper to do so and rewrite git_attr_check_clear() using\nit.\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Stefan Beller <sbeller@google.com>\n---\n attr.c | 11 +++++++++--\n 1 file changed, 9 insertions(+), 2 deletions(-)\n\ndiff --git a/attr.c b/attr.c\nindex 8d8df20..cb3b702 100644\n--- a/attr.c\n+++ b/attr.c\n@@ -746,6 +746,14 @@ static int attr_check_is_dynamic(const struct git_attr_check *check)\n \treturn (void *)(check->check) != (void *)(check + 1);\n }\n \n+static void empty_attr_check_elems(struct git_attr_check *check)\n+{\n+\tif (!attr_check_is_dynamic(check))\n+\t\tdie(\"BUG: emptying a statically initialized git_attr_check\");\n+\tcheck->check_nr = 0;\n+\tcheck->finalized = 0;\n+}\n+\n /*\n  * Collect attributes for path into the array pointed to by\n  * check_all_attr.  If check is not NULL, only attributes in\n@@ -912,12 +920,11 @@ struct git_attr_check_elem *git_attr_check_append(struct git_attr_check *check,\n \n void git_attr_check_clear(struct git_attr_check *check)\n {\n+\tempty_attr_check_elems(check);\n \tif (!attr_check_is_dynamic(check))\n \t\tdie(\"BUG: clearing a statically initialized git_attr_check\");\n \tfree(check->check);\n-\tcheck->check_nr = 0;\n \tcheck->check_alloc = 0;\n-\tcheck->finalized = 0;\n }\n \n void git_attr_check_free(struct git_attr_check *check)\n-- \n2.10.1.382.ga23ca1b.dirty\n\n"},{"id":"303845","messageId":"20161011002115.23312-29-sbeller@google.com","threadId":"44273","inReplyTo":"20161011002115.23312-1-sbeller@google.com","subject":"[PATCH 28/28] attr: convert to new threadsafe API","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-10-11T00:21:15Z","receivedAt":"2016-10-11T00:22:30Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"This revamps the API of the attr subsystem to be thread safe.\nBefore we had the question and its results in one struct type.\nThe typical usage of the API was\n\n    static struct git_attr_check;\n\n    if (!check)\n        check = git_attr_check_initl(\"text\", NULL);\n\n    git_check_attr(path, check);\n    act_on(check->value[0]);\n\nThis has a couple of issues when it comes to thread safety:\n\n* the initialization is racy in this implementation. To make it\n  thread safe, we need to acquire a mutex, such that only one\n  thread is executing the code in git_attr_check_initl.\n  As we do not want to introduce a mutex at each call site,\n  this is best done in the attr code. However to do so, we need\n  to have access to the `check` variable, i.e. the API has to\n  look like\n    git_attr_check_initl(struct git_attr_check*, ...);\n  Then one of the threads calling git_attr_check_initl will\n  acquire the mutex and init the `check`, while all other threads\n  will wait on the mutex just to realize they're late to the\n  party and they'll return with no work done.\n\n* While the check for attributes to be questioned only need to\n  be initalized once as that part will be read only after its\n  initialisation, the answer may be different for each path.\n  Because of that we need to decouple the check and the answer,\n  such that each thread can obtain an answer for the path it\n  is currently processing.\n\nThis commit only changes the API, i.e. this doesn't implement\nactual thread safety. However a later commit that introduces\nthread safety needs to touch code in attr.c only and doesn't\nhave to add changes all over the place.\n\nSigned-off-by: Stefan Beller <sbeller@google.com>\n---\n Documentation/technical/api-gitattributes.txt |  80 +++++++++++++-------\n archive.c                                     |  11 ++-\n attr.c                                        | 101 +++++++++++++++-----------\n attr.h                                        |  67 ++++++++++-------\n builtin/check-attr.c                          |  27 ++++---\n builtin/pack-objects.c                        |   9 ++-\n convert.c                                     |  36 ++++-----\n ll-merge.c                                    |  21 ++++--\n userdiff.c                                    |  15 ++--\n ws.c                                          |  13 ++--\n 10 files changed, 230 insertions(+), 150 deletions(-)\n\ndiff --git a/Documentation/technical/api-gitattributes.txt b/Documentation/technical/api-gitattributes.txt\nindex 92fc32a..2059aab 100644\n--- a/Documentation/technical/api-gitattributes.txt\n+++ b/Documentation/technical/api-gitattributes.txt\n@@ -8,6 +8,18 @@ attributes to set of paths.\n Data Structure\n --------------\n \n+extern struct git_attr *git_attr(const char *);\n+\n+/*\n+ * Return the name of the attribute represented by the argument.  The\n+ * return value is a pointer to a null-delimited string that is part\n+ * of the internal data structure; it should not be modified or freed.\n+ */\n+extern const char *git_attr_name(const struct git_attr *);\n+\n+extern int attr_name_valid(const char *name, size_t namelen);\n+extern void invalid_attr_name_message(struct strbuf *, const char *, int);\n+\n `struct git_attr`::\n \n \tAn attribute is an opaque object that is identified by its name.\n@@ -16,15 +28,17 @@ Data Structure\n \tof no interest to the calling programs.  The name of the\n \tattribute can be retrieved by calling `git_attr_name()`.\n \n-`struct git_attr_check_elem`::\n-\n-\tThis structure represents one attribute and its value.\n-\n `struct git_attr_check`::\n \n-\tThis structure represents a collection of `git_attr_check_elem`.\n+\tThis structure represents a collection of `struct git_attrs`.\n \tIt is passed to `git_check_attr()` function, specifying the\n-\tattributes to check, and receives their values.\n+\tattributes to check, and receives their values into a corresponding\n+\t`struct git_attr_result`.\n+\n+`struct git_attr_result`::\n+\n+\tThis structure represents a collection of results to its\n+\tcorresponding `struct git_attr_check`, that has the same order.\n \n \n Attribute Values\n@@ -56,16 +70,22 @@ Querying Specific Attributes\n * Prepare `struct git_attr_check` using git_attr_check_initl()\n   function, enumerating the names of attributes whose values you are\n   interested in, terminated with a NULL pointer.  Alternatively, an\n-  empty `struct git_attr_check` can be prepared by calling\n-  `git_attr_check_alloc()` function and then attributes you want to\n-  ask about can be added to it with `git_attr_check_append()`\n-  function.\n+  empty `struct git_attr_check` as alloced by git_attr_check_alloc()\n+  can be prepared by calling `git_attr_check_alloc()` function and\n+  then attributes you want to ask about can be added to it with\n+  `git_attr_check_append()` function.\n+  git_attr_check_initl is thread safe, i.e. you can call it\n+  from different threads at the same time; internally however only one\n+  call at a time is processed. If the calls from different threads have\n+  the same arguments, the returned `git_attr_check` may be the same.\n \n-* Call `git_check_attr()` to check the attributes for the path.\n+* Call `git_check_attr()` to check the attributes for the path,\n+  the returned `git_attr_result` contains the result.\n \n-* Inspect `git_attr_check` structure to see how each of the\n-  attribute in the array is defined for the path.\n+* Inspect the returned `git_attr_result` structure to see how\n+  each of the attribute in the array is defined for the path.\n \n+* Do not free the result as the memory is owned by the attr subsystem.\n \n Example\n -------\n@@ -89,15 +109,20 @@ static void setup_check(void)\n \n ------------\n \tconst char *path;\n+\tstruct git_attr_result *result;\n \n \tsetup_check();\n-\tgit_check_attr(path, check);\n+\tresult = git_check_attr(path, check);\n ------------\n \n-. Act on `.value` member of the result, left in `check->check[]`:\n+The `result` must not be free'd as it is owned by the attr subsystem.\n+It is reused by the same thread, so a subsequent call to git_check_attr\n+in the same thread will overwrite the result.\n+\n+. Act on `result->value[]`:\n \n ------------\n-\tconst char *value = check->check[0].value;\n+\tconst char *value = result->value[0];\n \n \tif (ATTR_TRUE(value)) {\n \t\tThe attribute is Set, by listing only the name of the\n@@ -123,6 +148,8 @@ the first step in the above would be different.\n static struct git_attr_check *check;\n static void setup_check(const char **argv)\n {\n+\tif (check)\n+\t\treturn; /* already done */\n \tcheck = git_attr_check_alloc();\n \twhile (*argv) {\n \t\tstruct git_attr *attr = git_attr(*argv);\n@@ -138,17 +165,20 @@ Querying All Attributes\n \n To get the values of all attributes associated with a file:\n \n-* Prepare an empty `git_attr_check` structure by calling\n-  `git_attr_check_alloc()`.\n+* Setup a local variables on the stack for both the question\n+  `struct git_attr_check` as well as the result `struct git_attr_result`.\n+  Zero them out via their respective _INIT macro.\n \n-* Call `git_all_attrs()`, which populates the `git_attr_check`\n-  with the attributes attached to the path.\n+* Call `git_all_attrs()`\n \n-* Iterate over the `git_attr_check.check[]` array to examine\n-  the attribute names and values.  The name of the attribute\n-  described by a  `git_attr_check.check[]` object can be retrieved via\n-  `git_attr_name(check->check[i].attr)`.  (Please note that no items\n+* Iterate over the `git_attr_check.attr[]` array to examine the\n+  attribute names.  The name of the attribute described by a\n+  `git_attr_check.attr[]` object can be retrieved via\n+  `git_attr_name(check->attr[i])`.  (Please note that no items\n   will be returned for unset attributes, so `ATTR_UNSET()` will return\n   false for all returned `git_array_check` objects.)\n+  The respective value for an attribute can be found in the same\n+  index position in of `git_attr_result`.\n \n-* Free the `git_array_check` by calling `git_attr_check_free()`.\n+* Clear the local variables by calling `git_attr_check_clear()` and\n+  `git_attr_result_clear()`.\ndiff --git a/archive.c b/archive.c\nindex 11e3951..795678a 100644\n--- a/archive.c\n+++ b/archive.c\n@@ -111,6 +111,7 @@ static int write_archive_entry(const unsigned char *sha1, const char *base,\n \tstruct archiver_args *args = c->args;\n \twrite_archive_entry_fn_t write_entry = c->write_entry;\n \tstatic struct git_attr_check *check;\n+\tstruct git_attr_result *result;\n \tconst char *path_without_prefix;\n \tint err;\n \n@@ -125,11 +126,13 @@ static int write_archive_entry(const unsigned char *sha1, const char *base,\n \tpath_without_prefix = path.buf + args->baselen;\n \n \tif (!check)\n-\t\tcheck = git_attr_check_initl(\"export-ignore\", \"export-subst\", NULL);\n-\tif (!git_check_attr(path_without_prefix, check)) {\n-\t\tif (ATTR_TRUE(check->check[0].value))\n+\t\tgit_attr_check_initl(&check, \"export-ignore\", \"export-subst\", NULL);\n+\n+\tresult = git_check_attr(path_without_prefix, check);\n+\tif (result) {\n+\t\tif (ATTR_TRUE(result->value[0]))\n \t\t\treturn 0;\n-\t\targs->convert = ATTR_TRUE(check->check[1].value);\n+\t\targs->convert = ATTR_TRUE(result->value[1]);\n \t}\n \n \tif (S_ISDIR(mode) || S_ISGITLINK(mode)) {\ndiff --git a/attr.c b/attr.c\nindex fea0f10..12205b7 100644\n--- a/attr.c\n+++ b/attr.c\n@@ -55,6 +55,16 @@ static struct git_attr *(git_attr_hash[HASHSIZE]);\n  */\n static int cannot_trust_maybe_real;\n \n+/*\n+ * Send one or more git_attr_check to git_check_attrs(), and\n+ * each 'value' member tells what its value is.\n+ * Unset one is returned as NULL.\n+ */\n+struct git_attr_check_elem {\n+\tconst struct git_attr *attr;\n+\tconst char *value;\n+};\n+\n /* NEEDSWORK: This will become per git_attr_check */\n static struct git_attr_check_elem *check_all_attr;\n \n@@ -781,7 +791,7 @@ static int macroexpand_one(int nr, int rem)\n \n static int attr_check_is_dynamic(const struct git_attr_check *check)\n {\n-\treturn (void *)(check->check) != (void *)(check + 1);\n+\treturn (void *)(check->attr) != (void *)(check + 1);\n }\n \n static void empty_attr_check_elems(struct git_attr_check *check)\n@@ -799,7 +809,8 @@ static void empty_attr_check_elems(struct git_attr_check *check)\n  * any and all attributes that are visible are collected in it.\n  */\n static void collect_some_attrs(const char *path, int pathlen,\n-\t\t\t       struct git_attr_check *check, int collect_all)\n+\t\t\t       struct git_attr_check *check,\n+\t\t\t       struct git_attr_result *result, int collect_all)\n \n {\n \tstruct attr_stack *stk;\n@@ -825,13 +836,11 @@ static void collect_some_attrs(const char *path, int pathlen,\n \t\tcheck_all_attr[i].value = ATTR__UNKNOWN;\n \n \tif (!collect_all && !cannot_trust_maybe_real) {\n-\t\tstruct git_attr_check_elem *celem = check->check;\n-\n \t\trem = 0;\n \t\tfor (i = 0; i < check->check_nr; i++) {\n-\t\t\tif (!celem[i].attr->maybe_real) {\n+\t\t\tif (!check->attr[i]->maybe_real) {\n \t\t\t\tstruct git_attr_check_elem *c;\n-\t\t\t\tc = check_all_attr + celem[i].attr->attr_nr;\n+\t\t\t\tc = check_all_attr + check->attr[i]->attr_nr;\n \t\t\t\tc->value = ATTR__UNSET;\n \t\t\t\trem++;\n \t\t\t}\n@@ -845,38 +854,45 @@ static void collect_some_attrs(const char *path, int pathlen,\n \t\trem = fill(path, pathlen, basename_offset, stk, rem);\n \n \tif (collect_all) {\n-\t\tempty_attr_check_elems(check);\n \t\tfor (i = 0; i < attr_nr; i++) {\n \t\t\tconst struct git_attr *attr = check_all_attr[i].attr;\n \t\t\tconst char *value = check_all_attr[i].value;\n \t\t\tif (value == ATTR__UNSET || value == ATTR__UNKNOWN)\n \t\t\t\tcontinue;\n-\t\t\tgit_attr_check_append(check, attr)->value = value;\n+\n+\t\t\tgit_attr_check_append(check, attr);\n+\n+\t\t\tALLOC_GROW(result->value,\n+\t\t\t\t   result->check_nr + 1,\n+\t\t\t\t   result->check_alloc);\n+\t\t\tresult->value[result->check_nr++] = value;\n \t\t}\n \t}\n }\n \n static int git_check_attrs(const char *path, int pathlen,\n-\t\t\t   struct git_attr_check *check)\n+\t\t\t   struct git_attr_check *check,\n+\t\t\t   struct git_attr_result *result)\n {\n \tint i;\n-\tstruct git_attr_check_elem *celem = check->check;\n \n-\tcollect_some_attrs(path, pathlen, check, 0);\n+\tcollect_some_attrs(path, pathlen, check, result, 0);\n \n \tfor (i = 0; i < check->check_nr; i++) {\n-\t\tconst char *value = check_all_attr[celem[i].attr->attr_nr].value;\n+\t\tconst char *value = check_all_attr[check->attr[i]->attr_nr].value;\n \t\tif (value == ATTR__UNKNOWN)\n \t\t\tvalue = ATTR__UNSET;\n-\t\tcelem[i].value = value;\n+\t\tresult->value[i] = value;\n \t}\n \n \treturn 0;\n }\n \n-void git_all_attrs(const char *path, struct git_attr_check *check)\n+void git_all_attrs(const char *path,\n+\t\t   struct git_attr_check *check,\n+\t\t   struct git_attr_result *result)\n {\n-\tcollect_some_attrs(path, strlen(path), check, 1);\n+\tcollect_some_attrs(path, strlen(path), check, result, 1);\n }\n \n void git_attr_set_direction(enum git_attr_direction new, struct index_state *istate)\n@@ -892,36 +908,41 @@ void git_attr_set_direction(enum git_attr_direction new, struct index_state *ist\n \tuse_index = istate;\n }\n \n-static int git_check_attr_counted(const char *path, int pathlen,\n-\t\t\t\t  struct git_attr_check *check)\n+struct git_attr_result *git_check_attr(const char *path,\n+\t\t\t\t       struct git_attr_check *check)\n {\n-\tcheck->finalized = 1;\n-\treturn git_check_attrs(path, pathlen, check);\n-}\n+\tstatic struct git_attr_result *result;\n+\tif (!result)\n+\t\tresult = xcalloc(1, sizeof(*result));\n \n-int git_check_attr(const char *path, struct git_attr_check *check)\n-{\n-\treturn git_check_attr_counted(path, strlen(path), check);\n+\tALLOC_GROW(result->value, check->check_nr, result->check_alloc);\n+\tresult->check_nr = check->check_nr;\n+\n+\tcheck->finalized = 1;\n+\tgit_check_attrs(path, strlen(path), check, result);\n+\treturn result;\n }\n \n-struct git_attr_check *git_attr_check_initl(const char *one, ...)\n+extern void git_attr_check_initl(struct git_attr_check **check_,\n+\t\t\t\t const char *one, ...)\n {\n-\tstruct git_attr_check *check;\n \tint cnt;\n \tva_list params;\n \tconst char *param;\n+\tstruct git_attr_check *check;\n \n \tva_start(params, one);\n \tfor (cnt = 1; (param = va_arg(params, const char *)) != NULL; cnt++)\n \t\t;\n \tva_end(params);\n+\n \tcheck = xcalloc(1,\n-\t\t\tsizeof(*check) + cnt * sizeof(*(check->check)));\n+\t\t\tsizeof(*check) + cnt * sizeof(*(check->attr)));\n \tcheck->check_nr = cnt;\n \tcheck->finalized = 1;\n-\tcheck->check = (struct git_attr_check_elem *)(check + 1);\n+\tcheck->attr = (const struct git_attr **)(check + 1);\n \n-\tcheck->check[0].attr = git_attr(one);\n+\tcheck->attr[0] = git_attr(one);\n \tva_start(params, one);\n \tfor (cnt = 1; cnt < check->check_nr; cnt++) {\n \t\tstruct git_attr *attr;\n@@ -932,10 +953,10 @@ struct git_attr_check *git_attr_check_initl(const char *one, ...)\n \t\tattr = git_attr(param);\n \t\tif (!attr)\n \t\t\tdie(\"BUG: %s: not a valid attribute name\", param);\n-\t\tcheck->check[cnt].attr = attr;\n+\t\tcheck->attr[cnt] = attr;\n \t}\n \tva_end(params);\n-\treturn check;\n+\t*check_ = check;\n }\n \n struct git_attr_check *git_attr_check_alloc(void)\n@@ -943,18 +964,15 @@ struct git_attr_check *git_attr_check_alloc(void)\n \treturn xcalloc(1, sizeof(struct git_attr_check));\n }\n \n-struct git_attr_check_elem *git_attr_check_append(struct git_attr_check *check,\n-\t\t\t\t\t\t  const struct git_attr *attr)\n+void git_attr_check_append(struct git_attr_check *check,\n+\t\t\t   const struct git_attr *attr)\n {\n-\tstruct git_attr_check_elem *elem;\n \tif (check->finalized)\n \t\tdie(\"BUG: append after git_attr_check structure is finalized\");\n \tif (!attr_check_is_dynamic(check))\n \t\tdie(\"BUG: appending to a statically initialized git_attr_check\");\n-\tALLOC_GROW(check->check, check->check_nr + 1, check->check_alloc);\n-\telem = &check->check[check->check_nr++];\n-\telem->attr = attr;\n-\treturn elem;\n+\tALLOC_GROW(check->attr, check->check_nr + 1, check->check_alloc);\n+\tcheck->attr[check->check_nr++] = attr;\n }\n \n void git_attr_check_clear(struct git_attr_check *check)\n@@ -962,12 +980,13 @@ void git_attr_check_clear(struct git_attr_check *check)\n \tempty_attr_check_elems(check);\n \tif (!attr_check_is_dynamic(check))\n \t\tdie(\"BUG: clearing a statically initialized git_attr_check\");\n-\tfree(check->check);\n+\tfree(check->attr);\n \tcheck->check_alloc = 0;\n }\n \n-void git_attr_check_free(struct git_attr_check *check)\n+void git_attr_result_clear(struct git_attr_result *result)\n {\n-\tgit_attr_check_clear(check);\n-\tfree(check);\n+\tfree(result->value);\n+\tresult->check_nr = 0;\n+\tresult->check_alloc = 0;\n }\ndiff --git a/attr.h b/attr.h\nindex 163fcd6..30bed1e 100644\n--- a/attr.h\n+++ b/attr.h\n@@ -10,6 +10,13 @@ struct git_attr;\n  */\n extern struct git_attr *git_attr(const char *);\n \n+/*\n+ * Return the name of the attribute represented by the argument.  The\n+ * return value is a pointer to a null-delimited string that is part\n+ * of the internal data structure; it should not be modified or freed.\n+ */\n+extern const char *git_attr_name(const struct git_attr *);\n+\n extern int attr_name_valid(const char *name, size_t namelen);\n extern void invalid_attr_name_message(struct strbuf *, const char *, int);\n \n@@ -22,44 +29,52 @@ extern const char git_attr__false[];\n #define ATTR_FALSE(v) ((v) == git_attr__false)\n #define ATTR_UNSET(v) ((v) == NULL)\n \n-/*\n- * Send one or more git_attr_check to git_check_attrs(), and\n- * each 'value' member tells what its value is.\n- * Unset one is returned as NULL.\n- */\n-struct git_attr_check_elem {\n-\tconst struct git_attr *attr;\n-\tconst char *value;\n-};\n-\n struct git_attr_check {\n \tint finalized;\n \tint check_nr;\n \tint check_alloc;\n-\tstruct git_attr_check_elem *check;\n+\tconst struct git_attr **attr;\n };\n+#define GIT_ATTR_CHECK_INIT {0, 0, 0, NULL}\n \n-extern struct git_attr_check *git_attr_check_initl(const char *, ...);\n-extern int git_check_attr(const char *path, struct git_attr_check *);\n+struct git_attr_result {\n+\tint check_nr;\n+\tint check_alloc;\n+\tconst char **value;\n+};\n+#define GIT_ATTR_RESULT_INIT {0, 0, NULL}\n \n+/*\n+ * Initialize the `git_attr_check` via one of the following three functions:\n+ *\n+ * git_attr_check_alloc allocates an empty check, add more attributes via\n+ *                      git_attr_check_append.\n+ * git_all_attrs        allocates a check and fills in all attributes that\n+ *                      are set for the given path.\n+ * git_attr_check_initl takes a pointer to where the check will be initialized,\n+ *                      followed by all attributes that are to be checked.\n+ *                      This makes it potentially thread safe as it could\n+ *                      internally have a mutex for that memory location.\n+ *                      Currently it is not thread safe!\n+ */\n extern struct git_attr_check *git_attr_check_alloc(void);\n-extern struct git_attr_check_elem *git_attr_check_append(struct git_attr_check *, const struct git_attr *);\n+extern void git_attr_check_append(struct git_attr_check *, const struct git_attr *);\n+extern void git_all_attrs(const char *path, struct git_attr_check *, struct git_attr_result *);\n+extern void git_attr_check_initl(struct git_attr_check **, const char *, ...);\n \n-extern void git_attr_check_clear(struct git_attr_check *);\n-extern void git_attr_check_free(struct git_attr_check *);\n+/* Query a path for its attributes */\n+extern struct git_attr_result *git_check_attr(const char *path,\n+\t\t\t\t\t      struct git_attr_check *);\n \n-/*\n- * Return the name of the attribute represented by the argument.  The\n- * return value is a pointer to a null-delimited string that is part\n- * of the internal data structure; it should not be modified or freed.\n+/**\n+ * Free or clear the result struct. The underlying strings are not free'd\n+ * as they are globally known.\n  */\n-extern const char *git_attr_name(const struct git_attr *);\n+extern void git_attr_result_free(struct git_attr_result *);\n+extern void git_attr_result_clear(struct git_attr_result *);\n+\n+extern void git_attr_check_clear(struct git_attr_check *);\n \n-/*\n- * Retrieve all attributes that apply to the specified path.\n- * check holds the attributes and their values.\n- */\n-void git_all_attrs(const char *path, struct git_attr_check *check);\n \n enum git_attr_direction {\n \tGIT_ATTR_CHECKIN,\ndiff --git a/builtin/check-attr.c b/builtin/check-attr.c\nindex ec61476..609b192 100644\n--- a/builtin/check-attr.c\n+++ b/builtin/check-attr.c\n@@ -24,13 +24,17 @@ static const struct option check_attr_options[] = {\n \tOPT_END()\n };\n \n-static void output_attr(struct git_attr_check *check, const char *file)\n+static void output_attr(struct git_attr_check *check,\n+\t\t\tstruct git_attr_result *result, const char *file)\n {\n \tint j;\n \tint cnt = check->check_nr;\n \n+\tif (check->check_nr != result->check_nr)\n+\t\tdie(\"BUG: confused check and result internally\");\n+\n \tfor (j = 0; j < cnt; j++) {\n-\t\tconst char *value = check->check[j].value;\n+\t\tconst char *value = result->value[j];\n \n \t\tif (ATTR_TRUE(value))\n \t\t\tvalue = \"set\";\n@@ -44,11 +48,11 @@ static void output_attr(struct git_attr_check *check, const char *file)\n \t\t\t       \"%s%c\" /* attrname */\n \t\t\t       \"%s%c\" /* attrvalue */,\n \t\t\t       file, 0,\n-\t\t\t       git_attr_name(check->check[j].attr), 0, value, 0);\n+\t\t\t       git_attr_name(check->attr[j]), 0, value, 0);\n \t\t} else {\n \t\t\tquote_c_style(file, NULL, stdout, 0);\n \t\t\tprintf(\": %s: %s\\n\",\n-\t\t\t       git_attr_name(check->check[j].attr), value);\n+\t\t\t       git_attr_name(check->attr[j]), value);\n \t\t}\n \t}\n }\n@@ -60,14 +64,17 @@ static void check_attr(const char *prefix,\n \tchar *full_path =\n \t\tprefix_path(prefix, prefix ? strlen(prefix) : 0, file);\n \tif (check != NULL) {\n-\t\tif (git_check_attr(full_path, check))\n+\t\tstruct git_attr_result *result = git_check_attr(full_path, check);\n+\t\tif (!result)\n \t\t\tdie(\"git_check_attr died\");\n-\t\toutput_attr(check, file);\n+\t\toutput_attr(check, result, file);\n \t} else {\n-\t\tcheck = git_attr_check_alloc();\n-\t\tgit_all_attrs(full_path, check);\n-\t\toutput_attr(check, file);\n-\t\tgit_attr_check_free(check);\n+\t\tstruct git_attr_check check = GIT_ATTR_CHECK_INIT;\n+\t\tstruct git_attr_result result = GIT_ATTR_RESULT_INIT;\n+\t\tgit_all_attrs(full_path, &check, &result);\n+\t\toutput_attr(&check, &result, file);\n+\t\tgit_attr_result_clear(&result);\n+\t\tgit_attr_check_clear(&check);\n \t}\n \tfree(full_path);\n }\ndiff --git a/builtin/pack-objects.c b/builtin/pack-objects.c\nindex df4e6b6..fa8d7a9 100644\n--- a/builtin/pack-objects.c\n+++ b/builtin/pack-objects.c\n@@ -898,12 +898,15 @@ static void write_pack_file(void)\n static int no_try_delta(const char *path)\n {\n \tstatic struct git_attr_check *check;\n+\tstruct git_attr_result *result;\n \n \tif (!check)\n-\t\tcheck = git_attr_check_initl(\"delta\", NULL);\n-\tif (git_check_attr(path, check))\n+\t\tgit_attr_check_initl(&check, \"delta\", NULL);\n+\n+\tresult = git_check_attr(path, check);\n+\tif (!result)\n \t\treturn 0;\n-\tif (ATTR_FALSE(check->check[0].value))\n+\tif (ATTR_FALSE(result->value[0]))\n \t\treturn 1;\n \treturn 0;\n }\ndiff --git a/convert.c b/convert.c\nindex bb2435a..8f1c405 100644\n--- a/convert.c\n+++ b/convert.c\n@@ -718,10 +718,8 @@ static int ident_to_worktree(const char *path, const char *src, size_t len,\n \treturn 1;\n }\n \n-static enum crlf_action git_path_check_crlf(struct git_attr_check_elem *check)\n+static enum crlf_action git_path_check_crlf(const char *value)\n {\n-\tconst char *value = check->value;\n-\n \tif (ATTR_TRUE(value))\n \t\treturn CRLF_TEXT;\n \telse if (ATTR_FALSE(value))\n@@ -735,10 +733,8 @@ static enum crlf_action git_path_check_crlf(struct git_attr_check_elem *check)\n \treturn CRLF_UNDEFINED;\n }\n \n-static enum eol git_path_check_eol(struct git_attr_check_elem *check)\n+static enum eol git_path_check_eol(const char *value)\n {\n-\tconst char *value = check->value;\n-\n \tif (ATTR_UNSET(value))\n \t\t;\n \telse if (!strcmp(value, \"lf\"))\n@@ -748,9 +744,8 @@ static enum eol git_path_check_eol(struct git_attr_check_elem *check)\n \treturn EOL_UNSET;\n }\n \n-static struct convert_driver *git_path_check_convert(struct git_attr_check_elem *check)\n+static struct convert_driver *git_path_check_convert(const char *value)\n {\n-\tconst char *value = check->value;\n \tstruct convert_driver *drv;\n \n \tif (ATTR_TRUE(value) || ATTR_FALSE(value) || ATTR_UNSET(value))\n@@ -761,10 +756,8 @@ static struct convert_driver *git_path_check_convert(struct git_attr_check_elem\n \treturn NULL;\n }\n \n-static int git_path_check_ident(struct git_attr_check_elem *check)\n+static int git_path_check_ident(const char *value)\n {\n-\tconst char *value = check->value;\n-\n \treturn !!ATTR_TRUE(value);\n }\n \n@@ -778,25 +771,26 @@ struct conv_attrs {\n static void convert_attrs(struct conv_attrs *ca, const char *path)\n {\n \tstatic struct git_attr_check *check;\n+\tstruct git_attr_result *result;\n \n \tif (!check) {\n-\t\tcheck = git_attr_check_initl(\"crlf\", \"ident\",\n-\t\t\t\t\t     \"filter\", \"eol\", \"text\",\n-\t\t\t\t\t     NULL);\n+\t\tgit_attr_check_initl(&check, \"crlf\", \"ident\", \"filter\",\n+\t\t\t\t     \"eol\", \"text\", NULL);\n \t\tuser_convert_tail = &user_convert;\n \t\tgit_config(read_convert_config, NULL);\n \t}\n \n-\tif (!git_check_attr(path, check)) {\n-\t\tstruct git_attr_check_elem *ccheck = check->check;\n-\t\tca->crlf_action = git_path_check_crlf(ccheck + 4);\n+\tresult = git_check_attr(path, check);\n+\n+\tif (result) {\n+\t\tca->crlf_action = git_path_check_crlf(result->value[4]);\n \t\tif (ca->crlf_action == CRLF_UNDEFINED)\n-\t\t\tca->crlf_action = git_path_check_crlf(ccheck + 0);\n+\t\t\tca->crlf_action = git_path_check_crlf(result->value[0]);\n \t\tca->attr_action = ca->crlf_action;\n-\t\tca->ident = git_path_check_ident(ccheck + 1);\n-\t\tca->drv = git_path_check_convert(ccheck + 2);\n+\t\tca->ident = git_path_check_ident(result->value[1]);\n+\t\tca->drv = git_path_check_convert(result->value[2]);\n \t\tif (ca->crlf_action != CRLF_BINARY) {\n-\t\t\tenum eol eol_attr = git_path_check_eol(ccheck + 3);\n+\t\t\tenum eol eol_attr = git_path_check_eol(result->value[3]);\n \t\t\tif (ca->crlf_action == CRLF_AUTO && eol_attr == EOL_LF)\n \t\t\t\tca->crlf_action = CRLF_AUTO_INPUT;\n \t\t\telse if (ca->crlf_action == CRLF_AUTO && eol_attr == EOL_CRLF)\ndiff --git a/ll-merge.c b/ll-merge.c\nindex bc6479c..385c26a 100644\n--- a/ll-merge.c\n+++ b/ll-merge.c\n@@ -355,6 +355,7 @@ int ll_merge(mmbuffer_t *result_buf,\n {\n \tstatic struct git_attr_check *check;\n \tstatic const struct ll_merge_options default_opts;\n+\tstruct git_attr_result *result;\n \tconst char *ll_driver_name = NULL;\n \tint marker_size = DEFAULT_CONFLICT_MARKER_SIZE;\n \tconst struct ll_merge_driver *driver;\n@@ -369,12 +370,13 @@ int ll_merge(mmbuffer_t *result_buf,\n \t}\n \n \tif (!check)\n-\t\tcheck = git_attr_check_initl(\"merge\", \"conflict-marker-size\", NULL);\n+\t\tgit_attr_check_initl(&check, \"merge\", \"conflict-marker-size\", NULL);\n \n-\tif (!git_check_attr(path, check)) {\n-\t\tll_driver_name = check->check[0].value;\n-\t\tif (check->check[1].value) {\n-\t\t\tmarker_size = atoi(check->check[1].value);\n+\tresult = git_check_attr(path, check);\n+\tif (result) {\n+\t\tll_driver_name = result->value[0];\n+\t\tif (result->value[1]) {\n+\t\t\tmarker_size = atoi(result->value[1]);\n \t\t\tif (marker_size <= 0)\n \t\t\t\tmarker_size = DEFAULT_CONFLICT_MARKER_SIZE;\n \t\t}\n@@ -394,12 +396,15 @@ int ll_merge(mmbuffer_t *result_buf,\n int ll_merge_marker_size(const char *path)\n {\n \tstatic struct git_attr_check *check;\n+\tstruct git_attr_result *result;\n \tint marker_size = DEFAULT_CONFLICT_MARKER_SIZE;\n \n \tif (!check)\n-\t\tcheck = git_attr_check_initl(\"conflict-marker-size\", NULL);\n-\tif (!git_check_attr(path, check) && check->check[0].value) {\n-\t\tmarker_size = atoi(check->check[0].value);\n+\t\tgit_attr_check_initl(&check, \"conflict-marker-size\", NULL);\n+\n+\tresult = git_check_attr(path, check);\n+\tif (result && result->value[0]) {\n+\t\tmarker_size = atoi(result->value[0]);\n \t\tif (marker_size <= 0)\n \t\t\tmarker_size = DEFAULT_CONFLICT_MARKER_SIZE;\n \t}\ndiff --git a/userdiff.c b/userdiff.c\nindex 46dfd32..afc142a 100644\n--- a/userdiff.c\n+++ b/userdiff.c\n@@ -263,21 +263,24 @@ struct userdiff_driver *userdiff_find_by_name(const char *name) {\n struct userdiff_driver *userdiff_find_by_path(const char *path)\n {\n \tstatic struct git_attr_check *check;\n+\tstruct git_attr_result *result;\n \n \tif (!check)\n-\t\tcheck = git_attr_check_initl(\"diff\", NULL);\n+\t\tgit_attr_check_initl(&check, \"diff\", NULL);\n \tif (!path)\n \t\treturn NULL;\n-\tif (git_check_attr(path, check))\n+\n+\tresult = git_check_attr(path, check);\n+\tif (!result)\n \t\treturn NULL;\n \n-\tif (ATTR_TRUE(check->check[0].value))\n+\tif (ATTR_TRUE(result->value[0]))\n \t\treturn &driver_true;\n-\tif (ATTR_FALSE(check->check[0].value))\n+\tif (ATTR_FALSE(result->value[0]))\n \t\treturn &driver_false;\n-\tif (ATTR_UNSET(check->check[0].value))\n+\tif (ATTR_UNSET(result->value[0]))\n \t\treturn NULL;\n-\treturn userdiff_find_by_name(check->check[0].value);\n+\treturn userdiff_find_by_name(result->value[0]);\n }\n \n struct userdiff_driver *userdiff_get_textconv(struct userdiff_driver *driver)\ndiff --git a/ws.c b/ws.c\nindex bb3270c..a6ffbae 100644\n--- a/ws.c\n+++ b/ws.c\n@@ -73,15 +73,16 @@ unsigned parse_whitespace_rule(const char *string)\n \n unsigned whitespace_rule(const char *pathname)\n {\n-\tstatic struct git_attr_check *attr_whitespace_rule;\n+\tstatic struct git_attr_check *check;\n+\tstruct git_attr_result *result;\n \n-\tif (!attr_whitespace_rule)\n-\t\tattr_whitespace_rule = git_attr_check_initl(\"whitespace\", NULL);\n+\tif (!check)\n+\t\tgit_attr_check_initl(&check, \"whitespace\", NULL);\n \n-\tif (!git_check_attr(pathname, attr_whitespace_rule)) {\n-\t\tconst char *value;\n+\tresult = git_check_attr(pathname, check);\n \n-\t\tvalue = attr_whitespace_rule->check[0].value;\n+\tif (result) {\n+\t\tconst char *value = result->value[0];\n \t\tif (ATTR_TRUE(value)) {\n \t\t\t/* true (whitespace) */\n \t\t\tunsigned all_rule = ws_tab_width(whitespace_rule_cfg);\n-- \n2.10.1.382.ga23ca1b.dirty\n\n"},{"id":"303846","messageId":"20161011002115.23312-25-sbeller@google.com","threadId":"44273","inReplyTo":"20161011002115.23312-1-sbeller@google.com","subject":"[PATCH 24/28] attr.c: always pass check[] to collect_some_attrs()","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-10-11T00:21:11Z","receivedAt":"2016-10-11T00:22:31Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"From: Junio C Hamano <gitster@pobox.com>\n\nThis function used to be called with check=NULL to signal it to\ncollect all attributes in the global check_all_attr[] array.\n\nBecause the longer term plan is to allocate check_all_attr[] and\nattr_stack data structures per git_attr_check instance (i.e. \"check\"\nhere) to make the attr subsystem thread-safe, it is unacceptable.\n\nPass \"Are we grabbing all attributes defined in the system?\" bit as\na separate argument and pass it from the callers.\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Stefan Beller <sbeller@google.com>\n---\n attr.c | 37 +++++++++++++++++++------------------\n 1 file changed, 19 insertions(+), 18 deletions(-)\n\ndiff --git a/attr.c b/attr.c\nindex cb3b702..a08783a 100644\n--- a/attr.c\n+++ b/attr.c\n@@ -756,11 +756,12 @@ static void empty_attr_check_elems(struct git_attr_check *check)\n \n /*\n  * Collect attributes for path into the array pointed to by\n- * check_all_attr.  If check is not NULL, only attributes in\n- * check[] are collected. Otherwise all attributes are collected.\n+ * check_all_attr.  If collect_all is zero, only attributes in\n+ * check[] are collected.  Otherwise, check[] is cleared and\n+ * any and all attributes that are visible are collected in it.\n  */\n static void collect_some_attrs(const char *path, int pathlen,\n-\t\t\t       struct git_attr_check *check)\n+\t\t\t       struct git_attr_check *check, int collect_all)\n \n {\n \tstruct attr_stack *stk;\n@@ -781,10 +782,11 @@ static void collect_some_attrs(const char *path, int pathlen,\n \t}\n \n \tprepare_attr_stack(path, dirlen);\n+\n \tfor (i = 0; i < attr_nr; i++)\n \t\tcheck_all_attr[i].value = ATTR__UNKNOWN;\n \n-\tif (check && !cannot_trust_maybe_real) {\n+\tif (!collect_all && !cannot_trust_maybe_real) {\n \t\tstruct git_attr_check_elem *celem = check->check;\n \n \t\trem = 0;\n@@ -803,6 +805,17 @@ static void collect_some_attrs(const char *path, int pathlen,\n \trem = attr_nr;\n \tfor (stk = attr_stack; 0 < rem && stk; stk = stk->prev)\n \t\trem = fill(path, pathlen, basename_offset, stk, rem);\n+\n+\tif (collect_all) {\n+\t\tempty_attr_check_elems(check);\n+\t\tfor (i = 0; i < attr_nr; i++) {\n+\t\t\tconst struct git_attr *attr = check_all_attr[i].attr;\n+\t\t\tconst char *value = check_all_attr[i].value;\n+\t\t\tif (value == ATTR__UNSET || value == ATTR__UNKNOWN)\n+\t\t\t\tcontinue;\n+\t\t\tgit_attr_check_append(check, attr)->value = value;\n+\t\t}\n+\t}\n }\n \n static int git_check_attrs(const char *path, int pathlen,\n@@ -811,7 +824,7 @@ static int git_check_attrs(const char *path, int pathlen,\n \tint i;\n \tstruct git_attr_check_elem *celem = check->check;\n \n-\tcollect_some_attrs(path, pathlen, check);\n+\tcollect_some_attrs(path, pathlen, check, 0);\n \n \tfor (i = 0; i < check->check_nr; i++) {\n \t\tconst char *value = check_all_attr[celem[i].attr->attr_nr].value;\n@@ -825,19 +838,7 @@ static int git_check_attrs(const char *path, int pathlen,\n \n void git_all_attrs(const char *path, struct git_attr_check *check)\n {\n-\tint i;\n-\n-\tgit_attr_check_clear(check);\n-\tcollect_some_attrs(path, strlen(path), NULL);\n-\n-\tfor (i = 0; i < attr_nr; i++) {\n-\t\tconst char *name = check_all_attr[i].attr->name;\n-\t\tconst char *value = check_all_attr[i].value;\n-\t\tif (value == ATTR__UNSET || value == ATTR__UNKNOWN)\n-\t\t\tcontinue;\n-\t\tgit_attr_check_append(check, git_attr(name));\n-\t\tcheck->check[check->check_nr - 1].value = value;\n-\t}\n+\tcollect_some_attrs(path, strlen(path), check, 1);\n }\n \n void git_attr_set_direction(enum git_attr_direction new, struct index_state *istate)\n-- \n2.10.1.382.ga23ca1b.dirty\n\n"},{"id":"303847","messageId":"20161011002115.23312-23-sbeller@google.com","threadId":"44273","inReplyTo":"20161011002115.23312-1-sbeller@google.com","subject":"[PATCH 22/28] attr.c: correct ugly hack for git_all_attrs()","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-10-11T00:21:09Z","receivedAt":"2016-10-11T00:22:32Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"From: Junio C Hamano <gitster@pobox.com>\n\nThe collect_some_attrs() function has an ugly hack since\n\n06a604e6 (attr: avoid heavy work when we know the specified attr is\nnot defined, 2014-12-28) added an optimization that relies on the\nfact that the caller knows what attributes it is interested in, so\nthat we can leave once we know the final answer for all the\nattributes the caller asked.\n\ngit_all_attrs() that asks \"what attributes are on this path?\"\nhowever does not know what attributes it is interested in, other\nthan the vague \"we are interested in all of them\", which is not a\nvery useful thing to say.  As a way to disable this optimization\nfor this caller, the said commit added a code to skip it when\nthe caller passes a NULL for the check structure.\n\nHowever, it skipped the optimization not when check is NULL, but\nwhen the number of attributes being checked is 0, which is\nunnecessarily pessimistic.\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Stefan Beller <sbeller@google.com>\n---\n attr.c | 24 ++++++++----------------\n 1 file changed, 8 insertions(+), 16 deletions(-)\n\ndiff --git a/attr.c b/attr.c\nindex abf23d8..8d8df20 100644\n--- a/attr.c\n+++ b/attr.c\n@@ -748,8 +748,8 @@ static int attr_check_is_dynamic(const struct git_attr_check *check)\n \n /*\n  * Collect attributes for path into the array pointed to by\n- * check_all_attr. If num is non-zero, only attributes in check[] are\n- * collected. Otherwise all attributes are collected.\n+ * check_all_attr.  If check is not NULL, only attributes in\n+ * check[] are collected. Otherwise all attributes are collected.\n  */\n static void collect_some_attrs(const char *path, int pathlen,\n \t\t\t       struct git_attr_check *check)\n@@ -759,17 +759,6 @@ static void collect_some_attrs(const char *path, int pathlen,\n \tint i, rem, dirlen;\n \tconst char *cp, *last_slash = NULL;\n \tint basename_offset;\n-\tint num;\n-\tstruct git_attr_check_elem *celem;\n-\n-\tif (!check) {\n-\t\t/* Yuck - ugly git_all_attrs() hack! */\n-\t\tcelem = NULL;\n-\t\tnum = 0;\n-\t} else {\n-\t\tcelem = check->check;\n-\t\tnum = check->check_nr;\n-\t}\n \n \tfor (cp = path; cp < path + pathlen; cp++) {\n \t\tif (*cp == '/' && cp[1])\n@@ -786,9 +775,12 @@ static void collect_some_attrs(const char *path, int pathlen,\n \tprepare_attr_stack(path, dirlen);\n \tfor (i = 0; i < attr_nr; i++)\n \t\tcheck_all_attr[i].value = ATTR__UNKNOWN;\n-\tif (num && !cannot_trust_maybe_real) {\n+\n+\tif (check && !cannot_trust_maybe_real) {\n+\t\tstruct git_attr_check_elem *celem = check->check;\n+\n \t\trem = 0;\n-\t\tfor (i = 0; i < num; i++) {\n+\t\tfor (i = 0; i < check->check_nr; i++) {\n \t\t\tif (!celem[i].attr->maybe_real) {\n \t\t\t\tstruct git_attr_check_elem *c;\n \t\t\t\tc = check_all_attr + celem[i].attr->attr_nr;\n@@ -796,7 +788,7 @@ static void collect_some_attrs(const char *path, int pathlen,\n \t\t\t\trem++;\n \t\t\t}\n \t\t}\n-\t\tif (rem == num)\n+\t\tif (rem == check->check_nr)\n \t\t\treturn;\n \t}\n \n-- \n2.10.1.382.ga23ca1b.dirty\n\n"},{"id":"303848","messageId":"20161011002115.23312-12-sbeller@google.com","threadId":"44273","inReplyTo":"20161011002115.23312-1-sbeller@google.com","subject":"[PATCH 11/28] attr: (re)introduce git_check_attr() and struct git_attr_check","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-10-11T00:20:58Z","receivedAt":"2016-10-11T00:22:35Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"From: Junio C Hamano <gitster@pobox.com>\n\nA common pattern to check N attributes for many paths is to\n\n (1) prepare an array A of N git_attr_check_elem items;\n (2) call git_attr() to intern the N attribute names and fill A;\n (3) repeatedly call git_check_attrs() for path with N and A;\n\nA look-up for these N attributes for a single path P scans the\nentire attr_stack, starting from the .git/info/attributes file and\nthen .gitattributes file in the directory the path P is in, going\nupwards to find .gitattributes file found in parent directories.\n\nAn earlier commit 06a604e6 (attr: avoid heavy work when we know the\nspecified attr is not defined, 2014-12-28) tried to optimize out\nthis scanning for one trivial special case: when the attribute being\nsought is known not to exist, we do not have to scan for it.  While\nthis may be a cheap and effective heuristic, it would not work well\nwhen N is (much) more than 1.\n\nWhat we would want is a more customized way to skip irrelevant\nentries in the attribute stack, and the definition of irrelevance\nis tied to the set of attributes passed to git_check_attrs() call,\ni.e. the set of attributes being sought.  The data necessary for\nthis optimization needs to live alongside the set of attributes, but\na simple array of git_attr_check_elem simply does not have any place\nfor that.\n\nIntroduce \"struct git_attr_check\" that contains N, the number of\nattributes being sought, and A, the array that holds N\ngit_attr_check_elem items, and a function git_check_attr() that\ntakes a path P and this structure as its parameters.  This structure\ncan later be extended to hold extra data necessary for optimization.\n\nAlso, to make it easier to write the first two steps in common\ncases, introduce git_attr_check_initl() helper function, which takes\na NULL-terminated list of attribute names and initialize this\nstructure.\n\nAs an illustration of this new API, convert archive.c that asks for\nexport-subst and export-ignore attributes for each paths.\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Stefan Beller <sbeller@google.com>\n---\n archive.c | 24 ++++++------------------\n attr.c    | 34 ++++++++++++++++++++++++++++++++++\n attr.h    |  9 +++++++++\n 3 files changed, 49 insertions(+), 18 deletions(-)\n\ndiff --git a/archive.c b/archive.c\nindex 2dc8d6c..11e3951 100644\n--- a/archive.c\n+++ b/archive.c\n@@ -87,19 +87,6 @@ void *sha1_file_to_archive(const struct archiver_args *args,\n \treturn buffer;\n }\n \n-static void setup_archive_check(struct git_attr_check_elem *check)\n-{\n-\tstatic struct git_attr *attr_export_ignore;\n-\tstatic struct git_attr *attr_export_subst;\n-\n-\tif (!attr_export_ignore) {\n-\t\tattr_export_ignore = git_attr(\"export-ignore\");\n-\t\tattr_export_subst = git_attr(\"export-subst\");\n-\t}\n-\tcheck[0].attr = attr_export_ignore;\n-\tcheck[1].attr = attr_export_subst;\n-}\n-\n struct directory {\n \tstruct directory *up;\n \tstruct object_id oid;\n@@ -123,7 +110,7 @@ static int write_archive_entry(const unsigned char *sha1, const char *base,\n \tstruct archiver_context *c = context;\n \tstruct archiver_args *args = c->args;\n \twrite_archive_entry_fn_t write_entry = c->write_entry;\n-\tstruct git_attr_check_elem check[2];\n+\tstatic struct git_attr_check *check;\n \tconst char *path_without_prefix;\n \tint err;\n \n@@ -137,11 +124,12 @@ static int write_archive_entry(const unsigned char *sha1, const char *base,\n \t\tstrbuf_addch(&path, '/');\n \tpath_without_prefix = path.buf + args->baselen;\n \n-\tsetup_archive_check(check);\n-\tif (!git_check_attrs(path_without_prefix, ARRAY_SIZE(check), check)) {\n-\t\tif (ATTR_TRUE(check[0].value))\n+\tif (!check)\n+\t\tcheck = git_attr_check_initl(\"export-ignore\", \"export-subst\", NULL);\n+\tif (!git_check_attr(path_without_prefix, check)) {\n+\t\tif (ATTR_TRUE(check->check[0].value))\n \t\t\treturn 0;\n-\t\targs->convert = ATTR_TRUE(check[1].value);\n+\t\targs->convert = ATTR_TRUE(check->check[1].value);\n \t}\n \n \tif (S_ISDIR(mode) || S_ISGITLINK(mode)) {\ndiff --git a/attr.c b/attr.c\nindex c99e23a..861e1a2 100644\n--- a/attr.c\n+++ b/attr.c\n@@ -829,3 +829,37 @@ void git_attr_set_direction(enum git_attr_direction new, struct index_state *ist\n \t\tdrop_attr_stack();\n \tuse_index = istate;\n }\n+\n+int git_check_attr(const char *path, struct git_attr_check *check)\n+{\n+\treturn git_check_attrs(path, check->check_nr, check->check);\n+}\n+\n+struct git_attr_check *git_attr_check_initl(const char *one, ...)\n+{\n+\tstruct git_attr_check *check;\n+\tint cnt;\n+\tva_list params;\n+\tconst char *param;\n+\n+\tva_start(params, one);\n+\tfor (cnt = 1; (param = va_arg(params, const char *)) != NULL; cnt++)\n+\t\t;\n+\tva_end(params);\n+\tcheck = xcalloc(1,\n+\t\t\tsizeof(*check) + cnt * sizeof(*(check->check)));\n+\tcheck->check_nr = cnt;\n+\tcheck->check = (struct git_attr_check_elem *)(check + 1);\n+\n+\tcheck->check[0].attr = git_attr(one);\n+\tva_start(params, one);\n+\tfor (cnt = 1; cnt < check->check_nr; cnt++) {\n+\t\tparam = va_arg(params, const char *);\n+\t\tif (!param)\n+\t\t\tdie(\"BUG: counted %d != ended at %d\",\n+\t\t\t    check->check_nr, cnt);\n+\t\tcheck->check[cnt].attr = git_attr(param);\n+\t}\n+\tva_end(params);\n+\treturn check;\n+}\ndiff --git a/attr.h b/attr.h\nindex dd3c4a3..3fd8690 100644\n--- a/attr.h\n+++ b/attr.h\n@@ -29,6 +29,15 @@ struct git_attr_check_elem {\n \tconst char *value;\n };\n \n+struct git_attr_check {\n+\tint check_nr;\n+\tint check_alloc;\n+\tstruct git_attr_check_elem *check;\n+};\n+\n+extern struct git_attr_check *git_attr_check_initl(const char *, ...);\n+extern int git_check_attr(const char *path, struct git_attr_check *);\n+\n /*\n  * Return the name of the attribute represented by the argument.  The\n  * return value is a pointer to a null-delimited string that is part\n-- \n2.10.1.382.ga23ca1b.dirty\n\n"},{"id":"303849","messageId":"20161011002115.23312-13-sbeller@google.com","threadId":"44273","inReplyTo":"20161011002115.23312-1-sbeller@google.com","subject":"[PATCH 12/28] attr: convert git_all_attrs() to use \"struct git_attr_check\"","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-10-11T00:20:59Z","receivedAt":"2016-10-11T00:22:39Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"From: Junio C Hamano <gitster@pobox.com>\n\nThis updates the other two ways the attribute check is done via an\narray of \"struct git_attr_check_elem\" elements.  These two niches\nappear only in \"git check-attr\".\n\n * The caller does not know offhand what attributes it wants to ask\n   about and cannot use git_attr_check_initl() to prepare the\n   git_attr_check structure.\n\n * The caller may not know what attributes it wants to ask at all,\n   and instead wants to learn everything that the given path has.\n\nSuch a caller can call git_attr_check_alloc() to allocate an empty\ngit_attr_check, and then call git_attr_check_append() to add\nattribute names one by one.  A new attribute can be appended until\ngit_attr_check structure is \"finalized\", which happens when it is\nused to ask for attributes for any path by calling git_check_attr()\nor git_all_attrs().  A git_attr_check structure that is initialized\nby git_attr_check_initl() is already finalized when it is returned.\n\nI am not at all happy with the way git_all_attrs() API turned out to\nbe, but it is only to support one niche caller (\"check-attr --all\"),\nso I'll stop here for now.\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Stefan Beller <sbeller@google.com>\n---\n attr.c               | 75 ++++++++++++++++++++++++++++++++++++++--------------\n attr.h               | 16 ++++++-----\n builtin/check-attr.c | 51 ++++++++++++++++++-----------------\n 3 files changed, 90 insertions(+), 52 deletions(-)\n\ndiff --git a/attr.c b/attr.c\nindex 861e1a2..76f0d6b 100644\n--- a/attr.c\n+++ b/attr.c\n@@ -724,6 +724,11 @@ static int macroexpand_one(int nr, int rem)\n \treturn rem;\n }\n \n+static int attr_check_is_dynamic(const struct git_attr_check *check)\n+{\n+\treturn (void *)(check->check) != (void *)(check + 1);\n+}\n+\n /*\n  * Collect attributes for path into the array pointed to by\n  * check_all_attr. If num is non-zero, only attributes in check[] are\n@@ -789,32 +794,21 @@ int git_check_attrs(const char *path, int num, struct git_attr_check_elem *check\n \treturn 0;\n }\n \n-int git_all_attrs(const char *path, int *num, struct git_attr_check_elem **check)\n+void git_all_attrs(const char *path, struct git_attr_check *check)\n {\n-\tint i, count, j;\n+\tint i;\n \n+\tgit_attr_check_clear(check);\n \tcollect_some_attrs(path, 0, NULL);\n \n-\t/* Count the number of attributes that are set. */\n-\tcount = 0;\n-\tfor (i = 0; i < attr_nr; i++) {\n-\t\tconst char *value = check_all_attr[i].value;\n-\t\tif (value != ATTR__UNSET && value != ATTR__UNKNOWN)\n-\t\t\t++count;\n-\t}\n-\t*num = count;\n-\tALLOC_ARRAY(*check, count);\n-\tj = 0;\n \tfor (i = 0; i < attr_nr; i++) {\n+\t\tconst char *name = check_all_attr[i].attr->name;\n \t\tconst char *value = check_all_attr[i].value;\n-\t\tif (value != ATTR__UNSET && value != ATTR__UNKNOWN) {\n-\t\t\t(*check)[j].attr = check_all_attr[i].attr;\n-\t\t\t(*check)[j].value = value;\n-\t\t\t++j;\n-\t\t}\n+\t\tif (value == ATTR__UNSET || value == ATTR__UNKNOWN)\n+\t\t\tcontinue;\n+\t\tgit_attr_check_append(check, git_attr(name));\n+\t\tcheck->check[check->check_nr - 1].value = value;\n \t}\n-\n-\treturn 0;\n }\n \n void git_attr_set_direction(enum git_attr_direction new, struct index_state *istate)\n@@ -832,6 +826,7 @@ void git_attr_set_direction(enum git_attr_direction new, struct index_state *ist\n \n int git_check_attr(const char *path, struct git_attr_check *check)\n {\n+\tcheck->finalized = 1;\n \treturn git_check_attrs(path, check->check_nr, check->check);\n }\n \n@@ -849,17 +844,57 @@ struct git_attr_check *git_attr_check_initl(const char *one, ...)\n \tcheck = xcalloc(1,\n \t\t\tsizeof(*check) + cnt * sizeof(*(check->check)));\n \tcheck->check_nr = cnt;\n+\tcheck->finalized = 1;\n \tcheck->check = (struct git_attr_check_elem *)(check + 1);\n \n \tcheck->check[0].attr = git_attr(one);\n \tva_start(params, one);\n \tfor (cnt = 1; cnt < check->check_nr; cnt++) {\n+\t\tstruct git_attr *attr;\n \t\tparam = va_arg(params, const char *);\n \t\tif (!param)\n \t\t\tdie(\"BUG: counted %d != ended at %d\",\n \t\t\t    check->check_nr, cnt);\n-\t\tcheck->check[cnt].attr = git_attr(param);\n+\t\tattr = git_attr(param);\n+\t\tif (!attr)\n+\t\t\tdie(\"BUG: %s: not a valid attribute name\", param);\n+\t\tcheck->check[cnt].attr = attr;\n \t}\n \tva_end(params);\n \treturn check;\n }\n+\n+struct git_attr_check *git_attr_check_alloc(void)\n+{\n+\treturn xcalloc(1, sizeof(struct git_attr_check));\n+}\n+\n+struct git_attr_check_elem *git_attr_check_append(struct git_attr_check *check,\n+\t\t\t\t\t\t  const struct git_attr *attr)\n+{\n+\tstruct git_attr_check_elem *elem;\n+\tif (check->finalized)\n+\t\tdie(\"BUG: append after git_attr_check structure is finalized\");\n+\tif (!attr_check_is_dynamic(check))\n+\t\tdie(\"BUG: appending to a statically initialized git_attr_check\");\n+\tALLOC_GROW(check->check, check->check_nr + 1, check->check_alloc);\n+\telem = &check->check[check->check_nr++];\n+\telem->attr = attr;\n+\treturn elem;\n+}\n+\n+void git_attr_check_clear(struct git_attr_check *check)\n+{\n+\tif (!attr_check_is_dynamic(check))\n+\t\tdie(\"BUG: clearing a statically initialized git_attr_check\");\n+\tfree(check->check);\n+\tcheck->check_nr = 0;\n+\tcheck->check_alloc = 0;\n+\tcheck->finalized = 0;\n+}\n+\n+void git_attr_check_free(struct git_attr_check *check)\n+{\n+\tgit_attr_check_clear(check);\n+\tfree(check);\n+}\ndiff --git a/attr.h b/attr.h\nindex 3fd8690..0d94077 100644\n--- a/attr.h\n+++ b/attr.h\n@@ -30,6 +30,7 @@ struct git_attr_check_elem {\n };\n \n struct git_attr_check {\n+\tint finalized;\n \tint check_nr;\n \tint check_alloc;\n \tstruct git_attr_check_elem *check;\n@@ -38,6 +39,12 @@ struct git_attr_check {\n extern struct git_attr_check *git_attr_check_initl(const char *, ...);\n extern int git_check_attr(const char *path, struct git_attr_check *);\n \n+extern struct git_attr_check *git_attr_check_alloc(void);\n+extern struct git_attr_check_elem *git_attr_check_append(struct git_attr_check *, const struct git_attr *);\n+\n+extern void git_attr_check_clear(struct git_attr_check *);\n+extern void git_attr_check_free(struct git_attr_check *);\n+\n /*\n  * Return the name of the attribute represented by the argument.  The\n  * return value is a pointer to a null-delimited string that is part\n@@ -48,13 +55,10 @@ extern const char *git_attr_name(const struct git_attr *);\n int git_check_attrs(const char *path, int, struct git_attr_check_elem *);\n \n /*\n- * Retrieve all attributes that apply to the specified path.  *num\n- * will be set to the number of attributes on the path; **check will\n- * be set to point at a newly-allocated array of git_attr_check\n- * objects describing the attributes and their values.  *check must be\n- * free()ed by the caller.\n+ * Retrieve all attributes that apply to the specified path.\n+ * check holds the attributes and their values.\n  */\n-int git_all_attrs(const char *path, int *num, struct git_attr_check_elem **check);\n+void git_all_attrs(const char *path, struct git_attr_check *check);\n \n enum git_attr_direction {\n \tGIT_ATTR_CHECKIN,\ndiff --git a/builtin/check-attr.c b/builtin/check-attr.c\nindex 97e3837..ec61476 100644\n--- a/builtin/check-attr.c\n+++ b/builtin/check-attr.c\n@@ -24,12 +24,13 @@ static const struct option check_attr_options[] = {\n \tOPT_END()\n };\n \n-static void output_attr(int cnt, struct git_attr_check_elem *check,\n-\t\t\tconst char *file)\n+static void output_attr(struct git_attr_check *check, const char *file)\n {\n \tint j;\n+\tint cnt = check->check_nr;\n+\n \tfor (j = 0; j < cnt; j++) {\n-\t\tconst char *value = check[j].value;\n+\t\tconst char *value = check->check[j].value;\n \n \t\tif (ATTR_TRUE(value))\n \t\t\tvalue = \"set\";\n@@ -42,36 +43,37 @@ static void output_attr(int cnt, struct git_attr_check_elem *check,\n \t\t\tprintf(\"%s%c\" /* path */\n \t\t\t       \"%s%c\" /* attrname */\n \t\t\t       \"%s%c\" /* attrvalue */,\n-\t\t\t       file, 0, git_attr_name(check[j].attr), 0, value, 0);\n+\t\t\t       file, 0,\n+\t\t\t       git_attr_name(check->check[j].attr), 0, value, 0);\n \t\t} else {\n \t\t\tquote_c_style(file, NULL, stdout, 0);\n-\t\t\tprintf(\": %s: %s\\n\", git_attr_name(check[j].attr), value);\n+\t\t\tprintf(\": %s: %s\\n\",\n+\t\t\t       git_attr_name(check->check[j].attr), value);\n \t\t}\n-\n \t}\n }\n \n static void check_attr(const char *prefix,\n-\t\t       int cnt, struct git_attr_check_elem *check,\n+\t\t       struct git_attr_check *check,\n \t\t       const char *file)\n {\n \tchar *full_path =\n \t\tprefix_path(prefix, prefix ? strlen(prefix) : 0, file);\n \tif (check != NULL) {\n-\t\tif (git_check_attrs(full_path, cnt, check))\n-\t\t\tdie(\"git_check_attrs died\");\n-\t\toutput_attr(cnt, check, file);\n+\t\tif (git_check_attr(full_path, check))\n+\t\t\tdie(\"git_check_attr died\");\n+\t\toutput_attr(check, file);\n \t} else {\n-\t\tif (git_all_attrs(full_path, &cnt, &check))\n-\t\t\tdie(\"git_all_attrs died\");\n-\t\toutput_attr(cnt, check, file);\n-\t\tfree(check);\n+\t\tcheck = git_attr_check_alloc();\n+\t\tgit_all_attrs(full_path, check);\n+\t\toutput_attr(check, file);\n+\t\tgit_attr_check_free(check);\n \t}\n \tfree(full_path);\n }\n \n static void check_attr_stdin_paths(const char *prefix,\n-\t\t\t\t   int cnt, struct git_attr_check_elem *check)\n+\t\t\t\t   struct git_attr_check *check)\n {\n \tstruct strbuf buf = STRBUF_INIT;\n \tstruct strbuf unquoted = STRBUF_INIT;\n@@ -85,7 +87,7 @@ static void check_attr_stdin_paths(const char *prefix,\n \t\t\t\tdie(\"line is badly quoted\");\n \t\t\tstrbuf_swap(&buf, &unquoted);\n \t\t}\n-\t\tcheck_attr(prefix, cnt, check, buf.buf);\n+\t\tcheck_attr(prefix, check, buf.buf);\n \t\tmaybe_flush_or_die(stdout, \"attribute to stdout\");\n \t}\n \tstrbuf_release(&buf);\n@@ -100,7 +102,7 @@ static NORETURN void error_with_usage(const char *msg)\n \n int cmd_check_attr(int argc, const char **argv, const char *prefix)\n {\n-\tstruct git_attr_check_elem *check;\n+\tstruct git_attr_check *check;\n \tint cnt, i, doubledash, filei;\n \n \tif (!is_bare_repository())\n@@ -163,24 +165,21 @@ int cmd_check_attr(int argc, const char **argv, const char *prefix)\n \tif (all_attrs) {\n \t\tcheck = NULL;\n \t} else {\n-\t\tcheck = xcalloc(cnt, sizeof(*check));\n+\t\tcheck = git_attr_check_alloc();\n \t\tfor (i = 0; i < cnt; i++) {\n-\t\t\tconst char *name;\n-\t\t\tstruct git_attr *a;\n-\t\t\tname = argv[i];\n-\t\t\ta = git_attr(name);\n+\t\t\tstruct git_attr *a = git_attr(argv[i]);\n \t\t\tif (!a)\n \t\t\t\treturn error(\"%s: not a valid attribute name\",\n-\t\t\t\t\tname);\n-\t\t\tcheck[i].attr = a;\n+\t\t\t\t\t     argv[i]);\n+\t\t\tgit_attr_check_append(check, a);\n \t\t}\n \t}\n \n \tif (stdin_paths)\n-\t\tcheck_attr_stdin_paths(prefix, cnt, check);\n+\t\tcheck_attr_stdin_paths(prefix, check);\n \telse {\n \t\tfor (i = filei; i < argc; i++)\n-\t\t\tcheck_attr(prefix, cnt, check, argv[i]);\n+\t\t\tcheck_attr(prefix, check, argv[i]);\n \t\tmaybe_flush_or_die(stdout, \"attribute to stdout\");\n \t}\n \treturn 0;\n-- \n2.10.1.382.ga23ca1b.dirty\n\n"},{"id":"303889","messageId":"20161011165936.GA9085@google.com","threadId":"44273","inReplyTo":"20161011002115.23312-12-sbeller@google.com","subject":"Re: [PATCH 11/28] attr: (re)introduce git_check_attr() and struct git_attr_check","fromName":"Brandon Williams","fromEmail":"bmwill@google.com","sentAt":"2016-10-11T16:59:36Z","receivedAt":"2016-10-11T17:06:59Z","isPatch":true,"sender":{"key":"bwilliams.eng@gmail.com","avatar":null},"body":"On 10/10, Stefan Beller wrote:\n> From: Junio C Hamano <gitster@pobox.com>\n> \n> A common pattern to check N attributes for many paths is to\n> \n>  (1) prepare an array A of N git_attr_check_elem items;\n>  (2) call git_attr() to intern the N attribute names and fill A;\n\nDoes the word 'intern' here mean internalize?  It took me a few reads to\nstop picturing college students running around an office :)\n\n-- \nBrandon Williams\n"},{"id":"303891","messageId":"20161011173749.GC9085@google.com","threadId":"44273","inReplyTo":"20161011002115.23312-27-sbeller@google.com","subject":"Re: [PATCH 26/28] attr: make git_attr_counted static","fromName":"Brandon Williams","fromEmail":"bmwill@google.com","sentAt":"2016-10-11T17:37:49Z","receivedAt":"2016-10-11T17:37:58Z","isPatch":true,"sender":{"key":"bwilliams.eng@gmail.com","avatar":null},"body":"On 10/10, Stefan Beller wrote:\n> It's not used outside the attr code, so let's keep it private.\n> \n> Change-Id: I0d15e0f2ea944b31d68b9cf1a4edecac0ca2170d\n\nLooks like you forgot to remove this :)\n\n-- \nBrandon Williams\n"},{"id":"303892","messageId":"xmqqinsyaikk.fsf@gitster.mtv.corp.google.com","threadId":"44273","inReplyTo":"20161011165936.GA9085@google.com","subject":"Re: [PATCH 11/28] attr: (re)introduce git_check_attr() and struct git_attr_check","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-10-11T17:42:19Z","receivedAt":"2016-10-11T17:44:53Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Brandon Williams <bmwill@google.com> writes:\n\n> On 10/10, Stefan Beller wrote:\n>> From: Junio C Hamano <gitster@pobox.com>\n>> \n>> A common pattern to check N attributes for many paths is to\n>> \n>>  (1) prepare an array A of N git_attr_check_elem items;\n>>  (2) call git_attr() to intern the N attribute names and fill A;\n>\n> Does the word 'intern' here mean internalize?  It took me a few reads to\n> stop picturing college students running around an office :)\n\nThe verb comes from Lisp world where you \"intern\" a string to make a\nsymbol.\n\n"},{"id":"303893","messageId":"20161011174506.GD9085@google.com","threadId":"44273","inReplyTo":"20161011002115.23312-29-sbeller@google.com","subject":"Re: [PATCH 28/28] attr: convert to new threadsafe API","fromName":"Brandon Williams","fromEmail":"bmwill@google.com","sentAt":"2016-10-11T17:45:06Z","receivedAt":"2016-10-11T17:55:49Z","isPatch":true,"sender":{"key":"bwilliams.eng@gmail.com","avatar":null},"body":"On 10/10, Stefan Beller wrote:\n>   be initalized once as that part will be read only after its\n       initialized\n>   initialisation, the answer may be different for each path.\nshould this be the US spelling 'initialization'?\n\n-- \nBrandon Williams\n"},{"id":"303896","messageId":"CAGZ79kaQKDdJfMOjDKEK_dZJhgj+R7rByQS++B3OOBy6uO1x2w@mail.gmail.com","threadId":"44273","inReplyTo":"xmqqmviaaina.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH 28/28] attr: convert to new threadsafe API","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-10-11T17:56:17Z","receivedAt":"2016-10-11T17:58:04Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"On Tue, Oct 11, 2016 at 10:40 AM, Junio C Hamano <gitster@pobox.com> wrote:\n> Stefan Beller <sbeller@google.com> writes:\n>\n>> This revamps the API of the attr subsystem to be thread safe.\n>> Before we had the question and its results in one struct type.\n>> The typical usage of the API was\n>>\n>>     static struct git_attr_check;\n>\n> I think you meant \"*check\" at the end, perhaps?\n>\n>>\n>>     if (!check)\n>>         check = git_attr_check_initl(\"text\", NULL);\n>>\n>>     git_check_attr(path, check);\n>>     act_on(check->value[0]);\n>>\n>> * While the check for attributes to be questioned only need to\n>>   be initalized once as that part will be read only after its\n>>   initialisation, the answer may be different for each path.\n>>   Because of that we need to decouple the check and the answer,\n>>   such that each thread can obtain an answer for the path it\n>>   is currently processing.\n>\n> Yes, it is good to separate questions and answers.  I think answers\n> should be owned by the caller, though.  I do not think of a good\n> reason why you want to make it impossible to do something like this:\n>\n>         struct git_attr_check_result *result_1 = ...allocate...;\n>         struct git_attr_check_result *result_2 = ...allocate...;\n>\n>         loop {\n>                 struct strbuf path = next_path();\n>                 git_check_attr(result_1, path.buf, check_1);\n>                 if (strbuf_strip_suffix(&path, \".c\")) {\n>                         strbuf_addstr(&path, \".h\");\n>                         git_check_attr(result_2, path.buf, check_2);\n>                         do something using result_1[] and result_2[];\n>                 } else {\n>                         do something using result_1[];\n>                 }\n>         }\n>\n> Do we already have a design of the \"result\" thing that is concrete\n> enough to require us to declare that the result is owned by the\n> implementation and asking another question has to destroy the answer\n> to the previous question?  Otherwise, I'd rather not to see us make\n> the API unnecessarily hard to use.  While I do want us to avoid\n> overengineering for excessive flexibility, I somehow feel \"you\n> cannot control the lifetime of the result, it is owned by the\n> subsystem\" falls the other side of the line.\n\nTrue, we had that issue for other APIs (IIRC path related things,\nwith 4 static buffers that round robin). I did not like that design decision,\nbut I felt it was okay, as the above did not occur to me.\n\nIn the case above, we could just copy the result_1->values and\nthen re-use the result_1, but I agree that this may be somewhat error prone\nif you're not familiar with the decisions in this series.\n\nSo in case of the caller owning the result, we could pull the static\ntrick for now\nand only use a different approach when we use it in actual threaded code, i.e.\nthe code in convert.c could become:\n\n    static struct git_attr_check *check;\n    static struct git_attr_result *result;\n\n    if (!check) {\n        check = git_attr_check_initl(\"crlf\", \"ident\",\n            \"filter\", \"eol\", \"text\", NULL);\n        result = git_attr_result_alloc(5);\n        user_convert_tail = &user_convert;\n        git_config(read_convert_config, NULL);\n    }\n\n    if (!git_check_attr(path, check, result)) {\n        ...\n\n>> +  empty `struct git_attr_check` as alloced by git_attr_check_alloc()\n>\n> \"allocated\", not \"alloced\".\n\nok.\n\n>\n>> +  can be prepared by calling `git_attr_check_alloc()` function and\n>> +  then attributes you want to ask about can be added to it with\n>> +  `git_attr_check_append()` function.\n>> +  git_attr_check_initl is thread safe, i.e. you can call it\n>\n> Spell it `git_attr_check_initl()` for consistency.\n\nok.\n\n>\n>> +  from different threads at the same time; internally however only one\n>> +  call at a time is processed. If the calls from different threads have\n>> +  the same arguments, the returned `git_attr_check` may be the same.\n>\n> I find this description a bit confusing.  At least the way I\n> envisioned was that when this piece of code is run by multiple\n> people at the same time,\n>\n>         static struct git_attr_check *check = NULL;\n>         git_attr_check_initl(&check, ...);\n>\n> we won't waste the \"check\" by allocated by the first one by\n> overwriting it with another one allocated by the second one.  So\n> \"the same arguments\" does not come into the picture.  A single\n> variable is either\n>\n>  * already allocated and initialised by the an earlier call to\n>    initl() by somebody else, or\n>\n>  * originally NULL when you call initl(), and the implementation\n>    makes sure that other people wait while you allocate, initialise\n>    and assign it to the variable, or\n>\n>  * originally NULL when you call initl(), but the implementation\n>    notices that somebody else is doing the second bullet point\n>    above, and you wait until that somebody else finishes and then\n>    you return without doing anything (because by that time, \"check\"\n>    is filled by that other party doing the second bullet point\n>    above).\n>\n> There is no need to check for \"the same arguments\".\n>\n\nI see. So we assume that there are no different arguments at the same time,\ni.e. all threads run the same code when it comes to attrs.\n\nBrandon wrote:\n> On 10/10, Stefan Beller wrote:\n>>   be initalized once as that part will be read only after its\n>>       initialized\n>>   initialisation, the answer may be different for each path.\n> should this be the US spelling 'initialization'?\n\nYes, we'd want to be consistent, indeed. Sometimes the British spelling\nslips through as that's what I learned in high school.\n\nSpecifically for initialise:\n\n$ git grep initialise\ncontrib/examples/git-notes.sh:                  die \"Will not\ninitialise with empty tree\"\nobject.h: * it can return \"yes we have, and here is a half-initialised object\"\nobject.h: * half-initialised objects, the caller is expected to initialize them\nrevision.c:static struct treesame_state *initialise_treesame(struct\nrev_info *revs, struct commit *commit)\nrevision.c:                             ts = initialise_treesame(revs, commit);\n+ a lot of french translations.\n\nThe American spelling is found a lot more.\n\nThanks,\nStefan\n"},{"id":"303897","messageId":"xmqqmviaaina.fsf@gitster.mtv.corp.google.com","threadId":"44273","inReplyTo":"20161011002115.23312-29-sbeller@google.com","subject":"Re: [PATCH 28/28] attr: convert to new threadsafe API","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-10-11T17:40:41Z","receivedAt":"2016-10-11T17:58:47Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Stefan Beller <sbeller@google.com> writes:\n\n> This revamps the API of the attr subsystem to be thread safe.\n> Before we had the question and its results in one struct type.\n> The typical usage of the API was\n>\n>     static struct git_attr_check;\n\nI think you meant \"*check\" at the end, perhaps?\n\n>\n>     if (!check)\n>         check = git_attr_check_initl(\"text\", NULL);\n>\n>     git_check_attr(path, check);\n>     act_on(check->value[0]);\n>\n> * While the check for attributes to be questioned only need to\n>   be initalized once as that part will be read only after its\n>   initialisation, the answer may be different for each path.\n>   Because of that we need to decouple the check and the answer,\n>   such that each thread can obtain an answer for the path it\n>   is currently processing.\n\nYes, it is good to separate questions and answers.  I think answers\nshould be owned by the caller, though.  I do not think of a good\nreason why you want to make it impossible to do something like this:\n\n\tstruct git_attr_check_result *result_1 = ...allocate...;\n\tstruct git_attr_check_result *result_2 = ...allocate...;\n\n\tloop {\n                struct strbuf path = next_path();\n                git_check_attr(result_1, path.buf, check_1);\n                if (strbuf_strip_suffix(&path, \".c\")) {\n                        strbuf_addstr(&path, \".h\");\n                        git_check_attr(result_2, path.buf, check_2);\n                        do something using result_1[] and result_2[];\n                } else {\n\t\t\tdo something using result_1[];\n\t\t}\n\t}\n\nDo we already have a design of the \"result\" thing that is concrete\nenough to require us to declare that the result is owned by the\nimplementation and asking another question has to destroy the answer\nto the previous question?  Otherwise, I'd rather not to see us make\nthe API unnecessarily hard to use.  While I do want us to avoid\noverengineering for excessive flexibility, I somehow feel \"you\ncannot control the lifetime of the result, it is owned by the\nsubsystem\" falls the other side of the line.\n\n> diff --git a/Documentation/technical/api-gitattributes.txt b/Documentation/technical/api-gitattributes.txt\n> index 92fc32a..2059aab 100644\n> --- a/Documentation/technical/api-gitattributes.txt\n> +++ b/Documentation/technical/api-gitattributes.txt\n> @@ -8,6 +8,18 @@ attributes to set of paths.\n>  Data Structure\n>  --------------\n>  \n> +extern struct git_attr *git_attr(const char *);\n> +\n> +/*\n> + * Return the name of the attribute represented by the argument.  The\n> + * return value is a pointer to a null-delimited string that is part\n> + * of the internal data structure; it should not be modified or freed.\n> + */\n> +extern const char *git_attr_name(const struct git_attr *);\n> +\n> +extern int attr_name_valid(const char *name, size_t namelen);\n> +extern void invalid_attr_name_message(struct strbuf *, const char *, int);\n> +\n>  `struct git_attr`::\n>  \n>  \tAn attribute is an opaque object that is identified by its name.\n> @@ -16,15 +28,17 @@ Data Structure\n>  \tof no interest to the calling programs.  The name of the\n>  \tattribute can be retrieved by calling `git_attr_name()`.\n>  \n> -`struct git_attr_check_elem`::\n> -\n> -\tThis structure represents one attribute and its value.\n> -\n>  `struct git_attr_check`::\n>  \n> -\tThis structure represents a collection of `git_attr_check_elem`.\n> +\tThis structure represents a collection of `struct git_attrs`.\n>  \tIt is passed to `git_check_attr()` function, specifying the\n> -\tattributes to check, and receives their values.\n> +\tattributes to check, and receives their values into a corresponding\n> +\t`struct git_attr_result`.\n> +\n> +`struct git_attr_result`::\n> +\n> +\tThis structure represents a collection of results to its\n> +\tcorresponding `struct git_attr_check`, that has the same order.\n>  \n>  \n>  Attribute Values\n> @@ -56,16 +70,22 @@ Querying Specific Attributes\n>  * Prepare `struct git_attr_check` using git_attr_check_initl()\n>    function, enumerating the names of attributes whose values you are\n>    interested in, terminated with a NULL pointer.  Alternatively, an\n> -  empty `struct git_attr_check` can be prepared by calling\n> -  `git_attr_check_alloc()` function and then attributes you want to\n> -  ask about can be added to it with `git_attr_check_append()`\n> -  function.\n> +  empty `struct git_attr_check` as alloced by git_attr_check_alloc()\n\n\"allocated\", not \"alloced\".\n\n> +  can be prepared by calling `git_attr_check_alloc()` function and\n> +  then attributes you want to ask about can be added to it with\n> +  `git_attr_check_append()` function.\n> +  git_attr_check_initl is thread safe, i.e. you can call it\n\nSpell it `git_attr_check_initl()` for consistency.\n\n> +  from different threads at the same time; internally however only one\n> +  call at a time is processed. If the calls from different threads have\n> +  the same arguments, the returned `git_attr_check` may be the same.\n\nI find this description a bit confusing.  At least the way I\nenvisioned was that when this piece of code is run by multiple\npeople at the same time,\n\n\tstatic struct git_attr_check *check = NULL;\n\tgit_attr_check_initl(&check, ...);\n\nwe won't waste the \"check\" by allocated by the first one by\noverwriting it with another one allocated by the second one.  So\n\"the same arguments\" does not come into the picture.  A single\nvariable is either \n\n * already allocated and initialised by the an earlier call to\n   initl() by somebody else, or\n\n * originally NULL when you call initl(), and the implementation\n   makes sure that other people wait while you allocate, initialise\n   and assign it to the variable, or\n\n * originally NULL when you call initl(), but the implementation\n   notices that somebody else is doing the second bullet point\n   above, and you wait until that somebody else finishes and then\n   you return without doing anything (because by that time, \"check\"\n   is filled by that other party doing the second bullet point\n   above).\n\nThere is no need to check for \"the same arguments\".\n\n"},{"id":"303898","messageId":"xmqqa8eaagoq.fsf@gitster.mtv.corp.google.com","threadId":"44273","inReplyTo":"CAGZ79kaQKDdJfMOjDKEK_dZJhgj+R7rByQS++B3OOBy6uO1x2w@mail.gmail.com","subject":"Re: [PATCH 28/28] attr: convert to new threadsafe API","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-10-11T18:23:01Z","receivedAt":"2016-10-11T18:23:11Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Stefan Beller <sbeller@google.com> writes:\n\n>> I find this description a bit confusing.  At least the way I\n>> envisioned was that when this piece of code is run by multiple\n>> people at the same time,\n>>\n>>         static struct git_attr_check *check = NULL;\n>>         git_attr_check_initl(&check, ...);\n>>\n>> we won't waste the \"check\" by allocated by the first one by\n>> overwriting it with another one allocated by the second one.  So\n>> \"the same arguments\" does not come into the picture.  A single\n>> variable is either\n>>\n>>  * already allocated and initialised by the an earlier call to\n>>    initl() by somebody else, or\n>>\n>>  * originally NULL when you call initl(), and the implementation\n>>    makes sure that other people wait while you allocate, initialise\n>>    and assign it to the variable, or\n>>\n>>  * originally NULL when you call initl(), but the implementation\n>>    notices that somebody else is doing the second bullet point\n>>    above, and you wait until that somebody else finishes and then\n>>    you return without doing anything (because by that time, \"check\"\n>>    is filled by that other party doing the second bullet point\n>>    above).\n>>\n>> There is no need to check for \"the same arguments\".\n>>\n>\n> I see. So we assume that there are no different arguments at the same time,\n> i.e. all threads run the same code when it comes to attrs.\n\nSorry, but I fail to see how you can jump to that conclusion.\nPuzzled.\n\nYou can have many different callsites (think: hits \"git grep\" finds)\nthat call git_attr_check_initl() and they all may be asking for\ndifferent set of attributes.  As long as they are using different\n\"check\" instance to receive these sets of attributes, they are OK.\n\nIt is insane to use the same \"check\" variable to receive sets of\nattributes for different attributes, be it from the same call or\ndifferent one, it is insane to do this:\n\n\tfunc(char *anotherattr)\n\t{\n\t\tstatic struct git_attr_check *check = NULL;\n\t\tgit_attr_check_initl(&check, \"one\", anotherattr, ...);\n\n\t\t... use \"check\" to ask question ...\n\t}\n\nThe whole point of having a static, initialize-once instance of\n\"check\" is so that initl() can do potentially heavy preparation just\nonce and keep reusing it.  Allowing a later caller of func() to pass\na value of anotherattr that is different from the one used in the\nfirst call that did cause initl() to allocate-initialise-assign to\n\"check\" is simply insane, even there is no threading issue.\n\nAnd in a threaded environment it is even worse; the first thread may\ncall initl() to get one set of attributes in \"check\" and it may be\nabout to ask the question, while the second call may call initl()\nand by your definition it will notice they have different sets of\nattributes and returns different \"check\"?  Either the earlier one is\nleaked, or it gets destroyed even though the first thread hasn't\nfinished with \"check\" it got.\n\nIt is perfectly OK to drop \"static\" from the above example code.\nThen it no longer is insane--it is perfectly normal code whose\ninefficiency cannot be avoided because it wants to do dynamic\nqueries.\n"},{"id":"303899","messageId":"20161011172805.GB9085@google.com","threadId":"44273","inReplyTo":"20161011002115.23312-18-sbeller@google.com","subject":"Re: [PATCH 17/28] attr: expose validity check for attribute names","fromName":"Brandon Williams","fromEmail":"bmwill@google.com","sentAt":"2016-10-11T17:28:05Z","receivedAt":"2016-10-11T18:24:06Z","isPatch":true,"sender":{"key":"bwilliams.eng@gmail.com","avatar":null},"body":"On 10/10, Stefan Beller wrote:\n> From: Junio C Hamano <gitster@pobox.com>\n> -static int invalid_attr_name(const char *name, int namelen)\n> +int attr_name_valid(const char *name, size_t namelen)\n>  {\n>  \t/*\n>  \t * Attribute name cannot begin with '-' and must consist of\n>  \t * characters from [-A-Za-z0-9_.].\n>  \t */\n>  \tif (namelen <= 0 || *name == '-')\n> -\t\treturn -1;\n> +\t\treturn 0;\n>  \twhile (namelen--) {\n>  \t\tchar ch = *name++;\n>  \t\tif (! (ch == '-' || ch == '.' || ch == '_' ||\n>  \t\t       ('0' <= ch && ch <= '9') ||\n>  \t\t       ('a' <= ch && ch <= 'z') ||\n>  \t\t       ('A' <= ch && ch <= 'Z')) )\n> -\t\t\treturn -1;\n> +\t\t\treturn 0;\n>  \t}\n> -\treturn 0;\n> +\treturn -1;\n> +}\n\nWhats the reason behind returning -1 for a valid attr name vs 1?\n\n-- \nBrandon Williams\n"},{"id":"303900","messageId":"CAGZ79kbqikTtUGxXFB3W9k1nL6ST2iwRRi5zEuL9BOpHxZWc8A@mail.gmail.com","threadId":"44273","inReplyTo":"20161011172805.GB9085@google.com","subject":"Re: [PATCH 17/28] attr: expose validity check for attribute names","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-10-11T18:28:07Z","receivedAt":"2016-10-11T18:28:13Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"On Tue, Oct 11, 2016 at 10:28 AM, Brandon Williams <bmwill@google.com> wrote:\n> On 10/10, Stefan Beller wrote:\n>> From: Junio C Hamano <gitster@pobox.com>\n>> -static int invalid_attr_name(const char *name, int namelen)\n>> +int attr_name_valid(const char *name, size_t namelen)\n>>  {\n>>       /*\n>>        * Attribute name cannot begin with '-' and must consist of\n>>        * characters from [-A-Za-z0-9_.].\n>>        */\n>>       if (namelen <= 0 || *name == '-')\n>> -             return -1;\n>> +             return 0;\n>>       while (namelen--) {\n>>               char ch = *name++;\n>>               if (! (ch == '-' || ch == '.' || ch == '_' ||\n>>                      ('0' <= ch && ch <= '9') ||\n>>                      ('a' <= ch && ch <= 'z') ||\n>>                      ('A' <= ch && ch <= 'Z')) )\n>> -                     return -1;\n>> +                     return 0;\n>>       }\n>> -     return 0;\n>> +     return -1;\n>> +}\n>\n> Whats the reason behind returning -1 for a valid attr name vs 1?\n>\n\nUsually we prefer negative values for errors, whereas 0,1,2...\nis used for actual return values.\n\nIf you're not really interested in the value, but rather \"does it work at all?\"\nyou can use it via\n\n    if (function() < 0)\n        die_errno(\"function has error\");\n\nOtherwise you'd do a\n\n    int value = function();\n    if (value < 0)\n        die(...);\n\n    switch(value) {\n    default:\n    case 0:\n        doZeroThing(); break;\n    case 1:\n        doOtherThing();\n    }\n\nAt first I thought the two different return path for -1 are different\nerror classes (one being just a minor error compared to the other),\nbut they are not, so having the same code there makes sense.\n\nSo I think we could change it to +1 in this instance, as a non zero\nvalue would just indicate the attr name is not valid, but not necessarily\nan error.\n"},{"id":"303901","messageId":"CAGZ79kaM_5MGwPPAhQKWgCGirbHvioBY7_PtkKxza8qV6ZyRnw@mail.gmail.com","threadId":"44273","inReplyTo":"20161011184010.GA16014@google.com","subject":"Re: [PATCH 17/28] attr: expose validity check for attribute names","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-10-11T18:44:57Z","receivedAt":"2016-10-11T18:45:08Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"On Tue, Oct 11, 2016 at 11:40 AM, Brandon Williams <bmwill@google.com> wrote:\n\n> Wouldn't a +1 indicate that the attr name is valid while the 0\n> indicates that it is invalid?\n\nRight.\n\n>  It looks to me like we are checking each\n> character and if we run into one that doesn't work then we have an error\n> and return 0 indicating that the attr name we are checking is invalid,\n> otherwise the name is valid and we would want to return a +1 indicating\n> a success.\n>\n> Am I understanding the intent of this function properly?\n>\n\nI will change it to +1; I was just rambling about when you may\nexpect -1 in this project. :)\n"},{"id":"303903","messageId":"20161011184010.GA16014@google.com","threadId":"44273","inReplyTo":"CAGZ79kbqikTtUGxXFB3W9k1nL6ST2iwRRi5zEuL9BOpHxZWc8A@mail.gmail.com","subject":"Re: [PATCH 17/28] attr: expose validity check for attribute names","fromName":"Brandon Williams","fromEmail":"bmwill@google.com","sentAt":"2016-10-11T18:40:10Z","receivedAt":"2016-10-11T18:49:58Z","isPatch":true,"sender":{"key":"bwilliams.eng@gmail.com","avatar":null},"body":"On 10/11, Stefan Beller wrote:\n> On Tue, Oct 11, 2016 at 10:28 AM, Brandon Williams <bmwill@google.com> wrote:\n> > On 10/10, Stefan Beller wrote:\n> >> From: Junio C Hamano <gitster@pobox.com>\n> >> -static int invalid_attr_name(const char *name, int namelen)\n> >> +int attr_name_valid(const char *name, size_t namelen)\n> >>  {\n> >>       /*\n> >>        * Attribute name cannot begin with '-' and must consist of\n> >>        * characters from [-A-Za-z0-9_.].\n> >>        */\n> >>       if (namelen <= 0 || *name == '-')\n> >> -             return -1;\n> >> +             return 0;\n> >>       while (namelen--) {\n> >>               char ch = *name++;\n> >>               if (! (ch == '-' || ch == '.' || ch == '_' ||\n> >>                      ('0' <= ch && ch <= '9') ||\n> >>                      ('a' <= ch && ch <= 'z') ||\n> >>                      ('A' <= ch && ch <= 'Z')) )\n> >> -                     return -1;\n> >> +                     return 0;\n> >>       }\n> >> -     return 0;\n> >> +     return -1;\n> >> +}\n> >\n> > Whats the reason behind returning -1 for a valid attr name vs 1?\n> >\n> At first I thought the two different return path for -1 are different\n> error classes (one being just a minor error compared to the other),\n> but they are not, so having the same code there makes sense.\n> \n> So I think we could change it to +1 in this instance, as a non zero\n> value would just indicate the attr name is not valid, but not necessarily\n> an error.\n\nWouldn't a +1 indicate that the attr name is valid while the 0\nindicates that it is invalid? It looks to me like we are checking each\ncharacter and if we run into one that doesn't work then we have an error\nand return 0 indicating that the attr name we are checking is invalid,\notherwise the name is valid and we would want to return a +1 indicating\na success.\n\nAm I understanding the intent of this function properly?\n\n-- \nBrandon Williams\n"},{"id":"303904","messageId":"20161011184943.GB16014@google.com","threadId":"44273","inReplyTo":"CAGZ79kaM_5MGwPPAhQKWgCGirbHvioBY7_PtkKxza8qV6ZyRnw@mail.gmail.com","subject":"Re: [PATCH 17/28] attr: expose validity check for attribute names","fromName":"Brandon Williams","fromEmail":"bmwill@google.com","sentAt":"2016-10-11T18:49:43Z","receivedAt":"2016-10-11T18:50:53Z","isPatch":true,"sender":{"key":"bwilliams.eng@gmail.com","avatar":null},"body":"On 10/11, Stefan Beller wrote:\n> On Tue, Oct 11, 2016 at 11:40 AM, Brandon Williams <bmwill@google.com> wrote:\n> \n> > Wouldn't a +1 indicate that the attr name is valid while the 0\n> > indicates that it is invalid?\n> \n> Right.\n> \n> >  It looks to me like we are checking each\n> > character and if we run into one that doesn't work then we have an error\n> > and return 0 indicating that the attr name we are checking is invalid,\n> > otherwise the name is valid and we would want to return a +1 indicating\n> > a success.\n> >\n> > Am I understanding the intent of this function properly?\n> >\n> \n> I will change it to +1; I was just rambling about when you may\n> expect -1 in this project. :)\n\nAh got it :D\n\n-- \nBrandon Williams\n"},{"id":"303905","messageId":"CAGZ79kbdvs88Gt_XW801DPs7rj8kvcfPqCV7kjL_Jz9XiVn7fg@mail.gmail.com","threadId":"44273","inReplyTo":"xmqqa8eaagoq.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH 28/28] attr: convert to new threadsafe API","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-10-11T18:56:04Z","receivedAt":"2016-10-11T18:56:17Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"On Tue, Oct 11, 2016 at 11:23 AM, Junio C Hamano <gitster@pobox.com> wrote:\n> Stefan Beller <sbeller@google.com> writes:\n>\n>>> I find this description a bit confusing.  At least the way I\n>>> envisioned was that when this piece of code is run by multiple\n>>> people at the same time,\n>>>\n>>>         static struct git_attr_check *check = NULL;\n>>>         git_attr_check_initl(&check, ...);\n>>>\n>>> we won't waste the \"check\" by allocated by the first one by\n>>> overwriting it with another one allocated by the second one.  So\n>>> \"the same arguments\" does not come into the picture.  A single\n>>> variable is either\n>>>\n>>>  * already allocated and initialised by the an earlier call to\n>>>    initl() by somebody else, or\n>>>\n>>>  * originally NULL when you call initl(), and the implementation\n>>>    makes sure that other people wait while you allocate, initialise\n>>>    and assign it to the variable, or\n>>>\n>>>  * originally NULL when you call initl(), but the implementation\n>>>    notices that somebody else is doing the second bullet point\n>>>    above, and you wait until that somebody else finishes and then\n>>>    you return without doing anything (because by that time, \"check\"\n>>>    is filled by that other party doing the second bullet point\n>>>    above).\n>>>\n>>> There is no need to check for \"the same arguments\".\n>>>\n>>\n>> I see. So we assume that there are no different arguments at the same time,\n>> i.e. all threads run the same code when it comes to attrs.\n>\n> Sorry, but I fail to see how you can jump to that conclusion.\n> Puzzled.\n>\n> You can have many different callsites (think: hits \"git grep\" finds)\n> that call git_attr_check_initl() and they all may be asking for\n> different set of attributes.  As long as they are using different\n> \"check\" instance to receive these sets of attributes, they are OK.\n\nRight, but that requires a mutex per callsite; up to now I imagined\na global mutex only, which is how I came to the conclusion.\n\n>\n> It is insane to use the same \"check\" variable to receive sets of\n> attributes for different attributes,\n\nI agree.\n\n> be it from the same call or\n> different one, it is insane to do this:\n>\n>         func(char *anotherattr)\n>         {\n>                 static struct git_attr_check *check = NULL;\n>                 git_attr_check_initl(&check, \"one\", anotherattr, ...);\n>\n>                 ... use \"check\" to ask question ...\n>         }\n>\n> The whole point of having a static, initialize-once instance of\n> \"check\" is so that initl() can do potentially heavy preparation just\n> once and keep reusing it.  Allowing a later caller of func() to pass\n> a value of anotherattr that is different from the one used in the\n> first call that did cause initl() to allocate-initialise-assign to\n> \"check\" is simply insane, even there is no threading issue.\n\nI was imagining a file.c like that:\n\nstatic struct git_attr_check *check = NULL;\n\nvoid one_func()\n{\n    git_attr_check_initl(&check, \"one\", ...);\n    ...\n}\n\nvoid two_func()\n{\n    git_attr_check_initl(&check, \"two\", ...);\n    ...\n}\n\n\nint foo_cmd(const char *prefix int argc, char **argv)\n{\n    foreach_path(get_paths(...))\n        one_func();\n    check = NULL;\n    foreach_path(get_paths(...))\n        two_func();\n}\n\nThis is correct single threaded code, but as soon as you want to\nput phase one,two into threads, as they can be parallelized, this\ngoes horribly wrong.\n\n\n>\n> And in a threaded environment it is even worse; the first thread may\n> call initl() to get one set of attributes in \"check\" and it may be\n> about to ask the question, while the second call may call initl()\n> and by your definition it will notice they have different sets of\n> attributes and returns different \"check\"?  Either the earlier one is\n> leaked, or it gets destroyed even though the first thread hasn't\n> finished with \"check\" it got.\n>\n> It is perfectly OK to drop \"static\" from the above example code.\n> Then it no longer is insane--it is perfectly normal code whose\n> inefficiency cannot be avoided because it wants to do dynamic\n> queries.\n\nI think we had a misunderstanding here, as I was just assuming a\nsingle mutex later on.\n"},{"id":"303916","messageId":"xmqqeg3m8y6y.fsf@gitster.mtv.corp.google.com","threadId":"44273","inReplyTo":"CAGZ79kbdvs88Gt_XW801DPs7rj8kvcfPqCV7kjL_Jz9XiVn7fg@mail.gmail.com","subject":"Re: [PATCH 28/28] attr: convert to new threadsafe API","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-10-11T19:47:49Z","receivedAt":"2016-10-11T19:50:49Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Stefan Beller <sbeller@google.com> writes:\n\n>> You can have many different callsites (think: hits \"git grep\" finds)\n>> that call git_attr_check_initl() and they all may be asking for\n>> different set of attributes.  As long as they are using different\n>> \"check\" instance to receive these sets of attributes, they are OK.\n>\n> Right, but that requires a mutex per callsite; up to now I imagined\n> a global mutex only, which is how I came to the conclusion.\n\nHmph, why?  I agree it would make it easier to allow a mutex keyed\nby the address of \"check\" to enhance parallelism, but I do not think\nit _requires_ it, in the sense that if you were anticipating low\ncontention, one mutex to cover any and all callers to initl() would\nstill give you a correct result.\n\nI wonder if we can assume and rely on atomicity of store, e.g. if we\ncan cheaply notice \"ah this check is already initialized\" without\nlocking.  Then initl() might become something like\n\n    initl(char **variable, ...)\n    {\n\tstruct git_attr_check *check;\n\n    \tif (*variable)\n            return; /* somebody else did it already */\n        acquire lock;\n        if (*variable) {\n\t    /* somebody else was about to finish when we checked earlier */\n\t    release lock;\n\t    return;\n        }\n\tcheck = alloc();\n\tpopulate check;\n\t*variable = check;\n\trelease lock;\n    }\n\nand once the system starts running and starts asking the same\nquestion for thousands of paths, the majority of initl() calls\ncan still be almost no-op, like the original single-threaded\n\n\tstatic struct git_attr_check *check = NULL;\n        if (!check)\n\t\tcheck = init();\n\ncan avoid heavyweight init() most of the time.\n"},{"id":"303939","messageId":"CAGZ79kYYW9VE1yWvLgNHXg=UG2FTh0V9c=38aSLnLVvrpU2Pmw@mail.gmail.com","threadId":"44273","inReplyTo":"20161011173749.GC9085@google.com","subject":"Re: [PATCH 26/28] attr: make git_attr_counted static","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-10-11T21:53:14Z","receivedAt":"2016-10-11T21:53:19Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"On Tue, Oct 11, 2016 at 10:37 AM, Brandon Williams <bmwill@google.com> wrote:\n> On 10/10, Stefan Beller wrote:\n>> It's not used outside the attr code, so let's keep it private.\n>>\n>> Change-Id: I0d15e0f2ea944b31d68b9cf1a4edecac0ca2170d\n>\n> Looks like you forgot to remove this :)\n\nwill be fixed in a reroll.\nthanks!\n\n>\n> --\n> Brandon Williams\n"}]}