4
#include "cache.h"
5
#include "diff.h"
6
#include "diffcore.h"
7
+#include "hash.h"
8
9
/* Table of rename/copy destinations */
10
94
return &(rename_src[first]);
95
}
96
96
-static int is_exact_match(struct diff_filespec *src,
97
- struct diff_filespec *dst,
98
- int contents_too)
99
-{
100
- if (src->sha1_valid && dst->sha1_valid &&
101
- !hashcmp(src->sha1, dst->sha1))
102
- return 1;
103
- if (!contents_too)
104
- return 0;
105
- if (diff_populate_filespec(src, 1) || diff_populate_filespec(dst, 1))
106
- return 0;
107
- if (src->size != dst->size)
108
- return 0;
109
- if (src->sha1_valid && dst->sha1_valid)
110
- return !hashcmp(src->sha1, dst->sha1);
111
- if (diff_populate_filespec(src, 0) || diff_populate_filespec(dst, 0))
112
- return 0;
113
- if (src->size == dst->size &&
114
- !memcmp(src->data, dst->data, src->size))
115
- return 1;
116
- return 0;
117
-}
118
-
97
static int basename_same(struct diff_filespec *src, struct diff_filespec *dst)
98
{
99
int src_len = strlen(src->path), dst_len = strlen(dst->path);
220
return b->score - a->score;
221
}
222
223
+struct file_similarity {
224
+ int src_dst, index;
225
+ struct diff_filespec *filespec;
226
+ struct file_similarity *next;
227
+};
228
+
229
+static int find_identical_files(struct file_similarity *src,
230
+ struct file_similarity *dst)
231
+{
232
+ int renames = 0;
233
+
234
+ /*
235
+ * Walk over all the destinations ...
236
+ */
237
+ do {
238
+ struct diff_filespec *one = dst->filespec;
239
+ struct file_similarity *p, *best;
240
+ int i = 100;
241
+
242
+ /*
243
+ * .. to find the best source match
244
+ */
245
+ best = NULL;
246
+ for (p = src; p; p = p->next) {
247
+ struct diff_filespec *two = p->filespec;
248
+
249
+ /* False hash collission? */
250
+ if (hashcmp(one->sha1, two->sha1))
251
+ continue;
252
+ /* Non-regular files? If so, the modes must match! */
253
+ if (!S_ISREG(one->mode) || !S_ISREG(two->mode)) {
254
+ if (one->mode != two->mode)
255
+ continue;
256
+ }
257
+ best = p;
258
+ if (basename_same(one, two))
259
+ break;
260
+
261
+ /* Too many identical alternatives? Pick one */
262
+ if (!--i)
263
+ break;
264
+ }
265
+ if (best) {
266
+ record_rename_pair(dst->index, best->index, MAX_SCORE);
267
+ renames++;
268
+ }
269
+ } while ((dst = dst->next) != NULL);
270
+ return renames;
271
+}
272
+
273
+/*
274
+ * Note: the rest of the rename logic depends on this
275
+ * phase also populating all the filespecs for any
276
+ * entry that isn't matched up with an exact rename.
277
+ */
278
+static void free_similarity_list(struct file_similarity *p)
279
+{
280
+ while (p) {
281
+ struct file_similarity *entry = p;
282
+ p = p->next;
283
+
284
+ /* Stupid special case, see note above! */
285
+ diff_populate_filespec(entry->filespec, 0);
286
+ free(entry);
287
+ }
288
+}
289
+
290
+static int find_same_files(void *ptr)
291
+{
292
+ int ret;
293
+ struct file_similarity *p = ptr;
294
+ struct file_similarity *src = NULL, *dst = NULL;
295
+
296
+ /* Split the hash list up into sources and destinations */
297
+ do {
298
+ struct file_similarity *entry = p;
299
+ p = p->next;
300
+ if (entry->src_dst < 0) {
301
+ entry->next = src;
302
+ src = entry;
303
+ } else {
304
+ entry->next = dst;
305
+ dst = entry;
306
+ }
307
+ } while (p);
308
+
309
+ /*
310
+ * If we have both sources *and* destinations, see if
311
+ * we can match them up
312
+ */
313
+ ret = (src && dst) ? find_identical_files(src, dst) : 0;
314
+
315
+ /* Free the hashes and return the number of renames found */
316
+ free_similarity_list(src);
317
+ free_similarity_list(dst);
318
+ return ret;
319
+}
320
+
321
+static unsigned int hash_filespec(struct diff_filespec *filespec)
322
+{
323
+ unsigned int hash;
324
+ if (!filespec->sha1_valid) {
325
+ if (diff_populate_filespec(filespec, 0))
326
+ return 0;
327
+ hash_sha1_file(filespec->data, filespec->size, "blob", filespec->sha1);
328
+ }
329
+ memcpy(&hash, filespec->sha1, sizeof(hash));
330
+ return hash;
331
+}
332
+
333
+static void insert_file_table(struct hash_table *table, int src_dst, int index, struct diff_filespec *filespec)
334
+{
335
+ void **pos;
336
+ unsigned int hash;
337
+ struct file_similarity *entry = xmalloc(sizeof(*entry));
338
+
339
+ entry->src_dst = src_dst;
340
+ entry->index = index;
341
+ entry->filespec = filespec;
342
+ entry->next = NULL;
343
+
344
+ hash = hash_filespec(filespec);
345
+ pos = insert_hash(hash, entry, table);
346
+
347
+ /* We already had an entry there? */
348
+ if (pos) {
349
+ entry->next = *pos;
350
+ *pos = entry;
351
+ }
352
+}
353
+
354
/*
355
* Find exact renames first.
356
*
357
* The first round matches up the up-to-date entries,
358
* and then during the second round we try to match
359
* cache-dirty entries as well.
251
- *
252
- * Note: the rest of the rename logic depends on this
253
- * phase also populating all the filespecs for any
254
- * entry that isn't matched up with an exact rename,
255
- * see "is_exact_match()".
360
*/
361
static int find_exact_renames(void)
362
{
259
- int rename_count = 0;
260
- int contents_too;
261
-
262
- for (contents_too = 0; contents_too < 2; contents_too++) {
263
- int i;
264
-
265
- for (i = 0; i < rename_dst_nr; i++) {
266
- struct diff_filespec *two = rename_dst[i].two;
267
- int j;
268
-
269
- if (rename_dst[i].pair)
270
- continue; /* dealt with an earlier round */
271
- for (j = 0; j < rename_src_nr; j++) {
272
- int k;
273
- struct diff_filespec *one = rename_src[j].one;
274
- if (!is_exact_match(one, two, contents_too))
275
- continue;
363
+ int i;
364
+ struct hash_table file_table;
365
277
- /* see if there is a basename match, too */
278
- for (k = j; k < rename_src_nr; k++) {
279
- one = rename_src[k].one;
280
- if (basename_same(one, two) &&
281
- is_exact_match(one, two,
282
- contents_too)) {
283
- j = k;
284
- break;
285
- }
286
- }
287
-
288
- record_rename_pair(i, j, (int)MAX_SCORE);
289
- rename_count++;
290
- break; /* we are done with this entry */
291
- }
292
- }
293
- }
294
- return rename_count;
366
+ init_hash(&file_table);
367
+ for (i = 0; i < rename_src_nr; i++)
368
+ insert_file_table(&file_table, -1, i, rename_src[i].one);
369
+
370
+ for (i = 0; i < rename_dst_nr; i++)
371
+ insert_file_table(&file_table, 1, i, rename_dst[i].two);
372
+
373
+ /* Find the renames */
374
+ i = for_each_hash(&file_table, find_same_files);
375
+
376
+ /* .. and free the hash data structure */
377
+ free_hash(&file_table);
378
+
379
+ return i;
380
}
381
382
void diffcore_rename(struct diff_options *options)