@matheusbsbs / matheus-bagliano / commits / ee425e4643
Fork 0

Export three helper functions from ls-files

This exports three helper functions from ls-files. * pathspec_match() checks if a given path matches a set of pathspecs and optionally records which pathspec was used. This function used to be called "match()" but renamed to be a bit less vague. * report_path_error() takes a set of pathspecs and the record pathspec_match() above leaves, and gives error message. This was split out of the main function of ls-files. * overlay_tree_on_cache() takes a tree-ish (typically "HEAD") and overlays it on the current in-core index. By iterating over the resulting index, the caller can find out the paths in either the index or the HEAD. This function used to be called "overlay_tree()" but renamed to be a bit more descriptive. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Nov 18, 2007 at 01:13 UTC ee425e4643aa2d7be72cb4586d7554cecce44d6e
2 files changed +58 -46
builtin-ls-files.c
+52 -46
@@ -38,28 +38,28 @@ static const char *tag_modified = "";
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,7 +80,7 @@ static void show_dir_entry(const char *tag, struct dir_entry *ent)
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,7 +185,7 @@ static void show_ce_entry(const char *tag, struct cache_entry *ce)
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,7 +331,7 @@ static const char *verify_pathspec(const char *prefix)
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,6 +384,42 @@ static void overlay_tree(const char *tree_name, const char *prefix)
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>] "
@@ -568,47 +604,17 @@ int cmd_ls_files(int argc, const char **argv, const char *prefix)
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;
cache.h
+6
@@ -610,4 +610,10 @@ extern int diff_auto_refresh_index;
610 /* match-trees.c */
611 void shift_tree(const unsigned char *, const unsigned char *, unsigned char *, int);
612
613 +
614 +/* ls-files */
615 +int pathspec_match(const char **spec, char *matched, const char *filename, int skiplen);
616 +int report_path_error(const char *ps_matched, const char **pathspec, int prefix_offset);
617 +void overlay_tree_on_cache(const char *tree_name, const char *prefix);
618 +
619 #endif /* CACHE_H */