[PATCH] Finish initial cut of git-pack-object/git-unpack-object pair.
[git/git-svn.git] / diffcore-pickaxe.c
blob4c26b422f9d4ea16581bc44cf8a2ce4f5586a7f1
1 /*
2 * Copyright (C) 2005 Junio C Hamano
3 */
4 #include "cache.h"
5 #include "diff.h"
6 #include "diffcore.h"
8 static int contains(struct diff_filespec *one,
9 const char *needle, unsigned long len)
11 unsigned long offset, sz;
12 const char *data;
13 if (diff_populate_filespec(one, 0))
14 return 0;
15 sz = one->size;
16 data = one->data;
17 for (offset = 0; offset + len <= sz; offset++)
18 if (!strncmp(needle, data + offset, len))
19 return 1;
20 return 0;
23 void diffcore_pickaxe(const char *needle, int opts)
25 struct diff_queue_struct *q = &diff_queued_diff;
26 unsigned long len = strlen(needle);
27 int i, has_changes;
28 struct diff_queue_struct outq;
29 outq.queue = NULL;
30 outq.nr = outq.alloc = 0;
32 if (opts & DIFF_PICKAXE_ALL) {
33 /* Showing the whole changeset if needle exists */
34 for (i = has_changes = 0; !has_changes && i < q->nr; i++) {
35 struct diff_filepair *p = q->queue[i];
36 if (!DIFF_FILE_VALID(p->one)) {
37 if (!DIFF_FILE_VALID(p->two))
38 continue; /* ignore unmerged */
39 /* created */
40 if (contains(p->two, needle, len))
41 has_changes++;
43 else if (!DIFF_FILE_VALID(p->two)) {
44 if (contains(p->one, needle, len))
45 has_changes++;
47 else if (!diff_unmodified_pair(p) &&
48 contains(p->one, needle, len) !=
49 contains(p->two, needle, len))
50 has_changes++;
52 if (has_changes)
53 return; /* not munge the queue */
55 /* otherwise we will clear the whole queue
56 * by copying the empty outq at the end of this
57 * function, but first clear the current entries
58 * in the queue.
60 for (i = 0; i < q->nr; i++)
61 diff_free_filepair(q->queue[i]);
63 else
64 /* Showing only the filepairs that has the needle */
65 for (i = 0; i < q->nr; i++) {
66 struct diff_filepair *p = q->queue[i];
67 has_changes = 0;
68 if (!DIFF_FILE_VALID(p->one)) {
69 if (!DIFF_FILE_VALID(p->two))
70 ; /* ignore unmerged */
71 /* created */
72 else if (contains(p->two, needle, len))
73 has_changes = 1;
75 else if (!DIFF_FILE_VALID(p->two)) {
76 if (contains(p->one, needle, len))
77 has_changes = 1;
79 else if (!diff_unmodified_pair(p) &&
80 contains(p->one, needle, len) !=
81 contains(p->two, needle, len))
82 has_changes = 1;
84 if (has_changes)
85 diff_q(&outq, p);
86 else
87 diff_free_filepair(p);
90 free(q->queue);
91 *q = outq;
92 return;