{"thread":{"id":"28468","subject":"[PATCH] Teach '--with-tree' option to check-attr","startedAt":"2011-09-23T07:25:29Z","lastAt":"2011-09-23T07:25:29Z","messageCount":1,"participants":["Jay Soffian"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"176047","messageId":"1316762729-11211-1-git-send-email-jaysoffian@gmail.com","threadId":"28468","inReplyTo":null,"subject":"[PATCH] Teach '--with-tree' option to check-attr","fromName":"Jay Soffian","fromEmail":"jaysoffian@gmail.com","sentAt":"2011-09-23T07:25:29Z","receivedAt":"2011-09-23T07:25:29Z","isPatch":true,"sender":{"key":"jaysoffian@gmail.com","avatar":"https://avatars.githubusercontent.com/u/155970?v=4"},"body":"Jakub Narebski writes:\n\n> Nb. the ability to read gitattributes from given commit would be\n> useful also for gitweb (the `encoding` gitattribute, etc.).\n\nSigned-off-by: Jay Soffian <jaysoffian@gmail.com>\n---\n2011/9/22 Jakub Narebski <jnareb@gmail.com>:\n\n> Unfortunately it doesn't seem to be there mechanism to query about\n> state of gitattributes at given commit.\n>\n> There is a slight problem from the UI point of view of git-check-attr,\n> namely that there are _three_ pieces of information: a place to read\n> .gitattributes from (working tree, index, commit), list of attributes\n> to check (or --all) and list of files (list of paths). You can use\n> \"--\" to separate _two_ pieces of information.\n>\n> Nb. the ability to read gitattributes from given commit would be\n> useful also for gitweb (the `encoding` gitattribute, etc.).\n\nHow's this? Builds on top of js/check-attr-cached.\n\n Documentation/git-check-attr.txt |    4 ++++\n builtin/check-attr.c             |   33 ++++++++++++++++++++++++++++++++-\n t/t0003-attributes.sh            |    7 +++++++\n 3 files changed, 43 insertions(+), 1 deletions(-)\n\ndiff --git a/Documentation/git-check-attr.txt b/Documentation/git-check-attr.txt\nindex 5abdbaa51c..06e5d95e0b 100644\n--- a/Documentation/git-check-attr.txt\n+++ b/Documentation/git-check-attr.txt\n@@ -27,6 +27,10 @@ OPTIONS\n --cached::\n \tConsider `.gitattributes` in the index only, ignoring the working tree.\n \n+--with-tree=<tree-ish>::\n+\tConsider .gitattributes in <tree-ish> only, ignoring the working tree\n+\tand index.\n+\n --stdin::\n \tRead file names from stdin instead of from the command-line.\n \ndiff --git a/builtin/check-attr.c b/builtin/check-attr.c\nindex ded0d836d3..fe926d3c97 100644\n--- a/builtin/check-attr.c\n+++ b/builtin/check-attr.c\n@@ -3,10 +3,13 @@\n #include \"attr.h\"\n #include \"quote.h\"\n #include \"parse-options.h\"\n+#include \"tree-walk.h\"\n+#include \"unpack-trees.h\"\n \n static int all_attrs;\n static int cached_attrs;\n static int stdin_paths;\n+static const char *with_tree;\n static const char * const check_attr_usage[] = {\n \"git check-attr [-a | --all | attr...] [--] pathname...\",\n \"git check-attr --stdin [-a | --all | attr...] < <list-of-paths>\",\n@@ -18,6 +21,7 @@ static int null_term_line;\n static const struct option check_attr_options[] = {\n \tOPT_BOOLEAN('a', \"all\", &all_attrs, \"report all attributes set on file\"),\n \tOPT_BOOLEAN(0,  \"cached\", &cached_attrs, \"use .gitattributes only from the index\"),\n+\tOPT_STRING(0,  \"with-tree\", &with_tree, \"tree-ish\", \"use .gitattributes only from <tree-ish>\"),\n \tOPT_BOOLEAN(0 , \"stdin\", &stdin_paths, \"read file names from stdin\"),\n \tOPT_BOOLEAN('z', NULL, &null_term_line,\n \t\t\"input paths are terminated by a null character\"),\n@@ -101,8 +105,35 @@ int cmd_check_attr(int argc, const char **argv, const char *prefix)\n \t\tdie(\"invalid cache\");\n \t}\n \n-\tif (cached_attrs)\n+\tif (cached_attrs && with_tree)\n+\t\terror_with_usage(\"Cannot use --cached and --with-tree together\");\n+\n+\tif (cached_attrs) {\n \t\tgit_attr_set_direction(GIT_ATTR_INDEX, NULL);\n+\t} else if (with_tree) {\n+\t\tunsigned char sha1[20];\n+\t\tstruct tree *tree;\n+\t\tstruct unpack_trees_options opts;\n+\t\tstruct tree_desc t;\n+\n+\t\tif (get_sha1(with_tree, sha1))\n+\t\t\tdie(\"Not a valid object name\");\n+\n+\t\ttree = parse_tree_indirect(sha1);\n+\t\tif (tree == NULL)\n+\t\t\tdie(\"Not a tree object\");\n+\n+\t\tmemset(&opts, 0, sizeof(opts));\n+\t\topts.index_only = 1;\n+\t\topts.head_idx = -1;\n+\t\topts.src_index = &the_index;\n+\t\topts.dst_index = &the_index;\n+\t\topts.fn = oneway_merge;\n+\t\tinit_tree_desc(&t, tree->buffer, tree->size);\n+\t\tif (unpack_trees(1, &t, &opts))\n+\t\t\treturn -1;\n+\t\tgit_attr_set_direction(GIT_ATTR_INDEX, &the_index);\n+\t}\n \n \tdoubledash = -1;\n \tfor (i = 0; doubledash < 0 && i < argc; i++) {\ndiff --git a/t/t0003-attributes.sh b/t/t0003-attributes.sh\nindex 46b0736b35..36ac3a02da 100755\n--- a/t/t0003-attributes.sh\n+++ b/t/t0003-attributes.sh\n@@ -140,6 +140,7 @@ test_expect_success 'root subdir attribute test' '\n '\n \n test_expect_success 'setup bare' '\n+\tgit commit -m \".gitattributes for testing --with-tree below\" &&\n \tgit clone --bare . bare.git &&\n \tcd bare.git\n '\n@@ -163,6 +164,12 @@ test_expect_success 'bare repository: check that --cached honors index' '\n \ttest_cmp ../specified-all actual\n '\n \n+test_expect_success 'bare repository: check --with-tree' '\n+\tgit check-attr --with-tree=HEAD --stdin --all < ../stdin-all |\n+\t\tsort > actual &&\n+\ttest_cmp ../specified-all actual\n+'\n+\n test_expect_success 'bare repository: test info/attributes' '\n \t(\n \t\techo \"f\ttest=f\"\n-- \n1.7.7.rc2.5.g12a2f\n"}]}