]>
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 JH |
23 | int len = s[i].len; |
24 | if (! strncmp(s[i].spec, name, len) && | |
6b14d7fa JH |
25 | len <= namelen && |
26 | (name[len] == 0 || name[len] == '/')) | |
27 | return 1; | |
28 | } | |
29 | return 0; | |
30 | } | |
31 | ||
32 | void diffcore_pathspec(const char **pathspec) | |
33 | { | |
34 | struct diff_queue_struct *q = &diff_queued_diff; | |
35 | int i, speccnt; | |
36 | struct diff_queue_struct outq; | |
37 | struct path_spec *spec; | |
38 | ||
39 | outq.queue = NULL; | |
40 | outq.nr = outq.alloc = 0; | |
41 | ||
42 | for (i = 0; pathspec[i]; i++) | |
43 | ; | |
44 | speccnt = i; | |
45 | spec = xmalloc(sizeof(*spec) * speccnt); | |
46 | for (i = 0; pathspec[i]; i++) { | |
a9c9cef1 | 47 | int l; |
6b14d7fa | 48 | spec[i].spec = pathspec[i]; |
a9c9cef1 JH |
49 | l = strlen(pathspec[i]); |
50 | while (l > 0 && pathspec[i][l-1] == '/') | |
51 | l--; | |
52 | spec[i].len = l; | |
6b14d7fa JH |
53 | } |
54 | ||
55 | for (i = 0; i < q->nr; i++) { | |
56 | struct diff_filepair *p = q->queue[i]; | |
847941fd | 57 | if (matches_pathspec(p->two->path, spec, speccnt)) |
6b14d7fa JH |
58 | diff_q(&outq, p); |
59 | else | |
226406f6 | 60 | diff_free_filepair(p); |
6b14d7fa JH |
61 | } |
62 | free(q->queue); | |
63 | *q = outq; | |
64 | return; | |
65 | } |