{"thread":{"id":"34432","subject":"[PATCH v2 00/46] \"struct pathspec\" conversion and :(glob) and :(icase)","startedAt":"2013-07-14T08:35:23Z","lastAt":"2013-08-18T19:40:26Z","messageCount":52,"participants":["Nguyễn Thái Ngọc Duy","Eric Sunshine","Junio C Hamano","Michael Haggerty"],"isPatch":true,"patchVersion":2,"patchTotal":46},"messages":[{"id":"223273","messageId":"1373790969-13000-1-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":null,"subject":"[PATCH v2 00/46] \"struct pathspec\" conversion and :(glob) and :(icase)","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:35:23Z","receivedAt":"2013-07-14T08:35:23Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Compared to the last round [1] this series mainly fixes comments and\ncommit messages suggested by Eric and Junio. It also fixes a conflict\nwith cb/log-follow-with-combined (in master) and introduces :(icase)\nmentioned in the last round.\n\n[1] http://thread.gmane.org/gmane.comp.version-control.git/226892\n\nNguyễn Thái Ngọc Duy (46):\n  clean: remove unused variable \"seen\"\n  Move struct pathspec and related functions to pathspec.[ch]\n  pathspec: i18n-ize error strings in pathspec parsing code\n  pathspec: add copy_pathspec\n  Add parse_pathspec() that converts cmdline args to struct pathspec\n  parse_pathspec: save original pathspec for reporting\n  parse_pathspec: add PATHSPEC_PREFER_{CWD,FULL}\n  Convert some get_pathspec() calls to parse_pathspec()\n  parse_pathspec: add special flag for max_depth feature\n  parse_pathspec: support stripping submodule trailing slashes\n  parse_pathspec: support stripping/checking submodule paths\n  parse_pathspec: support prefixing original patterns\n  Guard against new pathspec magic in pathspec matching code\n  clean: convert to use parse_pathspec\n  commit: convert to use parse_pathspec\n  status: convert to use parse_pathspec\n  rerere: convert to use parse_pathspec\n  checkout: convert to use parse_pathspec\n  rm: convert to use parse_pathspec\n  ls-files: convert to use parse_pathspec\n  archive: convert to use parse_pathspec\n  check-ignore: convert to use parse_pathspec\n  add: convert to use parse_pathspec\n  reset: convert to use parse_pathspec\n  line-log: convert to use parse_pathspec\n  Convert read_cache_preload() to take struct pathspec\n  Convert run_add_interactive to use struct pathspec\n  Convert unmerge_cache to take struct pathspec\n  checkout: convert read_tree_some to take struct pathspec\n  Convert report_path_error to take struct pathspec\n  Convert refresh_index to take struct pathspec\n  Convert {read,fill}_directory to take struct pathspec\n  Convert add_files_to_cache to take struct pathspec\n  Convert common_prefix() to use struct pathspec\n  Remove diff_tree_{setup,release}_paths\n  Remove init_pathspec() in favor of parse_pathspec()\n  Remove match_pathspec() in favor of match_pathspec_depth()\n  tree-diff: remove the use of pathspec's raw[] in follow-rename codepath\n  Rename field \"raw\" to \"_raw\" in struct pathspec\n  parse_pathspec: make sure the prefix part is wildcard-free\n  parse_pathspec: preserve prefix length via PATHSPEC_PREFIX_ORIGIN\n  Kill limit_pathspec_to_literal() as it's only used by parse_pathspec()\n  pathspec: support :(literal) syntax for noglob pathspec\n  pathspec: make --literal-pathspecs disable pathspec magic\n  pathspec: support :(glob) syntax\n  parse_pathspec: accept :(icase)path syntax\n\n Documentation/git.txt                 |  31 ++-\n Documentation/glossary-content.txt    |  52 +++-\n Documentation/technical/api-setup.txt |  38 ++-\n archive.c                             |  18 +-\n archive.h                             |   4 +-\n builtin/add.c                         | 166 ++++++-------\n builtin/blame.c                       |  14 +-\n builtin/check-ignore.c                |  35 ++-\n builtin/checkout.c                    |  40 +--\n builtin/clean.c                       |  24 +-\n builtin/commit.c                      |  37 ++-\n builtin/diff-files.c                  |   2 +-\n builtin/diff-index.c                  |   2 +-\n builtin/diff.c                        |   6 +-\n builtin/grep.c                        |  10 +-\n builtin/log.c                         |   2 +-\n builtin/ls-files.c                    |  75 +++---\n builtin/ls-tree.c                     |  13 +-\n builtin/mv.c                          |  13 +-\n builtin/rerere.c                      |   8 +-\n builtin/reset.c                       |  33 ++-\n builtin/rm.c                          |  24 +-\n builtin/update-index.c                |   6 +-\n cache.h                               |  35 +--\n combine-diff.c                        |   4 +-\n commit.h                              |   2 +-\n diff-lib.c                            |   3 +-\n diff.h                                |   3 +-\n dir.c                                 | 319 +++++++++---------------\n dir.h                                 |  18 +-\n git.c                                 |  12 +\n line-log.c                            |   2 +-\n merge-recursive.c                     |   2 +-\n notes-merge.c                         |   4 +-\n path.c                                |  15 +-\n pathspec.c                            | 449 +++++++++++++++++++++++++++++++---\n pathspec.h                            |  88 ++++++-\n preload-index.c                       |  21 +-\n read-cache.c                          |   5 +-\n rerere.c                              |   7 +-\n rerere.h                              |   4 +-\n resolve-undo.c                        |   4 +-\n resolve-undo.h                        |   2 +-\n revision.c                            |  11 +-\n setup.c                               | 173 ++-----------\n t/t0008-ignores.sh                    |   8 +-\n t/t6130-pathspec-noglob.sh            |  87 +++++++\n t/t6131-pathspec-icase.sh (new +x)    |  97 ++++++++\n tree-diff.c                           |  48 ++--\n tree-walk.c                           |  78 ++++--\n tree.c                                |   4 +-\n tree.h                                |   2 +-\n wt-status.c                           |  16 +-\n wt-status.h                           |   2 +-\n 54 files changed, 1385 insertions(+), 793 deletions(-)\n create mode 100755 t/t6131-pathspec-icase.sh\n\n-- \n1.8.2.83.gc99314b\n"},{"id":"223274","messageId":"1373790969-13000-2-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 01/46] clean: remove unused variable \"seen\"","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:35:24Z","receivedAt":"2013-07-14T08:35:24Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n builtin/clean.c | 11 ++---------\n 1 file changed, 2 insertions(+), 9 deletions(-)\n\ndiff --git a/builtin/clean.c b/builtin/clean.c\nindex 04e396b..f955a40 100644\n--- a/builtin/clean.c\n+++ b/builtin/clean.c\n@@ -155,7 +155,6 @@ int cmd_clean(int argc, const char **argv, const char *prefix)\n \tstruct string_list exclude_list = STRING_LIST_INIT_NODUP;\n \tstruct exclude_list *el;\n \tconst char *qname;\n-\tchar *seen = NULL;\n \tstruct option options[] = {\n \t\tOPT__QUIET(&quiet, N_(\"do not print names of files removed\")),\n \t\tOPT__DRY_RUN(&dry_run, N_(\"dry run\")),\n@@ -214,9 +213,6 @@ int cmd_clean(int argc, const char **argv, const char *prefix)\n \n \tfill_directory(&dir, pathspec);\n \n-\tif (pathspec)\n-\t\tseen = xmalloc(argc > 0 ? argc : 1);\n-\n \tfor (i = 0; i < dir.nr; i++) {\n \t\tstruct dir_entry *ent = dir.entries[i];\n \t\tint len, pos;\n@@ -250,11 +246,9 @@ int cmd_clean(int argc, const char **argv, const char *prefix)\n \t\tif (lstat(ent->name, &st))\n \t\t\tcontinue;\n \n-\t\tif (pathspec) {\n-\t\t\tmemset(seen, 0, argc > 0 ? argc : 1);\n+\t\tif (pathspec)\n \t\t\tmatches = match_pathspec(pathspec, ent->name, len,\n-\t\t\t\t\t\t 0, seen);\n-\t\t}\n+\t\t\t\t\t\t 0, NULL);\n \n \t\tif (S_ISDIR(st.st_mode)) {\n \t\t\tstrbuf_addstr(&directory, ent->name);\n@@ -281,7 +275,6 @@ int cmd_clean(int argc, const char **argv, const char *prefix)\n \t\t\t}\n \t\t}\n \t}\n-\tfree(seen);\n \n \tstrbuf_release(&directory);\n \tstring_list_clear(&exclude_list, 0);\n-- \n1.8.2.83.gc99314b\n"},{"id":"223275","messageId":"1373790969-13000-3-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 02/46] Move struct pathspec and related functions to pathspec.[ch]","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:35:25Z","receivedAt":"2013-07-14T08:35:25Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n archive.c              |   1 +\n builtin/grep.c         |   1 +\n builtin/ls-files.c     |   1 +\n builtin/ls-tree.c      |   1 +\n builtin/update-index.c |   1 +\n cache.h                |  22 +-------\n diff.h                 |   1 +\n dir.c                  |   1 +\n pathspec.c             | 150 +++++++++++++++++++++++++++++++++++++++++++++++++\n pathspec.h             |  21 +++++++\n preload-index.c        |   1 +\n setup.c                | 149 ------------------------------------------------\n tree-walk.c            |   1 +\n 13 files changed, 182 insertions(+), 169 deletions(-)\n\ndiff --git a/archive.c b/archive.c\nindex d254fa5..c699a2d 100644\n--- a/archive.c\n+++ b/archive.c\n@@ -5,6 +5,7 @@\n #include \"archive.h\"\n #include \"parse-options.h\"\n #include \"unpack-trees.h\"\n+#include \"pathspec.h\"\n \n static char const * const archive_usage[] = {\n \tN_(\"git archive [options] <tree-ish> [<path>...]\"),\ndiff --git a/builtin/grep.c b/builtin/grep.c\nindex 159e65d..4de49df 100644\n--- a/builtin/grep.c\n+++ b/builtin/grep.c\n@@ -17,6 +17,7 @@\n #include \"grep.h\"\n #include \"quote.h\"\n #include \"dir.h\"\n+#include \"pathspec.h\"\n \n static char const * const grep_usage[] = {\n \tN_(\"git grep [options] [-e] <pattern> [<rev>...] [[--] <path>...]\"),\ndiff --git a/builtin/ls-files.c b/builtin/ls-files.c\nindex 3a410c3..30357df 100644\n--- a/builtin/ls-files.c\n+++ b/builtin/ls-files.c\n@@ -13,6 +13,7 @@\n #include \"parse-options.h\"\n #include \"resolve-undo.h\"\n #include \"string-list.h\"\n+#include \"pathspec.h\"\n \n static int abbrev;\n static int show_deleted;\ndiff --git a/builtin/ls-tree.c b/builtin/ls-tree.c\nindex fb76e38..93fc3a0 100644\n--- a/builtin/ls-tree.c\n+++ b/builtin/ls-tree.c\n@@ -10,6 +10,7 @@\n #include \"quote.h\"\n #include \"builtin.h\"\n #include \"parse-options.h\"\n+#include \"pathspec.h\"\n \n static int line_termination = '\\n';\n #define LS_RECURSIVE 1\ndiff --git a/builtin/update-index.c b/builtin/update-index.c\nindex 5c7762e..b9c2bd0 100644\n--- a/builtin/update-index.c\n+++ b/builtin/update-index.c\n@@ -11,6 +11,7 @@\n #include \"refs.h\"\n #include \"resolve-undo.h\"\n #include \"parse-options.h\"\n+#include \"pathspec.h\"\n \n /*\n  * Default to not allowing changes to the list of files. The\ndiff --git a/cache.h b/cache.h\nindex dd0fb33..fd0a6f8 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -189,6 +189,8 @@ struct cache_entry {\n #error \"CE_EXTENDED_FLAGS out of range\"\n #endif\n \n+struct pathspec;\n+\n /*\n  * Copy the sha1 and stat state of a cache entry from one to\n  * another. But we never change the name, or the hash state!\n@@ -489,28 +491,8 @@ extern void *read_blob_data_from_index(struct index_state *, const char *, unsig\n extern int ie_match_stat(const struct index_state *, const struct cache_entry *, struct stat *, unsigned int);\n extern int ie_modified(const struct index_state *, const struct cache_entry *, struct stat *, unsigned int);\n \n-#define PATHSPEC_ONESTAR 1\t/* the pathspec pattern sastisfies GFNM_ONESTAR */\n-\n-struct pathspec {\n-\tconst char **raw; /* get_pathspec() result, not freed by free_pathspec() */\n-\tint nr;\n-\tunsigned int has_wildcard:1;\n-\tunsigned int recursive:1;\n-\tint max_depth;\n-\tstruct pathspec_item {\n-\t\tconst char *match;\n-\t\tint len;\n-\t\tint nowildcard_len;\n-\t\tint flags;\n-\t} *items;\n-};\n-\n-extern int init_pathspec(struct pathspec *, const char **);\n-extern void free_pathspec(struct pathspec *);\n extern int ce_path_match(const struct cache_entry *ce, const struct pathspec *pathspec);\n \n-extern int limit_pathspec_to_literal(void);\n-\n #define HASH_WRITE_OBJECT 1\n #define HASH_FORMAT_CHECK 2\n extern int index_fd(unsigned char *sha1, int fd, struct stat *st, enum object_type type, const char *path, unsigned flags);\ndiff --git a/diff.h b/diff.h\nindex 78b4091..d1bc914 100644\n--- a/diff.h\n+++ b/diff.h\n@@ -5,6 +5,7 @@\n #define DIFF_H\n \n #include \"tree-walk.h\"\n+#include \"pathspec.h\"\n \n struct rev_info;\n struct diff_options;\ndiff --git a/dir.c b/dir.c\nindex 0480419..b0599dd 100644\n--- a/dir.c\n+++ b/dir.c\n@@ -11,6 +11,7 @@\n #include \"dir.h\"\n #include \"refs.h\"\n #include \"wildmatch.h\"\n+#include \"pathspec.h\"\n \n struct path_simplify {\n \tint len;\ndiff --git a/pathspec.c b/pathspec.c\nindex 284f397..133f8be 100644\n--- a/pathspec.c\n+++ b/pathspec.c\n@@ -99,3 +99,153 @@ void die_if_path_beyond_symlink(const char *path, const char *prefix)\n \t\tdie(_(\"'%s' is beyond a symbolic link\"), path + len);\n \t}\n }\n+\n+/*\n+ * Magic pathspec\n+ *\n+ * NEEDSWORK: These need to be moved to dir.h or even to a new\n+ * pathspec.h when we restructure get_pathspec() users to use the\n+ * \"struct pathspec\" interface.\n+ *\n+ * Possible future magic semantics include stuff like:\n+ *\n+ *\t{ PATHSPEC_NOGLOB, '!', \"noglob\" },\n+ *\t{ PATHSPEC_ICASE, '\\0', \"icase\" },\n+ *\t{ PATHSPEC_RECURSIVE, '*', \"recursive\" },\n+ *\t{ PATHSPEC_REGEXP, '\\0', \"regexp\" },\n+ *\n+ */\n+#define PATHSPEC_FROMTOP    (1<<0)\n+\n+static struct pathspec_magic {\n+\tunsigned bit;\n+\tchar mnemonic; /* this cannot be ':'! */\n+\tconst char *name;\n+} pathspec_magic[] = {\n+\t{ PATHSPEC_FROMTOP, '/', \"top\" },\n+};\n+\n+/*\n+ * Take an element of a pathspec and check for magic signatures.\n+ * Append the result to the prefix.\n+ *\n+ * For now, we only parse the syntax and throw out anything other than\n+ * \"top\" magic.\n+ *\n+ * NEEDSWORK: This needs to be rewritten when we start migrating\n+ * get_pathspec() users to use the \"struct pathspec\" interface.  For\n+ * example, a pathspec element may be marked as case-insensitive, but\n+ * the prefix part must always match literally, and a single stupid\n+ * string cannot express such a case.\n+ */\n+static const char *prefix_pathspec(const char *prefix, int prefixlen, const char *elt)\n+{\n+\tunsigned magic = 0;\n+\tconst char *copyfrom = elt;\n+\tint i;\n+\n+\tif (elt[0] != ':') {\n+\t\t; /* nothing to do */\n+\t} else if (elt[1] == '(') {\n+\t\t/* longhand */\n+\t\tconst char *nextat;\n+\t\tfor (copyfrom = elt + 2;\n+\t\t     *copyfrom && *copyfrom != ')';\n+\t\t     copyfrom = nextat) {\n+\t\t\tsize_t len = strcspn(copyfrom, \",)\");\n+\t\t\tif (copyfrom[len] == ',')\n+\t\t\t\tnextat = copyfrom + len + 1;\n+\t\t\telse\n+\t\t\t\t/* handle ')' and '\\0' */\n+\t\t\t\tnextat = copyfrom + len;\n+\t\t\tif (!len)\n+\t\t\t\tcontinue;\n+\t\t\tfor (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)\n+\t\t\t\tif (strlen(pathspec_magic[i].name) == len &&\n+\t\t\t\t    !strncmp(pathspec_magic[i].name, copyfrom, len)) {\n+\t\t\t\t\tmagic |= pathspec_magic[i].bit;\n+\t\t\t\t\tbreak;\n+\t\t\t\t}\n+\t\t\tif (ARRAY_SIZE(pathspec_magic) <= i)\n+\t\t\t\tdie(\"Invalid pathspec magic '%.*s' in '%s'\",\n+\t\t\t\t    (int) len, copyfrom, elt);\n+\t\t}\n+\t\tif (*copyfrom != ')')\n+\t\t\tdie(\"Missing ')' at the end of pathspec magic in '%s'\", elt);\n+\t\tcopyfrom++;\n+\t} else {\n+\t\t/* shorthand */\n+\t\tfor (copyfrom = elt + 1;\n+\t\t     *copyfrom && *copyfrom != ':';\n+\t\t     copyfrom++) {\n+\t\t\tchar ch = *copyfrom;\n+\n+\t\t\tif (!is_pathspec_magic(ch))\n+\t\t\t\tbreak;\n+\t\t\tfor (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)\n+\t\t\t\tif (pathspec_magic[i].mnemonic == ch) {\n+\t\t\t\t\tmagic |= pathspec_magic[i].bit;\n+\t\t\t\t\tbreak;\n+\t\t\t\t}\n+\t\t\tif (ARRAY_SIZE(pathspec_magic) <= i)\n+\t\t\t\tdie(\"Unimplemented pathspec magic '%c' in '%s'\",\n+\t\t\t\t    ch, elt);\n+\t\t}\n+\t\tif (*copyfrom == ':')\n+\t\t\tcopyfrom++;\n+\t}\n+\n+\tif (magic & PATHSPEC_FROMTOP)\n+\t\treturn xstrdup(copyfrom);\n+\telse\n+\t\treturn prefix_path(prefix, prefixlen, copyfrom);\n+}\n+\n+/*\n+ * N.B. get_pathspec() is deprecated in favor of the \"struct pathspec\"\n+ * based interface - see pathspec_magic above.\n+ *\n+ * Arguments:\n+ *  - prefix - a path relative to the root of the working tree\n+ *  - pathspec - a list of paths underneath the prefix path\n+ *\n+ * Iterates over pathspec, prepending each path with prefix,\n+ * and return the resulting list.\n+ *\n+ * If pathspec is empty, return a singleton list containing prefix.\n+ *\n+ * If pathspec and prefix are both empty, return an empty list.\n+ *\n+ * This is typically used by built-in commands such as add.c, in order\n+ * to normalize argv arguments provided to the built-in into a list of\n+ * paths to process, all relative to the root of the working tree.\n+ */\n+const char **get_pathspec(const char *prefix, const char **pathspec)\n+{\n+\tconst char *entry = *pathspec;\n+\tconst char **src, **dst;\n+\tint prefixlen;\n+\n+\tif (!prefix && !entry)\n+\t\treturn NULL;\n+\n+\tif (!entry) {\n+\t\tstatic const char *spec[2];\n+\t\tspec[0] = prefix;\n+\t\tspec[1] = NULL;\n+\t\treturn spec;\n+\t}\n+\n+\t/* Otherwise we have to re-write the entries.. */\n+\tsrc = pathspec;\n+\tdst = pathspec;\n+\tprefixlen = prefix ? strlen(prefix) : 0;\n+\twhile (*src) {\n+\t\t*(dst++) = prefix_pathspec(prefix, prefixlen, *src);\n+\t\tsrc++;\n+\t}\n+\t*dst = NULL;\n+\tif (!*pathspec)\n+\t\treturn NULL;\n+\treturn pathspec;\n+}\ndiff --git a/pathspec.h b/pathspec.h\nindex db0184a..7884068 100644\n--- a/pathspec.h\n+++ b/pathspec.h\n@@ -1,6 +1,27 @@\n #ifndef PATHSPEC_H\n #define PATHSPEC_H\n \n+#define PATHSPEC_ONESTAR 1\t/* the pathspec pattern sastisfies GFNM_ONESTAR */\n+\n+struct pathspec {\n+\tconst char **raw; /* get_pathspec() result, not freed by free_pathspec() */\n+\tint nr;\n+\tunsigned int has_wildcard:1;\n+\tunsigned int recursive:1;\n+\tint max_depth;\n+\tstruct pathspec_item {\n+\t\tconst char *match;\n+\t\tint len;\n+\t\tint nowildcard_len;\n+\t\tint flags;\n+\t} *items;\n+};\n+\n+extern int init_pathspec(struct pathspec *, const char **);\n+extern void free_pathspec(struct pathspec *);\n+\n+extern int limit_pathspec_to_literal(void);\n+\n extern char *find_pathspecs_matching_against_index(const char **pathspec);\n extern void add_pathspec_matches_against_index(const char **pathspec, char *seen, int specs);\n extern const char *check_path_for_gitlink(const char *path);\ndiff --git a/preload-index.c b/preload-index.c\nindex 49cb08d..cddfffa 100644\n--- a/preload-index.c\n+++ b/preload-index.c\n@@ -2,6 +2,7 @@\n  * Copyright (C) 2008 Linus Torvalds\n  */\n #include \"cache.h\"\n+#include \"pathspec.h\"\n \n #ifdef NO_PTHREADS\n static void preload_index(struct index_state *index, const char **pathspec)\ndiff --git a/setup.c b/setup.c\nindex 94c1e61..d1ece5d 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -154,155 +154,6 @@ void verify_non_filename(const char *prefix, const char *arg)\n \t    \"'git <command> [<revision>...] -- [<file>...]'\", arg);\n }\n \n-/*\n- * Magic pathspec\n- *\n- * NEEDSWORK: These need to be moved to dir.h or even to a new\n- * pathspec.h when we restructure get_pathspec() users to use the\n- * \"struct pathspec\" interface.\n- *\n- * Possible future magic semantics include stuff like:\n- *\n- *\t{ PATHSPEC_NOGLOB, '!', \"noglob\" },\n- *\t{ PATHSPEC_ICASE, '\\0', \"icase\" },\n- *\t{ PATHSPEC_RECURSIVE, '*', \"recursive\" },\n- *\t{ PATHSPEC_REGEXP, '\\0', \"regexp\" },\n- *\n- */\n-#define PATHSPEC_FROMTOP    (1<<0)\n-\n-static struct pathspec_magic {\n-\tunsigned bit;\n-\tchar mnemonic; /* this cannot be ':'! */\n-\tconst char *name;\n-} pathspec_magic[] = {\n-\t{ PATHSPEC_FROMTOP, '/', \"top\" },\n-};\n-\n-/*\n- * Take an element of a pathspec and check for magic signatures.\n- * Append the result to the prefix.\n- *\n- * For now, we only parse the syntax and throw out anything other than\n- * \"top\" magic.\n- *\n- * NEEDSWORK: This needs to be rewritten when we start migrating\n- * get_pathspec() users to use the \"struct pathspec\" interface.  For\n- * example, a pathspec element may be marked as case-insensitive, but\n- * the prefix part must always match literally, and a single stupid\n- * string cannot express such a case.\n- */\n-static const char *prefix_pathspec(const char *prefix, int prefixlen, const char *elt)\n-{\n-\tunsigned magic = 0;\n-\tconst char *copyfrom = elt;\n-\tint i;\n-\n-\tif (elt[0] != ':') {\n-\t\t; /* nothing to do */\n-\t} else if (elt[1] == '(') {\n-\t\t/* longhand */\n-\t\tconst char *nextat;\n-\t\tfor (copyfrom = elt + 2;\n-\t\t     *copyfrom && *copyfrom != ')';\n-\t\t     copyfrom = nextat) {\n-\t\t\tsize_t len = strcspn(copyfrom, \",)\");\n-\t\t\tif (copyfrom[len] == ',')\n-\t\t\t\tnextat = copyfrom + len + 1;\n-\t\t\telse\n-\t\t\t\t/* handle ')' and '\\0' */\n-\t\t\t\tnextat = copyfrom + len;\n-\t\t\tif (!len)\n-\t\t\t\tcontinue;\n-\t\t\tfor (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)\n-\t\t\t\tif (strlen(pathspec_magic[i].name) == len &&\n-\t\t\t\t    !strncmp(pathspec_magic[i].name, copyfrom, len)) {\n-\t\t\t\t\tmagic |= pathspec_magic[i].bit;\n-\t\t\t\t\tbreak;\n-\t\t\t\t}\n-\t\t\tif (ARRAY_SIZE(pathspec_magic) <= i)\n-\t\t\t\tdie(\"Invalid pathspec magic '%.*s' in '%s'\",\n-\t\t\t\t    (int) len, copyfrom, elt);\n-\t\t}\n-\t\tif (*copyfrom != ')')\n-\t\t\tdie(\"Missing ')' at the end of pathspec magic in '%s'\", elt);\n-\t\tcopyfrom++;\n-\t} else {\n-\t\t/* shorthand */\n-\t\tfor (copyfrom = elt + 1;\n-\t\t     *copyfrom && *copyfrom != ':';\n-\t\t     copyfrom++) {\n-\t\t\tchar ch = *copyfrom;\n-\n-\t\t\tif (!is_pathspec_magic(ch))\n-\t\t\t\tbreak;\n-\t\t\tfor (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)\n-\t\t\t\tif (pathspec_magic[i].mnemonic == ch) {\n-\t\t\t\t\tmagic |= pathspec_magic[i].bit;\n-\t\t\t\t\tbreak;\n-\t\t\t\t}\n-\t\t\tif (ARRAY_SIZE(pathspec_magic) <= i)\n-\t\t\t\tdie(\"Unimplemented pathspec magic '%c' in '%s'\",\n-\t\t\t\t    ch, elt);\n-\t\t}\n-\t\tif (*copyfrom == ':')\n-\t\t\tcopyfrom++;\n-\t}\n-\n-\tif (magic & PATHSPEC_FROMTOP)\n-\t\treturn xstrdup(copyfrom);\n-\telse\n-\t\treturn prefix_path(prefix, prefixlen, copyfrom);\n-}\n-\n-/*\n- * N.B. get_pathspec() is deprecated in favor of the \"struct pathspec\"\n- * based interface - see pathspec_magic above.\n- *\n- * Arguments:\n- *  - prefix - a path relative to the root of the working tree\n- *  - pathspec - a list of paths underneath the prefix path\n- *\n- * Iterates over pathspec, prepending each path with prefix,\n- * and return the resulting list.\n- *\n- * If pathspec is empty, return a singleton list containing prefix.\n- *\n- * If pathspec and prefix are both empty, return an empty list.\n- *\n- * This is typically used by built-in commands such as add.c, in order\n- * to normalize argv arguments provided to the built-in into a list of\n- * paths to process, all relative to the root of the working tree.\n- */\n-const char **get_pathspec(const char *prefix, const char **pathspec)\n-{\n-\tconst char *entry = *pathspec;\n-\tconst char **src, **dst;\n-\tint prefixlen;\n-\n-\tif (!prefix && !entry)\n-\t\treturn NULL;\n-\n-\tif (!entry) {\n-\t\tstatic const char *spec[2];\n-\t\tspec[0] = prefix;\n-\t\tspec[1] = NULL;\n-\t\treturn spec;\n-\t}\n-\n-\t/* Otherwise we have to re-write the entries.. */\n-\tsrc = pathspec;\n-\tdst = pathspec;\n-\tprefixlen = prefix ? strlen(prefix) : 0;\n-\twhile (*src) {\n-\t\t*(dst++) = prefix_pathspec(prefix, prefixlen, *src);\n-\t\tsrc++;\n-\t}\n-\t*dst = NULL;\n-\tif (!*pathspec)\n-\t\treturn NULL;\n-\treturn pathspec;\n-}\n \n /*\n  * Test if it looks like we're at a git directory.\ndiff --git a/tree-walk.c b/tree-walk.c\nindex 6e30ef9..72a9613 100644\n--- a/tree-walk.c\n+++ b/tree-walk.c\n@@ -3,6 +3,7 @@\n #include \"unpack-trees.h\"\n #include \"dir.h\"\n #include \"tree.h\"\n+#include \"pathspec.h\"\n \n static const char *get_mode(const char *str, unsigned int *modep)\n {\n-- \n1.8.2.83.gc99314b\n"},{"id":"223276","messageId":"1373790969-13000-4-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 03/46] pathspec: i18n-ize error strings in pathspec parsing code","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:35:26Z","receivedAt":"2013-07-14T08:35:26Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n pathspec.c | 6 +++---\n 1 file changed, 3 insertions(+), 3 deletions(-)\n\ndiff --git a/pathspec.c b/pathspec.c\nindex 133f8be..403095b 100644\n--- a/pathspec.c\n+++ b/pathspec.c\n@@ -167,11 +167,11 @@ static const char *prefix_pathspec(const char *prefix, int prefixlen, const char\n \t\t\t\t\tbreak;\n \t\t\t\t}\n \t\t\tif (ARRAY_SIZE(pathspec_magic) <= i)\n-\t\t\t\tdie(\"Invalid pathspec magic '%.*s' in '%s'\",\n+\t\t\t\tdie(_(\"Invalid pathspec magic '%.*s' in '%s'\"),\n \t\t\t\t    (int) len, copyfrom, elt);\n \t\t}\n \t\tif (*copyfrom != ')')\n-\t\t\tdie(\"Missing ')' at the end of pathspec magic in '%s'\", elt);\n+\t\t\tdie(_(\"Missing ')' at the end of pathspec magic in '%s'\"), elt);\n \t\tcopyfrom++;\n \t} else {\n \t\t/* shorthand */\n@@ -188,7 +188,7 @@ static const char *prefix_pathspec(const char *prefix, int prefixlen, const char\n \t\t\t\t\tbreak;\n \t\t\t\t}\n \t\t\tif (ARRAY_SIZE(pathspec_magic) <= i)\n-\t\t\t\tdie(\"Unimplemented pathspec magic '%c' in '%s'\",\n+\t\t\t\tdie(_(\"Unimplemented pathspec magic '%c' in '%s'\"),\n \t\t\t\t    ch, elt);\n \t\t}\n \t\tif (*copyfrom == ':')\n-- \n1.8.2.83.gc99314b\n"},{"id":"223277","messageId":"1373790969-13000-5-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 04/46] pathspec: add copy_pathspec","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:35:27Z","receivedAt":"2013-07-14T08:35:27Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Because free_pathspec wants to free \"items\" pointer in the pathspec\nstructure, a simple structure assignment is not enough if you want to\ncopy an existing pathspec into another.  Freeing the original will\ndamage the copy unless a deep copy is made.\n\nNote that the strings in pathspec->items->match and the array\npathspec->raw[] are still shared between the original and the copy.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n builtin/mv.c | 13 +++++++------\n pathspec.c   |  8 ++++++++\n pathspec.h   |  1 +\n 3 files changed, 16 insertions(+), 6 deletions(-)\n\ndiff --git a/builtin/mv.c b/builtin/mv.c\nindex 034fec9..16ce99b 100644\n--- a/builtin/mv.c\n+++ b/builtin/mv.c\n@@ -15,8 +15,9 @@ static const char * const builtin_mv_usage[] = {\n \tNULL\n };\n \n-static const char **copy_pathspec(const char *prefix, const char **pathspec,\n-\t\t\t\t  int count, int base_name)\n+static const char **internal_copy_pathspec(const char *prefix,\n+\t\t\t\t\t   const char **pathspec,\n+\t\t\t\t\t   int count, int base_name)\n {\n \tint i;\n \tconst char **result = xmalloc((count + 1) * sizeof(const char *));\n@@ -81,17 +82,17 @@ int cmd_mv(int argc, const char **argv, const char *prefix)\n \tif (read_cache() < 0)\n \t\tdie(_(\"index file corrupt\"));\n \n-\tsource = copy_pathspec(prefix, argv, argc, 0);\n+\tsource = internal_copy_pathspec(prefix, argv, argc, 0);\n \tmodes = xcalloc(argc, sizeof(enum update_mode));\n-\tdest_path = copy_pathspec(prefix, argv + argc, 1, 0);\n+\tdest_path = internal_copy_pathspec(prefix, argv + argc, 1, 0);\n \n \tif (dest_path[0][0] == '\\0')\n \t\t/* special case: \".\" was normalized to \"\" */\n-\t\tdestination = copy_pathspec(dest_path[0], argv, argc, 1);\n+\t\tdestination = internal_copy_pathspec(dest_path[0], argv, argc, 1);\n \telse if (!lstat(dest_path[0], &st) &&\n \t\t\tS_ISDIR(st.st_mode)) {\n \t\tdest_path[0] = add_slash(dest_path[0]);\n-\t\tdestination = copy_pathspec(dest_path[0], argv, argc, 1);\n+\t\tdestination = internal_copy_pathspec(dest_path[0], argv, argc, 1);\n \t} else {\n \t\tif (argc != 1)\n \t\t\tdie(\"destination '%s' is not a directory\", dest_path[0]);\ndiff --git a/pathspec.c b/pathspec.c\nindex 403095b..8fe56cd 100644\n--- a/pathspec.c\n+++ b/pathspec.c\n@@ -249,3 +249,11 @@ const char **get_pathspec(const char *prefix, const char **pathspec)\n \t\treturn NULL;\n \treturn pathspec;\n }\n+\n+void copy_pathspec(struct pathspec *dst, const struct pathspec *src)\n+{\n+\t*dst = *src;\n+\tdst->items = xmalloc(sizeof(struct pathspec_item) * dst->nr);\n+\tmemcpy(dst->items, src->items,\n+\t       sizeof(struct pathspec_item) * dst->nr);\n+}\ndiff --git a/pathspec.h b/pathspec.h\nindex 7884068..a621676 100644\n--- a/pathspec.h\n+++ b/pathspec.h\n@@ -18,6 +18,7 @@ struct pathspec {\n };\n \n extern int init_pathspec(struct pathspec *, const char **);\n+extern void copy_pathspec(struct pathspec *dst, const struct pathspec *src);\n extern void free_pathspec(struct pathspec *);\n \n extern int limit_pathspec_to_literal(void);\n-- \n1.8.2.83.gc99314b\n"},{"id":"223278","messageId":"1373790969-13000-6-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 05/46] Add parse_pathspec() that converts cmdline args to struct pathspec","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:35:28Z","receivedAt":"2013-07-14T08:35:28Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Currently to fill a struct pathspec, we do:\n\n   const char **paths;\n   paths = get_pathspec(prefix, argv);\n   ...\n   init_pathspec(&pathspec, paths);\n\n\"paths\" can only carry bare strings, which loses information from\ncommand line arguments such as pathspec magic or the prefix part's\nlength for each argument.\n\nparse_pathspec() is introduced to combine the two calls into one. The\nplan is gradually replace all get_pathspec() and init_pathspec() with\nparse_pathspec(). get_pathspec() now becomes a thin wrapper of\nparse_pathspec().\n\nparse_pathspec() allows the caller to reject the pathspec magics that\nit does not support. When a new pathspec magic is introduced, we can\nenable it per command after making sure that all underlying code has no\nproblem with the new magic.\n\n\"flags\" parameter is currently unused. But it would allow callers to\npass certain instructions to parse_pathspec, for example forcing\nliteral pathspec when no magic is used.\n\nWith the introduction of parse_pathspec, there are now two functions\nthat can initialize struct pathspec: init_pathspec and\nparse_pathspec. Any semantic changes in struct pathspec must be\nreflected in both functions. init_pathspec() will be phased out in\nfavor of parse_pathspec().\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n Documentation/technical/api-setup.txt |  19 +++-\n dir.c                                 |   4 +-\n dir.h                                 |   2 +\n pathspec.c                            | 168 ++++++++++++++++++++++++++--------\n pathspec.h                            |  11 +++\n 5 files changed, 163 insertions(+), 41 deletions(-)\n\ndiff --git a/Documentation/technical/api-setup.txt b/Documentation/technical/api-setup.txt\nindex 4f63a04..90d1aff 100644\n--- a/Documentation/technical/api-setup.txt\n+++ b/Documentation/technical/api-setup.txt\n@@ -8,6 +8,23 @@ Talk about\n * is_inside_git_dir()\n * is_inside_work_tree()\n * setup_work_tree()\n-* get_pathspec()\n \n (Dscho)\n+\n+Pathspec\n+--------\n+\n+See glossary-context.txt for the syntax of pathspec. In memory, a\n+pathspec set is represented by \"struct pathspec\" and is prepared by\n+parse_pathspec(). This function takes several arguments:\n+\n+- magic_mask specifies what features that are NOT supported by the\n+  following code. If a user attempts to use such a feature,\n+  parse_pathspec() can reject it early.\n+\n+- flags specifies other things that the caller wants parse_pathspec to\n+  perform.\n+\n+- prefix and args come from cmd_* functions\n+\n+get_pathspec() is obsolete and should never be used in new code.\ndiff --git a/dir.c b/dir.c\nindex b0599dd..5f86e46 100644\n--- a/dir.c\n+++ b/dir.c\n@@ -381,7 +381,7 @@ int match_pathspec_depth(const struct pathspec *ps,\n /*\n  * Return the length of the \"simple\" part of a path match limiter.\n  */\n-static int simple_length(const char *match)\n+int simple_length(const char *match)\n {\n \tint len = -1;\n \n@@ -393,7 +393,7 @@ static int simple_length(const char *match)\n \t}\n }\n \n-static int no_wildcard(const char *string)\n+int no_wildcard(const char *string)\n {\n \treturn string[simple_length(string)] == '\\0';\n }\ndiff --git a/dir.h b/dir.h\nindex 3d6b80c..229ccc8 100644\n--- a/dir.h\n+++ b/dir.h\n@@ -128,6 +128,8 @@ struct dir_struct {\n #define MATCHED_RECURSIVELY 1\n #define MATCHED_FNMATCH 2\n #define MATCHED_EXACTLY 3\n+extern int simple_length(const char *match);\n+extern int no_wildcard(const char *string);\n extern char *common_prefix(const char **pathspec);\n extern int match_pathspec(const char **pathspec, const char *name, int namelen, int prefix, char *seen);\n extern int match_pathspec_depth(const struct pathspec *pathspec,\ndiff --git a/pathspec.c b/pathspec.c\nindex 8fe56cd..ce942db 100644\n--- a/pathspec.c\n+++ b/pathspec.c\n@@ -103,10 +103,6 @@ void die_if_path_beyond_symlink(const char *path, const char *prefix)\n /*\n  * Magic pathspec\n  *\n- * NEEDSWORK: These need to be moved to dir.h or even to a new\n- * pathspec.h when we restructure get_pathspec() users to use the\n- * \"struct pathspec\" interface.\n- *\n  * Possible future magic semantics include stuff like:\n  *\n  *\t{ PATHSPEC_NOGLOB, '!', \"noglob\" },\n@@ -115,7 +111,6 @@ void die_if_path_beyond_symlink(const char *path, const char *prefix)\n  *\t{ PATHSPEC_REGEXP, '\\0', \"regexp\" },\n  *\n  */\n-#define PATHSPEC_FROMTOP    (1<<0)\n \n static struct pathspec_magic {\n \tunsigned bit;\n@@ -127,7 +122,7 @@ static struct pathspec_magic {\n \n /*\n  * Take an element of a pathspec and check for magic signatures.\n- * Append the result to the prefix.\n+ * Append the result to the prefix. Return the magic bitmap.\n  *\n  * For now, we only parse the syntax and throw out anything other than\n  * \"top\" magic.\n@@ -138,10 +133,15 @@ static struct pathspec_magic {\n  * the prefix part must always match literally, and a single stupid\n  * string cannot express such a case.\n  */\n-static const char *prefix_pathspec(const char *prefix, int prefixlen, const char *elt)\n+static unsigned prefix_pathspec(struct pathspec_item *item,\n+\t\t\t\tunsigned *p_short_magic,\n+\t\t\t\tconst char **raw, unsigned flags,\n+\t\t\t\tconst char *prefix, int prefixlen,\n+\t\t\t\tconst char *elt)\n {\n-\tunsigned magic = 0;\n+\tunsigned magic = 0, short_magic = 0;\n \tconst char *copyfrom = elt;\n+\tchar *match;\n \tint i;\n \n \tif (elt[0] != ':') {\n@@ -184,7 +184,7 @@ static const char *prefix_pathspec(const char *prefix, int prefixlen, const char\n \t\t\t\tbreak;\n \t\t\tfor (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)\n \t\t\t\tif (pathspec_magic[i].mnemonic == ch) {\n-\t\t\t\t\tmagic |= pathspec_magic[i].bit;\n+\t\t\t\t\tshort_magic |= pathspec_magic[i].bit;\n \t\t\t\t\tbreak;\n \t\t\t\t}\n \t\t\tif (ARRAY_SIZE(pathspec_magic) <= i)\n@@ -195,15 +195,128 @@ static const char *prefix_pathspec(const char *prefix, int prefixlen, const char\n \t\t\tcopyfrom++;\n \t}\n \n+\tmagic |= short_magic;\n+\t*p_short_magic = short_magic;\n+\n \tif (magic & PATHSPEC_FROMTOP)\n-\t\treturn xstrdup(copyfrom);\n+\t\tmatch = xstrdup(copyfrom);\n \telse\n-\t\treturn prefix_path(prefix, prefixlen, copyfrom);\n+\t\tmatch = prefix_path(prefix, prefixlen, copyfrom);\n+\t*raw = item->match = match;\n+\titem->len = strlen(item->match);\n+\tif (limit_pathspec_to_literal())\n+\t\titem->nowildcard_len = item->len;\n+\telse\n+\t\titem->nowildcard_len = simple_length(item->match);\n+\titem->flags = 0;\n+\tif (item->nowildcard_len < item->len &&\n+\t    item->match[item->nowildcard_len] == '*' &&\n+\t    no_wildcard(item->match + item->nowildcard_len + 1))\n+\t\titem->flags |= PATHSPEC_ONESTAR;\n+\treturn magic;\n+}\n+\n+static int pathspec_item_cmp(const void *a_, const void *b_)\n+{\n+\tstruct pathspec_item *a, *b;\n+\n+\ta = (struct pathspec_item *)a_;\n+\tb = (struct pathspec_item *)b_;\n+\treturn strcmp(a->match, b->match);\n+}\n+\n+static void NORETURN unsupported_magic(const char *pattern,\n+\t\t\t\t       unsigned magic,\n+\t\t\t\t       unsigned short_magic)\n+{\n+\tstruct strbuf sb = STRBUF_INIT;\n+\tint i, n;\n+\tfor (n = i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {\n+\t\tconst struct pathspec_magic *m = pathspec_magic + i;\n+\t\tif (!(magic & m->bit))\n+\t\t\tcontinue;\n+\t\tif (sb.len)\n+\t\t\tstrbuf_addstr(&sb, \" \");\n+\t\tif (short_magic & m->bit)\n+\t\t\tstrbuf_addf(&sb, \"'%c'\", m->mnemonic);\n+\t\telse\n+\t\t\tstrbuf_addf(&sb, \"'%s'\", m->name);\n+\t\tn++;\n+\t}\n+\t/*\n+\t * We may want to substitute \"this command\" with a command\n+\t * name. E.g. when add--interactive dies when running\n+\t * \"checkout -p\"\n+\t */\n+\tdie(_(\"%s: pathspec magic not supported by this command: %s\"),\n+\t    pattern, sb.buf);\n+}\n+\n+/*\n+ * Given command line arguments and a prefix, convert the input to\n+ * pathspec. die() if any magic in magic_mask is used.\n+ */\n+void parse_pathspec(struct pathspec *pathspec,\n+\t\t    unsigned magic_mask, unsigned flags,\n+\t\t    const char *prefix, const char **argv)\n+{\n+\tstruct pathspec_item *item;\n+\tconst char *entry = argv ? *argv : NULL;\n+\tint i, n, prefixlen;\n+\n+\tmemset(pathspec, 0, sizeof(*pathspec));\n+\n+\t/* No arguments, no prefix -> no pathspec */\n+\tif (!entry && !prefix)\n+\t\treturn;\n+\n+\t/* No arguments with prefix -> prefix pathspec */\n+\tif (!entry) {\n+\t\tstatic const char *raw[2];\n+\n+\t\tpathspec->items = item = xmalloc(sizeof(*item));\n+\t\tmemset(item, 0, sizeof(*item));\n+\t\titem->match = prefix;\n+\t\titem->nowildcard_len = item->len = strlen(prefix);\n+\t\traw[0] = prefix;\n+\t\traw[1] = NULL;\n+\t\tpathspec->nr = 1;\n+\t\tpathspec->raw = raw;\n+\t\treturn;\n+\t}\n+\n+\tn = 0;\n+\twhile (argv[n])\n+\t\tn++;\n+\n+\tpathspec->nr = n;\n+\tpathspec->items = item = xmalloc(sizeof(*item) * n);\n+\tpathspec->raw = argv;\n+\tprefixlen = prefix ? strlen(prefix) : 0;\n+\n+\tfor (i = 0; i < n; i++) {\n+\t\tunsigned short_magic;\n+\t\tentry = argv[i];\n+\n+\t\titem[i].magic = prefix_pathspec(item + i, &short_magic,\n+\t\t\t\t\t\targv + i, flags,\n+\t\t\t\t\t\tprefix, prefixlen, entry);\n+\t\tif (item[i].magic & magic_mask)\n+\t\t\tunsupported_magic(entry,\n+\t\t\t\t\t  item[i].magic & magic_mask,\n+\t\t\t\t\t  short_magic);\n+\t\tif (item[i].nowildcard_len < item[i].len)\n+\t\t\tpathspec->has_wildcard = 1;\n+\t\tpathspec->magic |= item[i].magic;\n+\t}\n+\n+\tqsort(pathspec->items, pathspec->nr,\n+\t      sizeof(struct pathspec_item), pathspec_item_cmp);\n }\n \n /*\n  * N.B. get_pathspec() is deprecated in favor of the \"struct pathspec\"\n- * based interface - see pathspec_magic above.\n+ * based interface - see pathspec.c:parse_pathspec().\n  *\n  * Arguments:\n  *  - prefix - a path relative to the root of the working tree\n@@ -222,32 +335,11 @@ static const char *prefix_pathspec(const char *prefix, int prefixlen, const char\n  */\n const char **get_pathspec(const char *prefix, const char **pathspec)\n {\n-\tconst char *entry = *pathspec;\n-\tconst char **src, **dst;\n-\tint prefixlen;\n-\n-\tif (!prefix && !entry)\n-\t\treturn NULL;\n-\n-\tif (!entry) {\n-\t\tstatic const char *spec[2];\n-\t\tspec[0] = prefix;\n-\t\tspec[1] = NULL;\n-\t\treturn spec;\n-\t}\n-\n-\t/* Otherwise we have to re-write the entries.. */\n-\tsrc = pathspec;\n-\tdst = pathspec;\n-\tprefixlen = prefix ? strlen(prefix) : 0;\n-\twhile (*src) {\n-\t\t*(dst++) = prefix_pathspec(prefix, prefixlen, *src);\n-\t\tsrc++;\n-\t}\n-\t*dst = NULL;\n-\tif (!*pathspec)\n-\t\treturn NULL;\n-\treturn pathspec;\n+\tstruct pathspec ps;\n+\tparse_pathspec(&ps,\n+\t\t       PATHSPEC_ALL_MAGIC & ~PATHSPEC_FROMTOP,\n+\t\t       0, prefix, pathspec);\n+\treturn ps.raw;\n }\n \n void copy_pathspec(struct pathspec *dst, const struct pathspec *src)\ndiff --git a/pathspec.h b/pathspec.h\nindex a621676..937ec91 100644\n--- a/pathspec.h\n+++ b/pathspec.h\n@@ -1,6 +1,10 @@\n #ifndef PATHSPEC_H\n #define PATHSPEC_H\n \n+/* Pathspec magic */\n+#define PATHSPEC_FROMTOP\t(1<<0)\n+#define PATHSPEC_ALL_MAGIC PATHSPEC_FROMTOP\n+\n #define PATHSPEC_ONESTAR 1\t/* the pathspec pattern sastisfies GFNM_ONESTAR */\n \n struct pathspec {\n@@ -8,9 +12,11 @@ struct pathspec {\n \tint nr;\n \tunsigned int has_wildcard:1;\n \tunsigned int recursive:1;\n+\tunsigned magic;\n \tint max_depth;\n \tstruct pathspec_item {\n \t\tconst char *match;\n+\t\tunsigned magic;\n \t\tint len;\n \t\tint nowildcard_len;\n \t\tint flags;\n@@ -18,6 +24,11 @@ struct pathspec {\n };\n \n extern int init_pathspec(struct pathspec *, const char **);\n+extern void parse_pathspec(struct pathspec *pathspec,\n+\t\t\t   unsigned magic_mask,\n+\t\t\t   unsigned flags,\n+\t\t\t   const char *prefix,\n+\t\t\t   const char **args);\n extern void copy_pathspec(struct pathspec *dst, const struct pathspec *src);\n extern void free_pathspec(struct pathspec *);\n \n-- \n1.8.2.83.gc99314b\n"},{"id":"223279","messageId":"1373790969-13000-7-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 06/46] parse_pathspec: save original pathspec for reporting","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:35:29Z","receivedAt":"2013-07-14T08:35:29Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"We usually use pathspec_item's match field for pathspec error\nreporting. However \"match\" (or \"raw\") does not show the magic part,\nwhich will play more important role later on. Preserve exact user\ninput for reporting.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n dir.c      | 1 +\n pathspec.c | 2 ++\n pathspec.h | 1 +\n 3 files changed, 4 insertions(+)\n\ndiff --git a/dir.c b/dir.c\nindex 5f86e46..308028e 100644\n--- a/dir.c\n+++ b/dir.c\n@@ -1599,6 +1599,7 @@ int init_pathspec(struct pathspec *pathspec, const char **paths)\n \t\tconst char *path = paths[i];\n \n \t\titem->match = path;\n+\t\titem->original = path;\n \t\titem->len = strlen(path);\n \t\titem->flags = 0;\n \t\tif (limit_pathspec_to_literal()) {\ndiff --git a/pathspec.c b/pathspec.c\nindex ce942db..f94beb6 100644\n--- a/pathspec.c\n+++ b/pathspec.c\n@@ -203,6 +203,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item,\n \telse\n \t\tmatch = prefix_path(prefix, prefixlen, copyfrom);\n \t*raw = item->match = match;\n+\titem->original = elt;\n \titem->len = strlen(item->match);\n \tif (limit_pathspec_to_literal())\n \t\titem->nowildcard_len = item->len;\n@@ -277,6 +278,7 @@ void parse_pathspec(struct pathspec *pathspec,\n \t\tpathspec->items = item = xmalloc(sizeof(*item));\n \t\tmemset(item, 0, sizeof(*item));\n \t\titem->match = prefix;\n+\t\titem->original = prefix;\n \t\titem->nowildcard_len = item->len = strlen(prefix);\n \t\traw[0] = prefix;\n \t\traw[1] = NULL;\ndiff --git a/pathspec.h b/pathspec.h\nindex 937ec91..cc5841b 100644\n--- a/pathspec.h\n+++ b/pathspec.h\n@@ -16,6 +16,7 @@ struct pathspec {\n \tint max_depth;\n \tstruct pathspec_item {\n \t\tconst char *match;\n+\t\tconst char *original;\n \t\tunsigned magic;\n \t\tint len;\n \t\tint nowildcard_len;\n-- \n1.8.2.83.gc99314b\n"},{"id":"223280","messageId":"1373790969-13000-8-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 07/46] parse_pathspec: add PATHSPEC_PREFER_{CWD,FULL}","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:35:30Z","receivedAt":"2013-07-14T08:35:30Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"We have two ways of dealing with empty pathspec:\n\n1. limit it to current prefix\n2. match the entire working directory\n\nSome commands go with #1, some #2. get_pathspec() and parse_pathspec()\nonly support #1. Make parse_pathspec() reject empty pathspec by\ndefault. #1 and #2 can be specified via new flags. This makes it more\nexpressive about default behavior at command level.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n pathspec.c | 13 ++++++++++++-\n pathspec.h |  4 ++++\n 2 files changed, 16 insertions(+), 1 deletion(-)\n\ndiff --git a/pathspec.c b/pathspec.c\nindex f94beb6..6d99a3d 100644\n--- a/pathspec.c\n+++ b/pathspec.c\n@@ -271,10 +271,20 @@ void parse_pathspec(struct pathspec *pathspec,\n \tif (!entry && !prefix)\n \t\treturn;\n \n+\tif ((flags & PATHSPEC_PREFER_CWD) &&\n+\t    (flags & PATHSPEC_PREFER_FULL))\n+\t\tdie(\"BUG: PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible\");\n+\n \t/* No arguments with prefix -> prefix pathspec */\n \tif (!entry) {\n \t\tstatic const char *raw[2];\n \n+\t\tif (flags & PATHSPEC_PREFER_FULL)\n+\t\t\treturn;\n+\n+\t\tif (!(flags & PATHSPEC_PREFER_CWD))\n+\t\t\tdie(\"BUG: PATHSPEC_PREFER_CWD requires arguments\");\n+\n \t\tpathspec->items = item = xmalloc(sizeof(*item));\n \t\tmemset(item, 0, sizeof(*item));\n \t\titem->match = prefix;\n@@ -340,7 +350,8 @@ const char **get_pathspec(const char *prefix, const char **pathspec)\n \tstruct pathspec ps;\n \tparse_pathspec(&ps,\n \t\t       PATHSPEC_ALL_MAGIC & ~PATHSPEC_FROMTOP,\n-\t\t       0, prefix, pathspec);\n+\t\t       PATHSPEC_PREFER_CWD,\n+\t\t       prefix, pathspec);\n \treturn ps.raw;\n }\n \ndiff --git a/pathspec.h b/pathspec.h\nindex cc5841b..d630e8b 100644\n--- a/pathspec.h\n+++ b/pathspec.h\n@@ -24,6 +24,10 @@ struct pathspec {\n \t} *items;\n };\n \n+/* parse_pathspec flags */\n+#define PATHSPEC_PREFER_CWD (1<<0) /* No args means match cwd */\n+#define PATHSPEC_PREFER_FULL (1<<1) /* No args means match everything */\n+\n extern int init_pathspec(struct pathspec *, const char **);\n extern void parse_pathspec(struct pathspec *pathspec,\n \t\t\t   unsigned magic_mask,\n-- \n1.8.2.83.gc99314b\n"},{"id":"223281","messageId":"1373790969-13000-9-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 08/46] Convert some get_pathspec() calls to parse_pathspec()","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:35:31Z","receivedAt":"2013-07-14T08:35:31Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"These call sites follow the pattern:\n\n   paths = get_pathspec(prefix, argv);\n   init_pathspec(&pathspec, paths);\n\nwhich can be converted into a single parse_pathspec() call.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n builtin/grep.c         |  6 +++---\n builtin/ls-tree.c      | 10 +++++++++-\n builtin/update-index.c |  5 +++--\n revision.c             |  4 ++--\n 4 files changed, 17 insertions(+), 8 deletions(-)\n\ndiff --git a/builtin/grep.c b/builtin/grep.c\nindex 4de49df..1a6c028 100644\n--- a/builtin/grep.c\n+++ b/builtin/grep.c\n@@ -631,7 +631,6 @@ int cmd_grep(int argc, const char **argv, const char *prefix)\n \tconst char *show_in_pager = NULL, *default_pager = \"dummy\";\n \tstruct grep_opt opt;\n \tstruct object_array list = OBJECT_ARRAY_INIT;\n-\tconst char **paths = NULL;\n \tstruct pathspec pathspec;\n \tstruct string_list path_list = STRING_LIST_INIT_NODUP;\n \tint i;\n@@ -858,8 +857,9 @@ int cmd_grep(int argc, const char **argv, const char *prefix)\n \t\t\tverify_filename(prefix, argv[j], j == i);\n \t}\n \n-\tpaths = get_pathspec(prefix, argv + i);\n-\tinit_pathspec(&pathspec, paths);\n+\tparse_pathspec(&pathspec, 0,\n+\t\t       PATHSPEC_PREFER_CWD,\n+\t\t       prefix, argv + i);\n \tpathspec.max_depth = opt.max_depth;\n \tpathspec.recursive = 1;\n \ndiff --git a/builtin/ls-tree.c b/builtin/ls-tree.c\nindex 93fc3a0..bdb03f3 100644\n--- a/builtin/ls-tree.c\n+++ b/builtin/ls-tree.c\n@@ -167,7 +167,15 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)\n \tif (get_sha1(argv[0], sha1))\n \t\tdie(\"Not a valid object name %s\", argv[0]);\n \n-\tinit_pathspec(&pathspec, get_pathspec(prefix, argv + 1));\n+\t/*\n+\t * show_recursive() rolls its own matching code and is\n+\t * generally ignorant of 'struct pathspec'. The magic mask\n+\t * cannot be lifted until it is converted to use\n+\t * match_pathspec_depth() or tree_entry_interesting()\n+\t */\n+\tparse_pathspec(&pathspec, 0,\n+\t\t       PATHSPEC_PREFER_CWD,\n+\t\t       prefix, argv + 1);\n \tfor (i = 0; i < pathspec.nr; i++)\n \t\tpathspec.items[i].nowildcard_len = pathspec.items[i].len;\n \tpathspec.has_wildcard = 0;\ndiff --git a/builtin/update-index.c b/builtin/update-index.c\nindex b9c2bd0..e795818 100644\n--- a/builtin/update-index.c\n+++ b/builtin/update-index.c\n@@ -547,10 +547,11 @@ static int do_reupdate(int ac, const char **av,\n \t */\n \tint pos;\n \tint has_head = 1;\n-\tconst char **paths = get_pathspec(prefix, av + 1);\n \tstruct pathspec pathspec;\n \n-\tinit_pathspec(&pathspec, paths);\n+\tparse_pathspec(&pathspec, 0,\n+\t\t       PATHSPEC_PREFER_CWD,\n+\t\t       prefix, av + 1);\n \n \tif (read_ref(\"HEAD\", head_sha1))\n \t\t/* If there is no HEAD, that means it is an initial\ndiff --git a/revision.c b/revision.c\nindex 2f0142f..cdc7f45 100644\n--- a/revision.c\n+++ b/revision.c\n@@ -2120,8 +2120,8 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s\n \t\t */\n \t\tALLOC_GROW(prune_data.path, prune_data.nr+1, prune_data.alloc);\n \t\tprune_data.path[prune_data.nr++] = NULL;\n-\t\tinit_pathspec(&revs->prune_data,\n-\t\t\t      get_pathspec(revs->prefix, prune_data.path));\n+\t\tparse_pathspec(&revs->prune_data, 0, 0,\n+\t\t\t       revs->prefix, prune_data.path);\n \t}\n \n \tif (revs->def == NULL)\n-- \n1.8.2.83.gc99314b\n"},{"id":"223282","messageId":"1373790969-13000-10-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 09/46] parse_pathspec: add special flag for max_depth feature","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:35:32Z","receivedAt":"2013-07-14T08:35:32Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"match_pathspec_depth() and tree_entry_interesting() check max_depth\nfield in order to support \"git grep --max-depth\". The feature\nactivation is tied to \"recursive\" field, which led to some unwanted\nactivation, e.g. 5c8eeb8 (diff-index: enable recursive pathspec\nmatching in unpack_trees - 2012-01-15).\n\nThis patch decouples the activation from \"recursive\" field, puts it in\n\"magic\" field instead. This makes sure that only \"git grep\" can\nactivate this feature. And because parse_pathspec knows when the\nfeature is not used, it does not need to sort pathspec (required for\nmax_depth to work correctly). A small win for non-grep cases.\n\nEven though a new magic flag is introduced, no magic syntax is. The\nmagic can be only enabled by parse_pathspec() caller. We might someday\nwant to support \":(maxdepth:10)src.\" It all depends on actual use\ncases.\n\nmax_depth feature cannot be enabled via init_pathspec() anymore. But\nthat's ok because init_pathspec() is on its way to /dev/null.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n builtin/grep.c | 3 ++-\n diff-lib.c     | 1 -\n dir.c          | 8 ++++++--\n pathspec.c     | 8 ++++++--\n pathspec.h     | 6 +++++-\n tree-diff.c    | 1 -\n tree-walk.c    | 8 ++++++--\n 7 files changed, 25 insertions(+), 10 deletions(-)\n\ndiff --git a/builtin/grep.c b/builtin/grep.c\nindex 1a6c028..4bc0754 100644\n--- a/builtin/grep.c\n+++ b/builtin/grep.c\n@@ -858,7 +858,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)\n \t}\n \n \tparse_pathspec(&pathspec, 0,\n-\t\t       PATHSPEC_PREFER_CWD,\n+\t\t       PATHSPEC_PREFER_CWD |\n+\t\t       (opt.max_depth != -1 ? PATHSPEC_MAXDEPTH_VALID : 0),\n \t\t       prefix, argv + i);\n \tpathspec.max_depth = opt.max_depth;\n \tpathspec.recursive = 1;\ndiff --git a/diff-lib.c b/diff-lib.c\nindex b6f4b21..d4e17f7 100644\n--- a/diff-lib.c\n+++ b/diff-lib.c\n@@ -476,7 +476,6 @@ static int diff_cache(struct rev_info *revs,\n \topts.dst_index = NULL;\n \topts.pathspec = &revs->diffopt.pathspec;\n \topts.pathspec->recursive = 1;\n-\topts.pathspec->max_depth = -1;\n \n \tinit_tree_desc(&t, tree->buffer, tree->size);\n \treturn unpack_trees(1, &t, &opts);\ndiff --git a/dir.c b/dir.c\nindex 308028e..e28bc0d 100644\n--- a/dir.c\n+++ b/dir.c\n@@ -341,7 +341,9 @@ int match_pathspec_depth(const struct pathspec *ps,\n \tint i, retval = 0;\n \n \tif (!ps->nr) {\n-\t\tif (!ps->recursive || ps->max_depth == -1)\n+\t\tif (!ps->recursive ||\n+\t\t    !(ps->magic & PATHSPEC_MAXDEPTH) ||\n+\t\t    ps->max_depth == -1)\n \t\t\treturn MATCHED_RECURSIVELY;\n \n \t\tif (within_depth(name, namelen, 0, ps->max_depth))\n@@ -358,7 +360,9 @@ int match_pathspec_depth(const struct pathspec *ps,\n \t\tif (seen && seen[i] == MATCHED_EXACTLY)\n \t\t\tcontinue;\n \t\thow = match_pathspec_item(ps->items+i, prefix, name, namelen);\n-\t\tif (ps->recursive && ps->max_depth != -1 &&\n+\t\tif (ps->recursive &&\n+\t\t    (ps->magic & PATHSPEC_MAXDEPTH) &&\n+\t\t    ps->max_depth != -1 &&\n \t\t    how && how != MATCHED_FNMATCH) {\n \t\t\tint len = ps->items[i].len;\n \t\t\tif (name[len] == '/')\ndiff --git a/pathspec.c b/pathspec.c\nindex 6d99a3d..06778fc 100644\n--- a/pathspec.c\n+++ b/pathspec.c\n@@ -267,6 +267,9 @@ void parse_pathspec(struct pathspec *pathspec,\n \n \tmemset(pathspec, 0, sizeof(*pathspec));\n \n+\tif (flags & PATHSPEC_MAXDEPTH_VALID)\n+\t\tpathspec->magic |= PATHSPEC_MAXDEPTH;\n+\n \t/* No arguments, no prefix -> no pathspec */\n \tif (!entry && !prefix)\n \t\treturn;\n@@ -322,8 +325,9 @@ void parse_pathspec(struct pathspec *pathspec,\n \t\tpathspec->magic |= item[i].magic;\n \t}\n \n-\tqsort(pathspec->items, pathspec->nr,\n-\t      sizeof(struct pathspec_item), pathspec_item_cmp);\n+\tif (pathspec->magic & PATHSPEC_MAXDEPTH)\n+\t\tqsort(pathspec->items, pathspec->nr,\n+\t\t      sizeof(struct pathspec_item), pathspec_item_cmp);\n }\n \n /*\ndiff --git a/pathspec.h b/pathspec.h\nindex d630e8b..aa98597 100644\n--- a/pathspec.h\n+++ b/pathspec.h\n@@ -3,7 +3,10 @@\n \n /* Pathspec magic */\n #define PATHSPEC_FROMTOP\t(1<<0)\n-#define PATHSPEC_ALL_MAGIC PATHSPEC_FROMTOP\n+#define PATHSPEC_MAXDEPTH\t(1<<1)\n+#define PATHSPEC_ALL_MAGIC\t  \\\n+\t(PATHSPEC_FROMTOP\t| \\\n+\t PATHSPEC_MAXDEPTH)\n \n #define PATHSPEC_ONESTAR 1\t/* the pathspec pattern sastisfies GFNM_ONESTAR */\n \n@@ -27,6 +30,7 @@ struct pathspec {\n /* parse_pathspec flags */\n #define PATHSPEC_PREFER_CWD (1<<0) /* No args means match cwd */\n #define PATHSPEC_PREFER_FULL (1<<1) /* No args means match everything */\n+#define PATHSPEC_MAXDEPTH_VALID (1<<2) /* max_depth field is valid */\n \n extern int init_pathspec(struct pathspec *, const char **);\n extern void parse_pathspec(struct pathspec *pathspec,\ndiff --git a/tree-diff.c b/tree-diff.c\nindex ba01563..826512e 100644\n--- a/tree-diff.c\n+++ b/tree-diff.c\n@@ -138,7 +138,6 @@ int diff_tree(struct tree_desc *t1, struct tree_desc *t2,\n \n \t/* Enable recursion indefinitely */\n \topt->pathspec.recursive = DIFF_OPT_TST(opt, RECURSIVE);\n-\topt->pathspec.max_depth = -1;\n \n \tstrbuf_init(&base, PATH_MAX);\n \tstrbuf_add(&base, base_str, baselen);\ndiff --git a/tree-walk.c b/tree-walk.c\nindex 72a9613..d399ca9 100644\n--- a/tree-walk.c\n+++ b/tree-walk.c\n@@ -637,7 +637,9 @@ enum interesting tree_entry_interesting(const struct name_entry *entry,\n \t\tentry_not_interesting : all_entries_not_interesting;\n \n \tif (!ps->nr) {\n-\t\tif (!ps->recursive || ps->max_depth == -1)\n+\t\tif (!ps->recursive ||\n+\t\t    !(ps->magic & PATHSPEC_MAXDEPTH) ||\n+\t\t    ps->max_depth == -1)\n \t\t\treturn all_entries_interesting;\n \t\treturn within_depth(base->buf + base_offset, baselen,\n \t\t\t\t    !!S_ISDIR(entry->mode),\n@@ -658,7 +660,9 @@ enum interesting tree_entry_interesting(const struct name_entry *entry,\n \t\t\tif (!match_dir_prefix(base_str, match, matchlen))\n \t\t\t\tgoto match_wildcards;\n \n-\t\t\tif (!ps->recursive || ps->max_depth == -1)\n+\t\t\tif (!ps->recursive ||\n+\t\t\t    !(ps->magic & PATHSPEC_MAXDEPTH) ||\n+\t\t\t    ps->max_depth == -1)\n \t\t\t\treturn all_entries_interesting;\n \n \t\t\treturn within_depth(base_str + matchlen + 1,\n-- \n1.8.2.83.gc99314b\n"},{"id":"223283","messageId":"1373790969-13000-11-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 10/46] parse_pathspec: support stripping submodule trailing slashes","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:35:33Z","receivedAt":"2013-07-14T08:35:33Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"This flag is equivalent to builtin/ls-files.c:strip_trailing_slashes()\nand is intended to replace that function when ls-files is converted to\nuse parse_pathspec.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n pathspec.c | 9 +++++++++\n pathspec.h | 2 ++\n 2 files changed, 11 insertions(+)\n\ndiff --git a/pathspec.c b/pathspec.c\nindex 06778fc..1c07c23 100644\n--- a/pathspec.c\n+++ b/pathspec.c\n@@ -205,6 +205,15 @@ static unsigned prefix_pathspec(struct pathspec_item *item,\n \t*raw = item->match = match;\n \titem->original = elt;\n \titem->len = strlen(item->match);\n+\n+\tif ((flags & PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP) &&\n+\t    (item->len >= 1 && item->match[item->len - 1] == '/') &&\n+\t    (i = cache_name_pos(item->match, item->len - 1)) >= 0 &&\n+\t    S_ISGITLINK(active_cache[i]->ce_mode)) {\n+\t\titem->len--;\n+\t\tmatch[item->len] = '\\0';\n+\t}\n+\n \tif (limit_pathspec_to_literal())\n \t\titem->nowildcard_len = item->len;\n \telse\ndiff --git a/pathspec.h b/pathspec.h\nindex aa98597..5144851 100644\n--- a/pathspec.h\n+++ b/pathspec.h\n@@ -31,6 +31,8 @@ struct pathspec {\n #define PATHSPEC_PREFER_CWD (1<<0) /* No args means match cwd */\n #define PATHSPEC_PREFER_FULL (1<<1) /* No args means match everything */\n #define PATHSPEC_MAXDEPTH_VALID (1<<2) /* max_depth field is valid */\n+/* strip the trailing slash if the given path is a gitlink */\n+#define PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP (1<<3)\n \n extern int init_pathspec(struct pathspec *, const char **);\n extern void parse_pathspec(struct pathspec *pathspec,\n-- \n1.8.2.83.gc99314b\n"},{"id":"223284","messageId":"1373790969-13000-12-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 11/46] parse_pathspec: support stripping/checking submodule paths","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:35:34Z","receivedAt":"2013-07-14T08:35:34Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"PATHSPEC_SYMLINK_LEADING_PATH and _STRIP_SUBMODULE_SLASH_EXPENSIVE are\nrespectively the alternate implementation of\npathspec.c:die_if_path_beyond_symlink() and\npathspec.c:check_path_for_gitlink(). They are intended to replace\nthose functions when builtin/add.c and builtin/check-ignore.c are\nconverted to use parse_pathspec.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n pathspec.c | 26 ++++++++++++++++++++++++++\n pathspec.h | 10 ++++++++++\n 2 files changed, 36 insertions(+)\n\ndiff --git a/pathspec.c b/pathspec.c\nindex 1c07c23..e2a4f91 100644\n--- a/pathspec.c\n+++ b/pathspec.c\n@@ -214,6 +214,26 @@ static unsigned prefix_pathspec(struct pathspec_item *item,\n \t\tmatch[item->len] = '\\0';\n \t}\n \n+\tif (flags & PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE)\n+\t\tfor (i = 0; i < active_nr; i++) {\n+\t\t\tstruct cache_entry *ce = active_cache[i];\n+\t\t\tint ce_len = ce_namelen(ce);\n+\n+\t\t\tif (!S_ISGITLINK(ce->ce_mode))\n+\t\t\t\tcontinue;\n+\n+\t\t\tif (item->len <= ce_len || match[ce_len] != '/' ||\n+\t\t\t    memcmp(ce->name, match, ce_len))\n+\t\t\t\tcontinue;\n+\t\t\tif (item->len == ce_len + 1) {\n+\t\t\t\t/* strip trailing slash */\n+\t\t\t\titem->len--;\n+\t\t\t\tmatch[item->len] = '\\0';\n+\t\t\t} else\n+\t\t\t\tdie (_(\"Pathspec '%s' is in submodule '%.*s'\"),\n+\t\t\t\t     elt, ce_len, ce->name);\n+\t\t}\n+\n \tif (limit_pathspec_to_literal())\n \t\titem->nowildcard_len = item->len;\n \telse\n@@ -329,6 +349,12 @@ void parse_pathspec(struct pathspec *pathspec,\n \t\t\tunsupported_magic(entry,\n \t\t\t\t\t  item[i].magic & magic_mask,\n \t\t\t\t\t  short_magic);\n+\n+\t\tif ((flags & PATHSPEC_SYMLINK_LEADING_PATH) &&\n+\t\t    has_symlink_leading_path(item[i].match, item[i].len)) {\n+\t\t\tdie(_(\"pathspec '%s' is beyond a symbolic link\"), entry);\n+\t\t}\n+\n \t\tif (item[i].nowildcard_len < item[i].len)\n \t\t\tpathspec->has_wildcard = 1;\n \t\tpathspec->magic |= item[i].magic;\ndiff --git a/pathspec.h b/pathspec.h\nindex 5144851..450fc03 100644\n--- a/pathspec.h\n+++ b/pathspec.h\n@@ -33,6 +33,16 @@ struct pathspec {\n #define PATHSPEC_MAXDEPTH_VALID (1<<2) /* max_depth field is valid */\n /* strip the trailing slash if the given path is a gitlink */\n #define PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP (1<<3)\n+/* die if a symlink is part of the given path's directory */\n+#define PATHSPEC_SYMLINK_LEADING_PATH (1<<4)\n+/*\n+ * This is like a combination of ..LEADING_PATH and .._SLASH_CHEAP\n+ * (but not the same): it strips the trailing slash if the given path\n+ * is a gitlink but also checks and dies if gitlink is part of the\n+ * leading path (i.e. the given path goes beyond a submodule). It's\n+ * safer than _SLASH_CHEAP and also more expensive.\n+ */\n+#define PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE (1<<5)\n \n extern int init_pathspec(struct pathspec *, const char **);\n extern void parse_pathspec(struct pathspec *pathspec,\n-- \n1.8.2.83.gc99314b\n"},{"id":"223285","messageId":"1373790969-13000-13-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 12/46] parse_pathspec: support prefixing original patterns","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:35:35Z","receivedAt":"2013-07-14T08:35:35Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"This makes 'original' suitable for passing to an external command\nbecause all pathspec magic is left in place, provided that the\nexternal command understands pathspec. The prefixing is needed because\nwe usually launch a subcommand at worktree's top directory and the\nsubcommand can no longer calculate the prefix itself.\n\nThis slightly affects the original purpose of 'original'\n(i.e. reporting). We should report without prefixing. So only turn\nthis flag on when you know you are about to pass the result straight\naway to an external command.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n pathspec.c | 12 +++++++++++-\n pathspec.h |  1 +\n 2 files changed, 12 insertions(+), 1 deletion(-)\n\ndiff --git a/pathspec.c b/pathspec.c\nindex e2a4f91..ba6408a 100644\n--- a/pathspec.c\n+++ b/pathspec.c\n@@ -203,7 +203,17 @@ static unsigned prefix_pathspec(struct pathspec_item *item,\n \telse\n \t\tmatch = prefix_path(prefix, prefixlen, copyfrom);\n \t*raw = item->match = match;\n-\titem->original = elt;\n+\t/*\n+\t * Prefix the pathspec (keep all magic) and assign to\n+\t * original. Useful for passing to another command.\n+\t */\n+\tif (flags & PATHSPEC_PREFIX_ORIGIN) {\n+\t\tstruct strbuf sb = STRBUF_INIT;\n+\t\tstrbuf_add(&sb, elt, copyfrom - elt);\n+\t\tstrbuf_addstr(&sb, match);\n+\t\titem->original = strbuf_detach(&sb, NULL);\n+\t} else\n+\t\titem->original = elt;\n \titem->len = strlen(item->match);\n \n \tif ((flags & PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP) &&\ndiff --git a/pathspec.h b/pathspec.h\nindex 450fc03..2e427d7 100644\n--- a/pathspec.h\n+++ b/pathspec.h\n@@ -43,6 +43,7 @@ struct pathspec {\n  * safer than _SLASH_CHEAP and also more expensive.\n  */\n #define PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE (1<<5)\n+#define PATHSPEC_PREFIX_ORIGIN (1<<6)\n \n extern int init_pathspec(struct pathspec *, const char **);\n extern void parse_pathspec(struct pathspec *pathspec,\n-- \n1.8.2.83.gc99314b\n"},{"id":"223286","messageId":"1373790969-13000-14-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 13/46] Guard against new pathspec magic in pathspec matching code","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:35:36Z","receivedAt":"2013-07-14T08:35:36Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"GUARD_PATHSPEC() marks pathspec-sensitive code, basically all those\nthat touch anything in 'struct pathspec' except fields \"nr\" and\n\"original\". GUARD_PATHSPEC() is not supposed to fail. It's mainly to\nhelp the designers catch unsupported codepaths.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n Documentation/technical/api-setup.txt | 19 +++++++++++++++++++\n builtin/diff.c                        |  2 ++\n dir.c                                 |  2 ++\n pathspec.h                            |  7 +++++++\n tree-diff.c                           | 19 +++++++++++++++++++\n tree-walk.c                           |  2 ++\n 6 files changed, 51 insertions(+)\n\ndiff --git a/Documentation/technical/api-setup.txt b/Documentation/technical/api-setup.txt\nindex 90d1aff..540e455 100644\n--- a/Documentation/technical/api-setup.txt\n+++ b/Documentation/technical/api-setup.txt\n@@ -28,3 +28,22 @@ parse_pathspec(). This function takes several arguments:\n - prefix and args come from cmd_* functions\n \n get_pathspec() is obsolete and should never be used in new code.\n+\n+parse_pathspec() helps catch unsupported features and reject them\n+politely. At a lower level, different pathspec-related functions may\n+not support the same set of features. Such pathspec-sensitive\n+functions are guarded with GUARD_PATHSPEC(), which will die in an\n+unfriendly way when an unsupported feature is requested.\n+\n+The command designers are supposed to make sure that GUARD_PATHSPEC()\n+never dies. They have to make sure all unsupported features are caught\n+by parse_pathspec(), not by GUARD_PATHSPEC. grepping GUARD_PATHSPEC()\n+should give the designers all pathspec-sensitive codepaths and what\n+features they support.\n+\n+A similar process is applied when a new pathspec magic is added. The\n+designer lifts the GUARD_PATHSPEC restriction in the functions that\n+support the new magic. At the same time (s)he has to make sure this\n+new feature will be caught at parse_pathspec() in commands that cannot\n+handle the new magic in some cases. grepping parse_pathspec() should\n+help.\ndiff --git a/builtin/diff.c b/builtin/diff.c\nindex 9fc273d..6bb41af 100644\n--- a/builtin/diff.c\n+++ b/builtin/diff.c\n@@ -367,6 +367,8 @@ int cmd_diff(int argc, const char **argv, const char *prefix)\n \t\t}\n \t}\n \tif (rev.prune_data.nr) {\n+\t\t/* builtin_diff_b_f() */\n+\t\tGUARD_PATHSPEC(&rev.prune_data, PATHSPEC_FROMTOP);\n \t\tif (!path)\n \t\t\tpath = rev.prune_data.items[0].match;\n \t\tpaths += rev.prune_data.nr;\ndiff --git a/dir.c b/dir.c\nindex e28bc0d..19978d3 100644\n--- a/dir.c\n+++ b/dir.c\n@@ -340,6 +340,8 @@ int match_pathspec_depth(const struct pathspec *ps,\n {\n \tint i, retval = 0;\n \n+\tGUARD_PATHSPEC(ps, PATHSPEC_FROMTOP | PATHSPEC_MAXDEPTH);\n+\n \tif (!ps->nr) {\n \t\tif (!ps->recursive ||\n \t\t    !(ps->magic & PATHSPEC_MAXDEPTH) ||\ndiff --git a/pathspec.h b/pathspec.h\nindex 2e427d7..6baf205 100644\n--- a/pathspec.h\n+++ b/pathspec.h\n@@ -27,6 +27,13 @@ struct pathspec {\n \t} *items;\n };\n \n+#define GUARD_PATHSPEC(ps, mask) \\\n+\tdo { \\\n+\t\tif ((ps)->magic & ~(mask))\t       \\\n+\t\t\tdie(\"BUG:%s:%d: unsupported magic %x\",\t\\\n+\t\t\t    __FILE__, __LINE__, (ps)->magic & ~(mask)); \\\n+\t} while (0)\n+\n /* parse_pathspec flags */\n #define PATHSPEC_PREFER_CWD (1<<0) /* No args means match cwd */\n #define PATHSPEC_PREFER_FULL (1<<1) /* No args means match everything */\ndiff --git a/tree-diff.c b/tree-diff.c\nindex 826512e..5a87614 100644\n--- a/tree-diff.c\n+++ b/tree-diff.c\n@@ -198,6 +198,25 @@ static void try_to_follow_renames(struct tree_desc *t1, struct tree_desc *t2, co\n \tconst char *paths[1];\n \tint i;\n \n+\t/*\n+\t * follow-rename code is very specific, we need exactly one\n+\t * path. Magic that matches more than one path is not\n+\t * supported.\n+\t */\n+\tGUARD_PATHSPEC(&opt->pathspec, PATHSPEC_FROMTOP);\n+#if 0\n+\t/*\n+\t * We should reject wildcards as well. Unfortunately we\n+\t * haven't got a reliable way to detect that 'foo\\*bar' in\n+\t * fact has no wildcards. nowildcard_len is merely a hint for\n+\t * optimization. Let it slip for now until wildmatch is taught\n+\t * about dry-run mode and returns wildcard info.\n+\t */\n+\tif (opt->pathspec.has_wildcard)\n+\t\tdie(\"BUG:%s:%d: wildcards are not supported\",\n+\t\t    __FILE__, __LINE__);\n+#endif\n+\n \t/* Remove the file creation entry from the diff queue, and remember it */\n \tchoice = q->queue[0];\n \tq->nr = 0;\ndiff --git a/tree-walk.c b/tree-walk.c\nindex d399ca9..37b157e 100644\n--- a/tree-walk.c\n+++ b/tree-walk.c\n@@ -636,6 +636,8 @@ enum interesting tree_entry_interesting(const struct name_entry *entry,\n \tenum interesting never_interesting = ps->has_wildcard ?\n \t\tentry_not_interesting : all_entries_not_interesting;\n \n+\tGUARD_PATHSPEC(ps, PATHSPEC_FROMTOP | PATHSPEC_MAXDEPTH);\n+\n \tif (!ps->nr) {\n \t\tif (!ps->recursive ||\n \t\t    !(ps->magic & PATHSPEC_MAXDEPTH) ||\n-- \n1.8.2.83.gc99314b\n"},{"id":"223287","messageId":"1373790969-13000-15-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 14/46] clean: convert to use parse_pathspec","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:35:37Z","receivedAt":"2013-07-14T08:35:37Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n builtin/clean.c | 17 ++++++++++-------\n 1 file changed, 10 insertions(+), 7 deletions(-)\n\ndiff --git a/builtin/clean.c b/builtin/clean.c\nindex f955a40..fdd4980 100644\n--- a/builtin/clean.c\n+++ b/builtin/clean.c\n@@ -13,6 +13,7 @@\n #include \"refs.h\"\n #include \"string-list.h\"\n #include \"quote.h\"\n+#include \"pathspec.h\"\n \n static int force = -1; /* unset */\n \n@@ -150,7 +151,7 @@ int cmd_clean(int argc, const char **argv, const char *prefix)\n \tint rm_flags = REMOVE_DIR_KEEP_NESTED_GIT;\n \tstruct strbuf directory = STRBUF_INIT;\n \tstruct dir_struct dir;\n-\tstatic const char **pathspec;\n+\tstruct pathspec pathspec;\n \tstruct strbuf buf = STRBUF_INIT;\n \tstruct string_list exclude_list = STRING_LIST_INIT_NODUP;\n \tstruct exclude_list *el;\n@@ -209,9 +210,11 @@ int cmd_clean(int argc, const char **argv, const char *prefix)\n \tfor (i = 0; i < exclude_list.nr; i++)\n \t\tadd_exclude(exclude_list.items[i].string, \"\", 0, el, -(i+1));\n \n-\tpathspec = get_pathspec(prefix, argv);\n+\tparse_pathspec(&pathspec, 0,\n+\t\t       PATHSPEC_PREFER_CWD,\n+\t\t       prefix, argv);\n \n-\tfill_directory(&dir, pathspec);\n+\tfill_directory(&dir, pathspec.raw);\n \n \tfor (i = 0; i < dir.nr; i++) {\n \t\tstruct dir_entry *ent = dir.entries[i];\n@@ -246,9 +249,9 @@ int cmd_clean(int argc, const char **argv, const char *prefix)\n \t\tif (lstat(ent->name, &st))\n \t\t\tcontinue;\n \n-\t\tif (pathspec)\n-\t\t\tmatches = match_pathspec(pathspec, ent->name, len,\n-\t\t\t\t\t\t 0, NULL);\n+\t\tif (pathspec.nr)\n+\t\t\tmatches = match_pathspec_depth(&pathspec, ent->name,\n+\t\t\t\t\t\t       len, 0, NULL);\n \n \t\tif (S_ISDIR(st.st_mode)) {\n \t\t\tstrbuf_addstr(&directory, ent->name);\n@@ -262,7 +265,7 @@ int cmd_clean(int argc, const char **argv, const char *prefix)\n \t\t\t}\n \t\t\tstrbuf_reset(&directory);\n \t\t} else {\n-\t\t\tif (pathspec && !matches)\n+\t\t\tif (pathspec.nr && !matches)\n \t\t\t\tcontinue;\n \t\t\tres = dry_run ? 0 : unlink(ent->name);\n \t\t\tif (res) {\n-- \n1.8.2.83.gc99314b\n"},{"id":"223288","messageId":"1373790969-13000-16-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 15/46] commit: convert to use parse_pathspec","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:35:38Z","receivedAt":"2013-07-14T08:35:38Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n builtin/commit.c | 18 +++++++++---------\n 1 file changed, 9 insertions(+), 9 deletions(-)\n\ndiff --git a/builtin/commit.c b/builtin/commit.c\nindex 790e5ab..530f0ed 100644\n--- a/builtin/commit.c\n+++ b/builtin/commit.c\n@@ -284,17 +284,17 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,\n {\n \tint fd;\n \tstruct string_list partial;\n-\tconst char **pathspec = NULL;\n+\tstruct pathspec pathspec;\n \tchar *old_index_env = NULL;\n \tint refresh_flags = REFRESH_QUIET;\n \n \tif (is_status)\n \t\trefresh_flags |= REFRESH_UNMERGED;\n+\tparse_pathspec(&pathspec, 0,\n+\t\t       PATHSPEC_PREFER_FULL,\n+\t\t       prefix, argv);\n \n-\tif (*argv)\n-\t\tpathspec = get_pathspec(prefix, argv);\n-\n-\tif (read_cache_preload(pathspec) < 0)\n+\tif (read_cache_preload(pathspec.raw) < 0)\n \t\tdie(_(\"index file corrupt\"));\n \n \tif (interactive) {\n@@ -336,9 +336,9 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,\n \t * (A) if all goes well, commit the real index;\n \t * (B) on failure, rollback the real index.\n \t */\n-\tif (all || (also && pathspec && *pathspec)) {\n+\tif (all || (also && pathspec.nr)) {\n \t\tfd = hold_locked_index(&index_lock, 1);\n-\t\tadd_files_to_cache(also ? prefix : NULL, pathspec, 0);\n+\t\tadd_files_to_cache(also ? prefix : NULL, pathspec.raw, 0);\n \t\trefresh_cache_or_die(refresh_flags);\n \t\tupdate_main_cache_tree(WRITE_TREE_SILENT);\n \t\tif (write_cache(fd, active_cache, active_nr) ||\n@@ -357,7 +357,7 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,\n \t * and create commit from the_index.\n \t * We still need to refresh the index here.\n \t */\n-\tif (!only && (!pathspec || !*pathspec)) {\n+\tif (!only && !pathspec.nr) {\n \t\tfd = hold_locked_index(&index_lock, 1);\n \t\trefresh_cache_or_die(refresh_flags);\n \t\tif (active_cache_changed) {\n@@ -402,7 +402,7 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,\n \n \tmemset(&partial, 0, sizeof(partial));\n \tpartial.strdup_strings = 1;\n-\tif (list_paths(&partial, !current_head ? NULL : \"HEAD\", prefix, pathspec))\n+\tif (list_paths(&partial, !current_head ? NULL : \"HEAD\", prefix, pathspec.raw))\n \t\texit(1);\n \n \tdiscard_cache();\n-- \n1.8.2.83.gc99314b\n"},{"id":"223289","messageId":"1373790969-13000-17-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 16/46] status: convert to use parse_pathspec","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:35:39Z","receivedAt":"2013-07-14T08:35:39Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n builtin/commit.c |  9 +++++----\n wt-status.c      | 16 +++++++---------\n wt-status.h      |  2 +-\n 3 files changed, 13 insertions(+), 14 deletions(-)\n\ndiff --git a/builtin/commit.c b/builtin/commit.c\nindex 530f0ed..64d1a3d 100644\n--- a/builtin/commit.c\n+++ b/builtin/commit.c\n@@ -1241,11 +1241,12 @@ int cmd_status(int argc, const char **argv, const char *prefix)\n \thandle_untracked_files_arg(&s);\n \tif (show_ignored_in_status)\n \t\ts.show_ignored_files = 1;\n-\tif (*argv)\n-\t\ts.pathspec = get_pathspec(prefix, argv);\n+\tparse_pathspec(&s.pathspec, 0,\n+\t\t       PATHSPEC_PREFER_FULL,\n+\t\t       prefix, argv);\n \n-\tread_cache_preload(s.pathspec);\n-\trefresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, s.pathspec, NULL, NULL);\n+\tread_cache_preload(s.pathspec.raw);\n+\trefresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, s.pathspec.raw, NULL, NULL);\n \n \tfd = hold_locked_index(&index_lock, 0);\n \tif (0 <= fd)\ndiff --git a/wt-status.c b/wt-status.c\nindex 9c4d086..fae0c27 100644\n--- a/wt-status.c\n+++ b/wt-status.c\n@@ -1,4 +1,5 @@\n #include \"cache.h\"\n+#include \"pathspec.h\"\n #include \"wt-status.h\"\n #include \"object.h\"\n #include \"dir.h\"\n@@ -438,7 +439,7 @@ static void wt_status_collect_changes_worktree(struct wt_status *s)\n \t}\n \trev.diffopt.format_callback = wt_status_collect_changed_cb;\n \trev.diffopt.format_callback_data = s;\n-\tinit_pathspec(&rev.prune_data, s->pathspec);\n+\tcopy_pathspec(&rev.prune_data, &s->pathspec);\n \trun_diff_files(&rev, 0);\n }\n \n@@ -463,22 +464,20 @@ static void wt_status_collect_changes_index(struct wt_status *s)\n \trev.diffopt.detect_rename = 1;\n \trev.diffopt.rename_limit = 200;\n \trev.diffopt.break_opt = 0;\n-\tinit_pathspec(&rev.prune_data, s->pathspec);\n+\tcopy_pathspec(&rev.prune_data, &s->pathspec);\n \trun_diff_index(&rev, 1);\n }\n \n static void wt_status_collect_changes_initial(struct wt_status *s)\n {\n-\tstruct pathspec pathspec;\n \tint i;\n \n-\tinit_pathspec(&pathspec, s->pathspec);\n \tfor (i = 0; i < active_nr; i++) {\n \t\tstruct string_list_item *it;\n \t\tstruct wt_status_change_data *d;\n \t\tstruct cache_entry *ce = active_cache[i];\n \n-\t\tif (!ce_path_match(ce, &pathspec))\n+\t\tif (!ce_path_match(ce, &s->pathspec))\n \t\t\tcontinue;\n \t\tit = string_list_insert(&s->change, ce->name);\n \t\td = it->util;\n@@ -493,7 +492,6 @@ static void wt_status_collect_changes_initial(struct wt_status *s)\n \t\telse\n \t\t\td->index_status = DIFF_STATUS_ADDED;\n \t}\n-\tfree_pathspec(&pathspec);\n }\n \n static void wt_status_collect_untracked(struct wt_status *s)\n@@ -516,12 +514,12 @@ static void wt_status_collect_untracked(struct wt_status *s)\n \t\tdir.flags |= DIR_SHOW_IGNORED_TOO;\n \tsetup_standard_excludes(&dir);\n \n-\tfill_directory(&dir, s->pathspec);\n+\tfill_directory(&dir, s->pathspec.raw);\n \n \tfor (i = 0; i < dir.nr; i++) {\n \t\tstruct dir_entry *ent = dir.entries[i];\n \t\tif (cache_name_is_other(ent->name, ent->len) &&\n-\t\t    match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL))\n+\t\t    match_pathspec_depth(&s->pathspec, ent->name, ent->len, 0, NULL))\n \t\t\tstring_list_insert(&s->untracked, ent->name);\n \t\tfree(ent);\n \t}\n@@ -529,7 +527,7 @@ static void wt_status_collect_untracked(struct wt_status *s)\n \tfor (i = 0; i < dir.ignored_nr; i++) {\n \t\tstruct dir_entry *ent = dir.ignored[i];\n \t\tif (cache_name_is_other(ent->name, ent->len) &&\n-\t\t    match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL))\n+\t\t    match_pathspec_depth(&s->pathspec, ent->name, ent->len, 0, NULL))\n \t\t\tstring_list_insert(&s->ignored, ent->name);\n \t\tfree(ent);\n \t}\ndiff --git a/wt-status.h b/wt-status.h\nindex 4121bc2..8463672 100644\n--- a/wt-status.h\n+++ b/wt-status.h\n@@ -44,7 +44,7 @@ struct wt_status {\n \tint is_initial;\n \tchar *branch;\n \tconst char *reference;\n-\tconst char **pathspec;\n+\tstruct pathspec pathspec;\n \tint verbose;\n \tint amend;\n \tenum commit_whence whence;\n-- \n1.8.2.83.gc99314b\n"},{"id":"223290","messageId":"1373790969-13000-18-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 17/46] rerere: convert to use parse_pathspec","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:35:40Z","receivedAt":"2013-07-14T08:35:40Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n builtin/rerere.c | 8 +++++---\n rerere.c         | 9 +++++----\n rerere.h         | 4 +++-\n 3 files changed, 13 insertions(+), 8 deletions(-)\n\ndiff --git a/builtin/rerere.c b/builtin/rerere.c\nindex dc1708e..4e51add 100644\n--- a/builtin/rerere.c\n+++ b/builtin/rerere.c\n@@ -6,6 +6,7 @@\n #include \"rerere.h\"\n #include \"xdiff/xdiff.h\"\n #include \"xdiff-interface.h\"\n+#include \"pathspec.h\"\n \n static const char * const rerere_usage[] = {\n \tN_(\"git rerere [clear | forget path... | status | remaining | diff | gc]\"),\n@@ -68,11 +69,12 @@ int cmd_rerere(int argc, const char **argv, const char *prefix)\n \t\treturn rerere(flags);\n \n \tif (!strcmp(argv[0], \"forget\")) {\n-\t\tconst char **pathspec;\n+\t\tstruct pathspec pathspec;\n \t\tif (argc < 2)\n \t\t\twarning(\"'git rerere forget' without paths is deprecated\");\n-\t\tpathspec = get_pathspec(prefix, argv + 1);\n-\t\treturn rerere_forget(pathspec);\n+\t\tparse_pathspec(&pathspec, 0, PATHSPEC_PREFER_CWD,\n+\t\t\t       prefix, argv + 1);\n+\t\treturn rerere_forget(&pathspec);\n \t}\n \n \tfd = setup_rerere(&merge_rr, flags);\ndiff --git a/rerere.c b/rerere.c\nindex 98e3e29..27afbfe 100644\n--- a/rerere.c\n+++ b/rerere.c\n@@ -6,6 +6,7 @@\n #include \"resolve-undo.h\"\n #include \"ll-merge.h\"\n #include \"attr.h\"\n+#include \"pathspec.h\"\n \n #define RESOLVED 0\n #define PUNTED 1\n@@ -656,7 +657,7 @@ static int rerere_forget_one_path(const char *path, struct string_list *rr)\n \treturn 0;\n }\n \n-int rerere_forget(const char **pathspec)\n+int rerere_forget(struct pathspec *pathspec)\n {\n \tint i, fd;\n \tstruct string_list conflict = STRING_LIST_INIT_DUP;\n@@ -667,12 +668,12 @@ int rerere_forget(const char **pathspec)\n \n \tfd = setup_rerere(&merge_rr, RERERE_NOAUTOUPDATE);\n \n-\tunmerge_cache(pathspec);\n+\tunmerge_cache(pathspec->raw);\n \tfind_conflict(&conflict);\n \tfor (i = 0; i < conflict.nr; i++) {\n \t\tstruct string_list_item *it = &conflict.items[i];\n-\t\tif (!match_pathspec(pathspec, it->string, strlen(it->string),\n-\t\t\t\t    0, NULL))\n+\t\tif (!match_pathspec_depth(pathspec, it->string, strlen(it->string),\n+\t\t\t\t\t  0, NULL))\n \t\t\tcontinue;\n \t\trerere_forget_one_path(it->string, &merge_rr);\n \t}\ndiff --git a/rerere.h b/rerere.h\nindex 156d2aa..4aa06c9 100644\n--- a/rerere.h\n+++ b/rerere.h\n@@ -3,6 +3,8 @@\n \n #include \"string-list.h\"\n \n+struct pathspec;\n+\n #define RERERE_AUTOUPDATE   01\n #define RERERE_NOAUTOUPDATE 02\n \n@@ -16,7 +18,7 @@ extern void *RERERE_RESOLVED;\n extern int setup_rerere(struct string_list *, int);\n extern int rerere(int);\n extern const char *rerere_path(const char *hex, const char *file);\n-extern int rerere_forget(const char **);\n+extern int rerere_forget(struct pathspec *);\n extern int rerere_remaining(struct string_list *);\n extern void rerere_clear(struct string_list *);\n extern void rerere_gc(struct string_list *);\n-- \n1.8.2.83.gc99314b\n"},{"id":"223291","messageId":"1373790969-13000-19-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 18/46] checkout: convert to use parse_pathspec","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:35:41Z","receivedAt":"2013-07-14T08:35:41Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n builtin/checkout.c | 34 +++++++++++++++++++++-------------\n 1 file changed, 21 insertions(+), 13 deletions(-)\n\ndiff --git a/builtin/checkout.c b/builtin/checkout.c\nindex 7fe0bff..6721de2 100644\n--- a/builtin/checkout.c\n+++ b/builtin/checkout.c\n@@ -46,7 +46,7 @@ struct checkout_opts {\n \n \tint branch_exists;\n \tconst char *prefix;\n-\tconst char **pathspec;\n+\tstruct pathspec pathspec;\n \tstruct tree *source_tree;\n };\n \n@@ -257,20 +257,18 @@ static int checkout_paths(const struct checkout_opts *opts,\n \n \tif (opts->patch_mode)\n \t\treturn run_add_interactive(revision, \"--patch=checkout\",\n-\t\t\t\t\t   opts->pathspec);\n+\t\t\t\t\t   opts->pathspec.raw);\n \n \tlock_file = xcalloc(1, sizeof(struct lock_file));\n \n \tnewfd = hold_locked_index(lock_file, 1);\n-\tif (read_cache_preload(opts->pathspec) < 0)\n+\tif (read_cache_preload(opts->pathspec.raw) < 0)\n \t\treturn error(_(\"corrupt index file\"));\n \n \tif (opts->source_tree)\n-\t\tread_tree_some(opts->source_tree, opts->pathspec);\n+\t\tread_tree_some(opts->source_tree, opts->pathspec.raw);\n \n-\tfor (pos = 0; opts->pathspec[pos]; pos++)\n-\t\t;\n-\tps_matched = xcalloc(1, pos);\n+\tps_matched = xcalloc(1, opts->pathspec.nr);\n \n \t/*\n \t * Make sure all pathspecs participated in locating the paths\n@@ -304,12 +302,12 @@ static int checkout_paths(const struct checkout_opts *opts,\n \t\t * match_pathspec() for _all_ entries when\n \t\t * opts->source_tree != NULL.\n \t\t */\n-\t\tif (match_pathspec(opts->pathspec, ce->name, ce_namelen(ce),\n+\t\tif (match_pathspec_depth(&opts->pathspec, ce->name, ce_namelen(ce),\n \t\t\t\t   0, ps_matched))\n \t\t\tce->ce_flags |= CE_MATCHED;\n \t}\n \n-\tif (report_path_error(ps_matched, opts->pathspec, opts->prefix)) {\n+\tif (report_path_error(ps_matched, opts->pathspec.raw, opts->prefix)) {\n \t\tfree(ps_matched);\n \t\treturn 1;\n \t}\n@@ -1002,7 +1000,7 @@ static int switch_unborn_to_new_branch(const struct checkout_opts *opts)\n static int checkout_branch(struct checkout_opts *opts,\n \t\t\t   struct branch_info *new)\n {\n-\tif (opts->pathspec)\n+\tif (opts->pathspec.nr)\n \t\tdie(_(\"paths cannot be used with switching branches\"));\n \n \tif (opts->patch_mode)\n@@ -1154,9 +1152,19 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)\n \t}\n \n \tif (argc) {\n-\t\topts.pathspec = get_pathspec(prefix, argv);\n+\t\t/*\n+\t\t * In patch mode (opts.patch_mode != 0), we pass the\n+\t\t * pathspec to an external program, git-add--interactive.\n+\t\t * Do not accept any kind of magic that that program\n+\t\t * cannot handle. Magic mask is pretty safe to be\n+\t\t * lifted for new magic when opts.patch_mode == 0.\n+\t\t */\n+\t\tparse_pathspec(&opts.pathspec,\n+\t\t\t       opts.patch_mode == 0 ? 0 :\n+\t\t\t       (PATHSPEC_ALL_MAGIC & ~PATHSPEC_FROMTOP),\n+\t\t\t       0, prefix, argv);\n \n-\t\tif (!opts.pathspec)\n+\t\tif (!opts.pathspec.nr)\n \t\t\tdie(_(\"invalid path specification\"));\n \n \t\t/*\n@@ -1188,7 +1196,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)\n \t\tstrbuf_release(&buf);\n \t}\n \n-\tif (opts.patch_mode || opts.pathspec)\n+\tif (opts.patch_mode || opts.pathspec.nr)\n \t\treturn checkout_paths(&opts, new.name);\n \telse\n \t\treturn checkout_branch(&opts, &new);\n-- \n1.8.2.83.gc99314b\n"},{"id":"223292","messageId":"1373790969-13000-20-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 19/46] rm: convert to use parse_pathspec","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:35:42Z","receivedAt":"2013-07-14T08:35:42Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n builtin/rm.c | 24 ++++++++++++------------\n 1 file changed, 12 insertions(+), 12 deletions(-)\n\ndiff --git a/builtin/rm.c b/builtin/rm.c\nindex 06025a2..ee0ae4c 100644\n--- a/builtin/rm.c\n+++ b/builtin/rm.c\n@@ -11,6 +11,7 @@\n #include \"parse-options.h\"\n #include \"string-list.h\"\n #include \"submodule.h\"\n+#include \"pathspec.h\"\n \n static const char * const builtin_rm_usage[] = {\n \tN_(\"git rm [options] [--] <file>...\"),\n@@ -279,7 +280,7 @@ static struct option builtin_rm_options[] = {\n int cmd_rm(int argc, const char **argv, const char *prefix)\n {\n \tint i, newfd;\n-\tconst char **pathspec;\n+\tstruct pathspec pathspec;\n \tchar *seen;\n \n \tgit_config(git_default_config, NULL);\n@@ -312,31 +313,30 @@ int cmd_rm(int argc, const char **argv, const char *prefix)\n \t\t}\n \t}\n \n-\tpathspec = get_pathspec(prefix, argv);\n-\trefresh_index(&the_index, REFRESH_QUIET, pathspec, NULL, NULL);\n+\tparse_pathspec(&pathspec, 0, PATHSPEC_PREFER_CWD, prefix, argv);\n+\trefresh_index(&the_index, REFRESH_QUIET, pathspec.raw, NULL, NULL);\n \n \tseen = NULL;\n-\tfor (i = 0; pathspec[i] ; i++)\n-\t\t/* nothing */;\n-\tseen = xcalloc(i, 1);\n+\tseen = xcalloc(pathspec.nr, 1);\n \n \tfor (i = 0; i < active_nr; i++) {\n \t\tstruct cache_entry *ce = active_cache[i];\n-\t\tif (!match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, seen))\n+\t\tif (!match_pathspec_depth(&pathspec, ce->name, ce_namelen(ce), 0, seen))\n \t\t\tcontinue;\n \t\tALLOC_GROW(list.entry, list.nr + 1, list.alloc);\n \t\tlist.entry[list.nr].name = ce->name;\n \t\tlist.entry[list.nr++].is_submodule = S_ISGITLINK(ce->ce_mode);\n \t}\n \n-\tif (pathspec) {\n-\t\tconst char *match;\n+\tif (pathspec.nr) {\n+\t\tconst char *original;\n \t\tint seen_any = 0;\n-\t\tfor (i = 0; (match = pathspec[i]) != NULL ; i++) {\n+\t\tfor (i = 0; i < pathspec.nr; i++) {\n+\t\t\toriginal = pathspec.items[i].original;\n \t\t\tif (!seen[i]) {\n \t\t\t\tif (!ignore_unmatch) {\n \t\t\t\t\tdie(_(\"pathspec '%s' did not match any files\"),\n-\t\t\t\t\t    match);\n+\t\t\t\t\t    original);\n \t\t\t\t}\n \t\t\t}\n \t\t\telse {\n@@ -344,7 +344,7 @@ int cmd_rm(int argc, const char **argv, const char *prefix)\n \t\t\t}\n \t\t\tif (!recursive && seen[i] == MATCHED_RECURSIVELY)\n \t\t\t\tdie(_(\"not removing '%s' recursively without -r\"),\n-\t\t\t\t    *match ? match : \".\");\n+\t\t\t\t    *original ? original : \".\");\n \t\t}\n \n \t\tif (! seen_any)\n-- \n1.8.2.83.gc99314b\n"},{"id":"223293","messageId":"1373790969-13000-21-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 20/46] ls-files: convert to use parse_pathspec","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:35:43Z","receivedAt":"2013-07-14T08:35:43Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n builtin/ls-files.c | 46 +++++++++++++---------------------------------\n 1 file changed, 13 insertions(+), 33 deletions(-)\n\ndiff --git a/builtin/ls-files.c b/builtin/ls-files.c\nindex 30357df..f1be425 100644\n--- a/builtin/ls-files.c\n+++ b/builtin/ls-files.c\n@@ -31,7 +31,7 @@ static int debug_mode;\n static const char *prefix;\n static int max_prefix_len;\n static int prefix_len;\n-static const char **pathspec;\n+static struct pathspec pathspec;\n static int error_unmatch;\n static char *ps_matched;\n static const char *with_tree;\n@@ -60,7 +60,7 @@ static void show_dir_entry(const char *tag, struct dir_entry *ent)\n \tif (len >= ent->len)\n \t\tdie(\"git ls-files: internal error - directory entry not superset of prefix\");\n \n-\tif (!match_pathspec(pathspec, ent->name, ent->len, len, ps_matched))\n+\tif (!match_pathspec_depth(&pathspec, ent->name, ent->len, len, ps_matched))\n \t\treturn;\n \n \tfputs(tag, stdout);\n@@ -135,7 +135,7 @@ static void show_ce_entry(const char *tag, struct cache_entry *ce)\n \tif (len >= ce_namelen(ce))\n \t\tdie(\"git ls-files: internal error - cache entry not superset of prefix\");\n \n-\tif (!match_pathspec(pathspec, ce->name, ce_namelen(ce), len, ps_matched))\n+\tif (!match_pathspec_depth(&pathspec, ce->name, ce_namelen(ce), len, ps_matched))\n \t\treturn;\n \n \tif (tag && *tag && show_valid_bit &&\n@@ -191,7 +191,7 @@ static void show_ru_info(void)\n \t\tlen = strlen(path);\n \t\tif (len < max_prefix_len)\n \t\t\tcontinue; /* outside of the prefix */\n-\t\tif (!match_pathspec(pathspec, path, len, max_prefix_len, ps_matched))\n+\t\tif (!match_pathspec_depth(&pathspec, path, len, max_prefix_len, ps_matched))\n \t\t\tcontinue; /* uninterested */\n \t\tfor (i = 0; i < 3; i++) {\n \t\t\tif (!ui->mode[i])\n@@ -216,7 +216,7 @@ static void show_files(struct dir_struct *dir)\n \n \t/* For cached/deleted files we don't need to even do the readdir */\n \tif (show_others || show_killed) {\n-\t\tfill_directory(dir, pathspec);\n+\t\tfill_directory(dir, pathspec.raw);\n \t\tif (show_others)\n \t\t\tshow_other_files(dir);\n \t\tif (show_killed)\n@@ -284,21 +284,6 @@ static void prune_cache(const char *prefix)\n \tactive_nr = last;\n }\n \n-static void strip_trailing_slash_from_submodules(void)\n-{\n-\tconst char **p;\n-\n-\tfor (p = pathspec; *p != NULL; p++) {\n-\t\tint len = strlen(*p), pos;\n-\n-\t\tif (len < 1 || (*p)[len - 1] != '/')\n-\t\t\tcontinue;\n-\t\tpos = cache_name_pos(*p, len - 1);\n-\t\tif (pos >= 0 && S_ISGITLINK(active_cache[pos]->ce_mode))\n-\t\t\t*p = xstrndup(*p, len - 1);\n-\t}\n-}\n-\n /*\n  * Read the tree specified with --with-tree option\n  * (typically, HEAD) into stage #1 and then\n@@ -552,23 +537,18 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)\n \tif (require_work_tree && !is_inside_work_tree())\n \t\tsetup_work_tree();\n \n-\tpathspec = get_pathspec(prefix, argv);\n-\n-\t/* be nice with submodule paths ending in a slash */\n-\tif (pathspec)\n-\t\tstrip_trailing_slash_from_submodules();\n+\tparse_pathspec(&pathspec, 0,\n+\t\t       PATHSPEC_PREFER_CWD |\n+\t\t       PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP,\n+\t\t       prefix, argv);\n \n \t/* Find common prefix for all pathspec's */\n-\tmax_prefix = common_prefix(pathspec);\n+\tmax_prefix = common_prefix(pathspec.raw);\n \tmax_prefix_len = max_prefix ? strlen(max_prefix) : 0;\n \n \t/* Treat unmatching pathspec elements as errors */\n-\tif (pathspec && error_unmatch) {\n-\t\tint num;\n-\t\tfor (num = 0; pathspec[num]; num++)\n-\t\t\t;\n-\t\tps_matched = xcalloc(1, num);\n-\t}\n+\tif (pathspec.nr && error_unmatch)\n+\t\tps_matched = xcalloc(1, pathspec.nr);\n \n \tif ((dir.flags & DIR_SHOW_IGNORED) && !exc_given)\n \t\tdie(\"ls-files --ignored needs some exclude pattern\");\n@@ -595,7 +575,7 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)\n \n \tif (ps_matched) {\n \t\tint bad;\n-\t\tbad = report_path_error(ps_matched, pathspec, prefix);\n+\t\tbad = report_path_error(ps_matched, pathspec.raw, prefix);\n \t\tif (bad)\n \t\t\tfprintf(stderr, \"Did you forget to 'git add'?\\n\");\n \n-- \n1.8.2.83.gc99314b\n"},{"id":"223294","messageId":"1373790969-13000-22-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 21/46] archive: convert to use parse_pathspec","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:35:44Z","receivedAt":"2013-07-14T08:35:44Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n archive.c | 19 +++++++++++--------\n archive.h |  4 +++-\n 2 files changed, 14 insertions(+), 9 deletions(-)\n\ndiff --git a/archive.c b/archive.c\nindex c699a2d..99fadc8 100644\n--- a/archive.c\n+++ b/archive.c\n@@ -5,7 +5,6 @@\n #include \"archive.h\"\n #include \"parse-options.h\"\n #include \"unpack-trees.h\"\n-#include \"pathspec.h\"\n \n static char const * const archive_usage[] = {\n \tN_(\"git archive [options] <tree-ish> [<path>...]\"),\n@@ -152,7 +151,6 @@ int write_archive_entries(struct archiver_args *args,\n \tstruct archiver_context context;\n \tstruct unpack_trees_options opts;\n \tstruct tree_desc t;\n-\tstruct pathspec pathspec;\n \tint err;\n \n \tif (args->baselen > 0 && args->base[args->baselen - 1] == '/') {\n@@ -187,10 +185,8 @@ int write_archive_entries(struct archiver_args *args,\n \t\tgit_attr_set_direction(GIT_ATTR_INDEX, &the_index);\n \t}\n \n-\tinit_pathspec(&pathspec, args->pathspec);\n-\terr = read_tree_recursive(args->tree, \"\", 0, 0, &pathspec,\n+\terr = read_tree_recursive(args->tree, \"\", 0, 0, &args->pathspec,\n \t\t\t\t  write_archive_entry, &context);\n-\tfree_pathspec(&pathspec);\n \tif (err == READ_TREE_RECURSIVE)\n \t\terr = 0;\n \treturn err;\n@@ -223,7 +219,7 @@ static int path_exists(struct tree *tree, const char *path)\n \tstruct pathspec pathspec;\n \tint ret;\n \n-\tinit_pathspec(&pathspec, paths);\n+\tparse_pathspec(&pathspec, 0, 0, \"\", paths);\n \tret = read_tree_recursive(tree, \"\", 0, 0, &pathspec, reject_entry, NULL);\n \tfree_pathspec(&pathspec);\n \treturn ret != 0;\n@@ -232,11 +228,18 @@ static int path_exists(struct tree *tree, const char *path)\n static void parse_pathspec_arg(const char **pathspec,\n \t\tstruct archiver_args *ar_args)\n {\n-\tar_args->pathspec = pathspec = get_pathspec(\"\", pathspec);\n+\t/*\n+\t * must be consistent with parse_pathspec in path_exists()\n+\t * Also if pathspec patterns are dependent, we're in big\n+\t * trouble as we test each one separately\n+\t */\n+\tparse_pathspec(&ar_args->pathspec, 0,\n+\t\t       PATHSPEC_PREFER_FULL,\n+\t\t       \"\", pathspec);\n \tif (pathspec) {\n \t\twhile (*pathspec) {\n \t\t\tif (**pathspec && !path_exists(ar_args->tree, *pathspec))\n-\t\t\t\tdie(\"path not found: %s\", *pathspec);\n+\t\t\t\tdie(_(\"pathspec '%s' did not match any files\"), *pathspec);\n \t\t\tpathspec++;\n \t\t}\n \t}\ndiff --git a/archive.h b/archive.h\nindex 895afcd..4a791e1 100644\n--- a/archive.h\n+++ b/archive.h\n@@ -1,6 +1,8 @@\n #ifndef ARCHIVE_H\n #define ARCHIVE_H\n \n+#include \"pathspec.h\"\n+\n struct archiver_args {\n \tconst char *base;\n \tsize_t baselen;\n@@ -8,7 +10,7 @@ struct archiver_args {\n \tconst unsigned char *commit_sha1;\n \tconst struct commit *commit;\n \ttime_t time;\n-\tconst char **pathspec;\n+\tstruct pathspec pathspec;\n \tunsigned int verbose : 1;\n \tunsigned int worktree_attributes : 1;\n \tunsigned int convert : 1;\n-- \n1.8.2.83.gc99314b\n"},{"id":"223295","messageId":"1373790969-13000-23-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 22/46] check-ignore: convert to use parse_pathspec","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:35:45Z","receivedAt":"2013-07-14T08:35:45Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"check-ignore (at least the test suite) seems to rely on the pattern\norder. PATHSPEC_KEEP_ORDER is introduced to explictly express this.\nThe lack of PATHSPEC_MAXDEPTH_VALID is sufficient because it's the\nonly flag that reorders pathspecs, but it's less obvious that way.\n\nCc: Adam Spiers <git@adamspiers.org>\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n builtin/check-ignore.c | 35 ++++++++++++++++++++++-------------\n pathspec.c             |  6 +++++-\n pathspec.h             |  1 +\n t/t0008-ignores.sh     |  8 ++++----\n 4 files changed, 32 insertions(+), 18 deletions(-)\n\ndiff --git a/builtin/check-ignore.c b/builtin/check-ignore.c\nindex 4a8fc70..d49c083 100644\n--- a/builtin/check-ignore.c\n+++ b/builtin/check-ignore.c\n@@ -64,37 +64,45 @@ static void output_exclude(const char *path, struct exclude *exclude)\n }\n \n static int check_ignore(struct dir_struct *dir,\n-\t\t\tconst char *prefix, const char **pathspec)\n+\t\t\tconst char *prefix, int argc, const char **argv)\n {\n-\tconst char *path, *full_path;\n+\tconst char *full_path;\n \tchar *seen;\n \tint num_ignored = 0, dtype = DT_UNKNOWN, i;\n \tstruct exclude *exclude;\n+\tstruct pathspec pathspec;\n \n-\tif (!pathspec || !*pathspec) {\n+\tif (!argc) {\n \t\tif (!quiet)\n \t\t\tfprintf(stderr, \"no pathspec given.\\n\");\n \t\treturn 0;\n \t}\n \n \t/*\n+\t * check-ignore just needs paths. Magic beyond :/ is really\n+\t * irrelevant.\n+\t */\n+\tparse_pathspec(&pathspec,\n+\t\t       PATHSPEC_ALL_MAGIC & ~PATHSPEC_FROMTOP,\n+\t\t       PATHSPEC_SYMLINK_LEADING_PATH |\n+\t\t       PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE |\n+\t\t       PATHSPEC_KEEP_ORDER,\n+\t\t       prefix, argv);\n+\n+\t/*\n \t * look for pathspecs matching entries in the index, since these\n \t * should not be ignored, in order to be consistent with\n \t * 'git status', 'git add' etc.\n \t */\n-\tseen = find_pathspecs_matching_against_index(pathspec);\n-\tfor (i = 0; pathspec[i]; i++) {\n-\t\tpath = pathspec[i];\n-\t\tfull_path = prefix_path(prefix, prefix\n-\t\t\t\t\t? strlen(prefix) : 0, path);\n-\t\tfull_path = check_path_for_gitlink(full_path);\n-\t\tdie_if_path_beyond_symlink(full_path, prefix);\n+\tseen = find_pathspecs_matching_against_index(pathspec.raw);\n+\tfor (i = 0; i < pathspec.nr; i++) {\n+\t\tfull_path = pathspec.raw[i];\n \t\texclude = NULL;\n \t\tif (!seen[i]) {\n \t\t\texclude = last_exclude_matching(dir, full_path, &dtype);\n \t\t}\n \t\tif (!quiet && (exclude || show_non_matching))\n-\t\t\toutput_exclude(path, exclude);\n+\t\t\toutput_exclude(pathspec.items[i].original, exclude);\n \t\tif (exclude)\n \t\t\tnum_ignored++;\n \t}\n@@ -120,7 +128,8 @@ static int check_ignore_stdin_paths(struct dir_struct *dir, const char *prefix)\n \t\t\tstrbuf_swap(&buf, &nbuf);\n \t\t}\n \t\tpathspec[0] = buf.buf;\n-\t\tnum_ignored += check_ignore(dir, prefix, (const char **)pathspec);\n+\t\tnum_ignored += check_ignore(dir, prefix,\n+\t\t\t\t\t    1, (const char **)pathspec);\n \t\tmaybe_flush_or_die(stdout, \"check-ignore to stdout\");\n \t}\n \tstrbuf_release(&buf);\n@@ -166,7 +175,7 @@ int cmd_check_ignore(int argc, const char **argv, const char *prefix)\n \tif (stdin_paths) {\n \t\tnum_ignored = check_ignore_stdin_paths(&dir, prefix);\n \t} else {\n-\t\tnum_ignored = check_ignore(&dir, prefix, argv);\n+\t\tnum_ignored = check_ignore(&dir, prefix, argc, argv);\n \t\tmaybe_flush_or_die(stdout, \"ignore to stdout\");\n \t}\n \ndiff --git a/pathspec.c b/pathspec.c\nindex ba6408a..5c9631a 100644\n--- a/pathspec.c\n+++ b/pathspec.c\n@@ -370,9 +370,13 @@ void parse_pathspec(struct pathspec *pathspec,\n \t\tpathspec->magic |= item[i].magic;\n \t}\n \n-\tif (pathspec->magic & PATHSPEC_MAXDEPTH)\n+\n+\tif (pathspec->magic & PATHSPEC_MAXDEPTH) {\n+\t\tif (flags & PATHSPEC_KEEP_ORDER)\n+\t\t\tdie(\"BUG: PATHSPEC_MAXDEPTH_VALID and PATHSPEC_KEEP_ORDER are incompatible\");\n \t\tqsort(pathspec->items, pathspec->nr,\n \t\t      sizeof(struct pathspec_item), pathspec_item_cmp);\n+\t}\n }\n \n /*\ndiff --git a/pathspec.h b/pathspec.h\nindex 6baf205..02dded3 100644\n--- a/pathspec.h\n+++ b/pathspec.h\n@@ -51,6 +51,7 @@ struct pathspec {\n  */\n #define PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE (1<<5)\n #define PATHSPEC_PREFIX_ORIGIN (1<<6)\n+#define PATHSPEC_KEEP_ORDER (1<<7)\n \n extern int init_pathspec(struct pathspec *, const char **);\n extern void parse_pathspec(struct pathspec *pathspec,\ndiff --git a/t/t0008-ignores.sh b/t/t0008-ignores.sh\nindex a56db80..2ced8e9 100755\n--- a/t/t0008-ignores.sh\n+++ b/t/t0008-ignores.sh\n@@ -432,7 +432,7 @@ test_expect_success_multi SYMLINKS 'symlink' '::\ta/symlink' '\n \n test_expect_success_multi SYMLINKS 'beyond a symlink' '' '\n \ttest_check_ignore \"a/symlink/foo\" 128 &&\n-\ttest_stderr \"fatal: '\\''a/symlink/foo'\\'' is beyond a symbolic link\"\n+\ttest_stderr \"fatal: pathspec '\\''a/symlink/foo'\\'' is beyond a symbolic link\"\n '\n \n test_expect_success_multi SYMLINKS 'beyond a symlink from subdirectory' '' '\n@@ -440,7 +440,7 @@ test_expect_success_multi SYMLINKS 'beyond a symlink from subdirectory' '' '\n \t\tcd a &&\n \t\ttest_check_ignore \"symlink/foo\" 128\n \t) &&\n-\ttest_stderr \"fatal: '\\''symlink/foo'\\'' is beyond a symbolic link\"\n+\ttest_stderr \"fatal: pathspec '\\''symlink/foo'\\'' is beyond a symbolic link\"\n '\n \n ############################################################################\n@@ -449,7 +449,7 @@ test_expect_success_multi SYMLINKS 'beyond a symlink from subdirectory' '' '\n \n test_expect_success_multi 'submodule' '' '\n \ttest_check_ignore \"a/submodule/one\" 128 &&\n-\ttest_stderr \"fatal: Path '\\''a/submodule/one'\\'' is in submodule '\\''a/submodule'\\''\"\n+\ttest_stderr \"fatal: Pathspec '\\''a/submodule/one'\\'' is in submodule '\\''a/submodule'\\''\"\n '\n \n test_expect_success_multi 'submodule from subdirectory' '' '\n@@ -457,7 +457,7 @@ test_expect_success_multi 'submodule from subdirectory' '' '\n \t\tcd a &&\n \t\ttest_check_ignore \"submodule/one\" 128\n \t) &&\n-\ttest_stderr \"fatal: Path '\\''a/submodule/one'\\'' is in submodule '\\''a/submodule'\\''\"\n+\ttest_stderr \"fatal: Pathspec '\\''submodule/one'\\'' is in submodule '\\''a/submodule'\\''\"\n '\n \n ############################################################################\n-- \n1.8.2.83.gc99314b\n"},{"id":"223296","messageId":"1373790969-13000-24-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 23/46] add: convert to use parse_pathspec","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:35:46Z","receivedAt":"2013-07-14T08:35:46Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n builtin/add.c | 103 +++++++++++++++++++++++++---------------------------------\n pathspec.c    |  43 ------------------------\n 2 files changed, 45 insertions(+), 101 deletions(-)\n\ndiff --git a/builtin/add.c b/builtin/add.c\nindex f45d9d4..9a7235e 100644\n--- a/builtin/add.c\n+++ b/builtin/add.c\n@@ -226,21 +226,6 @@ static char *prune_directory(struct dir_struct *dir, const char **pathspec,\n \treturn seen;\n }\n \n-/*\n- * Checks the index to see whether any path in pathspec refers to\n- * something inside a submodule.  If so, dies with an error message.\n- */\n-static void treat_gitlinks(const char **pathspec)\n-{\n-\tint i;\n-\n-\tif (!pathspec || !*pathspec)\n-\t\treturn;\n-\n-\tfor (i = 0; pathspec[i]; i++)\n-\t\tpathspec[i] = check_path_for_gitlink(pathspec[i]);\n-}\n-\n static void refresh(int verbose, const char **pathspec)\n {\n \tchar *seen;\n@@ -258,25 +243,6 @@ static void refresh(int verbose, const char **pathspec)\n         free(seen);\n }\n \n-/*\n- * Normalizes argv relative to prefix, via get_pathspec(), and then\n- * runs die_if_path_beyond_symlink() on each path in the normalized\n- * list.\n- */\n-static const char **validate_pathspec(const char **argv, const char *prefix)\n-{\n-\tconst char **pathspec = get_pathspec(prefix, argv);\n-\n-\tif (pathspec) {\n-\t\tconst char **p;\n-\t\tfor (p = pathspec; *p; p++) {\n-\t\t\tdie_if_path_beyond_symlink(*p, prefix);\n-\t\t}\n-\t}\n-\n-\treturn pathspec;\n-}\n-\n int run_add_interactive(const char *revision, const char *patch_mode,\n \t\t\tconst char **pathspec)\n {\n@@ -308,17 +274,23 @@ int run_add_interactive(const char *revision, const char *patch_mode,\n \n int interactive_add(int argc, const char **argv, const char *prefix, int patch)\n {\n-\tconst char **pathspec = NULL;\n+\tstruct pathspec pathspec;\n \n-\tif (argc) {\n-\t\tpathspec = validate_pathspec(argv, prefix);\n-\t\tif (!pathspec)\n-\t\t\treturn -1;\n-\t}\n+\t/*\n+\t * git-add--interactive itself does not parse pathspec. It\n+\t * simply passes the pathspec to other builtin commands. Let's\n+\t * hope all of them support all magic, or we'll need to limit\n+\t * the magic here. There is still a problem with prefix. But\n+\t * that'll be worked on later on.\n+\t */\n+\tparse_pathspec(&pathspec, PATHSPEC_ALL_MAGIC & ~PATHSPEC_FROMTOP,\n+\t\t       PATHSPEC_PREFER_FULL |\n+\t\t       PATHSPEC_SYMLINK_LEADING_PATH,\n+\t\t       prefix, argv);\n \n \treturn run_add_interactive(NULL,\n \t\t\t\t   patch ? \"--patch\" : NULL,\n-\t\t\t\t   pathspec);\n+\t\t\t\t   pathspec.raw);\n }\n \n static int edit_patch(int argc, const char **argv, const char *prefix)\n@@ -445,7 +417,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)\n {\n \tint exit_status = 0;\n \tint newfd;\n-\tconst char **pathspec;\n+\tstruct pathspec pathspec;\n \tstruct dir_struct dir;\n \tint flags;\n \tint add_new_files;\n@@ -526,11 +498,19 @@ int cmd_add(int argc, const char **argv, const char *prefix)\n \t\tfprintf(stderr, _(\"Maybe you wanted to say 'git add .'?\\n\"));\n \t\treturn 0;\n \t}\n-\tpathspec = validate_pathspec(argv, prefix);\n \n \tif (read_cache() < 0)\n \t\tdie(_(\"index file corrupt\"));\n-\ttreat_gitlinks(pathspec);\n+\n+\t/*\n+\t * Check the \"pathspec '%s' did not match any files\" block\n+\t * below before enabling new magic.\n+\t */\n+\tparse_pathspec(&pathspec, 0,\n+\t\t       PATHSPEC_PREFER_FULL |\n+\t\t       PATHSPEC_SYMLINK_LEADING_PATH |\n+\t\t       PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE,\n+\t\t       prefix, argv);\n \n \tif (add_new_files) {\n \t\tint baselen;\n@@ -543,34 +523,40 @@ int cmd_add(int argc, const char **argv, const char *prefix)\n \t\t}\n \n \t\t/* This picks up the paths that are not tracked */\n-\t\tbaselen = fill_directory(&dir, implicit_dot ? NULL : pathspec);\n-\t\tif (pathspec)\n-\t\t\tseen = prune_directory(&dir, pathspec, baselen,\n+\t\tbaselen = fill_directory(&dir, implicit_dot ? NULL : pathspec.raw);\n+\t\tif (pathspec.nr)\n+\t\t\tseen = prune_directory(&dir, pathspec.raw, baselen,\n \t\t\t\t\timplicit_dot ? WARN_IMPLICIT_DOT : 0);\n \t}\n \n \tif (refresh_only) {\n-\t\trefresh(verbose, pathspec);\n+\t\trefresh(verbose, pathspec.raw);\n \t\tgoto finish;\n \t}\n \tif (implicit_dot && prefix)\n \t\trefresh_cache(REFRESH_QUIET);\n \n-\tif (pathspec) {\n+\tif (pathspec.nr) {\n \t\tint i;\n \n \t\tif (!seen)\n-\t\t\tseen = find_pathspecs_matching_against_index(pathspec);\n-\t\tfor (i = 0; pathspec[i]; i++) {\n-\t\t\tif (!seen[i] && pathspec[i][0]\n-\t\t\t    && !file_exists(pathspec[i])) {\n+\t\t\tseen = find_pathspecs_matching_against_index(pathspec.raw);\n+\n+\t\t/*\n+\t\t * file_exists() assumes exact match\n+\t\t */\n+\t\tGUARD_PATHSPEC(&pathspec, PATHSPEC_FROMTOP);\n+\n+\t\tfor (i = 0; pathspec.raw[i]; i++) {\n+\t\t\tif (!seen[i] && pathspec.raw[i][0]\n+\t\t\t    && !file_exists(pathspec.raw[i])) {\n \t\t\t\tif (ignore_missing) {\n \t\t\t\t\tint dtype = DT_UNKNOWN;\n-\t\t\t\t\tif (is_excluded(&dir, pathspec[i], &dtype))\n-\t\t\t\t\t\tdir_add_ignored(&dir, pathspec[i], strlen(pathspec[i]));\n+\t\t\t\t\tif (is_excluded(&dir, pathspec.raw[i], &dtype))\n+\t\t\t\t\t\tdir_add_ignored(&dir, pathspec.raw[i], strlen(pathspec.raw[i]));\n \t\t\t\t} else\n \t\t\t\t\tdie(_(\"pathspec '%s' did not match any files\"),\n-\t\t\t\t\t    pathspec[i]);\n+\t\t\t\t\t    pathspec.raw[i]);\n \t\t\t}\n \t\t}\n \t\tfree(seen);\n@@ -586,10 +572,11 @@ int cmd_add(int argc, const char **argv, const char *prefix)\n \t\t */\n \t\tupdate_data.implicit_dot = prefix;\n \t\tupdate_data.implicit_dot_len = strlen(prefix);\n-\t\tpathspec = NULL;\n+\t\tfree_pathspec(&pathspec);\n+\t\tmemset(&pathspec, 0, sizeof(pathspec));\n \t}\n \tupdate_data.flags = flags & ~ADD_CACHE_IMPLICIT_DOT;\n-\tupdate_files_in_cache(prefix, pathspec, &update_data);\n+\tupdate_files_in_cache(prefix, pathspec.raw, &update_data);\n \n \texit_status |= !!update_data.add_errors;\n \tif (add_new_files)\ndiff --git a/pathspec.c b/pathspec.c\nindex 5c9631a..2774bda 100644\n--- a/pathspec.c\n+++ b/pathspec.c\n@@ -58,49 +58,6 @@ char *find_pathspecs_matching_against_index(const char **pathspec)\n }\n \n /*\n- * Check the index to see whether path refers to a submodule, or\n- * something inside a submodule.  If the former, returns the path with\n- * any trailing slash stripped.  If the latter, dies with an error\n- * message.\n- */\n-const char *check_path_for_gitlink(const char *path)\n-{\n-\tint i, path_len = strlen(path);\n-\tfor (i = 0; i < active_nr; i++) {\n-\t\tstruct cache_entry *ce = active_cache[i];\n-\t\tif (S_ISGITLINK(ce->ce_mode)) {\n-\t\t\tint ce_len = ce_namelen(ce);\n-\t\t\tif (path_len <= ce_len || path[ce_len] != '/' ||\n-\t\t\t    memcmp(ce->name, path, ce_len))\n-\t\t\t\t/* path does not refer to this\n-\t\t\t\t * submodule or anything inside it */\n-\t\t\t\tcontinue;\n-\t\t\tif (path_len == ce_len + 1) {\n-\t\t\t\t/* path refers to submodule;\n-\t\t\t\t * strip trailing slash */\n-\t\t\t\treturn xstrndup(ce->name, ce_len);\n-\t\t\t} else {\n-\t\t\t\tdie (_(\"Path '%s' is in submodule '%.*s'\"),\n-\t\t\t\t     path, ce_len, ce->name);\n-\t\t\t}\n-\t\t}\n-\t}\n-\treturn path;\n-}\n-\n-/*\n- * Dies if the given path refers to a file inside a symlinked\n- * directory in the index.\n- */\n-void die_if_path_beyond_symlink(const char *path, const char *prefix)\n-{\n-\tif (has_symlink_leading_path(path, strlen(path))) {\n-\t\tint len = prefix ? strlen(prefix) : 0;\n-\t\tdie(_(\"'%s' is beyond a symbolic link\"), path + len);\n-\t}\n-}\n-\n-/*\n  * Magic pathspec\n  *\n  * Possible future magic semantics include stuff like:\n-- \n1.8.2.83.gc99314b\n"},{"id":"223297","messageId":"1373790969-13000-25-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 24/46] reset: convert to use parse_pathspec","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:35:47Z","receivedAt":"2013-07-14T08:35:47Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n builtin/reset.c | 26 ++++++++++++++++----------\n 1 file changed, 16 insertions(+), 10 deletions(-)\n\ndiff --git a/builtin/reset.c b/builtin/reset.c\nindex afa6e02..313b296 100644\n--- a/builtin/reset.c\n+++ b/builtin/reset.c\n@@ -174,7 +174,10 @@ static void die_if_unmerged_cache(int reset_type)\n \n }\n \n-static const char **parse_args(const char **argv, const char *prefix, const char **rev_ret)\n+static void parse_args(struct pathspec *pathspec,\n+\t\t       const char **argv, const char *prefix,\n+\t\t       int patch_mode,\n+\t\t       const char **rev_ret)\n {\n \tconst char *rev = \"HEAD\";\n \tunsigned char unused[20];\n@@ -216,7 +219,10 @@ static const char **parse_args(const char **argv, const char *prefix, const char\n \t\t}\n \t}\n \t*rev_ret = rev;\n-\treturn argv[0] ? get_pathspec(prefix, argv) : NULL;\n+\tparse_pathspec(pathspec,\n+\t\t       patch_mode ? PATHSPEC_ALL_MAGIC & ~PATHSPEC_FROMTOP : 0,\n+\t\t       PATHSPEC_PREFER_FULL,\n+\t\t       prefix, argv);\n }\n \n static int update_refs(const char *rev, const unsigned char *sha1)\n@@ -246,7 +252,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)\n \tint patch_mode = 0, unborn;\n \tconst char *rev;\n \tunsigned char sha1[20];\n-\tconst char **pathspec = NULL;\n+\tstruct pathspec pathspec;\n \tconst struct option options[] = {\n \t\tOPT__QUIET(&quiet, N_(\"be quiet, only report errors\")),\n \t\tOPT_SET_INT(0, \"mixed\", &reset_type,\n@@ -266,13 +272,13 @@ int cmd_reset(int argc, const char **argv, const char *prefix)\n \n \targc = parse_options(argc, argv, prefix, options, git_reset_usage,\n \t\t\t\t\t\tPARSE_OPT_KEEP_DASHDASH);\n-\tpathspec = parse_args(argv, prefix, &rev);\n+\tparse_args(&pathspec, argv, prefix, patch_mode, &rev);\n \n \tunborn = !strcmp(rev, \"HEAD\") && get_sha1(\"HEAD\", sha1);\n \tif (unborn) {\n \t\t/* reset on unborn branch: treat as reset to empty tree */\n \t\thashcpy(sha1, EMPTY_TREE_SHA1_BIN);\n-\t} else if (!pathspec) {\n+\t} else if (!pathspec.nr) {\n \t\tstruct commit *commit;\n \t\tif (get_sha1_committish(rev, sha1))\n \t\t\tdie(_(\"Failed to resolve '%s' as a valid revision.\"), rev);\n@@ -293,13 +299,13 @@ int cmd_reset(int argc, const char **argv, const char *prefix)\n \tif (patch_mode) {\n \t\tif (reset_type != NONE)\n \t\t\tdie(_(\"--patch is incompatible with --{hard,mixed,soft}\"));\n-\t\treturn run_add_interactive(sha1_to_hex(sha1), \"--patch=reset\", pathspec);\n+\t\treturn run_add_interactive(sha1_to_hex(sha1), \"--patch=reset\", pathspec.raw);\n \t}\n \n \t/* git reset tree [--] paths... can be used to\n \t * load chosen paths from the tree into the index without\n \t * affecting the working tree nor HEAD. */\n-\tif (pathspec) {\n+\tif (pathspec.nr) {\n \t\tif (reset_type == MIXED)\n \t\t\twarning(_(\"--mixed with paths is deprecated; use 'git reset -- <paths>' instead.\"));\n \t\telse if (reset_type != NONE)\n@@ -326,7 +332,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)\n \t\tstruct lock_file *lock = xcalloc(1, sizeof(struct lock_file));\n \t\tint newfd = hold_locked_index(lock, 1);\n \t\tif (reset_type == MIXED) {\n-\t\t\tif (read_from_tree(pathspec, sha1))\n+\t\t\tif (read_from_tree(pathspec.raw, sha1))\n \t\t\t\treturn 1;\n \t\t} else {\n \t\t\tint err = reset_index(sha1, reset_type, quiet);\n@@ -347,7 +353,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)\n \t\t\tdie(_(\"Could not write new index file.\"));\n \t}\n \n-\tif (!pathspec && !unborn) {\n+\tif (!pathspec.nr && !unborn) {\n \t\t/* Any resets without paths update HEAD to the head being\n \t\t * switched to, saving the previous head in ORIG_HEAD before. */\n \t\tupdate_ref_status = update_refs(rev, sha1);\n@@ -355,7 +361,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)\n \t\tif (reset_type == HARD && !update_ref_status && !quiet)\n \t\t\tprint_new_head_line(lookup_commit_reference(sha1));\n \t}\n-\tif (!pathspec)\n+\tif (!pathspec.nr)\n \t\tremove_branch_state();\n \n \treturn update_ref_status;\n-- \n1.8.2.83.gc99314b\n"},{"id":"223298","messageId":"1373790969-13000-26-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 25/46] line-log: convert to use parse_pathspec","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:35:48Z","receivedAt":"2013-07-14T08:35:48Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n line-log.c | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)\n\ndiff --git a/line-log.c b/line-log.c\nindex 4bbb09b..843a334 100644\n--- a/line-log.c\n+++ b/line-log.c\n@@ -747,7 +747,7 @@ void line_log_init(struct rev_info *rev, const char *prefix, struct string_list\n \t\t\tr = r->next;\n \t\t}\n \t\tpaths[count] = NULL;\n-\t\tinit_pathspec(&rev->diffopt.pathspec, paths);\n+\t\tparse_pathspec(&rev->diffopt.pathspec, 0, 0, \"\", paths);\n \t\tfree(paths);\n \t}\n }\n-- \n1.8.2.83.gc99314b\n"},{"id":"223299","messageId":"1373790969-13000-27-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 26/46] Convert read_cache_preload() to take struct pathspec","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:35:49Z","receivedAt":"2013-07-14T08:35:49Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n builtin/checkout.c   |  2 +-\n builtin/commit.c     |  4 ++--\n builtin/diff-files.c |  2 +-\n builtin/diff-index.c |  2 +-\n builtin/diff.c       |  4 ++--\n cache.h              |  2 +-\n preload-index.c      | 20 +++++++++++---------\n 7 files changed, 19 insertions(+), 17 deletions(-)\n\ndiff --git a/builtin/checkout.c b/builtin/checkout.c\nindex 6721de2..2f0fb8d 100644\n--- a/builtin/checkout.c\n+++ b/builtin/checkout.c\n@@ -262,7 +262,7 @@ static int checkout_paths(const struct checkout_opts *opts,\n \tlock_file = xcalloc(1, sizeof(struct lock_file));\n \n \tnewfd = hold_locked_index(lock_file, 1);\n-\tif (read_cache_preload(opts->pathspec.raw) < 0)\n+\tif (read_cache_preload(&opts->pathspec) < 0)\n \t\treturn error(_(\"corrupt index file\"));\n \n \tif (opts->source_tree)\ndiff --git a/builtin/commit.c b/builtin/commit.c\nindex 64d1a3d..0344ec7 100644\n--- a/builtin/commit.c\n+++ b/builtin/commit.c\n@@ -294,7 +294,7 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,\n \t\t       PATHSPEC_PREFER_FULL,\n \t\t       prefix, argv);\n \n-\tif (read_cache_preload(pathspec.raw) < 0)\n+\tif (read_cache_preload(&pathspec) < 0)\n \t\tdie(_(\"index file corrupt\"));\n \n \tif (interactive) {\n@@ -1245,7 +1245,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)\n \t\t       PATHSPEC_PREFER_FULL,\n \t\t       prefix, argv);\n \n-\tread_cache_preload(s.pathspec.raw);\n+\tread_cache_preload(&s.pathspec);\n \trefresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, s.pathspec.raw, NULL, NULL);\n \n \tfd = hold_locked_index(&index_lock, 0);\ndiff --git a/builtin/diff-files.c b/builtin/diff-files.c\nindex 46085f8..9200069 100644\n--- a/builtin/diff-files.c\n+++ b/builtin/diff-files.c\n@@ -61,7 +61,7 @@ int cmd_diff_files(int argc, const char **argv, const char *prefix)\n \t    (rev.diffopt.output_format & DIFF_FORMAT_PATCH))\n \t\trev.combine_merges = rev.dense_combined_merges = 1;\n \n-\tif (read_cache_preload(rev.diffopt.pathspec.raw) < 0) {\n+\tif (read_cache_preload(&rev.diffopt.pathspec) < 0) {\n \t\tperror(\"read_cache_preload\");\n \t\treturn -1;\n \t}\ndiff --git a/builtin/diff-index.c b/builtin/diff-index.c\nindex 1c737f7..ce15b23 100644\n--- a/builtin/diff-index.c\n+++ b/builtin/diff-index.c\n@@ -43,7 +43,7 @@ int cmd_diff_index(int argc, const char **argv, const char *prefix)\n \t\tusage(diff_cache_usage);\n \tif (!cached) {\n \t\tsetup_work_tree();\n-\t\tif (read_cache_preload(rev.diffopt.pathspec.raw) < 0) {\n+\t\tif (read_cache_preload(&rev.diffopt.pathspec) < 0) {\n \t\t\tperror(\"read_cache_preload\");\n \t\t\treturn -1;\n \t\t}\ndiff --git a/builtin/diff.c b/builtin/diff.c\nindex 6bb41af..bb84ba0 100644\n--- a/builtin/diff.c\n+++ b/builtin/diff.c\n@@ -140,7 +140,7 @@ static int builtin_diff_index(struct rev_info *revs,\n \t\tusage(builtin_diff_usage);\n \tif (!cached) {\n \t\tsetup_work_tree();\n-\t\tif (read_cache_preload(revs->diffopt.pathspec.raw) < 0) {\n+\t\tif (read_cache_preload(&revs->diffopt.pathspec) < 0) {\n \t\t\tperror(\"read_cache_preload\");\n \t\t\treturn -1;\n \t\t}\n@@ -242,7 +242,7 @@ static int builtin_diff_files(struct rev_info *revs, int argc, const char **argv\n \t\trevs->combine_merges = revs->dense_combined_merges = 1;\n \n \tsetup_work_tree();\n-\tif (read_cache_preload(revs->diffopt.pathspec.raw) < 0) {\n+\tif (read_cache_preload(&revs->diffopt.pathspec) < 0) {\n \t\tperror(\"read_cache_preload\");\n \t\treturn -1;\n \t}\ndiff --git a/cache.h b/cache.h\nindex fd0a6f8..0370546 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -449,7 +449,7 @@ extern int init_db(const char *template_dir, unsigned int flags);\n \n /* Initialize and use the cache information */\n extern int read_index(struct index_state *);\n-extern int read_index_preload(struct index_state *, const char **pathspec);\n+extern int read_index_preload(struct index_state *, const struct pathspec *pathspec);\n extern int read_index_from(struct index_state *, const char *path);\n extern int is_index_unborn(struct index_state *);\n extern int read_index_unmerged(struct index_state *);\ndiff --git a/preload-index.c b/preload-index.c\nindex cddfffa..8c44ceb 100644\n--- a/preload-index.c\n+++ b/preload-index.c\n@@ -5,7 +5,8 @@\n #include \"pathspec.h\"\n \n #ifdef NO_PTHREADS\n-static void preload_index(struct index_state *index, const char **pathspec)\n+static void preload_index(struct index_state *index,\n+\t\t\t  const struct pathspec *pathspec)\n {\n \t; /* nothing */\n }\n@@ -25,7 +26,7 @@ static void preload_index(struct index_state *index, const char **pathspec)\n struct thread_data {\n \tpthread_t pthread;\n \tstruct index_state *index;\n-\tconst char **pathspec;\n+\tstruct pathspec pathspec;\n \tint offset, nr;\n };\n \n@@ -36,9 +37,7 @@ static void *preload_thread(void *_data)\n \tstruct index_state *index = p->index;\n \tstruct cache_entry **cep = index->cache + p->offset;\n \tstruct cache_def cache;\n-\tstruct pathspec pathspec;\n \n-\tinit_pathspec(&pathspec, p->pathspec);\n \tmemset(&cache, 0, sizeof(cache));\n \tnr = p->nr;\n \tif (nr + p->offset > index->cache_nr)\n@@ -54,7 +53,7 @@ static void *preload_thread(void *_data)\n \t\t\tcontinue;\n \t\tif (ce_uptodate(ce))\n \t\t\tcontinue;\n-\t\tif (!ce_path_match(ce, &pathspec))\n+\t\tif (!ce_path_match(ce, &p->pathspec))\n \t\t\tcontinue;\n \t\tif (threaded_has_symlink_leading_path(&cache, ce->name, ce_namelen(ce)))\n \t\t\tcontinue;\n@@ -64,11 +63,11 @@ static void *preload_thread(void *_data)\n \t\t\tcontinue;\n \t\tce_mark_uptodate(ce);\n \t} while (--nr > 0);\n-\tfree_pathspec(&pathspec);\n \treturn NULL;\n }\n \n-static void preload_index(struct index_state *index, const char **pathspec)\n+static void preload_index(struct index_state *index,\n+\t\t\t  const struct pathspec *pathspec)\n {\n \tint threads, i, work, offset;\n \tstruct thread_data data[MAX_PARALLEL];\n@@ -83,10 +82,12 @@ static void preload_index(struct index_state *index, const char **pathspec)\n \t\tthreads = MAX_PARALLEL;\n \toffset = 0;\n \twork = DIV_ROUND_UP(index->cache_nr, threads);\n+\tmemset(&data, 0, sizeof(data));\n \tfor (i = 0; i < threads; i++) {\n \t\tstruct thread_data *p = data+i;\n \t\tp->index = index;\n-\t\tp->pathspec = pathspec;\n+\t\tif (pathspec)\n+\t\t\tcopy_pathspec(&p->pathspec, pathspec);\n \t\tp->offset = offset;\n \t\tp->nr = work;\n \t\toffset += work;\n@@ -101,7 +102,8 @@ static void preload_index(struct index_state *index, const char **pathspec)\n }\n #endif\n \n-int read_index_preload(struct index_state *index, const char **pathspec)\n+int read_index_preload(struct index_state *index,\n+\t\t       const struct pathspec *pathspec)\n {\n \tint retval = read_index(index);\n \n-- \n1.8.2.83.gc99314b\n"},{"id":"223300","messageId":"1373790969-13000-28-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 27/46] Convert run_add_interactive to use struct pathspec","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:35:50Z","receivedAt":"2013-07-14T08:35:50Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"This passes the pathspec, more or less unmodified, to\ngit-add--interactive. The command itself does not process pathspec. It\nsimply passes the pathspec to other builtin commands. So if all those\ncommands support pathspec, we're good.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n builtin/add.c      | 26 ++++++++++----------------\n builtin/checkout.c |  9 ++++-----\n builtin/reset.c    |  8 ++++----\n commit.h           |  2 +-\n 4 files changed, 19 insertions(+), 26 deletions(-)\n\ndiff --git a/builtin/add.c b/builtin/add.c\nindex 9a7235e..d039fc9 100644\n--- a/builtin/add.c\n+++ b/builtin/add.c\n@@ -244,16 +244,12 @@ static void refresh(int verbose, const char **pathspec)\n }\n \n int run_add_interactive(const char *revision, const char *patch_mode,\n-\t\t\tconst char **pathspec)\n+\t\t\tconst struct pathspec *pathspec)\n {\n-\tint status, ac, pc = 0;\n+\tint status, ac, i;\n \tconst char **args;\n \n-\tif (pathspec)\n-\t\twhile (pathspec[pc])\n-\t\t\tpc++;\n-\n-\targs = xcalloc(sizeof(const char *), (pc + 5));\n+\targs = xcalloc(sizeof(const char *), (pathspec->nr + 6));\n \tac = 0;\n \targs[ac++] = \"add--interactive\";\n \tif (patch_mode)\n@@ -261,11 +257,9 @@ int run_add_interactive(const char *revision, const char *patch_mode,\n \tif (revision)\n \t\targs[ac++] = revision;\n \targs[ac++] = \"--\";\n-\tif (pc) {\n-\t\tmemcpy(&(args[ac]), pathspec, sizeof(const char *) * pc);\n-\t\tac += pc;\n-\t}\n-\targs[ac] = NULL;\n+\tfor (i = 0; i < pathspec->nr; i++)\n+\t\t/* pass original pathspec, to be re-parsed */\n+\t\targs[ac++] = pathspec->items[i].original;\n \n \tstatus = run_command_v_opt(args, RUN_GIT_CMD);\n \tfree(args);\n@@ -280,17 +274,17 @@ int interactive_add(int argc, const char **argv, const char *prefix, int patch)\n \t * git-add--interactive itself does not parse pathspec. It\n \t * simply passes the pathspec to other builtin commands. Let's\n \t * hope all of them support all magic, or we'll need to limit\n-\t * the magic here. There is still a problem with prefix. But\n-\t * that'll be worked on later on.\n+\t * the magic here.\n \t */\n \tparse_pathspec(&pathspec, PATHSPEC_ALL_MAGIC & ~PATHSPEC_FROMTOP,\n \t\t       PATHSPEC_PREFER_FULL |\n-\t\t       PATHSPEC_SYMLINK_LEADING_PATH,\n+\t\t       PATHSPEC_SYMLINK_LEADING_PATH |\n+\t\t       PATHSPEC_PREFIX_ORIGIN,\n \t\t       prefix, argv);\n \n \treturn run_add_interactive(NULL,\n \t\t\t\t   patch ? \"--patch\" : NULL,\n-\t\t\t\t   pathspec.raw);\n+\t\t\t\t   &pathspec);\n }\n \n static int edit_patch(int argc, const char **argv, const char *prefix)\ndiff --git a/builtin/checkout.c b/builtin/checkout.c\nindex 2f0fb8d..5d31767 100644\n--- a/builtin/checkout.c\n+++ b/builtin/checkout.c\n@@ -257,7 +257,7 @@ static int checkout_paths(const struct checkout_opts *opts,\n \n \tif (opts->patch_mode)\n \t\treturn run_add_interactive(revision, \"--patch=checkout\",\n-\t\t\t\t\t   opts->pathspec.raw);\n+\t\t\t\t\t   &opts->pathspec);\n \n \tlock_file = xcalloc(1, sizeof(struct lock_file));\n \n@@ -1159,10 +1159,9 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)\n \t\t * cannot handle. Magic mask is pretty safe to be\n \t\t * lifted for new magic when opts.patch_mode == 0.\n \t\t */\n-\t\tparse_pathspec(&opts.pathspec,\n-\t\t\t       opts.patch_mode == 0 ? 0 :\n-\t\t\t       (PATHSPEC_ALL_MAGIC & ~PATHSPEC_FROMTOP),\n-\t\t\t       0, prefix, argv);\n+\t\tparse_pathspec(&opts.pathspec, 0,\n+\t\t\t       opts.patch_mode ? PATHSPEC_PREFIX_ORIGIN : 0,\n+\t\t\t       prefix, argv);\n \n \t\tif (!opts.pathspec.nr)\n \t\t\tdie(_(\"invalid path specification\"));\ndiff --git a/builtin/reset.c b/builtin/reset.c\nindex 313b296..9bef869 100644\n--- a/builtin/reset.c\n+++ b/builtin/reset.c\n@@ -219,9 +219,9 @@ static void parse_args(struct pathspec *pathspec,\n \t\t}\n \t}\n \t*rev_ret = rev;\n-\tparse_pathspec(pathspec,\n-\t\t       patch_mode ? PATHSPEC_ALL_MAGIC & ~PATHSPEC_FROMTOP : 0,\n-\t\t       PATHSPEC_PREFER_FULL,\n+\tparse_pathspec(pathspec, 0,\n+\t\t       PATHSPEC_PREFER_FULL |\n+\t\t       (patch_mode ? PATHSPEC_PREFIX_ORIGIN : 0),\n \t\t       prefix, argv);\n }\n \n@@ -299,7 +299,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)\n \tif (patch_mode) {\n \t\tif (reset_type != NONE)\n \t\t\tdie(_(\"--patch is incompatible with --{hard,mixed,soft}\"));\n-\t\treturn run_add_interactive(sha1_to_hex(sha1), \"--patch=reset\", pathspec.raw);\n+\t\treturn run_add_interactive(sha1_to_hex(sha1), \"--patch=reset\", &pathspec);\n \t}\n \n \t/* git reset tree [--] paths... can be used to\ndiff --git a/commit.h b/commit.h\nindex 4d452dc..1f1350c 100644\n--- a/commit.h\n+++ b/commit.h\n@@ -194,7 +194,7 @@ int in_merge_bases_many(struct commit *, int, struct commit **);\n \n extern int interactive_add(int argc, const char **argv, const char *prefix, int patch);\n extern int run_add_interactive(const char *revision, const char *patch_mode,\n-\t\t\t       const char **pathspec);\n+\t\t\t       const struct pathspec *pathspec);\n \n static inline int single_parent(struct commit *commit)\n {\n-- \n1.8.2.83.gc99314b\n"},{"id":"223301","messageId":"1373790969-13000-29-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 28/46] Convert unmerge_cache to take struct pathspec","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:35:51Z","receivedAt":"2013-07-14T08:35:51Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n rerere.c       | 2 +-\n resolve-undo.c | 4 ++--\n resolve-undo.h | 2 +-\n 3 files changed, 4 insertions(+), 4 deletions(-)\n\ndiff --git a/rerere.c b/rerere.c\nindex 27afbfe..4105bca 100644\n--- a/rerere.c\n+++ b/rerere.c\n@@ -668,7 +668,7 @@ int rerere_forget(struct pathspec *pathspec)\n \n \tfd = setup_rerere(&merge_rr, RERERE_NOAUTOUPDATE);\n \n-\tunmerge_cache(pathspec->raw);\n+\tunmerge_cache(pathspec);\n \tfind_conflict(&conflict);\n \tfor (i = 0; i < conflict.nr; i++) {\n \t\tstruct string_list_item *it = &conflict.items[i];\ndiff --git a/resolve-undo.c b/resolve-undo.c\nindex 639eb9c..4b78e6f 100644\n--- a/resolve-undo.c\n+++ b/resolve-undo.c\n@@ -173,7 +173,7 @@ void unmerge_marked_index(struct index_state *istate)\n \t}\n }\n \n-void unmerge_index(struct index_state *istate, const char **pathspec)\n+void unmerge_index(struct index_state *istate, const struct pathspec *pathspec)\n {\n \tint i;\n \n@@ -182,7 +182,7 @@ void unmerge_index(struct index_state *istate, const char **pathspec)\n \n \tfor (i = 0; i < istate->cache_nr; i++) {\n \t\tstruct cache_entry *ce = istate->cache[i];\n-\t\tif (!match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, NULL))\n+\t\tif (!match_pathspec_depth(pathspec, ce->name, ce_namelen(ce), 0, NULL))\n \t\t\tcontinue;\n \t\ti = unmerge_index_entry_at(istate, i);\n \t}\ndiff --git a/resolve-undo.h b/resolve-undo.h\nindex 7a30206..4630645 100644\n--- a/resolve-undo.h\n+++ b/resolve-undo.h\n@@ -11,7 +11,7 @@ extern void resolve_undo_write(struct strbuf *, struct string_list *);\n extern struct string_list *resolve_undo_read(const char *, unsigned long);\n extern void resolve_undo_clear_index(struct index_state *);\n extern int unmerge_index_entry_at(struct index_state *, int);\n-extern void unmerge_index(struct index_state *, const char **);\n+extern void unmerge_index(struct index_state *, const struct pathspec *);\n extern void unmerge_marked_index(struct index_state *);\n \n #endif\n-- \n1.8.2.83.gc99314b\n"},{"id":"223302","messageId":"1373790969-13000-30-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 29/46] checkout: convert read_tree_some to take struct pathspec","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:35:52Z","receivedAt":"2013-07-14T08:35:52Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n builtin/checkout.c | 9 +++------\n tree.c             | 4 ++--\n tree.h             | 2 +-\n 3 files changed, 6 insertions(+), 9 deletions(-)\n\ndiff --git a/builtin/checkout.c b/builtin/checkout.c\nindex 5d31767..c2f3571 100644\n--- a/builtin/checkout.c\n+++ b/builtin/checkout.c\n@@ -83,12 +83,9 @@ static int update_some(const unsigned char *sha1, const char *base, int baselen,\n \treturn 0;\n }\n \n-static int read_tree_some(struct tree *tree, const char **pathspec)\n+static int read_tree_some(struct tree *tree, const struct pathspec *pathspec)\n {\n-\tstruct pathspec ps;\n-\tinit_pathspec(&ps, pathspec);\n-\tread_tree_recursive(tree, \"\", 0, 0, &ps, update_some, NULL);\n-\tfree_pathspec(&ps);\n+\tread_tree_recursive(tree, \"\", 0, 0, pathspec, update_some, NULL);\n \n \t/* update the index with the given tree's info\n \t * for all args, expanding wildcards, and exit\n@@ -266,7 +263,7 @@ static int checkout_paths(const struct checkout_opts *opts,\n \t\treturn error(_(\"corrupt index file\"));\n \n \tif (opts->source_tree)\n-\t\tread_tree_some(opts->source_tree, opts->pathspec.raw);\n+\t\tread_tree_some(opts->source_tree, &opts->pathspec);\n \n \tps_matched = xcalloc(1, opts->pathspec.nr);\n \ndiff --git a/tree.c b/tree.c\nindex 62fed63..ff72f67 100644\n--- a/tree.c\n+++ b/tree.c\n@@ -47,7 +47,7 @@ static int read_one_entry_quick(const unsigned char *sha1, const char *base, int\n }\n \n static int read_tree_1(struct tree *tree, struct strbuf *base,\n-\t\t       int stage, struct pathspec *pathspec,\n+\t\t       int stage, const struct pathspec *pathspec,\n \t\t       read_tree_fn_t fn, void *context)\n {\n \tstruct tree_desc desc;\n@@ -116,7 +116,7 @@ static int read_tree_1(struct tree *tree, struct strbuf *base,\n \n int read_tree_recursive(struct tree *tree,\n \t\t\tconst char *base, int baselen,\n-\t\t\tint stage, struct pathspec *pathspec,\n+\t\t\tint stage, const struct pathspec *pathspec,\n \t\t\tread_tree_fn_t fn, void *context)\n {\n \tstruct strbuf sb = STRBUF_INIT;\ndiff --git a/tree.h b/tree.h\nindex 69bcb5e..9dc90ba 100644\n--- a/tree.h\n+++ b/tree.h\n@@ -25,7 +25,7 @@ typedef int (*read_tree_fn_t)(const unsigned char *, const char *, int, const ch\n \n extern int read_tree_recursive(struct tree *tree,\n \t\t\t       const char *base, int baselen,\n-\t\t\t       int stage, struct pathspec *pathspec,\n+\t\t\t       int stage, const struct pathspec *pathspec,\n \t\t\t       read_tree_fn_t fn, void *context);\n \n extern int read_tree(struct tree *tree, int stage, struct pathspec *pathspec);\n-- \n1.8.2.83.gc99314b\n"},{"id":"223303","messageId":"1373790969-13000-31-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 30/46] Convert report_path_error to take struct pathspec","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:35:53Z","receivedAt":"2013-07-14T08:35:53Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n builtin/checkout.c |  2 +-\n builtin/commit.c   | 14 ++++++--------\n builtin/ls-files.c | 19 +++++++++++--------\n cache.h            |  2 +-\n 4 files changed, 19 insertions(+), 18 deletions(-)\n\ndiff --git a/builtin/checkout.c b/builtin/checkout.c\nindex c2f3571..7ea1100 100644\n--- a/builtin/checkout.c\n+++ b/builtin/checkout.c\n@@ -304,7 +304,7 @@ static int checkout_paths(const struct checkout_opts *opts,\n \t\t\tce->ce_flags |= CE_MATCHED;\n \t}\n \n-\tif (report_path_error(ps_matched, opts->pathspec.raw, opts->prefix)) {\n+\tif (report_path_error(ps_matched, &opts->pathspec, opts->prefix)) {\n \t\tfree(ps_matched);\n \t\treturn 1;\n \t}\ndiff --git a/builtin/commit.c b/builtin/commit.c\nindex 0344ec7..eaecf7c 100644\n--- a/builtin/commit.c\n+++ b/builtin/commit.c\n@@ -188,20 +188,18 @@ static int commit_index_files(void)\n  * and return the paths that match the given pattern in list.\n  */\n static int list_paths(struct string_list *list, const char *with_tree,\n-\t\t      const char *prefix, const char **pattern)\n+\t\t      const char *prefix, const struct pathspec *pattern)\n {\n \tint i;\n \tchar *m;\n \n-\tif (!pattern)\n+\tif (!pattern->nr)\n \t\treturn 0;\n \n-\tfor (i = 0; pattern[i]; i++)\n-\t\t;\n-\tm = xcalloc(1, i);\n+\tm = xcalloc(1, pattern->nr);\n \n \tif (with_tree) {\n-\t\tchar *max_prefix = common_prefix(pattern);\n+\t\tchar *max_prefix = common_prefix(pattern->raw);\n \t\toverlay_tree_on_cache(with_tree, max_prefix ? max_prefix : prefix);\n \t\tfree(max_prefix);\n \t}\n@@ -212,7 +210,7 @@ static int list_paths(struct string_list *list, const char *with_tree,\n \n \t\tif (ce->ce_flags & CE_UPDATE)\n \t\t\tcontinue;\n-\t\tif (!match_pathspec(pattern, ce->name, ce_namelen(ce), 0, m))\n+\t\tif (!match_pathspec_depth(pattern, ce->name, ce_namelen(ce), 0, m))\n \t\t\tcontinue;\n \t\titem = string_list_insert(list, ce->name);\n \t\tif (ce_skip_worktree(ce))\n@@ -402,7 +400,7 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,\n \n \tmemset(&partial, 0, sizeof(partial));\n \tpartial.strdup_strings = 1;\n-\tif (list_paths(&partial, !current_head ? NULL : \"HEAD\", prefix, pathspec.raw))\n+\tif (list_paths(&partial, !current_head ? NULL : \"HEAD\", prefix, &pathspec))\n \t\texit(1);\n \n \tdiscard_cache();\ndiff --git a/builtin/ls-files.c b/builtin/ls-files.c\nindex f1be425..50e6edf 100644\n--- a/builtin/ls-files.c\n+++ b/builtin/ls-files.c\n@@ -346,15 +346,16 @@ void overlay_tree_on_cache(const char *tree_name, const char *prefix)\n \t}\n }\n \n-int report_path_error(const char *ps_matched, const char **pathspec, const char *prefix)\n+int report_path_error(const char *ps_matched,\n+\t\t      const struct pathspec *pathspec,\n+\t\t      const char *prefix)\n {\n \t/*\n \t * Make sure all pathspec matched; otherwise it is an error.\n \t */\n \tstruct strbuf sb = STRBUF_INIT;\n-\tconst char *name;\n \tint num, errors = 0;\n-\tfor (num = 0; pathspec[num]; num++) {\n+\tfor (num = 0; num < pathspec->nr; num++) {\n \t\tint other, found_dup;\n \n \t\tif (ps_matched[num])\n@@ -362,13 +363,16 @@ int report_path_error(const char *ps_matched, const char **pathspec, const char\n \t\t/*\n \t\t * The caller might have fed identical pathspec\n \t\t * twice.  Do not barf on such a mistake.\n+\t\t * FIXME: parse_pathspec should have eliminated\n+\t\t * duplicate pathspec.\n \t\t */\n \t\tfor (found_dup = other = 0;\n-\t\t     !found_dup && pathspec[other];\n+\t\t     !found_dup && other < pathspec->nr;\n \t\t     other++) {\n \t\t\tif (other == num || !ps_matched[other])\n \t\t\t\tcontinue;\n-\t\t\tif (!strcmp(pathspec[other], pathspec[num]))\n+\t\t\tif (!strcmp(pathspec->items[other].original,\n+\t\t\t\t    pathspec->items[num].original))\n \t\t\t\t/*\n \t\t\t\t * Ok, we have a match already.\n \t\t\t\t */\n@@ -377,9 +381,8 @@ int report_path_error(const char *ps_matched, const char **pathspec, const char\n \t\tif (found_dup)\n \t\t\tcontinue;\n \n-\t\tname = quote_path_relative(pathspec[num], -1, &sb, prefix);\n \t\terror(\"pathspec '%s' did not match any file(s) known to git.\",\n-\t\t      name);\n+\t\t      pathspec->items[num].original);\n \t\terrors++;\n \t}\n \tstrbuf_release(&sb);\n@@ -575,7 +578,7 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)\n \n \tif (ps_matched) {\n \t\tint bad;\n-\t\tbad = report_path_error(ps_matched, pathspec.raw, prefix);\n+\t\tbad = report_path_error(ps_matched, &pathspec, prefix);\n \t\tif (bad)\n \t\t\tfprintf(stderr, \"Did you forget to 'git add'?\\n\");\n \ndiff --git a/cache.h b/cache.h\nindex 0370546..42c9920 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -1312,7 +1312,7 @@ extern int ws_blank_line(const char *line, int len, unsigned ws_rule);\n #define ws_tab_width(rule)     ((rule) & WS_TAB_WIDTH_MASK)\n \n /* ls-files */\n-int report_path_error(const char *ps_matched, const char **pathspec, const char *prefix);\n+int report_path_error(const char *ps_matched, const struct pathspec *pathspec, const char *prefix);\n void overlay_tree_on_cache(const char *tree_name, const char *prefix);\n \n char *alias_lookup(const char *alias);\n-- \n1.8.2.83.gc99314b\n"},{"id":"223304","messageId":"1373790969-13000-32-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 31/46] Convert refresh_index to take struct pathspec","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:35:54Z","receivedAt":"2013-07-14T08:35:54Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n builtin/add.c    | 15 +++++++--------\n builtin/commit.c |  2 +-\n builtin/rm.c     |  2 +-\n cache.h          |  2 +-\n read-cache.c     |  5 +++--\n 5 files changed, 13 insertions(+), 13 deletions(-)\n\ndiff --git a/builtin/add.c b/builtin/add.c\nindex d039fc9..f5d6a33 100644\n--- a/builtin/add.c\n+++ b/builtin/add.c\n@@ -226,19 +226,18 @@ static char *prune_directory(struct dir_struct *dir, const char **pathspec,\n \treturn seen;\n }\n \n-static void refresh(int verbose, const char **pathspec)\n+static void refresh(int verbose, const struct pathspec *pathspec)\n {\n \tchar *seen;\n-\tint i, specs;\n+\tint i;\n \n-\tfor (specs = 0; pathspec[specs];  specs++)\n-\t\t/* nothing */;\n-\tseen = xcalloc(specs, 1);\n+\tseen = xcalloc(pathspec->nr, 1);\n \trefresh_index(&the_index, verbose ? REFRESH_IN_PORCELAIN : REFRESH_QUIET,\n \t\t      pathspec, seen, _(\"Unstaged changes after refreshing the index:\"));\n-\tfor (i = 0; i < specs; i++) {\n+\tfor (i = 0; i < pathspec->nr; i++) {\n \t\tif (!seen[i])\n-\t\t\tdie(_(\"pathspec '%s' did not match any files\"), pathspec[i]);\n+\t\t\tdie(_(\"pathspec '%s' did not match any files\"),\n+\t\t\t    pathspec->items[i].match);\n \t}\n         free(seen);\n }\n@@ -524,7 +523,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)\n \t}\n \n \tif (refresh_only) {\n-\t\trefresh(verbose, pathspec.raw);\n+\t\trefresh(verbose, &pathspec);\n \t\tgoto finish;\n \t}\n \tif (implicit_dot && prefix)\ndiff --git a/builtin/commit.c b/builtin/commit.c\nindex eaecf7c..d34baab 100644\n--- a/builtin/commit.c\n+++ b/builtin/commit.c\n@@ -1244,7 +1244,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)\n \t\t       prefix, argv);\n \n \tread_cache_preload(&s.pathspec);\n-\trefresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, s.pathspec.raw, NULL, NULL);\n+\trefresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, &s.pathspec, NULL, NULL);\n \n \tfd = hold_locked_index(&index_lock, 0);\n \tif (0 <= fd)\ndiff --git a/builtin/rm.c b/builtin/rm.c\nindex ee0ae4c..f08561d 100644\n--- a/builtin/rm.c\n+++ b/builtin/rm.c\n@@ -314,7 +314,7 @@ int cmd_rm(int argc, const char **argv, const char *prefix)\n \t}\n \n \tparse_pathspec(&pathspec, 0, PATHSPEC_PREFER_CWD, prefix, argv);\n-\trefresh_index(&the_index, REFRESH_QUIET, pathspec.raw, NULL, NULL);\n+\trefresh_index(&the_index, REFRESH_QUIET, &pathspec, NULL, NULL);\n \n \tseen = NULL;\n \tseen = xcalloc(pathspec.nr, 1);\ndiff --git a/cache.h b/cache.h\nindex 42c9920..b294277 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -520,7 +520,7 @@ extern void fill_stat_cache_info(struct cache_entry *ce, struct stat *st);\n #define REFRESH_IGNORE_MISSING\t0x0008\t/* ignore non-existent */\n #define REFRESH_IGNORE_SUBMODULES\t0x0010\t/* ignore submodules */\n #define REFRESH_IN_PORCELAIN\t0x0020\t/* user friendly output, not \"needs update\" */\n-extern int refresh_index(struct index_state *, unsigned int flags, const char **pathspec, char *seen, const char *header_msg);\n+extern int refresh_index(struct index_state *, unsigned int flags, const struct pathspec *pathspec, char *seen, const char *header_msg);\n \n struct lock_file {\n \tstruct lock_file *next;\ndiff --git a/read-cache.c b/read-cache.c\nindex d5201f9..6ad2ff6 100644\n--- a/read-cache.c\n+++ b/read-cache.c\n@@ -1114,7 +1114,8 @@ static void show_file(const char * fmt, const char * name, int in_porcelain,\n \tprintf(fmt, name);\n }\n \n-int refresh_index(struct index_state *istate, unsigned int flags, const char **pathspec,\n+int refresh_index(struct index_state *istate, unsigned int flags,\n+\t\t  const struct pathspec *pathspec,\n \t\t  char *seen, const char *header_msg)\n {\n \tint i;\n@@ -1149,7 +1150,7 @@ int refresh_index(struct index_state *istate, unsigned int flags, const char **p\n \t\t\tcontinue;\n \n \t\tif (pathspec &&\n-\t\t    !match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, seen))\n+\t\t    !match_pathspec_depth(pathspec, ce->name, ce_namelen(ce), 0, seen))\n \t\t\tfiltered = 1;\n \n \t\tif (ce_stage(ce)) {\n-- \n1.8.2.83.gc99314b\n"},{"id":"223305","messageId":"1373790969-13000-33-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 32/46] Convert {read,fill}_directory to take struct pathspec","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:35:55Z","receivedAt":"2013-07-14T08:35:55Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n builtin/add.c      |  4 +++-\n builtin/clean.c    |  2 +-\n builtin/grep.c     |  2 +-\n builtin/ls-files.c |  2 +-\n dir.c              | 16 +++++++++++-----\n dir.h              |  4 ++--\n wt-status.c        |  2 +-\n 7 files changed, 20 insertions(+), 12 deletions(-)\n\ndiff --git a/builtin/add.c b/builtin/add.c\nindex f5d6a33..34c9358 100644\n--- a/builtin/add.c\n+++ b/builtin/add.c\n@@ -507,6 +507,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)\n \n \tif (add_new_files) {\n \t\tint baselen;\n+\t\tstruct pathspec empty_pathspec;\n \n \t\t/* Set up the default git porcelain excludes */\n \t\tmemset(&dir, 0, sizeof(dir));\n@@ -515,8 +516,9 @@ int cmd_add(int argc, const char **argv, const char *prefix)\n \t\t\tsetup_standard_excludes(&dir);\n \t\t}\n \n+\t\tmemset(&empty_pathspec, 0, sizeof(empty_pathspec));\n \t\t/* This picks up the paths that are not tracked */\n-\t\tbaselen = fill_directory(&dir, implicit_dot ? NULL : pathspec.raw);\n+\t\tbaselen = fill_directory(&dir, implicit_dot ? &empty_pathspec : &pathspec);\n \t\tif (pathspec.nr)\n \t\t\tseen = prune_directory(&dir, pathspec.raw, baselen,\n \t\t\t\t\timplicit_dot ? WARN_IMPLICIT_DOT : 0);\ndiff --git a/builtin/clean.c b/builtin/clean.c\nindex fdd4980..d540ca4 100644\n--- a/builtin/clean.c\n+++ b/builtin/clean.c\n@@ -214,7 +214,7 @@ int cmd_clean(int argc, const char **argv, const char *prefix)\n \t\t       PATHSPEC_PREFER_CWD,\n \t\t       prefix, argv);\n \n-\tfill_directory(&dir, pathspec.raw);\n+\tfill_directory(&dir, &pathspec);\n \n \tfor (i = 0; i < dir.nr; i++) {\n \t\tstruct dir_entry *ent = dir.entries[i];\ndiff --git a/builtin/grep.c b/builtin/grep.c\nindex 4bc0754..76a6a60 100644\n--- a/builtin/grep.c\n+++ b/builtin/grep.c\n@@ -523,7 +523,7 @@ static int grep_directory(struct grep_opt *opt, const struct pathspec *pathspec,\n \tif (exc_std)\n \t\tsetup_standard_excludes(&dir);\n \n-\tfill_directory(&dir, pathspec->raw);\n+\tfill_directory(&dir, pathspec);\n \tfor (i = 0; i < dir.nr; i++) {\n \t\tconst char *name = dir.entries[i]->name;\n \t\tint namelen = strlen(name);\ndiff --git a/builtin/ls-files.c b/builtin/ls-files.c\nindex 50e6edf..fa1a6be 100644\n--- a/builtin/ls-files.c\n+++ b/builtin/ls-files.c\n@@ -216,7 +216,7 @@ static void show_files(struct dir_struct *dir)\n \n \t/* For cached/deleted files we don't need to even do the readdir */\n \tif (show_others || show_killed) {\n-\t\tfill_directory(dir, pathspec.raw);\n+\t\tfill_directory(dir, &pathspec);\n \t\tif (show_others)\n \t\t\tshow_other_files(dir);\n \t\tif (show_killed)\ndiff --git a/dir.c b/dir.c\nindex 19978d3..290c7a3 100644\n--- a/dir.c\n+++ b/dir.c\n@@ -142,7 +142,7 @@ char *common_prefix(const char **pathspec)\n \treturn len ? xmemdupz(*pathspec, len) : NULL;\n }\n \n-int fill_directory(struct dir_struct *dir, const char **pathspec)\n+int fill_directory(struct dir_struct *dir, const struct pathspec *pathspec)\n {\n \tsize_t len;\n \n@@ -150,10 +150,10 @@ int fill_directory(struct dir_struct *dir, const char **pathspec)\n \t * Calculate common prefix for the pathspec, and\n \t * use that to optimize the directory walk\n \t */\n-\tlen = common_prefix_len(pathspec);\n+\tlen = common_prefix_len(pathspec->raw);\n \n \t/* Read the directory and prune it */\n-\tread_directory(dir, pathspec ? *pathspec : \"\", len, pathspec);\n+\tread_directory(dir, pathspec->nr ? pathspec->raw[0] : \"\", len, pathspec);\n \treturn len;\n }\n \n@@ -1388,14 +1388,20 @@ static int treat_leading_path(struct dir_struct *dir,\n \treturn rc;\n }\n \n-int read_directory(struct dir_struct *dir, const char *path, int len, const char **pathspec)\n+int read_directory(struct dir_struct *dir, const char *path, int len, const struct pathspec *pathspec)\n {\n \tstruct path_simplify *simplify;\n \n+\t/*\n+\t * Check out create_simplify()\n+\t */\n+\tif (pathspec)\n+\t\tGUARD_PATHSPEC(pathspec, PATHSPEC_FROMTOP | PATHSPEC_MAXDEPTH);\n+\n \tif (has_symlink_leading_path(path, len))\n \t\treturn dir->nr;\n \n-\tsimplify = create_simplify(pathspec);\n+\tsimplify = create_simplify(pathspec ? pathspec->raw : NULL);\n \tif (!len || treat_leading_path(dir, path, len, simplify))\n \t\tread_directory_recursive(dir, path, len, 0, simplify);\n \tfree_simplify(simplify);\ndiff --git a/dir.h b/dir.h\nindex 229ccc8..076dd96 100644\n--- a/dir.h\n+++ b/dir.h\n@@ -137,8 +137,8 @@ extern int match_pathspec_depth(const struct pathspec *pathspec,\n \t\t\t\tint prefix, char *seen);\n extern int within_depth(const char *name, int namelen, int depth, int max_depth);\n \n-extern int fill_directory(struct dir_struct *dir, const char **pathspec);\n-extern int read_directory(struct dir_struct *, const char *path, int len, const char **pathspec);\n+extern int fill_directory(struct dir_struct *dir, const struct pathspec *pathspec);\n+extern int read_directory(struct dir_struct *, const char *path, int len, const struct pathspec *pathspec);\n \n extern int is_excluded_from_list(const char *pathname, int pathlen, const char *basename,\n \t\t\t\t int *dtype, struct exclude_list *el);\ndiff --git a/wt-status.c b/wt-status.c\nindex fae0c27..33baf0a 100644\n--- a/wt-status.c\n+++ b/wt-status.c\n@@ -514,7 +514,7 @@ static void wt_status_collect_untracked(struct wt_status *s)\n \t\tdir.flags |= DIR_SHOW_IGNORED_TOO;\n \tsetup_standard_excludes(&dir);\n \n-\tfill_directory(&dir, s->pathspec.raw);\n+\tfill_directory(&dir, &s->pathspec);\n \n \tfor (i = 0; i < dir.nr; i++) {\n \t\tstruct dir_entry *ent = dir.entries[i];\n-- \n1.8.2.83.gc99314b\n"},{"id":"223306","messageId":"1373790969-13000-34-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 33/46] Convert add_files_to_cache to take struct pathspec","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:35:56Z","receivedAt":"2013-07-14T08:35:56Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n builtin/add.c    | 11 +++++++----\n builtin/commit.c |  2 +-\n cache.h          |  2 +-\n 3 files changed, 9 insertions(+), 6 deletions(-)\n\ndiff --git a/builtin/add.c b/builtin/add.c\nindex 34c9358..a47aeb4 100644\n--- a/builtin/add.c\n+++ b/builtin/add.c\n@@ -166,14 +166,16 @@ static void update_callback(struct diff_queue_struct *q,\n \t}\n }\n \n-static void update_files_in_cache(const char *prefix, const char **pathspec,\n+static void update_files_in_cache(const char *prefix,\n+\t\t\t\t  const struct pathspec *pathspec,\n \t\t\t\t  struct update_callback_data *data)\n {\n \tstruct rev_info rev;\n \n \tinit_revisions(&rev, prefix);\n \tsetup_revisions(0, NULL, &rev, NULL);\n-\tinit_pathspec(&rev.prune_data, pathspec);\n+\tif (pathspec)\n+\t\tcopy_pathspec(&rev.prune_data, pathspec);\n \trev.diffopt.output_format = DIFF_FORMAT_CALLBACK;\n \trev.diffopt.format_callback = update_callback;\n \trev.diffopt.format_callback_data = data;\n@@ -181,7 +183,8 @@ static void update_files_in_cache(const char *prefix, const char **pathspec,\n \trun_diff_files(&rev, DIFF_RACY_IS_MODIFIED);\n }\n \n-int add_files_to_cache(const char *prefix, const char **pathspec, int flags)\n+int add_files_to_cache(const char *prefix,\n+\t\t       const struct pathspec *pathspec, int flags)\n {\n \tstruct update_callback_data data;\n \n@@ -571,7 +574,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)\n \t\tmemset(&pathspec, 0, sizeof(pathspec));\n \t}\n \tupdate_data.flags = flags & ~ADD_CACHE_IMPLICIT_DOT;\n-\tupdate_files_in_cache(prefix, pathspec.raw, &update_data);\n+\tupdate_files_in_cache(prefix, &pathspec, &update_data);\n \n \texit_status |= !!update_data.add_errors;\n \tif (add_new_files)\ndiff --git a/builtin/commit.c b/builtin/commit.c\nindex d34baab..3b4dd60 100644\n--- a/builtin/commit.c\n+++ b/builtin/commit.c\n@@ -336,7 +336,7 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,\n \t */\n \tif (all || (also && pathspec.nr)) {\n \t\tfd = hold_locked_index(&index_lock, 1);\n-\t\tadd_files_to_cache(also ? prefix : NULL, pathspec.raw, 0);\n+\t\tadd_files_to_cache(also ? prefix : NULL, &pathspec, 0);\n \t\trefresh_cache_or_die(refresh_flags);\n \t\tupdate_main_cache_tree(WRITE_TREE_SILENT);\n \t\tif (write_cache(fd, active_cache, active_nr) ||\ndiff --git a/cache.h b/cache.h\nindex b294277..b0ed117 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -1278,7 +1278,7 @@ void packet_trace_identity(const char *prog);\n  * return 0 if success, 1 - if addition of a file failed and\n  * ADD_FILES_IGNORE_ERRORS was specified in flags\n  */\n-int add_files_to_cache(const char *prefix, const char **pathspec, int flags);\n+int add_files_to_cache(const char *prefix, const struct pathspec *pathspec, int flags);\n \n /* diff.c */\n extern int diff_auto_refresh_index;\n-- \n1.8.2.83.gc99314b\n"},{"id":"223307","messageId":"1373790969-13000-35-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 34/46] Convert common_prefix() to use struct pathspec","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:35:57Z","receivedAt":"2013-07-14T08:35:57Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"The code now takes advantage of nowildcard_len field.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n builtin/commit.c   |  2 +-\n builtin/ls-files.c |  2 +-\n dir.c              | 31 +++++++++++++++----------------\n dir.h              |  2 +-\n 4 files changed, 18 insertions(+), 19 deletions(-)\n\ndiff --git a/builtin/commit.c b/builtin/commit.c\nindex 3b4dd60..4ee9ba6 100644\n--- a/builtin/commit.c\n+++ b/builtin/commit.c\n@@ -199,7 +199,7 @@ static int list_paths(struct string_list *list, const char *with_tree,\n \tm = xcalloc(1, pattern->nr);\n \n \tif (with_tree) {\n-\t\tchar *max_prefix = common_prefix(pattern->raw);\n+\t\tchar *max_prefix = common_prefix(pattern);\n \t\toverlay_tree_on_cache(with_tree, max_prefix ? max_prefix : prefix);\n \t\tfree(max_prefix);\n \t}\ndiff --git a/builtin/ls-files.c b/builtin/ls-files.c\nindex fa1a6be..c074e6f 100644\n--- a/builtin/ls-files.c\n+++ b/builtin/ls-files.c\n@@ -546,7 +546,7 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)\n \t\t       prefix, argv);\n \n \t/* Find common prefix for all pathspec's */\n-\tmax_prefix = common_prefix(pathspec.raw);\n+\tmax_prefix = common_prefix(&pathspec);\n \tmax_prefix_len = max_prefix ? strlen(max_prefix) : 0;\n \n \t/* Treat unmatching pathspec elements as errors */\ndiff --git a/dir.c b/dir.c\nindex 290c7a3..019ad09 100644\n--- a/dir.c\n+++ b/dir.c\n@@ -103,26 +103,25 @@ static int fnmatch_icase_mem(const char *pattern, int patternlen,\n \treturn match_status;\n }\n \n-static size_t common_prefix_len(const char **pathspec)\n+static size_t common_prefix_len(const struct pathspec *pathspec)\n {\n-\tconst char *n, *first;\n+\tint n;\n \tsize_t max = 0;\n-\tint literal = limit_pathspec_to_literal();\n \n-\tif (!pathspec)\n-\t\treturn max;\n-\n-\tfirst = *pathspec;\n-\twhile ((n = *pathspec++)) {\n-\t\tsize_t i, len = 0;\n-\t\tfor (i = 0; first == n || i < max; i++) {\n-\t\t\tchar c = n[i];\n-\t\t\tif (!c || c != first[i] || (!literal && is_glob_special(c)))\n+\tGUARD_PATHSPEC(pathspec, PATHSPEC_FROMTOP | PATHSPEC_MAXDEPTH);\n+\n+\tfor (n = 0; n < pathspec->nr; n++) {\n+\t\tsize_t i = 0, len = 0;\n+\t\twhile (i < pathspec->items[n].nowildcard_len &&\n+\t\t       (n == 0 || i < max)) {\n+\t\t\tchar c = pathspec->items[n].match[i];\n+\t\t\tif (c != pathspec->items[0].match[i])\n \t\t\t\tbreak;\n \t\t\tif (c == '/')\n \t\t\t\tlen = i + 1;\n+\t\t\ti++;\n \t\t}\n-\t\tif (first == n || len < max) {\n+\t\tif (n == 0 || len < max) {\n \t\t\tmax = len;\n \t\t\tif (!max)\n \t\t\t\tbreak;\n@@ -135,11 +134,11 @@ static size_t common_prefix_len(const char **pathspec)\n  * Returns a copy of the longest leading path common among all\n  * pathspecs.\n  */\n-char *common_prefix(const char **pathspec)\n+char *common_prefix(const struct pathspec *pathspec)\n {\n \tunsigned long len = common_prefix_len(pathspec);\n \n-\treturn len ? xmemdupz(*pathspec, len) : NULL;\n+\treturn len ? xmemdupz(pathspec->items[0].match, len) : NULL;\n }\n \n int fill_directory(struct dir_struct *dir, const struct pathspec *pathspec)\n@@ -150,7 +149,7 @@ int fill_directory(struct dir_struct *dir, const struct pathspec *pathspec)\n \t * Calculate common prefix for the pathspec, and\n \t * use that to optimize the directory walk\n \t */\n-\tlen = common_prefix_len(pathspec->raw);\n+\tlen = common_prefix_len(pathspec);\n \n \t/* Read the directory and prune it */\n \tread_directory(dir, pathspec->nr ? pathspec->raw[0] : \"\", len, pathspec);\ndiff --git a/dir.h b/dir.h\nindex 076dd96..ba40e69 100644\n--- a/dir.h\n+++ b/dir.h\n@@ -130,7 +130,7 @@ struct dir_struct {\n #define MATCHED_EXACTLY 3\n extern int simple_length(const char *match);\n extern int no_wildcard(const char *string);\n-extern char *common_prefix(const char **pathspec);\n+extern char *common_prefix(const struct pathspec *pathspec);\n extern int match_pathspec(const char **pathspec, const char *name, int namelen, int prefix, char *seen);\n extern int match_pathspec_depth(const struct pathspec *pathspec,\n \t\t\t\tconst char *name, int namelen,\n-- \n1.8.2.83.gc99314b\n"},{"id":"223308","messageId":"1373790969-13000-36-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 35/46] Remove diff_tree_{setup,release}_paths","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:35:58Z","receivedAt":"2013-07-14T08:35:58Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n builtin/blame.c | 12 ++++++------\n builtin/reset.c |  9 +++++----\n combine-diff.c  |  4 ++--\n diff.h          |  2 --\n notes-merge.c   |  4 ++--\n revision.c      |  5 +++--\n tree-diff.c     | 18 ++++--------------\n 7 files changed, 22 insertions(+), 32 deletions(-)\n\ndiff --git a/builtin/blame.c b/builtin/blame.c\nindex 079dcd3..5bd721d 100644\n--- a/builtin/blame.c\n+++ b/builtin/blame.c\n@@ -408,7 +408,7 @@ static struct origin *find_origin(struct scoreboard *sb,\n \tpaths[0] = origin->path;\n \tpaths[1] = NULL;\n \n-\tdiff_tree_setup_paths(paths, &diff_opts);\n+\tinit_pathspec(&diff_opts.pathspec, paths);\n \tdiff_setup_done(&diff_opts);\n \n \tif (is_null_sha1(origin->commit->object.sha1))\n@@ -458,7 +458,7 @@ static struct origin *find_origin(struct scoreboard *sb,\n \t\t}\n \t}\n \tdiff_flush(&diff_opts);\n-\tdiff_tree_release_paths(&diff_opts);\n+\tfree_pathspec(&diff_opts.pathspec);\n \tif (porigin) {\n \t\t/*\n \t\t * Create a freestanding copy that is not part of\n@@ -494,7 +494,7 @@ static struct origin *find_rename(struct scoreboard *sb,\n \tdiff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;\n \tdiff_opts.single_follow = origin->path;\n \tpaths[0] = NULL;\n-\tdiff_tree_setup_paths(paths, &diff_opts);\n+\tinit_pathspec(&diff_opts.pathspec, paths);\n \tdiff_setup_done(&diff_opts);\n \n \tif (is_null_sha1(origin->commit->object.sha1))\n@@ -516,7 +516,7 @@ static struct origin *find_rename(struct scoreboard *sb,\n \t\t}\n \t}\n \tdiff_flush(&diff_opts);\n-\tdiff_tree_release_paths(&diff_opts);\n+\tfree_pathspec(&diff_opts.pathspec);\n \treturn porigin;\n }\n \n@@ -1079,7 +1079,7 @@ static int find_copy_in_parent(struct scoreboard *sb,\n \tdiff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;\n \n \tpaths[0] = NULL;\n-\tdiff_tree_setup_paths(paths, &diff_opts);\n+\tinit_pathspec(&diff_opts.pathspec, paths);\n \tdiff_setup_done(&diff_opts);\n \n \t/* Try \"find copies harder\" on new path if requested;\n@@ -1162,7 +1162,7 @@ static int find_copy_in_parent(struct scoreboard *sb,\n \t}\n \treset_scanned_flag(sb);\n \tdiff_flush(&diff_opts);\n-\tdiff_tree_release_paths(&diff_opts);\n+\tfree_pathspec(&diff_opts.pathspec);\n \treturn retval;\n }\n \ndiff --git a/builtin/reset.c b/builtin/reset.c\nindex 9bef869..86150d1 100644\n--- a/builtin/reset.c\n+++ b/builtin/reset.c\n@@ -133,12 +133,13 @@ static void update_index_from_diff(struct diff_queue_struct *q,\n \t}\n }\n \n-static int read_from_tree(const char **pathspec, unsigned char *tree_sha1)\n+static int read_from_tree(const struct pathspec *pathspec,\n+\t\t\t  unsigned char *tree_sha1)\n {\n \tstruct diff_options opt;\n \n \tmemset(&opt, 0, sizeof(opt));\n-\tdiff_tree_setup_paths(pathspec, &opt);\n+\tcopy_pathspec(&opt.pathspec, pathspec);\n \topt.output_format = DIFF_FORMAT_CALLBACK;\n \topt.format_callback = update_index_from_diff;\n \n@@ -147,7 +148,7 @@ static int read_from_tree(const char **pathspec, unsigned char *tree_sha1)\n \t\treturn 1;\n \tdiffcore_std(&opt);\n \tdiff_flush(&opt);\n-\tdiff_tree_release_paths(&opt);\n+\tfree_pathspec(&opt.pathspec);\n \n \treturn 0;\n }\n@@ -332,7 +333,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)\n \t\tstruct lock_file *lock = xcalloc(1, sizeof(struct lock_file));\n \t\tint newfd = hold_locked_index(lock, 1);\n \t\tif (reset_type == MIXED) {\n-\t\t\tif (read_from_tree(pathspec.raw, sha1))\n+\t\t\tif (read_from_tree(&pathspec, sha1))\n \t\t\t\treturn 1;\n \t\t} else {\n \t\t\tint err = reset_index(sha1, reset_type, quiet);\ndiff --git a/combine-diff.c b/combine-diff.c\nindex 6dc0609..c973f5d 100644\n--- a/combine-diff.c\n+++ b/combine-diff.c\n@@ -1305,7 +1305,7 @@ void diff_tree_combined(const unsigned char *sha1,\n \tint i, num_paths, needsep, show_log_first, num_parent = parents->nr;\n \n \tdiffopts = *opt;\n-\tdiff_tree_setup_paths(diffopts.pathspec.raw, &diffopts);\n+\tcopy_pathspec(&diffopts.pathspec, &opt->pathspec);\n \tdiffopts.output_format = DIFF_FORMAT_NO_OUTPUT;\n \tDIFF_OPT_SET(&diffopts, RECURSIVE);\n \tDIFF_OPT_CLR(&diffopts, ALLOW_EXTERNAL);\n@@ -1377,7 +1377,7 @@ void diff_tree_combined(const unsigned char *sha1,\n \t\tfree(tmp);\n \t}\n \n-\tdiff_tree_release_paths(&diffopts);\n+\tfree_pathspec(&diffopts.pathspec);\n }\n \n void diff_tree_combined_merge(const struct commit *commit, int dense,\ndiff --git a/diff.h b/diff.h\nindex d1bc914..b8df245 100644\n--- a/diff.h\n+++ b/diff.h\n@@ -180,8 +180,6 @@ const char *diff_line_prefix(struct diff_options *);\n \n extern const char mime_boundary_leader[];\n \n-extern void diff_tree_setup_paths(const char **paths, struct diff_options *);\n-extern void diff_tree_release_paths(struct diff_options *);\n extern int diff_tree(struct tree_desc *t1, struct tree_desc *t2,\n \t\t     const char *base, struct diff_options *opt);\n extern int diff_tree_sha1(const unsigned char *old, const unsigned char *new,\ndiff --git a/notes-merge.c b/notes-merge.c\nindex ab18857..94a1a8a 100644\n--- a/notes-merge.c\n+++ b/notes-merge.c\n@@ -170,7 +170,7 @@ static struct notes_merge_pair *diff_tree_remote(struct notes_merge_options *o,\n \t\t       sha1_to_hex(mp->remote));\n \t}\n \tdiff_flush(&opt);\n-\tdiff_tree_release_paths(&opt);\n+\tfree_pathspec(&opt.pathspec);\n \n \t*num_changes = len;\n \treturn changes;\n@@ -256,7 +256,7 @@ static void diff_tree_local(struct notes_merge_options *o,\n \t\t       sha1_to_hex(mp->local));\n \t}\n \tdiff_flush(&opt);\n-\tdiff_tree_release_paths(&opt);\n+\tfree_pathspec(&opt.pathspec);\n }\n \n static void check_notes_merge_worktree(struct notes_merge_options *o)\ndiff --git a/revision.c b/revision.c\nindex cdc7f45..c2d44b7 100644\n--- a/revision.c\n+++ b/revision.c\n@@ -2154,12 +2154,13 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s\n \t\trevs->limited = 1;\n \n \tif (revs->prune_data.nr) {\n-\t\tdiff_tree_setup_paths(revs->prune_data.raw, &revs->pruning);\n+\t\tcopy_pathspec(&revs->pruning.pathspec, &revs->prune_data);\n \t\t/* Can't prune commits with rename following: the paths change.. */\n \t\tif (!DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))\n \t\t\trevs->prune = 1;\n \t\tif (!revs->full_diff)\n-\t\t\tdiff_tree_setup_paths(revs->prune_data.raw, &revs->diffopt);\n+\t\t\tcopy_pathspec(&revs->diffopt.pathspec,\n+\t\t\t\t      &revs->prune_data);\n \t}\n \tif (revs->combine_merges)\n \t\trevs->ignore_merges = 0;\ndiff --git a/tree-diff.c b/tree-diff.c\nindex 5a87614..f4c92f6 100644\n--- a/tree-diff.c\n+++ b/tree-diff.c\n@@ -229,11 +229,11 @@ static void try_to_follow_renames(struct tree_desc *t1, struct tree_desc *t2, co\n \tdiff_opts.break_opt = opt->break_opt;\n \tdiff_opts.rename_score = opt->rename_score;\n \tpaths[0] = NULL;\n-\tdiff_tree_setup_paths(paths, &diff_opts);\n+\tinit_pathspec(&diff_opts.pathspec, paths);\n \tdiff_setup_done(&diff_opts);\n \tdiff_tree(t1, t2, base, &diff_opts);\n \tdiffcore_std(&diff_opts);\n-\tdiff_tree_release_paths(&diff_opts);\n+\tfree_pathspec(&diff_opts.pathspec);\n \n \t/* Go through the new set of filepairing, and see if we find a more interesting one */\n \topt->found_follow = 0;\n@@ -252,9 +252,9 @@ static void try_to_follow_renames(struct tree_desc *t1, struct tree_desc *t2, co\n \t\t\tchoice = p;\n \n \t\t\t/* Update the path we use from now on.. */\n-\t\t\tdiff_tree_release_paths(opt);\n+\t\t\tfree_pathspec(&opt->pathspec);\n \t\t\topt->pathspec.raw[0] = xstrdup(p->one->path);\n-\t\t\tdiff_tree_setup_paths(opt->pathspec.raw, opt);\n+\t\t\tinit_pathspec(&opt->pathspec, opt->pathspec.raw);\n \n \t\t\t/*\n \t\t\t * The caller expects us to return a set of vanilla\n@@ -328,13 +328,3 @@ int diff_root_tree_sha1(const unsigned char *new, const char *base, struct diff_\n \tfree(tree);\n \treturn retval;\n }\n-\n-void diff_tree_release_paths(struct diff_options *opt)\n-{\n-\tfree_pathspec(&opt->pathspec);\n-}\n-\n-void diff_tree_setup_paths(const char **p, struct diff_options *opt)\n-{\n-\tinit_pathspec(&opt->pathspec, p);\n-}\n-- \n1.8.2.83.gc99314b\n"},{"id":"223309","messageId":"1373790969-13000-37-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 36/46] Remove init_pathspec() in favor of parse_pathspec()","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:35:59Z","receivedAt":"2013-07-14T08:35:59Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"While at there, move free_pathspec() to pathspec.c\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n builtin/blame.c    |  8 +-------\n builtin/log.c      |  2 +-\n builtin/ls-files.c | 11 +++++------\n diff-lib.c         |  2 +-\n dir.c              | 58 ------------------------------------------------------\n merge-recursive.c  |  2 +-\n pathspec.c         |  6 ++++++\n pathspec.h         |  1 -\n revision.c         |  2 +-\n tree-diff.c        | 10 +++++-----\n 10 files changed, 21 insertions(+), 81 deletions(-)\n\ndiff --git a/builtin/blame.c b/builtin/blame.c\nindex 5bd721d..56e3d6b 100644\n--- a/builtin/blame.c\n+++ b/builtin/blame.c\n@@ -408,7 +408,7 @@ static struct origin *find_origin(struct scoreboard *sb,\n \tpaths[0] = origin->path;\n \tpaths[1] = NULL;\n \n-\tinit_pathspec(&diff_opts.pathspec, paths);\n+\tparse_pathspec(&diff_opts.pathspec, PATHSPEC_ALL_MAGIC, 0, \"\", paths);\n \tdiff_setup_done(&diff_opts);\n \n \tif (is_null_sha1(origin->commit->object.sha1))\n@@ -486,15 +486,12 @@ static struct origin *find_rename(struct scoreboard *sb,\n \tstruct origin *porigin = NULL;\n \tstruct diff_options diff_opts;\n \tint i;\n-\tconst char *paths[2];\n \n \tdiff_setup(&diff_opts);\n \tDIFF_OPT_SET(&diff_opts, RECURSIVE);\n \tdiff_opts.detect_rename = DIFF_DETECT_RENAME;\n \tdiff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;\n \tdiff_opts.single_follow = origin->path;\n-\tpaths[0] = NULL;\n-\tinit_pathspec(&diff_opts.pathspec, paths);\n \tdiff_setup_done(&diff_opts);\n \n \tif (is_null_sha1(origin->commit->object.sha1))\n@@ -1064,7 +1061,6 @@ static int find_copy_in_parent(struct scoreboard *sb,\n \t\t\t       int opt)\n {\n \tstruct diff_options diff_opts;\n-\tconst char *paths[1];\n \tint i, j;\n \tint retval;\n \tstruct blame_list *blame_list;\n@@ -1078,8 +1074,6 @@ static int find_copy_in_parent(struct scoreboard *sb,\n \tDIFF_OPT_SET(&diff_opts, RECURSIVE);\n \tdiff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;\n \n-\tpaths[0] = NULL;\n-\tinit_pathspec(&diff_opts.pathspec, paths);\n \tdiff_setup_done(&diff_opts);\n \n \t/* Try \"find copies harder\" on new path if requested;\ndiff --git a/builtin/log.c b/builtin/log.c\nindex e3222ed..873af69 100644\n--- a/builtin/log.c\n+++ b/builtin/log.c\n@@ -503,7 +503,7 @@ int cmd_show(int argc, const char **argv, const char *prefix)\n \tinit_grep_defaults();\n \tgit_config(git_log_config, NULL);\n \n-\tinit_pathspec(&match_all, NULL);\n+\tmemset(&match_all, 0, sizeof(match_all));\n \tinit_revisions(&rev, prefix);\n \trev.diff = 1;\n \trev.always_show_header = 1;\ndiff --git a/builtin/ls-files.c b/builtin/ls-files.c\nindex c074e6f..d3a0495 100644\n--- a/builtin/ls-files.c\n+++ b/builtin/ls-files.c\n@@ -315,13 +315,12 @@ void overlay_tree_on_cache(const char *tree_name, const char *prefix)\n \t}\n \n \tif (prefix) {\n-\t\tstatic const char *(matchbuf[2]);\n-\t\tmatchbuf[0] = prefix;\n-\t\tmatchbuf[1] = NULL;\n-\t\tinit_pathspec(&pathspec, matchbuf);\n-\t\tpathspec.items[0].nowildcard_len = pathspec.items[0].len;\n+\t\tstatic const char *(matchbuf[1]);\n+\t\tmatchbuf[0] = NULL;\n+\t\tparse_pathspec(&pathspec, PATHSPEC_ALL_MAGIC,\n+\t\t\t       PATHSPEC_PREFER_CWD, prefix, matchbuf);\n \t} else\n-\t\tinit_pathspec(&pathspec, NULL);\n+\t\tmemset(&pathspec, 0, sizeof(pathspec));\n \tif (read_tree(tree, 1, &pathspec))\n \t\tdie(\"unable to read tree entries %s\", tree_name);\n \ndiff --git a/diff-lib.c b/diff-lib.c\nindex d4e17f7..0a95763 100644\n--- a/diff-lib.c\n+++ b/diff-lib.c\n@@ -501,7 +501,7 @@ int do_diff_cache(const unsigned char *tree_sha1, struct diff_options *opt)\n \tstruct rev_info revs;\n \n \tinit_revisions(&revs, NULL);\n-\tinit_pathspec(&revs.prune_data, opt->pathspec.raw);\n+\tcopy_pathspec(&revs.prune_data, &opt->pathspec);\n \trevs.diffopt = *opt;\n \n \tif (diff_cache(&revs, tree_sha1, NULL, 1))\ndiff --git a/dir.c b/dir.c\nindex 019ad09..959e694 100644\n--- a/dir.c\n+++ b/dir.c\n@@ -1580,64 +1580,6 @@ int remove_path(const char *name)\n \treturn 0;\n }\n \n-static int pathspec_item_cmp(const void *a_, const void *b_)\n-{\n-\tstruct pathspec_item *a, *b;\n-\n-\ta = (struct pathspec_item *)a_;\n-\tb = (struct pathspec_item *)b_;\n-\treturn strcmp(a->match, b->match);\n-}\n-\n-int init_pathspec(struct pathspec *pathspec, const char **paths)\n-{\n-\tconst char **p = paths;\n-\tint i;\n-\n-\tmemset(pathspec, 0, sizeof(*pathspec));\n-\tif (!p)\n-\t\treturn 0;\n-\twhile (*p)\n-\t\tp++;\n-\tpathspec->raw = paths;\n-\tpathspec->nr = p - paths;\n-\tif (!pathspec->nr)\n-\t\treturn 0;\n-\n-\tpathspec->items = xmalloc(sizeof(struct pathspec_item)*pathspec->nr);\n-\tfor (i = 0; i < pathspec->nr; i++) {\n-\t\tstruct pathspec_item *item = pathspec->items+i;\n-\t\tconst char *path = paths[i];\n-\n-\t\titem->match = path;\n-\t\titem->original = path;\n-\t\titem->len = strlen(path);\n-\t\titem->flags = 0;\n-\t\tif (limit_pathspec_to_literal()) {\n-\t\t\titem->nowildcard_len = item->len;\n-\t\t} else {\n-\t\t\titem->nowildcard_len = simple_length(path);\n-\t\t\tif (item->nowildcard_len < item->len) {\n-\t\t\t\tpathspec->has_wildcard = 1;\n-\t\t\t\tif (path[item->nowildcard_len] == '*' &&\n-\t\t\t\t    no_wildcard(path + item->nowildcard_len + 1))\n-\t\t\t\t\titem->flags |= PATHSPEC_ONESTAR;\n-\t\t\t}\n-\t\t}\n-\t}\n-\n-\tqsort(pathspec->items, pathspec->nr,\n-\t      sizeof(struct pathspec_item), pathspec_item_cmp);\n-\n-\treturn 0;\n-}\n-\n-void free_pathspec(struct pathspec *pathspec)\n-{\n-\tfree(pathspec->items);\n-\tpathspec->items = NULL;\n-}\n-\n int limit_pathspec_to_literal(void)\n {\n \tstatic int flag = -1;\ndiff --git a/merge-recursive.c b/merge-recursive.c\nindex ea9dbd3..8395b9b 100644\n--- a/merge-recursive.c\n+++ b/merge-recursive.c\n@@ -297,7 +297,7 @@ static int get_files_dirs(struct merge_options *o, struct tree *tree)\n {\n \tint n;\n \tstruct pathspec match_all;\n-\tinit_pathspec(&match_all, NULL);\n+\tmemset(&match_all, 0, sizeof(match_all));\n \tif (read_tree_recursive(tree, \"\", 0, 0, &match_all, save_files_dirs, o))\n \t\treturn 0;\n \tn = o->current_file_set.nr + o->current_directory_set.nr;\ndiff --git a/pathspec.c b/pathspec.c\nindex 2774bda..74f0203 100644\n--- a/pathspec.c\n+++ b/pathspec.c\n@@ -372,3 +372,9 @@ void copy_pathspec(struct pathspec *dst, const struct pathspec *src)\n \tmemcpy(dst->items, src->items,\n \t       sizeof(struct pathspec_item) * dst->nr);\n }\n+\n+void free_pathspec(struct pathspec *pathspec)\n+{\n+\tfree(pathspec->items);\n+\tpathspec->items = NULL;\n+}\ndiff --git a/pathspec.h b/pathspec.h\nindex 02dded3..cadd46c 100644\n--- a/pathspec.h\n+++ b/pathspec.h\n@@ -53,7 +53,6 @@ struct pathspec {\n #define PATHSPEC_PREFIX_ORIGIN (1<<6)\n #define PATHSPEC_KEEP_ORDER (1<<7)\n \n-extern int init_pathspec(struct pathspec *, const char **);\n extern void parse_pathspec(struct pathspec *pathspec,\n \t\t\t   unsigned magic_mask,\n \t\t\t   unsigned flags,\ndiff --git a/revision.c b/revision.c\nindex c2d44b7..001623a 100644\n--- a/revision.c\n+++ b/revision.c\n@@ -1372,7 +1372,7 @@ static void prepare_show_merge(struct rev_info *revs)\n \t\t\ti++;\n \t}\n \tfree_pathspec(&revs->prune_data);\n-\tinit_pathspec(&revs->prune_data, prune);\n+\tparse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC, 0, \"\", prune);\n \trevs->limited = 1;\n }\n \ndiff --git a/tree-diff.c b/tree-diff.c\nindex f4c92f6..e1145c6 100644\n--- a/tree-diff.c\n+++ b/tree-diff.c\n@@ -195,7 +195,6 @@ static void try_to_follow_renames(struct tree_desc *t1, struct tree_desc *t2, co\n \tstruct diff_options diff_opts;\n \tstruct diff_queue_struct *q = &diff_queued_diff;\n \tstruct diff_filepair *choice;\n-\tconst char *paths[1];\n \tint i;\n \n \t/*\n@@ -228,8 +227,6 @@ static void try_to_follow_renames(struct tree_desc *t1, struct tree_desc *t2, co\n \tdiff_opts.single_follow = opt->pathspec.raw[0];\n \tdiff_opts.break_opt = opt->break_opt;\n \tdiff_opts.rename_score = opt->rename_score;\n-\tpaths[0] = NULL;\n-\tinit_pathspec(&diff_opts.pathspec, paths);\n \tdiff_setup_done(&diff_opts);\n \tdiff_tree(t1, t2, base, &diff_opts);\n \tdiffcore_std(&diff_opts);\n@@ -247,14 +244,17 @@ static void try_to_follow_renames(struct tree_desc *t1, struct tree_desc *t2, co\n \t\t */\n \t\tif ((p->status == 'R' || p->status == 'C') &&\n \t\t    !strcmp(p->two->path, opt->pathspec.raw[0])) {\n+\t\t\tconst char *path[2];\n+\n \t\t\t/* Switch the file-pairs around */\n \t\t\tq->queue[i] = choice;\n \t\t\tchoice = p;\n \n \t\t\t/* Update the path we use from now on.. */\n+\t\t\tpath[0] = p->one->path;\n+\t\t\tpath[1] = NULL;\n \t\t\tfree_pathspec(&opt->pathspec);\n-\t\t\topt->pathspec.raw[0] = xstrdup(p->one->path);\n-\t\t\tinit_pathspec(&opt->pathspec, opt->pathspec.raw);\n+\t\t\tparse_pathspec(&opt->pathspec, PATHSPEC_ALL_MAGIC, 0, \"\", path);\n \n \t\t\t/*\n \t\t\t * The caller expects us to return a set of vanilla\n-- \n1.8.2.83.gc99314b\n"},{"id":"223310","messageId":"1373790969-13000-38-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 37/46] Remove match_pathspec() in favor of match_pathspec_depth()","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:36:00Z","receivedAt":"2013-07-14T08:36:00Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"match_pathspec_depth was created to replace match_pathspec (see\n61cf282 (pathspec: add match_pathspec_depth() - 2010-12-15). It took\nmore than two years, but the replacement finally happens :-)\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n builtin/add.c          |  30 +++++++-------\n builtin/check-ignore.c |   4 +-\n dir.c                  | 107 -------------------------------------------------\n dir.h                  |   1 -\n pathspec.c             |  19 ++++-----\n pathspec.h             |   4 +-\n 6 files changed, 25 insertions(+), 140 deletions(-)\n\ndiff --git a/builtin/add.c b/builtin/add.c\nindex a47aeb4..0b80fa8 100644\n--- a/builtin/add.c\n+++ b/builtin/add.c\n@@ -195,23 +195,21 @@ int add_files_to_cache(const char *prefix,\n }\n \n #define WARN_IMPLICIT_DOT (1u << 0)\n-static char *prune_directory(struct dir_struct *dir, const char **pathspec,\n+static char *prune_directory(struct dir_struct *dir, struct pathspec *pathspec,\n \t\t\t     int prefix, unsigned flag)\n {\n \tchar *seen;\n-\tint i, specs;\n+\tint i;\n \tstruct dir_entry **src, **dst;\n \n-\tfor (specs = 0; pathspec[specs];  specs++)\n-\t\t/* nothing */;\n-\tseen = xcalloc(specs, 1);\n+\tseen = xcalloc(pathspec->nr, 1);\n \n \tsrc = dst = dir->entries;\n \ti = dir->nr;\n \twhile (--i >= 0) {\n \t\tstruct dir_entry *entry = *src++;\n-\t\tif (match_pathspec(pathspec, entry->name, entry->len,\n-\t\t\t\t   prefix, seen))\n+\t\tif (match_pathspec_depth(pathspec, entry->name, entry->len,\n+\t\t\t\t\t prefix, seen))\n \t\t\t*dst++ = entry;\n \t\telse if (flag & WARN_IMPLICIT_DOT)\n \t\t\t/*\n@@ -225,7 +223,7 @@ static char *prune_directory(struct dir_struct *dir, const char **pathspec,\n \t\t\twarn_pathless_add();\n \t}\n \tdir->nr = dst - dir->entries;\n-\tadd_pathspec_matches_against_index(pathspec, seen, specs);\n+\tadd_pathspec_matches_against_index(pathspec, seen);\n \treturn seen;\n }\n \n@@ -523,7 +521,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)\n \t\t/* This picks up the paths that are not tracked */\n \t\tbaselen = fill_directory(&dir, implicit_dot ? &empty_pathspec : &pathspec);\n \t\tif (pathspec.nr)\n-\t\t\tseen = prune_directory(&dir, pathspec.raw, baselen,\n+\t\t\tseen = prune_directory(&dir, &pathspec, baselen,\n \t\t\t\t\timplicit_dot ? WARN_IMPLICIT_DOT : 0);\n \t}\n \n@@ -538,23 +536,23 @@ int cmd_add(int argc, const char **argv, const char *prefix)\n \t\tint i;\n \n \t\tif (!seen)\n-\t\t\tseen = find_pathspecs_matching_against_index(pathspec.raw);\n+\t\t\tseen = find_pathspecs_matching_against_index(&pathspec);\n \n \t\t/*\n \t\t * file_exists() assumes exact match\n \t\t */\n \t\tGUARD_PATHSPEC(&pathspec, PATHSPEC_FROMTOP);\n \n-\t\tfor (i = 0; pathspec.raw[i]; i++) {\n-\t\t\tif (!seen[i] && pathspec.raw[i][0]\n-\t\t\t    && !file_exists(pathspec.raw[i])) {\n+\t\tfor (i = 0; i < pathspec.nr; i++) {\n+\t\t\tconst char *path = pathspec.items[i].match;\n+\t\t\tif (!seen[i] && !file_exists(path)) {\n \t\t\t\tif (ignore_missing) {\n \t\t\t\t\tint dtype = DT_UNKNOWN;\n-\t\t\t\t\tif (is_excluded(&dir, pathspec.raw[i], &dtype))\n-\t\t\t\t\t\tdir_add_ignored(&dir, pathspec.raw[i], strlen(pathspec.raw[i]));\n+\t\t\t\t\tif (is_excluded(&dir, path, &dtype))\n+\t\t\t\t\t\tdir_add_ignored(&dir, path, pathspec.items[i].len);\n \t\t\t\t} else\n \t\t\t\t\tdie(_(\"pathspec '%s' did not match any files\"),\n-\t\t\t\t\t    pathspec.raw[i]);\n+\t\t\t\t\t    pathspec.items[i].original);\n \t\t\t}\n \t\t}\n \t\tfree(seen);\ndiff --git a/builtin/check-ignore.c b/builtin/check-ignore.c\nindex d49c083..70537c8 100644\n--- a/builtin/check-ignore.c\n+++ b/builtin/check-ignore.c\n@@ -94,9 +94,9 @@ static int check_ignore(struct dir_struct *dir,\n \t * should not be ignored, in order to be consistent with\n \t * 'git status', 'git add' etc.\n \t */\n-\tseen = find_pathspecs_matching_against_index(pathspec.raw);\n+\tseen = find_pathspecs_matching_against_index(&pathspec);\n \tfor (i = 0; i < pathspec.nr; i++) {\n-\t\tfull_path = pathspec.raw[i];\n+\t\tfull_path = pathspec.items[i].match;\n \t\texclude = NULL;\n \t\tif (!seen[i]) {\n \t\t\texclude = last_exclude_matching(dir, full_path, &dtype);\ndiff --git a/dir.c b/dir.c\nindex 959e694..9423fbb 100644\n--- a/dir.c\n+++ b/dir.c\n@@ -183,113 +183,6 @@ int within_depth(const char *name, int namelen,\n  *\n  * It returns 0 when there is no match.\n  */\n-static int match_one(const char *match, const char *name, int namelen)\n-{\n-\tint matchlen;\n-\tint literal = limit_pathspec_to_literal();\n-\n-\t/* If the match was just the prefix, we matched */\n-\tif (!*match)\n-\t\treturn MATCHED_RECURSIVELY;\n-\n-\tif (ignore_case) {\n-\t\tfor (;;) {\n-\t\t\tunsigned char c1 = tolower(*match);\n-\t\t\tunsigned char c2 = tolower(*name);\n-\t\t\tif (c1 == '\\0' || (!literal && is_glob_special(c1)))\n-\t\t\t\tbreak;\n-\t\t\tif (c1 != c2)\n-\t\t\t\treturn 0;\n-\t\t\tmatch++;\n-\t\t\tname++;\n-\t\t\tnamelen--;\n-\t\t}\n-\t} else {\n-\t\tfor (;;) {\n-\t\t\tunsigned char c1 = *match;\n-\t\t\tunsigned char c2 = *name;\n-\t\t\tif (c1 == '\\0' || (!literal && is_glob_special(c1)))\n-\t\t\t\tbreak;\n-\t\t\tif (c1 != c2)\n-\t\t\t\treturn 0;\n-\t\t\tmatch++;\n-\t\t\tname++;\n-\t\t\tnamelen--;\n-\t\t}\n-\t}\n-\n-\t/*\n-\t * If we don't match the matchstring exactly,\n-\t * we need to match by fnmatch\n-\t */\n-\tmatchlen = strlen(match);\n-\tif (strncmp_icase(match, name, matchlen)) {\n-\t\tif (literal)\n-\t\t\treturn 0;\n-\t\treturn !fnmatch_icase(match, name, 0) ? MATCHED_FNMATCH : 0;\n-\t}\n-\n-\tif (namelen == matchlen)\n-\t\treturn MATCHED_EXACTLY;\n-\tif (match[matchlen-1] == '/' || name[matchlen] == '/')\n-\t\treturn MATCHED_RECURSIVELY;\n-\treturn 0;\n-}\n-\n-/*\n- * Given a name and a list of pathspecs, returns the nature of the\n- * closest (i.e. most specific) match of the name to any of the\n- * pathspecs.\n- *\n- * The caller typically calls this multiple times with the same\n- * pathspec and seen[] array but with different name/namelen\n- * (e.g. entries from the index) and is interested in seeing if and\n- * how each pathspec matches all the names it calls this function\n- * with.  A mark is left in the seen[] array for each pathspec element\n- * indicating the closest type of match that element achieved, so if\n- * seen[n] remains zero after multiple invocations, that means the nth\n- * pathspec did not match any names, which could indicate that the\n- * user mistyped the nth pathspec.\n- */\n-int match_pathspec(const char **pathspec, const char *name, int namelen,\n-\t\tint prefix, char *seen)\n-{\n-\tint i, retval = 0;\n-\n-\tif (!pathspec)\n-\t\treturn 1;\n-\n-\tname += prefix;\n-\tnamelen -= prefix;\n-\n-\tfor (i = 0; pathspec[i] != NULL; i++) {\n-\t\tint how;\n-\t\tconst char *match = pathspec[i] + prefix;\n-\t\tif (seen && seen[i] == MATCHED_EXACTLY)\n-\t\t\tcontinue;\n-\t\thow = match_one(match, name, namelen);\n-\t\tif (how) {\n-\t\t\tif (retval < how)\n-\t\t\t\tretval = how;\n-\t\t\tif (seen && seen[i] < how)\n-\t\t\t\tseen[i] = how;\n-\t\t}\n-\t}\n-\treturn retval;\n-}\n-\n-/*\n- * Does 'match' match the given name?\n- * A match is found if\n- *\n- * (1) the 'match' string is leading directory of 'name', or\n- * (2) the 'match' string is a wildcard and matches 'name', or\n- * (3) the 'match' string is exactly the same as 'name'.\n- *\n- * and the return value tells which case it was.\n- *\n- * It returns 0 when there is no match.\n- */\n static int match_pathspec_item(const struct pathspec_item *item, int prefix,\n \t\t\t       const char *name, int namelen)\n {\ndiff --git a/dir.h b/dir.h\nindex ba40e69..7d051e3 100644\n--- a/dir.h\n+++ b/dir.h\n@@ -131,7 +131,6 @@ struct dir_struct {\n extern int simple_length(const char *match);\n extern int no_wildcard(const char *string);\n extern char *common_prefix(const struct pathspec *pathspec);\n-extern int match_pathspec(const char **pathspec, const char *name, int namelen, int prefix, char *seen);\n extern int match_pathspec_depth(const struct pathspec *pathspec,\n \t\t\t\tconst char *name, int namelen,\n \t\t\t\tint prefix, char *seen);\ndiff --git a/pathspec.c b/pathspec.c\nindex 74f0203..3d1386d 100644\n--- a/pathspec.c\n+++ b/pathspec.c\n@@ -15,8 +15,8 @@\n  * If seen[] has not already been written to, it may make sense\n  * to use find_pathspecs_matching_against_index() instead.\n  */\n-void add_pathspec_matches_against_index(const char **pathspec,\n-\t\t\t\t\tchar *seen, int specs)\n+void add_pathspec_matches_against_index(const struct pathspec *pathspec,\n+\t\t\t\t\tchar *seen)\n {\n \tint num_unmatched = 0, i;\n \n@@ -26,14 +26,14 @@ void add_pathspec_matches_against_index(const char **pathspec,\n \t * mistakenly think that the user gave a pathspec that did not match\n \t * anything.\n \t */\n-\tfor (i = 0; i < specs; i++)\n+\tfor (i = 0; i < pathspec->nr; i++)\n \t\tif (!seen[i])\n \t\t\tnum_unmatched++;\n \tif (!num_unmatched)\n \t\treturn;\n \tfor (i = 0; i < active_nr; i++) {\n \t\tstruct cache_entry *ce = active_cache[i];\n-\t\tmatch_pathspec(pathspec, ce->name, ce_namelen(ce), 0, seen);\n+\t\tmatch_pathspec_depth(pathspec, ce->name, ce_namelen(ce), 0, seen);\n \t}\n }\n \n@@ -45,15 +45,10 @@ void add_pathspec_matches_against_index(const char **pathspec,\n  * nature of the \"closest\" (i.e. most specific) matches which each of the\n  * given pathspecs achieves against all items in the index.\n  */\n-char *find_pathspecs_matching_against_index(const char **pathspec)\n+char *find_pathspecs_matching_against_index(const struct pathspec *pathspec)\n {\n-\tchar *seen;\n-\tint i;\n-\n-\tfor (i = 0; pathspec[i];  i++)\n-\t\t; /* just counting */\n-\tseen = xcalloc(i, 1);\n-\tadd_pathspec_matches_against_index(pathspec, seen, i);\n+\tchar *seen = xcalloc(pathspec->nr, 1);\n+\tadd_pathspec_matches_against_index(pathspec, seen);\n \treturn seen;\n }\n \ndiff --git a/pathspec.h b/pathspec.h\nindex cadd46c..31a9c96 100644\n--- a/pathspec.h\n+++ b/pathspec.h\n@@ -63,8 +63,8 @@ extern void free_pathspec(struct pathspec *);\n \n extern int limit_pathspec_to_literal(void);\n \n-extern char *find_pathspecs_matching_against_index(const char **pathspec);\n-extern void add_pathspec_matches_against_index(const char **pathspec, char *seen, int specs);\n+extern char *find_pathspecs_matching_against_index(const struct pathspec *pathspec);\n+extern void add_pathspec_matches_against_index(const struct pathspec *pathspec, char *seen);\n extern const char *check_path_for_gitlink(const char *path);\n extern void die_if_path_beyond_symlink(const char *path, const char *prefix);\n \n-- \n1.8.2.83.gc99314b\n"},{"id":"223311","messageId":"1373790969-13000-39-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 38/46] tree-diff: remove the use of pathspec's raw[] in follow-rename codepath","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:36:01Z","receivedAt":"2013-07-14T08:36:01Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Put a checkpoint to guard unsupported pathspec features in future.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n tree-diff.c | 4 ++--\n 1 file changed, 2 insertions(+), 2 deletions(-)\n\ndiff --git a/tree-diff.c b/tree-diff.c\nindex e1145c6..21a50d8 100644\n--- a/tree-diff.c\n+++ b/tree-diff.c\n@@ -224,7 +224,7 @@ static void try_to_follow_renames(struct tree_desc *t1, struct tree_desc *t2, co\n \tDIFF_OPT_SET(&diff_opts, RECURSIVE);\n \tDIFF_OPT_SET(&diff_opts, FIND_COPIES_HARDER);\n \tdiff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;\n-\tdiff_opts.single_follow = opt->pathspec.raw[0];\n+\tdiff_opts.single_follow = opt->pathspec.items[0].match;\n \tdiff_opts.break_opt = opt->break_opt;\n \tdiff_opts.rename_score = opt->rename_score;\n \tdiff_setup_done(&diff_opts);\n@@ -243,7 +243,7 @@ static void try_to_follow_renames(struct tree_desc *t1, struct tree_desc *t2, co\n \t\t * the future!\n \t\t */\n \t\tif ((p->status == 'R' || p->status == 'C') &&\n-\t\t    !strcmp(p->two->path, opt->pathspec.raw[0])) {\n+\t\t    !strcmp(p->two->path, opt->pathspec.items[0].match)) {\n \t\t\tconst char *path[2];\n \n \t\t\t/* Switch the file-pairs around */\n-- \n1.8.2.83.gc99314b\n"},{"id":"223312","messageId":"1373790969-13000-40-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 39/46] Rename field \"raw\" to \"_raw\" in struct pathspec","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:36:02Z","receivedAt":"2013-07-14T08:36:02Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"This patch is essentially no-op. It helps catching new use of this\nfield though. This field is introduced as an intermediate step for the\npathspec conversion and will be removed eventually. At this stage no\nmore access sites should be introduced.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n builtin/ls-tree.c | 2 +-\n dir.c             | 4 ++--\n pathspec.c        | 6 +++---\n pathspec.h        | 2 +-\n 4 files changed, 7 insertions(+), 7 deletions(-)\n\ndiff --git a/builtin/ls-tree.c b/builtin/ls-tree.c\nindex bdb03f3..1c4f48e 100644\n--- a/builtin/ls-tree.c\n+++ b/builtin/ls-tree.c\n@@ -36,7 +36,7 @@ static int show_recursive(const char *base, int baselen, const char *pathname)\n \tif (ls_options & LS_RECURSIVE)\n \t\treturn 1;\n \n-\ts = pathspec.raw;\n+\ts = pathspec._raw;\n \tif (!s)\n \t\treturn 0;\n \ndiff --git a/dir.c b/dir.c\nindex 9423fbb..bf22498 100644\n--- a/dir.c\n+++ b/dir.c\n@@ -152,7 +152,7 @@ int fill_directory(struct dir_struct *dir, const struct pathspec *pathspec)\n \tlen = common_prefix_len(pathspec);\n \n \t/* Read the directory and prune it */\n-\tread_directory(dir, pathspec->nr ? pathspec->raw[0] : \"\", len, pathspec);\n+\tread_directory(dir, pathspec->nr ? pathspec->_raw[0] : \"\", len, pathspec);\n \treturn len;\n }\n \n@@ -1293,7 +1293,7 @@ int read_directory(struct dir_struct *dir, const char *path, int len, const stru\n \tif (has_symlink_leading_path(path, len))\n \t\treturn dir->nr;\n \n-\tsimplify = create_simplify(pathspec ? pathspec->raw : NULL);\n+\tsimplify = create_simplify(pathspec ? pathspec->_raw : NULL);\n \tif (!len || treat_leading_path(dir, path, len, simplify))\n \t\tread_directory_recursive(dir, path, len, 0, simplify);\n \tfree_simplify(simplify);\ndiff --git a/pathspec.c b/pathspec.c\nindex 3d1386d..da802e2 100644\n--- a/pathspec.c\n+++ b/pathspec.c\n@@ -287,7 +287,7 @@ void parse_pathspec(struct pathspec *pathspec,\n \t\traw[0] = prefix;\n \t\traw[1] = NULL;\n \t\tpathspec->nr = 1;\n-\t\tpathspec->raw = raw;\n+\t\tpathspec->_raw = raw;\n \t\treturn;\n \t}\n \n@@ -297,7 +297,7 @@ void parse_pathspec(struct pathspec *pathspec,\n \n \tpathspec->nr = n;\n \tpathspec->items = item = xmalloc(sizeof(*item) * n);\n-\tpathspec->raw = argv;\n+\tpathspec->_raw = argv;\n \tprefixlen = prefix ? strlen(prefix) : 0;\n \n \tfor (i = 0; i < n; i++) {\n@@ -357,7 +357,7 @@ const char **get_pathspec(const char *prefix, const char **pathspec)\n \t\t       PATHSPEC_ALL_MAGIC & ~PATHSPEC_FROMTOP,\n \t\t       PATHSPEC_PREFER_CWD,\n \t\t       prefix, pathspec);\n-\treturn ps.raw;\n+\treturn ps._raw;\n }\n \n void copy_pathspec(struct pathspec *dst, const struct pathspec *src)\ndiff --git a/pathspec.h b/pathspec.h\nindex 31a9c96..0f6739d 100644\n--- a/pathspec.h\n+++ b/pathspec.h\n@@ -11,7 +11,7 @@\n #define PATHSPEC_ONESTAR 1\t/* the pathspec pattern sastisfies GFNM_ONESTAR */\n \n struct pathspec {\n-\tconst char **raw; /* get_pathspec() result, not freed by free_pathspec() */\n+\tconst char **_raw; /* get_pathspec() result, not freed by free_pathspec() */\n \tint nr;\n \tunsigned int has_wildcard:1;\n \tunsigned int recursive:1;\n-- \n1.8.2.83.gc99314b\n"},{"id":"223313","messageId":"1373790969-13000-41-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 40/46] parse_pathspec: make sure the prefix part is wildcard-free","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:36:03Z","receivedAt":"2013-07-14T08:36:03Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Prepending prefix to pathspec is a trick to workaround the fact that\ncommands can be executed in a subdirectory, but all git commands run\nat worktree's root. The prefix part should always be treated as\nliteral string. Make it so.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n cache.h    |  2 ++\n path.c     | 15 ++++++++++++++-\n pathspec.c | 21 +++++++++++++++++----\n pathspec.h |  2 +-\n setup.c    | 24 ++++++++++++++++++++----\n 5 files changed, 54 insertions(+), 10 deletions(-)\n\ndiff --git a/cache.h b/cache.h\nindex b0ed117..13e3c94 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -414,6 +414,7 @@ extern void setup_work_tree(void);\n extern const char *setup_git_directory_gently(int *);\n extern const char *setup_git_directory(void);\n extern char *prefix_path(const char *prefix, int len, const char *path);\n+extern char *prefix_path_gently(const char *prefix, int len, int *remaining, const char *path);\n extern const char *prefix_filename(const char *prefix, int len, const char *path);\n extern int check_filename(const char *prefix, const char *name);\n extern void verify_filename(const char *prefix,\n@@ -741,6 +742,7 @@ const char *real_path(const char *path);\n const char *real_path_if_valid(const char *path);\n const char *absolute_path(const char *path);\n const char *relative_path(const char *abs, const char *base);\n+int normalize_path_copy_len(char *dst, const char *src, int *prefix_len);\n int normalize_path_copy(char *dst, const char *src);\n int longest_ancestor_length(const char *path, struct string_list *prefixes);\n char *strip_path_suffix(const char *path, const char *suffix);\ndiff --git a/path.c b/path.c\nindex 04ff148..f4b49d6 100644\n--- a/path.c\n+++ b/path.c\n@@ -492,8 +492,14 @@ const char *relative_path(const char *abs, const char *base)\n  *\n  * Note that this function is purely textual.  It does not follow symlinks,\n  * verify the existence of the path, or make any system calls.\n+ *\n+ * prefix_len != NULL is for a specific case of prefix_pathspec():\n+ * assume that src == dst and src[0..prefix_len-1] is already\n+ * normalized, any time \"../\" eats up to the prefix_len part,\n+ * prefix_len is reduced. In the end prefix_len is the remaining\n+ * prefix that has not been overridden by user pathspec.\n  */\n-int normalize_path_copy(char *dst, const char *src)\n+int normalize_path_copy_len(char *dst, const char *src, int *prefix_len)\n {\n \tchar *dst0;\n \n@@ -568,11 +574,18 @@ int normalize_path_copy(char *dst, const char *src)\n \t\t/* Windows: dst[-1] cannot be backslash anymore */\n \t\twhile (dst0 < dst && dst[-1] != '/')\n \t\t\tdst--;\n+\t\tif (prefix_len && *prefix_len > dst - dst0)\n+\t\t\t*prefix_len = dst - dst0;\n \t}\n \t*dst = '\\0';\n \treturn 0;\n }\n \n+int normalize_path_copy(char *dst, const char *src)\n+{\n+\treturn normalize_path_copy_len(dst, src, NULL);\n+}\n+\n /*\n  * path = Canonical absolute path\n  * prefixes = string_list containing normalized, absolute paths without\ndiff --git a/pathspec.c b/pathspec.c\nindex da802e2..71e5eaf 100644\n--- a/pathspec.c\n+++ b/pathspec.c\n@@ -150,10 +150,14 @@ static unsigned prefix_pathspec(struct pathspec_item *item,\n \tmagic |= short_magic;\n \t*p_short_magic = short_magic;\n \n-\tif (magic & PATHSPEC_FROMTOP)\n+\tif (magic & PATHSPEC_FROMTOP) {\n \t\tmatch = xstrdup(copyfrom);\n-\telse\n-\t\tmatch = prefix_path(prefix, prefixlen, copyfrom);\n+\t\tprefixlen = 0;\n+\t} else {\n+\t\tmatch = prefix_path_gently(prefix, prefixlen, &prefixlen, copyfrom);\n+\t\tif (!match)\n+\t\t\tdie(_(\"%s: '%s' is outside repository\"), elt, copyfrom);\n+\t}\n \t*raw = item->match = match;\n \t/*\n \t * Prefix the pathspec (keep all magic) and assign to\n@@ -167,6 +171,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item,\n \t} else\n \t\titem->original = elt;\n \titem->len = strlen(item->match);\n+\titem->prefix = prefixlen;\n \n \tif ((flags & PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP) &&\n \t    (item->len >= 1 && item->match[item->len - 1] == '/') &&\n@@ -198,13 +203,20 @@ static unsigned prefix_pathspec(struct pathspec_item *item,\n \n \tif (limit_pathspec_to_literal())\n \t\titem->nowildcard_len = item->len;\n-\telse\n+\telse {\n \t\titem->nowildcard_len = simple_length(item->match);\n+\t\tif (item->nowildcard_len < prefixlen)\n+\t\t\titem->nowildcard_len = prefixlen;\n+\t}\n \titem->flags = 0;\n \tif (item->nowildcard_len < item->len &&\n \t    item->match[item->nowildcard_len] == '*' &&\n \t    no_wildcard(item->match + item->nowildcard_len + 1))\n \t\titem->flags |= PATHSPEC_ONESTAR;\n+\n+\t/* sanity checks, pathspec matchers assume these are sane */\n+\tassert(item->nowildcard_len <= item->len &&\n+\t       item->prefix         <= item->len);\n \treturn magic;\n }\n \n@@ -284,6 +296,7 @@ void parse_pathspec(struct pathspec *pathspec,\n \t\titem->match = prefix;\n \t\titem->original = prefix;\n \t\titem->nowildcard_len = item->len = strlen(prefix);\n+\t\titem->prefix = item->len;\n \t\traw[0] = prefix;\n \t\traw[1] = NULL;\n \t\tpathspec->nr = 1;\ndiff --git a/pathspec.h b/pathspec.h\nindex 0f6739d..2f3532e 100644\n--- a/pathspec.h\n+++ b/pathspec.h\n@@ -21,7 +21,7 @@ struct pathspec {\n \t\tconst char *match;\n \t\tconst char *original;\n \t\tunsigned magic;\n-\t\tint len;\n+\t\tint len, prefix;\n \t\tint nowildcard_len;\n \t\tint flags;\n \t} *items;\ndiff --git a/setup.c b/setup.c\nindex d1ece5d..ff4499e 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -5,7 +5,19 @@\n static int inside_git_dir = -1;\n static int inside_work_tree = -1;\n \n-static char *prefix_path_gently(const char *prefix, int len, const char *path)\n+/*\n+ * Normalize \"path\", prepending the \"prefix\" for relative paths. If\n+ * remaining_prefix is not NULL, return the actual prefix still\n+ * remains in the path. For example, prefix = sub1/sub2/ and path is\n+ *\n+ *  foo          -> sub1/sub2/foo  (full prefix)\n+ *  ../foo       -> sub1/foo       (remaining prefix is sub1/)\n+ *  ../../bar    -> bar            (no remaining prefix)\n+ *  ../../sub1/sub2/foo -> sub1/sub2/foo (but no remaining prefix)\n+ *  `pwd`/../bar -> sub1/bar       (no remaining prefix)\n+ */\n+char *prefix_path_gently(const char *prefix, int len,\n+\t\t\t int *remaining_prefix, const char *path)\n {\n \tconst char *orig = path;\n \tchar *sanitized;\n@@ -13,13 +25,17 @@ static char *prefix_path_gently(const char *prefix, int len, const char *path)\n \t\tconst char *temp = real_path(path);\n \t\tsanitized = xmalloc(len + strlen(temp) + 1);\n \t\tstrcpy(sanitized, temp);\n+\t\tif (remaining_prefix)\n+\t\t\t*remaining_prefix = 0;\n \t} else {\n \t\tsanitized = xmalloc(len + strlen(path) + 1);\n \t\tif (len)\n \t\t\tmemcpy(sanitized, prefix, len);\n \t\tstrcpy(sanitized + len, path);\n+\t\tif (remaining_prefix)\n+\t\t\t*remaining_prefix = len;\n \t}\n-\tif (normalize_path_copy(sanitized, sanitized))\n+\tif (normalize_path_copy_len(sanitized, sanitized, remaining_prefix))\n \t\tgoto error_out;\n \tif (is_absolute_path(orig)) {\n \t\tsize_t root_len, len, total;\n@@ -44,7 +60,7 @@ static char *prefix_path_gently(const char *prefix, int len, const char *path)\n \n char *prefix_path(const char *prefix, int len, const char *path)\n {\n-\tchar *r = prefix_path_gently(prefix, len, path);\n+\tchar *r = prefix_path_gently(prefix, len, NULL, path);\n \tif (!r)\n \t\tdie(\"'%s' is outside repository\", path);\n \treturn r;\n@@ -53,7 +69,7 @@ char *prefix_path(const char *prefix, int len, const char *path)\n int path_inside_repo(const char *prefix, const char *path)\n {\n \tint len = prefix ? strlen(prefix) : 0;\n-\tchar *r = prefix_path_gently(prefix, len, path);\n+\tchar *r = prefix_path_gently(prefix, len, NULL, path);\n \tif (r) {\n \t\tfree(r);\n \t\treturn 1;\n-- \n1.8.2.83.gc99314b\n"},{"id":"223314","messageId":"1373790969-13000-42-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 41/46] parse_pathspec: preserve prefix length via PATHSPEC_PREFIX_ORIGIN","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:36:04Z","receivedAt":"2013-07-14T08:36:04Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"The prefix length is passed from one command to another via the new\nmagic 'prefix'. The magic is for parse_pathspec's internal use only,\nnot visible to parse_pathspec's callers.\n\nPrefix length is not preserved across commands when --literal-pathspecs\nis specified (no magic is allowed, including 'prefix'). That's OK\nbecause we know all paths are literal. No magic, no special treatment\nregarding prefix. (This may be no longer true if we make :(glob)\ndefault)\n\nOther options to preserve the prefix include saving it to env variable\nor quoting. Env var way (at least _one_ env var) is not suitable\nbecause the prefix is not the same for all pathspecs. Pathspecs\nstarting with \"../\" will eat into the prefix part.\n\nWe could also preserve 'prefix' across commands by quoting the prefix\npart, then dequoting on receiving. But it may not be 100% accurate, we\nmay dequote longer than the original prefix part, for example. That\nmay be good or not, but it's not the purpose.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n pathspec.c | 41 ++++++++++++++++++++++++++++++++++++-----\n 1 file changed, 36 insertions(+), 5 deletions(-)\n\ndiff --git a/pathspec.c b/pathspec.c\nindex 71e5eaf..82ede57 100644\n--- a/pathspec.c\n+++ b/pathspec.c\n@@ -92,9 +92,9 @@ static unsigned prefix_pathspec(struct pathspec_item *item,\n \t\t\t\tconst char *elt)\n {\n \tunsigned magic = 0, short_magic = 0;\n-\tconst char *copyfrom = elt;\n+\tconst char *copyfrom = elt, *long_magic_end = NULL;\n \tchar *match;\n-\tint i;\n+\tint i, pathspec_prefix = -1;\n \n \tif (elt[0] != ':') {\n \t\t; /* nothing to do */\n@@ -112,18 +112,29 @@ static unsigned prefix_pathspec(struct pathspec_item *item,\n \t\t\t\tnextat = copyfrom + len;\n \t\t\tif (!len)\n \t\t\t\tcontinue;\n-\t\t\tfor (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)\n+\t\t\tfor (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {\n \t\t\t\tif (strlen(pathspec_magic[i].name) == len &&\n \t\t\t\t    !strncmp(pathspec_magic[i].name, copyfrom, len)) {\n \t\t\t\t\tmagic |= pathspec_magic[i].bit;\n \t\t\t\t\tbreak;\n \t\t\t\t}\n+\t\t\t\tif (!prefixcmp(copyfrom, \"prefix:\")) {\n+\t\t\t\t\tchar *endptr;\n+\t\t\t\t\tpathspec_prefix = strtol(copyfrom + 7,\n+\t\t\t\t\t\t\t\t &endptr, 10);\n+\t\t\t\t\tif (endptr - copyfrom != len)\n+\t\t\t\t\t\tdie(_(\"invalid parameter for pathspec magic 'prefix'\"));\n+\t\t\t\t\t/* \"i\" would be wrong, but it does not matter */\n+\t\t\t\t\tbreak;\n+\t\t\t\t}\n+\t\t\t}\n \t\t\tif (ARRAY_SIZE(pathspec_magic) <= i)\n \t\t\t\tdie(_(\"Invalid pathspec magic '%.*s' in '%s'\"),\n \t\t\t\t    (int) len, copyfrom, elt);\n \t\t}\n \t\tif (*copyfrom != ')')\n \t\t\tdie(_(\"Missing ')' at the end of pathspec magic in '%s'\"), elt);\n+\t\tlong_magic_end = copyfrom;\n \t\tcopyfrom++;\n \t} else {\n \t\t/* shorthand */\n@@ -150,7 +161,14 @@ static unsigned prefix_pathspec(struct pathspec_item *item,\n \tmagic |= short_magic;\n \t*p_short_magic = short_magic;\n \n-\tif (magic & PATHSPEC_FROMTOP) {\n+\tif (pathspec_prefix >= 0 &&\n+\t    (prefixlen || (prefix && *prefix)))\n+\t\tdie(\"BUG: 'prefix' magic is supposed to be used at worktree's root\");\n+\n+\tif (pathspec_prefix >= 0) {\n+\t\tmatch = xstrdup(copyfrom);\n+\t\tprefixlen = pathspec_prefix;\n+\t} else if (magic & PATHSPEC_FROMTOP) {\n \t\tmatch = xstrdup(copyfrom);\n \t\tprefixlen = 0;\n \t} else {\n@@ -165,7 +183,20 @@ static unsigned prefix_pathspec(struct pathspec_item *item,\n \t */\n \tif (flags & PATHSPEC_PREFIX_ORIGIN) {\n \t\tstruct strbuf sb = STRBUF_INIT;\n-\t\tstrbuf_add(&sb, elt, copyfrom - elt);\n+\t\tconst char *start = elt;\n+\t\tif (prefixlen && !limit_pathspec_to_literal()) {\n+\t\t\t/* Preserve the actual prefix length of each pattern */\n+\t\t\tif (long_magic_end) {\n+\t\t\t\tstrbuf_add(&sb, start, long_magic_end - start);\n+\t\t\t\tstrbuf_addf(&sb, \",prefix:%d\", prefixlen);\n+\t\t\t\tstart = long_magic_end;\n+\t\t\t} else {\n+\t\t\t\tif (*start == ':')\n+\t\t\t\t\tstart++;\n+\t\t\t\tstrbuf_addf(&sb, \":(prefix:%d)\", prefixlen);\n+\t\t\t}\n+\t\t}\n+\t\tstrbuf_add(&sb, start, copyfrom - start);\n \t\tstrbuf_addstr(&sb, match);\n \t\titem->original = strbuf_detach(&sb, NULL);\n \t} else\n-- \n1.8.2.83.gc99314b\n"},{"id":"223315","messageId":"1373790969-13000-43-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 42/46] Kill limit_pathspec_to_literal() as it's only used by parse_pathspec()","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:36:05Z","receivedAt":"2013-07-14T08:36:05Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n dir.c      | 8 --------\n pathspec.c | 8 ++++++--\n pathspec.h | 2 --\n 3 files changed, 6 insertions(+), 12 deletions(-)\n\ndiff --git a/dir.c b/dir.c\nindex bf22498..79465e7 100644\n--- a/dir.c\n+++ b/dir.c\n@@ -1473,14 +1473,6 @@ int remove_path(const char *name)\n \treturn 0;\n }\n \n-int limit_pathspec_to_literal(void)\n-{\n-\tstatic int flag = -1;\n-\tif (flag < 0)\n-\t\tflag = git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT, 0);\n-\treturn flag;\n-}\n-\n /*\n  * Frees memory within dir which was allocated for exclude lists and\n  * the exclude_stack.  Does not free dir itself.\ndiff --git a/pathspec.c b/pathspec.c\nindex 82ede57..b2e3a87 100644\n--- a/pathspec.c\n+++ b/pathspec.c\n@@ -91,11 +91,15 @@ static unsigned prefix_pathspec(struct pathspec_item *item,\n \t\t\t\tconst char *prefix, int prefixlen,\n \t\t\t\tconst char *elt)\n {\n+\tstatic int literal_global = -1;\n \tunsigned magic = 0, short_magic = 0;\n \tconst char *copyfrom = elt, *long_magic_end = NULL;\n \tchar *match;\n \tint i, pathspec_prefix = -1;\n \n+\tif (literal_global < 0)\n+\t\tliteral_global = git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT, 0);\n+\n \tif (elt[0] != ':') {\n \t\t; /* nothing to do */\n \t} else if (elt[1] == '(') {\n@@ -184,7 +188,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item,\n \tif (flags & PATHSPEC_PREFIX_ORIGIN) {\n \t\tstruct strbuf sb = STRBUF_INIT;\n \t\tconst char *start = elt;\n-\t\tif (prefixlen && !limit_pathspec_to_literal()) {\n+\t\tif (prefixlen && !literal_global) {\n \t\t\t/* Preserve the actual prefix length of each pattern */\n \t\t\tif (long_magic_end) {\n \t\t\t\tstrbuf_add(&sb, start, long_magic_end - start);\n@@ -232,7 +236,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item,\n \t\t\t\t     elt, ce_len, ce->name);\n \t\t}\n \n-\tif (limit_pathspec_to_literal())\n+\tif (literal_global)\n \t\titem->nowildcard_len = item->len;\n \telse {\n \t\titem->nowildcard_len = simple_length(item->match);\ndiff --git a/pathspec.h b/pathspec.h\nindex 2f3532e..7ef9896 100644\n--- a/pathspec.h\n+++ b/pathspec.h\n@@ -61,8 +61,6 @@ extern void parse_pathspec(struct pathspec *pathspec,\n extern void copy_pathspec(struct pathspec *dst, const struct pathspec *src);\n extern void free_pathspec(struct pathspec *);\n \n-extern int limit_pathspec_to_literal(void);\n-\n extern char *find_pathspecs_matching_against_index(const struct pathspec *pathspec);\n extern void add_pathspec_matches_against_index(const struct pathspec *pathspec, char *seen);\n extern const char *check_path_for_gitlink(const char *path);\n-- \n1.8.2.83.gc99314b\n"},{"id":"223316","messageId":"1373790969-13000-44-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 43/46] pathspec: support :(literal) syntax for noglob pathspec","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:36:06Z","receivedAt":"2013-07-14T08:36:06Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n Documentation/glossary-content.txt | 20 ++++++++++++++++----\n builtin/add.c                      |  2 +-\n builtin/diff.c                     |  2 +-\n dir.c                              | 15 ++++++++++++---\n pathspec.c                         | 11 ++++++++---\n pathspec.h                         |  4 +++-\n t/t6130-pathspec-noglob.sh         | 18 ++++++++++++++++++\n tree-diff.c                        |  2 +-\n tree-walk.c                        |  5 ++++-\n 9 files changed, 64 insertions(+), 15 deletions(-)\n\ndiff --git a/Documentation/glossary-content.txt b/Documentation/glossary-content.txt\nindex dba5062..ca9f20f 100644\n--- a/Documentation/glossary-content.txt\n+++ b/Documentation/glossary-content.txt\n@@ -322,10 +322,22 @@ and a close parentheses `)`, and the remainder is the pattern to match\n against the path.\n +\n The \"magic signature\" consists of an ASCII symbol that is not\n-alphanumeric. Currently only the slash `/` is recognized as a\n-\"magic signature\": it makes the pattern match from the root of\n-the working tree, even when you are running the command from\n-inside a subdirectory.\n+alphanumeric.\n++\n+--\n+top `/`;;\n+\tThe magic word `top` (mnemonic: `/`) makes the pattern match\n+\tfrom the root of the working tree, even when you are running\n+\tthe command from inside a subdirectory.\n+\n+literal;;\n+\tWildcards in the pattern such as `*` or `?` are treated\n+\tas literal characters.\n+--\n++\n+Currently only the slash `/` is recognized as the \"magic signature\",\n+but it is envisioned that we will support more types of magic in later\n+versions of Git.\n +\n A pathspec with only a colon means \"there is no pathspec\". This form\n should not be combined with other pathspec.\ndiff --git a/builtin/add.c b/builtin/add.c\nindex 0b80fa8..663ddd1 100644\n--- a/builtin/add.c\n+++ b/builtin/add.c\n@@ -541,7 +541,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)\n \t\t/*\n \t\t * file_exists() assumes exact match\n \t\t */\n-\t\tGUARD_PATHSPEC(&pathspec, PATHSPEC_FROMTOP);\n+\t\tGUARD_PATHSPEC(&pathspec, PATHSPEC_FROMTOP | PATHSPEC_LITERAL);\n \n \t\tfor (i = 0; i < pathspec.nr; i++) {\n \t\t\tconst char *path = pathspec.items[i].match;\ndiff --git a/builtin/diff.c b/builtin/diff.c\nindex bb84ba0..2fb8c5d 100644\n--- a/builtin/diff.c\n+++ b/builtin/diff.c\n@@ -368,7 +368,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix)\n \t}\n \tif (rev.prune_data.nr) {\n \t\t/* builtin_diff_b_f() */\n-\t\tGUARD_PATHSPEC(&rev.prune_data, PATHSPEC_FROMTOP);\n+\t\tGUARD_PATHSPEC(&rev.prune_data, PATHSPEC_FROMTOP | PATHSPEC_LITERAL);\n \t\tif (!path)\n \t\t\tpath = rev.prune_data.items[0].match;\n \t\tpaths += rev.prune_data.nr;\ndiff --git a/dir.c b/dir.c\nindex 79465e7..50ec2f5 100644\n--- a/dir.c\n+++ b/dir.c\n@@ -108,7 +108,10 @@ static size_t common_prefix_len(const struct pathspec *pathspec)\n \tint n;\n \tsize_t max = 0;\n \n-\tGUARD_PATHSPEC(pathspec, PATHSPEC_FROMTOP | PATHSPEC_MAXDEPTH);\n+\tGUARD_PATHSPEC(pathspec,\n+\t\t       PATHSPEC_FROMTOP |\n+\t\t       PATHSPEC_MAXDEPTH |\n+\t\t       PATHSPEC_LITERAL);\n \n \tfor (n = 0; n < pathspec->nr; n++) {\n \t\tsize_t i = 0, len = 0;\n@@ -232,7 +235,10 @@ int match_pathspec_depth(const struct pathspec *ps,\n {\n \tint i, retval = 0;\n \n-\tGUARD_PATHSPEC(ps, PATHSPEC_FROMTOP | PATHSPEC_MAXDEPTH);\n+\tGUARD_PATHSPEC(ps,\n+\t\t       PATHSPEC_FROMTOP |\n+\t\t       PATHSPEC_MAXDEPTH |\n+\t\t       PATHSPEC_LITERAL);\n \n \tif (!ps->nr) {\n \t\tif (!ps->recursive ||\n@@ -1288,7 +1294,10 @@ int read_directory(struct dir_struct *dir, const char *path, int len, const stru\n \t * Check out create_simplify()\n \t */\n \tif (pathspec)\n-\t\tGUARD_PATHSPEC(pathspec, PATHSPEC_FROMTOP | PATHSPEC_MAXDEPTH);\n+\t\tGUARD_PATHSPEC(pathspec,\n+\t\t\t       PATHSPEC_FROMTOP |\n+\t\t\t       PATHSPEC_MAXDEPTH |\n+\t\t\t       PATHSPEC_LITERAL);\n \n \tif (has_symlink_leading_path(path, len))\n \t\treturn dir->nr;\ndiff --git a/pathspec.c b/pathspec.c\nindex b2e3a87..6a16938 100644\n--- a/pathspec.c\n+++ b/pathspec.c\n@@ -70,6 +70,7 @@ static struct pathspec_magic {\n \tconst char *name;\n } pathspec_magic[] = {\n \t{ PATHSPEC_FROMTOP, '/', \"top\" },\n+\t{ PATHSPEC_LITERAL,   0, \"literal\" },\n };\n \n /*\n@@ -92,13 +93,15 @@ static unsigned prefix_pathspec(struct pathspec_item *item,\n \t\t\t\tconst char *elt)\n {\n \tstatic int literal_global = -1;\n-\tunsigned magic = 0, short_magic = 0;\n+\tunsigned magic = 0, short_magic = 0, global_magic = 0;\n \tconst char *copyfrom = elt, *long_magic_end = NULL;\n \tchar *match;\n \tint i, pathspec_prefix = -1;\n \n \tif (literal_global < 0)\n \t\tliteral_global = git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT, 0);\n+\tif (literal_global)\n+\t\tglobal_magic |= PATHSPEC_LITERAL;\n \n \tif (elt[0] != ':') {\n \t\t; /* nothing to do */\n@@ -164,6 +167,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item,\n \n \tmagic |= short_magic;\n \t*p_short_magic = short_magic;\n+\tmagic |= global_magic;\n \n \tif (pathspec_prefix >= 0 &&\n \t    (prefixlen || (prefix && *prefix)))\n@@ -236,7 +240,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item,\n \t\t\t\t     elt, ce_len, ce->name);\n \t\t}\n \n-\tif (literal_global)\n+\tif (magic & PATHSPEC_LITERAL)\n \t\titem->nowildcard_len = item->len;\n \telse {\n \t\titem->nowildcard_len = simple_length(item->match);\n@@ -402,7 +406,8 @@ const char **get_pathspec(const char *prefix, const char **pathspec)\n {\n \tstruct pathspec ps;\n \tparse_pathspec(&ps,\n-\t\t       PATHSPEC_ALL_MAGIC & ~PATHSPEC_FROMTOP,\n+\t\t       PATHSPEC_ALL_MAGIC &\n+\t\t       ~(PATHSPEC_FROMTOP | PATHSPEC_LITERAL),\n \t\t       PATHSPEC_PREFER_CWD,\n \t\t       prefix, pathspec);\n \treturn ps._raw;\ndiff --git a/pathspec.h b/pathspec.h\nindex 7ef9896..987d70c 100644\n--- a/pathspec.h\n+++ b/pathspec.h\n@@ -4,9 +4,11 @@\n /* Pathspec magic */\n #define PATHSPEC_FROMTOP\t(1<<0)\n #define PATHSPEC_MAXDEPTH\t(1<<1)\n+#define PATHSPEC_LITERAL\t(1<<2)\n #define PATHSPEC_ALL_MAGIC\t  \\\n \t(PATHSPEC_FROMTOP\t| \\\n-\t PATHSPEC_MAXDEPTH)\n+\t PATHSPEC_MAXDEPTH\t| \\\n+\t PATHSPEC_LITERAL)\n \n #define PATHSPEC_ONESTAR 1\t/* the pathspec pattern sastisfies GFNM_ONESTAR */\n \ndiff --git a/t/t6130-pathspec-noglob.sh b/t/t6130-pathspec-noglob.sh\nindex 39ef619..49c148e 100755\n--- a/t/t6130-pathspec-noglob.sh\n+++ b/t/t6130-pathspec-noglob.sh\n@@ -47,18 +47,36 @@ test_expect_success 'no-glob option matches literally (vanilla)' '\n \ttest_cmp expect actual\n '\n \n+test_expect_success 'no-glob option matches literally (vanilla)' '\n+\techo vanilla >expect &&\n+\tgit log --format=%s -- \":(literal)foo\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n test_expect_success 'no-glob option matches literally (star)' '\n \techo star >expect &&\n \tgit --literal-pathspecs log --format=%s -- \"f*\" >actual &&\n \ttest_cmp expect actual\n '\n \n+test_expect_success 'no-glob option matches literally (star)' '\n+\techo star >expect &&\n+\tgit log --format=%s -- \":(literal)f*\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n test_expect_success 'no-glob option matches literally (bracket)' '\n \techo bracket >expect &&\n \tgit --literal-pathspecs log --format=%s -- \"f[o][o]\" >actual &&\n \ttest_cmp expect actual\n '\n \n+test_expect_success 'no-glob option matches literally (bracket)' '\n+\techo bracket >expect &&\n+\tgit log --format=%s -- \":(literal)f[o][o]\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n test_expect_success 'no-glob environment variable works' '\n \techo star >expect &&\n \tGIT_LITERAL_PATHSPECS=1 git log --format=%s -- \"f*\" >actual &&\ndiff --git a/tree-diff.c b/tree-diff.c\nindex 21a50d8..ccf9d7c 100644\n--- a/tree-diff.c\n+++ b/tree-diff.c\n@@ -202,7 +202,7 @@ static void try_to_follow_renames(struct tree_desc *t1, struct tree_desc *t2, co\n \t * path. Magic that matches more than one path is not\n \t * supported.\n \t */\n-\tGUARD_PATHSPEC(&opt->pathspec, PATHSPEC_FROMTOP);\n+\tGUARD_PATHSPEC(&opt->pathspec, PATHSPEC_FROMTOP | PATHSPEC_LITERAL);\n #if 0\n \t/*\n \t * We should reject wildcards as well. Unfortunately we\ndiff --git a/tree-walk.c b/tree-walk.c\nindex 37b157e..676bd7f 100644\n--- a/tree-walk.c\n+++ b/tree-walk.c\n@@ -636,7 +636,10 @@ enum interesting tree_entry_interesting(const struct name_entry *entry,\n \tenum interesting never_interesting = ps->has_wildcard ?\n \t\tentry_not_interesting : all_entries_not_interesting;\n \n-\tGUARD_PATHSPEC(ps, PATHSPEC_FROMTOP | PATHSPEC_MAXDEPTH);\n+\tGUARD_PATHSPEC(ps,\n+\t\t       PATHSPEC_FROMTOP |\n+\t\t       PATHSPEC_MAXDEPTH |\n+\t\t       PATHSPEC_LITERAL);\n \n \tif (!ps->nr) {\n \t\tif (!ps->recursive ||\n-- \n1.8.2.83.gc99314b\n"},{"id":"223317","messageId":"1373790969-13000-45-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 44/46] pathspec: make --literal-pathspecs disable pathspec magic","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:36:07Z","receivedAt":"2013-07-14T08:36:07Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"--literal-pathspecs and its equivalent environment variable are\nprobably used for scripting. In that setting, pathspec magic may be\nunwanted. Disabling globbing in individual pathspec can be done via\n:(literal) magic.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n Documentation/git.txt      | 4 ++--\n pathspec.c                 | 2 +-\n t/t6130-pathspec-noglob.sh | 6 ++++++\n 3 files changed, 9 insertions(+), 3 deletions(-)\n\ndiff --git a/Documentation/git.txt b/Documentation/git.txt\nindex b738a40..80139ae 100644\n--- a/Documentation/git.txt\n+++ b/Documentation/git.txt\n@@ -450,8 +450,8 @@ help ...`.\n \tlinkgit:git-replace[1] for more information.\n \n --literal-pathspecs::\n-\tTreat pathspecs literally, rather than as glob patterns. This is\n-\tequivalent to setting the `GIT_LITERAL_PATHSPECS` environment\n+\tTreat pathspecs literally (i.e. no globbing, no pathspec magic).\n+\tThis is equivalent to setting the `GIT_LITERAL_PATHSPECS` environment\n \tvariable to `1`.\n \n \ndiff --git a/pathspec.c b/pathspec.c\nindex 6a16938..b6d8e74 100644\n--- a/pathspec.c\n+++ b/pathspec.c\n@@ -103,7 +103,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item,\n \tif (literal_global)\n \t\tglobal_magic |= PATHSPEC_LITERAL;\n \n-\tif (elt[0] != ':') {\n+\tif (elt[0] != ':' || literal_global) {\n \t\t; /* nothing to do */\n \t} else if (elt[1] == '(') {\n \t\t/* longhand */\ndiff --git a/t/t6130-pathspec-noglob.sh b/t/t6130-pathspec-noglob.sh\nindex 49c148e..8551b02 100755\n--- a/t/t6130-pathspec-noglob.sh\n+++ b/t/t6130-pathspec-noglob.sh\n@@ -77,6 +77,12 @@ test_expect_success 'no-glob option matches literally (bracket)' '\n \ttest_cmp expect actual\n '\n \n+test_expect_success 'no-glob option disables :(literal)' '\n+\t: >expect &&\n+\tgit --literal-pathspecs log --format=%s -- \":(literal)foo\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n test_expect_success 'no-glob environment variable works' '\n \techo star >expect &&\n \tGIT_LITERAL_PATHSPECS=1 git log --format=%s -- \"f*\" >actual &&\n-- \n1.8.2.83.gc99314b\n"},{"id":"223318","messageId":"1373790969-13000-46-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 45/46] pathspec: support :(glob) syntax","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:36:08Z","receivedAt":"2013-07-14T08:36:08Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":":(glob)path differs from plain pathspec that it uses wildmatch with\nWM_PATHNAME while the other uses fnmatch without FNM_PATHNAME. The\ndifference lies in how '*' (and '**') is processed.\n\nWith the introduction of :(glob) and :(literal) and their global\noptions --[no]glob-pathspecs, the user can:\n\n - make everything literal by default via --noglob-pathspecs\n   --literal-pathspecs cannot be used for this purpose as it\n   disables _all_ pathspec magic.\n\n - individually turn on globbing with :(glob)\n\n - make everything globbing by default via --glob-pathspecs\n\n - individually turn off globbing with :(literal)\n\nThe implication behind this is, there is no way to gain the default\nmatching behavior (i.e. fnmatch without FNM_PATHNAME). You either get\nnew globbing or literal. The old fnmatch behavior is considered\ndeprecated and discouraged to use.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n Documentation/git.txt              | 19 ++++++++++++\n Documentation/glossary-content.txt | 29 ++++++++++++++++++\n builtin/add.c                      |  9 ++++--\n builtin/ls-tree.c                  |  2 +-\n cache.h                            |  2 ++\n dir.c                              | 28 +++++++++--------\n dir.h                              |  9 +++---\n git.c                              |  8 +++++\n pathspec.c                         | 47 +++++++++++++++++++++++++---\n pathspec.h                         |  4 ++-\n t/t6130-pathspec-noglob.sh         | 63 ++++++++++++++++++++++++++++++++++++++\n tree-walk.c                        |  9 +++---\n 12 files changed, 198 insertions(+), 31 deletions(-)\n\ndiff --git a/Documentation/git.txt b/Documentation/git.txt\nindex 80139ae..3571a1b 100644\n--- a/Documentation/git.txt\n+++ b/Documentation/git.txt\n@@ -454,6 +454,17 @@ help ...`.\n \tThis is equivalent to setting the `GIT_LITERAL_PATHSPECS` environment\n \tvariable to `1`.\n \n+--glob-pathspecs:\n+\tAdd \"glob\" magic to all pathspec. This is equivalent to setting\n+\tthe `GIT_GLOB_PATHSPECS` environment variable to `1`. Disabling\n+\tglobbing on individual pathspecs can be done using pathspec\n+\tmagic \":(literal)\"\n+\n+--noglob-pathspecs:\n+\tAdd \"literal\" magic to all pathspec. This is equivalent to setting\n+\tthe `GIT_NOGLOB_PATHSPECS` environment variable to `1`. Enabling\n+\tglobbing on individual pathspecs can be done using pathspec\n+\tmagic \":(glob)\"\n \n GIT COMMANDS\n ------------\n@@ -860,6 +871,14 @@ GIT_LITERAL_PATHSPECS::\n \tliteral paths to Git (e.g., paths previously given to you by\n \t`git ls-tree`, `--raw` diff output, etc).\n \n+GIT_GLOB_PATHSPECS::\n+\tSetting this variable to `1` will cause Git to treat all\n+\tpathspecs as glob patterns (aka \"glob\" magic).\n+\n+GIT_NOGLOB_PATHSPECS::\n+\tSetting this variable to `1` will cause Git to treat all\n+\tpathspecs as literal (aka \"literal\" magic).\n+\n \n Discussion[[Discussion]]\n ------------------------\ndiff --git a/Documentation/glossary-content.txt b/Documentation/glossary-content.txt\nindex ca9f20f..a3d9029 100644\n--- a/Documentation/glossary-content.txt\n+++ b/Documentation/glossary-content.txt\n@@ -333,6 +333,35 @@ top `/`;;\n literal;;\n \tWildcards in the pattern such as `*` or `?` are treated\n \tas literal characters.\n+\n+glob;;\n+\tGit treats the pattern as a shell glob suitable for\n+\tconsumption by fnmatch(3) with the FNM_PATHNAME flag:\n+\twildcards in the pattern will not match a / in the pathname.\n+\tFor example, \"Documentation/{asterisk}.html\" matches\n+\t\"Documentation/git.html\" but not \"Documentation/ppc/ppc.html\"\n+\tor \"tools/perf/Documentation/perf.html\".\n++\n+Two consecutive asterisks (\"`**`\") in patterns matched against\n+full pathname may have special meaning:\n+\n+ - A leading \"`**`\" followed by a slash means match in all\n+   directories. For example, \"`**/foo`\" matches file or directory\n+   \"`foo`\" anywhere, the same as pattern \"`foo`\". \"**/foo/bar\"\n+   matches file or directory \"`bar`\" anywhere that is directly\n+   under directory \"`foo`\".\n+\n+ - A trailing \"/**\" matches everything inside. For example,\n+   \"abc/**\" matches all files inside directory \"abc\", relative\n+   to the location of the `.gitignore` file, with infinite depth.\n+\n+ - A slash followed by two consecutive asterisks then a slash\n+   matches zero or more directories. For example, \"`a/**/b`\"\n+   matches \"`a/b`\", \"`a/x/b`\", \"`a/x/y/b`\" and so on.\n+\n+ - Other consecutive asterisks are considered invalid.\n++\n+Glob magic is incompatible with literal magic.\n --\n +\n Currently only the slash `/` is recognized as the \"magic signature\",\ndiff --git a/builtin/add.c b/builtin/add.c\nindex 663ddd1..1dab246 100644\n--- a/builtin/add.c\n+++ b/builtin/add.c\n@@ -541,11 +541,16 @@ int cmd_add(int argc, const char **argv, const char *prefix)\n \t\t/*\n \t\t * file_exists() assumes exact match\n \t\t */\n-\t\tGUARD_PATHSPEC(&pathspec, PATHSPEC_FROMTOP | PATHSPEC_LITERAL);\n+\t\tGUARD_PATHSPEC(&pathspec,\n+\t\t\t       PATHSPEC_FROMTOP |\n+\t\t\t       PATHSPEC_LITERAL |\n+\t\t\t       PATHSPEC_GLOB);\n \n \t\tfor (i = 0; i < pathspec.nr; i++) {\n \t\t\tconst char *path = pathspec.items[i].match;\n-\t\t\tif (!seen[i] && !file_exists(path)) {\n+\t\t\tif (!seen[i] &&\n+\t\t\t    ((pathspec.items[i].magic & PATHSPEC_GLOB) ||\n+\t\t\t     !file_exists(path))) {\n \t\t\t\tif (ignore_missing) {\n \t\t\t\t\tint dtype = DT_UNKNOWN;\n \t\t\t\t\tif (is_excluded(&dir, path, &dtype))\ndiff --git a/builtin/ls-tree.c b/builtin/ls-tree.c\nindex 1c4f48e..7882352 100644\n--- a/builtin/ls-tree.c\n+++ b/builtin/ls-tree.c\n@@ -173,7 +173,7 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)\n \t * cannot be lifted until it is converted to use\n \t * match_pathspec_depth() or tree_entry_interesting()\n \t */\n-\tparse_pathspec(&pathspec, 0,\n+\tparse_pathspec(&pathspec, PATHSPEC_GLOB,\n \t\t       PATHSPEC_PREFER_CWD,\n \t\t       prefix, argv + 1);\n \tfor (i = 0; i < pathspec.nr; i++)\ndiff --git a/cache.h b/cache.h\nindex 13e3c94..dc4d2ee 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -367,6 +367,8 @@ static inline enum object_type object_type(unsigned int mode)\n #define GIT_NOTES_REWRITE_REF_ENVIRONMENT \"GIT_NOTES_REWRITE_REF\"\n #define GIT_NOTES_REWRITE_MODE_ENVIRONMENT \"GIT_NOTES_REWRITE_MODE\"\n #define GIT_LITERAL_PATHSPECS_ENVIRONMENT \"GIT_LITERAL_PATHSPECS\"\n+#define GIT_GLOB_PATHSPECS_ENVIRONMENT \"GIT_GLOB_PATHSPECS\"\n+#define GIT_NOGLOB_PATHSPECS_ENVIRONMENT \"GIT_NOGLOB_PATHSPECS\"\n \n /*\n  * This environment variable is expected to contain a boolean indicating\ndiff --git a/dir.c b/dir.c\nindex 50ec2f5..076bd46 100644\n--- a/dir.c\n+++ b/dir.c\n@@ -52,26 +52,28 @@ int fnmatch_icase(const char *pattern, const char *string, int flags)\n \treturn fnmatch(pattern, string, flags | (ignore_case ? FNM_CASEFOLD : 0));\n }\n \n-inline int git_fnmatch(const char *pattern, const char *string,\n-\t\t       int flags, int prefix)\n+inline int git_fnmatch(const struct pathspec_item *item,\n+\t\t       const char *pattern, const char *string,\n+\t\t       int prefix)\n {\n-\tint fnm_flags = 0;\n-\tif (flags & GFNM_PATHNAME)\n-\t\tfnm_flags |= FNM_PATHNAME;\n \tif (prefix > 0) {\n \t\tif (strncmp(pattern, string, prefix))\n \t\t\treturn FNM_NOMATCH;\n \t\tpattern += prefix;\n \t\tstring += prefix;\n \t}\n-\tif (flags & GFNM_ONESTAR) {\n+\tif (item->flags & PATHSPEC_ONESTAR) {\n \t\tint pattern_len = strlen(++pattern);\n \t\tint string_len = strlen(string);\n \t\treturn string_len < pattern_len ||\n \t\t       strcmp(pattern,\n \t\t\t      string + string_len - pattern_len);\n \t}\n-\treturn fnmatch(pattern, string, fnm_flags);\n+\tif (item->magic & PATHSPEC_GLOB)\n+\t\treturn wildmatch(pattern, string, WM_PATHNAME, NULL);\n+\telse\n+\t\t/* wildmatch has not learned no FNM_PATHNAME mode yet */\n+\t\treturn fnmatch(pattern, string, 0);\n }\n \n static int fnmatch_icase_mem(const char *pattern, int patternlen,\n@@ -111,7 +113,8 @@ static size_t common_prefix_len(const struct pathspec *pathspec)\n \tGUARD_PATHSPEC(pathspec,\n \t\t       PATHSPEC_FROMTOP |\n \t\t       PATHSPEC_MAXDEPTH |\n-\t\t       PATHSPEC_LITERAL);\n+\t\t       PATHSPEC_LITERAL |\n+\t\t       PATHSPEC_GLOB);\n \n \tfor (n = 0; n < pathspec->nr; n++) {\n \t\tsize_t i = 0, len = 0;\n@@ -206,8 +209,7 @@ static int match_pathspec_item(const struct pathspec_item *item, int prefix,\n \t}\n \n \tif (item->nowildcard_len < item->len &&\n-\t    !git_fnmatch(match, name,\n-\t\t\t item->flags & PATHSPEC_ONESTAR ? GFNM_ONESTAR : 0,\n+\t    !git_fnmatch(item, match, name,\n \t\t\t item->nowildcard_len - prefix))\n \t\treturn MATCHED_FNMATCH;\n \n@@ -238,7 +240,8 @@ int match_pathspec_depth(const struct pathspec *ps,\n \tGUARD_PATHSPEC(ps,\n \t\t       PATHSPEC_FROMTOP |\n \t\t       PATHSPEC_MAXDEPTH |\n-\t\t       PATHSPEC_LITERAL);\n+\t\t       PATHSPEC_LITERAL |\n+\t\t       PATHSPEC_GLOB);\n \n \tif (!ps->nr) {\n \t\tif (!ps->recursive ||\n@@ -1297,7 +1300,8 @@ int read_directory(struct dir_struct *dir, const char *path, int len, const stru\n \t\tGUARD_PATHSPEC(pathspec,\n \t\t\t       PATHSPEC_FROMTOP |\n \t\t\t       PATHSPEC_MAXDEPTH |\n-\t\t\t       PATHSPEC_LITERAL);\n+\t\t\t       PATHSPEC_LITERAL |\n+\t\t\t       PATHSPEC_GLOB);\n \n \tif (has_symlink_leading_path(path, len))\n \t\treturn dir->nr;\ndiff --git a/dir.h b/dir.h\nindex 7d051e3..343ec7a 100644\n--- a/dir.h\n+++ b/dir.h\n@@ -199,10 +199,9 @@ extern int fnmatch_icase(const char *pattern, const char *string, int flags);\n /*\n  * The prefix part of pattern must not contains wildcards.\n  */\n-#define GFNM_PATHNAME 1\t\t/* similar to FNM_PATHNAME */\n-#define GFNM_ONESTAR  2\t\t/* there is only _one_ wildcard, a star */\n-\n-extern int git_fnmatch(const char *pattern, const char *string,\n-\t\t       int flags, int prefix);\n+struct pathspec_item;\n+extern int git_fnmatch(const struct pathspec_item *item,\n+\t\t       const char *pattern, const char *string,\n+\t\t       int prefix);\n \n #endif\ndiff --git a/git.c b/git.c\nindex 4359086..2509675 100644\n--- a/git.c\n+++ b/git.c\n@@ -147,6 +147,14 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)\n \t\t\tsetenv(GIT_LITERAL_PATHSPECS_ENVIRONMENT, \"0\", 1);\n \t\t\tif (envchanged)\n \t\t\t\t*envchanged = 1;\n+\t\t} else if (!strcmp(cmd, \"--glob-pathspecs\")) {\n+\t\t\tsetenv(GIT_GLOB_PATHSPECS_ENVIRONMENT, \"1\", 1);\n+\t\t\tif (envchanged)\n+\t\t\t\t*envchanged = 1;\n+\t\t} else if (!strcmp(cmd, \"--noglob-pathspecs\")) {\n+\t\t\tsetenv(GIT_NOGLOB_PATHSPECS_ENVIRONMENT, \"1\", 1);\n+\t\t\tif (envchanged)\n+\t\t\t\t*envchanged = 1;\n \t\t} else if (!strcmp(cmd, \"--shallow-file\")) {\n \t\t\t(*argv)++;\n \t\t\t(*argc)--;\ndiff --git a/pathspec.c b/pathspec.c\nindex b6d8e74..c1e6917 100644\n--- a/pathspec.c\n+++ b/pathspec.c\n@@ -57,7 +57,6 @@ char *find_pathspecs_matching_against_index(const struct pathspec *pathspec)\n  *\n  * Possible future magic semantics include stuff like:\n  *\n- *\t{ PATHSPEC_NOGLOB, '!', \"noglob\" },\n  *\t{ PATHSPEC_ICASE, '\\0', \"icase\" },\n  *\t{ PATHSPEC_RECURSIVE, '*', \"recursive\" },\n  *\t{ PATHSPEC_REGEXP, '\\0', \"regexp\" },\n@@ -71,6 +70,7 @@ static struct pathspec_magic {\n } pathspec_magic[] = {\n \t{ PATHSPEC_FROMTOP, '/', \"top\" },\n \t{ PATHSPEC_LITERAL,   0, \"literal\" },\n+\t{ PATHSPEC_GLOB,   '\\0', \"glob\" },\n };\n \n /*\n@@ -93,6 +93,8 @@ static unsigned prefix_pathspec(struct pathspec_item *item,\n \t\t\t\tconst char *elt)\n {\n \tstatic int literal_global = -1;\n+\tstatic int glob_global = -1;\n+\tstatic int noglob_global = -1;\n \tunsigned magic = 0, short_magic = 0, global_magic = 0;\n \tconst char *copyfrom = elt, *long_magic_end = NULL;\n \tchar *match;\n@@ -103,6 +105,22 @@ static unsigned prefix_pathspec(struct pathspec_item *item,\n \tif (literal_global)\n \t\tglobal_magic |= PATHSPEC_LITERAL;\n \n+\tif (glob_global < 0)\n+\t\tglob_global = git_env_bool(GIT_GLOB_PATHSPECS_ENVIRONMENT, 0);\n+\tif (glob_global)\n+\t\tglobal_magic |= PATHSPEC_GLOB;\n+\n+\tif (noglob_global < 0)\n+\t\tnoglob_global = git_env_bool(GIT_NOGLOB_PATHSPECS_ENVIRONMENT, 0);\n+\n+\tif (glob_global && noglob_global)\n+\t\tdie(_(\"global 'glob' and 'noglob' pathspec settings are incompatible\"));\n+\n+\tif ((global_magic & PATHSPEC_LITERAL) &&\n+\t    (global_magic & ~PATHSPEC_LITERAL))\n+\t\tdie(_(\"global 'literal' pathspec setting is incompatible \"\n+\t\t      \"with all other global pathspec settings\"));\n+\n \tif (elt[0] != ':' || literal_global) {\n \t\t; /* nothing to do */\n \t} else if (elt[1] == '(') {\n@@ -167,12 +185,24 @@ static unsigned prefix_pathspec(struct pathspec_item *item,\n \n \tmagic |= short_magic;\n \t*p_short_magic = short_magic;\n+\n+\t/* --noglob-pathspec adds :(literal) _unless_ :(glob) is specifed */\n+\tif (noglob_global && !(magic & PATHSPEC_GLOB))\n+\t\tglobal_magic |= PATHSPEC_LITERAL;\n+\n+\t/* --glob-pathspec is overriden by :(literal) */\n+\tif ((global_magic & PATHSPEC_GLOB) && (magic & PATHSPEC_LITERAL))\n+\t\tglobal_magic &= ~PATHSPEC_GLOB;\n+\n \tmagic |= global_magic;\n \n \tif (pathspec_prefix >= 0 &&\n \t    (prefixlen || (prefix && *prefix)))\n \t\tdie(\"BUG: 'prefix' magic is supposed to be used at worktree's root\");\n \n+\tif ((magic & PATHSPEC_LITERAL) && (magic & PATHSPEC_GLOB))\n+\t\tdie(_(\"%s: 'literal' and 'glob' are incompatible\"), elt);\n+\n \tif (pathspec_prefix >= 0) {\n \t\tmatch = xstrdup(copyfrom);\n \t\tprefixlen = pathspec_prefix;\n@@ -248,10 +278,17 @@ static unsigned prefix_pathspec(struct pathspec_item *item,\n \t\t\titem->nowildcard_len = prefixlen;\n \t}\n \titem->flags = 0;\n-\tif (item->nowildcard_len < item->len &&\n-\t    item->match[item->nowildcard_len] == '*' &&\n-\t    no_wildcard(item->match + item->nowildcard_len + 1))\n-\t\titem->flags |= PATHSPEC_ONESTAR;\n+\tif (magic & PATHSPEC_GLOB) {\n+\t\t/*\n+\t\t * FIXME: should we enable ONESTAR in _GLOB for\n+\t\t * pattern \"* * / * . c\"?\n+\t\t */\n+\t} else {\n+\t\tif (item->nowildcard_len < item->len &&\n+\t\t    item->match[item->nowildcard_len] == '*' &&\n+\t\t    no_wildcard(item->match + item->nowildcard_len + 1))\n+\t\t\titem->flags |= PATHSPEC_ONESTAR;\n+\t}\n \n \t/* sanity checks, pathspec matchers assume these are sane */\n \tassert(item->nowildcard_len <= item->len &&\ndiff --git a/pathspec.h b/pathspec.h\nindex 987d70c..cdf2fa3 100644\n--- a/pathspec.h\n+++ b/pathspec.h\n@@ -5,10 +5,12 @@\n #define PATHSPEC_FROMTOP\t(1<<0)\n #define PATHSPEC_MAXDEPTH\t(1<<1)\n #define PATHSPEC_LITERAL\t(1<<2)\n+#define PATHSPEC_GLOB\t\t(1<<3)\n #define PATHSPEC_ALL_MAGIC\t  \\\n \t(PATHSPEC_FROMTOP\t| \\\n \t PATHSPEC_MAXDEPTH\t| \\\n-\t PATHSPEC_LITERAL)\n+\t PATHSPEC_LITERAL\t| \\\n+\t PATHSPEC_GLOB)\n \n #define PATHSPEC_ONESTAR 1\t/* the pathspec pattern sastisfies GFNM_ONESTAR */\n \ndiff --git a/t/t6130-pathspec-noglob.sh b/t/t6130-pathspec-noglob.sh\nindex 8551b02..ea00d71 100755\n--- a/t/t6130-pathspec-noglob.sh\n+++ b/t/t6130-pathspec-noglob.sh\n@@ -32,6 +32,16 @@ test_expect_success 'star pathspec globs' '\n \ttest_cmp expect actual\n '\n \n+test_expect_success 'star pathspec globs' '\n+\tcat >expect <<-\\EOF &&\n+\tbracket\n+\tstar\n+\tvanilla\n+\tEOF\n+\tgit log --format=%s -- \":(glob)f*\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n test_expect_success 'bracket pathspec globs and matches literal brackets' '\n \tcat >expect <<-\\EOF &&\n \tbracket\n@@ -41,6 +51,15 @@ test_expect_success 'bracket pathspec globs and matches literal brackets' '\n \ttest_cmp expect actual\n '\n \n+test_expect_success 'bracket pathspec globs and matches literal brackets' '\n+\tcat >expect <<-\\EOF &&\n+\tbracket\n+\tvanilla\n+\tEOF\n+\tgit log --format=%s -- \":(glob)f[o][o]\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n test_expect_success 'no-glob option matches literally (vanilla)' '\n \techo vanilla >expect &&\n \tgit --literal-pathspecs log --format=%s -- foo >actual &&\n@@ -89,4 +108,48 @@ test_expect_success 'no-glob environment variable works' '\n \ttest_cmp expect actual\n '\n \n+test_expect_success 'setup xxx/bar' '\n+\tmkdir xxx &&\n+\ttest_commit xxx xxx/bar\n+'\n+\n+test_expect_success '**/ works with :(glob)' '\n+\tcat >expect <<-\\EOF &&\n+\txxx\n+\tunrelated\n+\tEOF\n+\tgit log --format=%s -- \":(glob)**/bar\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success '**/ does not work with --noglob-pathspecs' '\n+\t: >expect &&\n+\tgit --noglob-pathspecs log --format=%s -- \"**/bar\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success '**/ works with :(glob) and --noglob-pathspecs' '\n+\tcat >expect <<-\\EOF &&\n+\txxx\n+\tunrelated\n+\tEOF\n+\tgit --noglob-pathspecs log --format=%s -- \":(glob)**/bar\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success '**/ works with --glob-pathspecs' '\n+\tcat >expect <<-\\EOF &&\n+\txxx\n+\tunrelated\n+\tEOF\n+\tgit --glob-pathspecs log --format=%s -- \"**/bar\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success '**/ does not work with :(literal) and --glob-pathspecs' '\n+\t: >expect &&\n+\tgit --glob-pathspecs log --format=%s -- \":(literal)**/bar\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n test_done\ndiff --git a/tree-walk.c b/tree-walk.c\nindex 676bd7f..a44f528 100644\n--- a/tree-walk.c\n+++ b/tree-walk.c\n@@ -639,7 +639,8 @@ enum interesting tree_entry_interesting(const struct name_entry *entry,\n \tGUARD_PATHSPEC(ps,\n \t\t       PATHSPEC_FROMTOP |\n \t\t       PATHSPEC_MAXDEPTH |\n-\t\t       PATHSPEC_LITERAL);\n+\t\t       PATHSPEC_LITERAL |\n+\t\t       PATHSPEC_GLOB);\n \n \tif (!ps->nr) {\n \t\tif (!ps->recursive ||\n@@ -685,8 +686,7 @@ enum interesting tree_entry_interesting(const struct name_entry *entry,\n \t\t\t\treturn entry_interesting;\n \n \t\t\tif (item->nowildcard_len < item->len) {\n-\t\t\t\tif (!git_fnmatch(match + baselen, entry->path,\n-\t\t\t\t\t\t item->flags & PATHSPEC_ONESTAR ? GFNM_ONESTAR : 0,\n+\t\t\t\tif (!git_fnmatch(item, match + baselen, entry->path,\n \t\t\t\t\t\t item->nowildcard_len - baselen))\n \t\t\t\t\treturn entry_interesting;\n \n@@ -727,8 +727,7 @@ match_wildcards:\n \n \t\tstrbuf_add(base, entry->path, pathlen);\n \n-\t\tif (!git_fnmatch(match, base->buf + base_offset,\n-\t\t\t\t item->flags & PATHSPEC_ONESTAR ? GFNM_ONESTAR : 0,\n+\t\tif (!git_fnmatch(item, match, base->buf + base_offset,\n \t\t\t\t item->nowildcard_len)) {\n \t\t\tstrbuf_setlen(base, base_offset + baselen);\n \t\t\treturn entry_interesting;\n-- \n1.8.2.83.gc99314b\n"},{"id":"223319","messageId":"1373790969-13000-47-git-send-email-pclouds@gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 46/46] parse_pathspec: accept :(icase)path syntax","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-07-14T08:36:09Z","receivedAt":"2013-07-14T08:36:09Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n Documentation/git.txt              |  8 ++++\n Documentation/glossary-content.txt |  3 ++\n builtin/add.c                      |  6 ++-\n builtin/ls-tree.c                  |  2 +-\n cache.h                            |  1 +\n dir.c                              | 74 ++++++++++++++++++++++++-----\n git.c                              |  4 ++\n pathspec.c                         |  9 +++-\n pathspec.h                         | 22 ++++++++-\n t/t6131-pathspec-icase.sh (new +x) | 97 ++++++++++++++++++++++++++++++++++++++\n tree-walk.c                        | 59 ++++++++++++++++++-----\n 11 files changed, 257 insertions(+), 28 deletions(-)\n create mode 100755 t/t6131-pathspec-icase.sh\n\ndiff --git a/Documentation/git.txt b/Documentation/git.txt\nindex 3571a1b..546eea4 100644\n--- a/Documentation/git.txt\n+++ b/Documentation/git.txt\n@@ -466,6 +466,10 @@ help ...`.\n \tglobbing on individual pathspecs can be done using pathspec\n \tmagic \":(glob)\"\n \n+--icase-pathspecs:\n+\tAdd \"icase\" magic to all pathspec. This is equivalent to setting\n+\tthe `GIT_ICASE_PATHSPECS` environment variable to `1`.\n+\n GIT COMMANDS\n ------------\n \n@@ -879,6 +883,10 @@ GIT_NOGLOB_PATHSPECS::\n \tSetting this variable to `1` will cause Git to treat all\n \tpathspecs as literal (aka \"literal\" magic).\n \n+GIT_ICASE_PATHSPECS::\n+\tSetting this variable to `1` will cause Git to treat all\n+\tpathspeccs as case-insensitive.\n+\n \n Discussion[[Discussion]]\n ------------------------\ndiff --git a/Documentation/glossary-content.txt b/Documentation/glossary-content.txt\nindex a3d9029..13a64d3 100644\n--- a/Documentation/glossary-content.txt\n+++ b/Documentation/glossary-content.txt\n@@ -334,6 +334,9 @@ literal;;\n \tWildcards in the pattern such as `*` or `?` are treated\n \tas literal characters.\n \n+icase;;\n+\tCase insensitive match.\n+\n glob;;\n \tGit treats the pattern as a shell glob suitable for\n \tconsumption by fnmatch(3) with the FNM_PATHNAME flag:\ndiff --git a/builtin/add.c b/builtin/add.c\nindex 1dab246..9d52fc7 100644\n--- a/builtin/add.c\n+++ b/builtin/add.c\n@@ -544,12 +544,14 @@ int cmd_add(int argc, const char **argv, const char *prefix)\n \t\tGUARD_PATHSPEC(&pathspec,\n \t\t\t       PATHSPEC_FROMTOP |\n \t\t\t       PATHSPEC_LITERAL |\n-\t\t\t       PATHSPEC_GLOB);\n+\t\t\t       PATHSPEC_GLOB |\n+\t\t\t       PATHSPEC_ICASE);\n \n \t\tfor (i = 0; i < pathspec.nr; i++) {\n \t\t\tconst char *path = pathspec.items[i].match;\n \t\t\tif (!seen[i] &&\n-\t\t\t    ((pathspec.items[i].magic & PATHSPEC_GLOB) ||\n+\t\t\t    ((pathspec.items[i].magic &\n+\t\t\t      (PATHSPEC_GLOB | PATHSPEC_ICASE)) ||\n \t\t\t     !file_exists(path))) {\n \t\t\t\tif (ignore_missing) {\n \t\t\t\t\tint dtype = DT_UNKNOWN;\ndiff --git a/builtin/ls-tree.c b/builtin/ls-tree.c\nindex 7882352..f6d8215 100644\n--- a/builtin/ls-tree.c\n+++ b/builtin/ls-tree.c\n@@ -173,7 +173,7 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)\n \t * cannot be lifted until it is converted to use\n \t * match_pathspec_depth() or tree_entry_interesting()\n \t */\n-\tparse_pathspec(&pathspec, PATHSPEC_GLOB,\n+\tparse_pathspec(&pathspec, PATHSPEC_GLOB | PATHSPEC_ICASE,\n \t\t       PATHSPEC_PREFER_CWD,\n \t\t       prefix, argv + 1);\n \tfor (i = 0; i < pathspec.nr; i++)\ndiff --git a/cache.h b/cache.h\nindex dc4d2ee..3cff825 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -369,6 +369,7 @@ static inline enum object_type object_type(unsigned int mode)\n #define GIT_LITERAL_PATHSPECS_ENVIRONMENT \"GIT_LITERAL_PATHSPECS\"\n #define GIT_GLOB_PATHSPECS_ENVIRONMENT \"GIT_GLOB_PATHSPECS\"\n #define GIT_NOGLOB_PATHSPECS_ENVIRONMENT \"GIT_NOGLOB_PATHSPECS\"\n+#define GIT_ICASE_PATHSPECS_ENVIRONMENT \"GIT_ICASE_PATHSPECS\"\n \n /*\n  * This environment variable is expected to contain a boolean indicating\ndiff --git a/dir.c b/dir.c\nindex 076bd46..8543736 100644\n--- a/dir.c\n+++ b/dir.c\n@@ -57,7 +57,7 @@ inline int git_fnmatch(const struct pathspec_item *item,\n \t\t       int prefix)\n {\n \tif (prefix > 0) {\n-\t\tif (strncmp(pattern, string, prefix))\n+\t\tif (ps_strncmp(item, pattern, string, prefix))\n \t\t\treturn FNM_NOMATCH;\n \t\tpattern += prefix;\n \t\tstring += prefix;\n@@ -66,14 +66,18 @@ inline int git_fnmatch(const struct pathspec_item *item,\n \t\tint pattern_len = strlen(++pattern);\n \t\tint string_len = strlen(string);\n \t\treturn string_len < pattern_len ||\n-\t\t       strcmp(pattern,\n-\t\t\t      string + string_len - pattern_len);\n+\t\t\tps_strcmp(item, pattern,\n+\t\t\t\t  string + string_len - pattern_len);\n \t}\n \tif (item->magic & PATHSPEC_GLOB)\n-\t\treturn wildmatch(pattern, string, WM_PATHNAME, NULL);\n+\t\treturn wildmatch(pattern, string,\n+\t\t\t\t WM_PATHNAME |\n+\t\t\t\t (item->magic & PATHSPEC_ICASE ? WM_CASEFOLD : 0),\n+\t\t\t\t NULL);\n \telse\n \t\t/* wildmatch has not learned no FNM_PATHNAME mode yet */\n-\t\treturn fnmatch(pattern, string, 0);\n+\t\treturn fnmatch(pattern, string,\n+\t\t\t       item->magic & PATHSPEC_ICASE ? FNM_CASEFOLD : 0);\n }\n \n static int fnmatch_icase_mem(const char *pattern, int patternlen,\n@@ -110,16 +114,27 @@ static size_t common_prefix_len(const struct pathspec *pathspec)\n \tint n;\n \tsize_t max = 0;\n \n+\t/*\n+\t * \":(icase)path\" is treated as a pathspec full of\n+\t * wildcard. In other words, only prefix is considered common\n+\t * prefix. If the pathspec is abc/foo abc/bar, running in\n+\t * subdir xyz, the common prefix is still xyz, not xuz/abc as\n+\t * in non-:(icase).\n+\t */\n \tGUARD_PATHSPEC(pathspec,\n \t\t       PATHSPEC_FROMTOP |\n \t\t       PATHSPEC_MAXDEPTH |\n \t\t       PATHSPEC_LITERAL |\n-\t\t       PATHSPEC_GLOB);\n+\t\t       PATHSPEC_GLOB |\n+\t\t       PATHSPEC_ICASE);\n \n \tfor (n = 0; n < pathspec->nr; n++) {\n-\t\tsize_t i = 0, len = 0;\n-\t\twhile (i < pathspec->items[n].nowildcard_len &&\n-\t\t       (n == 0 || i < max)) {\n+\t\tsize_t i = 0, len = 0, item_len;\n+\t\tif (pathspec->items[n].magic & PATHSPEC_ICASE)\n+\t\t\titem_len = pathspec->items[n].prefix;\n+\t\telse\n+\t\t\titem_len = pathspec->items[n].nowildcard_len;\n+\t\twhile (i < item_len && (n == 0 || i < max)) {\n \t\t\tchar c = pathspec->items[n].match[i];\n \t\t\tif (c != pathspec->items[0].match[i])\n \t\t\t\tbreak;\n@@ -196,11 +211,44 @@ static int match_pathspec_item(const struct pathspec_item *item, int prefix,\n \tconst char *match = item->match + prefix;\n \tint matchlen = item->len - prefix;\n \n+\t/*\n+\t * The normal call pattern is:\n+\t * 1. prefix = common_prefix_len(ps);\n+\t * 2. prune something, or fill_directory\n+\t * 3. match_pathspec_depth()\n+\t *\n+\t * 'prefix' at #1 may be shorter than the command's prefix and\n+\t * it's ok for #2 to match extra files. Those extras will be\n+\t * trimmed at #3.\n+\t *\n+\t * Suppose the pathspec is 'foo' and '../bar' running from\n+\t * subdir 'xyz'. The common prefix at #1 will be empty, thanks\n+\t * to \"../\". We may have xyz/foo _and_ XYZ/foo after #2. The\n+\t * user does not want XYZ/foo, only the \"foo\" part should be\n+\t * case-insensitive. We need to filter out XYZ/foo here. In\n+\t * other words, we do not trust the caller on comparing the\n+\t * prefix part when :(icase) is involved. We do exact\n+\t * comparison ourselves.\n+\t *\n+\t * Normally the caller (common_prefix_len() in fact) does\n+\t * _exact_ matching on name[-prefix+1..-1] and we do not need\n+\t * to check that part. Be defensive and check it anyway, in\n+\t * case common_prefix_len is changed, or a new caller is\n+\t * introduced that does not use common_prefix_len.\n+\t *\n+\t * If the penalty turns out too high when prefix is really\n+\t * long, maybe change it to\n+\t * strncmp(match, name, item->prefix - prefix)\n+\t */\n+\tif (item->prefix && (item->magic & PATHSPEC_ICASE) &&\n+\t    strncmp(item->match, name - prefix, item->prefix))\n+\t\treturn 0;\n+\n \t/* If the match was just the prefix, we matched */\n \tif (!*match)\n \t\treturn MATCHED_RECURSIVELY;\n \n-\tif (matchlen <= namelen && !strncmp(match, name, matchlen)) {\n+\tif (matchlen <= namelen && !ps_strncmp(item, match, name, matchlen)) {\n \t\tif (matchlen == namelen)\n \t\t\treturn MATCHED_EXACTLY;\n \n@@ -241,7 +289,8 @@ int match_pathspec_depth(const struct pathspec *ps,\n \t\t       PATHSPEC_FROMTOP |\n \t\t       PATHSPEC_MAXDEPTH |\n \t\t       PATHSPEC_LITERAL |\n-\t\t       PATHSPEC_GLOB);\n+\t\t       PATHSPEC_GLOB |\n+\t\t       PATHSPEC_ICASE);\n \n \tif (!ps->nr) {\n \t\tif (!ps->recursive ||\n@@ -1301,7 +1350,8 @@ int read_directory(struct dir_struct *dir, const char *path, int len, const stru\n \t\t\t       PATHSPEC_FROMTOP |\n \t\t\t       PATHSPEC_MAXDEPTH |\n \t\t\t       PATHSPEC_LITERAL |\n-\t\t\t       PATHSPEC_GLOB);\n+\t\t\t       PATHSPEC_GLOB |\n+\t\t\t       PATHSPEC_ICASE);\n \n \tif (has_symlink_leading_path(path, len))\n \t\treturn dir->nr;\ndiff --git a/git.c b/git.c\nindex 2509675..cebf882 100644\n--- a/git.c\n+++ b/git.c\n@@ -155,6 +155,10 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)\n \t\t\tsetenv(GIT_NOGLOB_PATHSPECS_ENVIRONMENT, \"1\", 1);\n \t\t\tif (envchanged)\n \t\t\t\t*envchanged = 1;\n+\t\t} else if (!strcmp(cmd, \"--icase-pathspecs\")) {\n+\t\t\tsetenv(GIT_ICASE_PATHSPECS_ENVIRONMENT, \"1\", 1);\n+\t\t\tif (envchanged)\n+\t\t\t\t*envchanged = 1;\n \t\t} else if (!strcmp(cmd, \"--shallow-file\")) {\n \t\t\t(*argv)++;\n \t\t\t(*argc)--;\ndiff --git a/pathspec.c b/pathspec.c\nindex c1e6917..d9f4143 100644\n--- a/pathspec.c\n+++ b/pathspec.c\n@@ -57,7 +57,6 @@ char *find_pathspecs_matching_against_index(const struct pathspec *pathspec)\n  *\n  * Possible future magic semantics include stuff like:\n  *\n- *\t{ PATHSPEC_ICASE, '\\0', \"icase\" },\n  *\t{ PATHSPEC_RECURSIVE, '*', \"recursive\" },\n  *\t{ PATHSPEC_REGEXP, '\\0', \"regexp\" },\n  *\n@@ -71,6 +70,7 @@ static struct pathspec_magic {\n \t{ PATHSPEC_FROMTOP, '/', \"top\" },\n \t{ PATHSPEC_LITERAL,   0, \"literal\" },\n \t{ PATHSPEC_GLOB,   '\\0', \"glob\" },\n+\t{ PATHSPEC_ICASE,  '\\0', \"icase\" },\n };\n \n /*\n@@ -95,6 +95,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item,\n \tstatic int literal_global = -1;\n \tstatic int glob_global = -1;\n \tstatic int noglob_global = -1;\n+\tstatic int icase_global = -1;\n \tunsigned magic = 0, short_magic = 0, global_magic = 0;\n \tconst char *copyfrom = elt, *long_magic_end = NULL;\n \tchar *match;\n@@ -116,6 +117,12 @@ static unsigned prefix_pathspec(struct pathspec_item *item,\n \tif (glob_global && noglob_global)\n \t\tdie(_(\"global 'glob' and 'noglob' pathspec settings are incompatible\"));\n \n+\n+\tif (icase_global < 0)\n+\t\ticase_global = git_env_bool(GIT_ICASE_PATHSPECS_ENVIRONMENT, 0);\n+\tif (icase_global)\n+\t\tglobal_magic |= PATHSPEC_ICASE;\n+\n \tif ((global_magic & PATHSPEC_LITERAL) &&\n \t    (global_magic & ~PATHSPEC_LITERAL))\n \t\tdie(_(\"global 'literal' pathspec setting is incompatible \"\ndiff --git a/pathspec.h b/pathspec.h\nindex cdf2fa3..04b632f 100644\n--- a/pathspec.h\n+++ b/pathspec.h\n@@ -6,11 +6,13 @@\n #define PATHSPEC_MAXDEPTH\t(1<<1)\n #define PATHSPEC_LITERAL\t(1<<2)\n #define PATHSPEC_GLOB\t\t(1<<3)\n+#define PATHSPEC_ICASE\t\t(1<<4)\n #define PATHSPEC_ALL_MAGIC\t  \\\n \t(PATHSPEC_FROMTOP\t| \\\n \t PATHSPEC_MAXDEPTH\t| \\\n \t PATHSPEC_LITERAL\t| \\\n-\t PATHSPEC_GLOB)\n+\t PATHSPEC_GLOB\t\t| \\\n+\t PATHSPEC_ICASE)\n \n #define PATHSPEC_ONESTAR 1\t/* the pathspec pattern sastisfies GFNM_ONESTAR */\n \n@@ -65,6 +67,24 @@ extern void parse_pathspec(struct pathspec *pathspec,\n extern void copy_pathspec(struct pathspec *dst, const struct pathspec *src);\n extern void free_pathspec(struct pathspec *);\n \n+static inline int ps_strncmp(const struct pathspec_item *item,\n+\t\t\t     const char *s1, const char *s2, size_t n)\n+{\n+\tif (item->magic & PATHSPEC_ICASE)\n+\t\treturn strncasecmp(s1, s2, n);\n+\telse\n+\t\treturn strncmp(s1, s2, n);\n+}\n+\n+static inline int ps_strcmp(const struct pathspec_item *item,\n+\t\t\t    const char *s1, const char *s2)\n+{\n+\tif (item->magic & PATHSPEC_ICASE)\n+\t\treturn strcasecmp(s1, s2);\n+\telse\n+\t\treturn strcmp(s1, s2);\n+}\n+\n extern char *find_pathspecs_matching_against_index(const struct pathspec *pathspec);\n extern void add_pathspec_matches_against_index(const struct pathspec *pathspec, char *seen);\n extern const char *check_path_for_gitlink(const char *path);\ndiff --git a/t/t6131-pathspec-icase.sh b/t/t6131-pathspec-icase.sh\nnew file mode 100755\nindex 0000000..3215eef\n--- /dev/null\n+++ b/t/t6131-pathspec-icase.sh\n@@ -0,0 +1,97 @@\n+#!/bin/sh\n+\n+test_description='test case insensitive pathspec limiting'\n+. ./test-lib.sh\n+\n+test_expect_success 'create commits with glob characters' '\n+\ttest_commit bar bar &&\n+\ttest_commit bAr bAr &&\n+\ttest_commit BAR BAR &&\n+\tmkdir foo &&\n+\ttest_commit foo/bar foo/bar &&\n+\ttest_commit foo/bAr foo/bAr &&\n+\ttest_commit foo/BAR foo/BAR &&\n+\tmkdir fOo &&\n+\ttest_commit fOo/bar fOo/bar &&\n+\ttest_commit fOo/bAr fOo/bAr &&\n+\ttest_commit fOo/BAR fOo/BAR &&\n+\tmkdir FOO &&\n+\ttest_commit FOO/bar FOO/bar &&\n+\ttest_commit FOO/bAr FOO/bAr &&\n+\ttest_commit FOO/BAR FOO/BAR\n+'\n+\n+test_expect_success 'tree_entry_interesting matches bar' '\n+\techo bar >expect &&\n+\tgit log --format=%s -- \"bar\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'tree_entry_interesting matches :(icase)bar' '\n+\tcat <<-EOF >expect &&\n+\tBAR\n+\tbAr\n+\tbar\n+\tEOF\n+\tgit log --format=%s -- \":(icase)bar\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'tree_entry_interesting matches :(icase)bar with prefix' '\n+\tcat <<-EOF >expect &&\n+\tfOo/BAR\n+\tfOo/bAr\n+\tfOo/bar\n+\tEOF\n+\t( cd fOo && git log --format=%s -- \":(icase)bar\" ) >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'tree_entry_interesting matches :(icase)bar with empty prefix' '\n+\tcat <<-EOF >expect &&\n+\tFOO/BAR\n+\tFOO/bAr\n+\tFOO/bar\n+\tfOo/BAR\n+\tfOo/bAr\n+\tfOo/bar\n+\tfoo/BAR\n+\tfoo/bAr\n+\tfoo/bar\n+\tEOF\n+\t( cd fOo && git log --format=%s -- \":(icase)../foo/bar\" ) >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'match_pathspec_depth matches :(icase)bar' '\n+\tcat <<-EOF >expect &&\n+\tBAR\n+\tbAr\n+\tbar\n+\tEOF\n+\tgit ls-files \":(icase)bar\" >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'match_pathspec_depth matches :(icase)bar with prefix' '\n+\tcat <<-EOF >expect &&\n+\tfOo/BAR\n+\tfOo/bAr\n+\tfOo/bar\n+\tEOF\n+\t( cd fOo && git ls-files --full-name \":(icase)bar\" ) >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'match_pathspec_depth matches :(icase)bar with empty prefix' '\n+\tcat <<-EOF >expect &&\n+\tbar\n+\tfOo/BAR\n+\tfOo/bAr\n+\tfOo/bar\n+\tEOF\n+\t( cd fOo && git ls-files --full-name \":(icase)bar\" ../bar ) >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_done\ndiff --git a/tree-walk.c b/tree-walk.c\nindex a44f528..c366852 100644\n--- a/tree-walk.c\n+++ b/tree-walk.c\n@@ -489,13 +489,25 @@ int get_tree_entry(const unsigned char *tree_sha1, const char *name, unsigned ch\n \treturn retval;\n }\n \n-static int match_entry(const struct name_entry *entry, int pathlen,\n+static int match_entry(const struct pathspec_item *item,\n+\t\t       const struct name_entry *entry, int pathlen,\n \t\t       const char *match, int matchlen,\n \t\t       enum interesting *never_interesting)\n {\n \tint m = -1; /* signals that we haven't called strncmp() */\n \n-\tif (*never_interesting != entry_not_interesting) {\n+\tif (item->magic & PATHSPEC_ICASE)\n+\t\t/*\n+\t\t * \"Never interesting\" trick requires exact\n+\t\t * matching. We could do something clever with inexact\n+\t\t * matching, but it's trickier (and not to forget that\n+\t\t * strcasecmp is locale-dependent, at least in\n+\t\t * glibc). Just disable it for now. It can't be worse\n+\t\t * than the wildcard's codepath of '[Tt][Hi][Is][Ss]'\n+\t\t * pattern.\n+\t\t */\n+\t\t*never_interesting = entry_not_interesting;\n+\telse if (*never_interesting != entry_not_interesting) {\n \t\t/*\n \t\t * We have not seen any match that sorts later\n \t\t * than the current path.\n@@ -541,7 +553,7 @@ static int match_entry(const struct name_entry *entry, int pathlen,\n \t\t * we cheated and did not do strncmp(), so we do\n \t\t * that here.\n \t\t */\n-\t\tm = strncmp(match, entry->path, pathlen);\n+\t\tm = ps_strncmp(item, match, entry->path, pathlen);\n \n \t/*\n \t * If common part matched earlier then it is a hit,\n@@ -549,15 +561,39 @@ static int match_entry(const struct name_entry *entry, int pathlen,\n \t * leading directory and is shorter than match.\n \t */\n \tif (!m)\n+\t\t/*\n+\t\t * match_entry does not check if the prefix part is\n+\t\t * matched case-sensitively. If the entry is a\n+\t\t * directory and part of prefix, it'll be rematched\n+\t\t * eventually by basecmp with special treatment for\n+\t\t * the prefix.\n+\t\t */\n \t\treturn 1;\n \n \treturn 0;\n }\n \n-static int match_dir_prefix(const char *base,\n+/* :(icase)-aware string compare */\n+static int basecmp(const struct pathspec_item *item,\n+\t\t   const char *base, const char *match, int len)\n+{\n+\tif (item->magic & PATHSPEC_ICASE) {\n+\t\tint ret, n = len > item->prefix ? item->prefix : len;\n+\t\tret = strncmp(base, match, n);\n+\t\tif (ret)\n+\t\t\treturn ret;\n+\t\tbase += n;\n+\t\tmatch += n;\n+\t\tlen -= n;\n+\t}\n+\treturn ps_strncmp(item, base, match, len);\n+}\n+\n+static int match_dir_prefix(const struct pathspec_item *item,\n+\t\t\t    const char *base,\n \t\t\t    const char *match, int matchlen)\n {\n-\tif (strncmp(base, match, matchlen))\n+\tif (basecmp(item, base, match, matchlen))\n \t\treturn 0;\n \n \t/*\n@@ -594,7 +630,7 @@ static int match_wildcard_base(const struct pathspec_item *item,\n \t\t */\n \t\tif (baselen >= matchlen) {\n \t\t\t*matched = matchlen;\n-\t\t\treturn !strncmp(base, match, matchlen);\n+\t\t\treturn !basecmp(item, base, match, matchlen);\n \t\t}\n \n \t\tdirlen = matchlen;\n@@ -607,7 +643,7 @@ static int match_wildcard_base(const struct pathspec_item *item,\n \t\t * base ends with '/' so we are sure it really matches\n \t\t * directory\n \t\t */\n-\t\tif (strncmp(base, match, baselen))\n+\t\tif (basecmp(item, base, match, baselen))\n \t\t\treturn 0;\n \t\t*matched = baselen;\n \t} else\n@@ -640,7 +676,8 @@ enum interesting tree_entry_interesting(const struct name_entry *entry,\n \t\t       PATHSPEC_FROMTOP |\n \t\t       PATHSPEC_MAXDEPTH |\n \t\t       PATHSPEC_LITERAL |\n-\t\t       PATHSPEC_GLOB);\n+\t\t       PATHSPEC_GLOB |\n+\t\t       PATHSPEC_ICASE);\n \n \tif (!ps->nr) {\n \t\tif (!ps->recursive ||\n@@ -663,7 +700,7 @@ enum interesting tree_entry_interesting(const struct name_entry *entry,\n \n \t\tif (baselen >= matchlen) {\n \t\t\t/* If it doesn't match, move along... */\n-\t\t\tif (!match_dir_prefix(base_str, match, matchlen))\n+\t\t\tif (!match_dir_prefix(item, base_str, match, matchlen))\n \t\t\t\tgoto match_wildcards;\n \n \t\t\tif (!ps->recursive ||\n@@ -679,8 +716,8 @@ enum interesting tree_entry_interesting(const struct name_entry *entry,\n \t\t}\n \n \t\t/* Either there must be no base, or the base must match. */\n-\t\tif (baselen == 0 || !strncmp(base_str, match, baselen)) {\n-\t\t\tif (match_entry(entry, pathlen,\n+\t\tif (baselen == 0 || !basecmp(item, base_str, match, baselen)) {\n+\t\t\tif (match_entry(item, entry, pathlen,\n \t\t\t\t\tmatch + baselen, matchlen - baselen,\n \t\t\t\t\t&never_interesting))\n \t\t\t\treturn entry_interesting;\n-- \n1.8.2.83.gc99314b\n"},{"id":"223320","messageId":"CAPig+cQp71KkaXzieCc5JX+hVUJ06ydGy_RDaBMtt6AKRG+SNg@mail.gmail.com","threadId":"34432","inReplyTo":"1373790969-13000-47-git-send-email-pclouds@gmail.com","subject":"Re: [PATCH v2 46/46] parse_pathspec: accept :(icase)path syntax","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2013-07-14T08:48:34Z","receivedAt":"2013-07-14T08:48:34Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Sun, Jul 14, 2013 at 4:36 AM, Nguyễn Thái Ngọc Duy <pclouds@gmail.com> wrote:\n> diff --git a/Documentation/git.txt b/Documentation/git.txt\n> index 3571a1b..546eea4 100644\n> --- a/Documentation/git.txt\n> +++ b/Documentation/git.txt\n> @@ -879,6 +883,10 @@ GIT_NOGLOB_PATHSPECS::\n>         Setting this variable to `1` will cause Git to treat all\n>         pathspecs as literal (aka \"literal\" magic).\n>\n> +GIT_ICASE_PATHSPECS::\n> +       Setting this variable to `1` will cause Git to treat all\n> +       pathspeccs as case-insensitive.\n\ns/pathspeccs/pathspecs/\n\n> @@ -110,16 +114,27 @@ static size_t common_prefix_len(const struct pathspec *pathspec)\n>         int n;\n>         size_t max = 0;\n>\n> +       /*\n> +        * \":(icase)path\" is treated as a pathspec full of\n> +        * wildcard. In other words, only prefix is considered common\n> +        * prefix. If the pathspec is abc/foo abc/bar, running in\n> +        * subdir xyz, the common prefix is still xyz, not xuz/abc as\n\ns/xuz/xyz/\n"},{"id":"223476","messageId":"7v61wbbpzp.fsf@alter.siamese.dyndns.org","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"Re: [PATCH v2 00/46] \"struct pathspec\" conversion and :(glob) and :(icase)","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2013-07-15T19:08:26Z","receivedAt":"2013-07-15T19:08:26Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Thanks for a reroll.  I am slowly reading it through ;-).\n"},{"id":"223484","messageId":"7va9lna48n.fsf@alter.siamese.dyndns.org","threadId":"34432","inReplyTo":"1373790969-13000-1-git-send-email-pclouds@gmail.com","subject":"Re: [PATCH v2 00/46] \"struct pathspec\" conversion and :(glob) and :(icase)","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2013-07-15T21:43:36Z","receivedAt":"2013-07-15T21:43:36Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Nguyễn Thái Ngọc Duy  <pclouds@gmail.com> writes:\n\n> Compared to the last round [1] this series mainly fixes comments and\n> commit messages suggested by Eric and Junio. It also fixes a conflict\n> with cb/log-follow-with-combined (in master) and introduces :(icase)\n> mentioned in the last round.\n>\n> [1] http://thread.gmane.org/gmane.comp.version-control.git/226892\n>\n> Nguyễn Thái Ngọc Duy (46):\n>   clean: remove unused variable \"seen\"\n>   ...\n>   parse_pathspec: accept :(icase)path syntax\n\nThis was a good exercise for git-imerge.  I merged it just below the\n\"2.0 migration topic\" that is kept outside the 'next' first by hand,\nand then used imerge.  Interested parties may try to do this, after\nfetching from me:\n\n\n\t$ git checkout cb5d2fc\n\t$ git branch nd/magic-pathspec 93d93537\n\t$ git imerge start --first-parent --name bogo nd/magic-pathspec\n\n\t... imerge thinks and then will stop, asking you to resolve\n\t... and commit, repeatedly.  Do the next two steps until it\n        ... stops.\n\n\t$ edit\n        $ git commit -a --no-edit && git imerge continue\n\n\t... and then\n        $ git imerge finish\n\nThe imerge program was a bit under-help (yet), and also it was a bit\ncumbersome at times having to resolve trivial and obvious two\nconflicts in the same file (e.g. the series adds description for\nGIT_*_PATHSPECS environment variables to Documentation/git.txt in\ntwo separate patches, and the resolution is obvious for a single\nmerge, but imerge wants me to resolve them in two steps), but in the\nend, after \"imerge finish\", it produced identical result as my hand\nmerge, so at least it seems to be doing something right ;-)\n\nA few things I noticed:\n\n - It was not immediately clear from \"git imerge -h\" text what the\n   simplest \"hello world\" workflow was.\n\n - It was not clear what it wanted for --name parameter (it becomes\n   the name of the temporary and final branch that stores the\n   result).\n\n - The final step \"imerge finish\" gave me this ugliness:\n\n     Merge commit 93d9353... into commit cb5d2fc7\n\n   Perhaps you can at least use the initial branch name\n   \"nd/magic-pathspec\" I gave you, and use \"git fmt-merge-msg\"?\n\nA bigger test will be to try merging jn/add-2.0-u-A-sans-pathspec\nand jc/add-2.0-ignore-removal topics on top of the merge result\n(2857023 in today's pushout).  These two merges, especially the\nlatter one, have to involve some \"evil merges\", so it would be\ninteresting to see how well it fares.\n\nIn any case, thanks for a fun tool.\n"},{"id":"225344","messageId":"520E322F.4040509@alum.mit.edu","threadId":"34432","inReplyTo":"7va9lna48n.fsf@alter.siamese.dyndns.org","subject":"\"git fmt-merge-msg\" usage (was [PATCH v2 00/46] \"struct pathspec\" conversion and :(glob) and :(icase))","fromName":"Michael Haggerty","fromEmail":"mhagger@alum.mit.edu","sentAt":"2013-08-16T14:07:43Z","receivedAt":"2013-08-16T14:07:43Z","isPatch":true,"sender":{"key":"mhagger@alum.mit.edu","avatar":"https://avatars.githubusercontent.com/u/119718?v=4"},"body":"On 07/15/2013 11:43 PM, Junio C Hamano wrote:\n> [...]\n> This was a good exercise for git-imerge.[...]\n> \n> A few things I noticed:\n>  [...]\n> \n>  - The final step \"imerge finish\" gave me this ugliness:\n> \n>      Merge commit 93d9353... into commit cb5d2fc7\n> \n>    Perhaps you can at least use the initial branch name\n>    \"nd/magic-pathspec\" I gave you, and use \"git fmt-merge-msg\"?\n\nI tried to implement this but it is not obvious from the documentation\n(to say the least) how to use \"git fmt-merge-msg\".  It appears that this\nprogram takes, on standard input, something like\n\n    <sha1> TAB TAB <text1> LF\n    <sha1> TAB TAB <text2> LF\n    <sha1> TAB TAB <text3> LF\n    ...\n\n(the two TABs are required!).  Then the <text> is somehow stuck in the\nfirst line of the merge log message, like\n\n    Merge <text1>; <text3>\n\nexcept apparently the second line is omitted.  If somebody would explain\nto me the point of this and how it is intended to be used, then I\nvolunteer to improve the documentation.\n\nBut a bit of the magic of these merge messages is how the <text> are\ngenerated in the first place; e.g.,\n\n    refs/heads/foo -> \"branch 'foo'\"\n    refs/remotes/bar/baz -> \"remote-tracking branch 'bar/baz'\"\n\nIs this magic available via any Git commands, or do I have to replicate it?\n\nI'm sorely tempted to have git-imerge do a throw-away \"git merge\" of the\ntwo branch tips just to have Git generate a full merge commit message\nfor me (it might even be nice to include the list of the files that\nwould have conflicted).\n\nThanks,\nMichael\n\n-- \nMichael Haggerty\nmhagger@alum.mit.edu\nhttp://softwareswirl.blogspot.com/\n"},{"id":"225400","messageId":"7vbo4uokid.fsf@alter.siamese.dyndns.org","threadId":"34432","inReplyTo":"520E322F.4040509@alum.mit.edu","subject":"Re: \"git fmt-merge-msg\" usage","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2013-08-18T19:40:26Z","receivedAt":"2013-08-18T19:40:26Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Michael Haggerty <mhagger@alum.mit.edu> writes:\n\n> On 07/15/2013 11:43 PM, Junio C Hamano wrote:\n>> [...]\n>> This was a good exercise for git-imerge.[...]\n>> \n>> A few things I noticed:\n>>  [...]\n>> \n>>  - The final step \"imerge finish\" gave me this ugliness:\n>> \n>>      Merge commit 93d9353... into commit cb5d2fc7\n>> \n>>    Perhaps you can at least use the initial branch name\n>>    \"nd/magic-pathspec\" I gave you, and use \"git fmt-merge-msg\"?\n>\n> I tried to implement this but it is not obvious from the documentation\n> (to say the least) how to use \"git fmt-merge-msg\".  It appears that this\n> program takes, on standard input, something like\n>\n>     <sha1> TAB TAB <text1> LF\n>     <sha1> TAB TAB <text2> LF\n>     <sha1> TAB TAB <text3> LF\n>     ...\n>\n> (the two TABs are required!).\n\nCorrect; fmt-merge-msg is designed to read FETCH_HEAD that can have\n'not-for-merge' marker between these two HTs.  <text$N> are also\nexpected to be in a specific format to explain where the object\nbeing merged described by the line came from.\n\n> But a bit of the magic of these merge messages is how the <text> are\n> generated in the first place; e.g.,\n>\n>     refs/heads/foo -> \"branch 'foo'\"\n>     refs/remotes/bar/baz -> \"remote-tracking branch 'bar/baz'\"\n>\n> Is this magic available via any Git commands, or do I have to replicate it?\n\nThis is all internal to \"fetch\" and \"git merge\", which are the only\nthings that need to know the specifics.  store_updated_refs() is\nwhere entries in FETCH_HEAD are written.\n"}]}