Skip to content

Commit 3c5a782

Browse files
committed
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
2 parents 78089b7 + 08de915 commit 3c5a782

11 files changed

Lines changed: 124 additions & 116 deletions

File tree

builtin/add.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "revision.h"
1818
#include "bulk-checkin.h"
1919
#include "argv-array.h"
20+
#include "submodule.h"
2021

2122
static const char * const builtin_add_usage[] = {
2223
N_("git add [<options>] [--] <pathspec>..."),
@@ -135,7 +136,7 @@ static char *prune_directory(struct dir_struct *dir, struct pathspec *pathspec,
135136
*dst++ = entry;
136137
}
137138
dir->nr = dst - dir->entries;
138-
add_pathspec_matches_against_index(pathspec, seen);
139+
add_pathspec_matches_against_index(pathspec, &the_index, seen);
139140
return seen;
140141
}
141142

@@ -379,16 +380,19 @@ int cmd_add(int argc, const char **argv, const char *prefix)
379380
if (read_cache() < 0)
380381
die(_("index file corrupt"));
381382

383+
die_in_unpopulated_submodule(&the_index, prefix);
384+
382385
/*
383386
* Check the "pathspec '%s' did not match any files" block
384387
* below before enabling new magic.
385388
*/
386389
parse_pathspec(&pathspec, 0,
387390
PATHSPEC_PREFER_FULL |
388-
PATHSPEC_SYMLINK_LEADING_PATH |
389-
PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE,
391+
PATHSPEC_SYMLINK_LEADING_PATH,
390392
prefix, argv);
391393

394+
die_path_inside_submodule(&the_index, &pathspec);
395+
392396
if (add_new_files) {
393397
int baselen;
394398

@@ -414,7 +418,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
414418
int i;
415419

416420
if (!seen)
417-
seen = find_pathspecs_matching_against_index(&pathspec);
421+
seen = find_pathspecs_matching_against_index(&pathspec, &the_index);
418422

419423
/*
420424
* file_exists() assumes exact match

builtin/check-ignore.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "quote.h"
55
#include "pathspec.h"
66
#include "parse-options.h"
7+
#include "submodule.h"
78

89
static int quiet, verbose, stdin_paths, show_non_matching, no_index;
910
static const char * const check_ignore_usage[] = {
@@ -87,16 +88,17 @@ static int check_ignore(struct dir_struct *dir,
8788
parse_pathspec(&pathspec,
8889
PATHSPEC_ALL_MAGIC & ~PATHSPEC_FROMTOP,
8990
PATHSPEC_SYMLINK_LEADING_PATH |
90-
PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE |
9191
PATHSPEC_KEEP_ORDER,
9292
prefix, argv);
9393

94+
die_path_inside_submodule(&the_index, &pathspec);
95+
9496
/*
9597
* look for pathspecs matching entries in the index, since these
9698
* should not be ignored, in order to be consistent with
9799
* 'git status', 'git add' etc.
98100
*/
99-
seen = find_pathspecs_matching_against_index(&pathspec);
101+
seen = find_pathspecs_matching_against_index(&pathspec, &the_index);
100102
for (i = 0; i < pathspec.nr; i++) {
101103
full_path = pathspec.items[i].match;
102104
exclude = NULL;

builtin/ls-files.c

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ static void show_dir_entry(const char *tag, struct dir_entry *ent)
9797
{
9898
int len = max_prefix_len;
9999

100-
if (len >= ent->len)
100+
if (len > ent->len)
101101
die("git ls-files: internal error - directory entry not superset of prefix");
102102

103103
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)
238238
strbuf_addstr(&name, super_prefix);
239239
strbuf_addstr(&name, ce->name);
240240

241-
if (len >= ce_namelen(ce))
241+
if (len > ce_namelen(ce))
242242
die("git ls-files: internal error - cache entry not superset of prefix");
243243

244244
if (recurse_submodules && S_ISGITLINK(ce->ce_mode) &&
@@ -403,6 +403,25 @@ static void prune_cache(const char *prefix, size_t prefixlen)
403403
active_nr = last - pos;
404404
}
405405

406+
static int get_common_prefix_len(const char *common_prefix)
407+
{
408+
int common_prefix_len;
409+
410+
if (!common_prefix)
411+
return 0;
412+
413+
common_prefix_len = strlen(common_prefix);
414+
415+
/*
416+
* If the prefix has a trailing slash, strip it so that submodules wont
417+
* be pruned from the index.
418+
*/
419+
if (common_prefix[common_prefix_len - 1] == '/')
420+
common_prefix_len--;
421+
422+
return common_prefix_len;
423+
}
424+
406425
/*
407426
* Read the tree specified with --with-tree option
408427
* (typically, HEAD) into stage #1 and then
@@ -624,8 +643,7 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
624643
"--error-unmatch");
625644

626645
parse_pathspec(&pathspec, 0,
627-
PATHSPEC_PREFER_CWD |
628-
PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP,
646+
PATHSPEC_PREFER_CWD,
629647
prefix, argv);
630648

631649
/*
@@ -637,7 +655,9 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
637655
max_prefix = NULL;
638656
else
639657
max_prefix = common_prefix(&pathspec);
640-
max_prefix_len = max_prefix ? strlen(max_prefix) : 0;
658+
max_prefix_len = get_common_prefix_len(max_prefix);
659+
660+
prune_cache(max_prefix, max_prefix_len);
641661

642662
/* Treat unmatching pathspec elements as errors */
643663
if (pathspec.nr && error_unmatch)
@@ -651,7 +671,6 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
651671
show_killed || show_modified || show_resolve_undo))
652672
show_cached = 1;
653673

654-
prune_cache(max_prefix, max_prefix_len);
655674
if (with_tree) {
656675
/*
657676
* Basic sanity check; show-stages and show-unmerged

builtin/reset.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,6 @@ static void parse_args(struct pathspec *pathspec,
257257

258258
parse_pathspec(pathspec, 0,
259259
PATHSPEC_PREFER_FULL |
260-
PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP |
261260
(patch_mode ? PATHSPEC_PREFIX_ORIGIN : 0),
262261
prefix, argv);
263262
}

builtin/rm.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,7 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
271271
die(_("index file corrupt"));
272272

273273
parse_pathspec(&pathspec, 0,
274-
PATHSPEC_PREFER_CWD |
275-
PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP,
274+
PATHSPEC_PREFER_CWD,
276275
prefix, argv);
277276
refresh_index(&the_index, REFRESH_QUIET, &pathspec, NULL, NULL);
278277

builtin/submodule--helper.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,7 @@ static int module_list_compute(int argc, const char **argv,
233233
int i, result = 0;
234234
char *ps_matched = NULL;
235235
parse_pathspec(pathspec, 0,
236-
PATHSPEC_PREFER_FULL |
237-
PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP,
236+
PATHSPEC_PREFER_FULL,
238237
prefix, argv);
239238

240239
if (pathspec->nr)

pathspec.c

Lines changed: 8 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#define NO_THE_INDEX_COMPATIBILITY_MACROS
12
#include "cache.h"
23
#include "dir.h"
34
#include "pathspec.h"
@@ -17,6 +18,7 @@
1718
* to use find_pathspecs_matching_against_index() instead.
1819
*/
1920
void add_pathspec_matches_against_index(const struct pathspec *pathspec,
21+
const struct index_state *istate,
2022
char *seen)
2123
{
2224
int num_unmatched = 0, i;
@@ -32,8 +34,8 @@ void add_pathspec_matches_against_index(const struct pathspec *pathspec,
3234
num_unmatched++;
3335
if (!num_unmatched)
3436
return;
35-
for (i = 0; i < active_nr; i++) {
36-
const struct cache_entry *ce = active_cache[i];
37+
for (i = 0; i < istate->cache_nr; i++) {
38+
const struct cache_entry *ce = istate->cache[i];
3739
ce_path_match(ce, pathspec, seen);
3840
}
3941
}
@@ -46,10 +48,11 @@ void add_pathspec_matches_against_index(const struct pathspec *pathspec,
4648
* nature of the "closest" (i.e. most specific) matches which each of the
4749
* given pathspecs achieves against all items in the index.
4850
*/
49-
char *find_pathspecs_matching_against_index(const struct pathspec *pathspec)
51+
char *find_pathspecs_matching_against_index(const struct pathspec *pathspec,
52+
const struct index_state *istate)
5053
{
5154
char *seen = xcalloc(pathspec->nr, 1);
52-
add_pathspec_matches_against_index(pathspec, seen);
55+
add_pathspec_matches_against_index(pathspec, istate, seen);
5356
return seen;
5457
}
5558

@@ -386,65 +389,6 @@ static const char *parse_element_magic(unsigned *magic, int *prefix_len,
386389
return parse_short_magic(magic, elem);
387390
}
388391

389-
static void strip_submodule_slash_cheap(struct pathspec_item *item)
390-
{
391-
if (item->len >= 1 && item->match[item->len - 1] == '/') {
392-
int i = cache_name_pos(item->match, item->len - 1);
393-
394-
if (i >= 0 && S_ISGITLINK(active_cache[i]->ce_mode)) {
395-
item->len--;
396-
item->match[item->len] = '\0';
397-
}
398-
}
399-
}
400-
401-
static void strip_submodule_slash_expensive(struct pathspec_item *item)
402-
{
403-
int i;
404-
405-
for (i = 0; i < active_nr; i++) {
406-
struct cache_entry *ce = active_cache[i];
407-
int ce_len = ce_namelen(ce);
408-
409-
if (!S_ISGITLINK(ce->ce_mode))
410-
continue;
411-
412-
if (item->len <= ce_len || item->match[ce_len] != '/' ||
413-
memcmp(ce->name, item->match, ce_len))
414-
continue;
415-
416-
if (item->len == ce_len + 1) {
417-
/* strip trailing slash */
418-
item->len--;
419-
item->match[item->len] = '\0';
420-
} else {
421-
die(_("Pathspec '%s' is in submodule '%.*s'"),
422-
item->original, ce_len, ce->name);
423-
}
424-
}
425-
}
426-
427-
static void die_inside_submodule_path(struct pathspec_item *item)
428-
{
429-
int i;
430-
431-
for (i = 0; i < active_nr; i++) {
432-
struct cache_entry *ce = active_cache[i];
433-
int ce_len = ce_namelen(ce);
434-
435-
if (!S_ISGITLINK(ce->ce_mode))
436-
continue;
437-
438-
if (item->len < ce_len ||
439-
!(item->match[ce_len] == '/' || item->match[ce_len] == '\0') ||
440-
memcmp(ce->name, item->match, ce_len))
441-
continue;
442-
443-
die(_("Pathspec '%s' is in submodule '%.*s'"),
444-
item->original, ce_len, ce->name);
445-
}
446-
}
447-
448392
/*
449393
* Perform the initialization of a pathspec_item based on a pathspec element.
450394
*/
@@ -517,12 +461,6 @@ static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
517461
item->original = xstrdup(elt);
518462
}
519463

520-
if (flags & PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP)
521-
strip_submodule_slash_cheap(item);
522-
523-
if (flags & PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE)
524-
strip_submodule_slash_expensive(item);
525-
526464
if (magic & PATHSPEC_LITERAL) {
527465
item->nowildcard_len = item->len;
528466
} else {
@@ -547,15 +485,7 @@ static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
547485
/* sanity checks, pathspec matchers assume these are sane */
548486
if (item->nowildcard_len > item->len ||
549487
item->prefix > item->len) {
550-
/*
551-
* This case can be triggered by the user pointing us to a
552-
* pathspec inside a submodule, which is an input error.
553-
* Detect that here and complain, but fallback in the
554-
* non-submodule case to a BUG, as we have no idea what
555-
* would trigger that.
556-
*/
557-
die_inside_submodule_path(item);
558-
die ("BUG: item->nowildcard_len > item->len || item->prefix > item->len)");
488+
die ("BUG: error initializing pathspec_item");
559489
}
560490
}
561491

pathspec.h

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,27 +58,17 @@ struct pathspec {
5858
#define PATHSPEC_PREFER_CWD (1<<0) /* No args means match cwd */
5959
#define PATHSPEC_PREFER_FULL (1<<1) /* No args means match everything */
6060
#define PATHSPEC_MAXDEPTH_VALID (1<<2) /* max_depth field is valid */
61-
/* strip the trailing slash if the given path is a gitlink */
62-
#define PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP (1<<3)
6361
/* die if a symlink is part of the given path's directory */
64-
#define PATHSPEC_SYMLINK_LEADING_PATH (1<<4)
65-
/*
66-
* This is like a combination of ..LEADING_PATH and .._SLASH_CHEAP
67-
* (but not the same): it strips the trailing slash if the given path
68-
* is a gitlink but also checks and dies if gitlink is part of the
69-
* leading path (i.e. the given path goes beyond a submodule). It's
70-
* safer than _SLASH_CHEAP and also more expensive.
71-
*/
72-
#define PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE (1<<5)
73-
#define PATHSPEC_PREFIX_ORIGIN (1<<6)
74-
#define PATHSPEC_KEEP_ORDER (1<<7)
62+
#define PATHSPEC_SYMLINK_LEADING_PATH (1<<3)
63+
#define PATHSPEC_PREFIX_ORIGIN (1<<4)
64+
#define PATHSPEC_KEEP_ORDER (1<<5)
7565
/*
7666
* For the callers that just need pure paths from somewhere else, not
7767
* from command line. Global --*-pathspecs options are ignored. No
7868
* magic is parsed in each pathspec either. If PATHSPEC_LITERAL is
7969
* allowed, then it will automatically set for every pathspec.
8070
*/
81-
#define PATHSPEC_LITERAL_PATH (1<<8)
71+
#define PATHSPEC_LITERAL_PATH (1<<6)
8272

8373
extern void parse_pathspec(struct pathspec *pathspec,
8474
unsigned magic_mask,
@@ -106,7 +96,10 @@ static inline int ps_strcmp(const struct pathspec_item *item,
10696
return strcmp(s1, s2);
10797
}
10898

109-
extern char *find_pathspecs_matching_against_index(const struct pathspec *pathspec);
110-
extern void add_pathspec_matches_against_index(const struct pathspec *pathspec, char *seen);
99+
extern void add_pathspec_matches_against_index(const struct pathspec *pathspec,
100+
const struct index_state *istate,
101+
char *seen);
102+
extern char *find_pathspecs_matching_against_index(const struct pathspec *pathspec,
103+
const struct index_state *istate);
111104

112105
#endif /* PATHSPEC_H */

0 commit comments

Comments
 (0)