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

builtin-add: warn on unmatched pathspecs

This is in the same spirit as what bba319b5 and 45e48120 tried to do to help users. A command such as "git add Documentaiton" with misspelled pathspecs would give a friendly reminder with this. Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Linus Torvalds committed May 17, 2006 at 13:23 UTC f25933987f29070e9cd79dfddf03018010e82e80
1 file changed +58 -19
builtin-add.c
+58 -19
@@ -44,50 +44,89 @@ static int common_prefix(const char **pathspec)
44 return prefix;
45 }
46
47 -static int match(const char **pathspec, const char *name, int namelen, int prefix)
47 +static int match_one(const char *match, const char *name, int namelen)
48 {
49 + int matchlen;
50 +
51 + /* If the match was just the prefix, we matched */
52 + matchlen = strlen(match);
53 + if (!matchlen)
54 + return 1;
55 +
56 + /*
57 + * If we don't match the matchstring exactly,
58 + * we need to match by fnmatch
59 + */
60 + if (strncmp(match, name, matchlen))
61 + return !fnmatch(match, name, 0);
62 +
63 + /*
64 + * If we did match the string exactly, we still
65 + * need to make sure that it happened on a path
66 + * component boundary (ie either the last character
67 + * of the match was '/', or the next character of
68 + * the name was '/' or the terminating NUL.
69 + */
70 + return match[matchlen-1] == '/' ||
71 + name[matchlen] == '/' ||
72 + !name[matchlen];
73 +}
74 +
75 +static int match(const char **pathspec, const char *name, int namelen, int prefix, char *seen)
76 +{
77 + int retval;
78 const char *match;
79
80 name += prefix;
81 namelen -= prefix;
82
54 - while ((match = *pathspec++) != NULL) {
55 - int matchlen;
56 -
83 + for (retval = 0; (match = *pathspec++) != NULL; seen++) {
84 + if (retval & *seen)
85 + continue;
86 match += prefix;
58 - matchlen = strlen(match);
59 - if (!matchlen)
60 - return 1;
61 - if (!strncmp(match, name, matchlen)) {
62 - if (match[matchlen-1] == '/')
63 - return 1;
64 - switch (name[matchlen]) {
65 - case '/': case '\0':
66 - return 1;
67 - }
87 + if (match_one(match, name, namelen)) {
88 + retval = 1;
89 + *seen = 1;
90 }
69 - if (!fnmatch(match, name, 0))
70 - return 1;
91 }
72 - return 0;
92 + return retval;
93 }
94
95 static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
96 {
77 - int i;
97 + char *seen;
98 + int i, specs;
99 struct dir_entry **src, **dst;
100
101 + for (specs = 0; pathspec[specs]; specs++)
102 + /* nothing */;
103 + seen = xmalloc(specs);
104 + memset(seen, 0, specs);
105 +
106 src = dst = dir->entries;
107 i = dir->nr;
108 while (--i >= 0) {
109 struct dir_entry *entry = *src++;
84 - if (!match(pathspec, entry->name, entry->len, prefix)) {
110 + if (!match(pathspec, entry->name, entry->len, prefix, seen)) {
111 free(entry);
112 continue;
113 }
114 *dst++ = entry;
115 }
116 dir->nr = dst - dir->entries;
117 +
118 + for (i = 0; i < specs; i++) {
119 + struct stat st;
120 + const char *match;
121 + if (seen[i])
122 + continue;
123 +
124 + /* Existing file? We must have ignored it */
125 + match = pathspec[i];
126 + if (!lstat(match, &st))
127 + continue;
128 + die("pathspec '%s' did not match any files", match);
129 + }
130 }
131
132 static void fill_directory(struct dir_struct *dir, const char **pathspec)