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