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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Karthik

On 21/12/2022 13:47, Karthik Nayak wrote:
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 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.

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.

Signed-off-by: Karthik Nayak <karthik.188@xxxxxxxxx>
Signed-off-by: Toon Claes <toon@xxxxxxxxx>
Co-authored-by: toon@xxxxxxxxx

diff --git a/attr.c b/attr.c
index 42ad6de8c7..6c69e82080 100644
--- a/attr.c
+++ b/attr.c
@@ -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.

> diff --git a/attr.h b/attr.h
> index 3fb40cced0..f4a2bedd68 100644
> --- a/attr.h
> +++ b/attr.h
> @@ -1,6 +1,8 @@
>  #ifndef ATTR_H
>  #define ATTR_H
>
> +#include "hash.h"

This include is not required.

diff --git a/builtin/check-attr.c b/builtin/check-attr.c
index 0fef10eb6b..04640e0297 100644
--- a/builtin/check-attr.c
+++ b/builtin/check-attr.c
@@ -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.

  #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()
  };

+	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.

+		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.

+	}
+
  	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");
  	}
[...]
diff --git a/t/t0003-attributes.sh b/t/t0003-attributes.sh
index b3aabb8aa3..6e6a909a46 100755
--- a/t/t0003-attributes.sh
+++ b/t/t0003-attributes.sh
@@ -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?

+	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'?

+	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 &&
+	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.

+	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' '
  	)
  '
+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



[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux