Skip to content

Commit c0fd1f5

Browse files
author
Linus Torvalds
committed
Make "ce_match_path()" a generic helper function
... and make git-diff-files use it too. This all _should_ make the diffcore-pathspec.c phase unnecessary, since the diff'ers now all do the path matching early interally.
1 parent fdee7d0 commit c0fd1f5

File tree

4 files changed

+33
-23
lines changed

4 files changed

+33
-23
lines changed

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ extern int remove_cache_entry_at(int pos);
148148
extern int remove_file_from_cache(char *path);
149149
extern int ce_same_name(struct cache_entry *a, struct cache_entry *b);
150150
extern int ce_match_stat(struct cache_entry *ce, struct stat *st);
151+
extern int ce_path_match(const struct cache_entry *ce, const char **pathspec);
151152
extern int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, const char *type);
152153
extern void fill_stat_cache_info(struct cache_entry *ce, struct stat *st);
153154

diff-cache.c

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -87,28 +87,6 @@ static int show_modified(struct cache_entry *old,
8787
return 0;
8888
}
8989

90-
static int ce_path_match(const struct cache_entry *ce, const char **pathspec)
91-
{
92-
const char *match, *name;
93-
int len;
94-
95-
if (!pathspec)
96-
return 1;
97-
98-
len = ce_namelen(ce);
99-
name = ce->name;
100-
while ((match = *pathspec++) != NULL) {
101-
int matchlen = strlen(match);
102-
if (matchlen > len)
103-
continue;
104-
if (memcmp(name, match, matchlen))
105-
continue;
106-
if (name[matchlen] == '/' || !name[matchlen])
107-
return 1;
108-
}
109-
return 0;
110-
}
111-
11290
static int diff_cache(struct cache_entry **ac, int entries, const char **pathspec)
11391
{
11492
while (entries) {

diff-files.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ static void show_modified(int oldmode, int mode,
4343
int main(int argc, const char **argv)
4444
{
4545
static const unsigned char null_sha1[20] = { 0, };
46+
const char **pathspec;
4647
int entries = read_cache();
4748
int i;
4849

@@ -95,6 +96,9 @@ int main(int argc, const char **argv)
9596
argv++; argc--;
9697
}
9798

99+
/* Do we have a pathspec? */
100+
pathspec = (argc > 1) ? argv + 1 : NULL;
101+
98102
if (find_copies_harder && detect_rename != DIFF_DETECT_COPY)
99103
usage(diff_files_usage);
100104

@@ -114,6 +118,9 @@ int main(int argc, const char **argv)
114118
struct cache_entry *ce = active_cache[i];
115119
int changed;
116120

121+
if (!ce_path_match(ce, pathspec))
122+
continue;
123+
117124
if (ce_stage(ce)) {
118125
show_unmerge(ce->name);
119126
while (i < entries &&
@@ -141,7 +148,7 @@ int main(int argc, const char **argv)
141148
ce->sha1, (changed ? null_sha1 : ce->sha1),
142149
ce->name);
143150
}
144-
diffcore_std((1 < argc) ? argv + 1 : NULL,
151+
diffcore_std(pathspec,
145152
detect_rename, diff_score_opt,
146153
pickaxe, pickaxe_opts,
147154
diff_break_opt,

read-cache.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,30 @@ int ce_same_name(struct cache_entry *a, struct cache_entry *b)
171171
return ce_namelen(b) == len && !memcmp(a->name, b->name, len);
172172
}
173173

174+
int ce_path_match(const struct cache_entry *ce, const char **pathspec)
175+
{
176+
const char *match, *name;
177+
int len;
178+
179+
if (!pathspec)
180+
return 1;
181+
182+
len = ce_namelen(ce);
183+
name = ce->name;
184+
while ((match = *pathspec++) != NULL) {
185+
int matchlen = strlen(match);
186+
if (matchlen > len)
187+
continue;
188+
if (memcmp(name, match, matchlen))
189+
continue;
190+
if (matchlen && name[matchlen-1] == '/')
191+
return 1;
192+
if (name[matchlen] == '/' || !name[matchlen])
193+
return 1;
194+
}
195+
return 0;
196+
}
197+
174198
/*
175199
* Do we have another file that has the beginning components being a
176200
* proper superset of the name we're trying to add?

0 commit comments

Comments
 (0)