[PATCH] Rename/copy detection fix.
[git.git] / diffcore-pathspec.c
1 /*
2  * Copyright (C) 2005 Junio C Hamano
3  */
4 #include "cache.h"
5 #include "diff.h"
6 #include "diffcore.h"
7 #include "delta.h"
8
9 struct path_spec {
10         const char *spec;
11         int len;
12 };
13
14 static int matches_pathspec(const char *name, struct path_spec *s, int cnt)
15 {
16         int i;
17         int namelen;
18
19         if (cnt == 0)
20                 return 1;
21
22         namelen = strlen(name);
23         for (i = 0; i < cnt; i++) {
24                 int len = s->len;
25                 if (! strncmp(s->spec, name, len) &&
26                     len <= namelen &&
27                     (name[len] == 0 || name[len] == '/'))
28                         return 1;
29         }
30         return 0;
31 }
32
33 void diffcore_pathspec(const char **pathspec)
34 {
35         struct diff_queue_struct *q = &diff_queued_diff;
36         int i, speccnt;
37         struct diff_queue_struct outq;
38         struct path_spec *spec;
39
40         outq.queue = NULL;
41         outq.nr = outq.alloc = 0;
42
43         for (i = 0; pathspec[i]; i++)
44                 ;
45         speccnt = i;
46         spec = xmalloc(sizeof(*spec) * speccnt);
47         for (i = 0; pathspec[i]; i++) {
48                 spec[i].spec = pathspec[i];
49                 spec[i].len = strlen(pathspec[i]);
50         }
51
52         for (i = 0; i < q->nr; i++) {
53                 struct diff_filepair *p = q->queue[i];
54                 if (matches_pathspec(p->one->path, spec, speccnt) ||
55                     matches_pathspec(p->two->path, spec, speccnt))
56                         diff_q(&outq, p);
57                 else
58                         free(p);
59         }
60         free(q->queue);
61         *q = outq;
62         return;
63 }