blob: 50e46ab863f71574622df4790ea33a8e521f2471 [file] [log] [blame]
Junio C Hamano52e95782005-05-21 02:40:01 -07001/*
2 * Copyright (C) 2005 Junio C Hamano
3 */
4#include "cache.h"
5#include "diff.h"
6#include "diffcore.h"
Junio C Hamano52e95782005-05-21 02:40:01 -07007
Junio C Hamano2002eed2005-07-23 16:35:25 -07008static unsigned int contains(struct diff_filespec *one,
9 const char *needle, unsigned long len)
Junio C Hamano52e95782005-05-21 02:40:01 -070010{
Junio C Hamano2002eed2005-07-23 16:35:25 -070011 unsigned int cnt;
Junio C Hamano52e95782005-05-21 02:40:01 -070012 unsigned long offset, sz;
13 const char *data;
Junio C Hamanof0c6b2a2005-05-27 15:56:38 -070014 if (diff_populate_filespec(one, 0))
Junio C Hamano52e95782005-05-21 02:40:01 -070015 return 0;
Junio C Hamano2002eed2005-07-23 16:35:25 -070016
Junio C Hamano52e95782005-05-21 02:40:01 -070017 sz = one->size;
18 data = one->data;
Junio C Hamano2002eed2005-07-23 16:35:25 -070019 cnt = 0;
20
21 /* Yes, I've heard of strstr(), but the thing is *data may
22 * not be NUL terminated. Sue me.
23 */
24 for (offset = 0; offset + len <= sz; offset++) {
25 /* we count non-overlapping occurrences of needle */
26 if (!memcmp(needle, data + offset, len)) {
27 offset += len - 1;
28 cnt++;
29 }
30 }
31 return cnt;
Junio C Hamano52e95782005-05-21 02:40:01 -070032}
33
Junio C Hamano367cec12005-05-27 15:55:28 -070034void diffcore_pickaxe(const char *needle, int opts)
Junio C Hamano52e95782005-05-21 02:40:01 -070035{
Junio C Hamano38c6f782005-05-21 19:40:36 -070036 struct diff_queue_struct *q = &diff_queued_diff;
Junio C Hamano52e95782005-05-21 02:40:01 -070037 unsigned long len = strlen(needle);
Junio C Hamano367cec12005-05-27 15:55:28 -070038 int i, has_changes;
Junio C Hamano52e95782005-05-21 02:40:01 -070039 struct diff_queue_struct outq;
40 outq.queue = NULL;
41 outq.nr = outq.alloc = 0;
42
Junio C Hamano367cec12005-05-27 15:55:28 -070043 if (opts & DIFF_PICKAXE_ALL) {
44 /* Showing the whole changeset if needle exists */
45 for (i = has_changes = 0; !has_changes && i < q->nr; i++) {
46 struct diff_filepair *p = q->queue[i];
47 if (!DIFF_FILE_VALID(p->one)) {
48 if (!DIFF_FILE_VALID(p->two))
49 continue; /* ignore unmerged */
50 /* created */
51 if (contains(p->two, needle, len))
52 has_changes++;
53 }
54 else if (!DIFF_FILE_VALID(p->two)) {
55 if (contains(p->one, needle, len))
56 has_changes++;
57 }
58 else if (!diff_unmodified_pair(p) &&
59 contains(p->one, needle, len) !=
60 contains(p->two, needle, len))
61 has_changes++;
Junio C Hamano52e95782005-05-21 02:40:01 -070062 }
Junio C Hamano367cec12005-05-27 15:55:28 -070063 if (has_changes)
64 return; /* not munge the queue */
65
66 /* otherwise we will clear the whole queue
67 * by copying the empty outq at the end of this
68 * function, but first clear the current entries
69 * in the queue.
70 */
71 for (i = 0; i < q->nr; i++)
72 diff_free_filepair(q->queue[i]);
Junio C Hamano52e95782005-05-21 02:40:01 -070073 }
Junio C Hamano367cec12005-05-27 15:55:28 -070074 else
75 /* Showing only the filepairs that has the needle */
76 for (i = 0; i < q->nr; i++) {
77 struct diff_filepair *p = q->queue[i];
78 has_changes = 0;
79 if (!DIFF_FILE_VALID(p->one)) {
80 if (!DIFF_FILE_VALID(p->two))
81 ; /* ignore unmerged */
82 /* created */
83 else if (contains(p->two, needle, len))
84 has_changes = 1;
85 }
86 else if (!DIFF_FILE_VALID(p->two)) {
87 if (contains(p->one, needle, len))
88 has_changes = 1;
89 }
90 else if (!diff_unmodified_pair(p) &&
91 contains(p->one, needle, len) !=
92 contains(p->two, needle, len))
93 has_changes = 1;
94
95 if (has_changes)
96 diff_q(&outq, p);
97 else
98 diff_free_filepair(p);
99 }
100
Junio C Hamano52e95782005-05-21 02:40:01 -0700101 free(q->queue);
102 *q = outq;
103 return;
104}