Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Helper functions for tree diff generation |
| 3 | */ |
Elijah Newren | 5e3f94d | 2023-04-22 20:17:23 +0000 | [diff] [blame] | 4 | #include "git-compat-util.h" |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 5 | #include "diff.h" |
Linus Torvalds | 750f7b6 | 2007-06-19 14:22:46 -0700 | [diff] [blame] | 6 | #include "diffcore.h" |
Elijah Newren | df6e874 | 2023-05-16 06:34:00 +0000 | [diff] [blame] | 7 | #include "hash.h" |
Peter Eriksen | 8e44025 | 2006-04-02 14:44:09 +0200 | [diff] [blame] | 8 | #include "tree.h" |
Elijah Newren | 0e312ea | 2023-04-22 20:17:28 +0000 | [diff] [blame] | 9 | #include "tree-walk.h" |
Jeff King | 7b61bd1 | 2023-08-31 02:22:08 -0400 | [diff] [blame] | 10 | #include "environment.h" |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 11 | |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 12 | /* |
Elijah Newren | 23a517e | 2023-04-22 20:17:19 +0000 | [diff] [blame] | 13 | * Some mode bits are also used internally for computations. |
| 14 | * |
| 15 | * They *must* not overlap with any valid modes, and they *must* not be emitted |
| 16 | * to outside world - i.e. appear on disk or network. In other words, it's just |
| 17 | * temporary fields, which we internally use, but they have to stay in-house. |
| 18 | * |
| 19 | * ( such approach is valid, as standard S_IF* fits into 16 bits, and in Git |
| 20 | * codebase mode is `unsigned int` which is assumed to be at least 32 bits ) |
| 21 | */ |
| 22 | |
| 23 | #define S_DIFFTREE_IFXMIN_NEQ 0x80000000 |
| 24 | |
| 25 | /* |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 26 | * internal mode marker, saying a tree entry != entry of tp[imin] |
| 27 | * (see ll_diff_tree_paths for what it means there) |
| 28 | * |
| 29 | * we will update/use/emit entry for diff only with it unset. |
| 30 | */ |
| 31 | #define S_IFXMIN_NEQ S_DIFFTREE_IFXMIN_NEQ |
Kirill Smelkov | b9081a6 | 2014-03-27 18:21:29 +0400 | [diff] [blame] | 32 | |
Jeff King | b8ba412 | 2016-06-07 18:53:00 -0400 | [diff] [blame] | 33 | #define FAST_ARRAY_ALLOC(x, nr) do { \ |
| 34 | if ((nr) <= 2) \ |
| 35 | (x) = xalloca((nr) * sizeof(*(x))); \ |
| 36 | else \ |
| 37 | ALLOC_ARRAY((x), nr); \ |
| 38 | } while(0) |
| 39 | #define FAST_ARRAY_FREE(x, nr) do { \ |
Carlo Marcelo Arenas Belón | 637799b | 2021-09-16 01:55:22 -0700 | [diff] [blame] | 40 | if ((nr) <= 2) \ |
| 41 | xalloca_free((x)); \ |
| 42 | else \ |
Jeff King | b8ba412 | 2016-06-07 18:53:00 -0400 | [diff] [blame] | 43 | free((x)); \ |
| 44 | } while(0) |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 45 | |
| 46 | static struct combine_diff_path *ll_diff_tree_paths( |
Brandon Williams | fda94b4 | 2017-05-30 10:31:06 -0700 | [diff] [blame] | 47 | struct combine_diff_path *p, const struct object_id *oid, |
| 48 | const struct object_id **parents_oid, int nparent, |
Jeff King | 7b61bd1 | 2023-08-31 02:22:08 -0400 | [diff] [blame] | 49 | struct strbuf *base, struct diff_options *opt, |
| 50 | int depth); |
SZEDER Gábor | 0ee3cb8 | 2020-06-05 13:00:27 +0000 | [diff] [blame] | 51 | static void ll_diff_tree_oid(const struct object_id *old_oid, |
| 52 | const struct object_id *new_oid, |
| 53 | struct strbuf *base, struct diff_options *opt); |
Kirill Smelkov | b9081a6 | 2014-03-27 18:21:29 +0400 | [diff] [blame] | 54 | |
Kirill Smelkov | 9bc0619 | 2014-02-24 20:21:41 +0400 | [diff] [blame] | 55 | /* |
| 56 | * Compare two tree entries, taking into account only path/S_ISDIR(mode), |
| 57 | * but not their sha1's. |
| 58 | * |
| 59 | * NOTE files and directories *always* compare differently, even when having |
| 60 | * the same name - thanks to base_name_compare(). |
Kirill Smelkov | 6ca844e | 2014-02-24 20:21:44 +0400 | [diff] [blame] | 61 | * |
| 62 | * NOTE empty (=invalid) descriptor(s) take part in comparison as +infty, |
| 63 | * so that they sort *after* valid tree entries. |
| 64 | * |
| 65 | * Due to this convention, if trees are scanned in sorted order, all |
| 66 | * non-empty descriptors will be processed first. |
Kirill Smelkov | 9bc0619 | 2014-02-24 20:21:41 +0400 | [diff] [blame] | 67 | */ |
| 68 | static int tree_entry_pathcmp(struct tree_desc *t1, struct tree_desc *t2) |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 69 | { |
Kirill Smelkov | 1a27a15 | 2014-02-24 20:21:43 +0400 | [diff] [blame] | 70 | struct name_entry *e1, *e2; |
| 71 | int cmp; |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 72 | |
Kirill Smelkov | 6ca844e | 2014-02-24 20:21:44 +0400 | [diff] [blame] | 73 | /* empty descriptors sort after valid tree entries */ |
| 74 | if (!t1->size) |
| 75 | return t2->size ? 1 : 0; |
| 76 | else if (!t2->size) |
| 77 | return -1; |
| 78 | |
Kirill Smelkov | 1a27a15 | 2014-02-24 20:21:43 +0400 | [diff] [blame] | 79 | e1 = &t1->entry; |
| 80 | e2 = &t2->entry; |
| 81 | cmp = base_name_compare(e1->path, tree_entry_len(e1), e1->mode, |
| 82 | e2->path, tree_entry_len(e2), e2->mode); |
Kirill Smelkov | 903bba6 | 2014-02-24 20:21:40 +0400 | [diff] [blame] | 83 | return cmp; |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Kirill Smelkov | d00e980 | 2014-02-24 20:21:38 +0400 | [diff] [blame] | 86 | |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 87 | /* |
| 88 | * convert path -> opt->diff_*() callbacks |
| 89 | * |
| 90 | * emits diff to first parent only, and tells diff tree-walker that we are done |
| 91 | * with p and it can be freed. |
| 92 | */ |
| 93 | static int emit_diff_first_parent_only(struct diff_options *opt, struct combine_diff_path *p) |
Kirill Smelkov | d00e980 | 2014-02-24 20:21:38 +0400 | [diff] [blame] | 94 | { |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 95 | struct combine_diff_parent *p0 = &p->parent[0]; |
| 96 | if (p->mode && p0->mode) { |
Brandon Williams | 94a0097 | 2017-05-30 10:30:49 -0700 | [diff] [blame] | 97 | opt->change(opt, p0->mode, p->mode, &p0->oid, &p->oid, |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 98 | 1, 1, p->path, 0, 0); |
Kirill Smelkov | d00e980 | 2014-02-24 20:21:38 +0400 | [diff] [blame] | 99 | } |
| 100 | else { |
Brandon Williams | c26022e | 2017-05-30 10:30:47 -0700 | [diff] [blame] | 101 | const struct object_id *oid; |
Kirill Smelkov | d00e980 | 2014-02-24 20:21:38 +0400 | [diff] [blame] | 102 | unsigned int mode; |
| 103 | int addremove; |
| 104 | |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 105 | if (p->mode) { |
Kirill Smelkov | d00e980 | 2014-02-24 20:21:38 +0400 | [diff] [blame] | 106 | addremove = '+'; |
Brandon Williams | c26022e | 2017-05-30 10:30:47 -0700 | [diff] [blame] | 107 | oid = &p->oid; |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 108 | mode = p->mode; |
Kirill Smelkov | d00e980 | 2014-02-24 20:21:38 +0400 | [diff] [blame] | 109 | } else { |
| 110 | addremove = '-'; |
Brandon Williams | c26022e | 2017-05-30 10:30:47 -0700 | [diff] [blame] | 111 | oid = &p0->oid; |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 112 | mode = p0->mode; |
Kirill Smelkov | d00e980 | 2014-02-24 20:21:38 +0400 | [diff] [blame] | 113 | } |
| 114 | |
Brandon Williams | c26022e | 2017-05-30 10:30:47 -0700 | [diff] [blame] | 115 | opt->add_remove(opt, addremove, mode, oid, 1, p->path, 0); |
Kirill Smelkov | d00e980 | 2014-02-24 20:21:38 +0400 | [diff] [blame] | 116 | } |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 117 | |
| 118 | return 0; /* we are done with p */ |
Kirill Smelkov | d00e980 | 2014-02-24 20:21:38 +0400 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 122 | /* |
| 123 | * Make a new combine_diff_path from path/mode/sha1 |
| 124 | * and append it to paths list tail. |
| 125 | * |
| 126 | * Memory for created elements could be reused: |
| 127 | * |
| 128 | * - if last->next == NULL, the memory is allocated; |
| 129 | * |
| 130 | * - if last->next != NULL, it is assumed that p=last->next was returned |
| 131 | * earlier by this function, and p->next was *not* modified. |
| 132 | * The memory is then reused from p. |
| 133 | * |
| 134 | * so for clients, |
| 135 | * |
| 136 | * - if you do need to keep the element |
| 137 | * |
| 138 | * p = path_appendnew(p, ...); |
| 139 | * process(p); |
| 140 | * p->next = NULL; |
| 141 | * |
| 142 | * - if you don't need to keep the element after processing |
| 143 | * |
| 144 | * pprev = p; |
| 145 | * p = path_appendnew(p, ...); |
| 146 | * process(p); |
| 147 | * p = pprev; |
| 148 | * ; don't forget to free tail->next in the end |
| 149 | * |
| 150 | * p->parent[] remains uninitialized. |
| 151 | */ |
| 152 | static struct combine_diff_path *path_appendnew(struct combine_diff_path *last, |
| 153 | int nparent, const struct strbuf *base, const char *path, int pathlen, |
Brandon Williams | 0e72462 | 2017-05-30 10:31:07 -0700 | [diff] [blame] | 154 | unsigned mode, const struct object_id *oid) |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 155 | { |
| 156 | struct combine_diff_path *p; |
Jeff King | 5b442c4 | 2016-02-19 06:21:30 -0500 | [diff] [blame] | 157 | size_t len = st_add(base->len, pathlen); |
| 158 | size_t alloclen = combine_diff_path_size(nparent, len); |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 159 | |
| 160 | /* if last->next is !NULL - it is a pre-allocated memory, we can reuse */ |
| 161 | p = last->next; |
| 162 | if (p && (alloclen > (intptr_t)p->next)) { |
Ævar Arnfjörð Bjarmason | 6a83d90 | 2017-06-15 23:15:46 +0000 | [diff] [blame] | 163 | FREE_AND_NULL(p); |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | if (!p) { |
| 167 | p = xmalloc(alloclen); |
| 168 | |
| 169 | /* |
| 170 | * until we go to it next round, .next holds how many bytes we |
| 171 | * allocated (for faster realloc - we don't need copying old data). |
| 172 | */ |
| 173 | p->next = (struct combine_diff_path *)(intptr_t)alloclen; |
| 174 | } |
| 175 | |
| 176 | last->next = p; |
| 177 | |
| 178 | p->path = (char *)&(p->parent[nparent]); |
| 179 | memcpy(p->path, base->buf, base->len); |
| 180 | memcpy(p->path + base->len, path, pathlen); |
| 181 | p->path[len] = 0; |
| 182 | p->mode = mode; |
brian m. carlson | 1422844 | 2021-04-26 01:02:56 +0000 | [diff] [blame] | 183 | oidcpy(&p->oid, oid ? oid : null_oid()); |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 184 | |
| 185 | return p; |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | * new path should be added to combine diff |
Kirill Smelkov | d00e980 | 2014-02-24 20:21:38 +0400 | [diff] [blame] | 190 | * |
| 191 | * 3 cases on how/when it should be called and behaves: |
| 192 | * |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 193 | * t, !tp -> path added, all parents lack it |
| 194 | * !t, tp -> path removed from all parents |
| 195 | * t, tp -> path modified/added |
| 196 | * (M for tp[i]=tp[imin], A otherwise) |
Kirill Smelkov | d00e980 | 2014-02-24 20:21:38 +0400 | [diff] [blame] | 197 | */ |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 198 | static struct combine_diff_path *emit_path(struct combine_diff_path *p, |
| 199 | struct strbuf *base, struct diff_options *opt, int nparent, |
| 200 | struct tree_desc *t, struct tree_desc *tp, |
Jeff King | 7b61bd1 | 2023-08-31 02:22:08 -0400 | [diff] [blame] | 201 | int imin, int depth) |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 202 | { |
Elijah Newren | 5ec1e72 | 2019-04-05 08:00:12 -0700 | [diff] [blame] | 203 | unsigned short mode; |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 204 | const char *path; |
Brandon Williams | fda94b4 | 2017-05-30 10:31:06 -0700 | [diff] [blame] | 205 | const struct object_id *oid; |
Kirill Smelkov | d00e980 | 2014-02-24 20:21:38 +0400 | [diff] [blame] | 206 | int pathlen; |
Nguyễn Thái Ngọc Duy | 4893267 | 2010-12-15 22:02:42 +0700 | [diff] [blame] | 207 | int old_baselen = base->len; |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 208 | int i, isdir, recurse = 0, emitthis = 1; |
Kirill Smelkov | d00e980 | 2014-02-24 20:21:38 +0400 | [diff] [blame] | 209 | |
| 210 | /* at least something has to be valid */ |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 211 | assert(t || tp); |
Kirill Smelkov | d00e980 | 2014-02-24 20:21:38 +0400 | [diff] [blame] | 212 | |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 213 | if (t) { |
Kirill Smelkov | d00e980 | 2014-02-24 20:21:38 +0400 | [diff] [blame] | 214 | /* path present in resulting tree */ |
Brandon Williams | fda94b4 | 2017-05-30 10:31:06 -0700 | [diff] [blame] | 215 | oid = tree_entry_extract(t, &path, &mode); |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 216 | pathlen = tree_entry_len(&t->entry); |
Kirill Smelkov | d00e980 | 2014-02-24 20:21:38 +0400 | [diff] [blame] | 217 | isdir = S_ISDIR(mode); |
| 218 | } else { |
| 219 | /* |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 220 | * a path was removed - take path from imin parent. Also take |
| 221 | * mode from that parent, to decide on recursion(1). |
| 222 | * |
| 223 | * 1) all modes for tp[i]=tp[imin] should be the same wrt |
| 224 | * S_ISDIR, thanks to base_name_compare(). |
Kirill Smelkov | d00e980 | 2014-02-24 20:21:38 +0400 | [diff] [blame] | 225 | */ |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 226 | tree_entry_extract(&tp[imin], &path, &mode); |
| 227 | pathlen = tree_entry_len(&tp[imin].entry); |
Kirill Smelkov | d00e980 | 2014-02-24 20:21:38 +0400 | [diff] [blame] | 228 | |
| 229 | isdir = S_ISDIR(mode); |
Brandon Williams | fda94b4 | 2017-05-30 10:31:06 -0700 | [diff] [blame] | 230 | oid = NULL; |
Kirill Smelkov | d00e980 | 2014-02-24 20:21:38 +0400 | [diff] [blame] | 231 | mode = 0; |
| 232 | } |
| 233 | |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 234 | if (opt->flags.recursive && isdir) { |
Kirill Smelkov | d00e980 | 2014-02-24 20:21:38 +0400 | [diff] [blame] | 235 | recurse = 1; |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 236 | emitthis = opt->flags.tree_in_recursive; |
Kirill Smelkov | d00e980 | 2014-02-24 20:21:38 +0400 | [diff] [blame] | 237 | } |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 238 | |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 239 | if (emitthis) { |
| 240 | int keep; |
| 241 | struct combine_diff_path *pprev = p; |
Brandon Williams | 0e72462 | 2017-05-30 10:31:07 -0700 | [diff] [blame] | 242 | p = path_appendnew(p, nparent, base, path, pathlen, mode, oid); |
Nguyễn Thái Ngọc Duy | 4893267 | 2010-12-15 22:02:42 +0700 | [diff] [blame] | 243 | |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 244 | for (i = 0; i < nparent; ++i) { |
| 245 | /* |
| 246 | * tp[i] is valid, if present and if tp[i]==tp[imin] - |
| 247 | * otherwise, we should ignore it. |
| 248 | */ |
| 249 | int tpi_valid = tp && !(tp[i].entry.mode & S_IFXMIN_NEQ); |
| 250 | |
Brandon Williams | fda94b4 | 2017-05-30 10:31:06 -0700 | [diff] [blame] | 251 | const struct object_id *oid_i; |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 252 | unsigned mode_i; |
| 253 | |
| 254 | p->parent[i].status = |
| 255 | !t ? DIFF_STATUS_DELETED : |
| 256 | tpi_valid ? |
| 257 | DIFF_STATUS_MODIFIED : |
| 258 | DIFF_STATUS_ADDED; |
| 259 | |
| 260 | if (tpi_valid) { |
brian m. carlson | ea82b2a | 2019-01-15 00:39:44 +0000 | [diff] [blame] | 261 | oid_i = &tp[i].entry.oid; |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 262 | mode_i = tp[i].entry.mode; |
| 263 | } |
| 264 | else { |
brian m. carlson | 1422844 | 2021-04-26 01:02:56 +0000 | [diff] [blame] | 265 | oid_i = null_oid(); |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 266 | mode_i = 0; |
| 267 | } |
| 268 | |
| 269 | p->parent[i].mode = mode_i; |
Brandon Williams | fda94b4 | 2017-05-30 10:31:06 -0700 | [diff] [blame] | 270 | oidcpy(&p->parent[i].oid, oid_i); |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | keep = 1; |
| 274 | if (opt->pathchange) |
| 275 | keep = opt->pathchange(opt, p); |
| 276 | |
| 277 | /* |
| 278 | * If a path was filtered or consumed - we don't need to add it |
| 279 | * to the list and can reuse its memory, leaving it as |
| 280 | * pre-allocated element on the tail. |
| 281 | * |
| 282 | * On the other hand, if path needs to be kept, we need to |
| 283 | * correct its .next to NULL, as it was pre-initialized to how |
| 284 | * much memory was allocated. |
| 285 | * |
| 286 | * see path_appendnew() for details. |
| 287 | */ |
| 288 | if (!keep) |
| 289 | p = pprev; |
| 290 | else |
| 291 | p->next = NULL; |
| 292 | } |
Kirill Smelkov | d00e980 | 2014-02-24 20:21:38 +0400 | [diff] [blame] | 293 | |
| 294 | if (recurse) { |
Brandon Williams | fda94b4 | 2017-05-30 10:31:06 -0700 | [diff] [blame] | 295 | const struct object_id **parents_oid; |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 296 | |
Brandon Williams | fda94b4 | 2017-05-30 10:31:06 -0700 | [diff] [blame] | 297 | FAST_ARRAY_ALLOC(parents_oid, nparent); |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 298 | for (i = 0; i < nparent; ++i) { |
| 299 | /* same rule as in emitthis */ |
| 300 | int tpi_valid = tp && !(tp[i].entry.mode & S_IFXMIN_NEQ); |
| 301 | |
brian m. carlson | ea82b2a | 2019-01-15 00:39:44 +0000 | [diff] [blame] | 302 | parents_oid[i] = tpi_valid ? &tp[i].entry.oid : NULL; |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | strbuf_add(base, path, pathlen); |
Nguyễn Thái Ngọc Duy | 4893267 | 2010-12-15 22:02:42 +0700 | [diff] [blame] | 306 | strbuf_addch(base, '/'); |
Jeff King | 7b61bd1 | 2023-08-31 02:22:08 -0400 | [diff] [blame] | 307 | p = ll_diff_tree_paths(p, oid, parents_oid, nparent, base, opt, |
| 308 | depth + 1); |
Brandon Williams | fda94b4 | 2017-05-30 10:31:06 -0700 | [diff] [blame] | 309 | FAST_ARRAY_FREE(parents_oid, nparent); |
Kirill Smelkov | d00e980 | 2014-02-24 20:21:38 +0400 | [diff] [blame] | 310 | } |
Nguyễn Thái Ngọc Duy | 4893267 | 2010-12-15 22:02:42 +0700 | [diff] [blame] | 311 | |
| 312 | strbuf_setlen(base, old_baselen); |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 313 | return p; |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Nguyễn Thái Ngọc Duy | 4893267 | 2010-12-15 22:02:42 +0700 | [diff] [blame] | 316 | static void skip_uninteresting(struct tree_desc *t, struct strbuf *base, |
Kirill Smelkov | e906612 | 2014-02-03 16:47:18 +0400 | [diff] [blame] | 317 | struct diff_options *opt) |
Linus Torvalds | 5d86501 | 2007-03-18 15:18:30 -0700 | [diff] [blame] | 318 | { |
Kirill Smelkov | e906612 | 2014-02-03 16:47:18 +0400 | [diff] [blame] | 319 | enum interesting match; |
| 320 | |
Linus Torvalds | 5d86501 | 2007-03-18 15:18:30 -0700 | [diff] [blame] | 321 | while (t->size) { |
Nguyễn Thái Ngọc Duy | 67022e0 | 2018-11-18 17:47:57 +0100 | [diff] [blame] | 322 | match = tree_entry_interesting(opt->repo->index, &t->entry, |
Junio C Hamano | 0ad927e | 2023-07-07 15:21:15 -0700 | [diff] [blame] | 323 | base, &opt->pathspec); |
Kirill Smelkov | e906612 | 2014-02-03 16:47:18 +0400 | [diff] [blame] | 324 | if (match) { |
| 325 | if (match == all_entries_not_interesting) |
Nguyễn Thái Ngọc Duy | 97d0b74 | 2011-03-25 16:34:20 +0700 | [diff] [blame] | 326 | t->size = 0; |
| 327 | break; |
Linus Torvalds | 5d86501 | 2007-03-18 15:18:30 -0700 | [diff] [blame] | 328 | } |
Nguyễn Thái Ngọc Duy | 97d0b74 | 2011-03-25 16:34:20 +0700 | [diff] [blame] | 329 | update_tree_entry(t); |
Linus Torvalds | 5d86501 | 2007-03-18 15:18:30 -0700 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
Linus Torvalds | 304de2d | 2007-03-17 20:06:24 -0700 | [diff] [blame] | 333 | |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 334 | /* |
Brandon Williams | fda94b4 | 2017-05-30 10:31:06 -0700 | [diff] [blame] | 335 | * generate paths for combined diff D(sha1,parents_oid[]) |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 336 | * |
| 337 | * Resulting paths are appended to combine_diff_path linked list, and also, are |
| 338 | * emitted on the go via opt->pathchange() callback, so it is possible to |
| 339 | * process the result as batch or incrementally. |
| 340 | * |
| 341 | * The paths are generated scanning new tree and all parents trees |
| 342 | * simultaneously, similarly to what diff_tree() was doing for 2 trees. |
| 343 | * The theory behind such scan is as follows: |
| 344 | * |
| 345 | * |
| 346 | * D(T,P1...Pn) calculation scheme |
| 347 | * ------------------------------- |
| 348 | * |
| 349 | * D(T,P1...Pn) = D(T,P1) ^ ... ^ D(T,Pn) (regarding resulting paths set) |
| 350 | * |
| 351 | * D(T,Pj) - diff between T..Pj |
| 352 | * D(T,P1...Pn) - combined diff from T to parents P1,...,Pn |
| 353 | * |
| 354 | * |
| 355 | * We start from all trees, which are sorted, and compare their entries in |
| 356 | * lock-step: |
| 357 | * |
| 358 | * T P1 Pn |
| 359 | * - - - |
| 360 | * |t| |p1| |pn| |
| 361 | * |-| |--| ... |--| imin = argmin(p1...pn) |
| 362 | * | | | | | | |
| 363 | * |-| |--| |--| |
| 364 | * |.| |. | |. | |
| 365 | * . . . |
| 366 | * . . . |
| 367 | * |
| 368 | * at any time there could be 3 cases: |
| 369 | * |
| 370 | * 1) t < p[imin]; |
| 371 | * 2) t > p[imin]; |
| 372 | * 3) t = p[imin]. |
| 373 | * |
| 374 | * Schematic deduction of what every case means, and what to do, follows: |
| 375 | * |
| 376 | * 1) t < p[imin] -> ∀j t ∉ Pj -> "+t" ∈ D(T,Pj) -> D += "+t"; t↓ |
| 377 | * |
| 378 | * 2) t > p[imin] |
| 379 | * |
| 380 | * 2.1) ∃j: pj > p[imin] -> "-p[imin]" ∉ D(T,Pj) -> D += ø; ∀ pi=p[imin] pi↓ |
| 381 | * 2.2) ∀i pi = p[imin] -> pi ∉ T -> "-pi" ∈ D(T,Pi) -> D += "-p[imin]"; ∀i pi↓ |
| 382 | * |
| 383 | * 3) t = p[imin] |
| 384 | * |
| 385 | * 3.1) ∃j: pj > p[imin] -> "+t" ∈ D(T,Pj) -> only pi=p[imin] remains to investigate |
| 386 | * 3.2) pi = p[imin] -> investigate δ(t,pi) |
| 387 | * | |
| 388 | * | |
| 389 | * v |
| 390 | * |
| 391 | * 3.1+3.2) looking at δ(t,pi) ∀i: pi=p[imin] - if all != ø -> |
| 392 | * |
| 393 | * ⎧δ(t,pi) - if pi=p[imin] |
| 394 | * -> D += ⎨ |
| 395 | * ⎩"+t" - if pi>p[imin] |
| 396 | * |
| 397 | * |
| 398 | * in any case t↓ ∀ pi=p[imin] pi↓ |
| 399 | * |
| 400 | * |
| 401 | * ~~~~~~~~ |
| 402 | * |
| 403 | * NOTE |
| 404 | * |
| 405 | * Usual diff D(A,B) is by definition the same as combined diff D(A,[B]), |
| 406 | * so this diff paths generator can, and is used, for plain diffs |
| 407 | * generation too. |
| 408 | * |
| 409 | * Please keep attention to the common D(A,[B]) case when working on the |
| 410 | * code, in order not to slow it down. |
| 411 | * |
| 412 | * NOTE |
| 413 | * nparent must be > 0. |
| 414 | */ |
| 415 | |
| 416 | |
| 417 | /* ∀ pi=p[imin] pi↓ */ |
| 418 | static inline void update_tp_entries(struct tree_desc *tp, int nparent) |
| 419 | { |
| 420 | int i; |
| 421 | for (i = 0; i < nparent; ++i) |
| 422 | if (!(tp[i].entry.mode & S_IFXMIN_NEQ)) |
| 423 | update_tree_entry(&tp[i]); |
| 424 | } |
| 425 | |
| 426 | static struct combine_diff_path *ll_diff_tree_paths( |
Brandon Williams | fda94b4 | 2017-05-30 10:31:06 -0700 | [diff] [blame] | 427 | struct combine_diff_path *p, const struct object_id *oid, |
| 428 | const struct object_id **parents_oid, int nparent, |
Jeff King | 7b61bd1 | 2023-08-31 02:22:08 -0400 | [diff] [blame] | 429 | struct strbuf *base, struct diff_options *opt, |
| 430 | int depth) |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 431 | { |
| 432 | struct tree_desc t, *tp; |
| 433 | void *ttree, **tptree; |
| 434 | int i; |
| 435 | |
Jeff King | 7b61bd1 | 2023-08-31 02:22:08 -0400 | [diff] [blame] | 436 | if (depth > max_allowed_tree_depth) |
| 437 | die("exceeded maximum allowed tree depth"); |
| 438 | |
Jeff King | b8ba412 | 2016-06-07 18:53:00 -0400 | [diff] [blame] | 439 | FAST_ARRAY_ALLOC(tp, nparent); |
| 440 | FAST_ARRAY_ALLOC(tptree, nparent); |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 441 | |
| 442 | /* |
| 443 | * load parents first, as they are probably already cached. |
| 444 | * |
| 445 | * ( log_tree_diff() parses commit->parent before calling here via |
Brandon Williams | 66f414f | 2017-05-30 10:31:03 -0700 | [diff] [blame] | 446 | * diff_tree_oid(parent, commit) ) |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 447 | */ |
| 448 | for (i = 0; i < nparent; ++i) |
Nguyễn Thái Ngọc Duy | 5e57580 | 2019-06-27 16:28:48 +0700 | [diff] [blame] | 449 | tptree[i] = fill_tree_descriptor(opt->repo, &tp[i], parents_oid[i]); |
| 450 | ttree = fill_tree_descriptor(opt->repo, &t, oid); |
Kirill Smelkov | 52894e7 | 2014-03-27 18:24:38 +0400 | [diff] [blame] | 451 | |
Nguyễn Thái Ngọc Duy | bc96cc8 | 2010-12-15 22:02:44 +0700 | [diff] [blame] | 452 | /* Enable recursion indefinitely */ |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 453 | opt->pathspec.recursive = opt->flags.recursive; |
Nguyễn Thái Ngọc Duy | bc96cc8 | 2010-12-15 22:02:44 +0700 | [diff] [blame] | 454 | |
Linus Torvalds | 5d86501 | 2007-03-18 15:18:30 -0700 | [diff] [blame] | 455 | for (;;) { |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 456 | int imin, cmp; |
Kirill Smelkov | 903bba6 | 2014-02-24 20:21:40 +0400 | [diff] [blame] | 457 | |
Junio C Hamano | 28b9264 | 2011-05-31 09:14:17 -0700 | [diff] [blame] | 458 | if (diff_can_quit_early(opt)) |
Junio C Hamano | 822cac0 | 2007-03-14 11:12:51 -0700 | [diff] [blame] | 459 | break; |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 460 | |
Derrick Stolee | b16a827 | 2020-09-16 14:07:52 -0400 | [diff] [blame] | 461 | if (opt->max_changes && diff_queued_diff.nr > opt->max_changes) |
Derrick Stolee | e369698 | 2020-03-30 00:31:27 +0000 | [diff] [blame] | 462 | break; |
| 463 | |
Nguyễn Thái Ngọc Duy | 66f1362 | 2010-12-15 22:02:38 +0700 | [diff] [blame] | 464 | if (opt->pathspec.nr) { |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 465 | skip_uninteresting(&t, base, opt); |
| 466 | for (i = 0; i < nparent; i++) |
| 467 | skip_uninteresting(&tp[i], base, opt); |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 468 | } |
Kirill Smelkov | 5dfb2bb | 2014-02-24 20:21:39 +0400 | [diff] [blame] | 469 | |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 470 | /* comparing is finished when all trees are done */ |
| 471 | if (!t.size) { |
| 472 | int done = 1; |
| 473 | for (i = 0; i < nparent; ++i) |
| 474 | if (tp[i].size) { |
| 475 | done = 0; |
| 476 | break; |
| 477 | } |
| 478 | if (done) |
| 479 | break; |
| 480 | } |
Kirill Smelkov | 5dfb2bb | 2014-02-24 20:21:39 +0400 | [diff] [blame] | 481 | |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 482 | /* |
| 483 | * lookup imin = argmin(p1...pn), |
| 484 | * mark entries whether they =p[imin] along the way |
| 485 | */ |
| 486 | imin = 0; |
| 487 | tp[0].entry.mode &= ~S_IFXMIN_NEQ; |
| 488 | |
| 489 | for (i = 1; i < nparent; ++i) { |
| 490 | cmp = tree_entry_pathcmp(&tp[i], &tp[imin]); |
| 491 | if (cmp < 0) { |
| 492 | imin = i; |
| 493 | tp[i].entry.mode &= ~S_IFXMIN_NEQ; |
| 494 | } |
| 495 | else if (cmp == 0) { |
| 496 | tp[i].entry.mode &= ~S_IFXMIN_NEQ; |
| 497 | } |
| 498 | else { |
| 499 | tp[i].entry.mode |= S_IFXMIN_NEQ; |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | /* fixup markings for entries before imin */ |
| 504 | for (i = 0; i < imin; ++i) |
| 505 | tp[i].entry.mode |= S_IFXMIN_NEQ; /* pi > p[imin] */ |
| 506 | |
| 507 | |
| 508 | |
| 509 | /* compare t vs p[imin] */ |
| 510 | cmp = tree_entry_pathcmp(&t, &tp[imin]); |
| 511 | |
| 512 | /* t = p[imin] */ |
Kirill Smelkov | 5dfb2bb | 2014-02-24 20:21:39 +0400 | [diff] [blame] | 513 | if (cmp == 0) { |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 514 | /* are either pi > p[imin] or diff(t,pi) != ø ? */ |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 515 | if (!opt->flags.find_copies_harder) { |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 516 | for (i = 0; i < nparent; ++i) { |
| 517 | /* p[i] > p[imin] */ |
| 518 | if (tp[i].entry.mode & S_IFXMIN_NEQ) |
| 519 | continue; |
Kirill Smelkov | 903bba6 | 2014-02-24 20:21:40 +0400 | [diff] [blame] | 520 | |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 521 | /* diff(t,pi) != ø */ |
brian m. carlson | ea82b2a | 2019-01-15 00:39:44 +0000 | [diff] [blame] | 522 | if (!oideq(&t.entry.oid, &tp[i].entry.oid) || |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 523 | (t.entry.mode != tp[i].entry.mode)) |
| 524 | continue; |
| 525 | |
| 526 | goto skip_emit_t_tp; |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | /* D += {δ(t,pi) if pi=p[imin]; "+a" if pi > p[imin]} */ |
| 531 | p = emit_path(p, base, opt, nparent, |
Jeff King | 7b61bd1 | 2023-08-31 02:22:08 -0400 | [diff] [blame] | 532 | &t, tp, imin, depth); |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 533 | |
| 534 | skip_emit_t_tp: |
| 535 | /* t↓, ∀ pi=p[imin] pi↓ */ |
| 536 | update_tree_entry(&t); |
| 537 | update_tp_entries(tp, nparent); |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 538 | } |
Kirill Smelkov | 5dfb2bb | 2014-02-24 20:21:39 +0400 | [diff] [blame] | 539 | |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 540 | /* t < p[imin] */ |
Kirill Smelkov | 5dfb2bb | 2014-02-24 20:21:39 +0400 | [diff] [blame] | 541 | else if (cmp < 0) { |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 542 | /* D += "+t" */ |
| 543 | p = emit_path(p, base, opt, nparent, |
Jeff King | 7b61bd1 | 2023-08-31 02:22:08 -0400 | [diff] [blame] | 544 | &t, /*tp=*/NULL, -1, depth); |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 545 | |
| 546 | /* t↓ */ |
| 547 | update_tree_entry(&t); |
Kirill Smelkov | 5dfb2bb | 2014-02-24 20:21:39 +0400 | [diff] [blame] | 548 | } |
| 549 | |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 550 | /* t > p[imin] */ |
Kirill Smelkov | 5dfb2bb | 2014-02-24 20:21:39 +0400 | [diff] [blame] | 551 | else { |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 552 | /* ∀i pi=p[imin] -> D += "-p[imin]" */ |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 553 | if (!opt->flags.find_copies_harder) { |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 554 | for (i = 0; i < nparent; ++i) |
| 555 | if (tp[i].entry.mode & S_IFXMIN_NEQ) |
| 556 | goto skip_emit_tp; |
| 557 | } |
| 558 | |
| 559 | p = emit_path(p, base, opt, nparent, |
Jeff King | 7b61bd1 | 2023-08-31 02:22:08 -0400 | [diff] [blame] | 560 | /*t=*/NULL, tp, imin, depth); |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 561 | |
| 562 | skip_emit_tp: |
| 563 | /* ∀ pi=p[imin] pi↓ */ |
| 564 | update_tp_entries(tp, nparent); |
Kirill Smelkov | 5dfb2bb | 2014-02-24 20:21:39 +0400 | [diff] [blame] | 565 | } |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 566 | } |
Nguyễn Thái Ngọc Duy | 4893267 | 2010-12-15 22:02:42 +0700 | [diff] [blame] | 567 | |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 568 | free(ttree); |
| 569 | for (i = nparent-1; i >= 0; i--) |
| 570 | free(tptree[i]); |
Jeff King | b8ba412 | 2016-06-07 18:53:00 -0400 | [diff] [blame] | 571 | FAST_ARRAY_FREE(tptree, nparent); |
| 572 | FAST_ARRAY_FREE(tp, nparent); |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 573 | |
| 574 | return p; |
| 575 | } |
| 576 | |
| 577 | struct combine_diff_path *diff_tree_paths( |
Brandon Williams | fda94b4 | 2017-05-30 10:31:06 -0700 | [diff] [blame] | 578 | struct combine_diff_path *p, const struct object_id *oid, |
| 579 | const struct object_id **parents_oid, int nparent, |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 580 | struct strbuf *base, struct diff_options *opt) |
| 581 | { |
Jeff King | 7b61bd1 | 2023-08-31 02:22:08 -0400 | [diff] [blame] | 582 | p = ll_diff_tree_paths(p, oid, parents_oid, nparent, base, opt, 0); |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 583 | |
| 584 | /* |
| 585 | * free pre-allocated last element, if any |
| 586 | * (see path_appendnew() for details about why) |
| 587 | */ |
Ævar Arnfjörð Bjarmason | ce528de | 2018-08-17 13:02:50 +0000 | [diff] [blame] | 588 | FREE_AND_NULL(p->next); |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 589 | |
| 590 | return p; |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Linus Torvalds | 750f7b6 | 2007-06-19 14:22:46 -0700 | [diff] [blame] | 593 | /* |
| 594 | * Does it look like the resulting diff might be due to a rename? |
| 595 | * - single entry |
| 596 | * - not a valid previous file |
| 597 | */ |
| 598 | static inline int diff_might_be_rename(void) |
| 599 | { |
| 600 | return diff_queued_diff.nr == 1 && |
| 601 | !DIFF_FILE_VALID(diff_queued_diff.queue[0]->one); |
| 602 | } |
| 603 | |
Brandon Williams | 128be87 | 2017-05-30 10:31:05 -0700 | [diff] [blame] | 604 | static void try_to_follow_renames(const struct object_id *old_oid, |
| 605 | const struct object_id *new_oid, |
| 606 | struct strbuf *base, struct diff_options *opt) |
Linus Torvalds | 750f7b6 | 2007-06-19 14:22:46 -0700 | [diff] [blame] | 607 | { |
| 608 | struct diff_options diff_opts; |
Linus Torvalds | 9f38e1e | 2007-06-21 10:22:59 -0700 | [diff] [blame] | 609 | struct diff_queue_struct *q = &diff_queued_diff; |
| 610 | struct diff_filepair *choice; |
Linus Torvalds | 750f7b6 | 2007-06-19 14:22:46 -0700 | [diff] [blame] | 611 | int i; |
| 612 | |
Nguyễn Thái Ngọc Duy | 8f4f8f4 | 2013-07-14 15:35:36 +0700 | [diff] [blame] | 613 | /* |
| 614 | * follow-rename code is very specific, we need exactly one |
| 615 | * path. Magic that matches more than one path is not |
| 616 | * supported. |
| 617 | */ |
Nguyễn Thái Ngọc Duy | 5c6933d | 2013-07-14 15:36:06 +0700 | [diff] [blame] | 618 | GUARD_PATHSPEC(&opt->pathspec, PATHSPEC_FROMTOP | PATHSPEC_LITERAL); |
Nguyễn Thái Ngọc Duy | 8f4f8f4 | 2013-07-14 15:35:36 +0700 | [diff] [blame] | 619 | #if 0 |
| 620 | /* |
| 621 | * We should reject wildcards as well. Unfortunately we |
| 622 | * haven't got a reliable way to detect that 'foo\*bar' in |
| 623 | * fact has no wildcards. nowildcard_len is merely a hint for |
| 624 | * optimization. Let it slip for now until wildmatch is taught |
| 625 | * about dry-run mode and returns wildcard info. |
| 626 | */ |
| 627 | if (opt->pathspec.has_wildcard) |
Ævar Arnfjörð Bjarmason | a78537a | 2021-12-07 12:05:53 +0100 | [diff] [blame] | 628 | BUG("wildcards are not supported"); |
Nguyễn Thái Ngọc Duy | 8f4f8f4 | 2013-07-14 15:35:36 +0700 | [diff] [blame] | 629 | #endif |
| 630 | |
Linus Torvalds | 9f38e1e | 2007-06-21 10:22:59 -0700 | [diff] [blame] | 631 | /* Remove the file creation entry from the diff queue, and remember it */ |
| 632 | choice = q->queue[0]; |
| 633 | q->nr = 0; |
| 634 | |
Nguyễn Thái Ngọc Duy | dcf4286 | 2018-09-21 17:57:36 +0200 | [diff] [blame] | 635 | repo_diff_setup(opt->repo, &diff_opts); |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 636 | diff_opts.flags.recursive = 1; |
| 637 | diff_opts.flags.find_copies_harder = 1; |
Linus Torvalds | 750f7b6 | 2007-06-19 14:22:46 -0700 | [diff] [blame] | 638 | diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; |
Nguyễn Thái Ngọc Duy | 61588cc | 2013-07-14 15:36:01 +0700 | [diff] [blame] | 639 | diff_opts.single_follow = opt->pathspec.items[0].match; |
Linus Torvalds | 6dd4b66 | 2007-10-20 12:31:31 -0700 | [diff] [blame] | 640 | diff_opts.break_opt = opt->break_opt; |
Jeff King | dd98d88 | 2011-12-16 06:27:50 -0500 | [diff] [blame] | 641 | diff_opts.rename_score = opt->rename_score; |
Thomas Rast | 2845265 | 2012-08-03 14:16:24 +0200 | [diff] [blame] | 642 | diff_setup_done(&diff_opts); |
Brandon Williams | fda94b4 | 2017-05-30 10:31:06 -0700 | [diff] [blame] | 643 | ll_diff_tree_oid(old_oid, new_oid, base, &diff_opts); |
Linus Torvalds | 750f7b6 | 2007-06-19 14:22:46 -0700 | [diff] [blame] | 644 | diffcore_std(&diff_opts); |
Junio C Hamano | ed6e803 | 2016-06-02 14:09:22 -0700 | [diff] [blame] | 645 | clear_pathspec(&diff_opts.pathspec); |
Linus Torvalds | 750f7b6 | 2007-06-19 14:22:46 -0700 | [diff] [blame] | 646 | |
Linus Torvalds | 9f38e1e | 2007-06-21 10:22:59 -0700 | [diff] [blame] | 647 | /* Go through the new set of filepairing, and see if we find a more interesting one */ |
Junio C Hamano | 44c48a9 | 2010-08-13 12:17:45 -0700 | [diff] [blame] | 648 | opt->found_follow = 0; |
Linus Torvalds | 9f38e1e | 2007-06-21 10:22:59 -0700 | [diff] [blame] | 649 | for (i = 0; i < q->nr; i++) { |
| 650 | struct diff_filepair *p = q->queue[i]; |
Linus Torvalds | 750f7b6 | 2007-06-19 14:22:46 -0700 | [diff] [blame] | 651 | |
| 652 | /* |
| 653 | * Found a source? Not only do we use that for the new |
Linus Torvalds | 9f38e1e | 2007-06-21 10:22:59 -0700 | [diff] [blame] | 654 | * diff_queued_diff, we will also use that as the path in |
Linus Torvalds | 750f7b6 | 2007-06-19 14:22:46 -0700 | [diff] [blame] | 655 | * the future! |
| 656 | */ |
Nguyễn Thái Ngọc Duy | 66f1362 | 2010-12-15 22:02:38 +0700 | [diff] [blame] | 657 | if ((p->status == 'R' || p->status == 'C') && |
Nguyễn Thái Ngọc Duy | 61588cc | 2013-07-14 15:36:01 +0700 | [diff] [blame] | 658 | !strcmp(p->two->path, opt->pathspec.items[0].match)) { |
Nguyễn Thái Ngọc Duy | 9a08727 | 2013-07-14 15:35:59 +0700 | [diff] [blame] | 659 | const char *path[2]; |
| 660 | |
Linus Torvalds | 9f38e1e | 2007-06-21 10:22:59 -0700 | [diff] [blame] | 661 | /* Switch the file-pairs around */ |
| 662 | q->queue[i] = choice; |
| 663 | choice = p; |
| 664 | |
| 665 | /* Update the path we use from now on.. */ |
Nguyễn Thái Ngọc Duy | 9a08727 | 2013-07-14 15:35:59 +0700 | [diff] [blame] | 666 | path[0] = p->one->path; |
| 667 | path[1] = NULL; |
Junio C Hamano | ed6e803 | 2016-06-02 14:09:22 -0700 | [diff] [blame] | 668 | clear_pathspec(&opt->pathspec); |
Nguyễn Thái Ngọc Duy | 4a2d5ae | 2013-10-26 09:09:20 +0700 | [diff] [blame] | 669 | parse_pathspec(&opt->pathspec, |
| 670 | PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL, |
| 671 | PATHSPEC_LITERAL_PATH, "", path); |
Junio C Hamano | 44c48a9 | 2010-08-13 12:17:45 -0700 | [diff] [blame] | 672 | |
| 673 | /* |
| 674 | * The caller expects us to return a set of vanilla |
| 675 | * filepairs to let a later call to diffcore_std() |
| 676 | * it makes to sort the renames out (among other |
| 677 | * things), but we already have found renames |
| 678 | * ourselves; signal diffcore_std() not to muck with |
| 679 | * rename information. |
| 680 | */ |
| 681 | opt->found_follow = 1; |
Linus Torvalds | 750f7b6 | 2007-06-19 14:22:46 -0700 | [diff] [blame] | 682 | break; |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | /* |
Mike Ralphson | 3ea3c21 | 2009-04-17 19:13:30 +0100 | [diff] [blame] | 687 | * Then, discard all the non-relevant file pairs... |
Linus Torvalds | 750f7b6 | 2007-06-19 14:22:46 -0700 | [diff] [blame] | 688 | */ |
Linus Torvalds | 9f38e1e | 2007-06-21 10:22:59 -0700 | [diff] [blame] | 689 | for (i = 0; i < q->nr; i++) { |
| 690 | struct diff_filepair *p = q->queue[i]; |
| 691 | diff_free_filepair(p); |
| 692 | } |
| 693 | |
| 694 | /* |
| 695 | * .. and re-instate the one we want (which might be either the |
| 696 | * original one, or the rename/copy we found) |
| 697 | */ |
| 698 | q->queue[0] = choice; |
| 699 | q->nr = 1; |
Linus Torvalds | 750f7b6 | 2007-06-19 14:22:46 -0700 | [diff] [blame] | 700 | } |
| 701 | |
SZEDER Gábor | 0ee3cb8 | 2020-06-05 13:00:27 +0000 | [diff] [blame] | 702 | static void ll_diff_tree_oid(const struct object_id *old_oid, |
| 703 | const struct object_id *new_oid, |
| 704 | struct strbuf *base, struct diff_options *opt) |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 705 | { |
| 706 | struct combine_diff_path phead, *p; |
| 707 | pathchange_fn_t pathchange_old = opt->pathchange; |
| 708 | |
| 709 | phead.next = NULL; |
| 710 | opt->pathchange = emit_diff_first_parent_only; |
Brandon Williams | fda94b4 | 2017-05-30 10:31:06 -0700 | [diff] [blame] | 711 | diff_tree_paths(&phead, new_oid, &old_oid, 1, base, opt); |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 712 | |
| 713 | for (p = phead.next; p;) { |
| 714 | struct combine_diff_path *pprev = p; |
| 715 | p = p->next; |
| 716 | free(pprev); |
| 717 | } |
| 718 | |
| 719 | opt->pathchange = pathchange_old; |
Kirill Smelkov | 72441af | 2014-04-07 01:46:26 +0400 | [diff] [blame] | 720 | } |
| 721 | |
SZEDER Gábor | 0ee3cb8 | 2020-06-05 13:00:27 +0000 | [diff] [blame] | 722 | void diff_tree_oid(const struct object_id *old_oid, |
| 723 | const struct object_id *new_oid, |
| 724 | const char *base_str, struct diff_options *opt) |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 725 | { |
Kirill Smelkov | 12cd817 | 2014-03-27 18:22:07 +0400 | [diff] [blame] | 726 | struct strbuf base; |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 727 | |
Kirill Smelkov | 12cd817 | 2014-03-27 18:22:07 +0400 | [diff] [blame] | 728 | strbuf_init(&base, PATH_MAX); |
| 729 | strbuf_addstr(&base, base_str); |
| 730 | |
SZEDER Gábor | 0ee3cb8 | 2020-06-05 13:00:27 +0000 | [diff] [blame] | 731 | ll_diff_tree_oid(old_oid, new_oid, &base, opt); |
Brandon Williams | 0d1e0e7 | 2017-10-31 11:19:11 -0700 | [diff] [blame] | 732 | if (!*base_str && opt->flags.follow_renames && diff_might_be_rename()) |
Brandon Williams | 128be87 | 2017-05-30 10:31:05 -0700 | [diff] [blame] | 733 | try_to_follow_renames(old_oid, new_oid, &base, opt); |
Kirill Smelkov | 12cd817 | 2014-03-27 18:22:07 +0400 | [diff] [blame] | 734 | |
| 735 | strbuf_release(&base); |
Linus Torvalds | ac1b3d1 | 2005-10-20 21:05:05 -0700 | [diff] [blame] | 736 | } |
| 737 | |
SZEDER Gábor | 0ee3cb8 | 2020-06-05 13:00:27 +0000 | [diff] [blame] | 738 | void diff_root_tree_oid(const struct object_id *new_oid, |
| 739 | const char *base, |
| 740 | struct diff_options *opt) |
Rene Scharfe | 2b60356 | 2006-10-26 18:52:39 +0200 | [diff] [blame] | 741 | { |
SZEDER Gábor | 0ee3cb8 | 2020-06-05 13:00:27 +0000 | [diff] [blame] | 742 | diff_tree_oid(NULL, new_oid, base, opt); |
Rene Scharfe | 2b60356 | 2006-10-26 18:52:39 +0200 | [diff] [blame] | 743 | } |