t7501: add tests for --include and --only
[git/gitster.git] / diffcore-delta.c
blob4927ab8fb0c91554b849e3046e424cccc7eff325
1 #include "git-compat-util.h"
2 #include "diffcore.h"
4 /*
5 * Idea here is very simple.
7 * Almost all data we are interested in are text, but sometimes we have
8 * to deal with binary data. So we cut them into chunks delimited by
9 * LF byte, or 64-byte sequence, whichever comes first, and hash them.
11 * For those chunks, if the source buffer has more instances of it
12 * than the destination buffer, that means the difference are the
13 * number of bytes not copied from source to destination. If the
14 * counts are the same, everything was copied from source to
15 * destination. If the destination has more, everything was copied,
16 * and destination added more.
18 * We are doing an approximation so we do not really have to waste
19 * memory by actually storing the sequence. We just hash them into
20 * somewhere around 2^16 hashbuckets and count the occurrences.
23 /* Wild guess at the initial hash size */
24 #define INITIAL_HASH_SIZE 9
26 /* We leave more room in smaller hash but do not let it
27 * grow to have unused hole too much.
29 #define INITIAL_FREE(sz_log2) ((1<<(sz_log2))*(sz_log2-3)/(sz_log2))
31 /* A prime rather carefully chosen between 2^16..2^17, so that
32 * HASHBASE < INITIAL_FREE(17). We want to keep the maximum hashtable
33 * size under the current 2<<17 maximum, which can hold this many
34 * different values before overflowing to hashtable of size 2<<18.
36 #define HASHBASE 107927
38 struct spanhash {
39 unsigned int hashval;
40 unsigned int cnt;
42 struct spanhash_top {
43 int alloc_log2;
44 int free;
45 struct spanhash data[FLEX_ARRAY];
48 static struct spanhash_top *spanhash_rehash(struct spanhash_top *orig)
50 struct spanhash_top *new_spanhash;
51 int i;
52 int osz = 1 << orig->alloc_log2;
53 int sz = osz << 1;
55 new_spanhash = xmalloc(st_add(sizeof(*orig),
56 st_mult(sizeof(struct spanhash), sz)));
57 new_spanhash->alloc_log2 = orig->alloc_log2 + 1;
58 new_spanhash->free = INITIAL_FREE(new_spanhash->alloc_log2);
59 memset(new_spanhash->data, 0, sizeof(struct spanhash) * sz);
60 for (i = 0; i < osz; i++) {
61 struct spanhash *o = &(orig->data[i]);
62 int bucket;
63 if (!o->cnt)
64 continue;
65 bucket = o->hashval & (sz - 1);
66 while (1) {
67 struct spanhash *h = &(new_spanhash->data[bucket++]);
68 if (!h->cnt) {
69 h->hashval = o->hashval;
70 h->cnt = o->cnt;
71 new_spanhash->free--;
72 break;
74 if (sz <= bucket)
75 bucket = 0;
78 free(orig);
79 return new_spanhash;
82 static struct spanhash_top *add_spanhash(struct spanhash_top *top,
83 unsigned int hashval, int cnt)
85 int bucket, lim;
86 struct spanhash *h;
88 lim = (1 << top->alloc_log2);
89 bucket = hashval & (lim - 1);
90 while (1) {
91 h = &(top->data[bucket++]);
92 if (!h->cnt) {
93 h->hashval = hashval;
94 h->cnt = cnt;
95 top->free--;
96 if (top->free < 0)
97 return spanhash_rehash(top);
98 return top;
100 if (h->hashval == hashval) {
101 h->cnt += cnt;
102 return top;
104 if (lim <= bucket)
105 bucket = 0;
109 static int spanhash_cmp(const void *a_, const void *b_)
111 const struct spanhash *a = a_;
112 const struct spanhash *b = b_;
114 /* A count of zero compares at the end.. */
115 if (!a->cnt)
116 return !b->cnt ? 0 : 1;
117 if (!b->cnt)
118 return -1;
119 return a->hashval < b->hashval ? -1 :
120 a->hashval > b->hashval ? 1 : 0;
123 static struct spanhash_top *hash_chars(struct repository *r,
124 struct diff_filespec *one)
126 int i, n;
127 unsigned int accum1, accum2, hashval;
128 struct spanhash_top *hash;
129 unsigned char *buf = one->data;
130 unsigned int sz = one->size;
131 int is_text = !diff_filespec_is_binary(r, one);
133 i = INITIAL_HASH_SIZE;
134 hash = xmalloc(st_add(sizeof(*hash),
135 st_mult(sizeof(struct spanhash), (size_t)1 << i)));
136 hash->alloc_log2 = i;
137 hash->free = INITIAL_FREE(i);
138 memset(hash->data, 0, sizeof(struct spanhash) * ((size_t)1 << i));
140 n = 0;
141 accum1 = accum2 = 0;
142 while (sz) {
143 unsigned int c = *buf++;
144 unsigned int old_1 = accum1;
145 sz--;
147 /* Ignore CR in CRLF sequence if text */
148 if (is_text && c == '\r' && sz && *buf == '\n')
149 continue;
151 accum1 = (accum1 << 7) ^ (accum2 >> 25);
152 accum2 = (accum2 << 7) ^ (old_1 >> 25);
153 accum1 += c;
154 if (++n < 64 && c != '\n')
155 continue;
156 hashval = (accum1 + accum2 * 0x61) % HASHBASE;
157 hash = add_spanhash(hash, hashval, n);
158 n = 0;
159 accum1 = accum2 = 0;
161 QSORT(hash->data, (size_t)1ul << hash->alloc_log2, spanhash_cmp);
162 return hash;
165 int diffcore_count_changes(struct repository *r,
166 struct diff_filespec *src,
167 struct diff_filespec *dst,
168 void **src_count_p,
169 void **dst_count_p,
170 unsigned long *src_copied,
171 unsigned long *literal_added)
173 struct spanhash *s, *d;
174 struct spanhash_top *src_count, *dst_count;
175 unsigned long sc, la;
177 src_count = dst_count = NULL;
178 if (src_count_p)
179 src_count = *src_count_p;
180 if (!src_count) {
181 src_count = hash_chars(r, src);
182 if (src_count_p)
183 *src_count_p = src_count;
185 if (dst_count_p)
186 dst_count = *dst_count_p;
187 if (!dst_count) {
188 dst_count = hash_chars(r, dst);
189 if (dst_count_p)
190 *dst_count_p = dst_count;
192 sc = la = 0;
194 s = src_count->data;
195 d = dst_count->data;
196 for (;;) {
197 unsigned dst_cnt, src_cnt;
198 if (!s->cnt)
199 break; /* we checked all in src */
200 while (d->cnt) {
201 if (d->hashval >= s->hashval)
202 break;
203 la += d->cnt;
204 d++;
206 src_cnt = s->cnt;
207 dst_cnt = 0;
208 if (d->cnt && d->hashval == s->hashval) {
209 dst_cnt = d->cnt;
210 d++;
212 if (src_cnt < dst_cnt) {
213 la += dst_cnt - src_cnt;
214 sc += src_cnt;
216 else
217 sc += dst_cnt;
218 s++;
220 while (d->cnt) {
221 la += d->cnt;
222 d++;
225 if (!src_count_p)
226 free(src_count);
227 if (!dst_count_p)
228 free(dst_count);
229 *src_copied = sc;
230 *literal_added = la;
231 return 0;