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