blob: 68fe009132d1e900138a9d61bb7c658e23d9b76d [file] [log] [blame]
Junio C Hamano6b14d7f2005-05-22 10:04:37 -07001/*
2 * Copyright (C) 2005 Junio C Hamano
3 */
4#include "cache.h"
5#include "diff.h"
6#include "diffcore.h"
Junio C Hamano6b14d7f2005-05-22 10:04:37 -07007
8struct path_spec {
9 const char *spec;
10 int len;
11};
12
13static 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++) {
Junio C Hamanof4d89f22005-05-24 23:35:38 -070023 int len = s[i].len;
Linus Torvalds1d9e6f92005-05-31 15:17:58 -070024 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] == '/')
Junio C Hamano6b14d7f2005-05-22 10:04:37 -070031 return 1;
Linus Torvaldsf3327262005-08-16 20:44:32 -070032 if (!len)
33 return 1;
Junio C Hamano6b14d7f2005-05-22 10:04:37 -070034 }
35 return 0;
36}
37
38void 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;
51 spec = xmalloc(sizeof(*spec) * speccnt);
52 for (i = 0; pathspec[i]; i++) {
53 spec[i].spec = pathspec[i];
Linus Torvalds1d9e6f92005-05-31 15:17:58 -070054 spec[i].len = strlen(pathspec[i]);
Junio C Hamano6b14d7f2005-05-22 10:04:37 -070055 }
56
57 for (i = 0; i < q->nr; i++) {
58 struct diff_filepair *p = q->queue[i];
Junio C Hamano847941f2005-05-27 15:51:52 -070059 if (matches_pathspec(p->two->path, spec, speccnt))
Junio C Hamano6b14d7f2005-05-22 10:04:37 -070060 diff_q(&outq, p);
61 else
Junio C Hamano226406f2005-05-27 15:50:30 -070062 diff_free_filepair(p);
Junio C Hamano6b14d7f2005-05-22 10:04:37 -070063 }
64 free(q->queue);
65 *q = outq;
66 return;
67}