Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 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" |
Junio C Hamano | 8597697 | 2005-05-24 12:09:32 -0700 | [diff] [blame] | 8 | #include "count-delta.h" |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 9 | |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 10 | /* Table of rename/copy destinations */ |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 11 | |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 12 | static struct diff_rename_dst { |
| 13 | struct diff_filespec *two; |
| 14 | struct diff_filepair *pair; |
| 15 | } *rename_dst; |
| 16 | static int rename_dst_nr, rename_dst_alloc; |
| 17 | |
| 18 | static struct diff_rename_dst *locate_rename_dst(struct diff_filespec *two, |
| 19 | int insert_ok) |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 20 | { |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 21 | int first, last; |
| 22 | |
| 23 | first = 0; |
| 24 | last = rename_dst_nr; |
| 25 | while (last > first) { |
| 26 | int next = (last + first) >> 1; |
| 27 | struct diff_rename_dst *dst = &(rename_dst[next]); |
| 28 | int cmp = strcmp(two->path, dst->two->path); |
| 29 | if (!cmp) |
| 30 | return dst; |
| 31 | if (cmp < 0) { |
| 32 | last = next; |
| 33 | continue; |
| 34 | } |
| 35 | first = next+1; |
| 36 | } |
| 37 | /* not found */ |
| 38 | if (!insert_ok) |
| 39 | return NULL; |
| 40 | /* insert to make it at "first" */ |
| 41 | if (rename_dst_alloc <= rename_dst_nr) { |
| 42 | rename_dst_alloc = alloc_nr(rename_dst_alloc); |
| 43 | rename_dst = xrealloc(rename_dst, |
| 44 | rename_dst_alloc * sizeof(*rename_dst)); |
| 45 | } |
| 46 | rename_dst_nr++; |
| 47 | if (first < rename_dst_nr) |
| 48 | memmove(rename_dst + first + 1, rename_dst + first, |
| 49 | (rename_dst_nr - first - 1) * sizeof(*rename_dst)); |
Junio C Hamano | 5098baf | 2005-09-15 16:13:43 -0700 | [diff] [blame] | 50 | rename_dst[first].two = alloc_filespec(two->path); |
| 51 | fill_filespec(rename_dst[first].two, two->sha1, two->mode); |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 52 | rename_dst[first].pair = NULL; |
| 53 | return &(rename_dst[first]); |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Junio C Hamano | 15d061b | 2005-05-27 15:55:55 -0700 | [diff] [blame] | 56 | /* Table of rename/copy src files */ |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 57 | static struct diff_rename_src { |
| 58 | struct diff_filespec *one; |
Junio C Hamano | 6bac10d | 2005-09-10 12:42:32 -0700 | [diff] [blame] | 59 | unsigned src_path_left : 1; |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 60 | } *rename_src; |
| 61 | static int rename_src_nr, rename_src_alloc; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 62 | |
Junio C Hamano | 15d061b | 2005-05-27 15:55:55 -0700 | [diff] [blame] | 63 | static struct diff_rename_src *register_rename_src(struct diff_filespec *one, |
Junio C Hamano | 6bac10d | 2005-09-10 12:42:32 -0700 | [diff] [blame] | 64 | int src_path_left) |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 65 | { |
| 66 | int first, last; |
| 67 | |
| 68 | first = 0; |
| 69 | last = rename_src_nr; |
| 70 | while (last > first) { |
| 71 | int next = (last + first) >> 1; |
| 72 | struct diff_rename_src *src = &(rename_src[next]); |
| 73 | int cmp = strcmp(one->path, src->one->path); |
| 74 | if (!cmp) |
| 75 | return src; |
| 76 | if (cmp < 0) { |
| 77 | last = next; |
| 78 | continue; |
| 79 | } |
| 80 | first = next+1; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 81 | } |
Junio C Hamano | 15d061b | 2005-05-27 15:55:55 -0700 | [diff] [blame] | 82 | |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 83 | /* insert to make it at "first" */ |
| 84 | if (rename_src_alloc <= rename_src_nr) { |
| 85 | rename_src_alloc = alloc_nr(rename_src_alloc); |
| 86 | rename_src = xrealloc(rename_src, |
| 87 | rename_src_alloc * sizeof(*rename_src)); |
| 88 | } |
| 89 | rename_src_nr++; |
| 90 | if (first < rename_src_nr) |
| 91 | memmove(rename_src + first + 1, rename_src + first, |
| 92 | (rename_src_nr - first - 1) * sizeof(*rename_src)); |
| 93 | rename_src[first].one = one; |
Junio C Hamano | 6bac10d | 2005-09-10 12:42:32 -0700 | [diff] [blame] | 94 | rename_src[first].src_path_left = src_path_left; |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 95 | return &(rename_src[first]); |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static int is_exact_match(struct diff_filespec *src, struct diff_filespec *dst) |
| 99 | { |
| 100 | if (src->sha1_valid && dst->sha1_valid && |
| 101 | !memcmp(src->sha1, dst->sha1, 20)) |
| 102 | return 1; |
Junio C Hamano | f0c6b2a | 2005-05-27 15:56:38 -0700 | [diff] [blame] | 103 | if (diff_populate_filespec(src, 1) || diff_populate_filespec(dst, 1)) |
| 104 | return 0; |
| 105 | if (src->size != dst->size) |
| 106 | return 0; |
| 107 | if (diff_populate_filespec(src, 0) || diff_populate_filespec(dst, 0)) |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 108 | return 0; |
| 109 | if (src->size == dst->size && |
| 110 | !memcmp(src->data, dst->data, src->size)) |
| 111 | return 1; |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | struct diff_score { |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 116 | int src; /* index in rename_src */ |
| 117 | int dst; /* index in rename_dst */ |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 118 | int score; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | static int estimate_similarity(struct diff_filespec *src, |
| 122 | struct diff_filespec *dst, |
| 123 | int minimum_score) |
| 124 | { |
| 125 | /* src points at a file that existed in the original tree (or |
| 126 | * optionally a file in the destination tree) and dst points |
| 127 | * at a newly created file. They may be quite similar, in which |
| 128 | * case we want to say src is renamed to dst or src is copied into |
| 129 | * dst, and then some edit has been applied to dst. |
| 130 | * |
| 131 | * Compare them and return how similar they are, representing |
Junio C Hamano | f0c6b2a | 2005-05-27 15:56:38 -0700 | [diff] [blame] | 132 | * the score as an integer between 0 and MAX_SCORE. |
| 133 | * |
| 134 | * When there is an exact match, it is considered a better |
| 135 | * match than anything else; the destination does not even |
| 136 | * call into this function in that case. |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 137 | */ |
| 138 | void *delta; |
Junio C Hamano | 355e76a | 2005-06-03 01:36:03 -0700 | [diff] [blame] | 139 | unsigned long delta_size, base_size, src_copied, literal_added; |
Junio C Hamano | 75c660a | 2005-06-28 16:58:27 -0700 | [diff] [blame] | 140 | unsigned long delta_limit; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 141 | int score; |
| 142 | |
Junio C Hamano | 60896c7 | 2005-05-22 21:24:49 -0700 | [diff] [blame] | 143 | /* We deal only with regular files. Symlink renames are handled |
| 144 | * only when they are exact matches --- in other words, no edits |
| 145 | * after renaming. |
| 146 | */ |
| 147 | if (!S_ISREG(src->mode) || !S_ISREG(dst->mode)) |
| 148 | return 0; |
| 149 | |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 150 | delta_size = ((src->size < dst->size) ? |
| 151 | (dst->size - src->size) : (src->size - dst->size)); |
Junio C Hamano | 58b103f | 2005-05-21 15:55:18 -0700 | [diff] [blame] | 152 | base_size = ((src->size < dst->size) ? src->size : dst->size); |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 153 | |
Junio C Hamano | 58b103f | 2005-05-21 15:55:18 -0700 | [diff] [blame] | 154 | /* We would not consider edits that change the file size so |
| 155 | * drastically. delta_size must be smaller than |
Junio C Hamano | cd1870e | 2005-05-22 01:31:28 -0700 | [diff] [blame] | 156 | * (MAX_SCORE-minimum_score)/MAX_SCORE * min(src->size, dst->size). |
Junio C Hamano | f0c6b2a | 2005-05-27 15:56:38 -0700 | [diff] [blame] | 157 | * |
Junio C Hamano | 58b103f | 2005-05-21 15:55:18 -0700 | [diff] [blame] | 158 | * Note that base_size == 0 case is handled here already |
| 159 | * and the final score computation below would not have a |
| 160 | * divide-by-zero issue. |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 161 | */ |
Junio C Hamano | cd1870e | 2005-05-22 01:31:28 -0700 | [diff] [blame] | 162 | if (base_size * (MAX_SCORE-minimum_score) < delta_size * MAX_SCORE) |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 163 | return 0; |
| 164 | |
Junio C Hamano | f0c6b2a | 2005-05-27 15:56:38 -0700 | [diff] [blame] | 165 | if (diff_populate_filespec(src, 0) || diff_populate_filespec(dst, 0)) |
| 166 | return 0; /* error but caught downstream */ |
| 167 | |
Junio C Hamano | 75c660a | 2005-06-28 16:58:27 -0700 | [diff] [blame] | 168 | delta_limit = base_size * (MAX_SCORE-minimum_score) / MAX_SCORE; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 169 | delta = diff_delta(src->data, src->size, |
| 170 | dst->data, dst->size, |
Junio C Hamano | 75c660a | 2005-06-28 16:58:27 -0700 | [diff] [blame] | 171 | &delta_size, delta_limit); |
| 172 | if (!delta) |
| 173 | /* If delta_limit is exceeded, we have too much differences */ |
| 174 | return 0; |
Junio C Hamano | 8597697 | 2005-05-24 12:09:32 -0700 | [diff] [blame] | 175 | |
| 176 | /* A delta that has a lot of literal additions would have |
| 177 | * big delta_size no matter what else it does. |
Junio C Hamano | 58b103f | 2005-05-21 15:55:18 -0700 | [diff] [blame] | 178 | */ |
Junio C Hamano | a00d7d1 | 2005-05-27 15:49:54 -0700 | [diff] [blame] | 179 | if (base_size * (MAX_SCORE-minimum_score) < delta_size * MAX_SCORE) |
Junio C Hamano | 8597697 | 2005-05-24 12:09:32 -0700 | [diff] [blame] | 180 | return 0; |
| 181 | |
| 182 | /* Estimate the edit size by interpreting delta. */ |
Junio C Hamano | 355e76a | 2005-06-03 01:36:03 -0700 | [diff] [blame] | 183 | if (count_delta(delta, delta_size, &src_copied, &literal_added)) { |
| 184 | free(delta); |
Junio C Hamano | 8597697 | 2005-05-24 12:09:32 -0700 | [diff] [blame] | 185 | return 0; |
Junio C Hamano | 355e76a | 2005-06-03 01:36:03 -0700 | [diff] [blame] | 186 | } |
| 187 | free(delta); |
| 188 | |
| 189 | /* Extent of damage */ |
| 190 | if (src->size + literal_added < src_copied) |
| 191 | delta_size = 0; |
| 192 | else |
| 193 | delta_size = (src->size - src_copied) + literal_added; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 194 | |
Junio C Hamano | 58b103f | 2005-05-21 15:55:18 -0700 | [diff] [blame] | 195 | /* |
| 196 | * Now we will give some score to it. 100% edit gets 0 points |
| 197 | * and 0% edit gets MAX_SCORE points. |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 198 | */ |
Junio C Hamano | 58b103f | 2005-05-21 15:55:18 -0700 | [diff] [blame] | 199 | score = MAX_SCORE - (MAX_SCORE * delta_size / base_size); |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 200 | if (score < 0) return 0; |
| 201 | if (MAX_SCORE < score) return MAX_SCORE; |
| 202 | return score; |
| 203 | } |
| 204 | |
Junio C Hamano | 5098baf | 2005-09-15 16:13:43 -0700 | [diff] [blame] | 205 | static void record_rename_pair(int dst_index, int src_index, int score) |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 206 | { |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 207 | struct diff_filespec *one, *two, *src, *dst; |
| 208 | struct diff_filepair *dp; |
Junio C Hamano | f7c1512 | 2005-05-22 21:26:09 -0700 | [diff] [blame] | 209 | |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 210 | if (rename_dst[dst_index].pair) |
| 211 | die("internal error: dst already matched."); |
| 212 | |
| 213 | src = rename_src[src_index].one; |
| 214 | one = alloc_filespec(src->path); |
| 215 | fill_filespec(one, src->sha1, src->mode); |
| 216 | |
| 217 | dst = rename_dst[dst_index].two; |
| 218 | two = alloc_filespec(dst->path); |
| 219 | fill_filespec(two, dst->sha1, dst->mode); |
| 220 | |
Junio C Hamano | 5098baf | 2005-09-15 16:13:43 -0700 | [diff] [blame] | 221 | dp = diff_queue(NULL, one, two); |
Junio C Hamano | 01c4e70 | 2005-05-29 16:56:48 -0700 | [diff] [blame] | 222 | dp->score = score; |
Junio C Hamano | 6bac10d | 2005-09-10 12:42:32 -0700 | [diff] [blame] | 223 | dp->source_stays = rename_src[src_index].src_path_left; |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 224 | rename_dst[dst_index].pair = dp; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | /* |
| 228 | * We sort the rename similarity matrix with the score, in descending |
Junio C Hamano | 15d061b | 2005-05-27 15:55:55 -0700 | [diff] [blame] | 229 | * order (the most similar first). |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 230 | */ |
| 231 | static int score_compare(const void *a_, const void *b_) |
| 232 | { |
| 233 | const struct diff_score *a = a_, *b = b_; |
| 234 | return b->score - a->score; |
| 235 | } |
| 236 | |
Junio C Hamano | 6bac10d | 2005-09-10 12:42:32 -0700 | [diff] [blame] | 237 | static int compute_stays(struct diff_queue_struct *q, |
| 238 | struct diff_filespec *one) |
| 239 | { |
| 240 | int i; |
| 241 | for (i = 0; i < q->nr; i++) { |
| 242 | struct diff_filepair *p = q->queue[i]; |
| 243 | if (strcmp(one->path, p->two->path)) |
| 244 | continue; |
| 245 | if (DIFF_PAIR_RENAME(p)) { |
| 246 | return 0; /* something else is renamed into this */ |
| 247 | } |
| 248 | } |
| 249 | return 1; |
| 250 | } |
| 251 | |
Junio C Hamano | 8082d8d | 2005-09-21 00:18:27 -0700 | [diff] [blame] | 252 | void diffcore_rename(struct diff_options *options) |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 253 | { |
Junio C Hamano | 8082d8d | 2005-09-21 00:18:27 -0700 | [diff] [blame] | 254 | int detect_rename = options->detect_rename; |
| 255 | int minimum_score = options->rename_score; |
| 256 | int rename_limit = options->rename_limit; |
Junio C Hamano | 38c6f78 | 2005-05-21 19:40:36 -0700 | [diff] [blame] | 257 | struct diff_queue_struct *q = &diff_queued_diff; |
Junio C Hamano | 5098baf | 2005-09-15 16:13:43 -0700 | [diff] [blame] | 258 | struct diff_queue_struct outq; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 259 | struct diff_score *mx; |
Junio C Hamano | 5098baf | 2005-09-15 16:13:43 -0700 | [diff] [blame] | 260 | int i, j, rename_count; |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 261 | int num_create, num_src, dst_cnt; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 262 | |
Junio C Hamano | 26dee0a | 2005-05-21 23:33:32 -0700 | [diff] [blame] | 263 | if (!minimum_score) |
Junio C Hamano | f345b0a | 2005-05-30 00:08:37 -0700 | [diff] [blame] | 264 | minimum_score = DEFAULT_RENAME_SCORE; |
Junio C Hamano | 5098baf | 2005-09-15 16:13:43 -0700 | [diff] [blame] | 265 | rename_count = 0; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 266 | |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 267 | for (i = 0; i < q->nr; i++) { |
Junio C Hamano | 52e9578 | 2005-05-21 02:40:01 -0700 | [diff] [blame] | 268 | struct diff_filepair *p = q->queue[i]; |
Junio C Hamano | 81e50ea | 2005-05-21 19:42:18 -0700 | [diff] [blame] | 269 | if (!DIFF_FILE_VALID(p->one)) |
| 270 | if (!DIFF_FILE_VALID(p->two)) |
Junio C Hamano | 60896c7 | 2005-05-22 21:24:49 -0700 | [diff] [blame] | 271 | continue; /* unmerged */ |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 272 | else |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 273 | locate_rename_dst(p->two, 1); |
Junio C Hamano | 2210100 | 2005-06-11 20:55:20 -0700 | [diff] [blame] | 274 | else if (!DIFF_FILE_VALID(p->two)) { |
| 275 | /* If the source is a broken "delete", and |
| 276 | * they did not really want to get broken, |
| 277 | * that means the source actually stays. |
| 278 | */ |
| 279 | int stays = (p->broken_pair && !p->score); |
| 280 | register_rename_src(p->one, stays); |
| 281 | } |
Junio C Hamano | 15d061b | 2005-05-27 15:55:55 -0700 | [diff] [blame] | 282 | else if (detect_rename == DIFF_DETECT_COPY) |
| 283 | register_rename_src(p->one, 1); |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 284 | } |
Junio C Hamano | 8082d8d | 2005-09-21 00:18:27 -0700 | [diff] [blame] | 285 | if (rename_dst_nr == 0 || |
Junio C Hamano | 3299c6f | 2005-11-15 12:48:08 -0800 | [diff] [blame] | 286 | (0 < rename_limit && rename_limit < rename_dst_nr)) |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 287 | goto cleanup; /* nothing to do */ |
| 288 | |
| 289 | /* We really want to cull the candidates list early |
| 290 | * with cheap tests in order to avoid doing deltas. |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 291 | */ |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 292 | for (i = 0; i < rename_dst_nr; i++) { |
| 293 | struct diff_filespec *two = rename_dst[i].two; |
| 294 | for (j = 0; j < rename_src_nr; j++) { |
| 295 | struct diff_filespec *one = rename_src[j].one; |
| 296 | if (!is_exact_match(one, two)) |
| 297 | continue; |
Junio C Hamano | 5098baf | 2005-09-15 16:13:43 -0700 | [diff] [blame] | 298 | record_rename_pair(i, j, MAX_SCORE); |
| 299 | rename_count++; |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 300 | break; /* we are done with this entry */ |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 301 | } |
| 302 | } |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 303 | |
| 304 | /* Have we run out the created file pool? If so we can avoid |
| 305 | * doing the delta matrix altogether. |
| 306 | */ |
Junio C Hamano | 5098baf | 2005-09-15 16:13:43 -0700 | [diff] [blame] | 307 | if (rename_count == rename_dst_nr) |
Junio C Hamano | 15d061b | 2005-05-27 15:55:55 -0700 | [diff] [blame] | 308 | goto cleanup; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 309 | |
Junio C Hamano | 9f70b80 | 2005-11-21 12:18:23 -0800 | [diff] [blame] | 310 | if (minimum_score == MAX_SCORE) |
| 311 | goto cleanup; |
| 312 | |
Junio C Hamano | 5098baf | 2005-09-15 16:13:43 -0700 | [diff] [blame] | 313 | num_create = (rename_dst_nr - rename_count); |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 314 | num_src = rename_src_nr; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 315 | mx = xmalloc(sizeof(*mx) * num_create * num_src); |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 316 | for (dst_cnt = i = 0; i < rename_dst_nr; i++) { |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 317 | int base = dst_cnt * num_src; |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 318 | struct diff_filespec *two = rename_dst[i].two; |
| 319 | if (rename_dst[i].pair) |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 320 | continue; /* dealt with exact match already. */ |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 321 | for (j = 0; j < rename_src_nr; j++) { |
| 322 | struct diff_filespec *one = rename_src[j].one; |
| 323 | struct diff_score *m = &mx[base+j]; |
| 324 | m->src = j; |
| 325 | m->dst = i; |
| 326 | m->score = estimate_similarity(one, two, |
| 327 | minimum_score); |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 328 | } |
| 329 | dst_cnt++; |
| 330 | } |
| 331 | /* cost matrix sorted by most to least similar pair */ |
| 332 | qsort(mx, num_create * num_src, sizeof(*mx), score_compare); |
| 333 | for (i = 0; i < num_create * num_src; i++) { |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 334 | struct diff_rename_dst *dst = &rename_dst[mx[i].dst]; |
| 335 | if (dst->pair) |
| 336 | continue; /* already done, either exact or fuzzy. */ |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 337 | if (mx[i].score < minimum_score) |
Junio C Hamano | 15d061b | 2005-05-27 15:55:55 -0700 | [diff] [blame] | 338 | break; /* there is no more usable pair. */ |
Junio C Hamano | 5098baf | 2005-09-15 16:13:43 -0700 | [diff] [blame] | 339 | record_rename_pair(mx[i].dst, mx[i].src, mx[i].score); |
| 340 | rename_count++; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 341 | } |
| 342 | free(mx); |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 343 | |
Junio C Hamano | 15d061b | 2005-05-27 15:55:55 -0700 | [diff] [blame] | 344 | cleanup: |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 345 | /* At this point, we have found some renames and copies and they |
Junio C Hamano | 5098baf | 2005-09-15 16:13:43 -0700 | [diff] [blame] | 346 | * are recorded in rename_dst. The original list is still in *q. |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 347 | */ |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 348 | outq.queue = NULL; |
| 349 | outq.nr = outq.alloc = 0; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 350 | for (i = 0; i < q->nr; i++) { |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 351 | struct diff_filepair *p = q->queue[i]; |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 352 | struct diff_filepair *pair_to_free = NULL; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 353 | |
Junio C Hamano | 2cd6888 | 2005-05-30 00:08:07 -0700 | [diff] [blame] | 354 | if (!DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two)) { |
| 355 | /* |
| 356 | * Creation |
| 357 | * |
| 358 | * We would output this create record if it has |
| 359 | * not been turned into a rename/copy already. |
| 360 | */ |
| 361 | struct diff_rename_dst *dst = |
| 362 | locate_rename_dst(p->two, 0); |
| 363 | if (dst && dst->pair) { |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 364 | diff_q(&outq, dst->pair); |
| 365 | pair_to_free = p; |
| 366 | } |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 367 | else |
Junio C Hamano | 2cd6888 | 2005-05-30 00:08:07 -0700 | [diff] [blame] | 368 | /* no matching rename/copy source, so |
| 369 | * record this as a creation. |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 370 | */ |
| 371 | diff_q(&outq, p); |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 372 | } |
Junio C Hamano | 2cd6888 | 2005-05-30 00:08:07 -0700 | [diff] [blame] | 373 | else if (DIFF_FILE_VALID(p->one) && !DIFF_FILE_VALID(p->two)) { |
| 374 | /* |
| 375 | * Deletion |
| 376 | * |
Junio C Hamano | f345b0a | 2005-05-30 00:08:37 -0700 | [diff] [blame] | 377 | * We would output this delete record if: |
| 378 | * |
| 379 | * (1) this is a broken delete and the counterpart |
| 380 | * broken create remains in the output; or |
Junio C Hamano | 5098baf | 2005-09-15 16:13:43 -0700 | [diff] [blame] | 381 | * (2) this is not a broken delete, and rename_dst |
| 382 | * does not have a rename/copy to move p->one->path |
| 383 | * out of existence. |
Junio C Hamano | f345b0a | 2005-05-30 00:08:37 -0700 | [diff] [blame] | 384 | * |
| 385 | * Otherwise, the counterpart broken create |
| 386 | * has been turned into a rename-edit; or |
| 387 | * delete did not have a matching create to |
| 388 | * begin with. |
Junio C Hamano | 2cd6888 | 2005-05-30 00:08:07 -0700 | [diff] [blame] | 389 | */ |
Junio C Hamano | f345b0a | 2005-05-30 00:08:37 -0700 | [diff] [blame] | 390 | if (DIFF_PAIR_BROKEN(p)) { |
| 391 | /* broken delete */ |
| 392 | struct diff_rename_dst *dst = |
| 393 | locate_rename_dst(p->one, 0); |
| 394 | if (dst && dst->pair) |
| 395 | /* counterpart is now rename/copy */ |
| 396 | pair_to_free = p; |
| 397 | } |
| 398 | else { |
Junio C Hamano | 5098baf | 2005-09-15 16:13:43 -0700 | [diff] [blame] | 399 | for (j = 0; j < rename_dst_nr; j++) { |
| 400 | if (!rename_dst[j].pair) |
| 401 | continue; |
| 402 | if (strcmp(rename_dst[j].pair-> |
| 403 | one->path, |
| 404 | p->one->path)) |
| 405 | continue; |
| 406 | break; |
| 407 | } |
| 408 | if (j < rename_dst_nr) |
Junio C Hamano | f345b0a | 2005-05-30 00:08:37 -0700 | [diff] [blame] | 409 | /* this path remains */ |
| 410 | pair_to_free = p; |
| 411 | } |
Junio C Hamano | 2cd6888 | 2005-05-30 00:08:07 -0700 | [diff] [blame] | 412 | |
| 413 | if (pair_to_free) |
| 414 | ; |
| 415 | else |
| 416 | diff_q(&outq, p); |
| 417 | } |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 418 | else if (!diff_unmodified_pair(p)) |
Junio C Hamano | 15d061b | 2005-05-27 15:55:55 -0700 | [diff] [blame] | 419 | /* all the usual ones need to be kept */ |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 420 | diff_q(&outq, p); |
Junio C Hamano | 15d061b | 2005-05-27 15:55:55 -0700 | [diff] [blame] | 421 | else |
| 422 | /* no need to keep unmodified pairs */ |
| 423 | pair_to_free = p; |
| 424 | |
Junio C Hamano | 226406f | 2005-05-27 15:50:30 -0700 | [diff] [blame] | 425 | if (pair_to_free) |
| 426 | diff_free_filepair(pair_to_free); |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 427 | } |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 428 | diff_debug_queue("done copying original", &outq); |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 429 | |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 430 | free(q->queue); |
| 431 | *q = outq; |
| 432 | diff_debug_queue("done collapsing", q); |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 433 | |
Junio C Hamano | 6bac10d | 2005-09-10 12:42:32 -0700 | [diff] [blame] | 434 | /* We need to see which rename source really stays here; |
| 435 | * earlier we only checked if the path is left in the result, |
| 436 | * but even if a path remains in the result, if that is coming |
| 437 | * from copying something else on top of it, then the original |
| 438 | * source is lost and does not stay. |
| 439 | */ |
| 440 | for (i = 0; i < q->nr; i++) { |
| 441 | struct diff_filepair *p = q->queue[i]; |
| 442 | if (DIFF_PAIR_RENAME(p) && p->source_stays) { |
| 443 | /* If one appears as the target of a rename-copy, |
| 444 | * then mark p->source_stays = 0; otherwise |
| 445 | * leave it as is. |
| 446 | */ |
| 447 | p->source_stays = compute_stays(q, p->one); |
| 448 | } |
| 449 | } |
| 450 | |
Junio C Hamano | 5098baf | 2005-09-15 16:13:43 -0700 | [diff] [blame] | 451 | for (i = 0; i < rename_dst_nr; i++) { |
| 452 | diff_free_filespec_data(rename_dst[i].two); |
| 453 | free(rename_dst[i].two); |
| 454 | } |
| 455 | |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 456 | free(rename_dst); |
| 457 | rename_dst = NULL; |
| 458 | rename_dst_nr = rename_dst_alloc = 0; |
| 459 | free(rename_src); |
| 460 | rename_src = NULL; |
| 461 | rename_src_nr = rename_src_alloc = 0; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 462 | return; |
| 463 | } |