]>
Commit | Line | Data |
---|---|---|
ae563542 | 1 | #include "cache.h" |
cbd53a21 | 2 | #include "object-store.h" |
ae563542 LT |
3 | #include "tag.h" |
4 | #include "blob.h" | |
5 | #include "tree.h" | |
6 | #include "commit.h" | |
a4a88b2b | 7 | #include "diff.h" |
a37eec63 | 8 | #include "diff-merges.h" |
ae563542 LT |
9 | #include "refs.h" |
10 | #include "revision.h" | |
23a3f0cb | 11 | #include "repository.h" |
7fefda5c | 12 | #include "graph.h" |
8ecae9b0 | 13 | #include "grep.h" |
8860fd42 | 14 | #include "reflog-walk.h" |
d7a17cad | 15 | #include "patch-ids.h" |
f35f5603 | 16 | #include "decorate.h" |
78892e32 | 17 | #include "log-tree.h" |
894a9d33 | 18 | #include "string-list.h" |
12da1d1f | 19 | #include "line-log.h" |
d72fbe81 | 20 | #include "mailmap.h" |
53d00b39 | 21 | #include "commit-slab.h" |
429bb40a | 22 | #include "dir.h" |
4fe10219 | 23 | #include "cache-tree.h" |
cb46d630 | 24 | #include "bisect.h" |
150e3001 | 25 | #include "packfile.h" |
be489d02 | 26 | #include "worktree.h" |
dbbcd44f | 27 | #include "strvec.h" |
64043556 | 28 | #include "commit-reach.h" |
f0d9cc41 | 29 | #include "commit-graph.h" |
b4542418 | 30 | #include "prio-queue.h" |
d5d2e935 | 31 | #include "hashmap.h" |
44570188 | 32 | #include "utf8.h" |
a56b9464 | 33 | #include "bloom.h" |
42e50e78 | 34 | #include "json-writer.h" |
ae563542 | 35 | |
cdcefbc9 LT |
36 | volatile show_early_output_fn_t show_early_output; |
37 | ||
cb46d630 AD |
38 | static const char *term_bad; |
39 | static const char *term_good; | |
40 | ||
87be2523 NTND |
41 | implement_shared_commit_slab(revision_sources, char *); |
42 | ||
3cb9d2b6 SG |
43 | static inline int want_ancestry(const struct rev_info *revs); |
44 | ||
de1e67d0 | 45 | void show_object_with_name(FILE *out, struct object *obj, const char *name) |
ae563542 | 46 | { |
de1e67d0 | 47 | const char *p; |
91f17516 | 48 | |
f2fd0760 | 49 | fprintf(out, "%s ", oid_to_hex(&obj->oid)); |
f9fb9d0e JK |
50 | for (p = name; *p && *p != '\n'; p++) |
51 | fputc(*p, out); | |
beba25ab | 52 | fputc('\n', out); |
91f17516 JH |
53 | } |
54 | ||
ae563542 LT |
55 | static void mark_blob_uninteresting(struct blob *blob) |
56 | { | |
c1ee9013 MK |
57 | if (!blob) |
58 | return; | |
ae563542 LT |
59 | if (blob->object.flags & UNINTERESTING) |
60 | return; | |
61 | blob->object.flags |= UNINTERESTING; | |
62 | } | |
63 | ||
b3c7eef9 NTND |
64 | static void mark_tree_contents_uninteresting(struct repository *r, |
65 | struct tree *tree) | |
ae563542 | 66 | { |
f75e53ed | 67 | struct tree_desc desc; |
4c068a98 | 68 | struct name_entry entry; |
ae563542 | 69 | |
a5411df0 | 70 | if (parse_tree_gently(tree, 1) < 0) |
ae563542 | 71 | return; |
f75e53ed | 72 | |
6fda5e51 | 73 | init_tree_desc(&desc, tree->buffer, tree->size); |
4c068a98 | 74 | while (tree_entry(&desc, &entry)) { |
4d1012c3 LT |
75 | switch (object_type(entry.mode)) { |
76 | case OBJ_TREE: | |
ea82b2a0 | 77 | mark_tree_uninteresting(r, lookup_tree(r, &entry.oid)); |
4d1012c3 LT |
78 | break; |
79 | case OBJ_BLOB: | |
ea82b2a0 | 80 | mark_blob_uninteresting(lookup_blob(r, &entry.oid)); |
4d1012c3 LT |
81 | break; |
82 | default: | |
83 | /* Subproject commit - not in this repository */ | |
84 | break; | |
85 | } | |
ae563542 | 86 | } |
f75e53ed LT |
87 | |
88 | /* | |
89 | * We don't care about the tree any more | |
90 | * after it has been marked uninteresting. | |
91 | */ | |
6e454b9a | 92 | free_tree_buffer(tree); |
ae563542 LT |
93 | } |
94 | ||
b3c7eef9 | 95 | void mark_tree_uninteresting(struct repository *r, struct tree *tree) |
2ac5e447 | 96 | { |
a2678df3 | 97 | struct object *obj; |
2ac5e447 JH |
98 | |
99 | if (!tree) | |
100 | return; | |
a2678df3 SN |
101 | |
102 | obj = &tree->object; | |
2ac5e447 JH |
103 | if (obj->flags & UNINTERESTING) |
104 | return; | |
105 | obj->flags |= UNINTERESTING; | |
b3c7eef9 | 106 | mark_tree_contents_uninteresting(r, tree); |
ae563542 LT |
107 | } |
108 | ||
d5d2e935 DS |
109 | struct path_and_oids_entry { |
110 | struct hashmap_entry ent; | |
111 | char *path; | |
112 | struct oidset trees; | |
113 | }; | |
114 | ||
115 | static int path_and_oids_cmp(const void *hashmap_cmp_fn_data, | |
939af16e EW |
116 | const struct hashmap_entry *eptr, |
117 | const struct hashmap_entry *entry_or_key, | |
d5d2e935 DS |
118 | const void *keydata) |
119 | { | |
939af16e EW |
120 | const struct path_and_oids_entry *e1, *e2; |
121 | ||
122 | e1 = container_of(eptr, const struct path_and_oids_entry, ent); | |
123 | e2 = container_of(entry_or_key, const struct path_and_oids_entry, ent); | |
124 | ||
d5d2e935 DS |
125 | return strcmp(e1->path, e2->path); |
126 | } | |
127 | ||
d5d2e935 DS |
128 | static void paths_and_oids_clear(struct hashmap *map) |
129 | { | |
130 | struct hashmap_iter iter; | |
131 | struct path_and_oids_entry *entry; | |
d5d2e935 | 132 | |
23dee69f | 133 | hashmap_for_each_entry(map, &iter, entry, ent /* member name */) { |
d5d2e935 DS |
134 | oidset_clear(&entry->trees); |
135 | free(entry->path); | |
136 | } | |
137 | ||
6da1a258 | 138 | hashmap_clear_and_free(map, struct path_and_oids_entry, ent); |
d5d2e935 DS |
139 | } |
140 | ||
141 | static void paths_and_oids_insert(struct hashmap *map, | |
142 | const char *path, | |
143 | const struct object_id *oid) | |
144 | { | |
145 | int hash = strhash(path); | |
146 | struct path_and_oids_entry key; | |
147 | struct path_and_oids_entry *entry; | |
148 | ||
d22245a2 | 149 | hashmap_entry_init(&key.ent, hash); |
d5d2e935 DS |
150 | |
151 | /* use a shallow copy for the lookup */ | |
152 | key.path = (char *)path; | |
153 | oidset_init(&key.trees, 0); | |
154 | ||
404ab78e | 155 | entry = hashmap_get_entry(map, &key, ent, NULL); |
b6c52416 | 156 | if (!entry) { |
ca56dadb | 157 | CALLOC_ARRAY(entry, 1); |
d22245a2 | 158 | hashmap_entry_init(&entry->ent, hash); |
d5d2e935 DS |
159 | entry->path = xstrdup(key.path); |
160 | oidset_init(&entry->trees, 16); | |
26b455f2 | 161 | hashmap_put(map, &entry->ent); |
d5d2e935 DS |
162 | } |
163 | ||
164 | oidset_insert(&entry->trees, oid); | |
165 | } | |
166 | ||
167 | static void add_children_by_path(struct repository *r, | |
168 | struct tree *tree, | |
169 | struct hashmap *map) | |
170 | { | |
171 | struct tree_desc desc; | |
172 | struct name_entry entry; | |
173 | ||
174 | if (!tree) | |
175 | return; | |
176 | ||
177 | if (parse_tree_gently(tree, 1) < 0) | |
178 | return; | |
179 | ||
180 | init_tree_desc(&desc, tree->buffer, tree->size); | |
181 | while (tree_entry(&desc, &entry)) { | |
182 | switch (object_type(entry.mode)) { | |
183 | case OBJ_TREE: | |
5fda3433 | 184 | paths_and_oids_insert(map, entry.path, &entry.oid); |
d5d2e935 DS |
185 | |
186 | if (tree->object.flags & UNINTERESTING) { | |
5fda3433 | 187 | struct tree *child = lookup_tree(r, &entry.oid); |
d5d2e935 DS |
188 | if (child) |
189 | child->object.flags |= UNINTERESTING; | |
190 | } | |
191 | break; | |
192 | case OBJ_BLOB: | |
193 | if (tree->object.flags & UNINTERESTING) { | |
5fda3433 | 194 | struct blob *child = lookup_blob(r, &entry.oid); |
d5d2e935 DS |
195 | if (child) |
196 | child->object.flags |= UNINTERESTING; | |
197 | } | |
198 | break; | |
199 | default: | |
200 | /* Subproject commit - not in this repository */ | |
201 | break; | |
202 | } | |
203 | } | |
204 | ||
205 | free_tree_buffer(tree); | |
206 | } | |
207 | ||
f1f5de44 DS |
208 | void mark_trees_uninteresting_sparse(struct repository *r, |
209 | struct oidset *trees) | |
210 | { | |
d5d2e935 | 211 | unsigned has_interesting = 0, has_uninteresting = 0; |
b19315d8 | 212 | struct hashmap map = HASHMAP_INIT(path_and_oids_cmp, NULL); |
d5d2e935 DS |
213 | struct hashmap_iter map_iter; |
214 | struct path_and_oids_entry *entry; | |
f1f5de44 DS |
215 | struct object_id *oid; |
216 | struct oidset_iter iter; | |
217 | ||
218 | oidset_iter_init(trees, &iter); | |
d5d2e935 DS |
219 | while ((!has_interesting || !has_uninteresting) && |
220 | (oid = oidset_iter_next(&iter))) { | |
f1f5de44 DS |
221 | struct tree *tree = lookup_tree(r, oid); |
222 | ||
223 | if (!tree) | |
224 | continue; | |
225 | ||
d5d2e935 DS |
226 | if (tree->object.flags & UNINTERESTING) |
227 | has_uninteresting = 1; | |
228 | else | |
229 | has_interesting = 1; | |
f1f5de44 | 230 | } |
d5d2e935 DS |
231 | |
232 | /* Do not walk unless we have both types of trees. */ | |
233 | if (!has_uninteresting || !has_interesting) | |
234 | return; | |
235 | ||
d5d2e935 DS |
236 | oidset_iter_init(trees, &iter); |
237 | while ((oid = oidset_iter_next(&iter))) { | |
238 | struct tree *tree = lookup_tree(r, oid); | |
239 | add_children_by_path(r, tree, &map); | |
240 | } | |
241 | ||
23dee69f | 242 | hashmap_for_each_entry(&map, &map_iter, entry, ent /* member name */) |
d5d2e935 DS |
243 | mark_trees_uninteresting_sparse(r, &entry->trees); |
244 | ||
245 | paths_and_oids_clear(&map); | |
f1f5de44 DS |
246 | } |
247 | ||
43fc643b JK |
248 | struct commit_stack { |
249 | struct commit **items; | |
250 | size_t nr, alloc; | |
251 | }; | |
252 | #define COMMIT_STACK_INIT { NULL, 0, 0 } | |
253 | ||
254 | static void commit_stack_push(struct commit_stack *stack, struct commit *commit) | |
255 | { | |
256 | ALLOC_GROW(stack->items, stack->nr + 1, stack->alloc); | |
257 | stack->items[stack->nr++] = commit; | |
258 | } | |
259 | ||
260 | static struct commit *commit_stack_pop(struct commit_stack *stack) | |
261 | { | |
262 | return stack->nr ? stack->items[--stack->nr] : NULL; | |
263 | } | |
264 | ||
265 | static void commit_stack_clear(struct commit_stack *stack) | |
266 | { | |
267 | FREE_AND_NULL(stack->items); | |
268 | stack->nr = stack->alloc = 0; | |
269 | } | |
270 | ||
8702b30f JK |
271 | static void mark_one_parent_uninteresting(struct commit *commit, |
272 | struct commit_stack *pending) | |
273 | { | |
274 | struct commit_list *l; | |
275 | ||
276 | if (commit->object.flags & UNINTERESTING) | |
277 | return; | |
278 | commit->object.flags |= UNINTERESTING; | |
279 | ||
280 | /* | |
281 | * Normally we haven't parsed the parent | |
282 | * yet, so we won't have a parent of a parent | |
283 | * here. However, it may turn out that we've | |
284 | * reached this commit some other way (where it | |
285 | * wasn't uninteresting), in which case we need | |
286 | * to mark its parents recursively too.. | |
287 | */ | |
288 | for (l = commit->parents; l; l = l->next) | |
289 | commit_stack_push(pending, l->item); | |
290 | } | |
291 | ||
ae563542 LT |
292 | void mark_parents_uninteresting(struct commit *commit) |
293 | { | |
43fc643b JK |
294 | struct commit_stack pending = COMMIT_STACK_INIT; |
295 | struct commit_list *l; | |
941ba8db NTND |
296 | |
297 | for (l = commit->parents; l; l = l->next) | |
8702b30f JK |
298 | mark_one_parent_uninteresting(l->item, &pending); |
299 | ||
300 | while (pending.nr > 0) | |
301 | mark_one_parent_uninteresting(commit_stack_pop(&pending), | |
302 | &pending); | |
43fc643b JK |
303 | |
304 | commit_stack_clear(&pending); | |
ae563542 LT |
305 | } |
306 | ||
20739490 | 307 | static void add_pending_object_with_path(struct rev_info *revs, |
ff5f5f26 | 308 | struct object *obj, |
20739490 JK |
309 | const char *name, unsigned mode, |
310 | const char *path) | |
ae563542 | 311 | { |
a4f66a78 | 312 | struct interpret_branch_name_options options = { 0 }; |
cc243c3c JH |
313 | if (!obj) |
314 | return; | |
aa27e461 | 315 | if (revs->no_walk && (obj->flags & UNINTERESTING)) |
f222abde | 316 | revs->no_walk = 0; |
105e4733 JH |
317 | if (revs->reflog_info && obj->type == OBJ_COMMIT) { |
318 | struct strbuf buf = STRBUF_INIT; | |
1d72b604 JK |
319 | size_t namelen = strlen(name); |
320 | int len = interpret_branch_name(name, namelen, &buf, &options); | |
105e4733 | 321 | |
1d72b604 | 322 | if (0 < len && len < namelen && buf.len) |
105e4733 | 323 | strbuf_addstr(&buf, name + len); |
d08565bf JK |
324 | add_reflog_for_walk(revs->reflog_info, |
325 | (struct commit *)obj, | |
326 | buf.buf[0] ? buf.buf: name); | |
105e4733 | 327 | strbuf_release(&buf); |
d08565bf | 328 | return; /* do not add the commit itself */ |
105e4733 | 329 | } |
20739490 JK |
330 | add_object_array_with_path(obj, name, &revs->pending, mode, path); |
331 | } | |
332 | ||
333 | static void add_pending_object_with_mode(struct rev_info *revs, | |
334 | struct object *obj, | |
335 | const char *name, unsigned mode) | |
336 | { | |
337 | add_pending_object_with_path(revs, obj, name, mode, NULL); | |
ae563542 LT |
338 | } |
339 | ||
ff5f5f26 MH |
340 | void add_pending_object(struct rev_info *revs, |
341 | struct object *obj, const char *name) | |
2d93b9fa JH |
342 | { |
343 | add_pending_object_with_mode(revs, obj, name, S_IFINVALID); | |
344 | } | |
345 | ||
3384a2df JH |
346 | void add_head_to_pending(struct rev_info *revs) |
347 | { | |
654b9a90 | 348 | struct object_id oid; |
3384a2df | 349 | struct object *obj; |
654b9a90 | 350 | if (get_oid("HEAD", &oid)) |
3384a2df | 351 | return; |
b3c7eef9 | 352 | obj = parse_object(revs->repo, &oid); |
3384a2df JH |
353 | if (!obj) |
354 | return; | |
355 | add_pending_object(revs, obj, "HEAD"); | |
356 | } | |
357 | ||
ff5f5f26 | 358 | static struct object *get_reference(struct rev_info *revs, const char *name, |
654b9a90 | 359 | const struct object_id *oid, |
ff5f5f26 | 360 | unsigned int flags) |
ae563542 LT |
361 | { |
362 | struct object *object; | |
363 | ||
ec0c5798 JT |
364 | /* |
365 | * If the repository has commit graphs, repo_parse_commit() avoids | |
366 | * reading the object buffer, so use it whenever possible. | |
367 | */ | |
368 | if (oid_object_info(revs->repo, oid, NULL) == OBJ_COMMIT) { | |
369 | struct commit *c = lookup_commit(revs->repo, oid); | |
370 | if (!repo_parse_commit(revs->repo, c)) | |
371 | object = (struct object *) c; | |
372 | else | |
373 | object = NULL; | |
374 | } else { | |
375 | object = parse_object(revs->repo, oid); | |
376 | } | |
377 | ||
cc243c3c JH |
378 | if (!object) { |
379 | if (revs->ignore_missing) | |
380 | return object; | |
df11e196 JT |
381 | if (revs->exclude_promisor_objects && is_promisor_object(oid)) |
382 | return NULL; | |
ae563542 | 383 | die("bad object %s", name); |
cc243c3c | 384 | } |
cd2bdc53 LT |
385 | object->flags |= flags; |
386 | return object; | |
387 | } | |
388 | ||
a58a1b01 | 389 | void add_pending_oid(struct rev_info *revs, const char *name, |
390 | const struct object_id *oid, unsigned int flags) | |
26c3177e | 391 | { |
654b9a90 | 392 | struct object *object = get_reference(revs, name, oid, flags); |
26c3177e RS |
393 | add_pending_object(revs, object, name); |
394 | } | |
395 | ||
ff5f5f26 | 396 | static struct commit *handle_commit(struct rev_info *revs, |
20739490 | 397 | struct object_array_entry *entry) |
cd2bdc53 | 398 | { |
20739490 JK |
399 | struct object *object = entry->item; |
400 | const char *name = entry->name; | |
401 | const char *path = entry->path; | |
402 | unsigned int mode = entry->mode; | |
cd2bdc53 | 403 | unsigned long flags = object->flags; |
ae563542 LT |
404 | |
405 | /* | |
406 | * Tag object? Look what it points to.. | |
407 | */ | |
1974632c | 408 | while (object->type == OBJ_TAG) { |
ae563542 | 409 | struct tag *tag = (struct tag *) object; |
cd2bdc53 | 410 | if (revs->tag_objects && !(flags & UNINTERESTING)) |
ae563542 | 411 | add_pending_object(revs, object, tag->tag); |
dad3f060 | 412 | object = parse_object(revs->repo, get_tagged_oid(tag)); |
aeeae1b7 | 413 | if (!object) { |
a3ba6bf1 | 414 | if (revs->ignore_missing_links || (flags & UNINTERESTING)) |
aeeae1b7 | 415 | return NULL; |
dc0a13f6 JT |
416 | if (revs->exclude_promisor_objects && |
417 | is_promisor_object(&tag->tagged->oid)) | |
418 | return NULL; | |
f2fd0760 | 419 | die("bad object %s", oid_to_hex(&tag->tagged->oid)); |
aeeae1b7 | 420 | } |
a7435286 | 421 | object->flags |= flags; |
20739490 JK |
422 | /* |
423 | * We'll handle the tagged object by looping or dropping | |
424 | * through to the non-tag handlers below. Do not | |
728350b7 | 425 | * propagate path data from the tag's pending entry. |
20739490 | 426 | */ |
20739490 JK |
427 | path = NULL; |
428 | mode = 0; | |
ae563542 LT |
429 | } |
430 | ||
431 | /* | |
432 | * Commit object? Just return it, we'll do all the complex | |
433 | * reachability crud. | |
434 | */ | |
1974632c | 435 | if (object->type == OBJ_COMMIT) { |
ae563542 | 436 | struct commit *commit = (struct commit *)object; |
87be2523 | 437 | |
ea3f7e59 | 438 | if (repo_parse_commit(revs->repo, commit) < 0) |
ae563542 | 439 | die("unable to parse commit %s", name); |
d9a83684 | 440 | if (flags & UNINTERESTING) { |
ae563542 | 441 | mark_parents_uninteresting(commit); |
1b4d8827 DS |
442 | |
443 | if (!revs->topo_order || !generation_numbers_enabled(the_repository)) | |
444 | revs->limited = 1; | |
d9a83684 | 445 | } |
87be2523 NTND |
446 | if (revs->sources) { |
447 | char **slot = revision_sources_at(revs->sources, commit); | |
448 | ||
449 | if (!*slot) | |
450 | *slot = xstrdup(name); | |
451 | } | |
ae563542 LT |
452 | return commit; |
453 | } | |
454 | ||
455 | /* | |
3ea3c215 | 456 | * Tree object? Either mark it uninteresting, or add it |
ae563542 LT |
457 | * to the list of objects to look at later.. |
458 | */ | |
1974632c | 459 | if (object->type == OBJ_TREE) { |
ae563542 LT |
460 | struct tree *tree = (struct tree *)object; |
461 | if (!revs->tree_objects) | |
462 | return NULL; | |
463 | if (flags & UNINTERESTING) { | |
b3c7eef9 | 464 | mark_tree_contents_uninteresting(revs->repo, tree); |
ae563542 LT |
465 | return NULL; |
466 | } | |
20739490 | 467 | add_pending_object_with_path(revs, object, name, mode, path); |
ae563542 LT |
468 | return NULL; |
469 | } | |
470 | ||
471 | /* | |
472 | * Blob object? You know the drill by now.. | |
473 | */ | |
1974632c | 474 | if (object->type == OBJ_BLOB) { |
ae563542 LT |
475 | if (!revs->blob_objects) |
476 | return NULL; | |
a7435286 | 477 | if (flags & UNINTERESTING) |
ae563542 | 478 | return NULL; |
20739490 | 479 | add_pending_object_with_path(revs, object, name, mode, path); |
ae563542 LT |
480 | return NULL; |
481 | } | |
482 | die("%s is unknown object", name); | |
483 | } | |
484 | ||
b6e8a3b5 JK |
485 | static int everybody_uninteresting(struct commit_list *orig, |
486 | struct commit **interesting_cache) | |
a4a88b2b LT |
487 | { |
488 | struct commit_list *list = orig; | |
b6e8a3b5 JK |
489 | |
490 | if (*interesting_cache) { | |
491 | struct commit *commit = *interesting_cache; | |
492 | if (!(commit->object.flags & UNINTERESTING)) | |
493 | return 0; | |
494 | } | |
495 | ||
a4a88b2b LT |
496 | while (list) { |
497 | struct commit *commit = list->item; | |
498 | list = list->next; | |
499 | if (commit->object.flags & UNINTERESTING) | |
500 | continue; | |
ae40ebda SB |
501 | |
502 | *interesting_cache = commit; | |
a4a88b2b LT |
503 | return 0; |
504 | } | |
505 | return 1; | |
506 | } | |
507 | ||
4d826608 KB |
508 | /* |
509 | * A definition of "relevant" commit that we can use to simplify limited graphs | |
510 | * by eliminating side branches. | |
511 | * | |
512 | * A "relevant" commit is one that is !UNINTERESTING (ie we are including it | |
513 | * in our list), or that is a specified BOTTOM commit. Then after computing | |
514 | * a limited list, during processing we can generally ignore boundary merges | |
515 | * coming from outside the graph, (ie from irrelevant parents), and treat | |
516 | * those merges as if they were single-parent. TREESAME is defined to consider | |
517 | * only relevant parents, if any. If we are TREESAME to our on-graph parents, | |
518 | * we don't care if we were !TREESAME to non-graph parents. | |
519 | * | |
520 | * Treating bottom commits as relevant ensures that a limited graph's | |
521 | * connection to the actual bottom commit is not viewed as a side branch, but | |
522 | * treated as part of the graph. For example: | |
523 | * | |
524 | * ....Z...A---X---o---o---B | |
525 | * . / | |
526 | * W---Y | |
527 | * | |
528 | * When computing "A..B", the A-X connection is at least as important as | |
529 | * Y-X, despite A being flagged UNINTERESTING. | |
530 | * | |
531 | * And when computing --ancestry-path "A..B", the A-X connection is more | |
532 | * important than Y-X, despite both A and Y being flagged UNINTERESTING. | |
533 | */ | |
534 | static inline int relevant_commit(struct commit *commit) | |
535 | { | |
536 | return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING; | |
537 | } | |
538 | ||
539 | /* | |
540 | * Return a single relevant commit from a parent list. If we are a TREESAME | |
541 | * commit, and this selects one of our parents, then we can safely simplify to | |
542 | * that parent. | |
543 | */ | |
544 | static struct commit *one_relevant_parent(const struct rev_info *revs, | |
545 | struct commit_list *orig) | |
546 | { | |
547 | struct commit_list *list = orig; | |
548 | struct commit *relevant = NULL; | |
549 | ||
550 | if (!orig) | |
551 | return NULL; | |
552 | ||
553 | /* | |
554 | * For 1-parent commits, or if first-parent-only, then return that | |
555 | * first parent (even if not "relevant" by the above definition). | |
556 | * TREESAME will have been set purely on that parent. | |
557 | */ | |
558 | if (revs->first_parent_only || !orig->next) | |
559 | return orig->item; | |
560 | ||
561 | /* | |
562 | * For multi-parent commits, identify a sole relevant parent, if any. | |
563 | * If we have only one relevant parent, then TREESAME will be set purely | |
564 | * with regard to that parent, and we can simplify accordingly. | |
565 | * | |
566 | * If we have more than one relevant parent, or no relevant parents | |
567 | * (and multiple irrelevant ones), then we can't select a parent here | |
568 | * and return NULL. | |
569 | */ | |
570 | while (list) { | |
571 | struct commit *commit = list->item; | |
572 | list = list->next; | |
573 | if (relevant_commit(commit)) { | |
574 | if (relevant) | |
575 | return NULL; | |
576 | relevant = commit; | |
577 | } | |
578 | } | |
579 | return relevant; | |
580 | } | |
581 | ||
0a4ba7f8 JH |
582 | /* |
583 | * The goal is to get REV_TREE_NEW as the result only if the | |
ceff8e7a LT |
584 | * diff consists of all '+' (and no other changes), REV_TREE_OLD |
585 | * if the whole diff is removal of old data, and otherwise | |
586 | * REV_TREE_DIFFERENT (of course if the trees are the same we | |
587 | * want REV_TREE_SAME). | |
a937b37e JK |
588 | * |
589 | * The only time we care about the distinction is when | |
590 | * remove_empty_trees is in effect, in which case we care only about | |
591 | * whether the whole change is REV_TREE_NEW, or if there's another type | |
592 | * of change. Which means we can stop the diff early in either of these | |
593 | * cases: | |
594 | * | |
595 | * 1. We're not using remove_empty_trees at all. | |
596 | * | |
597 | * 2. We saw anything except REV_TREE_NEW. | |
0a4ba7f8 | 598 | */ |
8efdc326 | 599 | static int tree_difference = REV_TREE_SAME; |
a4a88b2b LT |
600 | |
601 | static void file_add_remove(struct diff_options *options, | |
602 | int addremove, unsigned mode, | |
c26022ea BW |
603 | const struct object_id *oid, |
604 | int oid_valid, | |
e3d42c47 | 605 | const char *fullpath, unsigned dirty_submodule) |
a4a88b2b | 606 | { |
ceff8e7a | 607 | int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD; |
a937b37e | 608 | struct rev_info *revs = options->change_fn_data; |
a4a88b2b | 609 | |
ceff8e7a | 610 | tree_difference |= diff; |
a937b37e | 611 | if (!revs->remove_empty_trees || tree_difference != REV_TREE_NEW) |
0d1e0e78 | 612 | options->flags.has_changes = 1; |
a4a88b2b LT |
613 | } |
614 | ||
615 | static void file_change(struct diff_options *options, | |
616 | unsigned old_mode, unsigned new_mode, | |
94a0097a BW |
617 | const struct object_id *old_oid, |
618 | const struct object_id *new_oid, | |
619 | int old_oid_valid, int new_oid_valid, | |
e3d42c47 JL |
620 | const char *fullpath, |
621 | unsigned old_dirty_submodule, unsigned new_dirty_submodule) | |
a4a88b2b | 622 | { |
8efdc326 | 623 | tree_difference = REV_TREE_DIFFERENT; |
0d1e0e78 | 624 | options->flags.has_changes = 1; |
a4a88b2b LT |
625 | } |
626 | ||
42e50e78 GS |
627 | static int bloom_filter_atexit_registered; |
628 | static unsigned int count_bloom_filter_maybe; | |
629 | static unsigned int count_bloom_filter_definitely_not; | |
630 | static unsigned int count_bloom_filter_false_positive; | |
631 | static unsigned int count_bloom_filter_not_present; | |
42e50e78 GS |
632 | |
633 | static void trace2_bloom_filter_statistics_atexit(void) | |
634 | { | |
635 | struct json_writer jw = JSON_WRITER_INIT; | |
636 | ||
637 | jw_object_begin(&jw, 0); | |
638 | jw_object_intmax(&jw, "filter_not_present", count_bloom_filter_not_present); | |
42e50e78 GS |
639 | jw_object_intmax(&jw, "maybe", count_bloom_filter_maybe); |
640 | jw_object_intmax(&jw, "definitely_not", count_bloom_filter_definitely_not); | |
641 | jw_object_intmax(&jw, "false_positive", count_bloom_filter_false_positive); | |
642 | jw_end(&jw); | |
643 | ||
644 | trace2_data_json("bloom", the_repository, "statistics", &jw); | |
645 | ||
646 | jw_release(&jw); | |
647 | } | |
648 | ||
8918e379 DS |
649 | static int forbid_bloom_filters(struct pathspec *spec) |
650 | { | |
651 | if (spec->has_wildcard) | |
652 | return 1; | |
653 | if (spec->nr > 1) | |
654 | return 1; | |
655 | if (spec->magic & ~PATHSPEC_LITERAL) | |
656 | return 1; | |
657 | if (spec->nr && (spec->items[0].magic & ~PATHSPEC_LITERAL)) | |
658 | return 1; | |
659 | ||
660 | return 0; | |
661 | } | |
662 | ||
a56b9464 GS |
663 | static void prepare_to_use_bloom_filter(struct rev_info *revs) |
664 | { | |
665 | struct pathspec_item *pi; | |
666 | char *path_alloc = NULL; | |
c525ce95 | 667 | const char *path, *p; |
c525ce95 SG |
668 | size_t len; |
669 | int path_component_nr = 1; | |
a56b9464 GS |
670 | |
671 | if (!revs->commits) | |
8918e379 DS |
672 | return; |
673 | ||
674 | if (forbid_bloom_filters(&revs->prune_data)) | |
675 | return; | |
a56b9464 GS |
676 | |
677 | repo_parse_commit(revs->repo, revs->commits->item); | |
678 | ||
4f364405 | 679 | revs->bloom_filter_settings = get_bloom_filter_settings(revs->repo); |
a56b9464 GS |
680 | if (!revs->bloom_filter_settings) |
681 | return; | |
682 | ||
f32dde8c DS |
683 | if (!revs->pruning.pathspec.nr) |
684 | return; | |
685 | ||
a56b9464 | 686 | pi = &revs->pruning.pathspec.items[0]; |
a56b9464 GS |
687 | |
688 | /* remove single trailing slash from path, if needed */ | |
fd9a631c JK |
689 | if (pi->len > 0 && pi->match[pi->len - 1] == '/') { |
690 | path_alloc = xmemdupz(pi->match, pi->len - 1); | |
dc8e95ba | 691 | path = path_alloc; |
a56b9464 | 692 | } else |
dc8e95ba | 693 | path = pi->match; |
a56b9464 GS |
694 | |
695 | len = strlen(path); | |
f3c2a368 TB |
696 | if (!len) { |
697 | revs->bloom_filter_settings = NULL; | |
398e659e | 698 | free(path_alloc); |
f3c2a368 TB |
699 | return; |
700 | } | |
a56b9464 | 701 | |
c525ce95 SG |
702 | p = path; |
703 | while (*p) { | |
704 | /* | |
705 | * At this point, the path is normalized to use Unix-style | |
706 | * path separators. This is required due to how the | |
707 | * changed-path Bloom filters store the paths. | |
708 | */ | |
709 | if (*p == '/') | |
710 | path_component_nr++; | |
711 | p++; | |
712 | } | |
713 | ||
714 | revs->bloom_keys_nr = path_component_nr; | |
715 | ALLOC_ARRAY(revs->bloom_keys, revs->bloom_keys_nr); | |
a56b9464 | 716 | |
c525ce95 SG |
717 | fill_bloom_key(path, len, &revs->bloom_keys[0], |
718 | revs->bloom_filter_settings); | |
719 | path_component_nr = 1; | |
720 | ||
721 | p = path + len - 1; | |
722 | while (p > path) { | |
723 | if (*p == '/') | |
724 | fill_bloom_key(path, p - path, | |
725 | &revs->bloom_keys[path_component_nr++], | |
726 | revs->bloom_filter_settings); | |
727 | p--; | |
728 | } | |
a56b9464 | 729 | |
42e50e78 GS |
730 | if (trace2_is_enabled() && !bloom_filter_atexit_registered) { |
731 | atexit(trace2_bloom_filter_statistics_atexit); | |
732 | bloom_filter_atexit_registered = 1; | |
733 | } | |
734 | ||
a56b9464 GS |
735 | free(path_alloc); |
736 | } | |
737 | ||
738 | static int check_maybe_different_in_bloom_filter(struct rev_info *revs, | |
739 | struct commit *commit) | |
740 | { | |
741 | struct bloom_filter *filter; | |
c525ce95 | 742 | int result = 1, j; |
a56b9464 GS |
743 | |
744 | if (!revs->repo->objects->commit_graph) | |
745 | return -1; | |
746 | ||
c49c82aa | 747 | if (commit_graph_generation(commit) == GENERATION_NUMBER_INFINITY) |
a56b9464 GS |
748 | return -1; |
749 | ||
312cff52 | 750 | filter = get_bloom_filter(revs->repo, commit); |
a56b9464 GS |
751 | |
752 | if (!filter) { | |
42e50e78 | 753 | count_bloom_filter_not_present++; |
a56b9464 GS |
754 | return -1; |
755 | } | |
756 | ||
c525ce95 SG |
757 | for (j = 0; result && j < revs->bloom_keys_nr; j++) { |
758 | result = bloom_filter_contains(filter, | |
759 | &revs->bloom_keys[j], | |
760 | revs->bloom_filter_settings); | |
a56b9464 GS |
761 | } |
762 | ||
42e50e78 GS |
763 | if (result) |
764 | count_bloom_filter_maybe++; | |
765 | else | |
766 | count_bloom_filter_definitely_not++; | |
767 | ||
a56b9464 GS |
768 | return result; |
769 | } | |
770 | ||
ff5f5f26 | 771 | static int rev_compare_tree(struct rev_info *revs, |
a56b9464 | 772 | struct commit *parent, struct commit *commit, int nth_parent) |
a4a88b2b | 773 | { |
2e27bd77 DS |
774 | struct tree *t1 = get_commit_tree(parent); |
775 | struct tree *t2 = get_commit_tree(commit); | |
a56b9464 | 776 | int bloom_ret = 1; |
3a5e8608 | 777 | |
a4a88b2b | 778 | if (!t1) |
8efdc326 | 779 | return REV_TREE_NEW; |
ceff8e7a LT |
780 | if (!t2) |
781 | return REV_TREE_OLD; | |
78892e32 LT |
782 | |
783 | if (revs->simplify_by_decoration) { | |
784 | /* | |
785 | * If we are simplifying by decoration, then the commit | |
786 | * is worth showing if it has a tag pointing at it. | |
787 | */ | |
2608c249 | 788 | if (get_name_decoration(&commit->object)) |
78892e32 LT |
789 | return REV_TREE_DIFFERENT; |
790 | /* | |
791 | * A commit that is not pointed by a tag is uninteresting | |
792 | * if we are not limited by path. This means that you will | |
793 | * see the usual "commits that touch the paths" plus any | |
794 | * tagged commit by specifying both --simplify-by-decoration | |
795 | * and pathspec. | |
796 | */ | |
afe069d1 | 797 | if (!revs->prune_data.nr) |
78892e32 LT |
798 | return REV_TREE_SAME; |
799 | } | |
ceff8e7a | 800 | |
c525ce95 | 801 | if (revs->bloom_keys_nr && !nth_parent) { |
a56b9464 GS |
802 | bloom_ret = check_maybe_different_in_bloom_filter(revs, commit); |
803 | ||
804 | if (bloom_ret == 0) | |
805 | return REV_TREE_SAME; | |
806 | } | |
807 | ||
8efdc326 | 808 | tree_difference = REV_TREE_SAME; |
0d1e0e78 | 809 | revs->pruning.flags.has_changes = 0; |
0ee3cb88 | 810 | diff_tree_oid(&t1->object.oid, &t2->object.oid, "", &revs->pruning); |
a56b9464 | 811 | |
42e50e78 GS |
812 | if (!nth_parent) |
813 | if (bloom_ret == 1 && tree_difference == REV_TREE_SAME) | |
814 | count_bloom_filter_false_positive++; | |
815 | ||
a4a88b2b LT |
816 | return tree_difference; |
817 | } | |
818 | ||
3a5e8608 | 819 | static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit) |
a4a88b2b | 820 | { |
2e27bd77 | 821 | struct tree *t1 = get_commit_tree(commit); |
a4a88b2b LT |
822 | |
823 | if (!t1) | |
824 | return 0; | |
825 | ||
0a4ba7f8 | 826 | tree_difference = REV_TREE_SAME; |
0d1e0e78 | 827 | revs->pruning.flags.has_changes = 0; |
0ee3cb88 | 828 | diff_tree_oid(NULL, &t1->object.oid, "", &revs->pruning); |
a4a88b2b | 829 | |
0ee3cb88 | 830 | return tree_difference == REV_TREE_SAME; |
a4a88b2b LT |
831 | } |
832 | ||
d0af663e KB |
833 | struct treesame_state { |
834 | unsigned int nparents; | |
835 | unsigned char treesame[FLEX_ARRAY]; | |
836 | }; | |
837 | ||
838 | static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit) | |
839 | { | |
840 | unsigned n = commit_list_count(commit->parents); | |
50a6c8ef | 841 | struct treesame_state *st = xcalloc(1, st_add(sizeof(*st), n)); |
d0af663e KB |
842 | st->nparents = n; |
843 | add_decoration(&revs->treesame, &commit->object, st); | |
844 | return st; | |
845 | } | |
846 | ||
847 | /* | |
848 | * Must be called immediately after removing the nth_parent from a commit's | |
849 | * parent list, if we are maintaining the per-parent treesame[] decoration. | |
850 | * This does not recalculate the master TREESAME flag - update_treesame() | |
851 | * should be called to update it after a sequence of treesame[] modifications | |
852 | * that may have affected it. | |
853 | */ | |
854 | static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent) | |
855 | { | |
856 | struct treesame_state *st; | |
857 | int old_same; | |
858 | ||
859 | if (!commit->parents) { | |
860 | /* | |
861 | * Have just removed the only parent from a non-merge. | |
862 | * Different handling, as we lack decoration. | |
863 | */ | |
864 | if (nth_parent != 0) | |
865 | die("compact_treesame %u", nth_parent); | |
866 | old_same = !!(commit->object.flags & TREESAME); | |
867 | if (rev_same_tree_as_empty(revs, commit)) | |
868 | commit->object.flags |= TREESAME; | |
869 | else | |
870 | commit->object.flags &= ~TREESAME; | |
871 | return old_same; | |
872 | } | |
873 | ||
874 | st = lookup_decoration(&revs->treesame, &commit->object); | |
875 | if (!st || nth_parent >= st->nparents) | |
876 | die("compact_treesame %u", nth_parent); | |
877 | ||
878 | old_same = st->treesame[nth_parent]; | |
879 | memmove(st->treesame + nth_parent, | |
880 | st->treesame + nth_parent + 1, | |
881 | st->nparents - nth_parent - 1); | |
882 | ||
883 | /* | |
884 | * If we've just become a non-merge commit, update TREESAME | |
885 | * immediately, and remove the no-longer-needed decoration. | |
886 | * If still a merge, defer update until update_treesame(). | |
887 | */ | |
888 | if (--st->nparents == 1) { | |
889 | if (commit->parents->next) | |
890 | die("compact_treesame parents mismatch"); | |
891 | if (st->treesame[0] && revs->dense) | |
892 | commit->object.flags |= TREESAME; | |
893 | else | |
894 | commit->object.flags &= ~TREESAME; | |
895 | free(add_decoration(&revs->treesame, &commit->object, NULL)); | |
896 | } | |
897 | ||
898 | return old_same; | |
899 | } | |
900 | ||
901 | static unsigned update_treesame(struct rev_info *revs, struct commit *commit) | |
902 | { | |
903 | if (commit->parents && commit->parents->next) { | |
904 | unsigned n; | |
905 | struct treesame_state *st; | |
4d826608 KB |
906 | struct commit_list *p; |
907 | unsigned relevant_parents; | |
908 | unsigned relevant_change, irrelevant_change; | |
d0af663e KB |
909 | |
910 | st = lookup_decoration(&revs->treesame, &commit->object); | |
911 | if (!st) | |
f2fd0760 | 912 | die("update_treesame %s", oid_to_hex(&commit->object.oid)); |
4d826608 KB |
913 | relevant_parents = 0; |
914 | relevant_change = irrelevant_change = 0; | |
915 | for (p = commit->parents, n = 0; p; n++, p = p->next) { | |
916 | if (relevant_commit(p->item)) { | |
917 | relevant_change |= !st->treesame[n]; | |
918 | relevant_parents++; | |
919 | } else | |
920 | irrelevant_change |= !st->treesame[n]; | |
d0af663e | 921 | } |
4d826608 KB |
922 | if (relevant_parents ? relevant_change : irrelevant_change) |
923 | commit->object.flags &= ~TREESAME; | |
924 | else | |
925 | commit->object.flags |= TREESAME; | |
d0af663e KB |
926 | } |
927 | ||
928 | return commit->object.flags & TREESAME; | |
929 | } | |
930 | ||
4d826608 KB |
931 | static inline int limiting_can_increase_treesame(const struct rev_info *revs) |
932 | { | |
933 | /* | |
934 | * TREESAME is irrelevant unless prune && dense; | |
935 | * if simplify_history is set, we can't have a mixture of TREESAME and | |
936 | * !TREESAME INTERESTING parents (and we don't have treesame[] | |
937 | * decoration anyway); | |
938 | * if first_parent_only is set, then the TREESAME flag is locked | |
939 | * against the first parent (and again we lack treesame[] decoration). | |
940 | */ | |
941 | return revs->prune && revs->dense && | |
942 | !revs->simplify_history && | |
943 | !revs->first_parent_only; | |
944 | } | |
945 | ||
a4a88b2b LT |
946 | static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit) |
947 | { | |
948 | struct commit_list **pp, *parent; | |
d0af663e | 949 | struct treesame_state *ts = NULL; |
4d826608 KB |
950 | int relevant_change = 0, irrelevant_change = 0; |
951 | int relevant_parents, nth_parent; | |
a4a88b2b | 952 | |
53b2c823 LT |
953 | /* |
954 | * If we don't do pruning, everything is interesting | |
955 | */ | |
7dc0fe3b | 956 | if (!revs->prune) |
53b2c823 | 957 | return; |
53b2c823 | 958 | |
2e27bd77 | 959 | if (!get_commit_tree(commit)) |
a4a88b2b LT |
960 | return; |
961 | ||
962 | if (!commit->parents) { | |
3a5e8608 | 963 | if (rev_same_tree_as_empty(revs, commit)) |
7dc0fe3b | 964 | commit->object.flags |= TREESAME; |
a4a88b2b LT |
965 | return; |
966 | } | |
967 | ||
53b2c823 LT |
968 | /* |
969 | * Normal non-merge commit? If we don't want to make the | |
970 | * history dense, we consider it always to be a change.. | |
971 | */ | |
7dc0fe3b | 972 | if (!revs->dense && !commit->parents->next) |
53b2c823 | 973 | return; |
53b2c823 | 974 | |
4d826608 | 975 | for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0; |
d0af663e KB |
976 | (parent = *pp) != NULL; |
977 | pp = &parent->next, nth_parent++) { | |
a4a88b2b | 978 | struct commit *p = parent->item; |
4d826608 KB |
979 | if (relevant_commit(p)) |
980 | relevant_parents++; | |
a4a88b2b | 981 | |
d0af663e KB |
982 | if (nth_parent == 1) { |
983 | /* | |
984 | * This our second loop iteration - so we now know | |
985 | * we're dealing with a merge. | |
986 | * | |
987 | * Do not compare with later parents when we care only about | |
988 | * the first parent chain, in order to avoid derailing the | |
989 | * traversal to follow a side branch that brought everything | |
990 | * in the path we are limited to by the pathspec. | |
991 | */ | |
992 | if (revs->first_parent_only) | |
993 | break; | |
994 | /* | |
995 | * If this will remain a potentially-simplifiable | |
996 | * merge, remember per-parent treesame if needed. | |
997 | * Initialise the array with the comparison from our | |
998 | * first iteration. | |
999 | */ | |
1000 | if (revs->treesame.name && | |
1001 | !revs->simplify_history && | |
1002 | !(commit->object.flags & UNINTERESTING)) { | |
1003 | ts = initialise_treesame(revs, commit); | |
4d826608 | 1004 | if (!(irrelevant_change || relevant_change)) |
d0af663e KB |
1005 | ts->treesame[0] = 1; |
1006 | } | |
1007 | } | |
ea3f7e59 | 1008 | if (repo_parse_commit(revs->repo, p) < 0) |
cc0e6c5a | 1009 | die("cannot simplify commit %s (because of %s)", |
f2fd0760 | 1010 | oid_to_hex(&commit->object.oid), |
1011 | oid_to_hex(&p->object.oid)); | |
a56b9464 | 1012 | switch (rev_compare_tree(revs, p, commit, nth_parent)) { |
8efdc326 | 1013 | case REV_TREE_SAME: |
141efdba | 1014 | if (!revs->simplify_history || !relevant_commit(p)) { |
f3219fbb JH |
1015 | /* Even if a merge with an uninteresting |
1016 | * side branch brought the entire change | |
1017 | * we are interested in, we do not want | |
1018 | * to lose the other branches of this | |
1019 | * merge, so we just keep going. | |
1020 | */ | |
d0af663e KB |
1021 | if (ts) |
1022 | ts->treesame[nth_parent] = 1; | |
f3219fbb JH |
1023 | continue; |
1024 | } | |
a4a88b2b LT |
1025 | parent->next = NULL; |
1026 | commit->parents = parent; | |
8d049e18 DS |
1027 | |
1028 | /* | |
1029 | * A merge commit is a "diversion" if it is not | |
1030 | * TREESAME to its first parent but is TREESAME | |
1031 | * to a later parent. In the simplified history, | |
1032 | * we "divert" the history walk to the later | |
1033 | * parent. These commits are shown when "show_pulls" | |
1034 | * is enabled, so do not mark the object as | |
1035 | * TREESAME here. | |
1036 | */ | |
1037 | if (!revs->show_pulls || !nth_parent) | |
1038 | commit->object.flags |= TREESAME; | |
1039 | ||
a4a88b2b LT |
1040 | return; |
1041 | ||
8efdc326 FK |
1042 | case REV_TREE_NEW: |
1043 | if (revs->remove_empty_trees && | |
3a5e8608 | 1044 | rev_same_tree_as_empty(revs, p)) { |
c348f31a JH |
1045 | /* We are adding all the specified |
1046 | * paths from this parent, so the | |
1047 | * history beyond this parent is not | |
1048 | * interesting. Remove its parents | |
1049 | * (they are grandparents for us). | |
1050 | * IOW, we pretend this parent is a | |
1051 | * "root" commit. | |
a41e109c | 1052 | */ |
ea3f7e59 | 1053 | if (repo_parse_commit(revs->repo, p) < 0) |
cc0e6c5a | 1054 | die("cannot simplify commit %s (invalid %s)", |
f2fd0760 | 1055 | oid_to_hex(&commit->object.oid), |
1056 | oid_to_hex(&p->object.oid)); | |
c348f31a | 1057 | p->parents = NULL; |
a4a88b2b LT |
1058 | } |
1059 | /* fallthrough */ | |
ceff8e7a | 1060 | case REV_TREE_OLD: |
8efdc326 | 1061 | case REV_TREE_DIFFERENT: |
4d826608 KB |
1062 | if (relevant_commit(p)) |
1063 | relevant_change = 1; | |
1064 | else | |
1065 | irrelevant_change = 1; | |
8d049e18 DS |
1066 | |
1067 | if (!nth_parent) | |
1068 | commit->object.flags |= PULL_MERGE; | |
1069 | ||
a4a88b2b LT |
1070 | continue; |
1071 | } | |
f2fd0760 | 1072 | die("bad tree compare for commit %s", oid_to_hex(&commit->object.oid)); |
a4a88b2b | 1073 | } |
4d826608 KB |
1074 | |
1075 | /* | |
1076 | * TREESAME is straightforward for single-parent commits. For merge | |
1077 | * commits, it is most useful to define it so that "irrelevant" | |
1078 | * parents cannot make us !TREESAME - if we have any relevant | |
1079 | * parents, then we only consider TREESAMEness with respect to them, | |
1080 | * allowing irrelevant merges from uninteresting branches to be | |
1081 | * simplified away. Only if we have only irrelevant parents do we | |
1082 | * base TREESAME on them. Note that this logic is replicated in | |
1083 | * update_treesame, which should be kept in sync. | |
1084 | */ | |
1085 | if (relevant_parents ? !relevant_change : !irrelevant_change) | |
1086 | commit->object.flags |= TREESAME; | |
a4a88b2b LT |
1087 | } |
1088 | ||
5284fc5c | 1089 | static int process_parents(struct rev_info *revs, struct commit *commit, |
8320b1db | 1090 | struct commit_list **list, struct prio_queue *queue) |
a4a88b2b LT |
1091 | { |
1092 | struct commit_list *parent = commit->parents; | |
577ed5c2 | 1093 | unsigned left_flag; |
a4a88b2b | 1094 | |
3381c790 | 1095 | if (commit->object.flags & ADDED) |
cc0e6c5a | 1096 | return 0; |
3381c790 LT |
1097 | commit->object.flags |= ADDED; |
1098 | ||
a330de31 VM |
1099 | if (revs->include_check && |
1100 | !revs->include_check(commit, revs->include_check_data)) | |
1101 | return 0; | |
1102 | ||
a4a88b2b LT |
1103 | /* |
1104 | * If the commit is uninteresting, don't try to | |
1105 | * prune parents - we want the maximal uninteresting | |
1106 | * set. | |
1107 | * | |
1108 | * Normally we haven't parsed the parent | |
1109 | * yet, so we won't have a parent of a parent | |
1110 | * here. However, it may turn out that we've | |
1111 | * reached this commit some other way (where it | |
1112 | * wasn't uninteresting), in which case we need | |
1113 | * to mark its parents recursively too.. | |
1114 | */ | |
1115 | if (commit->object.flags & UNINTERESTING) { | |
1116 | while (parent) { | |
1117 | struct commit *p = parent->item; | |
1118 | parent = parent->next; | |
aeeae1b7 JH |
1119 | if (p) |
1120 | p->object.flags |= UNINTERESTING; | |
ea3f7e59 | 1121 | if (repo_parse_commit_gently(revs->repo, p, 1) < 0) |
aeeae1b7 | 1122 | continue; |
a4a88b2b LT |
1123 | if (p->parents) |
1124 | mark_parents_uninteresting(p); | |
1125 | if (p->object.flags & SEEN) | |
1126 | continue; | |
b2025da3 | 1127 | p->object.flags |= (SEEN | NOT_USER_GIVEN); |
5284fc5c | 1128 | if (list) |
8320b1db JK |
1129 | commit_list_insert_by_date(p, list); |
1130 | if (queue) | |
1131 | prio_queue_put(queue, p); | |
a4a88b2b | 1132 | } |
cc0e6c5a | 1133 | return 0; |
a4a88b2b LT |
1134 | } |
1135 | ||
1136 | /* | |
1137 | * Ok, the commit wasn't uninteresting. Try to | |
1138 | * simplify the commit history and find the parent | |
1139 | * that has no differences in the path set if one exists. | |
1140 | */ | |
53b2c823 | 1141 | try_to_simplify_commit(revs, commit); |
a4a88b2b | 1142 | |
ba1d4505 | 1143 | if (revs->no_walk) |
cc0e6c5a | 1144 | return 0; |
ba1d4505 | 1145 | |
577ed5c2 | 1146 | left_flag = (commit->object.flags & SYMMETRIC_LEFT); |
0053e902 | 1147 | |
d9c292e8 | 1148 | for (parent = commit->parents; parent; parent = parent->next) { |
a4a88b2b | 1149 | struct commit *p = parent->item; |
df11e196 JT |
1150 | int gently = revs->ignore_missing_links || |
1151 | revs->exclude_promisor_objects; | |
ea3f7e59 | 1152 | if (repo_parse_commit_gently(revs->repo, p, gently) < 0) { |
df11e196 JT |
1153 | if (revs->exclude_promisor_objects && |
1154 | is_promisor_object(&p->object.oid)) { | |
1155 | if (revs->first_parent_only) | |
1156 | break; | |
1157 | continue; | |
1158 | } | |
cc0e6c5a | 1159 | return -1; |
df11e196 | 1160 | } |
87be2523 NTND |
1161 | if (revs->sources) { |
1162 | char **slot = revision_sources_at(revs->sources, p); | |
1163 | ||
1164 | if (!*slot) | |
1165 | *slot = *revision_sources_at(revs->sources, commit); | |
1166 | } | |
577ed5c2 | 1167 | p->object.flags |= left_flag; |
ad1012eb | 1168 | if (!(p->object.flags & SEEN)) { |
b2025da3 | 1169 | p->object.flags |= (SEEN | NOT_USER_GIVEN); |
5284fc5c | 1170 | if (list) |
8320b1db JK |
1171 | commit_list_insert_by_date(p, list); |
1172 | if (queue) | |
1173 | prio_queue_put(queue, p); | |
ad1012eb | 1174 | } |
60d30b02 | 1175 | if (revs->first_parent_only) |
d9c292e8 | 1176 | break; |
a4a88b2b | 1177 | } |
cc0e6c5a | 1178 | return 0; |
a4a88b2b LT |
1179 | } |
1180 | ||
36d56de6 | 1181 | static void cherry_pick_list(struct commit_list *list, struct rev_info *revs) |
d7a17cad JH |
1182 | { |
1183 | struct commit_list *p; | |
1184 | int left_count = 0, right_count = 0; | |
1185 | int left_first; | |
1186 | struct patch_ids ids; | |
adbbb31e | 1187 | unsigned cherry_flag; |
d7a17cad JH |
1188 | |
1189 | /* First count the commits on the left and on the right */ | |
1190 | for (p = list; p; p = p->next) { | |
1191 | struct commit *commit = p->item; | |
1192 | unsigned flags = commit->object.flags; | |
1193 | if (flags & BOUNDARY) | |
1194 | ; | |
1195 | else if (flags & SYMMETRIC_LEFT) | |
1196 | left_count++; | |
1197 | else | |
1198 | right_count++; | |
1199 | } | |
1200 | ||
36c07975 TR |
1201 | if (!left_count || !right_count) |
1202 | return; | |
1203 | ||
d7a17cad | 1204 | left_first = left_count < right_count; |
2abf3503 | 1205 | init_patch_ids(revs->repo, &ids); |
66f13625 | 1206 | ids.diffopts.pathspec = revs->diffopt.pathspec; |
d7a17cad JH |
1207 | |
1208 | /* Compute patch-ids for one side */ | |
1209 | for (p = list; p; p = p->next) { | |
1210 | struct commit *commit = p->item; | |
1211 | unsigned flags = commit->object.flags; | |
1212 | ||
1213 | if (flags & BOUNDARY) | |
1214 | continue; | |
1215 | /* | |
1216 | * If we have fewer left, left_first is set and we omit | |
1217 | * commits on the right branch in this loop. If we have | |
1218 | * fewer right, we skip the left ones. | |
1219 | */ | |
1220 | if (left_first != !!(flags & SYMMETRIC_LEFT)) | |
1221 | continue; | |
683f17ec | 1222 | add_commit_patch_id(commit, &ids); |
d7a17cad JH |
1223 | } |
1224 | ||
adbbb31e MG |
1225 | /* either cherry_mark or cherry_pick are true */ |
1226 | cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN; | |
1227 | ||
d7a17cad JH |
1228 | /* Check the other side */ |
1229 | for (p = list; p; p = p->next) { | |
1230 | struct commit *commit = p->item; | |
1231 | struct patch_id *id; | |
1232 | unsigned flags = commit->object.flags; | |
1233 | ||
1234 | if (flags & BOUNDARY) | |
1235 | continue; | |
1236 | /* | |
1237 | * If we have fewer left, left_first is set and we omit | |
1238 | * commits on the left branch in this loop. | |
1239 | */ | |
1240 | if (left_first == !!(flags & SYMMETRIC_LEFT)) | |
1241 | continue; | |
1242 | ||
1243 | /* | |
1244 | * Have we seen the same patch id? | |
1245 | */ | |
c9e3a4e7 | 1246 | id = patch_id_iter_first(commit, &ids); |
d7a17cad JH |
1247 | if (!id) |
1248 | continue; | |
d7a17cad | 1249 | |
683f17ec | 1250 | commit->object.flags |= cherry_flag; |
c9e3a4e7 JK |
1251 | do { |
1252 | id->commit->object.flags |= cherry_flag; | |
1253 | } while ((id = patch_id_iter_next(id, &ids))); | |
d7a17cad JH |
1254 | } |
1255 | ||
1256 | free_patch_ids(&ids); | |
1257 | } | |
1258 | ||
7d004199 LT |
1259 | /* How many extra uninteresting commits we want to see.. */ |
1260 | #define SLOP 5 | |
1261 | ||
dddbad72 | 1262 | static int still_interesting(struct commit_list *src, timestamp_t date, int slop, |
b6e8a3b5 | 1263 | struct commit **interesting_cache) |
3131b713 | 1264 | { |
7d004199 LT |
1265 | /* |
1266 | * No source list at all? We're definitely done.. | |
1267 | */ | |
1268 | if (!src) | |
1269 | return 0; | |
1270 | ||
1271 | /* | |
1272 | * Does the destination list contain entries with a date | |
1273 | * before the source list? Definitely _not_ done. | |
1274 | */ | |
c19d1b4e | 1275 | if (date <= src->item->date) |
7d004199 LT |
1276 | return SLOP; |
1277 | ||
1278 | /* | |
1279 | * Does the source list still have interesting commits in | |
1280 | * it? Definitely not done.. | |
1281 | */ | |
b6e8a3b5 | 1282 | if (!everybody_uninteresting(src, interesting_cache)) |
7d004199 LT |
1283 | return SLOP; |
1284 | ||
1285 | /* Ok, we're closing in.. */ | |
1286 | return slop-1; | |
3131b713 LT |
1287 | } |
1288 | ||
ebdc94f3 JH |
1289 | /* |
1290 | * "rev-list --ancestry-path A..B" computes commits that are ancestors | |
1291 | * of B but not ancestors of A but further limits the result to those | |
1292 | * that are descendants of A. This takes the list of bottom commits and | |
1293 | * the result of "A..B" without --ancestry-path, and limits the latter | |
1294 | * further to the ones that can reach one of the commits in "bottom". | |
1295 | */ | |
1296 | static void limit_to_ancestry(struct commit_list *bottom, struct commit_list *list) | |
1297 | { | |
1298 | struct commit_list *p; | |
1299 | struct commit_list *rlist = NULL; | |
1300 | int made_progress; | |
1301 | ||
1302 | /* | |
1303 | * Reverse the list so that it will be likely that we would | |
1304 | * process parents before children. | |
1305 | */ | |
1306 | for (p = list; p; p = p->next) | |
1307 | commit_list_insert(p->item, &rlist); | |
1308 | ||
1309 | for (p = bottom; p; p = p->next) | |
1310 | p->item->object.flags |= TMP_MARK; | |
1311 | ||
1312 | /* | |
1313 | * Mark the ones that can reach bottom commits in "list", | |
1314 | * in a bottom-up fashion. | |
1315 | */ | |
1316 | do { | |
1317 | made_progress = 0; | |
1318 | for (p = rlist; p; p = p->next) { | |
1319 | struct commit *c = p->item; | |
1320 | struct commit_list *parents; | |
1321 | if (c->object.flags & (TMP_MARK | UNINTERESTING)) | |
1322 | continue; | |
1323 | for (parents = c->parents; | |
1324 | parents; | |
1325 | parents = parents->next) { | |
1326 | if (!(parents->item->object.flags & TMP_MARK)) | |
1327 | continue; | |
1328 | c->object.flags |= TMP_MARK; | |
1329 | made_progress = 1; | |
1330 | break; | |
1331 | } | |
1332 | } | |
1333 | } while (made_progress); | |
1334 | ||
1335 | /* | |
1336 | * NEEDSWORK: decide if we want to remove parents that are | |
1337 | * not marked with TMP_MARK from commit->parents for commits | |
1338 | * in the resulting list. We may not want to do that, though. | |
1339 | */ | |
1340 | ||
1341 | /* | |
1342 | * The ones that are not marked with TMP_MARK are uninteresting | |
1343 | */ | |
1344 | for (p = list; p; p = p->next) { | |
1345 | struct commit *c = p->item; | |
1346 | if (c->object.flags & TMP_MARK) | |
1347 | continue; | |
1348 | c->object.flags |= UNINTERESTING; | |
1349 | } | |
1350 | ||
1351 | /* We are done with the TMP_MARK */ | |
1352 | for (p = list; p; p = p->next) | |
1353 | p->item->object.flags &= ~TMP_MARK; | |
1354 | for (p = bottom; p; p = p->next) | |
1355 | p->item->object.flags &= ~TMP_MARK; | |
1356 | free_commit_list(rlist); | |
1357 | } | |
1358 | ||
1359 | /* | |
1360 | * Before walking the history, keep the set of "negative" refs the | |
1361 | * caller has asked to exclude. | |
1362 | * | |
1363 | * This is used to compute "rev-list --ancestry-path A..B", as we need | |
1364 | * to filter the result of "A..B" further to the ones that can actually | |
1365 | * reach A. | |
1366 | */ | |
7f34a46f | 1367 | static struct commit_list *collect_bottom_commits(struct commit_list *list) |
ebdc94f3 | 1368 | { |
7f34a46f KB |
1369 | struct commit_list *elem, *bottom = NULL; |
1370 | for (elem = list; elem; elem = elem->next) | |
1371 | if (elem->item->object.flags & BOTTOM) | |
1372 | commit_list_insert(elem->item, &bottom); | |
ebdc94f3 JH |
1373 | return bottom; |
1374 | } | |
1375 | ||
60adf7d7 MG |
1376 | /* Assumes either left_only or right_only is set */ |
1377 | static void limit_left_right(struct commit_list *list, struct rev_info *revs) | |
1378 | { | |
1379 | struct commit_list *p; | |
1380 | ||
1381 | for (p = list; p; p = p->next) { | |
1382 | struct commit *commit = p->item; | |
1383 | ||
1384 | if (revs->right_only) { | |
1385 | if (commit->object.flags & SYMMETRIC_LEFT) | |
1386 | commit->object.flags |= SHOWN; | |
1387 | } else /* revs->left_only is set */ | |
1388 | if (!(commit->object.flags & SYMMETRIC_LEFT)) | |
1389 | commit->object.flags |= SHOWN; | |
1390 | } | |
1391 | } | |
1392 | ||
cc0e6c5a | 1393 | static int limit_list(struct rev_info *revs) |
a4a88b2b | 1394 | { |
7d004199 | 1395 | int slop = SLOP; |
dddbad72 | 1396 | timestamp_t date = TIME_MAX; |
db69bf60 | 1397 | struct commit_list *original_list = revs->commits; |
a4a88b2b LT |
1398 | struct commit_list *newlist = NULL; |
1399 | struct commit_list **p = &newlist; | |
ebdc94f3 | 1400 | struct commit_list *bottom = NULL; |
b6e8a3b5 | 1401 | struct commit *interesting_cache = NULL; |
ebdc94f3 JH |
1402 | |
1403 | if (revs->ancestry_path) { | |
db69bf60 | 1404 | bottom = collect_bottom_commits(original_list); |
ebdc94f3 | 1405 | if (!bottom) |
97b03c35 | 1406 | die("--ancestry-path given but there are no bottom commits"); |
ebdc94f3 | 1407 | } |
a4a88b2b | 1408 | |
db69bf60 AH |
1409 | while (original_list) { |
1410 | struct commit *commit = pop_commit(&original_list); | |
a4a88b2b | 1411 | struct object *obj = &commit->object; |
cdcefbc9 | 1412 | show_early_output_fn_t show; |
a4a88b2b | 1413 | |
b6e8a3b5 JK |
1414 | if (commit == interesting_cache) |
1415 | interesting_cache = NULL; | |
1416 | ||
a4a88b2b LT |
1417 | if (revs->max_age != -1 && (commit->date < revs->max_age)) |
1418 | obj->flags |= UNINTERESTING; | |
db69bf60 | 1419 | if (process_parents(revs, commit, &original_list, NULL) < 0) |
cc0e6c5a | 1420 | return -1; |
a4a88b2b LT |
1421 | if (obj->flags & UNINTERESTING) { |
1422 | mark_parents_uninteresting(commit); | |
db69bf60 | 1423 | slop = still_interesting(original_list, date, slop, &interesting_cache); |
7d004199 | 1424 | if (slop) |
3131b713 | 1425 | continue; |
7d004199 | 1426 | break; |
a4a88b2b | 1427 | } |
01faa91c RS |
1428 | if (revs->min_age != -1 && (commit->date > revs->min_age) && |
1429 | !revs->line_level_traverse) | |
a4a88b2b | 1430 | continue; |
7d004199 | 1431 | date = commit->date; |
a4a88b2b | 1432 | p = &commit_list_insert(commit, p)->next; |
cdcefbc9 LT |
1433 | |
1434 | show = show_early_output; | |
1435 | if (!show) | |
1436 | continue; | |
1437 | ||
1438 | show(revs, newlist); | |
1439 | show_early_output = NULL; | |
a4a88b2b | 1440 | } |
adbbb31e | 1441 | if (revs->cherry_pick || revs->cherry_mark) |
36d56de6 | 1442 | cherry_pick_list(newlist, revs); |
d7a17cad | 1443 | |
60adf7d7 MG |
1444 | if (revs->left_only || revs->right_only) |
1445 | limit_left_right(newlist, revs); | |
1446 | ||
ebdc94f3 JH |
1447 | if (bottom) { |
1448 | limit_to_ancestry(bottom, newlist); | |
1449 | free_commit_list(bottom); | |
1450 | } | |
1451 | ||
4d826608 KB |
1452 | /* |
1453 | * Check if any commits have become TREESAME by some of their parents | |
1454 | * becoming UNINTERESTING. | |
1455 | */ | |
db69bf60 AH |
1456 | if (limiting_can_increase_treesame(revs)) { |
1457 | struct commit_list *list = NULL; | |
4d826608 KB |
1458 | for (list = newlist; list; list = list->next) { |
1459 | struct commit *c = list->item; | |
1460 | if (c->object.flags & (UNINTERESTING | TREESAME)) | |
1461 | continue; | |
1462 | update_treesame(revs, c); | |
1463 | } | |
db69bf60 | 1464 | } |
4d826608 | 1465 | |
db69bf60 | 1466 | free_commit_list(original_list); |
a4a88b2b | 1467 | revs->commits = newlist; |
cc0e6c5a | 1468 | return 0; |
a4a88b2b LT |
1469 | } |
1470 | ||
df835d3a MH |
1471 | /* |
1472 | * Add an entry to refs->cmdline with the specified information. | |
1473 | * *name is copied. | |
1474 | */ | |
281eee47 JH |
1475 | static void add_rev_cmdline(struct rev_info *revs, |
1476 | struct object *item, | |
1477 | const char *name, | |
1478 | int whence, | |
1479 | unsigned flags) | |
1480 | { | |
1481 | struct rev_cmdline_info *info = &revs->cmdline; | |
071bcaab | 1482 | unsigned int nr = info->nr; |
281eee47 JH |
1483 | |
1484 | ALLOC_GROW(info->rev, nr + 1, info->alloc); | |
1485 | info->rev[nr].item = item; | |
df835d3a | 1486 | info->rev[nr].name = xstrdup(name); |
281eee47 JH |
1487 | info->rev[nr].whence = whence; |
1488 | info->rev[nr].flags = flags; | |
1489 | info->nr++; | |
1490 | } | |
1491 | ||
a765499a KB |
1492 | static void add_rev_cmdline_list(struct rev_info *revs, |
1493 | struct commit_list *commit_list, | |
1494 | int whence, | |
1495 | unsigned flags) | |
1496 | { | |
1497 | while (commit_list) { | |
1498 | struct object *object = &commit_list->item->object; | |
f2fd0760 | 1499 | add_rev_cmdline(revs, object, oid_to_hex(&object->oid), |
a765499a KB |
1500 | whence, flags); |
1501 | commit_list = commit_list->next; | |
1502 | } | |
1503 | } | |
1504 | ||
63049292 JH |
1505 | struct all_refs_cb { |
1506 | int all_flags; | |
71b03b42 | 1507 | int warned_bad_reflog; |
63049292 JH |
1508 | struct rev_info *all_revs; |
1509 | const char *name_for_errormsg; | |
ab3e1f78 | 1510 | struct worktree *wt; |
63049292 | 1511 | }; |
ae563542 | 1512 | |
ff32d342 | 1513 | int ref_excluded(struct string_list *ref_excludes, const char *path) |
e7b432c5 JH |
1514 | { |
1515 | struct string_list_item *item; | |
1516 | ||
ff32d342 | 1517 | if (!ref_excludes) |
e7b432c5 | 1518 | return 0; |
ff32d342 | 1519 | for_each_string_list_item(item, ref_excludes) { |
55d34269 | 1520 | if (!wildmatch(item->string, path, 0)) |
e7b432c5 JH |
1521 | return 1; |
1522 | } | |
1523 | return 0; | |
1524 | } | |
1525 | ||
a217dcbd MH |
1526 | static int handle_one_ref(const char *path, const struct object_id *oid, |
1527 | int flag, void *cb_data) | |
ae563542 | 1528 | { |
63049292 | 1529 | struct all_refs_cb *cb = cb_data; |
e7b432c5 JH |
1530 | struct object *object; |
1531 | ||
ff32d342 | 1532 | if (ref_excluded(cb->all_revs->ref_excludes, path)) |
e7b432c5 JH |
1533 | return 0; |
1534 | ||
654b9a90 | 1535 | object = get_reference(cb->all_revs, path, oid, cb->all_flags); |
281eee47 | 1536 | add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags); |
a58a1b01 | 1537 | add_pending_oid(cb->all_revs, path, oid, cb->all_flags); |
ae563542 LT |
1538 | return 0; |
1539 | } | |
1540 | ||
d08bae7e IL |
1541 | static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs, |
1542 | unsigned flags) | |
1543 | { | |
1544 | cb->all_revs = revs; | |
1545 | cb->all_flags = flags; | |
7ba82629 | 1546 | revs->rev_input_given = 1; |
ab3e1f78 | 1547 | cb->wt = NULL; |
d08bae7e IL |
1548 | } |
1549 | ||
ff32d342 | 1550 | void clear_ref_exclusion(struct string_list **ref_excludes_p) |
e7b432c5 | 1551 | { |
ff32d342 JH |
1552 | if (*ref_excludes_p) { |
1553 | string_list_clear(*ref_excludes_p, 0); | |
1554 | free(*ref_excludes_p); | |
e7b432c5 | 1555 | } |
ff32d342 | 1556 | *ref_excludes_p = NULL; |
e7b432c5 JH |
1557 | } |
1558 | ||
ff32d342 | 1559 | void add_ref_exclusion(struct string_list **ref_excludes_p, const char *exclude) |
e7b432c5 | 1560 | { |
ff32d342 | 1561 | if (!*ref_excludes_p) { |
ca56dadb | 1562 | CALLOC_ARRAY(*ref_excludes_p, 1); |
ff32d342 | 1563 | (*ref_excludes_p)->strdup_strings = 1; |
e7b432c5 | 1564 | } |
ff32d342 | 1565 | string_list_append(*ref_excludes_p, exclude); |
e7b432c5 JH |
1566 | } |
1567 | ||
073cf63c NTND |
1568 | static void handle_refs(struct ref_store *refs, |
1569 | struct rev_info *revs, unsigned flags, | |
1570 | int (*for_each)(struct ref_store *, each_ref_fn, void *)) | |
ae563542 | 1571 | { |
63049292 | 1572 | struct all_refs_cb cb; |
073cf63c NTND |
1573 | |
1574 | if (!refs) { | |
1575 | /* this could happen with uninitialized submodules */ | |
1576 | return; | |
1577 | } | |
1578 | ||
d08bae7e | 1579 | init_all_refs_cb(&cb, revs, flags); |
073cf63c | 1580 | for_each(refs, handle_one_ref, &cb); |
63049292 JH |
1581 | } |
1582 | ||
9461d272 | 1583 | static void handle_one_reflog_commit(struct object_id *oid, void *cb_data) |
63049292 JH |
1584 | { |
1585 | struct all_refs_cb *cb = cb_data; | |
9461d272 | 1586 | if (!is_null_oid(oid)) { |
b3c7eef9 | 1587 | struct object *o = parse_object(cb->all_revs->repo, oid); |
71b03b42 SP |
1588 | if (o) { |
1589 | o->flags |= cb->all_flags; | |
281eee47 | 1590 | /* ??? CMDLINEFLAGS ??? */ |
71b03b42 SP |
1591 | add_pending_object(cb->all_revs, o, ""); |
1592 | } | |
1593 | else if (!cb->warned_bad_reflog) { | |
46efd2d9 | 1594 | warning("reflog of '%s' references pruned commits", |
71b03b42 SP |
1595 | cb->name_for_errormsg); |
1596 | cb->warned_bad_reflog = 1; | |
1597 | } | |
63049292 | 1598 | } |
71b03b42 SP |
1599 | } |
1600 | ||
9461d272 | 1601 | static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid, |
dddbad72 | 1602 | const char *email, timestamp_t timestamp, int tz, |
883d60fa | 1603 | const char *message, void *cb_data) |
71b03b42 | 1604 | { |
9461d272 | 1605 | handle_one_reflog_commit(ooid, cb_data); |
1606 | handle_one_reflog_commit(noid, cb_data); | |
63049292 JH |
1607 | return 0; |
1608 | } | |
1609 | ||
ab3e1f78 | 1610 | static int handle_one_reflog(const char *refname_in_wt, |
061e420a | 1611 | const struct object_id *oid, |
a89caf4b | 1612 | int flag, void *cb_data) |
63049292 JH |
1613 | { |
1614 | struct all_refs_cb *cb = cb_data; | |
ab3e1f78 NTND |
1615 | struct strbuf refname = STRBUF_INIT; |
1616 | ||
71b03b42 | 1617 | cb->warned_bad_reflog = 0; |
ab3e1f78 NTND |
1618 | strbuf_worktree_ref(cb->wt, &refname, refname_in_wt); |
1619 | cb->name_for_errormsg = refname.buf; | |
1620 | refs_for_each_reflog_ent(get_main_ref_store(the_repository), | |
1621 | refname.buf, | |
acd9544a | 1622 | handle_one_reflog_ent, cb_data); |
ab3e1f78 | 1623 | strbuf_release(&refname); |
63049292 JH |
1624 | return 0; |
1625 | } | |
1626 | ||
acd9544a NTND |
1627 | static void add_other_reflogs_to_pending(struct all_refs_cb *cb) |
1628 | { | |
1629 | struct worktree **worktrees, **p; | |
1630 | ||
03f2465b | 1631 | worktrees = get_worktrees(); |
acd9544a NTND |
1632 | for (p = worktrees; *p; p++) { |
1633 | struct worktree *wt = *p; | |
1634 | ||
1635 | if (wt->is_current) | |
1636 | continue; | |
1637 | ||
ab3e1f78 NTND |
1638 | cb->wt = wt; |
1639 | refs_for_each_reflog(get_worktree_ref_store(wt), | |
acd9544a NTND |
1640 | handle_one_reflog, |
1641 | cb); | |
1642 | } | |
1643 | free_worktrees(worktrees); | |
1644 | } | |
1645 | ||
718ccc97 | 1646 | void add_reflogs_to_pending(struct rev_info *revs, unsigned flags) |
63049292 JH |
1647 | { |
1648 | struct all_refs_cb cb; | |
2b2a5be3 | 1649 | |
63049292 JH |
1650 | cb.all_revs = revs; |
1651 | cb.all_flags = flags; | |
ab3e1f78 | 1652 | cb.wt = NULL; |
a89caf4b | 1653 | for_each_reflog(handle_one_reflog, &cb); |
acd9544a NTND |
1654 | |
1655 | if (!revs->single_worktree) | |
1656 | add_other_reflogs_to_pending(&cb); | |
ae563542 LT |
1657 | } |
1658 | ||
4fe10219 | 1659 | static void add_cache_tree(struct cache_tree *it, struct rev_info *revs, |
b4cfcde4 | 1660 | struct strbuf *path, unsigned int flags) |
4fe10219 JK |
1661 | { |
1662 | size_t baselen = path->len; | |
1663 | int i; | |
1664 | ||
1665 | if (it->entry_count >= 0) { | |
b3c7eef9 | 1666 | struct tree *tree = lookup_tree(revs->repo, &it->oid); |
b4cfcde4 | 1667 | tree->object.flags |= flags; |
4fe10219 JK |
1668 | add_pending_object_with_path(revs, &tree->object, "", |
1669 | 040000, path->buf); | |
1670 | } | |
1671 | ||
1672 | for (i = 0; i < it->subtree_nr; i++) { | |
1673 | struct cache_tree_sub *sub = it->down[i]; | |
1674 | strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name); | |
b4cfcde4 | 1675 | add_cache_tree(sub->cache_tree, revs, path, flags); |
4fe10219 JK |
1676 | strbuf_setlen(path, baselen); |
1677 | } | |
1678 | ||
1679 | } | |
1680 | ||
6c3d8181 | 1681 | static void do_add_index_objects_to_pending(struct rev_info *revs, |
b4cfcde4 JK |
1682 | struct index_state *istate, |
1683 | unsigned int flags) | |
4fe10219 JK |
1684 | { |
1685 | int i; | |
1686 | ||
f5fed74f DS |
1687 | /* TODO: audit for interaction with sparse-index. */ |
1688 | ensure_full_index(istate); | |
6c3d8181 NTND |
1689 | for (i = 0; i < istate->cache_nr; i++) { |
1690 | struct cache_entry *ce = istate->cache[i]; | |
4fe10219 JK |
1691 | struct blob *blob; |
1692 | ||
1693 | if (S_ISGITLINK(ce->ce_mode)) | |
1694 | continue; | |
1695 | ||
b3c7eef9 | 1696 | blob = lookup_blob(revs->repo, &ce->oid); |
4fe10219 JK |
1697 | if (!blob) |
1698 | die("unable to add index blob to traversal"); | |
b4cfcde4 | 1699 | blob->object.flags |= flags; |
4fe10219 JK |
1700 | add_pending_object_with_path(revs, &blob->object, "", |
1701 | ce->ce_mode, ce->name); | |
1702 | } | |
1703 | ||
6c3d8181 | 1704 | if (istate->cache_tree) { |
4fe10219 | 1705 | struct strbuf path = STRBUF_INIT; |
b4cfcde4 | 1706 | add_cache_tree(istate->cache_tree, revs, &path, flags); |
4fe10219 JK |
1707 | strbuf_release(&path); |
1708 | } | |
1709 | } | |
1710 | ||
6c3d8181 NTND |
1711 | void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags) |
1712 | { | |
be489d02 NTND |
1713 | struct worktree **worktrees, **p; |
1714 | ||
e1ff0a32 | 1715 | repo_read_index(revs->repo); |
b4cfcde4 | 1716 | do_add_index_objects_to_pending(revs, revs->repo->index, flags); |
be489d02 NTND |
1717 | |
1718 | if (revs->single_worktree) | |
1719 | return; | |
1720 | ||
03f2465b | 1721 | worktrees = get_worktrees(); |
be489d02 NTND |
1722 | for (p = worktrees; *p; p++) { |
1723 | struct worktree *wt = *p; | |
1724 | struct index_state istate = { NULL }; | |
1725 | ||
1726 | if (wt->is_current) | |
1727 | continue; /* current index already taken care of */ | |
1728 | ||
1729 | if (read_index_from(&istate, | |
a125a223 TG |
1730 | worktree_git_path(wt, "index"), |
1731 | get_worktree_git_dir(wt)) > 0) | |
b4cfcde4 | 1732 | do_add_index_objects_to_pending(revs, &istate, flags); |
be489d02 NTND |
1733 | discard_index(&istate); |
1734 | } | |
1735 | free_worktrees(worktrees); | |
6c3d8181 NTND |
1736 | } |
1737 | ||
39b44ba7 JK |
1738 | struct add_alternate_refs_data { |
1739 | struct rev_info *revs; | |
1740 | unsigned int flags; | |
1741 | }; | |
1742 | ||
1743 | static void add_one_alternate_ref(const struct object_id *oid, | |
1744 | void *vdata) | |
1745 | { | |
1746 | const char *name = ".alternate"; | |
1747 | struct add_alternate_refs_data *data = vdata; | |
1748 | struct object *obj; | |
1749 | ||
1750 | obj = get_reference(data->revs, name, oid, data->flags); | |
1751 | add_rev_cmdline(data->revs, obj, name, REV_CMD_REV, data->flags); | |
1752 | add_pending_object(data->revs, obj, name); | |
1753 | } | |
1754 | ||
1755 | static void add_alternate_refs_to_pending(struct rev_info *revs, | |
1756 | unsigned int flags) | |
1757 | { | |
1758 | struct add_alternate_refs_data data; | |
1759 | data.revs = revs; | |
1760 | data.flags = flags; | |
1761 | for_each_alternate_ref(add_one_alternate_ref, &data); | |
1762 | } | |
1763 | ||
8779351d VN |
1764 | static int add_parents_only(struct rev_info *revs, const char *arg_, int flags, |
1765 | int exclude_parent) | |
ea4a19e1 | 1766 | { |
654b9a90 | 1767 | struct object_id oid; |
ea4a19e1 JH |
1768 | struct object *it; |
1769 | struct commit *commit; | |
1770 | struct commit_list *parents; | |
8779351d | 1771 | int parent_number; |
281eee47 | 1772 | const char *arg = arg_; |
ea4a19e1 JH |
1773 | |
1774 | if (*arg == '^') { | |
7f34a46f | 1775 | flags ^= UNINTERESTING | BOTTOM; |
ea4a19e1 JH |
1776 | arg++; |
1777 | } | |
e82caf38 | 1778 | if (get_oid_committish(arg, &oid)) |
ea4a19e1 JH |
1779 | return 0; |
1780 | while (1) { | |
654b9a90 | 1781 | it = get_reference(revs, arg, &oid, 0); |
cc243c3c JH |
1782 | if (!it && revs->ignore_missing) |
1783 | return 0; | |
1974632c | 1784 | if (it->type != OBJ_TAG) |
ea4a19e1 | 1785 | break; |
9684afd9 MK |
1786 | if (!((struct tag*)it)->tagged) |
1787 | return 0; | |
654b9a90 | 1788 | oidcpy(&oid, &((struct tag*)it)->tagged->oid); |
ea4a19e1 | 1789 | } |
1974632c | 1790 | if (it->type != OBJ_COMMIT) |
ea4a19e1 JH |
1791 | return 0; |
1792 | commit = (struct commit *)it; | |
8779351d VN |
1793 | if (exclude_parent && |
1794 | exclude_parent > commit_list_count(commit->parents)) | |
1795 | return 0; | |
1796 | for (parents = commit->parents, parent_number = 1; | |
1797 | parents; | |
1798 | parents = parents->next, parent_number++) { | |
1799 | if (exclude_parent && parent_number != exclude_parent) | |
1800 | continue; | |
1801 | ||
ea4a19e1 JH |
1802 | it = &parents->item->object; |
1803 | it->flags |= flags; | |
281eee47 | 1804 | add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags); |
ea4a19e1 JH |
1805 | add_pending_object(revs, it, arg); |
1806 | } | |
1807 | return 1; | |
1808 | } | |
1809 | ||
2abf3503 NTND |
1810 | void repo_init_revisions(struct repository *r, |
1811 | struct rev_info *revs, | |
1812 | const char *prefix) | |
8efdc326 FK |
1813 | { |
1814 | memset(revs, 0, sizeof(*revs)); | |
8e8f9987 | 1815 | |
2abf3503 | 1816 | revs->repo = r; |
6b9c58f4 | 1817 | revs->abbrev = DEFAULT_ABBREV; |
9202434c | 1818 | revs->simplify_history = 1; |
67022e02 | 1819 | revs->pruning.repo = r; |
0d1e0e78 BW |
1820 | revs->pruning.flags.recursive = 1; |
1821 | revs->pruning.flags.quick = 1; | |
cd2bdc53 LT |
1822 | revs->pruning.add_remove = file_add_remove; |
1823 | revs->pruning.change = file_change; | |
a937b37e | 1824 | revs->pruning.change_fn_data = revs; |
08f704f2 | 1825 | revs->sort_order = REV_SORT_IN_GRAPH_ORDER; |
8efdc326 | 1826 | revs->dense = 1; |
db6296a5 | 1827 | revs->prefix = prefix; |
8efdc326 FK |
1828 | revs->max_age = -1; |
1829 | revs->min_age = -1; | |
d5db6c9e | 1830 | revs->skip_count = -1; |
8efdc326 | 1831 | revs->max_count = -1; |
ad5aeede | 1832 | revs->max_parents = -1; |
0893eec8 | 1833 | revs->expand_tabs_in_log = -1; |
8efdc326 | 1834 | |
cd2bdc53 | 1835 | revs->commit_format = CMIT_FMT_DEFAULT; |
fe37a9c5 | 1836 | revs->expand_tabs_in_log_default = 8; |
cd2bdc53 | 1837 | |
2abf3503 | 1838 | grep_init(&revs->grep_filter, revs->repo, prefix); |
0843acfd | 1839 | revs->grep_filter.status_only = 1; |
0843acfd | 1840 | |
2abf3503 | 1841 | repo_diff_setup(revs->repo, &revs->diffopt); |
c0cb4a06 | 1842 | if (prefix && !revs->diffopt.prefix) { |
cd676a51 JH |
1843 | revs->diffopt.prefix = prefix; |
1844 | revs->diffopt.prefix_length = strlen(prefix); | |
1845 | } | |
3a03cf6b | 1846 | |
e6e230ee | 1847 | init_display_notes(&revs->notes_opt); |
8efdc326 FK |
1848 | } |
1849 | ||
0d2c9d67 | 1850 | static void add_pending_commit_list(struct rev_info *revs, |
ec36c42a NTND |
1851 | struct commit_list *commit_list, |
1852 | unsigned int flags) | |
0d2c9d67 RS |
1853 | { |
1854 | while (commit_list) { | |
1855 | struct object *object = &commit_list->item->object; | |
1856 | object->flags |= flags; | |
f2fd0760 | 1857 | add_pending_object(revs, object, oid_to_hex(&object->oid)); |
0d2c9d67 RS |
1858 | commit_list = commit_list->next; |
1859 | } | |
1860 | } | |
1861 | ||
ae3e5e1e JH |
1862 | static void prepare_show_merge(struct rev_info *revs) |
1863 | { | |
1864 | struct commit_list *bases; | |
1865 | struct commit *head, *other; | |
68ab61dd | 1866 | struct object_id oid; |
ae3e5e1e JH |
1867 | const char **prune = NULL; |
1868 | int i, prune_num = 1; /* counting terminating NULL */ | |
2abf3503 | 1869 | struct index_state *istate = revs->repo->index; |
ae3e5e1e | 1870 | |
68ab61dd | 1871 | if (get_oid("HEAD", &oid)) |
ae3e5e1e | 1872 | die("--merge without HEAD?"); |
bc83266a | 1873 | head = lookup_commit_or_die(&oid, "HEAD"); |
68ab61dd | 1874 | if (get_oid("MERGE_HEAD", &oid)) |
ae3e5e1e | 1875 | die("--merge without MERGE_HEAD?"); |
bc83266a | 1876 | other = lookup_commit_or_die(&oid, "MERGE_HEAD"); |
ae3e5e1e JH |
1877 | add_pending_object(revs, &head->object, "HEAD"); |
1878 | add_pending_object(revs, &other->object, "MERGE_HEAD"); | |
2ce406cc | 1879 | bases = get_merge_bases(head, other); |
7f34a46f KB |
1880 | add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM); |
1881 | add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM); | |
e82447b1 JH |
1882 | free_commit_list(bases); |
1883 | head->object.flags |= SYMMETRIC_LEFT; | |
ae3e5e1e | 1884 | |
2abf3503 | 1885 | if (!istate->cache_nr) |
e1ff0a32 | 1886 | repo_read_index(revs->repo); |
2abf3503 NTND |
1887 | for (i = 0; i < istate->cache_nr; i++) { |
1888 | const struct cache_entry *ce = istate->cache[i]; | |
ae3e5e1e JH |
1889 | if (!ce_stage(ce)) |
1890 | continue; | |
2abf3503 | 1891 | if (ce_path_match(istate, ce, &revs->prune_data, NULL)) { |
ae3e5e1e | 1892 | prune_num++; |
2756ca43 | 1893 | REALLOC_ARRAY(prune, prune_num); |
ae3e5e1e JH |
1894 | prune[prune_num-2] = ce->name; |
1895 | prune[prune_num-1] = NULL; | |
1896 | } | |
2abf3503 NTND |
1897 | while ((i+1 < istate->cache_nr) && |
1898 | ce_same_name(ce, istate->cache[i+1])) | |
ae3e5e1e JH |
1899 | i++; |
1900 | } | |
ed6e8038 | 1901 | clear_pathspec(&revs->prune_data); |
4a2d5ae2 | 1902 | parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL, |
c6f1b920 | 1903 | PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune); |
e82447b1 | 1904 | revs->limited = 1; |
ae3e5e1e JH |
1905 | } |
1906 | ||
62faad5a JK |
1907 | static int dotdot_missing(const char *arg, char *dotdot, |
1908 | struct rev_info *revs, int symmetric) | |
1909 | { | |
1910 | if (revs->ignore_missing) | |
1911 | return 0; | |
1912 | /* de-munge so we report the full argument */ | |
1913 | *dotdot = '.'; | |
1914 | die(symmetric | |
1915 | ? "Invalid symmetric difference expression %s" | |
1916 | : "Invalid revision range %s", arg); | |
1917 | } | |
1918 | ||
1919 | static int handle_dotdot_1(const char *arg, char *dotdot, | |
1920 | struct rev_info *revs, int flags, | |
18f1ad76 JK |
1921 | int cant_be_filename, |
1922 | struct object_context *a_oc, | |
1923 | struct object_context *b_oc) | |
62faad5a JK |
1924 | { |
1925 | const char *a_name, *b_name; | |
1926 | struct object_id a_oid, b_oid; | |
1927 | struct object *a_obj, *b_obj; | |
1928 | unsigned int a_flags, b_flags; | |
1929 | int symmetric = 0; | |
1930 | unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM); | |
321c89bf | 1931 | unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH; |
62faad5a JK |
1932 | |
1933 | a_name = arg; | |
1934 | if (!*a_name) | |
1935 | a_name = "HEAD"; | |
1936 | ||
1937 | b_name = dotdot + 2; | |
1938 | if (*b_name == '.') { | |
1939 | symmetric = 1; | |
1940 | b_name++; | |
1941 | } | |
1942 | if (!*b_name) | |
1943 | b_name = "HEAD"; | |
1944 | ||
3a7a698e NTND |
1945 | if (get_oid_with_context(revs->repo, a_name, oc_flags, &a_oid, a_oc) || |
1946 | get_oid_with_context(revs->repo, b_name, oc_flags, &b_oid, b_oc)) | |
62faad5a JK |
1947 | return -1; |
1948 | ||
1949 | if (!cant_be_filename) { | |
1950 | *dotdot = '.'; | |
1951 | verify_non_filename(revs->prefix, arg); | |
1952 | *dotdot = '\0'; | |
1953 | } | |
1954 | ||
b3c7eef9 NTND |
1955 | a_obj = parse_object(revs->repo, &a_oid); |
1956 | b_obj = parse_object(revs->repo, &b_oid); | |
62faad5a JK |
1957 | if (!a_obj || !b_obj) |
1958 | return dotdot_missing(arg, dotdot, revs, symmetric); | |
1959 | ||
1960 | if (!symmetric) { | |
1961 | /* just A..B */ | |
1962 | b_flags = flags; | |
1963 | a_flags = flags_exclude; | |
1964 | } else { | |
1965 | /* A...B -- find merge bases between the two */ | |
1966 | struct commit *a, *b; | |
1967 | struct commit_list *exclude; | |
1968 | ||
b3c7eef9 NTND |
1969 | a = lookup_commit_reference(revs->repo, &a_obj->oid); |
1970 | b = lookup_commit_reference(revs->repo, &b_obj->oid); | |
62faad5a JK |
1971 | if (!a || !b) |
1972 | return dotdot_missing(arg, dotdot, revs, symmetric); | |
1973 | ||
1974 | exclude = get_merge_bases(a, b); | |
1975 | add_rev_cmdline_list(revs, exclude, REV_CMD_MERGE_BASE, | |
1976 | flags_exclude); | |
1977 | add_pending_commit_list(revs, exclude, flags_exclude); | |
1978 | free_commit_list(exclude); | |
1979 | ||
1980 | b_flags = flags; | |
1981 | a_flags = flags | SYMMETRIC_LEFT; | |
1982 | } | |
1983 | ||
1984 | a_obj->flags |= a_flags; | |
1985 | b_obj->flags |= b_flags; | |
1986 | add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags); | |
1987 | add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags); | |
18f1ad76 JK |
1988 | add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path); |
1989 | add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path); | |
62faad5a JK |
1990 | return 0; |
1991 | } | |
1992 | ||
1993 | static int handle_dotdot(const char *arg, | |
1994 | struct rev_info *revs, int flags, | |
1995 | int cant_be_filename) | |
1996 | { | |
18f1ad76 | 1997 | struct object_context a_oc, b_oc; |
62faad5a JK |
1998 | char *dotdot = strstr(arg, ".."); |
1999 | int ret; | |
2000 | ||
2001 | if (!dotdot) | |
2002 | return -1; | |
2003 | ||
18f1ad76 JK |
2004 | memset(&a_oc, 0, sizeof(a_oc)); |
2005 | memset(&b_oc, 0, sizeof(b_oc)); | |
2006 | ||
62faad5a | 2007 | *dotdot = '\0'; |
18f1ad76 JK |
2008 | ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename, |
2009 | &a_oc, &b_oc); | |
62faad5a JK |
2010 | *dotdot = '.'; |
2011 | ||
18f1ad76 JK |
2012 | free(a_oc.path); |
2013 | free(b_oc.path); | |
2014 | ||
62faad5a JK |
2015 | return ret; |
2016 | } | |
2017 | ||
04a0e985 | 2018 | static int handle_revision_arg_1(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt) |
5d6f0935 | 2019 | { |
249c8f4a | 2020 | struct object_context oc; |
f632dedd | 2021 | char *mark; |
5d6f0935 | 2022 | struct object *object; |
654b9a90 | 2023 | struct object_id oid; |
5d6f0935 | 2024 | int local_flags; |
281eee47 | 2025 | const char *arg = arg_; |
8e676e8b | 2026 | int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME; |
321c89bf | 2027 | unsigned get_sha1_flags = GET_OID_RECORD_PATH; |
5d6f0935 | 2028 | |
7f34a46f KB |
2029 | flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM; |
2030 | ||
d89797fe JK |
2031 | if (!cant_be_filename && !strcmp(arg, "..")) { |
2032 | /* | |
2033 | * Just ".."? That is not a range but the | |
2034 | * pathspec for the parent directory. | |
2035 | */ | |
2036 | return -1; | |
5d6f0935 | 2037 | } |
8779351d | 2038 | |
62faad5a JK |
2039 | if (!handle_dotdot(arg, revs, flags, revarg_opt)) |
2040 | return 0; | |
8779351d | 2041 | |
f632dedd JK |
2042 | mark = strstr(arg, "^@"); |
2043 | if (mark && !mark[2]) { | |
2044 | *mark = 0; | |
8779351d | 2045 | if (add_parents_only(revs, arg, flags, 0)) |
5d6f0935 | 2046 | return 0; |
f632dedd | 2047 | *mark = '^'; |
5d6f0935 | 2048 | } |
f632dedd JK |
2049 | mark = strstr(arg, "^!"); |
2050 | if (mark && !mark[2]) { | |
2051 | *mark = 0; | |
8779351d | 2052 | if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0)) |
f632dedd | 2053 | *mark = '^'; |
8779351d | 2054 | } |
f632dedd JK |
2055 | mark = strstr(arg, "^-"); |
2056 | if (mark) { | |
8779351d VN |
2057 | int exclude_parent = 1; |
2058 | ||
f632dedd | 2059 | if (mark[2]) { |
8779351d | 2060 | char *end; |
f632dedd | 2061 | exclude_parent = strtoul(mark + 2, &end, 10); |
8779351d VN |
2062 | if (*end != '\0' || !exclude_parent) |
2063 | return -1; | |
2064 | } | |
2065 | ||
f632dedd | 2066 | *mark = 0; |
8779351d | 2067 | if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent)) |
f632dedd | 2068 | *mark = '^'; |
62476c8e JH |
2069 | } |
2070 | ||
5d6f0935 JH |
2071 | local_flags = 0; |
2072 | if (*arg == '^') { | |
7f34a46f | 2073 | local_flags = UNINTERESTING | BOTTOM; |
5d6f0935 JH |
2074 | arg++; |
2075 | } | |
d5f6b1d7 JH |
2076 | |
2077 | if (revarg_opt & REVARG_COMMITTISH) | |
321c89bf | 2078 | get_sha1_flags |= GET_OID_COMMITTISH; |
d5f6b1d7 | 2079 | |
3a7a698e | 2080 | if (get_oid_with_context(revs->repo, arg, get_sha1_flags, &oid, &oc)) |
cc243c3c | 2081 | return revs->ignore_missing ? 0 : -1; |
5d6f0935 JH |
2082 | if (!cant_be_filename) |
2083 | verify_non_filename(revs->prefix, arg); | |
654b9a90 | 2084 | object = get_reference(revs, arg, &oid, flags ^ local_flags); |
4cf67869 MD |
2085 | if (!object) |
2086 | return revs->ignore_missing ? 0 : -1; | |
281eee47 | 2087 | add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags); |
18f1ad76 JK |
2088 | add_pending_object_with_path(revs, object, arg, oc.mode, oc.path); |
2089 | free(oc.path); | |
5d6f0935 JH |
2090 | return 0; |
2091 | } | |
2092 | ||
04a0e985 JK |
2093 | int handle_revision_arg(const char *arg, struct rev_info *revs, int flags, unsigned revarg_opt) |
2094 | { | |
2095 | int ret = handle_revision_arg_1(arg, revs, flags, revarg_opt); | |
2096 | if (!ret) | |
2097 | revs->rev_input_given = 1; | |
2098 | return ret; | |
2099 | } | |
2100 | ||
91633995 | 2101 | static void read_pathspec_from_stdin(struct strbuf *sb, |
c972bf4c | 2102 | struct strvec *prune) |
4da5af31 | 2103 | { |
7fa3c2ad | 2104 | while (strbuf_getline(sb, stdin) != EOF) |
c972bf4c | 2105 | strvec_push(prune, sb->buf); |
60da8b15 JH |
2106 | } |
2107 | ||
4da5af31 | 2108 | static void read_revisions_from_stdin(struct rev_info *revs, |
c972bf4c | 2109 | struct strvec *prune) |
1fc561d1 | 2110 | { |
63d564b3 | 2111 | struct strbuf sb; |
60da8b15 | 2112 | int seen_dashdash = 0; |
4c30d504 JK |
2113 | int save_warning; |
2114 | ||
2115 | save_warning = warn_on_object_refname_ambiguity; | |
2116 | warn_on_object_refname_ambiguity = 0; | |
1fc561d1 | 2117 | |
63d564b3 | 2118 | strbuf_init(&sb, 1000); |
6e8d46f9 | 2119 | while (strbuf_getline(&sb, stdin) != EOF) { |
63d564b3 | 2120 | int len = sb.len; |
1fc561d1 AB |
2121 | if (!len) |
2122 | break; | |
60da8b15 JH |
2123 | if (sb.buf[0] == '-') { |
2124 | if (len == 2 && sb.buf[1] == '-') { | |
2125 | seen_dashdash = 1; | |
2126 | break; | |
2127 | } | |
1fc561d1 | 2128 | die("options not supported in --stdin mode"); |
60da8b15 | 2129 | } |
31faeb20 | 2130 | if (handle_revision_arg(sb.buf, revs, 0, |
70d26c6e | 2131 | REVARG_CANNOT_BE_FILENAME)) |
63d564b3 | 2132 | die("bad revision '%s'", sb.buf); |
1fc561d1 | 2133 | } |
60da8b15 | 2134 | if (seen_dashdash) |
91633995 | 2135 | read_pathspec_from_stdin(&sb, prune); |
4c30d504 | 2136 | |
63d564b3 | 2137 | strbuf_release(&sb); |
4c30d504 | 2138 | warn_on_object_refname_ambiguity = save_warning; |
1fc561d1 AB |
2139 | } |
2140 | ||
2d10c555 | 2141 | static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what) |
bd95fcd3 | 2142 | { |
0843acfd | 2143 | append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what); |
2d10c555 JH |
2144 | } |
2145 | ||
a4d7d2c6 | 2146 | static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern) |
2d10c555 | 2147 | { |
a4d7d2c6 | 2148 | append_header_grep_pattern(&revs->grep_filter, field, pattern); |
bd95fcd3 JH |
2149 | } |
2150 | ||
2151 | static void add_message_grep(struct rev_info *revs, const char *pattern) | |
2152 | { | |
2d10c555 | 2153 | add_grep(revs, pattern, GREP_PATTERN_BODY); |
bd95fcd3 JH |
2154 | } |
2155 | ||
6b61ec05 | 2156 | static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv, |
bbcde41a MD |
2157 | int *unkc, const char **unkv, |
2158 | const struct setup_revision_opt* opt) | |
02e54220 PH |
2159 | { |
2160 | const char *arg = argv[0]; | |
7d7b86f7 MM |
2161 | const char *optarg; |
2162 | int argcount; | |
fd521245 | 2163 | const unsigned hexsz = the_hash_algo->hexsz; |
02e54220 PH |
2164 | |
2165 | /* pseudo revision arguments */ | |
2166 | if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") || | |
2167 | !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") || | |
2168 | !strcmp(arg, "--reflog") || !strcmp(arg, "--not") || | |
ad3f9a71 | 2169 | !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") || |
59556548 | 2170 | !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") || |
4fe10219 | 2171 | !strcmp(arg, "--indexed-objects") || |
39b44ba7 | 2172 | !strcmp(arg, "--alternate-refs") || |
07768e03 | 2173 | starts_with(arg, "--exclude=") || |
59556548 CC |
2174 | starts_with(arg, "--branches=") || starts_with(arg, "--tags=") || |
2175 | starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk=")) | |
02e54220 PH |
2176 | { |
2177 | unkv[(*unkc)++] = arg; | |
0fe8c138 | 2178 | return 1; |
02e54220 PH |
2179 | } |
2180 | ||
7d7b86f7 MM |
2181 | if ((argcount = parse_long_opt("max-count", argv, &optarg))) { |
2182 | revs->max_count = atoi(optarg); | |
5853caec | 2183 | revs->no_walk = 0; |
7d7b86f7 MM |
2184 | return argcount; |
2185 | } else if ((argcount = parse_long_opt("skip", argv, &optarg))) { | |
2186 | revs->skip_count = atoi(optarg); | |
2187 | return argcount; | |
02e54220 | 2188 | } else if ((*arg == '-') && isdigit(arg[1])) { |
e3fa568c JH |
2189 | /* accept -<digit>, like traditional "head" */ |
2190 | if (strtol_i(arg + 1, 10, &revs->max_count) < 0 || | |
2191 | revs->max_count < 0) | |
2192 | die("'%s': not a non-negative integer", arg + 1); | |
5853caec | 2193 | revs->no_walk = 0; |
02e54220 PH |
2194 | } else if (!strcmp(arg, "-n")) { |
2195 | if (argc <= 1) | |
2196 | return error("-n requires an argument"); | |
2197 | revs->max_count = atoi(argv[1]); | |
5853caec | 2198 | revs->no_walk = 0; |
02e54220 | 2199 | return 2; |
479b3d97 SG |
2200 | } else if (skip_prefix(arg, "-n", &optarg)) { |
2201 | revs->max_count = atoi(optarg); | |
5853caec | 2202 | revs->no_walk = 0; |
7d7b86f7 MM |
2203 | } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) { |
2204 | revs->max_age = atoi(optarg); | |
2205 | return argcount; | |
2206 | } else if ((argcount = parse_long_opt("since", argv, &optarg))) { | |
2207 | revs->max_age = approxidate(optarg); | |
2208 | return argcount; | |
2209 | } else if ((argcount = parse_long_opt("after", argv, &optarg))) { | |
2210 | revs->max_age = approxidate(optarg); | |
2211 | return argcount; | |
2212 | } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) { | |
2213 | revs->min_age = atoi(optarg); | |
2214 | return argcount; | |
2215 | } else if ((argcount = parse_long_opt("before", argv, &optarg))) { | |
2216 | revs->min_age = approxidate(optarg); | |
2217 | return argcount; | |
2218 | } else if ((argcount = parse_long_opt("until", argv, &optarg))) { | |
2219 | revs->min_age = approxidate(optarg); | |
2220 | return argcount; | |
02e54220 PH |
2221 | } else if (!strcmp(arg, "--first-parent")) { |
2222 | revs->first_parent_only = 1; | |
ebdc94f3 JH |
2223 | } else if (!strcmp(arg, "--ancestry-path")) { |
2224 | revs->ancestry_path = 1; | |
cb7529e1 | 2225 | revs->simplify_history = 0; |
ebdc94f3 | 2226 | revs->limited = 1; |
02e54220 PH |
2227 | } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) { |
2228 | init_reflog_walk(&revs->reflog_info); | |
2229 | } else if (!strcmp(arg, "--default")) { | |
2230 | if (argc <= 1) | |
2231 | return error("bad --default argument"); | |
2232 | revs->def = argv[1]; | |
2233 | return 2; | |
2234 | } else if (!strcmp(arg, "--merge")) { | |
2235 | revs->show_merge = 1; | |
2236 | } else if (!strcmp(arg, "--topo-order")) { | |
08f704f2 | 2237 | revs->sort_order = REV_SORT_IN_GRAPH_ORDER; |
02e54220 | 2238 | revs->topo_order = 1; |
6546b593 JH |
2239 | } else if (!strcmp(arg, "--simplify-merges")) { |
2240 | revs->simplify_merges = 1; | |
a52f0071 | 2241 | revs->topo_order = 1; |
6546b593 JH |
2242 | revs->rewrite_parents = 1; |
2243 | revs->simplify_history = 0; | |
2244 | revs->limited = 1; | |
78892e32 LT |
2245 | } else if (!strcmp(arg, "--simplify-by-decoration")) { |
2246 | revs->simplify_merges = 1; | |
a52f0071 | 2247 | revs->topo_order = 1; |
78892e32 LT |
2248 | revs->rewrite_parents = 1; |
2249 | revs->simplify_history = 0; | |
2250 | revs->simplify_by_decoration = 1; | |
2251 | revs->limited = 1; | |
2252 | revs->prune = 1; | |
02e54220 | 2253 | } else if (!strcmp(arg, "--date-order")) { |
08f704f2 | 2254 | revs->sort_order = REV_SORT_BY_COMMIT_DATE; |
02e54220 | 2255 | revs->topo_order = 1; |
81c6b38b JH |
2256 | } else if (!strcmp(arg, "--author-date-order")) { |
2257 | revs->sort_order = REV_SORT_BY_AUTHOR_DATE; | |
02e54220 | 2258 | revs->topo_order = 1; |
dffc651e SG |
2259 | } else if (!strcmp(arg, "--early-output")) { |
2260 | revs->early_output = 100; | |
2261 | revs->topo_order = 1; | |
2262 | } else if (skip_prefix(arg, "--early-output=", &optarg)) { | |
2263 | if (strtoul_ui(optarg, 10, &revs->early_output) < 0) | |
2264 | die("'%s': not a non-negative integer", optarg); | |
2265 | revs->topo_order = 1; | |
02e54220 PH |
2266 | } else if (!strcmp(arg, "--parents")) { |
2267 | revs->rewrite_parents = 1; | |
2268 | revs->print_parents = 1; | |
2269 | } else if (!strcmp(arg, "--dense")) { | |
2270 | revs->dense = 1; | |
2271 | } else if (!strcmp(arg, "--sparse")) { | |
2272 | revs->dense = 0; | |
ce5b6f9b SB |
2273 | } else if (!strcmp(arg, "--in-commit-order")) { |
2274 | revs->tree_blobs_in_commit_order = 1; | |
02e54220 PH |
2275 | } else if (!strcmp(arg, "--remove-empty")) { |
2276 | revs->remove_empty_trees = 1; | |
b8e8db28 | 2277 | } else if (!strcmp(arg, "--merges")) { |
ad5aeede | 2278 | revs->min_parents = 2; |
02e54220 | 2279 | } else if (!strcmp(arg, "--no-merges")) { |
ad5aeede | 2280 | revs->max_parents = 1; |
479b3d97 SG |
2281 | } else if (skip_prefix(arg, "--min-parents=", &optarg)) { |
2282 | revs->min_parents = atoi(optarg); | |
9ada7aee | 2283 | } else if (!strcmp(arg, "--no-min-parents")) { |
ad5aeede | 2284 | revs->min_parents = 0; |
479b3d97 SG |
2285 | } else if (skip_prefix(arg, "--max-parents=", &optarg)) { |
2286 | revs->max_parents = atoi(optarg); | |
9ada7aee | 2287 | } else if (!strcmp(arg, "--no-max-parents")) { |
ad5aeede | 2288 | revs->max_parents = -1; |
02e54220 PH |
2289 | } else if (!strcmp(arg, "--boundary")) { |
2290 | revs->boundary = 1; | |
2291 | } else if (!strcmp(arg, "--left-right")) { | |
2292 | revs->left_right = 1; | |
60adf7d7 | 2293 | } else if (!strcmp(arg, "--left-only")) { |
24852d91 | 2294 | if (revs->right_only) |
94f605ec MG |
2295 | die("--left-only is incompatible with --right-only" |
2296 | " or --cherry"); | |
60adf7d7 MG |
2297 | revs->left_only = 1; |
2298 | } else if (!strcmp(arg, "--right-only")) { | |
24852d91 JH |
2299 | if (revs->left_only) |
2300 | die("--right-only is incompatible with --left-only"); | |
60adf7d7 | 2301 | revs->right_only = 1; |
94f605ec MG |
2302 | } else if (!strcmp(arg, "--cherry")) { |
2303 | if (revs->left_only) | |
2304 | die("--cherry is incompatible with --left-only"); | |
2305 | revs->cherry_mark = 1; | |
2306 | revs->right_only = 1; | |
ad5aeede | 2307 | revs->max_parents = 1; |
94f605ec | 2308 | revs->limited = 1; |
f69c5018 TR |
2309 | } else if (!strcmp(arg, "--count")) { |
2310 | revs->count = 1; | |
adbbb31e MG |
2311 | } else if (!strcmp(arg, "--cherry-mark")) { |
2312 | if (revs->cherry_pick) | |
2313 | die("--cherry-mark is incompatible with --cherry-pick"); | |
2314 | revs->cherry_mark = 1; | |
2315 | revs->limited = 1; /* needs limit_list() */ | |
02e54220 | 2316 | } else if (!strcmp(arg, "--cherry-pick")) { |
adbbb31e MG |
2317 | if (revs->cherry_mark) |
2318 | die("--cherry-pick is incompatible with --cherry-mark"); | |
02e54220 PH |
2319 | revs->cherry_pick = 1; |
2320 | revs->limited = 1; | |
2321 | } else if (!strcmp(arg, "--objects")) { | |
2322 | revs->tag_objects = 1; | |
2323 | revs->tree_objects = 1; | |
2324 | revs->blob_objects = 1; | |
2325 | } else if (!strcmp(arg, "--objects-edge")) { | |
2326 | revs->tag_objects = 1; | |
2327 | revs->tree_objects = 1; | |
2328 | revs->blob_objects = 1; | |
2329 | revs->edge_hint = 1; | |
1684c1b2 | 2330 | } else if (!strcmp(arg, "--objects-edge-aggressive")) { |
2331 | revs->tag_objects = 1; | |
2332 | revs->tree_objects = 1; | |
2333 | revs->blob_objects = 1; | |
2334 | revs->edge_hint = 1; | |
2335 | revs->edge_hint_aggressive = 1; | |
5a48d240 JH |
2336 | } else if (!strcmp(arg, "--verify-objects")) { |
2337 | revs->tag_objects = 1; | |
2338 | revs->tree_objects = 1; | |
2339 | revs->blob_objects = 1; | |
2340 | revs->verify_objects = 1; | |
02e54220 PH |
2341 | } else if (!strcmp(arg, "--unpacked")) { |
2342 | revs->unpacked = 1; | |
59556548 | 2343 | } else if (starts_with(arg, "--unpacked=")) { |
f649aaaf | 2344 | die(_("--unpacked=<packfile> no longer supported")); |
c9fff000 TB |
2345 | } else if (!strcmp(arg, "--no-kept-objects")) { |
2346 | revs->no_kept_objects = 1; | |
2347 | revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS; | |
2348 | revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS; | |
2349 | } else if (skip_prefix(arg, "--no-kept-objects=", &optarg)) { | |
2350 | revs->no_kept_objects = 1; | |
2351 | if (!strcmp(optarg, "in-core")) | |
2352 | revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS; | |
2353 | if (!strcmp(optarg, "on-disk")) | |
2354 | revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS; | |
02e54220 PH |
2355 | } else if (!strcmp(arg, "-r")) { |
2356 | revs->diff = 1; | |
0d1e0e78 | 2357 | revs->diffopt.flags.recursive = 1; |
02e54220 PH |
2358 | } else if (!strcmp(arg, "-t")) { |
2359 | revs->diff = 1; | |
0d1e0e78 BW |
2360 | revs->diffopt.flags.recursive = 1; |
2361 | revs->diffopt.flags.tree_in_recursive = 1; | |
18f09473 | 2362 | } else if ((argcount = diff_merges_parse_opts(revs, argv))) { |
6501580f | 2363 | return argcount; |
02e54220 PH |
2364 | } else if (!strcmp(arg, "-v")) { |
2365 | revs->verbose_header = 1; | |
2366 | } else if (!strcmp(arg, "--pretty")) { | |
2367 | revs->verbose_header = 1; | |
66b2ed09 | 2368 | revs->pretty_given = 1; |
ae18165f | 2369 | get_commit_format(NULL, revs); |
479b3d97 SG |
2370 | } else if (skip_prefix(arg, "--pretty=", &optarg) || |
2371 | skip_prefix(arg, "--format=", &optarg)) { | |
7d7b86f7 MM |
2372 | /* |
2373 | * Detached form ("--pretty X" as opposed to "--pretty=X") | |
2374 | * not allowed, since the argument is optional. | |
2375 | */ | |
02e54220 | 2376 | revs->verbose_header = 1; |
66b2ed09 | 2377 | revs->pretty_given = 1; |
479b3d97 | 2378 | get_commit_format(optarg, revs); |
7cc13c71 | 2379 | } else if (!strcmp(arg, "--expand-tabs")) { |
fe37a9c5 | 2380 | revs->expand_tabs_in_log = 8; |
0893eec8 JH |
2381 | } else if (!strcmp(arg, "--no-expand-tabs")) { |
2382 | revs->expand_tabs_in_log = 0; | |
fe37a9c5 JH |
2383 | } else if (skip_prefix(arg, "--expand-tabs=", &arg)) { |
2384 | int val; | |
2385 | if (strtol_i(arg, 10, &val) < 0 || val < 0) | |
2386 | die("'%s': not a non-negative integer", arg); | |
2387 | revs->expand_tabs_in_log = val; | |
7249e912 | 2388 | } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) { |
1d729751 | 2389 | enable_default_display_notes(&revs->notes_opt, &revs->show_notes); |
66b2ed09 | 2390 | revs->show_notes_given = 1; |
0c37f1fc JH |
2391 | } else if (!strcmp(arg, "--show-signature")) { |
2392 | revs->show_signature = 1; | |
aa379999 MJ |
2393 | } else if (!strcmp(arg, "--no-show-signature")) { |
2394 | revs->show_signature = 0; | |
479b3d97 SG |
2395 | } else if (!strcmp(arg, "--show-linear-break")) { |
2396 | revs->break_bar = " .........."; | |
1b32dece NTND |
2397 | revs->track_linear = 1; |
2398 | revs->track_first_time = 1; | |
479b3d97 SG |
2399 | } else if (skip_prefix(arg, "--show-linear-break=", &optarg)) { |
2400 | revs->break_bar = xstrdup(optarg); | |
1b32dece NTND |
2401 | revs->track_linear = 1; |
2402 | revs->track_first_time = 1; | |
479b3d97 SG |
2403 | } else if (skip_prefix(arg, "--show-notes=", &optarg) || |
2404 | skip_prefix(arg, "--notes=", &optarg)) { | |
479b3d97 SG |
2405 | if (starts_with(arg, "--show-notes=") && |
2406 | revs->notes_opt.use_default_notes < 0) | |
2407 | revs->notes_opt.use_default_notes = 1; | |
1d729751 | 2408 | enable_ref_display_notes(&revs->notes_opt, &revs->show_notes, optarg); |
452538c3 | 2409 | revs->show_notes_given = 1; |
66b2ed09 | 2410 | } else if (!strcmp(arg, "--no-notes")) { |
1d729751 | 2411 | disable_display_notes(&revs->notes_opt, &revs->show_notes); |
66b2ed09 | 2412 | revs->show_notes_given = 1; |
894a9d33 TR |
2413 | } else if (!strcmp(arg, "--standard-notes")) { |
2414 | revs->show_notes_given = 1; | |
3a03cf6b | 2415 | revs->notes_opt.use_default_notes = 1; |
894a9d33 | 2416 | } else if (!strcmp(arg, "--no-standard-notes")) { |
3a03cf6b | 2417 | revs->notes_opt.use_default_notes = 0; |
de84accc NS |
2418 | } else if (!strcmp(arg, "--oneline")) { |
2419 | revs->verbose_header = 1; | |
2420 | get_commit_format("oneline", revs); | |
7dccadf3 | 2421 | revs->pretty_given = 1; |
de84accc | 2422 | revs->abbrev_commit = 1; |
02e54220 PH |
2423 | } else if (!strcmp(arg, "--graph")) { |
2424 | revs->topo_order = 1; | |
2425 | revs->rewrite_parents = 1; | |
2426 | revs->graph = graph_init(revs); | |
19d097e3 EB |
2427 | } else if (!strcmp(arg, "--encode-email-headers")) { |
2428 | revs->encode_email_headers = 1; | |
2429 | } else if (!strcmp(arg, "--no-encode-email-headers")) { | |
2430 | revs->encode_email_headers = 0; | |
02e54220 PH |
2431 | } else if (!strcmp(arg, "--root")) { |
2432 | revs->show_root_diff = 1; | |
2433 | } else if (!strcmp(arg, "--no-commit-id")) { | |
2434 | revs->no_commit_id = 1; | |
2435 | } else if (!strcmp(arg, "--always")) { | |
2436 | revs->always_show_header = 1; | |
2437 | } else if (!strcmp(arg, "--no-abbrev")) { | |
2438 | revs->abbrev = 0; | |
2439 | } else if (!strcmp(arg, "--abbrev")) { | |
2440 | revs->abbrev = DEFAULT_ABBREV; | |
479b3d97 SG |
2441 | } else if (skip_prefix(arg, "--abbrev=", &optarg)) { |
2442 | revs->abbrev = strtoul(optarg, NULL, 10); | |
02e54220 PH |
2443 | if (revs->abbrev < MINIMUM_ABBREV) |
2444 | revs->abbrev = MINIMUM_ABBREV; | |
fd521245 | 2445 | else if (revs->abbrev > hexsz) |
2446 | revs->abbrev = hexsz; | |
02e54220 PH |
2447 | } else if (!strcmp(arg, "--abbrev-commit")) { |
2448 | revs->abbrev_commit = 1; | |
0c47695a JS |
2449 | revs->abbrev_commit_given = 1; |
2450 | } else if (!strcmp(arg, "--no-abbrev-commit")) { | |
2451 | revs->abbrev_commit = 0; | |
02e54220 PH |
2452 | } else if (!strcmp(arg, "--full-diff")) { |
2453 | revs->diff = 1; | |
2454 | revs->full_diff = 1; | |
8d049e18 DS |
2455 | } else if (!strcmp(arg, "--show-pulls")) { |
2456 | revs->show_pulls = 1; | |
02e54220 PH |
2457 | } else if (!strcmp(arg, "--full-history")) { |
2458 | revs->simplify_history = 0; | |
2459 | } else if (!strcmp(arg, "--relative-date")) { | |
a5481a6c | 2460 | revs->date_mode.type = DATE_RELATIVE; |
f4ea32f0 | 2461 | revs->date_mode_explicit = 1; |
7d7b86f7 | 2462 | } else if ((argcount = parse_long_opt("date", argv, &optarg))) { |
a5481a6c | 2463 | parse_date_format(optarg, &revs->date_mode); |
f4ea32f0 | 2464 | revs->date_mode_explicit = 1; |
7d7b86f7 | 2465 | return argcount; |
02e54220 PH |
2466 | } else if (!strcmp(arg, "--log-size")) { |
2467 | revs->show_log_size = 1; | |
2468 | } | |
2469 | /* | |
2470 | * Grepping the commit log | |
2471 | */ | |
7d7b86f7 MM |
2472 | else if ((argcount = parse_long_opt("author", argv, &optarg))) { |
2473 | add_header_grep(revs, GREP_HEADER_AUTHOR, optarg); | |
2474 | return argcount; | |
2475 | } else if ((argcount = parse_long_opt("committer", argv, &optarg))) { | |
2476 | add_header_grep(revs, GREP_HEADER_COMMITTER, optarg); | |
2477 | return argcount; | |
72fd13f7 NTND |
2478 | } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) { |
2479 | add_header_grep(revs, GREP_HEADER_REFLOG, optarg); | |
2480 | return argcount; | |
7d7b86f7 MM |
2481 | } else if ((argcount = parse_long_opt("grep", argv, &optarg))) { |
2482 | add_message_grep(revs, optarg); | |
2483 | return argcount; | |
727b6fc3 | 2484 | } else if (!strcmp(arg, "--basic-regexp")) { |
8465541e | 2485 | revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE; |
02e54220 | 2486 | } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) { |
8465541e | 2487 | revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE; |
02e54220 | 2488 | } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) { |
9e3cbc59 | 2489 | revs->grep_filter.ignore_case = 1; |
c1ddc461 | 2490 | revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE; |
02e54220 | 2491 | } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) { |
8465541e | 2492 | revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED; |
7531a2dd | 2493 | } else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) { |
8465541e | 2494 | revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE; |
02e54220 | 2495 | } else if (!strcmp(arg, "--all-match")) { |
0843acfd | 2496 | revs->grep_filter.all_match = 1; |
22dfa8a2 CJ |
2497 | } else if (!strcmp(arg, "--invert-grep")) { |
2498 | revs->invert_grep = 1; | |
7d7b86f7 MM |
2499 | } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) { |
2500 | if (strcmp(optarg, "none")) | |
2501 | git_log_output_encoding = xstrdup(optarg); | |
02e54220 PH |
2502 | else |
2503 | git_log_output_encoding = ""; | |
7d7b86f7 | 2504 | return argcount; |
02e54220 PH |
2505 | } else if (!strcmp(arg, "--reverse")) { |
2506 | revs->reverse ^= 1; | |
2507 | } else if (!strcmp(arg, "--children")) { | |
2508 | revs->children.name = "children"; | |
2509 | revs->limited = 1; | |
cc243c3c JH |
2510 | } else if (!strcmp(arg, "--ignore-missing")) { |
2511 | revs->ignore_missing = 1; | |
bbcde41a | 2512 | } else if (opt && opt->allow_exclude_promisor_objects && |
669b1d2a | 2513 | !strcmp(arg, "--exclude-promisor-objects")) { |
df11e196 | 2514 | if (fetch_if_missing) |
033abf97 | 2515 | BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0"); |
df11e196 | 2516 | revs->exclude_promisor_objects = 1; |
02e54220 | 2517 | } else { |
a97262c6 | 2518 | int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix); |
02e54220 PH |
2519 | if (!opts) |
2520 | unkv[(*unkc)++] = arg; | |
2521 | return opts; | |
2522 | } | |
1b32dece NTND |
2523 | if (revs->graph && revs->track_linear) |
2524 | die("--show-linear-break and --graph are incompatible"); | |
02e54220 PH |
2525 | |
2526 | return 1; | |
2527 | } | |
2528 | ||
6b61ec05 PH |
2529 | void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx, |
2530 | const struct option *options, | |
2531 | const char * const usagestr[]) | |
2532 | { | |
2533 | int n = handle_revision_opt(revs, ctx->argc, ctx->argv, | |
bbcde41a | 2534 | &ctx->cpidx, ctx->out, NULL); |
6b61ec05 PH |
2535 | if (n <= 0) { |
2536 | error("unknown option `%s'", ctx->argv[0]); | |
2537 | usage_with_options(usagestr, options); | |
2538 | } | |
2539 | ctx->argv += n; | |
2540 | ctx->argc -= n; | |
2541 | } | |
2542 | ||
073cf63c NTND |
2543 | static int for_each_bisect_ref(struct ref_store *refs, each_ref_fn fn, |
2544 | void *cb_data, const char *term) | |
2545 | { | |
cb46d630 AD |
2546 | struct strbuf bisect_refs = STRBUF_INIT; |
2547 | int status; | |
2548 | strbuf_addf(&bisect_refs, "refs/bisect/%s", term); | |
073cf63c | 2549 | status = refs_for_each_fullref_in(refs, bisect_refs.buf, fn, cb_data, 0); |
cb46d630 AD |
2550 | strbuf_release(&bisect_refs); |
2551 | return status; | |
2552 | } | |
2553 | ||
073cf63c | 2554 | static int for_each_bad_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data) |
ad3f9a71 | 2555 | { |
073cf63c | 2556 | return for_each_bisect_ref(refs, fn, cb_data, term_bad); |
ad3f9a71 LT |
2557 | } |
2558 | ||
073cf63c | 2559 | static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data) |
ad3f9a71 | 2560 | { |
073cf63c | 2561 | return for_each_bisect_ref(refs, fn, cb_data, term_good); |
ad3f9a71 LT |
2562 | } |
2563 | ||
f6aca0dc | 2564 | static int handle_revision_pseudo_opt(const char *submodule, |
e885a84f JK |
2565 | struct rev_info *revs, |
2566 | const char **argv, int *flags) | |
f6aca0dc JN |
2567 | { |
2568 | const char *arg = argv[0]; | |
2569 | const char *optarg; | |
073cf63c | 2570 | struct ref_store *refs; |
f6aca0dc JN |
2571 | int argcount; |
2572 | ||
073cf63c | 2573 | if (submodule) { |
d0c39a49 NTND |
2574 | /* |
2575 | * We need some something like get_submodule_worktrees() | |
2576 | * before we can go through all worktrees of a submodule, | |
2577 | * .e.g with adding all HEADs from --all, which is not | |
2578 | * supported right now, so stick to single worktree. | |
2579 | */ | |
2580 | if (!revs->single_worktree) | |
033abf97 | 2581 | BUG("--single-worktree cannot be used together with submodule"); |
073cf63c NTND |
2582 | refs = get_submodule_ref_store(submodule); |
2583 | } else | |
b3c7eef9 | 2584 | refs = get_main_ref_store(revs->repo); |
073cf63c | 2585 | |
0fc63ec4 JN |
2586 | /* |
2587 | * NOTE! | |
2588 | * | |
2589 | * Commands like "git shortlog" will not accept the options below | |
2590 | * unless parse_revision_opt queues them (as opposed to erroring | |
2591 | * out). | |
2592 | * | |
2593 | * When implementing your new pseudo-option, remember to | |
2594 | * register it in the list at the top of handle_revision_opt. | |
2595 | */ | |
f6aca0dc | 2596 | if (!strcmp(arg, "--all")) { |
073cf63c NTND |
2597 | handle_refs(refs, revs, *flags, refs_for_each_ref); |
2598 | handle_refs(refs, revs, *flags, refs_head_ref); | |
d0c39a49 NTND |
2599 | if (!revs->single_worktree) { |
2600 | struct all_refs_cb cb; | |
2601 | ||
2602 | init_all_refs_cb(&cb, revs, *flags); | |
2603 | other_head_refs(handle_one_ref, &cb); | |
2604 | } | |
ff32d342 | 2605 | clear_ref_exclusion(&revs->ref_excludes); |
f6aca0dc | 2606 | } else if (!strcmp(arg, "--branches")) { |
073cf63c | 2607 | handle_refs(refs, revs, *flags, refs_for_each_branch_ref); |
ff32d342 | 2608 | clear_ref_exclusion(&revs->ref_excludes); |
f6aca0dc | 2609 | } else if (!strcmp(arg, "--bisect")) { |
cb46d630 | 2610 | read_bisect_terms(&term_bad, &term_good); |
073cf63c NTND |
2611 | handle_refs(refs, revs, *flags, for_each_bad_bisect_ref); |
2612 | handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM), | |
2613 | for_each_good_bisect_ref); | |
f6aca0dc JN |
2614 | revs->bisect = 1; |
2615 | } else if (!strcmp(arg, "--tags")) { | |
073cf63c | 2616 | handle_refs(refs, revs, *flags, refs_for_each_tag_ref); |
ff32d342 | 2617 | clear_ref_exclusion(&revs->ref_excludes); |
f6aca0dc | 2618 | } else if (!strcmp(arg, "--remotes")) { |
073cf63c | 2619 | handle_refs(refs, revs, *flags, refs_for_each_remote_ref); |
ff32d342 | 2620 | clear_ref_exclusion(&revs->ref_excludes); |
f6aca0dc JN |
2621 | } else if ((argcount = parse_long_opt("glob", argv, &optarg))) { |
2622 | struct all_refs_cb cb; | |
2623 | init_all_refs_cb(&cb, revs, *flags); | |
a217dcbd | 2624 | for_each_glob_ref(handle_one_ref, optarg, &cb); |
ff32d342 | 2625 | clear_ref_exclusion(&revs->ref_excludes); |
e7b432c5 JH |
2626 | return argcount; |
2627 | } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) { | |
ff32d342 | 2628 | add_ref_exclusion(&revs->ref_excludes, optarg); |
f6aca0dc | 2629 | return argcount; |
8b1d9136 | 2630 | } else if (skip_prefix(arg, "--branches=", &optarg)) { |
f6aca0dc JN |
2631 | struct all_refs_cb cb; |
2632 | init_all_refs_cb(&cb, revs, *flags); | |
8b1d9136 | 2633 | for_each_glob_ref_in(handle_one_ref, optarg, "refs/heads/", &cb); |
ff32d342 | 2634 | clear_ref_exclusion(&revs->ref_excludes); |
8b1d9136 | 2635 | } else if (skip_prefix(arg, "--tags=", &optarg)) { |
f6aca0dc JN |
2636 | struct all_refs_cb cb; |
2637 | init_all_refs_cb(&cb, revs, *flags); | |
8b1d9136 | 2638 | for_each_glob_ref_in(handle_one_ref, optarg, "refs/tags/", &cb); |
ff32d342 | 2639 | clear_ref_exclusion(&revs->ref_excludes); |
8b1d9136 | 2640 | } else if (skip_prefix(arg, "--remotes=", &optarg)) { |
f6aca0dc JN |
2641 | struct all_refs_cb cb; |
2642 | init_all_refs_cb(&cb, revs, *flags); | |
8b1d9136 | 2643 | for_each_glob_ref_in(handle_one_ref, optarg, "refs/remotes/", &cb); |
ff32d342 | 2644 | clear_ref_exclusion(&revs->ref_excludes); |
f6aca0dc | 2645 | } else if (!strcmp(arg, "--reflog")) { |
718ccc97 | 2646 | add_reflogs_to_pending(revs, *flags); |
4fe10219 JK |
2647 | } else if (!strcmp(arg, "--indexed-objects")) { |
2648 | add_index_objects_to_pending(revs, *flags); | |
39b44ba7 JK |
2649 | } else if (!strcmp(arg, "--alternate-refs")) { |
2650 | add_alternate_refs_to_pending(revs, *flags); | |
f6aca0dc | 2651 | } else if (!strcmp(arg, "--not")) { |
7f34a46f | 2652 | *flags ^= UNINTERESTING | BOTTOM; |
f6aca0dc | 2653 | } else if (!strcmp(arg, "--no-walk")) { |
ca92e59e | 2654 | revs->no_walk = REVISION_WALK_NO_WALK_SORTED; |
8b1d9136 | 2655 | } else if (skip_prefix(arg, "--no-walk=", &optarg)) { |
ca92e59e MZ |
2656 | /* |
2657 | * Detached form ("--no-walk X" as opposed to "--no-walk=X") | |
2658 | * not allowed, since the argument is optional. | |
2659 | */ | |
8b1d9136 | 2660 | if (!strcmp(optarg, "sorted")) |
ca92e59e | 2661 | revs->no_walk = REVISION_WALK_NO_WALK_SORTED; |
8b1d9136 | 2662 | else if (!strcmp(optarg, "unsorted")) |
ca92e59e MZ |
2663 | revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED; |
2664 | else | |
2665 | return error("invalid argument to --no-walk"); | |
f6aca0dc JN |
2666 | } else if (!strcmp(arg, "--do-walk")) { |
2667 | revs->no_walk = 0; | |
32619f99 NTND |
2668 | } else if (!strcmp(arg, "--single-worktree")) { |
2669 | revs->single_worktree = 1; | |
f6aca0dc JN |
2670 | } else { |
2671 | return 0; | |
2672 | } | |
2673 | ||
2674 | return 1; | |
2675 | } | |
2676 | ||
ce113604 JK |
2677 | static void NORETURN diagnose_missing_default(const char *def) |
2678 | { | |
ce113604 JK |
2679 | int flags; |
2680 | const char *refname; | |
2681 | ||
744c040b | 2682 | refname = resolve_ref_unsafe(def, 0, NULL, &flags); |
ce113604 JK |
2683 | if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN)) |
2684 | die(_("your current branch appears to be broken")); | |
2685 | ||
2686 | skip_prefix(refname, "refs/heads/", &refname); | |
2687 | die(_("your current branch '%s' does not have any commits yet"), | |
2688 | refname); | |
2689 | } | |
2690 | ||
ae563542 LT |
2691 | /* |
2692 | * Parse revision information, filling in the "rev_info" structure, | |
2693 | * and removing the used arguments from the argument list. | |
2694 | * | |
765ac8ec LT |
2695 | * Returns the number of arguments left that weren't recognized |
2696 | * (which are also moved to the head of the argument list) | |
ae563542 | 2697 | */ |
32962c9b | 2698 | int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt) |
ae563542 | 2699 | { |
04a0e985 | 2700 | int i, flags, left, seen_dashdash, revarg_opt; |
c972bf4c | 2701 | struct strvec prune_data = STRVEC_INIT; |
9ef6aeb0 | 2702 | const char *submodule = NULL; |
19e8789b | 2703 | int seen_end_of_options = 0; |
9ef6aeb0 HV |
2704 | |
2705 | if (opt) | |
2706 | submodule = opt->submodule; | |
ae563542 | 2707 | |
ae563542 | 2708 | /* First, search for "--" */ |
6d5b93f2 | 2709 | if (opt && opt->assume_dashdash) { |
ae563542 | 2710 | seen_dashdash = 1; |
6d5b93f2 CB |
2711 | } else { |
2712 | seen_dashdash = 0; | |
2713 | for (i = 1; i < argc; i++) { | |
2714 | const char *arg = argv[i]; | |
2715 | if (strcmp(arg, "--")) | |
2716 | continue; | |
2717 | argv[i] = NULL; | |
2718 | argc = i; | |
2719 | if (argv[i + 1]) | |
c972bf4c | 2720 | strvec_pushv(&prune_data, argv + i + 1); |
6d5b93f2 CB |
2721 | seen_dashdash = 1; |
2722 | break; | |
2723 | } | |
ae563542 LT |
2724 | } |
2725 | ||
02e54220 PH |
2726 | /* Second, deal with arguments and options */ |
2727 | flags = 0; | |
d5f6b1d7 JH |
2728 | revarg_opt = opt ? opt->revarg_opt : 0; |
2729 | if (seen_dashdash) | |
2730 | revarg_opt |= REVARG_CANNOT_BE_FILENAME; | |
02e54220 | 2731 | for (left = i = 1; i < argc; i++) { |
ae563542 | 2732 | const char *arg = argv[i]; |
19e8789b | 2733 | if (!seen_end_of_options && *arg == '-') { |
cd2bdc53 | 2734 | int opts; |
02e54220 | 2735 | |
f6aca0dc | 2736 | opts = handle_revision_pseudo_opt(submodule, |
e885a84f | 2737 | revs, argv + i, |
f6aca0dc JN |
2738 | &flags); |
2739 | if (opts > 0) { | |
2740 | i += opts - 1; | |
8e64006e JS |
2741 | continue; |
2742 | } | |
f6aca0dc | 2743 | |
8b3dce56 JH |
2744 | if (!strcmp(arg, "--stdin")) { |
2745 | if (revs->disable_stdin) { | |
2746 | argv[left++] = arg; | |
2747 | continue; | |
2748 | } | |
a12cbe23 | 2749 | if (revs->read_from_stdin++) |
8b3dce56 | 2750 | die("--stdin given twice?"); |
60da8b15 | 2751 | read_revisions_from_stdin(revs, &prune_data); |
8b3dce56 JH |
2752 | continue; |
2753 | } | |
2d10c555 | 2754 | |
19e8789b JK |
2755 | if (!strcmp(arg, "--end-of-options")) { |
2756 | seen_end_of_options = 1; | |
2757 | continue; | |
2758 | } | |
2759 | ||
bbcde41a MD |
2760 | opts = handle_revision_opt(revs, argc - i, argv + i, |
2761 | &left, argv, opt); | |
cd2bdc53 | 2762 | if (opts > 0) { |
cd2bdc53 LT |
2763 | i += opts - 1; |
2764 | continue; | |
2765 | } | |
02e54220 PH |
2766 | if (opts < 0) |
2767 | exit(128); | |
ae563542 LT |
2768 | continue; |
2769 | } | |
ae563542 | 2770 | |
8e676e8b JH |
2771 | |
2772 | if (handle_revision_arg(arg, revs, flags, revarg_opt)) { | |
5d6f0935 JH |
2773 | int j; |
2774 | if (seen_dashdash || *arg == '^') | |
ae563542 LT |
2775 | die("bad revision '%s'", arg); |
2776 | ||
ea92f41f JH |
2777 | /* If we didn't have a "--": |
2778 | * (1) all filenames must exist; | |
2779 | * (2) all rev-args must not be interpretable | |
2780 | * as a valid filename. | |
2781 | * but the latter we have checked in the main loop. | |
2782 | */ | |
e23d0b4a | 2783 | for (j = i; j < argc; j++) |
023e37c3 | 2784 | verify_filename(revs->prefix, argv[j], j == i); |
e23d0b4a | 2785 | |
c972bf4c | 2786 | strvec_pushv(&prune_data, argv + i); |
ae563542 LT |
2787 | break; |
2788 | } | |
ae563542 | 2789 | } |
5d6f0935 | 2790 | |
d70a9eb6 | 2791 | if (prune_data.nr) { |
93e7d672 JH |
2792 | /* |
2793 | * If we need to introduce the magic "a lone ':' means no | |
2794 | * pathspec whatsoever", here is the place to do so. | |
2795 | * | |
2796 | * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) { | |
2797 | * prune_data.nr = 0; | |
2798 | * prune_data.alloc = 0; | |
2799 | * free(prune_data.path); | |
2800 | * prune_data.path = NULL; | |
2801 | * } else { | |
2802 | * terminate prune_data.alloc with NULL and | |
2803 | * call init_pathspec() to set revs->prune_data here. | |
2804 | * } | |
2805 | */ | |
0fdc2ae5 | 2806 | parse_pathspec(&revs->prune_data, 0, 0, |
d70a9eb6 | 2807 | revs->prefix, prune_data.v); |
4da5af31 | 2808 | } |
c972bf4c | 2809 | strvec_clear(&prune_data); |
5486ef0e | 2810 | |
02e54220 | 2811 | if (revs->def == NULL) |
32962c9b | 2812 | revs->def = opt ? opt->def : NULL; |
b4490059 JH |
2813 | if (opt && opt->tweak) |
2814 | opt->tweak(revs, opt); | |
02e54220 | 2815 | if (revs->show_merge) |
ae3e5e1e | 2816 | prepare_show_merge(revs); |
04a0e985 | 2817 | if (revs->def && !revs->pending.nr && !revs->rev_input_given) { |
654b9a90 | 2818 | struct object_id oid; |
cd2bdc53 | 2819 | struct object *object; |
249c8f4a | 2820 | struct object_context oc; |
3a7a698e | 2821 | if (get_oid_with_context(revs->repo, revs->def, 0, &oid, &oc)) |
ce113604 | 2822 | diagnose_missing_default(revs->def); |
654b9a90 | 2823 | object = get_reference(revs, revs->def, &oid, 0); |
249c8f4a | 2824 | add_pending_object_with_mode(revs, object, revs->def, oc.mode); |
ae563542 | 2825 | } |
8efdc326 | 2826 | |
b7bb760d LT |
2827 | /* Did the user ask for any diff output? Run the diff! */ |
2828 | if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) | |
2829 | revs->diff = 1; | |
2830 | ||
0faf2da7 | 2831 | /* Pickaxe, diff-filter and rename following need diffs */ |
cf63051a | 2832 | if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) || |
0faf2da7 | 2833 | revs->diffopt.filter || |
0d1e0e78 | 2834 | revs->diffopt.flags.follow_renames) |
b7bb760d LT |
2835 | revs->diff = 1; |
2836 | ||
15af58c1 SB |
2837 | if (revs->diffopt.objfind) |
2838 | revs->simplify_history = 0; | |
2839 | ||
002933f3 SG |
2840 | if (revs->line_level_traverse) { |
2841 | if (want_ancestry(revs)) | |
2842 | revs->limited = 1; | |
2843 | revs->topo_order = 1; | |
2844 | } | |
2845 | ||
f0d9cc41 | 2846 | if (revs->topo_order && !generation_numbers_enabled(the_repository)) |
53069686 JH |
2847 | revs->limited = 1; |
2848 | ||
afe069d1 | 2849 | if (revs->prune_data.nr) { |
bd1928df | 2850 | copy_pathspec(&revs->pruning.pathspec, &revs->prune_data); |
750f7b66 | 2851 | /* Can't prune commits with rename following: the paths change.. */ |
0d1e0e78 | 2852 | if (!revs->diffopt.flags.follow_renames) |
53b2c823 | 2853 | revs->prune = 1; |
cd2bdc53 | 2854 | if (!revs->full_diff) |
bd1928df NTND |
2855 | copy_pathspec(&revs->diffopt.pathspec, |
2856 | &revs->prune_data); | |
8efdc326 | 2857 | } |
299a6634 | 2858 | |
18f09473 | 2859 | diff_merges_setup_revs(revs); |
d76ce4f7 | 2860 | |
cd2bdc53 | 2861 | revs->diffopt.abbrev = revs->abbrev; |
12da1d1f | 2862 | |
28452655 | 2863 | diff_setup_done(&revs->diffopt); |
8efdc326 | 2864 | |
918d4e1c JH |
2865 | grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED, |
2866 | &revs->grep_filter); | |
44570188 ÆAB |
2867 | if (!is_encoding_utf8(get_log_output_encoding())) |
2868 | revs->grep_filter.ignore_locale = 1; | |
0843acfd | 2869 | compile_grep_patterns(&revs->grep_filter); |
bd95fcd3 | 2870 | |
d56651c0 SP |
2871 | if (revs->reverse && revs->reflog_info) |
2872 | die("cannot combine --reverse with --walk-reflogs"); | |
82fd0f4a JK |
2873 | if (revs->reflog_info && revs->limited) |
2874 | die("cannot combine --walk-reflogs with history-limiting options"); | |
8bb65883 | 2875 | if (revs->rewrite_parents && revs->children.name) |
f35f5603 | 2876 | die("cannot combine --parents and --children"); |
d56651c0 | 2877 | |
7fefda5c AS |
2878 | /* |
2879 | * Limitations on the graph functionality | |
2880 | */ | |
2881 | if (revs->reverse && revs->graph) | |
2882 | die("cannot combine --reverse with --graph"); | |
2883 | ||
2884 | if (revs->reflog_info && revs->graph) | |
2885 | die("cannot combine --walk-reflogs with --graph"); | |
695985f4 DJ |
2886 | if (revs->no_walk && revs->graph) |
2887 | die("cannot combine --no-walk with --graph"); | |
baa6378f JH |
2888 | if (!revs->reflog_info && revs->grep_filter.use_reflog_filter) |
2889 | die("cannot use --grep-reflog without --walk-reflogs"); | |
7fefda5c | 2890 | |
05314efa JK |
2891 | if (revs->line_level_traverse && |
2892 | (revs->diffopt.output_format & ~(DIFF_FORMAT_PATCH | DIFF_FORMAT_NO_OUTPUT))) | |
2893 | die(_("-L does not yet support diff formats besides -p and -s")); | |
2894 | ||
0893eec8 JH |
2895 | if (revs->expand_tabs_in_log < 0) |
2896 | revs->expand_tabs_in_log = revs->expand_tabs_in_log_default; | |
2897 | ||
ae563542 LT |
2898 | return left; |
2899 | } | |
a4a88b2b | 2900 | |
f35f5603 JH |
2901 | static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child) |
2902 | { | |
2903 | struct commit_list *l = xcalloc(1, sizeof(*l)); | |
2904 | ||
2905 | l->item = child; | |
2906 | l->next = add_decoration(&revs->children, &parent->object, l); | |
2907 | } | |
2908 | ||
d0af663e | 2909 | static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit) |
6546b593 | 2910 | { |
d0af663e | 2911 | struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object); |
6546b593 JH |
2912 | struct commit_list **pp, *p; |
2913 | int surviving_parents; | |
2914 | ||
2915 | /* Examine existing parents while marking ones we have seen... */ | |
2916 | pp = &commit->parents; | |
d0af663e | 2917 | surviving_parents = 0; |
6546b593 JH |
2918 | while ((p = *pp) != NULL) { |
2919 | struct commit *parent = p->item; | |
2920 | if (parent->object.flags & TMP_MARK) { | |
2921 | *pp = p->next; | |
d0af663e KB |
2922 | if (ts) |
2923 | compact_treesame(revs, commit, surviving_parents); | |
6546b593 JH |
2924 | continue; |
2925 | } | |
2926 | parent->object.flags |= TMP_MARK; | |
d0af663e | 2927 | surviving_parents++; |
6546b593 JH |
2928 | pp = &p->next; |
2929 | } | |
d0af663e | 2930 | /* clear the temporary mark */ |
6546b593 JH |
2931 | for (p = commit->parents; p; p = p->next) { |
2932 | p->item->object.flags &= ~TMP_MARK; | |
6546b593 | 2933 | } |
d0af663e | 2934 | /* no update_treesame() - removing duplicates can't affect TREESAME */ |
6546b593 JH |
2935 | return surviving_parents; |
2936 | } | |
2937 | ||
faf0156b JH |
2938 | struct merge_simplify_state { |
2939 | struct commit *simplified; | |
2940 | }; | |
2941 | ||
2942 | static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit) | |
2943 | { | |
2944 | struct merge_simplify_state *st; | |
2945 | ||
2946 | st = lookup_decoration(&revs->merge_simplification, &commit->object); | |
2947 | if (!st) { | |
ca56dadb | 2948 | CALLOC_ARRAY(st, 1); |
faf0156b JH |
2949 | add_decoration(&revs->merge_simplification, &commit->object, st); |
2950 | } | |
2951 | return st; | |
2952 | } | |
2953 | ||
91633995 | 2954 | static int mark_redundant_parents(struct commit *commit) |
d0af663e KB |
2955 | { |
2956 | struct commit_list *h = reduce_heads(commit->parents); | |
2957 | int i = 0, marked = 0; | |
2958 | struct commit_list *po, *pn; | |
2959 | ||
2960 | /* Want these for sanity-checking only */ | |
2961 | int orig_cnt = commit_list_count(commit->parents); | |
2962 | int cnt = commit_list_count(h); | |
2963 | ||
2964 | /* | |
2965 | * Not ready to remove items yet, just mark them for now, based | |
2966 | * on the output of reduce_heads(). reduce_heads outputs the reduced | |
2967 | * set in its original order, so this isn't too hard. | |
2968 | */ | |
2969 | po = commit->parents; | |
2970 | pn = h; | |
2971 | while (po) { | |
2972 | if (pn && po->item == pn->item) { | |
2973 | pn = pn->next; | |
2974 | i++; | |
2975 | } else { | |
2976 | po->item->object.flags |= TMP_MARK; | |
2977 | marked++; | |
2978 | } | |
2979 | po=po->next; | |
2980 | } | |
2981 | ||
2982 | if (i != cnt || cnt+marked != orig_cnt) | |
2983 | die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked); | |
2984 | ||
2985 | free_commit_list(h); | |
2986 | ||
2987 | return marked; | |
2988 | } | |
2989 | ||
91633995 | 2990 | static int mark_treesame_root_parents(struct commit *commit) |
143f1eaf KB |
2991 | { |
2992 | struct commit_list *p; | |
2993 | int marked = 0; | |
2994 | ||
2995 | for (p = commit->parents; p; p = p->next) { | |
2996 | struct commit *parent = p->item; | |
2997 | if (!parent->parents && (parent->object.flags & TREESAME)) { | |
2998 | parent->object.flags |= TMP_MARK; | |
2999 | marked++; | |
3000 | } | |
3001 | } | |
3002 | ||
3003 | return marked; | |
3004 | } | |
3005 | ||
9c129eab KB |
3006 | /* |
3007 | * Awkward naming - this means one parent we are TREESAME to. | |
3008 | * cf mark_treesame_root_parents: root parents that are TREESAME (to an | |
3009 | * empty tree). Better name suggestions? | |
3010 | */ | |
3011 | static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit) | |
3012 | { | |
3013 | struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object); | |
3014 | struct commit *unmarked = NULL, *marked = NULL; | |
3015 | struct commit_list *p; | |
3016 | unsigned n; | |
3017 | ||
3018 | for (p = commit->parents, n = 0; p; p = p->next, n++) { | |
3019 | if (ts->treesame[n]) { | |
3020 | if (p->item->object.flags & TMP_MARK) { | |
3021 | if (!marked) | |
3022 | marked = p->item; | |
3023 | } else { | |
3024 | if (!unmarked) { | |
3025 | unmarked = p->item; | |
3026 | break; | |
3027 | } | |
3028 | } | |
3029 | } | |
3030 | } | |
3031 | ||
3032 | /* | |
3033 | * If we are TREESAME to a marked-for-deletion parent, but not to any | |
3034 | * unmarked parents, unmark the first TREESAME parent. This is the | |
3035 | * parent that the default simplify_history==1 scan would have followed, | |
3036 | * and it doesn't make sense to omit that path when asking for a | |
3037 | * simplified full history. Retaining it improves the chances of | |
3038 | * understanding odd missed merges that took an old version of a file. | |
3039 | * | |
3040 | * Example: | |
3041 | * | |
3042 | * I--------*X A modified the file, but mainline merge X used | |
3043 | * \ / "-s ours", so took the version from I. X is | |
3044 | * `-*A--' TREESAME to I and !TREESAME to A. | |
3045 | * | |
3046 | * Default log from X would produce "I". Without this check, | |
3047 | * --full-history --simplify-merges would produce "I-A-X", showing | |
3048 | * the merge commit X and that it changed A, but not making clear that | |
3049 | * it had just taken the I version. With this check, the topology above | |
3050 | * is retained. | |
3051 | * | |
3052 | * Note that it is possible that the simplification chooses a different | |
3053 | * TREESAME parent from the default, in which case this test doesn't | |
3054 | * activate, and we _do_ drop the default parent. Example: | |
3055 | * | |
3056 | * I------X A modified the file, but it was reverted in B, | |
3057 | * \ / meaning mainline merge X is TREESAME to both | |
3058 | * *A-*B parents. | |
3059 | * | |
3060 | * Default log would produce "I" by following the first parent; | |
3061 | * --full-history --simplify-merges will produce "I-A-B". But this is a | |
3062 | * reasonable result - it presents a logical full history leading from | |
3063 | * I to X, and X is not an important merge. | |
3064 | */ | |
3065 | if (!unmarked && marked) { | |
3066 | marked->object.flags &= ~TMP_MARK; | |
3067 | return 1; | |
3068 | } | |
3069 | ||
3070 | return 0; | |
3071 | } | |
3072 | ||
d0af663e KB |
3073 | static int remove_marked_parents(struct rev_info *revs, struct commit *commit) |
3074 | { | |
3075 | struct commit_list **pp, *p; | |
3076 | int nth_parent, removed = 0; | |
3077 | ||
3078 | pp = &commit->parents; | |
3079 | nth_parent = 0; | |
3080 | while ((p = *pp) != NULL) { | |
3081 | struct commit *parent = p->item; | |
3082 | if (parent->object.flags & TMP_MARK) { | |
3083 | parent->object.flags &= ~TMP_MARK; | |
3084 | *pp = p->next; | |
3085 | free(p); | |
3086 | removed++; | |
3087 | compact_treesame(revs, commit, nth_parent); | |
3088 | continue; | |
3089 | } | |
3090 | pp = &p->next; | |
3091 | nth_parent++; | |
3092 | } | |
3093 | ||
3094 | /* Removing parents can only increase TREESAMEness */ | |
3095 | if (removed && !(commit->object.flags & TREESAME)) | |
3096 | update_treesame(revs, commit); | |
3097 | ||
3098 | return nth_parent; | |
3099 | } | |
3100 | ||
faf0156b | 3101 | static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail) |
6546b593 JH |
3102 | { |
3103 | struct commit_list *p; | |
4d826608 | 3104 | struct commit *parent; |
faf0156b | 3105 | struct merge_simplify_state *st, *pst; |
6546b593 JH |
3106 | int cnt; |
3107 | ||
faf0156b JH |
3108 | st = locate_simplify_state(revs, commit); |
3109 | ||
6546b593 | 3110 | /* |
6546b593 JH |
3111 | * Have we handled this one? |
3112 | */ | |
faf0156b | 3113 | if (st->simplified) |
6546b593 JH |
3114 | return tail; |
3115 | ||
3116 | /* | |
3117 | * An UNINTERESTING commit simplifies to itself, so does a | |
3118 | * root commit. We do not rewrite parents of such commit | |
3119 | * anyway. | |
3120 | */ | |
3121 | if ((commit->object.flags & UNINTERESTING) || !commit->parents) { | |
faf0156b | 3122 | st->simplified = commit; |
6546b593 JH |
3123 | return tail; |
3124 | } | |
3125 | ||
3126 | /* | |
6e513ba3 JH |
3127 | * Do we know what commit all of our parents that matter |
3128 | * should be rewritten to? Otherwise we are not ready to | |
3129 | * rewrite this one yet. | |
6546b593 JH |
3130 | */ |
3131 | for (cnt = 0, p = commit->parents; p; p = p->next) { | |
faf0156b JH |
3132 | pst = locate_simplify_state(revs, p->item); |
3133 | if (!pst->simplified) { | |
6546b593 JH |
3134 | tail = &commit_list_insert(p->item, tail)->next; |
3135 | cnt++; | |
3136 | } | |
6e513ba3 JH |
3137 | if (revs->first_parent_only) |
3138 | break; | |
6546b593 | 3139 | } |
53030f8d JH |
3140 | if (cnt) { |
3141 | tail = &commit_list_insert(commit, tail)->next; | |
6546b593 | 3142 | return tail; |
53030f8d | 3143 | } |
6546b593 JH |
3144 | |
3145 | /* | |
d0af663e KB |
3146 | * Rewrite our list of parents. Note that this cannot |
3147 | * affect our TREESAME flags in any way - a commit is | |
3148 | * always TREESAME to its simplification. | |
6546b593 | 3149 | */ |
faf0156b JH |
3150 | for (p = commit->parents; p; p = p->next) { |
3151 | pst = locate_simplify_state(revs, p->item); | |
3152 | p->item = pst->simplified; | |
6e513ba3 JH |
3153 | if (revs->first_parent_only) |
3154 | break; | |
faf0156b | 3155 | } |
4b7f53da | 3156 | |
0290bf12 | 3157 | if (revs->first_parent_only) |
6e513ba3 | 3158 | cnt = 1; |
0290bf12 | 3159 | else |
d0af663e | 3160 | cnt = remove_duplicate_parents(revs, commit); |
6546b593 JH |
3161 | |
3162 | /* | |
3163 | * It is possible that we are a merge and one side branch | |
3164 | * does not have any commit that touches the given paths; | |
d0af663e KB |
3165 | * in such a case, the immediate parent from that branch |
3166 | * will be rewritten to be the merge base. | |
6546b593 JH |
3167 | * |
3168 | * o----X X: the commit we are looking at; | |
3169 | * / / o: a commit that touches the paths; | |
3170 | * ---o----' | |
3171 | * | |
143f1eaf KB |
3172 | * Further, a merge of an independent branch that doesn't |
3173 | * touch the path will reduce to a treesame root parent: | |
3174 | * | |
3175 | * ----o----X X: the commit we are looking at; | |
3176 | * / o: a commit that touches the paths; | |
3177 | * r r: a root commit not touching the paths | |
3178 | * | |
3179 | * Detect and simplify both cases. | |
6546b593 JH |
3180 | */ |
3181 | if (1 < cnt) { | |
91633995 JK |
3182 | int marked = mark_redundant_parents(commit); |
3183 | marked += mark_treesame_root_parents(commit); | |
9c129eab KB |
3184 | if (marked) |
3185 | marked -= leave_one_treesame_to_parent(revs, commit); | |
d0af663e KB |
3186 | if (marked) |
3187 | cnt = remove_marked_parents(revs, commit); | |
6546b593 JH |
3188 | } |
3189 | ||
3190 | /* | |
3191 | * A commit simplifies to itself if it is a root, if it is | |
3192 | * UNINTERESTING, if it touches the given paths, or if it is a | |
4d826608 | 3193 | * merge and its parents don't simplify to one relevant commit |
6546b593 JH |
3194 | * (the first two cases are already handled at the beginning of |
3195 | * this function). | |
3196 | * | |
4d826608 KB |
3197 | * Otherwise, it simplifies to what its sole relevant parent |
3198 | * simplifies to. | |
6546b593 JH |
3199 | */ |
3200 | if (!cnt || | |
3201 | (commit->object.flags & UNINTERESTING) || | |
3202 | !(commit->object.flags & TREESAME) || | |
8d049e18 DS |
3203 | (parent = one_relevant_parent(revs, commit->parents)) == NULL || |
3204 | (revs->show_pulls && (commit->object.flags & PULL_MERGE))) | |
faf0156b JH |
3205 | st->simplified = commit; |
3206 | else { | |
4d826608 | 3207 | pst = locate_simplify_state(revs, parent); |
faf0156b JH |
3208 | st->simplified = pst->simplified; |
3209 | } | |
6546b593 JH |
3210 | return tail; |
3211 | } | |
3212 | ||
3213 | static void simplify_merges(struct rev_info *revs) | |
3214 | { | |
ab9d75a8 | 3215 | struct commit_list *list, *next; |
6546b593 | 3216 | struct commit_list *yet_to_do, **tail; |
ab9d75a8 | 3217 | struct commit *commit; |
6546b593 | 3218 | |
5eac739e JH |
3219 | if (!revs->prune) |
3220 | return; | |
65347030 | 3221 | |
6546b593 JH |
3222 | /* feed the list reversed */ |
3223 | yet_to_do = NULL; | |
ab9d75a8 JH |
3224 | for (list = revs->commits; list; list = next) { |
3225 | commit = list->item; | |
3226 | next = list->next; | |
3227 | /* | |
3228 | * Do not free(list) here yet; the original list | |
3229 | * is used later in this function. | |
3230 | */ | |
3231 | commit_list_insert(commit, &yet_to_do); | |
3232 | } | |
6546b593 JH |
3233 | while (yet_to_do) { |
3234 | list = yet_to_do; | |
3235 | yet_to_do = NULL; | |
3236 | tail = &yet_to_do; | |
3237 | while (list) { | |
e510ab89 | 3238 | commit = pop_commit(&list); |
faf0156b | 3239 | tail = simplify_one(revs, commit, tail); |
6546b593 JH |
3240 | } |
3241 | } | |
3242 | ||
3243 | /* clean up the result, removing the simplified ones */ | |
3244 | list = revs->commits; | |
3245 | revs->commits = NULL; | |
3246 | tail = &revs->commits; | |
3247 | while (list) { | |
faf0156b | 3248 | struct merge_simplify_state *st; |
ab9d75a8 | 3249 | |
e510ab89 | 3250 | commit = pop_commit(&list); |
faf0156b JH |
3251 | st = locate_simplify_state(revs, commit); |
3252 | if (st->simplified == commit) | |
6546b593 JH |
3253 | tail = &commit_list_insert(commit, tail)->next; |
3254 | } | |
6546b593 JH |
3255 | } |
3256 | ||
f35f5603 JH |
3257 | static void set_children(struct rev_info *revs) |
3258 | { | |
3259 | struct commit_list *l; | |
3260 | for (l = revs->commits; l; l = l->next) { | |
3261 | struct commit *commit = l->item; | |
3262 | struct commit_list *p; | |
3263 | ||
3264 | for (p = commit->parents; p; p = p->next) | |
3265 | add_child(revs, p->item, commit); | |
3266 | } | |
3267 | } | |
3268 | ||
bcc0a3ea HV |
3269 | void reset_revision_walk(void) |
3270 | { | |
ffa1f28f | 3271 | clear_object_flags(SEEN | ADDED | SHOWN | TOPO_WALK_EXPLORED | TOPO_WALK_INDEGREE); |
bcc0a3ea HV |
3272 | } |
3273 | ||
df11e196 JT |
3274 | static int mark_uninteresting(const struct object_id *oid, |
3275 | struct packed_git *pack, | |
3276 | uint32_t pos, | |
b3c7eef9 | 3277 | void *cb) |
df11e196 | 3278 | { |
b3c7eef9 | 3279 | struct rev_info *revs = cb; |
c1fa951d | 3280 | struct object *o = lookup_unknown_object(revs->repo, oid); |
df11e196 JT |
3281 | o->flags |= UNINTERESTING | SEEN; |
3282 | return 0; | |
3283 | } | |
3284 | ||
b4542418 DS |
3285 | define_commit_slab(indegree_slab, int); |
3286 | define_commit_slab(author_date_slab, timestamp_t); | |
3287 | ||
3288 | struct topo_walk_info { | |
d7f92784 | 3289 | timestamp_t min_generation; |
b4542418 DS |
3290 | struct prio_queue explore_queue; |
3291 | struct prio_queue indegree_queue; | |
3292 | struct prio_queue topo_queue; | |
3293 | struct indegree_slab indegree; | |
3294 | struct author_date_slab author_date; | |
3295 | }; | |
3296 | ||
90b666da DS |
3297 | static int topo_walk_atexit_registered; |
3298 | static unsigned int count_explore_walked; | |
3299 | static unsigned int count_indegree_walked; | |
3300 | static unsigned int count_topo_walked; | |
3301 | ||
3302 | static void trace2_topo_walk_statistics_atexit(void) | |
3303 | { | |
3304 | struct json_writer jw = JSON_WRITER_INIT; | |
3305 | ||
3306 | jw_object_begin(&jw, 0); | |
3307 | jw_object_intmax(&jw, "count_explore_walked", count_explore_walked); | |
3308 | jw_object_intmax(&jw, "count_indegree_walked", count_indegree_walked); | |
3309 | jw_object_intmax(&jw, "count_topo_walked", count_topo_walked); | |
3310 | jw_end(&jw); | |
3311 | ||
3312 | trace2_data_json("topo_walk", the_repository, "statistics", &jw); | |
3313 | ||
3314 | jw_release(&jw); | |
3315 | } | |
3316 | ||
b4542418 DS |
3317 | static inline void test_flag_and_insert(struct prio_queue *q, struct commit *c, int flag) |
3318 | { | |
3319 | if (c->object.flags & flag) | |
3320 | return; | |
3321 | ||
3322 | c->object.flags |= flag; | |
3323 | prio_queue_put(q, c); | |
3324 | } | |
3325 | ||
3326 | static void explore_walk_step(struct rev_info *revs) | |
3327 | { | |
3328 | struct topo_walk_info *info = revs->topo_walk_info; | |
3329 | struct commit_list *p; | |
3330 | struct commit *c = prio_queue_get(&info->explore_queue); | |
3331 | ||
3332 | if (!c) | |
3333 | return; | |
3334 | ||
ea3f7e59 | 3335 | if (repo_parse_commit_gently(revs->repo, c, 1) < 0) |
b4542418 DS |
3336 | return; |
3337 | ||
90b666da DS |
3338 | count_explore_walked++; |
3339 | ||
b4542418 DS |
3340 | if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE) |
3341 | record_author_date(&info->author_date, c); | |
3342 | ||
3343 | if (revs->max_age != -1 && (c->date < revs->max_age)) | |
3344 | c->object.flags |= UNINTERESTING; | |
3345 | ||
3346 | if (process_parents(revs, c, NULL, NULL) < 0) | |
3347 | return; | |
3348 | ||
3349 | if (c->object.flags & UNINTERESTING) | |
3350 | mark_parents_uninteresting(c); | |
3351 | ||
3352 | for (p = c->parents; p; p = p->next) | |
3353 | test_flag_and_insert(&info->explore_queue, p->item, TOPO_WALK_EXPLORED); | |
3354 | } | |
3355 | ||
3356 | static void explore_to_depth(struct rev_info *revs, | |
d7f92784 | 3357 | timestamp_t gen_cutoff) |
b4542418 DS |
3358 | { |
3359 | struct topo_walk_info *info = revs->topo_walk_info; | |
3360 | struct commit *c; | |
3361 | while ((c = prio_queue_peek(&info->explore_queue)) && | |
c49c82aa | 3362 | commit_graph_generation(c) >= gen_cutoff) |
b4542418 DS |
3363 | explore_walk_step(revs); |
3364 | } | |
3365 | ||
3366 | static void indegree_walk_step(struct rev_info *revs) | |
3367 | { | |
3368 | struct commit_list *p; | |
3369 | struct topo_walk_info *info = revs->topo_walk_info; | |
3370 | struct commit *c = prio_queue_get(&info->indegree_queue); | |
3371 | ||
3372 | if (!c) | |
3373 | return; | |
3374 | ||
ea3f7e59 | 3375 | if (repo_parse_commit_gently(revs->repo, c, 1) < 0) |
b4542418 DS |
3376 | return; |
3377 | ||
90b666da DS |
3378 | count_indegree_walked++; |
3379 | ||
c49c82aa | 3380 | explore_to_depth(revs, commit_graph_generation(c)); |
b4542418 DS |
3381 | |
3382 | for (p = c->parents; p; p = p->next) { | |
3383 | struct commit *parent = p->item; | |
3384 | int *pi = indegree_slab_at(&info->indegree, parent); | |
3385 | ||
2f9bbb6d AK |
3386 | if (repo_parse_commit_gently(revs->repo, parent, 1) < 0) |
3387 | return; | |
3388 | ||
b4542418 DS |
3389 | if (*pi) |
3390 | (*pi)++; | |
3391 | else | |
3392 | *pi = 2; | |
3393 | ||
3394 | test_flag_and_insert(&info->indegree_queue, parent, TOPO_WALK_INDEGREE); | |
3395 | ||
3396 | if (revs->first_parent_only) | |
3397 | return; | |
3398 | } | |
3399 | } | |
3400 | ||
3401 | static void compute_indegrees_to_depth(struct rev_info *revs, | |
d7f92784 | 3402 | timestamp_t gen_cutoff) |
b4542418 DS |
3403 | { |
3404 | struct topo_walk_info *info = revs->topo_walk_info; | |
3405 | struct commit *c; | |
3406 | while ((c = prio_queue_peek(&info->indegree_queue)) && | |
c49c82aa | 3407 | commit_graph_generation(c) >= gen_cutoff) |
b4542418 DS |
3408 | indegree_walk_step(revs); |
3409 | } | |
f0d9cc41 | 3410 | |
0aa0c2b2 MH |
3411 | static void reset_topo_walk(struct rev_info *revs) |
3412 | { | |
3413 | struct topo_walk_info *info = revs->topo_walk_info; | |
3414 | ||
3415 | clear_prio_queue(&info->explore_queue); | |
3416 | clear_prio_queue(&info->indegree_queue); | |
3417 | clear_prio_queue(&info->topo_queue); | |
3418 | clear_indegree_slab(&info->indegree); | |
3419 | clear_author_date_slab(&info->author_date); | |
3420 | ||
3421 | FREE_AND_NULL(revs->topo_walk_info); | |
3422 | } | |
3423 | ||
f0d9cc41 DS |
3424 | static void init_topo_walk(struct rev_info *revs) |
3425 | { | |
3426 | struct topo_walk_info *info; | |
b4542418 | 3427 | struct commit_list *list; |
0aa0c2b2 MH |
3428 | if (revs->topo_walk_info) |
3429 | reset_topo_walk(revs); | |
3430 | ||
f0d9cc41 DS |
3431 | revs->topo_walk_info = xmalloc(sizeof(struct topo_walk_info)); |
3432 | info = revs->topo_walk_info; | |
3433 | memset(info, 0, sizeof(struct topo_walk_info)); | |
3434 | ||
b4542418 DS |
3435 | init_indegree_slab(&info->indegree); |
3436 | memset(&info->explore_queue, 0, sizeof(info->explore_queue)); | |
3437 | memset(&info->indegree_queue, 0, sizeof(info->indegree_queue)); | |
3438 | memset(&info->topo_queue, 0, sizeof(info->topo_queue)); | |
3439 | ||
3440 | switch (revs->sort_order) { | |
3441 | default: /* REV_SORT_IN_GRAPH_ORDER */ | |
3442 | info->topo_queue.compare = NULL; | |
3443 | break; | |
3444 | case REV_SORT_BY_COMMIT_DATE: | |
3445 | info->topo_queue.compare = compare_commits_by_commit_date; | |
3446 | break; | |
3447 | case REV_SORT_BY_AUTHOR_DATE: | |
3448 | init_author_date_slab(&info->author_date); | |
3449 | info->topo_queue.compare = compare_commits_by_author_date; | |
3450 | info->topo_queue.cb_data = &info->author_date; | |
3451 | break; | |
3452 | } | |
3453 | ||
3454 | info->explore_queue.compare = compare_commits_by_gen_then_commit_date; | |
3455 | info->indegree_queue.compare = compare_commits_by_gen_then_commit_date; | |
3456 | ||
3457 | info->min_generation = GENERATION_NUMBER_INFINITY; | |
3458 | for (list = revs->commits; list; list = list->next) { | |
3459 | struct commit *c = list->item; | |
d7f92784 | 3460 | timestamp_t generation; |
b4542418 | 3461 | |
ea3f7e59 | 3462 | if (repo_parse_commit_gently(revs->repo, c, 1)) |
b4542418 DS |
3463 | continue; |
3464 | ||
3465 | test_flag_and_insert(&info->explore_queue, c, TOPO_WALK_EXPLORED); | |
3466 | test_flag_and_insert(&info->indegree_queue, c, TOPO_WALK_INDEGREE); | |
3467 | ||
c752ad09 AK |
3468 | generation = commit_graph_generation(c); |
3469 | if (generation < info->min_generation) | |
3470 | info->min_generation = generation; | |
b4542418 DS |
3471 | |
3472 | *(indegree_slab_at(&info->indegree, c)) = 1; | |
3473 | ||
3474 | if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE) | |
3475 | record_author_date(&info->author_date, c); | |
3476 | } | |
3477 | compute_indegrees_to_depth(revs, info->min_generation); | |
3478 | ||
3479 | for (list = revs->commits; list; list = list->next) { | |
3480 | struct commit *c = list->item; | |
3481 | ||
3482 | if (*(indegree_slab_at(&info->indegree, c)) == 1) | |
3483 | prio_queue_put(&info->topo_queue, c); | |
3484 | } | |
3485 | ||
3486 | /* | |
3487 | * This is unfortunate; the initial tips need to be shown | |
3488 | * in the order given from the revision traversal machinery. | |
3489 | */ | |
3490 | if (revs->sort_order == REV_SORT_IN_GRAPH_ORDER) | |
3491 | prio_queue_reverse(&info->topo_queue); | |
90b666da DS |
3492 | |
3493 | if (trace2_is_enabled() && !topo_walk_atexit_registered) { | |
3494 | atexit(trace2_topo_walk_statistics_atexit); | |
3495 | topo_walk_atexit_registered = 1; | |
3496 | } | |
f0d9cc41 DS |
3497 | } |
3498 | ||
3499 | static struct commit *next_topo_commit(struct rev_info *revs) | |
3500 | { | |
b4542418 DS |
3501 | struct commit *c; |
3502 | struct topo_walk_info *info = revs->topo_walk_info; | |
3503 | ||
3504 | /* pop next off of topo_queue */ | |
3505 | c = prio_queue_get(&info->topo_queue); | |
3506 | ||
3507 | if (c) | |
3508 | *(indegree_slab_at(&info->indegree, c)) = 0; | |
3509 | ||
3510 | return c; | |
f0d9cc41 DS |
3511 | } |
3512 | ||
3513 | static void expand_topo_walk(struct rev_info *revs, struct commit *commit) | |
3514 | { | |
b4542418 DS |
3515 | struct commit_list *p; |
3516 | struct topo_walk_info *info = revs->topo_walk_info; | |
3517 | if (process_parents(revs, commit, NULL, NULL) < 0) { | |
f0d9cc41 DS |
3518 | if (!revs->ignore_missing_links) |
3519 | die("Failed to traverse parents of commit %s", | |
3520 | oid_to_hex(&commit->object.oid)); | |
3521 | } | |
b4542418 | 3522 | |
90b666da DS |
3523 | count_topo_walked++; |
3524 | ||
b4542418 DS |
3525 | for (p = commit->parents; p; p = p->next) { |
3526 | struct commit *parent = p->item; | |
3527 | int *pi; | |
d7f92784 | 3528 | timestamp_t generation; |
b4542418 | 3529 | |
1d8e31a3 DS |
3530 | if (parent->object.flags & UNINTERESTING) |
3531 | continue; | |
3532 | ||
ea3f7e59 | 3533 | if (repo_parse_commit_gently(revs->repo, parent, 1) < 0) |
b4542418 DS |
3534 | continue; |
3535 | ||
c752ad09 AK |
3536 | generation = commit_graph_generation(parent); |
3537 | if (generation < info->min_generation) { | |
3538 | info->min_generation = generation; | |
b4542418 DS |
3539 | compute_indegrees_to_depth(revs, info->min_generation); |
3540 | } | |
3541 | ||
3542 | pi = indegree_slab_at(&info->indegree, parent); | |
3543 | ||
3544 | (*pi)--; | |
3545 | if (*pi == 1) | |
3546 | prio_queue_put(&info->topo_queue, parent); | |
3547 | ||
3548 | if (revs->first_parent_only) | |
3549 | return; | |
3550 | } | |
f0d9cc41 DS |
3551 | } |
3552 | ||
cc0e6c5a | 3553 | int prepare_revision_walk(struct rev_info *revs) |
a4a88b2b | 3554 | { |
1da1e07c JK |
3555 | int i; |
3556 | struct object_array old_pending; | |
2e7da8e9 | 3557 | struct commit_list **next = &revs->commits; |
cd2bdc53 | 3558 | |
1da1e07c | 3559 | memcpy(&old_pending, &revs->pending, sizeof(old_pending)); |
1f1e895f LT |
3560 | revs->pending.nr = 0; |
3561 | revs->pending.alloc = 0; | |
3562 | revs->pending.objects = NULL; | |
1da1e07c JK |
3563 | for (i = 0; i < old_pending.nr; i++) { |
3564 | struct object_array_entry *e = old_pending.objects + i; | |
20739490 | 3565 | struct commit *commit = handle_commit(revs, e); |
cd2bdc53 LT |
3566 | if (commit) { |
3567 | if (!(commit->object.flags & SEEN)) { | |
3568 | commit->object.flags |= SEEN; | |
2e7da8e9 | 3569 | next = commit_list_append(commit, next); |
cd2bdc53 LT |
3570 | } |
3571 | } | |
cd2bdc53 | 3572 | } |
f1230fb5 | 3573 | object_array_clear(&old_pending); |
cd2bdc53 | 3574 | |
d0af663e | 3575 | /* Signal whether we need per-parent treesame decoration */ |
4d826608 KB |
3576 | if (revs->simplify_merges || |
3577 | (revs->limited && limiting_can_increase_treesame(revs))) | |
d0af663e KB |
3578 | revs->treesame.name = "treesame"; |
3579 | ||
df11e196 | 3580 | if (revs->exclude_promisor_objects) { |
b3c7eef9 | 3581 | for_each_packed_object(mark_uninteresting, revs, |
df11e196 JT |
3582 | FOR_EACH_OBJECT_PROMISOR_ONLY); |
3583 | } | |
3584 | ||
f32dde8c | 3585 | if (!revs->reflog_info) |
a56b9464 | 3586 | prepare_to_use_bloom_filter(revs); |
ca92e59e MZ |
3587 | if (revs->no_walk != REVISION_WALK_NO_WALK_UNSORTED) |
3588 | commit_list_sort_by_date(&revs->commits); | |
ba1d4505 | 3589 | if (revs->no_walk) |
cc0e6c5a | 3590 | return 0; |
f0d9cc41 | 3591 | if (revs->limited) { |
cc0e6c5a AR |
3592 | if (limit_list(revs) < 0) |
3593 | return -1; | |
f0d9cc41 DS |
3594 | if (revs->topo_order) |
3595 | sort_in_topological_order(&revs->commits, revs->sort_order); | |
3596 | } else if (revs->topo_order) | |
3597 | init_topo_walk(revs); | |
3cb9d2b6 SG |
3598 | if (revs->line_level_traverse && want_ancestry(revs)) |
3599 | /* | |
3600 | * At the moment we can only do line-level log with parent | |
3601 | * rewriting by performing this expensive pre-filtering step. | |
3602 | * If parent rewriting is not requested, then we rather | |
3603 | * perform the line-level log filtering during the regular | |
3604 | * history traversal. | |
3605 | */ | |
12da1d1f | 3606 | line_log_filter(revs); |
6546b593 JH |
3607 | if (revs->simplify_merges) |
3608 | simplify_merges(revs); | |
f35f5603 JH |
3609 | if (revs->children.name) |
3610 | set_children(revs); | |
a56b9464 | 3611 | |
cc0e6c5a | 3612 | return 0; |
a4a88b2b LT |
3613 | } |
3614 | ||
8320b1db JK |
3615 | static enum rewrite_result rewrite_one_1(struct rev_info *revs, |
3616 | struct commit **pp, | |
3617 | struct prio_queue *queue) | |
765ac8ec LT |
3618 | { |
3619 | for (;;) { | |
3620 | struct commit *p = *pp; | |
3381c790 | 3621 | if (!revs->limited) |
8320b1db | 3622 | if (process_parents(revs, p, NULL, queue) < 0) |
cc0e6c5a | 3623 | return rewrite_one_error; |
7dc0fe3b LT |
3624 | if (p->object.flags & UNINTERESTING) |
3625 | return rewrite_one_ok; | |
3626 | if (!(p->object.flags & TREESAME)) | |
cc0e6c5a | 3627 | return rewrite_one_ok; |
765ac8ec | 3628 | if (!p->parents) |
cc0e6c5a | 3629 | return rewrite_one_noparents; |
4d826608 KB |
3630 | if ((p = one_relevant_parent(revs, p->parents)) == NULL) |
3631 | return rewrite_one_ok; | |
3632 | *pp = p; | |
765ac8ec LT |
3633 | } |
3634 | } | |
3635 | ||
8320b1db JK |
3636 | static void merge_queue_into_list(struct prio_queue *q, struct commit_list **list) |
3637 | { | |
3638 | while (q->nr) { | |
3639 | struct commit *item = prio_queue_peek(q); | |
3640 | struct commit_list *p = *list; | |
3641 | ||
3642 | if (p && p->item->date >= item->date) | |
3643 | list = &p->next; | |
3644 | else { | |
3645 | p = commit_list_insert(item, list); | |
3646 | list = &p->next; /* skip newly added item */ | |
3647 | prio_queue_get(q); /* pop item */ | |
3648 | } | |
3649 | } | |
3650 | } | |
3651 | ||
3652 | static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp) | |
3653 | { | |
3654 | struct prio_queue queue = { compare_commits_by_commit_date }; | |
3655 | enum rewrite_result ret = rewrite_one_1(revs, pp, &queue); | |
3656 | merge_queue_into_list(&queue, &revs->commits); | |
3657 | clear_prio_queue(&queue); | |
3658 | return ret; | |
3659 | } | |
3660 | ||
c7edcae0 BY |
3661 | int rewrite_parents(struct rev_info *revs, struct commit *commit, |
3662 | rewrite_parent_fn_t rewrite_parent) | |
765ac8ec LT |
3663 | { |
3664 | struct commit_list **pp = &commit->parents; | |
3665 | while (*pp) { | |
3666 | struct commit_list *parent = *pp; | |
c7edcae0 | 3667 | switch (rewrite_parent(revs, &parent->item)) { |
cc0e6c5a AR |
3668 | case rewrite_one_ok: |
3669 | break; | |
3670 | case rewrite_one_noparents: | |
765ac8ec LT |
3671 | *pp = parent->next; |
3672 | continue; | |
cc0e6c5a AR |
3673 | case rewrite_one_error: |
3674 | return -1; | |
765ac8ec LT |
3675 | } |
3676 | pp = &parent->next; | |
3677 | } | |
d0af663e | 3678 | remove_duplicate_parents(revs, commit); |
cc0e6c5a | 3679 | return 0; |
765ac8ec LT |
3680 | } |
3681 | ||
d72fbe81 AP |
3682 | static int commit_rewrite_person(struct strbuf *buf, const char *what, struct string_list *mailmap) |
3683 | { | |
3684 | char *person, *endp; | |
3685 | size_t len, namelen, maillen; | |
3686 | const char *name; | |
3687 | const char *mail; | |
3688 | struct ident_split ident; | |
3689 | ||
3690 | person = strstr(buf->buf, what); | |
3691 | if (!person) | |
3692 | return 0; | |
3693 | ||
3694 | person += strlen(what); | |
3695 | endp = strchr(person, '\n'); | |
3696 | if (!endp) | |
3697 | return 0; | |
3698 | ||
3699 | len = endp - person; | |
3700 | ||
3701 | if (split_ident_line(&ident, person, len)) | |
3702 | return 0; | |
3703 | ||
3704 | mail = ident.mail_begin; | |
3705 | maillen = ident.mail_end - ident.mail_begin; | |
3706 | name = ident.name_begin; | |
3707 | namelen = ident.name_end - ident.name_begin; | |
3708 | ||
3709 | if (map_user(mailmap, &mail, &maillen, &name, &namelen)) { | |
3710 | struct strbuf namemail = STRBUF_INIT; | |
3711 | ||
3712 | strbuf_addf(&namemail, "%.*s <%.*s>", | |
3713 | (int)namelen, name, (int)maillen, mail); | |
3714 | ||
3715 | strbuf_splice(buf, ident.name_begin - buf->buf, | |
3716 | ident.mail_end - ident.name_begin + 1, | |
3717 | namemail.buf, namemail.len); | |
3718 | ||
3719 | strbuf_release(&namemail); | |
3720 | ||
3721 | return 1; | |
3722 | } | |
3723 | ||
3724 | return 0; | |
3725 | } | |
3726 | ||
8ecae9b0 JH |
3727 | static int commit_match(struct commit *commit, struct rev_info *opt) |
3728 | { | |
72fd13f7 | 3729 | int retval; |
04deccda | 3730 | const char *encoding; |
b000c59b | 3731 | const char *message; |
72fd13f7 | 3732 | struct strbuf buf = STRBUF_INIT; |
04deccda | 3733 | |
80235ba7 | 3734 | if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list) |
8ecae9b0 | 3735 | return 1; |
baa6378f JH |
3736 | |
3737 | /* Prepend "fake" headers as needed */ | |
3738 | if (opt->grep_filter.use_reflog_filter) { | |
72fd13f7 NTND |
3739 | strbuf_addstr(&buf, "reflog "); |
3740 | get_reflog_message(&buf, opt->reflog_info); | |
3741 | strbuf_addch(&buf, '\n'); | |
72fd13f7 | 3742 | } |
baa6378f | 3743 | |
04deccda JK |
3744 | /* |
3745 | * We grep in the user's output encoding, under the assumption that it | |
3746 | * is the encoding they are most likely to write their grep pattern | |
3747 | * for. In addition, it means we will match the "notes" encoding below, | |
3748 | * so we will not end up with a buffer that has two different encodings | |
3749 | * in it. | |
3750 | */ | |
3751 | encoding = get_log_output_encoding(); | |
5a10d236 | 3752 | message = logmsg_reencode(commit, NULL, encoding); |
04deccda | 3753 | |
baa6378f JH |
3754 | /* Copy the commit to temporary if we are using "fake" headers */ |
3755 | if (buf.len) | |
04deccda | 3756 | strbuf_addstr(&buf, message); |
baa6378f | 3757 | |
df874fa8 | 3758 | if (opt->grep_filter.header_list && opt->mailmap) { |
d72fbe81 | 3759 | if (!buf.len) |
04deccda | 3760 | strbuf_addstr(&buf, message); |
d72fbe81 AP |
3761 | |
3762 | commit_rewrite_person(&buf, "\nauthor ", opt->mailmap); | |
3763 | commit_rewrite_person(&buf, "\ncommitter ", opt->mailmap); | |
3764 | } | |
3765 | ||
38cfe915 NTND |
3766 | /* Append "fake" message parts as needed */ |
3767 | if (opt->show_notes) { | |
3768 | if (!buf.len) | |
04deccda | 3769 | strbuf_addstr(&buf, message); |
fb61e4d3 | 3770 | format_display_notes(&commit->object.oid, &buf, encoding, 1); |
38cfe915 NTND |
3771 | } |
3772 | ||
b000c59b JK |
3773 | /* |
3774 | * Find either in the original commit message, or in the temporary. | |
3775 | * Note that we cast away the constness of "message" here. It is | |
3776 | * const because it may come from the cached commit buffer. That's OK, | |
3777 | * because we know that it is modifiable heap memory, and that while | |
3778 | * grep_buffer may modify it for speed, it will restore any | |
3779 | * changes before returning. | |
3780 | */ | |
72fd13f7 NTND |
3781 | if (buf.len) |
3782 | retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len); | |
3783 | else | |
3784 | retval = grep_buffer(&opt->grep_filter, | |
b000c59b | 3785 | (char *)message, strlen(message)); |
72fd13f7 | 3786 | strbuf_release(&buf); |
b66103c3 | 3787 | unuse_commit_buffer(commit, message); |
22dfa8a2 | 3788 | return opt->invert_grep ? !retval : retval; |
8ecae9b0 JH |
3789 | } |
3790 | ||
53d00b39 | 3791 | static inline int want_ancestry(const struct rev_info *revs) |
f35f5603 | 3792 | { |
8bb65883 | 3793 | return (revs->rewrite_parents || revs->children.name); |
f35f5603 JH |
3794 | } |
3795 | ||
de239446 JK |
3796 | /* |
3797 | * Return a timestamp to be used for --since/--until comparisons for this | |
3798 | * commit, based on the revision options. | |
3799 | */ | |
3800 | static timestamp_t comparison_date(const struct rev_info *revs, | |
3801 | struct commit *commit) | |
3802 | { | |
3803 | return revs->reflog_info ? | |
3804 | get_reflog_timestamp(revs->reflog_info) : | |
3805 | commit->date; | |
3806 | } | |
3807 | ||
beb5af43 | 3808 | enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit) |
252a7c02 LT |
3809 | { |
3810 | if (commit->object.flags & SHOWN) | |
3811 | return commit_ignore; | |
14c3c80c | 3812 | if (revs->unpacked && has_object_pack(&commit->object.oid)) |
252a7c02 | 3813 | return commit_ignore; |
c9fff000 TB |
3814 | if (revs->no_kept_objects) { |
3815 | if (has_object_kept_pack(&commit->object.oid, | |
3816 | revs->keep_pack_cache_flags)) | |
3817 | return commit_ignore; | |
3818 | } | |
252a7c02 LT |
3819 | if (commit->object.flags & UNINTERESTING) |
3820 | return commit_ignore; | |
3cb9d2b6 SG |
3821 | if (revs->line_level_traverse && !want_ancestry(revs)) { |
3822 | /* | |
3823 | * In case of line-level log with parent rewriting | |
3824 | * prepare_revision_walk() already took care of all line-level | |
3825 | * log filtering, and there is nothing left to do here. | |
3826 | * | |
3827 | * If parent rewriting was not requested, then this is the | |
3828 | * place to perform the line-level log filtering. Notably, | |
3829 | * this check, though expensive, must come before the other, | |
3830 | * cheaper filtering conditions, because the tracked line | |
3831 | * ranges must be adjusted even when the commit will end up | |
3832 | * being ignored based on other conditions. | |
3833 | */ | |
3834 | if (!line_log_process_ranges_arbitrary_commit(revs, commit)) | |
3835 | return commit_ignore; | |
3836 | } | |
de239446 JK |
3837 | if (revs->min_age != -1 && |
3838 | comparison_date(revs, commit) > revs->min_age) | |
3839 | return commit_ignore; | |
ad5aeede | 3840 | if (revs->min_parents || (revs->max_parents >= 0)) { |
bf3418b0 | 3841 | int n = commit_list_count(commit->parents); |
ad5aeede MG |
3842 | if ((n < revs->min_parents) || |
3843 | ((revs->max_parents >= 0) && (n > revs->max_parents))) | |
3844 | return commit_ignore; | |
3845 | } | |
252a7c02 LT |
3846 | if (!commit_match(commit, revs)) |
3847 | return commit_ignore; | |
53b2c823 | 3848 | if (revs->prune && revs->dense) { |
252a7c02 | 3849 | /* Commit without changes? */ |
7dc0fe3b | 3850 | if (commit->object.flags & TREESAME) { |
bf3418b0 KB |
3851 | int n; |
3852 | struct commit_list *p; | |
252a7c02 | 3853 | /* drop merges unless we want parenthood */ |
f35f5603 | 3854 | if (!want_ancestry(revs)) |
252a7c02 | 3855 | return commit_ignore; |
8d049e18 DS |
3856 | |
3857 | if (revs->show_pulls && (commit->object.flags & PULL_MERGE)) | |
3858 | return commit_show; | |
3859 | ||
bf3418b0 KB |
3860 | /* |
3861 | * If we want ancestry, then need to keep any merges | |
3862 | * between relevant commits to tie together topology. | |
3863 | * For consistency with TREESAME and simplification | |
3864 | * use "relevant" here rather than just INTERESTING, | |
3865 | * to treat bottom commit(s) as part of the topology. | |
3866 | */ | |
3867 | for (n = 0, p = commit->parents; p; p = p->next) | |
3868 | if (relevant_commit(p->item)) | |
3869 | if (++n >= 2) | |
3870 | return commit_show; | |
3871 | return commit_ignore; | |
252a7c02 | 3872 | } |
252a7c02 LT |
3873 | } |
3874 | return commit_show; | |
3875 | } | |
3876 | ||
0131c490 JH |
3877 | define_commit_slab(saved_parents, struct commit_list *); |
3878 | ||
3879 | #define EMPTY_PARENT_LIST ((struct commit_list *)-1) | |
3880 | ||
3881 | /* | |
3882 | * You may only call save_parents() once per commit (this is checked | |
3883 | * for non-root commits). | |
3884 | */ | |
3885 | static void save_parents(struct rev_info *revs, struct commit *commit) | |
3886 | { | |
3887 | struct commit_list **pp; | |
3888 | ||
3889 | if (!revs->saved_parents_slab) { | |
3890 | revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents)); | |
3891 | init_saved_parents(revs->saved_parents_slab); | |
3892 | } | |
3893 | ||
3894 | pp = saved_parents_at(revs->saved_parents_slab, commit); | |
3895 | ||
3896 | /* | |
3897 | * When walking with reflogs, we may visit the same commit | |
3898 | * several times: once for each appearance in the reflog. | |
3899 | * | |
3900 | * In this case, save_parents() will be called multiple times. | |
3901 | * We want to keep only the first set of parents. We need to | |
3902 | * store a sentinel value for an empty (i.e., NULL) parent | |
3903 | * list to distinguish it from a not-yet-saved list, however. | |
3904 | */ | |
3905 | if (*pp) | |
3906 | return; | |
3907 | if (commit->parents) | |
3908 | *pp = copy_commit_list(commit->parents); | |
3909 | else | |
3910 | *pp = EMPTY_PARENT_LIST; | |
3911 | } | |
3912 | ||
3913 | static void free_saved_parents(struct rev_info *revs) | |
3914 | { | |
3915 | if (revs->saved_parents_slab) | |
3916 | clear_saved_parents(revs->saved_parents_slab); | |
3917 | } | |
3918 | ||
3919 | struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit) | |
3920 | { | |
3921 | struct commit_list *parents; | |
3922 | ||
3923 | if (!revs->saved_parents_slab) | |
3924 | return commit->parents; | |
3925 | ||
3926 | parents = *saved_parents_at(revs->saved_parents_slab, commit); | |
3927 | if (parents == EMPTY_PARENT_LIST) | |
3928 | return NULL; | |
3929 | return parents; | |
3930 | } | |
3931 | ||
beb5af43 AS |
3932 | enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit) |
3933 | { | |
3934 | enum commit_action action = get_commit_action(revs, commit); | |
3935 | ||
3936 | if (action == commit_show && | |
beb5af43 | 3937 | revs->prune && revs->dense && want_ancestry(revs)) { |
53d00b39 TR |
3938 | /* |
3939 | * --full-diff on simplified parents is no good: it | |
3940 | * will show spurious changes from the commits that | |
3941 | * were elided. So we save the parents on the side | |
3942 | * when --full-diff is in effect. | |
3943 | */ | |
3944 | if (revs->full_diff) | |
3945 | save_parents(revs, commit); | |
c7edcae0 | 3946 | if (rewrite_parents(revs, commit, rewrite_one) < 0) |
beb5af43 AS |
3947 | return commit_error; |
3948 | } | |
3949 | return action; | |
3950 | } | |
3951 | ||
1b32dece NTND |
3952 | static void track_linear(struct rev_info *revs, struct commit *commit) |
3953 | { | |
3954 | if (revs->track_first_time) { | |
3955 | revs->linear = 1; | |
3956 | revs->track_first_time = 0; | |
3957 | } else { | |
3958 | struct commit_list *p; | |
3959 | for (p = revs->previous_parents; p; p = p->next) | |
3960 | if (p->item == NULL || /* first commit */ | |
4a7e27e9 | 3961 | oideq(&p->item->object.oid, &commit->object.oid)) |
1b32dece NTND |
3962 | break; |
3963 | revs->linear = p != NULL; | |
3964 | } | |
3965 | if (revs->reverse) { | |
3966 | if (revs->linear) | |
3967 | commit->object.flags |= TRACK_LINEAR; | |
3968 | } | |
3969 | free_commit_list(revs->previous_parents); | |
3970 | revs->previous_parents = copy_commit_list(commit->parents); | |
3971 | } | |
3972 | ||
d5db6c9e | 3973 | static struct commit *get_revision_1(struct rev_info *revs) |
a4a88b2b | 3974 | { |
7c2f08aa | 3975 | while (1) { |
d08565bf | 3976 | struct commit *commit; |
a4a88b2b | 3977 | |
d08565bf JK |
3978 | if (revs->reflog_info) |
3979 | commit = next_reflog_entry(revs->reflog_info); | |
f0d9cc41 DS |
3980 | else if (revs->topo_walk_info) |
3981 | commit = next_topo_commit(revs); | |
d08565bf JK |
3982 | else |
3983 | commit = pop_commit(&revs->commits); | |
2a0925be | 3984 | |
7c2f08aa JK |
3985 | if (!commit) |
3986 | return NULL; | |
3987 | ||
d08565bf | 3988 | if (revs->reflog_info) |
ffa1eeae | 3989 | commit->object.flags &= ~(ADDED | SEEN | SHOWN); |
8860fd42 | 3990 | |
2a0925be LT |
3991 | /* |
3992 | * If we haven't done the list limiting, we need to look at | |
be7db6e5 LT |
3993 | * the parents here. We also need to do the date-based limiting |
3994 | * that we'd otherwise have done in limit_list(). | |
2a0925be | 3995 | */ |
be7db6e5 | 3996 | if (!revs->limited) { |
744f4985 | 3997 | if (revs->max_age != -1 && |
de239446 | 3998 | comparison_date(revs, commit) < revs->max_age) |
86ab4906 | 3999 | continue; |
d08565bf JK |
4000 | |
4001 | if (revs->reflog_info) | |
4002 | try_to_simplify_commit(revs, commit); | |
f0d9cc41 DS |
4003 | else if (revs->topo_walk_info) |
4004 | expand_topo_walk(revs, commit); | |
5284fc5c | 4005 | else if (process_parents(revs, commit, &revs->commits, NULL) < 0) { |
2db1a43f VM |
4006 | if (!revs->ignore_missing_links) |
4007 | die("Failed to traverse parents of commit %s", | |
f2fd0760 | 4008 | oid_to_hex(&commit->object.oid)); |
2db1a43f | 4009 | } |
be7db6e5 | 4010 | } |
744f4985 | 4011 | |
252a7c02 LT |
4012 | switch (simplify_commit(revs, commit)) { |
4013 | case commit_ignore: | |
8ecae9b0 | 4014 | continue; |
252a7c02 | 4015 | case commit_error: |
ed62089c | 4016 | die("Failed to simplify parents of commit %s", |
f2fd0760 | 4017 | oid_to_hex(&commit->object.oid)); |
252a7c02 | 4018 | default: |
1b32dece NTND |
4019 | if (revs->track_linear) |
4020 | track_linear(revs, commit); | |
252a7c02 | 4021 | return commit; |
384e99a4 | 4022 | } |
7c2f08aa | 4023 | } |
765ac8ec | 4024 | } |
d5db6c9e | 4025 | |
be6754c6 MH |
4026 | /* |
4027 | * Return true for entries that have not yet been shown. (This is an | |
4028 | * object_array_each_func_t.) | |
4029 | */ | |
4030 | static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused) | |
86ab4906 | 4031 | { |
be6754c6 MH |
4032 | return !(entry->item->flags & SHOWN); |
4033 | } | |
86ab4906 | 4034 | |
be6754c6 MH |
4035 | /* |
4036 | * If array is on the verge of a realloc, garbage-collect any entries | |
4037 | * that have already been shown to try to free up some space. | |
4038 | */ | |
4039 | static void gc_boundary(struct object_array *array) | |
4040 | { | |
4041 | if (array->nr == array->alloc) | |
4042 | object_array_filter(array, entry_unshown, NULL); | |
86ab4906 JH |
4043 | } |
4044 | ||
4603ec0f AS |
4045 | static void create_boundary_commit_list(struct rev_info *revs) |
4046 | { | |
4047 | unsigned i; | |
4048 | struct commit *c; | |
4049 | struct object_array *array = &revs->boundary_commits; | |
4050 | struct object_array_entry *objects = array->objects; | |
4051 | ||
4052 | /* | |
4053 | * If revs->commits is non-NULL at this point, an error occurred in | |
4054 | * get_revision_1(). Ignore the error and continue printing the | |
4055 | * boundary commits anyway. (This is what the code has always | |
4056 | * done.) | |
4057 | */ | |
4058 | if (revs->commits) { | |
4059 | free_commit_list(revs->commits); | |
4060 | revs->commits = NULL; | |
4061 | } | |
4062 | ||
4063 | /* | |
4064 | * Put all of the actual boundary commits from revs->boundary_commits | |
4065 | * into revs->commits | |
4066 | */ | |
4067 | for (i = 0; i < array->nr; i++) { | |
4068 | c = (struct commit *)(objects[i].item); | |
4069 | if (!c) | |
4070 | continue; | |
4071 | if (!(c->object.flags & CHILD_SHOWN)) | |
4072 | continue; | |
4073 | if (c->object.flags & (SHOWN | BOUNDARY)) | |
4074 | continue; | |
4075 | c->object.flags |= BOUNDARY; | |
4076 | commit_list_insert(c, &revs->commits); | |
4077 | } | |
4078 | ||
4079 | /* | |
4080 | * If revs->topo_order is set, sort the boundary commits | |
4081 | * in topological order | |
4082 | */ | |
08f704f2 | 4083 | sort_in_topological_order(&revs->commits, revs->sort_order); |
4603ec0f AS |
4084 | } |
4085 | ||
7fefda5c | 4086 | static struct commit *get_revision_internal(struct rev_info *revs) |
d5db6c9e JH |
4087 | { |
4088 | struct commit *c = NULL; | |
86ab4906 JH |
4089 | struct commit_list *l; |
4090 | ||
4091 | if (revs->boundary == 2) { | |
4603ec0f AS |
4092 | /* |
4093 | * All of the normal commits have already been returned, | |
4094 | * and we are now returning boundary commits. | |
4095 | * create_boundary_commit_list() has populated | |
4096 | * revs->commits with the remaining commits to return. | |
4097 | */ | |
4098 | c = pop_commit(&revs->commits); | |
4099 | if (c) | |
4100 | c->object.flags |= SHOWN; | |
9c5e66e9 JS |
4101 | return c; |
4102 | } | |
4103 | ||
86ab4906 | 4104 | /* |
b72a1904 JK |
4105 | * If our max_count counter has reached zero, then we are done. We |
4106 | * don't simply return NULL because we still might need to show | |
4107 | * boundary commits. But we want to avoid calling get_revision_1, which | |
4108 | * might do a considerable amount of work finding the next commit only | |
4109 | * for us to throw it away. | |
4110 | * | |
4111 | * If it is non-zero, then either we don't have a max_count at all | |
4112 | * (-1), or it is still counting, in which case we decrement. | |
86ab4906 | 4113 | */ |
b72a1904 JK |
4114 | if (revs->max_count) { |
4115 | c = get_revision_1(revs); | |
4116 | if (c) { | |
9e57ac55 | 4117 | while (revs->skip_count > 0) { |
b72a1904 JK |
4118 | revs->skip_count--; |
4119 | c = get_revision_1(revs); | |
4120 | if (!c) | |
4121 | break; | |
4122 | } | |
8839ac94 | 4123 | } |
86ab4906 | 4124 | |
b72a1904 JK |
4125 | if (revs->max_count > 0) |
4126 | revs->max_count--; | |
d5db6c9e | 4127 | } |
86ab4906 | 4128 | |
c33d8593 JH |
4129 | if (c) |
4130 | c->object.flags |= SHOWN; | |
4131 | ||
9e57ac55 | 4132 | if (!revs->boundary) |
d5db6c9e | 4133 | return c; |
86ab4906 JH |
4134 | |
4135 | if (!c) { | |
4136 | /* | |
4137 | * get_revision_1() runs out the commits, and | |
4138 | * we are done computing the boundaries. | |
4139 | * switch to boundary commits output mode. | |
4140 | */ | |
4141 | revs->boundary = 2; | |
4603ec0f AS |
4142 | |
4143 | /* | |
4144 | * Update revs->commits to contain the list of | |
4145 | * boundary commits. | |
4146 | */ | |
4147 | create_boundary_commit_list(revs); | |
4148 | ||
3c68d67b | 4149 | return get_revision_internal(revs); |
86ab4906 JH |
4150 | } |
4151 | ||
4152 | /* | |
4153 | * boundary commits are the commits that are parents of the | |
4154 | * ones we got from get_revision_1() but they themselves are | |
4155 | * not returned from get_revision_1(). Before returning | |
4156 | * 'c', we need to mark its parents that they could be boundaries. | |
4157 | */ | |
4158 | ||
4159 | for (l = c->parents; l; l = l->next) { | |
4160 | struct object *p; | |
4161 | p = &(l->item->object); | |
c33d8593 | 4162 | if (p->flags & (CHILD_SHOWN | SHOWN)) |
86ab4906 JH |
4163 | continue; |
4164 | p->flags |= CHILD_SHOWN; | |
4165 | gc_boundary(&revs->boundary_commits); | |
4166 | add_object_array(p, NULL, &revs->boundary_commits); | |
4167 | } | |
4168 | ||
4169 | return c; | |
d5db6c9e | 4170 | } |
7fefda5c AS |
4171 | |
4172 | struct commit *get_revision(struct rev_info *revs) | |
4173 | { | |
498bcd31 TR |
4174 | struct commit *c; |
4175 | struct commit_list *reversed; | |
4176 | ||
4177 | if (revs->reverse) { | |
4178 | reversed = NULL; | |
9e57ac55 | 4179 | while ((c = get_revision_internal(revs))) |
498bcd31 | 4180 | commit_list_insert(c, &reversed); |
498bcd31 TR |
4181 | revs->commits = reversed; |
4182 | revs->reverse = 0; | |
4183 | revs->reverse_output_stage = 1; | |
4184 | } | |
4185 | ||
1b32dece NTND |
4186 | if (revs->reverse_output_stage) { |
4187 | c = pop_commit(&revs->commits); | |
4188 | if (revs->track_linear) | |
4189 | revs->linear = !!(c && c->object.flags & TRACK_LINEAR); | |
4190 | return c; | |
4191 | } | |
498bcd31 TR |
4192 | |
4193 | c = get_revision_internal(revs); | |
7fefda5c AS |
4194 | if (c && revs->graph) |
4195 | graph_update(revs->graph, c); | |
1b32dece | 4196 | if (!c) { |
53d00b39 | 4197 | free_saved_parents(revs); |
1b32dece NTND |
4198 | if (revs->previous_parents) { |
4199 | free_commit_list(revs->previous_parents); | |
4200 | revs->previous_parents = NULL; | |
4201 | } | |
4202 | } | |
7fefda5c AS |
4203 | return c; |
4204 | } | |
1df2d656 | 4205 | |
49825164 | 4206 | const char *get_revision_mark(const struct rev_info *revs, const struct commit *commit) |
1df2d656 MG |
4207 | { |
4208 | if (commit->object.flags & BOUNDARY) | |
4209 | return "-"; | |
4210 | else if (commit->object.flags & UNINTERESTING) | |
4211 | return "^"; | |
adbbb31e MG |
4212 | else if (commit->object.flags & PATCHSAME) |
4213 | return "="; | |
1df2d656 MG |
4214 | else if (!revs || revs->left_right) { |
4215 | if (commit->object.flags & SYMMETRIC_LEFT) | |
4216 | return "<"; | |
4217 | else | |
4218 | return ">"; | |
4219 | } else if (revs->graph) | |
4220 | return "*"; | |
adbbb31e MG |
4221 | else if (revs->cherry_mark) |
4222 | return "+"; | |
1df2d656 MG |
4223 | return ""; |
4224 | } | |
b1b47554 MG |
4225 | |
4226 | void put_revision_mark(const struct rev_info *revs, const struct commit *commit) | |
4227 | { | |
49825164 | 4228 | const char *mark = get_revision_mark(revs, commit); |
b1b47554 MG |
4229 | if (!strlen(mark)) |
4230 | return; | |
4231 | fputs(mark, stdout); | |
4232 | putchar(' '); | |
4233 | } |