38
39
40
/*
41
- * Match a pathspec against a filename. The first "len" characters
41
+ * Match a pathspec against a filename. The first "skiplen" characters
42
* are the common prefix
43
*/
44
-static int match(const char **spec, char *ps_matched,
45
- const char *filename, int len)
44
+int pathspec_match(const char **spec, char *ps_matched,
45
+ const char *filename, int skiplen)
46
{
47
const char *m;
48
49
while ((m = *spec++) != NULL) {
50
- int matchlen = strlen(m + len);
50
+ int matchlen = strlen(m + skiplen);
51
52
if (!matchlen)
53
goto matched;
54
- if (!strncmp(m + len, filename + len, matchlen)) {
55
- if (m[len + matchlen - 1] == '/')
54
+ if (!strncmp(m + skiplen, filename + skiplen, matchlen)) {
55
+ if (m[skiplen + matchlen - 1] == '/')
56
goto matched;
57
- switch (filename[len + matchlen]) {
57
+ switch (filename[skiplen + matchlen]) {
58
case '/': case '\0':
59
goto matched;
60
}
61
}
62
- if (!fnmatch(m + len, filename + len, 0))
62
+ if (!fnmatch(m + skiplen, filename + skiplen, 0))
63
goto matched;
64
if (ps_matched)
65
ps_matched++;
80
if (len >= ent->len)
81
die("git-ls-files: internal error - directory entry not superset of prefix");
82
83
- if (pathspec && !match(pathspec, ps_matched, ent->name, len))
83
+ if (pathspec && !pathspec_match(pathspec, ps_matched, ent->name, len))
84
return;
85
86
fputs(tag, stdout);
185
if (len >= ce_namelen(ce))
186
die("git-ls-files: internal error - cache entry not superset of prefix");
187
188
- if (pathspec && !match(pathspec, ps_matched, ce->name, len))
188
+ if (pathspec && !pathspec_match(pathspec, ps_matched, ce->name, len))
189
return;
190
191
if (tag && *tag && show_valid_bit &&
331
* that were given from the command line. We are not
332
* going to write this index out.
333
*/
334
-static void overlay_tree(const char *tree_name, const char *prefix)
334
+void overlay_tree_on_cache(const char *tree_name, const char *prefix)
335
{
336
struct tree *tree;
337
unsigned char sha1[20];
384
}
385
}
386
387
+int report_path_error(const char *ps_matched, const char **pathspec, int prefix_offset)
388
+{
389
+ /*
390
+ * Make sure all pathspec matched; otherwise it is an error.
391
+ */
392
+ int num, errors = 0;
393
+ for (num = 0; pathspec[num]; num++) {
394
+ int other, found_dup;
395
+
396
+ if (ps_matched[num])
397
+ continue;
398
+ /*
399
+ * The caller might have fed identical pathspec
400
+ * twice. Do not barf on such a mistake.
401
+ */
402
+ for (found_dup = other = 0;
403
+ !found_dup && pathspec[other];
404
+ other++) {
405
+ if (other == num || !ps_matched[other])
406
+ continue;
407
+ if (!strcmp(pathspec[other], pathspec[num]))
408
+ /*
409
+ * Ok, we have a match already.
410
+ */
411
+ found_dup = 1;
412
+ }
413
+ if (found_dup)
414
+ continue;
415
+
416
+ error("pathspec '%s' did not match any file(s) known to git.",
417
+ pathspec[num] + prefix_offset);
418
+ errors++;
419
+ }
420
+ return errors;
421
+}
422
+
423
static const char ls_files_usage[] =
424
"git-ls-files [-z] [-t] [-v] (--[cached|deleted|others|stage|unmerged|killed|modified])* "
425
"[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] "
604
*/
605
if (show_stage || show_unmerged)
606
die("ls-files --with-tree is incompatible with -s or -u");
571
- overlay_tree(with_tree, prefix);
607
+ overlay_tree_on_cache(with_tree, prefix);
608
}
609
show_files(&dir, prefix);
610
611
if (ps_matched) {
576
- /* We need to make sure all pathspec matched otherwise
577
- * it is an error.
578
- */
579
- int num, errors = 0;
580
- for (num = 0; pathspec[num]; num++) {
581
- int other, found_dup;
582
-
583
- if (ps_matched[num])
584
- continue;
585
- /*
586
- * The caller might have fed identical pathspec
587
- * twice. Do not barf on such a mistake.
588
- */
589
- for (found_dup = other = 0;
590
- !found_dup && pathspec[other];
591
- other++) {
592
- if (other == num || !ps_matched[other])
593
- continue;
594
- if (!strcmp(pathspec[other], pathspec[num]))
595
- /*
596
- * Ok, we have a match already.
597
- */
598
- found_dup = 1;
599
- }
600
- if (found_dup)
601
- continue;
602
-
603
- error("pathspec '%s' did not match any file(s) known to git.",
604
- pathspec[num] + prefix_offset);
605
- errors++;
606
- }
607
-
608
- if (errors)
612
+ int bad;
613
+ bad = report_path_error(ps_matched, pathspec, prefix_offset);
614
+ if (bad)
615
fprintf(stderr, "Did you forget to 'git add'?\n");
616
611
- return errors ? 1 : 0;
617
+ return bad ? 1 : 0;
618
}
619
620
return 0;