diffcore-rename.c 10.4 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * Copyright (C) 2005 Junio C Hamano
 */
#include "cache.h"
#include "diff.h"
#include "diffcore.h"
#include "delta.h"

9 10 11 12 13 14 15
/* Table of rename/copy destinations */

static struct diff_rename_dst {
	struct diff_filespec *two;
	struct diff_filepair *pair;
} *rename_dst;
static int rename_dst_nr, rename_dst_alloc;
16

17 18
static struct diff_rename_dst *locate_rename_dst(struct diff_filespec *two,
						 int insert_ok)
19
{
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
	int first, last;

	first = 0;
	last = rename_dst_nr;
	while (last > first) {
		int next = (last + first) >> 1;
		struct diff_rename_dst *dst = &(rename_dst[next]);
		int cmp = strcmp(two->path, dst->two->path);
		if (!cmp)
			return dst;
		if (cmp < 0) {
			last = next;
			continue;
		}
		first = next+1;
	}
	/* not found */
	if (!insert_ok)
		return NULL;
	/* insert to make it at "first" */
	if (rename_dst_alloc <= rename_dst_nr) {
		rename_dst_alloc = alloc_nr(rename_dst_alloc);
		rename_dst = xrealloc(rename_dst,
				      rename_dst_alloc * sizeof(*rename_dst));
	}
	rename_dst_nr++;
	if (first < rename_dst_nr)
		memmove(rename_dst + first + 1, rename_dst + first,
			(rename_dst_nr - first - 1) * sizeof(*rename_dst));
	rename_dst[first].two = two;
	rename_dst[first].pair = NULL;
	return &(rename_dst[first]);
52 53
}

54 55 56 57 58
static struct diff_rename_src {
	struct diff_filespec *one;
	unsigned src_used : 1;
} *rename_src;
static int rename_src_nr, rename_src_alloc;
59

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
static struct diff_rename_src *locate_rename_src(struct diff_filespec *one,
						 int insert_ok)
{
	int first, last;

	first = 0;
	last = rename_src_nr;
	while (last > first) {
		int next = (last + first) >> 1;
		struct diff_rename_src *src = &(rename_src[next]);
		int cmp = strcmp(one->path, src->one->path);
		if (!cmp)
			return src;
		if (cmp < 0) {
			last = next;
			continue;
		}
		first = next+1;
	}
	/* not found */
	if (!insert_ok)
		return NULL;
	/* insert to make it at "first" */
	if (rename_src_alloc <= rename_src_nr) {
		rename_src_alloc = alloc_nr(rename_src_alloc);
		rename_src = xrealloc(rename_src,
				      rename_src_alloc * sizeof(*rename_src));
87
	}
88 89 90 91 92 93 94
	rename_src_nr++;
	if (first < rename_src_nr)
		memmove(rename_src + first + 1, rename_src + first,
			(rename_src_nr - first - 1) * sizeof(*rename_src));
	rename_src[first].one = one;
	rename_src[first].src_used = 0;
	return &(rename_src[first]);
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
}

static int is_exact_match(struct diff_filespec *src, struct diff_filespec *dst)
{
	if (src->sha1_valid && dst->sha1_valid &&
	    !memcmp(src->sha1, dst->sha1, 20))
		return 1;
	if (diff_populate_filespec(src) || diff_populate_filespec(dst))
		/* this is an error but will be caught downstream */
		return 0;
	if (src->size == dst->size &&
	    !memcmp(src->data, dst->data, src->size))
		return 1;
	return 0;
}

struct diff_score {
112 113
	int src; /* index in rename_src */
	int dst; /* index in rename_dst */
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
	int score;
	int rank;
};

static int estimate_similarity(struct diff_filespec *src,
			       struct diff_filespec *dst,
			       int minimum_score)
{
	/* src points at a file that existed in the original tree (or
	 * optionally a file in the destination tree) and dst points
	 * at a newly created file.  They may be quite similar, in which
	 * case we want to say src is renamed to dst or src is copied into
	 * dst, and then some edit has been applied to dst.
	 *
	 * Compare them and return how similar they are, representing
	 * the score as an integer between 0 and 10000, except
	 * where they match exactly it is considered better than anything
	 * else.
	 */
	void *delta;
134
	unsigned long delta_size, base_size;
135 136
	int score;

137 138 139 140 141 142 143
	/* We deal only with regular files.  Symlink renames are handled
	 * only when they are exact matches --- in other words, no edits
	 * after renaming.
	 */
	if (!S_ISREG(src->mode) || !S_ISREG(dst->mode))
		return 0;

144 145
	delta_size = ((src->size < dst->size) ?
		      (dst->size - src->size) : (src->size - dst->size));
146
	base_size = ((src->size < dst->size) ? src->size : dst->size);
147

148 149
	/* We would not consider edits that change the file size so
	 * drastically.  delta_size must be smaller than
150
	 * (MAX_SCORE-minimum_score)/MAX_SCORE * min(src->size, dst->size).
151 152 153
	 * Note that base_size == 0 case is handled here already
	 * and the final score computation below would not have a
	 * divide-by-zero issue.
154
	 */
155
	if (base_size * (MAX_SCORE-minimum_score) < delta_size * MAX_SCORE)
156 157 158 159 160
		return 0;

	delta = diff_delta(src->data, src->size,
			   dst->data, dst->size,
			   &delta_size);
161 162 163 164 165 166
	/*
	 * We currently punt here, but we may later end up parsing the
	 * delta to really assess the extent of damage.  A big consecutive
	 * remove would produce small delta_size that affects quite a
	 * big portion of the file.
	 */
167 168
	free(delta);

169 170 171
	/*
	 * Now we will give some score to it.  100% edit gets 0 points
	 * and 0% edit gets MAX_SCORE points.
172
	 */
173
	score = MAX_SCORE - (MAX_SCORE * delta_size / base_size); 
174 175 176 177 178
	if (score < 0) return 0;
	if (MAX_SCORE < score) return MAX_SCORE;
	return score;
}

179 180
static void record_rename_pair(struct diff_queue_struct *renq,
			       int dst_index, int src_index, int score)
181
{
182 183
	struct diff_filespec *one, *two, *src, *dst;
	struct diff_filepair *dp;
184

185 186
	if (rename_dst[dst_index].pair)
		die("internal error: dst already matched.");
187

188 189 190
	src = rename_src[src_index].one;
	one = alloc_filespec(src->path);
	fill_filespec(one, src->sha1, src->mode);
191

192 193 194
	dst = rename_dst[dst_index].two;
	two = alloc_filespec(dst->path);
	fill_filespec(two, dst->sha1, dst->mode);
195

196 197
	dp = diff_queue(renq, one, two);
	dp->score = score;
198

199 200
	rename_src[src_index].src_used = 1;
	rename_dst[dst_index].pair = dp;
201 202 203 204 205 206 207 208 209 210 211 212
}

/*
 * We sort the rename similarity matrix with the score, in descending
 * order (more similar first).
 */
static int score_compare(const void *a_, const void *b_)
{
	const struct diff_score *a = a_, *b = b_;
	return b->score - a->score;
}

J
Junio C Hamano 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
int diff_scoreopt_parse(const char *opt)
{
	int diglen, num, scale, i;
	if (opt[0] != '-' || (opt[1] != 'M' && opt[1] != 'C'))
		return -1; /* that is not a -M nor -C option */
	diglen = strspn(opt+2, "0123456789");
	if (diglen == 0 || strlen(opt+2) != diglen)
		return 0; /* use default */
	sscanf(opt+2, "%d", &num);
	for (i = 0, scale = 1; i < diglen; i++)
		scale *= 10;

	/* user says num divided by scale and we say internally that
	 * is MAX_SCORE * num / scale.
	 */
	return MAX_SCORE * num / scale;
}

void diffcore_rename(int detect_rename, int minimum_score)
232
{
233
	struct diff_queue_struct *q = &diff_queued_diff;
234
	struct diff_queue_struct renq, outq;
235
	struct diff_score *mx;
236 237
	int i, j;
	int num_create, num_src, dst_cnt;
238

239 240
	if (!minimum_score)
		minimum_score = DEFAULT_MINIMUM_SCORE;
241 242
	renq.queue = NULL;
	renq.nr = renq.alloc = 0;
243 244

	for (i = 0; i < q->nr; i++) {
245
		struct diff_filepair *p = q->queue[i];
246 247
		if (!DIFF_FILE_VALID(p->one))
			if (!DIFF_FILE_VALID(p->two))
248
				continue; /* unmerged */
249
			else
250
				locate_rename_dst(p->two, 1);
251
		else if (!DIFF_FILE_VALID(p->two))
252
			locate_rename_src(p->one, 1);
253
		else if (1 < detect_rename) /* find copy, too */
254
			locate_rename_src(p->one, 1);
255
	}
256
	if (rename_dst_nr == 0)
257 258 259 260 261
		goto cleanup; /* nothing to do */

	/* We really want to cull the candidates list early
	 * with cheap tests in order to avoid doing deltas.
	 */
262 263 264 265 266 267 268 269
	for (i = 0; i < rename_dst_nr; i++) {
		struct diff_filespec *two = rename_dst[i].two;
		for (j = 0; j < rename_src_nr; j++) {
			struct diff_filespec *one = rename_src[j].one;
			if (!is_exact_match(one, two))
				continue;
			record_rename_pair(&renq, i, j, MAX_SCORE);
			break; /* we are done with this entry */
270 271
		}
	}
272
	diff_debug_queue("done detecting exact", &renq);
273 274 275 276

	/* Have we run out the created file pool?  If so we can avoid
	 * doing the delta matrix altogether.
	 */
277
	if (renq.nr == rename_dst_nr)
278 279
		goto flush_rest;

280 281
	num_create = (rename_dst_nr - renq.nr);
	num_src = rename_src_nr;
282
	mx = xmalloc(sizeof(*mx) * num_create * num_src);
283
	for (dst_cnt = i = 0; i < rename_dst_nr; i++) {
284
		int base = dst_cnt * num_src;
285 286
		struct diff_filespec *two = rename_dst[i].two;
		if (rename_dst[i].pair)
287
			continue; /* dealt with exact match already. */
288 289 290 291 292 293 294
		for (j = 0; j < rename_src_nr; j++) {
			struct diff_filespec *one = rename_src[j].one;
			struct diff_score *m = &mx[base+j];
			m->src = j;
			m->dst = i;
			m->score = estimate_similarity(one, two,
						       minimum_score);
295 296 297 298 299 300
		}
		dst_cnt++;
	}
	/* cost matrix sorted by most to least similar pair */
	qsort(mx, num_create * num_src, sizeof(*mx), score_compare);
	for (i = 0; i < num_create * num_src; i++) {
301 302 303
		struct diff_rename_dst *dst = &rename_dst[mx[i].dst];
		if (dst->pair)
			continue; /* already done, either exact or fuzzy. */
304
		if (mx[i].score < minimum_score)
305
			break; /* there is not any more diffs applicable. */
306
		record_rename_pair(&renq, mx[i].dst, mx[i].src, mx[i].score);
307 308
	}
	free(mx);
309
	diff_debug_queue("done detecting fuzzy", &renq);
310 311 312

 flush_rest:
	/* At this point, we have found some renames and copies and they
313
	 * are kept in renq.  The original list is still in *q.
314 315 316 317 318 319
	 *
	 * Scan the original list and move them into the outq; we will sort
	 * outq and swap it into the queue supplied to pass that to
	 * downstream, so we assign the sort keys in this loop.
	 *
	 * See comments at the top of record_rename_pair for numbers used
320
	 * to assign rename_rank.
321
	 */
322 323
	outq.queue = NULL;
	outq.nr = outq.alloc = 0;
324
	for (i = 0; i < q->nr; i++) {
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
		struct diff_filepair *p = q->queue[i];
		struct diff_rename_src *src = locate_rename_src(p->one, 0);
		struct diff_rename_dst *dst = locate_rename_dst(p->two, 0);
		struct diff_filepair *pair_to_free = NULL;

		if (dst) {
			/* creation */
			if (dst->pair) {
				/* renq has rename/copy already to produce
				 * this file, so we do not emit the creation
				 * record in the output.
				 */
				diff_q(&outq, dst->pair);
				pair_to_free = p;
			}
			else
				/* no matching rename/copy source, so record
				 * this as a creation.
				 */
				diff_q(&outq, p);
345
		}
346 347 348
		else if (!diff_unmodified_pair(p))
			/* all the other cases need to be recorded as is */
			diff_q(&outq, p);
349
		else {
350 351 352 353 354
			/* unmodified pair needs to be recorded only if
			 * it is used as the source of rename/copy
			 */
			if (src && src->src_used)
				diff_q(&outq, p);
355
			else
356
				pair_to_free = p;
357
		}
358 359 360 361
		if (pair_to_free) {
			diff_free_filespec_data(pair_to_free->one);
			diff_free_filespec_data(pair_to_free->two);
			free(pair_to_free);
362 363
		}
	}
364
	diff_debug_queue("done copying original", &outq);
365

366 367 368 369
	free(renq.queue);
	free(q->queue);
	*q = outq;
	diff_debug_queue("done collapsing", q);
370 371

 cleanup:
372 373 374 375 376 377
	free(rename_dst);
	rename_dst = NULL;
	rename_dst_nr = rename_dst_alloc = 0;
	free(rename_src);
	rename_src = NULL;
	rename_src_nr = rename_src_alloc = 0;
378 379
	return;
}