pathspec cleanup
PATCH 1/17 mv: convert to using pathspec struct interface
In reply to Brandon WilliamsConvert the 'internal_copy_pathspec()' function to use the pathspec struct interface from using the deprecated 'get_pathspec()' interface.
In addition to this, fix a memory leak caused by only duplicating some of the pathspec elements. Instead always duplicate all of the the pathspec elements as an intermediate step (with modificationed based on the passed in flags). This way the intermediate strings can then be freed prior to duplicating the result of parse_pathspec (which contains each of the elements with the prefix prepended).
--- builtin/mv.c | 45 ++++++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 13 deletions(-)
builtin/mv.c +32 -13
@@ -4,6 +4,7 @@* Copyright (C) 2006 Johannes Schindelin */ #include "builtin.h" +#include "pathspec.h" #include "lockfile.h" #include "dir.h" #include "cache-tree.h"
@@ -25,25 +26,43 @@ static const char **internal_copy_pathspec(const char *prefix, {int i;const char **result; +struct pathspec ps;ALLOC_ARRAY (result, count +1 ); -COPY_ARRAY (result, pathspec, count); - result[count] =NULL ; + +/* Create an intermediate copy of the pathspec based on the flags */ for (i =0 ; i < count; i++) { -int length =strlen (result[i]); +int length =strlen (pathspec[i]);int to_copy = length; +char *it;while (!(flags &KEEP_TRAILING_SLASH ) && - to_copy >0 &&is_dir_sep (result[i][to_copy -1 ])) + to_copy >0 &&is_dir_sep (pathspec[i][to_copy -1 ])) to_copy--; -if (to_copy != length || flags &DUP_BASENAME ) { -char *it =xmemdupz (result[i], to_copy); -if (flags &DUP_BASENAME ) { - result[i] =xstrdup (basename (it)); -free (it); - }else - result[i] = it; - } + + it =xmemdupz (pathspec[i], to_copy); +if (flags &DUP_BASENAME ) { + result[i] =xstrdup (basename (it)); +free (it); + }else + result[i] = it; + } + result[count] =NULL ; + +parse_pathspec (&ps, +PATHSPEC_ALL_MAGIC & + ~(PATHSPEC_FROMTOP |PATHSPEC_LITERAL ), +PATHSPEC_KEEP_ORDER |PATHSPEC_PREFER_CWD , + prefix, result); +assert (count == ps.nr); + +/* Copy the pathspec and free the old intermediate strings */ +for (i =0 ; i < count; i++) { +const char *match =xstrdup (ps.items[i].match); +free ((char *) result[i]); + result[i] = match; } -return get_pathspec (prefix, result); + +clear_pathspec (&ps); +return result; }static const char *add_slash (const char *path)
Signature · 1 line
2.8.0.rc3.226.g39d4020
cover pathspec cleanup
The intent of this series is to cleanup some of the pathspec initialization code as well as finally migrating the remaining users of the _raw field or get_pathspec() to the pathspec struct interface. This way both the _raw field and get_pathspec() can be removed from the codebase. This also removes the functionality where parse_pathspec() modified the const char * argv array that was passed in (which felt kind of odd to me as I wouldn't have expected the passed in array to be modified).
I also noticed that there are memory leaks associated with the 'original' and 'match' strings. To fix this the pathspec struct needed to take ownership of the memory for these fields so that they can be cleaned up when clearing the pathspec struct.
Most of the work went to simplifying the prefix_pathspec function. This consisted of factoring out long sections of code into their own helper functions. The overall result is a much more readable function.
Brandon Williams (17):
mv: convert to using pathspec struct interface
dir: convert create_simplify to use the pathspec struct interface
dir: convert fill_directory to use the pathspec struct interface
ls-tree: convert show_recursive to use the pathspec struct interface
pathspec: remove the deprecated get_pathspec function
pathspec: copy and free owned memory
mv: small code cleanup
pathspec: remove unused variable from unsupported_magic
pathspec: always show mnemonic and name in unsupported_magic
pathspec: simpler logic to prefix original pathspec elements
pathspec: factor global magic into its own function
pathspec: create parse_short_magic function
pathspec: create parse_long_magic function
pathspec: create parse_element_magic helper
pathspec: create strip submodule slash helpers
pathspec: small readability changes
pathspec: remove outdated comment
Documentation/technical/api-setup.txt | 2 - builtin/ls-tree.c | 12 +- builtin/mv.c | 44 +++- cache.h | 1 - dir.c | 28 +-- pathspec.c | 449 +++++++++++++++++++--------------- pathspec.h | 5 +- 7 files changed, 301 insertions(+), 240 deletions(-)
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH 2/17 dir: convert create_simplify to use the pathspec struct interface
Convert 'create_simplify()' to use the pathspec struct interface from using the '_raw' entry in the pathspec.
--- dir.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-)
dir.c +12 -13
@@ -1787,25 +1787,24 @@ static int cmp_name(const void *p1, const void *p2)return name_compare (e1->name, e1->len, e2->name, e2->len); } -static struct path_simplify *create_simplify (const char **pathspec) +static struct path_simplify *create_simplify (const struct pathspec *pathspec) { -int nr, alloc =0 ; +int i;struct path_simplify *simplify =NULL ; -if (!pathspec) +if (!pathspec || !pathspec->nr)return NULL ; -for (nr =0 ; ; nr++) { +ALLOC_ARRAY (simplify, pathspec->nr +1 ); +for (i =0 ; i < pathspec->nr; i++) {const char *match; -ALLOC_GROW (simplify, nr +1 , alloc); - match = *pathspec++; -if (!match) -break ; - simplify[nr].path = match; - simplify[nr].len =simple_length (match); + match = pathspec->items[i].match; + simplify[i].path = match; + simplify[i].len = pathspec->items[i].nowildcard_len; } - simplify[nr].path =NULL ; - simplify[nr].len =0 ; + simplify[i].path =NULL ; + simplify[i].len =0 ; +return simplify; }
@@ -2036,7 +2035,7 @@ int read_directory(struct dir_struct *dir, const char *path, int len, const stru* subset of positive ones, which has no impacts on * create_simplify(). */ - simplify =create_simplify (pathspec ? pathspec->_raw :NULL ); + simplify =create_simplify (pathspec); untracked =validate_untracked_cache (dir, len, pathspec);if (!untracked)/*
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH 3/17 dir: convert fill_directory to use the pathspec struct interface
In reply to Brandon WilliamsConvert 'fill_directory()' to use the pathspec struct interface from using the '_raw' entry in the pathspec struct.
--- dir.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
dir.c +2 -1
@@ -188,7 +188,8 @@ int fill_directory(struct dir_struct *dir, const struct pathspec *pathspec) len =common_prefix_len (pathspec);/* Read the directory and prune it */ -read_directory (dir, pathspec->nr ? pathspec->_raw[0 ] :"" , len, pathspec); +read_directory (dir, pathspec->nr ? pathspec->items[0 ].match :"" , + len, pathspec);return len; }
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH 10/17 pathspec: simpler logic to prefix original pathspec elements
In reply to Brandon WilliamsThe logic used to prefix an original pathspec element with 'prefix' magic is more general purpose and can be used for more than just short magic. Remove the extra code paths and rename 'prefix_short_magic' to 'prefix_magic' to better indicate that it can be used in more general situations.
Also, slightly change the logic which decides when to prefix the original element in order to prevent a pathspec of "." from getting converted to "" (empty string).
--- pathspec.c | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-)
pathspec.c +13 -20
@@ -74,13 +74,12 @@ static struct pathspec_magic { { PATHSPEC_EXCLUDE, '!', "exclude" }, }; -static void prefix_short_magic (struct strbuf *sb,int prefixlen, -unsigned short_magic) +static void prefix_magic (struct strbuf *sb,int prefixlen,unsigned magic) {int i;strbuf_addstr (sb,":(" );for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) -if (short_magic & pathspec_magic[i].bit) { +if (magic & pathspec_magic[i].bit) {if (sb->buf[sb->len -1 ] !='(' )strbuf_addch (sb,',' );strbuf_addstr (sb, pathspec_magic[i].name);
@@ -110,8 +109,8 @@ static unsigned prefix_pathspec(struct pathspec_item *item,static int glob_global =-1 ;static int noglob_global =-1 ;static int icase_global =-1 ; -unsigned magic =0 , short_magic =0 , global_magic =0 ; -const char *copyfrom = elt, *long_magic_end =NULL ; +unsigned magic =0 , element_magic =0 , global_magic =0 ; +const char *copyfrom = elt;char *match;int i, pathspec_prefix =-1 ;
@@ -165,7 +164,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item,for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) {if (strlen (pathspec_magic[i].name) == len && !strncmp (pathspec_magic[i].name, copyfrom, len)) { - magic |= pathspec_magic[i].bit; + element_magic |= pathspec_magic[i].bit;break ; }if (starts_with (copyfrom,"prefix:" )) {
@@ -184,7 +183,6 @@ static unsigned prefix_pathspec(struct pathspec_item *item, }if (*copyfrom !=')' )die (_ ("Missing ')' at the end of pathspec magic in '%s'" ), elt); - long_magic_end = copyfrom; copyfrom++; }else {/* shorthand */
@@ -197,7 +195,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item,break ;for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++)if (pathspec_magic[i].mnemonic == ch) { - short_magic |= pathspec_magic[i].bit; + element_magic |= pathspec_magic[i].bit;break ; }if (ARRAY_SIZE (pathspec_magic) <= i)
@@ -208,7 +206,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item, copyfrom++; } - magic |= short_magic; + magic |= element_magic;/* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */ if (noglob_global && !(magic &PATHSPEC_GLOB ))
@@ -243,18 +241,13 @@ static unsigned prefix_pathspec(struct pathspec_item *item,* Prefix the pathspec (keep all magic) and assign to * original. Useful for passing to another command. */ -if (flags &PATHSPEC_PREFIX_ORIGIN ) { +if ((flags &PATHSPEC_PREFIX_ORIGIN ) && + prefixlen && !literal_global) {struct strbuf sb =STRBUF_INIT ; -if (prefixlen && !literal_global) { -/* Preserve the actual prefix length of each pattern */ -if (short_magic) -prefix_short_magic (&sb, prefixlen, short_magic); -else if (long_magic_end) { -strbuf_add (&sb, elt, long_magic_end - elt); -strbuf_addf (&sb,",prefix:%d)" , prefixlen); - }else -strbuf_addf (&sb,":(prefix:%d)" , prefixlen); - } + +/* Preserve the actual prefix length of each pattern */ +prefix_magic (&sb, prefixlen, element_magic); +strbuf_addstr (&sb, match); item->original =strbuf_detach (&sb,NULL ); }else {
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH 8/17 pathspec: remove unused variable from unsupported_magic
In reply to Brandon WilliamsRemoved unused variable 'n' from the 'unsupported_magic()' function.
--- pathspec.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
pathspec.c +2 -3
@@ -333,8 +333,8 @@ static void NORETURN unsupported_magic(const char *pattern, unsigned short_magic) {struct strbuf sb =STRBUF_INIT ; -int i, n; -for (n = i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) { +int i; +for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) {const struct pathspec_magic *m = pathspec_magic + i;if (!(magic & m->bit))continue ;
@@ -344,7 +344,6 @@ static void NORETURN unsupported_magic(const char *pattern,strbuf_addf (&sb,"'%c'" , m->mnemonic); else strbuf_addf(&sb, "'%s'", m->name); - n++; }/* * We may want to substitute "this command" with a command
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH 9/17 pathspec: always show mnemonic and name in unsupported_magic
In reply to Brandon WilliamsFor better clarity, always show the mnemonic and name of the unsupported magic being used. This lets users have a more clear understanding of what magic feature isn't supported. And if they supplied a mnemonic, the user will be told what its corresponding name is which will allow them to more easily search the man pages for that magic type.
This also avoids passing an extra parameter around the pathspec initialization code.
--- pathspec.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-)
pathspec.c +7 -11
@@ -68,7 +68,7 @@ static struct pathspec_magic {const char *name; } pathspec_magic[] = { { PATHSPEC_FROMTOP, '/', "top" }, - { PATHSPEC_LITERAL, 0, "literal" }, + { PATHSPEC_LITERAL,'\0', "literal" }, { PATHSPEC_GLOB, '\0', "glob" }, { PATHSPEC_ICASE, '\0', "icase" }, { PATHSPEC_EXCLUDE, '!', "exclude" },
@@ -102,7 +102,6 @@ static void prefix_short_magic(struct strbuf *sb, int prefixlen,* string cannot express such a case. */ static unsigned prefix_pathspec (struct pathspec_item *item, -unsigned *p_short_magic,unsigned flags,const char *prefix,int prefixlen,const char *elt)
@@ -210,7 +209,6 @@ static unsigned prefix_pathspec(struct pathspec_item *item, } magic |= short_magic; - *p_short_magic = short_magic;/* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */ if (noglob_global && !(magic &PATHSPEC_GLOB ))
@@ -329,8 +327,7 @@ static int pathspec_item_cmp(const void *a_, const void *b_) } static void NORETURN unsupported_magic(const char *pattern, -unsigned magic, -unsigned short_magic) +unsigned magic) {struct strbuf sb =STRBUF_INIT ;int i;
@@ -340,8 +337,9 @@ static void NORETURN unsupported_magic(const char *pattern,continue ;if (sb.len)strbuf_addch (&sb,' ' ); -if (short_magic & m->bit) -strbuf_addf (&sb,"'%c'" , m->mnemonic); + +if (m->mnemonic) +strbuf_addf (&sb,"'(%c)%s'" , m->mnemonic, m->name);else strbuf_addf (&sb,"'%s'" , m->name); }
@@ -413,10 +411,9 @@ void parse_pathspec(struct pathspec *pathspec, prefixlen = prefix ?strlen (prefix) :0 ;for (i =0 ; i < n; i++) { -unsigned short_magic; entry = argv[i]; - item[i].magic =prefix_pathspec (item + i, &short_magic, + item[i].magic =prefix_pathspec (item + i, flags, prefix, prefixlen, entry); if ((flags & PATHSPEC_LITERAL_PATH) &&
@@ -426,8 +423,7 @@ void parse_pathspec(struct pathspec *pathspec, nr_exclude++;if (item[i].magic & magic_mask)unsupported_magic (entry, - item[i].magic & magic_mask, - short_magic); + item[i].magic & magic_mask);if ((flags &PATHSPEC_SYMLINK_LEADING_PATH ) &&has_symlink_leading_path (item[i].match, item[i].len)) {
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH 16/17 pathspec: small readability changes
In reply to Brandon WilliamsA few small changes to improve readability. This is done by grouping related assignments, adding blank lines, ensuring lines are <80 characters, etc.
--- pathspec.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)
pathspec.c +10 -5
@@ -334,6 +334,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item,if ((magic &PATHSPEC_LITERAL ) && (magic &PATHSPEC_GLOB ))die (_ ("%s: 'literal' and 'glob' are incompatible" ), elt); +/* Create match string which will be used for pathspec matching */ if (pathspec_prefix >=0 ) { match =xstrdup (copyfrom); prefixlen = pathspec_prefix;
@@ -341,11 +342,16 @@ static unsigned prefix_pathspec(struct pathspec_item *item, match =xstrdup (copyfrom); prefixlen =0 ; }else { - match =prefix_path_gently (prefix, prefixlen, &prefixlen, copyfrom); + match =prefix_path_gently (prefix, prefixlen, + &prefixlen, copyfrom);if (!match)die (_ ("%s: '%s' is outside repository" ), elt, copyfrom); } + item->match = match; + item->len =strlen (item->match); + item->prefix = prefixlen; +/* * Prefix the pathspec (keep all magic) and assign to * original. Useful for passing to another command.
@@ -362,8 +368,6 @@ static unsigned prefix_pathspec(struct pathspec_item *item, }else { item->original =xstrdup (elt); } - item->len =strlen (item->match); - item->prefix = prefixlen;if (flags &PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP )strip_submodule_slash_cheap (item);
@@ -371,13 +375,14 @@ static unsigned prefix_pathspec(struct pathspec_item *item,if (flags &PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE )strip_submodule_slash_expensive (item); -if (magic &PATHSPEC_LITERAL ) +if (magic &PATHSPEC_LITERAL ) { item->nowildcard_len = item->len; -else { + }else { item->nowildcard_len =simple_length (item->match);if (item->nowildcard_len < prefixlen) item->nowildcard_len = prefixlen; } + item->flags =0 ;if (magic &PATHSPEC_GLOB ) {/*
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH 13/17 pathspec: create parse_long_magic function
In reply to Brandon WilliamsFactor out the logic responsible for parsing long magic into its own function. As well as hoist the prefix check logic outside of the inner loop as there isn't anything that needs to be done after matching "prefix:".
--- pathspec.c | 92 ++++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 57 insertions(+), 35 deletions(-)
pathspec.c +57 -35
@@ -156,6 +156,60 @@ static int get_global_magic(int element_magic) }/* +* Parse the pathspec element looking for long magic +* +* saves all magic in 'magic' +* if prefix magic is used, save the prefix length in 'prefix_len' +* returns the position in 'elem' after all magic has been parsed +*/ +static const char *parse_long_magic (unsigned *magic,int *prefix_len, +const char *elem) +{ +const char *pos; +const char *nextat; + +for (pos = elem +2 ; *pos && *pos !=')' ; pos = nextat) { +size_t len =strcspn (pos,",)" ); +int i; + +if (pos[len] ==',' ) + nextat = pos + len +1 ;/* handle ',' */ +else + nextat = pos + len;/* handle ')' and '\0' */ + +if (!len) +continue ; + +if (starts_with (pos,"prefix:" )) { +char *endptr; + *prefix_len =strtol (pos +7 , &endptr,10 ); +if (endptr - pos != len) +die (_ ("invalid parameter for pathspec magic 'prefix'" )); +continue ; + } + +for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) { +if (strlen (pathspec_magic[i].name) == len && + !strncmp (pathspec_magic[i].name, pos, len)) { + *magic |= pathspec_magic[i].bit; +break ; + } + } + +if (ARRAY_SIZE (pathspec_magic) <= i) +die (_ ("Invalid pathspec magic '%.*s' in '%s'" ), + (int ) len, pos, elem); + } + +if (*pos !=')' ) +die (_ ("Missing ')' at the end of pathspec magic in '%s'" ), + elem); + pos++; + +return pos; +} + +/* * Parse the pathspec element looking for short magic * * saves all magic in 'magic'
@@ -218,41 +272,9 @@ static unsigned prefix_pathspec(struct pathspec_item *item, ;/* nothing to do */ }else if (elt[1 ] =='(' ) {/* longhand */ -const char *nextat; -for (copyfrom = elt +2 ; - *copyfrom && *copyfrom !=')' ; - copyfrom = nextat) { -size_t len =strcspn (copyfrom,",)" ); -if (copyfrom[len] ==',' ) - nextat = copyfrom + len +1 ; -else -/* handle ')' and '\0' */ - nextat = copyfrom + len; -if (!len) -continue ; -for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) { -if (strlen (pathspec_magic[i].name) == len && - !strncmp (pathspec_magic[i].name, copyfrom, len)) { - element_magic |= pathspec_magic[i].bit; -break ; - } -if (starts_with (copyfrom,"prefix:" )) { -char *endptr; - pathspec_prefix =strtol (copyfrom +7 , - &endptr,10 ); -if (endptr - copyfrom != len) -die (_ ("invalid parameter for pathspec magic 'prefix'" )); -/* "i" would be wrong, but it does not matter */ -break ; - } - } -if (ARRAY_SIZE (pathspec_magic) <= i) -die (_ ("Invalid pathspec magic '%.*s' in '%s'" ), - (int ) len, copyfrom, elt); - } -if (*copyfrom !=')' ) -die (_ ("Missing ')' at the end of pathspec magic in '%s'" ), elt); - copyfrom++; + copyfrom =parse_long_magic (&element_magic, + &pathspec_prefix, + elt); }else {/* shorthand */ copyfrom =parse_short_magic (&element_magic, elt);
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH 14/17 pathspec: create parse_element_magic helper
In reply to Brandon WilliamsFactor out the logic responsible for the magic in a pathspec element into its own function.
Also avoid calling into the parsing functions when PATHSPEC_LITERAL_PATH is specified since it causes magic to be ignored and all paths to be treated as literals.
--- pathspec.c | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-)
pathspec.c +19 -16
@@ -244,6 +244,19 @@ static const char *parse_short_magic(unsigned *magic, const char *elem)return pos; } +static const char *parse_element_magic (unsigned *magic,int *prefix_len, +const char *elem) +{ +if (elem[0 ] !=':' ||get_literal_global ()) +return elem;/* nothing to do */ +else if (elem[1 ] =='(' ) +/* longhand */ +return parse_long_magic (magic, prefix_len, elem); +else +/* shorthand */ +return parse_short_magic (magic, elem); +} +/* * Take an element of a pathspec and check for magic signatures. * Append the result to the prefix. Return the magic bitmap.
@@ -267,24 +280,14 @@ static unsigned prefix_pathspec(struct pathspec_item *item,char *match;int i, pathspec_prefix =-1 ; -if (elt[0 ] !=':' ||get_literal_global () || - (flags &PATHSPEC_LITERAL_PATH )) { - ;/* nothing to do */ - }else if (elt[1 ] =='(' ) { -/* longhand */ - copyfrom =parse_long_magic (&element_magic, - &pathspec_prefix, - elt); - }else { -/* shorthand */ - copyfrom =parse_short_magic (&element_magic, elt); - } - - magic |= element_magic; -/* PATHSPEC_LITERAL_PATH ignores magic */ -if (!(flags &PATHSPEC_LITERAL_PATH )) +if (!(flags &PATHSPEC_LITERAL_PATH )) { + copyfrom =parse_element_magic (&element_magic, + &pathspec_prefix, + elt); + magic |= element_magic; magic |=get_global_magic (element_magic); + }if (pathspec_prefix >=0 && (prefixlen || (prefix && *prefix)))
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH 11/17 pathspec: factor global magic into its own function
In reply to Brandon WilliamsCreate helper functions to read the global magic environment variables in additon to factoring out the global magic gathering logic into its own function.
--- pathspec.c | 120 +++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 74 insertions(+), 46 deletions(-)
pathspec.c +74 -46
@@ -87,6 +87,74 @@ static void prefix_magic(struct strbuf *sb, int prefixlen, unsigned magic)strbuf_addf (sb,",prefix:%d)" , prefixlen); } +static inline int get_literal_global (void ) +{ +static int literal_global =-1 ; + +if (literal_global <0 ) + literal_global =git_env_bool (GIT_LITERAL_PATHSPECS_ENVIRONMENT , +0 ); +return literal_global; +} + +static inline int get_glob_global (void ) +{ +static int glob_global =-1 ; + +if (glob_global <0 ) + glob_global =git_env_bool (GIT_GLOB_PATHSPECS_ENVIRONMENT ,0 ); +return glob_global; +} + +static inline int get_noglob_global (void ) +{ +static int noglob_global =-1 ; + +if (noglob_global <0 ) + noglob_global =git_env_bool (GIT_NOGLOB_PATHSPECS_ENVIRONMENT , +0 ); +return noglob_global; +} + +static inline int get_icase_global (void ) +{ +static int icase_global =-1 ; + +if (icase_global <0 ) + icase_global =git_env_bool (GIT_ICASE_PATHSPECS_ENVIRONMENT ,0 ); + +return icase_global; +} + +static int get_global_magic (int element_magic) +{ +int global_magic =0 ; + +if (get_literal_global ()) + global_magic |=PATHSPEC_LITERAL ; + +/* --glob-pathspec is overridden by :(literal) */ +if (get_glob_global () && !(element_magic &PATHSPEC_LITERAL )) + global_magic |=PATHSPEC_GLOB ; + +if (get_glob_global () &&get_noglob_global ()) +die (_ ("global 'glob' and 'noglob' pathspec settings are incompatible" )); + +if (get_icase_global ()) + global_magic |=PATHSPEC_ICASE ; + +if ((global_magic &PATHSPEC_LITERAL ) && + (global_magic & ~PATHSPEC_LITERAL )) +die (_ ("global 'literal' pathspec setting is incompatible " +"with all other global pathspec settings" )); + +/* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */ +if (get_noglob_global () && !(element_magic &PATHSPEC_GLOB )) + global_magic |=PATHSPEC_LITERAL ; + +return global_magic; +} +/* * Take an element of a pathspec and check for magic signatures. * Append the result to the prefix. Return the magic bitmap.
@@ -105,46 +173,12 @@ static unsigned prefix_pathspec(struct pathspec_item *item, const char *prefix, int prefixlen, const char *elt) { - static int literal_global = -1; -static int glob_global =-1 ; -static int noglob_global =-1 ; -static int icase_global =-1 ; -unsigned magic =0 , element_magic =0 , global_magic =0 ; + unsigned magic = 0, element_magic = 0;const char *copyfrom = elt;char *match;int i, pathspec_prefix =-1 ; -if (literal_global <0 ) - literal_global =git_env_bool (GIT_LITERAL_PATHSPECS_ENVIRONMENT ,0 ); -if (literal_global) - global_magic |=PATHSPEC_LITERAL ; - -if (glob_global <0 ) - glob_global =git_env_bool (GIT_GLOB_PATHSPECS_ENVIRONMENT ,0 ); -if (glob_global) - global_magic |=PATHSPEC_GLOB ; - -if (noglob_global <0 ) - noglob_global =git_env_bool (GIT_NOGLOB_PATHSPECS_ENVIRONMENT ,0 ); - -if (glob_global && noglob_global) -die (_ ("global 'glob' and 'noglob' pathspec settings are incompatible" )); - - -if (icase_global <0 ) - icase_global =git_env_bool (GIT_ICASE_PATHSPECS_ENVIRONMENT ,0 ); -if (icase_global) - global_magic |=PATHSPEC_ICASE ; - -if ((global_magic &PATHSPEC_LITERAL ) && - (global_magic & ~PATHSPEC_LITERAL )) -die (_ ("global 'literal' pathspec setting is incompatible " -"with all other global pathspec settings" )); - -if (flags &PATHSPEC_LITERAL_PATH ) - global_magic =0 ; - -if (elt[0 ] !=':' || literal_global || +if (elt[0 ] !=':' ||get_literal_global () || (flags &PATHSPEC_LITERAL_PATH )) { ;/* nothing to do */ }else if (elt[1 ] =='(' ) {
@@ -208,15 +242,9 @@ static unsigned prefix_pathspec(struct pathspec_item *item, magic |= element_magic; -/* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */ -if (noglob_global && !(magic &PATHSPEC_GLOB )) - global_magic |=PATHSPEC_LITERAL ; - -/* --glob-pathspec is overridden by :(literal) */ -if ((global_magic &PATHSPEC_GLOB ) && (magic &PATHSPEC_LITERAL )) - global_magic &= ~PATHSPEC_GLOB ; - - magic |= global_magic; +/* PATHSPEC_LITERAL_PATH ignores magic */ +if (!(flags &PATHSPEC_LITERAL_PATH )) + magic |=get_global_magic (element_magic);if (pathspec_prefix >=0 && (prefixlen || (prefix && *prefix)))
@@ -242,7 +270,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item,* original. Useful for passing to another command. */ if ((flags &PATHSPEC_PREFIX_ORIGIN ) && - prefixlen && !literal_global) { + prefixlen && !get_literal_global ()) {struct strbuf sb =STRBUF_INIT ;/* Preserve the actual prefix length of each pattern */
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH 12/17 pathspec: create parse_short_magic function
In reply to Brandon WilliamsFactor out the logic responsible for parsing short magic into its own function.
--- pathspec.c | 54 ++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 18 deletions(-)
pathspec.c +36 -18
@@ -156,6 +156,41 @@ static int get_global_magic(int element_magic) }/* +* Parse the pathspec element looking for short magic +* +* saves all magic in 'magic' +* returns the position in 'elem' after all magic has been parsed +*/ +static const char *parse_short_magic (unsigned *magic,const char *elem) +{ +const char *pos; + +for (pos = elem +1 ; *pos && *pos !=':' ; pos++) { +char ch = *pos; +int i; + +if (!is_pathspec_magic (ch)) +break ; + +for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) { +if (pathspec_magic[i].mnemonic == ch) { + *magic |= pathspec_magic[i].bit; +break ; + } + } + +if (ARRAY_SIZE (pathspec_magic) <= i) +die (_ ("Unimplemented pathspec magic '%c' in '%s'" ), + ch, elem); + } + +if (*pos ==':' ) + pos++; + +return pos; +} + +/* * Take an element of a pathspec and check for magic signatures. * Append the result to the prefix. Return the magic bitmap. *
@@ -220,24 +255,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item, copyfrom++; }else {/* shorthand */ -for (copyfrom = elt +1 ; - *copyfrom && *copyfrom !=':' ; - copyfrom++) { -char ch = *copyfrom; - -if (!is_pathspec_magic (ch)) -break ; -for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) -if (pathspec_magic[i].mnemonic == ch) { - element_magic |= pathspec_magic[i].bit; -break ; - } -if (ARRAY_SIZE (pathspec_magic) <= i) -die (_ ("Unimplemented pathspec magic '%c' in '%s'" ), - ch, elt); - } -if (*copyfrom ==':' ) - copyfrom++; + copyfrom =parse_short_magic (&element_magic, elt); } magic |= element_magic;
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH 7/17 mv: small code cleanup
In reply to Brandon WilliamsNow that the call to 'parse_pathspec()' doesn't modify the passed in const char **array there isn't a need to duplicate the pathspec element prior to freeing the intermediate strings. This small cleanup just makes the code a bit easier to read.
--- builtin/mv.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
builtin/mv.c +1 -2
@@ -56,9 +56,8 @@ static const char **internal_copy_pathspec(const char *prefix,/* Copy the pathspec and free the old intermediate strings */ for (i =0 ; i < count; i++) { -const char *match =xstrdup (ps.items[i].match);free ((char *) result[i]); - result[i] = match; + result[i] =xstrdup (ps.items[i].match); }clear_pathspec (&ps);
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH 6/17 pathspec: copy and free owned memory
In reply to Brandon WilliamsThe 'original' string entry in a pathspec_item is only duplicated some of the time, instead always make a copy of the original and take ownership of the memory.
Since both 'match' and 'original' string entries in a pathspec_item are owned by the pathspec struct, they need to be freed when clearing the pathspec struct (in 'clear_pathspec()') and duplicated when copying the pathspec struct (in 'copy_pathspec()').
Also change the type of 'match' and 'original' to 'char *' in order to more explicitly show the ownership of the memory.
--- pathspec.c | 22 ++++++++++++++++++---- pathspec.h | 4 ++-- 2 files changed, 20 insertions(+), 6 deletions(-)
pathspec.c +18 -4
@@ -259,8 +259,9 @@ static unsigned prefix_pathspec(struct pathspec_item *item, }strbuf_addstr (&sb, match); item->original =strbuf_detach (&sb,NULL ); - }else - item->original = elt; + }else { + item->original =xstrdup (elt); + } item->len =strlen (item->match); item->prefix = prefixlen;
@@ -388,8 +389,8 @@ void parse_pathspec(struct pathspec *pathspec,die ("BUG: PATHSPEC_PREFER_CWD requires arguments" ); pathspec->items = item =xcalloc (1 ,sizeof (*item)); - item->match = prefix; - item->original = prefix; + item->match =xstrdup (prefix); + item->original =xstrdup (prefix); item->nowildcard_len = item->len =strlen (prefix); item->prefix = item->len; pathspec->nr =1 ;
@@ -453,13 +454,26 @@ void parse_pathspec(struct pathspec *pathspec,void copy_pathspec (struct pathspec *dst,const struct pathspec *src) { +int i; + *dst = *src;ALLOC_ARRAY (dst->items, dst->nr);COPY_ARRAY (dst->items, src->items, dst->nr); + +for (i =0 ; i < dst->nr; i++) { + dst->items[i].match =xstrdup (src->items[i].match); + dst->items[i].original =xstrdup (src->items[i].original); + } }void clear_pathspec (struct pathspec *pathspec) { +int i; + +for (i =0 ; i < pathspec->nr; i++) { +free (pathspec->items[i].match); +free (pathspec->items[i].original); + }free (pathspec->items); pathspec->items =NULL ; }
pathspec.h +2 -2
@@ -25,8 +25,8 @@ struct pathspec {unsigned magic;int max_depth;struct pathspec_item { -const char *match; -const char *original; +char *match; +char *original;unsigned magic;int len, prefix;int nowildcard_len;
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH 4/17 ls-tree: convert show_recursive to use the pathspec struct interface
In reply to Brandon WilliamsConvert 'show_recursive()' to use the pathspec struct interface from using the '_raw' entry in the pathspec struct.
--- builtin/ls-tree.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-)
builtin/ls-tree.c +5 -7
@@ -31,21 +31,18 @@ static const char * const ls_tree_usage[] = {static int show_recursive (const char *base,int baselen,const char *pathname) { -const char **s; +int i;if (ls_options &LS_RECURSIVE )return 1 ; - s = pathspec._raw; -if (!s) +if (!pathspec.nr)return 0 ; -for (;;) { -const char *spec = *s++; +for (i =0 ; i < pathspec.nr; i++) { +const char *spec = pathspec.items[i].match;int len, speclen; -if (!spec) -return 0 ;if (strncmp (base, spec, baselen))continue ; len =strlen (pathname);
@@ -59,6 +56,7 @@ static int show_recursive(const char *base, int baselen, const char *pathname)continue ;return 1 ; } +return 0 ; } static int show_tree(const unsigned char *sha1, struct strbuf *base,
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH 5/17 pathspec: remove the deprecated get_pathspec function
In reply to Brandon WilliamsNow that all callers of the old 'get_pathspec' interface have been migrated to use the new pathspec struct interface it can be removed from the codebase.
Since there are no more users of the '_raw' field in the pathspec struct it can also be removed. This patch also removes the old functionality of modifying the const char **argv array that was passed into parse_pathspec. Instead the constructed 'match' string (which is a pathspec element with the prefix prepended) is only stored in its corresponding pathspec_item entry.
--- Documentation/technical/api-setup.txt | 2 -- cache.h | 1 - pathspec.c | 42 +++-------------------------------- pathspec.h | 1 - 4 files changed, 3 insertions(+), 43 deletions(-)
Documentation/technical/api-setup.txt +0 -2
@@ -27,8 +27,6 @@ parse_pathspec(). This function takes several arguments: - prefix and args come from cmd_* functions -get_pathspec() is obsolete and should never be used in new code. - parse_pathspec() helps catch unsupported features and reject them politely. At a lower level, different pathspec-related functions may not support the same set of features. Such pathspec-sensitive
cache.h +0 -1
@@ -514,7 +514,6 @@ extern void set_git_work_tree(const char *tree);#define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES" -extern const char **get_pathspec (const char *prefix,const char **pathspec);extern void setup_work_tree (void );extern const char *setup_git_directory_gently (int *);extern const char *setup_git_directory (void );
pathspec.c +3 -39
85 lines not on this page · raw message
pathspec.h +0 -1
@@ -19,7 +19,6 @@#define PATHSPEC_ONESTAR 1/* the pathspec pattern satisfies GFNM_ONESTAR */ struct pathspec { -const char **_raw;/* get_pathspec() result, not freed by clear_pathspec() */ int nr;unsigned int has_wildcard:1 ;unsigned int recursive:1 ;
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH 17/17 pathspec: remove outdated comment
In reply to Brandon WilliamsRemove part of the function header comment to prefix_pathspec as it is no longer relevant.
--- pathspec.c | 9 --------- 1 file changed, 9 deletions(-)
pathspec.c +0 -9
@@ -298,15 +298,6 @@ static void strip_submodule_slash_expensive(struct pathspec_item *item)/* * Take an element of a pathspec and check for magic signatures. * Append the result to the prefix. Return the magic bitmap. -* -* For now, we only parse the syntax and throw out anything other than -* "top" magic. -* -* NEEDSWORK: This needs to be rewritten when we start migrating -* get_pathspec() users to use the "struct pathspec" interface. For -* example, a pathspec element may be marked as case-insensitive, but -* the prefix part must always match literally, and a single stupid -* string cannot express such a case. */ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH 15/17 pathspec: create strip submodule slash helpers
In reply to Brandon WilliamsFactor out the logic responsible for stripping the trailing slash on pathspecs referencing submodules into its own function.
--- pathspec.c | 68 ++++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 42 insertions(+), 26 deletions(-)
pathspec.c +42 -26
@@ -257,6 +257,44 @@ static const char *parse_element_magic(unsigned *magic, int *prefix_len,return parse_short_magic (magic, elem); } +static void strip_submodule_slash_cheap (struct pathspec_item *item) +{ +int i; + +if ((item->len >=1 && item->match[item->len -1 ] =='/' ) && + (i =cache_name_pos (item->match, item->len -1 )) >=0 && +S_ISGITLINK (active_cache[i]->ce_mode)) { + item->len--; + item->match[item->len] ='\0' ; + } +} + +static void strip_submodule_slash_expensive (struct pathspec_item *item) +{ +int i; + +for (i =0 ; i < active_nr; i++) { +struct cache_entry *ce = active_cache[i]; +int ce_len =ce_namelen (ce); + +if (!S_ISGITLINK (ce->ce_mode)) +continue ; + +if (item->len <= ce_len || item->match[ce_len] !='/' || +memcmp (ce->name, item->match, ce_len)) +continue ; + +if (item->len == ce_len +1 ) { +/* strip trailing slash */ + item->len--; + item->match[item->len] ='\0' ; + }else { +die (_ ("Pathspec '%s' is in submodule '%.*s'" ), + item->original, ce_len, ce->name); + } + } +} +/* * Take an element of a pathspec and check for magic signatures. * Append the result to the prefix. Return the magic bitmap.
@@ -278,7 +316,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item,unsigned magic =0 , element_magic =0 ;const char *copyfrom = elt;char *match; -int i, pathspec_prefix =-1 ; +int pathspec_prefix =-1 ;/* PATHSPEC_LITERAL_PATH ignores magic */ if (!(flags &PATHSPEC_LITERAL_PATH )) {
@@ -327,33 +365,11 @@ static unsigned prefix_pathspec(struct pathspec_item *item, item->len =strlen (item->match); item->prefix = prefixlen; -if ((flags &PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP ) && - (item->len >=1 && item->match[item->len -1 ] =='/' ) && - (i =cache_name_pos (item->match, item->len -1 )) >=0 && -S_ISGITLINK (active_cache[i]->ce_mode)) { - item->len--; - match[item->len] ='\0' ; - } +if (flags &PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP ) +strip_submodule_slash_cheap (item);if (flags &PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE ) -for (i =0 ; i < active_nr; i++) { -struct cache_entry *ce = active_cache[i]; -int ce_len =ce_namelen (ce); - -if (!S_ISGITLINK (ce->ce_mode)) -continue ; - -if (item->len <= ce_len || match[ce_len] !='/' || -memcmp (ce->name, match, ce_len)) -continue ; -if (item->len == ce_len +1 ) { -/* strip trailing slash */ - item->len--; - match[item->len] ='\0' ; - }else -die (_ ("Pathspec '%s' is in submodule '%.*s'" ), - elt, ce_len, ce->name); - } +strip_submodule_slash_expensive (item);if (magic &PATHSPEC_LITERAL ) item->nowildcard_len = item->len;
Signature · 1 line
2.8.0.rc3.226.g39d4020
Re: [PATCH 10/17] pathspec: simpler logic to prefix original pathspec elements
In reply to Brandon Williamspathspec.c static unsigned prefix_pathspec(struct pathspec_item *item, · line 256
Quoted 10 lines · Brandon Williams
struct strbuf sb = STRBUF_INIT;pathspec.c +0
-9-if (prefixlen && !literal_global) { -/* Preserve the actual prefix length of each pattern */ -if (short_magic) -prefix_short_magic (&sb, prefixlen, short_magic); -else if (long_magic_end) { -strbuf_add (&sb, elt, long_magic_end - elt); -strbuf_addf (&sb,",prefix:%d)" , prefixlen); - }else -strbuf_addf (&sb,":(prefix:%d)" , prefixlen);
This fixes the issue with add -p . mentioned somewhere else on the mailing list.
pathspec.c static unsigned prefix_pathspec(struct pathspec_item *item, · line 250
Quoted 5 lines
pathspec.c +4
-1- } + +/* Preserve the actual prefix length of each pattern */ +prefix_magic (&sb, prefixlen, element_magic); +
Did you find a reason why we passed magic literally, i.e. short magic was passed as short magic and long magic as long magic before?
I cannot think of any reason why that would have been the case, but I assume there had to be a reason for that.
Another note: This collides with the attr system refactoring, which I postpone redoing until the submodule checkout is done, so maybe you want to pickup this patch:
https://public-inbox.org/git/[email protected]/
which only relies on one patch prior
https://public-inbox.org/git/[email protected]/
Re: [PATCH 16/17] pathspec: small readability changes
In reply to Brandon WilliamsQuoted 2 lines · Brandon Williams
A few small changes to improve readability. This is done by grouping related assignments, adding blank lines, ensuring lines are <80 characters, etc.
The 'etc' sounds a bit sloppy in the commit message. Maybe s/etc/and adding proper comments/ ?
Code looks good.
Re: [PATCH 10/17] pathspec: simpler logic to prefix original pathspec elements
In reply to Stefan BellerQuoted 24 lines · Stefan Beller
pathspec.c static unsigned prefix_pathspec(struct pathspec_item *item, · line 256
Quoted 10 lines · Brandon Williams
struct strbuf sb = STRBUF_INIT;pathspec.c +0
-9-if (prefixlen && !literal_global) { -/* Preserve the actual prefix length of each pattern */ -if (short_magic) -prefix_short_magic (&sb, prefixlen, short_magic); -else if (long_magic_end) { -strbuf_add (&sb, elt, long_magic_end - elt); -strbuf_addf (&sb,",prefix:%d)" , prefixlen); - }else -strbuf_addf (&sb,":(prefix:%d)" , prefixlen);This fixes the issue with add -p . mentioned somewhere else on the mailing list.
pathspec.c static unsigned prefix_pathspec(struct pathspec_item *item, · line 250
Quoted 5 lines
pathspec.c +4
-1- } + +/* Preserve the actual prefix length of each pattern */ +prefix_magic (&sb, prefixlen, element_magic); +Did you find a reason why we passed magic literally, i.e. short magic was passed as short magic and long magic as long magic before?
Quoted 2 lines
I cannot think of any reason why that would have been the case, but I assume there had to be a reason for that.
nope, perhaps it was because we technically already have the long magic string and the short magic needs to be converted to long magic (as you can't mix short and long magic).
Quoted 6 lines
Another note: This collides with the attr system refactoring, which I postpone redoing until the submodule checkout is done, so maybe you want to pickup this patch:
https://public-inbox.org/git/[email protected]/
which only relies on one patch prior
https://public-inbox.org/git/[email protected]/
After looking at those patches I think I do something extremely similar in a future patch in this series, the parse_long_magic patch.
Signature · 1 line
Brandon Williams
Re: [PATCH 01/17] mv: convert to using pathspec struct interface
In reply to Brandon WilliamsQuoted 10 lines · Brandon Williams
Convert the '
internal_copy_pathspec()' function to use the pathspec struct interface from using the deprecated 'get_pathspec()' interface.In addition to this, fix a memory leak caused by only duplicating some of the pathspec elements. Instead always duplicate all of the the pathspec elements as an intermediate step (with modificationed based on the passed in flags). This way the intermediate strings can then be freed prior to duplicating the result of
parse_pathspec(which contains each of the elements with the prefix prepended).
Quoted 4 lines
Signed-off-by Brandon Williams <[email protected]>--- builtin/mv.c | 45 ++++++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 13 deletions(-)
Stefan did something similar last year [1] but I couldn't find why it did not get merged. Not sure if the reasons are still relevant or not...
[1] http://public-inbox.org/git/%[email protected]%3E/
Quoted 51 lines
builtin/mv.c +19
-12@@ -4,6 +4,7 @@* Copyright (C) 2006 Johannes Schindelin */ #include "builtin.h" +#include "pathspec.h" #include "lockfile.h" #include "dir.h" #include "cache-tree.h" @@ -25,25 +26,43 @@ static const char **internal_copy_pathspec(const char *prefix, {int i;const char **result; +struct pathspec ps;ALLOC_ARRAY (result, count +1 ); -COPY_ARRAY (result, pathspec, count); - result[count] =NULL ; + +/* Create an intermediate copy of the pathspec based on the flags */ for (i =0 ; i < count; i++) { -int length =strlen (result[i]); +int length =strlen (pathspec[i]);int to_copy = length; +char *it;while (!(flags &KEEP_TRAILING_SLASH ) && - to_copy >0 &&is_dir_sep (result[i][to_copy -1 ])) + to_copy >0 &&is_dir_sep (pathspec[i][to_copy -1 ])) to_copy--; -if (to_copy != length || flags &DUP_BASENAME ) { -char *it =xmemdupz (result[i], to_copy); -if (flags &DUP_BASENAME ) { - result[i] =xstrdup (basename (it)); -free (it); - }else - result[i] = it; - } + + it =xmemdupz (pathspec[i], to_copy); +if (flags &DUP_BASENAME ) { + result[i] =xstrdup (basename (it)); +free (it); + }else + result[i] = it; + } + result[count] =NULL ; + + parse_pathspec(&ps, + PATHSPEC_ALL_MAGIC &
builtin/mv.c static const char **internal_copy_pathspec(const char *prefix, · line 61
Quoted 10 lines
builtin/mv.c +10
-0+ ~(PATHSPEC_FROMTOP |PATHSPEC_LITERAL ), +PATHSPEC_KEEP_ORDER |PATHSPEC_PREFER_CWD , + prefix, result); +assert (count == ps.nr); + +/* Copy the pathspec and free the old intermediate strings */ +for (i =0 ; i < count; i++) { +const char *match =xstrdup (ps.items[i].match); +free ((char *) result[i]); + result[i] = match;
Sigh.. it looks so weird that we do all the parsing (in a _copy_ pathspec function) then remove struct pathspec and return the plain string. I guess we can't do anything more until we rework cmd_mv code to handle pathspec natively.
At the least I think we should rename this function to something else. But if you have time I really wish we could kill this function. I haven't stared at cmd_mv() long and hard, but it looks to me that we combining two separate functionalities in the same function here.
If "mv" takes n arguments, then the first <n-1> arguments may be pathspec, the last one is always a plain path. The "dest_path = internal_copy_pathspec..." could be as simple as "dest_path = prefix_path(argv[argc - 1])". the special treatment for this last argument [1] can live here. Then, we can do parse_pathspec for the <n-1> arguments in cmd_mv(). It's still far from perfect, because cmd_mv can't handle pathspec properly, but it reduces the messy mess in internal_copy_pathspec a bit, I hope.
[1] c57f628 (mv: let 'git mv file no-such-dir/' error out - 2013-12-03)
Quoted 11 lines
builtin/mv.c +3
-2} -return get_pathspec (prefix, result); + +clear_pathspec (&ps); +return result; }static const char *add_slash (const char *path) --2.8.0.rc3.226.g39d4020
Signature · 1 line
Duy
Re: [PATCH 02/17] dir: convert create_simplify to use the pathspec struct interface
In reply to Brandon WilliamsQuoted 2 lines · Brandon Williams
Convert '
create_simplify()' to use the pathspec struct interface from using the '_raw' entry in the pathspec.
It would be even better to kill this create_simplify() and let simplify_away() handle struct pathspec directly.
There is a bug in this code, that might have been found if we simpify_away() handled pathspec directly: the memcmp() in simplify_away() will not play well with :(icase) magic. My bad. If :(icase) is used, the easiest/safe way is simplify nothing. Later on maybe we can teach simplify_away() to do strncasecmp instead. We could ignore exclude patterns there too (although not excluding is not a bug).
Signature · 1 line
Duy
Re: [PATCH 03/17] dir: convert fill_directory to use the pathspec struct interface
In reply to Brandon WilliamsQuoted 13 lines · Brandon Williams
Convert '
fill_directory()' to use the pathspec struct interface from using the '_raw' entry in the pathspec struct.Signed-off-by Brandon Williams <[email protected]>--- dir.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)dir.c +0
-0
dir.c int fill_directory(struct dir_struct *dir, const struct pathspec *pathspec) · line 192
Quoted 6 lines
dir.c +2
-1@@ -188,7 +188,8 @@ int fill_directory(struct dir_struct *dir, const struct pathspec *pathspec) len =common_prefix_len (pathspec);/* Read the directory and prune it */ -read_directory (dir, pathspec->nr ? pathspec->_raw[0 ] :"" , len, pathspec); +read_directory (dir, pathspec->nr ? pathspec->items[0 ].match :"" , + len, pathspec);
Or even better, use common_prefix()'s return value here. I took me a while to realize this code was not buggy. It is fine to just pick the first item because the first <len> characters of _all_ pathspec items must be the same. Something like this
prefix = common_prefix(..) read_directory(..., prefix, strlen(prefix), pathspec);
expresses it much better. Yeah one extra mem allocation, no big deal since fill_directory() is not called very often.
Quoted 6 lines
dir.c +0
-1return len; } --2.8.0.rc3.226.g39d4020
Signature · 1 line
Duy
Re: [PATCH 04/17] ls-tree: convert show_recursive to use the pathspec struct interface
In reply to Brandon WilliamsQuoted 2 lines · Brandon Williams
Convert '
show_recursive()' to use the pathspec struct interface from using the '_raw' entry in the pathspec struct.
Slightly off-topic (sorry, but you made me look at this code! :D), could you update the magic_mask argument of parse_pathspec() in this file to PATHSPEC_ALL_MAGIC & ~(PATHSPEC_FROMTOP | PATHSPEC_LITERAL)? It makes sure all future magic will be caught as unsupported (and I think Stefan is adding one, but understandably he did not find this code).
I think it's in the spirit of renaming _raw to match too. By limiting magic to fromtop and literal, we are sure match can only be path and nothing else, which is good because this show_recursive can't handle anything else either.
Quoted 47 lines
Signed-off-by Brandon Williams <[email protected]>--- builtin/ls-tree.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-)builtin/ls-tree.c +5
-8@@ -31,21 +31,18 @@ static const char * const ls_tree_usage[] = {static int show_recursive (const char *base,int baselen,const char *pathname) { -const char **s; +int i;if (ls_options &LS_RECURSIVE )return 1 ; - s = pathspec._raw; -if (!s) +if (!pathspec.nr)return 0 ; -for (;;) { -const char *spec = *s++; +for (i =0 ; i < pathspec.nr; i++) { +const char *spec = pathspec.items[i].match;int len, speclen; -if (!spec) -return 0 ;if (strncmp (base, spec, baselen))continue ; len =strlen (pathname);@@ -59,6 +56,7 @@ static int show_recursive(const char *base, int baselen, const char *pathname)continue ;return 1 ; } +return 0 ; } static int show_tree(const unsigned char *sha1, struct strbuf *base, --2.8.0.rc3.226.g39d4020
Signature · 1 line
Duy
Re: [PATCH 09/17] pathspec: always show mnemonic and name in unsupported_magic
In reply to Brandon Williamspathspec.c void parse_pathspec(struct pathspec *pathspec, · line 418
Quoted 11 lines · Brandon Williams
pathspec.c +1
-2@@ -413,10 +411,9 @@ void parse_pathspec(struct pathspec *pathspec, prefixlen = prefix ?strlen (prefix) :0 ;for (i =0 ; i < n; i++) { -unsigned short_magic; entry = argv[i]; - item[i].magic =prefix_pathspec (item + i, &short_magic, + item[i].magic =prefix_pathspec (item + i, flags, prefix, prefixlen, entry);
The final output looks a bit ...um.. strangely tall, with the first two lines that have one argument each, then the last line comes with three arguments. Maybe put 'flags' in the same line as 'item + i'?
Quoted 2 lines
pathspec.c +0
-0if ((flags & PATHSPEC_LITERAL_PATH) &&
Signature · 1 line
Duy
Re: [PATCH 09/17] pathspec: always show mnemonic and name in unsupported_magic
In reply to Brandon Williamspathspec.c void parse_pathspec(struct pathspec *pathspec, · line 426
Quoted 7 lines · Brandon Williams
pathspec.c +1
-2@@ -426,8 +423,7 @@ void parse_pathspec(struct pathspec *pathspec, nr_exclude++;if (item[i].magic & magic_mask)unsupported_magic (entry, - item[i].magic & magic_mask, - short_magic); + item[i].magic & magic_mask);
Same here. Maybe put both arguments in the same line. It looks a bit better. (sorry for two mails on the same patch, I'm reading the final output first before going through individual patches that breaks this function down)
Quoted 3 lines
pathspec.c +0
-0if ((flags &PATHSPEC_SYMLINK_LEADING_PATH ) &&has_symlink_leading_path (item[i].match, item[i].len)) {
Signature · 1 line
Duy
Re: [PATCH 16/17] pathspec: small readability changes
In reply to Brandon WilliamsQuoted 52 lines · Brandon Williams
A few small changes to improve readability. This is done by grouping related assignments, adding blank lines, ensuring lines are <80 characters, etc.
Signed-off-by Brandon Williams <[email protected]>--- pathspec.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)pathspec.c +7
-4@@ -334,6 +334,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item,if ((magic &PATHSPEC_LITERAL ) && (magic &PATHSPEC_GLOB ))die (_ ("%s: 'literal' and 'glob' are incompatible" ), elt); +/* Create match string which will be used for pathspec matching */ if (pathspec_prefix >=0 ) { match =xstrdup (copyfrom); prefixlen = pathspec_prefix;@@ -341,11 +342,16 @@ static unsigned prefix_pathspec(struct pathspec_item *item, match =xstrdup (copyfrom); prefixlen =0 ; }else { - match =prefix_path_gently (prefix, prefixlen, &prefixlen, copyfrom); + match =prefix_path_gently (prefix, prefixlen, + &prefixlen, copyfrom);if (!match)die (_ ("%s: '%s' is outside repository" ), elt, copyfrom); } + item->match = match; + item->len =strlen (item->match); + item->prefix = prefixlen; +/* * Prefix the pathspec (keep all magic) and assign to * original. Useful for passing to another command. @@ -362,8 +368,6 @@ static unsigned prefix_pathspec(struct pathspec_item *item, }else { item->original =xstrdup (elt); } - item->len =strlen (item->match); - item->prefix = prefixlen;if (flags &PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP )strip_submodule_slash_cheap (item);@@ -371,13 +375,14 @@ static unsigned prefix_pathspec(struct pathspec_item *item,if (flags &PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE )strip_submodule_slash_expensive (item); -if (magic &PATHSPEC_LITERAL )
pathspec.c static unsigned prefix_pathspec(struct pathspec_item *item, · line 386
Quoted 10 lines
pathspec.c +3
-1+if (magic &PATHSPEC_LITERAL ) { item->nowildcard_len = item->len; - else { + }else { item->nowildcard_len =simple_length (item->match);if (item->nowildcard_len < prefixlen) item->nowildcard_len = prefixlen; } + item->flags =0 ;
You probably can move this line up with the others too.
And since you have broken this function down so nicely, it made me see that we could do
item->magic = magic instead of returning "magic" at the end, which is assigned to item->magic anyway by the caller.
Signature · 1 line
Duy
Re: [PATCH 11/17] pathspec: factor global magic into its own function
In reply to Brandon WilliamsQuoted 14 lines · Brandon Williams
Create helper functions to read the global magic environment variables in additon to factoring out the global magic gathering logic into its own function.
Signed-off-by Brandon Williams <[email protected]>--- pathspec.c | 120 +++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 74 insertions(+), 46 deletions(-)pathspec.c +0
-0
pathspec.c static void prefix_magic(struct strbuf *sb, int prefixlen, unsigned magic) · line 96
Quoted 10 lines
pathspec.c +7
-0@@ -87,6 +87,74 @@ static void prefix_magic(struct strbuf *sb, int prefixlen, unsigned magic)strbuf_addf (sb,",prefix:%d)" , prefixlen); } +static inline int get_literal_global (void ) +{ +static int literal_global =-1 ; + +if (literal_global <0 ) + literal_global =git_env_bool (GIT_LITERAL_PATHSPECS_ENVIRONMENT , +0 );
These zeros look so lonely. I know it would exceed 80 columns if we put it on the previous line. But I think it's ok for occasional exceptions. Or you could rename noglob_global to noglob.
Signature · 1 line
Duy
Re: [PATCH 16/17] pathspec: small readability changes
In reply to Brandon Williamspathspec.c static unsigned prefix_pathspec(struct pathspec_item *item,
Quoted 13 lines · Brandon Williams
A few small changes to improve readability. This is done by grouping related assignments, adding blank lines, ensuring lines are <80 characters, etc.
Signed-off-by Brandon Williams <[email protected]>--- pathspec.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)pathspec.c +0
-0@@ -334,6 +334,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item,
btw, since this function has stopped being "just prefix pathspec" for a long time, perhaps rename it to parse_pathspec_item, or something.
Signature · 1 line
Duy
Re: [PATCH 16/17] pathspec: small readability changes
Quoted 9 lines · Duy Nguyen
Quoted 8 lines · Brandon Williams
A few small changes to improve readability. This is done by grouping related assignments, adding blank lines, ensuring lines are <80 characters, etc.
Signed-off-by Brandon Williams <[email protected]>--- pathspec.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)
Quoted 8 lines
pathspec.c static unsigned prefix_pathspec(struct pathspec_item *item,
Quoted 5 lines
pathspec.c +0
-0@@ -334,6 +334,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item,btw, since this function has stopped being "just prefix pathspec" for a long time, perhaps rename it to
parse_pathspec_item, or something.
Not specifically responding to this comment, but thanks for all the constructive feedback messages.
Quoted 8 lines · Brandon Williams
The intent of this series is to cleanup some of the pathspec initialization code as well as finally migrating the remaining users of the
_rawfield orget_pathspec()to the pathspec struct interface. This way both the_rawfield andget_pathspec()can be removed from the codebase. This also removes the functionality whereparse_pathspec()modified the const char * argv array that was passed in (which felt kind of odd to me as I wouldn't have expected the passed in array to be modified).
Quoted 4 lines
I also noticed that there are memory leaks associated with the 'original' and 'match' strings. To fix this the pathspec struct needed to take ownership of the memory for these fields so that they can be cleaned up when clearing the pathspec struct.
Both good goals. Thanks for working on this.
Re: [PATCH 16/17] pathspec: small readability changes
In reply to Duy NguyenQuoted 9 lines · Duy Nguyen
Quoted 8 lines · Brandon Williams
A few small changes to improve readability. This is done by grouping related assignments, adding blank lines, ensuring lines are <80 characters, etc.
Signed-off-by Brandon Williams <[email protected]>--- pathspec.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)
Quoted 8 lines
pathspec.c static unsigned prefix_pathspec(struct pathspec_item *item,
Quoted 5 lines
pathspec.c +0
-0@@ -334,6 +334,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item,btw, since this function has stopped being "just prefix pathspec" for a long time, perhaps rename it to
parse_pathspec_item, or something.
I was thinking about doing that after I sent this out. Glad you also pointed that out.
Signature · 1 line
Brandon Williams
Re: [PATCH 11/17] pathspec: factor global magic into its own function
In reply to Duy NguyenQuoted 18 lines · Duy Nguyen
Quoted 17 lines · Brandon Williams
Create helper functions to read the global magic environment variables in additon to factoring out the global magic gathering logic into its own function.
Signed-off-by Brandon Williams <[email protected]>--- pathspec.c | 120 +++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 74 insertions(+), 46 deletions(-)pathspec.c +0
-0@@ -87,6 +87,74 @@ static void prefix_magic(struct strbuf *sb, int prefixlen, unsigned magic)strbuf_addf (sb,",prefix:%d)" , prefixlen); }
Quoted 11 lines
pathspec.c static void prefix_magic(struct strbuf *sb, int prefixlen, unsigned magic) · line 96
Quoted 7 lines
pathspec.c +7
-0+static inline int get_literal_global (void ) +{ +static int literal_global =-1 ; + +if (literal_global <0 ) + literal_global =git_env_bool (GIT_LITERAL_PATHSPECS_ENVIRONMENT , +0 );These zeros look so lonely. I know it would exceed 80 columns if we put it on the previous line. But I think it's ok for occasional exceptions. Or you could rename
noglob_globalto noglob.
I was thinking the same thing but was so torn between the char limit. I think it's probably ok to rename these vars by drooping the global since the function name themselves indicate they are global.
Signature · 1 line
Brandon Williams
Re: [PATCH 09/17] pathspec: always show mnemonic and name in unsupported_magic
In reply to Duy NguyenQuoted 13 lines · Duy Nguyen
pathspec.c void parse_pathspec(struct pathspec *pathspec, · line 426
Quoted 7 lines · Brandon Williams
pathspec.c +1
-2@@ -426,8 +423,7 @@ void parse_pathspec(struct pathspec *pathspec, nr_exclude++;if (item[i].magic & magic_mask)unsupported_magic (entry, - item[i].magic & magic_mask, - short_magic); + item[i].magic & magic_mask);Same here. Maybe put both arguments in the same line. It looks a bit better. (sorry for two mails on the same patch, I'm reading the final output first before going through individual patches that breaks this function down)
All good. Sometimes its easier to parse comments if they are in multiple small emails. I don't mind getting lots of mail :)
Quoted 6 lines
Quoted 4 lines
pathspec.c +0
-0if ((flags &PATHSPEC_SYMLINK_LEADING_PATH ) &&has_symlink_leading_path (item[i].match, item[i].len)) {Signature · 1 line
Duy
Signature · 1 line
Brandon Williams
Re: [PATCH 09/17] pathspec: always show mnemonic and name in unsupported_magic
In reply to Duy NguyenQuoted 8 lines · Duy Nguyen
Quoted 7 lines · Brandon Williams
pathspec.c +0
-1@@ -413,10 +411,9 @@ void parse_pathspec(struct pathspec *pathspec, prefixlen = prefix ?strlen (prefix) :0 ;for (i =0 ; i < n; i++) { -unsigned short_magic; entry = argv[i];
Quoted 8 lines
pathspec.c void parse_pathspec(struct pathspec *pathspec, · line 418
Quoted 4 lines
pathspec.c +1
-1- item[i].magic =prefix_pathspec (item + i, &short_magic, + item[i].magic =prefix_pathspec (item + i, flags, prefix, prefixlen, entry);The final output looks a bit ...um.. strangely tall, with the first two lines that have one argument each, then the last line comes with three arguments. Maybe put 'flags' in the same line as 'item + i'?
Yep you're right, it does look a bit funny.
Signature · 1 line
Brandon Williams
Re: [PATCH 04/17] ls-tree: convert show_recursive to use the pathspec struct interface
In reply to Duy NguyenQuoted 11 lines · Duy Nguyen
Quoted 2 lines · Brandon Williams
Convert '
show_recursive()' to use the pathspec struct interface from using the '_raw' entry in the pathspec struct.Slightly off-topic (sorry, but you made me look at this code! :D), could you update the
magic_maskargument ofparse_pathspec()in this file toPATHSPEC_ALL_MAGIC& ~(PATHSPEC_FROMTOP |PATHSPEC_LITERAL)? It makes sure all future magic will be caught as unsupported (and I think Stefan is adding one, but understandably he did not find this code).
Quoted 4 lines
I think it's in the spirit of renaming
_rawto match too. By limiting magic to fromtop and literal, we are sure match can only be path and nothing else, which is good because thisshow_recursivecan't handle anything else either.
Can do.
Signature · 1 line
Brandon Williams
Re: [PATCH 03/17] dir: convert fill_directory to use the pathspec struct interface
In reply to Duy NguyenQuoted 26 lines · Duy Nguyen
Quoted 13 lines · Brandon Williams
Convert '
fill_directory()' to use the pathspec struct interface from using the '_raw' entry in the pathspec struct.Signed-off-by Brandon Williams <[email protected]>--- dir.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)dir.c +0
-0dir.c int fill_directory(struct dir_struct *dir, const struct pathspec *pathspec) · line 192
Quoted 6 lines
dir.c +2
-1@@ -188,7 +188,8 @@ int fill_directory(struct dir_struct *dir, const struct pathspec *pathspec) len =common_prefix_len (pathspec);/* Read the directory and prune it */ -read_directory (dir, pathspec->nr ? pathspec->_raw[0 ] :"" , len, pathspec); +read_directory (dir, pathspec->nr ? pathspec->items[0 ].match :"" , + len, pathspec);Or even better, use common_prefix()'s return value here. I took me a while to realize this code was not buggy. It is fine to just pick the first item because the first <len> characters of _all_ pathspec items must be the same. Something like this
Quoted 5 lines
prefix =
common_prefix(..)read_directory(..., prefix, strlen(prefix), pathspec);expresses it much better. Yeah one extra mem allocation, no big deal since
fill_directory()is not called very often.
I didn't even notice that. Now looking at this you're right that its not immediately obvious that what's there is correct. I'll change this.
Quoted 9 lines
Quoted 7 lines
dir.c +0
-1return len; } --2.8.0.rc3.226.g39d4020
Signature · 1 line
Duy
Signature · 1 line
Brandon Williams
Re: [PATCH 16/17] pathspec: small readability changes
In reply to Duy NguyenQuoted 52 lines · Duy Nguyen
Quoted 51 lines · Brandon Williams
A few small changes to improve readability. This is done by grouping related assignments, adding blank lines, ensuring lines are <80 characters, etc.
Signed-off-by Brandon Williams <[email protected]>--- pathspec.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)pathspec.c +7
-3@@ -334,6 +334,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item,if ((magic &PATHSPEC_LITERAL ) && (magic &PATHSPEC_GLOB ))die (_ ("%s: 'literal' and 'glob' are incompatible" ), elt); +/* Create match string which will be used for pathspec matching */ if (pathspec_prefix >=0 ) { match =xstrdup (copyfrom); prefixlen = pathspec_prefix;@@ -341,11 +342,16 @@ static unsigned prefix_pathspec(struct pathspec_item *item, match =xstrdup (copyfrom); prefixlen =0 ; }else { - match =prefix_path_gently (prefix, prefixlen, &prefixlen, copyfrom); + match =prefix_path_gently (prefix, prefixlen, + &prefixlen, copyfrom);if (!match)die (_ ("%s: '%s' is outside repository" ), elt, copyfrom); } + item->match = match; + item->len =strlen (item->match); + item->prefix = prefixlen; +/* * Prefix the pathspec (keep all magic) and assign to * original. Useful for passing to another command. @@ -362,8 +368,6 @@ static unsigned prefix_pathspec(struct pathspec_item *item, }else { item->original =xstrdup (elt); } - item->len =strlen (item->match); - item->prefix = prefixlen;if (flags &PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP )strip_submodule_slash_cheap (item);@@ -371,13 +375,14 @@ static unsigned prefix_pathspec(struct pathspec_item *item,if (flags &PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE )strip_submodule_slash_expensive (item);
Quoted 13 lines
pathspec.c static unsigned prefix_pathspec(struct pathspec_item *item, · line 386
Quoted 11 lines
pathspec.c +3
-2-if (magic &PATHSPEC_LITERAL ) +if (magic &PATHSPEC_LITERAL ) { item->nowildcard_len = item->len; -else { + }else { item->nowildcard_len =simple_length (item->match);if (item->nowildcard_len < prefixlen) item->nowildcard_len = prefixlen; } + item->flags =0 ;You probably can move this line up with the others too.
I didn't move the item->flags assignment up since the code immediately following this assignment deal with setting item->flags. I made more sense to keep them grouped.
Signature · 1 line
Brandon Williams
Re: [PATCH 02/17] dir: convert create_simplify to use the pathspec struct interface
In reply to Duy NguyenQuoted 7 lines · Duy Nguyen
Quoted 2 lines · Brandon Williams
Convert '
create_simplify()' to use the pathspec struct interface from using the '_raw' entry in the pathspec.It would be even better to kill this
create_simplify()and letsimplify_away()handle struct pathspec directly.
Quoted 7 lines
There is a bug in this code, that might have been found if we
simpify_away()handled pathspec directly: thememcmp()insimplify_away()will not play well with :(icase) magic. My bad. If :(icase) is used, the easiest/safe way is simplify nothing. Later on maybe we can teachsimplify_away()to do strncasecmp instead. We could ignore exclude patterns there too (although not excluding is not a bug).
So are you implying that the simplify struct needs to be killed? That way the pathspec struct itself is being passed around instead?
Signature · 1 line
Brandon Williams
Re: [PATCH 01/17] mv: convert to using pathspec struct interface
In reply to Duy NguyenQuoted 112 lines · Duy Nguyen
Quoted 10 lines · Brandon Williams
Convert the '
internal_copy_pathspec()' function to use the pathspec struct interface from using the deprecated 'get_pathspec()' interface.In addition to this, fix a memory leak caused by only duplicating some of the pathspec elements. Instead always duplicate all of the the pathspec elements as an intermediate step (with modificationed based on the passed in flags). This way the intermediate strings can then be freed prior to duplicating the result of
parse_pathspec(which contains each of the elements with the prefix prepended).Quoted 4 lines
Signed-off-by Brandon Williams <[email protected]>--- builtin/mv.c | 45 ++++++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 13 deletions(-)Stefan did something similar last year [1] but I couldn't find why it did not get merged. Not sure if the reasons are still relevant or not...
[1] http://public-inbox.org/git/%[email protected]%3E/
Quoted 51 lines
builtin/mv.c +19
-12@@ -4,6 +4,7 @@* Copyright (C) 2006 Johannes Schindelin */ #include "builtin.h" +#include "pathspec.h" #include "lockfile.h" #include "dir.h" #include "cache-tree.h" @@ -25,25 +26,43 @@ static const char **internal_copy_pathspec(const char *prefix, {int i;const char **result; +struct pathspec ps;ALLOC_ARRAY (result, count +1 ); -COPY_ARRAY (result, pathspec, count); - result[count] =NULL ; + +/* Create an intermediate copy of the pathspec based on the flags */ for (i =0 ; i < count; i++) { -int length =strlen (result[i]); +int length =strlen (pathspec[i]);int to_copy = length; +char *it;while (!(flags &KEEP_TRAILING_SLASH ) && - to_copy >0 &&is_dir_sep (result[i][to_copy -1 ])) + to_copy >0 &&is_dir_sep (pathspec[i][to_copy -1 ])) to_copy--; -if (to_copy != length || flags &DUP_BASENAME ) { -char *it =xmemdupz (result[i], to_copy); -if (flags &DUP_BASENAME ) { - result[i] =xstrdup (basename (it)); -free (it); - }else - result[i] = it; - } + + it =xmemdupz (pathspec[i], to_copy); +if (flags &DUP_BASENAME ) { + result[i] =xstrdup (basename (it)); +free (it); + }else + result[i] = it; + } + result[count] =NULL ; + + parse_pathspec(&ps, + PATHSPEC_ALL_MAGIC &builtin/mv.c static const char **internal_copy_pathspec(const char *prefix, · line 61
Quoted 10 lines
builtin/mv.c +10
-0+ ~(PATHSPEC_FROMTOP |PATHSPEC_LITERAL ), +PATHSPEC_KEEP_ORDER |PATHSPEC_PREFER_CWD , + prefix, result); +assert (count == ps.nr); + +/* Copy the pathspec and free the old intermediate strings */ +for (i =0 ; i < count; i++) { +const char *match =xstrdup (ps.items[i].match); +free ((char *) result[i]); + result[i] = match;Sigh.. it looks so weird that we do all the parsing (in a _copy_ pathspec function) then remove struct pathspec and return the plain string. I guess we can't do anything more until we rework
cmd_mvcode to handle pathspec natively.At the least I think we should rename this function to something else. But if you have time I really wish we could kill this function. I haven't stared at
cmd_mv()long and hard, but it looks to me that we combining two separate functionalities in the same function here.If "mv" takes n arguments, then the first <n-1> arguments may be pathspec, the last one is always a plain path. The "
dest_path=internal_copy_pathspec..." could be as simple as "dest_path=prefix_path(argv[argc - 1])". the special treatment for this last argument [1] can live here. Then, we can doparse_pathspecfor the <n-1> arguments incmd_mv(). It's still far from perfect, becausecmd_mvcan't handle pathspec properly, but it reduces the messy mess ininternal_copy_pathspeca bit, I hope.[1] c57f628 (mv: let 'git mv file no-such-dir/' error out - 2013-12-03)
Quoted 7 lines
builtin/mv.c +3
-1} -return get_pathspec (prefix, result); + +clear_pathspec (&ps); +return result; }
Quoted 9 lines
Quoted 4 lines
builtin/mv.c +0
-1static const char *add_slash (const char *path) --2.8.0.rc3.226.g39d4020
Signature · 1 line
Duy
Actually, after looking at this a bit more it seems like we could technically use prefix_path for both source and dest (based on how the current code is structured) since the source's provied must all exist (as in no wildcards are allowed). We could drop using the pathspec struct completely in addition to renaming the function (to what I'm still unsure). I agree that this code should probably be rewritten and made a bit cleaner, I don't know if that fits in the scope of this series or should be done as a followup patch. If you think it fits here then I can try and find some time to do the rework.
Signature · 1 line
Brandon Williams
Re: [PATCH 11/17] pathspec: factor global magic into its own function
In reply to Brandon WilliamsQuoted 31 lines · Brandon Williams
Quoted 18 lines · Duy Nguyen
Quoted 17 lines · Brandon Williams
Create helper functions to read the global magic environment variables in additon to factoring out the global magic gathering logic into its own function.
Signed-off-by Brandon Williams <[email protected]>--- pathspec.c | 120 +++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 74 insertions(+), 46 deletions(-)pathspec.c +0
-0@@ -87,6 +87,74 @@ static void prefix_magic(struct strbuf *sb, int prefixlen, unsigned magic)strbuf_addf (sb,",prefix:%d)" , prefixlen); }Quoted 11 lines
pathspec.c static void prefix_magic(struct strbuf *sb, int prefixlen, unsigned magic) · line 96
Quoted 7 lines
pathspec.c +7
-0+static inline int get_literal_global (void ) +{ +static int literal_global =-1 ; + +if (literal_global <0 ) + literal_global =git_env_bool (GIT_LITERAL_PATHSPECS_ENVIRONMENT , +0 );These zeros look so lonely. I know it would exceed 80 columns if we put it on the previous line. But I think it's ok for occasional exceptions. Or you could rename
noglob_globalto noglob.
Quoted 3 lines
I was thinking the same thing but was so torn between the char limit. I think it's probably ok to rename these vars by drooping the global since the function name themselves indicate they are global.
Exactly. I almost suggested just "ret" for that reason, but it was a bit on the extreme side, relying entirely on the function's name for context.
Signature · 1 line
Duy
Re: [PATCH 16/17] pathspec: small readability changes
In reply to Brandon WilliamsQuoted 28 lines · Brandon Williams
Quoted 14 lines · Brandon Williams
Quoted 13 lines
pathspec.c +0
-2@@ -362,8 +368,6 @@ static unsigned prefix_pathspec(struct pathspec_item *item, }else { item->original =xstrdup (elt); } - item->len =strlen (item->match); - item->prefix = prefixlen;if (flags &PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP )strip_submodule_slash_cheap (item);@@ -371,13 +375,14 @@ static unsigned prefix_pathspec(struct pathspec_item *item,if (flags &PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE )strip_submodule_slash_expensive (item);Quoted 13 lines
pathspec.c static unsigned prefix_pathspec(struct pathspec_item *item, · line 386
Quoted 11 lines
pathspec.c +3
-2-if (magic &PATHSPEC_LITERAL ) +if (magic &PATHSPEC_LITERAL ) { item->nowildcard_len = item->len; -else { + }else { item->nowildcard_len =simple_length (item->match);if (item->nowildcard_len < prefixlen) item->nowildcard_len = prefixlen; } + item->flags =0 ;You probably can move this line up with the others too.
Quoted 3 lines
I didn't move the
item->flagsassignment up since the code immediately following this assignment deal with settingitem->flags. I made more sense to keep them grouped.
It's probably why I put it there in the beginning :) Yes let's leave it where it is then.
Signature · 1 line
Duy
Re: [PATCH 01/17] mv: convert to using pathspec struct interface
In reply to Brandon WilliamsQuoted 70 lines · Brandon Williams
Quoted 60 lines
Quoted 39 lines
builtin/mv.c +18
-12@@ -25,25 +26,43 @@ static const char **internal_copy_pathspec(const char *prefix, {int i;const char **result; +struct pathspec ps;ALLOC_ARRAY (result, count +1 ); -COPY_ARRAY (result, pathspec, count); - result[count] =NULL ; + +/* Create an intermediate copy of the pathspec based on the flags */ for (i =0 ; i < count; i++) { -int length =strlen (result[i]); +int length =strlen (pathspec[i]);int to_copy = length; +char *it;while (!(flags &KEEP_TRAILING_SLASH ) && - to_copy >0 &&is_dir_sep (result[i][to_copy -1 ])) + to_copy >0 &&is_dir_sep (pathspec[i][to_copy -1 ])) to_copy--; -if (to_copy != length || flags &DUP_BASENAME ) { -char *it =xmemdupz (result[i], to_copy); -if (flags &DUP_BASENAME ) { - result[i] =xstrdup (basename (it)); -free (it); - }else - result[i] = it; - } + + it =xmemdupz (pathspec[i], to_copy); +if (flags &DUP_BASENAME ) { + result[i] =xstrdup (basename (it)); +free (it); + }else + result[i] = it; + } + result[count] =NULL ; + + parse_pathspec(&ps, + PATHSPEC_ALL_MAGIC &builtin/mv.c static const char **internal_copy_pathspec(const char *prefix, · line 61
Quoted 10 lines
builtin/mv.c +10
-0+ ~(PATHSPEC_FROMTOP |PATHSPEC_LITERAL ), +PATHSPEC_KEEP_ORDER |PATHSPEC_PREFER_CWD , + prefix, result); +assert (count == ps.nr); + +/* Copy the pathspec and free the old intermediate strings */ +for (i =0 ; i < count; i++) { +const char *match =xstrdup (ps.items[i].match); +free ((char *) result[i]); + result[i] = match;Sigh.. it looks so weird that we do all the parsing (in a _copy_ pathspec function) then remove struct pathspec and return the plain string. I guess we can't do anything more until we rework
cmd_mvcode to handle pathspec natively.At the least I think we should rename this function to something else. But if you have time I really wish we could kill this function. I haven't stared at
cmd_mv()long and hard, but it looks to me that we combining two separate functionalities in the same function here.Quoted 9 lines
If "mv" takes n arguments, then the first <n-1> arguments may be pathspec, the last one is always a plain path. The "
dest_path=internal_copy_pathspec..." could be as simple as "dest_path=prefix_path(argv[argc - 1])". the special treatment for this last argument [1] can live here. Then, we can doparse_pathspecfor the <n-1> arguments incmd_mv(). It's still far from perfect, becausecmd_mvcan't handle pathspec properly, but it reduces the messy mess ininternal_copy_pathspeca bit, I hope.
Quoted 6 lines
Actually, after looking at this a bit more it seems like we could technically use
prefix_pathfor both source and dest (based on how the current code is structured) since the source's provied must all exist (as in no wildcards are allowed). We could drop using the pathspec struct completely in addition to renaming the function (to what I'm still unsure).
Yeah that sounds good too (with a caveat: I'm not a heavy user of git-mv nor touching this code a lot, I might be missing something). It'll take some looong time before somebody starts converting it to use pathspec properly, I guess. prefix_path() would keep the code clean meanwhile.
Quoted 4 lines
I agree that this code should probably be rewritten and made a bit cleaner, I don't know if that fits in the scope of this series or should be done as a followup patch. If you think it fits here then I can try and find some time to do the rework.
Signature · 1 line
Duy
Re: [PATCH 02/17] dir: convert create_simplify to use the pathspec struct interface
In reply to Brandon WilliamsQuoted 16 lines · Brandon Williams
Quoted 7 lines · Duy Nguyen
Quoted 2 lines · Brandon Williams
Convert '
create_simplify()' to use the pathspec struct interface from using the '_raw' entry in the pathspec.It would be even better to kill this
create_simplify()and letsimplify_away()handle struct pathspec directly.Quoted 7 lines
There is a bug in this code, that might have been found if we
simpify_away()handled pathspec directly: thememcmp()insimplify_away()will not play well with :(icase) magic. My bad. If :(icase) is used, the easiest/safe way is simplify nothing. Later on maybe we can teachsimplify_away()to do strncasecmp instead. We could ignore exclude patterns there too (although not excluding is not a bug).
Quoted 2 lines
So are you implying that the simplify struct needs to be killed? That way the pathspec struct itself is being passed around instead?
Yes. simplify struct was a thing when pathspec was an array of char *. At this point I think it can retire (when we have time to retire it)
Signature · 1 line
Duy
Re: [PATCH 01/17] mv: convert to using pathspec struct interface
In reply to Duy NguyenQuoted 78 lines · Duy Nguyen
Quoted 70 lines · Brandon Williams
Quoted 60 lines
Quoted 39 lines
builtin/mv.c +18
-12@@ -25,25 +26,43 @@ static const char **internal_copy_pathspec(const char *prefix, {int i;const char **result; +struct pathspec ps;ALLOC_ARRAY (result, count +1 ); -COPY_ARRAY (result, pathspec, count); - result[count] =NULL ; + +/* Create an intermediate copy of the pathspec based on the flags */ for (i =0 ; i < count; i++) { -int length =strlen (result[i]); +int length =strlen (pathspec[i]);int to_copy = length; +char *it;while (!(flags &KEEP_TRAILING_SLASH ) && - to_copy >0 &&is_dir_sep (result[i][to_copy -1 ])) + to_copy >0 &&is_dir_sep (pathspec[i][to_copy -1 ])) to_copy--; -if (to_copy != length || flags &DUP_BASENAME ) { -char *it =xmemdupz (result[i], to_copy); -if (flags &DUP_BASENAME ) { - result[i] =xstrdup (basename (it)); -free (it); - }else - result[i] = it; - } + + it =xmemdupz (pathspec[i], to_copy); +if (flags &DUP_BASENAME ) { + result[i] =xstrdup (basename (it)); +free (it); + }else + result[i] = it; + } + result[count] =NULL ; + + parse_pathspec(&ps, + PATHSPEC_ALL_MAGIC &builtin/mv.c static const char **internal_copy_pathspec(const char *prefix, · line 61
Quoted 10 lines
builtin/mv.c +10
-0+ ~(PATHSPEC_FROMTOP |PATHSPEC_LITERAL ), +PATHSPEC_KEEP_ORDER |PATHSPEC_PREFER_CWD , + prefix, result); +assert (count == ps.nr); + +/* Copy the pathspec and free the old intermediate strings */ +for (i =0 ; i < count; i++) { +const char *match =xstrdup (ps.items[i].match); +free ((char *) result[i]); + result[i] = match;Sigh.. it looks so weird that we do all the parsing (in a _copy_ pathspec function) then remove struct pathspec and return the plain string. I guess we can't do anything more until we rework
cmd_mvcode to handle pathspec natively.At the least I think we should rename this function to something else. But if you have time I really wish we could kill this function. I haven't stared at
cmd_mv()long and hard, but it looks to me that we combining two separate functionalities in the same function here.Quoted 9 lines
If "mv" takes n arguments, then the first <n-1> arguments may be pathspec, the last one is always a plain path. The "
dest_path=internal_copy_pathspec..." could be as simple as "dest_path=prefix_path(argv[argc - 1])". the special treatment for this last argument [1] can live here. Then, we can doparse_pathspecfor the <n-1> arguments incmd_mv(). It's still far from perfect, becausecmd_mvcan't handle pathspec properly, but it reduces the messy mess ininternal_copy_pathspeca bit, I hope.Quoted 6 lines
Actually, after looking at this a bit more it seems like we could technically use
prefix_pathfor both source and dest (based on how the current code is structured) since the source's provied must all exist (as in no wildcards are allowed). We could drop using the pathspec struct completely in addition to renaming the function (to what I'm still unsure).
Quoted 5 lines
Yeah that sounds good too (with a caveat: I'm not a heavy user of git-mv nor touching this code a lot, I might be missing something). It'll take some looong time before somebody starts converting it to use pathspec properly, I guess.
prefix_path()would keep the code clean meanwhile.
K for now I'll switch to using prefix_path() and rename the function internal_prefix_pathspec() as that is a bit more descriptive.
Signature · 1 line
Brandon Williams
Re: [PATCH 02/17] dir: convert create_simplify to use the pathspec struct interface
In reply to Duy NguyenQuoted 17 lines · Duy Nguyen
Quoted 16 lines · Brandon Williams
Quoted 14 lines · Duy Nguyen
Quoted 2 lines · Brandon Williams
Convert '
create_simplify()' to use the pathspec struct interface from using the '_raw' entry in the pathspec.It would be even better to kill this
create_simplify()and letsimplify_away()handle struct pathspec directly.There is a bug in this code, that might have been found if we
simpify_away()handled pathspec directly: thememcmp()insimplify_away()will not play well with :(icase) magic. My bad. If :(icase) is used, the easiest/safe way is simplify nothing. Later on maybe we can teachsimplify_away()to do strncasecmp instead. We could ignore exclude patterns there too (although not excluding is not a bug).
Quoted 5 lines
Quoted 2 lines
So are you implying that the simplify struct needs to be killed? That way the pathspec struct itself is being passed around instead?
Yes. simplify struct was a thing when pathspec was an array of char *. At this point I think it can retire (when we have time to retire it)
Alright, then for now I can leave this change as is and have a follow up series that kills the simplify struct.
Signature · 1 line
Brandon Williams
v2 cover pathspec cleanup
In reply to Brandon Williamsv2 of this series addresses the comments brought up in v1, most of which were small cosmetic changes (since this is mostly a cosmetic series to begin with).
Brandon Williams (16):
mv: remove use of deprecated 'get_pathspec()'
dir: convert create_simplify to use the pathspec struct interface
dir: convert fill_directory to use the pathspec struct interface
ls-tree: convert show_recursive to use the pathspec struct interface
pathspec: remove the deprecated get_pathspec function
pathspec: copy and free owned memory
pathspec: remove unused variable from unsupported_magic
pathspec: always show mnemonic and name in unsupported_magic
pathspec: simpler logic to prefix original pathspec elements
pathspec: factor global magic into its own function
pathspec: create parse_short_magic function
pathspec: create parse_long_magic function
pathspec: create parse_element_magic helper
pathspec: create strip submodule slash helpers
pathspec: small readability changes
pathspec: rename prefix_pathspec to init_pathspec_item
Documentation/technical/api-setup.txt | 2 - builtin/ls-tree.c | 16 +- builtin/mv.c | 50 ++-- cache.h | 1 - dir.c | 37 +-- pathspec.c | 468 +++++++++++++++++++--------------- pathspec.h | 5 +- 7 files changed, 317 insertions(+), 262 deletions(-)
--- interdiff from v1
builtin/ls-tree.c +2 -2
@@ -173,8 +173,8 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)* cannot be lifted until it is converted to use * match_pathspec() or tree_entry_interesting() */ -parse_pathspec (&pathspec,PATHSPEC_GLOB |PATHSPEC_ICASE | -PATHSPEC_EXCLUDE , +parse_pathspec (&pathspec,PATHSPEC_ALL_MAGIC & + ~(PATHSPEC_FROMTOP |PATHSPEC_LITERAL ),PATHSPEC_PREFER_CWD , prefix, argv +1 );for (i =0 ; i < pathspec.nr; i++)
builtin/mv.c +13 -19
@@ -20,13 +20,13 @@ static const char * const builtin_mv_usage[] = {#define DUP_BASENAME 1#define KEEP_TRAILING_SLASH 2 -static const char **internal_copy_pathspec (const char *prefix, -const char **pathspec, -int count,unsigned flags) +static const char **internal_prefix_pathspec (const char *prefix, +const char **pathspec, +int count,unsigned flags) {int i;const char **result; -struct pathspec ps; +int prefixlen = prefix ?strlen (prefix) :0 ;ALLOC_ARRAY (result, count +1 );/* Create an intermediate copy of the pathspec based on the flags */
@@ -42,25 +42,19 @@ static const char **internal_copy_pathspec(const char *prefix,if (flags &DUP_BASENAME ) { result[i] =xstrdup (basename (it));free (it); - }else + }else { result[i] = it; + } } result[count] =NULL ; -parse_pathspec (&ps, -PATHSPEC_ALL_MAGIC & - ~(PATHSPEC_FROMTOP |PATHSPEC_LITERAL ), -PATHSPEC_KEEP_ORDER |PATHSPEC_PREFER_CWD , - prefix, result); -assert (count == ps.nr); - -/* Copy the pathspec and free the old intermediate strings */ +/* Prefix the pathspec and free the old intermediate strings */ for (i =0 ; i < count; i++) { +const char *match =prefix_path (prefix, prefixlen, result[i]);free ((char *) result[i]); - result[i] =xstrdup (ps.items[i].match); + result[i] = match; } -clear_pathspec (&ps);return result; }
@@ -148,7 +142,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)if (read_cache () <0 )die (_ ("index file corrupt" )); - source =internal_copy_pathspec (prefix, argv, argc,0 ); + source =internal_prefix_pathspec (prefix, argv, argc,0 ); modes =xcalloc (argc,sizeof (enum update_mode ));/* * Keep trailing slash, needed to let
@@ -158,16 +152,16 @@ int cmd_mv(int argc, const char **argv, const char *prefix) flags =KEEP_TRAILING_SLASH ;if (argc ==1 &&is_directory (argv[0 ]) && !is_directory (argv[1 ])) flags =0 ; - dest_path =internal_copy_pathspec (prefix, argv + argc,1 , flags); + dest_path =internal_prefix_pathspec (prefix, argv + argc,1 , flags); submodule_gitfile =xcalloc (argc,sizeof (char *));if (dest_path[0 ][0 ] =='\0' )/* special case: "." was normalized to "" */ - destination =internal_copy_pathspec (dest_path[0 ], argv, argc,DUP_BASENAME ); + destination =internal_prefix_pathspec (dest_path[0 ], argv, argc,DUP_BASENAME );else if (!lstat (dest_path[0 ], &st) &&S_ISDIR (st.st_mode)) { dest_path[0 ] =add_slash (dest_path[0 ]); - destination =internal_copy_pathspec (dest_path[0 ], argv, argc,DUP_BASENAME ); + destination =internal_prefix_pathspec (dest_path[0 ], argv, argc,DUP_BASENAME ); }else {if (argc !=1 )die (_ ("destination '%s' is not a directory" ), dest_path[0 ]);
dir.c +8 -5
@@ -179,18 +179,21 @@ char *common_prefix(const struct pathspec *pathspec)int fill_directory (struct dir_struct *dir,const struct pathspec *pathspec) { -size_t len; +char *prefix; +size_t prefix_len;/* * Calculate common prefix for the pathspec, and * use that to optimize the directory walk */ - len =common_prefix_len (pathspec); + prefix =common_prefix (pathspec); + prefix_len = prefix ?strlen (prefix) :0 ;/* Read the directory and prune it */ -read_directory (dir, pathspec->nr ? pathspec->items[0 ].match :"" , - len, pathspec); -return len; +read_directory (dir, prefix, prefix_len, pathspec); + +free (prefix); +return prefix_len; } int within_depth(const char *name, int namelen,
pathspec.c +31 -34
122 lines not on this page · raw message
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH v2 1/16 mv: remove use of deprecated 'get_pathspec()'
Convert the 'internal_copy_pathspec()' function to 'prefix_path()' instead of using the deprecated 'get_pathspec()' interface. Also, rename 'internal_copy_pathspec()' to 'internal_prefix_pathspec()' to be more descriptive of what the funciton is actually doing.
In addition to this, fix a memory leak caused by only duplicating some of the pathspec elements. Instead always duplicate all of the the pathspec elements as an intermediate step (with modificationed based on the passed in flags). This way the intermediate strings can then be freed after getting the result from 'prefix_path()'.
--- builtin/mv.c | 50 +++++++++++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 19 deletions(-)
builtin/mv.c +31 -19
@@ -4,6 +4,7 @@* Copyright (C) 2006 Johannes Schindelin */ #include "builtin.h" +#include "pathspec.h" #include "lockfile.h" #include "dir.h" #include "cache-tree.h"
@@ -19,31 +20,42 @@ static const char * const builtin_mv_usage[] = {#define DUP_BASENAME 1#define KEEP_TRAILING_SLASH 2 -static const char **internal_copy_pathspec (const char *prefix, -const char **pathspec, -int count,unsigned flags) +static const char **internal_prefix_pathspec (const char *prefix, +const char **pathspec, +int count,unsigned flags) {int i;const char **result; +int prefixlen = prefix ?strlen (prefix) :0 ;ALLOC_ARRAY (result, count +1 ); -COPY_ARRAY (result, pathspec, count); - result[count] =NULL ; + +/* Create an intermediate copy of the pathspec based on the flags */ for (i =0 ; i < count; i++) { -int length =strlen (result[i]); +int length =strlen (pathspec[i]);int to_copy = length; +char *it;while (!(flags &KEEP_TRAILING_SLASH ) && - to_copy >0 &&is_dir_sep (result[i][to_copy -1 ])) + to_copy >0 &&is_dir_sep (pathspec[i][to_copy -1 ])) to_copy--; -if (to_copy != length || flags &DUP_BASENAME ) { -char *it =xmemdupz (result[i], to_copy); -if (flags &DUP_BASENAME ) { - result[i] =xstrdup (basename (it)); -free (it); - }else - result[i] = it; + + it =xmemdupz (pathspec[i], to_copy); +if (flags &DUP_BASENAME ) { + result[i] =xstrdup (basename (it)); +free (it); + }else { + result[i] = it; } } -return get_pathspec (prefix, result); + result[count] =NULL ; + +/* Prefix the pathspec and free the old intermediate strings */ +for (i =0 ; i < count; i++) { +const char *match =prefix_path (prefix, prefixlen, result[i]); +free ((char *) result[i]); + result[i] = match; + } + +return result; }static const char *add_slash (const char *path)
@@ -130,7 +142,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)if (read_cache () <0 )die (_ ("index file corrupt" )); - source =internal_copy_pathspec (prefix, argv, argc,0 ); + source =internal_prefix_pathspec (prefix, argv, argc,0 ); modes =xcalloc (argc,sizeof (enum update_mode ));/* * Keep trailing slash, needed to let
@@ -140,16 +152,16 @@ int cmd_mv(int argc, const char **argv, const char *prefix) flags =KEEP_TRAILING_SLASH ;if (argc ==1 &&is_directory (argv[0 ]) && !is_directory (argv[1 ])) flags =0 ; - dest_path =internal_copy_pathspec (prefix, argv + argc,1 , flags); + dest_path =internal_prefix_pathspec (prefix, argv + argc,1 , flags); submodule_gitfile =xcalloc (argc,sizeof (char *));if (dest_path[0 ][0 ] =='\0' )/* special case: "." was normalized to "" */ - destination =internal_copy_pathspec (dest_path[0 ], argv, argc,DUP_BASENAME ); + destination =internal_prefix_pathspec (dest_path[0 ], argv, argc,DUP_BASENAME );else if (!lstat (dest_path[0 ], &st) &&S_ISDIR (st.st_mode)) { dest_path[0 ] =add_slash (dest_path[0 ]); - destination =internal_copy_pathspec (dest_path[0 ], argv, argc,DUP_BASENAME ); + destination =internal_prefix_pathspec (dest_path[0 ], argv, argc,DUP_BASENAME ); }else {if (argc !=1 )die (_ ("destination '%s' is not a directory" ), dest_path[0 ]);
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH v2 2/16 dir: convert create_simplify to use the pathspec struct interface
In reply to Brandon WilliamsConvert 'create_simplify()' to use the pathspec struct interface from using the '_raw' entry in the pathspec.
--- dir.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-)
dir.c +12 -13
@@ -1787,25 +1787,24 @@ static int cmp_name(const void *p1, const void *p2)return name_compare (e1->name, e1->len, e2->name, e2->len); } -static struct path_simplify *create_simplify (const char **pathspec) +static struct path_simplify *create_simplify (const struct pathspec *pathspec) { -int nr, alloc =0 ; +int i;struct path_simplify *simplify =NULL ; -if (!pathspec) +if (!pathspec || !pathspec->nr)return NULL ; -for (nr =0 ; ; nr++) { +ALLOC_ARRAY (simplify, pathspec->nr +1 ); +for (i =0 ; i < pathspec->nr; i++) {const char *match; -ALLOC_GROW (simplify, nr +1 , alloc); - match = *pathspec++; -if (!match) -break ; - simplify[nr].path = match; - simplify[nr].len =simple_length (match); + match = pathspec->items[i].match; + simplify[i].path = match; + simplify[i].len = pathspec->items[i].nowildcard_len; } - simplify[nr].path =NULL ; - simplify[nr].len =0 ; + simplify[i].path =NULL ; + simplify[i].len =0 ; +return simplify; }
@@ -2036,7 +2035,7 @@ int read_directory(struct dir_struct *dir, const char *path, int len, const stru* subset of positive ones, which has no impacts on * create_simplify(). */ - simplify =create_simplify (pathspec ? pathspec->_raw :NULL ); + simplify =create_simplify (pathspec); untracked =validate_untracked_cache (dir, len, pathspec);if (!untracked)/*
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH v2 3/16 dir: convert fill_directory to use the pathspec struct interface
In reply to Brandon WilliamsConvert 'fill_directory()' to use the pathspec struct interface from using the '_raw' entry in the pathspec struct.
--- dir.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
dir.c +8 -4
@@ -179,17 +179,21 @@ char *common_prefix(const struct pathspec *pathspec)int fill_directory (struct dir_struct *dir,const struct pathspec *pathspec) { -size_t len; +char *prefix; +size_t prefix_len;/* * Calculate common prefix for the pathspec, and * use that to optimize the directory walk */ - len =common_prefix_len (pathspec); + prefix =common_prefix (pathspec); + prefix_len = prefix ?strlen (prefix) :0 ;/* Read the directory and prune it */ -read_directory (dir, pathspec->nr ? pathspec->_raw[0 ] :"" , len, pathspec); -return len; +read_directory (dir, prefix, prefix_len, pathspec); + +free (prefix); +return prefix_len; } int within_depth(const char *name, int namelen,
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH v2 6/16 pathspec: copy and free owned memory
In reply to Brandon WilliamsThe 'original' string entry in a pathspec_item is only duplicated some of the time, instead always make a copy of the original and take ownership of the memory.
Since both 'match' and 'original' string entries in a pathspec_item are owned by the pathspec struct, they need to be freed when clearing the pathspec struct (in 'clear_pathspec()') and duplicated when copying the pathspec struct (in 'copy_pathspec()').
Also change the type of 'match' and 'original' to 'char *' in order to more explicitly show the ownership of the memory.
--- pathspec.c | 22 ++++++++++++++++++---- pathspec.h | 4 ++-- 2 files changed, 20 insertions(+), 6 deletions(-)
pathspec.c +18 -4
@@ -259,8 +259,9 @@ static unsigned prefix_pathspec(struct pathspec_item *item, }strbuf_addstr (&sb, match); item->original =strbuf_detach (&sb,NULL ); - }else - item->original = elt; + }else { + item->original =xstrdup (elt); + } item->len =strlen (item->match); item->prefix = prefixlen;
@@ -388,8 +389,8 @@ void parse_pathspec(struct pathspec *pathspec,die ("BUG: PATHSPEC_PREFER_CWD requires arguments" ); pathspec->items = item =xcalloc (1 ,sizeof (*item)); - item->match = prefix; - item->original = prefix; + item->match =xstrdup (prefix); + item->original =xstrdup (prefix); item->nowildcard_len = item->len =strlen (prefix); item->prefix = item->len; pathspec->nr =1 ;
@@ -453,13 +454,26 @@ void parse_pathspec(struct pathspec *pathspec,void copy_pathspec (struct pathspec *dst,const struct pathspec *src) { +int i; + *dst = *src;ALLOC_ARRAY (dst->items, dst->nr);COPY_ARRAY (dst->items, src->items, dst->nr); + +for (i =0 ; i < dst->nr; i++) { + dst->items[i].match =xstrdup (src->items[i].match); + dst->items[i].original =xstrdup (src->items[i].original); + } }void clear_pathspec (struct pathspec *pathspec) { +int i; + +for (i =0 ; i < pathspec->nr; i++) { +free (pathspec->items[i].match); +free (pathspec->items[i].original); + }free (pathspec->items); pathspec->items =NULL ; }
pathspec.h +2 -2
@@ -25,8 +25,8 @@ struct pathspec {unsigned magic;int max_depth;struct pathspec_item { -const char *match; -const char *original; +char *match; +char *original;unsigned magic;int len, prefix;int nowildcard_len;
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH v2 11/16 pathspec: create parse_short_magic function
In reply to Brandon WilliamsFactor out the logic responsible for parsing short magic into its own function.
--- pathspec.c | 54 ++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 18 deletions(-)
pathspec.c +36 -18
@@ -157,6 +157,41 @@ static int get_global_magic(int element_magic) }/* +* Parse the pathspec element looking for short magic +* +* saves all magic in 'magic' +* returns the position in 'elem' after all magic has been parsed +*/ +static const char *parse_short_magic (unsigned *magic,const char *elem) +{ +const char *pos; + +for (pos = elem +1 ; *pos && *pos !=':' ; pos++) { +char ch = *pos; +int i; + +if (!is_pathspec_magic (ch)) +break ; + +for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) { +if (pathspec_magic[i].mnemonic == ch) { + *magic |= pathspec_magic[i].bit; +break ; + } + } + +if (ARRAY_SIZE (pathspec_magic) <= i) +die (_ ("Unimplemented pathspec magic '%c' in '%s'" ), + ch, elem); + } + +if (*pos ==':' ) + pos++; + +return pos; +} + +/* * Take an element of a pathspec and check for magic signatures. * Append the result to the prefix. Return the magic bitmap. *
@@ -220,24 +255,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags, copyfrom++; }else {/* shorthand */ -for (copyfrom = elt +1 ; - *copyfrom && *copyfrom !=':' ; - copyfrom++) { -char ch = *copyfrom; - -if (!is_pathspec_magic (ch)) -break ; -for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) -if (pathspec_magic[i].mnemonic == ch) { - element_magic |= pathspec_magic[i].bit; -break ; - } -if (ARRAY_SIZE (pathspec_magic) <= i) -die (_ ("Unimplemented pathspec magic '%c' in '%s'" ), - ch, elt); - } -if (*copyfrom ==':' ) - copyfrom++; + copyfrom =parse_short_magic (&element_magic, elt); } magic |= element_magic;
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH v2 10/16 pathspec: factor global magic into its own function
In reply to Brandon WilliamsCreate helper functions to read the global magic environment variables in additon to factoring out the global magic gathering logic into its own function.
--- pathspec.c | 127 +++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 78 insertions(+), 49 deletions(-)
pathspec.c +78 -49
@@ -87,6 +87,75 @@ static void prefix_magic(struct strbuf *sb, int prefixlen, unsigned magic)strbuf_addf (sb,",prefix:%d)" , prefixlen); } +static inline int get_literal_global (void ) +{ +static int literal =-1 ; + +if (literal <0 ) + literal =git_env_bool (GIT_LITERAL_PATHSPECS_ENVIRONMENT ,0 ); + +return literal; +} + +static inline int get_glob_global (void ) +{ +static int glob =-1 ; + +if (glob <0 ) + glob =git_env_bool (GIT_GLOB_PATHSPECS_ENVIRONMENT ,0 ); + +return glob; +} + +static inline int get_noglob_global (void ) +{ +static int noglob =-1 ; + +if (noglob <0 ) + noglob =git_env_bool (GIT_NOGLOB_PATHSPECS_ENVIRONMENT ,0 ); + +return noglob; +} + +static inline int get_icase_global (void ) +{ +static int icase =-1 ; + +if (icase <0 ) + icase =git_env_bool (GIT_ICASE_PATHSPECS_ENVIRONMENT ,0 ); + +return icase; +} + +static int get_global_magic (int element_magic) +{ +int global_magic =0 ; + +if (get_literal_global ()) + global_magic |=PATHSPEC_LITERAL ; + +/* --glob-pathspec is overridden by :(literal) */ +if (get_glob_global () && !(element_magic &PATHSPEC_LITERAL )) + global_magic |=PATHSPEC_GLOB ; + +if (get_glob_global () &&get_noglob_global ()) +die (_ ("global 'glob' and 'noglob' pathspec settings are incompatible" )); + +if (get_icase_global ()) + global_magic |=PATHSPEC_ICASE ; + +if ((global_magic &PATHSPEC_LITERAL ) && + (global_magic & ~PATHSPEC_LITERAL )) +die (_ ("global 'literal' pathspec setting is incompatible " +"with all other global pathspec settings" )); + +/* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */ +if (get_noglob_global () && !(element_magic &PATHSPEC_GLOB )) + global_magic |=PATHSPEC_LITERAL ; + +return global_magic; +} +/* * Take an element of a pathspec and check for magic signatures. * Append the result to the prefix. Return the magic bitmap.
@@ -104,46 +173,12 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags, const char *prefix, int prefixlen, const char *elt) { - static int literal_global = -1; -static int glob_global =-1 ; -static int noglob_global =-1 ; -static int icase_global =-1 ; -unsigned magic =0 , element_magic =0 , global_magic =0 ; + unsigned magic = 0, element_magic = 0;const char *copyfrom = elt;char *match;int i, pathspec_prefix =-1 ; -if (literal_global <0 ) - literal_global =git_env_bool (GIT_LITERAL_PATHSPECS_ENVIRONMENT ,0 ); -if (literal_global) - global_magic |=PATHSPEC_LITERAL ; - -if (glob_global <0 ) - glob_global =git_env_bool (GIT_GLOB_PATHSPECS_ENVIRONMENT ,0 ); -if (glob_global) - global_magic |=PATHSPEC_GLOB ; - -if (noglob_global <0 ) - noglob_global =git_env_bool (GIT_NOGLOB_PATHSPECS_ENVIRONMENT ,0 ); - -if (glob_global && noglob_global) -die (_ ("global 'glob' and 'noglob' pathspec settings are incompatible" )); - - -if (icase_global <0 ) - icase_global =git_env_bool (GIT_ICASE_PATHSPECS_ENVIRONMENT ,0 ); -if (icase_global) - global_magic |=PATHSPEC_ICASE ; - -if ((global_magic &PATHSPEC_LITERAL ) && - (global_magic & ~PATHSPEC_LITERAL )) -die (_ ("global 'literal' pathspec setting is incompatible " -"with all other global pathspec settings" )); - -if (flags &PATHSPEC_LITERAL_PATH ) - global_magic =0 ; - -if (elt[0 ] !=':' || literal_global || +if (elt[0 ] !=':' ||get_literal_global () || (flags &PATHSPEC_LITERAL_PATH )) { ;/* nothing to do */ }else if (elt[1 ] =='(' ) {
@@ -207,15 +242,11 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags, magic |= element_magic; -/* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */ -if (noglob_global && !(magic &PATHSPEC_GLOB )) - global_magic |=PATHSPEC_LITERAL ; - -/* --glob-pathspec is overridden by :(literal) */ -if ((global_magic &PATHSPEC_GLOB ) && (magic &PATHSPEC_LITERAL )) - global_magic &= ~PATHSPEC_GLOB ; - - magic |= global_magic; +/* PATHSPEC_LITERAL_PATH ignores magic */ +if (flags &PATHSPEC_LITERAL_PATH ) + magic =PATHSPEC_LITERAL ; +else + magic |=get_global_magic (element_magic);if (pathspec_prefix >=0 && (prefixlen || (prefix && *prefix)))
@@ -241,7 +272,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,* original. Useful for passing to another command. */ if ((flags &PATHSPEC_PREFIX_ORIGIN ) && - prefixlen && !literal_global) { + prefixlen && !get_literal_global ()) {struct strbuf sb =STRBUF_INIT ;/* Preserve the actual prefix length of each pattern */
@@ -407,9 +438,7 @@ void parse_pathspec(struct pathspec *pathspec, item[i].magic =prefix_pathspec (item + i, flags, prefix, prefixlen, entry); -if ((flags &PATHSPEC_LITERAL_PATH ) && - !(magic_mask &PATHSPEC_LITERAL )) - item[i].magic |=PATHSPEC_LITERAL ; +if (item[i].magic &PATHSPEC_EXCLUDE ) nr_exclude++;if (item[i].magic & magic_mask)
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH v2 9/16 pathspec: simpler logic to prefix original pathspec elements
In reply to Brandon WilliamsThe logic used to prefix an original pathspec element with 'prefix' magic is more general purpose and can be used for more than just short magic. Remove the extra code paths and rename 'prefix_short_magic' to 'prefix_magic' to better indicate that it can be used in more general situations.
Also, slightly change the logic which decides when to prefix the original element in order to prevent a pathspec of "." from getting converted to "" (empty string).
--- pathspec.c | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-)
pathspec.c +13 -20
@@ -74,13 +74,12 @@ static struct pathspec_magic { { PATHSPEC_EXCLUDE, '!', "exclude" }, }; -static void prefix_short_magic (struct strbuf *sb,int prefixlen, -unsigned short_magic) +static void prefix_magic (struct strbuf *sb,int prefixlen,unsigned magic) {int i;strbuf_addstr (sb,":(" );for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) -if (short_magic & pathspec_magic[i].bit) { +if (magic & pathspec_magic[i].bit) {if (sb->buf[sb->len -1 ] !='(' )strbuf_addch (sb,',' );strbuf_addstr (sb, pathspec_magic[i].name);
@@ -109,8 +108,8 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,static int glob_global =-1 ;static int noglob_global =-1 ;static int icase_global =-1 ; -unsigned magic =0 , short_magic =0 , global_magic =0 ; -const char *copyfrom = elt, *long_magic_end =NULL ; +unsigned magic =0 , element_magic =0 , global_magic =0 ; +const char *copyfrom = elt;char *match;int i, pathspec_prefix =-1 ;
@@ -164,7 +163,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) {if (strlen (pathspec_magic[i].name) == len && !strncmp (pathspec_magic[i].name, copyfrom, len)) { - magic |= pathspec_magic[i].bit; + element_magic |= pathspec_magic[i].bit;break ; }if (starts_with (copyfrom,"prefix:" )) {
@@ -183,7 +182,6 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags, }if (*copyfrom !=')' )die (_ ("Missing ')' at the end of pathspec magic in '%s'" ), elt); - long_magic_end = copyfrom; copyfrom++; }else {/* shorthand */
@@ -196,7 +194,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,break ;for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++)if (pathspec_magic[i].mnemonic == ch) { - short_magic |= pathspec_magic[i].bit; + element_magic |= pathspec_magic[i].bit;break ; }if (ARRAY_SIZE (pathspec_magic) <= i)
@@ -207,7 +205,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags, copyfrom++; } - magic |= short_magic; + magic |= element_magic;/* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */ if (noglob_global && !(magic &PATHSPEC_GLOB ))
@@ -242,18 +240,13 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,* Prefix the pathspec (keep all magic) and assign to * original. Useful for passing to another command. */ -if (flags &PATHSPEC_PREFIX_ORIGIN ) { +if ((flags &PATHSPEC_PREFIX_ORIGIN ) && + prefixlen && !literal_global) {struct strbuf sb =STRBUF_INIT ; -if (prefixlen && !literal_global) { -/* Preserve the actual prefix length of each pattern */ -if (short_magic) -prefix_short_magic (&sb, prefixlen, short_magic); -else if (long_magic_end) { -strbuf_add (&sb, elt, long_magic_end - elt); -strbuf_addf (&sb,",prefix:%d)" , prefixlen); - }else -strbuf_addf (&sb,":(prefix:%d)" , prefixlen); - } + +/* Preserve the actual prefix length of each pattern */ +prefix_magic (&sb, prefixlen, element_magic); +strbuf_addstr (&sb, match); item->original =strbuf_detach (&sb,NULL ); }else {
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH v2 12/16 pathspec: create parse_long_magic function
In reply to Brandon WilliamsFactor out the logic responsible for parsing long magic into its own function. As well as hoist the prefix check logic outside of the inner loop as there isn't anything that needs to be done after matching "prefix:".
--- pathspec.c | 92 ++++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 57 insertions(+), 35 deletions(-)
pathspec.c +57 -35
@@ -157,6 +157,60 @@ static int get_global_magic(int element_magic) }/* +* Parse the pathspec element looking for long magic +* +* saves all magic in 'magic' +* if prefix magic is used, save the prefix length in 'prefix_len' +* returns the position in 'elem' after all magic has been parsed +*/ +static const char *parse_long_magic (unsigned *magic,int *prefix_len, +const char *elem) +{ +const char *pos; +const char *nextat; + +for (pos = elem +2 ; *pos && *pos !=')' ; pos = nextat) { +size_t len =strcspn (pos,",)" ); +int i; + +if (pos[len] ==',' ) + nextat = pos + len +1 ;/* handle ',' */ +else + nextat = pos + len;/* handle ')' and '\0' */ + +if (!len) +continue ; + +if (starts_with (pos,"prefix:" )) { +char *endptr; + *prefix_len =strtol (pos +7 , &endptr,10 ); +if (endptr - pos != len) +die (_ ("invalid parameter for pathspec magic 'prefix'" )); +continue ; + } + +for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) { +if (strlen (pathspec_magic[i].name) == len && + !strncmp (pathspec_magic[i].name, pos, len)) { + *magic |= pathspec_magic[i].bit; +break ; + } + } + +if (ARRAY_SIZE (pathspec_magic) <= i) +die (_ ("Invalid pathspec magic '%.*s' in '%s'" ), + (int ) len, pos, elem); + } + +if (*pos !=')' ) +die (_ ("Missing ')' at the end of pathspec magic in '%s'" ), + elem); + pos++; + +return pos; +} + +/* * Parse the pathspec element looking for short magic * * saves all magic in 'magic'
@@ -218,41 +272,9 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags, ;/* nothing to do */ }else if (elt[1 ] =='(' ) {/* longhand */ -const char *nextat; -for (copyfrom = elt +2 ; - *copyfrom && *copyfrom !=')' ; - copyfrom = nextat) { -size_t len =strcspn (copyfrom,",)" ); -if (copyfrom[len] ==',' ) - nextat = copyfrom + len +1 ; -else -/* handle ')' and '\0' */ - nextat = copyfrom + len; -if (!len) -continue ; -for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) { -if (strlen (pathspec_magic[i].name) == len && - !strncmp (pathspec_magic[i].name, copyfrom, len)) { - element_magic |= pathspec_magic[i].bit; -break ; - } -if (starts_with (copyfrom,"prefix:" )) { -char *endptr; - pathspec_prefix =strtol (copyfrom +7 , - &endptr,10 ); -if (endptr - copyfrom != len) -die (_ ("invalid parameter for pathspec magic 'prefix'" )); -/* "i" would be wrong, but it does not matter */ -break ; - } - } -if (ARRAY_SIZE (pathspec_magic) <= i) -die (_ ("Invalid pathspec magic '%.*s' in '%s'" ), - (int ) len, copyfrom, elt); - } -if (*copyfrom !=')' ) -die (_ ("Missing ')' at the end of pathspec magic in '%s'" ), elt); - copyfrom++; + copyfrom =parse_long_magic (&element_magic, + &pathspec_prefix, + elt); }else {/* shorthand */ copyfrom =parse_short_magic (&element_magic, elt);
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH v2 16/16 pathspec: rename prefix_pathspec to init_pathspec_item
In reply to Brandon WilliamsGive a more relevant name to the prefix_pathspec function as it does more than just prefix a pathspec element.
--- pathspec.c | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-)
pathspec.c +7 -17
@@ -297,21 +297,11 @@ static void strip_submodule_slash_expensive(struct pathspec_item *item) }/* -* Take an element of a pathspec and check for magic signatures. -* Append the result to the prefix. Return the magic bitmap. -* -* For now, we only parse the syntax and throw out anything other than -* "top" magic. -* -* NEEDSWORK: This needs to be rewritten when we start migrating -* get_pathspec() users to use the "struct pathspec" interface. For -* example, a pathspec element may be marked as case-insensitive, but -* the prefix part must always match literally, and a single stupid -* string cannot express such a case. +* Perform the initialization of a pathspec_item based on a pathspec element. */ -static unsigned prefix_pathspec (struct pathspec_item *item,unsigned flags, -const char *prefix,int prefixlen, -const char *elt) +static void init_pathspec_item (struct pathspec_item *item,unsigned flags, +const char *prefix,int prefixlen, +const char *elt) {unsigned magic =0 , element_magic =0 ;const char *copyfrom = elt;
@@ -329,6 +319,8 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags, magic |=get_global_magic (element_magic); } + item->magic = magic; +if (pathspec_prefix >=0 && (prefixlen || (prefix && *prefix)))die ("BUG: 'prefix' magic is supposed to be used at worktree's root" );
@@ -401,7 +393,6 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,/* sanity checks, pathspec matchers assume these are sane */ assert (item->nowildcard_len <= item->len && item->prefix <= item->len); -return magic; }static int pathspec_item_cmp (const void *a_,const void *b_)
@@ -500,8 +491,7 @@ void parse_pathspec(struct pathspec *pathspec,for (i =0 ; i < n; i++) { entry = argv[i]; - item[i].magic =prefix_pathspec (item + i, flags, - prefix, prefixlen, entry); +init_pathspec_item (item + i, flags, prefix, prefixlen, entry);if (item[i].magic &PATHSPEC_EXCLUDE ) nr_exclude++;
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH v2 13/16 pathspec: create parse_element_magic helper
In reply to Brandon WilliamsFactor out the logic responsible for the magic in a pathspec element into its own function.
Also avoid calling into the parsing functions when PATHSPEC_LITERAL_PATH is specified since it causes magic to be ignored and all paths to be treated as literals.
--- pathspec.c | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-)
pathspec.c +20 -17
@@ -245,6 +245,19 @@ static const char *parse_short_magic(unsigned *magic, const char *elem)return pos; } +static const char *parse_element_magic (unsigned *magic,int *prefix_len, +const char *elem) +{ +if (elem[0 ] !=':' ||get_literal_global ()) +return elem;/* nothing to do */ +else if (elem[1 ] =='(' ) +/* longhand */ +return parse_long_magic (magic, prefix_len, elem); +else +/* shorthand */ +return parse_short_magic (magic, elem); +} +/* * Take an element of a pathspec and check for magic signatures. * Append the result to the prefix. Return the magic bitmap.
@@ -267,26 +280,16 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,char *match;int i, pathspec_prefix =-1 ; -if (elt[0 ] !=':' ||get_literal_global () || - (flags &PATHSPEC_LITERAL_PATH )) { - ;/* nothing to do */ - }else if (elt[1 ] =='(' ) { -/* longhand */ - copyfrom =parse_long_magic (&element_magic, - &pathspec_prefix, - elt); - }else { -/* shorthand */ - copyfrom =parse_short_magic (&element_magic, elt); - } - - magic |= element_magic; -/* PATHSPEC_LITERAL_PATH ignores magic */ -if (flags &PATHSPEC_LITERAL_PATH ) +if (flags &PATHSPEC_LITERAL_PATH ) { magic =PATHSPEC_LITERAL ; -else + }else { + copyfrom =parse_element_magic (&element_magic, + &pathspec_prefix, + elt); + magic |= element_magic; magic |=get_global_magic (element_magic); + }if (pathspec_prefix >=0 && (prefixlen || (prefix && *prefix)))
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH v2 15/16 pathspec: small readability changes
In reply to Brandon WilliamsA few small changes to improve readability. This is done by grouping related assignments, adding blank lines, ensuring lines are <80 characters, and adding additional comments.
--- pathspec.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)
pathspec.c +10 -5
@@ -336,6 +336,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,if ((magic &PATHSPEC_LITERAL ) && (magic &PATHSPEC_GLOB ))die (_ ("%s: 'literal' and 'glob' are incompatible" ), elt); +/* Create match string which will be used for pathspec matching */ if (pathspec_prefix >=0 ) { match =xstrdup (copyfrom); prefixlen = pathspec_prefix;
@@ -343,11 +344,16 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags, match =xstrdup (copyfrom); prefixlen =0 ; }else { - match =prefix_path_gently (prefix, prefixlen, &prefixlen, copyfrom); + match =prefix_path_gently (prefix, prefixlen, + &prefixlen, copyfrom);if (!match)die (_ ("%s: '%s' is outside repository" ), elt, copyfrom); } + item->match = match; + item->len =strlen (item->match); + item->prefix = prefixlen; +/* * Prefix the pathspec (keep all magic) and assign to * original. Useful for passing to another command.
@@ -364,8 +370,6 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags, }else { item->original =xstrdup (elt); } - item->len =strlen (item->match); - item->prefix = prefixlen;if (flags &PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP )strip_submodule_slash_cheap (item);
@@ -373,13 +377,14 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,if (flags &PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE )strip_submodule_slash_expensive (item); -if (magic &PATHSPEC_LITERAL ) +if (magic &PATHSPEC_LITERAL ) { item->nowildcard_len = item->len; -else { + }else { item->nowildcard_len =simple_length (item->match);if (item->nowildcard_len < prefixlen) item->nowildcard_len = prefixlen; } + item->flags =0 ;if (magic &PATHSPEC_GLOB ) {/*
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH v2 14/16 pathspec: create strip submodule slash helpers
In reply to Brandon WilliamsFactor out the logic responsible for stripping the trailing slash on pathspecs referencing submodules into its own function.
--- pathspec.c | 68 ++++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 42 insertions(+), 26 deletions(-)
pathspec.c +42 -26
@@ -258,6 +258,44 @@ static const char *parse_element_magic(unsigned *magic, int *prefix_len,return parse_short_magic (magic, elem); } +static void strip_submodule_slash_cheap (struct pathspec_item *item) +{ +int i; + +if ((item->len >=1 && item->match[item->len -1 ] =='/' ) && + (i =cache_name_pos (item->match, item->len -1 )) >=0 && +S_ISGITLINK (active_cache[i]->ce_mode)) { + item->len--; + item->match[item->len] ='\0' ; + } +} + +static void strip_submodule_slash_expensive (struct pathspec_item *item) +{ +int i; + +for (i =0 ; i < active_nr; i++) { +struct cache_entry *ce = active_cache[i]; +int ce_len =ce_namelen (ce); + +if (!S_ISGITLINK (ce->ce_mode)) +continue ; + +if (item->len <= ce_len || item->match[ce_len] !='/' || +memcmp (ce->name, item->match, ce_len)) +continue ; + +if (item->len == ce_len +1 ) { +/* strip trailing slash */ + item->len--; + item->match[item->len] ='\0' ; + }else { +die (_ ("Pathspec '%s' is in submodule '%.*s'" ), + item->original, ce_len, ce->name); + } + } +} +/* * Take an element of a pathspec and check for magic signatures. * Append the result to the prefix. Return the magic bitmap.
@@ -278,7 +316,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,unsigned magic =0 , element_magic =0 ;const char *copyfrom = elt;char *match; -int i, pathspec_prefix =-1 ; +int pathspec_prefix =-1 ;/* PATHSPEC_LITERAL_PATH ignores magic */ if (flags &PATHSPEC_LITERAL_PATH ) {
@@ -329,33 +367,11 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags, item->len =strlen (item->match); item->prefix = prefixlen; -if ((flags &PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP ) && - (item->len >=1 && item->match[item->len -1 ] =='/' ) && - (i =cache_name_pos (item->match, item->len -1 )) >=0 && -S_ISGITLINK (active_cache[i]->ce_mode)) { - item->len--; - match[item->len] ='\0' ; - } +if (flags &PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP ) +strip_submodule_slash_cheap (item);if (flags &PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE ) -for (i =0 ; i < active_nr; i++) { -struct cache_entry *ce = active_cache[i]; -int ce_len =ce_namelen (ce); - -if (!S_ISGITLINK (ce->ce_mode)) -continue ; - -if (item->len <= ce_len || match[ce_len] !='/' || -memcmp (ce->name, match, ce_len)) -continue ; -if (item->len == ce_len +1 ) { -/* strip trailing slash */ - item->len--; - match[item->len] ='\0' ; - }else -die (_ ("Pathspec '%s' is in submodule '%.*s'" ), - elt, ce_len, ce->name); - } +strip_submodule_slash_expensive (item);if (magic &PATHSPEC_LITERAL ) item->nowildcard_len = item->len;
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH v2 8/16 pathspec: always show mnemonic and name in unsupported_magic
In reply to Brandon WilliamsFor better clarity, always show the mnemonic and name of the unsupported magic being used. This lets users have a more clear understanding of what magic feature isn't supported. And if they supplied a mnemonic, the user will be told what its corresponding name is which will allow them to more easily search the man pages for that magic type.
This also avoids passing an extra parameter around the pathspec initialization code.
--- pathspec.c | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-)
pathspec.c +8 -15
@@ -68,7 +68,7 @@ static struct pathspec_magic {const char *name; } pathspec_magic[] = { { PATHSPEC_FROMTOP, '/', "top" }, - { PATHSPEC_LITERAL, 0, "literal" }, + { PATHSPEC_LITERAL,'\0', "literal" }, { PATHSPEC_GLOB, '\0', "glob" }, { PATHSPEC_ICASE, '\0', "icase" }, { PATHSPEC_EXCLUDE, '!', "exclude" },
@@ -101,9 +101,7 @@ static void prefix_short_magic(struct strbuf *sb, int prefixlen,* the prefix part must always match literally, and a single stupid * string cannot express such a case. */ -static unsigned prefix_pathspec (struct pathspec_item *item, -unsigned *p_short_magic, -unsigned flags, +static unsigned prefix_pathspec (struct pathspec_item *item,unsigned flags,const char *prefix,int prefixlen,const char *elt) {
@@ -210,7 +208,6 @@ static unsigned prefix_pathspec(struct pathspec_item *item, } magic |= short_magic; - *p_short_magic = short_magic;/* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */ if (noglob_global && !(magic &PATHSPEC_GLOB ))
@@ -329,8 +326,7 @@ static int pathspec_item_cmp(const void *a_, const void *b_) } static void NORETURN unsupported_magic(const char *pattern, -unsigned magic, -unsigned short_magic) +unsigned magic) {struct strbuf sb =STRBUF_INIT ;int i;
@@ -340,8 +336,9 @@ static void NORETURN unsupported_magic(const char *pattern,continue ;if (sb.len)strbuf_addch (&sb,' ' ); -if (short_magic & m->bit) -strbuf_addf (&sb,"'%c'" , m->mnemonic); + +if (m->mnemonic) +strbuf_addf (&sb,"'(%c)%s'" , m->mnemonic, m->name);else strbuf_addf (&sb,"'%s'" , m->name); }
@@ -413,11 +410,9 @@ void parse_pathspec(struct pathspec *pathspec, prefixlen = prefix ?strlen (prefix) :0 ;for (i =0 ; i < n; i++) { -unsigned short_magic; entry = argv[i]; - item[i].magic =prefix_pathspec (item + i, &short_magic, - flags, + item[i].magic =prefix_pathspec (item + i, flags, prefix, prefixlen, entry);if ((flags &PATHSPEC_LITERAL_PATH ) && !(magic_mask &PATHSPEC_LITERAL ))
@@ -425,9 +420,7 @@ void parse_pathspec(struct pathspec *pathspec,if (item[i].magic &PATHSPEC_EXCLUDE ) nr_exclude++;if (item[i].magic & magic_mask) -unsupported_magic (entry, - item[i].magic & magic_mask, - short_magic); +unsupported_magic (entry, item[i].magic & magic_mask);if ((flags &PATHSPEC_SYMLINK_LEADING_PATH ) &&has_symlink_leading_path (item[i].match, item[i].len)) {
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH v2 4/16 ls-tree: convert show_recursive to use the pathspec struct interface
In reply to Brandon WilliamsConvert 'show_recursive()' to use the pathspec struct interface from using the '_raw' entry in the pathspec struct.
--- builtin/ls-tree.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-)
builtin/ls-tree.c +7 -9
@@ -31,21 +31,18 @@ static const char * const ls_tree_usage[] = {static int show_recursive (const char *base,int baselen,const char *pathname) { -const char **s; +int i;if (ls_options &LS_RECURSIVE )return 1 ; - s = pathspec._raw; -if (!s) +if (!pathspec.nr)return 0 ; -for (;;) { -const char *spec = *s++; +for (i =0 ; i < pathspec.nr; i++) { +const char *spec = pathspec.items[i].match;int len, speclen; -if (!spec) -return 0 ;if (strncmp (base, spec, baselen))continue ; len =strlen (pathname);
@@ -59,6 +56,7 @@ static int show_recursive(const char *base, int baselen, const char *pathname)continue ;return 1 ; } +return 0 ; } static int show_tree(const unsigned char *sha1, struct strbuf *base,
@@ -175,8 +173,8 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)* cannot be lifted until it is converted to use * match_pathspec() or tree_entry_interesting() */ -parse_pathspec (&pathspec,PATHSPEC_GLOB |PATHSPEC_ICASE | -PATHSPEC_EXCLUDE , +parse_pathspec (&pathspec,PATHSPEC_ALL_MAGIC & + ~(PATHSPEC_FROMTOP |PATHSPEC_LITERAL ),PATHSPEC_PREFER_CWD , prefix, argv +1 );for (i =0 ; i < pathspec.nr; i++)
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH v2 5/16 pathspec: remove the deprecated get_pathspec function
In reply to Brandon WilliamsNow that all callers of the old 'get_pathspec' interface have been migrated to use the new pathspec struct interface it can be removed from the codebase.
Since there are no more users of the '_raw' field in the pathspec struct it can also be removed. This patch also removes the old functionality of modifying the const char **argv array that was passed into parse_pathspec. Instead the constructed 'match' string (which is a pathspec element with the prefix prepended) is only stored in its corresponding pathspec_item entry.
--- Documentation/technical/api-setup.txt | 2 -- cache.h | 1 - pathspec.c | 42 +++-------------------------------- pathspec.h | 1 - 4 files changed, 3 insertions(+), 43 deletions(-)
Documentation/technical/api-setup.txt +0 -2
@@ -27,8 +27,6 @@ parse_pathspec(). This function takes several arguments: - prefix and args come from cmd_* functions -get_pathspec() is obsolete and should never be used in new code. - parse_pathspec() helps catch unsupported features and reject them politely. At a lower level, different pathspec-related functions may not support the same set of features. Such pathspec-sensitive
cache.h +0 -1
@@ -514,7 +514,6 @@ extern void set_git_work_tree(const char *tree);#define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES" -extern const char **get_pathspec (const char *prefix,const char **pathspec);extern void setup_work_tree (void );extern const char *setup_git_directory_gently (int *);extern const char *setup_git_directory (void );
pathspec.c +3 -39
85 lines not on this page · raw message
pathspec.h +0 -1
@@ -19,7 +19,6 @@#define PATHSPEC_ONESTAR 1/* the pathspec pattern satisfies GFNM_ONESTAR */ struct pathspec { -const char **_raw;/* get_pathspec() result, not freed by clear_pathspec() */ int nr;unsigned int has_wildcard:1 ;unsigned int recursive:1 ;
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH v2 7/16 pathspec: remove unused variable from unsupported_magic
In reply to Brandon WilliamsRemoved unused variable 'n' from the 'unsupported_magic()' function.
--- pathspec.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
pathspec.c +2 -3
@@ -333,8 +333,8 @@ static void NORETURN unsupported_magic(const char *pattern, unsigned short_magic) {struct strbuf sb =STRBUF_INIT ; -int i, n; -for (n = i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) { +int i; +for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) {const struct pathspec_magic *m = pathspec_magic + i;if (!(magic & m->bit))continue ;
@@ -344,7 +344,6 @@ static void NORETURN unsupported_magic(const char *pattern,strbuf_addf (&sb,"'%c'" , m->mnemonic); else strbuf_addf(&sb, "'%s'", m->name); - n++; }/* * We may want to substitute "this command" with a command
Signature · 1 line
2.8.0.rc3.226.g39d4020
Will queue, but with fixes on issues spotted by my pre-acceptance mechanical filter squashed in, to fix style issues in the destination of code movements.
pathspec.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
pathspec.c +5 -5
@@ -68,7 +68,7 @@ static struct pathspec_magic {const char *name; } pathspec_magic[] = { { PATHSPEC_FROMTOP, '/', "top" }, - { PATHSPEC_LITERAL,'\0', "literal" }, + { PATHSPEC_LITERAL, '\0', "literal" }, { PATHSPEC_GLOB, '\0', "glob" }, { PATHSPEC_ICASE, '\0', "icase" }, { PATHSPEC_EXCLUDE, '!', "exclude" },
@@ -290,8 +290,8 @@ static void strip_submodule_slash_expensive(struct pathspec_item *item) item->len--; item->match[item->len] ='\0' ; }else { -die (_ ("Pathspec '%s' is in submodule '%.*s'" ), - item->original, ce_len, ce->name); +die (_ ("Pathspec '%s' is in submodule '%.*s'" ), + item->original, ce_len, ce->name); } } }
@@ -364,10 +364,10 @@ static void init_pathspec_item(struct pathspec_item *item, unsigned flags, }if (flags &PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP ) -strip_submodule_slash_cheap (item); +strip_submodule_slash_cheap (item);if (flags &PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE ) -strip_submodule_slash_expensive (item); +strip_submodule_slash_expensive (item);if (magic &PATHSPEC_LITERAL ) { item->nowildcard_len = item->len;
Re: [PATCH v2 14/16] pathspec: create strip submodule slash helpers
In reply to Brandon Williamspathspec.c static const char *parse_element_magic(unsigned *magic, int *prefix_len, · line 271
Quoted 11 lines · Brandon Williams
pathspec.c +11
-0+static void strip_submodule_slash_cheap (struct pathspec_item *item) +{ +int i; + +if ((item->len >=1 && item->match[item->len -1 ] =='/' ) && + (i =cache_name_pos (item->match, item->len -1 )) >=0 && +S_ISGITLINK (active_cache[i]->ce_mode)) { + item->len--; + item->match[item->len] ='\0' ; + } +}
I know that this is merely a moved code, but while I was reading this, it triggered "Do not make an assignment inside if condition" check. But more importantly, is the code even correct? If the path for the submodule is unmerged, we would get a negative i that points at the conflicting entry; don't we want to do something about it, at least when we have a submodule entry at stage #2 (i.e. ours)?
Re: [PATCH v2 14/16] pathspec: create strip submodule slash helpers
Quoted 17 lines · Junio C Hamano
pathspec.c static const char *parse_element_magic(unsigned *magic, int *prefix_len, · line 271
Quoted 11 lines · Brandon Williams
pathspec.c +11
-0+static void strip_submodule_slash_cheap (struct pathspec_item *item) +{ +int i; + +if ((item->len >=1 && item->match[item->len -1 ] =='/' ) && + (i =cache_name_pos (item->match, item->len -1 )) >=0 && +S_ISGITLINK (active_cache[i]->ce_mode)) { + item->len--; + item->match[item->len] ='\0' ; + } +}I know that this is merely a moved code, but while I was reading this, it triggered "Do not make an assignment inside if condition" check.
Yeah with a function of its own, it's probably better to separate that assignment.
Quoted 4 lines
But more importantly, is the code even correct? If the path for the submodule is unmerged, we would get a negative i that points at the conflicting entry; don't we want to do something about it, at least when we have a submodule entry at stage #2 (i.e. ours)?
In my defense I was simply moving (again!) the code from strip_trailing_slash_from_submodules() in b69bb3f:builtin/ls-files.c. Could be an improvement point for submodule people, I guess.
Signature · 1 line
Duy
Re: [PATCH 02/17] dir: convert create_simplify to use the pathspec struct interface
In reply to Brandon WilliamsQuoted 18 lines · Brandon Williams
Quoted 17 lines · Duy Nguyen
Quoted 16 lines · Brandon Williams
Quoted 14 lines · Duy Nguyen
Quoted 2 lines · Brandon Williams
Convert '
create_simplify()' to use the pathspec struct interface from using the '_raw' entry in the pathspec.It would be even better to kill this
create_simplify()and letsimplify_away()handle struct pathspec directly.There is a bug in this code, that might have been found if we
simpify_away()handled pathspec directly: thememcmp()insimplify_away()will not play well with :(icase) magic. My bad. If :(icase) is used, the easiest/safe way is simplify nothing. Later on maybe we can teachsimplify_away()to do strncasecmp instead. We could ignore exclude patterns there too (although not excluding is not a bug).
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
So are you implying that the simplify struct needs to be killed? That way the pathspec struct itself is being passed around instead?
Yes. simplify struct was a thing when pathspec was an array of char *. At this point I think it can retire (when we have time to retire it)
Alright, then for now I can leave this change as is and have a follow up series that kills the simplify struct.
Do let me know if you decide to drop it, so I can put it back in my backlog.
Signature · 1 line
Duy
Quoted 3 lines · Junio C Hamano
Will queue, but with fixes on issues spotted by my pre-acceptance mechanical filter squashed in, to fix style issues in the destination of code movements.
Is this pre-acceptance filter you use something that I could run locally?
Signature · 1 line
Brandon Williams
PATCH v2 14/16 pathspec: create strip submodule slash helpers
In reply to Duy NguyenFactor out the logic responsible for stripping the trailing slash on pathspecs referencing submodules into its own function.
--- pathspec.c | 68 ++++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 42 insertions(+), 26 deletions(-)
pathspec.c +42 -26
@@ -258,6 +258,44 @@ static const char *parse_element_magic(unsigned *magic, int *prefix_len,return parse_short_magic (magic, elem); } +static void strip_submodule_slash_cheap (struct pathspec_item *item) +{ +if (item->len >=1 && item->match[item->len -1 ] =='/' ) { +int i =cache_name_pos (item->match, item->len -1 ); + +if (i >=0 &&S_ISGITLINK (active_cache[i]->ce_mode)) { + item->len--; + item->match[item->len] ='\0' ; + } + } +} + +static void strip_submodule_slash_expensive (struct pathspec_item *item) +{ +int i; + +for (i =0 ; i < active_nr; i++) { +struct cache_entry *ce = active_cache[i]; +int ce_len =ce_namelen (ce); + +if (!S_ISGITLINK (ce->ce_mode)) +continue ; + +if (item->len <= ce_len || item->match[ce_len] !='/' || +memcmp (ce->name, item->match, ce_len)) +continue ; + +if (item->len == ce_len +1 ) { +/* strip trailing slash */ + item->len--; + item->match[item->len] ='\0' ; + }else { +die (_ ("Pathspec '%s' is in submodule '%.*s'" ), + item->original, ce_len, ce->name); + } + } +} +/* * Take an element of a pathspec and check for magic signatures. * Append the result to the prefix. Return the magic bitmap.
@@ -278,7 +316,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,unsigned magic =0 , element_magic =0 ;const char *copyfrom = elt;char *match; -int i, pathspec_prefix =-1 ; +int pathspec_prefix =-1 ;/* PATHSPEC_LITERAL_PATH ignores magic */ if (flags &PATHSPEC_LITERAL_PATH ) {
@@ -329,33 +367,11 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags, item->len =strlen (item->match); item->prefix = prefixlen; -if ((flags &PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP ) && - (item->len >=1 && item->match[item->len -1 ] =='/' ) && - (i =cache_name_pos (item->match, item->len -1 )) >=0 && -S_ISGITLINK (active_cache[i]->ce_mode)) { - item->len--; - match[item->len] ='\0' ; - } +if (flags &PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP ) +strip_submodule_slash_cheap (item);if (flags &PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE ) -for (i =0 ; i < active_nr; i++) { -struct cache_entry *ce = active_cache[i]; -int ce_len =ce_namelen (ce); - -if (!S_ISGITLINK (ce->ce_mode)) -continue ; - -if (item->len <= ce_len || match[ce_len] !='/' || -memcmp (ce->name, match, ce_len)) -continue ; -if (item->len == ce_len +1 ) { -/* strip trailing slash */ - item->len--; - match[item->len] ='\0' ; - }else -die (_ ("Pathspec '%s' is in submodule '%.*s'" ), - elt, ce_len, ce->name); - } +strip_submodule_slash_expensive (item);if (magic &PATHSPEC_LITERAL ) item->nowildcard_len = item->len;
Signature · 1 line
2.8.0.rc3.226.g39d4020
Re: [PATCH 02/17] dir: convert create_simplify to use the pathspec struct interface
In reply to Duy NguyenQuoted 22 lines · Duy Nguyen
Quoted 21 lines · Brandon Williams
Quoted 20 lines · Duy Nguyen
Quoted 18 lines · Brandon Williams
Quoted 7 lines · Duy Nguyen
Quoted 2 lines · Brandon Williams
Convert '
create_simplify()' to use the pathspec struct interface from using the '_raw' entry in the pathspec.It would be even better to kill this
create_simplify()and letsimplify_away()handle struct pathspec directly.Quoted 7 lines
There is a bug in this code, that might have been found if we
simpify_away()handled pathspec directly: thememcmp()insimplify_away()will not play well with :(icase) magic. My bad. If :(icase) is used, the easiest/safe way is simplify nothing. Later on maybe we can teachsimplify_away()to do strncasecmp instead. We could ignore exclude patterns there too (although not excluding is not a bug).So are you implying that the simplify struct needs to be killed? That way the pathspec struct itself is being passed around instead?
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
Yes. simplify struct was a thing when pathspec was an array of char *. At this point I think it can retire (when we have time to retire it)
Alright, then for now I can leave this change as is and have a follow up series that kills the simplify struct.
Do let me know if you decide to drop it, so I can put it back in my backlog.
K will do
Signature · 1 line
Brandon Williams
Re: [PATCH 14/16] pathspec: create strip submodule slash helpers
In reply to Brandon WilliamsQuoted 4 lines · Brandon Williams
Factor out the logic responsible for stripping the trailing slash on pathspecs referencing submodules into its own function.
Change-Id: Icad62647c04b4195309def0e3db416203d14f9e4
I think we should come up with a solution to wipe out change ids before sending emails. ;)
Quoted 103 lines
Signed-off-by Brandon Williams <[email protected]>--- pathspec.c | 68 ++++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 42 insertions(+), 26 deletions(-)pathspec.c +42
-27@@ -258,6 +258,44 @@ static const char *parse_element_magic(unsigned *magic, int *prefix_len,return parse_short_magic (magic, elem); } +static void strip_submodule_slash_cheap (struct pathspec_item *item) +{ +if (item->len >=1 && item->match[item->len -1 ] =='/' ) { +int i =cache_name_pos (item->match, item->len -1 ); + +if (i >=0 &&S_ISGITLINK (active_cache[i]->ce_mode)) { + item->len--; + item->match[item->len] ='\0' ; + } + } +} + +static void strip_submodule_slash_expensive (struct pathspec_item *item) +{ +int i; + +for (i =0 ; i < active_nr; i++) { +struct cache_entry *ce = active_cache[i]; +int ce_len =ce_namelen (ce); + +if (!S_ISGITLINK (ce->ce_mode)) +continue ; + +if (item->len <= ce_len || item->match[ce_len] !='/' || +memcmp (ce->name, item->match, ce_len)) +continue ; + +if (item->len == ce_len +1 ) { +/* strip trailing slash */ + item->len--; + item->match[item->len] ='\0' ; + }else { +die (_ ("Pathspec '%s' is in submodule '%.*s'" ), + item->original, ce_len, ce->name); + } + } +} +/* * Take an element of a pathspec and check for magic signatures. * Append the result to the prefix. Return the magic bitmap. @@ -278,7 +316,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,unsigned magic =0 , element_magic =0 ;const char *copyfrom = elt;char *match; -int i, pathspec_prefix =-1 ; +int pathspec_prefix =-1 ;/* PATHSPEC_LITERAL_PATH ignores magic */ if (flags &PATHSPEC_LITERAL_PATH ) {@@ -329,33 +367,11 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags, item->len =strlen (item->match); item->prefix = prefixlen; -if ((flags &PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP ) && - (item->len >=1 && item->match[item->len -1 ] =='/' ) && - (i =cache_name_pos (item->match, item->len -1 )) >=0 && -S_ISGITLINK (active_cache[i]->ce_mode)) { - item->len--; - match[item->len] ='\0' ; - } +if (flags &PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP ) +strip_submodule_slash_cheap (item);if (flags &PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE ) -for (i =0 ; i < active_nr; i++) { -struct cache_entry *ce = active_cache[i]; -int ce_len =ce_namelen (ce); - -if (!S_ISGITLINK (ce->ce_mode)) -continue ; - -if (item->len <= ce_len || match[ce_len] !='/' || -memcmp (ce->name, match, ce_len)) -continue ; -if (item->len == ce_len +1 ) { -/* strip trailing slash */ - item->len--; - match[item->len] ='\0' ; - }else -die (_ ("Pathspec '%s' is in submodule '%.*s'" ), - elt, ce_len, ce->name); - } +strip_submodule_slash_expensive (item);if (magic &PATHSPEC_LITERAL ) item->nowildcard_len = item->len; --2.8.0.rc3.226.g39d4020
Re: [PATCH 14/16] pathspec: create strip submodule slash helpers
Quoted 8 lines · Stefan Beller
Quoted 4 lines · Brandon Williams
Factor out the logic responsible for stripping the trailing slash on pathspecs referencing submodules into its own function.
Change-Id: Icad62647c04b4195309def0e3db416203d14f9e4
I think we should come up with a solution to wipe out change ids before sending emails. ;)
Darn! Yeah maybe a hook or something :D
Signature · 1 line
Brandon Williams
Re: [PATCH v2 12/16] pathspec: create parse_long_magic function
In reply to Brandon WilliamsQuoted 6 lines · Brandon Williams
Factor out the logic responsible for parsing long magic into its own function. As well as hoist the prefix check logic outside of the inner loop as there isn't anything that needs to be done after matching "prefix:".
Signed-off-by Brandon Williams <[email protected]>
These refactoring changes look like they are all going in the good direction. Stefan's :(attr:<attribute spec>)path" changes however have severe conflicts (e.g. the topic already does something similar to this step and calls the factored-out function eat_long_magic()).
My gut feeling is that we probably should ask Stefan's series to be rebased on top of this series that cleans up pathspec implementation, once it stabilizes. We could probably go the other way around, but logically it makes more sense to build "pathspec can also match using attributes information" on top of a refactored codebase.
Thoughts?
Re: [PATCH v2 12/16] pathspec: create parse_long_magic function
Quoted 9 lines · Junio C Hamano
Quoted 6 lines · Brandon Williams
Factor out the logic responsible for parsing long magic into its own function. As well as hoist the prefix check logic outside of the inner loop as there isn't anything that needs to be done after matching "prefix:".
Signed-off-by Brandon Williams <[email protected]>
Quoted 8 lines
These refactoring changes look like they are all going in the good direction. Stefan's :(attr:<attribute spec>)path" changes however have severe conflicts (e.g. the topic already does something similar to this step and calls the factored-out function
eat_long_magic()).My gut feeling is that we probably should ask Stefan's series to be rebased on top of this series that cleans up pathspec implementation, once it stabilizes.
Very much so.
Jonathan Nieder mentioned off list that he prefers to see that series rerolled without mutexes if possible. That is possible by creating the questions "struct attr_check" before preloading the index and then using the read only questions in the threaded code, to obtain answers fast; also no need for a mutex.
I did not look into that yet, though. So I think you could discard that series (again) until I find time to either redo the series or resend it with a proper explanation on why the approach above is not feasible.
Quoted 5 lines
We could probably go the other way around, but
logically it makes more sense to build "pathspec can also match using attributes information" on top of a refactored codebase.Thoughts?
Please let the refactoring in in favor of the attr series.
Re: [PATCH v2 12/16] pathspec: create parse_long_magic function
Quoted 32 lines · Stefan Beller
Quoted 9 lines · Junio C Hamano
Quoted 6 lines · Brandon Williams
Factor out the logic responsible for parsing long magic into its own function. As well as hoist the prefix check logic outside of the inner loop as there isn't anything that needs to be done after matching "prefix:".
Signed-off-by Brandon Williams <[email protected]>Quoted 8 lines
These refactoring changes look like they are all going in the good direction. Stefan's :(attr:<attribute spec>)path" changes however have severe conflicts (e.g. the topic already does something similar to this step and calls the factored-out function
eat_long_magic()).My gut feeling is that we probably should ask Stefan's series to be rebased on top of this series that cleans up pathspec implementation, once it stabilizes.
Very much so.
Jonathan Nieder mentioned off list that he prefers to see that series rerolled without mutexes if possible. That is possible by creating the questions "struct
attr_check" before preloading the index and then using the read only questions in the threaded code, to obtain answers fast; also no need for a mutex.I did not look into that yet, though. So I think you could discard that series (again) until I find time to either redo the series or resend it with a proper explanation on why the approach above is not feasible.
Quoted 7 lines
Quoted 5 lines
We could probably go the other way around, but
logically it makes more sense to build "pathspec can also match using attributes information" on top of a refactored codebase.Thoughts?
Please let the refactoring in in favor of the attr series.
Sounds good. I only looked at your series briefly, but I'm hoping that these refactoring changes I'm proposing make it relatively easy for you to build on top of in the future.
Signature · 1 line
Brandon Williams
Re: [PATCH v2 12/16] pathspec: create parse_long_magic function
In reply to Stefan BellerQuoted 5 lines · Stefan Beller
Jonathan Nieder mentioned off list that he prefers to see that series rerolled without mutexes if possible. That is possible by creating the questions "struct
attr_check" before preloading the index and then using the read only questions in the threaded code, to obtain answers fast; also no need for a mutex.
I do not see how it would work without further splitting the attr_stack. I think you made it per check[], but you would further split it per <check, thread> before losing the mutex, no?
Re: [PATCH v2 12/16] pathspec: create parse_long_magic function
Quoted 11 lines · Junio C Hamano
Quoted 5 lines · Stefan Beller
Jonathan Nieder mentioned off list that he prefers to see that series rerolled without mutexes if possible. That is possible by creating the questions "struct
attr_check" before preloading the index and then using the read only questions in the threaded code, to obtain answers fast; also no need for a mutex.I do not see how it would work without further splitting the
attr_stack. I think you made it per check[], but you would further split it per <check, thread> before losing the mutex, no?
Well I have not yet looked into it again, so my memories are rusty, but the <check> is read only, such that the answers only need to be per thread?
Re: [PATCH v2 12/16] pathspec: create parse_long_magic function
Quoted 7 lines · Stefan Beller
Quoted 3 lines
I do not see how it would work without further splitting the
attr_stack. I think you made it per check[], but you would further split it per <check, thread> before losing the mutex, no?Well I have not yet looked into it again, so my memories are rusty, but the <check> is read only, such that the answers only need to be per thread?
<check> is read-only, so as long as you populate the singleton attr's beforehand, they can be shared across threads. <answer> of course needs an instance per thread, and that is why you have them typically on stack.
The process of filling <answer> by asking for a set of attrs in <check> for one <path> goes roughly like:
- make sure attr_stack is set up for <path>, namely, the info/attributes and .gitattributes files for each leading directory are parsed.
- go over the attr_stack entries and see what entries match <path>, and collect the result for <check> in <answer>
Before d90675c151 ("attr: keep attr stack for each check", 2016-11-10), I had only one instance of an attr stack [*1*], but with that commit, you made it per <check>, which is a good move to allow us to optimize by keeping only the attributes relevant to <check> on the attr stack.
But it does not solve the threading issue.
If multiple threads are asking for the same set of attrs (i.e. running the same codepath using the same <check>) but for <path>s in different parts of the working tree (e.g. "git checkout" that is multi-threaded, each thread asking for eol related attributes and checking out different subdirectories), you'd need mutex for correct operation at least, but that won't perform well because you'd end up thrashing the attr stack. You'd need to split attr stack further and make it per (<check>, thread) tuple and you no longer need mutex at that point, but not before that.
[footnote]
*1* This is because the attr subsystem originally wasn't designed to
be used from multiple threads at the same time hence it was sufficient to have a single "currently interested are of the directory hierarchy".
Re: [PATCH 02/17] dir: convert create_simplify to use the pathspec struct interface
In reply to Brandon WilliamsQuoted 26 lines · Brandon Williams
Quoted 25 lines · Duy Nguyen
Quoted 24 lines · Brandon Williams
Quoted 22 lines · Duy Nguyen
Quoted 16 lines · Brandon Williams
Quoted 7 lines · Duy Nguyen
Quoted 2 lines · Brandon Williams
Convert '
create_simplify()' to use the pathspec struct interface from using the '_raw' entry in the pathspec.It would be even better to kill this
create_simplify()and letsimplify_away()handle struct pathspec directly.Quoted 7 lines
There is a bug in this code, that might have been found if we
simpify_away()handled pathspec directly: thememcmp()insimplify_away()will not play well with :(icase) magic. My bad. If :(icase) is used, the easiest/safe way is simplify nothing. Later on maybe we can teachsimplify_away()to do strncasecmp instead. We could ignore exclude patterns there too (although not excluding is not a bug).Quoted 2 lines
So are you implying that the simplify struct needs to be killed? That way the pathspec struct itself is being passed around instead?
Yes. simplify struct was a thing when pathspec was an array of char *. At this point I think it can retire (when we have time to retire it)
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
Alright, then for now I can leave this change as is and have a follow up series that kills the simplify struct.
Do let me know if you decide to drop it, so I can put it back in my backlog.
K will do
This actually turned out to be more straight forward than I thought. I'll reroll this series again (with a few other changes) and include killing the simplify struct.
Signature · 1 line
Brandon Williams
v3 cover pathspec cleanup
In reply to Brandon WilliamsDifferences in v3:
* more readable strip submodule slash helper function which conforms to git's style guide. [14/16]
* instead of having create_simply() use struct pathspec directly, remove the struct path_simplify entirely and use struct pathspec directly in both simplify_away() and exclude_matches_pathspec(). [02/16]
* small style issues corrected from v2. [15/16]
Brandon Williams (16):
mv: remove use of deprecated 'get_pathspec()'
dir: remove struct path_simplify
dir: convert fill_directory to use the pathspec struct interface
ls-tree: convert show_recursive to use the pathspec struct interface
pathspec: remove the deprecated get_pathspec function
pathspec: copy and free owned memory
pathspec: remove unused variable from unsupported_magic
pathspec: always show mnemonic and name in unsupported_magic
pathspec: simpler logic to prefix original pathspec elements
pathspec: factor global magic into its own function
pathspec: create parse_short_magic function
pathspec: create parse_long_magic function
pathspec: create parse_element_magic helper
pathspec: create strip submodule slash helpers
pathspec: small readability changes
pathspec: rename prefix_pathspec to init_pathspec_item
Documentation/technical/api-setup.txt | 2 - builtin/ls-tree.c | 16 +- builtin/mv.c | 50 ++-- cache.h | 1 - dir.c | 166 +++++------- pathspec.c | 476 +++++++++++++++++++--------------- pathspec.h | 5 +- 7 files changed, 369 insertions(+), 347 deletions(-)
--- interdiff on 'origin/bw/pathspec-cleanup'
dir.c +60 -93
@@ -16,11 +16,6 @@#include "varint.h" #include "ewah/ewok.h" -struct path_simplify { -int len; -const char *path; -}; -/* * Tells read_directory_recursive how a file or directory should be treated. * Values are ordered by significance, e.g. if a directory contains both
@@ -50,7 +45,7 @@ struct cached_dir {static enum path_treatment read_directory_recursive (struct dir_struct *dir,const char *path,int len,struct untracked_cache_dir *untracked, -int check_only,const struct path_simplify *simplify); +int check_only,const struct pathspec *pathspec);static int get_dtype (struct dirent *de,const char *path,int len);int fspathcmp (const char *a,const char *b)
@@ -1316,7 +1311,7 @@ static enum exist_status directory_exists_in_index(const char *dirname, int len)static enum path_treatment treat_directory (struct dir_struct *dir,struct untracked_cache_dir *untracked,const char *dirname,int len,int baselen,int exclude, -const struct path_simplify *simplify) +const struct pathspec *pathspec) {/* The "len-1" is to strip the final '/' */ switch (directory_exists_in_index (dirname, len-1 )) {
@@ -1345,7 +1340,7 @@ static enum path_treatment treat_directory(struct dir_struct *dir, untracked =lookup_untracked (dir->untracked, untracked, dirname + baselen, len - baselen);return read_directory_recursive (dir, dirname, len, - untracked,1 , simplify); + untracked,1 , pathspec); }/*
@@ -1353,24 +1348,25 @@ static enum path_treatment treat_directory(struct dir_struct *dir,* reading - if the path cannot possibly be in the pathspec, * return true, and we'll skip it early. */ -static int simplify_away (const char *path,int pathlen,const struct path_simplify *simplify) +static int simplify_away (const char *path,int pathlen, +const struct pathspec *pathspec) { -if (simplify) { -for (;;) { -const char *match = simplify->path; -int len = simplify->len; +int i; -if (!match) -break ; -if (len > pathlen) - len = pathlen; -if (!memcmp (path, match, len)) -return 0 ; - simplify++; - } -return 1 ; +if (!pathspec || !pathspec->nr) +return 0 ; + +for (i =0 ; i < pathspec->nr; i++) { +const struct pathspec_item *item = &pathspec->items[i]; +int len = item->nowildcard_len; + +if (len > pathlen) + len = pathlen; +if (!ps_strncmp (item, item->match, path, len)) +return 0 ; } -return 0 ; + +return 1 ; }/*
@@ -1384,19 +1380,25 @@ static int simplify_away(const char *path, int pathlen, const struct path_simpli* 2. the path is a directory prefix of some element in the * pathspec */ -static int exclude_matches_pathspec (const char *path,int len, -const struct path_simplify *simplify) -{ -if (simplify) { -for (; simplify->path; simplify++) { -if (len == simplify->len - && !memcmp (path, simplify->path, len)) -return 1 ; -if (len < simplify->len - && simplify->path[len] =='/' - && !memcmp (path, simplify->path, len)) -return 1 ; - } +static int exclude_matches_pathspec (const char *path,int pathlen, +const struct pathspec *pathspec) +{ +int i; + +if (!pathspec || !pathspec->nr) +return 0 ; + +for (i =0 ; i < pathspec->nr; i++) { +const struct pathspec_item *item = &pathspec->items[i]; +int len = item->nowildcard_len; + +if (len == pathlen && + !ps_strncmp (item, item->match, path, pathlen)) +return 1 ; +if (len > pathlen && + item->match[pathlen] =='/' && + !ps_strncmp (item, item->match, path, pathlen)) +return 1 ; }return 0 ; }
@@ -1464,7 +1466,7 @@ static enum path_treatment treat_one_path(struct dir_struct *dir,struct untracked_cache_dir *untracked, struct strbuf *path, int baselen, - const struct path_simplify *simplify, + const struct pathspec *pathspec, int dtype, struct dirent *de) { int exclude;
@@ -1516,7 +1518,7 @@ static enum path_treatment treat_one_path(struct dir_struct *dir,case DT_DIR :strbuf_addch (path,'/' );return treat_directory (dir, untracked, path->buf, path->len, - baselen, exclude, simplify); + baselen, exclude, pathspec);case DT_REG :case DT_LNK :return exclude ? path_excluded : path_untracked;
@@ -1528,7 +1530,7 @@ static enum path_treatment treat_path_fast(struct dir_struct *dir,struct cached_dir *cdir, struct strbuf *path, int baselen, - const struct path_simplify *simplify) + const struct pathspec *pathspec) {strbuf_setlen (path ,baselen );if (!cdir->ucd) {
@@ -1545,7 +1547,7 @@ static enum path_treatment treat_path_fast(struct dir_struct *dir,* with check_only set. */ return read_directory_recursive (dir, path->buf, path->len, - cdir->ucd,1 , simplify); + cdir->ucd,1 , pathspec);/* * We get path_recurse in the first run when * directory_exists_in_index() returns index_nonexistent. We
@@ -1560,23 +1562,23 @@ static enum path_treatment treat_path(struct dir_struct *dir,struct cached_dir *cdir, struct strbuf *path, int baselen, - const struct path_simplify *simplify) + const struct pathspec *pathspec) { int dtype;struct dirent *de = cdir->de;if (!de)return treat_path_fast (dir, untracked, cdir, path, - baselen, simplify); + baselen, pathspec);if (is_dot_or_dotdot (de->d_name) || !strcmp (de->d_name,".git" ))return path_none;strbuf_setlen (path, baselen);strbuf_addstr (path, de->d_name); -if (simplify_away (path->buf, path->len, simplify)) +if (simplify_away (path->buf, path->len, pathspec))return path_none; dtype =DTYPE (de); -return treat_one_path (dir, untracked, path, baselen, simplify, dtype, de); +return treat_one_path (dir, untracked, path, baselen, pathspec, dtype, de); }static void add_untracked (struct untracked_cache_dir *dir,const char *name)
@@ -1707,7 +1709,7 @@ static void close_cached_dir(struct cached_dir *cdir)static enum path_treatment read_directory_recursive (struct dir_struct *dir,const char *base,int baselen,struct untracked_cache_dir *untracked,int check_only, -const struct path_simplify *simplify) +const struct pathspec *pathspec) {struct cached_dir cdir;enum path_treatment state, subdir_state, dir_state = path_none;
@@ -1723,7 +1725,8 @@ static enum path_treatment read_directory_recursive(struct dir_struct *dir,while (!read_cached_dir (&cdir)) {/* check how the file or directory should be treated */ - state =treat_path (dir, untracked, &cdir, &path, baselen, simplify); + state =treat_path (dir, untracked, &cdir, &path, + baselen, pathspec);if (state > dir_state) dir_state = state;
@@ -1735,8 +1738,9 @@ static enum path_treatment read_directory_recursive(struct dir_struct *dir, path.buf + baselen, path.len - baselen); subdir_state = -read_directory_recursive (dir, path.buf, path.len, - ud, check_only, simplify); +read_directory_recursive (dir, path.buf, + path.len, ud, + check_only, pathspec);if (subdir_state > dir_state) dir_state = subdir_state; }
@@ -1760,7 +1764,7 @@ static enum path_treatment read_directory_recursive(struct dir_struct *dir,else if ((dir->flags &DIR_SHOW_IGNORED_TOO ) || ((dir->flags &DIR_COLLECT_IGNORED ) &&exclude_matches_pathspec (path.buf, path.len, - simplify))) + pathspec)))dir_add_ignored (dir, path.buf, path.len);break ;
@@ -1791,35 +1795,9 @@ static int cmp_name(const void *p1, const void *p2)return name_compare (e1->name, e1->len, e2->name, e2->len); } -static struct path_simplify *create_simplify (const struct pathspec *pathspec) -{ -int i; -struct path_simplify *simplify =NULL ; - -if (!pathspec || !pathspec->nr) -return NULL ; - -ALLOC_ARRAY (simplify, pathspec->nr +1 ); -for (i =0 ; i < pathspec->nr; i++) { -const char *match; - match = pathspec->items[i].match; - simplify[i].path = match; - simplify[i].len = pathspec->items[i].nowildcard_len; - } - simplify[i].path =NULL ; - simplify[i].len =0 ; - -return simplify; -} - -static void free_simplify (struct path_simplify *simplify) -{ -free (simplify); -} -static int treat_leading_path (struct dir_struct *dir,const char *path,int len, -const struct path_simplify *simplify) +const struct pathspec *pathspec) {struct strbuf sb =STRBUF_INIT ;int baselen, rc =0 ;
@@ -1843,9 +1821,9 @@ static int treat_leading_path(struct dir_struct *dir,strbuf_add (&sb, path, baselen);if (!is_directory (sb.buf))break ; -if (simplify_away (sb.buf, sb.len, simplify)) +if (simplify_away (sb.buf, sb.len, pathspec))break ; -if (treat_one_path (dir,NULL , &sb, baselen, simplify, +if (treat_one_path (dir,NULL , &sb, baselen, pathspec,DT_DIR ,NULL ) == path_none)break ;/* do not recurse into it */ if (len <= baselen) {
@@ -2013,14 +1991,11 @@ static struct untracked_cache_dir *validate_untracked_cache(struct dir_struct *dreturn root; } -int read_directory (struct dir_struct *dir,const char *path,int len,const struct pathspec *pathspec) +int read_directory (struct dir_struct *dir,const char *path, +int len,const struct pathspec *pathspec) { -struct path_simplify *simplify;struct untracked_cache_dir *untracked; -/* -* Check out create_simplify() -*/ if (pathspec) GUARD_PATHSPEC(pathspec, PATHSPEC_FROMTOP |
@@ -2033,13 +2008,6 @@ int read_directory(struct dir_struct *dir, const char *path, int len, const struif (has_symlink_leading_path (path, len))return dir->nr; -/* -* exclude patterns are treated like positive ones in -* create_simplify. Usually exclude patterns should be a -* subset of positive ones, which has no impacts on -* create_simplify(). -*/ - simplify =create_simplify (pathspec); untracked =validate_untracked_cache (dir, len, pathspec);if (!untracked)/*
@@ -2047,9 +2015,8 @@ int read_directory(struct dir_struct *dir, const char *path, int len, const stru* e.g. prep_exclude() */ dir->untracked =NULL ; -if (!len ||treat_leading_path (dir, path, len, simplify)) -read_directory_recursive (dir, path, len, untracked,0 , simplify); -free_simplify (simplify); +if (!len ||treat_leading_path (dir, path, len, pathspec)) +read_directory_recursive (dir, path, len, untracked,0 , pathspec);QSORT (dir->entries, dir->nr, cmp_name);QSORT (dir->ignored, dir->ignored_nr, cmp_name);if (dir->untracked) {
pathspec.c +10 -10
@@ -67,11 +67,11 @@ static struct pathspec_magic {char mnemonic;/* this cannot be ':'! */ const char *name; } pathspec_magic[] = { - { PATHSPEC_FROMTOP, '/', "top" }, + { PATHSPEC_FROMTOP, '/', "top" }, { PATHSPEC_LITERAL, '\0', "literal" }, - { PATHSPEC_GLOB, '\0', "glob" }, - { PATHSPEC_ICASE, '\0', "icase" }, - { PATHSPEC_EXCLUDE, '!', "exclude" }, + { PATHSPEC_GLOB, '\0', "glob" }, + { PATHSPEC_ICASE, '\0', "icase" }, + { PATHSPEC_EXCLUDE, '!', "exclude" }, }; static void prefix_magic(struct strbuf *sb, int prefixlen, unsigned magic)
@@ -260,13 +260,13 @@ static const char *parse_element_magic(unsigned *magic, int *prefix_len,static void strip_submodule_slash_cheap (struct pathspec_item *item) { -int i; +if (item->len >=1 && item->match[item->len -1 ] =='/' ) { +int i =cache_name_pos (item->match, item->len -1 ); -if ((item->len >=1 && item->match[item->len -1 ] =='/' ) && - (i =cache_name_pos (item->match, item->len -1 )) >=0 && -S_ISGITLINK (active_cache[i]->ce_mode)) { - item->len--; - item->match[item->len] ='\0' ; +if (i >=0 &&S_ISGITLINK (active_cache[i]->ce_mode)) { + item->len--; + item->match[item->len] ='\0' ; + } } }
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH v3 2/16 dir: remove struct path_simplify
Teach simplify_away() and exclude_matches_pathspec() to handle struct pathspec directly, eliminating the need for the struct path_simplify.
Also renamed the len parameter to pathlen in exclude_matches_pathspec() to match the parameter names used in simplify_away().
--- dir.c | 154 ++++++++++++++++++++++++++---------------------------------------- 1 file changed, 60 insertions(+), 94 deletions(-)
dir.c +60 -94
@@ -16,11 +16,6 @@#include "varint.h" #include "ewah/ewok.h" -struct path_simplify { -int len; -const char *path; -}; -/* * Tells read_directory_recursive how a file or directory should be treated. * Values are ordered by significance, e.g. if a directory contains both
@@ -50,7 +45,7 @@ struct cached_dir {static enum path_treatment read_directory_recursive (struct dir_struct *dir,const char *path,int len,struct untracked_cache_dir *untracked, -int check_only,const struct path_simplify *simplify); +int check_only,const struct pathspec *pathspec);static int get_dtype (struct dirent *de,const char *path,int len);int fspathcmp (const char *a,const char *b)
@@ -1312,7 +1307,7 @@ static enum exist_status directory_exists_in_index(const char *dirname, int len)static enum path_treatment treat_directory (struct dir_struct *dir,struct untracked_cache_dir *untracked,const char *dirname,int len,int baselen,int exclude, -const struct path_simplify *simplify) +const struct pathspec *pathspec) {/* The "len-1" is to strip the final '/' */ switch (directory_exists_in_index (dirname, len-1 )) {
@@ -1341,7 +1336,7 @@ static enum path_treatment treat_directory(struct dir_struct *dir, untracked =lookup_untracked (dir->untracked, untracked, dirname + baselen, len - baselen);return read_directory_recursive (dir, dirname, len, - untracked,1 , simplify); + untracked,1 , pathspec); }/*
@@ -1349,24 +1344,25 @@ static enum path_treatment treat_directory(struct dir_struct *dir,* reading - if the path cannot possibly be in the pathspec, * return true, and we'll skip it early. */ -static int simplify_away (const char *path,int pathlen,const struct path_simplify *simplify) +static int simplify_away (const char *path,int pathlen, +const struct pathspec *pathspec) { -if (simplify) { -for (;;) { -const char *match = simplify->path; -int len = simplify->len; +int i; -if (!match) -break ; -if (len > pathlen) - len = pathlen; -if (!memcmp (path, match, len)) -return 0 ; - simplify++; - } -return 1 ; +if (!pathspec || !pathspec->nr) +return 0 ; + +for (i =0 ; i < pathspec->nr; i++) { +const struct pathspec_item *item = &pathspec->items[i]; +int len = item->nowildcard_len; + +if (len > pathlen) + len = pathlen; +if (!ps_strncmp (item, item->match, path, len)) +return 0 ; } -return 0 ; + +return 1 ; }/*
@@ -1380,19 +1376,25 @@ static int simplify_away(const char *path, int pathlen, const struct path_simpli* 2. the path is a directory prefix of some element in the * pathspec */ -static int exclude_matches_pathspec (const char *path,int len, -const struct path_simplify *simplify) -{ -if (simplify) { -for (; simplify->path; simplify++) { -if (len == simplify->len - && !memcmp (path, simplify->path, len)) -return 1 ; -if (len < simplify->len - && simplify->path[len] =='/' - && !memcmp (path, simplify->path, len)) -return 1 ; - } +static int exclude_matches_pathspec (const char *path,int pathlen, +const struct pathspec *pathspec) +{ +int i; + +if (!pathspec || !pathspec->nr) +return 0 ; + +for (i =0 ; i < pathspec->nr; i++) { +const struct pathspec_item *item = &pathspec->items[i]; +int len = item->nowildcard_len; + +if (len == pathlen && + !ps_strncmp (item, item->match, path, pathlen)) +return 1 ; +if (len > pathlen && + item->match[pathlen] =='/' && + !ps_strncmp (item, item->match, path, pathlen)) +return 1 ; }return 0 ; }
@@ -1460,7 +1462,7 @@ static enum path_treatment treat_one_path(struct dir_struct *dir,struct untracked_cache_dir *untracked, struct strbuf *path, int baselen, - const struct path_simplify *simplify, + const struct pathspec *pathspec, int dtype, struct dirent *de) { int exclude;
@@ -1512,7 +1514,7 @@ static enum path_treatment treat_one_path(struct dir_struct *dir,case DT_DIR :strbuf_addch (path,'/' );return treat_directory (dir, untracked, path->buf, path->len, - baselen, exclude, simplify); + baselen, exclude, pathspec);case DT_REG :case DT_LNK :return exclude ? path_excluded : path_untracked;
@@ -1524,7 +1526,7 @@ static enum path_treatment treat_path_fast(struct dir_struct *dir,struct cached_dir *cdir, struct strbuf *path, int baselen, - const struct path_simplify *simplify) + const struct pathspec *pathspec) {strbuf_setlen (path ,baselen );if (!cdir->ucd) {
@@ -1541,7 +1543,7 @@ static enum path_treatment treat_path_fast(struct dir_struct *dir,* with check_only set. */ return read_directory_recursive (dir, path->buf, path->len, - cdir->ucd,1 , simplify); + cdir->ucd,1 , pathspec);/* * We get path_recurse in the first run when * directory_exists_in_index() returns index_nonexistent. We
@@ -1556,23 +1558,23 @@ static enum path_treatment treat_path(struct dir_struct *dir,struct cached_dir *cdir, struct strbuf *path, int baselen, - const struct path_simplify *simplify) + const struct pathspec *pathspec) { int dtype;struct dirent *de = cdir->de;if (!de)return treat_path_fast (dir, untracked, cdir, path, - baselen, simplify); + baselen, pathspec);if (is_dot_or_dotdot (de->d_name) || !strcmp (de->d_name,".git" ))return path_none;strbuf_setlen (path, baselen);strbuf_addstr (path, de->d_name); -if (simplify_away (path->buf, path->len, simplify)) +if (simplify_away (path->buf, path->len, pathspec))return path_none; dtype =DTYPE (de); -return treat_one_path (dir, untracked, path, baselen, simplify, dtype, de); +return treat_one_path (dir, untracked, path, baselen, pathspec, dtype, de); }static void add_untracked (struct untracked_cache_dir *dir,const char *name)
@@ -1703,7 +1705,7 @@ static void close_cached_dir(struct cached_dir *cdir)static enum path_treatment read_directory_recursive (struct dir_struct *dir,const char *base,int baselen,struct untracked_cache_dir *untracked,int check_only, -const struct path_simplify *simplify) +const struct pathspec *pathspec) {struct cached_dir cdir;enum path_treatment state, subdir_state, dir_state = path_none;
@@ -1719,7 +1721,8 @@ static enum path_treatment read_directory_recursive(struct dir_struct *dir,while (!read_cached_dir (&cdir)) {/* check how the file or directory should be treated */ - state =treat_path (dir, untracked, &cdir, &path, baselen, simplify); + state =treat_path (dir, untracked, &cdir, &path, + baselen, pathspec);if (state > dir_state) dir_state = state;
@@ -1731,8 +1734,9 @@ static enum path_treatment read_directory_recursive(struct dir_struct *dir, path.buf + baselen, path.len - baselen); subdir_state = -read_directory_recursive (dir, path.buf, path.len, - ud, check_only, simplify); +read_directory_recursive (dir, path.buf, + path.len, ud, + check_only, pathspec);if (subdir_state > dir_state) dir_state = subdir_state; }
@@ -1756,7 +1760,7 @@ static enum path_treatment read_directory_recursive(struct dir_struct *dir,else if ((dir->flags &DIR_SHOW_IGNORED_TOO ) || ((dir->flags &DIR_COLLECT_IGNORED ) &&exclude_matches_pathspec (path.buf, path.len, - simplify))) + pathspec)))dir_add_ignored (dir, path.buf, path.len);break ;
@@ -1787,36 +1791,9 @@ static int cmp_name(const void *p1, const void *p2)return name_compare (e1->name, e1->len, e2->name, e2->len); } -static struct path_simplify *create_simplify (const char **pathspec) -{ -int nr, alloc =0 ; -struct path_simplify *simplify =NULL ; - -if (!pathspec) -return NULL ; - -for (nr =0 ; ; nr++) { -const char *match; -ALLOC_GROW (simplify, nr +1 , alloc); - match = *pathspec++; -if (!match) -break ; - simplify[nr].path = match; - simplify[nr].len =simple_length (match); - } - simplify[nr].path =NULL ; - simplify[nr].len =0 ; -return simplify; -} - -static void free_simplify (struct path_simplify *simplify) -{ -free (simplify); -} -static int treat_leading_path (struct dir_struct *dir,const char *path,int len, -const struct path_simplify *simplify) +const struct pathspec *pathspec) {struct strbuf sb =STRBUF_INIT ;int baselen, rc =0 ;
@@ -1840,9 +1817,9 @@ static int treat_leading_path(struct dir_struct *dir,strbuf_add (&sb, path, baselen);if (!is_directory (sb.buf))break ; -if (simplify_away (sb.buf, sb.len, simplify)) +if (simplify_away (sb.buf, sb.len, pathspec))break ; -if (treat_one_path (dir,NULL , &sb, baselen, simplify, +if (treat_one_path (dir,NULL , &sb, baselen, pathspec,DT_DIR ,NULL ) == path_none)break ;/* do not recurse into it */ if (len <= baselen) {
@@ -2010,14 +1987,11 @@ static struct untracked_cache_dir *validate_untracked_cache(struct dir_struct *dreturn root; } -int read_directory (struct dir_struct *dir,const char *path,int len,const struct pathspec *pathspec) +int read_directory (struct dir_struct *dir,const char *path, +int len,const struct pathspec *pathspec) { -struct path_simplify *simplify;struct untracked_cache_dir *untracked; -/* -* Check out create_simplify() -*/ if (pathspec) GUARD_PATHSPEC(pathspec, PATHSPEC_FROMTOP |
@@ -2030,13 +2004,6 @@ int read_directory(struct dir_struct *dir, const char *path, int len, const struif (has_symlink_leading_path (path, len))return dir->nr; -/* -* exclude patterns are treated like positive ones in -* create_simplify. Usually exclude patterns should be a -* subset of positive ones, which has no impacts on -* create_simplify(). -*/ - simplify =create_simplify (pathspec ? pathspec->_raw :NULL ); untracked =validate_untracked_cache (dir, len, pathspec);if (!untracked)/*
@@ -2044,9 +2011,8 @@ int read_directory(struct dir_struct *dir, const char *path, int len, const stru* e.g. prep_exclude() */ dir->untracked =NULL ; -if (!len ||treat_leading_path (dir, path, len, simplify)) -read_directory_recursive (dir, path, len, untracked,0 , simplify); -free_simplify (simplify); +if (!len ||treat_leading_path (dir, path, len, pathspec)) +read_directory_recursive (dir, path, len, untracked,0 , pathspec);QSORT (dir->entries, dir->nr, cmp_name);QSORT (dir->ignored, dir->ignored_nr, cmp_name);if (dir->untracked) {
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH v3 4/16 ls-tree: convert show_recursive to use the pathspec struct interface
In reply to Brandon WilliamsConvert 'show_recursive()' to use the pathspec struct interface from using the '_raw' entry in the pathspec struct.
--- builtin/ls-tree.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-)
builtin/ls-tree.c +7 -9
@@ -31,21 +31,18 @@ static const char * const ls_tree_usage[] = {static int show_recursive (const char *base,int baselen,const char *pathname) { -const char **s; +int i;if (ls_options &LS_RECURSIVE )return 1 ; - s = pathspec._raw; -if (!s) +if (!pathspec.nr)return 0 ; -for (;;) { -const char *spec = *s++; +for (i =0 ; i < pathspec.nr; i++) { +const char *spec = pathspec.items[i].match;int len, speclen; -if (!spec) -return 0 ;if (strncmp (base, spec, baselen))continue ; len =strlen (pathname);
@@ -59,6 +56,7 @@ static int show_recursive(const char *base, int baselen, const char *pathname)continue ;return 1 ; } +return 0 ; } static int show_tree(const unsigned char *sha1, struct strbuf *base,
@@ -175,8 +173,8 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)* cannot be lifted until it is converted to use * match_pathspec() or tree_entry_interesting() */ -parse_pathspec (&pathspec,PATHSPEC_GLOB |PATHSPEC_ICASE | -PATHSPEC_EXCLUDE , +parse_pathspec (&pathspec,PATHSPEC_ALL_MAGIC & + ~(PATHSPEC_FROMTOP |PATHSPEC_LITERAL ),PATHSPEC_PREFER_CWD , prefix, argv +1 );for (i =0 ; i < pathspec.nr; i++)
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH v3 3/16 dir: convert fill_directory to use the pathspec struct interface
In reply to Brandon WilliamsConvert 'fill_directory()' to use the pathspec struct interface from using the '_raw' entry in the pathspec struct.
--- dir.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
dir.c +8 -4
@@ -174,17 +174,21 @@ char *common_prefix(const struct pathspec *pathspec)int fill_directory (struct dir_struct *dir,const struct pathspec *pathspec) { -size_t len; +char *prefix; +size_t prefix_len;/* * Calculate common prefix for the pathspec, and * use that to optimize the directory walk */ - len =common_prefix_len (pathspec); + prefix =common_prefix (pathspec); + prefix_len = prefix ?strlen (prefix) :0 ;/* Read the directory and prune it */ -read_directory (dir, pathspec->nr ? pathspec->_raw[0 ] :"" , len, pathspec); -return len; +read_directory (dir, prefix, prefix_len, pathspec); + +free (prefix); +return prefix_len; } int within_depth(const char *name, int namelen,
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH v3 7/16 pathspec: remove unused variable from unsupported_magic
In reply to Brandon WilliamsRemoved unused variable 'n' from the 'unsupported_magic()' function.
--- pathspec.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
pathspec.c +2 -3
@@ -333,8 +333,8 @@ static void NORETURN unsupported_magic(const char *pattern, unsigned short_magic) {struct strbuf sb =STRBUF_INIT ; -int i, n; -for (n = i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) { +int i; +for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) {const struct pathspec_magic *m = pathspec_magic + i;if (!(magic & m->bit))continue ;
@@ -344,7 +344,6 @@ static void NORETURN unsupported_magic(const char *pattern,strbuf_addf (&sb,"'%c'" , m->mnemonic); else strbuf_addf(&sb, "'%s'", m->name); - n++; }/* * We may want to substitute "this command" with a command
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH v3 8/16 pathspec: always show mnemonic and name in unsupported_magic
In reply to Brandon WilliamsFor better clarity, always show the mnemonic and name of the unsupported magic being used. This lets users have a more clear understanding of what magic feature isn't supported. And if they supplied a mnemonic, the user will be told what its corresponding name is which will allow them to more easily search the man pages for that magic type.
This also avoids passing an extra parameter around the pathspec initialization code.
--- pathspec.c | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-)
pathspec.c +7 -14
@@ -101,9 +101,7 @@ static void prefix_short_magic(struct strbuf *sb, int prefixlen,* the prefix part must always match literally, and a single stupid * string cannot express such a case. */ -static unsigned prefix_pathspec (struct pathspec_item *item, -unsigned *p_short_magic, -unsigned flags, +static unsigned prefix_pathspec (struct pathspec_item *item,unsigned flags,const char *prefix,int prefixlen,const char *elt) {
@@ -210,7 +208,6 @@ static unsigned prefix_pathspec(struct pathspec_item *item, } magic |= short_magic; - *p_short_magic = short_magic;/* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */ if (noglob_global && !(magic &PATHSPEC_GLOB ))
@@ -329,8 +326,7 @@ static int pathspec_item_cmp(const void *a_, const void *b_) } static void NORETURN unsupported_magic(const char *pattern, -unsigned magic, -unsigned short_magic) +unsigned magic) {struct strbuf sb =STRBUF_INIT ;int i;
@@ -340,8 +336,9 @@ static void NORETURN unsupported_magic(const char *pattern,continue ;if (sb.len)strbuf_addch (&sb,' ' ); -if (short_magic & m->bit) -strbuf_addf (&sb,"'%c'" , m->mnemonic); + +if (m->mnemonic) +strbuf_addf (&sb,"'(%c)%s'" , m->mnemonic, m->name);else strbuf_addf (&sb,"'%s'" , m->name); }
@@ -413,11 +410,9 @@ void parse_pathspec(struct pathspec *pathspec, prefixlen = prefix ?strlen (prefix) :0 ;for (i =0 ; i < n; i++) { -unsigned short_magic; entry = argv[i]; - item[i].magic =prefix_pathspec (item + i, &short_magic, - flags, + item[i].magic =prefix_pathspec (item + i, flags, prefix, prefixlen, entry);if ((flags &PATHSPEC_LITERAL_PATH ) && !(magic_mask &PATHSPEC_LITERAL ))
@@ -425,9 +420,7 @@ void parse_pathspec(struct pathspec *pathspec,if (item[i].magic &PATHSPEC_EXCLUDE ) nr_exclude++;if (item[i].magic & magic_mask) -unsupported_magic (entry, - item[i].magic & magic_mask, - short_magic); +unsupported_magic (entry, item[i].magic & magic_mask);if ((flags &PATHSPEC_SYMLINK_LEADING_PATH ) &&has_symlink_leading_path (item[i].match, item[i].len)) {
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH v3 6/16 pathspec: copy and free owned memory
In reply to Brandon WilliamsThe 'original' string entry in a pathspec_item is only duplicated some of the time, instead always make a copy of the original and take ownership of the memory.
Since both 'match' and 'original' string entries in a pathspec_item are owned by the pathspec struct, they need to be freed when clearing the pathspec struct (in 'clear_pathspec()') and duplicated when copying the pathspec struct (in 'copy_pathspec()').
Also change the type of 'match' and 'original' to 'char *' in order to more explicitly show the ownership of the memory.
--- pathspec.c | 22 ++++++++++++++++++---- pathspec.h | 4 ++-- 2 files changed, 20 insertions(+), 6 deletions(-)
pathspec.c +18 -4
@@ -259,8 +259,9 @@ static unsigned prefix_pathspec(struct pathspec_item *item, }strbuf_addstr (&sb, match); item->original =strbuf_detach (&sb,NULL ); - }else - item->original = elt; + }else { + item->original =xstrdup (elt); + } item->len =strlen (item->match); item->prefix = prefixlen;
@@ -388,8 +389,8 @@ void parse_pathspec(struct pathspec *pathspec,die ("BUG: PATHSPEC_PREFER_CWD requires arguments" ); pathspec->items = item =xcalloc (1 ,sizeof (*item)); - item->match = prefix; - item->original = prefix; + item->match =xstrdup (prefix); + item->original =xstrdup (prefix); item->nowildcard_len = item->len =strlen (prefix); item->prefix = item->len; pathspec->nr =1 ;
@@ -453,13 +454,26 @@ void parse_pathspec(struct pathspec *pathspec,void copy_pathspec (struct pathspec *dst,const struct pathspec *src) { +int i; + *dst = *src;ALLOC_ARRAY (dst->items, dst->nr);COPY_ARRAY (dst->items, src->items, dst->nr); + +for (i =0 ; i < dst->nr; i++) { + dst->items[i].match =xstrdup (src->items[i].match); + dst->items[i].original =xstrdup (src->items[i].original); + } }void clear_pathspec (struct pathspec *pathspec) { +int i; + +for (i =0 ; i < pathspec->nr; i++) { +free (pathspec->items[i].match); +free (pathspec->items[i].original); + }free (pathspec->items); pathspec->items =NULL ; }
pathspec.h +2 -2
@@ -25,8 +25,8 @@ struct pathspec {unsigned magic;int max_depth;struct pathspec_item { -const char *match; -const char *original; +char *match; +char *original;unsigned magic;int len, prefix;int nowildcard_len;
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH v3 1/16 mv: remove use of deprecated 'get_pathspec()'
In reply to Brandon WilliamsConvert the 'internal_copy_pathspec()' function to 'prefix_path()' instead of using the deprecated 'get_pathspec()' interface. Also, rename 'internal_copy_pathspec()' to 'internal_prefix_pathspec()' to be more descriptive of what the funciton is actually doing.
In addition to this, fix a memory leak caused by only duplicating some of the pathspec elements. Instead always duplicate all of the the pathspec elements as an intermediate step (with modificationed based on the passed in flags). This way the intermediate strings can then be freed after getting the result from 'prefix_path()'.
--- builtin/mv.c | 50 +++++++++++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 19 deletions(-)
builtin/mv.c +31 -19
@@ -4,6 +4,7 @@* Copyright (C) 2006 Johannes Schindelin */ #include "builtin.h" +#include "pathspec.h" #include "lockfile.h" #include "dir.h" #include "cache-tree.h"
@@ -19,31 +20,42 @@ static const char * const builtin_mv_usage[] = {#define DUP_BASENAME 1#define KEEP_TRAILING_SLASH 2 -static const char **internal_copy_pathspec (const char *prefix, -const char **pathspec, -int count,unsigned flags) +static const char **internal_prefix_pathspec (const char *prefix, +const char **pathspec, +int count,unsigned flags) {int i;const char **result; +int prefixlen = prefix ?strlen (prefix) :0 ;ALLOC_ARRAY (result, count +1 ); -COPY_ARRAY (result, pathspec, count); - result[count] =NULL ; + +/* Create an intermediate copy of the pathspec based on the flags */ for (i =0 ; i < count; i++) { -int length =strlen (result[i]); +int length =strlen (pathspec[i]);int to_copy = length; +char *it;while (!(flags &KEEP_TRAILING_SLASH ) && - to_copy >0 &&is_dir_sep (result[i][to_copy -1 ])) + to_copy >0 &&is_dir_sep (pathspec[i][to_copy -1 ])) to_copy--; -if (to_copy != length || flags &DUP_BASENAME ) { -char *it =xmemdupz (result[i], to_copy); -if (flags &DUP_BASENAME ) { - result[i] =xstrdup (basename (it)); -free (it); - }else - result[i] = it; + + it =xmemdupz (pathspec[i], to_copy); +if (flags &DUP_BASENAME ) { + result[i] =xstrdup (basename (it)); +free (it); + }else { + result[i] = it; } } -return get_pathspec (prefix, result); + result[count] =NULL ; + +/* Prefix the pathspec and free the old intermediate strings */ +for (i =0 ; i < count; i++) { +const char *match =prefix_path (prefix, prefixlen, result[i]); +free ((char *) result[i]); + result[i] = match; + } + +return result; }static const char *add_slash (const char *path)
@@ -130,7 +142,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)if (read_cache () <0 )die (_ ("index file corrupt" )); - source =internal_copy_pathspec (prefix, argv, argc,0 ); + source =internal_prefix_pathspec (prefix, argv, argc,0 ); modes =xcalloc (argc,sizeof (enum update_mode ));/* * Keep trailing slash, needed to let
@@ -140,16 +152,16 @@ int cmd_mv(int argc, const char **argv, const char *prefix) flags =KEEP_TRAILING_SLASH ;if (argc ==1 &&is_directory (argv[0 ]) && !is_directory (argv[1 ])) flags =0 ; - dest_path =internal_copy_pathspec (prefix, argv + argc,1 , flags); + dest_path =internal_prefix_pathspec (prefix, argv + argc,1 , flags); submodule_gitfile =xcalloc (argc,sizeof (char *));if (dest_path[0 ][0 ] =='\0' )/* special case: "." was normalized to "" */ - destination =internal_copy_pathspec (dest_path[0 ], argv, argc,DUP_BASENAME ); + destination =internal_prefix_pathspec (dest_path[0 ], argv, argc,DUP_BASENAME );else if (!lstat (dest_path[0 ], &st) &&S_ISDIR (st.st_mode)) { dest_path[0 ] =add_slash (dest_path[0 ]); - destination =internal_copy_pathspec (dest_path[0 ], argv, argc,DUP_BASENAME ); + destination =internal_prefix_pathspec (dest_path[0 ], argv, argc,DUP_BASENAME ); }else {if (argc !=1 )die (_ ("destination '%s' is not a directory" ), dest_path[0 ]);
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH v3 9/16 pathspec: simpler logic to prefix original pathspec elements
In reply to Brandon WilliamsThe logic used to prefix an original pathspec element with 'prefix' magic is more general purpose and can be used for more than just short magic. Remove the extra code paths and rename 'prefix_short_magic' to 'prefix_magic' to better indicate that it can be used in more general situations.
Also, slightly change the logic which decides when to prefix the original element in order to prevent a pathspec of "." from getting converted to "" (empty string).
--- pathspec.c | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-)
pathspec.c +13 -20
@@ -74,13 +74,12 @@ static struct pathspec_magic { { PATHSPEC_EXCLUDE, '!', "exclude" }, }; -static void prefix_short_magic (struct strbuf *sb,int prefixlen, -unsigned short_magic) +static void prefix_magic (struct strbuf *sb,int prefixlen,unsigned magic) {int i;strbuf_addstr (sb,":(" );for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) -if (short_magic & pathspec_magic[i].bit) { +if (magic & pathspec_magic[i].bit) {if (sb->buf[sb->len -1 ] !='(' )strbuf_addch (sb,',' );strbuf_addstr (sb, pathspec_magic[i].name);
@@ -109,8 +108,8 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,static int glob_global =-1 ;static int noglob_global =-1 ;static int icase_global =-1 ; -unsigned magic =0 , short_magic =0 , global_magic =0 ; -const char *copyfrom = elt, *long_magic_end =NULL ; +unsigned magic =0 , element_magic =0 , global_magic =0 ; +const char *copyfrom = elt;char *match;int i, pathspec_prefix =-1 ;
@@ -164,7 +163,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) {if (strlen (pathspec_magic[i].name) == len && !strncmp (pathspec_magic[i].name, copyfrom, len)) { - magic |= pathspec_magic[i].bit; + element_magic |= pathspec_magic[i].bit;break ; }if (starts_with (copyfrom,"prefix:" )) {
@@ -183,7 +182,6 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags, }if (*copyfrom !=')' )die (_ ("Missing ')' at the end of pathspec magic in '%s'" ), elt); - long_magic_end = copyfrom; copyfrom++; }else {/* shorthand */
@@ -196,7 +194,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,break ;for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++)if (pathspec_magic[i].mnemonic == ch) { - short_magic |= pathspec_magic[i].bit; + element_magic |= pathspec_magic[i].bit;break ; }if (ARRAY_SIZE (pathspec_magic) <= i)
@@ -207,7 +205,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags, copyfrom++; } - magic |= short_magic; + magic |= element_magic;/* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */ if (noglob_global && !(magic &PATHSPEC_GLOB ))
@@ -242,18 +240,13 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,* Prefix the pathspec (keep all magic) and assign to * original. Useful for passing to another command. */ -if (flags &PATHSPEC_PREFIX_ORIGIN ) { +if ((flags &PATHSPEC_PREFIX_ORIGIN ) && + prefixlen && !literal_global) {struct strbuf sb =STRBUF_INIT ; -if (prefixlen && !literal_global) { -/* Preserve the actual prefix length of each pattern */ -if (short_magic) -prefix_short_magic (&sb, prefixlen, short_magic); -else if (long_magic_end) { -strbuf_add (&sb, elt, long_magic_end - elt); -strbuf_addf (&sb,",prefix:%d)" , prefixlen); - }else -strbuf_addf (&sb,":(prefix:%d)" , prefixlen); - } + +/* Preserve the actual prefix length of each pattern */ +prefix_magic (&sb, prefixlen, element_magic); +strbuf_addstr (&sb, match); item->original =strbuf_detach (&sb,NULL ); }else {
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH v3 5/16 pathspec: remove the deprecated get_pathspec function
In reply to Brandon WilliamsNow that all callers of the old 'get_pathspec' interface have been migrated to use the new pathspec struct interface it can be removed from the codebase.
Since there are no more users of the '_raw' field in the pathspec struct it can also be removed. This patch also removes the old functionality of modifying the const char **argv array that was passed into parse_pathspec. Instead the constructed 'match' string (which is a pathspec element with the prefix prepended) is only stored in its corresponding pathspec_item entry.
--- Documentation/technical/api-setup.txt | 2 -- cache.h | 1 - pathspec.c | 42 +++-------------------------------- pathspec.h | 1 - 4 files changed, 3 insertions(+), 43 deletions(-)
Documentation/technical/api-setup.txt +0 -2
@@ -27,8 +27,6 @@ parse_pathspec(). This function takes several arguments: - prefix and args come from cmd_* functions -get_pathspec() is obsolete and should never be used in new code. - parse_pathspec() helps catch unsupported features and reject them politely. At a lower level, different pathspec-related functions may not support the same set of features. Such pathspec-sensitive
cache.h +0 -1
@@ -514,7 +514,6 @@ extern void set_git_work_tree(const char *tree);#define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES" -extern const char **get_pathspec (const char *prefix,const char **pathspec);extern void setup_work_tree (void );extern const char *setup_git_directory_gently (int *);extern const char *setup_git_directory (void );
pathspec.c +3 -39
85 lines not on this page · raw message
pathspec.h +0 -1
@@ -19,7 +19,6 @@#define PATHSPEC_ONESTAR 1/* the pathspec pattern satisfies GFNM_ONESTAR */ struct pathspec { -const char **_raw;/* get_pathspec() result, not freed by clear_pathspec() */ int nr;unsigned int has_wildcard:1 ;unsigned int recursive:1 ;
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH v3 13/16 pathspec: create parse_element_magic helper
In reply to Brandon WilliamsFactor out the logic responsible for the magic in a pathspec element into its own function.
Also avoid calling into the parsing functions when PATHSPEC_LITERAL_PATH is specified since it causes magic to be ignored and all paths to be treated as literals.
--- pathspec.c | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-)
pathspec.c +20 -17
@@ -245,6 +245,19 @@ static const char *parse_short_magic(unsigned *magic, const char *elem)return pos; } +static const char *parse_element_magic (unsigned *magic,int *prefix_len, +const char *elem) +{ +if (elem[0 ] !=':' ||get_literal_global ()) +return elem;/* nothing to do */ +else if (elem[1 ] =='(' ) +/* longhand */ +return parse_long_magic (magic, prefix_len, elem); +else +/* shorthand */ +return parse_short_magic (magic, elem); +} +/* * Take an element of a pathspec and check for magic signatures. * Append the result to the prefix. Return the magic bitmap.
@@ -267,26 +280,16 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,char *match;int i, pathspec_prefix =-1 ; -if (elt[0 ] !=':' ||get_literal_global () || - (flags &PATHSPEC_LITERAL_PATH )) { - ;/* nothing to do */ - }else if (elt[1 ] =='(' ) { -/* longhand */ - copyfrom =parse_long_magic (&element_magic, - &pathspec_prefix, - elt); - }else { -/* shorthand */ - copyfrom =parse_short_magic (&element_magic, elt); - } - - magic |= element_magic; -/* PATHSPEC_LITERAL_PATH ignores magic */ -if (flags &PATHSPEC_LITERAL_PATH ) +if (flags &PATHSPEC_LITERAL_PATH ) { magic =PATHSPEC_LITERAL ; -else + }else { + copyfrom =parse_element_magic (&element_magic, + &pathspec_prefix, + elt); + magic |= element_magic; magic |=get_global_magic (element_magic); + }if (pathspec_prefix >=0 && (prefixlen || (prefix && *prefix)))
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH v3 15/16 pathspec: small readability changes
In reply to Brandon WilliamsA few small changes to improve readability. This is done by grouping related assignments, adding blank lines, ensuring lines are <80 characters, and adding additional comments.
--- pathspec.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-)
pathspec.c +15 -10
@@ -67,11 +67,11 @@ static struct pathspec_magic {char mnemonic;/* this cannot be ':'! */ const char *name; } pathspec_magic[] = { - { PATHSPEC_FROMTOP, '/', "top" }, - { PATHSPEC_LITERAL, 0, "literal" }, - { PATHSPEC_GLOB, '\0', "glob" }, - { PATHSPEC_ICASE, '\0', "icase" }, - { PATHSPEC_EXCLUDE, '!', "exclude" }, + { PATHSPEC_FROMTOP, '/', "top" }, + { PATHSPEC_LITERAL, '\0', "literal" }, + { PATHSPEC_GLOB, '\0', "glob" }, + { PATHSPEC_ICASE, '\0', "icase" }, + { PATHSPEC_EXCLUDE, '!', "exclude" }, }; static void prefix_magic(struct strbuf *sb, int prefixlen, unsigned magic)
@@ -336,6 +336,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,if ((magic &PATHSPEC_LITERAL ) && (magic &PATHSPEC_GLOB ))die (_ ("%s: 'literal' and 'glob' are incompatible" ), elt); +/* Create match string which will be used for pathspec matching */ if (pathspec_prefix >=0 ) { match =xstrdup (copyfrom); prefixlen = pathspec_prefix;
@@ -343,11 +344,16 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags, match =xstrdup (copyfrom); prefixlen =0 ; }else { - match =prefix_path_gently (prefix, prefixlen, &prefixlen, copyfrom); + match =prefix_path_gently (prefix, prefixlen, + &prefixlen, copyfrom);if (!match)die (_ ("%s: '%s' is outside repository" ), elt, copyfrom); } + item->match = match; + item->len =strlen (item->match); + item->prefix = prefixlen; +/* * Prefix the pathspec (keep all magic) and assign to * original. Useful for passing to another command.
@@ -364,8 +370,6 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags, }else { item->original =xstrdup (elt); } - item->len =strlen (item->match); - item->prefix = prefixlen;if (flags &PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP )strip_submodule_slash_cheap (item);
@@ -373,13 +377,14 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,if (flags &PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE )strip_submodule_slash_expensive (item); -if (magic &PATHSPEC_LITERAL ) +if (magic &PATHSPEC_LITERAL ) { item->nowildcard_len = item->len; -else { + }else { item->nowildcard_len =simple_length (item->match);if (item->nowildcard_len < prefixlen) item->nowildcard_len = prefixlen; } + item->flags =0 ;if (magic &PATHSPEC_GLOB ) {/*
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH v3 14/16 pathspec: create strip submodule slash helpers
In reply to Brandon WilliamsFactor out the logic responsible for stripping the trailing slash on pathspecs referencing submodules into its own function.
--- pathspec.c | 68 ++++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 42 insertions(+), 26 deletions(-)
pathspec.c +42 -26
@@ -258,6 +258,44 @@ static const char *parse_element_magic(unsigned *magic, int *prefix_len,return parse_short_magic (magic, elem); } +static void strip_submodule_slash_cheap (struct pathspec_item *item) +{ +if (item->len >=1 && item->match[item->len -1 ] =='/' ) { +int i =cache_name_pos (item->match, item->len -1 ); + +if (i >=0 &&S_ISGITLINK (active_cache[i]->ce_mode)) { + item->len--; + item->match[item->len] ='\0' ; + } + } +} + +static void strip_submodule_slash_expensive (struct pathspec_item *item) +{ +int i; + +for (i =0 ; i < active_nr; i++) { +struct cache_entry *ce = active_cache[i]; +int ce_len =ce_namelen (ce); + +if (!S_ISGITLINK (ce->ce_mode)) +continue ; + +if (item->len <= ce_len || item->match[ce_len] !='/' || +memcmp (ce->name, item->match, ce_len)) +continue ; + +if (item->len == ce_len +1 ) { +/* strip trailing slash */ + item->len--; + item->match[item->len] ='\0' ; + }else { +die (_ ("Pathspec '%s' is in submodule '%.*s'" ), + item->original, ce_len, ce->name); + } + } +} +/* * Take an element of a pathspec and check for magic signatures. * Append the result to the prefix. Return the magic bitmap.
@@ -278,7 +316,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,unsigned magic =0 , element_magic =0 ;const char *copyfrom = elt;char *match; -int i, pathspec_prefix =-1 ; +int pathspec_prefix =-1 ;/* PATHSPEC_LITERAL_PATH ignores magic */ if (flags &PATHSPEC_LITERAL_PATH ) {
@@ -329,33 +367,11 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags, item->len =strlen (item->match); item->prefix = prefixlen; -if ((flags &PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP ) && - (item->len >=1 && item->match[item->len -1 ] =='/' ) && - (i =cache_name_pos (item->match, item->len -1 )) >=0 && -S_ISGITLINK (active_cache[i]->ce_mode)) { - item->len--; - match[item->len] ='\0' ; - } +if (flags &PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP ) +strip_submodule_slash_cheap (item);if (flags &PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE ) -for (i =0 ; i < active_nr; i++) { -struct cache_entry *ce = active_cache[i]; -int ce_len =ce_namelen (ce); - -if (!S_ISGITLINK (ce->ce_mode)) -continue ; - -if (item->len <= ce_len || match[ce_len] !='/' || -memcmp (ce->name, match, ce_len)) -continue ; -if (item->len == ce_len +1 ) { -/* strip trailing slash */ - item->len--; - match[item->len] ='\0' ; - }else -die (_ ("Pathspec '%s' is in submodule '%.*s'" ), - elt, ce_len, ce->name); - } +strip_submodule_slash_expensive (item);if (magic &PATHSPEC_LITERAL ) item->nowildcard_len = item->len;
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH v3 12/16 pathspec: create parse_long_magic function
In reply to Brandon WilliamsFactor out the logic responsible for parsing long magic into its own function. As well as hoist the prefix check logic outside of the inner loop as there isn't anything that needs to be done after matching "prefix:".
--- pathspec.c | 92 ++++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 57 insertions(+), 35 deletions(-)
pathspec.c +57 -35
@@ -157,6 +157,60 @@ static int get_global_magic(int element_magic) }/* +* Parse the pathspec element looking for long magic +* +* saves all magic in 'magic' +* if prefix magic is used, save the prefix length in 'prefix_len' +* returns the position in 'elem' after all magic has been parsed +*/ +static const char *parse_long_magic (unsigned *magic,int *prefix_len, +const char *elem) +{ +const char *pos; +const char *nextat; + +for (pos = elem +2 ; *pos && *pos !=')' ; pos = nextat) { +size_t len =strcspn (pos,",)" ); +int i; + +if (pos[len] ==',' ) + nextat = pos + len +1 ;/* handle ',' */ +else + nextat = pos + len;/* handle ')' and '\0' */ + +if (!len) +continue ; + +if (starts_with (pos,"prefix:" )) { +char *endptr; + *prefix_len =strtol (pos +7 , &endptr,10 ); +if (endptr - pos != len) +die (_ ("invalid parameter for pathspec magic 'prefix'" )); +continue ; + } + +for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) { +if (strlen (pathspec_magic[i].name) == len && + !strncmp (pathspec_magic[i].name, pos, len)) { + *magic |= pathspec_magic[i].bit; +break ; + } + } + +if (ARRAY_SIZE (pathspec_magic) <= i) +die (_ ("Invalid pathspec magic '%.*s' in '%s'" ), + (int ) len, pos, elem); + } + +if (*pos !=')' ) +die (_ ("Missing ')' at the end of pathspec magic in '%s'" ), + elem); + pos++; + +return pos; +} + +/* * Parse the pathspec element looking for short magic * * saves all magic in 'magic'
@@ -218,41 +272,9 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags, ;/* nothing to do */ }else if (elt[1 ] =='(' ) {/* longhand */ -const char *nextat; -for (copyfrom = elt +2 ; - *copyfrom && *copyfrom !=')' ; - copyfrom = nextat) { -size_t len =strcspn (copyfrom,",)" ); -if (copyfrom[len] ==',' ) - nextat = copyfrom + len +1 ; -else -/* handle ')' and '\0' */ - nextat = copyfrom + len; -if (!len) -continue ; -for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) { -if (strlen (pathspec_magic[i].name) == len && - !strncmp (pathspec_magic[i].name, copyfrom, len)) { - element_magic |= pathspec_magic[i].bit; -break ; - } -if (starts_with (copyfrom,"prefix:" )) { -char *endptr; - pathspec_prefix =strtol (copyfrom +7 , - &endptr,10 ); -if (endptr - copyfrom != len) -die (_ ("invalid parameter for pathspec magic 'prefix'" )); -/* "i" would be wrong, but it does not matter */ -break ; - } - } -if (ARRAY_SIZE (pathspec_magic) <= i) -die (_ ("Invalid pathspec magic '%.*s' in '%s'" ), - (int ) len, copyfrom, elt); - } -if (*copyfrom !=')' ) -die (_ ("Missing ')' at the end of pathspec magic in '%s'" ), elt); - copyfrom++; + copyfrom =parse_long_magic (&element_magic, + &pathspec_prefix, + elt); }else {/* shorthand */ copyfrom =parse_short_magic (&element_magic, elt);
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH v3 16/16 pathspec: rename prefix_pathspec to init_pathspec_item
In reply to Brandon WilliamsGive a more relevant name to the prefix_pathspec function as it does more than just prefix a pathspec element.
--- pathspec.c | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-)
pathspec.c +7 -17
@@ -297,21 +297,11 @@ static void strip_submodule_slash_expensive(struct pathspec_item *item) }/* -* Take an element of a pathspec and check for magic signatures. -* Append the result to the prefix. Return the magic bitmap. -* -* For now, we only parse the syntax and throw out anything other than -* "top" magic. -* -* NEEDSWORK: This needs to be rewritten when we start migrating -* get_pathspec() users to use the "struct pathspec" interface. For -* example, a pathspec element may be marked as case-insensitive, but -* the prefix part must always match literally, and a single stupid -* string cannot express such a case. +* Perform the initialization of a pathspec_item based on a pathspec element. */ -static unsigned prefix_pathspec (struct pathspec_item *item,unsigned flags, -const char *prefix,int prefixlen, -const char *elt) +static void init_pathspec_item (struct pathspec_item *item,unsigned flags, +const char *prefix,int prefixlen, +const char *elt) {unsigned magic =0 , element_magic =0 ;const char *copyfrom = elt;
@@ -329,6 +319,8 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags, magic |=get_global_magic (element_magic); } + item->magic = magic; +if (pathspec_prefix >=0 && (prefixlen || (prefix && *prefix)))die ("BUG: 'prefix' magic is supposed to be used at worktree's root" );
@@ -401,7 +393,6 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,/* sanity checks, pathspec matchers assume these are sane */ assert (item->nowildcard_len <= item->len && item->prefix <= item->len); -return magic; }static int pathspec_item_cmp (const void *a_,const void *b_)
@@ -500,8 +491,7 @@ void parse_pathspec(struct pathspec *pathspec,for (i =0 ; i < n; i++) { entry = argv[i]; - item[i].magic =prefix_pathspec (item + i, flags, - prefix, prefixlen, entry); +init_pathspec_item (item + i, flags, prefix, prefixlen, entry);if (item[i].magic &PATHSPEC_EXCLUDE ) nr_exclude++;
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH v3 11/16 pathspec: create parse_short_magic function
In reply to Brandon WilliamsFactor out the logic responsible for parsing short magic into its own function.
--- pathspec.c | 54 ++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 18 deletions(-)
pathspec.c +36 -18
@@ -157,6 +157,41 @@ static int get_global_magic(int element_magic) }/* +* Parse the pathspec element looking for short magic +* +* saves all magic in 'magic' +* returns the position in 'elem' after all magic has been parsed +*/ +static const char *parse_short_magic (unsigned *magic,const char *elem) +{ +const char *pos; + +for (pos = elem +1 ; *pos && *pos !=':' ; pos++) { +char ch = *pos; +int i; + +if (!is_pathspec_magic (ch)) +break ; + +for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) { +if (pathspec_magic[i].mnemonic == ch) { + *magic |= pathspec_magic[i].bit; +break ; + } + } + +if (ARRAY_SIZE (pathspec_magic) <= i) +die (_ ("Unimplemented pathspec magic '%c' in '%s'" ), + ch, elem); + } + +if (*pos ==':' ) + pos++; + +return pos; +} + +/* * Take an element of a pathspec and check for magic signatures. * Append the result to the prefix. Return the magic bitmap. *
@@ -220,24 +255,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags, copyfrom++; }else {/* shorthand */ -for (copyfrom = elt +1 ; - *copyfrom && *copyfrom !=':' ; - copyfrom++) { -char ch = *copyfrom; - -if (!is_pathspec_magic (ch)) -break ; -for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) -if (pathspec_magic[i].mnemonic == ch) { - element_magic |= pathspec_magic[i].bit; -break ; - } -if (ARRAY_SIZE (pathspec_magic) <= i) -die (_ ("Unimplemented pathspec magic '%c' in '%s'" ), - ch, elt); - } -if (*copyfrom ==':' ) - copyfrom++; + copyfrom =parse_short_magic (&element_magic, elt); } magic |= element_magic;
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH v3 10/16 pathspec: factor global magic into its own function
In reply to Brandon WilliamsCreate helper functions to read the global magic environment variables in additon to factoring out the global magic gathering logic into its own function.
--- pathspec.c | 127 +++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 78 insertions(+), 49 deletions(-)
pathspec.c +78 -49
@@ -87,6 +87,75 @@ static void prefix_magic(struct strbuf *sb, int prefixlen, unsigned magic)strbuf_addf (sb,",prefix:%d)" , prefixlen); } +static inline int get_literal_global (void ) +{ +static int literal =-1 ; + +if (literal <0 ) + literal =git_env_bool (GIT_LITERAL_PATHSPECS_ENVIRONMENT ,0 ); + +return literal; +} + +static inline int get_glob_global (void ) +{ +static int glob =-1 ; + +if (glob <0 ) + glob =git_env_bool (GIT_GLOB_PATHSPECS_ENVIRONMENT ,0 ); + +return glob; +} + +static inline int get_noglob_global (void ) +{ +static int noglob =-1 ; + +if (noglob <0 ) + noglob =git_env_bool (GIT_NOGLOB_PATHSPECS_ENVIRONMENT ,0 ); + +return noglob; +} + +static inline int get_icase_global (void ) +{ +static int icase =-1 ; + +if (icase <0 ) + icase =git_env_bool (GIT_ICASE_PATHSPECS_ENVIRONMENT ,0 ); + +return icase; +} + +static int get_global_magic (int element_magic) +{ +int global_magic =0 ; + +if (get_literal_global ()) + global_magic |=PATHSPEC_LITERAL ; + +/* --glob-pathspec is overridden by :(literal) */ +if (get_glob_global () && !(element_magic &PATHSPEC_LITERAL )) + global_magic |=PATHSPEC_GLOB ; + +if (get_glob_global () &&get_noglob_global ()) +die (_ ("global 'glob' and 'noglob' pathspec settings are incompatible" )); + +if (get_icase_global ()) + global_magic |=PATHSPEC_ICASE ; + +if ((global_magic &PATHSPEC_LITERAL ) && + (global_magic & ~PATHSPEC_LITERAL )) +die (_ ("global 'literal' pathspec setting is incompatible " +"with all other global pathspec settings" )); + +/* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */ +if (get_noglob_global () && !(element_magic &PATHSPEC_GLOB )) + global_magic |=PATHSPEC_LITERAL ; + +return global_magic; +} +/* * Take an element of a pathspec and check for magic signatures. * Append the result to the prefix. Return the magic bitmap.
@@ -104,46 +173,12 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags, const char *prefix, int prefixlen, const char *elt) { - static int literal_global = -1; -static int glob_global =-1 ; -static int noglob_global =-1 ; -static int icase_global =-1 ; -unsigned magic =0 , element_magic =0 , global_magic =0 ; + unsigned magic = 0, element_magic = 0;const char *copyfrom = elt;char *match;int i, pathspec_prefix =-1 ; -if (literal_global <0 ) - literal_global =git_env_bool (GIT_LITERAL_PATHSPECS_ENVIRONMENT ,0 ); -if (literal_global) - global_magic |=PATHSPEC_LITERAL ; - -if (glob_global <0 ) - glob_global =git_env_bool (GIT_GLOB_PATHSPECS_ENVIRONMENT ,0 ); -if (glob_global) - global_magic |=PATHSPEC_GLOB ; - -if (noglob_global <0 ) - noglob_global =git_env_bool (GIT_NOGLOB_PATHSPECS_ENVIRONMENT ,0 ); - -if (glob_global && noglob_global) -die (_ ("global 'glob' and 'noglob' pathspec settings are incompatible" )); - - -if (icase_global <0 ) - icase_global =git_env_bool (GIT_ICASE_PATHSPECS_ENVIRONMENT ,0 ); -if (icase_global) - global_magic |=PATHSPEC_ICASE ; - -if ((global_magic &PATHSPEC_LITERAL ) && - (global_magic & ~PATHSPEC_LITERAL )) -die (_ ("global 'literal' pathspec setting is incompatible " -"with all other global pathspec settings" )); - -if (flags &PATHSPEC_LITERAL_PATH ) - global_magic =0 ; - -if (elt[0 ] !=':' || literal_global || +if (elt[0 ] !=':' ||get_literal_global () || (flags &PATHSPEC_LITERAL_PATH )) { ;/* nothing to do */ }else if (elt[1 ] =='(' ) {
@@ -207,15 +242,11 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags, magic |= element_magic; -/* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */ -if (noglob_global && !(magic &PATHSPEC_GLOB )) - global_magic |=PATHSPEC_LITERAL ; - -/* --glob-pathspec is overridden by :(literal) */ -if ((global_magic &PATHSPEC_GLOB ) && (magic &PATHSPEC_LITERAL )) - global_magic &= ~PATHSPEC_GLOB ; - - magic |= global_magic; +/* PATHSPEC_LITERAL_PATH ignores magic */ +if (flags &PATHSPEC_LITERAL_PATH ) + magic =PATHSPEC_LITERAL ; +else + magic |=get_global_magic (element_magic);if (pathspec_prefix >=0 && (prefixlen || (prefix && *prefix)))
@@ -241,7 +272,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,* original. Useful for passing to another command. */ if ((flags &PATHSPEC_PREFIX_ORIGIN ) && - prefixlen && !literal_global) { + prefixlen && !get_literal_global ()) {struct strbuf sb =STRBUF_INIT ;/* Preserve the actual prefix length of each pattern */
@@ -407,9 +438,7 @@ void parse_pathspec(struct pathspec *pathspec, item[i].magic =prefix_pathspec (item + i, flags, prefix, prefixlen, entry); -if ((flags &PATHSPEC_LITERAL_PATH ) && - !(magic_mask &PATHSPEC_LITERAL )) - item[i].magic |=PATHSPEC_LITERAL ; +if (item[i].magic &PATHSPEC_EXCLUDE ) nr_exclude++;if (item[i].magic & magic_mask)
Signature · 1 line
2.8.0.rc3.226.g39d4020
Re: [PATCH v3 02/16] dir: remove struct path_simplify
In reply to Brandon WilliamsQuoted 7 lines · Brandon Williams
Teach
simplify_away()andexclude_matches_pathspec()to handle struct pathspec directly, eliminating the need for the structpath_simplify.Also renamed the len parameter to pathlen in
exclude_matches_pathspec()to match the parameter names used insimplify_away().Signed-off-by Brandon Williams <[email protected]>
Looks good to me,
Thanks,
Stefan
Re: [PATCH v3 02/16] dir: remove struct path_simplify
In reply to Brandon WilliamsQuoted 7 lines · Brandon Williams
dir.c +2
-1@@ -2010,14 +1987,11 @@ static struct untracked_cache_dir *validate_untracked_cache(struct dir_struct *dreturn root; } -int read_directory (struct dir_struct *dir,const char *path,int len,const struct pathspec *pathspec) +int read_directory (struct dir_struct *dir,const char *path, +int len,const struct pathspec *pathspec)
dir.c static struct untracked_cache_dir *validate_untracked_cache(struct dir_struct *d · line 1997
Quoted 10 lines
dir.c +0
-4{ -struct path_simplify *simplify;struct untracked_cache_dir *untracked; -/* -* Check out create_simplify() -*/ if (pathspec) GUARD_PATHSPEC(pathspec, PATHSPEC_FROMTOP |
This GUARD_PATHSPEC macro should be moved into simplify_away() and exclude_pathspec_matches(), so that next time somebody adds a new pathspec magic, they can basically grep GUARD_PATHSPEC and determine if these code can support their favorite magic or not.
Signature · 1 line
Duy
Re: [PATCH v3 06/16] pathspec: copy and free owned memory
In reply to Brandon Williamspathspec.c void parse_pathspec(struct pathspec *pathspec, · line 478
Quoted 10 lines · Brandon Williams
void
clear_pathspec(struct pathspec *pathspec)
{pathspec.c +6
-0+int i; + +for (i =0 ; i < pathspec->nr; i++) { +free (pathspec->items[i].match); +free (pathspec->items[i].original); + }free (pathspec->items); pathspec->items =NULL ;
We should set pathspec->nr to zero so that calling this function again won't cause any harm.
Quoted 1 line
}
Signature · 1 line
Duy
Re: [PATCH v3 08/16] pathspec: always show mnemonic and name in unsupported_magic
In reply to Brandon Williamspathspec.c static void NORETURN unsupported_magic(const char *pattern, · line 344
Quoted 12 lines · Brandon Williams
pathspec.c +3
-2@@ -340,8 +336,9 @@ static void NORETURN unsupported_magic(const char *pattern,continue ;if (sb.len)strbuf_addch (&sb,' ' ); -if (short_magic & m->bit) -strbuf_addf (&sb,"'%c'" , m->mnemonic); + +if (m->mnemonic) +strbuf_addf (&sb,"'(%c)%s'" , m->mnemonic, m->name);else strbuf_addf (&sb,"'%s'" , m->name); }
The die() call is out of diff context, but it'll print
pathspec magic not supported by this command: (!)top
which looks too much like :(<name>)<mnemonic> pathspec syntax too me and threw me off a bit. And it's a bit cryptic, isn't it? Since this is meant for human, maybe we can just write
pathspec magic not supported by this command: top (mnemonic: '!')
Signature · 1 line
Duy
Re: [PATCH v3 08/16] pathspec: always show mnemonic and name in unsupported_magic
Quoted 16 lines · Duy Nguyen
pathspec.c static void NORETURN unsupported_magic(const char *pattern, · line 344
Quoted 12 lines · Brandon Williams
pathspec.c +3
-2@@ -340,8 +336,9 @@ static void NORETURN unsupported_magic(const char *pattern,continue ;if (sb.len)strbuf_addch (&sb,' ' ); -if (short_magic & m->bit) -strbuf_addf (&sb,"'%c'" , m->mnemonic); + +if (m->mnemonic) +strbuf_addf (&sb,"'(%c)%s'" , m->mnemonic, m->name);else strbuf_addf (&sb,"'%s'" , m->name); }The
die()call is out of diff context, but it'll print
Quoted 7 lines
pathspec magic not supported by this command: (!)top
which looks too much like :(<name>)<mnemonic> pathspec syntax too me and threw me off a bit. And it's a bit cryptic, isn't it? Since this is meant for human, maybe we can just write
pathspec magic not supported by this command: top (mnemonic: '!')
I was trying to keep it short and sweet, turns out that ends up being more difficult to understand. I like your suggestion, it definitely makes things much clearer.
Signature · 1 line
Brandon Williams
PATCH v4 3/16 dir: convert fill_directory to use the pathspec struct interface
In reply to Brandon WilliamsConvert 'fill_directory()' to use the pathspec struct interface from using the '_raw' entry in the pathspec struct.
--- dir.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
dir.c +8 -4
@@ -174,17 +174,21 @@ char *common_prefix(const struct pathspec *pathspec)int fill_directory (struct dir_struct *dir,const struct pathspec *pathspec) { -size_t len; +char *prefix; +size_t prefix_len;/* * Calculate common prefix for the pathspec, and * use that to optimize the directory walk */ - len =common_prefix_len (pathspec); + prefix =common_prefix (pathspec); + prefix_len = prefix ?strlen (prefix) :0 ;/* Read the directory and prune it */ -read_directory (dir, pathspec->nr ? pathspec->_raw[0 ] :"" , len, pathspec); -return len; +read_directory (dir, prefix, prefix_len, pathspec); + +free (prefix); +return prefix_len; } int within_depth(const char *name, int namelen,
Signature · 1 line
2.11.0.390.gc69c2f50cf-goog
PATCH v4 10/16 pathspec: factor global magic into its own function
In reply to Brandon WilliamsCreate helper functions to read the global magic environment variables in additon to factoring out the global magic gathering logic into its own function.
--- pathspec.c | 127 +++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 78 insertions(+), 49 deletions(-)
pathspec.c +78 -49
@@ -87,6 +87,75 @@ static void prefix_magic(struct strbuf *sb, int prefixlen, unsigned magic)strbuf_addf (sb,",prefix:%d)" , prefixlen); } +static inline int get_literal_global (void ) +{ +static int literal =-1 ; + +if (literal <0 ) + literal =git_env_bool (GIT_LITERAL_PATHSPECS_ENVIRONMENT ,0 ); + +return literal; +} + +static inline int get_glob_global (void ) +{ +static int glob =-1 ; + +if (glob <0 ) + glob =git_env_bool (GIT_GLOB_PATHSPECS_ENVIRONMENT ,0 ); + +return glob; +} + +static inline int get_noglob_global (void ) +{ +static int noglob =-1 ; + +if (noglob <0 ) + noglob =git_env_bool (GIT_NOGLOB_PATHSPECS_ENVIRONMENT ,0 ); + +return noglob; +} + +static inline int get_icase_global (void ) +{ +static int icase =-1 ; + +if (icase <0 ) + icase =git_env_bool (GIT_ICASE_PATHSPECS_ENVIRONMENT ,0 ); + +return icase; +} + +static int get_global_magic (int element_magic) +{ +int global_magic =0 ; + +if (get_literal_global ()) + global_magic |=PATHSPEC_LITERAL ; + +/* --glob-pathspec is overridden by :(literal) */ +if (get_glob_global () && !(element_magic &PATHSPEC_LITERAL )) + global_magic |=PATHSPEC_GLOB ; + +if (get_glob_global () &&get_noglob_global ()) +die (_ ("global 'glob' and 'noglob' pathspec settings are incompatible" )); + +if (get_icase_global ()) + global_magic |=PATHSPEC_ICASE ; + +if ((global_magic &PATHSPEC_LITERAL ) && + (global_magic & ~PATHSPEC_LITERAL )) +die (_ ("global 'literal' pathspec setting is incompatible " +"with all other global pathspec settings" )); + +/* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */ +if (get_noglob_global () && !(element_magic &PATHSPEC_GLOB )) + global_magic |=PATHSPEC_LITERAL ; + +return global_magic; +} +/* * Take an element of a pathspec and check for magic signatures. * Append the result to the prefix. Return the magic bitmap.
@@ -104,46 +173,12 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags, const char *prefix, int prefixlen, const char *elt) { - static int literal_global = -1; -static int glob_global =-1 ; -static int noglob_global =-1 ; -static int icase_global =-1 ; -unsigned magic =0 , element_magic =0 , global_magic =0 ; + unsigned magic = 0, element_magic = 0;const char *copyfrom = elt;char *match;int i, pathspec_prefix =-1 ; -if (literal_global <0 ) - literal_global =git_env_bool (GIT_LITERAL_PATHSPECS_ENVIRONMENT ,0 ); -if (literal_global) - global_magic |=PATHSPEC_LITERAL ; - -if (glob_global <0 ) - glob_global =git_env_bool (GIT_GLOB_PATHSPECS_ENVIRONMENT ,0 ); -if (glob_global) - global_magic |=PATHSPEC_GLOB ; - -if (noglob_global <0 ) - noglob_global =git_env_bool (GIT_NOGLOB_PATHSPECS_ENVIRONMENT ,0 ); - -if (glob_global && noglob_global) -die (_ ("global 'glob' and 'noglob' pathspec settings are incompatible" )); - - -if (icase_global <0 ) - icase_global =git_env_bool (GIT_ICASE_PATHSPECS_ENVIRONMENT ,0 ); -if (icase_global) - global_magic |=PATHSPEC_ICASE ; - -if ((global_magic &PATHSPEC_LITERAL ) && - (global_magic & ~PATHSPEC_LITERAL )) -die (_ ("global 'literal' pathspec setting is incompatible " -"with all other global pathspec settings" )); - -if (flags &PATHSPEC_LITERAL_PATH ) - global_magic =0 ; - -if (elt[0 ] !=':' || literal_global || +if (elt[0 ] !=':' ||get_literal_global () || (flags &PATHSPEC_LITERAL_PATH )) { ;/* nothing to do */ }else if (elt[1 ] =='(' ) {
@@ -207,15 +242,11 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags, magic |= element_magic; -/* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */ -if (noglob_global && !(magic &PATHSPEC_GLOB )) - global_magic |=PATHSPEC_LITERAL ; - -/* --glob-pathspec is overridden by :(literal) */ -if ((global_magic &PATHSPEC_GLOB ) && (magic &PATHSPEC_LITERAL )) - global_magic &= ~PATHSPEC_GLOB ; - - magic |= global_magic; +/* PATHSPEC_LITERAL_PATH ignores magic */ +if (flags &PATHSPEC_LITERAL_PATH ) + magic =PATHSPEC_LITERAL ; +else + magic |=get_global_magic (element_magic);if (pathspec_prefix >=0 && (prefixlen || (prefix && *prefix)))
@@ -241,7 +272,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,* original. Useful for passing to another command. */ if ((flags &PATHSPEC_PREFIX_ORIGIN ) && - prefixlen && !literal_global) { + prefixlen && !get_literal_global ()) {struct strbuf sb =STRBUF_INIT ;/* Preserve the actual prefix length of each pattern */
@@ -408,9 +439,7 @@ void parse_pathspec(struct pathspec *pathspec, item[i].magic =prefix_pathspec (item + i, flags, prefix, prefixlen, entry); -if ((flags &PATHSPEC_LITERAL_PATH ) && - !(magic_mask &PATHSPEC_LITERAL )) - item[i].magic |=PATHSPEC_LITERAL ; +if (item[i].magic &PATHSPEC_EXCLUDE ) nr_exclude++;if (item[i].magic & magic_mask)
Signature · 1 line
2.11.0.390.gc69c2f50cf-goog
PATCH v4 4/16 ls-tree: convert show_recursive to use the pathspec struct interface
In reply to Brandon WilliamsConvert 'show_recursive()' to use the pathspec struct interface from using the '_raw' entry in the pathspec struct.
--- builtin/ls-tree.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-)
builtin/ls-tree.c +7 -9
@@ -31,21 +31,18 @@ static const char * const ls_tree_usage[] = {static int show_recursive (const char *base,int baselen,const char *pathname) { -const char **s; +int i;if (ls_options &LS_RECURSIVE )return 1 ; - s = pathspec._raw; -if (!s) +if (!pathspec.nr)return 0 ; -for (;;) { -const char *spec = *s++; +for (i =0 ; i < pathspec.nr; i++) { +const char *spec = pathspec.items[i].match;int len, speclen; -if (!spec) -return 0 ;if (strncmp (base, spec, baselen))continue ; len =strlen (pathname);
@@ -59,6 +56,7 @@ static int show_recursive(const char *base, int baselen, const char *pathname)continue ;return 1 ; } +return 0 ; } static int show_tree(const unsigned char *sha1, struct strbuf *base,
@@ -175,8 +173,8 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)* cannot be lifted until it is converted to use * match_pathspec() or tree_entry_interesting() */ -parse_pathspec (&pathspec,PATHSPEC_GLOB |PATHSPEC_ICASE | -PATHSPEC_EXCLUDE , +parse_pathspec (&pathspec,PATHSPEC_ALL_MAGIC & + ~(PATHSPEC_FROMTOP |PATHSPEC_LITERAL ),PATHSPEC_PREFER_CWD , prefix, argv +1 );for (i =0 ; i < pathspec.nr; i++)
Signature · 1 line
2.11.0.390.gc69c2f50cf-goog
PATCH v4 1/16 mv: remove use of deprecated 'get_pathspec()'
In reply to Brandon WilliamsConvert the 'internal_copy_pathspec()' function to 'prefix_path()' instead of using the deprecated 'get_pathspec()' interface. Also, rename 'internal_copy_pathspec()' to 'internal_prefix_pathspec()' to be more descriptive of what the funciton is actually doing.
In addition to this, fix a memory leak caused by only duplicating some of the pathspec elements. Instead always duplicate all of the the pathspec elements as an intermediate step (with modificationed based on the passed in flags). This way the intermediate strings can then be freed after getting the result from 'prefix_path()'.
--- builtin/mv.c | 50 +++++++++++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 19 deletions(-)
builtin/mv.c +31 -19
@@ -4,6 +4,7 @@* Copyright (C) 2006 Johannes Schindelin */ #include "builtin.h" +#include "pathspec.h" #include "lockfile.h" #include "dir.h" #include "cache-tree.h"
@@ -19,31 +20,42 @@ static const char * const builtin_mv_usage[] = {#define DUP_BASENAME 1#define KEEP_TRAILING_SLASH 2 -static const char **internal_copy_pathspec (const char *prefix, -const char **pathspec, -int count,unsigned flags) +static const char **internal_prefix_pathspec (const char *prefix, +const char **pathspec, +int count,unsigned flags) {int i;const char **result; +int prefixlen = prefix ?strlen (prefix) :0 ;ALLOC_ARRAY (result, count +1 ); -COPY_ARRAY (result, pathspec, count); - result[count] =NULL ; + +/* Create an intermediate copy of the pathspec based on the flags */ for (i =0 ; i < count; i++) { -int length =strlen (result[i]); +int length =strlen (pathspec[i]);int to_copy = length; +char *it;while (!(flags &KEEP_TRAILING_SLASH ) && - to_copy >0 &&is_dir_sep (result[i][to_copy -1 ])) + to_copy >0 &&is_dir_sep (pathspec[i][to_copy -1 ])) to_copy--; -if (to_copy != length || flags &DUP_BASENAME ) { -char *it =xmemdupz (result[i], to_copy); -if (flags &DUP_BASENAME ) { - result[i] =xstrdup (basename (it)); -free (it); - }else - result[i] = it; + + it =xmemdupz (pathspec[i], to_copy); +if (flags &DUP_BASENAME ) { + result[i] =xstrdup (basename (it)); +free (it); + }else { + result[i] = it; } } -return get_pathspec (prefix, result); + result[count] =NULL ; + +/* Prefix the pathspec and free the old intermediate strings */ +for (i =0 ; i < count; i++) { +const char *match =prefix_path (prefix, prefixlen, result[i]); +free ((char *) result[i]); + result[i] = match; + } + +return result; }static const char *add_slash (const char *path)
@@ -130,7 +142,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)if (read_cache () <0 )die (_ ("index file corrupt" )); - source =internal_copy_pathspec (prefix, argv, argc,0 ); + source =internal_prefix_pathspec (prefix, argv, argc,0 ); modes =xcalloc (argc,sizeof (enum update_mode ));/* * Keep trailing slash, needed to let
@@ -140,16 +152,16 @@ int cmd_mv(int argc, const char **argv, const char *prefix) flags =KEEP_TRAILING_SLASH ;if (argc ==1 &&is_directory (argv[0 ]) && !is_directory (argv[1 ])) flags =0 ; - dest_path =internal_copy_pathspec (prefix, argv + argc,1 , flags); + dest_path =internal_prefix_pathspec (prefix, argv + argc,1 , flags); submodule_gitfile =xcalloc (argc,sizeof (char *));if (dest_path[0 ][0 ] =='\0' )/* special case: "." was normalized to "" */ - destination =internal_copy_pathspec (dest_path[0 ], argv, argc,DUP_BASENAME ); + destination =internal_prefix_pathspec (dest_path[0 ], argv, argc,DUP_BASENAME );else if (!lstat (dest_path[0 ], &st) &&S_ISDIR (st.st_mode)) { dest_path[0 ] =add_slash (dest_path[0 ]); - destination =internal_copy_pathspec (dest_path[0 ], argv, argc,DUP_BASENAME ); + destination =internal_prefix_pathspec (dest_path[0 ], argv, argc,DUP_BASENAME ); }else {if (argc !=1 )die (_ ("destination '%s' is not a directory" ), dest_path[0 ]);
Signature · 1 line
2.11.0.390.gc69c2f50cf-goog
PATCH v4 9/16 pathspec: simpler logic to prefix original pathspec elements
In reply to Brandon WilliamsThe logic used to prefix an original pathspec element with 'prefix' magic is more general purpose and can be used for more than just short magic. Remove the extra code paths and rename 'prefix_short_magic' to 'prefix_magic' to better indicate that it can be used in more general situations.
Also, slightly change the logic which decides when to prefix the original element in order to prevent a pathspec of "." from getting converted to "" (empty string).
--- pathspec.c | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-)
pathspec.c +13 -20
@@ -74,13 +74,12 @@ static struct pathspec_magic { { PATHSPEC_EXCLUDE, '!', "exclude" }, }; -static void prefix_short_magic (struct strbuf *sb,int prefixlen, -unsigned short_magic) +static void prefix_magic (struct strbuf *sb,int prefixlen,unsigned magic) {int i;strbuf_addstr (sb,":(" );for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) -if (short_magic & pathspec_magic[i].bit) { +if (magic & pathspec_magic[i].bit) {if (sb->buf[sb->len -1 ] !='(' )strbuf_addch (sb,',' );strbuf_addstr (sb, pathspec_magic[i].name);
@@ -109,8 +108,8 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,static int glob_global =-1 ;static int noglob_global =-1 ;static int icase_global =-1 ; -unsigned magic =0 , short_magic =0 , global_magic =0 ; -const char *copyfrom = elt, *long_magic_end =NULL ; +unsigned magic =0 , element_magic =0 , global_magic =0 ; +const char *copyfrom = elt;char *match;int i, pathspec_prefix =-1 ;
@@ -164,7 +163,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) {if (strlen (pathspec_magic[i].name) == len && !strncmp (pathspec_magic[i].name, copyfrom, len)) { - magic |= pathspec_magic[i].bit; + element_magic |= pathspec_magic[i].bit;break ; }if (starts_with (copyfrom,"prefix:" )) {
@@ -183,7 +182,6 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags, }if (*copyfrom !=')' )die (_ ("Missing ')' at the end of pathspec magic in '%s'" ), elt); - long_magic_end = copyfrom; copyfrom++; }else {/* shorthand */
@@ -196,7 +194,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,break ;for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++)if (pathspec_magic[i].mnemonic == ch) { - short_magic |= pathspec_magic[i].bit; + element_magic |= pathspec_magic[i].bit;break ; }if (ARRAY_SIZE (pathspec_magic) <= i)
@@ -207,7 +205,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags, copyfrom++; } - magic |= short_magic; + magic |= element_magic;/* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */ if (noglob_global && !(magic &PATHSPEC_GLOB ))
@@ -242,18 +240,13 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,* Prefix the pathspec (keep all magic) and assign to * original. Useful for passing to another command. */ -if (flags &PATHSPEC_PREFIX_ORIGIN ) { +if ((flags &PATHSPEC_PREFIX_ORIGIN ) && + prefixlen && !literal_global) {struct strbuf sb =STRBUF_INIT ; -if (prefixlen && !literal_global) { -/* Preserve the actual prefix length of each pattern */ -if (short_magic) -prefix_short_magic (&sb, prefixlen, short_magic); -else if (long_magic_end) { -strbuf_add (&sb, elt, long_magic_end - elt); -strbuf_addf (&sb,",prefix:%d)" , prefixlen); - }else -strbuf_addf (&sb,":(prefix:%d)" , prefixlen); - } + +/* Preserve the actual prefix length of each pattern */ +prefix_magic (&sb, prefixlen, element_magic); +strbuf_addstr (&sb, match); item->original =strbuf_detach (&sb,NULL ); }else {
Signature · 1 line
2.11.0.390.gc69c2f50cf-goog
PATCH v4 14/16 pathspec: create strip submodule slash helpers
In reply to Brandon WilliamsFactor out the logic responsible for stripping the trailing slash on pathspecs referencing submodules into its own function.
--- pathspec.c | 68 ++++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 42 insertions(+), 26 deletions(-)
pathspec.c +42 -26
@@ -258,6 +258,44 @@ static const char *parse_element_magic(unsigned *magic, int *prefix_len,return parse_short_magic (magic, elem); } +static void strip_submodule_slash_cheap (struct pathspec_item *item) +{ +if (item->len >=1 && item->match[item->len -1 ] =='/' ) { +int i =cache_name_pos (item->match, item->len -1 ); + +if (i >=0 &&S_ISGITLINK (active_cache[i]->ce_mode)) { + item->len--; + item->match[item->len] ='\0' ; + } + } +} + +static void strip_submodule_slash_expensive (struct pathspec_item *item) +{ +int i; + +for (i =0 ; i < active_nr; i++) { +struct cache_entry *ce = active_cache[i]; +int ce_len =ce_namelen (ce); + +if (!S_ISGITLINK (ce->ce_mode)) +continue ; + +if (item->len <= ce_len || item->match[ce_len] !='/' || +memcmp (ce->name, item->match, ce_len)) +continue ; + +if (item->len == ce_len +1 ) { +/* strip trailing slash */ + item->len--; + item->match[item->len] ='\0' ; + }else { +die (_ ("Pathspec '%s' is in submodule '%.*s'" ), + item->original, ce_len, ce->name); + } + } +} +/* * Take an element of a pathspec and check for magic signatures. * Append the result to the prefix. Return the magic bitmap.
@@ -278,7 +316,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,unsigned magic =0 , element_magic =0 ;const char *copyfrom = elt;char *match; -int i, pathspec_prefix =-1 ; +int pathspec_prefix =-1 ;/* PATHSPEC_LITERAL_PATH ignores magic */ if (flags &PATHSPEC_LITERAL_PATH ) {
@@ -329,33 +367,11 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags, item->len =strlen (item->match); item->prefix = prefixlen; -if ((flags &PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP ) && - (item->len >=1 && item->match[item->len -1 ] =='/' ) && - (i =cache_name_pos (item->match, item->len -1 )) >=0 && -S_ISGITLINK (active_cache[i]->ce_mode)) { - item->len--; - match[item->len] ='\0' ; - } +if (flags &PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP ) +strip_submodule_slash_cheap (item);if (flags &PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE ) -for (i =0 ; i < active_nr; i++) { -struct cache_entry *ce = active_cache[i]; -int ce_len =ce_namelen (ce); - -if (!S_ISGITLINK (ce->ce_mode)) -continue ; - -if (item->len <= ce_len || match[ce_len] !='/' || -memcmp (ce->name, match, ce_len)) -continue ; -if (item->len == ce_len +1 ) { -/* strip trailing slash */ - item->len--; - match[item->len] ='\0' ; - }else -die (_ ("Pathspec '%s' is in submodule '%.*s'" ), - elt, ce_len, ce->name); - } +strip_submodule_slash_expensive (item);if (magic &PATHSPEC_LITERAL ) item->nowildcard_len = item->len;
Signature · 1 line
2.11.0.390.gc69c2f50cf-goog
PATCH v4 8/16 pathspec: always show mnemonic and name in unsupported_magic
In reply to Brandon WilliamsFor better clarity, always show the mnemonic and name of the unsupported magic being used. This lets users have a more clear understanding of what magic feature isn't supported. And if they supplied a mnemonic, the user will be told what its corresponding name is which will allow them to more easily search the man pages for that magic type.
This also avoids passing an extra parameter around the pathspec initialization code.
--- pathspec.c | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-)
pathspec.c +9 -15
@@ -101,9 +101,7 @@ static void prefix_short_magic(struct strbuf *sb, int prefixlen,* the prefix part must always match literally, and a single stupid * string cannot express such a case. */ -static unsigned prefix_pathspec (struct pathspec_item *item, -unsigned *p_short_magic, -unsigned flags, +static unsigned prefix_pathspec (struct pathspec_item *item,unsigned flags,const char *prefix,int prefixlen,const char *elt) {
@@ -210,7 +208,6 @@ static unsigned prefix_pathspec(struct pathspec_item *item, } magic |= short_magic; - *p_short_magic = short_magic;/* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */ if (noglob_global && !(magic &PATHSPEC_GLOB ))
@@ -329,8 +326,7 @@ static int pathspec_item_cmp(const void *a_, const void *b_) } static void NORETURN unsupported_magic(const char *pattern, -unsigned magic, -unsigned short_magic) +unsigned magic) {struct strbuf sb =STRBUF_INIT ;int i;
@@ -339,9 +335,11 @@ static void NORETURN unsupported_magic(const char *pattern,if (!(magic & m->bit))continue ;if (sb.len) -strbuf_addch (&sb,' ' ); -if (short_magic & m->bit) -strbuf_addf (&sb,"'%c'" , m->mnemonic); +strbuf_addstr (&sb,", " ); + +if (m->mnemonic) +strbuf_addf (&sb,"'%s' (mnemonic: '%c')" , + m->name, m->mnemonic);else strbuf_addf (&sb,"'%s'" , m->name); }
@@ -413,11 +411,9 @@ void parse_pathspec(struct pathspec *pathspec, prefixlen = prefix ?strlen (prefix) :0 ;for (i =0 ; i < n; i++) { -unsigned short_magic; entry = argv[i]; - item[i].magic =prefix_pathspec (item + i, &short_magic, - flags, + item[i].magic =prefix_pathspec (item + i, flags, prefix, prefixlen, entry);if ((flags &PATHSPEC_LITERAL_PATH ) && !(magic_mask &PATHSPEC_LITERAL ))
@@ -425,9 +421,7 @@ void parse_pathspec(struct pathspec *pathspec,if (item[i].magic &PATHSPEC_EXCLUDE ) nr_exclude++;if (item[i].magic & magic_mask) -unsupported_magic (entry, - item[i].magic & magic_mask, - short_magic); +unsupported_magic (entry, item[i].magic & magic_mask);if ((flags &PATHSPEC_SYMLINK_LEADING_PATH ) &&has_symlink_leading_path (item[i].match, item[i].len)) {
Signature · 1 line
2.11.0.390.gc69c2f50cf-goog
PATCH v4 7/16 pathspec: remove unused variable from unsupported_magic
In reply to Brandon WilliamsRemoved unused variable 'n' from the 'unsupported_magic()' function.
--- pathspec.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
pathspec.c +2 -3
@@ -333,8 +333,8 @@ static void NORETURN unsupported_magic(const char *pattern, unsigned short_magic) {struct strbuf sb =STRBUF_INIT ; -int i, n; -for (n = i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) { +int i; +for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) {const struct pathspec_magic *m = pathspec_magic + i;if (!(magic & m->bit))continue ;
@@ -344,7 +344,6 @@ static void NORETURN unsupported_magic(const char *pattern,strbuf_addf (&sb,"'%c'" , m->mnemonic); else strbuf_addf(&sb, "'%s'", m->name); - n++; }/* * We may want to substitute "this command" with a command
Signature · 1 line
2.11.0.390.gc69c2f50cf-goog
PATCH v4 5/16 pathspec: remove the deprecated get_pathspec function
In reply to Brandon WilliamsNow that all callers of the old 'get_pathspec' interface have been migrated to use the new pathspec struct interface it can be removed from the codebase.
Since there are no more users of the '_raw' field in the pathspec struct it can also be removed. This patch also removes the old functionality of modifying the const char **argv array that was passed into parse_pathspec. Instead the constructed 'match' string (which is a pathspec element with the prefix prepended) is only stored in its corresponding pathspec_item entry.
--- Documentation/technical/api-setup.txt | 2 -- cache.h | 1 - pathspec.c | 42 +++-------------------------------- pathspec.h | 1 - 4 files changed, 3 insertions(+), 43 deletions(-)
Documentation/technical/api-setup.txt +0 -2
@@ -27,8 +27,6 @@ parse_pathspec(). This function takes several arguments: - prefix and args come from cmd_* functions -get_pathspec() is obsolete and should never be used in new code. - parse_pathspec() helps catch unsupported features and reject them politely. At a lower level, different pathspec-related functions may not support the same set of features. Such pathspec-sensitive
cache.h +0 -1
@@ -514,7 +514,6 @@ extern void set_git_work_tree(const char *tree);#define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES" -extern const char **get_pathspec (const char *prefix,const char **pathspec);extern void setup_work_tree (void );extern const char *setup_git_directory_gently (int *);extern const char *setup_git_directory (void );
pathspec.c +3 -39
85 lines not on this page · raw message
pathspec.h +0 -1
@@ -19,7 +19,6 @@#define PATHSPEC_ONESTAR 1/* the pathspec pattern satisfies GFNM_ONESTAR */ struct pathspec { -const char **_raw;/* get_pathspec() result, not freed by clear_pathspec() */ int nr;unsigned int has_wildcard:1 ;unsigned int recursive:1 ;
Signature · 1 line
2.11.0.390.gc69c2f50cf-goog
PATCH v4 13/16 pathspec: create parse_element_magic helper
In reply to Brandon WilliamsFactor out the logic responsible for the magic in a pathspec element into its own function.
Also avoid calling into the parsing functions when PATHSPEC_LITERAL_PATH is specified since it causes magic to be ignored and all paths to be treated as literals.
--- pathspec.c | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-)
pathspec.c +20 -17
@@ -245,6 +245,19 @@ static const char *parse_short_magic(unsigned *magic, const char *elem)return pos; } +static const char *parse_element_magic (unsigned *magic,int *prefix_len, +const char *elem) +{ +if (elem[0 ] !=':' ||get_literal_global ()) +return elem;/* nothing to do */ +else if (elem[1 ] =='(' ) +/* longhand */ +return parse_long_magic (magic, prefix_len, elem); +else +/* shorthand */ +return parse_short_magic (magic, elem); +} +/* * Take an element of a pathspec and check for magic signatures. * Append the result to the prefix. Return the magic bitmap.
@@ -267,26 +280,16 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,char *match;int i, pathspec_prefix =-1 ; -if (elt[0 ] !=':' ||get_literal_global () || - (flags &PATHSPEC_LITERAL_PATH )) { - ;/* nothing to do */ - }else if (elt[1 ] =='(' ) { -/* longhand */ - copyfrom =parse_long_magic (&element_magic, - &pathspec_prefix, - elt); - }else { -/* shorthand */ - copyfrom =parse_short_magic (&element_magic, elt); - } - - magic |= element_magic; -/* PATHSPEC_LITERAL_PATH ignores magic */ -if (flags &PATHSPEC_LITERAL_PATH ) +if (flags &PATHSPEC_LITERAL_PATH ) { magic =PATHSPEC_LITERAL ; -else + }else { + copyfrom =parse_element_magic (&element_magic, + &pathspec_prefix, + elt); + magic |= element_magic; magic |=get_global_magic (element_magic); + }if (pathspec_prefix >=0 && (prefixlen || (prefix && *prefix)))
Signature · 1 line
2.11.0.390.gc69c2f50cf-goog
PATCH v4 11/16 pathspec: create parse_short_magic function
In reply to Brandon WilliamsFactor out the logic responsible for parsing short magic into its own function.
--- pathspec.c | 54 ++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 18 deletions(-)
pathspec.c +36 -18
@@ -157,6 +157,41 @@ static int get_global_magic(int element_magic) }/* +* Parse the pathspec element looking for short magic +* +* saves all magic in 'magic' +* returns the position in 'elem' after all magic has been parsed +*/ +static const char *parse_short_magic (unsigned *magic,const char *elem) +{ +const char *pos; + +for (pos = elem +1 ; *pos && *pos !=':' ; pos++) { +char ch = *pos; +int i; + +if (!is_pathspec_magic (ch)) +break ; + +for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) { +if (pathspec_magic[i].mnemonic == ch) { + *magic |= pathspec_magic[i].bit; +break ; + } + } + +if (ARRAY_SIZE (pathspec_magic) <= i) +die (_ ("Unimplemented pathspec magic '%c' in '%s'" ), + ch, elem); + } + +if (*pos ==':' ) + pos++; + +return pos; +} + +/* * Take an element of a pathspec and check for magic signatures. * Append the result to the prefix. Return the magic bitmap. *
@@ -220,24 +255,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags, copyfrom++; }else {/* shorthand */ -for (copyfrom = elt +1 ; - *copyfrom && *copyfrom !=':' ; - copyfrom++) { -char ch = *copyfrom; - -if (!is_pathspec_magic (ch)) -break ; -for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) -if (pathspec_magic[i].mnemonic == ch) { - element_magic |= pathspec_magic[i].bit; -break ; - } -if (ARRAY_SIZE (pathspec_magic) <= i) -die (_ ("Unimplemented pathspec magic '%c' in '%s'" ), - ch, elt); - } -if (*copyfrom ==':' ) - copyfrom++; + copyfrom =parse_short_magic (&element_magic, elt); } magic |= element_magic;
Signature · 1 line
2.11.0.390.gc69c2f50cf-goog
PATCH v4 6/16 pathspec: copy and free owned memory
In reply to Brandon WilliamsThe 'original' string entry in a pathspec_item is only duplicated some of the time, instead always make a copy of the original and take ownership of the memory.
Since both 'match' and 'original' string entries in a pathspec_item are owned by the pathspec struct, they need to be freed when clearing the pathspec struct (in 'clear_pathspec()') and duplicated when copying the pathspec struct (in 'copy_pathspec()').
Also change the type of 'match' and 'original' to 'char *' in order to more explicitly show the ownership of the memory.
--- pathspec.c | 23 +++++++++++++++++++---- pathspec.h | 4 ++-- 2 files changed, 21 insertions(+), 6 deletions(-)
pathspec.c +19 -4
@@ -259,8 +259,9 @@ static unsigned prefix_pathspec(struct pathspec_item *item, }strbuf_addstr (&sb, match); item->original =strbuf_detach (&sb,NULL ); - }else - item->original = elt; + }else { + item->original =xstrdup (elt); + } item->len =strlen (item->match); item->prefix = prefixlen;
@@ -388,8 +389,8 @@ void parse_pathspec(struct pathspec *pathspec,die ("BUG: PATHSPEC_PREFER_CWD requires arguments" ); pathspec->items = item =xcalloc (1 ,sizeof (*item)); - item->match = prefix; - item->original = prefix; + item->match =xstrdup (prefix); + item->original =xstrdup (prefix); item->nowildcard_len = item->len =strlen (prefix); item->prefix = item->len; pathspec->nr =1 ;
@@ -453,13 +454,27 @@ void parse_pathspec(struct pathspec *pathspec,void copy_pathspec (struct pathspec *dst,const struct pathspec *src) { +int i; + *dst = *src;ALLOC_ARRAY (dst->items, dst->nr);COPY_ARRAY (dst->items, src->items, dst->nr); + +for (i =0 ; i < dst->nr; i++) { + dst->items[i].match =xstrdup (src->items[i].match); + dst->items[i].original =xstrdup (src->items[i].original); + } }void clear_pathspec (struct pathspec *pathspec) { +int i; + +for (i =0 ; i < pathspec->nr; i++) { +free (pathspec->items[i].match); +free (pathspec->items[i].original); + }free (pathspec->items); pathspec->items =NULL ; + pathspec->nr =0 ; }
pathspec.h +2 -2
@@ -25,8 +25,8 @@ struct pathspec {unsigned magic;int max_depth;struct pathspec_item { -const char *match; -const char *original; +char *match; +char *original;unsigned magic;int len, prefix;int nowildcard_len;
Signature · 1 line
2.11.0.390.gc69c2f50cf-goog
v4 cover pathspec cleanup
In reply to Brandon Williamsv4 addresses a few comments from Duy.
* [2/16] push the guard pathspec macro into simplify_away() and exclude_matches_pathsepc().
* [6/16] when freeing a pathspec struct, set pathsepc->nr = 0.
* [8/16] tweak the die message when using unsupported magic to be more human readable.
Brandon Williams (16):
mv: remove use of deprecated 'get_pathspec()'
dir: remove struct path_simplify
dir: convert fill_directory to use the pathspec struct interface
ls-tree: convert show_recursive to use the pathspec struct interface
pathspec: remove the deprecated get_pathspec function
pathspec: copy and free owned memory
pathspec: remove unused variable from unsupported_magic
pathspec: always show mnemonic and name in unsupported_magic
pathspec: simpler logic to prefix original pathspec elements
pathspec: factor global magic into its own function
pathspec: create parse_short_magic function
pathspec: create parse_long_magic function
pathspec: create parse_element_magic helper
pathspec: create strip submodule slash helpers
pathspec: small readability changes
pathspec: rename prefix_pathspec to init_pathspec_item
Documentation/technical/api-setup.txt | 2 - builtin/ls-tree.c | 16 +- builtin/mv.c | 50 ++-- cache.h | 1 - dir.c | 193 ++++++-------- pathspec.c | 480 +++++++++++++++++++--------------- pathspec.h | 5 +- 7 files changed, 390 insertions(+), 357 deletions(-)
--- interdiff between v3 and v4
dir.c +18 -9
@@ -1353,6 +1353,15 @@ static int simplify_away(const char *path, int pathlen, {int i; +if (pathspec) +guard_pathspec (pathspec, + pathspec_fromtop | + pathspec_maxdepth | + pathspec_literal | + pathspec_glob | + pathspec_icase | + pathspec_exclude); +if (!pathspec || !pathspec->nr)return 0 ;
@@ -1385,6 +1394,15 @@ static int exclude_matches_pathspec(const char *path, int pathlen, {int i; +if (pathspec) +guard_pathspec (pathspec, + pathspec_fromtop | + pathspec_maxdepth | + pathspec_literal | + pathspec_glob | + pathspec_icase | + pathspec_exclude); +if (!pathspec || !pathspec->nr)return 0 ;
@@ -1996,15 +2014,6 @@ int read_directory(struct dir_struct *dir, const char *path, {struct untracked_cache_dir *untracked; -if (pathspec) -guard_pathspec (pathspec, - pathspec_fromtop | - pathspec_maxdepth | - pathspec_literal | - pathspec_glob | - pathspec_icase | - pathspec_exclude); -if (has_symlink_leading_path (path, len))return dir->nr;
pathspec.c +4 -2
@@ -414,10 +414,11 @@ static void NORETURN unsupported_magic(const char *pattern,if (!(magic & m->bit))continue ;if (sb.len) -strbuf_addch (&sb,' ' ); +strbuf_addstr (&sb,", " );if (m->mnemonic) -strbuf_addf (&sb,"'(%c)%s'" , m->mnemonic, m->name); +strbuf_addf (&sb,"'%s' (mnemonic: '%c')" , + m->name, m->mnemonic);else strbuf_addf (&sb,"'%s'" , m->name); }
@@ -544,4 +545,5 @@ void clear_pathspec(struct pathspec *pathspec) }free (pathspec->items); pathspec->items =NULL ; + pathspec->nr =0 ; }
Signature · 1 line
2.11.0.390.gc69c2f50cf-goog
PATCH v4 15/16 pathspec: small readability changes
A few small changes to improve readability. This is done by grouping related assignments, adding blank lines, ensuring lines are <80 characters, and adding additional comments.
--- pathspec.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-)
pathspec.c +15 -10
@@ -67,11 +67,11 @@ static struct pathspec_magic {char mnemonic;/* this cannot be ':'! */ const char *name; } pathspec_magic[] = { - { PATHSPEC_FROMTOP, '/', "top" }, - { PATHSPEC_LITERAL, 0, "literal" }, - { PATHSPEC_GLOB, '\0', "glob" }, - { PATHSPEC_ICASE, '\0', "icase" }, - { PATHSPEC_EXCLUDE, '!', "exclude" }, + { PATHSPEC_FROMTOP, '/', "top" }, + { PATHSPEC_LITERAL, '\0', "literal" }, + { PATHSPEC_GLOB, '\0', "glob" }, + { PATHSPEC_ICASE, '\0', "icase" }, + { PATHSPEC_EXCLUDE, '!', "exclude" }, }; static void prefix_magic(struct strbuf *sb, int prefixlen, unsigned magic)
@@ -336,6 +336,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,if ((magic &PATHSPEC_LITERAL ) && (magic &PATHSPEC_GLOB ))die (_ ("%s: 'literal' and 'glob' are incompatible" ), elt); +/* Create match string which will be used for pathspec matching */ if (pathspec_prefix >=0 ) { match =xstrdup (copyfrom); prefixlen = pathspec_prefix;
@@ -343,11 +344,16 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags, match =xstrdup (copyfrom); prefixlen =0 ; }else { - match =prefix_path_gently (prefix, prefixlen, &prefixlen, copyfrom); + match =prefix_path_gently (prefix, prefixlen, + &prefixlen, copyfrom);if (!match)die (_ ("%s: '%s' is outside repository" ), elt, copyfrom); } + item->match = match; + item->len =strlen (item->match); + item->prefix = prefixlen; +/* * Prefix the pathspec (keep all magic) and assign to * original. Useful for passing to another command.
@@ -364,8 +370,6 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags, }else { item->original =xstrdup (elt); } - item->len =strlen (item->match); - item->prefix = prefixlen;if (flags &PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP )strip_submodule_slash_cheap (item);
@@ -373,13 +377,14 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,if (flags &PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE )strip_submodule_slash_expensive (item); -if (magic &PATHSPEC_LITERAL ) +if (magic &PATHSPEC_LITERAL ) { item->nowildcard_len = item->len; -else { + }else { item->nowildcard_len =simple_length (item->match);if (item->nowildcard_len < prefixlen) item->nowildcard_len = prefixlen; } + item->flags =0 ;if (magic &PATHSPEC_GLOB ) {/*
Signature · 1 line
2.11.0.390.gc69c2f50cf-goog
PATCH v4 2/16 dir: remove struct path_simplify
In reply to Brandon WilliamsTeach simplify_away() and exclude_matches_pathspec() to handle struct pathspec directly, eliminating the need for the struct path_simplify.
Also renamed the len parameter to pathlen in exclude_matches_pathspec() to match the parameter names used in simplify_away().
--- dir.c | 181 +++++++++++++++++++++++++++++------------------------------------- 1 file changed, 78 insertions(+), 103 deletions(-)
dir.c +78 -103
@@ -16,11 +16,6 @@#include "varint.h" #include "ewah/ewok.h" -struct path_simplify { -int len; -const char *path; -}; -/* * Tells read_directory_recursive how a file or directory should be treated. * Values are ordered by significance, e.g. if a directory contains both
@@ -50,7 +45,7 @@ struct cached_dir {static enum path_treatment read_directory_recursive (struct dir_struct *dir,const char *path,int len,struct untracked_cache_dir *untracked, -int check_only,const struct path_simplify *simplify); +int check_only,const struct pathspec *pathspec);static int get_dtype (struct dirent *de,const char *path,int len);int fspathcmp (const char *a,const char *b)
@@ -1312,7 +1307,7 @@ static enum exist_status directory_exists_in_index(const char *dirname, int len)static enum path_treatment treat_directory (struct dir_struct *dir,struct untracked_cache_dir *untracked,const char *dirname,int len,int baselen,int exclude, -const struct path_simplify *simplify) +const struct pathspec *pathspec) {/* The "len-1" is to strip the final '/' */ switch (directory_exists_in_index (dirname, len-1 )) {
@@ -1341,7 +1336,7 @@ static enum path_treatment treat_directory(struct dir_struct *dir, untracked =lookup_untracked (dir->untracked, untracked, dirname + baselen, len - baselen);return read_directory_recursive (dir, dirname, len, - untracked,1 , simplify); + untracked,1 , pathspec); }/*
@@ -1349,24 +1344,34 @@ static enum path_treatment treat_directory(struct dir_struct *dir,* reading - if the path cannot possibly be in the pathspec, * return true, and we'll skip it early. */ -static int simplify_away (const char *path,int pathlen,const struct path_simplify *simplify) +static int simplify_away (const char *path,int pathlen, +const struct pathspec *pathspec) { -if (simplify) { -for (;;) { -const char *match = simplify->path; -int len = simplify->len; +int i; -if (!match) -break ; -if (len > pathlen) - len = pathlen; -if (!memcmp (path, match, len)) -return 0 ; - simplify++; - } -return 1 ; +if (pathspec) +GUARD_PATHSPEC (pathspec, +PATHSPEC_FROMTOP | +PATHSPEC_MAXDEPTH | +PATHSPEC_LITERAL | +PATHSPEC_GLOB | +PATHSPEC_ICASE | +PATHSPEC_EXCLUDE ); + +if (!pathspec || !pathspec->nr) +return 0 ; + +for (i =0 ; i < pathspec->nr; i++) { +const struct pathspec_item *item = &pathspec->items[i]; +int len = item->nowildcard_len; + +if (len > pathlen) + len = pathlen; +if (!ps_strncmp (item, item->match, path, len)) +return 0 ; } -return 0 ; + +return 1 ; }/*
@@ -1380,19 +1385,34 @@ static int simplify_away(const char *path, int pathlen, const struct path_simpli* 2. the path is a directory prefix of some element in the * pathspec */ -static int exclude_matches_pathspec (const char *path,int len, -const struct path_simplify *simplify) -{ -if (simplify) { -for (; simplify->path; simplify++) { -if (len == simplify->len - && !memcmp (path, simplify->path, len)) -return 1 ; -if (len < simplify->len - && simplify->path[len] =='/' - && !memcmp (path, simplify->path, len)) -return 1 ; - } +static int exclude_matches_pathspec (const char *path,int pathlen, +const struct pathspec *pathspec) +{ +int i; + +if (pathspec) +GUARD_PATHSPEC (pathspec, +PATHSPEC_FROMTOP | +PATHSPEC_MAXDEPTH | +PATHSPEC_LITERAL | +PATHSPEC_GLOB | +PATHSPEC_ICASE | +PATHSPEC_EXCLUDE ); + +if (!pathspec || !pathspec->nr) +return 0 ; + +for (i =0 ; i < pathspec->nr; i++) { +const struct pathspec_item *item = &pathspec->items[i]; +int len = item->nowildcard_len; + +if (len == pathlen && + !ps_strncmp (item, item->match, path, pathlen)) +return 1 ; +if (len > pathlen && + item->match[pathlen] =='/' && + !ps_strncmp (item, item->match, path, pathlen)) +return 1 ; }return 0 ; }
@@ -1460,7 +1480,7 @@ static enum path_treatment treat_one_path(struct dir_struct *dir,struct untracked_cache_dir *untracked, struct strbuf *path, int baselen, - const struct path_simplify *simplify, + const struct pathspec *pathspec, int dtype, struct dirent *de) { int exclude;
@@ -1512,7 +1532,7 @@ static enum path_treatment treat_one_path(struct dir_struct *dir,case DT_DIR :strbuf_addch (path,'/' );return treat_directory (dir, untracked, path->buf, path->len, - baselen, exclude, simplify); + baselen, exclude, pathspec);case DT_REG :case DT_LNK :return exclude ? path_excluded : path_untracked;
@@ -1524,7 +1544,7 @@ static enum path_treatment treat_path_fast(struct dir_struct *dir,struct cached_dir *cdir, struct strbuf *path, int baselen, - const struct path_simplify *simplify) + const struct pathspec *pathspec) {strbuf_setlen (path ,baselen );if (!cdir->ucd) {
@@ -1541,7 +1561,7 @@ static enum path_treatment treat_path_fast(struct dir_struct *dir,* with check_only set. */ return read_directory_recursive (dir, path->buf, path->len, - cdir->ucd,1 , simplify); + cdir->ucd,1 , pathspec);/* * We get path_recurse in the first run when * directory_exists_in_index() returns index_nonexistent. We
@@ -1556,23 +1576,23 @@ static enum path_treatment treat_path(struct dir_struct *dir,struct cached_dir *cdir, struct strbuf *path, int baselen, - const struct path_simplify *simplify) + const struct pathspec *pathspec) { int dtype;struct dirent *de = cdir->de;if (!de)return treat_path_fast (dir, untracked, cdir, path, - baselen, simplify); + baselen, pathspec);if (is_dot_or_dotdot (de->d_name) || !strcmp (de->d_name,".git" ))return path_none;strbuf_setlen (path, baselen);strbuf_addstr (path, de->d_name); -if (simplify_away (path->buf, path->len, simplify)) +if (simplify_away (path->buf, path->len, pathspec))return path_none; dtype =DTYPE (de); -return treat_one_path (dir, untracked, path, baselen, simplify, dtype, de); +return treat_one_path (dir, untracked, path, baselen, pathspec, dtype, de); }static void add_untracked (struct untracked_cache_dir *dir,const char *name)
@@ -1703,7 +1723,7 @@ static void close_cached_dir(struct cached_dir *cdir)static enum path_treatment read_directory_recursive (struct dir_struct *dir,const char *base,int baselen,struct untracked_cache_dir *untracked,int check_only, -const struct path_simplify *simplify) +const struct pathspec *pathspec) {struct cached_dir cdir;enum path_treatment state, subdir_state, dir_state = path_none;
@@ -1719,7 +1739,8 @@ static enum path_treatment read_directory_recursive(struct dir_struct *dir,while (!read_cached_dir (&cdir)) {/* check how the file or directory should be treated */ - state =treat_path (dir, untracked, &cdir, &path, baselen, simplify); + state =treat_path (dir, untracked, &cdir, &path, + baselen, pathspec);if (state > dir_state) dir_state = state;
@@ -1731,8 +1752,9 @@ static enum path_treatment read_directory_recursive(struct dir_struct *dir, path.buf + baselen, path.len - baselen); subdir_state = -read_directory_recursive (dir, path.buf, path.len, - ud, check_only, simplify); +read_directory_recursive (dir, path.buf, + path.len, ud, + check_only, pathspec);if (subdir_state > dir_state) dir_state = subdir_state; }
@@ -1756,7 +1778,7 @@ static enum path_treatment read_directory_recursive(struct dir_struct *dir,else if ((dir->flags &DIR_SHOW_IGNORED_TOO ) || ((dir->flags &DIR_COLLECT_IGNORED ) &&exclude_matches_pathspec (path.buf, path.len, - simplify))) + pathspec)))dir_add_ignored (dir, path.buf, path.len);break ;
@@ -1787,36 +1809,9 @@ static int cmp_name(const void *p1, const void *p2)return name_compare (e1->name, e1->len, e2->name, e2->len); } -static struct path_simplify *create_simplify (const char **pathspec) -{ -int nr, alloc =0 ; -struct path_simplify *simplify =NULL ; - -if (!pathspec) -return NULL ; - -for (nr =0 ; ; nr++) { -const char *match; -ALLOC_GROW (simplify, nr +1 , alloc); - match = *pathspec++; -if (!match) -break ; - simplify[nr].path = match; - simplify[nr].len =simple_length (match); - } - simplify[nr].path =NULL ; - simplify[nr].len =0 ; -return simplify; -} - -static void free_simplify (struct path_simplify *simplify) -{ -free (simplify); -} -static int treat_leading_path (struct dir_struct *dir,const char *path,int len, -const struct path_simplify *simplify) +const struct pathspec *pathspec) {struct strbuf sb =STRBUF_INIT ;int baselen, rc =0 ;
@@ -1840,9 +1835,9 @@ static int treat_leading_path(struct dir_struct *dir,strbuf_add (&sb, path, baselen);if (!is_directory (sb.buf))break ; -if (simplify_away (sb.buf, sb.len, simplify)) +if (simplify_away (sb.buf, sb.len, pathspec))break ; -if (treat_one_path (dir,NULL , &sb, baselen, simplify, +if (treat_one_path (dir,NULL , &sb, baselen, pathspec,DT_DIR ,NULL ) == path_none)break ;/* do not recurse into it */ if (len <= baselen) {
@@ -2010,33 +2005,14 @@ static struct untracked_cache_dir *validate_untracked_cache(struct dir_struct *dreturn root; } -int read_directory (struct dir_struct *dir,const char *path,int len,const struct pathspec *pathspec) +int read_directory (struct dir_struct *dir,const char *path, +int len,const struct pathspec *pathspec) { -struct path_simplify *simplify;struct untracked_cache_dir *untracked; -/* -* Check out create_simplify() -*/ -if (pathspec) -GUARD_PATHSPEC (pathspec, -PATHSPEC_FROMTOP | -PATHSPEC_MAXDEPTH | -PATHSPEC_LITERAL | -PATHSPEC_GLOB | -PATHSPEC_ICASE | -PATHSPEC_EXCLUDE ); -if (has_symlink_leading_path (path, len))return dir->nr; -/* -* exclude patterns are treated like positive ones in -* create_simplify. Usually exclude patterns should be a -* subset of positive ones, which has no impacts on -* create_simplify(). -*/ - simplify =create_simplify (pathspec ? pathspec->_raw :NULL ); untracked =validate_untracked_cache (dir, len, pathspec);if (!untracked)/*
@@ -2044,9 +2020,8 @@ int read_directory(struct dir_struct *dir, const char *path, int len, const stru* e.g. prep_exclude() */ dir->untracked =NULL ; -if (!len ||treat_leading_path (dir, path, len, simplify)) -read_directory_recursive (dir, path, len, untracked,0 , simplify); -free_simplify (simplify); +if (!len ||treat_leading_path (dir, path, len, pathspec)) +read_directory_recursive (dir, path, len, untracked,0 , pathspec);QSORT (dir->entries, dir->nr, cmp_name);QSORT (dir->ignored, dir->ignored_nr, cmp_name);if (dir->untracked) {
Signature · 1 line
2.11.0.390.gc69c2f50cf-goog
PATCH v4 12/16 pathspec: create parse_long_magic function
In reply to Brandon WilliamsFactor out the logic responsible for parsing long magic into its own function. As well as hoist the prefix check logic outside of the inner loop as there isn't anything that needs to be done after matching "prefix:".
--- pathspec.c | 92 ++++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 57 insertions(+), 35 deletions(-)
pathspec.c +57 -35
@@ -157,6 +157,60 @@ static int get_global_magic(int element_magic) }/* +* Parse the pathspec element looking for long magic +* +* saves all magic in 'magic' +* if prefix magic is used, save the prefix length in 'prefix_len' +* returns the position in 'elem' after all magic has been parsed +*/ +static const char *parse_long_magic (unsigned *magic,int *prefix_len, +const char *elem) +{ +const char *pos; +const char *nextat; + +for (pos = elem +2 ; *pos && *pos !=')' ; pos = nextat) { +size_t len =strcspn (pos,",)" ); +int i; + +if (pos[len] ==',' ) + nextat = pos + len +1 ;/* handle ',' */ +else + nextat = pos + len;/* handle ')' and '\0' */ + +if (!len) +continue ; + +if (starts_with (pos,"prefix:" )) { +char *endptr; + *prefix_len =strtol (pos +7 , &endptr,10 ); +if (endptr - pos != len) +die (_ ("invalid parameter for pathspec magic 'prefix'" )); +continue ; + } + +for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) { +if (strlen (pathspec_magic[i].name) == len && + !strncmp (pathspec_magic[i].name, pos, len)) { + *magic |= pathspec_magic[i].bit; +break ; + } + } + +if (ARRAY_SIZE (pathspec_magic) <= i) +die (_ ("Invalid pathspec magic '%.*s' in '%s'" ), + (int ) len, pos, elem); + } + +if (*pos !=')' ) +die (_ ("Missing ')' at the end of pathspec magic in '%s'" ), + elem); + pos++; + +return pos; +} + +/* * Parse the pathspec element looking for short magic * * saves all magic in 'magic'
@@ -218,41 +272,9 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags, ;/* nothing to do */ }else if (elt[1 ] =='(' ) {/* longhand */ -const char *nextat; -for (copyfrom = elt +2 ; - *copyfrom && *copyfrom !=')' ; - copyfrom = nextat) { -size_t len =strcspn (copyfrom,",)" ); -if (copyfrom[len] ==',' ) - nextat = copyfrom + len +1 ; -else -/* handle ')' and '\0' */ - nextat = copyfrom + len; -if (!len) -continue ; -for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) { -if (strlen (pathspec_magic[i].name) == len && - !strncmp (pathspec_magic[i].name, copyfrom, len)) { - element_magic |= pathspec_magic[i].bit; -break ; - } -if (starts_with (copyfrom,"prefix:" )) { -char *endptr; - pathspec_prefix =strtol (copyfrom +7 , - &endptr,10 ); -if (endptr - copyfrom != len) -die (_ ("invalid parameter for pathspec magic 'prefix'" )); -/* "i" would be wrong, but it does not matter */ -break ; - } - } -if (ARRAY_SIZE (pathspec_magic) <= i) -die (_ ("Invalid pathspec magic '%.*s' in '%s'" ), - (int ) len, copyfrom, elt); - } -if (*copyfrom !=')' ) -die (_ ("Missing ')' at the end of pathspec magic in '%s'" ), elt); - copyfrom++; + copyfrom =parse_long_magic (&element_magic, + &pathspec_prefix, + elt); }else {/* shorthand */ copyfrom =parse_short_magic (&element_magic, elt);
Signature · 1 line
2.11.0.390.gc69c2f50cf-goog
PATCH v4 16/16 pathspec: rename prefix_pathspec to init_pathspec_item
In reply to Brandon WilliamsGive a more relevant name to the prefix_pathspec function as it does more than just prefix a pathspec element.
--- pathspec.c | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-)
pathspec.c +7 -17
@@ -297,21 +297,11 @@ static void strip_submodule_slash_expensive(struct pathspec_item *item) }/* -* Take an element of a pathspec and check for magic signatures. -* Append the result to the prefix. Return the magic bitmap. -* -* For now, we only parse the syntax and throw out anything other than -* "top" magic. -* -* NEEDSWORK: This needs to be rewritten when we start migrating -* get_pathspec() users to use the "struct pathspec" interface. For -* example, a pathspec element may be marked as case-insensitive, but -* the prefix part must always match literally, and a single stupid -* string cannot express such a case. +* Perform the initialization of a pathspec_item based on a pathspec element. */ -static unsigned prefix_pathspec (struct pathspec_item *item,unsigned flags, -const char *prefix,int prefixlen, -const char *elt) +static void init_pathspec_item (struct pathspec_item *item,unsigned flags, +const char *prefix,int prefixlen, +const char *elt) {unsigned magic =0 , element_magic =0 ;const char *copyfrom = elt;
@@ -329,6 +319,8 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags, magic |=get_global_magic (element_magic); } + item->magic = magic; +if (pathspec_prefix >=0 && (prefixlen || (prefix && *prefix)))die ("BUG: 'prefix' magic is supposed to be used at worktree's root" );
@@ -401,7 +393,6 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,/* sanity checks, pathspec matchers assume these are sane */ assert (item->nowildcard_len <= item->len && item->prefix <= item->len); -return magic; }static int pathspec_item_cmp (const void *a_,const void *b_)
@@ -501,8 +492,7 @@ void parse_pathspec(struct pathspec *pathspec,for (i =0 ; i < n; i++) { entry = argv[i]; - item[i].magic =prefix_pathspec (item + i, flags, - prefix, prefixlen, entry); +init_pathspec_item (item + i, flags, prefix, prefixlen, entry);if (item[i].magic &PATHSPEC_EXCLUDE ) nr_exclude++;
Signature · 1 line
2.11.0.390.gc69c2f50cf-goog
Quoted 6 lines · Brandon Williams
dir.c +0
-0@@ -1353,6 +1353,15 @@ static int simplify_away(const char *path, int pathlen, {
dir.c static int simplify_away(const char *path, int pathlen, · line 1363
Quoted 10 lines
dir.c +8
-0int i; +if (pathspec) +guard_pathspec (pathspec, + pathspec_fromtop | + pathspec_maxdepth | + pathspec_literal | + pathspec_glob | + pathspec_icase | + pathspec_exclude);
You have done some magic (or your MTA/editor did) to lower case GUARD_PATHSPEC and all the flags. The real patch looks good though, so no problem.
dir.c static int simplify_away(const char *path, int pathlen, · line 1366
Quoted 3 lines
dir.c +1
-0+if (!pathspec || !pathspec->nr)return 0 ;
Super tiny nit, if GUARD_PATHSPEC is placed after this line, then we don't have to check if pathspec is non-NULL. Probably not worth a re-roll unless somebody else finds something else.
dir.c static int simplify_away(const char *path, int pathlen,
Quoted 4 lines
dir.c +2
-1if (m->mnemonic) -strbuf_addf (&sb,"'(%c)%s'" , m->mnemonic, m->name); +strbuf_addf (&sb,"'%s' (mnemonic: '%c')" , + m->name, m->mnemonic);
.. and that somebody might be me :) we need to mark "mnemonic" for translation. Putting _() around the string would do.
Ideally I would give the translators the whole sentence so they can have good context (and good translation as a result). But since we potentially concatenate multiple unsupported magic in the string, there's no way to provide one (or a few) fixed string(s) at compile time. So let's just _() it and leave it at that.
Signature · 1 line
Duy
Quoted 9 lines · Duy Nguyen
Quoted 8 lines · Brandon Williams
dir.c +0
-0@@ -1353,6 +1353,15 @@ static int simplify_away(const char *path, int pathlen, {int i;
Quoted 12 lines
dir.c static int simplify_away(const char *path, int pathlen, · line 1363
Quoted 8 lines
dir.c +8
-0+if (pathspec) +guard_pathspec (pathspec, + pathspec_fromtop | + pathspec_maxdepth | + pathspec_literal | + pathspec_glob | + pathspec_icase | + pathspec_exclude);You have done some magic (or your MTA/editor did) to lower case
GUARD_PATHSPECand all the flags. The real patch looks good though, so no problem.
That's really odd, I was sure I just did a cut and paste. I'll fix that :)
Quoted 9 lines
dir.c static int simplify_away(const char *path, int pathlen, · line 1366
Quoted 4 lines
dir.c +1
-0+if (!pathspec || !pathspec->nr)return 0 ;Super tiny nit, if
GUARD_PATHSPECis placed after this line, then we don't have to check if pathspec is non-NULL. Probably not worth a re-roll unless somebody else finds something else.
I saw this after I sent out the series again, and I agree. I'll move the guard to be after this check.
Quoted 6 lines
dir.c static int simplify_away(const char *path, int pathlen,
Quoted 5 lines
dir.c +2
-1if (m->mnemonic) -strbuf_addf (&sb,"'(%c)%s'" , m->mnemonic, m->name); +strbuf_addf (&sb,"'%s' (mnemonic: '%c')" , + m->name, m->mnemonic);
Quoted 8 lines
.. and that somebody might be me :) we need to mark "mnemonic" for translation. Putting
_()around the string would do.Ideally I would give the translators the whole sentence so they can have good context (and good translation as a result). But since we potentially concatenate multiple unsupported magic in the string, there's no way to provide one (or a few) fixed string(s) at compile time. So let's just
_()it and leave it at that.
Will do!
Signature · 1 line
Brandon Williams
Quoted 23 lines · Brandon Williams
Quoted 9 lines · Duy Nguyen
Quoted 8 lines · Brandon Williams
dir.c +0
-0@@ -1353,6 +1353,15 @@ static int simplify_away(const char *path, int pathlen, {int i;Quoted 12 lines
dir.c static int simplify_away(const char *path, int pathlen, · line 1363
Quoted 8 lines
dir.c +8
-0+if (pathspec) +guard_pathspec (pathspec, + pathspec_fromtop | + pathspec_maxdepth | + pathspec_literal | + pathspec_glob | + pathspec_icase | + pathspec_exclude);You have done some magic (or your MTA/editor did) to lower case
GUARD_PATHSPECand all the flags. The real patch looks good though, so no problem.
Quoted 2 lines
That's really odd, I was sure I just did a cut and paste. I'll fix that :)
So it looks like what ever I did to insert this into the cover letter made everything lower case. The actual patch itself looks fine.
Signature · 1 line
Brandon Williams
PATCH v5 15/16 pathspec: small readability changes
In reply to Brandon WilliamsA few small changes to improve readability. This is done by grouping related assignments, adding blank lines, ensuring lines are <80 characters, and adding additional comments.
--- pathspec.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-)
pathspec.c +15 -10
@@ -67,11 +67,11 @@ static struct pathspec_magic {char mnemonic;/* this cannot be ':'! */ const char *name; } pathspec_magic[] = { - { PATHSPEC_FROMTOP, '/', "top" }, - { PATHSPEC_LITERAL, 0, "literal" }, - { PATHSPEC_GLOB, '\0', "glob" }, - { PATHSPEC_ICASE, '\0', "icase" }, - { PATHSPEC_EXCLUDE, '!', "exclude" }, + { PATHSPEC_FROMTOP, '/', "top" }, + { PATHSPEC_LITERAL, '\0', "literal" }, + { PATHSPEC_GLOB, '\0', "glob" }, + { PATHSPEC_ICASE, '\0', "icase" }, + { PATHSPEC_EXCLUDE, '!', "exclude" }, }; static void prefix_magic(struct strbuf *sb, int prefixlen, unsigned magic)
@@ -336,6 +336,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,if ((magic &PATHSPEC_LITERAL ) && (magic &PATHSPEC_GLOB ))die (_ ("%s: 'literal' and 'glob' are incompatible" ), elt); +/* Create match string which will be used for pathspec matching */ if (pathspec_prefix >=0 ) { match =xstrdup (copyfrom); prefixlen = pathspec_prefix;
@@ -343,11 +344,16 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags, match =xstrdup (copyfrom); prefixlen =0 ; }else { - match =prefix_path_gently (prefix, prefixlen, &prefixlen, copyfrom); + match =prefix_path_gently (prefix, prefixlen, + &prefixlen, copyfrom);if (!match)die (_ ("%s: '%s' is outside repository" ), elt, copyfrom); } + item->match = match; + item->len =strlen (item->match); + item->prefix = prefixlen; +/* * Prefix the pathspec (keep all magic) and assign to * original. Useful for passing to another command.
@@ -364,8 +370,6 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags, }else { item->original =xstrdup (elt); } - item->len =strlen (item->match); - item->prefix = prefixlen;if (flags &PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP )strip_submodule_slash_cheap (item);
@@ -373,13 +377,14 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,if (flags &PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE )strip_submodule_slash_expensive (item); -if (magic &PATHSPEC_LITERAL ) +if (magic &PATHSPEC_LITERAL ) { item->nowildcard_len = item->len; -else { + }else { item->nowildcard_len =simple_length (item->match);if (item->nowildcard_len < prefixlen) item->nowildcard_len = prefixlen; } + item->flags =0 ;if (magic &PATHSPEC_GLOB ) {/*
Signature · 1 line
2.11.0.390.gc69c2f50cf-goog
PATCH v5 13/16 pathspec: create parse_element_magic helper
In reply to Brandon WilliamsFactor out the logic responsible for the magic in a pathspec element into its own function.
Also avoid calling into the parsing functions when PATHSPEC_LITERAL_PATH is specified since it causes magic to be ignored and all paths to be treated as literals.
--- pathspec.c | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-)
pathspec.c +20 -17
@@ -245,6 +245,19 @@ static const char *parse_short_magic(unsigned *magic, const char *elem)return pos; } +static const char *parse_element_magic (unsigned *magic,int *prefix_len, +const char *elem) +{ +if (elem[0 ] !=':' ||get_literal_global ()) +return elem;/* nothing to do */ +else if (elem[1 ] =='(' ) +/* longhand */ +return parse_long_magic (magic, prefix_len, elem); +else +/* shorthand */ +return parse_short_magic (magic, elem); +} +/* * Take an element of a pathspec and check for magic signatures. * Append the result to the prefix. Return the magic bitmap.
@@ -267,26 +280,16 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,char *match;int i, pathspec_prefix =-1 ; -if (elt[0 ] !=':' ||get_literal_global () || - (flags &PATHSPEC_LITERAL_PATH )) { - ;/* nothing to do */ - }else if (elt[1 ] =='(' ) { -/* longhand */ - copyfrom =parse_long_magic (&element_magic, - &pathspec_prefix, - elt); - }else { -/* shorthand */ - copyfrom =parse_short_magic (&element_magic, elt); - } - - magic |= element_magic; -/* PATHSPEC_LITERAL_PATH ignores magic */ -if (flags &PATHSPEC_LITERAL_PATH ) +if (flags &PATHSPEC_LITERAL_PATH ) { magic =PATHSPEC_LITERAL ; -else + }else { + copyfrom =parse_element_magic (&element_magic, + &pathspec_prefix, + elt); + magic |= element_magic; magic |=get_global_magic (element_magic); + }if (pathspec_prefix >=0 && (prefixlen || (prefix && *prefix)))
Signature · 1 line
2.11.0.390.gc69c2f50cf-goog
PATCH v5 12/16 pathspec: create parse_long_magic function
In reply to Brandon WilliamsFactor out the logic responsible for parsing long magic into its own function. As well as hoist the prefix check logic outside of the inner loop as there isn't anything that needs to be done after matching "prefix:".
--- pathspec.c | 92 ++++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 57 insertions(+), 35 deletions(-)
pathspec.c +57 -35
@@ -157,6 +157,60 @@ static int get_global_magic(int element_magic) }/* +* Parse the pathspec element looking for long magic +* +* saves all magic in 'magic' +* if prefix magic is used, save the prefix length in 'prefix_len' +* returns the position in 'elem' after all magic has been parsed +*/ +static const char *parse_long_magic (unsigned *magic,int *prefix_len, +const char *elem) +{ +const char *pos; +const char *nextat; + +for (pos = elem +2 ; *pos && *pos !=')' ; pos = nextat) { +size_t len =strcspn (pos,",)" ); +int i; + +if (pos[len] ==',' ) + nextat = pos + len +1 ;/* handle ',' */ +else + nextat = pos + len;/* handle ')' and '\0' */ + +if (!len) +continue ; + +if (starts_with (pos,"prefix:" )) { +char *endptr; + *prefix_len =strtol (pos +7 , &endptr,10 ); +if (endptr - pos != len) +die (_ ("invalid parameter for pathspec magic 'prefix'" )); +continue ; + } + +for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) { +if (strlen (pathspec_magic[i].name) == len && + !strncmp (pathspec_magic[i].name, pos, len)) { + *magic |= pathspec_magic[i].bit; +break ; + } + } + +if (ARRAY_SIZE (pathspec_magic) <= i) +die (_ ("Invalid pathspec magic '%.*s' in '%s'" ), + (int ) len, pos, elem); + } + +if (*pos !=')' ) +die (_ ("Missing ')' at the end of pathspec magic in '%s'" ), + elem); + pos++; + +return pos; +} + +/* * Parse the pathspec element looking for short magic * * saves all magic in 'magic'
@@ -218,41 +272,9 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags, ;/* nothing to do */ }else if (elt[1 ] =='(' ) {/* longhand */ -const char *nextat; -for (copyfrom = elt +2 ; - *copyfrom && *copyfrom !=')' ; - copyfrom = nextat) { -size_t len =strcspn (copyfrom,",)" ); -if (copyfrom[len] ==',' ) - nextat = copyfrom + len +1 ; -else -/* handle ')' and '\0' */ - nextat = copyfrom + len; -if (!len) -continue ; -for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) { -if (strlen (pathspec_magic[i].name) == len && - !strncmp (pathspec_magic[i].name, copyfrom, len)) { - element_magic |= pathspec_magic[i].bit; -break ; - } -if (starts_with (copyfrom,"prefix:" )) { -char *endptr; - pathspec_prefix =strtol (copyfrom +7 , - &endptr,10 ); -if (endptr - copyfrom != len) -die (_ ("invalid parameter for pathspec magic 'prefix'" )); -/* "i" would be wrong, but it does not matter */ -break ; - } - } -if (ARRAY_SIZE (pathspec_magic) <= i) -die (_ ("Invalid pathspec magic '%.*s' in '%s'" ), - (int ) len, copyfrom, elt); - } -if (*copyfrom !=')' ) -die (_ ("Missing ')' at the end of pathspec magic in '%s'" ), elt); - copyfrom++; + copyfrom =parse_long_magic (&element_magic, + &pathspec_prefix, + elt); }else {/* shorthand */ copyfrom =parse_short_magic (&element_magic, elt);
Signature · 1 line
2.11.0.390.gc69c2f50cf-goog
PATCH v5 10/16 pathspec: factor global magic into its own function
In reply to Brandon WilliamsCreate helper functions to read the global magic environment variables in additon to factoring out the global magic gathering logic into its own function.
--- pathspec.c | 127 +++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 78 insertions(+), 49 deletions(-)
pathspec.c +78 -49
@@ -87,6 +87,75 @@ static void prefix_magic(struct strbuf *sb, int prefixlen, unsigned magic)strbuf_addf (sb,",prefix:%d)" , prefixlen); } +static inline int get_literal_global (void ) +{ +static int literal =-1 ; + +if (literal <0 ) + literal =git_env_bool (GIT_LITERAL_PATHSPECS_ENVIRONMENT ,0 ); + +return literal; +} + +static inline int get_glob_global (void ) +{ +static int glob =-1 ; + +if (glob <0 ) + glob =git_env_bool (GIT_GLOB_PATHSPECS_ENVIRONMENT ,0 ); + +return glob; +} + +static inline int get_noglob_global (void ) +{ +static int noglob =-1 ; + +if (noglob <0 ) + noglob =git_env_bool (GIT_NOGLOB_PATHSPECS_ENVIRONMENT ,0 ); + +return noglob; +} + +static inline int get_icase_global (void ) +{ +static int icase =-1 ; + +if (icase <0 ) + icase =git_env_bool (GIT_ICASE_PATHSPECS_ENVIRONMENT ,0 ); + +return icase; +} + +static int get_global_magic (int element_magic) +{ +int global_magic =0 ; + +if (get_literal_global ()) + global_magic |=PATHSPEC_LITERAL ; + +/* --glob-pathspec is overridden by :(literal) */ +if (get_glob_global () && !(element_magic &PATHSPEC_LITERAL )) + global_magic |=PATHSPEC_GLOB ; + +if (get_glob_global () &&get_noglob_global ()) +die (_ ("global 'glob' and 'noglob' pathspec settings are incompatible" )); + +if (get_icase_global ()) + global_magic |=PATHSPEC_ICASE ; + +if ((global_magic &PATHSPEC_LITERAL ) && + (global_magic & ~PATHSPEC_LITERAL )) +die (_ ("global 'literal' pathspec setting is incompatible " +"with all other global pathspec settings" )); + +/* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */ +if (get_noglob_global () && !(element_magic &PATHSPEC_GLOB )) + global_magic |=PATHSPEC_LITERAL ; + +return global_magic; +} +/* * Take an element of a pathspec and check for magic signatures. * Append the result to the prefix. Return the magic bitmap.
@@ -104,46 +173,12 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags, const char *prefix, int prefixlen, const char *elt) { - static int literal_global = -1; -static int glob_global =-1 ; -static int noglob_global =-1 ; -static int icase_global =-1 ; -unsigned magic =0 , element_magic =0 , global_magic =0 ; + unsigned magic = 0, element_magic = 0;const char *copyfrom = elt;char *match;int i, pathspec_prefix =-1 ; -if (literal_global <0 ) - literal_global =git_env_bool (GIT_LITERAL_PATHSPECS_ENVIRONMENT ,0 ); -if (literal_global) - global_magic |=PATHSPEC_LITERAL ; - -if (glob_global <0 ) - glob_global =git_env_bool (GIT_GLOB_PATHSPECS_ENVIRONMENT ,0 ); -if (glob_global) - global_magic |=PATHSPEC_GLOB ; - -if (noglob_global <0 ) - noglob_global =git_env_bool (GIT_NOGLOB_PATHSPECS_ENVIRONMENT ,0 ); - -if (glob_global && noglob_global) -die (_ ("global 'glob' and 'noglob' pathspec settings are incompatible" )); - - -if (icase_global <0 ) - icase_global =git_env_bool (GIT_ICASE_PATHSPECS_ENVIRONMENT ,0 ); -if (icase_global) - global_magic |=PATHSPEC_ICASE ; - -if ((global_magic &PATHSPEC_LITERAL ) && - (global_magic & ~PATHSPEC_LITERAL )) -die (_ ("global 'literal' pathspec setting is incompatible " -"with all other global pathspec settings" )); - -if (flags &PATHSPEC_LITERAL_PATH ) - global_magic =0 ; - -if (elt[0 ] !=':' || literal_global || +if (elt[0 ] !=':' ||get_literal_global () || (flags &PATHSPEC_LITERAL_PATH )) { ;/* nothing to do */ }else if (elt[1 ] =='(' ) {
@@ -207,15 +242,11 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags, magic |= element_magic; -/* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */ -if (noglob_global && !(magic &PATHSPEC_GLOB )) - global_magic |=PATHSPEC_LITERAL ; - -/* --glob-pathspec is overridden by :(literal) */ -if ((global_magic &PATHSPEC_GLOB ) && (magic &PATHSPEC_LITERAL )) - global_magic &= ~PATHSPEC_GLOB ; - - magic |= global_magic; +/* PATHSPEC_LITERAL_PATH ignores magic */ +if (flags &PATHSPEC_LITERAL_PATH ) + magic =PATHSPEC_LITERAL ; +else + magic |=get_global_magic (element_magic);if (pathspec_prefix >=0 && (prefixlen || (prefix && *prefix)))
@@ -241,7 +272,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,* original. Useful for passing to another command. */ if ((flags &PATHSPEC_PREFIX_ORIGIN ) && - prefixlen && !literal_global) { + prefixlen && !get_literal_global ()) {struct strbuf sb =STRBUF_INIT ;/* Preserve the actual prefix length of each pattern */
@@ -408,9 +439,7 @@ void parse_pathspec(struct pathspec *pathspec, item[i].magic =prefix_pathspec (item + i, flags, prefix, prefixlen, entry); -if ((flags &PATHSPEC_LITERAL_PATH ) && - !(magic_mask &PATHSPEC_LITERAL )) - item[i].magic |=PATHSPEC_LITERAL ; +if (item[i].magic &PATHSPEC_EXCLUDE ) nr_exclude++;if (item[i].magic & magic_mask)
Signature · 1 line
2.11.0.390.gc69c2f50cf-goog
PATCH v5 8/16 pathspec: always show mnemonic and name in unsupported_magic
In reply to Brandon WilliamsFor better clarity, always show the mnemonic and name of the unsupported magic being used. This lets users have a more clear understanding of what magic feature isn't supported. And if they supplied a mnemonic, the user will be told what its corresponding name is which will allow them to more easily search the man pages for that magic type.
This also avoids passing an extra parameter around the pathspec initialization code.
--- pathspec.c | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-)
pathspec.c +9 -15
@@ -101,9 +101,7 @@ static void prefix_short_magic(struct strbuf *sb, int prefixlen,* the prefix part must always match literally, and a single stupid * string cannot express such a case. */ -static unsigned prefix_pathspec (struct pathspec_item *item, -unsigned *p_short_magic, -unsigned flags, +static unsigned prefix_pathspec (struct pathspec_item *item,unsigned flags,const char *prefix,int prefixlen,const char *elt) {
@@ -210,7 +208,6 @@ static unsigned prefix_pathspec(struct pathspec_item *item, } magic |= short_magic; - *p_short_magic = short_magic;/* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */ if (noglob_global && !(magic &PATHSPEC_GLOB ))
@@ -329,8 +326,7 @@ static int pathspec_item_cmp(const void *a_, const void *b_) } static void NORETURN unsupported_magic(const char *pattern, -unsigned magic, -unsigned short_magic) +unsigned magic) {struct strbuf sb =STRBUF_INIT ;int i;
@@ -339,9 +335,11 @@ static void NORETURN unsupported_magic(const char *pattern,if (!(magic & m->bit))continue ;if (sb.len) -strbuf_addch (&sb,' ' ); -if (short_magic & m->bit) -strbuf_addf (&sb,"'%c'" , m->mnemonic); +strbuf_addstr (&sb,", " ); + +if (m->mnemonic) +strbuf_addf (&sb,_ ("'%s' (mnemonic: '%c')" ), + m->name, m->mnemonic);else strbuf_addf (&sb,"'%s'" , m->name); }
@@ -413,11 +411,9 @@ void parse_pathspec(struct pathspec *pathspec, prefixlen = prefix ?strlen (prefix) :0 ;for (i =0 ; i < n; i++) { -unsigned short_magic; entry = argv[i]; - item[i].magic =prefix_pathspec (item + i, &short_magic, - flags, + item[i].magic =prefix_pathspec (item + i, flags, prefix, prefixlen, entry);if ((flags &PATHSPEC_LITERAL_PATH ) && !(magic_mask &PATHSPEC_LITERAL ))
@@ -425,9 +421,7 @@ void parse_pathspec(struct pathspec *pathspec,if (item[i].magic &PATHSPEC_EXCLUDE ) nr_exclude++;if (item[i].magic & magic_mask) -unsupported_magic (entry, - item[i].magic & magic_mask, - short_magic); +unsupported_magic (entry, item[i].magic & magic_mask);if ((flags &PATHSPEC_SYMLINK_LEADING_PATH ) &&has_symlink_leading_path (item[i].match, item[i].len)) {
Signature · 1 line
2.11.0.390.gc69c2f50cf-goog
PATCH v5 9/16 pathspec: simpler logic to prefix original pathspec elements
In reply to Brandon WilliamsThe logic used to prefix an original pathspec element with 'prefix' magic is more general purpose and can be used for more than just short magic. Remove the extra code paths and rename 'prefix_short_magic' to 'prefix_magic' to better indicate that it can be used in more general situations.
Also, slightly change the logic which decides when to prefix the original element in order to prevent a pathspec of "." from getting converted to "" (empty string).
--- pathspec.c | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-)
pathspec.c +13 -20
@@ -74,13 +74,12 @@ static struct pathspec_magic { { PATHSPEC_EXCLUDE, '!', "exclude" }, }; -static void prefix_short_magic (struct strbuf *sb,int prefixlen, -unsigned short_magic) +static void prefix_magic (struct strbuf *sb,int prefixlen,unsigned magic) {int i;strbuf_addstr (sb,":(" );for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) -if (short_magic & pathspec_magic[i].bit) { +if (magic & pathspec_magic[i].bit) {if (sb->buf[sb->len -1 ] !='(' )strbuf_addch (sb,',' );strbuf_addstr (sb, pathspec_magic[i].name);
@@ -109,8 +108,8 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,static int glob_global =-1 ;static int noglob_global =-1 ;static int icase_global =-1 ; -unsigned magic =0 , short_magic =0 , global_magic =0 ; -const char *copyfrom = elt, *long_magic_end =NULL ; +unsigned magic =0 , element_magic =0 , global_magic =0 ; +const char *copyfrom = elt;char *match;int i, pathspec_prefix =-1 ;
@@ -164,7 +163,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) {if (strlen (pathspec_magic[i].name) == len && !strncmp (pathspec_magic[i].name, copyfrom, len)) { - magic |= pathspec_magic[i].bit; + element_magic |= pathspec_magic[i].bit;break ; }if (starts_with (copyfrom,"prefix:" )) {
@@ -183,7 +182,6 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags, }if (*copyfrom !=')' )die (_ ("Missing ')' at the end of pathspec magic in '%s'" ), elt); - long_magic_end = copyfrom; copyfrom++; }else {/* shorthand */
@@ -196,7 +194,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,break ;for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++)if (pathspec_magic[i].mnemonic == ch) { - short_magic |= pathspec_magic[i].bit; + element_magic |= pathspec_magic[i].bit;break ; }if (ARRAY_SIZE (pathspec_magic) <= i)
@@ -207,7 +205,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags, copyfrom++; } - magic |= short_magic; + magic |= element_magic;/* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */ if (noglob_global && !(magic &PATHSPEC_GLOB ))
@@ -242,18 +240,13 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,* Prefix the pathspec (keep all magic) and assign to * original. Useful for passing to another command. */ -if (flags &PATHSPEC_PREFIX_ORIGIN ) { +if ((flags &PATHSPEC_PREFIX_ORIGIN ) && + prefixlen && !literal_global) {struct strbuf sb =STRBUF_INIT ; -if (prefixlen && !literal_global) { -/* Preserve the actual prefix length of each pattern */ -if (short_magic) -prefix_short_magic (&sb, prefixlen, short_magic); -else if (long_magic_end) { -strbuf_add (&sb, elt, long_magic_end - elt); -strbuf_addf (&sb,",prefix:%d)" , prefixlen); - }else -strbuf_addf (&sb,":(prefix:%d)" , prefixlen); - } + +/* Preserve the actual prefix length of each pattern */ +prefix_magic (&sb, prefixlen, element_magic); +strbuf_addstr (&sb, match); item->original =strbuf_detach (&sb,NULL ); }else {
Signature · 1 line
2.11.0.390.gc69c2f50cf-goog
PATCH v5 16/16 pathspec: rename prefix_pathspec to init_pathspec_item
In reply to Brandon WilliamsGive a more relevant name to the prefix_pathspec function as it does more than just prefix a pathspec element.
--- pathspec.c | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-)
pathspec.c +7 -17
@@ -297,21 +297,11 @@ static void strip_submodule_slash_expensive(struct pathspec_item *item) }/* -* Take an element of a pathspec and check for magic signatures. -* Append the result to the prefix. Return the magic bitmap. -* -* For now, we only parse the syntax and throw out anything other than -* "top" magic. -* -* NEEDSWORK: This needs to be rewritten when we start migrating -* get_pathspec() users to use the "struct pathspec" interface. For -* example, a pathspec element may be marked as case-insensitive, but -* the prefix part must always match literally, and a single stupid -* string cannot express such a case. +* Perform the initialization of a pathspec_item based on a pathspec element. */ -static unsigned prefix_pathspec (struct pathspec_item *item,unsigned flags, -const char *prefix,int prefixlen, -const char *elt) +static void init_pathspec_item (struct pathspec_item *item,unsigned flags, +const char *prefix,int prefixlen, +const char *elt) {unsigned magic =0 , element_magic =0 ;const char *copyfrom = elt;
@@ -329,6 +319,8 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags, magic |=get_global_magic (element_magic); } + item->magic = magic; +if (pathspec_prefix >=0 && (prefixlen || (prefix && *prefix)))die ("BUG: 'prefix' magic is supposed to be used at worktree's root" );
@@ -401,7 +393,6 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,/* sanity checks, pathspec matchers assume these are sane */ assert (item->nowildcard_len <= item->len && item->prefix <= item->len); -return magic; }static int pathspec_item_cmp (const void *a_,const void *b_)
@@ -501,8 +492,7 @@ void parse_pathspec(struct pathspec *pathspec,for (i =0 ; i < n; i++) { entry = argv[i]; - item[i].magic =prefix_pathspec (item + i, flags, - prefix, prefixlen, entry); +init_pathspec_item (item + i, flags, prefix, prefixlen, entry);if (item[i].magic &PATHSPEC_EXCLUDE ) nr_exclude++;
Signature · 1 line
2.11.0.390.gc69c2f50cf-goog
PATCH v5 14/16 pathspec: create strip submodule slash helpers
In reply to Brandon WilliamsFactor out the logic responsible for stripping the trailing slash on pathspecs referencing submodules into its own function.
--- pathspec.c | 68 ++++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 42 insertions(+), 26 deletions(-)
pathspec.c +42 -26
@@ -258,6 +258,44 @@ static const char *parse_element_magic(unsigned *magic, int *prefix_len,return parse_short_magic (magic, elem); } +static void strip_submodule_slash_cheap (struct pathspec_item *item) +{ +if (item->len >=1 && item->match[item->len -1 ] =='/' ) { +int i =cache_name_pos (item->match, item->len -1 ); + +if (i >=0 &&S_ISGITLINK (active_cache[i]->ce_mode)) { + item->len--; + item->match[item->len] ='\0' ; + } + } +} + +static void strip_submodule_slash_expensive (struct pathspec_item *item) +{ +int i; + +for (i =0 ; i < active_nr; i++) { +struct cache_entry *ce = active_cache[i]; +int ce_len =ce_namelen (ce); + +if (!S_ISGITLINK (ce->ce_mode)) +continue ; + +if (item->len <= ce_len || item->match[ce_len] !='/' || +memcmp (ce->name, item->match, ce_len)) +continue ; + +if (item->len == ce_len +1 ) { +/* strip trailing slash */ + item->len--; + item->match[item->len] ='\0' ; + }else { +die (_ ("Pathspec '%s' is in submodule '%.*s'" ), + item->original, ce_len, ce->name); + } + } +} +/* * Take an element of a pathspec and check for magic signatures. * Append the result to the prefix. Return the magic bitmap.
@@ -278,7 +316,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,unsigned magic =0 , element_magic =0 ;const char *copyfrom = elt;char *match; -int i, pathspec_prefix =-1 ; +int pathspec_prefix =-1 ;/* PATHSPEC_LITERAL_PATH ignores magic */ if (flags &PATHSPEC_LITERAL_PATH ) {
@@ -329,33 +367,11 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags, item->len =strlen (item->match); item->prefix = prefixlen; -if ((flags &PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP ) && - (item->len >=1 && item->match[item->len -1 ] =='/' ) && - (i =cache_name_pos (item->match, item->len -1 )) >=0 && -S_ISGITLINK (active_cache[i]->ce_mode)) { - item->len--; - match[item->len] ='\0' ; - } +if (flags &PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP ) +strip_submodule_slash_cheap (item);if (flags &PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE ) -for (i =0 ; i < active_nr; i++) { -struct cache_entry *ce = active_cache[i]; -int ce_len =ce_namelen (ce); - -if (!S_ISGITLINK (ce->ce_mode)) -continue ; - -if (item->len <= ce_len || match[ce_len] !='/' || -memcmp (ce->name, match, ce_len)) -continue ; -if (item->len == ce_len +1 ) { -/* strip trailing slash */ - item->len--; - match[item->len] ='\0' ; - }else -die (_ ("Pathspec '%s' is in submodule '%.*s'" ), - elt, ce_len, ce->name); - } +strip_submodule_slash_expensive (item);if (magic &PATHSPEC_LITERAL ) item->nowildcard_len = item->len;
Signature · 1 line
2.11.0.390.gc69c2f50cf-goog
PATCH v5 7/16 pathspec: remove unused variable from unsupported_magic
In reply to Brandon WilliamsRemoved unused variable 'n' from the 'unsupported_magic()' function.
--- pathspec.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
pathspec.c +2 -3
@@ -333,8 +333,8 @@ static void NORETURN unsupported_magic(const char *pattern, unsigned short_magic) {struct strbuf sb =STRBUF_INIT ; -int i, n; -for (n = i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) { +int i; +for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) {const struct pathspec_magic *m = pathspec_magic + i;if (!(magic & m->bit))continue ;
@@ -344,7 +344,6 @@ static void NORETURN unsupported_magic(const char *pattern,strbuf_addf (&sb,"'%c'" , m->mnemonic); else strbuf_addf(&sb, "'%s'", m->name); - n++; }/* * We may want to substitute "this command" with a command
Signature · 1 line
2.11.0.390.gc69c2f50cf-goog
PATCH v5 11/16 pathspec: create parse_short_magic function
In reply to Brandon WilliamsFactor out the logic responsible for parsing short magic into its own function.
--- pathspec.c | 54 ++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 18 deletions(-)
pathspec.c +36 -18
@@ -157,6 +157,41 @@ static int get_global_magic(int element_magic) }/* +* Parse the pathspec element looking for short magic +* +* saves all magic in 'magic' +* returns the position in 'elem' after all magic has been parsed +*/ +static const char *parse_short_magic (unsigned *magic,const char *elem) +{ +const char *pos; + +for (pos = elem +1 ; *pos && *pos !=':' ; pos++) { +char ch = *pos; +int i; + +if (!is_pathspec_magic (ch)) +break ; + +for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) { +if (pathspec_magic[i].mnemonic == ch) { + *magic |= pathspec_magic[i].bit; +break ; + } + } + +if (ARRAY_SIZE (pathspec_magic) <= i) +die (_ ("Unimplemented pathspec magic '%c' in '%s'" ), + ch, elem); + } + +if (*pos ==':' ) + pos++; + +return pos; +} + +/* * Take an element of a pathspec and check for magic signatures. * Append the result to the prefix. Return the magic bitmap. *
@@ -220,24 +255,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags, copyfrom++; }else {/* shorthand */ -for (copyfrom = elt +1 ; - *copyfrom && *copyfrom !=':' ; - copyfrom++) { -char ch = *copyfrom; - -if (!is_pathspec_magic (ch)) -break ; -for (i =0 ; i <ARRAY_SIZE (pathspec_magic); i++) -if (pathspec_magic[i].mnemonic == ch) { - element_magic |= pathspec_magic[i].bit; -break ; - } -if (ARRAY_SIZE (pathspec_magic) <= i) -die (_ ("Unimplemented pathspec magic '%c' in '%s'" ), - ch, elt); - } -if (*copyfrom ==':' ) - copyfrom++; + copyfrom =parse_short_magic (&element_magic, elt); } magic |= element_magic;
Signature · 1 line
2.11.0.390.gc69c2f50cf-goog
PATCH v5 4/16 ls-tree: convert show_recursive to use the pathspec struct interface
In reply to Brandon WilliamsConvert 'show_recursive()' to use the pathspec struct interface from using the '_raw' entry in the pathspec struct.
--- builtin/ls-tree.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-)
builtin/ls-tree.c +7 -9
@@ -31,21 +31,18 @@ static const char * const ls_tree_usage[] = {static int show_recursive (const char *base,int baselen,const char *pathname) { -const char **s; +int i;if (ls_options &LS_RECURSIVE )return 1 ; - s = pathspec._raw; -if (!s) +if (!pathspec.nr)return 0 ; -for (;;) { -const char *spec = *s++; +for (i =0 ; i < pathspec.nr; i++) { +const char *spec = pathspec.items[i].match;int len, speclen; -if (!spec) -return 0 ;if (strncmp (base, spec, baselen))continue ; len =strlen (pathname);
@@ -59,6 +56,7 @@ static int show_recursive(const char *base, int baselen, const char *pathname)continue ;return 1 ; } +return 0 ; } static int show_tree(const unsigned char *sha1, struct strbuf *base,
@@ -175,8 +173,8 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)* cannot be lifted until it is converted to use * match_pathspec() or tree_entry_interesting() */ -parse_pathspec (&pathspec,PATHSPEC_GLOB |PATHSPEC_ICASE | -PATHSPEC_EXCLUDE , +parse_pathspec (&pathspec,PATHSPEC_ALL_MAGIC & + ~(PATHSPEC_FROMTOP |PATHSPEC_LITERAL ),PATHSPEC_PREFER_CWD , prefix, argv +1 );for (i =0 ; i < pathspec.nr; i++)
Signature · 1 line
2.11.0.390.gc69c2f50cf-goog
PATCH v5 2/16 dir: remove struct path_simplify
In reply to Brandon WilliamsTeach simplify_away() and exclude_matches_pathspec() to handle struct pathspec directly, eliminating the need for the struct path_simplify.
Also renamed the len parameter to pathlen in exclude_matches_pathspec() to match the parameter names used in simplify_away().
--- dir.c | 179 ++++++++++++++++++++++++++++-------------------------------------- 1 file changed, 76 insertions(+), 103 deletions(-)
dir.c +76 -103
@@ -16,11 +16,6 @@#include "varint.h" #include "ewah/ewok.h" -struct path_simplify { -int len; -const char *path; -}; -/* * Tells read_directory_recursive how a file or directory should be treated. * Values are ordered by significance, e.g. if a directory contains both
@@ -50,7 +45,7 @@ struct cached_dir {static enum path_treatment read_directory_recursive (struct dir_struct *dir,const char *path,int len,struct untracked_cache_dir *untracked, -int check_only,const struct path_simplify *simplify); +int check_only,const struct pathspec *pathspec);static int get_dtype (struct dirent *de,const char *path,int len);int fspathcmp (const char *a,const char *b)
@@ -1312,7 +1307,7 @@ static enum exist_status directory_exists_in_index(const char *dirname, int len)static enum path_treatment treat_directory (struct dir_struct *dir,struct untracked_cache_dir *untracked,const char *dirname,int len,int baselen,int exclude, -const struct path_simplify *simplify) +const struct pathspec *pathspec) {/* The "len-1" is to strip the final '/' */ switch (directory_exists_in_index (dirname, len-1 )) {
@@ -1341,7 +1336,7 @@ static enum path_treatment treat_directory(struct dir_struct *dir, untracked =lookup_untracked (dir->untracked, untracked, dirname + baselen, len - baselen);return read_directory_recursive (dir, dirname, len, - untracked,1 , simplify); + untracked,1 , pathspec); }/*
@@ -1349,24 +1344,33 @@ static enum path_treatment treat_directory(struct dir_struct *dir,* reading - if the path cannot possibly be in the pathspec, * return true, and we'll skip it early. */ -static int simplify_away (const char *path,int pathlen,const struct path_simplify *simplify) +static int simplify_away (const char *path,int pathlen, +const struct pathspec *pathspec) { -if (simplify) { -for (;;) { -const char *match = simplify->path; -int len = simplify->len; +int i; -if (!match) -break ; -if (len > pathlen) - len = pathlen; -if (!memcmp (path, match, len)) -return 0 ; - simplify++; - } -return 1 ; +if (!pathspec || !pathspec->nr) +return 0 ; + +GUARD_PATHSPEC (pathspec, +PATHSPEC_FROMTOP | +PATHSPEC_MAXDEPTH | +PATHSPEC_LITERAL | +PATHSPEC_GLOB | +PATHSPEC_ICASE | +PATHSPEC_EXCLUDE ); + +for (i =0 ; i < pathspec->nr; i++) { +const struct pathspec_item *item = &pathspec->items[i]; +int len = item->nowildcard_len; + +if (len > pathlen) + len = pathlen; +if (!ps_strncmp (item, item->match, path, len)) +return 0 ; } -return 0 ; + +return 1 ; }/*
@@ -1380,19 +1384,33 @@ static int simplify_away(const char *path, int pathlen, const struct path_simpli* 2. the path is a directory prefix of some element in the * pathspec */ -static int exclude_matches_pathspec (const char *path,int len, -const struct path_simplify *simplify) -{ -if (simplify) { -for (; simplify->path; simplify++) { -if (len == simplify->len - && !memcmp (path, simplify->path, len)) -return 1 ; -if (len < simplify->len - && simplify->path[len] =='/' - && !memcmp (path, simplify->path, len)) -return 1 ; - } +static int exclude_matches_pathspec (const char *path,int pathlen, +const struct pathspec *pathspec) +{ +int i; + +if (!pathspec || !pathspec->nr) +return 0 ; + +GUARD_PATHSPEC (pathspec, +PATHSPEC_FROMTOP | +PATHSPEC_MAXDEPTH | +PATHSPEC_LITERAL | +PATHSPEC_GLOB | +PATHSPEC_ICASE | +PATHSPEC_EXCLUDE ); + +for (i =0 ; i < pathspec->nr; i++) { +const struct pathspec_item *item = &pathspec->items[i]; +int len = item->nowildcard_len; + +if (len == pathlen && + !ps_strncmp (item, item->match, path, pathlen)) +return 1 ; +if (len > pathlen && + item->match[pathlen] =='/' && + !ps_strncmp (item, item->match, path, pathlen)) +return 1 ; }return 0 ; }
@@ -1460,7 +1478,7 @@ static enum path_treatment treat_one_path(struct dir_struct *dir,struct untracked_cache_dir *untracked, struct strbuf *path, int baselen, - const struct path_simplify *simplify, + const struct pathspec *pathspec, int dtype, struct dirent *de) { int exclude;
@@ -1512,7 +1530,7 @@ static enum path_treatment treat_one_path(struct dir_struct *dir,case DT_DIR :strbuf_addch (path,'/' );return treat_directory (dir, untracked, path->buf, path->len, - baselen, exclude, simplify); + baselen, exclude, pathspec);case DT_REG :case DT_LNK :return exclude ? path_excluded : path_untracked;
@@ -1524,7 +1542,7 @@ static enum path_treatment treat_path_fast(struct dir_struct *dir,struct cached_dir *cdir, struct strbuf *path, int baselen, - const struct path_simplify *simplify) + const struct pathspec *pathspec) {strbuf_setlen (path ,baselen );if (!cdir->ucd) {
@@ -1541,7 +1559,7 @@ static enum path_treatment treat_path_fast(struct dir_struct *dir,* with check_only set. */ return read_directory_recursive (dir, path->buf, path->len, - cdir->ucd,1 , simplify); + cdir->ucd,1 , pathspec);/* * We get path_recurse in the first run when * directory_exists_in_index() returns index_nonexistent. We
@@ -1556,23 +1574,23 @@ static enum path_treatment treat_path(struct dir_struct *dir,struct cached_dir *cdir, struct strbuf *path, int baselen, - const struct path_simplify *simplify) + const struct pathspec *pathspec) { int dtype;struct dirent *de = cdir->de;if (!de)return treat_path_fast (dir, untracked, cdir, path, - baselen, simplify); + baselen, pathspec);if (is_dot_or_dotdot (de->d_name) || !strcmp (de->d_name,".git" ))return path_none;strbuf_setlen (path, baselen);strbuf_addstr (path, de->d_name); -if (simplify_away (path->buf, path->len, simplify)) +if (simplify_away (path->buf, path->len, pathspec))return path_none; dtype =DTYPE (de); -return treat_one_path (dir, untracked, path, baselen, simplify, dtype, de); +return treat_one_path (dir, untracked, path, baselen, pathspec, dtype, de); }static void add_untracked (struct untracked_cache_dir *dir,const char *name)
@@ -1703,7 +1721,7 @@ static void close_cached_dir(struct cached_dir *cdir)static enum path_treatment read_directory_recursive (struct dir_struct *dir,const char *base,int baselen,struct untracked_cache_dir *untracked,int check_only, -const struct path_simplify *simplify) +const struct pathspec *pathspec) {struct cached_dir cdir;enum path_treatment state, subdir_state, dir_state = path_none;
@@ -1719,7 +1737,8 @@ static enum path_treatment read_directory_recursive(struct dir_struct *dir,while (!read_cached_dir (&cdir)) {/* check how the file or directory should be treated */ - state =treat_path (dir, untracked, &cdir, &path, baselen, simplify); + state =treat_path (dir, untracked, &cdir, &path, + baselen, pathspec);if (state > dir_state) dir_state = state;
@@ -1731,8 +1750,9 @@ static enum path_treatment read_directory_recursive(struct dir_struct *dir, path.buf + baselen, path.len - baselen); subdir_state = -read_directory_recursive (dir, path.buf, path.len, - ud, check_only, simplify); +read_directory_recursive (dir, path.buf, + path.len, ud, + check_only, pathspec);if (subdir_state > dir_state) dir_state = subdir_state; }
@@ -1756,7 +1776,7 @@ static enum path_treatment read_directory_recursive(struct dir_struct *dir,else if ((dir->flags &DIR_SHOW_IGNORED_TOO ) || ((dir->flags &DIR_COLLECT_IGNORED ) &&exclude_matches_pathspec (path.buf, path.len, - simplify))) + pathspec)))dir_add_ignored (dir, path.buf, path.len);break ;
@@ -1787,36 +1807,9 @@ static int cmp_name(const void *p1, const void *p2)return name_compare (e1->name, e1->len, e2->name, e2->len); } -static struct path_simplify *create_simplify (const char **pathspec) -{ -int nr, alloc =0 ; -struct path_simplify *simplify =NULL ; - -if (!pathspec) -return NULL ; - -for (nr =0 ; ; nr++) { -const char *match; -ALLOC_GROW (simplify, nr +1 , alloc); - match = *pathspec++; -if (!match) -break ; - simplify[nr].path = match; - simplify[nr].len =simple_length (match); - } - simplify[nr].path =NULL ; - simplify[nr].len =0 ; -return simplify; -} - -static void free_simplify (struct path_simplify *simplify) -{ -free (simplify); -} -static int treat_leading_path (struct dir_struct *dir,const char *path,int len, -const struct path_simplify *simplify) +const struct pathspec *pathspec) {struct strbuf sb =STRBUF_INIT ;int baselen, rc =0 ;
@@ -1840,9 +1833,9 @@ static int treat_leading_path(struct dir_struct *dir,strbuf_add (&sb, path, baselen);if (!is_directory (sb.buf))break ; -if (simplify_away (sb.buf, sb.len, simplify)) +if (simplify_away (sb.buf, sb.len, pathspec))break ; -if (treat_one_path (dir,NULL , &sb, baselen, simplify, +if (treat_one_path (dir,NULL , &sb, baselen, pathspec,DT_DIR ,NULL ) == path_none)break ;/* do not recurse into it */ if (len <= baselen) {
@@ -2010,33 +2003,14 @@ static struct untracked_cache_dir *validate_untracked_cache(struct dir_struct *dreturn root; } -int read_directory (struct dir_struct *dir,const char *path,int len,const struct pathspec *pathspec) +int read_directory (struct dir_struct *dir,const char *path, +int len,const struct pathspec *pathspec) { -struct path_simplify *simplify;struct untracked_cache_dir *untracked; -/* -* Check out create_simplify() -*/ -if (pathspec) -GUARD_PATHSPEC (pathspec, -PATHSPEC_FROMTOP | -PATHSPEC_MAXDEPTH | -PATHSPEC_LITERAL | -PATHSPEC_GLOB | -PATHSPEC_ICASE | -PATHSPEC_EXCLUDE ); -if (has_symlink_leading_path (path, len))return dir->nr; -/* -* exclude patterns are treated like positive ones in -* create_simplify. Usually exclude patterns should be a -* subset of positive ones, which has no impacts on -* create_simplify(). -*/ - simplify =create_simplify (pathspec ? pathspec->_raw :NULL ); untracked =validate_untracked_cache (dir, len, pathspec);if (!untracked)/*
@@ -2044,9 +2018,8 @@ int read_directory(struct dir_struct *dir, const char *path, int len, const stru* e.g. prep_exclude() */ dir->untracked =NULL ; -if (!len ||treat_leading_path (dir, path, len, simplify)) -read_directory_recursive (dir, path, len, untracked,0 , simplify); -free_simplify (simplify); +if (!len ||treat_leading_path (dir, path, len, pathspec)) +read_directory_recursive (dir, path, len, untracked,0 , pathspec);QSORT (dir->entries, dir->nr, cmp_name);QSORT (dir->ignored, dir->ignored_nr, cmp_name);if (dir->untracked) {
Signature · 1 line
2.11.0.390.gc69c2f50cf-goog
PATCH v5 3/16 dir: convert fill_directory to use the pathspec struct interface
In reply to Brandon WilliamsConvert 'fill_directory()' to use the pathspec struct interface from using the '_raw' entry in the pathspec struct.
--- dir.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
dir.c +8 -4
@@ -174,17 +174,21 @@ char *common_prefix(const struct pathspec *pathspec)int fill_directory (struct dir_struct *dir,const struct pathspec *pathspec) { -size_t len; +char *prefix; +size_t prefix_len;/* * Calculate common prefix for the pathspec, and * use that to optimize the directory walk */ - len =common_prefix_len (pathspec); + prefix =common_prefix (pathspec); + prefix_len = prefix ?strlen (prefix) :0 ;/* Read the directory and prune it */ -read_directory (dir, pathspec->nr ? pathspec->_raw[0 ] :"" , len, pathspec); -return len; +read_directory (dir, prefix, prefix_len, pathspec); + +free (prefix); +return prefix_len; } int within_depth(const char *name, int namelen,
Signature · 1 line
2.11.0.390.gc69c2f50cf-goog
v5 cover pathspec cleanup
In reply to Brandon WilliamsChanges in v5:
* Move GUARD_PATHSPEC to prevent checking if pathspec is null twice.
* Mark a string containing 'mnemonic' for translation.
Brandon Williams (16):
mv: remove use of deprecated 'get_pathspec()'
dir: remove struct path_simplify
dir: convert fill_directory to use the pathspec struct interface
ls-tree: convert show_recursive to use the pathspec struct interface
pathspec: remove the deprecated get_pathspec function
pathspec: copy and free owned memory
pathspec: remove unused variable from unsupported_magic
pathspec: always show mnemonic and name in unsupported_magic
pathspec: simpler logic to prefix original pathspec elements
pathspec: factor global magic into its own function
pathspec: create parse_short_magic function
pathspec: create parse_long_magic function
pathspec: create parse_element_magic helper
pathspec: create strip submodule slash helpers
pathspec: small readability changes
pathspec: rename prefix_pathspec to init_pathspec_item
Documentation/technical/api-setup.txt | 2 - builtin/ls-tree.c | 16 +- builtin/mv.c | 50 ++-- cache.h | 1 - dir.c | 191 ++++++-------- pathspec.c | 480 +++++++++++++++++++--------------- pathspec.h | 5 +- 7 files changed, 388 insertions(+), 357 deletions(-)
--- interdiff between v4 and v5
dir.c +16 -18
@@ -1353,18 +1353,17 @@ static int simplify_away(const char *path, int pathlen, {int i; -if (pathspec) -GUARD_PATHSPEC (pathspec, -PATHSPEC_FROMTOP | -PATHSPEC_MAXDEPTH | -PATHSPEC_LITERAL | -PATHSPEC_GLOB | -PATHSPEC_ICASE | -PATHSPEC_EXCLUDE ); -if (!pathspec || !pathspec->nr)return 0 ; +GUARD_PATHSPEC (pathspec, +PATHSPEC_FROMTOP | +PATHSPEC_MAXDEPTH | +PATHSPEC_LITERAL | +PATHSPEC_GLOB | +PATHSPEC_ICASE | +PATHSPEC_EXCLUDE ); +for (i =0 ; i < pathspec->nr; i++) {const struct pathspec_item *item = &pathspec->items[i];int len = item->nowildcard_len;
@@ -1394,18 +1393,17 @@ static int exclude_matches_pathspec(const char *path, int pathlen, {int i; -if (pathspec) -GUARD_PATHSPEC (pathspec, -PATHSPEC_FROMTOP | -PATHSPEC_MAXDEPTH | -PATHSPEC_LITERAL | -PATHSPEC_GLOB | -PATHSPEC_ICASE | -PATHSPEC_EXCLUDE ); -if (!pathspec || !pathspec->nr)return 0 ; +GUARD_PATHSPEC (pathspec, +PATHSPEC_FROMTOP | +PATHSPEC_MAXDEPTH | +PATHSPEC_LITERAL | +PATHSPEC_GLOB | +PATHSPEC_ICASE | +PATHSPEC_EXCLUDE ); +for (i =0 ; i < pathspec->nr; i++) {const struct pathspec_item *item = &pathspec->items[i];int len = item->nowildcard_len;
pathspec.c +1 -1
@@ -417,7 +417,7 @@ static void NORETURN unsupported_magic(const char *pattern,strbuf_addstr (&sb,", " );if (m->mnemonic) -strbuf_addf (&sb,"'%s' (mnemonic: '%c')" , +strbuf_addf (&sb,_ ("'%s' (mnemonic: '%c')" ), m->name, m->mnemonic);else strbuf_addf (&sb,"'%s'" , m->name);
Signature · 1 line
2.11.0.390.gc69c2f50cf-goog
PATCH v5 1/16 mv: remove use of deprecated 'get_pathspec()'
Convert the 'internal_copy_pathspec()' function to 'prefix_path()' instead of using the deprecated 'get_pathspec()' interface. Also, rename 'internal_copy_pathspec()' to 'internal_prefix_pathspec()' to be more descriptive of what the funciton is actually doing.
In addition to this, fix a memory leak caused by only duplicating some of the pathspec elements. Instead always duplicate all of the the pathspec elements as an intermediate step (with modificationed based on the passed in flags). This way the intermediate strings can then be freed after getting the result from 'prefix_path()'.
--- builtin/mv.c | 50 +++++++++++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 19 deletions(-)
builtin/mv.c +31 -19
@@ -4,6 +4,7 @@* Copyright (C) 2006 Johannes Schindelin */ #include "builtin.h" +#include "pathspec.h" #include "lockfile.h" #include "dir.h" #include "cache-tree.h"
@@ -19,31 +20,42 @@ static const char * const builtin_mv_usage[] = {#define DUP_BASENAME 1#define KEEP_TRAILING_SLASH 2 -static const char **internal_copy_pathspec (const char *prefix, -const char **pathspec, -int count,unsigned flags) +static const char **internal_prefix_pathspec (const char *prefix, +const char **pathspec, +int count,unsigned flags) {int i;const char **result; +int prefixlen = prefix ?strlen (prefix) :0 ;ALLOC_ARRAY (result, count +1 ); -COPY_ARRAY (result, pathspec, count); - result[count] =NULL ; + +/* Create an intermediate copy of the pathspec based on the flags */ for (i =0 ; i < count; i++) { -int length =strlen (result[i]); +int length =strlen (pathspec[i]);int to_copy = length; +char *it;while (!(flags &KEEP_TRAILING_SLASH ) && - to_copy >0 &&is_dir_sep (result[i][to_copy -1 ])) + to_copy >0 &&is_dir_sep (pathspec[i][to_copy -1 ])) to_copy--; -if (to_copy != length || flags &DUP_BASENAME ) { -char *it =xmemdupz (result[i], to_copy); -if (flags &DUP_BASENAME ) { - result[i] =xstrdup (basename (it)); -free (it); - }else - result[i] = it; + + it =xmemdupz (pathspec[i], to_copy); +if (flags &DUP_BASENAME ) { + result[i] =xstrdup (basename (it)); +free (it); + }else { + result[i] = it; } } -return get_pathspec (prefix, result); + result[count] =NULL ; + +/* Prefix the pathspec and free the old intermediate strings */ +for (i =0 ; i < count; i++) { +const char *match =prefix_path (prefix, prefixlen, result[i]); +free ((char *) result[i]); + result[i] = match; + } + +return result; }static const char *add_slash (const char *path)
@@ -130,7 +142,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)if (read_cache () <0 )die (_ ("index file corrupt" )); - source =internal_copy_pathspec (prefix, argv, argc,0 ); + source =internal_prefix_pathspec (prefix, argv, argc,0 ); modes =xcalloc (argc,sizeof (enum update_mode ));/* * Keep trailing slash, needed to let
@@ -140,16 +152,16 @@ int cmd_mv(int argc, const char **argv, const char *prefix) flags =KEEP_TRAILING_SLASH ;if (argc ==1 &&is_directory (argv[0 ]) && !is_directory (argv[1 ])) flags =0 ; - dest_path =internal_copy_pathspec (prefix, argv + argc,1 , flags); + dest_path =internal_prefix_pathspec (prefix, argv + argc,1 , flags); submodule_gitfile =xcalloc (argc,sizeof (char *));if (dest_path[0 ][0 ] =='\0' )/* special case: "." was normalized to "" */ - destination =internal_copy_pathspec (dest_path[0 ], argv, argc,DUP_BASENAME ); + destination =internal_prefix_pathspec (dest_path[0 ], argv, argc,DUP_BASENAME );else if (!lstat (dest_path[0 ], &st) &&S_ISDIR (st.st_mode)) { dest_path[0 ] =add_slash (dest_path[0 ]); - destination =internal_copy_pathspec (dest_path[0 ], argv, argc,DUP_BASENAME ); + destination =internal_prefix_pathspec (dest_path[0 ], argv, argc,DUP_BASENAME ); }else {if (argc !=1 )die (_ ("destination '%s' is not a directory" ), dest_path[0 ]);
Signature · 1 line
2.11.0.390.gc69c2f50cf-goog
PATCH v5 5/16 pathspec: remove the deprecated get_pathspec function
In reply to Brandon WilliamsNow that all callers of the old 'get_pathspec' interface have been migrated to use the new pathspec struct interface it can be removed from the codebase.
Since there are no more users of the '_raw' field in the pathspec struct it can also be removed. This patch also removes the old functionality of modifying the const char **argv array that was passed into parse_pathspec. Instead the constructed 'match' string (which is a pathspec element with the prefix prepended) is only stored in its corresponding pathspec_item entry.
--- Documentation/technical/api-setup.txt | 2 -- cache.h | 1 - pathspec.c | 42 +++-------------------------------- pathspec.h | 1 - 4 files changed, 3 insertions(+), 43 deletions(-)
Documentation/technical/api-setup.txt +0 -2
@@ -27,8 +27,6 @@ parse_pathspec(). This function takes several arguments: - prefix and args come from cmd_* functions -get_pathspec() is obsolete and should never be used in new code. - parse_pathspec() helps catch unsupported features and reject them politely. At a lower level, different pathspec-related functions may not support the same set of features. Such pathspec-sensitive
cache.h +0 -1
@@ -514,7 +514,6 @@ extern void set_git_work_tree(const char *tree);#define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES" -extern const char **get_pathspec (const char *prefix,const char **pathspec);extern void setup_work_tree (void );extern const char *setup_git_directory_gently (int *);extern const char *setup_git_directory (void );
pathspec.c +3 -39
85 lines not on this page · raw message
pathspec.h +0 -1
@@ -19,7 +19,6 @@#define PATHSPEC_ONESTAR 1/* the pathspec pattern satisfies GFNM_ONESTAR */ struct pathspec { -const char **_raw;/* get_pathspec() result, not freed by clear_pathspec() */ int nr;unsigned int has_wildcard:1 ;unsigned int recursive:1 ;
Signature · 1 line
2.11.0.390.gc69c2f50cf-goog
PATCH v5 6/16 pathspec: copy and free owned memory
In reply to Brandon WilliamsThe 'original' string entry in a pathspec_item is only duplicated some of the time, instead always make a copy of the original and take ownership of the memory.
Since both 'match' and 'original' string entries in a pathspec_item are owned by the pathspec struct, they need to be freed when clearing the pathspec struct (in 'clear_pathspec()') and duplicated when copying the pathspec struct (in 'copy_pathspec()').
Also change the type of 'match' and 'original' to 'char *' in order to more explicitly show the ownership of the memory.
--- pathspec.c | 23 +++++++++++++++++++---- pathspec.h | 4 ++-- 2 files changed, 21 insertions(+), 6 deletions(-)
pathspec.c +19 -4
@@ -259,8 +259,9 @@ static unsigned prefix_pathspec(struct pathspec_item *item, }strbuf_addstr (&sb, match); item->original =strbuf_detach (&sb,NULL ); - }else - item->original = elt; + }else { + item->original =xstrdup (elt); + } item->len =strlen (item->match); item->prefix = prefixlen;
@@ -388,8 +389,8 @@ void parse_pathspec(struct pathspec *pathspec,die ("BUG: PATHSPEC_PREFER_CWD requires arguments" ); pathspec->items = item =xcalloc (1 ,sizeof (*item)); - item->match = prefix; - item->original = prefix; + item->match =xstrdup (prefix); + item->original =xstrdup (prefix); item->nowildcard_len = item->len =strlen (prefix); item->prefix = item->len; pathspec->nr =1 ;
@@ -453,13 +454,27 @@ void parse_pathspec(struct pathspec *pathspec,void copy_pathspec (struct pathspec *dst,const struct pathspec *src) { +int i; + *dst = *src;ALLOC_ARRAY (dst->items, dst->nr);COPY_ARRAY (dst->items, src->items, dst->nr); + +for (i =0 ; i < dst->nr; i++) { + dst->items[i].match =xstrdup (src->items[i].match); + dst->items[i].original =xstrdup (src->items[i].original); + } }void clear_pathspec (struct pathspec *pathspec) { +int i; + +for (i =0 ; i < pathspec->nr; i++) { +free (pathspec->items[i].match); +free (pathspec->items[i].original); + }free (pathspec->items); pathspec->items =NULL ; + pathspec->nr =0 ; }
pathspec.h +2 -2
@@ -25,8 +25,8 @@ struct pathspec {unsigned magic;int max_depth;struct pathspec_item { -const char *match; -const char *original; +char *match; +char *original;unsigned magic;int len, prefix;int nowildcard_len;
Signature · 1 line
2.11.0.390.gc69c2f50cf-goog
Quoted 3 lines · Brandon Williams
Changes in v5:
* MoveGUARD_PATHSPECto prevent checking if pathspec is null twice.
* Mark a string containing 'mnemonic' for translation.
Argh.. I've run out of things to complain about! Ack!
Signature · 1 line
Duy
Quoted 6 lines · Duy Nguyen
Quoted 3 lines · Brandon Williams
Changes in v5:
* MoveGUARD_PATHSPECto prevent checking if pathspec is null twice.
* Mark a string containing 'mnemonic' for translation.Argh.. I've run out of things to complain about! Ack!
Thanks ;-) Will queue with your reviewed-by.
Quoted 6 lines · Duy Nguyen
Quoted 3 lines · Brandon Williams
Changes in v5:
* MoveGUARD_PATHSPECto prevent checking if pathspec is null twice.
* Mark a string containing 'mnemonic' for translation.Argh.. I've run out of things to complain about! Ack!
haha! Well thanks again for your help in reviewing this series!
Signature · 1 line
Brandon Williams