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