]>
Commit | Line | Data |
---|---|---|
6b14d7fa JH |
1 | /* |
2 | * Copyright (C) 2005 Junio C Hamano | |
3 | */ | |
4 | #include "cache.h" | |
5 | #include "diff.h" | |
6 | #include "diffcore.h" | |
6b14d7fa JH |
7 | |
8 | struct path_spec { | |
9 | const char *spec; | |
10 | int len; | |
11 | }; | |
12 | ||
13 | static int matches_pathspec(const char *name, struct path_spec *s, int cnt) | |
14 | { | |
15 | int i; | |
16 | int namelen; | |
17 | ||
18 | if (cnt == 0) | |
19 | return 1; | |
20 | ||
21 | namelen = strlen(name); | |
22 | for (i = 0; i < cnt; i++) { | |
f4d89f26 | 23 | int len = s[i].len; |
1d9e6f92 LT |
24 | if (namelen < len) |
25 | continue; | |
26 | if (memcmp(s[i].spec, name, len)) | |
27 | continue; | |
28 | if (s[i].spec[len-1] == '/' || | |
29 | name[len] == 0 || | |
30 | name[len] == '/') | |
6b14d7fa | 31 | return 1; |
f332726e LT |
32 | if (!len) |
33 | return 1; | |
6b14d7fa JH |
34 | } |
35 | return 0; | |
36 | } | |
37 | ||
38 | void diffcore_pathspec(const char **pathspec) | |
39 | { | |
40 | struct diff_queue_struct *q = &diff_queued_diff; | |
41 | int i, speccnt; | |
42 | struct diff_queue_struct outq; | |
43 | struct path_spec *spec; | |
44 | ||
45 | outq.queue = NULL; | |
46 | outq.nr = outq.alloc = 0; | |
47 | ||
48 | for (i = 0; pathspec[i]; i++) | |
49 | ; | |
50 | speccnt = i; | |
7e4a2a84 JH |
51 | if (!speccnt) |
52 | return; | |
53 | ||
6b14d7fa JH |
54 | spec = xmalloc(sizeof(*spec) * speccnt); |
55 | for (i = 0; pathspec[i]; i++) { | |
56 | spec[i].spec = pathspec[i]; | |
1d9e6f92 | 57 | spec[i].len = strlen(pathspec[i]); |
6b14d7fa JH |
58 | } |
59 | ||
60 | for (i = 0; i < q->nr; i++) { | |
61 | struct diff_filepair *p = q->queue[i]; | |
847941fd | 62 | if (matches_pathspec(p->two->path, spec, speccnt)) |
6b14d7fa JH |
63 | diff_q(&outq, p); |
64 | else | |
226406f6 | 65 | diff_free_filepair(p); |
6b14d7fa JH |
66 | } |
67 | free(q->queue); | |
68 | *q = outq; | |
69 | return; | |
70 | } |