Add directory pattern matching to attributes
PATCH 1/1 Add directory pattern matching to attributes
The manpage of gitattributes says: "The rules how the pattern matches paths are the same as in .gitignore files" and the gitignore pattern rules has a pattern ending with / for directory matching.
This rule is specifically relevant for the 'export-ignore' rule used for git archive.
--- archive.c | 2 +- attr.c | 47 ++++++++++++++++++++++++------------- attr.h | 4 ++-- builtin/check-attr.c | 7 ++++-- builtin/pack-objects.c | 2 +- convert.c | 2 +- ll-merge.c | 4 ++-- t/t5002-archive-attr-pattern.sh | 49 +++++++++++++++++++++++++++++++++++++++ userdiff.c | 2 +- ws.c | 2 +- 10 files changed, 94 insertions(+), 27 deletions(-) create mode 100644 t/t5002-archive-attr-pattern.sh
archive.c +1 -1
@@ -123,7 +123,7 @@ static int write_archive_entry(const unsigned char *sha1, const char *base, path_without_prefix = path.buf + args->baselen;setup_archive_check (check); -if (!git_check_attr (path_without_prefix,ARRAY_SIZE (check), check)) { +if (!git_check_attr (path_without_prefix, mode,ARRAY_SIZE (check), check)) {if (ATTR_TRUE (check[0 ].value))return 0 ; args->convert =ATTR_TRUE (check[1 ].value);
attr.c +31 -16
117 lines not on this page · raw message
attr.h +2 -2
@@ -36,7 +36,7 @@ struct git_attr_check {*/ char *git_attr_name (struct git_attr *); -int git_check_attr (const char *path,int ,struct git_attr_check *); +int git_check_attr (const char *path,unsigned mode,int ,struct git_attr_check *);/* * Retrieve all attributes that apply to the specified path. *num
@@ -45,7 +45,7 @@ int git_check_attr(const char *path, int, struct git_attr_check *);* objects describing the attributes and their values. *check must be * free()ed by the caller. */ -int git_all_attrs (const char *path,int *num,struct git_attr_check **check); +int git_all_attrs (const char *path,unsigned mode,int *num,struct git_attr_check **check);enum git_attr_direction {GIT_ATTR_CHECKIN ,
builtin/check-attr.c +5 -2
@@ -46,14 +46,17 @@ static void output_attr(int cnt, struct git_attr_check *check,static void check_attr (const char *prefix,int cnt,struct git_attr_check *check,const char *file) { +struct stat st;char *full_path =prefix_path (prefix, prefix ?strlen (prefix) :0 , file); +stat (full_path,&st);if (check !=NULL ) { -if (git_check_attr (full_path, cnt, check)) +stat (full_path,&st); +if (git_check_attr (full_path, st.st_mode, cnt, check))die ("git_check_attr died" );output_attr (cnt, check, file); }else { -if (git_all_attrs (full_path, &cnt, &check)) +if (git_all_attrs (full_path, st.st_mode, &cnt, &check))die ("git_all_attrs died" );output_attr (cnt, check, file);free (check);
builtin/pack-objects.c +1 -1
@@ -894,7 +894,7 @@ static int no_try_delta(const char *path)struct git_attr_check check[1 ];setup_delta_attr_check (check); -if (git_check_attr (path,ARRAY_SIZE (check), check)) +if (git_check_attr (path,0 ,ARRAY_SIZE (check), check))return 0 ;if (ATTR_FALSE (check->value))return 1 ;
convert.c +1 -1
@@ -759,7 +759,7 @@ static void convert_attrs(struct conv_attrs *ca, const char *path)git_config (read_convert_config,NULL ); } -if (!git_check_attr (path,NUM_CONV_ATTRS , ccheck)) { +if (!git_check_attr (path,0 ,NUM_CONV_ATTRS , ccheck)) { ca->crlf_action =git_path_check_crlf (path, ccheck +4 );if (ca->crlf_action ==CRLF_GUESS ) ca->crlf_action =git_path_check_crlf (path, ccheck +0 );
ll-merge.c +2 -2
@@ -346,7 +346,7 @@ static int git_path_check_merge(const char *path, struct git_attr_check check[2] check[0 ].attr =git_attr ("merge" ); check[1 ].attr =git_attr ("conflict-marker-size" ); } -return git_check_attr (path,2 , check); +return git_check_attr (path,0 ,2 , check); }static void normalize_file (mmfile_t *mm,const char *path)
@@ -403,7 +403,7 @@ int ll_merge_marker_size(const char *path)if (!check.attr) check.attr =git_attr ("conflict-marker-size" ); -if (!git_check_attr (path,1 , &check) && check.value) { +if (!git_check_attr (path,0 ,1 , &check) && check.value) { marker_size =atoi (check.value);if (marker_size <=0 ) marker_size =DEFAULT_CONFLICT_MARKER_SIZE ;
t/t5002-archive-attr-pattern.sh new file +49 -0
49 lines not on this page · raw message
userdiff.c +1 -1
@@ -275,7 +275,7 @@ struct userdiff_driver *userdiff_find_by_path(const char *path)if (!path)return NULL ; -if (git_check_attr (path,1 , &check)) +if (git_check_attr (path,0 ,1 , &check))return NULL ;if (ATTR_TRUE (check.value))
ws.c +1 -1
@@ -88,7 +88,7 @@ unsigned whitespace_rule(const char *pathname)struct git_attr_check attr_whitespace_rule;setup_whitespace_attr_check (&attr_whitespace_rule); -if (!git_check_attr (pathname,1 , &attr_whitespace_rule)) { +if (!git_check_attr (pathname,0 ,1 , &attr_whitespace_rule)) {const char *value; value = attr_whitespace_rule.value;
Signature · 1 line
1.7.10.4
attr.c static void prepare_attr_stack(const char *path) · line 645
Quoted 7 lines · Jean-Noël AVILA
attr.c +1
-1-static void prepare_attr_stack (const char *path) +static void prepare_attr_stack (const char *path,unsigned mode) {struct attr_stack *elem, *info;int dirlen, len;@@ -645,28 +645,43 @@ static void prepare_attr_stack(const char *path) }
Why?
The new "mode" parameter does not seem to be used in this function at all.
Quoted 34 lines
attr.c +18
-5static int path_matches (const char *pathname,int pathlen, -const char *pattern, +const unsigned mode,char *pattern,const char *base,int baselen) { -if (!strchr (pattern,'/' )) { +size_t len; +char buf[PATH_MAX ]; +char * lpattern = buf; + len =strlen (pattern); +if (PATH_MAX <= len) +return 0 ; +strncpy (buf,pattern,len); + buf[len] ='\0' ; +if (len && lpattern[len -1 ] =='/' ) { +if (S_ISDIR (mode)) + lpattern[len -1 ] ='\0' ; +else +return 0 ; + } +if (!strchr (lpattern,'/' )) {/* match basename */ const char *basename =strrchr (pathname,'/' ); basename = basename ? basename +1 : pathname; -return (fnmatch_icase (pattern, basename,0 ) ==0 ); +return (fnmatch_icase (lpattern, basename,0 ) ==0 ); }/* * match with FNM_PATHNAME; the pattern has base implicitly * in front of it. */ -if (*pattern =='/' ) - pattern++; +if (*lpattern =='/' )
attr.c static void prepare_attr_stack(const char *path) · line 685
Quoted 10 lines
attr.c +2
-1+ lpattern++;if (pathlen < baselen || (baselen && pathname[baselen] !='/' ) ||strncmp (pathname, base, baselen))return 0 ;if (baselen !=0 ) baselen++; -return fnmatch_icase (pattern, pathname + baselen,FNM_PATHNAME ) ==0 ; +return fnmatch_icase (lpattern, pathname + baselen,FNM_PATHNAME ) ==0 ; }
It appears to me that you are forcing the caller to tell this function if the path is a directory, but in the attribute system, the caller does not necessarily know if the path it is passing is meant to be a directory or a regular file. "check-attr" is meant to be usable against a path that does not even exist on the working tree, so using stat() or lstat() in it is not a solution. In other words, it is unfair (read: unworkable) to force it to append a trailing slash after the path it calls this function with.
If you are interested in export-subst, all is not lost, though:
$ git init
$ mkdir a
$ >a/b
$ echo a export-ignore >.gitattributes
$ git add a/b .gitattributes
$ git commit -m initial
$ git archive HEAD | tar tf -
.gitattributes
$ exitYou could change the "echo" to
$ echo "a/*" export-ignore >.gitattributes
as well, but it seems to create an useless empty directory "a/" in the output, which I think is an unrelated bug in "git archive".
This patch seems to be based on a stale codebase. I do not think I'd be opposed to change the sementics to allow the callers that know that a path is a directory to optionally pass mode parameter by ending the pathname with slash (in other words, have "git check-attr" ask about a directory 'a' by saying "git check-attr export-subst a/", and lose the "mode" argument in this patch), or keep the "mode" parameter and instead allow "git check-attr" to ask about a directory that does not exist in the working tree by a more explicit "git check-attr --directory export-ignore a" or something. Such an enhancement should be done on top of the current codebase.
Thanks.
Quoted 58 lines · Junio C Hamano
attr.c static void prepare_attr_stack(const char *path) · line 645
Quoted 7 lines · Jean-Noël AVILA
attr.c +1
-1-static void prepare_attr_stack (const char *path) +static void prepare_attr_stack (const char *path,unsigned mode) {struct attr_stack *elem, *info;int dirlen, len;@@ -645,28 +645,43 @@ static void prepare_attr_stack(const char *path) }Why?
The new "mode" parameter does not seem to be used in this function at all.
Quoted 34 lines
attr.c +18
-5static int path_matches (const char *pathname,int pathlen, -const char *pattern, +const unsigned mode,char *pattern,const char *base,int baselen) { -if (!strchr (pattern,'/' )) { +size_t len; +char buf[PATH_MAX ]; +char * lpattern = buf; + len =strlen (pattern); +if (PATH_MAX <= len) +return 0 ; +strncpy (buf,pattern,len); + buf[len] ='\0' ; +if (len && lpattern[len -1 ] =='/' ) { +if (S_ISDIR (mode)) + lpattern[len -1 ] ='\0' ; +else +return 0 ; + } +if (!strchr (lpattern,'/' )) {/* match basename */ const char *basename =strrchr (pathname,'/' ); basename = basename ? basename +1 : pathname; -return (fnmatch_icase (pattern, basename,0 ) ==0 ); +return (fnmatch_icase (lpattern, basename,0 ) ==0 ); }/* * match with FNM_PATHNAME; the pattern has base implicitly * in front of it. */ -if (*pattern =='/' ) - pattern++; +if (*lpattern =='/' )attr.c static void prepare_attr_stack(const char *path) · line 685
Quoted 10 lines
attr.c +2
-1+ lpattern++;if (pathlen < baselen || (baselen && pathname[baselen] !='/' ) ||strncmp (pathname, base, baselen))return 0 ;if (baselen !=0 ) baselen++; -return fnmatch_icase (pattern, pathname + baselen,FNM_PATHNAME ) ==0 ; +return fnmatch_icase (lpattern, pathname + baselen,FNM_PATHNAME ) ==0 ; }
Quoted 8 lines
It appears to me that you are forcing the caller to tell this function if the path is a directory, but in the attribute system, the caller does not necessarily know if the path it is passing is meant to be a directory or a regular file. "check-attr" is meant to be usable against a path that does not even exist on the working tree, so using
stat()orlstat()in it is not a solution. In other words, it is unfair (read: unworkable) to force it to append a trailing slash after the path it calls this function with.
Thank you for your comments. Changing the whole attr.h interface header is definitely not a good option, but at some point, we may need more information on the path to be able to match a path pattern against it.
Quoted 13 lines
If you are interested in export-subst, all is not lost, though:
$ git init $ mkdir a $ >a/b $ echo a export-ignore >.gitattributes $ git add a/b .gitattributes $ git commit -m initial $ git archive HEAD | tar tf - .gitattributes $ exit
Quoted 6 lines
You could change the "echo" to
$ echo "a/*" export-ignore >.gitattributesas well, but it seems to create an useless empty directory "a/" in the output, which I think is an unrelated bug in "git archive".
This is quite different from the pattern matching documented for gitignore.
Moreover,
$ mkdir -p not-ignored-dir/ignored-dir $ echo test >not-ignored-dir/ignored-dir/ignored $ echo 'ignored-dir/*' >.gitattributes $ git add not-ignored-dir .gitattributes $ git commit -m '.' $ git archive HEAD | tar tf - .gitattributes not-ignored-dir/ not-ignored-dir/ignored-dir/ not-ignored-dir/ignored-dir/ignored
Quoted 2 lines
This patch seems to be based on a stale codebase.
Sorry. I thought the patch submissions had to be based on the 'maint' branch.
Quoted 10 lines
I do not think
I'd be opposed to change the sementics to allow the callers that know that a path is a directory to optionally pass mode parameter by ending the pathname with slash (in other words, have "git check-attr" ask about a directory 'a' by saying "git check-attr export-subst a/", and lose the "mode" argument in this patch), or keep the "mode" parameter and instead allow "git check-attr" to ask about a directory that does not exist in the working tree by a more explicit "git check-attr --directory export-ignore a" or something. Such an enhancement should be done on top of the current codebase.
OK. I like the idea of proposing a path ending with '/' when it is meant to be directory. This would not change the interface attr.h . I will rework with this idea.
Thank you.