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