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

match_pathspec() -- return how well the spec matched

This updates the return value from match_pathspec() so that the caller can tell cases between exact match, leading pathname match (i.e. file "foo/bar" matches a pathspec "foo"), or filename glob match. This can be used to prevent "rm dir" from removing "dir/file" without explicitly asking for recursive behaviour with -r flag, for example. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Dec 25, 2006 at 03:09 UTC e813d50e35653bdb0ce3329f99d1be7fc1c36de5
2 files changed +39 -16
dir.c
+35 -16
@@ -40,6 +40,18 @@ int common_prefix(const char **pathspec)
40 return prefix;
41 }
42
43 +/*
44 + * Does 'match' matches the given name?
45 + * A match is found if
46 + *
47 + * (1) the 'match' string is leading directory of 'name', or
48 + * (2) the 'match' string is a wildcard and matches 'name', or
49 + * (3) the 'match' string is exactly the same as 'name'.
50 + *
51 + * and the return value tells which case it was.
52 + *
53 + * It returns 0 when there is no match.
54 + */
55 static int match_one(const char *match, const char *name, int namelen)
56 {
57 int matchlen;
@@ -47,27 +59,30 @@ static int match_one(const char *match, const char *name, int namelen)
59 /* If the match was just the prefix, we matched */
60 matchlen = strlen(match);
61 if (!matchlen)
50 - return 1;
62 + return MATCHED_RECURSIVELY;
63
64 /*
65 * If we don't match the matchstring exactly,
66 * we need to match by fnmatch
67 */
68 if (strncmp(match, name, matchlen))
57 - return !fnmatch(match, name, 0);
69 + return !fnmatch(match, name, 0) ? MATCHED_FNMATCH : 0;
70
59 - /*
60 - * If we did match the string exactly, we still
61 - * need to make sure that it happened on a path
62 - * component boundary (ie either the last character
63 - * of the match was '/', or the next character of
64 - * the name was '/' or the terminating NUL.
65 - */
66 - return match[matchlen-1] == '/' ||
67 - name[matchlen] == '/' ||
68 - !name[matchlen];
71 + if (!name[matchlen])
72 + return MATCHED_EXACTLY;
73 + if (match[matchlen-1] == '/' || name[matchlen] == '/')
74 + return MATCHED_RECURSIVELY;
75 + return 0;
76 }
77
78 +/*
79 + * Given a name and a list of pathspecs, see if the name matches
80 + * any of the pathspecs. The caller is also interested in seeing
81 + * all pathspec matches some names it calls this function with
82 + * (otherwise the user could have mistyped the unmatched pathspec),
83 + * and a mark is left in seen[] array for pathspec element that
84 + * actually matched anything.
85 + */
86 int match_pathspec(const char **pathspec, const char *name, int namelen, int prefix, char *seen)
87 {
88 int retval;
@@ -77,12 +92,16 @@ int match_pathspec(const char **pathspec, const char *name, int namelen, int pre
92 namelen -= prefix;
93
94 for (retval = 0; (match = *pathspec++) != NULL; seen++) {
80 - if (retval & *seen)
95 + int how;
96 + if (retval && *seen == MATCHED_EXACTLY)
97 continue;
98 match += prefix;
83 - if (match_one(match, name, namelen)) {
84 - retval = 1;
85 - *seen = 1;
99 + how = match_one(match, name, namelen);
100 + if (how) {
101 + if (retval < how)
102 + retval = how;
103 + if (*seen < how)
104 + *seen = how;
105 }
106 }
107 return retval;
dir.h
+4
@@ -40,6 +40,10 @@ struct dir_struct {
40 };
41
42 extern int common_prefix(const char **pathspec);
43 +
44 +#define MATCHED_RECURSIVELY 1
45 +#define MATCHED_FNMATCH 2
46 +#define MATCHED_EXACTLY 3
47 extern int match_pathspec(const char **pathspec, const char *name, int namelen, int prefix, char *seen);
48
49 extern int read_directory(struct dir_struct *, const char *path, const char *base, int baselen);