Merge branch 'bw/pathspec-sans-the-index'

Simplify parse_pathspec() codepath and stop it from looking at the
default in-core index.

* bw/pathspec-sans-the-index:
  pathspec: convert find_pathspecs_matching_against_index to take an index
  pathspec: remove PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP
  ls-files: prevent prune_cache from overeagerly pruning submodules
  pathspec: remove PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE flag
  submodule: add die_in_unpopulated_submodule function
  pathspec: provide a more descriptive die message
This commit is contained in:
Junio C Hamano 2017-05-30 11:16:40 +09:00
Родитель 78089b71da 08de9151a8
Коммит 3c5a78280f
11 изменённых файлов: 124 добавлений и 116 удалений

Просмотреть файл

@ -17,6 +17,7 @@
#include "revision.h"
#include "bulk-checkin.h"
#include "argv-array.h"
#include "submodule.h"
static const char * const builtin_add_usage[] = {
N_("git add [<options>] [--] <pathspec>..."),
@ -135,7 +136,7 @@ static char *prune_directory(struct dir_struct *dir, struct pathspec *pathspec,
*dst++ = entry;
}
dir->nr = dst - dir->entries;
add_pathspec_matches_against_index(pathspec, seen);
add_pathspec_matches_against_index(pathspec, &the_index, seen);
return seen;
}
@ -379,16 +380,19 @@ int cmd_add(int argc, const char **argv, const char *prefix)
if (read_cache() < 0)
die(_("index file corrupt"));
die_in_unpopulated_submodule(&the_index, prefix);
/*
* Check the "pathspec '%s' did not match any files" block
* below before enabling new magic.
*/
parse_pathspec(&pathspec, 0,
PATHSPEC_PREFER_FULL |
PATHSPEC_SYMLINK_LEADING_PATH |
PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE,
PATHSPEC_SYMLINK_LEADING_PATH,
prefix, argv);
die_path_inside_submodule(&the_index, &pathspec);
if (add_new_files) {
int baselen;
@ -414,7 +418,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
int i;
if (!seen)
seen = find_pathspecs_matching_against_index(&pathspec);
seen = find_pathspecs_matching_against_index(&pathspec, &the_index);
/*
* file_exists() assumes exact match

Просмотреть файл

@ -4,6 +4,7 @@
#include "quote.h"
#include "pathspec.h"
#include "parse-options.h"
#include "submodule.h"
static int quiet, verbose, stdin_paths, show_non_matching, no_index;
static const char * const check_ignore_usage[] = {
@ -87,16 +88,17 @@ static int check_ignore(struct dir_struct *dir,
parse_pathspec(&pathspec,
PATHSPEC_ALL_MAGIC & ~PATHSPEC_FROMTOP,
PATHSPEC_SYMLINK_LEADING_PATH |
PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE |
PATHSPEC_KEEP_ORDER,
prefix, argv);
die_path_inside_submodule(&the_index, &pathspec);
/*
* look for pathspecs matching entries in the index, since these
* should not be ignored, in order to be consistent with
* 'git status', 'git add' etc.
*/
seen = find_pathspecs_matching_against_index(&pathspec);
seen = find_pathspecs_matching_against_index(&pathspec, &the_index);
for (i = 0; i < pathspec.nr; i++) {
full_path = pathspec.items[i].match;
exclude = NULL;

Просмотреть файл

@ -97,7 +97,7 @@ static void show_dir_entry(const char *tag, struct dir_entry *ent)
{
int len = max_prefix_len;
if (len >= ent->len)
if (len > ent->len)
die("git ls-files: internal error - directory entry not superset of prefix");
if (!dir_path_match(ent, &pathspec, len, ps_matched))
@ -238,7 +238,7 @@ static void show_ce_entry(const char *tag, const struct cache_entry *ce)
strbuf_addstr(&name, super_prefix);
strbuf_addstr(&name, ce->name);
if (len >= ce_namelen(ce))
if (len > ce_namelen(ce))
die("git ls-files: internal error - cache entry not superset of prefix");
if (recurse_submodules && S_ISGITLINK(ce->ce_mode) &&
@ -403,6 +403,25 @@ static void prune_cache(const char *prefix, size_t prefixlen)
active_nr = last - pos;
}
static int get_common_prefix_len(const char *common_prefix)
{
int common_prefix_len;
if (!common_prefix)
return 0;
common_prefix_len = strlen(common_prefix);
/*
* If the prefix has a trailing slash, strip it so that submodules wont
* be pruned from the index.
*/
if (common_prefix[common_prefix_len - 1] == '/')
common_prefix_len--;
return common_prefix_len;
}
/*
* Read the tree specified with --with-tree option
* (typically, HEAD) into stage #1 and then
@ -624,8 +643,7 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
"--error-unmatch");
parse_pathspec(&pathspec, 0,
PATHSPEC_PREFER_CWD |
PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP,
PATHSPEC_PREFER_CWD,
prefix, argv);
/*
@ -637,7 +655,9 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
max_prefix = NULL;
else
max_prefix = common_prefix(&pathspec);
max_prefix_len = max_prefix ? strlen(max_prefix) : 0;
max_prefix_len = get_common_prefix_len(max_prefix);
prune_cache(max_prefix, max_prefix_len);
/* Treat unmatching pathspec elements as errors */
if (pathspec.nr && error_unmatch)
@ -651,7 +671,6 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
show_killed || show_modified || show_resolve_undo))
show_cached = 1;
prune_cache(max_prefix, max_prefix_len);
if (with_tree) {
/*
* Basic sanity check; show-stages and show-unmerged

Просмотреть файл

@ -257,7 +257,6 @@ static void parse_args(struct pathspec *pathspec,
parse_pathspec(pathspec, 0,
PATHSPEC_PREFER_FULL |
PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP |
(patch_mode ? PATHSPEC_PREFIX_ORIGIN : 0),
prefix, argv);
}

Просмотреть файл

@ -271,8 +271,7 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
die(_("index file corrupt"));
parse_pathspec(&pathspec, 0,
PATHSPEC_PREFER_CWD |
PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP,
PATHSPEC_PREFER_CWD,
prefix, argv);
refresh_index(&the_index, REFRESH_QUIET, &pathspec, NULL, NULL);

Просмотреть файл

@ -233,8 +233,7 @@ static int module_list_compute(int argc, const char **argv,
int i, result = 0;
char *ps_matched = NULL;
parse_pathspec(pathspec, 0,
PATHSPEC_PREFER_FULL |
PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP,
PATHSPEC_PREFER_FULL,
prefix, argv);
if (pathspec->nr)

Просмотреть файл

@ -1,3 +1,4 @@
#define NO_THE_INDEX_COMPATIBILITY_MACROS
#include "cache.h"
#include "dir.h"
#include "pathspec.h"
@ -17,6 +18,7 @@
* to use find_pathspecs_matching_against_index() instead.
*/
void add_pathspec_matches_against_index(const struct pathspec *pathspec,
const struct index_state *istate,
char *seen)
{
int num_unmatched = 0, i;
@ -32,8 +34,8 @@ void add_pathspec_matches_against_index(const struct pathspec *pathspec,
num_unmatched++;
if (!num_unmatched)
return;
for (i = 0; i < active_nr; i++) {
const struct cache_entry *ce = active_cache[i];
for (i = 0; i < istate->cache_nr; i++) {
const struct cache_entry *ce = istate->cache[i];
ce_path_match(ce, pathspec, seen);
}
}
@ -46,10 +48,11 @@ void add_pathspec_matches_against_index(const struct pathspec *pathspec,
* nature of the "closest" (i.e. most specific) matches which each of the
* given pathspecs achieves against all items in the index.
*/
char *find_pathspecs_matching_against_index(const struct pathspec *pathspec)
char *find_pathspecs_matching_against_index(const struct pathspec *pathspec,
const struct index_state *istate)
{
char *seen = xcalloc(pathspec->nr, 1);
add_pathspec_matches_against_index(pathspec, seen);
add_pathspec_matches_against_index(pathspec, istate, seen);
return seen;
}
@ -386,65 +389,6 @@ static const char *parse_element_magic(unsigned *magic, int *prefix_len,
return parse_short_magic(magic, elem);
}
static void strip_submodule_slash_cheap(struct pathspec_item *item)
{
if (item->len >= 1 && item->match[item->len - 1] == '/') {
int i = cache_name_pos(item->match, item->len - 1);
if (i >= 0 && S_ISGITLINK(active_cache[i]->ce_mode)) {
item->len--;
item->match[item->len] = '\0';
}
}
}
static void strip_submodule_slash_expensive(struct pathspec_item *item)
{
int i;
for (i = 0; i < active_nr; i++) {
struct cache_entry *ce = active_cache[i];
int ce_len = ce_namelen(ce);
if (!S_ISGITLINK(ce->ce_mode))
continue;
if (item->len <= ce_len || item->match[ce_len] != '/' ||
memcmp(ce->name, item->match, ce_len))
continue;
if (item->len == ce_len + 1) {
/* strip trailing slash */
item->len--;
item->match[item->len] = '\0';
} else {
die(_("Pathspec '%s' is in submodule '%.*s'"),
item->original, ce_len, ce->name);
}
}
}
static void die_inside_submodule_path(struct pathspec_item *item)
{
int i;
for (i = 0; i < active_nr; i++) {
struct cache_entry *ce = active_cache[i];
int ce_len = ce_namelen(ce);
if (!S_ISGITLINK(ce->ce_mode))
continue;
if (item->len < ce_len ||
!(item->match[ce_len] == '/' || item->match[ce_len] == '\0') ||
memcmp(ce->name, item->match, ce_len))
continue;
die(_("Pathspec '%s' is in submodule '%.*s'"),
item->original, ce_len, ce->name);
}
}
/*
* Perform the initialization of a pathspec_item based on a pathspec element.
*/
@ -517,12 +461,6 @@ static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
item->original = xstrdup(elt);
}
if (flags & PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP)
strip_submodule_slash_cheap(item);
if (flags & PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE)
strip_submodule_slash_expensive(item);
if (magic & PATHSPEC_LITERAL) {
item->nowildcard_len = item->len;
} else {
@ -547,15 +485,7 @@ static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
/* sanity checks, pathspec matchers assume these are sane */
if (item->nowildcard_len > item->len ||
item->prefix > item->len) {
/*
* This case can be triggered by the user pointing us to a
* pathspec inside a submodule, which is an input error.
* Detect that here and complain, but fallback in the
* non-submodule case to a BUG, as we have no idea what
* would trigger that.
*/
die_inside_submodule_path(item);
die ("BUG: item->nowildcard_len > item->len || item->prefix > item->len)");
die ("BUG: error initializing pathspec_item");
}
}

Просмотреть файл

@ -58,27 +58,17 @@ struct pathspec {
#define PATHSPEC_PREFER_CWD (1<<0) /* No args means match cwd */
#define PATHSPEC_PREFER_FULL (1<<1) /* No args means match everything */
#define PATHSPEC_MAXDEPTH_VALID (1<<2) /* max_depth field is valid */
/* strip the trailing slash if the given path is a gitlink */
#define PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP (1<<3)
/* die if a symlink is part of the given path's directory */
#define PATHSPEC_SYMLINK_LEADING_PATH (1<<4)
/*
* This is like a combination of ..LEADING_PATH and .._SLASH_CHEAP
* (but not the same): it strips the trailing slash if the given path
* is a gitlink but also checks and dies if gitlink is part of the
* leading path (i.e. the given path goes beyond a submodule). It's
* safer than _SLASH_CHEAP and also more expensive.
*/
#define PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE (1<<5)
#define PATHSPEC_PREFIX_ORIGIN (1<<6)
#define PATHSPEC_KEEP_ORDER (1<<7)
#define PATHSPEC_SYMLINK_LEADING_PATH (1<<3)
#define PATHSPEC_PREFIX_ORIGIN (1<<4)
#define PATHSPEC_KEEP_ORDER (1<<5)
/*
* For the callers that just need pure paths from somewhere else, not
* from command line. Global --*-pathspecs options are ignored. No
* magic is parsed in each pathspec either. If PATHSPEC_LITERAL is
* allowed, then it will automatically set for every pathspec.
*/
#define PATHSPEC_LITERAL_PATH (1<<8)
#define PATHSPEC_LITERAL_PATH (1<<6)
extern void parse_pathspec(struct pathspec *pathspec,
unsigned magic_mask,
@ -106,7 +96,10 @@ static inline int ps_strcmp(const struct pathspec_item *item,
return strcmp(s1, s2);
}
extern char *find_pathspecs_matching_against_index(const struct pathspec *pathspec);
extern void add_pathspec_matches_against_index(const struct pathspec *pathspec, char *seen);
extern void add_pathspec_matches_against_index(const struct pathspec *pathspec,
const struct index_state *istate,
char *seen);
extern char *find_pathspecs_matching_against_index(const struct pathspec *pathspec,
const struct index_state *istate);
#endif /* PATHSPEC_H */

Просмотреть файл

@ -282,6 +282,69 @@ int is_submodule_populated_gently(const char *path, int *return_error_code)
return ret;
}
/*
* Dies if the provided 'prefix' corresponds to an unpopulated submodule
*/
void die_in_unpopulated_submodule(const struct index_state *istate,
const char *prefix)
{
int i, prefixlen;
if (!prefix)
return;
prefixlen = strlen(prefix);
for (i = 0; i < istate->cache_nr; i++) {
struct cache_entry *ce = istate->cache[i];
int ce_len = ce_namelen(ce);
if (!S_ISGITLINK(ce->ce_mode))
continue;
if (prefixlen <= ce_len)
continue;
if (strncmp(ce->name, prefix, ce_len))
continue;
if (prefix[ce_len] != '/')
continue;
die(_("in unpopulated submodule '%s'"), ce->name);
}
}
/*
* Dies if any paths in the provided pathspec descends into a submodule
*/
void die_path_inside_submodule(const struct index_state *istate,
const struct pathspec *ps)
{
int i, j;
for (i = 0; i < istate->cache_nr; i++) {
struct cache_entry *ce = istate->cache[i];
int ce_len = ce_namelen(ce);
if (!S_ISGITLINK(ce->ce_mode))
continue;
for (j = 0; j < ps->nr ; j++) {
const struct pathspec_item *item = &ps->items[j];
if (item->len <= ce_len)
continue;
if (item->match[ce_len] != '/')
continue;
if (strncmp(ce->name, item->match, ce_len))
continue;
if (item->len == ce_len + 1)
continue;
die(_("Pathspec '%s' is in submodule '%.*s'"),
item->original, ce_len, ce->name);
}
}
}
int parse_submodule_update_strategy(const char *value,
struct submodule_update_strategy *dst)
{

Просмотреть файл

@ -49,6 +49,10 @@ extern int is_submodule_initialized(const char *path);
* Otherwise the return error code is the same as of resolve_gitdir_gently.
*/
extern int is_submodule_populated_gently(const char *path, int *return_error_code);
extern void die_in_unpopulated_submodule(const struct index_state *istate,
const char *prefix);
extern void die_path_inside_submodule(const struct index_state *istate,
const struct pathspec *ps);
extern int parse_submodule_update_strategy(const char *value,
struct submodule_update_strategy *dst);
extern const char *submodule_strategy_to_string(const struct submodule_update_strategy *s);

Просмотреть файл

@ -24,13 +24,9 @@ test_expect_success 'error message for path inside submodule' '
test_i18ncmp expect actual
'
cat <<EOF >expect
fatal: Pathspec '.' is in submodule 'sub'
EOF
test_expect_success 'error message for path inside submodule from within submodule' '
test_must_fail git -C sub add . 2>actual &&
test_i18ncmp expect actual
test_i18ngrep "in unpopulated submodule" actual
'
test_done