[PATCH] Performance fix for pickaxe.
[git/dscho.git] / diffcore-pathspec.c
blob4b7adc396a04076dbdd27ab5b9c0d7f420a2986c
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"
9 struct path_spec {
10 const char *spec;
11 int len;
14 static int matches_pathspec(const char *name, struct path_spec *s, int cnt)
16 int i;
17 int namelen;
19 if (cnt == 0)
20 return 1;
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;
30 return 0;
33 void diffcore_pathspec(const char **pathspec)
35 struct diff_queue_struct *q = &diff_queued_diff;
36 int i, speccnt;
37 struct diff_queue_struct outq;
38 struct path_spec *spec;
40 outq.queue = NULL;
41 outq.nr = outq.alloc = 0;
43 for (i = 0; pathspec[i]; i++)
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]);
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);
60 free(q->queue);
61 *q = outq;
62 return;