Skip to contentSkip to conversation

check-attr: add support to work with revisions

PATCH Superseded Karthik Nayak 3 participants 8 messages Dec 21, 2022 – Dec 23, 2022 1 message not in the archive Documentation

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): https://lore.kernel.org/git/[email protected]/

#1 lore raw

v4 cover check-attr: add support to work with revisions

v1: https://lore.kernel.org/git/[email protected]/
v2: https://lore.kernel.org/git/CAOLa=ZSsFGBw3ta1jWN8cmUch2ca=zTEjp1xMA6Linafx9W53g@mail.gmail.com/T/#t
v3: https://lore.kernel.org/git/[email protected]/

Given a pathname, git-check-attr(1) will list the attributes which apply to that pathname by reading all relevant gitattributes files. Currently there is no way to specify a revision to read the gitattributes from.

This is specifically useful in bare repositories wherein the gitattributes are only present in the git working tree but not available directly on the filesystem.

This series aims to add a new flag --revision to git-check-attr(1) which allows us to read gitattributes from the specified revision.

Changes since version 3:
- Dropped the -r shorcut for the flag
- Fixed tests failing by initializing the tree_oid to NULL
- Incorporated some changes around the commit message

Range-diff against v3:

1:  6224754179 = 1:  6224754179 t0003: move setup for `--all` into new block
2:  d8f8b3c36c ! 2:  a161dbdf8b attr: add flag `-r|--revisions` to work with revisions
    @@ Metadata
     Author: Karthik Nayak <[email protected]>
     
      ## Commit message ##
    -    attr: add flag `-r|--revisions` to work with revisions
    +    attr: add flag `--revision` to work with revisions
     
         The contents of the .gitattributes files may evolve over time, but "git
         check-attr" always checks attributes against them in the working tree
         and/or in the index. It may be beneficial to optionally allow the users
    -    to check attributes against paths from older commits.
    +    to check attributes taken from a commit other than HEAD against paths.
     
    -    Add a new flag `--revision`/`-r` which will allow users to check the
    -    attributes against a tree-ish revision. When the user uses this flag, we
    -    go through the stack of .gitattributes files but instead of checking the
    -    current working tree and/or in the index, we check the blobs from the
    -    provided tree-ish object. This allows the command to also be used in
    -    bare repositories.
    +    Add a new flag `--revision` which will allow users to check the
    +    attributes against a commit (actually any tree-ish would do). When the
    +    user uses this flag, we go through the stack of .gitattributes files but
    +    instead of checking the current working tree and/or in the index, we
    +    check the blobs from the provided tree-ish object. This allows the
    +    command to also be used in bare repositories.
     
    -    Since we use a tree-ish object, the user can pass "-r HEAD:subdirectory"
    -    and all the attributes will be looked up as if subdirectory was the root
    -    directory of the repository.
    +    Since we use a tree-ish object, the user can pass "--revision
    +    HEAD:subdirectory" and all the attributes will be looked up as if
    +    subdirectory was the root directory of the repository.
     
    -    We cannot use the `<rev>:<path>` syntax like the one used in `git show`
    -    because any non-flag parameter before `--` is treated as an attribute
    -    and any parameter after `--` is treated as a pathname.
    +    We cannot simply use the `<rev>:<path>` syntax without the `--revision`
    +    flag, similar to how it is used in `git show` because any non-flag
    +    parameter before `--` is treated as an attribute and any parameter after
    +    `--` is treated as a pathname.
     
         The change involves creating a new function `read_attr_from_blob`, which
         given the path reads the blob for the path against the provided revision and
    @@ Commit message
         files.
     
         Signed-off-by: Karthik Nayak <[email protected]>
    +    Signed-off-by: Toon Claes <[email protected]>
         Co-authored-by: [email protected]
     
      ## Documentation/git-check-attr.txt ##
    @@ Documentation/git-check-attr.txt: git-check-attr - Display gitattributes informa
      [verse]
     -'git check-attr' [-a | --all | <attr>...] [--] <pathname>...
     -'git check-attr' --stdin [-z] [-a | --all | <attr>...]
    -+'git check-attr' [-r <revision>] [-a | --all | <attr>...] [--] <pathname>...
    -+'git check-attr' --stdin [-z] [-r <revision>] [-a | --all | <attr>...]
    ++'git check-attr' [--revision <revision>] [-a | --all | <attr>...] [--] <pathname>...
    ++'git check-attr' --stdin [-z] [--revision <revision>] [-a | --all | <attr>...]
      
      DESCRIPTION
      -----------
    @@ Documentation/git-check-attr.txt: OPTIONS
      	If `--stdin` is also given, input paths are separated
      	with a NUL character instead of a linefeed character.
      
    -+--r <revision>::
     +--revision=<revision>::
    -+	Check attributes against the specified tree-ish revision. All the
    -+	attributes will be checked against the provided revision. Paths provided
    -+	as part of the revision will be treated as the root directory.
    ++	Check attributes against the specified commit. All the attributes will
    ++	be checked against the provided revision. Paths provided as part of the
    ++	revision will be treated as the root directory.
     +
      \--::
      	Interpret all preceding arguments as attributes and all following
    @@ attr.c
      #include "exec-cmd.h"
      #include "attr.h"
      #include "dir.h"
    -+#include "git-compat-util.h"
     +#include "strbuf.h"
     +#include "tree-walk.h"
      #include "utf8.h"
    @@ builtin/check-attr.c
      static const char * const check_attr_usage[] = {
     -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>...]"),
      NULL
      };
      
    @@ builtin/check-attr.c: static const struct option check_attr_options[] = {
      	OPT_BOOL(0 , "stdin", &stdin_paths, N_("read file names from stdin")),
      	OPT_BOOL('z', NULL, &nul_term_line,
      		 N_("terminate input and output records by a NUL character")),
    -+	OPT_STRING('r', "revision", &revision, N_("revision"), N_("check attributes at this revision")),
    ++	OPT_STRING(0, "revision", &revision, N_("revision"), N_("check attributes at this revision")),
      	OPT_END()
      };
      
    @@ builtin/check-attr.c: static NORETURN void error_with_usage(const char *msg)
      int cmd_check_attr(int argc, const char **argv, const char *prefix)
      {
      	struct attr_check *check;
    -+	struct object_id tree_oid;
    ++	struct object_id *tree_oid = NULL;
      	int cnt, i, doubledash, filei;
      
      	if (!is_bare_repository())
    @@ builtin/check-attr.c: int cmd_check_attr(int argc, const char **argv, const char
      		}
      	}
      
    -+	if (revision)
    -+		if (repo_get_oid_tree(the_repository, revision, &tree_oid))
    ++	if (revision) {
    ++		tree_oid = xmalloc(sizeof(struct object_id));
    ++
    ++		if (repo_get_oid_tree(the_repository, revision, tree_oid))
     +			error("%s: not a valid revision", revision);
    ++	}
     +
      	if (stdin_paths)
     -		check_attr_stdin_paths(prefix, check, all_attrs);
    -+		check_attr_stdin_paths(prefix, check, &tree_oid, all_attrs);
    ++		check_attr_stdin_paths(prefix, check, tree_oid, all_attrs);
      	else {
      		for (i = filei; i < argc; i++)
     -			check_attr(prefix, check, all_attrs, argv[i]);
    -+			check_attr(prefix, check, &tree_oid, all_attrs, argv[i]);
    ++			check_attr(prefix, check, tree_oid, all_attrs, argv[i]);
      		maybe_flush_or_die(stdout, "attribute to stdout");
      	}
      
    @@ t/t0003-attributes.sh: attr_check_quote () {
     +attr_check_revision () {
     +	path="$1" expect="$2" revision="$3" git_opts="$4" &&
      
    -+	git $git_opts check-attr -r $revision test -- "$path" >actual 2>err &&
    ++	git $git_opts check-attr --revision $revision test -- "$path" >actual 2>err &&
     +	echo "$path: test: $expect" >expect &&
     +	test_cmp expect actual
      }
    @@ t/t0003-attributes.sh: test_expect_success 'setup' '
      	test_must_fail git check-attr test &&
      	test_must_fail git check-attr test -- &&
      	test_must_fail git check-attr -- f &&
    -+	test_must_fail git check-attr -r &&
    -+	test_must_fail git check-attr -r not-a-valid-ref &&
    ++	test_must_fail git check-attr --revision &&
    ++	test_must_fail git check-attr --revision not-a-valid-ref &&
      	echo "f" | test_must_fail git check-attr --stdin &&
      	echo "f" | test_must_fail git check-attr --stdin -- f &&
      	echo "f" | test_must_fail git check-attr --stdin test -- f &&

Karthik Nayak (2):
t0003: move setup for --all into new block
attr: add flag --revision to work with revisions

 Documentation/git-check-attr.txt |  9 ++-
 archive.c                        |  2 +-
 attr.c                           | 99 +++++++++++++++++++++++---------
 attr.h                           |  7 ++-
 builtin/check-attr.c             | 36 +++++++-----
 builtin/pack-objects.c           |  2 +-
 convert.c                        |  2 +-
 ll-merge.c                       |  4 +-
 pathspec.c                       |  2 +-
 t/t0003-attributes.sh            | 71 ++++++++++++++++++++++-
 userdiff.c                       |  2 +-
 ws.c                             |  2 +-
 12 files changed, 183 insertions(+), 55 deletions(-)
Signature · 1 line
2.39.0
#2 lore raw

PATCH v4 1/2 t0003: move setup for `--all` into new block

There is some setup code which is used by multiple tests being setup in attribute test: --all option. This means when we run "sh ./t0003-attributes.sh --run=setup,<num>" there is a chance of failing since we missed this setup block.

So to ensure that setups are independent of test logic, move this to a new setup block.

---
 t/t0003-attributes.sh | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
t/t0003-attributes.sh +5 -2
index f7ee2f2ff0..b3aabb8aa3 100755
@@ -203,9 +203,12 @@ test_expect_success 'attribute test: read paths from stdin' '
 	test_cmp expect actual
 '
 
-test_expect_success 'attribute test: --all option' '
+test_expect_success 'setup --all option' '
 	grep -v unspecified <expect-all | sort >specified-all &&
-	sed -e "s/:.*//" <expect-all | uniq >stdin-all &&
+	sed -e "s/:.*//" <expect-all | uniq >stdin-all
+'
+
+test_expect_success 'attribute test: --all option' '
 	git check-attr --stdin --all <stdin-all >tmp &&
 	sort tmp >actual &&
 	test_cmp specified-all actual
Signature · 1 line
2.39.0
#3 lore raw

PATCH v4 2/2 attr: add flag `--revision` to work with revisions

In reply to Karthik Nayak

The contents of the .gitattributes files may evolve over time, but "git check-attr" always checks attributes against them in the working tree and/or in the index. It may be beneficial to optionally allow the users to check attributes taken from a commit other than HEAD against paths.

Add a new flag --revision which will allow users to check the attributes against a commit (actually any tree-ish would do). When the user uses this flag, we go through the stack of .gitattributes files but instead of checking the current working tree and/or in the index, we check the blobs from the provided tree-ish object. This allows the command to also be used in bare repositories.

Since we use a tree-ish object, the user can pass "--revision HEAD:subdirectory" and all the attributes will be looked up as if subdirectory was the root directory of the repository.

We cannot simply use the <rev>:<path> syntax without the --revision flag, similar to how it is used in git show because any non-flag parameter before -- is treated as an attribute and any parameter after -- is treated as a pathname.

The change involves creating a new function read_attr_from_blob, which given the path reads the blob for the path against the provided revision and parses the attributes line by line. This function is plugged into read_attr() function wherein we go through the stack of attributes files.

---
 Documentation/git-check-attr.txt |  9 ++-
 archive.c                        |  2 +-
 attr.c                           | 99 +++++++++++++++++++++++---------
 attr.h                           |  7 ++-
 builtin/check-attr.c             | 36 +++++++-----
 builtin/pack-objects.c           |  2 +-
 convert.c                        |  2 +-
 ll-merge.c                       |  4 +-
 pathspec.c                       |  2 +-
 t/t0003-attributes.sh            | 64 ++++++++++++++++++++-
 userdiff.c                       |  2 +-
 ws.c                             |  2 +-
 12 files changed, 178 insertions(+), 53 deletions(-)
Documentation/git-check-attr.txt +7 -2
index 84f41a8e82..174019c71d 100644
@@ -9,8 +9,8 @@ git-check-attr - Display gitattributes information
 SYNOPSIS
 --------
 [verse]
-'git check-attr' [-a | --all | <attr>...] [--] <pathname>...
-'git check-attr' --stdin [-z] [-a | --all | <attr>...]
+'git check-attr' [--revision <revision>] [-a | --all | <attr>...] [--] <pathname>...
+'git check-attr' --stdin [-z] [--revision <revision>] [-a | --all | <attr>...]
 
 DESCRIPTION
 -----------
@@ -36,6 +36,11 @@ OPTIONS
 	If `--stdin` is also given, input paths are separated
 	with a NUL character instead of a linefeed character.
 
+--revision=<revision>::
+	Check attributes against the specified commit. All the attributes will
+	be checked against the provided revision. Paths provided as part of the
+	revision will be treated as the root directory.
+
 \--::
 	Interpret all preceding arguments as attributes and all following
 	arguments as path names.
archive.c +1 -1
index 941495f5d7..81ff76fce9 100644
@@ -120,7 +120,7 @@ static const struct attr_check *get_archive_attrs(struct index_state *istate,
 	static struct attr_check *check;
 	if (!check)
 		check = attr_check_initl("export-ignore", "export-subst", NULL);
-	git_check_attr(istate, path, check);
+	git_check_attr(istate, NULL, path, check);
 	return check;
 }
 
attr.c +72 -27

195 lines not on this page · raw message

attr.h +5 -2
index 3fb40cced0..f4a2bedd68 100644
@@ -1,6 +1,8 @@
 #ifndef ATTR_H
 #define ATTR_H
 
+#include "hash.h"
+
 /**
  * gitattributes mechanism gives a uniform way to associate various attributes
  * to set of paths.
@@ -190,13 +192,14 @@ void attr_check_free(struct attr_check *check);
 const char *git_attr_name(const struct git_attr *);
 
 void git_check_attr(struct index_state *istate,
-		    const char *path, struct attr_check *check);
+		    const struct object_id *tree_oid, const char *path,
+		    struct attr_check *check);
 
 /*
  * Retrieve all attributes that apply to the specified path.
  * check holds the attributes and their values.
  */
-void git_all_attrs(struct index_state *istate,
+void git_all_attrs(struct index_state *istate, const struct object_id *tree_oid,
 		   const char *path, struct attr_check *check);
 
 enum git_attr_direction {
builtin/check-attr.c +23 -13
index 0fef10eb6b..04640e0297 100644
@@ -1,3 +1,4 @@
+#include "repository.h"
 #define USE_THE_INDEX_VARIABLE
 #include "builtin.h"
 #include "cache.h"
@@ -9,9 +10,10 @@
 static int all_attrs;
 static int cached_attrs;
 static int stdin_paths;
+static char *revision;
 static const char * const check_attr_usage[] = {
-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>...]"),
 NULL
 };
 
@@ -23,6 +25,7 @@ static const struct option check_attr_options[] = {
 	OPT_BOOL(0 , "stdin", &stdin_paths, N_("read file names from stdin")),
 	OPT_BOOL('z', NULL, &nul_term_line,
 		 N_("terminate input and output records by a NUL character")),
+	OPT_STRING(0, "revision", &revision, N_("revision"), N_("check attributes at this revision")),
 	OPT_END()
 };
 
@@ -55,27 +58,26 @@ static void output_attr(struct attr_check *check, const char *file)
 	}
 }
 
-static void check_attr(const char *prefix,
-		       struct attr_check *check,
-		       int collect_all,
+static void check_attr(const char *prefix, struct attr_check *check,
+		       const struct object_id *tree_oid, int collect_all,
 		       const char *file)
+
 {
 	char *full_path =
 		prefix_path(prefix, prefix ? strlen(prefix) : 0, file);
 
 	if (collect_all) {
-		git_all_attrs(&the_index, full_path, check);
+		git_all_attrs(&the_index, tree_oid, full_path, check);
 	} else {
-		git_check_attr(&the_index, full_path, check);
+		git_check_attr(&the_index, tree_oid, full_path, check);
 	}
 	output_attr(check, file);
 
 	free(full_path);
 }
 
-static void check_attr_stdin_paths(const char *prefix,
-				   struct attr_check *check,
-				   int collect_all)
+static void check_attr_stdin_paths(const char *prefix, struct attr_check *check,
+				   const struct object_id *tree_oid, int collect_all)
 {
 	struct strbuf buf = STRBUF_INIT;
 	struct strbuf unquoted = STRBUF_INIT;
@@ -89,7 +91,7 @@ static void check_attr_stdin_paths(const char *prefix,
 				die("line is badly quoted");
 			strbuf_swap(&buf, &unquoted);
 		}
-		check_attr(prefix, check, collect_all, buf.buf);
+		check_attr(prefix, check, tree_oid, collect_all, buf.buf);
 		maybe_flush_or_die(stdout, "attribute to stdout");
 	}
 	strbuf_release(&buf);
@@ -105,6 +107,7 @@ static NORETURN void error_with_usage(const char *msg)
 int cmd_check_attr(int argc, const char **argv, const char *prefix)
 {
 	struct attr_check *check;
+	struct object_id *tree_oid = NULL;
 	int cnt, i, doubledash, filei;
 
 	if (!is_bare_repository())
@@ -176,11 +179,18 @@ int cmd_check_attr(int argc, const char **argv, const char *prefix)
 		}
 	}
 
+	if (revision) {
+		tree_oid = xmalloc(sizeof(struct object_id));
+
+		if (repo_get_oid_tree(the_repository, revision, tree_oid))
+			error("%s: not a valid revision", revision);
+	}
+
 	if (stdin_paths)
-		check_attr_stdin_paths(prefix, check, all_attrs);
+		check_attr_stdin_paths(prefix, check, tree_oid, all_attrs);
 	else {
 		for (i = filei; i < argc; i++)
-			check_attr(prefix, check, all_attrs, argv[i]);
+			check_attr(prefix, check, tree_oid, all_attrs, argv[i]);
 		maybe_flush_or_die(stdout, "attribute to stdout");
 	}
 
builtin/pack-objects.c +1 -1
index 573d0b20b7..89535cfa6a 100644
@@ -1318,7 +1318,7 @@ static int no_try_delta(const char *path)
 
 	if (!check)
 		check = attr_check_initl("delta", NULL);
-	git_check_attr(the_repository->index, path, check);
+	git_check_attr(the_repository->index, NULL, path, check);
 	if (ATTR_FALSE(check->items[0].value))
 		return 1;
 	return 0;
convert.c +1 -1
index 9b67649032..a54d1690c0 100644
@@ -1308,7 +1308,7 @@ void convert_attrs(struct index_state *istate,
 		git_config(read_convert_config, NULL);
 	}
 
-	git_check_attr(istate, path, check);
+	git_check_attr(istate, NULL, path, check);
 	ccheck = check->items;
 	ca->crlf_action = git_path_check_crlf(ccheck + 4);
 	if (ca->crlf_action == CRLF_UNDEFINED)
ll-merge.c +2 -2
index 22a603e8af..130d26501c 100644
@@ -391,7 +391,7 @@ enum ll_merge_result ll_merge(mmbuffer_t *result_buf,
 		normalize_file(theirs, path, istate);
 	}
 
-	git_check_attr(istate, path, check);
+	git_check_attr(istate, NULL, path, check);
 	ll_driver_name = check->items[0].value;
 	if (check->items[1].value) {
 		marker_size = atoi(check->items[1].value);
@@ -419,7 +419,7 @@ int ll_merge_marker_size(struct index_state *istate, const char *path)
 
 	if (!check)
 		check = attr_check_initl("conflict-marker-size", NULL);
-	git_check_attr(istate, path, check);
+	git_check_attr(istate, NULL, path, check);
 	if (check->items[0].value) {
 		marker_size = atoi(check->items[0].value);
 		if (marker_size <= 0)
pathspec.c +1 -1
index 46e77a85fe..48dec2c709 100644
@@ -732,7 +732,7 @@ int match_pathspec_attrs(struct index_state *istate,
 	if (name[namelen])
 		name = to_free = xmemdupz(name, namelen);
 
-	git_check_attr(istate, name, item->attr_check);
+	git_check_attr(istate, NULL, name, item->attr_check);
 
 	free(to_free);
 
t/t0003-attributes.sh +63 -1

101 lines not on this page · raw message

userdiff.c +1 -1
index 151d9a5278..b66f090a0b 100644
@@ -412,7 +412,7 @@ struct userdiff_driver *userdiff_find_by_path(struct index_state *istate,
 		check = attr_check_initl("diff", NULL);
 	if (!path)
 		return NULL;
-	git_check_attr(istate, path, check);
+	git_check_attr(istate, NULL, path, check);
 
 	if (ATTR_TRUE(check->items[0].value))
 		return &driver_true;
ws.c +1 -1
index 6e69877f25..eadbbe5667 100644
@@ -78,7 +78,7 @@ unsigned whitespace_rule(struct index_state *istate, const char *pathname)
 	if (!attr_whitespace_rule)
 		attr_whitespace_rule = attr_check_initl("whitespace", NULL);
 
-	git_check_attr(istate, pathname, attr_whitespace_rule);
+	git_check_attr(istate, NULL, pathname, attr_whitespace_rule);
 	value = attr_whitespace_rule->items[0].value;
 	if (ATTR_TRUE(value)) {
 		/* true (whitespace) */
Signature · 1 line
2.39.0
#4 lore raw

Re: [PATCH v4 2/2] attr: add flag `--revision` to work with revisions

Hi Karthik

Quoted 12 lines · Karthik Nayak

The contents of the .gitattributes files may evolve over time, but "git check-attr" always checks attributes against them in the working tree and/or in the index. It may be beneficial to optionally allow the users to check attributes taken from a commit other than HEAD against paths.

Add a new flag --revision which will allow users to check the attributes against a commit (actually any tree-ish would do). When the user uses this flag, we go through the stack of .gitattributes files but instead of checking the current working tree and/or in the index, we check the blobs from the provided tree-ish object. This allows the command to also be used in bare repositories.

Quoted 3 lines

Since we use a tree-ish object, the user can pass "--revision HEAD:subdirectory" and all the attributes will be looked up as if subdirectory was the root directory of the repository.

We should be clear in the documentation and option help that --revision takes a tree-ish (i.e. --revision=<tree-ish>). Maybe calling the option --tree would be clearer.

Quoted 10 lines

We cannot simply use the <rev>:<path> syntax without the --revision flag, similar to how it is used in git show because any non-flag parameter before -- is treated as an attribute and any parameter after -- is treated as a pathname.

The change involves creating a new function read_attr_from_blob, which given the path reads the blob for the path against the provided revision and parses the attributes line by line. This function is plugged into read_attr() function wherein we go through the stack of attributes files.

The implementation looks good apart from failing to bail out if it cannot parse the argument to --revision (perhaps we should add a test for that). I've left a few suggestions below.

Quoted 9 lines
attr.c +0 -0
index 42ad6de8c7..6c69e82080 100644

attr.c line 15

Quoted 5 lines
attr.c +2 -0
@@ -11,8 +11,12 @@
  #include "exec-cmd.h"
  #include "attr.h"
  #include "dir.h"
+#include "strbuf.h"
+#include "tree-walk.h"

These new includes are not required.

attr.h line 4

Quoted 9 lines
attr.h +1 -0
index 3fb40cced0..f4a2bedd68 100644
@@ -1,6 +1,8 @@
 #ifndef ATTR_H
 #define ATTR_H
 
+#include "hash.h"

This include is not required.

builtin/check-attr.c line 1

Quoted 6 lines
builtin/check-attr.c +1 -0
index 0fef10eb6b..04640e0297 100644
@@ -1,3 +1,4 @@
+#include "repository.h"

This include is not required. Also please add any new includes below cache.h as Junio has previously mentioned.

Quoted 17 lines
builtin/check-attr.c +3 -2
  #define USE_THE_INDEX_VARIABLE
  #include "builtin.h"
  #include "cache.h"
@@ -9,9 +10,10 @@
  static int all_attrs;
  static int cached_attrs;
  static int stdin_paths;
+static char *revision;
  static const char * const check_attr_usage[] = {
-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>...]"),
  NULL
  };
 

builtin/check-attr.c static const struct option check_attr_options[] = {

Quoted 9 lines
builtin/check-attr.c +3 -0
@@ -23,6 +25,7 @@ static const struct option check_attr_options[] = {
  	OPT_BOOL(0 , "stdin", &stdin_paths, N_("read file names from stdin")),
  	OPT_BOOL('z', NULL, &nul_term_line,
  		 N_("terminate input and output records by a NUL character")),
+	OPT_STRING(0, "revision", &revision, N_("revision"), N_("check attributes at this revision")),
  	OPT_END()
  };
 
+	if (revision) {
+		tree_oid = xmalloc(sizeof(struct object_id));

I think we prefer 'var = xmalloc(sizeof(*var));' to avoid errors if the type of var changes. This allocation does not appear to be freed anywhere. We could avoid the allocation by delcaring an automatic variable above and setting tree_oid to point to it here.

builtin/check-attr.c int cmd_check_attr(int argc, const char **argv, const char *prefix) · line 186

Quoted 2 lines
builtin/check-attr.c +2 -0
+		if (repo_get_oid_tree(the_repository, revision, tree_oid))
+			error("%s: not a valid revision", revision);

We should die() here rather than continuing with a bad tree.

Quoted 17 lines
builtin/check-attr.c +4 -2
+	}
+
  	if (stdin_paths)
-		check_attr_stdin_paths(prefix, check, all_attrs);
+		check_attr_stdin_paths(prefix, check, tree_oid, all_attrs);
  	else {
  		for (i = filei; i < argc; i++)
-			check_attr(prefix, check, all_attrs, argv[i]);
+			check_attr(prefix, check, tree_oid, all_attrs, argv[i]);
  		maybe_flush_or_die(stdout, "attribute to stdout");
  	}
[...]
t/t0003-attributes.sh +0 -0
index b3aabb8aa3..6e6a909a46 100755

t/t0003-attributes.sh attr_check_quote () { · line 31

Quoted 7 lines
t/t0003-attributes.sh +4 -0
@@ -25,7 +25,14 @@ attr_check_quote () {
  	git check-attr test -- "$path" >actual &&
  	echo "\"$quoted_path\": test: $expect" >expect &&
  	test_cmp expect actual
+}
+
+attr_check_revision () {
+	path="$1" expect="$2" revision="$3" git_opts="$4" &&

Is that line valid posix shell? I know it works with local (which is not in posix) but is it allowed for global variables?

t/t0003-attributes.sh attr_check_quote () { · line 33

Quoted 1 line
t/t0003-attributes.sh +1 -0
+	git $git_opts check-attr --revision $revision test -- "$path" >actual 2>err &&

err is never used. Should we be doing 'test_must_be_empty err'?

t/t0003-attributes.sh test_expect_success 'setup' '

Quoted 10 lines
t/t0003-attributes.sh +7 -0
+	echo "$path: test: $expect" >expect &&
+	test_cmp expect actual
  }
 
[...]
+test_expect_success 'setup branches' '
+	(
+		echo "f	test=f" &&
+		echo "a/i test=n"
+	) 

We'd normally write this as

test_write_lines "f test=f" "a/i test=n" | git hash-object ...

However I think it would be simpler to create the commit with something like

mkdir -p foo/bar &&

test_commit --printf "add .gitattributes" foo/bar/.gitattributes \
	"t test=f\na/i test=n\n" tag-1 &&

rm -r foo/bar/.gitattributes

which would also reduce the number of processes. Failing that a helper function to reduce the duplication would be a good idea.

| git hash-object -w --stdin >id &&

t/t0003-attributes.sh test_expect_success 'setup' ' · line 96

Quoted 3 lines
t/t0003-attributes.sh +3 -0
+	git update-index --add --cacheinfo 100644,$(cat id),foo/bar/.gitattributes &&
+	git write-tree >id &&
+	tree_id=$(cat id) &&

For future reference it is perfectly fine to write

	tree_oid=$(git write-tree) &&

as we will still detect a non-zero exit code from git.

Quoted 20 lines
t/t0003-attributes.sh +15 -0
+	git commit-tree $tree_id -m "random commit message" >id &&
+	commit_id=$(cat id) &&
+	git update-ref refs/heads/branch1 $commit_id &&
+
+	(
+		echo "g test=g" &&
+		echo "a/i test=m"
+	) | git hash-object -w --stdin >id &&
+	git update-index --add --cacheinfo 100644,$(cat id),foo/bar/.gitattributes &&
+	git write-tree >id &&
+	tree_id=$(cat id) &&
+	git commit-tree $tree_id -m "random commit message" >id &&
+	commit_id=$(cat id) &&
+	git update-ref refs/heads/branch2 $commit_id
+'
[...]
  test_expect_success 'setup bare' '
  	git clone --template= --bare . bare.git
  '

t/t0003-attributes.sh test_expect_success 'bare repository: check that .gitattribute is ignored' ' · line 352

Quoted 6 lines
t/t0003-attributes.sh +3 -0
@@ -306,6 +347,27 @@ test_expect_success 'bare repository: check that .gitattribute is ignored' '
  	)
  '
 
+test_expect_success 'bare repository: with --revision' '
+	(
+		cd bare.git &&

You could create a bare clone of the existing repo rather than having to recreate the commits here.

Best Wishes

Phillip

#5 lore raw

Re: [PATCH v4 2/2] attr: add flag `--revision` to work with revisions

Quoted 8 lines · Phillip Wood
Quoted 3 lines · Karthik Nayak

Since we use a tree-ish object, the user can pass "--revision HEAD:subdirectory" and all the attributes will be looked up as if subdirectory was the root directory of the repository.

We should be clear in the documentation and option help that --revision takes a tree-ish (i.e. --revision=<tree-ish>). Maybe calling the option --tree would be clearer.

If that's the case, shouldn't this be called --source (or some variation thereof) for consistency[1] with git-restore?

[1]: https://lore.kernel.org/git/CAPig+cT_xz3m-3kkL_Scf0opNXkikU94kSF46VO8KHkNMWbGzg@mail.gmail.com/

Quoted 5 lines

t/t0003-attributes.sh attr_check_quote () { · line 31

Quoted 2 lines
t/t0003-attributes.sh +2 -0
+attr_check_revision () {
+     path="$1" expect="$2" revision="$3" git_opts="$4" &&

Is that line valid posix shell? I know it works with local (which is not in posix) but is it allowed for global variables?

This is fine as-is; it's POSIX.

#6 lore raw

Re: [PATCH v4 2/2] attr: add flag `--revision` to work with revisions

Hi Eric

Quoted 6 lines · Eric Sunshine
Quoted 5 lines · Phillip Wood
Quoted 3 lines · Karthik Nayak

Since we use a tree-ish object, the user can pass "--revision HEAD:subdirectory" and all the attributes will be looked up as if subdirectory was the root directory of the repository.

Quoted 6 lines
Quoted 3 lines

We should be clear in the documentation and option help that --revision takes a tree-ish (i.e. --revision=<tree-ish>). Maybe calling the option --tree would be clearer.

If that's the case, shouldn't this be called --source (or some variation thereof) for consistency[1] with git-restore?

Yes, that's a good suggestion, sorry I'd forgotten that you'd mentioned that before.

Quoted 9 lines

[1]: https://lore.kernel.org/git/CAPig+cT_xz3m-3kkL_Scf0opNXkikU94kSF46VO8KHkNMWbGzg@mail.gmail.com/

Quoted 5 lines

t/t0003-attributes.sh attr_check_quote () { · line 31

Quoted 2 lines
t/t0003-attributes.sh +2 -0
+attr_check_revision () {
+     path="$1" expect="$2" revision="$3" git_opts="$4" &&

Is that line valid posix shell? I know it works with local (which is not in posix) but is it allowed for global variables?

This is fine as-is; it's POSIX.

Thanks for clarifying that

Best Wishes

Phillip

#7 lore raw

Re: [PATCH v4 2/2] attr: add flag `--revision` to work with revisions

In reply to Eric Sunshine
Quoted 7 lines · Eric Sunshine
Quoted 5 lines · Phillip Wood
Quoted 3 lines · Karthik Nayak

Since we use a tree-ish object, the user can pass "--revision HEAD:subdirectory" and all the attributes will be looked up as if subdirectory was the root directory of the repository.

Quoted 9 lines
Quoted 3 lines

We should be clear in the documentation and option help that --revision takes a tree-ish (i.e. --revision=<tree-ish>). Maybe calling the option --tree would be clearer.

If that's the case, shouldn't this be called --source (or some variation thereof) for consistency[1] with git-restore?

[1]: https://lore.kernel.org/git/CAPig+cT_xz3m-3kkL_Scf0opNXkikU94kSF46VO8KHkNMWbGzg@mail.gmail.com/

Let me rename it to source, and we can discuss on the next version if the name suits or not.

Signature · 1 line
- Karthik
#8 lore raw

Re: [PATCH v4 2/2] attr: add flag `--revision` to work with revisions

In reply to Phillip Wood

Hello Phillip,

Quoted 8 lines · Phillip Wood
Quoted 3 lines

Since we use a tree-ish object, the user can pass "--revision HEAD:subdirectory" and all the attributes will be looked up as if subdirectory was the root directory of the repository.

We should be clear in the documentation and option help that --revision takes a tree-ish (i.e. --revision=<tree-ish>). Maybe calling the option --tree would be clearer.

I think we had a discussion around this a bit earlier in the series.
https://lore.kernel.org/git/CAOLa=ZTSzUh2Ma_EMHHWcDunGyKMaUW9BaG=QdegtMqLd+69Wg@mail.gmail.com/

Mostly, that the idea of using --revision was taken from git-svn(1). I'm good to make that change,
what do you think would be best? --source or -tree? I like --tree better, but I'm open to suggestions.

Quoted 5 lines

The implementation looks good apart from failing to bail out if it cannot parse the argument to --revision (perhaps we should add a test for that). I've left a few suggestions below.

Thank you for the review!

Quoted 4 lines
Quoted 3 lines
Quoted 13 lines

attr.c line 15

Quoted 10 lines
attr.c +2 -0
index 42ad6de8c7..6c69e82080 100644
@@ -11,8 +11,12 @@
  #include "exec-cmd.h"
  #include "attr.h"
  #include "dir.h"
+#include "strbuf.h"
+#include "tree-walk.h"

These new includes are not required.

Will remove!

Quoted 8 lines
Quoted 8 lines
attr.h +0 -0
index 3fb40cced0..f4a2bedd68 100644
@@ -1,6 +1,8 @@
 #ifndef ATTR_H
 #define ATTR_H
 
Quoted 4 lines

attr.h line 4

Quoted 1 line
attr.h +1 -0
+#include "hash.h"

This include is not required.

Will remove!

Quoted 10 lines

builtin/check-attr.c line 1

Quoted 6 lines
builtin/check-attr.c +1 -0
index 0fef10eb6b..04640e0297 100644
@@ -1,3 +1,4 @@
+#include "repository.h"

This include is not required. Also please add any new includes below cache.h as Junio has previously mentioned.

Understood. I missed this.

Quoted 8 lines

builtin/check-attr.c int cmd_check_attr(int argc, const char **argv, const char *prefix) · line 183

Quoted 2 lines
builtin/check-attr.c +2 -0
+     if (revision) {
+             tree_oid = xmalloc(sizeof(struct object_id));

I think we prefer 'var = xmalloc(sizeof(*var));' to avoid errors if the type of var changes. This allocation does not appear to be freed anywhere. We could avoid the allocation by delcaring an automatic variable above and setting tree_oid to point to it here.

Understood, let me do that.

Quoted 5 lines

builtin/check-attr.c int cmd_check_attr(int argc, const char **argv, const char *prefix) · line 186

Quoted 2 lines
builtin/check-attr.c +2 -0
+             if (repo_get_oid_tree(the_repository, revision, tree_oid))
+                     error("%s: not a valid revision", revision);

We should die() here rather than continuing with a bad tree.

Will switch to die(...)

Quoted 4 lines
Quoted 1 line

+ git $git_opts check-attr --revision $revision test -- "$path" >actual 2>err &&

err is never used. Should we be doing 'test_must_be_empty err'?

Yeah this makes sense, let me add it in.

Quoted 17 lines

builtin/check-attr.c

Quoted 10 lines
builtin/check-attr.c +7 -0
+     echo "$path: test: $expect" >expect &&
+     test_cmp expect actual
  }
 
[...]
+test_expect_success 'setup branches' '
+     (
+             echo "f test=f" &&
+             echo "a/i test=n"
+     )

We'd normally write this as

test_write_lines "f test=f" "a/i test=n" | git hash-object ...

However I think it would be simpler to create the commit with something like

Quoted 8 lines

mkdir -p foo/bar &&

test_commit --printf "add .gitattributes" foo/bar/.gitattributes \
        "t test=f\na/i test=n\n" tag-1 &&

rm -r foo/bar/.gitattributes

which would also reduce the number of processes. Failing that a helper function to reduce the duplication would be a good idea.

Thanks for this, my method was mostly put together with what I could make work, this is much cleaner. We don't need to rm -r foo/bar/.gitattributes as far as I see.

Quoted 10 lines

| git hash-object -w --stdin >id &&

builtin/check-attr.c

Quoted 3 lines
builtin/check-attr.c +3 -0
+     git update-index --add --cacheinfo 100644,$(cat id),foo/bar/.gitattributes &&
+     git write-tree >id &&
+     tree_id=$(cat id) &&

For future reference it is perfectly fine to write

        tree_oid=$(git write-tree) &&

as we will still detect a non-zero exit code from git.

Noted.

Quoted 23 lines
Quoted 23 lines
builtin/check-attr.c +15 -0
+     git commit-tree $tree_id -m "random commit message" >id &&
+     commit_id=$(cat id) &&
+     git update-ref refs/heads/branch1 $commit_id &&
+
+     (
+             echo "g test=g" &&
+             echo "a/i test=m"
+     ) | git hash-object -w --stdin >id &&
+     git update-index --add --cacheinfo 100644,$(cat id),foo/bar/.gitattributes &&
+     git write-tree >id &&
+     tree_id=$(cat id) &&
+     git commit-tree $tree_id -m "random commit message" >id &&
+     commit_id=$(cat id) &&
+     git update-ref refs/heads/branch2 $commit_id
+'
[...]
  test_expect_success 'setup bare' '
      git clone --template= --bare . bare.git
  '
@@ -306,6 +347,27 @@ test_expect_success 'bare repository: check that .gitattribute is ignored' '
      )
  '
 
Quoted 7 lines

builtin/check-attr.c test_expect_success 'bare repository: check that .gitattribute is ignored' ' · line 352

Quoted 3 lines
builtin/check-attr.c +3 -0
+test_expect_success 'bare repository: with --revision' '
+     (
+             cd bare.git &&

You could create a bare clone of the existing repo rather than having to recreate the commits here.

Makes sense, let me simplify this too.

Signature · 1 line
- Karthik