2013-01-06 20:58:08 +04:00
|
|
|
#include "cache.h"
|
|
|
|
#include "dir.h"
|
|
|
|
#include "pathspec.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Finds which of the given pathspecs match items in the index.
|
|
|
|
*
|
|
|
|
* For each pathspec, sets the corresponding entry in the seen[] array
|
|
|
|
* (which should be specs items long, i.e. the same size as pathspec)
|
|
|
|
* to the nature of the "closest" (i.e. most specific) match found for
|
|
|
|
* that pathspec in the index, if it was a closer type of match than
|
|
|
|
* the existing entry. As an optimization, matching is skipped
|
|
|
|
* altogether if seen[] already only contains non-zero entries.
|
|
|
|
*
|
|
|
|
* If seen[] has not already been written to, it may make sense
|
2013-01-06 20:58:09 +04:00
|
|
|
* to use find_pathspecs_matching_against_index() instead.
|
2013-01-06 20:58:08 +04:00
|
|
|
*/
|
2013-07-14 12:36:00 +04:00
|
|
|
void add_pathspec_matches_against_index(const struct pathspec *pathspec,
|
|
|
|
char *seen)
|
2013-01-06 20:58:08 +04:00
|
|
|
{
|
|
|
|
int num_unmatched = 0, i;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Since we are walking the index as if we were walking the directory,
|
|
|
|
* we have to mark the matched pathspec as seen; otherwise we will
|
|
|
|
* mistakenly think that the user gave a pathspec that did not match
|
|
|
|
* anything.
|
|
|
|
*/
|
2013-07-14 12:36:00 +04:00
|
|
|
for (i = 0; i < pathspec->nr; i++)
|
2013-01-06 20:58:08 +04:00
|
|
|
if (!seen[i])
|
|
|
|
num_unmatched++;
|
|
|
|
if (!num_unmatched)
|
|
|
|
return;
|
|
|
|
for (i = 0; i < active_nr; i++) {
|
Convert "struct cache_entry *" to "const ..." wherever possible
I attempted to make index_state->cache[] a "const struct cache_entry **"
to find out how existing entries in index are modified and where. The
question I have is what do we do if we really need to keep track of on-disk
changes in the index. The result is
- diff-lib.c: setting CE_UPTODATE
- name-hash.c: setting CE_HASHED
- preload-index.c, read-cache.c, unpack-trees.c and
builtin/update-index: obvious
- entry.c: write_entry() may refresh the checked out entry via
fill_stat_cache_info(). This causes "non-const struct cache_entry
*" in builtin/apply.c, builtin/checkout-index.c and
builtin/checkout.c
- builtin/ls-files.c: --with-tree changes stagemask and may set
CE_UPDATE
Of these, write_entry() and its call sites are probably most
interesting because it modifies on-disk info. But this is stat info
and can be retrieved via refresh, at least for porcelain
commands. Other just uses ce_flags for local purposes.
So, keeping track of "dirty" entries is just a matter of setting a
flag in index modification functions exposed by read-cache.c. Except
unpack-trees, the rest of the code base does not do anything funny
behind read-cache's back.
The actual patch is less valueable than the summary above. But if
anyone wants to re-identify the above sites. Applying this patch, then
this:
diff --git a/cache.h b/cache.h
index 430d021..1692891 100644
--- a/cache.h
+++ b/cache.h
@@ -267,7 +267,7 @@ static inline unsigned int canon_mode(unsigned int mode)
#define cache_entry_size(len) (offsetof(struct cache_entry,name) + (len) + 1)
struct index_state {
- struct cache_entry **cache;
+ const struct cache_entry **cache;
unsigned int version;
unsigned int cache_nr, cache_alloc, cache_changed;
struct string_list *resolve_undo;
will help quickly identify them without bogus warnings.
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2013-07-09 19:29:00 +04:00
|
|
|
const struct cache_entry *ce = active_cache[i];
|
2014-01-24 17:40:28 +04:00
|
|
|
ce_path_match(ce, pathspec, seen);
|
2013-01-06 20:58:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Finds which of the given pathspecs match items in the index.
|
|
|
|
*
|
2013-01-06 20:58:09 +04:00
|
|
|
* This is a one-shot wrapper around add_pathspec_matches_against_index()
|
|
|
|
* which allocates, populates, and returns a seen[] array indicating the
|
|
|
|
* nature of the "closest" (i.e. most specific) matches which each of the
|
|
|
|
* given pathspecs achieves against all items in the index.
|
2013-01-06 20:58:08 +04:00
|
|
|
*/
|
2013-07-14 12:36:00 +04:00
|
|
|
char *find_pathspecs_matching_against_index(const struct pathspec *pathspec)
|
2013-01-06 20:58:08 +04:00
|
|
|
{
|
2013-07-14 12:36:00 +04:00
|
|
|
char *seen = xcalloc(pathspec->nr, 1);
|
|
|
|
add_pathspec_matches_against_index(pathspec, seen);
|
2013-01-06 20:58:08 +04:00
|
|
|
return seen;
|
|
|
|
}
|
2013-01-06 20:58:10 +04:00
|
|
|
|
|
|
|
/*
|
2013-07-14 12:35:25 +04:00
|
|
|
* Magic pathspec
|
|
|
|
*
|
|
|
|
* Possible future magic semantics include stuff like:
|
|
|
|
*
|
|
|
|
* { PATHSPEC_RECURSIVE, '*', "recursive" },
|
|
|
|
* { PATHSPEC_REGEXP, '\0', "regexp" },
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
static struct pathspec_magic {
|
|
|
|
unsigned bit;
|
|
|
|
char mnemonic; /* this cannot be ':'! */
|
|
|
|
const char *name;
|
|
|
|
} pathspec_magic[] = {
|
|
|
|
{ PATHSPEC_FROMTOP, '/', "top" },
|
2013-07-14 12:36:06 +04:00
|
|
|
{ PATHSPEC_LITERAL, 0, "literal" },
|
2013-07-14 12:36:08 +04:00
|
|
|
{ PATHSPEC_GLOB, '\0', "glob" },
|
2013-07-14 12:36:09 +04:00
|
|
|
{ PATHSPEC_ICASE, '\0', "icase" },
|
2013-12-06 11:30:48 +04:00
|
|
|
{ PATHSPEC_EXCLUDE, '!', "exclude" },
|
2013-07-14 12:35:25 +04:00
|
|
|
};
|
|
|
|
|
2017-01-04 21:04:04 +03:00
|
|
|
static void prefix_magic(struct strbuf *sb, int prefixlen, unsigned magic)
|
pathspec.c: support adding prefix magic to a pathspec with mnemonic magic
Back in 233c3e6 (parse_pathspec: preserve prefix length via
PATHSPEC_PREFIX_ORIGIN - 2013-07-14), parse_pathspec() is taught to
save prefix length as a dynamic magic. This is needed when the
pathspec is passed to another process and and prefix lenght would be
lost.
Back then we support two cases. If the pathspec is normal, e.g. "abc",
we simply add the prefix to become ":(prefix:2)abc". If the pathspec
contains long magic, e.g. ":(foo,bar)abc" then we turn it to
":(foo,bar,prefix:2)abc". We do not support prefixing on short form,
because the only supported mnemonic '/' disappears after the the
preprocessing steps.
With the introduction of exclude magic with mnemonic '!', we need to
add support for the short form case so that ':!abc' becomes
':(exclude,prefix:2)abc'. Without this, it will break
cd Documentation
git add -p -- . ':!technical'
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2013-12-06 11:30:49 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
strbuf_addstr(sb, ":(");
|
|
|
|
for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)
|
2017-01-04 21:04:04 +03:00
|
|
|
if (magic & pathspec_magic[i].bit) {
|
pathspec.c: support adding prefix magic to a pathspec with mnemonic magic
Back in 233c3e6 (parse_pathspec: preserve prefix length via
PATHSPEC_PREFIX_ORIGIN - 2013-07-14), parse_pathspec() is taught to
save prefix length as a dynamic magic. This is needed when the
pathspec is passed to another process and and prefix lenght would be
lost.
Back then we support two cases. If the pathspec is normal, e.g. "abc",
we simply add the prefix to become ":(prefix:2)abc". If the pathspec
contains long magic, e.g. ":(foo,bar)abc" then we turn it to
":(foo,bar,prefix:2)abc". We do not support prefixing on short form,
because the only supported mnemonic '/' disappears after the the
preprocessing steps.
With the introduction of exclude magic with mnemonic '!', we need to
add support for the short form case so that ':!abc' becomes
':(exclude,prefix:2)abc'. Without this, it will break
cd Documentation
git add -p -- . ':!technical'
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2013-12-06 11:30:49 +04:00
|
|
|
if (sb->buf[sb->len - 1] != '(')
|
|
|
|
strbuf_addch(sb, ',');
|
|
|
|
strbuf_addstr(sb, pathspec_magic[i].name);
|
|
|
|
}
|
|
|
|
strbuf_addf(sb, ",prefix:%d)", prefixlen);
|
|
|
|
}
|
|
|
|
|
2017-01-04 21:04:05 +03:00
|
|
|
static inline int get_literal_global(void)
|
|
|
|
{
|
|
|
|
static int literal = -1;
|
|
|
|
|
|
|
|
if (literal < 0)
|
|
|
|
literal = git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT, 0);
|
|
|
|
|
|
|
|
return literal;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int get_glob_global(void)
|
|
|
|
{
|
|
|
|
static int glob = -1;
|
|
|
|
|
|
|
|
if (glob < 0)
|
|
|
|
glob = git_env_bool(GIT_GLOB_PATHSPECS_ENVIRONMENT, 0);
|
|
|
|
|
|
|
|
return glob;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int get_noglob_global(void)
|
|
|
|
{
|
|
|
|
static int noglob = -1;
|
|
|
|
|
|
|
|
if (noglob < 0)
|
|
|
|
noglob = git_env_bool(GIT_NOGLOB_PATHSPECS_ENVIRONMENT, 0);
|
|
|
|
|
|
|
|
return noglob;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int get_icase_global(void)
|
|
|
|
{
|
|
|
|
static int icase = -1;
|
|
|
|
|
|
|
|
if (icase < 0)
|
|
|
|
icase = git_env_bool(GIT_ICASE_PATHSPECS_ENVIRONMENT, 0);
|
|
|
|
|
|
|
|
return icase;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int get_global_magic(int element_magic)
|
|
|
|
{
|
|
|
|
int global_magic = 0;
|
|
|
|
|
|
|
|
if (get_literal_global())
|
|
|
|
global_magic |= PATHSPEC_LITERAL;
|
|
|
|
|
|
|
|
/* --glob-pathspec is overridden by :(literal) */
|
|
|
|
if (get_glob_global() && !(element_magic & PATHSPEC_LITERAL))
|
|
|
|
global_magic |= PATHSPEC_GLOB;
|
|
|
|
|
|
|
|
if (get_glob_global() && get_noglob_global())
|
|
|
|
die(_("global 'glob' and 'noglob' pathspec settings are incompatible"));
|
|
|
|
|
|
|
|
if (get_icase_global())
|
|
|
|
global_magic |= PATHSPEC_ICASE;
|
|
|
|
|
|
|
|
if ((global_magic & PATHSPEC_LITERAL) &&
|
|
|
|
(global_magic & ~PATHSPEC_LITERAL))
|
|
|
|
die(_("global 'literal' pathspec setting is incompatible "
|
|
|
|
"with all other global pathspec settings"));
|
|
|
|
|
|
|
|
/* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */
|
|
|
|
if (get_noglob_global() && !(element_magic & PATHSPEC_GLOB))
|
|
|
|
global_magic |= PATHSPEC_LITERAL;
|
|
|
|
|
|
|
|
return global_magic;
|
|
|
|
}
|
|
|
|
|
2013-07-14 12:35:25 +04:00
|
|
|
/*
|
|
|
|
* Take an element of a pathspec and check for magic signatures.
|
2013-07-14 12:35:28 +04:00
|
|
|
* Append the result to the prefix. Return the magic bitmap.
|
2013-07-14 12:35:25 +04:00
|
|
|
*
|
|
|
|
* For now, we only parse the syntax and throw out anything other than
|
|
|
|
* "top" magic.
|
|
|
|
*
|
|
|
|
* NEEDSWORK: This needs to be rewritten when we start migrating
|
|
|
|
* get_pathspec() users to use the "struct pathspec" interface. For
|
|
|
|
* example, a pathspec element may be marked as case-insensitive, but
|
|
|
|
* the prefix part must always match literally, and a single stupid
|
|
|
|
* string cannot express such a case.
|
2013-01-06 20:58:10 +04:00
|
|
|
*/
|
2017-01-04 21:04:03 +03:00
|
|
|
static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,
|
2013-07-14 12:35:28 +04:00
|
|
|
const char *prefix, int prefixlen,
|
|
|
|
const char *elt)
|
2013-01-06 20:58:10 +04:00
|
|
|
{
|
2017-01-04 21:04:05 +03:00
|
|
|
unsigned magic = 0, element_magic = 0;
|
2017-01-04 21:04:04 +03:00
|
|
|
const char *copyfrom = elt;
|
2013-07-14 12:35:28 +04:00
|
|
|
char *match;
|
2013-07-14 12:36:04 +04:00
|
|
|
int i, pathspec_prefix = -1;
|
2013-07-14 12:35:25 +04:00
|
|
|
|
2017-01-04 21:04:05 +03:00
|
|
|
if (elt[0] != ':' || get_literal_global() ||
|
2013-10-26 06:09:20 +04:00
|
|
|
(flags & PATHSPEC_LITERAL_PATH)) {
|
2013-07-14 12:35:25 +04:00
|
|
|
; /* nothing to do */
|
|
|
|
} else if (elt[1] == '(') {
|
|
|
|
/* longhand */
|
|
|
|
const char *nextat;
|
|
|
|
for (copyfrom = elt + 2;
|
|
|
|
*copyfrom && *copyfrom != ')';
|
|
|
|
copyfrom = nextat) {
|
|
|
|
size_t len = strcspn(copyfrom, ",)");
|
|
|
|
if (copyfrom[len] == ',')
|
|
|
|
nextat = copyfrom + len + 1;
|
|
|
|
else
|
|
|
|
/* handle ')' and '\0' */
|
|
|
|
nextat = copyfrom + len;
|
|
|
|
if (!len)
|
2013-01-06 20:58:10 +04:00
|
|
|
continue;
|
2013-07-14 12:36:04 +04:00
|
|
|
for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
|
2013-07-14 12:35:25 +04:00
|
|
|
if (strlen(pathspec_magic[i].name) == len &&
|
|
|
|
!strncmp(pathspec_magic[i].name, copyfrom, len)) {
|
2017-01-04 21:04:04 +03:00
|
|
|
element_magic |= pathspec_magic[i].bit;
|
2013-07-14 12:35:25 +04:00
|
|
|
break;
|
|
|
|
}
|
2013-12-01 00:55:40 +04:00
|
|
|
if (starts_with(copyfrom, "prefix:")) {
|
2013-07-14 12:36:04 +04:00
|
|
|
char *endptr;
|
|
|
|
pathspec_prefix = strtol(copyfrom + 7,
|
|
|
|
&endptr, 10);
|
|
|
|
if (endptr - copyfrom != len)
|
|
|
|
die(_("invalid parameter for pathspec magic 'prefix'"));
|
|
|
|
/* "i" would be wrong, but it does not matter */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-07-14 12:35:25 +04:00
|
|
|
if (ARRAY_SIZE(pathspec_magic) <= i)
|
2013-07-14 12:35:26 +04:00
|
|
|
die(_("Invalid pathspec magic '%.*s' in '%s'"),
|
2013-07-14 12:35:25 +04:00
|
|
|
(int) len, copyfrom, elt);
|
|
|
|
}
|
|
|
|
if (*copyfrom != ')')
|
2013-07-14 12:35:26 +04:00
|
|
|
die(_("Missing ')' at the end of pathspec magic in '%s'"), elt);
|
2013-07-14 12:35:25 +04:00
|
|
|
copyfrom++;
|
|
|
|
} else {
|
|
|
|
/* shorthand */
|
|
|
|
for (copyfrom = elt + 1;
|
|
|
|
*copyfrom && *copyfrom != ':';
|
|
|
|
copyfrom++) {
|
|
|
|
char ch = *copyfrom;
|
|
|
|
|
|
|
|
if (!is_pathspec_magic(ch))
|
|
|
|
break;
|
|
|
|
for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)
|
|
|
|
if (pathspec_magic[i].mnemonic == ch) {
|
2017-01-04 21:04:04 +03:00
|
|
|
element_magic |= pathspec_magic[i].bit;
|
2013-07-14 12:35:25 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ARRAY_SIZE(pathspec_magic) <= i)
|
2013-07-14 12:35:26 +04:00
|
|
|
die(_("Unimplemented pathspec magic '%c' in '%s'"),
|
2013-07-14 12:35:25 +04:00
|
|
|
ch, elt);
|
|
|
|
}
|
|
|
|
if (*copyfrom == ':')
|
|
|
|
copyfrom++;
|
|
|
|
}
|
|
|
|
|
2017-01-04 21:04:04 +03:00
|
|
|
magic |= element_magic;
|
2013-07-14 12:36:08 +04:00
|
|
|
|
2017-01-04 21:04:05 +03:00
|
|
|
/* PATHSPEC_LITERAL_PATH ignores magic */
|
|
|
|
if (flags & PATHSPEC_LITERAL_PATH)
|
|
|
|
magic = PATHSPEC_LITERAL;
|
|
|
|
else
|
|
|
|
magic |= get_global_magic(element_magic);
|
2013-07-14 12:35:28 +04:00
|
|
|
|
2013-07-14 12:36:04 +04:00
|
|
|
if (pathspec_prefix >= 0 &&
|
|
|
|
(prefixlen || (prefix && *prefix)))
|
|
|
|
die("BUG: 'prefix' magic is supposed to be used at worktree's root");
|
|
|
|
|
2013-07-14 12:36:08 +04:00
|
|
|
if ((magic & PATHSPEC_LITERAL) && (magic & PATHSPEC_GLOB))
|
|
|
|
die(_("%s: 'literal' and 'glob' are incompatible"), elt);
|
|
|
|
|
2013-07-14 12:36:04 +04:00
|
|
|
if (pathspec_prefix >= 0) {
|
|
|
|
match = xstrdup(copyfrom);
|
|
|
|
prefixlen = pathspec_prefix;
|
|
|
|
} else if (magic & PATHSPEC_FROMTOP) {
|
2013-07-14 12:35:28 +04:00
|
|
|
match = xstrdup(copyfrom);
|
2013-07-14 12:36:03 +04:00
|
|
|
prefixlen = 0;
|
|
|
|
} else {
|
|
|
|
match = prefix_path_gently(prefix, prefixlen, &prefixlen, copyfrom);
|
|
|
|
if (!match)
|
|
|
|
die(_("%s: '%s' is outside repository"), elt, copyfrom);
|
|
|
|
}
|
2017-01-04 21:04:00 +03:00
|
|
|
item->match = match;
|
2013-07-14 12:35:35 +04:00
|
|
|
/*
|
|
|
|
* Prefix the pathspec (keep all magic) and assign to
|
|
|
|
* original. Useful for passing to another command.
|
|
|
|
*/
|
2017-01-04 21:04:04 +03:00
|
|
|
if ((flags & PATHSPEC_PREFIX_ORIGIN) &&
|
2017-01-04 21:04:05 +03:00
|
|
|
prefixlen && !get_literal_global()) {
|
2013-07-14 12:35:35 +04:00
|
|
|
struct strbuf sb = STRBUF_INIT;
|
2017-01-04 21:04:04 +03:00
|
|
|
|
|
|
|
/* Preserve the actual prefix length of each pattern */
|
|
|
|
prefix_magic(&sb, prefixlen, element_magic);
|
|
|
|
|
2013-07-14 12:35:35 +04:00
|
|
|
strbuf_addstr(&sb, match);
|
|
|
|
item->original = strbuf_detach(&sb, NULL);
|
2017-01-04 21:04:01 +03:00
|
|
|
} else {
|
|
|
|
item->original = xstrdup(elt);
|
|
|
|
}
|
2013-07-14 12:35:28 +04:00
|
|
|
item->len = strlen(item->match);
|
2013-07-14 12:36:03 +04:00
|
|
|
item->prefix = prefixlen;
|
2013-07-14 12:35:33 +04:00
|
|
|
|
|
|
|
if ((flags & PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP) &&
|
|
|
|
(item->len >= 1 && item->match[item->len - 1] == '/') &&
|
|
|
|
(i = cache_name_pos(item->match, item->len - 1)) >= 0 &&
|
|
|
|
S_ISGITLINK(active_cache[i]->ce_mode)) {
|
|
|
|
item->len--;
|
|
|
|
match[item->len] = '\0';
|
|
|
|
}
|
|
|
|
|
2013-07-14 12:35:34 +04:00
|
|
|
if (flags & PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE)
|
|
|
|
for (i = 0; i < active_nr; i++) {
|
|
|
|
struct cache_entry *ce = active_cache[i];
|
|
|
|
int ce_len = ce_namelen(ce);
|
|
|
|
|
|
|
|
if (!S_ISGITLINK(ce->ce_mode))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (item->len <= ce_len || match[ce_len] != '/' ||
|
|
|
|
memcmp(ce->name, match, ce_len))
|
|
|
|
continue;
|
|
|
|
if (item->len == ce_len + 1) {
|
|
|
|
/* strip trailing slash */
|
|
|
|
item->len--;
|
|
|
|
match[item->len] = '\0';
|
|
|
|
} else
|
|
|
|
die (_("Pathspec '%s' is in submodule '%.*s'"),
|
|
|
|
elt, ce_len, ce->name);
|
|
|
|
}
|
|
|
|
|
2013-07-14 12:36:06 +04:00
|
|
|
if (magic & PATHSPEC_LITERAL)
|
2013-07-14 12:35:28 +04:00
|
|
|
item->nowildcard_len = item->len;
|
2013-07-14 12:36:03 +04:00
|
|
|
else {
|
2013-07-14 12:35:28 +04:00
|
|
|
item->nowildcard_len = simple_length(item->match);
|
2013-07-14 12:36:03 +04:00
|
|
|
if (item->nowildcard_len < prefixlen)
|
|
|
|
item->nowildcard_len = prefixlen;
|
|
|
|
}
|
2013-07-14 12:35:28 +04:00
|
|
|
item->flags = 0;
|
2013-07-14 12:36:08 +04:00
|
|
|
if (magic & PATHSPEC_GLOB) {
|
|
|
|
/*
|
|
|
|
* FIXME: should we enable ONESTAR in _GLOB for
|
|
|
|
* pattern "* * / * . c"?
|
|
|
|
*/
|
|
|
|
} else {
|
|
|
|
if (item->nowildcard_len < item->len &&
|
|
|
|
item->match[item->nowildcard_len] == '*' &&
|
|
|
|
no_wildcard(item->match + item->nowildcard_len + 1))
|
|
|
|
item->flags |= PATHSPEC_ONESTAR;
|
2013-01-06 20:58:10 +04:00
|
|
|
}
|
2013-07-14 12:36:03 +04:00
|
|
|
|
|
|
|
/* sanity checks, pathspec matchers assume these are sane */
|
|
|
|
assert(item->nowildcard_len <= item->len &&
|
|
|
|
item->prefix <= item->len);
|
2013-07-14 12:35:28 +04:00
|
|
|
return magic;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pathspec_item_cmp(const void *a_, const void *b_)
|
|
|
|
{
|
|
|
|
struct pathspec_item *a, *b;
|
|
|
|
|
|
|
|
a = (struct pathspec_item *)a_;
|
|
|
|
b = (struct pathspec_item *)b_;
|
|
|
|
return strcmp(a->match, b->match);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void NORETURN unsupported_magic(const char *pattern,
|
2017-01-04 21:04:03 +03:00
|
|
|
unsigned magic)
|
2013-07-14 12:35:28 +04:00
|
|
|
{
|
|
|
|
struct strbuf sb = STRBUF_INIT;
|
2017-01-04 21:04:02 +03:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
|
2013-07-14 12:35:28 +04:00
|
|
|
const struct pathspec_magic *m = pathspec_magic + i;
|
|
|
|
if (!(magic & m->bit))
|
|
|
|
continue;
|
|
|
|
if (sb.len)
|
2017-01-04 21:04:03 +03:00
|
|
|
strbuf_addstr(&sb, ", ");
|
|
|
|
|
|
|
|
if (m->mnemonic)
|
|
|
|
strbuf_addf(&sb, _("'%s' (mnemonic: '%c')"),
|
|
|
|
m->name, m->mnemonic);
|
2013-07-14 12:35:28 +04:00
|
|
|
else
|
|
|
|
strbuf_addf(&sb, "'%s'", m->name);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* We may want to substitute "this command" with a command
|
|
|
|
* name. E.g. when add--interactive dies when running
|
|
|
|
* "checkout -p"
|
|
|
|
*/
|
|
|
|
die(_("%s: pathspec magic not supported by this command: %s"),
|
|
|
|
pattern, sb.buf);
|
2013-01-06 20:58:10 +04:00
|
|
|
}
|
2013-01-06 20:58:11 +04:00
|
|
|
|
|
|
|
/*
|
2013-07-14 12:35:28 +04:00
|
|
|
* Given command line arguments and a prefix, convert the input to
|
|
|
|
* pathspec. die() if any magic in magic_mask is used.
|
2013-01-06 20:58:11 +04:00
|
|
|
*/
|
2013-07-14 12:35:28 +04:00
|
|
|
void parse_pathspec(struct pathspec *pathspec,
|
|
|
|
unsigned magic_mask, unsigned flags,
|
|
|
|
const char *prefix, const char **argv)
|
2013-01-06 20:58:11 +04:00
|
|
|
{
|
2013-07-14 12:35:28 +04:00
|
|
|
struct pathspec_item *item;
|
|
|
|
const char *entry = argv ? *argv : NULL;
|
2016-06-23 02:00:24 +03:00
|
|
|
int i, n, prefixlen, warn_empty_string, nr_exclude = 0;
|
2013-07-14 12:35:28 +04:00
|
|
|
|
|
|
|
memset(pathspec, 0, sizeof(*pathspec));
|
|
|
|
|
2013-07-14 12:35:32 +04:00
|
|
|
if (flags & PATHSPEC_MAXDEPTH_VALID)
|
|
|
|
pathspec->magic |= PATHSPEC_MAXDEPTH;
|
|
|
|
|
2013-07-14 12:35:28 +04:00
|
|
|
/* No arguments, no prefix -> no pathspec */
|
|
|
|
if (!entry && !prefix)
|
|
|
|
return;
|
|
|
|
|
2013-07-14 12:35:30 +04:00
|
|
|
if ((flags & PATHSPEC_PREFER_CWD) &&
|
|
|
|
(flags & PATHSPEC_PREFER_FULL))
|
|
|
|
die("BUG: PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible");
|
|
|
|
|
2013-07-14 12:35:28 +04:00
|
|
|
/* No arguments with prefix -> prefix pathspec */
|
|
|
|
if (!entry) {
|
2013-07-14 12:35:30 +04:00
|
|
|
if (flags & PATHSPEC_PREFER_FULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!(flags & PATHSPEC_PREFER_CWD))
|
|
|
|
die("BUG: PATHSPEC_PREFER_CWD requires arguments");
|
|
|
|
|
2014-07-19 17:56:26 +04:00
|
|
|
pathspec->items = item = xcalloc(1, sizeof(*item));
|
2017-01-04 21:04:01 +03:00
|
|
|
item->match = xstrdup(prefix);
|
|
|
|
item->original = xstrdup(prefix);
|
2013-07-14 12:35:28 +04:00
|
|
|
item->nowildcard_len = item->len = strlen(prefix);
|
2013-07-14 12:36:03 +04:00
|
|
|
item->prefix = item->len;
|
2013-07-14 12:35:28 +04:00
|
|
|
pathspec->nr = 1;
|
|
|
|
return;
|
2013-01-06 20:58:11 +04:00
|
|
|
}
|
2013-07-14 12:35:28 +04:00
|
|
|
|
|
|
|
n = 0;
|
2016-06-23 02:00:24 +03:00
|
|
|
warn_empty_string = 1;
|
|
|
|
while (argv[n]) {
|
|
|
|
if (*argv[n] == '\0' && warn_empty_string) {
|
|
|
|
warning(_("empty strings as pathspecs will be made invalid in upcoming releases. "
|
|
|
|
"please use . instead if you meant to match all paths"));
|
|
|
|
warn_empty_string = 0;
|
|
|
|
}
|
2013-07-14 12:35:28 +04:00
|
|
|
n++;
|
2016-06-23 02:00:24 +03:00
|
|
|
}
|
2013-07-14 12:35:28 +04:00
|
|
|
|
|
|
|
pathspec->nr = n;
|
2016-02-23 01:44:25 +03:00
|
|
|
ALLOC_ARRAY(pathspec->items, n);
|
|
|
|
item = pathspec->items;
|
2013-07-14 12:35:28 +04:00
|
|
|
prefixlen = prefix ? strlen(prefix) : 0;
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
entry = argv[i];
|
|
|
|
|
2017-01-04 21:04:03 +03:00
|
|
|
item[i].magic = prefix_pathspec(item + i, flags,
|
2013-07-14 12:35:28 +04:00
|
|
|
prefix, prefixlen, entry);
|
2017-01-04 21:04:05 +03:00
|
|
|
|
2013-12-06 11:30:48 +04:00
|
|
|
if (item[i].magic & PATHSPEC_EXCLUDE)
|
|
|
|
nr_exclude++;
|
2013-07-14 12:35:28 +04:00
|
|
|
if (item[i].magic & magic_mask)
|
2017-01-04 21:04:03 +03:00
|
|
|
unsupported_magic(entry, item[i].magic & magic_mask);
|
2013-07-14 12:35:34 +04:00
|
|
|
|
|
|
|
if ((flags & PATHSPEC_SYMLINK_LEADING_PATH) &&
|
|
|
|
has_symlink_leading_path(item[i].match, item[i].len)) {
|
|
|
|
die(_("pathspec '%s' is beyond a symbolic link"), entry);
|
|
|
|
}
|
|
|
|
|
2013-07-14 12:35:28 +04:00
|
|
|
if (item[i].nowildcard_len < item[i].len)
|
|
|
|
pathspec->has_wildcard = 1;
|
|
|
|
pathspec->magic |= item[i].magic;
|
|
|
|
}
|
|
|
|
|
2013-12-06 11:30:48 +04:00
|
|
|
if (nr_exclude == n)
|
|
|
|
die(_("There is nothing to exclude from by :(exclude) patterns.\n"
|
|
|
|
"Perhaps you forgot to add either ':/' or '.' ?"));
|
|
|
|
|
2013-07-14 12:35:45 +04:00
|
|
|
|
|
|
|
if (pathspec->magic & PATHSPEC_MAXDEPTH) {
|
|
|
|
if (flags & PATHSPEC_KEEP_ORDER)
|
|
|
|
die("BUG: PATHSPEC_MAXDEPTH_VALID and PATHSPEC_KEEP_ORDER are incompatible");
|
2016-09-29 18:27:31 +03:00
|
|
|
QSORT(pathspec->items, pathspec->nr, pathspec_item_cmp);
|
2013-07-14 12:35:45 +04:00
|
|
|
}
|
2013-07-14 12:35:25 +04:00
|
|
|
}
|
|
|
|
|
2013-07-14 12:35:27 +04:00
|
|
|
void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
|
|
|
|
{
|
2017-01-04 21:04:01 +03:00
|
|
|
int i;
|
|
|
|
|
2013-07-14 12:35:27 +04:00
|
|
|
*dst = *src;
|
2016-02-23 01:44:25 +03:00
|
|
|
ALLOC_ARRAY(dst->items, dst->nr);
|
2016-09-25 10:24:03 +03:00
|
|
|
COPY_ARRAY(dst->items, src->items, dst->nr);
|
2017-01-04 21:04:01 +03:00
|
|
|
|
|
|
|
for (i = 0; i < dst->nr; i++) {
|
|
|
|
dst->items[i].match = xstrdup(src->items[i].match);
|
|
|
|
dst->items[i].original = xstrdup(src->items[i].original);
|
|
|
|
}
|
2013-07-14 12:35:27 +04:00
|
|
|
}
|
2013-07-14 12:35:59 +04:00
|
|
|
|
2016-06-03 00:09:22 +03:00
|
|
|
void clear_pathspec(struct pathspec *pathspec)
|
2013-07-14 12:35:59 +04:00
|
|
|
{
|
2017-01-04 21:04:01 +03:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < pathspec->nr; i++) {
|
|
|
|
free(pathspec->items[i].match);
|
|
|
|
free(pathspec->items[i].original);
|
|
|
|
}
|
2013-07-14 12:35:59 +04:00
|
|
|
free(pathspec->items);
|
|
|
|
pathspec->items = NULL;
|
2017-01-04 21:04:01 +03:00
|
|
|
pathspec->nr = 0;
|
2013-01-06 20:58:11 +04:00
|
|
|
}
|