1/* 2 * Copyright (C) 2005 Junio C Hamano 3 */ 4#include "cache.h" 5#include "diff.h" 6#include "diffcore.h" 7#include "delta.h" 8#include "count-delta.h" 9 10static int should_break(struct diff_filespec *src, 11 struct diff_filespec *dst, 12 int break_score, 13 int *merge_score_p) 14{ 15 /* dst is recorded as a modification of src. Are they so 16 * different that we are better off recording this as a pair 17 * of delete and create? 18 * 19 * There are two criteria used in this algorithm. For the 20 * purposes of helping later rename/copy, we take both delete 21 * and insert into account and estimate the amount of "edit". 22 * If the edit is very large, we break this pair so that 23 * rename/copy can pick the pieces up to match with other 24 * files. 25 * 26 * On the other hand, we would want to ignore inserts for the 27 * pure "complete rewrite" detection. As long as most of the 28 * existing contents were removed from the file, it is a 29 * complete rewrite, and if sizable chunk from the original 30 * still remains in the result, it is not a rewrite. It does 31 * not matter how much or how little new material is added to 32 * the file. 33 * 34 * The score we leave for such a broken filepair uses the 35 * latter definition so that later clean-up stage can find the 36 * pieces that should not have been broken according to the 37 * latter definition after rename/copy runs, and merge the 38 * broken pair that have a score lower than given criteria 39 * back together. The break operation itself happens 40 * according to the former definition. 41 * 42 * The minimum_edit parameter tells us when to break (the 43 * amount of "edit" required for us to consider breaking the 44 * pair). We leave the amount of deletion in *merge_score_p 45 * when we return. 46 * 47 * The value we return is 1 if we want the pair to be broken, 48 * or 0 if we do not. 49 */ 50 void *delta; 51 unsigned long delta_size, base_size, src_copied, literal_added; 52 int to_break = 0; 53 54 *merge_score_p = 0; /* assume no deletion --- "do not break" 55 * is the default. 56 */ 57 58 if (!S_ISREG(src->mode) || !S_ISREG(dst->mode)) 59 return 0; /* leave symlink rename alone */ 60 61 if (diff_populate_filespec(src, 0) || diff_populate_filespec(dst, 0)) 62 return 0; /* error but caught downstream */ 63 64 base_size = ((src->size < dst->size) ? src->size : dst->size); 65 66 delta = diff_delta(src->data, src->size, 67 dst->data, dst->size, 68 &delta_size, 0); 69 if (!delta) 70 return 0; /* error but caught downstream */ 71 72 /* Estimate the edit size by interpreting delta. */ 73 if (count_delta(delta, delta_size, 74 &src_copied, &literal_added)) { 75 free(delta); 76 return 0; /* we cannot tell */ 77 } 78 free(delta); 79 80 /* Compute merge-score, which is "how much is removed 81 * from the source material". The clean-up stage will 82 * merge the surviving pair together if the score is 83 * less than the minimum, after rename/copy runs. 84 */ 85 if (src->size <= src_copied) 86 ; /* all copied, nothing removed */ 87 else { 88 delta_size = src->size - src_copied; 89 *merge_score_p = delta_size * MAX_SCORE / src->size; 90 } 91 92 /* Extent of damage, which counts both inserts and 93 * deletes. 94 */ 95 if (src->size + literal_added <= src_copied) 96 delta_size = 0; /* avoid wrapping around */ 97 else 98 delta_size = (src->size - src_copied) + literal_added; 99 100 /* We break if the edit exceeds the minimum. 101 * i.e. (break_score / MAX_SCORE < delta_size / base_size) 102 */ 103 if (break_score * base_size < delta_size * MAX_SCORE) 104 to_break = 1; 105 106 return to_break; 107} 108 109void diffcore_break(int break_score) 110{ 111 struct diff_queue_struct *q = &diff_queued_diff; 112 struct diff_queue_struct outq; 113 114 /* When the filepair has this much edit (insert and delete), 115 * it is first considered to be a rewrite and broken into a 116 * create and delete filepair. This is to help breaking a 117 * file that had too much new stuff added, possibly from 118 * moving contents from another file, so that rename/copy can 119 * match it with the other file. 120 * 121 * int break_score; we reuse incoming parameter for this. 122 */ 123 124 /* After a pair is broken according to break_score and 125 * subjected to rename/copy, both of them may survive intact, 126 * due to lack of suitable rename/copy peer. Or, the caller 127 * may be calling us without using rename/copy. When that 128 * happens, we merge the broken pieces back into one 129 * modification together if the pair did not have more than 130 * this much delete. For this computation, we do not take 131 * insert into account at all. If you start from a 100-line 132 * file and delete 97 lines of it, it does not matter if you 133 * add 27 lines to it to make a new 30-line file or if you add 134 * 997 lines to it to make a 1000-line file. Either way what 135 * you did was a rewrite of 97%. On the other hand, if you 136 * delete 3 lines, keeping 97 lines intact, it does not matter 137 * if you add 3 lines to it to make a new 100-line file or if 138 * you add 903 lines to it to make a new 1000-line file. 139 * Either way you did a lot of additions and not a rewrite. 140 * This merge happens to catch the latter case. A merge_score 141 * of 80% would be a good default value (a broken pair that 142 * has score lower than merge_score will be merged back 143 * together). 144 */ 145 int merge_score; 146 int i; 147 148 /* See comment on DEFAULT_BREAK_SCORE and 149 * DEFAULT_MERGE_SCORE in diffcore.h 150 */ 151 merge_score = (break_score >> 16) & 0xFFFF; 152 break_score = (break_score & 0xFFFF); 153 154 if (!break_score) 155 break_score = DEFAULT_BREAK_SCORE; 156 if (!merge_score) 157 merge_score = DEFAULT_MERGE_SCORE; 158 159 outq.nr = outq.alloc = 0; 160 outq.queue = NULL; 161 162 for (i = 0; i < q->nr; i++) { 163 struct diff_filepair *p = q->queue[i]; 164 int score; 165 166 /* We deal only with in-place edit of non directory. 167 * We do not break anything else. 168 */ 169 if (DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two) && 170 !S_ISDIR(p->one->mode) && !S_ISDIR(p->two->mode) && 171 !strcmp(p->one->path, p->two->path)) { 172 if (should_break(p->one, p->two, 173 break_score, &score) && 174 MINIMUM_BREAK_SIZE <= p->one->size) { 175 /* Split this into delete and create */ 176 struct diff_filespec *null_one, *null_two; 177 struct diff_filepair *dp; 178 179 /* Set score to 0 for the pair that 180 * needs to be merged back together 181 * should they survive rename/copy. 182 * Also we do not want to break very 183 * small files. 184 */ 185 if (score < merge_score) 186 score = 0; 187 188 /* deletion of one */ 189 null_one = alloc_filespec(p->one->path); 190 dp = diff_queue(&outq, p->one, null_one); 191 dp->score = score; 192 dp->broken_pair = 1; 193 194 /* creation of two */ 195 null_two = alloc_filespec(p->two->path); 196 dp = diff_queue(&outq, null_two, p->two); 197 dp->score = score; 198 dp->broken_pair = 1; 199 200 free(p); /* not diff_free_filepair(), we are 201 * reusing one and two here. 202 */ 203 continue; 204 } 205 } 206 diff_q(&outq, p); 207 } 208 free(q->queue); 209 *q = outq; 210 211 return; 212} 213 214static void merge_broken(struct diff_filepair *p, 215 struct diff_filepair *pp, 216 struct diff_queue_struct *outq) 217{ 218 /* p and pp are broken pairs we want to merge */ 219 struct diff_filepair *c = p, *d = pp, *dp; 220 if (DIFF_FILE_VALID(p->one)) { 221 /* this must be a delete half */ 222 d = p; c = pp; 223 } 224 /* Sanity check */ 225 if (!DIFF_FILE_VALID(d->one)) 226 die("internal error in merge #1"); 227 if (DIFF_FILE_VALID(d->two)) 228 die("internal error in merge #2"); 229 if (DIFF_FILE_VALID(c->one)) 230 die("internal error in merge #3"); 231 if (!DIFF_FILE_VALID(c->two)) 232 die("internal error in merge #4"); 233 234 dp = diff_queue(outq, d->one, c->two); 235 dp->score = p->score; 236 diff_free_filespec_data(d->two); 237 diff_free_filespec_data(c->one); 238 free(d); 239 free(c); 240} 241 242void diffcore_merge_broken(void) 243{ 244 struct diff_queue_struct *q = &diff_queued_diff; 245 struct diff_queue_struct outq; 246 int i, j; 247 248 outq.nr = outq.alloc = 0; 249 outq.queue = NULL; 250 251 for (i = 0; i < q->nr; i++) { 252 struct diff_filepair *p = q->queue[i]; 253 if (!p) 254 /* we already merged this with its peer */ 255 continue; 256 else if (p->broken_pair && 257 !strcmp(p->one->path, p->two->path)) { 258 /* If the peer also survived rename/copy, then 259 * we merge them back together. 260 */ 261 for (j = i + 1; j < q->nr; j++) { 262 struct diff_filepair *pp = q->queue[j]; 263 if (pp->broken_pair && 264 !strcmp(pp->one->path, pp->two->path) && 265 !strcmp(p->one->path, pp->two->path)) { 266 /* Peer survived. Merge them */ 267 merge_broken(p, pp, &outq); 268 q->queue[j] = NULL; 269 break; 270 } 271 } 272 if (q->nr <= j) 273 /* The peer did not survive, so we keep 274 * it in the output. 275 */ 276 diff_q(&outq, p); 277 } 278 else 279 diff_q(&outq, p); 280 } 281 free(q->queue); 282 *q = outq; 283 284 return; 285}