git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Phillip Wood <phillip.wood123@gmail.com>
To: Karthik Nayak <karthik.188@gmail.com>, git@vger.kernel.org
Cc: Toon Claes <toon@iotcl.com>, Junio C Hamano <gitster@pobox.com>
Subject: Re: [PATCH v4 2/2] attr: add flag `--revision` to work with revisions
Date: Wed, 21 Dec 2022 20:57:33 +0000	[thread overview]
Message-ID: <cc2e5f1e-8f53-ad83-1f5d-ae910ab60c53@dunelm.org.uk> (raw)
In-Reply-To: <a161dbdf8b70354716d0bfeccc707c242c7f33a1.1671630304.git.karthik.188@gmail.com>

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@gmail.com>
> Signed-off-by: Toon Claes <toon@iotcl.com>
> Co-authored-by: toon@iotcl.com

> 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

  reply	other threads:[~2022-12-21 20:58 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <https://lore.kernel.org/git/20221216093552.3171319-1-karthik.188@gmail.com/>
2022-12-21 13:46 ` [PATCH v4 0/2] check-attr: add support to work with revisions Karthik Nayak
2022-12-21 13:47   ` [PATCH v4 1/2] t0003: move setup for `--all` into new block Karthik Nayak
2022-12-21 13:47   ` [PATCH v4 2/2] attr: add flag `--revision` to work with revisions Karthik Nayak
2022-12-21 20:57     ` Phillip Wood [this message]
2022-12-21 21:13       ` Eric Sunshine
2022-12-22 14:36         ` Phillip Wood
2022-12-23 10:44         ` Karthik Nayak
2022-12-23 10:43       ` Karthik Nayak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=cc2e5f1e-8f53-ad83-1f5d-ae910ab60c53@dunelm.org.uk \
    --to=phillip.wood123@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=karthik.188@gmail.com \
    --cc=phillip.wood@dunelm.org.uk \
    --cc=toon@iotcl.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).