Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 1 | /* |
Jonathan Tan | 95acf11 | 2020-04-07 15:11:43 -0700 | [diff] [blame] | 2 | * |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 3 | * Copyright (C) 2005 Junio C Hamano |
| 4 | */ |
Elijah Newren | fc7bd51 | 2023-02-24 00:09:34 +0000 | [diff] [blame] | 5 | #include "git-compat-util.h" |
Elijah Newren | 36bf195 | 2023-02-24 00:09:24 +0000 | [diff] [blame] | 6 | #include "alloc.h" |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 7 | #include "diff.h" |
| 8 | #include "diffcore.h" |
Stefan Beller | cbd53a2 | 2018-05-15 16:42:15 -0700 | [diff] [blame] | 9 | #include "object-store.h" |
Karsten Blees | f79d9c5 | 2013-11-14 20:20:26 +0100 | [diff] [blame] | 10 | #include "hashmap.h" |
Elijah Newren | fc7bd51 | 2023-02-24 00:09:34 +0000 | [diff] [blame] | 11 | #include "mem-pool.h" |
| 12 | #include "oid-array.h" |
Jeff King | 3ac942d | 2011-02-20 04:51:16 -0500 | [diff] [blame] | 13 | #include "progress.h" |
Jonathan Tan | 95acf11 | 2020-04-07 15:11:43 -0700 | [diff] [blame] | 14 | #include "promisor-remote.h" |
Elijah Newren | fc7bd51 | 2023-02-24 00:09:34 +0000 | [diff] [blame] | 15 | #include "string-list.h" |
Elijah Newren | 9db2ac5 | 2020-12-11 09:08:47 +0000 | [diff] [blame] | 16 | #include "strmap.h" |
Elijah Newren | fc7bd51 | 2023-02-24 00:09:34 +0000 | [diff] [blame] | 17 | #include "trace2.h" |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 18 | |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 19 | /* Table of rename/copy destinations */ |
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 | static struct diff_rename_dst { |
Elijah Newren | 9db2ac5 | 2020-12-11 09:08:47 +0000 | [diff] [blame] | 22 | struct diff_filepair *p; |
| 23 | struct diff_filespec *filespec_to_free; |
| 24 | int is_rename; /* false -> just a create; true -> rename or copy */ |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 25 | } *rename_dst; |
| 26 | static int rename_dst_nr, rename_dst_alloc; |
Elijah Newren | 9db2ac5 | 2020-12-11 09:08:47 +0000 | [diff] [blame] | 27 | /* Mapping from break source pathname to break destination index */ |
| 28 | static struct strintmap *break_idx = NULL; |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 29 | |
Elijah Newren | 9db2ac5 | 2020-12-11 09:08:47 +0000 | [diff] [blame] | 30 | static struct diff_rename_dst *locate_rename_dst(struct diff_filepair *p) |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 31 | { |
Elijah Newren | 9db2ac5 | 2020-12-11 09:08:47 +0000 | [diff] [blame] | 32 | /* Lookup by p->ONE->path */ |
| 33 | int idx = break_idx ? strintmap_get(break_idx, p->one->path) : -1; |
| 34 | return (idx == -1) ? NULL : &rename_dst[idx]; |
Jeff King | f98c2f7 | 2015-02-26 20:39:48 -0500 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | /* |
| 38 | * Returns 0 on success, -1 if we found a duplicate. |
| 39 | */ |
Elijah Newren | 9db2ac5 | 2020-12-11 09:08:47 +0000 | [diff] [blame] | 40 | static int add_rename_dst(struct diff_filepair *p) |
Jeff King | f98c2f7 | 2015-02-26 20:39:48 -0500 | [diff] [blame] | 41 | { |
Dmitry S. Dolzhenko | 337ce24 | 2014-03-04 02:31:54 +0400 | [diff] [blame] | 42 | ALLOC_GROW(rename_dst, rename_dst_nr + 1, rename_dst_alloc); |
Elijah Newren | 9db2ac5 | 2020-12-11 09:08:47 +0000 | [diff] [blame] | 43 | rename_dst[rename_dst_nr].p = p; |
| 44 | rename_dst[rename_dst_nr].filespec_to_free = NULL; |
| 45 | rename_dst[rename_dst_nr].is_rename = 0; |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 46 | rename_dst_nr++; |
Jeff King | f98c2f7 | 2015-02-26 20:39:48 -0500 | [diff] [blame] | 47 | return 0; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Junio C Hamano | 15d061b | 2005-05-27 15:55:55 -0700 | [diff] [blame] | 50 | /* Table of rename/copy src files */ |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 51 | static struct diff_rename_src { |
Junio C Hamano | e88d6bc | 2011-01-06 13:50:05 -0800 | [diff] [blame] | 52 | struct diff_filepair *p; |
Junio C Hamano | fc58071 | 2006-04-08 20:17:46 -0700 | [diff] [blame] | 53 | unsigned short score; /* to remember the break score */ |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 54 | } *rename_src; |
| 55 | static int rename_src_nr, rename_src_alloc; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 56 | |
Elijah Newren | b970b4e | 2020-12-11 09:08:46 +0000 | [diff] [blame] | 57 | static void register_rename_src(struct diff_filepair *p) |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 58 | { |
Elijah Newren | 9db2ac5 | 2020-12-11 09:08:47 +0000 | [diff] [blame] | 59 | if (p->broken_pair) { |
| 60 | if (!break_idx) { |
| 61 | break_idx = xmalloc(sizeof(*break_idx)); |
Elijah Newren | 61bf449 | 2021-06-08 16:11:40 +0000 | [diff] [blame] | 62 | strintmap_init_with_options(break_idx, -1, NULL, 0); |
Elijah Newren | 9db2ac5 | 2020-12-11 09:08:47 +0000 | [diff] [blame] | 63 | } |
| 64 | strintmap_set(break_idx, p->one->path, rename_dst_nr); |
| 65 | } |
| 66 | |
Dmitry S. Dolzhenko | 337ce24 | 2014-03-04 02:31:54 +0400 | [diff] [blame] | 67 | ALLOC_GROW(rename_src, rename_src_nr + 1, rename_src_alloc); |
Elijah Newren | b970b4e | 2020-12-11 09:08:46 +0000 | [diff] [blame] | 68 | rename_src[rename_src_nr].p = p; |
| 69 | rename_src[rename_src_nr].score = p->score; |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 70 | rename_src_nr++; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Johannes Schindelin | 0ce3964 | 2007-06-21 12:52:11 +0100 | [diff] [blame] | 73 | static int basename_same(struct diff_filespec *src, struct diff_filespec *dst) |
| 74 | { |
| 75 | int src_len = strlen(src->path), dst_len = strlen(dst->path); |
| 76 | while (src_len && dst_len) { |
| 77 | char c1 = src->path[--src_len]; |
| 78 | char c2 = dst->path[--dst_len]; |
| 79 | if (c1 != c2) |
| 80 | return 0; |
| 81 | if (c1 == '/') |
| 82 | return 1; |
| 83 | } |
| 84 | return (!src_len || src->path[src_len - 1] == '/') && |
| 85 | (!dst_len || dst->path[dst_len - 1] == '/'); |
| 86 | } |
| 87 | |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 88 | struct diff_score { |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 89 | int src; /* index in rename_src */ |
| 90 | int dst; /* index in rename_dst */ |
Junio C Hamano | 6d24ad9 | 2008-01-29 20:54:56 -0800 | [diff] [blame] | 91 | unsigned short score; |
| 92 | short name_score; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 93 | }; |
| 94 | |
Elijah Newren | 1aedd03 | 2021-06-22 08:04:40 +0000 | [diff] [blame] | 95 | struct inexact_prefetch_options { |
Jonathan Tan | 95acf11 | 2020-04-07 15:11:43 -0700 | [diff] [blame] | 96 | struct repository *repo; |
| 97 | int skip_unmodified; |
| 98 | }; |
Elijah Newren | 1aedd03 | 2021-06-22 08:04:40 +0000 | [diff] [blame] | 99 | static void inexact_prefetch(void *prefetch_options) |
Jonathan Tan | 95acf11 | 2020-04-07 15:11:43 -0700 | [diff] [blame] | 100 | { |
Elijah Newren | 1aedd03 | 2021-06-22 08:04:40 +0000 | [diff] [blame] | 101 | struct inexact_prefetch_options *options = prefetch_options; |
Jonathan Tan | 95acf11 | 2020-04-07 15:11:43 -0700 | [diff] [blame] | 102 | int i; |
| 103 | struct oid_array to_fetch = OID_ARRAY_INIT; |
| 104 | |
| 105 | for (i = 0; i < rename_dst_nr; i++) { |
Elijah Newren | 9db2ac5 | 2020-12-11 09:08:47 +0000 | [diff] [blame] | 106 | if (rename_dst[i].p->renamed_pair) |
Jonathan Tan | 95acf11 | 2020-04-07 15:11:43 -0700 | [diff] [blame] | 107 | /* |
| 108 | * The loop in diffcore_rename() will not need these |
| 109 | * blobs, so skip prefetching. |
| 110 | */ |
| 111 | continue; /* already found exact match */ |
| 112 | diff_add_if_missing(options->repo, &to_fetch, |
Elijah Newren | 9db2ac5 | 2020-12-11 09:08:47 +0000 | [diff] [blame] | 113 | rename_dst[i].p->two); |
Jonathan Tan | 95acf11 | 2020-04-07 15:11:43 -0700 | [diff] [blame] | 114 | } |
| 115 | for (i = 0; i < rename_src_nr; i++) { |
| 116 | if (options->skip_unmodified && |
| 117 | diff_unmodified_pair(rename_src[i].p)) |
| 118 | /* |
| 119 | * The loop in diffcore_rename() will not need these |
| 120 | * blobs, so skip prefetching. |
| 121 | */ |
| 122 | continue; |
| 123 | diff_add_if_missing(options->repo, &to_fetch, |
| 124 | rename_src[i].p->one); |
| 125 | } |
| 126 | promisor_remote_get_direct(options->repo, to_fetch.oid, to_fetch.nr); |
| 127 | oid_array_clear(&to_fetch); |
| 128 | } |
| 129 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 130 | static int estimate_similarity(struct repository *r, |
| 131 | struct diff_filespec *src, |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 132 | struct diff_filespec *dst, |
Jonathan Tan | 95acf11 | 2020-04-07 15:11:43 -0700 | [diff] [blame] | 133 | int minimum_score, |
Elijah Newren | d331dd3 | 2021-06-22 08:04:39 +0000 | [diff] [blame] | 134 | struct diff_populate_filespec_options *dpf_opt) |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 135 | { |
| 136 | /* src points at a file that existed in the original tree (or |
| 137 | * optionally a file in the destination tree) and dst points |
| 138 | * at a newly created file. They may be quite similar, in which |
| 139 | * case we want to say src is renamed to dst or src is copied into |
| 140 | * dst, and then some edit has been applied to dst. |
| 141 | * |
| 142 | * Compare them and return how similar they are, representing |
Junio C Hamano | f0c6b2a | 2005-05-27 15:56:38 -0700 | [diff] [blame] | 143 | * the score as an integer between 0 and MAX_SCORE. |
| 144 | * |
| 145 | * When there is an exact match, it is considered a better |
| 146 | * match than anything else; the destination does not even |
| 147 | * call into this function in that case. |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 148 | */ |
Linus Torvalds | 90bd932 | 2006-03-12 22:26:34 -0800 | [diff] [blame] | 149 | unsigned long max_size, delta_size, base_size, src_copied, literal_added; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 150 | int score; |
| 151 | |
Junio C Hamano | 60896c7 | 2005-05-22 21:24:49 -0700 | [diff] [blame] | 152 | /* We deal only with regular files. Symlink renames are handled |
| 153 | * only when they are exact matches --- in other words, no edits |
| 154 | * after renaming. |
| 155 | */ |
| 156 | if (!S_ISREG(src->mode) || !S_ISREG(dst->mode)) |
| 157 | return 0; |
| 158 | |
Linus Torvalds | 81ac051 | 2007-10-26 16:51:28 -0700 | [diff] [blame] | 159 | /* |
| 160 | * Need to check that source and destination sizes are |
| 161 | * filled in before comparing them. |
| 162 | * |
| 163 | * If we already have "cnt_data" filled in, we know it's |
| 164 | * all good (avoid checking the size for zero, as that |
| 165 | * is a possible size - we really should have a flag to |
| 166 | * say whether the size is valid or not!) |
| 167 | */ |
Elijah Newren | d331dd3 | 2021-06-22 08:04:39 +0000 | [diff] [blame] | 168 | dpf_opt->check_size_only = 1; |
| 169 | |
Nguyễn Thái Ngọc Duy | 8e5dd3d | 2014-08-16 10:08:04 +0700 | [diff] [blame] | 170 | if (!src->cnt_data && |
Elijah Newren | d331dd3 | 2021-06-22 08:04:39 +0000 | [diff] [blame] | 171 | diff_populate_filespec(r, src, dpf_opt)) |
Linus Torvalds | 81ac051 | 2007-10-26 16:51:28 -0700 | [diff] [blame] | 172 | return 0; |
Nguyễn Thái Ngọc Duy | 8e5dd3d | 2014-08-16 10:08:04 +0700 | [diff] [blame] | 173 | if (!dst->cnt_data && |
Elijah Newren | d331dd3 | 2021-06-22 08:04:39 +0000 | [diff] [blame] | 174 | diff_populate_filespec(r, dst, dpf_opt)) |
Linus Torvalds | 81ac051 | 2007-10-26 16:51:28 -0700 | [diff] [blame] | 175 | return 0; |
| 176 | |
Linus Torvalds | 90bd932 | 2006-03-12 22:26:34 -0800 | [diff] [blame] | 177 | max_size = ((src->size > dst->size) ? src->size : dst->size); |
Junio C Hamano | 58b103f | 2005-05-21 15:55:18 -0700 | [diff] [blame] | 178 | base_size = ((src->size < dst->size) ? src->size : dst->size); |
Linus Torvalds | 90bd932 | 2006-03-12 22:26:34 -0800 | [diff] [blame] | 179 | delta_size = max_size - base_size; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 180 | |
Junio C Hamano | 58b103f | 2005-05-21 15:55:18 -0700 | [diff] [blame] | 181 | /* We would not consider edits that change the file size so |
| 182 | * drastically. delta_size must be smaller than |
Junio C Hamano | cd1870e | 2005-05-22 01:31:28 -0700 | [diff] [blame] | 183 | * (MAX_SCORE-minimum_score)/MAX_SCORE * min(src->size, dst->size). |
Junio C Hamano | f0c6b2a | 2005-05-27 15:56:38 -0700 | [diff] [blame] | 184 | * |
Junio C Hamano | 58b103f | 2005-05-21 15:55:18 -0700 | [diff] [blame] | 185 | * Note that base_size == 0 case is handled here already |
| 186 | * and the final score computation below would not have a |
| 187 | * divide-by-zero issue. |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 188 | */ |
Linus Torvalds | 3a4d676 | 2011-02-18 20:12:06 -0800 | [diff] [blame] | 189 | if (max_size * (MAX_SCORE-minimum_score) < delta_size * MAX_SCORE) |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 190 | return 0; |
| 191 | |
Elijah Newren | d331dd3 | 2021-06-22 08:04:39 +0000 | [diff] [blame] | 192 | dpf_opt->check_size_only = 0; |
Jonathan Tan | 95acf11 | 2020-04-07 15:11:43 -0700 | [diff] [blame] | 193 | |
Elijah Newren | d331dd3 | 2021-06-22 08:04:39 +0000 | [diff] [blame] | 194 | if (!src->cnt_data && diff_populate_filespec(r, src, dpf_opt)) |
Björn Steinbrink | 885c716 | 2009-01-20 16:59:57 +0100 | [diff] [blame] | 195 | return 0; |
Elijah Newren | d331dd3 | 2021-06-22 08:04:39 +0000 | [diff] [blame] | 196 | if (!dst->cnt_data && diff_populate_filespec(r, dst, dpf_opt)) |
Björn Steinbrink | 885c716 | 2009-01-20 16:59:57 +0100 | [diff] [blame] | 197 | return 0; |
| 198 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 199 | if (diffcore_count_changes(r, src, dst, |
Junio C Hamano | c06c796 | 2006-03-12 03:22:10 -0800 | [diff] [blame] | 200 | &src->cnt_data, &dst->cnt_data, |
Junio C Hamano | 6541675 | 2006-02-28 16:01:36 -0800 | [diff] [blame] | 201 | &src_copied, &literal_added)) |
Junio C Hamano | 75c660a | 2005-06-28 16:58:27 -0700 | [diff] [blame] | 202 | return 0; |
Junio C Hamano | 8597697 | 2005-05-24 12:09:32 -0700 | [diff] [blame] | 203 | |
Junio C Hamano | 1706306 | 2006-03-02 22:11:25 -0800 | [diff] [blame] | 204 | /* How similar are they? |
| 205 | * what percentage of material in dst are from source? |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 206 | */ |
Linus Torvalds | 90bd932 | 2006-03-12 22:26:34 -0800 | [diff] [blame] | 207 | if (!dst->size) |
Junio C Hamano | 1706306 | 2006-03-02 22:11:25 -0800 | [diff] [blame] | 208 | score = 0; /* should not happen */ |
René Scharfe | cfc0aef | 2007-06-25 00:23:28 +0200 | [diff] [blame] | 209 | else |
Shawn O. Pearce | dc49cd7 | 2007-03-06 20:44:37 -0500 | [diff] [blame] | 210 | score = (int)(src_copied * MAX_SCORE / max_size); |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 211 | return score; |
| 212 | } |
| 213 | |
Junio C Hamano | 5098baf | 2005-09-15 16:13:43 -0700 | [diff] [blame] | 214 | 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] | 215 | { |
Elijah Newren | 9db2ac5 | 2020-12-11 09:08:47 +0000 | [diff] [blame] | 216 | struct diff_filepair *src = rename_src[src_index].p; |
| 217 | struct diff_filepair *dst = rename_dst[dst_index].p; |
Junio C Hamano | f7c1512 | 2005-05-22 21:26:09 -0700 | [diff] [blame] | 218 | |
Elijah Newren | 9db2ac5 | 2020-12-11 09:08:47 +0000 | [diff] [blame] | 219 | if (dst->renamed_pair) |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 220 | die("internal error: dst already matched."); |
| 221 | |
Elijah Newren | 9db2ac5 | 2020-12-11 09:08:47 +0000 | [diff] [blame] | 222 | src->one->rename_used++; |
| 223 | src->one->count++; |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 224 | |
Elijah Newren | 9db2ac5 | 2020-12-11 09:08:47 +0000 | [diff] [blame] | 225 | rename_dst[dst_index].filespec_to_free = dst->one; |
| 226 | rename_dst[dst_index].is_rename = 1; |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 227 | |
Elijah Newren | 9db2ac5 | 2020-12-11 09:08:47 +0000 | [diff] [blame] | 228 | dst->one = src->one; |
| 229 | dst->renamed_pair = 1; |
| 230 | if (!strcmp(dst->one->path, dst->two->path)) |
| 231 | dst->score = rename_src[src_index].score; |
Junio C Hamano | fc58071 | 2006-04-08 20:17:46 -0700 | [diff] [blame] | 232 | else |
Elijah Newren | 9db2ac5 | 2020-12-11 09:08:47 +0000 | [diff] [blame] | 233 | dst->score = score; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | /* |
| 237 | * We sort the rename similarity matrix with the score, in descending |
Junio C Hamano | 15d061b | 2005-05-27 15:55:55 -0700 | [diff] [blame] | 238 | * order (the most similar first). |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 239 | */ |
| 240 | static int score_compare(const void *a_, const void *b_) |
| 241 | { |
| 242 | const struct diff_score *a = a_, *b = b_; |
René Scharfe | cfc0aef | 2007-06-25 00:23:28 +0200 | [diff] [blame] | 243 | |
Junio C Hamano | 6d24ad9 | 2008-01-29 20:54:56 -0800 | [diff] [blame] | 244 | /* sink the unused ones to the bottom */ |
| 245 | if (a->dst < 0) |
| 246 | return (0 <= b->dst); |
| 247 | else if (b->dst < 0) |
| 248 | return -1; |
| 249 | |
René Scharfe | cfc0aef | 2007-06-25 00:23:28 +0200 | [diff] [blame] | 250 | if (a->score == b->score) |
| 251 | return b->name_score - a->name_score; |
| 252 | |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 253 | return b->score - a->score; |
| 254 | } |
| 255 | |
Linus Torvalds | 9027f53 | 2007-10-25 11:23:26 -0700 | [diff] [blame] | 256 | struct file_similarity { |
Karsten Blees | f79d9c5 | 2013-11-14 20:20:26 +0100 | [diff] [blame] | 257 | struct hashmap_entry entry; |
Karsten Blees | 7c85f8a | 2013-11-14 20:19:34 +0100 | [diff] [blame] | 258 | int index; |
Linus Torvalds | 9027f53 | 2007-10-25 11:23:26 -0700 | [diff] [blame] | 259 | struct diff_filespec *filespec; |
Linus Torvalds | 9027f53 | 2007-10-25 11:23:26 -0700 | [diff] [blame] | 260 | }; |
| 261 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 262 | static unsigned int hash_filespec(struct repository *r, |
| 263 | struct diff_filespec *filespec) |
Karsten Blees | 48f6407 | 2013-11-14 20:19:04 +0100 | [diff] [blame] | 264 | { |
brian m. carlson | 41c9560 | 2016-06-24 23:09:24 +0000 | [diff] [blame] | 265 | if (!filespec->oid_valid) { |
Jonathan Tan | 1c37e86 | 2020-04-07 15:11:41 -0700 | [diff] [blame] | 266 | if (diff_populate_filespec(r, filespec, NULL)) |
Karsten Blees | 48f6407 | 2013-11-14 20:19:04 +0100 | [diff] [blame] | 267 | return 0; |
Matheus Tavares | 2dcde20 | 2020-01-30 17:32:22 -0300 | [diff] [blame] | 268 | hash_object_file(r->hash_algo, filespec->data, filespec->size, |
Ævar Arnfjörð Bjarmason | 44439c1 | 2022-02-05 00:48:32 +0100 | [diff] [blame] | 269 | OBJ_BLOB, &filespec->oid); |
Karsten Blees | 48f6407 | 2013-11-14 20:19:04 +0100 | [diff] [blame] | 270 | } |
Jeff King | d40abc8 | 2019-06-20 03:41:49 -0400 | [diff] [blame] | 271 | return oidhash(&filespec->oid); |
Karsten Blees | 48f6407 | 2013-11-14 20:19:04 +0100 | [diff] [blame] | 272 | } |
| 273 | |
Karsten Blees | f79d9c5 | 2013-11-14 20:20:26 +0100 | [diff] [blame] | 274 | static int find_identical_files(struct hashmap *srcs, |
Karsten Blees | 7c85f8a | 2013-11-14 20:19:34 +0100 | [diff] [blame] | 275 | int dst_index, |
Linus Torvalds | 11f944d | 2011-02-18 19:55:19 -0800 | [diff] [blame] | 276 | struct diff_options *options) |
Linus Torvalds | 9027f53 | 2007-10-25 11:23:26 -0700 | [diff] [blame] | 277 | { |
| 278 | int renames = 0; |
Elijah Newren | 9db2ac5 | 2020-12-11 09:08:47 +0000 | [diff] [blame] | 279 | struct diff_filespec *target = rename_dst[dst_index].p->two; |
Karsten Blees | ab73a9d | 2014-07-03 00:22:11 +0200 | [diff] [blame] | 280 | struct file_similarity *p, *best = NULL; |
Karsten Blees | 48f6407 | 2013-11-14 20:19:04 +0100 | [diff] [blame] | 281 | int i = 100, best_score = -1; |
Eric Wong | f0e63c4 | 2019-10-06 23:30:35 +0000 | [diff] [blame] | 282 | unsigned int hash = hash_filespec(options->repo, target); |
Linus Torvalds | 9027f53 | 2007-10-25 11:23:26 -0700 | [diff] [blame] | 283 | |
Karsten Blees | 48f6407 | 2013-11-14 20:19:04 +0100 | [diff] [blame] | 284 | /* |
Karsten Blees | 7c85f8a | 2013-11-14 20:19:34 +0100 | [diff] [blame] | 285 | * Find the best source match for specified destination. |
Karsten Blees | 48f6407 | 2013-11-14 20:19:04 +0100 | [diff] [blame] | 286 | */ |
Eric Wong | f0e63c4 | 2019-10-06 23:30:35 +0000 | [diff] [blame] | 287 | p = hashmap_get_entry_from_hash(srcs, hash, NULL, |
| 288 | struct file_similarity, entry); |
Eric Wong | 23dee69 | 2019-10-06 23:30:41 +0000 | [diff] [blame] | 289 | hashmap_for_each_entry_from(srcs, p, entry) { |
Karsten Blees | 48f6407 | 2013-11-14 20:19:04 +0100 | [diff] [blame] | 290 | int score; |
| 291 | struct diff_filespec *source = p->filespec; |
Linus Torvalds | 9027f53 | 2007-10-25 11:23:26 -0700 | [diff] [blame] | 292 | |
Karsten Blees | 48f6407 | 2013-11-14 20:19:04 +0100 | [diff] [blame] | 293 | /* False hash collision? */ |
Jeff King | 9001dc2 | 2018-08-28 17:22:48 -0400 | [diff] [blame] | 294 | if (!oideq(&source->oid, &target->oid)) |
Karsten Blees | 48f6407 | 2013-11-14 20:19:04 +0100 | [diff] [blame] | 295 | continue; |
| 296 | /* Non-regular files? If so, the modes must match! */ |
| 297 | if (!S_ISREG(source->mode) || !S_ISREG(target->mode)) { |
| 298 | if (source->mode != target->mode) |
Linus Torvalds | 9027f53 | 2007-10-25 11:23:26 -0700 | [diff] [blame] | 299 | continue; |
Karsten Blees | 48f6407 | 2013-11-14 20:19:04 +0100 | [diff] [blame] | 300 | } |
| 301 | /* Give higher scores to sources that haven't been used already */ |
| 302 | score = !source->rename_used; |
| 303 | if (source->rename_used && options->detect_rename != DIFF_DETECT_COPY) |
| 304 | continue; |
| 305 | score += basename_same(source, target); |
| 306 | if (score > best_score) { |
| 307 | best = p; |
| 308 | best_score = score; |
| 309 | if (score == 2) |
Linus Torvalds | 9027f53 | 2007-10-25 11:23:26 -0700 | [diff] [blame] | 310 | break; |
| 311 | } |
Karsten Blees | 48f6407 | 2013-11-14 20:19:04 +0100 | [diff] [blame] | 312 | |
| 313 | /* Too many identical alternatives? Pick one */ |
| 314 | if (!--i) |
| 315 | break; |
| 316 | } |
| 317 | if (best) { |
Karsten Blees | 7c85f8a | 2013-11-14 20:19:34 +0100 | [diff] [blame] | 318 | record_rename_pair(dst_index, best->index, MAX_SCORE); |
Karsten Blees | 48f6407 | 2013-11-14 20:19:04 +0100 | [diff] [blame] | 319 | renames++; |
| 320 | } |
Linus Torvalds | 9027f53 | 2007-10-25 11:23:26 -0700 | [diff] [blame] | 321 | return renames; |
| 322 | } |
| 323 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 324 | static void insert_file_table(struct repository *r, |
Elijah Newren | fa0e936 | 2021-07-30 11:47:37 +0000 | [diff] [blame] | 325 | struct mem_pool *pool, |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 326 | struct hashmap *table, int index, |
| 327 | struct diff_filespec *filespec) |
Linus Torvalds | 9027f53 | 2007-10-25 11:23:26 -0700 | [diff] [blame] | 328 | { |
Elijah Newren | fa0e936 | 2021-07-30 11:47:37 +0000 | [diff] [blame] | 329 | struct file_similarity *entry = mem_pool_alloc(pool, sizeof(*entry)); |
Linus Torvalds | 9027f53 | 2007-10-25 11:23:26 -0700 | [diff] [blame] | 330 | |
Linus Torvalds | 9027f53 | 2007-10-25 11:23:26 -0700 | [diff] [blame] | 331 | entry->index = index; |
| 332 | entry->filespec = filespec; |
Linus Torvalds | 9027f53 | 2007-10-25 11:23:26 -0700 | [diff] [blame] | 333 | |
Eric Wong | d22245a | 2019-10-06 23:30:27 +0000 | [diff] [blame] | 334 | hashmap_entry_init(&entry->entry, hash_filespec(r, filespec)); |
Eric Wong | b94e5c1 | 2019-10-06 23:30:29 +0000 | [diff] [blame] | 335 | hashmap_add(table, &entry->entry); |
Linus Torvalds | 9027f53 | 2007-10-25 11:23:26 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Linus Torvalds | cb1491b | 2007-10-25 11:17:55 -0700 | [diff] [blame] | 338 | /* |
| 339 | * Find exact renames first. |
| 340 | * |
| 341 | * The first round matches up the up-to-date entries, |
| 342 | * and then during the second round we try to match |
| 343 | * cache-dirty entries as well. |
Linus Torvalds | cb1491b | 2007-10-25 11:17:55 -0700 | [diff] [blame] | 344 | */ |
Elijah Newren | fa0e936 | 2021-07-30 11:47:37 +0000 | [diff] [blame] | 345 | static int find_exact_renames(struct diff_options *options, |
| 346 | struct mem_pool *pool) |
Linus Torvalds | cb1491b | 2007-10-25 11:17:55 -0700 | [diff] [blame] | 347 | { |
Karsten Blees | 7c85f8a | 2013-11-14 20:19:34 +0100 | [diff] [blame] | 348 | int i, renames = 0; |
Karsten Blees | f79d9c5 | 2013-11-14 20:20:26 +0100 | [diff] [blame] | 349 | struct hashmap file_table; |
Linus Torvalds | cb1491b | 2007-10-25 11:17:55 -0700 | [diff] [blame] | 350 | |
SZEDER Gábor | ca4e3ca | 2016-03-30 10:35:07 +0200 | [diff] [blame] | 351 | /* Add all sources to the hash table in reverse order, because |
| 352 | * later on they will be retrieved in LIFO order. |
| 353 | */ |
Stefan Beller | 7663cdc | 2017-06-30 12:14:05 -0700 | [diff] [blame] | 354 | hashmap_init(&file_table, NULL, NULL, rename_src_nr); |
SZEDER Gábor | ca4e3ca | 2016-03-30 10:35:07 +0200 | [diff] [blame] | 355 | for (i = rename_src_nr-1; i >= 0; i--) |
Elijah Newren | fa0e936 | 2021-07-30 11:47:37 +0000 | [diff] [blame] | 356 | insert_file_table(options->repo, pool, |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 357 | &file_table, i, |
| 358 | rename_src[i].p->one); |
Linus Torvalds | cb1491b | 2007-10-25 11:17:55 -0700 | [diff] [blame] | 359 | |
Karsten Blees | 7c85f8a | 2013-11-14 20:19:34 +0100 | [diff] [blame] | 360 | /* Walk the destinations and find best source match */ |
Linus Torvalds | 9027f53 | 2007-10-25 11:23:26 -0700 | [diff] [blame] | 361 | for (i = 0; i < rename_dst_nr; i++) |
Karsten Blees | 7c85f8a | 2013-11-14 20:19:34 +0100 | [diff] [blame] | 362 | renames += find_identical_files(&file_table, i, options); |
Linus Torvalds | cb1491b | 2007-10-25 11:17:55 -0700 | [diff] [blame] | 363 | |
Elijah Newren | fa0e936 | 2021-07-30 11:47:37 +0000 | [diff] [blame] | 364 | /* Free the hash data structure (entries will be freed with the pool) */ |
| 365 | hashmap_clear(&file_table); |
Linus Torvalds | cb1491b | 2007-10-25 11:17:55 -0700 | [diff] [blame] | 366 | |
Karsten Blees | 7c85f8a | 2013-11-14 20:19:34 +0100 | [diff] [blame] | 367 | return renames; |
Linus Torvalds | cb1491b | 2007-10-25 11:17:55 -0700 | [diff] [blame] | 368 | } |
| 369 | |
Elijah Newren | bde8b9f | 2021-02-27 00:30:40 +0000 | [diff] [blame] | 370 | struct dir_rename_info { |
| 371 | struct strintmap idx_map; |
| 372 | struct strmap dir_rename_guess; |
| 373 | struct strmap *dir_rename_count; |
Elijah Newren | a49b55d | 2021-03-13 22:22:02 +0000 | [diff] [blame] | 374 | struct strintmap *relevant_source_dirs; |
Elijah Newren | bde8b9f | 2021-02-27 00:30:40 +0000 | [diff] [blame] | 375 | unsigned setup; |
| 376 | }; |
| 377 | |
| 378 | static char *get_dirname(const char *filename) |
| 379 | { |
| 380 | char *slash = strrchr(filename, '/'); |
| 381 | return slash ? xstrndup(filename, slash - filename) : xstrdup(""); |
| 382 | } |
| 383 | |
Elijah Newren | 0c4fd73 | 2021-02-27 00:30:42 +0000 | [diff] [blame] | 384 | static void dirname_munge(char *filename) |
| 385 | { |
| 386 | char *slash = strrchr(filename, '/'); |
| 387 | if (!slash) |
| 388 | slash = filename; |
| 389 | *slash = '\0'; |
| 390 | } |
| 391 | |
Elijah Newren | 81afdf7 | 2021-02-27 00:30:48 +0000 | [diff] [blame] | 392 | static const char *get_highest_rename_path(struct strintmap *counts) |
| 393 | { |
| 394 | int highest_count = 0; |
| 395 | const char *highest_destination_dir = NULL; |
| 396 | struct hashmap_iter iter; |
| 397 | struct strmap_entry *entry; |
| 398 | |
| 399 | strintmap_for_each_entry(counts, &iter, entry) { |
| 400 | const char *destination_dir = entry->key; |
| 401 | intptr_t count = (intptr_t)entry->value; |
| 402 | if (count > highest_count) { |
| 403 | highest_count = count; |
| 404 | highest_destination_dir = destination_dir; |
| 405 | } |
| 406 | } |
| 407 | return highest_destination_dir; |
| 408 | } |
| 409 | |
Elijah Newren | 0491d39 | 2021-03-13 22:22:05 +0000 | [diff] [blame] | 410 | static char *UNKNOWN_DIR = "/"; /* placeholder -- short, illegal directory */ |
| 411 | |
| 412 | static int dir_rename_already_determinable(struct strintmap *counts) |
| 413 | { |
| 414 | struct hashmap_iter iter; |
| 415 | struct strmap_entry *entry; |
| 416 | int first = 0, second = 0, unknown = 0; |
| 417 | strintmap_for_each_entry(counts, &iter, entry) { |
| 418 | const char *destination_dir = entry->key; |
| 419 | intptr_t count = (intptr_t)entry->value; |
| 420 | if (!strcmp(destination_dir, UNKNOWN_DIR)) { |
| 421 | unknown = count; |
| 422 | } else if (count >= first) { |
| 423 | second = first; |
| 424 | first = count; |
| 425 | } else if (count >= second) { |
| 426 | second = count; |
| 427 | } |
| 428 | } |
| 429 | return first > second + unknown; |
| 430 | } |
| 431 | |
Elijah Newren | b6e3d27 | 2021-02-27 00:30:44 +0000 | [diff] [blame] | 432 | static void increment_count(struct dir_rename_info *info, |
Elijah Newren | 0c4fd73 | 2021-02-27 00:30:42 +0000 | [diff] [blame] | 433 | char *old_dir, |
| 434 | char *new_dir) |
| 435 | { |
| 436 | struct strintmap *counts; |
| 437 | struct strmap_entry *e; |
| 438 | |
| 439 | /* Get the {new_dirs -> counts} mapping using old_dir */ |
Elijah Newren | b6e3d27 | 2021-02-27 00:30:44 +0000 | [diff] [blame] | 440 | e = strmap_get_entry(info->dir_rename_count, old_dir); |
Elijah Newren | 0c4fd73 | 2021-02-27 00:30:42 +0000 | [diff] [blame] | 441 | if (e) { |
| 442 | counts = e->value; |
| 443 | } else { |
| 444 | counts = xmalloc(sizeof(*counts)); |
| 445 | strintmap_init_with_options(counts, 0, NULL, 1); |
Elijah Newren | b6e3d27 | 2021-02-27 00:30:44 +0000 | [diff] [blame] | 446 | strmap_put(info->dir_rename_count, old_dir, counts); |
Elijah Newren | 0c4fd73 | 2021-02-27 00:30:42 +0000 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | /* Increment the count for new_dir */ |
| 450 | strintmap_incr(counts, new_dir, 1); |
| 451 | } |
| 452 | |
Elijah Newren | b6e3d27 | 2021-02-27 00:30:44 +0000 | [diff] [blame] | 453 | static void update_dir_rename_counts(struct dir_rename_info *info, |
Elijah Newren | a49b55d | 2021-03-13 22:22:02 +0000 | [diff] [blame] | 454 | struct strintmap *dirs_removed, |
Elijah Newren | 0c4fd73 | 2021-02-27 00:30:42 +0000 | [diff] [blame] | 455 | const char *oldname, |
| 456 | const char *newname) |
| 457 | { |
Andrzej Hunt | 4e3250b | 2021-07-25 15:08:23 +0200 | [diff] [blame] | 458 | char *old_dir; |
| 459 | char *new_dir; |
| 460 | const char new_dir_first_char = newname[0]; |
Elijah Newren | 0c4fd73 | 2021-02-27 00:30:42 +0000 | [diff] [blame] | 461 | int first_time_in_loop = 1; |
| 462 | |
Elijah Newren | 1ad69eb | 2021-02-27 00:30:46 +0000 | [diff] [blame] | 463 | if (!info->setup) |
| 464 | /* |
| 465 | * info->setup is 0 here in two cases: (1) all auxiliary |
| 466 | * vars (like dirs_removed) were NULL so |
| 467 | * initialize_dir_rename_info() returned early, or (2) |
| 468 | * either break detection or copy detection are active so |
| 469 | * that we never called initialize_dir_rename_info(). In |
| 470 | * the former case, we don't have enough info to know if |
| 471 | * directories were renamed (because dirs_removed lets us |
| 472 | * know about a necessary prerequisite, namely if they were |
| 473 | * removed), and in the latter, we don't care about |
| 474 | * directory renames or find_basename_matches. |
| 475 | * |
| 476 | * This matters because both basename and inexact matching |
| 477 | * will also call update_dir_rename_counts(). In either of |
| 478 | * the above two cases info->dir_rename_counts will not |
| 479 | * have been properly initialized which prevents us from |
| 480 | * updating it, but in these two cases we don't care about |
| 481 | * dir_rename_counts anyway, so we can just exit early. |
| 482 | */ |
| 483 | return; |
| 484 | |
Andrzej Hunt | 4e3250b | 2021-07-25 15:08:23 +0200 | [diff] [blame] | 485 | |
| 486 | old_dir = xstrdup(oldname); |
| 487 | new_dir = xstrdup(newname); |
| 488 | |
Elijah Newren | 0c4fd73 | 2021-02-27 00:30:42 +0000 | [diff] [blame] | 489 | while (1) { |
Elijah Newren | e54385b | 2021-03-13 22:22:04 +0000 | [diff] [blame] | 490 | int drd_flag = NOT_RELEVANT; |
| 491 | |
Elijah Newren | 333899e | 2021-02-27 00:30:47 +0000 | [diff] [blame] | 492 | /* Get old_dir, skip if its directory isn't relevant. */ |
Elijah Newren | 0c4fd73 | 2021-02-27 00:30:42 +0000 | [diff] [blame] | 493 | dirname_munge(old_dir); |
Elijah Newren | 333899e | 2021-02-27 00:30:47 +0000 | [diff] [blame] | 494 | if (info->relevant_source_dirs && |
Elijah Newren | a49b55d | 2021-03-13 22:22:02 +0000 | [diff] [blame] | 495 | !strintmap_contains(info->relevant_source_dirs, old_dir)) |
Elijah Newren | 333899e | 2021-02-27 00:30:47 +0000 | [diff] [blame] | 496 | break; |
| 497 | |
| 498 | /* Get new_dir */ |
Elijah Newren | 0c4fd73 | 2021-02-27 00:30:42 +0000 | [diff] [blame] | 499 | dirname_munge(new_dir); |
| 500 | |
| 501 | /* |
| 502 | * When renaming |
| 503 | * "a/b/c/d/e/foo.c" -> "a/b/some/thing/else/e/foo.c" |
| 504 | * then this suggests that both |
| 505 | * a/b/c/d/e/ => a/b/some/thing/else/e/ |
| 506 | * a/b/c/d/ => a/b/some/thing/else/ |
| 507 | * so we want to increment counters for both. We do NOT, |
| 508 | * however, also want to suggest that there was the following |
| 509 | * rename: |
| 510 | * a/b/c/ => a/b/some/thing/ |
| 511 | * so we need to quit at that point. |
| 512 | * |
| 513 | * Note the when first_time_in_loop, we only strip off the |
| 514 | * basename, and we don't care if that's different. |
| 515 | */ |
| 516 | if (!first_time_in_loop) { |
| 517 | char *old_sub_dir = strchr(old_dir, '\0')+1; |
| 518 | char *new_sub_dir = strchr(new_dir, '\0')+1; |
| 519 | if (!*new_dir) { |
| 520 | /* |
| 521 | * Special case when renaming to root directory, |
| 522 | * i.e. when new_dir == "". In this case, we had |
| 523 | * something like |
| 524 | * a/b/subdir => subdir |
| 525 | * and so dirname_munge() sets things up so that |
| 526 | * old_dir = "a/b\0subdir\0" |
| 527 | * new_dir = "\0ubdir\0" |
| 528 | * We didn't have a '/' to overwrite a '\0' onto |
| 529 | * in new_dir, so we have to compare differently. |
| 530 | */ |
| 531 | if (new_dir_first_char != old_sub_dir[0] || |
| 532 | strcmp(old_sub_dir+1, new_sub_dir)) |
| 533 | break; |
| 534 | } else { |
| 535 | if (strcmp(old_sub_dir, new_sub_dir)) |
| 536 | break; |
| 537 | } |
| 538 | } |
| 539 | |
Elijah Newren | e54385b | 2021-03-13 22:22:04 +0000 | [diff] [blame] | 540 | /* |
| 541 | * Above we suggested that we'd keep recording renames for |
| 542 | * all ancestor directories where the trailing directories |
| 543 | * matched, i.e. for |
| 544 | * "a/b/c/d/e/foo.c" -> "a/b/some/thing/else/e/foo.c" |
| 545 | * we'd increment rename counts for each of |
| 546 | * a/b/c/d/e/ => a/b/some/thing/else/e/ |
| 547 | * a/b/c/d/ => a/b/some/thing/else/ |
| 548 | * However, we only need the rename counts for directories |
| 549 | * in dirs_removed whose value is RELEVANT_FOR_SELF. |
| 550 | * However, we add one special case of also recording it for |
| 551 | * first_time_in_loop because find_basename_matches() can |
| 552 | * use that as a hint to find a good pairing. |
| 553 | */ |
| 554 | if (dirs_removed) |
| 555 | drd_flag = strintmap_get(dirs_removed, old_dir); |
| 556 | if (drd_flag == RELEVANT_FOR_SELF || first_time_in_loop) |
Elijah Newren | b6e3d27 | 2021-02-27 00:30:44 +0000 | [diff] [blame] | 557 | increment_count(info, old_dir, new_dir); |
Elijah Newren | 0c4fd73 | 2021-02-27 00:30:42 +0000 | [diff] [blame] | 558 | |
Elijah Newren | e54385b | 2021-03-13 22:22:04 +0000 | [diff] [blame] | 559 | first_time_in_loop = 0; |
| 560 | if (drd_flag == NOT_RELEVANT) |
| 561 | break; |
Elijah Newren | 0c4fd73 | 2021-02-27 00:30:42 +0000 | [diff] [blame] | 562 | /* If we hit toplevel directory ("") for old or new dir, quit */ |
| 563 | if (!*old_dir || !*new_dir) |
| 564 | break; |
Elijah Newren | 0c4fd73 | 2021-02-27 00:30:42 +0000 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | /* Free resources we don't need anymore */ |
| 568 | free(old_dir); |
| 569 | free(new_dir); |
| 570 | } |
| 571 | |
Elijah Newren | 1ad69eb | 2021-02-27 00:30:46 +0000 | [diff] [blame] | 572 | static void initialize_dir_rename_info(struct dir_rename_info *info, |
Elijah Newren | a49b55d | 2021-03-13 22:22:02 +0000 | [diff] [blame] | 573 | struct strintmap *relevant_sources, |
| 574 | struct strintmap *dirs_removed, |
Elijah Newren | 25e65b6 | 2021-05-20 06:09:41 +0000 | [diff] [blame] | 575 | struct strmap *dir_rename_count, |
| 576 | struct strmap *cached_pairs) |
Elijah Newren | 0c4fd73 | 2021-02-27 00:30:42 +0000 | [diff] [blame] | 577 | { |
Elijah Newren | 81afdf7 | 2021-02-27 00:30:48 +0000 | [diff] [blame] | 578 | struct hashmap_iter iter; |
| 579 | struct strmap_entry *entry; |
Elijah Newren | 0c4fd73 | 2021-02-27 00:30:42 +0000 | [diff] [blame] | 580 | int i; |
| 581 | |
Elijah Newren | e4fd06e | 2021-03-11 00:38:31 +0000 | [diff] [blame] | 582 | if (!dirs_removed && !relevant_sources) { |
Elijah Newren | 1ad69eb | 2021-02-27 00:30:46 +0000 | [diff] [blame] | 583 | info->setup = 0; |
| 584 | return; |
Elijah Newren | 0c4fd73 | 2021-02-27 00:30:42 +0000 | [diff] [blame] | 585 | } |
Elijah Newren | ae8cf74 | 2021-02-27 00:30:41 +0000 | [diff] [blame] | 586 | info->setup = 1; |
| 587 | |
Elijah Newren | 1ad69eb | 2021-02-27 00:30:46 +0000 | [diff] [blame] | 588 | info->dir_rename_count = dir_rename_count; |
| 589 | if (!info->dir_rename_count) { |
| 590 | info->dir_rename_count = xmalloc(sizeof(*dir_rename_count)); |
| 591 | strmap_init(info->dir_rename_count); |
| 592 | } |
Elijah Newren | ae8cf74 | 2021-02-27 00:30:41 +0000 | [diff] [blame] | 593 | strintmap_init_with_options(&info->idx_map, -1, NULL, 0); |
| 594 | strmap_init_with_options(&info->dir_rename_guess, NULL, 0); |
Elijah Newren | ae8cf74 | 2021-02-27 00:30:41 +0000 | [diff] [blame] | 595 | |
Elijah Newren | 333899e | 2021-02-27 00:30:47 +0000 | [diff] [blame] | 596 | /* Setup info->relevant_source_dirs */ |
Elijah Newren | e4fd06e | 2021-03-11 00:38:31 +0000 | [diff] [blame] | 597 | info->relevant_source_dirs = NULL; |
| 598 | if (dirs_removed || !relevant_sources) { |
| 599 | info->relevant_source_dirs = dirs_removed; /* might be NULL */ |
| 600 | } else { |
| 601 | info->relevant_source_dirs = xmalloc(sizeof(struct strintmap)); |
Elijah Newren | a49b55d | 2021-03-13 22:22:02 +0000 | [diff] [blame] | 602 | strintmap_init(info->relevant_source_dirs, 0 /* unused */); |
| 603 | strintmap_for_each_entry(relevant_sources, &iter, entry) { |
Elijah Newren | e4fd06e | 2021-03-11 00:38:31 +0000 | [diff] [blame] | 604 | char *dirname = get_dirname(entry->key); |
| 605 | if (!dirs_removed || |
Elijah Newren | a49b55d | 2021-03-13 22:22:02 +0000 | [diff] [blame] | 606 | strintmap_contains(dirs_removed, dirname)) |
| 607 | strintmap_set(info->relevant_source_dirs, |
| 608 | dirname, 0 /* value irrelevant */); |
Elijah Newren | e4fd06e | 2021-03-11 00:38:31 +0000 | [diff] [blame] | 609 | free(dirname); |
| 610 | } |
| 611 | } |
Elijah Newren | 333899e | 2021-02-27 00:30:47 +0000 | [diff] [blame] | 612 | |
Elijah Newren | ae8cf74 | 2021-02-27 00:30:41 +0000 | [diff] [blame] | 613 | /* |
Elijah Newren | 1ad69eb | 2021-02-27 00:30:46 +0000 | [diff] [blame] | 614 | * Loop setting up both info->idx_map, and doing setup of |
| 615 | * info->dir_rename_count. |
Elijah Newren | ae8cf74 | 2021-02-27 00:30:41 +0000 | [diff] [blame] | 616 | */ |
| 617 | for (i = 0; i < rename_dst_nr; ++i) { |
| 618 | /* |
| 619 | * For non-renamed files, make idx_map contain mapping of |
| 620 | * filename -> index (index within rename_dst, that is) |
| 621 | */ |
| 622 | if (!rename_dst[i].is_rename) { |
| 623 | char *filename = rename_dst[i].p->two->path; |
| 624 | strintmap_set(&info->idx_map, filename, i); |
Elijah Newren | 1ad69eb | 2021-02-27 00:30:46 +0000 | [diff] [blame] | 625 | continue; |
Elijah Newren | ae8cf74 | 2021-02-27 00:30:41 +0000 | [diff] [blame] | 626 | } |
Elijah Newren | 1ad69eb | 2021-02-27 00:30:46 +0000 | [diff] [blame] | 627 | |
| 628 | /* |
| 629 | * For everything else (i.e. renamed files), make |
| 630 | * dir_rename_count contain a map of a map: |
| 631 | * old_directory -> {new_directory -> count} |
| 632 | * In other words, for every pair look at the directories for |
| 633 | * the old filename and the new filename and count how many |
| 634 | * times that pairing occurs. |
| 635 | */ |
| 636 | update_dir_rename_counts(info, dirs_removed, |
| 637 | rename_dst[i].p->one->path, |
| 638 | rename_dst[i].p->two->path); |
Elijah Newren | ae8cf74 | 2021-02-27 00:30:41 +0000 | [diff] [blame] | 639 | } |
Elijah Newren | 81afdf7 | 2021-02-27 00:30:48 +0000 | [diff] [blame] | 640 | |
Elijah Newren | 25e65b6 | 2021-05-20 06:09:41 +0000 | [diff] [blame] | 641 | /* Add cached_pairs to counts */ |
| 642 | strmap_for_each_entry(cached_pairs, &iter, entry) { |
| 643 | const char *old_name = entry->key; |
| 644 | const char *new_name = entry->value; |
| 645 | if (!new_name) |
| 646 | /* known delete; ignore it */ |
| 647 | continue; |
| 648 | |
| 649 | update_dir_rename_counts(info, dirs_removed, old_name, new_name); |
| 650 | } |
| 651 | |
Elijah Newren | 81afdf7 | 2021-02-27 00:30:48 +0000 | [diff] [blame] | 652 | /* |
| 653 | * Now we collapse |
| 654 | * dir_rename_count: old_directory -> {new_directory -> count} |
| 655 | * down to |
| 656 | * dir_rename_guess: old_directory -> best_new_directory |
| 657 | * where best_new_directory is the one with the highest count. |
| 658 | */ |
| 659 | strmap_for_each_entry(info->dir_rename_count, &iter, entry) { |
| 660 | /* entry->key is source_dir */ |
| 661 | struct strintmap *counts = entry->value; |
| 662 | char *best_newdir; |
| 663 | |
| 664 | best_newdir = xstrdup(get_highest_rename_path(counts)); |
| 665 | strmap_put(&info->dir_rename_guess, entry->key, |
| 666 | best_newdir); |
| 667 | } |
Elijah Newren | ae8cf74 | 2021-02-27 00:30:41 +0000 | [diff] [blame] | 668 | } |
| 669 | |
Elijah Newren | cd52e00 | 2021-02-27 00:30:43 +0000 | [diff] [blame] | 670 | void partial_clear_dir_rename_count(struct strmap *dir_rename_count) |
| 671 | { |
| 672 | struct hashmap_iter iter; |
| 673 | struct strmap_entry *entry; |
| 674 | |
| 675 | strmap_for_each_entry(dir_rename_count, &iter, entry) { |
| 676 | struct strintmap *counts = entry->value; |
| 677 | strintmap_clear(counts); |
| 678 | } |
| 679 | strmap_partial_clear(dir_rename_count, 1); |
| 680 | } |
| 681 | |
Elijah Newren | b147301 | 2021-02-27 00:30:45 +0000 | [diff] [blame] | 682 | static void cleanup_dir_rename_info(struct dir_rename_info *info, |
Elijah Newren | a49b55d | 2021-03-13 22:22:02 +0000 | [diff] [blame] | 683 | struct strintmap *dirs_removed, |
Elijah Newren | b147301 | 2021-02-27 00:30:45 +0000 | [diff] [blame] | 684 | int keep_dir_rename_count) |
Elijah Newren | ae8cf74 | 2021-02-27 00:30:41 +0000 | [diff] [blame] | 685 | { |
Elijah Newren | b147301 | 2021-02-27 00:30:45 +0000 | [diff] [blame] | 686 | struct hashmap_iter iter; |
| 687 | struct strmap_entry *entry; |
| 688 | struct string_list to_remove = STRING_LIST_INIT_NODUP; |
| 689 | int i; |
| 690 | |
Elijah Newren | ae8cf74 | 2021-02-27 00:30:41 +0000 | [diff] [blame] | 691 | if (!info->setup) |
| 692 | return; |
| 693 | |
| 694 | /* idx_map */ |
| 695 | strintmap_clear(&info->idx_map); |
| 696 | |
| 697 | /* dir_rename_guess */ |
| 698 | strmap_clear(&info->dir_rename_guess, 1); |
| 699 | |
Elijah Newren | e4fd06e | 2021-03-11 00:38:31 +0000 | [diff] [blame] | 700 | /* relevant_source_dirs */ |
| 701 | if (info->relevant_source_dirs && |
| 702 | info->relevant_source_dirs != dirs_removed) { |
Elijah Newren | a49b55d | 2021-03-13 22:22:02 +0000 | [diff] [blame] | 703 | strintmap_clear(info->relevant_source_dirs); |
Elijah Newren | e4fd06e | 2021-03-11 00:38:31 +0000 | [diff] [blame] | 704 | FREE_AND_NULL(info->relevant_source_dirs); |
| 705 | } |
| 706 | |
Elijah Newren | b6e3d27 | 2021-02-27 00:30:44 +0000 | [diff] [blame] | 707 | /* dir_rename_count */ |
Elijah Newren | b147301 | 2021-02-27 00:30:45 +0000 | [diff] [blame] | 708 | if (!keep_dir_rename_count) { |
| 709 | partial_clear_dir_rename_count(info->dir_rename_count); |
| 710 | strmap_clear(info->dir_rename_count, 1); |
| 711 | FREE_AND_NULL(info->dir_rename_count); |
| 712 | return; |
| 713 | } |
| 714 | |
| 715 | /* |
| 716 | * Although dir_rename_count was passed in |
| 717 | * diffcore_rename_extended() and we want to keep it around and |
Elijah Newren | bf238b7 | 2021-03-13 22:22:06 +0000 | [diff] [blame] | 718 | * return it to that caller, we first want to remove any counts in |
| 719 | * the maps associated with UNKNOWN_DIR entries and any data |
Elijah Newren | b147301 | 2021-02-27 00:30:45 +0000 | [diff] [blame] | 720 | * associated with directories that weren't renamed. |
| 721 | */ |
| 722 | strmap_for_each_entry(info->dir_rename_count, &iter, entry) { |
| 723 | const char *source_dir = entry->key; |
| 724 | struct strintmap *counts = entry->value; |
| 725 | |
Elijah Newren | fb52938 | 2021-03-13 22:22:03 +0000 | [diff] [blame] | 726 | if (!strintmap_get(dirs_removed, source_dir)) { |
Elijah Newren | b147301 | 2021-02-27 00:30:45 +0000 | [diff] [blame] | 727 | string_list_append(&to_remove, source_dir); |
| 728 | strintmap_clear(counts); |
| 729 | continue; |
| 730 | } |
Elijah Newren | bf238b7 | 2021-03-13 22:22:06 +0000 | [diff] [blame] | 731 | |
| 732 | if (strintmap_contains(counts, UNKNOWN_DIR)) |
| 733 | strintmap_remove(counts, UNKNOWN_DIR); |
Elijah Newren | b147301 | 2021-02-27 00:30:45 +0000 | [diff] [blame] | 734 | } |
| 735 | for (i = 0; i < to_remove.nr; ++i) |
| 736 | strmap_remove(info->dir_rename_count, |
| 737 | to_remove.items[i].string, 1); |
| 738 | string_list_clear(&to_remove, 0); |
Elijah Newren | ae8cf74 | 2021-02-27 00:30:41 +0000 | [diff] [blame] | 739 | } |
| 740 | |
Elijah Newren | a35df33 | 2021-02-14 07:51:47 +0000 | [diff] [blame] | 741 | static const char *get_basename(const char *filename) |
| 742 | { |
| 743 | /* |
| 744 | * gitbasename() has to worry about special drives, multiple |
| 745 | * directory separator characters, trailing slashes, NULL or |
| 746 | * empty strings, etc. We only work on filenames as stored in |
| 747 | * git, and thus get to ignore all those complications. |
| 748 | */ |
| 749 | const char *base = strrchr(filename, '/'); |
| 750 | return base ? base + 1 : filename; |
| 751 | } |
| 752 | |
Elijah Newren | bde8b9f | 2021-02-27 00:30:40 +0000 | [diff] [blame] | 753 | static int idx_possible_rename(char *filename, struct dir_rename_info *info) |
Elijah Newren | 37a2514 | 2021-02-27 00:30:39 +0000 | [diff] [blame] | 754 | { |
Elijah Newren | bde8b9f | 2021-02-27 00:30:40 +0000 | [diff] [blame] | 755 | /* |
| 756 | * Our comparison of files with the same basename (see |
| 757 | * find_basename_matches() below), is only helpful when after exact |
| 758 | * rename detection we have exactly one file with a given basename |
| 759 | * among the rename sources and also only exactly one file with |
| 760 | * that basename among the rename destinations. When we have |
| 761 | * multiple files with the same basename in either set, we do not |
| 762 | * know which to compare against. However, there are some |
| 763 | * filenames that occur in large numbers (particularly |
| 764 | * build-related filenames such as 'Makefile', '.gitignore', or |
| 765 | * 'build.gradle' that potentially exist within every single |
| 766 | * subdirectory), and for performance we want to be able to quickly |
| 767 | * find renames for these files too. |
| 768 | * |
| 769 | * The reason basename comparisons are a useful heuristic was that it |
| 770 | * is common for people to move files across directories while keeping |
| 771 | * their filename the same. If we had a way of determining or even |
| 772 | * making a good educated guess about which directory these non-unique |
| 773 | * basename files had moved the file to, we could check it. |
| 774 | * Luckily... |
| 775 | * |
| 776 | * When an entire directory is in fact renamed, we have two factors |
| 777 | * helping us out: |
| 778 | * (a) the original directory disappeared giving us a hint |
| 779 | * about when we can apply an extra heuristic. |
| 780 | * (a) we often have several files within that directory and |
| 781 | * subdirectories that are renamed without changes |
| 782 | * So, rules for a heuristic: |
| 783 | * (0) If there basename matches are non-unique (the condition under |
| 784 | * which this function is called) AND |
| 785 | * (1) the directory in which the file was found has disappeared |
| 786 | * (i.e. dirs_removed is non-NULL and has a relevant entry) THEN |
| 787 | * (2) use exact renames of files within the directory to determine |
| 788 | * where the directory is likely to have been renamed to. IF |
| 789 | * there is at least one exact rename from within that |
| 790 | * directory, we can proceed. |
| 791 | * (3) If there are multiple places the directory could have been |
| 792 | * renamed to based on exact renames, ignore all but one of them. |
| 793 | * Just use the destination with the most renames going to it. |
| 794 | * (4) Check if applying that directory rename to the original file |
| 795 | * would result in a destination filename that is in the |
| 796 | * potential rename set. If so, return the index of the |
| 797 | * destination file (the index within rename_dst). |
| 798 | * (5) Compare the original file and returned destination for |
| 799 | * similarity, and if they are sufficiently similar, record the |
| 800 | * rename. |
| 801 | * |
| 802 | * This function, idx_possible_rename(), is only responsible for (4). |
Elijah Newren | 81afdf7 | 2021-02-27 00:30:48 +0000 | [diff] [blame] | 803 | * The conditions/steps in (1)-(3) are handled via setting up |
| 804 | * dir_rename_count and dir_rename_guess in |
| 805 | * initialize_dir_rename_info(). Steps (0) and (5) are handled by |
| 806 | * the caller of this function. |
Elijah Newren | bde8b9f | 2021-02-27 00:30:40 +0000 | [diff] [blame] | 807 | */ |
| 808 | char *old_dir, *new_dir; |
| 809 | struct strbuf new_path = STRBUF_INIT; |
| 810 | int idx; |
| 811 | |
| 812 | if (!info->setup) |
| 813 | return -1; |
| 814 | |
| 815 | old_dir = get_dirname(filename); |
| 816 | new_dir = strmap_get(&info->dir_rename_guess, old_dir); |
| 817 | free(old_dir); |
| 818 | if (!new_dir) |
| 819 | return -1; |
| 820 | |
| 821 | strbuf_addstr(&new_path, new_dir); |
| 822 | strbuf_addch(&new_path, '/'); |
| 823 | strbuf_addstr(&new_path, get_basename(filename)); |
| 824 | |
| 825 | idx = strintmap_get(&info->idx_map, new_path.buf); |
| 826 | strbuf_release(&new_path); |
| 827 | return idx; |
Elijah Newren | 37a2514 | 2021-02-27 00:30:39 +0000 | [diff] [blame] | 828 | } |
| 829 | |
Elijah Newren | 1aedd03 | 2021-06-22 08:04:40 +0000 | [diff] [blame] | 830 | struct basename_prefetch_options { |
| 831 | struct repository *repo; |
| 832 | struct strintmap *relevant_sources; |
| 833 | struct strintmap *sources; |
| 834 | struct strintmap *dests; |
| 835 | struct dir_rename_info *info; |
| 836 | }; |
| 837 | static void basename_prefetch(void *prefetch_options) |
| 838 | { |
| 839 | struct basename_prefetch_options *options = prefetch_options; |
| 840 | struct strintmap *relevant_sources = options->relevant_sources; |
| 841 | struct strintmap *sources = options->sources; |
| 842 | struct strintmap *dests = options->dests; |
| 843 | struct dir_rename_info *info = options->info; |
| 844 | int i; |
| 845 | struct oid_array to_fetch = OID_ARRAY_INIT; |
| 846 | |
| 847 | /* |
| 848 | * TODO: The following loops mirror the code/logic from |
| 849 | * find_basename_matches(), though not quite exactly. Maybe |
| 850 | * abstract the iteration logic out somehow? |
| 851 | */ |
| 852 | for (i = 0; i < rename_src_nr; ++i) { |
| 853 | char *filename = rename_src[i].p->one->path; |
| 854 | const char *base = NULL; |
| 855 | intptr_t src_index; |
| 856 | intptr_t dst_index; |
| 857 | |
| 858 | /* Skip irrelevant sources */ |
| 859 | if (relevant_sources && |
| 860 | !strintmap_contains(relevant_sources, filename)) |
| 861 | continue; |
| 862 | |
| 863 | /* |
| 864 | * If the basename is unique among remaining sources, then |
| 865 | * src_index will equal 'i' and we can attempt to match it |
| 866 | * to a unique basename in the destinations. Otherwise, |
| 867 | * use directory rename heuristics, if possible. |
| 868 | */ |
| 869 | base = get_basename(filename); |
| 870 | src_index = strintmap_get(sources, base); |
| 871 | assert(src_index == -1 || src_index == i); |
| 872 | |
| 873 | if (strintmap_contains(dests, base)) { |
| 874 | struct diff_filespec *one, *two; |
| 875 | |
| 876 | /* Find a matching destination, if possible */ |
| 877 | dst_index = strintmap_get(dests, base); |
| 878 | if (src_index == -1 || dst_index == -1) { |
| 879 | src_index = i; |
| 880 | dst_index = idx_possible_rename(filename, info); |
| 881 | } |
| 882 | if (dst_index == -1) |
| 883 | continue; |
| 884 | |
| 885 | /* Ignore this dest if already used in a rename */ |
| 886 | if (rename_dst[dst_index].is_rename) |
| 887 | continue; /* already used previously */ |
| 888 | |
| 889 | one = rename_src[src_index].p->one; |
| 890 | two = rename_dst[dst_index].p->two; |
| 891 | |
| 892 | /* Add the pairs */ |
| 893 | diff_add_if_missing(options->repo, &to_fetch, two); |
| 894 | diff_add_if_missing(options->repo, &to_fetch, one); |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | promisor_remote_get_direct(options->repo, to_fetch.oid, to_fetch.nr); |
| 899 | oid_array_clear(&to_fetch); |
| 900 | } |
| 901 | |
Elijah Newren | a35df33 | 2021-02-14 07:51:47 +0000 | [diff] [blame] | 902 | static int find_basename_matches(struct diff_options *options, |
Elijah Newren | bde8b9f | 2021-02-27 00:30:40 +0000 | [diff] [blame] | 903 | int minimum_score, |
Elijah Newren | 1ad69eb | 2021-02-27 00:30:46 +0000 | [diff] [blame] | 904 | struct dir_rename_info *info, |
Elijah Newren | a49b55d | 2021-03-13 22:22:02 +0000 | [diff] [blame] | 905 | struct strintmap *relevant_sources, |
| 906 | struct strintmap *dirs_removed) |
Elijah Newren | a35df33 | 2021-02-14 07:51:47 +0000 | [diff] [blame] | 907 | { |
Elijah Newren | da09f65 | 2021-02-14 07:51:48 +0000 | [diff] [blame] | 908 | /* |
| 909 | * When I checked in early 2020, over 76% of file renames in linux |
| 910 | * just moved files to a different directory but kept the same |
| 911 | * basename. gcc did that with over 64% of renames, gecko did it |
| 912 | * with over 79%, and WebKit did it with over 89%. |
| 913 | * |
| 914 | * Therefore we can bypass the normal exhaustive NxM matrix |
| 915 | * comparison of similarities between all potential rename sources |
| 916 | * and destinations by instead using file basename as a hint (i.e. |
| 917 | * the portion of the filename after the last '/'), checking for |
| 918 | * similarity between files with the same basename, and if we find |
| 919 | * a pair that are sufficiently similar, record the rename pair and |
| 920 | * exclude those two from the NxM matrix. |
| 921 | * |
| 922 | * This *might* cause us to find a less than optimal pairing (if |
| 923 | * there is another file that we are even more similar to but has a |
| 924 | * different basename). Given the huge performance advantage |
| 925 | * basename matching provides, and given the frequency with which |
| 926 | * people use the same basename in real world projects, that's a |
| 927 | * trade-off we are willing to accept when doing just rename |
| 928 | * detection. |
| 929 | * |
| 930 | * If someone wants copy detection that implies they are willing to |
| 931 | * spend more cycles to find similarities between files, so it may |
| 932 | * be less likely that this heuristic is wanted. If someone is |
| 933 | * doing break detection, that means they do not want filename |
| 934 | * similarity to imply any form of content similiarity, and thus |
| 935 | * this heuristic would definitely be incompatible. |
| 936 | */ |
| 937 | |
| 938 | int i, renames = 0; |
Elijah Newren | a35df33 | 2021-02-14 07:51:47 +0000 | [diff] [blame] | 939 | struct strintmap sources; |
| 940 | struct strintmap dests; |
Elijah Newren | d331dd3 | 2021-06-22 08:04:39 +0000 | [diff] [blame] | 941 | struct diff_populate_filespec_options dpf_options = { |
| 942 | .check_binary = 0, |
| 943 | .missing_object_cb = NULL, |
| 944 | .missing_object_data = NULL |
| 945 | }; |
Elijah Newren | 1aedd03 | 2021-06-22 08:04:40 +0000 | [diff] [blame] | 946 | struct basename_prefetch_options prefetch_options = { |
Elijah Newren | d331dd3 | 2021-06-22 08:04:39 +0000 | [diff] [blame] | 947 | .repo = options->repo, |
Elijah Newren | 1aedd03 | 2021-06-22 08:04:40 +0000 | [diff] [blame] | 948 | .relevant_sources = relevant_sources, |
| 949 | .sources = &sources, |
| 950 | .dests = &dests, |
| 951 | .info = info |
Elijah Newren | d331dd3 | 2021-06-22 08:04:39 +0000 | [diff] [blame] | 952 | }; |
Elijah Newren | a35df33 | 2021-02-14 07:51:47 +0000 | [diff] [blame] | 953 | |
| 954 | /* |
| 955 | * Create maps of basename -> fullname(s) for remaining sources and |
| 956 | * dests. |
| 957 | */ |
| 958 | strintmap_init_with_options(&sources, -1, NULL, 0); |
| 959 | strintmap_init_with_options(&dests, -1, NULL, 0); |
| 960 | for (i = 0; i < rename_src_nr; ++i) { |
| 961 | char *filename = rename_src[i].p->one->path; |
| 962 | const char *base; |
| 963 | |
| 964 | /* exact renames removed in remove_unneeded_paths_from_src() */ |
| 965 | assert(!rename_src[i].p->one->rename_used); |
| 966 | |
| 967 | /* Record index within rename_src (i) if basename is unique */ |
| 968 | base = get_basename(filename); |
| 969 | if (strintmap_contains(&sources, base)) |
| 970 | strintmap_set(&sources, base, -1); |
| 971 | else |
| 972 | strintmap_set(&sources, base, i); |
| 973 | } |
| 974 | for (i = 0; i < rename_dst_nr; ++i) { |
| 975 | char *filename = rename_dst[i].p->two->path; |
| 976 | const char *base; |
| 977 | |
| 978 | if (rename_dst[i].is_rename) |
| 979 | continue; /* involved in exact match already. */ |
| 980 | |
| 981 | /* Record index within rename_dst (i) if basename is unique */ |
| 982 | base = get_basename(filename); |
| 983 | if (strintmap_contains(&dests, base)) |
| 984 | strintmap_set(&dests, base, -1); |
| 985 | else |
| 986 | strintmap_set(&dests, base, i); |
| 987 | } |
| 988 | |
Elijah Newren | d331dd3 | 2021-06-22 08:04:39 +0000 | [diff] [blame] | 989 | if (options->repo == the_repository && has_promisor_remote()) { |
Elijah Newren | 1aedd03 | 2021-06-22 08:04:40 +0000 | [diff] [blame] | 990 | dpf_options.missing_object_cb = basename_prefetch; |
Elijah Newren | d331dd3 | 2021-06-22 08:04:39 +0000 | [diff] [blame] | 991 | dpf_options.missing_object_data = &prefetch_options; |
| 992 | } |
| 993 | |
Elijah Newren | da09f65 | 2021-02-14 07:51:48 +0000 | [diff] [blame] | 994 | /* Now look for basename matchups and do similarity estimation */ |
Elijah Newren | 37a2514 | 2021-02-27 00:30:39 +0000 | [diff] [blame] | 995 | for (i = 0; i < rename_src_nr; ++i) { |
| 996 | char *filename = rename_src[i].p->one->path; |
| 997 | const char *base = NULL; |
| 998 | intptr_t src_index; |
Elijah Newren | da09f65 | 2021-02-14 07:51:48 +0000 | [diff] [blame] | 999 | intptr_t dst_index; |
Elijah Newren | da09f65 | 2021-02-14 07:51:48 +0000 | [diff] [blame] | 1000 | |
Elijah Newren | e4fd06e | 2021-03-11 00:38:31 +0000 | [diff] [blame] | 1001 | /* Skip irrelevant sources */ |
| 1002 | if (relevant_sources && |
Elijah Newren | a49b55d | 2021-03-13 22:22:02 +0000 | [diff] [blame] | 1003 | !strintmap_contains(relevant_sources, filename)) |
Elijah Newren | e4fd06e | 2021-03-11 00:38:31 +0000 | [diff] [blame] | 1004 | continue; |
| 1005 | |
Elijah Newren | 37a2514 | 2021-02-27 00:30:39 +0000 | [diff] [blame] | 1006 | /* |
| 1007 | * If the basename is unique among remaining sources, then |
| 1008 | * src_index will equal 'i' and we can attempt to match it |
| 1009 | * to a unique basename in the destinations. Otherwise, |
| 1010 | * use directory rename heuristics, if possible. |
| 1011 | */ |
| 1012 | base = get_basename(filename); |
| 1013 | src_index = strintmap_get(&sources, base); |
| 1014 | assert(src_index == -1 || src_index == i); |
| 1015 | |
| 1016 | if (strintmap_contains(&dests, base)) { |
Elijah Newren | da09f65 | 2021-02-14 07:51:48 +0000 | [diff] [blame] | 1017 | struct diff_filespec *one, *two; |
| 1018 | int score; |
| 1019 | |
Elijah Newren | 37a2514 | 2021-02-27 00:30:39 +0000 | [diff] [blame] | 1020 | /* Find a matching destination, if possible */ |
| 1021 | dst_index = strintmap_get(&dests, base); |
| 1022 | if (src_index == -1 || dst_index == -1) { |
| 1023 | src_index = i; |
Elijah Newren | bde8b9f | 2021-02-27 00:30:40 +0000 | [diff] [blame] | 1024 | dst_index = idx_possible_rename(filename, info); |
Elijah Newren | 37a2514 | 2021-02-27 00:30:39 +0000 | [diff] [blame] | 1025 | } |
| 1026 | if (dst_index == -1) |
| 1027 | continue; |
| 1028 | |
| 1029 | /* Ignore this dest if already used in a rename */ |
| 1030 | if (rename_dst[dst_index].is_rename) |
| 1031 | continue; /* already used previously */ |
| 1032 | |
Elijah Newren | da09f65 | 2021-02-14 07:51:48 +0000 | [diff] [blame] | 1033 | /* Estimate the similarity */ |
| 1034 | one = rename_src[src_index].p->one; |
| 1035 | two = rename_dst[dst_index].p->two; |
| 1036 | score = estimate_similarity(options->repo, one, two, |
Elijah Newren | d331dd3 | 2021-06-22 08:04:39 +0000 | [diff] [blame] | 1037 | minimum_score, &dpf_options); |
Elijah Newren | da09f65 | 2021-02-14 07:51:48 +0000 | [diff] [blame] | 1038 | |
| 1039 | /* If sufficiently similar, record as rename pair */ |
| 1040 | if (score < minimum_score) |
| 1041 | continue; |
| 1042 | record_rename_pair(dst_index, src_index, score); |
| 1043 | renames++; |
Elijah Newren | 1ad69eb | 2021-02-27 00:30:46 +0000 | [diff] [blame] | 1044 | update_dir_rename_counts(info, dirs_removed, |
| 1045 | one->path, two->path); |
Elijah Newren | da09f65 | 2021-02-14 07:51:48 +0000 | [diff] [blame] | 1046 | |
| 1047 | /* |
| 1048 | * Found a rename so don't need text anymore; if we |
| 1049 | * didn't find a rename, the filespec_blob would get |
| 1050 | * re-used when doing the matrix of comparisons. |
| 1051 | */ |
| 1052 | diff_free_filespec_blob(one); |
| 1053 | diff_free_filespec_blob(two); |
| 1054 | } |
| 1055 | } |
Elijah Newren | a35df33 | 2021-02-14 07:51:47 +0000 | [diff] [blame] | 1056 | |
| 1057 | strintmap_clear(&sources); |
| 1058 | strintmap_clear(&dests); |
| 1059 | |
Elijah Newren | da09f65 | 2021-02-14 07:51:48 +0000 | [diff] [blame] | 1060 | return renames; |
Elijah Newren | a35df33 | 2021-02-14 07:51:47 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
Junio C Hamano | 6d24ad9 | 2008-01-29 20:54:56 -0800 | [diff] [blame] | 1063 | #define NUM_CANDIDATE_PER_DST 4 |
| 1064 | static void record_if_better(struct diff_score m[], struct diff_score *o) |
| 1065 | { |
| 1066 | int i, worst; |
| 1067 | |
| 1068 | /* find the worst one */ |
| 1069 | worst = 0; |
| 1070 | for (i = 1; i < NUM_CANDIDATE_PER_DST; i++) |
| 1071 | if (score_compare(&m[i], &m[worst]) > 0) |
| 1072 | worst = i; |
| 1073 | |
| 1074 | /* is it better than the worst one? */ |
| 1075 | if (score_compare(&m[worst], o) > 0) |
| 1076 | m[worst] = *o; |
| 1077 | } |
| 1078 | |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 1079 | /* |
| 1080 | * Returns: |
| 1081 | * 0 if we are under the limit; |
| 1082 | * 1 if we need to disable inexact rename detection; |
| 1083 | * 2 if we would be under the limit if we were given -C instead of -C -C. |
| 1084 | */ |
Elijah Newren | 00b8ccc | 2020-12-11 09:08:41 +0000 | [diff] [blame] | 1085 | static int too_many_rename_candidates(int num_destinations, int num_sources, |
Junio C Hamano | 9d8a5a5 | 2011-01-06 13:50:04 -0800 | [diff] [blame] | 1086 | struct diff_options *options) |
| 1087 | { |
| 1088 | int rename_limit = options->rename_limit; |
Elijah Newren | 00b8ccc | 2020-12-11 09:08:41 +0000 | [diff] [blame] | 1089 | int i, limited_sources; |
Junio C Hamano | 9d8a5a5 | 2011-01-06 13:50:04 -0800 | [diff] [blame] | 1090 | |
| 1091 | options->needed_rename_limit = 0; |
| 1092 | |
| 1093 | /* |
| 1094 | * This basically does a test for the rename matrix not |
| 1095 | * growing larger than a "rename_limit" square matrix, ie: |
| 1096 | * |
Elijah Newren | 00b8ccc | 2020-12-11 09:08:41 +0000 | [diff] [blame] | 1097 | * num_destinations * num_sources > rename_limit * rename_limit |
Elijah Newren | ad8a1be | 2020-12-11 09:08:42 +0000 | [diff] [blame] | 1098 | * |
| 1099 | * We use st_mult() to check overflow conditions; in the |
| 1100 | * exceptional circumstance that size_t isn't large enough to hold |
| 1101 | * the multiplication, the system won't be able to allocate enough |
| 1102 | * memory for the matrix anyway. |
Junio C Hamano | 9d8a5a5 | 2011-01-06 13:50:04 -0800 | [diff] [blame] | 1103 | */ |
Jonathan Tan | 8997355 | 2017-11-29 12:11:54 -0800 | [diff] [blame] | 1104 | if (rename_limit <= 0) |
Elijah Newren | 9dd29db | 2021-07-15 00:45:23 +0000 | [diff] [blame] | 1105 | return 0; /* treat as unlimited */ |
Elijah Newren | ad8a1be | 2020-12-11 09:08:42 +0000 | [diff] [blame] | 1106 | if (st_mult(num_destinations, num_sources) |
| 1107 | <= st_mult(rename_limit, rename_limit)) |
Junio C Hamano | 9d8a5a5 | 2011-01-06 13:50:04 -0800 | [diff] [blame] | 1108 | return 0; |
| 1109 | |
| 1110 | options->needed_rename_limit = |
Elijah Newren | 00b8ccc | 2020-12-11 09:08:41 +0000 | [diff] [blame] | 1111 | num_sources > num_destinations ? num_sources : num_destinations; |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 1112 | |
| 1113 | /* Are we running under -C -C? */ |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 1114 | if (!options->flags.find_copies_harder) |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 1115 | return 1; |
| 1116 | |
| 1117 | /* Would we bust the limit if we were running under -C? */ |
Elijah Newren | 00b8ccc | 2020-12-11 09:08:41 +0000 | [diff] [blame] | 1118 | for (limited_sources = i = 0; i < num_sources; i++) { |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 1119 | if (diff_unmodified_pair(rename_src[i].p)) |
| 1120 | continue; |
Elijah Newren | 00b8ccc | 2020-12-11 09:08:41 +0000 | [diff] [blame] | 1121 | limited_sources++; |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 1122 | } |
Elijah Newren | ad8a1be | 2020-12-11 09:08:42 +0000 | [diff] [blame] | 1123 | if (st_mult(num_destinations, limited_sources) |
| 1124 | <= st_mult(rename_limit, rename_limit)) |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 1125 | return 2; |
Junio C Hamano | 9d8a5a5 | 2011-01-06 13:50:04 -0800 | [diff] [blame] | 1126 | return 1; |
| 1127 | } |
| 1128 | |
Elijah Newren | 1ad69eb | 2021-02-27 00:30:46 +0000 | [diff] [blame] | 1129 | static int find_renames(struct diff_score *mx, |
| 1130 | int dst_cnt, |
| 1131 | int minimum_score, |
| 1132 | int copies, |
| 1133 | struct dir_rename_info *info, |
Elijah Newren | a49b55d | 2021-03-13 22:22:02 +0000 | [diff] [blame] | 1134 | struct strintmap *dirs_removed) |
Linus Torvalds | 0940e5f | 2011-02-18 20:10:32 -0800 | [diff] [blame] | 1135 | { |
| 1136 | int count = 0, i; |
| 1137 | |
| 1138 | for (i = 0; i < dst_cnt * NUM_CANDIDATE_PER_DST; i++) { |
| 1139 | struct diff_rename_dst *dst; |
| 1140 | |
| 1141 | if ((mx[i].dst < 0) || |
| 1142 | (mx[i].score < minimum_score)) |
| 1143 | break; /* there is no more usable pair. */ |
| 1144 | dst = &rename_dst[mx[i].dst]; |
Elijah Newren | 9db2ac5 | 2020-12-11 09:08:47 +0000 | [diff] [blame] | 1145 | if (dst->is_rename) |
Linus Torvalds | 0940e5f | 2011-02-18 20:10:32 -0800 | [diff] [blame] | 1146 | continue; /* already done, either exact or fuzzy. */ |
Junio C Hamano | e88d6bc | 2011-01-06 13:50:05 -0800 | [diff] [blame] | 1147 | if (!copies && rename_src[mx[i].src].p->one->rename_used) |
Linus Torvalds | 0940e5f | 2011-02-18 20:10:32 -0800 | [diff] [blame] | 1148 | continue; |
| 1149 | record_rename_pair(mx[i].dst, mx[i].src, mx[i].score); |
| 1150 | count++; |
Elijah Newren | 1ad69eb | 2021-02-27 00:30:46 +0000 | [diff] [blame] | 1151 | update_dir_rename_counts(info, dirs_removed, |
| 1152 | rename_src[mx[i].src].p->one->path, |
| 1153 | rename_dst[mx[i].dst].p->two->path); |
Linus Torvalds | 0940e5f | 2011-02-18 20:10:32 -0800 | [diff] [blame] | 1154 | } |
| 1155 | return count; |
| 1156 | } |
| 1157 | |
Elijah Newren | 9799889 | 2021-03-11 00:38:24 +0000 | [diff] [blame] | 1158 | static void remove_unneeded_paths_from_src(int detecting_copies, |
Elijah Newren | a49b55d | 2021-03-13 22:22:02 +0000 | [diff] [blame] | 1159 | struct strintmap *interesting) |
Elijah Newren | 829514c | 2021-02-14 07:35:01 +0000 | [diff] [blame] | 1160 | { |
| 1161 | int i, new_num_src; |
| 1162 | |
Elijah Newren | 9799889 | 2021-03-11 00:38:24 +0000 | [diff] [blame] | 1163 | if (detecting_copies && !interesting) |
Elijah Newren | 829514c | 2021-02-14 07:35:01 +0000 | [diff] [blame] | 1164 | return; /* nothing to remove */ |
| 1165 | if (break_idx) |
| 1166 | return; /* culling incompatible with break detection */ |
| 1167 | |
| 1168 | /* |
| 1169 | * Note on reasons why we cull unneeded sources but not destinations: |
| 1170 | * 1) Pairings are stored in rename_dst (not rename_src), which we |
| 1171 | * need to keep around. So, we just can't cull rename_dst even |
| 1172 | * if we wanted to. But doing so wouldn't help because... |
| 1173 | * |
| 1174 | * 2) There is a matrix pairwise comparison that follows the |
| 1175 | * "Performing inexact rename detection" progress message. |
| 1176 | * Iterating over the destinations is done in the outer loop, |
| 1177 | * hence we only iterate over each of those once and we can |
| 1178 | * easily skip the outer loop early if the destination isn't |
| 1179 | * relevant. That's only one check per destination path to |
| 1180 | * skip. |
| 1181 | * |
| 1182 | * By contrast, the sources are iterated in the inner loop; if |
| 1183 | * we check whether a source can be skipped, then we'll be |
| 1184 | * checking it N separate times, once for each destination. |
| 1185 | * We don't want to have to iterate over known-not-needed |
| 1186 | * sources N times each, so avoid that by removing the sources |
| 1187 | * from rename_src here. |
| 1188 | */ |
| 1189 | for (i = 0, new_num_src = 0; i < rename_src_nr; i++) { |
Elijah Newren | 9799889 | 2021-03-11 00:38:24 +0000 | [diff] [blame] | 1190 | struct diff_filespec *one = rename_src[i].p->one; |
| 1191 | |
Elijah Newren | 829514c | 2021-02-14 07:35:01 +0000 | [diff] [blame] | 1192 | /* |
| 1193 | * renames are stored in rename_dst, so if a rename has |
| 1194 | * already been detected using this source, we can just |
| 1195 | * remove the source knowing rename_dst has its info. |
| 1196 | */ |
Elijah Newren | 9799889 | 2021-03-11 00:38:24 +0000 | [diff] [blame] | 1197 | if (!detecting_copies && one->rename_used) |
| 1198 | continue; |
| 1199 | |
| 1200 | /* If we don't care about the source path, skip it */ |
Elijah Newren | a49b55d | 2021-03-13 22:22:02 +0000 | [diff] [blame] | 1201 | if (interesting && !strintmap_contains(interesting, one->path)) |
Elijah Newren | 829514c | 2021-02-14 07:35:01 +0000 | [diff] [blame] | 1202 | continue; |
| 1203 | |
| 1204 | if (new_num_src < i) |
| 1205 | memcpy(&rename_src[new_num_src], &rename_src[i], |
| 1206 | sizeof(struct diff_rename_src)); |
| 1207 | new_num_src++; |
| 1208 | } |
| 1209 | |
| 1210 | rename_src_nr = new_num_src; |
| 1211 | } |
| 1212 | |
Elijah Newren | ae1db7b | 2021-03-13 22:22:01 +0000 | [diff] [blame] | 1213 | static void handle_early_known_dir_renames(struct dir_rename_info *info, |
Elijah Newren | a49b55d | 2021-03-13 22:22:02 +0000 | [diff] [blame] | 1214 | struct strintmap *relevant_sources, |
| 1215 | struct strintmap *dirs_removed) |
Elijah Newren | ae1db7b | 2021-03-13 22:22:01 +0000 | [diff] [blame] | 1216 | { |
| 1217 | /* |
Elijah Newren | 0491d39 | 2021-03-13 22:22:05 +0000 | [diff] [blame] | 1218 | * Directory renames are determined via an aggregate of all renames |
| 1219 | * under them and using a "majority wins" rule. The fact that |
| 1220 | * "majority wins", though, means we don't need all the renames |
| 1221 | * under the given directory, we only need enough to ensure we have |
| 1222 | * a majority. |
Elijah Newren | ae1db7b | 2021-03-13 22:22:01 +0000 | [diff] [blame] | 1223 | */ |
Elijah Newren | 0491d39 | 2021-03-13 22:22:05 +0000 | [diff] [blame] | 1224 | |
Elijah Newren | 9bd3421 | 2021-03-13 22:22:08 +0000 | [diff] [blame] | 1225 | int i, new_num_src; |
Elijah Newren | 0491d39 | 2021-03-13 22:22:05 +0000 | [diff] [blame] | 1226 | struct hashmap_iter iter; |
| 1227 | struct strmap_entry *entry; |
| 1228 | |
| 1229 | if (!dirs_removed || !relevant_sources) |
| 1230 | return; /* nothing to cull */ |
| 1231 | if (break_idx) |
| 1232 | return; /* culling incompatbile with break detection */ |
| 1233 | |
| 1234 | /* |
Elijah Newren | bf238b7 | 2021-03-13 22:22:06 +0000 | [diff] [blame] | 1235 | * Supplement dir_rename_count with number of potential renames, |
| 1236 | * marking all potential rename sources as mapping to UNKNOWN_DIR. |
Elijah Newren | 0491d39 | 2021-03-13 22:22:05 +0000 | [diff] [blame] | 1237 | */ |
Elijah Newren | bf238b7 | 2021-03-13 22:22:06 +0000 | [diff] [blame] | 1238 | for (i = 0; i < rename_src_nr; i++) { |
| 1239 | char *old_dir; |
| 1240 | struct diff_filespec *one = rename_src[i].p->one; |
| 1241 | |
| 1242 | /* |
| 1243 | * sources that are part of a rename will have already been |
| 1244 | * removed by a prior call to remove_unneeded_paths_from_src() |
| 1245 | */ |
| 1246 | assert(!one->rename_used); |
| 1247 | |
| 1248 | old_dir = get_dirname(one->path); |
| 1249 | while (*old_dir != '\0' && |
| 1250 | NOT_RELEVANT != strintmap_get(dirs_removed, old_dir)) { |
| 1251 | char *freeme = old_dir; |
| 1252 | |
| 1253 | increment_count(info, old_dir, UNKNOWN_DIR); |
| 1254 | old_dir = get_dirname(old_dir); |
| 1255 | |
| 1256 | /* Free resources we don't need anymore */ |
| 1257 | free(freeme); |
| 1258 | } |
| 1259 | /* |
| 1260 | * old_dir and new_dir free'd in increment_count, but |
| 1261 | * get_dirname() gives us a new pointer we need to free for |
| 1262 | * old_dir. Also, if the loop runs 0 times we need old_dir |
| 1263 | * to be freed. |
| 1264 | */ |
| 1265 | free(old_dir); |
| 1266 | } |
Elijah Newren | 0491d39 | 2021-03-13 22:22:05 +0000 | [diff] [blame] | 1267 | |
| 1268 | /* |
| 1269 | * For any directory which we need a potential rename detected for |
| 1270 | * (i.e. those marked as RELEVANT_FOR_SELF in dirs_removed), check |
| 1271 | * whether we have enough renames to satisfy the "majority rules" |
| 1272 | * requirement such that detecting any more renames of files under |
| 1273 | * it won't change the result. For any such directory, mark that |
| 1274 | * we no longer need to detect a rename for it. However, since we |
| 1275 | * might need to still detect renames for an ancestor of that |
| 1276 | * directory, use RELEVANT_FOR_ANCESTOR. |
| 1277 | */ |
| 1278 | strmap_for_each_entry(info->dir_rename_count, &iter, entry) { |
| 1279 | /* entry->key is source_dir */ |
| 1280 | struct strintmap *counts = entry->value; |
| 1281 | |
| 1282 | if (strintmap_get(dirs_removed, entry->key) == |
| 1283 | RELEVANT_FOR_SELF && |
| 1284 | dir_rename_already_determinable(counts)) { |
| 1285 | strintmap_set(dirs_removed, entry->key, |
| 1286 | RELEVANT_FOR_ANCESTOR); |
| 1287 | } |
| 1288 | } |
Elijah Newren | 9bd3421 | 2021-03-13 22:22:08 +0000 | [diff] [blame] | 1289 | |
| 1290 | for (i = 0, new_num_src = 0; i < rename_src_nr; i++) { |
| 1291 | struct diff_filespec *one = rename_src[i].p->one; |
| 1292 | int val; |
| 1293 | |
| 1294 | val = strintmap_get(relevant_sources, one->path); |
| 1295 | |
| 1296 | /* |
| 1297 | * sources that were not found in relevant_sources should |
| 1298 | * have already been removed by a prior call to |
| 1299 | * remove_unneeded_paths_from_src() |
| 1300 | */ |
| 1301 | assert(val != -1); |
| 1302 | |
| 1303 | if (val == RELEVANT_LOCATION) { |
| 1304 | int removable = 1; |
| 1305 | char *dir = get_dirname(one->path); |
| 1306 | while (1) { |
| 1307 | char *freeme = dir; |
| 1308 | int res = strintmap_get(dirs_removed, dir); |
| 1309 | |
| 1310 | /* Quit if not found or irrelevant */ |
| 1311 | if (res == NOT_RELEVANT) |
| 1312 | break; |
| 1313 | /* If RELEVANT_FOR_SELF, can't remove */ |
| 1314 | if (res == RELEVANT_FOR_SELF) { |
| 1315 | removable = 0; |
| 1316 | break; |
| 1317 | } |
| 1318 | /* Else continue searching upwards */ |
| 1319 | assert(res == RELEVANT_FOR_ANCESTOR); |
| 1320 | dir = get_dirname(dir); |
| 1321 | free(freeme); |
| 1322 | } |
| 1323 | free(dir); |
| 1324 | if (removable) { |
| 1325 | strintmap_set(relevant_sources, one->path, |
| 1326 | RELEVANT_NO_MORE); |
| 1327 | continue; |
| 1328 | } |
| 1329 | } |
| 1330 | |
| 1331 | if (new_num_src < i) |
| 1332 | memcpy(&rename_src[new_num_src], &rename_src[i], |
| 1333 | sizeof(struct diff_rename_src)); |
| 1334 | new_num_src++; |
| 1335 | } |
| 1336 | |
| 1337 | rename_src_nr = new_num_src; |
Elijah Newren | ae1db7b | 2021-03-13 22:22:01 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
Elijah Newren | a8791ef | 2021-07-30 11:47:41 +0000 | [diff] [blame] | 1340 | static void free_filespec_data(struct diff_filespec *spec) |
| 1341 | { |
| 1342 | if (!--spec->count) |
| 1343 | diff_free_filespec_data(spec); |
| 1344 | } |
| 1345 | |
Elijah Newren | a8791ef | 2021-07-30 11:47:41 +0000 | [diff] [blame] | 1346 | static void pool_free_filespec(struct mem_pool *pool, |
| 1347 | struct diff_filespec *spec) |
| 1348 | { |
| 1349 | if (!pool) { |
| 1350 | free_filespec(spec); |
| 1351 | return; |
| 1352 | } |
| 1353 | |
| 1354 | /* |
| 1355 | * Similar to free_filespec(), but only frees the data. The spec |
| 1356 | * itself was allocated in the pool and should not be individually |
| 1357 | * freed. |
| 1358 | */ |
| 1359 | free_filespec_data(spec); |
| 1360 | } |
| 1361 | |
Elijah Newren | a8791ef | 2021-07-30 11:47:41 +0000 | [diff] [blame] | 1362 | void pool_diff_free_filepair(struct mem_pool *pool, |
| 1363 | struct diff_filepair *p) |
| 1364 | { |
| 1365 | if (!pool) { |
| 1366 | diff_free_filepair(p); |
| 1367 | return; |
| 1368 | } |
| 1369 | |
| 1370 | /* |
| 1371 | * Similar to diff_free_filepair() but only frees the data from the |
| 1372 | * filespecs; not the filespecs or the filepair which were |
| 1373 | * allocated from the pool. |
| 1374 | */ |
| 1375 | free_filespec_data(p->one); |
| 1376 | free_filespec_data(p->two); |
| 1377 | } |
| 1378 | |
Elijah Newren | 0c4fd73 | 2021-02-27 00:30:42 +0000 | [diff] [blame] | 1379 | void diffcore_rename_extended(struct diff_options *options, |
Elijah Newren | f239fff | 2021-07-30 11:47:42 +0000 | [diff] [blame] | 1380 | struct mem_pool *pool, |
Elijah Newren | a49b55d | 2021-03-13 22:22:02 +0000 | [diff] [blame] | 1381 | struct strintmap *relevant_sources, |
| 1382 | struct strintmap *dirs_removed, |
Elijah Newren | 25e65b6 | 2021-05-20 06:09:41 +0000 | [diff] [blame] | 1383 | struct strmap *dir_rename_count, |
| 1384 | struct strmap *cached_pairs) |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 1385 | { |
Junio C Hamano | 8082d8d | 2005-09-21 00:18:27 -0700 | [diff] [blame] | 1386 | int detect_rename = options->detect_rename; |
| 1387 | int minimum_score = options->rename_score; |
Junio C Hamano | 38c6f78 | 2005-05-21 19:40:36 -0700 | [diff] [blame] | 1388 | struct diff_queue_struct *q = &diff_queued_diff; |
Junio C Hamano | 5098baf | 2005-09-15 16:13:43 -0700 | [diff] [blame] | 1389 | struct diff_queue_struct outq; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 1390 | struct diff_score *mx; |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 1391 | int i, j, rename_count, skip_unmodified = 0; |
Elijah Newren | 26a66a6 | 2020-12-11 09:08:40 +0000 | [diff] [blame] | 1392 | int num_destinations, dst_cnt; |
Elijah Newren | f15eb7c | 2021-02-03 20:03:46 +0000 | [diff] [blame] | 1393 | int num_sources, want_copies; |
Jeff King | 3ac942d | 2011-02-20 04:51:16 -0500 | [diff] [blame] | 1394 | struct progress *progress = NULL; |
Elijah Newren | fa0e936 | 2021-07-30 11:47:37 +0000 | [diff] [blame] | 1395 | struct mem_pool local_pool; |
Elijah Newren | bde8b9f | 2021-02-27 00:30:40 +0000 | [diff] [blame] | 1396 | struct dir_rename_info info; |
Elijah Newren | d331dd3 | 2021-06-22 08:04:39 +0000 | [diff] [blame] | 1397 | struct diff_populate_filespec_options dpf_options = { |
| 1398 | .check_binary = 0, |
| 1399 | .missing_object_cb = NULL, |
| 1400 | .missing_object_data = NULL |
| 1401 | }; |
Elijah Newren | 1aedd03 | 2021-06-22 08:04:40 +0000 | [diff] [blame] | 1402 | struct inexact_prefetch_options prefetch_options = { |
Elijah Newren | d331dd3 | 2021-06-22 08:04:39 +0000 | [diff] [blame] | 1403 | .repo = options->repo |
| 1404 | }; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 1405 | |
Elijah Newren | 557ac03 | 2021-01-23 22:01:12 -0800 | [diff] [blame] | 1406 | trace2_region_enter("diff", "setup", options->repo); |
Elijah Newren | bde8b9f | 2021-02-27 00:30:40 +0000 | [diff] [blame] | 1407 | info.setup = 0; |
Elijah Newren | 0c4fd73 | 2021-02-27 00:30:42 +0000 | [diff] [blame] | 1408 | assert(!dir_rename_count || strmap_empty(dir_rename_count)); |
Elijah Newren | f15eb7c | 2021-02-03 20:03:46 +0000 | [diff] [blame] | 1409 | want_copies = (detect_rename == DIFF_DETECT_COPY); |
Elijah Newren | 1ad69eb | 2021-02-27 00:30:46 +0000 | [diff] [blame] | 1410 | if (dirs_removed && (break_idx || want_copies)) |
| 1411 | BUG("dirs_removed incompatible with break/copy detection"); |
Elijah Newren | 9799889 | 2021-03-11 00:38:24 +0000 | [diff] [blame] | 1412 | if (break_idx && relevant_sources) |
| 1413 | BUG("break detection incompatible with source specification"); |
Junio C Hamano | 26dee0a | 2005-05-21 23:33:32 -0700 | [diff] [blame] | 1414 | if (!minimum_score) |
Junio C Hamano | f345b0a | 2005-05-30 00:08:37 -0700 | [diff] [blame] | 1415 | minimum_score = DEFAULT_RENAME_SCORE; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 1416 | |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 1417 | for (i = 0; i < q->nr; i++) { |
Junio C Hamano | 52e9578 | 2005-05-21 02:40:01 -0700 | [diff] [blame] | 1418 | struct diff_filepair *p = q->queue[i]; |
Junio C Hamano | 2f3f8b2 | 2006-11-02 00:02:11 -0800 | [diff] [blame] | 1419 | if (!DIFF_FILE_VALID(p->one)) { |
Junio C Hamano | 81e50ea | 2005-05-21 19:42:18 -0700 | [diff] [blame] | 1420 | if (!DIFF_FILE_VALID(p->two)) |
Junio C Hamano | 60896c7 | 2005-05-22 21:24:49 -0700 | [diff] [blame] | 1421 | continue; /* unmerged */ |
Junio C Hamano | 2f3f8b2 | 2006-11-02 00:02:11 -0800 | [diff] [blame] | 1422 | else if (options->single_follow && |
| 1423 | strcmp(options->single_follow, p->two->path)) |
| 1424 | continue; /* not interested */ |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 1425 | else if (!options->flags.rename_empty && |
Brandon Williams | 02491b6 | 2017-05-30 10:31:08 -0700 | [diff] [blame] | 1426 | is_empty_blob_oid(&p->two->oid)) |
Jeff King | 90d43b0 | 2012-03-22 18:52:13 -0400 | [diff] [blame] | 1427 | continue; |
Elijah Newren | 9db2ac5 | 2020-12-11 09:08:47 +0000 | [diff] [blame] | 1428 | else if (add_rename_dst(p) < 0) { |
Jeff King | 4d6be03 | 2015-02-26 20:42:27 -0500 | [diff] [blame] | 1429 | warning("skipping rename detection, detected" |
| 1430 | " duplicate destination '%s'", |
| 1431 | p->two->path); |
| 1432 | goto cleanup; |
| 1433 | } |
Junio C Hamano | 2f3f8b2 | 2006-11-02 00:02:11 -0800 | [diff] [blame] | 1434 | } |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 1435 | else if (!options->flags.rename_empty && |
Brandon Williams | 02491b6 | 2017-05-30 10:31:08 -0700 | [diff] [blame] | 1436 | is_empty_blob_oid(&p->one->oid)) |
Jeff King | 90d43b0 | 2012-03-22 18:52:13 -0400 | [diff] [blame] | 1437 | continue; |
Martin von Zweigbergk | d7c9bf2 | 2011-03-23 22:41:01 -0400 | [diff] [blame] | 1438 | else if (!DIFF_PAIR_UNMERGED(p) && !DIFF_FILE_VALID(p->two)) { |
Linus Torvalds | 6447971 | 2007-10-25 11:20:56 -0700 | [diff] [blame] | 1439 | /* |
| 1440 | * If the source is a broken "delete", and |
Junio C Hamano | 2210100 | 2005-06-11 20:55:20 -0700 | [diff] [blame] | 1441 | * they did not really want to get broken, |
| 1442 | * that means the source actually stays. |
Linus Torvalds | 6447971 | 2007-10-25 11:20:56 -0700 | [diff] [blame] | 1443 | * So we increment the "rename_used" score |
| 1444 | * by one, to indicate ourselves as a user |
Junio C Hamano | 2210100 | 2005-06-11 20:55:20 -0700 | [diff] [blame] | 1445 | */ |
Linus Torvalds | 6447971 | 2007-10-25 11:20:56 -0700 | [diff] [blame] | 1446 | if (p->broken_pair && !p->score) |
| 1447 | p->one->rename_used++; |
Junio C Hamano | e88d6bc | 2011-01-06 13:50:05 -0800 | [diff] [blame] | 1448 | register_rename_src(p); |
Junio C Hamano | 2210100 | 2005-06-11 20:55:20 -0700 | [diff] [blame] | 1449 | } |
Elijah Newren | f15eb7c | 2021-02-03 20:03:46 +0000 | [diff] [blame] | 1450 | else if (want_copies) { |
Linus Torvalds | 6447971 | 2007-10-25 11:20:56 -0700 | [diff] [blame] | 1451 | /* |
| 1452 | * Increment the "rename_used" score by |
| 1453 | * one, to indicate ourselves as a user. |
| 1454 | */ |
| 1455 | p->one->rename_used++; |
Junio C Hamano | e88d6bc | 2011-01-06 13:50:05 -0800 | [diff] [blame] | 1456 | register_rename_src(p); |
Linus Torvalds | 6447971 | 2007-10-25 11:20:56 -0700 | [diff] [blame] | 1457 | } |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 1458 | } |
Elijah Newren | 557ac03 | 2021-01-23 22:01:12 -0800 | [diff] [blame] | 1459 | trace2_region_leave("diff", "setup", options->repo); |
Linus Torvalds | 0024a54 | 2007-09-14 10:39:48 -0700 | [diff] [blame] | 1460 | if (rename_dst_nr == 0 || rename_src_nr == 0) |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 1461 | goto cleanup; /* nothing to do */ |
| 1462 | |
Elijah Newren | 557ac03 | 2021-01-23 22:01:12 -0800 | [diff] [blame] | 1463 | trace2_region_enter("diff", "exact renames", options->repo); |
Elijah Newren | fa0e936 | 2021-07-30 11:47:37 +0000 | [diff] [blame] | 1464 | mem_pool_init(&local_pool, 32*1024); |
Linus Torvalds | 0024a54 | 2007-09-14 10:39:48 -0700 | [diff] [blame] | 1465 | /* |
Linus Torvalds | 17559a6 | 2007-10-25 11:24:47 -0700 | [diff] [blame] | 1466 | * We really want to cull the candidates list early |
| 1467 | * with cheap tests in order to avoid doing deltas. |
| 1468 | */ |
Elijah Newren | fa0e936 | 2021-07-30 11:47:37 +0000 | [diff] [blame] | 1469 | rename_count = find_exact_renames(options, &local_pool); |
| 1470 | /* |
| 1471 | * Discard local_pool immediately instead of at "cleanup:" in order |
| 1472 | * to reduce maximum memory usage; inexact rename detection uses up |
| 1473 | * a fair amount of memory, and mem_pools can too. |
| 1474 | */ |
| 1475 | mem_pool_discard(&local_pool, 0); |
Elijah Newren | 557ac03 | 2021-01-23 22:01:12 -0800 | [diff] [blame] | 1476 | trace2_region_leave("diff", "exact renames", options->repo); |
Linus Torvalds | 17559a6 | 2007-10-25 11:24:47 -0700 | [diff] [blame] | 1477 | |
Linus Torvalds | 42899ac | 2007-10-26 16:56:34 -0700 | [diff] [blame] | 1478 | /* Did we only want exact renames? */ |
| 1479 | if (minimum_score == MAX_SCORE) |
| 1480 | goto cleanup; |
| 1481 | |
Elijah Newren | f15eb7c | 2021-02-03 20:03:46 +0000 | [diff] [blame] | 1482 | num_sources = rename_src_nr; |
Linus Torvalds | 42899ac | 2007-10-26 16:56:34 -0700 | [diff] [blame] | 1483 | |
Elijah Newren | bd24aa2 | 2021-02-14 07:51:49 +0000 | [diff] [blame] | 1484 | if (want_copies || break_idx) { |
| 1485 | /* |
| 1486 | * Cull sources: |
| 1487 | * - remove ones corresponding to exact renames |
Elijah Newren | 9799889 | 2021-03-11 00:38:24 +0000 | [diff] [blame] | 1488 | * - remove ones not found in relevant_sources |
Elijah Newren | bd24aa2 | 2021-02-14 07:51:49 +0000 | [diff] [blame] | 1489 | */ |
| 1490 | trace2_region_enter("diff", "cull after exact", options->repo); |
Elijah Newren | 9799889 | 2021-03-11 00:38:24 +0000 | [diff] [blame] | 1491 | remove_unneeded_paths_from_src(want_copies, relevant_sources); |
Elijah Newren | bd24aa2 | 2021-02-14 07:51:49 +0000 | [diff] [blame] | 1492 | trace2_region_leave("diff", "cull after exact", options->repo); |
| 1493 | } else { |
| 1494 | /* Determine minimum score to match basenames */ |
| 1495 | double factor = 0.5; |
| 1496 | char *basename_factor = getenv("GIT_BASENAME_FACTOR"); |
| 1497 | int min_basename_score; |
| 1498 | |
| 1499 | if (basename_factor) |
| 1500 | factor = strtol(basename_factor, NULL, 10)/100.0; |
| 1501 | assert(factor >= 0.0 && factor <= 1.0); |
| 1502 | min_basename_score = minimum_score + |
| 1503 | (int)(factor * (MAX_SCORE - minimum_score)); |
| 1504 | |
| 1505 | /* |
| 1506 | * Cull sources: |
| 1507 | * - remove ones involved in renames (found via exact match) |
| 1508 | */ |
| 1509 | trace2_region_enter("diff", "cull after exact", options->repo); |
Elijah Newren | 9799889 | 2021-03-11 00:38:24 +0000 | [diff] [blame] | 1510 | remove_unneeded_paths_from_src(want_copies, NULL); |
Elijah Newren | bd24aa2 | 2021-02-14 07:51:49 +0000 | [diff] [blame] | 1511 | trace2_region_leave("diff", "cull after exact", options->repo); |
| 1512 | |
Elijah Newren | ae8cf74 | 2021-02-27 00:30:41 +0000 | [diff] [blame] | 1513 | /* Preparation for basename-driven matching. */ |
| 1514 | trace2_region_enter("diff", "dir rename setup", options->repo); |
Elijah Newren | e4fd06e | 2021-03-11 00:38:31 +0000 | [diff] [blame] | 1515 | initialize_dir_rename_info(&info, relevant_sources, |
Elijah Newren | 25e65b6 | 2021-05-20 06:09:41 +0000 | [diff] [blame] | 1516 | dirs_removed, dir_rename_count, |
| 1517 | cached_pairs); |
Elijah Newren | ae8cf74 | 2021-02-27 00:30:41 +0000 | [diff] [blame] | 1518 | trace2_region_leave("diff", "dir rename setup", options->repo); |
| 1519 | |
Elijah Newren | bd24aa2 | 2021-02-14 07:51:49 +0000 | [diff] [blame] | 1520 | /* Utilize file basenames to quickly find renames. */ |
| 1521 | trace2_region_enter("diff", "basename matches", options->repo); |
| 1522 | rename_count += find_basename_matches(options, |
Elijah Newren | bde8b9f | 2021-02-27 00:30:40 +0000 | [diff] [blame] | 1523 | min_basename_score, |
Elijah Newren | e4fd06e | 2021-03-11 00:38:31 +0000 | [diff] [blame] | 1524 | &info, |
| 1525 | relevant_sources, |
| 1526 | dirs_removed); |
Elijah Newren | bd24aa2 | 2021-02-14 07:51:49 +0000 | [diff] [blame] | 1527 | trace2_region_leave("diff", "basename matches", options->repo); |
| 1528 | |
| 1529 | /* |
| 1530 | * Cull sources, again: |
| 1531 | * - remove ones involved in renames (found via basenames) |
Elijah Newren | 9799889 | 2021-03-11 00:38:24 +0000 | [diff] [blame] | 1532 | * - remove ones not found in relevant_sources |
Elijah Newren | ae1db7b | 2021-03-13 22:22:01 +0000 | [diff] [blame] | 1533 | * and |
| 1534 | * - remove ones in relevant_sources which are needed only |
| 1535 | * for directory renames IF no ancestory directory |
| 1536 | * actually needs to know any more individual path |
| 1537 | * renames under them |
Elijah Newren | bd24aa2 | 2021-02-14 07:51:49 +0000 | [diff] [blame] | 1538 | */ |
| 1539 | trace2_region_enter("diff", "cull basename", options->repo); |
Elijah Newren | 9799889 | 2021-03-11 00:38:24 +0000 | [diff] [blame] | 1540 | remove_unneeded_paths_from_src(want_copies, relevant_sources); |
Elijah Newren | ae1db7b | 2021-03-13 22:22:01 +0000 | [diff] [blame] | 1541 | handle_early_known_dir_renames(&info, relevant_sources, |
| 1542 | dirs_removed); |
Elijah Newren | bd24aa2 | 2021-02-14 07:51:49 +0000 | [diff] [blame] | 1543 | trace2_region_leave("diff", "cull basename", options->repo); |
| 1544 | } |
| 1545 | |
| 1546 | /* Calculate how many rename destinations are left */ |
| 1547 | num_destinations = (rename_dst_nr - rename_count); |
| 1548 | num_sources = rename_src_nr; /* rename_src_nr reflects lower number */ |
| 1549 | |
Linus Torvalds | 42899ac | 2007-10-26 16:56:34 -0700 | [diff] [blame] | 1550 | /* All done? */ |
Elijah Newren | f15eb7c | 2021-02-03 20:03:46 +0000 | [diff] [blame] | 1551 | if (!num_destinations || !num_sources) |
Linus Torvalds | 42899ac | 2007-10-26 16:56:34 -0700 | [diff] [blame] | 1552 | goto cleanup; |
| 1553 | |
Elijah Newren | f15eb7c | 2021-02-03 20:03:46 +0000 | [diff] [blame] | 1554 | switch (too_many_rename_candidates(num_destinations, num_sources, |
Elijah Newren | 00b8ccc | 2020-12-11 09:08:41 +0000 | [diff] [blame] | 1555 | options)) { |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 1556 | case 1: |
Linus Torvalds | 0024a54 | 2007-09-14 10:39:48 -0700 | [diff] [blame] | 1557 | goto cleanup; |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 1558 | case 2: |
| 1559 | options->degraded_cc_to_c = 1; |
| 1560 | skip_unmodified = 1; |
| 1561 | break; |
| 1562 | default: |
| 1563 | break; |
| 1564 | } |
Linus Torvalds | 0024a54 | 2007-09-14 10:39:48 -0700 | [diff] [blame] | 1565 | |
Elijah Newren | 557ac03 | 2021-01-23 22:01:12 -0800 | [diff] [blame] | 1566 | trace2_region_enter("diff", "inexact renames", options->repo); |
Jeff King | 3ac942d | 2011-02-20 04:51:16 -0500 | [diff] [blame] | 1567 | if (options->show_rename_progress) { |
Junio C Hamano | 8aade10 | 2017-08-19 10:39:41 -0700 | [diff] [blame] | 1568 | progress = start_delayed_progress( |
Nguyễn Thái Ngọc Duy | 754dbc4 | 2014-02-21 19:50:18 +0700 | [diff] [blame] | 1569 | _("Performing inexact rename detection"), |
Elijah Newren | f15eb7c | 2021-02-03 20:03:46 +0000 | [diff] [blame] | 1570 | (uint64_t)num_destinations * (uint64_t)num_sources); |
Jeff King | 3ac942d | 2011-02-20 04:51:16 -0500 | [diff] [blame] | 1571 | } |
| 1572 | |
Elijah Newren | d331dd3 | 2021-06-22 08:04:39 +0000 | [diff] [blame] | 1573 | /* Finish setting up dpf_options */ |
| 1574 | prefetch_options.skip_unmodified = skip_unmodified; |
| 1575 | if (options->repo == the_repository && has_promisor_remote()) { |
Elijah Newren | 1aedd03 | 2021-06-22 08:04:40 +0000 | [diff] [blame] | 1576 | dpf_options.missing_object_cb = inexact_prefetch; |
Elijah Newren | d331dd3 | 2021-06-22 08:04:39 +0000 | [diff] [blame] | 1577 | dpf_options.missing_object_data = &prefetch_options; |
| 1578 | } |
| 1579 | |
René Scharfe | ca56dad | 2021-03-13 17:17:22 +0100 | [diff] [blame] | 1580 | CALLOC_ARRAY(mx, st_mult(NUM_CANDIDATE_PER_DST, num_destinations)); |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 1581 | for (dst_cnt = i = 0; i < rename_dst_nr; i++) { |
Elijah Newren | 9db2ac5 | 2020-12-11 09:08:47 +0000 | [diff] [blame] | 1582 | struct diff_filespec *two = rename_dst[i].p->two; |
Junio C Hamano | 6d24ad9 | 2008-01-29 20:54:56 -0800 | [diff] [blame] | 1583 | struct diff_score *m; |
| 1584 | |
Elijah Newren | 9db2ac5 | 2020-12-11 09:08:47 +0000 | [diff] [blame] | 1585 | if (rename_dst[i].is_rename) |
Elijah Newren | bd24aa2 | 2021-02-14 07:51:49 +0000 | [diff] [blame] | 1586 | continue; /* exact or basename match already handled */ |
Junio C Hamano | 6d24ad9 | 2008-01-29 20:54:56 -0800 | [diff] [blame] | 1587 | |
| 1588 | m = &mx[dst_cnt * NUM_CANDIDATE_PER_DST]; |
| 1589 | for (j = 0; j < NUM_CANDIDATE_PER_DST; j++) |
| 1590 | m[j].dst = -1; |
| 1591 | |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 1592 | for (j = 0; j < rename_src_nr; j++) { |
Junio C Hamano | e88d6bc | 2011-01-06 13:50:05 -0800 | [diff] [blame] | 1593 | struct diff_filespec *one = rename_src[j].p->one; |
Junio C Hamano | 6d24ad9 | 2008-01-29 20:54:56 -0800 | [diff] [blame] | 1594 | struct diff_score this_src; |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 1595 | |
Elijah Newren | 829514c | 2021-02-14 07:35:01 +0000 | [diff] [blame] | 1596 | assert(!one->rename_used || want_copies || break_idx); |
Elijah Newren | f15eb7c | 2021-02-03 20:03:46 +0000 | [diff] [blame] | 1597 | |
Junio C Hamano | f31027c | 2011-01-06 13:50:06 -0800 | [diff] [blame] | 1598 | if (skip_unmodified && |
| 1599 | diff_unmodified_pair(rename_src[j].p)) |
| 1600 | continue; |
| 1601 | |
Nguyễn Thái Ngọc Duy | b78ea5f | 2018-09-21 17:57:19 +0200 | [diff] [blame] | 1602 | this_src.score = estimate_similarity(options->repo, |
| 1603 | one, two, |
Jonathan Tan | 95acf11 | 2020-04-07 15:11:43 -0700 | [diff] [blame] | 1604 | minimum_score, |
Elijah Newren | d331dd3 | 2021-06-22 08:04:39 +0000 | [diff] [blame] | 1605 | &dpf_options); |
Junio C Hamano | 6d24ad9 | 2008-01-29 20:54:56 -0800 | [diff] [blame] | 1606 | this_src.name_score = basename_same(one, two); |
| 1607 | this_src.dst = i; |
| 1608 | this_src.src = j; |
| 1609 | record_if_better(m, &this_src); |
Junio C Hamano | 809809b | 2009-11-20 22:13:47 -0800 | [diff] [blame] | 1610 | /* |
| 1611 | * Once we run estimate_similarity, |
| 1612 | * We do not need the text anymore. |
| 1613 | */ |
Junio C Hamano | 8ae92e6 | 2007-10-02 21:01:03 -0700 | [diff] [blame] | 1614 | diff_free_filespec_blob(one); |
Junio C Hamano | 809809b | 2009-11-20 22:13:47 -0800 | [diff] [blame] | 1615 | diff_free_filespec_blob(two); |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 1616 | } |
| 1617 | dst_cnt++; |
Elijah Newren | 81c4bf0 | 2020-12-11 09:08:43 +0000 | [diff] [blame] | 1618 | display_progress(progress, |
Elijah Newren | f15eb7c | 2021-02-03 20:03:46 +0000 | [diff] [blame] | 1619 | (uint64_t)dst_cnt * (uint64_t)num_sources); |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 1620 | } |
Jeff King | 3ac942d | 2011-02-20 04:51:16 -0500 | [diff] [blame] | 1621 | stop_progress(&progress); |
Junio C Hamano | 6d24ad9 | 2008-01-29 20:54:56 -0800 | [diff] [blame] | 1622 | |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 1623 | /* cost matrix sorted by most to least similar pair */ |
Johannes Schindelin | 2049b8d | 2019-09-30 10:21:55 -0700 | [diff] [blame] | 1624 | STABLE_QSORT(mx, dst_cnt * NUM_CANDIDATE_PER_DST, score_compare); |
Junio C Hamano | 6d24ad9 | 2008-01-29 20:54:56 -0800 | [diff] [blame] | 1625 | |
Elijah Newren | 1ad69eb | 2021-02-27 00:30:46 +0000 | [diff] [blame] | 1626 | rename_count += find_renames(mx, dst_cnt, minimum_score, 0, |
| 1627 | &info, dirs_removed); |
Elijah Newren | f15eb7c | 2021-02-03 20:03:46 +0000 | [diff] [blame] | 1628 | if (want_copies) |
Elijah Newren | 1ad69eb | 2021-02-27 00:30:46 +0000 | [diff] [blame] | 1629 | rename_count += find_renames(mx, dst_cnt, minimum_score, 1, |
| 1630 | &info, dirs_removed); |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 1631 | free(mx); |
Elijah Newren | 557ac03 | 2021-01-23 22:01:12 -0800 | [diff] [blame] | 1632 | trace2_region_leave("diff", "inexact renames", options->repo); |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 1633 | |
Junio C Hamano | 15d061b | 2005-05-27 15:55:55 -0700 | [diff] [blame] | 1634 | cleanup: |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 1635 | /* 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] | 1636 | * 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] | 1637 | */ |
Elijah Newren | 557ac03 | 2021-01-23 22:01:12 -0800 | [diff] [blame] | 1638 | trace2_region_enter("diff", "write back to queue", options->repo); |
Bo Yang | 9ca5df9 | 2010-05-06 21:52:27 -0700 | [diff] [blame] | 1639 | DIFF_QUEUE_CLEAR(&outq); |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 1640 | for (i = 0; i < q->nr; i++) { |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 1641 | struct diff_filepair *p = q->queue[i]; |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 1642 | struct diff_filepair *pair_to_free = NULL; |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 1643 | |
Martin von Zweigbergk | d7c9bf2 | 2011-03-23 22:41:01 -0400 | [diff] [blame] | 1644 | if (DIFF_PAIR_UNMERGED(p)) { |
| 1645 | diff_q(&outq, p); |
| 1646 | } |
| 1647 | else if (!DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two)) { |
Elijah Newren | 9db2ac5 | 2020-12-11 09:08:47 +0000 | [diff] [blame] | 1648 | /* Creation */ |
| 1649 | diff_q(&outq, p); |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 1650 | } |
Junio C Hamano | 2cd6888 | 2005-05-30 00:08:07 -0700 | [diff] [blame] | 1651 | else if (DIFF_FILE_VALID(p->one) && !DIFF_FILE_VALID(p->two)) { |
| 1652 | /* |
| 1653 | * Deletion |
| 1654 | * |
Junio C Hamano | f345b0a | 2005-05-30 00:08:37 -0700 | [diff] [blame] | 1655 | * We would output this delete record if: |
| 1656 | * |
| 1657 | * (1) this is a broken delete and the counterpart |
| 1658 | * broken create remains in the output; or |
Junio C Hamano | 5098baf | 2005-09-15 16:13:43 -0700 | [diff] [blame] | 1659 | * (2) this is not a broken delete, and rename_dst |
| 1660 | * does not have a rename/copy to move p->one->path |
| 1661 | * out of existence. |
Junio C Hamano | f345b0a | 2005-05-30 00:08:37 -0700 | [diff] [blame] | 1662 | * |
| 1663 | * Otherwise, the counterpart broken create |
| 1664 | * has been turned into a rename-edit; or |
| 1665 | * delete did not have a matching create to |
| 1666 | * begin with. |
Junio C Hamano | 2cd6888 | 2005-05-30 00:08:07 -0700 | [diff] [blame] | 1667 | */ |
Junio C Hamano | f345b0a | 2005-05-30 00:08:37 -0700 | [diff] [blame] | 1668 | if (DIFF_PAIR_BROKEN(p)) { |
| 1669 | /* broken delete */ |
Elijah Newren | 9db2ac5 | 2020-12-11 09:08:47 +0000 | [diff] [blame] | 1670 | struct diff_rename_dst *dst = locate_rename_dst(p); |
| 1671 | if (!dst) |
| 1672 | BUG("tracking failed somehow; failed to find associated dst for broken pair"); |
| 1673 | if (dst->is_rename) |
Junio C Hamano | f345b0a | 2005-05-30 00:08:37 -0700 | [diff] [blame] | 1674 | /* counterpart is now rename/copy */ |
| 1675 | pair_to_free = p; |
| 1676 | } |
| 1677 | else { |
Linus Torvalds | 6447971 | 2007-10-25 11:20:56 -0700 | [diff] [blame] | 1678 | if (p->one->rename_used) |
Junio C Hamano | f345b0a | 2005-05-30 00:08:37 -0700 | [diff] [blame] | 1679 | /* this path remains */ |
| 1680 | pair_to_free = p; |
| 1681 | } |
Junio C Hamano | 2cd6888 | 2005-05-30 00:08:07 -0700 | [diff] [blame] | 1682 | |
Elijah Newren | 9db2ac5 | 2020-12-11 09:08:47 +0000 | [diff] [blame] | 1683 | if (!pair_to_free) |
Junio C Hamano | 2cd6888 | 2005-05-30 00:08:07 -0700 | [diff] [blame] | 1684 | diff_q(&outq, p); |
| 1685 | } |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 1686 | else if (!diff_unmodified_pair(p)) |
Junio C Hamano | 15d061b | 2005-05-27 15:55:55 -0700 | [diff] [blame] | 1687 | /* all the usual ones need to be kept */ |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 1688 | diff_q(&outq, p); |
Junio C Hamano | 15d061b | 2005-05-27 15:55:55 -0700 | [diff] [blame] | 1689 | else |
Elijah Newren | 356da0f | 2021-06-08 16:11:41 +0000 | [diff] [blame] | 1690 | /* no need to keep unmodified pairs */ |
Junio C Hamano | 15d061b | 2005-05-27 15:55:55 -0700 | [diff] [blame] | 1691 | pair_to_free = p; |
| 1692 | |
Junio C Hamano | 226406f | 2005-05-27 15:50:30 -0700 | [diff] [blame] | 1693 | if (pair_to_free) |
Elijah Newren | f239fff | 2021-07-30 11:47:42 +0000 | [diff] [blame] | 1694 | pool_diff_free_filepair(pool, pair_to_free); |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 1695 | } |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 1696 | diff_debug_queue("done copying original", &outq); |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 1697 | |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 1698 | free(q->queue); |
| 1699 | *q = outq; |
| 1700 | diff_debug_queue("done collapsing", q); |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 1701 | |
Linus Torvalds | 9fb8841 | 2007-10-25 11:19:10 -0700 | [diff] [blame] | 1702 | for (i = 0; i < rename_dst_nr; i++) |
Elijah Newren | 9db2ac5 | 2020-12-11 09:08:47 +0000 | [diff] [blame] | 1703 | if (rename_dst[i].filespec_to_free) |
Elijah Newren | f239fff | 2021-07-30 11:47:42 +0000 | [diff] [blame] | 1704 | pool_free_filespec(pool, rename_dst[i].filespec_to_free); |
Junio C Hamano | 5098baf | 2005-09-15 16:13:43 -0700 | [diff] [blame] | 1705 | |
Elijah Newren | b147301 | 2021-02-27 00:30:45 +0000 | [diff] [blame] | 1706 | cleanup_dir_rename_info(&info, dirs_removed, dir_rename_count != NULL); |
Ævar Arnfjörð Bjarmason | 6a83d90 | 2017-06-15 23:15:46 +0000 | [diff] [blame] | 1707 | FREE_AND_NULL(rename_dst); |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 1708 | rename_dst_nr = rename_dst_alloc = 0; |
Ævar Arnfjörð Bjarmason | 6a83d90 | 2017-06-15 23:15:46 +0000 | [diff] [blame] | 1709 | FREE_AND_NULL(rename_src); |
Junio C Hamano | 25d5ea4 | 2005-05-24 01:10:48 -0700 | [diff] [blame] | 1710 | rename_src_nr = rename_src_alloc = 0; |
Elijah Newren | 9db2ac5 | 2020-12-11 09:08:47 +0000 | [diff] [blame] | 1711 | if (break_idx) { |
| 1712 | strintmap_clear(break_idx); |
| 1713 | FREE_AND_NULL(break_idx); |
| 1714 | } |
Elijah Newren | 557ac03 | 2021-01-23 22:01:12 -0800 | [diff] [blame] | 1715 | trace2_region_leave("diff", "write back to queue", options->repo); |
Junio C Hamano | 427dcb4 | 2005-05-21 02:39:09 -0700 | [diff] [blame] | 1716 | return; |
| 1717 | } |
Elijah Newren | 0c4fd73 | 2021-02-27 00:30:42 +0000 | [diff] [blame] | 1718 | |
| 1719 | void diffcore_rename(struct diff_options *options) |
| 1720 | { |
Elijah Newren | f239fff | 2021-07-30 11:47:42 +0000 | [diff] [blame] | 1721 | diffcore_rename_extended(options, NULL, NULL, NULL, NULL, NULL); |
Elijah Newren | 0c4fd73 | 2021-02-27 00:30:42 +0000 | [diff] [blame] | 1722 | } |