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