check-attr: add support to work with tree-ish
v5 cover check-attr: add support to work with tree-ish
In reply to a message not in the archivev1: 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]/
v4: 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 tree-ish 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 --source to git-check-attr(1) which allows us to read gitattributes from the specified tree-ish.
Changes since version 4:
- Changed the flag from --revision to --source
- Removed uneeded header imports
- Using a pre-initialized variable instead of malloc for the tree_oid
- Using die() instead of error() for bad tree-ish provided
Range-diff against v4:
1: 6224754179 = 1: 6224754179 t0003: move setup for `--all` into new block
2: a161dbdf8b ! 2: d835d989ad attr: add flag `--revision` to work with revisions
@@ Metadata
Author: Karthik Nayak <[email protected]>
## Commit message ##
- attr: add flag `--revision` to work with revisions
+ attr: add flag `--source` to work with tree-ish
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
+ Add a new flag `--source` 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
+ Since we use a tree-ish object, the user can pass "--source
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`
+ We cannot simply use the `<rev>:<path>` syntax without the `--source`
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
+ given the path reads the blob for the path against the provided source 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: 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' [--revision <revision>] [-a | --all | <attr>...] [--] <pathname>...
-+'git check-attr' --stdin [-z] [--revision <revision>] [-a | --all | <attr>...]
++'git check-attr' [--source <tree>] [-a | --all | <attr>...] [--] <pathname>...
++'git check-attr' --stdin [-z] [--source <tree>] [-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.
-+--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.
++--source=<tree>::
++ Check attributes against the specified tree-ish. Paths provided as part
++ of the revision will be treated as the root directory. It is common to
++ specify the source tree by naming a commit, branch or tag associated
++ with it.
+
\--::
Interpret all preceding arguments as attributes and all following
@@ archive.c: static const struct attr_check *get_archive_attrs(struct index_state
## attr.c ##
@@
- #include "exec-cmd.h"
- #include "attr.h"
#include "dir.h"
-+#include "strbuf.h"
-+#include "tree-walk.h"
#include "utf8.h"
#include "quote.h"
+#include "revision.h"
@@ attr.c: void git_check_attr(struct index_state *istate,
const char *name = check->all_attrs[i].attr->name;
## attr.h ##
-@@
- #ifndef ATTR_H
- #define ATTR_H
-
-+#include "hash.h"
-+
- /**
- * gitattributes mechanism gives a uniform way to associate various attributes
- * to set of paths.
@@ attr.h: void attr_check_free(struct attr_check *check);
const char *git_attr_name(const struct git_attr *);
@@ attr.h: void attr_check_free(struct attr_check *check);
enum git_attr_direction {
## builtin/check-attr.c ##
-@@
-+#include "repository.h"
- #define USE_THE_INDEX_VARIABLE
- #include "builtin.h"
- #include "cache.h"
@@
static int all_attrs;
static int cached_attrs;
static int stdin_paths;
-+static char *revision;
++static char *source;
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>...]"),
++N_("git check-attr [--source <tree>] [-a | --all | <attr>...] [--] <pathname>..."),
++N_("git check-attr --stdin [-z] [--source <tree>] [-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(0, "revision", &revision, N_("revision"), N_("check attributes at this revision")),
++ OPT_STRING(0, "source", &source, N_("<tree-ish>"), N_("which tree-ish to check attributes at")),
OPT_END()
};
@@ builtin/check-attr.c: static NORETURN void error_with_usage(const char *msg)
{
struct attr_check *check;
+ struct object_id *tree_oid = NULL;
++ struct object_id initialized_oid;
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) {
-+ 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 (source) {
++ if (repo_get_oid_tree(the_repository, source, &initialized_oid))
++ die("%s: not a valid tree-ish source", source);
++ tree_oid = &initialized_oid;
+ }
+
if (stdin_paths)
@@ t/t0003-attributes.sh: attr_check_quote () {
test_cmp expect actual
+}
+
-+attr_check_revision () {
-+ path="$1" expect="$2" revision="$3" git_opts="$4" &&
++attr_check_source () {
++ path="$1" expect="$2" source="$3" git_opts="$4" &&
-+ git $git_opts check-attr --revision $revision test -- "$path" >actual 2>err &&
++ git $git_opts check-attr --source $source test -- "$path" >actual 2>err &&
+ echo "$path: test: $expect" >expect &&
+ test_cmp expect actual
++ test_must_be_empty err
}
test_expect_success 'open-quoted pathname' '
@@ t/t0003-attributes.sh: test_expect_success 'setup' '
'
+test_expect_success 'setup branches' '
-+ (
-+ echo "f test=f" &&
-+ echo "a/i test=n"
-+ ) | 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/branch1 $commit_id &&
++ mkdir -p foo/bar &&
++ test_commit --printf "add .gitattributes" foo/bar/.gitattribute \
++ "f test=f\na/i test=n\n" tag-1 &&
+
-+ (
-+ 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
++ mkdir -p foo/bar &&
++ test_commit --printf "add .gitattributes" foo/bar/.gitattribute \
++ "g test=g\na/i test=m\n" tag-2
+'
+
test_expect_success 'command line checks' '
@@ 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 --revision &&
-+ test_must_fail git check-attr --revision not-a-valid-ref &&
++ test_must_fail git check-attr --source &&
++ test_must_fail git check-attr --source 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 &&
@@ t/t0003-attributes.sh: test_expect_success 'using --git-dir and --work-tree' '
)
'
-+test_expect_success 'using --revision' '
-+ attr_check_revision foo/bar/f f branch1 &&
-+ attr_check_revision foo/bar/a/i n branch1 &&
-+ attr_check_revision foo/bar/f unspecified branch2 &&
-+ attr_check_revision foo/bar/a/i m branch2 &&
-+ attr_check_revision foo/bar/g g branch2 &&
-+ attr_check_revision foo/bar/g unspecified branch1
++test_expect_success 'using --source' '
++ attr_check_source foo/bar/f f tag-1 &&
++ attr_check_source foo/bar/a/i n tag-1 &&
++ attr_check_source foo/bar/f unspecified tag-2 &&
++ attr_check_source foo/bar/a/i m tag-2 &&
++ attr_check_source foo/bar/g g tag-2 &&
++ attr_check_source foo/bar/g unspecified tag-1
+'
+
test_expect_success 'setup bare' '
@@ t/t0003-attributes.sh: test_expect_success 'bare repository: check that .gitattr
)
'
-+test_expect_success 'bare repository: with --revision' '
++test_expect_success 'bare repository: with --source' '
+ (
+ cd bare.git &&
-+ (
-+ echo "f test=f" &&
-+ echo "a/i test=a/i"
-+ ) | git hash-object -w --stdin >id &&
-+ git update-index --add --cacheinfo 100644 $(cat id) .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/master $commit_id &&
-+ attr_check_revision f f HEAD &&
-+ attr_check_revision a/f f HEAD &&
-+ attr_check_revision a/c/f f HEAD &&
-+ attr_check_revision a/i a/i HEAD &&
-+ attr_check_revision subdir/a/i unspecified HEAD
++ attr_check_source foo/bar/f f tag-1 &&
++ attr_check_source foo/bar/a/i n tag-1 &&
++ attr_check_source foo/bar/f unspecified tag-2 &&
++ attr_check_source foo/bar/a/i m tag-2 &&
++ attr_check_source foo/bar/g g tag-2 &&
++ attr_check_source foo/bar/g unspecified tag-1
+ )
+'
+Karthik Nayak (2):
t0003: move setup for --all into new block
attr: add flag --source to work with tree-ish
Documentation/git-check-attr.txt | 10 +++- archive.c | 2 +- attr.c | 97 +++++++++++++++++++++++--------- attr.h | 5 +- builtin/check-attr.c | 35 +++++++----- builtin/pack-objects.c | 2 +- convert.c | 2 +- ll-merge.c | 4 +- pathspec.c | 2 +- t/t0003-attributes.sh | 49 +++++++++++++++- userdiff.c | 2 +- ws.c | 2 +- 12 files changed, 157 insertions(+), 55 deletions(-)
Signature · 1 line
2.39.0
PATCH v5 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
@@ -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
PATCH v5 2/2 attr: add flag `--source` to work with tree-ish
In reply to Karthik NayakThe 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 --source 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 "--source 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 --source 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 source 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 | 10 +++- archive.c | 2 +- attr.c | 97 +++++++++++++++++++++++--------- attr.h | 5 +- builtin/check-attr.c | 35 +++++++----- builtin/pack-objects.c | 2 +- convert.c | 2 +- ll-merge.c | 4 +- pathspec.c | 2 +- t/t0003-attributes.sh | 42 +++++++++++++- userdiff.c | 2 +- ws.c | 2 +- 12 files changed, 152 insertions(+), 53 deletions(-)
Documentation/git-check-attr.txt +8 -2
@@ -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' [--source <tree>] [-a | --all | <attr>...] [--] <pathname>... +'git check-attr' --stdin [-z] [--source <tree>] [-a | --all | <attr>...] DESCRIPTION -----------
@@ -36,6 +36,12 @@ OPTIONS If `--stdin` is also given, input paths are separated with a NUL character instead of a linefeed character. +--source=<tree>:: + Check attributes against the specified tree-ish. Paths provided as part + of the revision will be treated as the root directory. It is common to + specify the source tree by naming a commit, branch or tag associated + with it. + \--:: Interpret all preceding arguments as attributes and all following arguments as path names.
archive.c +1 -1
@@ -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 +70 -27
191 lines not on this page · raw message
attr.h +3 -2
@@ -190,13 +190,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 +22 -13
@@ -9,9 +9,10 @@static int all_attrs;static int cached_attrs;static int stdin_paths; +static char *source;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 [--source <tree>] [-a | --all | <attr>...] [--] <pathname>..." ), +N_ ("git check-attr --stdin [-z] [--source <tree>] [-a | --all | <attr>...]" ),NULL };
@@ -23,6 +24,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 ,"source" , &source,N_ ("<tree-ish>" ),N_ ("which tree-ish to check attributes at" )), OPT_END() };
@@ -55,27 +57,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 +90,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 +106,8 @@ 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 ; +struct object_id initialized_oid;int cnt, i, doubledash, filei;if (!is_bare_repository ())
@@ -176,11 +179,17 @@ int cmd_check_attr(int argc, const char **argv, const char *prefix) } } +if (source) { +if (repo_get_oid_tree (the_repository, source, &initialized_oid)) +die ("%s: not a valid revision" , source); + tree_oid = &initialized_oid; + } +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
@@ -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
@@ -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
@@ -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
@@ -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 +41 -1
79 lines not on this page · raw message
userdiff.c +1 -1
@@ -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
@@ -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
Re: [PATCH v5 2/2] attr: add flag `--source` to work with tree-ish
Quoted 298 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
--sourcewhich 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 "--source 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--sourceflag, similar to how it is used ingit showbecause 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 source and parses the attributes line by line. This function is plugged intoread_attr()function wherein we go through the stack of attributes 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 | 10 +++- archive.c | 2 +- attr.c | 97 +++++++++++++++++++++++--------- attr.h | 5 +- builtin/check-attr.c | 35 +++++++----- builtin/pack-objects.c | 2 +- convert.c | 2 +- ll-merge.c | 4 +- pathspec.c | 2 +- t/t0003-attributes.sh | 42 +++++++++++++- userdiff.c | 2 +- ws.c | 2 +- 12 files changed, 152 insertions(+), 53 deletions(-)Documentation/git-check-attr.txt +8
-2@@ -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' [--source <tree>] [-a | --all | <attr>...] [--] <pathname>... +'git check-attr' --stdin [-z] [--source <tree>] [-a | --all | <attr>...] DESCRIPTION -----------@@ -36,6 +36,12 @@ OPTIONS If `--stdin` is also given, input paths are separated with a NUL character instead of a linefeed character. +--source=<tree>:: + Check attributes against the specified tree-ish. Paths provided as part + of the revision will be treated as the root directory. It is common to + specify the source tree by naming a commit, branch or tag associated + with it. + \--:: Interpret all preceding arguments as attributes and all following arguments as path names.archive.c +1
-1@@ -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 +70
-27@@ -13,6 +13,8 @@#include "dir.h" #include "utf8.h" #include "quote.h" +#include "revision.h" +#include "object-store.h" #include "thread-utils.h" const char git_attr__true[] ="(builtin)true" ;@@ -729,14 +731,62 @@ static struct attr_stack *read_attr_from_file(const char *path, unsigned flags)return res; } -static struct attr_stack *read_attr_from_index (struct index_state *istate, -const char *path, -unsigned flags) +static struct attr_stack *read_attr_from_buf (char *buf,const char *path, +unsigned flags) {struct attr_stack *res; -char *buf, *sp; +char *sp;int lineno =0 ; +if (!buf) +return NULL ; + +CALLOC_ARRAY (res,1 ); +for (sp = buf; *sp;) { +char *ep; +int more; + + ep =strchrnul (sp,'\n' ); + more = (*ep =='\n' ); + *ep ='\0' ; +handle_attr_line (res, sp, path, ++lineno, flags); + sp = ep + more; + } +free (buf); + +return res; +} + +static struct attr_stack *read_attr_from_blob (struct index_state *istate, +const struct object_id *tree_oid, +const char *path,unsigned flags) +{ +struct object_id oid; +unsigned long sz; +enum object_type type; +void *buf; +unsigned short mode; + +if (!tree_oid) +return NULL ; + +if (get_tree_entry (istate->repo, tree_oid, path, &oid, &mode)) +return NULL ; + + buf =read_object_file (&oid, &type, &sz); +if (!buf || type !=OBJ_BLOB ) { +free (buf); +return NULL ; + } + +return read_attr_from_buf (buf, path, flags); +} + +static struct attr_stack *read_attr_from_index (struct index_state *istate, +const char *path,unsigned flags) +{ +char *buf; +if (!istate)return NULL ;@@ -758,28 +808,19 @@ static struct attr_stack *read_attr_from_index(struct index_state *istate,if (!buf)return NULL ; -CALLOC_ARRAY (res,1 ); -for (sp = buf; *sp; ) { -char *ep; -int more; - - ep =strchrnul (sp,'\n' ); - more = (*ep =='\n' ); - *ep ='\0' ; -handle_attr_line (res, sp, path, ++lineno, flags); - sp = ep + more; - } -free (buf); -return res; +return read_attr_from_buf (buf, path, flags); }static struct attr_stack *read_attr (struct index_state *istate, +const struct object_id *tree_oid,const char *path,unsigned flags) {struct attr_stack *res =NULL ;if (direction ==GIT_ATTR_INDEX ) { res =read_attr_from_index (istate, path, flags); + }else if (tree_oid) { + res =read_attr_from_blob (istate, tree_oid, path, flags); }else if (!is_bare_repository ()) {if (direction ==GIT_ATTR_CHECKOUT ) { res =read_attr_from_index (istate, path, flags);@@ -839,6 +880,7 @@ static void push_stack(struct attr_stack **attr_stack_p, }static void bootstrap_attr_stack (struct index_state *istate, +const struct object_id *tree_oid,struct attr_stack **stack) {struct attr_stack *e;@@ -864,7 +906,7 @@ static void bootstrap_attr_stack(struct index_state *istate, }/* root directory */ - e =read_attr (istate,GITATTRIBUTES_FILE , flags |READ_ATTR_NOFOLLOW ); + e =read_attr (istate, tree_oid,GITATTRIBUTES_FILE , flags |READ_ATTR_NOFOLLOW );push_stack (stack, e,xstrdup ("" ),0 );/* info frame */ @@ -878,6 +920,7 @@ static void bootstrap_attr_stack(struct index_state *istate, }static void prepare_attr_stack (struct index_state *istate, +const struct object_id *tree_oid,const char *path,int dirlen,struct attr_stack **stack) {@@ -899,7 +942,7 @@ static void prepare_attr_stack(struct index_state *istate,* .gitattributes in deeper directories to shallower ones, * and finally use the built-in set as the default. */ -bootstrap_attr_stack (istate, stack); +bootstrap_attr_stack (istate, tree_oid, stack);/* * Pop the "info" one that is always at the top of the stack. @@ -954,7 +997,7 @@ static void prepare_attr_stack(struct index_state *istate,strbuf_add (&pathbuf, path + pathbuf.len, (len - pathbuf.len));strbuf_addf (&pathbuf,"/%s" ,GITATTRIBUTES_FILE ); - next =read_attr (istate, pathbuf.buf,READ_ATTR_NOFOLLOW ); + next =read_attr (istate, tree_oid, pathbuf.buf,READ_ATTR_NOFOLLOW );/* reset the pathbuf to not include "/.gitattributes" */ strbuf_setlen (&pathbuf, len);@@ -1074,8 +1117,8 @@ static void determine_macros(struct all_attrs_item *all_attrs,* Otherwise all attributes are collected. */ static void collect_some_attrs (struct index_state *istate, -const char *path, -struct attr_check *check) +const struct object_id *tree_oid, +const char *path,struct attr_check *check) {int pathlen, rem, dirlen;const char *cp, *last_slash =NULL ;@@ -1094,7 +1137,7 @@ static void collect_some_attrs(struct index_state *istate, dirlen =0 ; } -prepare_attr_stack (istate, path, dirlen, &check->stack); +prepare_attr_stack (istate, tree_oid, path, dirlen, &check->stack);all_attrs_init (&g_attr_hashmap, check);determine_macros (check->all_attrs, check->stack);@@ -1103,12 +1146,12 @@ static void collect_some_attrs(struct index_state *istate, }void git_check_attr (struct index_state *istate, -const char *path, +const struct object_id *tree_oid,const char *path,struct attr_check *check) {int i; -collect_some_attrs (istate, path, check); +collect_some_attrs (istate, tree_oid, path, check);for (i =0 ; i < check->nr; i++) {size_t n = check->items[i].attr->attr_nr;@@ -1119,13 +1162,13 @@ void git_check_attr(struct index_state *istate, } } -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) {int i;attr_check_reset (check); -collect_some_attrs (istate, path, check); +collect_some_attrs (istate, tree_oid, path, check);for (i =0 ; i < check->all_attrs_nr; i++) {const char *name = check->all_attrs[i].attr->name;attr.h +0
-0
attr.h void attr_check_free(struct attr_check *check); · line 194
Quoted 6 lines
attr.h +2
-1@@ -190,13 +190,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);
FYI, this breaks "make hdr-check".
attr.h void attr_check_free(struct attr_check *check); · line 200
Quoted 7 lines
attr.h +1
-1/* * 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);
Likewise.
Perhaps squash something like this in in the next iteration?
attr.h | 1 + 1 file changed, 1 insertion(+)
attr.h +1 -0
@@ -108,6 +108,7 @@*/ struct index_state ; +struct object_id ;/** * An attribute is an opaque object that is identified by its name. Pass the
Re: [PATCH v5 2/2] attr: add flag `--source` to work with tree-ish
Quoted 13 lines · Junio C Hamano
Likewise.
Perhaps squash something like this in in the next iteration?
attr.h | 1 + 1 file changed, 1 insertion(+)attr.h +0
-0
attr.h line 114
Quoted 7 lines
attr.h +1
-0@@ -108,6 +108,7 @@*/ struct index_state ; +struct object_id ;/** * An attribute is an opaque object that is identified by its name. Pass the
Thanks Junio, I'll squash this in, waiting for more reviews before rolling out a new version.
Signature · 1 line
- Karthik
Bumping for reviews, before I re-roll with the fix for make hdr-check
- Karthik
Quoted 315 lines · Karthik Nayak
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]/
v4: 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 tree-ish 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
--sourceto git-check-attr(1) which allows us to read gitattributes from the specified tree-ish.Changes since version 4:
- Changed the flag from--revisionto--source
- Removed uneeded header imports
- Using a pre-initialized variable instead of malloc for thetree_oid
- Usingdie()instead oferror()for bad tree-ish providedRange-diff against v4:
1: 6224754179 = 1: 6224754179 t0003: move setup for `--all` into new block 2: a161dbdf8b ! 2: d835d989ad attr: add flag `--revision` to work with revisions @@ Metadata Author: Karthik Nayak <[email protected]> ## Commit message ## - attr: add flag `--revision` to work with revisions + attr: add flag `--source` to work with tree-ish 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 + Add a new flag `--source` 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 + Since we use a tree-ish object, the user can pass "--source 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` + We cannot simply use the `<rev>:<path>` syntax without the `--source` 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 + given the path reads the blob for the path against the provided source 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: 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' [--revision <revision>] [-a | --all | <attr>...] [--] <pathname>... -+'git check-attr' --stdin [-z] [--revision <revision>] [-a | --all | <attr>...] ++'git check-attr' [--source <tree>] [-a | --all | <attr>...] [--] <pathname>... ++'git check-attr' --stdin [-z] [--source <tree>] [-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. -+--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. ++--source=<tree>:: ++ Check attributes against the specified tree-ish. Paths provided as part ++ of the revision will be treated as the root directory. It is common to ++ specify the source tree by naming a commit, branch or tag associated ++ with it. + \--:: Interpret all preceding arguments as attributes and all following @@ archive.c: static const struct attr_check *get_archive_attrs(struct index_state ## attr.c ## @@ - #include "exec-cmd.h" - #include "attr.h" #include "dir.h" -+#include "strbuf.h" -+#include "tree-walk.h" #include "utf8.h" #include "quote.h" +#include "revision.h" @@ attr.c: void git_check_attr(struct index_state *istate, const char *name = check->all_attrs[i].attr->name; ## attr.h ## -@@ - #ifndef ATTR_H - #define ATTR_H - -+#include "hash.h" -+ - /** - * gitattributes mechanism gives a uniform way to associate various attributes - * to set of paths. @@ attr.h: void attr_check_free(struct attr_check *check); const char *git_attr_name(const struct git_attr *); @@ attr.h: void attr_check_free(struct attr_check *check); enum git_attr_direction { ## builtin/check-attr.c ## -@@ -+#include "repository.h" - #define USE_THE_INDEX_VARIABLE - #include "builtin.h" - #include "cache.h" @@ static int all_attrs; static int cached_attrs; static int stdin_paths; -+static char *revision; ++static char *source; 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>...]"), ++N_("git check-attr [--source <tree>] [-a | --all | <attr>...] [--] <pathname>..."), ++N_("git check-attr --stdin [-z] [--source <tree>] [-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(0, "revision", &revision, N_("revision"), N_("check attributes at this revision")), ++ OPT_STRING(0, "source", &source, N_("<tree-ish>"), N_("which tree-ish to check attributes at")), OPT_END() }; @@ builtin/check-attr.c: static NORETURN void error_with_usage(const char *msg) { struct attr_check *check; + struct object_id *tree_oid = NULL; ++ struct object_id initialized_oid; 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) { -+ 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 (source) { ++ if (repo_get_oid_tree(the_repository, source, &initialized_oid)) ++ die("%s: not a valid tree-ish source", source); ++ tree_oid = &initialized_oid; + } + if (stdin_paths) @@ t/t0003-attributes.sh: attr_check_quote () { test_cmp expect actual +} + -+attr_check_revision () { -+ path="$1" expect="$2" revision="$3" git_opts="$4" && ++attr_check_source () { ++ path="$1" expect="$2" source="$3" git_opts="$4" && -+ git $git_opts check-attr --revision $revision test -- "$path" >actual 2>err && ++ git $git_opts check-attr --source $source test -- "$path" >actual 2>err && + echo "$path: test: $expect" >expect && + test_cmp expect actual ++ test_must_be_empty err } test_expect_success 'open-quoted pathname' ' @@ t/t0003-attributes.sh: test_expect_success 'setup' ' ' +test_expect_success 'setup branches' ' -+ ( -+ echo "f test=f" && -+ echo "a/i test=n" -+ ) | 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/branch1 $commit_id && ++ mkdir -p foo/bar && ++ test_commit --printf "add .gitattributes" foo/bar/.gitattribute \ ++ "f test=f\na/i test=n\n" tag-1 && + -+ ( -+ 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 ++ mkdir -p foo/bar && ++ test_commit --printf "add .gitattributes" foo/bar/.gitattribute \ ++ "g test=g\na/i test=m\n" tag-2 +' + test_expect_success 'command line checks' ' @@ 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 --revision && -+ test_must_fail git check-attr --revision not-a-valid-ref && ++ test_must_fail git check-attr --source && ++ test_must_fail git check-attr --source 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 && @@ t/t0003-attributes.sh: test_expect_success 'using --git-dir and --work-tree' ' ) ' -+test_expect_success 'using --revision' ' -+ attr_check_revision foo/bar/f f branch1 && -+ attr_check_revision foo/bar/a/i n branch1 && -+ attr_check_revision foo/bar/f unspecified branch2 && -+ attr_check_revision foo/bar/a/i m branch2 && -+ attr_check_revision foo/bar/g g branch2 && -+ attr_check_revision foo/bar/g unspecified branch1 ++test_expect_success 'using --source' ' ++ attr_check_source foo/bar/f f tag-1 && ++ attr_check_source foo/bar/a/i n tag-1 && ++ attr_check_source foo/bar/f unspecified tag-2 && ++ attr_check_source foo/bar/a/i m tag-2 && ++ attr_check_source foo/bar/g g tag-2 && ++ attr_check_source foo/bar/g unspecified tag-1 +' + test_expect_success 'setup bare' ' @@ t/t0003-attributes.sh: test_expect_success 'bare repository: check that .gitattr ) ' -+test_expect_success 'bare repository: with --revision' ' ++test_expect_success 'bare repository: with --source' ' + ( + cd bare.git && -+ ( -+ echo "f test=f" && -+ echo "a/i test=a/i" -+ ) | git hash-object -w --stdin >id && -+ git update-index --add --cacheinfo 100644 $(cat id) .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/master $commit_id && -+ attr_check_revision f f HEAD && -+ attr_check_revision a/f f HEAD && -+ attr_check_revision a/c/f f HEAD && -+ attr_check_revision a/i a/i HEAD && -+ attr_check_revision subdir/a/i unspecified HEAD ++ attr_check_source foo/bar/f f tag-1 && ++ attr_check_source foo/bar/a/i n tag-1 && ++ attr_check_source foo/bar/f unspecified tag-2 && ++ attr_check_source foo/bar/a/i m tag-2 && ++ attr_check_source foo/bar/g g tag-2 && ++ attr_check_source foo/bar/g unspecified tag-1 + ) +' +Karthik Nayak (2):
t0003: move setup for--allinto new block
attr: add flag--sourceto work with tree-ishDocumentation/git-check-attr.txt | 10 +++- archive.c | 2 +- attr.c | 97 +++++++++++++++++++++++--------- attr.h | 5 +- builtin/check-attr.c | 35 +++++++----- builtin/pack-objects.c | 2 +- convert.c | 2 +- ll-merge.c | 4 +- pathspec.c | 2 +- t/t0003-attributes.sh | 49 +++++++++++++++- userdiff.c | 2 +- ws.c | 2 +- 12 files changed, 157 insertions(+), 55 deletions(-)--
2.39.0
Re: [PATCH v5 2/2] attr: add flag `--source` to work with tree-ish
In reply to Karthik NayakHi 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
--sourcewhich 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 "--source HEAD:subdirectory" and all the attributes will be looked up as if subdirectory was the root directory of the repository.
I think changing to --source is a good idea. I've left a few comments below - the tests are broken at the moment. I didn't look very closely at the implementation beyond scanning the range-diff as it looks like there are not any significant changes there.
Quoted 11 lines
We cannot simply use the
<rev>:<path>syntax without the--sourceflag, similar to how it is used ingit showbecause 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 source and parses the attributes line by line. This function is plugged intoread_attr()function wherein we go through the stack of attributes files.
Quoted 8 lines
Signed-off-by Karthik Nayak <[email protected]>Signed-off-by Toon Claes <[email protected]>Co-authored-by [email protected]---
-'git check-attr' [-a | --all | <attr>...] [--] <pathname>... -'git check-attr' --stdin [-z] [-a | --all | <attr>...]
+'git check-attr' [--source <tree>] [-a | --all | <attr>...] [--] <pathname>... +'git check-attr' --stdin [-z] [--source <tree>] [-a | --all | <attr>...]
I think "<tree>" would be better as "<tree-ish>" (see my comments on the --source option implementation below)
Documentation/git-check-attr.txt OPTIONS · line 43
Quoted 12 lines
DESCRIPTION
-----------Documentation/git-check-attr.txt +5
-0@@ -36,6 +36,12 @@ OPTIONS If `--stdin` is also given, input paths are separated with a NUL character instead of a linefeed character. +--source=<tree>:: + Check attributes against the specified tree-ish. Paths provided as part + of the revision will be treated as the root directory. It is common to + specify the source tree by naming a commit, branch or tag associated + with it.
I think it is confusing to keep the reference to "revision" here, we could just drop that sentence.
builtin/check-attr.c line 15
Quoted 4 lines
builtin/check-attr.c +2
-2-N_ ("git check-attr [-a | --all | <attr>...] [--] <pathname>..." ), -N_("git check-attr --stdin [-z] [-a | --all | <attr>...]"), +N_ ("git check-attr [--source <tree>] [-a | --all | <attr>...] [--] <pathname>..." ), +N_("git check-attr --stdin [-z] [--source <tree>] [-a | --all | <attr>...]"),
I think we should use "<tree-ish>" rather than "<tree>" so it is clear one can specify a commit or tag. That's what "git restore" does.
Quoted 6 lines
t/t0003-attributes.sh +0
-0@@ -25,7 +25,15 @@ attr_check_quote () { git check-attr test -- "$path" >actual &&
t/t0003-attributes.sh attr_check_quote () { · line 35
Quoted 10 lines
t/t0003-attributes.sh +7
-0echo "\"$quoted_path\": test: $expect" >expect && test_cmp expect actual +} + +attr_check_source () { + path="$1" expect="$2" source="$3" git_opts="$4" && + git $git_opts check-attr --source $source test -- "$path" >actual 2>err && + echo "$path: test: $expect" >expect && + test_cmp expect actual
This is missing && which means we miss some test failures later
t/t0003-attributes.sh test_expect_success 'setup' ' · line 92
Quoted 7 lines
t/t0003-attributes.sh +4
-0+ test_must_be_empty err } +test_expect_success 'setup branches' ' + mkdir -p foo/bar && + test_commit --printf "add .gitattributes" foo/bar/.gitattribute \
The file should be foo/bar/.gitattributes (not .gitattribute). We're missing failures due to this because of the missing && above
t/t0003-attributes.sh test_expect_success 'setup' ' · line 95
Quoted 3 lines
t/t0003-attributes.sh +3
-0+ "f test=f\na/i test=n\n" tag-1 && + + mkdir -p foo/bar &&
We don't need to make the directory again here
t/t0003-attributes.sh
Quoted 2 lines
t/t0003-attributes.sh +2
-0+ test_commit --printf "add .gitattributes" foo/bar/.gitattribute \ + "g test=g\na/i test=m\n" tag-2
I think it would be worth either removing foo/bar/.gitattributes or donig test_write_lines to change it. That way we can be sure all the --source tests are actually using the tree-ish we give it and not just reading from the filesystem.
Best Wishes
Phillip
Quoted 74 lines
+'
t/t0003-attributes.sh +24
-0+ test_expect_success 'command line checks' ' test_must_fail git check-attr && test_must_fail git check-attr -- && 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 --source && + test_must_fail git check-attr --source 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 &&@@ -287,6 +306,15 @@ test_expect_success 'using --git-dir and --work-tree' ' ) ' +test_expect_success 'using --source' ' + attr_check_source foo/bar/f f tag-1 && + attr_check_source foo/bar/a/i n tag-1 && + attr_check_source foo/bar/f unspecified tag-2 && + attr_check_source foo/bar/a/i m tag-2 && + attr_check_source foo/bar/g g tag-2 && + attr_check_source foo/bar/g unspecified tag-1 +' + test_expect_success 'setup bare' ' git clone --template= --bare . bare.git '@@ -306,6 +334,18 @@ test_expect_success 'bare repository: check that .gitattribute is ignored' ' ) ' +test_expect_success 'bare repository: with --source' ' + ( + cd bare.git && + attr_check_source foo/bar/f f tag-1 && + attr_check_source foo/bar/a/i n tag-1 && + attr_check_source foo/bar/f unspecified tag-2 && + attr_check_source foo/bar/a/i m tag-2 && + attr_check_source foo/bar/g g tag-2 && + attr_check_source foo/bar/g unspecified tag-1 + ) +' + test_expect_success 'bare repository: check that --cached honors index' ' ( cd bare.git &&userdiff.c +1
-1@@ -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@@ -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) */
Re: [PATCH v5 2/2] attr: add flag `--source` to work with tree-ish
Quoted 3 lines · Phillip Wood
Hi Karthik
Hello Philip!
Quoted 13 lines · Karthik Nayak
Quoted 12 lines
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
--sourcewhich 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 9 lines
Quoted 3 lines
Since we use a tree-ish object, the user can pass "--source HEAD:subdirectory" and all the attributes will be looked up as if subdirectory was the root directory of the repository.
I think changing to --source is a good idea. I've left a few comments below - the tests are broken at the moment. I didn't look very closely at the implementation beyond scanning the range-diff as it looks like there are not any significant changes there.
Thanks for the review!
Quoted 20 lines
Quoted 11 lines
We cannot simply use the
<rev>:<path>syntax without the--sourceflag, similar to how it is used ingit showbecause 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 source and parses the attributes line by line. This function is plugged intoread_attr()function wherein we go through the stack of attributes files.Quoted 8 lines
Signed-off-by Karthik Nayak <[email protected]>Signed-off-by Toon Claes <[email protected]>Co-authored-by [email protected]---
-'git check-attr' [-a | --all | <attr>...] [--] <pathname>... -'git check-attr' --stdin [-z] [-a | --all | <attr>...]
+'git check-attr' [--source <tree>] [-a | --all | <attr>...] [--] <pathname>... +'git check-attr' --stdin [-z] [--source <tree>] [-a | --all | <attr>...]
Quoted 2 lines
I think "<tree>" would be better as "<tree-ish>" (see my comments on the --source option implementation below)
Will change!
Quoted 7 lines
Quoted 7 lines
DESCRIPTION
-----------Documentation/git-check-attr.txt +0
-0@@ -36,6 +36,12 @@ OPTIONS If `--stdin` is also given, input paths are separated with a NUL character instead of a linefeed character.
Quoted 9 lines
Documentation/git-check-attr.txt OPTIONS · line 43
Quoted 5 lines
Documentation/git-check-attr.txt +5
-0+--source=<tree>:: + Check attributes against the specified tree-ish. Paths provided as part + of the revision will be treated as the root directory. It is common to + specify the source tree by naming a commit, branch or tag associated + with it.I think it is confusing to keep the reference to "revision" here, we could just drop that sentence.
Will do!
Quoted 8 lines
builtin/check-attr.c line 15
Quoted 4 lines
builtin/check-attr.c +2
-2-N_ ("git check-attr [-a | --all | <attr>...] [--] <pathname>..." ), -N_("git check-attr --stdin [-z] [-a | --all | <attr>...]"), +N_ ("git check-attr [--source <tree>] [-a | --all | <attr>...] [--] <pathname>..." ), +N_("git check-attr --stdin [-z] [--source <tree>] [-a | --all | <attr>...]"),I think we should use "<tree-ish>" rather than "<tree>" so it is clear one can specify a commit or tag. That's what "git restore" does.
Yeah sure!
Quoted 13 lines
Quoted 13 lines
t/t0003-attributes.sh +4
-0@@ -25,7 +25,15 @@ attr_check_quote () { git check-attr test -- "$path" >actual && echo "\"$quoted_path\": test: $expect" >expect && test_cmp expect actual +} + +attr_check_source () { + path="$1" expect="$2" source="$3" git_opts="$4" &&
Quoted 6 lines
t/t0003-attributes.sh attr_check_quote () { · line 35
Quoted 3 lines
t/t0003-attributes.sh +3
-0+ git $git_opts check-attr --source $source test -- "$path" >actual 2>err && + echo "$path: test: $expect" >expect && + test_cmp expect actualThis is missing && which means we miss some test failures later
Good catch on this!
Quoted 11 lines
t/t0003-attributes.sh test_expect_success 'setup' ' · line 92
Quoted 7 lines
t/t0003-attributes.sh +4
-0+ test_must_be_empty err } +test_expect_success 'setup branches' ' + mkdir -p foo/bar && + test_commit --printf "add .gitattributes" foo/bar/.gitattribute \The file should be foo/bar/.gitattributes (not .gitattribute). We're missing failures due to this because of the missing && above
Yup, this fixes the test. Thanks!
Quoted 6 lines
t/t0003-attributes.sh test_expect_success 'setup' ' · line 95
Quoted 3 lines
t/t0003-attributes.sh +3
-0+ "f test=f\na/i test=n\n" tag-1 && + + mkdir -p foo/bar &&We don't need to make the directory again here
Quoted 8 lines
t/t0003-attributes.sh
Quoted 2 lines
t/t0003-attributes.sh +2
-0+ test_commit --printf "add .gitattributes" foo/bar/.gitattribute \ + "g test=g\na/i test=m\n" tag-2I think it would be worth either removing foo/bar/.gitattributes or donig
test_write_linesto change it. That way we can be sure all the --source tests are actually using the tree-ish we give it and not just reading from the filesystem.
Will do a rm on the file.
Re: [PATCH v5 2/2] attr: add flag `--source` to work with tree-ish
In reply to Karthik NayakQuoted 7 lines · Karthik Nayak
attr.c +7
-0+static struct attr_stack *read_attr_from_blob (struct index_state *istate, +const struct object_id *tree_oid, +const char *path,unsigned flags) +{ +struct object_id oid; +unsigned long sz; +enum object_type type;
attr.c static struct attr_stack *read_attr_from_file(const char *path, unsigned flags) · line 776
Quoted 10 lines
attr.c +10
-0+void *buf; +unsigned short mode; + +if (!tree_oid) +return NULL ; + +if (get_tree_entry (istate->repo, tree_oid, path, &oid, &mode)) +return NULL ; + + buf =read_object_file (&oid, &type, &sz);
Here you flip-flop between istate->repo and "the_repository". I think you want to use repo_read_object_file(istate->repo, ...) instead.
Re: [PATCH v5 2/2] attr: add flag `--source` to work with tree-ish
Quoted 16 lines · Ævar Arnfjörð Bjarmason
Quoted 12 lines · Karthik Nayak
attr.c +12
-0+static struct attr_stack *read_attr_from_blob (struct index_state *istate, +const struct object_id *tree_oid, +const char *path,unsigned flags) +{ +struct object_id oid; +unsigned long sz; +enum object_type type; +void *buf; +unsigned short mode; + +if (!tree_oid) +return NULL ;
Quoted 8 lines
attr.c static struct attr_stack *read_attr_from_file(const char *path, unsigned flags) · line 776
Quoted 5 lines
attr.c +5
-0+ +if (get_tree_entry (istate->repo, tree_oid, path, &oid, &mode)) +return NULL ; + + buf =read_object_file (&oid, &type, &sz);Here you flip-flop between
istate->repoand "the_repository". I think you want to userepo_read_object_file(istate->repo, ...)instead.
Let me add this to v7! Thanks