{"thread":{"id":"58990","subject":"[PATCH v4 0/2] check-attr: add support to work with revisions","startedAt":"2022-12-21T13:47:04Z","lastAt":"2022-12-23T10:45:30Z","messageCount":8,"participants":["Karthik Nayak","Phillip Wood","Eric Sunshine"],"isPatch":true,"patchVersion":4,"patchTotal":2},"messages":[{"id":"469437","messageId":"cover.1671630304.git.karthik.188@gmail.com","threadId":"58990","inReplyTo":"https://lore.kernel.org/git/20221216093552.3171319-1-karthik.188@gmail.com/","subject":"[PATCH v4 0/2] check-attr: add support to work with revisions","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2022-12-21T13:46:52Z","receivedAt":"2022-12-21T13:47:04Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"v1: https://lore.kernel.org/git/20221206103736.53909-1-karthik.188@gmail.com/\nv2: https://lore.kernel.org/git/CAOLa=ZSsFGBw3ta1jWN8cmUch2ca=zTEjp1xMA6Linafx9W53g@mail.gmail.com/T/#t\nv3: https://lore.kernel.org/git/20221216093552.3171319-1-karthik.188@gmail.com/\n\nGiven a pathname, git-check-attr(1) will list the attributes which apply to that\npathname by reading all relevant gitattributes files. Currently there is no way\nto specify a revision to read the gitattributes from.\n\nThis is specifically useful in bare repositories wherein the gitattributes are\nonly present in the git working tree but not available directly on the\nfilesystem.\n\nThis series aims to add a new flag `--revision` to git-check-attr(1) which\nallows us to read gitattributes from the specified revision.\n\nChanges since version 3:\n- Dropped the `-r` shorcut for the flag\n- Fixed tests failing by initializing the tree_oid to NULL\n- Incorporated some changes around the commit message\n\nRange-diff against v3:\n\n1:  6224754179 = 1:  6224754179 t0003: move setup for `--all` into new block\n2:  d8f8b3c36c ! 2:  a161dbdf8b attr: add flag `-r|--revisions` to work with revisions\n    @@ Metadata\n     Author: Karthik Nayak <karthik.188@gmail.com>\n     \n      ## Commit message ##\n    -    attr: add flag `-r|--revisions` to work with revisions\n    +    attr: add flag `--revision` to work with revisions\n     \n         The contents of the .gitattributes files may evolve over time, but \"git\n         check-attr\" always checks attributes against them in the working tree\n         and/or in the index. It may be beneficial to optionally allow the users\n    -    to check attributes against paths from older commits.\n    +    to check attributes taken from a commit other than HEAD against paths.\n     \n    -    Add a new flag `--revision`/`-r` which will allow users to check the\n    -    attributes against a tree-ish revision. When the user uses this flag, we\n    -    go through the stack of .gitattributes files but instead of checking the\n    -    current working tree and/or in the index, we check the blobs from the\n    -    provided tree-ish object. This allows the command to also be used in\n    -    bare repositories.\n    +    Add a new flag `--revision` which will allow users to check the\n    +    attributes against a commit (actually any tree-ish would do). When the\n    +    user uses this flag, we go through the stack of .gitattributes files but\n    +    instead of checking the current working tree and/or in the index, we\n    +    check the blobs from the provided tree-ish object. This allows the\n    +    command to also be used in bare repositories.\n     \n    -    Since we use a tree-ish object, the user can pass \"-r HEAD:subdirectory\"\n    -    and all the attributes will be looked up as if subdirectory was the root\n    -    directory of the repository.\n    +    Since we use a tree-ish object, the user can pass \"--revision\n    +    HEAD:subdirectory\" and all the attributes will be looked up as if\n    +    subdirectory was the root directory of the repository.\n     \n    -    We cannot use the `<rev>:<path>` syntax like the one used in `git show`\n    -    because any non-flag parameter before `--` is treated as an attribute\n    -    and any parameter after `--` is treated as a pathname.\n    +    We cannot simply use the `<rev>:<path>` syntax without the `--revision`\n    +    flag, similar to how it is used in `git show` because any non-flag\n    +    parameter before `--` is treated as an attribute and any parameter after\n    +    `--` is treated as a pathname.\n     \n         The change involves creating a new function `read_attr_from_blob`, which\n         given the path reads the blob for the path against the provided revision and\n    @@ Commit message\n         files.\n     \n         Signed-off-by: Karthik Nayak <karthik.188@gmail.com>\n    +    Signed-off-by: Toon Claes <toon@iotcl.com>\n         Co-authored-by: toon@iotcl.com\n     \n      ## Documentation/git-check-attr.txt ##\n    @@ Documentation/git-check-attr.txt: git-check-attr - Display gitattributes informa\n      [verse]\n     -'git check-attr' [-a | --all | <attr>...] [--] <pathname>...\n     -'git check-attr' --stdin [-z] [-a | --all | <attr>...]\n    -+'git check-attr' [-r <revision>] [-a | --all | <attr>...] [--] <pathname>...\n    -+'git check-attr' --stdin [-z] [-r <revision>] [-a | --all | <attr>...]\n    ++'git check-attr' [--revision <revision>] [-a | --all | <attr>...] [--] <pathname>...\n    ++'git check-attr' --stdin [-z] [--revision <revision>] [-a | --all | <attr>...]\n      \n      DESCRIPTION\n      -----------\n    @@ Documentation/git-check-attr.txt: OPTIONS\n      \tIf `--stdin` is also given, input paths are separated\n      \twith a NUL character instead of a linefeed character.\n      \n    -+--r <revision>::\n     +--revision=<revision>::\n    -+\tCheck attributes against the specified tree-ish revision. All the\n    -+\tattributes will be checked against the provided revision. Paths provided\n    -+\tas part of the revision will be treated as the root directory.\n    ++\tCheck attributes against the specified commit. All the attributes will\n    ++\tbe checked against the provided revision. Paths provided as part of the\n    ++\trevision will be treated as the root directory.\n     +\n      \\--::\n      \tInterpret all preceding arguments as attributes and all following\n    @@ attr.c\n      #include \"exec-cmd.h\"\n      #include \"attr.h\"\n      #include \"dir.h\"\n    -+#include \"git-compat-util.h\"\n     +#include \"strbuf.h\"\n     +#include \"tree-walk.h\"\n      #include \"utf8.h\"\n    @@ builtin/check-attr.c\n      static const char * const check_attr_usage[] = {\n     -N_(\"git check-attr [-a | --all | <attr>...] [--] <pathname>...\"),\n     -N_(\"git check-attr --stdin [-z] [-a | --all | <attr>...]\"),\n    -+N_(\"git check-attr [-r <revision>] [-a | --all | <attr>...] [--] <pathname>...\"),\n    -+N_(\"git check-attr --stdin [-z] [-r <revision>] [-a | --all | <attr>...]\"),\n    ++N_(\"git check-attr [--revision <revision>] [-a | --all | <attr>...] [--] <pathname>...\"),\n    ++N_(\"git check-attr --stdin [-z] [--revision <revision>] [-a | --all | <attr>...]\"),\n      NULL\n      };\n      \n    @@ builtin/check-attr.c: static const struct option check_attr_options[] = {\n      \tOPT_BOOL(0 , \"stdin\", &stdin_paths, N_(\"read file names from stdin\")),\n      \tOPT_BOOL('z', NULL, &nul_term_line,\n      \t\t N_(\"terminate input and output records by a NUL character\")),\n    -+\tOPT_STRING('r', \"revision\", &revision, N_(\"revision\"), N_(\"check attributes at this revision\")),\n    ++\tOPT_STRING(0, \"revision\", &revision, N_(\"revision\"), N_(\"check attributes at this revision\")),\n      \tOPT_END()\n      };\n      \n    @@ builtin/check-attr.c: static NORETURN void error_with_usage(const char *msg)\n      int cmd_check_attr(int argc, const char **argv, const char *prefix)\n      {\n      \tstruct attr_check *check;\n    -+\tstruct object_id tree_oid;\n    ++\tstruct object_id *tree_oid = NULL;\n      \tint cnt, i, doubledash, filei;\n      \n      \tif (!is_bare_repository())\n    @@ builtin/check-attr.c: int cmd_check_attr(int argc, const char **argv, const char\n      \t\t}\n      \t}\n      \n    -+\tif (revision)\n    -+\t\tif (repo_get_oid_tree(the_repository, revision, &tree_oid))\n    ++\tif (revision) {\n    ++\t\ttree_oid = xmalloc(sizeof(struct object_id));\n    ++\n    ++\t\tif (repo_get_oid_tree(the_repository, revision, tree_oid))\n     +\t\t\terror(\"%s: not a valid revision\", revision);\n    ++\t}\n     +\n      \tif (stdin_paths)\n     -\t\tcheck_attr_stdin_paths(prefix, check, all_attrs);\n    -+\t\tcheck_attr_stdin_paths(prefix, check, &tree_oid, all_attrs);\n    ++\t\tcheck_attr_stdin_paths(prefix, check, tree_oid, all_attrs);\n      \telse {\n      \t\tfor (i = filei; i < argc; i++)\n     -\t\t\tcheck_attr(prefix, check, all_attrs, argv[i]);\n    -+\t\t\tcheck_attr(prefix, check, &tree_oid, all_attrs, argv[i]);\n    ++\t\t\tcheck_attr(prefix, check, tree_oid, all_attrs, argv[i]);\n      \t\tmaybe_flush_or_die(stdout, \"attribute to stdout\");\n      \t}\n      \n    @@ t/t0003-attributes.sh: attr_check_quote () {\n     +attr_check_revision () {\n     +\tpath=\"$1\" expect=\"$2\" revision=\"$3\" git_opts=\"$4\" &&\n      \n    -+\tgit $git_opts check-attr -r $revision test -- \"$path\" >actual 2>err &&\n    ++\tgit $git_opts check-attr --revision $revision test -- \"$path\" >actual 2>err &&\n     +\techo \"$path: test: $expect\" >expect &&\n     +\ttest_cmp expect actual\n      }\n    @@ t/t0003-attributes.sh: test_expect_success 'setup' '\n      \ttest_must_fail git check-attr test &&\n      \ttest_must_fail git check-attr test -- &&\n      \ttest_must_fail git check-attr -- f &&\n    -+\ttest_must_fail git check-attr -r &&\n    -+\ttest_must_fail git check-attr -r not-a-valid-ref &&\n    ++\ttest_must_fail git check-attr --revision &&\n    ++\ttest_must_fail git check-attr --revision not-a-valid-ref &&\n      \techo \"f\" | test_must_fail git check-attr --stdin &&\n      \techo \"f\" | test_must_fail git check-attr --stdin -- f &&\n      \techo \"f\" | test_must_fail git check-attr --stdin test -- f &&\n\n\nKarthik Nayak (2):\n  t0003: move setup for `--all` into new block\n  attr: add flag `--revision` to work with revisions\n\n Documentation/git-check-attr.txt |  9 ++-\n archive.c                        |  2 +-\n attr.c                           | 99 +++++++++++++++++++++++---------\n attr.h                           |  7 ++-\n builtin/check-attr.c             | 36 +++++++-----\n builtin/pack-objects.c           |  2 +-\n convert.c                        |  2 +-\n ll-merge.c                       |  4 +-\n pathspec.c                       |  2 +-\n t/t0003-attributes.sh            | 71 ++++++++++++++++++++++-\n userdiff.c                       |  2 +-\n ws.c                             |  2 +-\n 12 files changed, 183 insertions(+), 55 deletions(-)\n\n-- \n2.39.0\n\n"},{"id":"469438","messageId":"622475417916cf48b8fb48e3587f6c87750d43ed.1671630304.git.karthik.188@gmail.com","threadId":"58990","inReplyTo":"cover.1671630304.git.karthik.188@gmail.com","subject":"[PATCH v4 1/2] t0003: move setup for `--all` into new block","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2022-12-21T13:47:31Z","receivedAt":"2022-12-21T13:47:53Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"There is some setup code which is used by multiple tests being setup in\n`attribute test: --all option`. This means when we run \"sh\n./t0003-attributes.sh --run=setup,<num>\" there is a chance of failing\nsince we missed this setup block.\n\nSo to ensure that setups are independent of test logic, move this to a\nnew setup block.\n\nSigned-off-by: Karthik Nayak <karthik.188@gmail.com>\nCo-authored-by: toon@iotcl.com\n---\n t/t0003-attributes.sh | 7 +++++--\n 1 file changed, 5 insertions(+), 2 deletions(-)\n\ndiff --git a/t/t0003-attributes.sh b/t/t0003-attributes.sh\nindex f7ee2f2ff0..b3aabb8aa3 100755\n--- a/t/t0003-attributes.sh\n+++ b/t/t0003-attributes.sh\n@@ -203,9 +203,12 @@ test_expect_success 'attribute test: read paths from stdin' '\n \ttest_cmp expect actual\n '\n \n-test_expect_success 'attribute test: --all option' '\n+test_expect_success 'setup --all option' '\n \tgrep -v unspecified <expect-all | sort >specified-all &&\n-\tsed -e \"s/:.*//\" <expect-all | uniq >stdin-all &&\n+\tsed -e \"s/:.*//\" <expect-all | uniq >stdin-all\n+'\n+\n+test_expect_success 'attribute test: --all option' '\n \tgit check-attr --stdin --all <stdin-all >tmp &&\n \tsort tmp >actual &&\n \ttest_cmp specified-all actual\n-- \n2.39.0\n\n"},{"id":"469439","messageId":"a161dbdf8b70354716d0bfeccc707c242c7f33a1.1671630304.git.karthik.188@gmail.com","threadId":"58990","inReplyTo":"cover.1671630304.git.karthik.188@gmail.com","subject":"[PATCH v4 2/2] attr: add flag `--revision` to work with revisions","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2022-12-21T13:47:32Z","receivedAt":"2022-12-21T13:48:02Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"The contents of the .gitattributes files may evolve over time, but \"git\ncheck-attr\" always checks attributes against them in the working tree\nand/or in the index. It may be beneficial to optionally allow the users\nto check attributes taken from a commit other than HEAD against paths.\n\nAdd a new flag `--revision` which will allow users to check the\nattributes against a commit (actually any tree-ish would do). When the\nuser uses this flag, we go through the stack of .gitattributes files but\ninstead of checking the current working tree and/or in the index, we\ncheck the blobs from the provided tree-ish object. This allows the\ncommand to also be used in bare repositories.\n\nSince we use a tree-ish object, the user can pass \"--revision\nHEAD:subdirectory\" and all the attributes will be looked up as if\nsubdirectory was the root directory of the repository.\n\nWe cannot simply use the `<rev>:<path>` syntax without the `--revision`\nflag, similar to how it is used in `git show` because any non-flag\nparameter before `--` is treated as an attribute and any parameter after\n`--` is treated as a pathname.\n\nThe change involves creating a new function `read_attr_from_blob`, which\ngiven the path reads the blob for the path against the provided revision and\nparses the attributes line by line. This function is plugged into\n`read_attr()` function wherein we go through the stack of attributes\nfiles.\n\nSigned-off-by: Karthik Nayak <karthik.188@gmail.com>\nSigned-off-by: Toon Claes <toon@iotcl.com>\nCo-authored-by: toon@iotcl.com\n---\n Documentation/git-check-attr.txt |  9 ++-\n archive.c                        |  2 +-\n attr.c                           | 99 +++++++++++++++++++++++---------\n attr.h                           |  7 ++-\n builtin/check-attr.c             | 36 +++++++-----\n builtin/pack-objects.c           |  2 +-\n convert.c                        |  2 +-\n ll-merge.c                       |  4 +-\n pathspec.c                       |  2 +-\n t/t0003-attributes.sh            | 64 ++++++++++++++++++++-\n userdiff.c                       |  2 +-\n ws.c                             |  2 +-\n 12 files changed, 178 insertions(+), 53 deletions(-)\n\ndiff --git a/Documentation/git-check-attr.txt b/Documentation/git-check-attr.txt\nindex 84f41a8e82..174019c71d 100644\n--- a/Documentation/git-check-attr.txt\n+++ b/Documentation/git-check-attr.txt\n@@ -9,8 +9,8 @@ git-check-attr - Display gitattributes information\n SYNOPSIS\n --------\n [verse]\n-'git check-attr' [-a | --all | <attr>...] [--] <pathname>...\n-'git check-attr' --stdin [-z] [-a | --all | <attr>...]\n+'git check-attr' [--revision <revision>] [-a | --all | <attr>...] [--] <pathname>...\n+'git check-attr' --stdin [-z] [--revision <revision>] [-a | --all | <attr>...]\n \n DESCRIPTION\n -----------\n@@ -36,6 +36,11 @@ OPTIONS\n \tIf `--stdin` is also given, input paths are separated\n \twith a NUL character instead of a linefeed character.\n \n+--revision=<revision>::\n+\tCheck attributes against the specified commit. All the attributes will\n+\tbe checked against the provided revision. Paths provided as part of the\n+\trevision will be treated as the root directory.\n+\n \\--::\n \tInterpret all preceding arguments as attributes and all following\n \targuments as path names.\ndiff --git a/archive.c b/archive.c\nindex 941495f5d7..81ff76fce9 100644\n--- a/archive.c\n+++ b/archive.c\n@@ -120,7 +120,7 @@ static const struct attr_check *get_archive_attrs(struct index_state *istate,\n \tstatic struct attr_check *check;\n \tif (!check)\n \t\tcheck = attr_check_initl(\"export-ignore\", \"export-subst\", NULL);\n-\tgit_check_attr(istate, path, check);\n+\tgit_check_attr(istate, NULL, path, check);\n \treturn check;\n }\n \ndiff --git a/attr.c b/attr.c\nindex 42ad6de8c7..6c69e82080 100644\n--- a/attr.c\n+++ b/attr.c\n@@ -11,8 +11,12 @@\n #include \"exec-cmd.h\"\n #include \"attr.h\"\n #include \"dir.h\"\n+#include \"strbuf.h\"\n+#include \"tree-walk.h\"\n #include \"utf8.h\"\n #include \"quote.h\"\n+#include \"revision.h\"\n+#include \"object-store.h\"\n #include \"thread-utils.h\"\n \n const char git_attr__true[] = \"(builtin)true\";\n@@ -729,14 +733,62 @@ static struct attr_stack *read_attr_from_file(const char *path, unsigned flags)\n \treturn res;\n }\n \n-static struct attr_stack *read_attr_from_index(struct index_state *istate,\n-\t\t\t\t\t       const char *path,\n-\t\t\t\t\t       unsigned flags)\n+static struct attr_stack *read_attr_from_buf(char *buf, const char *path,\n+\t\t\t\t\t     unsigned flags)\n {\n \tstruct attr_stack *res;\n-\tchar *buf, *sp;\n+\tchar *sp;\n \tint lineno = 0;\n \n+\tif (!buf)\n+\t\treturn NULL;\n+\n+\tCALLOC_ARRAY(res, 1);\n+\tfor (sp = buf; *sp;) {\n+\t\tchar *ep;\n+\t\tint more;\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, flags);\n+\t\tsp = ep + more;\n+\t}\n+\tfree(buf);\n+\n+\treturn res;\n+}\n+\n+static struct attr_stack *read_attr_from_blob(struct index_state *istate,\n+\t\t\t\t\t      const struct object_id *tree_oid,\n+\t\t\t\t\t      const char *path, unsigned flags)\n+{\n+\tstruct object_id oid;\n+\tunsigned long sz;\n+\tenum object_type type;\n+\tvoid *buf;\n+\tunsigned short mode;\n+\n+\tif (!tree_oid)\n+\t\treturn NULL;\n+\n+\tif (get_tree_entry(istate->repo, tree_oid, path, &oid, &mode))\n+\t\treturn NULL;\n+\n+\tbuf = read_object_file(&oid, &type, &sz);\n+\tif (!buf || type != OBJ_BLOB) {\n+\t\tfree(buf);\n+\t\treturn NULL;\n+\t}\n+\n+\treturn read_attr_from_buf(buf, path, flags);\n+}\n+\n+static struct attr_stack *read_attr_from_index(struct index_state *istate,\n+\t\t\t\t\t       const char *path, unsigned flags)\n+{\n+\tchar *buf;\n+\n \tif (!istate)\n \t\treturn NULL;\n \n@@ -758,28 +810,19 @@ static struct attr_stack *read_attr_from_index(struct index_state *istate,\n \tif (!buf)\n \t\treturn NULL;\n \n-\tCALLOC_ARRAY(res, 1);\n-\tfor (sp = buf; *sp; ) {\n-\t\tchar *ep;\n-\t\tint more;\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, flags);\n-\t\tsp = ep + more;\n-\t}\n-\tfree(buf);\n-\treturn res;\n+\treturn read_attr_from_buf(buf, path, flags);\n }\n \n static struct attr_stack *read_attr(struct index_state *istate,\n+\t\t\t\t    const struct object_id *tree_oid,\n \t\t\t\t    const char *path, unsigned flags)\n {\n \tstruct attr_stack *res = NULL;\n \n \tif (direction == GIT_ATTR_INDEX) {\n \t\tres = read_attr_from_index(istate, path, flags);\n+\t} else if (tree_oid) {\n+\t\tres = read_attr_from_blob(istate, tree_oid, path, flags);\n \t} else if (!is_bare_repository()) {\n \t\tif (direction == GIT_ATTR_CHECKOUT) {\n \t\t\tres = read_attr_from_index(istate, path, flags);\n@@ -839,6 +882,7 @@ static void push_stack(struct attr_stack **attr_stack_p,\n }\n \n static void bootstrap_attr_stack(struct index_state *istate,\n+\t\t\t\t const struct object_id *tree_oid,\n \t\t\t\t struct attr_stack **stack)\n {\n \tstruct attr_stack *e;\n@@ -864,7 +908,7 @@ static void bootstrap_attr_stack(struct index_state *istate,\n \t}\n \n \t/* root directory */\n-\te = read_attr(istate, GITATTRIBUTES_FILE, flags | READ_ATTR_NOFOLLOW);\n+\te = read_attr(istate, tree_oid, GITATTRIBUTES_FILE, flags | READ_ATTR_NOFOLLOW);\n \tpush_stack(stack, e, xstrdup(\"\"), 0);\n \n \t/* info frame */\n@@ -878,6 +922,7 @@ static void bootstrap_attr_stack(struct index_state *istate,\n }\n \n static void prepare_attr_stack(struct index_state *istate,\n+\t\t\t       const struct object_id *tree_oid,\n \t\t\t       const char *path, int dirlen,\n \t\t\t       struct attr_stack **stack)\n {\n@@ -899,7 +944,7 @@ static void prepare_attr_stack(struct index_state *istate,\n \t * .gitattributes in deeper directories to shallower ones,\n \t * and finally use the built-in set as the default.\n \t */\n-\tbootstrap_attr_stack(istate, stack);\n+\tbootstrap_attr_stack(istate, tree_oid, stack);\n \n \t/*\n \t * Pop the \"info\" one that is always at the top of the stack.\n@@ -954,7 +999,7 @@ static void prepare_attr_stack(struct index_state *istate,\n \t\tstrbuf_add(&pathbuf, path + pathbuf.len, (len - pathbuf.len));\n \t\tstrbuf_addf(&pathbuf, \"/%s\", GITATTRIBUTES_FILE);\n \n-\t\tnext = read_attr(istate, pathbuf.buf, READ_ATTR_NOFOLLOW);\n+\t\tnext = read_attr(istate, tree_oid, pathbuf.buf, READ_ATTR_NOFOLLOW);\n \n \t\t/* reset the pathbuf to not include \"/.gitattributes\" */\n \t\tstrbuf_setlen(&pathbuf, len);\n@@ -1074,8 +1119,8 @@ static void determine_macros(struct all_attrs_item *all_attrs,\n  * Otherwise all attributes are collected.\n  */\n static void collect_some_attrs(struct index_state *istate,\n-\t\t\t       const char *path,\n-\t\t\t       struct attr_check *check)\n+\t\t\t       const struct object_id *tree_oid,\n+\t\t\t       const char *path, struct attr_check *check)\n {\n \tint pathlen, rem, dirlen;\n \tconst char *cp, *last_slash = NULL;\n@@ -1094,7 +1139,7 @@ static void collect_some_attrs(struct index_state *istate,\n \t\tdirlen = 0;\n \t}\n \n-\tprepare_attr_stack(istate, path, dirlen, &check->stack);\n+\tprepare_attr_stack(istate, tree_oid, path, dirlen, &check->stack);\n \tall_attrs_init(&g_attr_hashmap, check);\n \tdetermine_macros(check->all_attrs, check->stack);\n \n@@ -1103,12 +1148,12 @@ static void collect_some_attrs(struct index_state *istate,\n }\n \n void git_check_attr(struct index_state *istate,\n-\t\t    const char *path,\n+\t\t    const struct object_id *tree_oid, const char *path,\n \t\t    struct attr_check *check)\n {\n \tint i;\n \n-\tcollect_some_attrs(istate, path, check);\n+\tcollect_some_attrs(istate, tree_oid, path, check);\n \n \tfor (i = 0; i < check->nr; i++) {\n \t\tsize_t n = check->items[i].attr->attr_nr;\n@@ -1119,13 +1164,13 @@ void git_check_attr(struct index_state *istate,\n \t}\n }\n \n-void git_all_attrs(struct index_state *istate,\n+void git_all_attrs(struct index_state *istate, const struct object_id *tree_oid,\n \t\t   const char *path, struct attr_check *check)\n {\n \tint i;\n \n \tattr_check_reset(check);\n-\tcollect_some_attrs(istate, path, check);\n+\tcollect_some_attrs(istate, tree_oid, path, check);\n \n \tfor (i = 0; i < check->all_attrs_nr; i++) {\n \t\tconst char *name = check->all_attrs[i].attr->name;\ndiff --git a/attr.h b/attr.h\nindex 3fb40cced0..f4a2bedd68 100644\n--- a/attr.h\n+++ b/attr.h\n@@ -1,6 +1,8 @@\n #ifndef ATTR_H\n #define ATTR_H\n \n+#include \"hash.h\"\n+\n /**\n  * gitattributes mechanism gives a uniform way to associate various attributes\n  * to set of paths.\n@@ -190,13 +192,14 @@ void attr_check_free(struct attr_check *check);\n const char *git_attr_name(const struct git_attr *);\n \n void git_check_attr(struct index_state *istate,\n-\t\t    const char *path, struct attr_check *check);\n+\t\t    const struct object_id *tree_oid, const char *path,\n+\t\t    struct attr_check *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(struct index_state *istate,\n+void git_all_attrs(struct index_state *istate, const struct object_id *tree_oid,\n \t\t   const char *path, struct attr_check *check);\n \n enum git_attr_direction {\ndiff --git a/builtin/check-attr.c b/builtin/check-attr.c\nindex 0fef10eb6b..04640e0297 100644\n--- a/builtin/check-attr.c\n+++ b/builtin/check-attr.c\n@@ -1,3 +1,4 @@\n+#include \"repository.h\"\n #define USE_THE_INDEX_VARIABLE\n #include \"builtin.h\"\n #include \"cache.h\"\n@@ -9,9 +10,10 @@\n static int all_attrs;\n static int cached_attrs;\n static int stdin_paths;\n+static char *revision;\n static const char * const check_attr_usage[] = {\n-N_(\"git check-attr [-a | --all | <attr>...] [--] <pathname>...\"),\n-N_(\"git check-attr --stdin [-z] [-a | --all | <attr>...]\"),\n+N_(\"git check-attr [--revision <revision>] [-a | --all | <attr>...] [--] <pathname>...\"),\n+N_(\"git check-attr --stdin [-z] [--revision <revision>] [-a | --all | <attr>...]\"),\n NULL\n };\n \n@@ -23,6 +25,7 @@ static const struct option check_attr_options[] = {\n \tOPT_BOOL(0 , \"stdin\", &stdin_paths, N_(\"read file names from stdin\")),\n \tOPT_BOOL('z', NULL, &nul_term_line,\n \t\t N_(\"terminate input and output records by a NUL character\")),\n+\tOPT_STRING(0, \"revision\", &revision, N_(\"revision\"), N_(\"check attributes at this revision\")),\n \tOPT_END()\n };\n \n@@ -55,27 +58,26 @@ static void output_attr(struct attr_check *check, const char *file)\n \t}\n }\n \n-static void check_attr(const char *prefix,\n-\t\t       struct attr_check *check,\n-\t\t       int collect_all,\n+static void check_attr(const char *prefix, struct attr_check *check,\n+\t\t       const struct object_id *tree_oid, int collect_all,\n \t\t       const char *file)\n+\n {\n \tchar *full_path =\n \t\tprefix_path(prefix, prefix ? strlen(prefix) : 0, file);\n \n \tif (collect_all) {\n-\t\tgit_all_attrs(&the_index, full_path, check);\n+\t\tgit_all_attrs(&the_index, tree_oid, full_path, check);\n \t} else {\n-\t\tgit_check_attr(&the_index, full_path, check);\n+\t\tgit_check_attr(&the_index, tree_oid, full_path, check);\n \t}\n \toutput_attr(check, file);\n \n \tfree(full_path);\n }\n \n-static void check_attr_stdin_paths(const char *prefix,\n-\t\t\t\t   struct attr_check *check,\n-\t\t\t\t   int collect_all)\n+static void check_attr_stdin_paths(const char *prefix, struct attr_check *check,\n+\t\t\t\t   const struct object_id *tree_oid, int collect_all)\n {\n \tstruct strbuf buf = STRBUF_INIT;\n \tstruct strbuf unquoted = STRBUF_INIT;\n@@ -89,7 +91,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, check, collect_all, buf.buf);\n+\t\tcheck_attr(prefix, check, tree_oid, collect_all, buf.buf);\n \t\tmaybe_flush_or_die(stdout, \"attribute to stdout\");\n \t}\n \tstrbuf_release(&buf);\n@@ -105,6 +107,7 @@ static NORETURN void error_with_usage(const char *msg)\n int cmd_check_attr(int argc, const char **argv, const char *prefix)\n {\n \tstruct attr_check *check;\n+\tstruct object_id *tree_oid = NULL;\n \tint cnt, i, doubledash, filei;\n \n \tif (!is_bare_repository())\n@@ -176,11 +179,18 @@ int cmd_check_attr(int argc, const char **argv, const char *prefix)\n \t\t}\n \t}\n \n+\tif (revision) {\n+\t\ttree_oid = xmalloc(sizeof(struct object_id));\n+\n+\t\tif (repo_get_oid_tree(the_repository, revision, tree_oid))\n+\t\t\terror(\"%s: not a valid revision\", revision);\n+\t}\n+\n \tif (stdin_paths)\n-\t\tcheck_attr_stdin_paths(prefix, check, all_attrs);\n+\t\tcheck_attr_stdin_paths(prefix, check, tree_oid, all_attrs);\n \telse {\n \t\tfor (i = filei; i < argc; i++)\n-\t\t\tcheck_attr(prefix, check, all_attrs, argv[i]);\n+\t\t\tcheck_attr(prefix, check, tree_oid, all_attrs, argv[i]);\n \t\tmaybe_flush_or_die(stdout, \"attribute to stdout\");\n \t}\n \ndiff --git a/builtin/pack-objects.c b/builtin/pack-objects.c\nindex 573d0b20b7..89535cfa6a 100644\n--- a/builtin/pack-objects.c\n+++ b/builtin/pack-objects.c\n@@ -1318,7 +1318,7 @@ static int no_try_delta(const char *path)\n \n \tif (!check)\n \t\tcheck = attr_check_initl(\"delta\", NULL);\n-\tgit_check_attr(the_repository->index, path, check);\n+\tgit_check_attr(the_repository->index, NULL, path, check);\n \tif (ATTR_FALSE(check->items[0].value))\n \t\treturn 1;\n \treturn 0;\ndiff --git a/convert.c b/convert.c\nindex 9b67649032..a54d1690c0 100644\n--- a/convert.c\n+++ b/convert.c\n@@ -1308,7 +1308,7 @@ void convert_attrs(struct index_state *istate,\n \t\tgit_config(read_convert_config, NULL);\n \t}\n \n-\tgit_check_attr(istate, path, check);\n+\tgit_check_attr(istate, NULL, path, check);\n \tccheck = check->items;\n \tca->crlf_action = git_path_check_crlf(ccheck + 4);\n \tif (ca->crlf_action == CRLF_UNDEFINED)\ndiff --git a/ll-merge.c b/ll-merge.c\nindex 22a603e8af..130d26501c 100644\n--- a/ll-merge.c\n+++ b/ll-merge.c\n@@ -391,7 +391,7 @@ enum ll_merge_result ll_merge(mmbuffer_t *result_buf,\n \t\tnormalize_file(theirs, path, istate);\n \t}\n \n-\tgit_check_attr(istate, path, check);\n+\tgit_check_attr(istate, NULL, path, check);\n \tll_driver_name = check->items[0].value;\n \tif (check->items[1].value) {\n \t\tmarker_size = atoi(check->items[1].value);\n@@ -419,7 +419,7 @@ int ll_merge_marker_size(struct index_state *istate, const char *path)\n \n \tif (!check)\n \t\tcheck = attr_check_initl(\"conflict-marker-size\", NULL);\n-\tgit_check_attr(istate, path, check);\n+\tgit_check_attr(istate, NULL, path, check);\n \tif (check->items[0].value) {\n \t\tmarker_size = atoi(check->items[0].value);\n \t\tif (marker_size <= 0)\ndiff --git a/pathspec.c b/pathspec.c\nindex 46e77a85fe..48dec2c709 100644\n--- a/pathspec.c\n+++ b/pathspec.c\n@@ -732,7 +732,7 @@ int match_pathspec_attrs(struct index_state *istate,\n \tif (name[namelen])\n \t\tname = to_free = xmemdupz(name, namelen);\n \n-\tgit_check_attr(istate, name, item->attr_check);\n+\tgit_check_attr(istate, NULL, name, item->attr_check);\n \n \tfree(to_free);\n \ndiff --git a/t/t0003-attributes.sh b/t/t0003-attributes.sh\nindex b3aabb8aa3..6e6a909a46 100755\n--- a/t/t0003-attributes.sh\n+++ b/t/t0003-attributes.sh\n@@ -25,7 +25,14 @@ attr_check_quote () {\n \tgit check-attr test -- \"$path\" >actual &&\n \techo \"\\\"$quoted_path\\\": test: $expect\" >expect &&\n \ttest_cmp expect actual\n+}\n+\n+attr_check_revision () {\n+\tpath=\"$1\" expect=\"$2\" revision=\"$3\" git_opts=\"$4\" &&\n \n+\tgit $git_opts check-attr --revision $revision test -- \"$path\" >actual 2>err &&\n+\techo \"$path: test: $expect\" >expect &&\n+\ttest_cmp expect actual\n }\n \n test_expect_success 'open-quoted pathname' '\n@@ -33,7 +40,6 @@ test_expect_success 'open-quoted pathname' '\n \tattr_check a unspecified\n '\n \n-\n test_expect_success 'setup' '\n \tmkdir -p a/b/d a/c b &&\n \t(\n@@ -80,12 +86,38 @@ test_expect_success 'setup' '\n \tEOF\n '\n \n+test_expect_success 'setup branches' '\n+\t(\n+\t\techo \"f\ttest=f\" &&\n+\t\techo \"a/i test=n\"\n+\t) | git hash-object -w --stdin >id &&\n+\tgit update-index --add --cacheinfo 100644,$(cat id),foo/bar/.gitattributes &&\n+\tgit write-tree >id &&\n+\ttree_id=$(cat id) &&\n+\tgit commit-tree $tree_id -m \"random commit message\" >id &&\n+\tcommit_id=$(cat id) &&\n+\tgit update-ref refs/heads/branch1 $commit_id &&\n+\n+\t(\n+\t\techo \"g test=g\" &&\n+\t\techo \"a/i test=m\"\n+\t) | git hash-object -w --stdin >id &&\n+\tgit update-index --add --cacheinfo 100644,$(cat id),foo/bar/.gitattributes &&\n+\tgit write-tree >id &&\n+\ttree_id=$(cat id) &&\n+\tgit commit-tree $tree_id -m \"random commit message\" >id &&\n+\tcommit_id=$(cat id) &&\n+\tgit update-ref refs/heads/branch2 $commit_id\n+'\n+\n test_expect_success 'command line checks' '\n \ttest_must_fail git check-attr &&\n \ttest_must_fail git check-attr -- &&\n \ttest_must_fail git check-attr test &&\n \ttest_must_fail git check-attr test -- &&\n \ttest_must_fail git check-attr -- f &&\n+\ttest_must_fail git check-attr --revision &&\n+\ttest_must_fail git check-attr --revision not-a-valid-ref &&\n \techo \"f\" | test_must_fail git check-attr --stdin &&\n \techo \"f\" | test_must_fail git check-attr --stdin -- f &&\n \techo \"f\" | test_must_fail git check-attr --stdin test -- f &&\n@@ -287,6 +319,15 @@ test_expect_success 'using --git-dir and --work-tree' '\n \t)\n '\n \n+test_expect_success 'using --revision' '\n+\tattr_check_revision foo/bar/f f branch1 &&\n+\tattr_check_revision foo/bar/a/i n branch1 &&\n+\tattr_check_revision foo/bar/f unspecified branch2 &&\n+\tattr_check_revision foo/bar/a/i m branch2 &&\n+\tattr_check_revision foo/bar/g g branch2 &&\n+\tattr_check_revision foo/bar/g unspecified branch1\n+'\n+\n test_expect_success 'setup bare' '\n \tgit clone --template= --bare . bare.git\n '\n@@ -306,6 +347,27 @@ test_expect_success 'bare repository: check that .gitattribute is ignored' '\n \t)\n '\n \n+test_expect_success 'bare repository: with --revision' '\n+\t(\n+\t\tcd bare.git &&\n+\t\t(\n+\t\t\techo \"f\ttest=f\" &&\n+\t\t\techo \"a/i test=a/i\"\n+\t\t) | git hash-object -w --stdin >id &&\n+\t\tgit update-index --add --cacheinfo 100644 $(cat id) .gitattributes &&\n+\t\tgit write-tree >id &&\n+\t\ttree_id=$(cat id) &&\n+\t\tgit commit-tree $tree_id -m \"random commit message\" >id &&\n+\t\tcommit_id=$(cat id) &&\n+\t\tgit update-ref refs/heads/master $commit_id &&\n+\t\tattr_check_revision f f HEAD &&\n+\t\tattr_check_revision a/f f HEAD &&\n+\t\tattr_check_revision a/c/f f HEAD &&\n+\t\tattr_check_revision a/i a/i HEAD &&\n+\t\tattr_check_revision subdir/a/i unspecified HEAD\n+\t)\n+'\n+\n test_expect_success 'bare repository: check that --cached honors index' '\n \t(\n \t\tcd bare.git &&\ndiff --git a/userdiff.c b/userdiff.c\nindex 151d9a5278..b66f090a0b 100644\n--- a/userdiff.c\n+++ b/userdiff.c\n@@ -412,7 +412,7 @@ struct userdiff_driver *userdiff_find_by_path(struct index_state *istate,\n \t\tcheck = attr_check_initl(\"diff\", NULL);\n \tif (!path)\n \t\treturn NULL;\n-\tgit_check_attr(istate, path, check);\n+\tgit_check_attr(istate, NULL, path, check);\n \n \tif (ATTR_TRUE(check->items[0].value))\n \t\treturn &driver_true;\ndiff --git a/ws.c b/ws.c\nindex 6e69877f25..eadbbe5667 100644\n--- a/ws.c\n+++ b/ws.c\n@@ -78,7 +78,7 @@ unsigned whitespace_rule(struct index_state *istate, const char *pathname)\n \tif (!attr_whitespace_rule)\n \t\tattr_whitespace_rule = attr_check_initl(\"whitespace\", NULL);\n \n-\tgit_check_attr(istate, pathname, attr_whitespace_rule);\n+\tgit_check_attr(istate, NULL, pathname, attr_whitespace_rule);\n \tvalue = attr_whitespace_rule->items[0].value;\n \tif (ATTR_TRUE(value)) {\n \t\t/* true (whitespace) */\n-- \n2.39.0\n\n"},{"id":"469443","messageId":"cc2e5f1e-8f53-ad83-1f5d-ae910ab60c53@dunelm.org.uk","threadId":"58990","inReplyTo":"a161dbdf8b70354716d0bfeccc707c242c7f33a1.1671630304.git.karthik.188@gmail.com","subject":"Re: [PATCH v4 2/2] attr: add flag `--revision` to work with revisions","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2022-12-21T20:57:33Z","receivedAt":"2022-12-21T20:57:41Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"Hi Karthik\n\nOn 21/12/2022 13:47, Karthik Nayak wrote:\n> The contents of the .gitattributes files may evolve over time, but \"git\n> check-attr\" always checks attributes against them in the working tree\n> and/or in the index. It may be beneficial to optionally allow the users\n> to check attributes taken from a commit other than HEAD against paths.\n> \n> Add a new flag `--revision` which will allow users to check the\n> attributes against a commit (actually any tree-ish would do). When the\n> user uses this flag, we go through the stack of .gitattributes files but\n> instead of checking the current working tree and/or in the index, we\n> check the blobs from the provided tree-ish object. This allows the\n> command to also be used in bare repositories.\n> \n> Since we use a tree-ish object, the user can pass \"--revision\n> HEAD:subdirectory\" and all the attributes will be looked up as if\n> subdirectory was the root directory of the repository.\n\nWe should be clear in the documentation and option help that --revision \ntakes a tree-ish (i.e. --revision=<tree-ish>). Maybe calling the option \n--tree would be clearer.\n\n> We cannot simply use the `<rev>:<path>` syntax without the `--revision`\n> flag, similar to how it is used in `git show` because any non-flag\n> parameter before `--` is treated as an attribute and any parameter after\n> `--` is treated as a pathname.\n> \n> The change involves creating a new function `read_attr_from_blob`, which\n> given the path reads the blob for the path against the provided revision and\n> parses the attributes line by line. This function is plugged into\n> `read_attr()` function wherein we go through the stack of attributes\n> files.\n\nThe implementation looks good apart from failing to bail out if it \ncannot parse the argument to --revision (perhaps we should add a test \nfor that). I've left a few suggestions below.\n\n> Signed-off-by: Karthik Nayak <karthik.188@gmail.com>\n> Signed-off-by: Toon Claes <toon@iotcl.com>\n> Co-authored-by: toon@iotcl.com\n\n> diff --git a/attr.c b/attr.c\n> index 42ad6de8c7..6c69e82080 100644\n> --- a/attr.c\n> +++ b/attr.c\n> @@ -11,8 +11,12 @@\n>   #include \"exec-cmd.h\"\n>   #include \"attr.h\"\n>   #include \"dir.h\"\n> +#include \"strbuf.h\"\n> +#include \"tree-walk.h\"\n\nThese new includes are not required.\n\n > diff --git a/attr.h b/attr.h\n > index 3fb40cced0..f4a2bedd68 100644\n > --- a/attr.h\n > +++ b/attr.h\n > @@ -1,6 +1,8 @@\n >  #ifndef ATTR_H\n >  #define ATTR_H\n >\n > +#include \"hash.h\"\n\nThis include is not required.\n\n> diff --git a/builtin/check-attr.c b/builtin/check-attr.c\n> index 0fef10eb6b..04640e0297 100644\n> --- a/builtin/check-attr.c\n> +++ b/builtin/check-attr.c\n> @@ -1,3 +1,4 @@\n> +#include \"repository.h\"\n\nThis include is not required. Also please add any new includes below \ncache.h as Junio has previously mentioned.\n\n>   #define USE_THE_INDEX_VARIABLE\n>   #include \"builtin.h\"\n>   #include \"cache.h\"\n> @@ -9,9 +10,10 @@\n>   static int all_attrs;\n>   static int cached_attrs;\n>   static int stdin_paths;\n> +static char *revision;\n>   static const char * const check_attr_usage[] = {\n> -N_(\"git check-attr [-a | --all | <attr>...] [--] <pathname>...\"),\n> -N_(\"git check-attr --stdin [-z] [-a | --all | <attr>...]\"),\n> +N_(\"git check-attr [--revision <revision>] [-a | --all | <attr>...] [--] <pathname>...\"),\n> +N_(\"git check-attr --stdin [-z] [--revision <revision>] [-a | --all | <attr>...]\"),\n>   NULL\n>   };\n>   \n> @@ -23,6 +25,7 @@ static const struct option check_attr_options[] = {\n>   \tOPT_BOOL(0 , \"stdin\", &stdin_paths, N_(\"read file names from stdin\")),\n>   \tOPT_BOOL('z', NULL, &nul_term_line,\n>   \t\t N_(\"terminate input and output records by a NUL character\")),\n> +\tOPT_STRING(0, \"revision\", &revision, N_(\"revision\"), N_(\"check attributes at this revision\")),\n>   \tOPT_END()\n>   };\n\n> +\tif (revision) {\n> +\t\ttree_oid = xmalloc(sizeof(struct object_id));\n\nI think we prefer 'var = xmalloc(sizeof(*var));' to avoid errors if the \ntype of var changes. This allocation does not appear to be freed \nanywhere. We could avoid the allocation by delcaring an automatic \nvariable above and setting tree_oid to point to it here.\n\n> +\t\tif (repo_get_oid_tree(the_repository, revision, tree_oid))\n> +\t\t\terror(\"%s: not a valid revision\", revision);\n\nWe should die() here rather than continuing with a bad tree.\n\n> +\t}\n> +\n>   \tif (stdin_paths)\n> -\t\tcheck_attr_stdin_paths(prefix, check, all_attrs);\n> +\t\tcheck_attr_stdin_paths(prefix, check, tree_oid, all_attrs);\n>   \telse {\n>   \t\tfor (i = filei; i < argc; i++)\n> -\t\t\tcheck_attr(prefix, check, all_attrs, argv[i]);\n> +\t\t\tcheck_attr(prefix, check, tree_oid, all_attrs, argv[i]);\n>   \t\tmaybe_flush_or_die(stdout, \"attribute to stdout\");\n>   \t}\n> [...]\n> diff --git a/t/t0003-attributes.sh b/t/t0003-attributes.sh\n> index b3aabb8aa3..6e6a909a46 100755\n> --- a/t/t0003-attributes.sh\n> +++ b/t/t0003-attributes.sh\n> @@ -25,7 +25,14 @@ attr_check_quote () {\n>   \tgit check-attr test -- \"$path\" >actual &&\n>   \techo \"\\\"$quoted_path\\\": test: $expect\" >expect &&\n>   \ttest_cmp expect actual\n> +}\n> +\n> +attr_check_revision () {\n> +\tpath=\"$1\" expect=\"$2\" revision=\"$3\" git_opts=\"$4\" &&\n\nIs that line valid posix shell? I know it works with local (which is not \nin posix) but is it allowed for global variables?\n\n> +\tgit $git_opts check-attr --revision $revision test -- \"$path\" >actual 2>err &&\n\nerr is never used. Should we be doing 'test_must_be_empty err'?\n\n> +\techo \"$path: test: $expect\" >expect &&\n> +\ttest_cmp expect actual\n>   }\n>   \n> [...]   \n> +test_expect_success 'setup branches' '\n> +\t(\n> +\t\techo \"f\ttest=f\" &&\n> +\t\techo \"a/i test=n\"\n> +\t) \n\nWe'd normally write this as\n\n\ttest_write_lines \"f test=f\" \"a/i test=n\" | git hash-object ...\n\nHowever I think it would be simpler to create the commit with something like\n\nmkdir -p foo/bar &&\ntest_commit --printf \"add .gitattributes\" foo/bar/.gitattributes \\\n\t\"t test=f\\na/i test=n\\n\" tag-1 &&\nrm -r foo/bar/.gitattributes\n\nwhich would also reduce the number of processes. Failing that a helper \nfunction to reduce the duplication would be a good idea.\n\n| git hash-object -w --stdin >id &&\n> +\tgit update-index --add --cacheinfo 100644,$(cat id),foo/bar/.gitattributes &&\n> +\tgit write-tree >id &&\n> +\ttree_id=$(cat id) &&\n\nFor future reference it is perfectly fine to write\n\ttree_oid=$(git write-tree) &&\n\nas we will still detect a non-zero exit code from git.\n\n> +\tgit commit-tree $tree_id -m \"random commit message\" >id &&\n> +\tcommit_id=$(cat id) &&\n> +\tgit update-ref refs/heads/branch1 $commit_id &&\n> +\n> +\t(\n> +\t\techo \"g test=g\" &&\n> +\t\techo \"a/i test=m\"\n> +\t) | git hash-object -w --stdin >id &&\n> +\tgit update-index --add --cacheinfo 100644,$(cat id),foo/bar/.gitattributes &&\n> +\tgit write-tree >id &&\n> +\ttree_id=$(cat id) &&\n> +\tgit commit-tree $tree_id -m \"random commit message\" >id &&\n> +\tcommit_id=$(cat id) &&\n> +\tgit update-ref refs/heads/branch2 $commit_id\n> +'\n> [...]\n>   test_expect_success 'setup bare' '\n>   \tgit clone --template= --bare . bare.git\n>   '\n> @@ -306,6 +347,27 @@ test_expect_success 'bare repository: check that .gitattribute is ignored' '\n>   \t)\n>   '\n>   \n> +test_expect_success 'bare repository: with --revision' '\n> +\t(\n> +\t\tcd bare.git &&\n\nYou could create a bare clone of the existing repo rather than having to \nrecreate the commits here.\n\nBest Wishes\n\nPhillip\n"},{"id":"469444","messageId":"CAPig+cTHJzHsUum0RnyCcqxj1qhxJR9EcKRjoShCznQ_oXYHOA@mail.gmail.com","threadId":"58990","inReplyTo":"cc2e5f1e-8f53-ad83-1f5d-ae910ab60c53@dunelm.org.uk","subject":"Re: [PATCH v4 2/2] attr: add flag `--revision` to work with revisions","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2022-12-21T21:13:14Z","receivedAt":"2022-12-21T21:13:31Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Wed, Dec 21, 2022 at 3:58 PM Phillip Wood <phillip.wood123@gmail.com> wrote:\n> On 21/12/2022 13:47, Karthik Nayak wrote:\n> > Since we use a tree-ish object, the user can pass \"--revision\n> > HEAD:subdirectory\" and all the attributes will be looked up as if\n> > subdirectory was the root directory of the repository.\n>\n> We should be clear in the documentation and option help that --revision\n> takes a tree-ish (i.e. --revision=<tree-ish>). Maybe calling the option\n> --tree would be clearer.\n\nIf that's the case, shouldn't this be called --source (or some\nvariation thereof) for consistency[1] with git-restore?\n\n[1]: https://lore.kernel.org/git/CAPig+cT_xz3m-3kkL_Scf0opNXkikU94kSF46VO8KHkNMWbGzg@mail.gmail.com/\n\n> > +attr_check_revision () {\n> > +     path=\"$1\" expect=\"$2\" revision=\"$3\" git_opts=\"$4\" &&\n>\n> Is that line valid posix shell? I know it works with local (which is not\n> in posix) but is it allowed for global variables?\n\nThis is fine as-is; it's POSIX.\n"},{"id":"469456","messageId":"1d40c799-e98a-2f44-9edd-795ade2b189f@dunelm.org.uk","threadId":"58990","inReplyTo":"CAPig+cTHJzHsUum0RnyCcqxj1qhxJR9EcKRjoShCznQ_oXYHOA@mail.gmail.com","subject":"Re: [PATCH v4 2/2] attr: add flag `--revision` to work with revisions","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2022-12-22T14:36:38Z","receivedAt":"2022-12-22T14:36:49Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"Hi Eric\n\nOn 21/12/2022 21:13, Eric Sunshine wrote:\n> On Wed, Dec 21, 2022 at 3:58 PM Phillip Wood <phillip.wood123@gmail.com> wrote:\n>> On 21/12/2022 13:47, Karthik Nayak wrote:\n>>> Since we use a tree-ish object, the user can pass \"--revision\n>>> HEAD:subdirectory\" and all the attributes will be looked up as if\n>>> subdirectory was the root directory of the repository.\n>>\n>> We should be clear in the documentation and option help that --revision\n>> takes a tree-ish (i.e. --revision=<tree-ish>). Maybe calling the option\n>> --tree would be clearer.\n> \n> If that's the case, shouldn't this be called --source (or some\n> variation thereof) for consistency[1] with git-restore?\n\nYes, that's a good suggestion, sorry I'd forgotten that you'd mentioned \nthat before.\n\n> [1]: https://lore.kernel.org/git/CAPig+cT_xz3m-3kkL_Scf0opNXkikU94kSF46VO8KHkNMWbGzg@mail.gmail.com/\n> \n>>> +attr_check_revision () {\n>>> +     path=\"$1\" expect=\"$2\" revision=\"$3\" git_opts=\"$4\" &&\n>>\n>> Is that line valid posix shell? I know it works with local (which is not\n>> in posix) but is it allowed for global variables?\n> \n> This is fine as-is; it's POSIX.\n\nThanks for clarifying that\n\nBest Wishes\n\nPhillip\n"},{"id":"469490","messageId":"CAOLa=ZS=b+6S2ym1P0mxqf9EH4hV9TH9gdYKpeOTDZT3SPREkg@mail.gmail.com","threadId":"58990","inReplyTo":"cc2e5f1e-8f53-ad83-1f5d-ae910ab60c53@dunelm.org.uk","subject":"Re: [PATCH v4 2/2] attr: add flag `--revision` to work with revisions","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2022-12-23T10:43:52Z","receivedAt":"2022-12-23T10:44:23Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Hello Phillip,\n\nOn Wed, Dec 21, 2022 at 9:57 PM Phillip Wood <phillip.wood123@gmail.com> wrote:\n> > Since we use a tree-ish object, the user can pass \"--revision\n> > HEAD:subdirectory\" and all the attributes will be looked up as if\n> > subdirectory was the root directory of the repository.\n>\n> We should be clear in the documentation and option help that --revision\n> takes a tree-ish (i.e. --revision=<tree-ish>). Maybe calling the option\n> --tree would be clearer.\n>\n\nI think we had a discussion around this a bit earlier in the series.\nhttps://lore.kernel.org/git/CAOLa=ZTSzUh2Ma_EMHHWcDunGyKMaUW9BaG=QdegtMqLd+69Wg@mail.gmail.com/\n\nMostly, that the idea of using `--revision` was taken from\n`git-svn(1)`. I'm good to make that change,\nwhat do you think would be best? `--source` or `-tree`? I like\n`--tree` better, but I'm open to suggestions.\n\n>\n> The implementation looks good apart from failing to bail out if it\n> cannot parse the argument to --revision (perhaps we should add a test\n> for that). I've left a few suggestions below.\n>\n\nThank you for the review!\n\n> > Signed-off-by: Karthik Nayak <karthik.188@gmail.com>\n> > Signed-off-by: Toon Claes <toon@iotcl.com>\n> > Co-authored-by: toon@iotcl.com\n>\n> > diff --git a/attr.c b/attr.c\n> > index 42ad6de8c7..6c69e82080 100644\n> > --- a/attr.c\n> > +++ b/attr.c\n> > @@ -11,8 +11,12 @@\n> >   #include \"exec-cmd.h\"\n> >   #include \"attr.h\"\n> >   #include \"dir.h\"\n> > +#include \"strbuf.h\"\n> > +#include \"tree-walk.h\"\n>\n> These new includes are not required.\n>\n\nWill remove!\n\n>  > diff --git a/attr.h b/attr.h\n>  > index 3fb40cced0..f4a2bedd68 100644\n>  > --- a/attr.h\n>  > +++ b/attr.h\n>  > @@ -1,6 +1,8 @@\n>  >  #ifndef ATTR_H\n>  >  #define ATTR_H\n>  >\n>  > +#include \"hash.h\"\n>\n> This include is not required.\n>\n\nWill remove!\n\n> > diff --git a/builtin/check-attr.c b/builtin/check-attr.c\n> > index 0fef10eb6b..04640e0297 100644\n> > --- a/builtin/check-attr.c\n> > +++ b/builtin/check-attr.c\n> > @@ -1,3 +1,4 @@\n> > +#include \"repository.h\"\n>\n> This include is not required. Also please add any new includes below\n> cache.h as Junio has previously mentioned.\n>\n\nUnderstood. I missed this.\n\n> > +     if (revision) {\n> > +             tree_oid = xmalloc(sizeof(struct object_id));\n>\n> I think we prefer 'var = xmalloc(sizeof(*var));' to avoid errors if the\n> type of var changes. This allocation does not appear to be freed\n> anywhere. We could avoid the allocation by delcaring an automatic\n> variable above and setting tree_oid to point to it here.\n>\n\nUnderstood, let me do that.\n\n> > +             if (repo_get_oid_tree(the_repository, revision, tree_oid))\n> > +                     error(\"%s: not a valid revision\", revision);\n>\n> We should die() here rather than continuing with a bad tree.\n>\n\nWill switch to `die(...)`\n\n> > +     git $git_opts check-attr --revision $revision test -- \"$path\" >actual 2>err &&\n>\n> err is never used. Should we be doing 'test_must_be_empty err'?\n>\n\nYeah this makes sense, let me add it in.\n\n> > +     echo \"$path: test: $expect\" >expect &&\n> > +     test_cmp expect actual\n> >   }\n> >\n> > [...]\n> > +test_expect_success 'setup branches' '\n> > +     (\n> > +             echo \"f test=f\" &&\n> > +             echo \"a/i test=n\"\n> > +     )\n>\n> We'd normally write this as\n>\n>         test_write_lines \"f test=f\" \"a/i test=n\" | git hash-object ...\n>\n> However I think it would be simpler to create the commit with something like\n>\n> mkdir -p foo/bar &&\n> test_commit --printf \"add .gitattributes\" foo/bar/.gitattributes \\\n>         \"t test=f\\na/i test=n\\n\" tag-1 &&\n> rm -r foo/bar/.gitattributes\n>\n> which would also reduce the number of processes. Failing that a helper\n> function to reduce the duplication would be a good idea.\n>\n\nThanks for this, my method was mostly put together with what I could make work,\nthis is much cleaner. We don't need to ` rm -r foo/bar/.gitattributes`\nas far as I see.\n\n> | git hash-object -w --stdin >id &&\n> > +     git update-index --add --cacheinfo 100644,$(cat id),foo/bar/.gitattributes &&\n> > +     git write-tree >id &&\n> > +     tree_id=$(cat id) &&\n>\n> For future reference it is perfectly fine to write\n>         tree_oid=$(git write-tree) &&\n>\n> as we will still detect a non-zero exit code from git.\n>\n\nNoted.\n\n> > +     git commit-tree $tree_id -m \"random commit message\" >id &&\n> > +     commit_id=$(cat id) &&\n> > +     git update-ref refs/heads/branch1 $commit_id &&\n> > +\n> > +     (\n> > +             echo \"g test=g\" &&\n> > +             echo \"a/i test=m\"\n> > +     ) | git hash-object -w --stdin >id &&\n> > +     git update-index --add --cacheinfo 100644,$(cat id),foo/bar/.gitattributes &&\n> > +     git write-tree >id &&\n> > +     tree_id=$(cat id) &&\n> > +     git commit-tree $tree_id -m \"random commit message\" >id &&\n> > +     commit_id=$(cat id) &&\n> > +     git update-ref refs/heads/branch2 $commit_id\n> > +'\n> > [...]\n> >   test_expect_success 'setup bare' '\n> >       git clone --template= --bare . bare.git\n> >   '\n> > @@ -306,6 +347,27 @@ test_expect_success 'bare repository: check that .gitattribute is ignored' '\n> >       )\n> >   '\n> >\n> > +test_expect_success 'bare repository: with --revision' '\n> > +     (\n> > +             cd bare.git &&\n>\n> You could create a bare clone of the existing repo rather than having to\n> recreate the commits here.\n>\n\nMakes sense, let me simplify this too.\n\n\n-- \n- Karthik\n"},{"id":"469491","messageId":"CAOLa=ZRb65r8ifhnNqGv0Mhq1=R-71X5NYgjBLiBkEBhn+nT=g@mail.gmail.com","threadId":"58990","inReplyTo":"CAPig+cTHJzHsUum0RnyCcqxj1qhxJR9EcKRjoShCznQ_oXYHOA@mail.gmail.com","subject":"Re: [PATCH v4 2/2] attr: add flag `--revision` to work with revisions","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2022-12-23T10:44:59Z","receivedAt":"2022-12-23T10:45:30Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"On Wed, Dec 21, 2022 at 10:13 PM Eric Sunshine <sunshine@sunshineco.com> wrote:\n>\n> On Wed, Dec 21, 2022 at 3:58 PM Phillip Wood <phillip.wood123@gmail.com> wrote:\n> > On 21/12/2022 13:47, Karthik Nayak wrote:\n> > > Since we use a tree-ish object, the user can pass \"--revision\n> > > HEAD:subdirectory\" and all the attributes will be looked up as if\n> > > subdirectory was the root directory of the repository.\n> >\n> > We should be clear in the documentation and option help that --revision\n> > takes a tree-ish (i.e. --revision=<tree-ish>). Maybe calling the option\n> > --tree would be clearer.\n>\n> If that's the case, shouldn't this be called --source (or some\n> variation thereof) for consistency[1] with git-restore?\n>\n> [1]: https://lore.kernel.org/git/CAPig+cT_xz3m-3kkL_Scf0opNXkikU94kSF46VO8KHkNMWbGzg@mail.gmail.com/\n>\n\nLet me rename it to source, and we can discuss on the next version if\nthe name suits or not.\n\n-- \n- Karthik\n"}]}