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
9struct path_spec {
10 const char *spec;
11 int len;
12};
13
14static 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[i].len;
25 if (! strncmp(s[i].spec, name, len) &&
26 len <= namelen &&
27 (name[len] == 0 || name[len] == '/'))
28 return 1;
29 }
30 return 0;
31}
32
33void 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 int l;
49 spec[i].spec = pathspec[i];
50 l = strlen(pathspec[i]);
51 while (l > 0 && pathspec[i][l-1] == '/')
52 l--;
53 spec[i].len = l;
54 }
55
56 for (i = 0; i < q->nr; i++) {
57 struct diff_filepair *p = q->queue[i];
58 if (matches_pathspec(p->one->path, spec, speccnt) ||
59 matches_pathspec(p->two->path, spec, speccnt))
60 diff_q(&outq, p);
61 else
62 free(p);
63 }
64 free(q->queue);
65 *q = outq;
66 return;
67}