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