Skip to content

Commit 827f4d6

Browse files
pcloudsgitster
authored andcommitted
convert common_prefix() to use struct pathspec
The code now takes advantage of nowildcard_len field. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3efe8e4 commit 827f4d6

File tree

4 files changed

+18
-19
lines changed

4 files changed

+18
-19
lines changed

builtin/commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ static int list_paths(struct string_list *list, const char *with_tree,
199199
m = xcalloc(1, pattern->nr);
200200

201201
if (with_tree) {
202-
char *max_prefix = common_prefix(pattern->raw);
202+
char *max_prefix = common_prefix(pattern);
203203
overlay_tree_on_cache(with_tree, max_prefix ? max_prefix : prefix);
204204
free(max_prefix);
205205
}

builtin/ls-files.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
546546
prefix, argv);
547547

548548
/* Find common prefix for all pathspec's */
549-
max_prefix = common_prefix(pathspec.raw);
549+
max_prefix = common_prefix(&pathspec);
550550
max_prefix_len = max_prefix ? strlen(max_prefix) : 0;
551551

552552
/* Treat unmatching pathspec elements as errors */

dir.c

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -103,26 +103,25 @@ static int fnmatch_icase_mem(const char *pattern, int patternlen,
103103
return match_status;
104104
}
105105

106-
static size_t common_prefix_len(const char **pathspec)
106+
static size_t common_prefix_len(const struct pathspec *pathspec)
107107
{
108-
const char *n, *first;
108+
int n;
109109
size_t max = 0;
110-
int literal = limit_pathspec_to_literal();
111110

112-
if (!pathspec)
113-
return max;
114-
115-
first = *pathspec;
116-
while ((n = *pathspec++)) {
117-
size_t i, len = 0;
118-
for (i = 0; first == n || i < max; i++) {
119-
char c = n[i];
120-
if (!c || c != first[i] || (!literal && is_glob_special(c)))
111+
GUARD_PATHSPEC(pathspec, PATHSPEC_FROMTOP | PATHSPEC_MAXDEPTH);
112+
113+
for (n = 0; n < pathspec->nr; n++) {
114+
size_t i = 0, len = 0;
115+
while (i < pathspec->items[n].nowildcard_len &&
116+
(n == 0 || i < max)) {
117+
char c = pathspec->items[n].match[i];
118+
if (c != pathspec->items[0].match[i])
121119
break;
122120
if (c == '/')
123121
len = i + 1;
122+
i++;
124123
}
125-
if (first == n || len < max) {
124+
if (n == 0 || len < max) {
126125
max = len;
127126
if (!max)
128127
break;
@@ -135,11 +134,11 @@ static size_t common_prefix_len(const char **pathspec)
135134
* Returns a copy of the longest leading path common among all
136135
* pathspecs.
137136
*/
138-
char *common_prefix(const char **pathspec)
137+
char *common_prefix(const struct pathspec *pathspec)
139138
{
140139
unsigned long len = common_prefix_len(pathspec);
141140

142-
return len ? xmemdupz(*pathspec, len) : NULL;
141+
return len ? xmemdupz(pathspec->items[0].match, len) : NULL;
143142
}
144143

145144
int fill_directory(struct dir_struct *dir, const struct pathspec *pathspec)
@@ -150,7 +149,7 @@ int fill_directory(struct dir_struct *dir, const struct pathspec *pathspec)
150149
* Calculate common prefix for the pathspec, and
151150
* use that to optimize the directory walk
152151
*/
153-
len = common_prefix_len(pathspec->raw);
152+
len = common_prefix_len(pathspec);
154153

155154
/* Read the directory and prune it */
156155
read_directory(dir, pathspec->nr ? pathspec->raw[0] : "", len, pathspec);

dir.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ struct dir_struct {
130130
#define MATCHED_EXACTLY 3
131131
extern int simple_length(const char *match);
132132
extern int no_wildcard(const char *string);
133-
extern char *common_prefix(const char **pathspec);
133+
extern char *common_prefix(const struct pathspec *pathspec);
134134
extern int match_pathspec(const char **pathspec, const char *name, int namelen, int prefix, char *seen);
135135
extern int match_pathspec_depth(const struct pathspec *pathspec,
136136
const char *name, int namelen,

0 commit comments

Comments
 (0)