Coverage Report

Created: 2026-12-01 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/git/revision.c
Line
Count
Source
1
#define USE_THE_REPOSITORY_VARIABLE
2
#define DISABLE_SIGN_COMPARE_WARNINGS
3
4
#include "git-compat-util.h"
5
#include "config.h"
6
#include "environment.h"
7
#include "gettext.h"
8
#include "hex.h"
9
#include "object-name.h"
10
#include "object-file.h"
11
#include "odb.h"
12
#include "oidset.h"
13
#include "tag.h"
14
#include "blob.h"
15
#include "tree.h"
16
#include "commit.h"
17
#include "diff.h"
18
#include "diff-merges.h"
19
#include "refs.h"
20
#include "revision.h"
21
#include "repository.h"
22
#include "graph.h"
23
#include "grep.h"
24
#include "reflog-walk.h"
25
#include "patch-ids.h"
26
#include "decorate.h"
27
#include "string-list.h"
28
#include "line-log.h"
29
#include "log-tree.h"
30
#include "mailmap.h"
31
#include "commit-slab.h"
32
#include "cache-tree.h"
33
#include "bisect.h"
34
#include "packfile.h"
35
#include "worktree.h"
36
#include "path.h"
37
#include "read-cache.h"
38
#include "setup.h"
39
#include "sparse-index.h"
40
#include "strvec.h"
41
#include "trace2.h"
42
#include "commit-reach.h"
43
#include "commit-graph.h"
44
#include "prio-queue.h"
45
#include "hashmap.h"
46
#include "utf8.h"
47
#include "bloom.h"
48
#include "json-writer.h"
49
#include "list-objects-filter-options.h"
50
#include "resolve-undo.h"
51
#include "parse-options.h"
52
#include "wildmatch.h"
53
54
static char *term_bad;
55
static char *term_good;
56
57
0
implement_shared_commit_slab(revision_sources, char *);
Unexecuted instantiation: init_revision_sources_with_stride
Unexecuted instantiation: clear_revision_sources
Unexecuted instantiation: revision_sources_at_peek
58
0
59
0
static inline int want_ancestry(const struct rev_info *revs);
60
0
61
0
static void mark_blob_uninteresting(struct blob *blob)
62
0
{
63
0
  if (!blob)
64
0
    return;
65
0
  if (blob->object.flags & UNINTERESTING)
66
0
    return;
67
0
  blob->object.flags |= UNINTERESTING;
68
0
}
69
70
static void mark_tree_contents_uninteresting(struct repository *r,
71
               struct tree *tree)
72
0
{
73
0
  struct tree_desc desc;
74
0
  struct name_entry entry;
75
76
0
  if (repo_parse_tree_gently(the_repository, tree, 1) < 0)
77
0
    return;
78
79
0
  init_tree_desc(&desc, &tree->object.oid, tree->buffer, tree->size);
80
0
  while (tree_entry(&desc, &entry)) {
81
0
    switch (object_type(entry.mode)) {
82
0
    case OBJ_TREE:
83
0
      mark_tree_uninteresting(r, lookup_tree(r, &entry.oid));
84
0
      break;
85
0
    case OBJ_BLOB:
86
0
      mark_blob_uninteresting(lookup_blob(r, &entry.oid));
87
0
      break;
88
0
    default:
89
      /* Subproject commit - not in this repository */
90
0
      break;
91
0
    }
92
0
  }
93
94
  /*
95
   * We don't care about the tree any more
96
   * after it has been marked uninteresting.
97
   */
98
0
  free_tree_buffer(tree);
99
0
}
100
101
void mark_tree_uninteresting(struct repository *r, struct tree *tree)
102
0
{
103
0
  struct object *obj;
104
105
0
  if (!tree)
106
0
    return;
107
108
0
  obj = &tree->object;
109
0
  if (obj->flags & UNINTERESTING)
110
0
    return;
111
0
  obj->flags |= UNINTERESTING;
112
0
  mark_tree_contents_uninteresting(r, tree);
113
0
}
114
115
struct path_and_oids_entry {
116
  struct hashmap_entry ent;
117
  char *path;
118
  struct oidset trees;
119
};
120
121
static int path_and_oids_cmp(const void *hashmap_cmp_fn_data UNUSED,
122
           const struct hashmap_entry *eptr,
123
           const struct hashmap_entry *entry_or_key,
124
           const void *keydata UNUSED)
125
0
{
126
0
  const struct path_and_oids_entry *e1, *e2;
127
128
0
  e1 = container_of(eptr, const struct path_and_oids_entry, ent);
129
0
  e2 = container_of(entry_or_key, const struct path_and_oids_entry, ent);
130
131
0
  return strcmp(e1->path, e2->path);
132
0
}
133
134
static void paths_and_oids_clear(struct hashmap *map)
135
0
{
136
0
  struct hashmap_iter iter;
137
0
  struct path_and_oids_entry *entry;
138
139
0
  hashmap_for_each_entry(map, &iter, entry, ent /* member name */) {
140
0
    oidset_clear(&entry->trees);
141
0
    free(entry->path);
142
0
  }
143
144
0
  hashmap_clear_and_free(map, struct path_and_oids_entry, ent);
145
0
}
146
147
static void paths_and_oids_insert(struct hashmap *map,
148
          const char *path,
149
          const struct object_id *oid)
150
0
{
151
0
  int hash = strhash(path);
152
0
  struct path_and_oids_entry key;
153
0
  struct path_and_oids_entry *entry;
154
155
0
  hashmap_entry_init(&key.ent, hash);
156
157
  /* use a shallow copy for the lookup */
158
0
  key.path = (char *)path;
159
0
  oidset_init(&key.trees, 0);
160
161
0
  entry = hashmap_get_entry(map, &key, ent, NULL);
162
0
  if (!entry) {
163
0
    CALLOC_ARRAY(entry, 1);
164
0
    hashmap_entry_init(&entry->ent, hash);
165
0
    entry->path = xstrdup(key.path);
166
0
    oidset_init(&entry->trees, 16);
167
0
    hashmap_put(map, &entry->ent);
168
0
  }
169
170
0
  oidset_insert(&entry->trees, oid);
171
0
}
172
173
static void add_children_by_path(struct repository *r,
174
         struct tree *tree,
175
         struct hashmap *map)
176
0
{
177
0
  struct tree_desc desc;
178
0
  struct name_entry entry;
179
180
0
  if (!tree)
181
0
    return;
182
183
0
  if (repo_parse_tree_gently(the_repository, tree, 1) < 0)
184
0
    return;
185
186
0
  init_tree_desc(&desc, &tree->object.oid, tree->buffer, tree->size);
187
0
  while (tree_entry(&desc, &entry)) {
188
0
    switch (object_type(entry.mode)) {
189
0
    case OBJ_TREE:
190
0
      paths_and_oids_insert(map, entry.path, &entry.oid);
191
192
0
      if (tree->object.flags & UNINTERESTING) {
193
0
        struct tree *child = lookup_tree(r, &entry.oid);
194
0
        if (child)
195
0
          child->object.flags |= UNINTERESTING;
196
0
      }
197
0
      break;
198
0
    case OBJ_BLOB:
199
0
      if (tree->object.flags & UNINTERESTING) {
200
0
        struct blob *child = lookup_blob(r, &entry.oid);
201
0
        if (child)
202
0
          child->object.flags |= UNINTERESTING;
203
0
      }
204
0
      break;
205
0
    default:
206
      /* Subproject commit - not in this repository */
207
0
      break;
208
0
    }
209
0
  }
210
211
0
  free_tree_buffer(tree);
212
0
}
213
214
void mark_trees_uninteresting_sparse(struct repository *r,
215
             struct oidset *trees)
216
0
{
217
0
  unsigned has_interesting = 0, has_uninteresting = 0;
218
0
  struct hashmap map = HASHMAP_INIT(path_and_oids_cmp, NULL);
219
0
  struct hashmap_iter map_iter;
220
0
  struct path_and_oids_entry *entry;
221
0
  struct object_id *oid;
222
0
  struct oidset_iter iter;
223
224
0
  oidset_iter_init(trees, &iter);
225
0
  while ((!has_interesting || !has_uninteresting) &&
226
0
         (oid = oidset_iter_next(&iter))) {
227
0
    struct tree *tree = lookup_tree(r, oid);
228
229
0
    if (!tree)
230
0
      continue;
231
232
0
    if (tree->object.flags & UNINTERESTING)
233
0
      has_uninteresting = 1;
234
0
    else
235
0
      has_interesting = 1;
236
0
  }
237
238
  /* Do not walk unless we have both types of trees. */
239
0
  if (!has_uninteresting || !has_interesting)
240
0
    return;
241
242
0
  oidset_iter_init(trees, &iter);
243
0
  while ((oid = oidset_iter_next(&iter))) {
244
0
    struct tree *tree = lookup_tree(r, oid);
245
0
    add_children_by_path(r, tree, &map);
246
0
  }
247
248
0
  hashmap_for_each_entry(&map, &map_iter, entry, ent /* member name */)
249
0
    mark_trees_uninteresting_sparse(r, &entry->trees);
250
251
0
  paths_and_oids_clear(&map);
252
0
}
253
254
static void mark_one_parent_uninteresting(struct rev_info *revs, struct commit *commit,
255
            struct commit_stack *pending)
256
0
{
257
0
  struct commit_list *l;
258
259
0
  if (commit->object.flags & UNINTERESTING)
260
0
    return;
261
0
  commit->object.flags |= UNINTERESTING;
262
263
  /*
264
   * Normally we haven't parsed the parent
265
   * yet, so we won't have a parent of a parent
266
   * here. However, it may turn out that we've
267
   * reached this commit some other way (where it
268
   * wasn't uninteresting), in which case we need
269
   * to mark its parents recursively too..
270
   */
271
0
  for (l = commit->parents; l; l = l->next) {
272
0
    commit_stack_push(pending, l->item);
273
0
    if (revs && revs->exclude_first_parent_only)
274
0
      break;
275
0
  }
276
0
}
277
278
void mark_parents_uninteresting(struct rev_info *revs, struct commit *commit)
279
0
{
280
0
  struct commit_stack pending = COMMIT_STACK_INIT;
281
0
  struct commit_list *l;
282
283
0
  for (l = commit->parents; l; l = l->next) {
284
0
    mark_one_parent_uninteresting(revs, l->item, &pending);
285
0
    if (revs && revs->exclude_first_parent_only)
286
0
      break;
287
0
  }
288
289
0
  while (pending.nr > 0)
290
0
    mark_one_parent_uninteresting(revs, commit_stack_pop(&pending),
291
0
                &pending);
292
293
0
  commit_stack_clear(&pending);
294
0
}
295
296
static void add_pending_object_with_path(struct rev_info *revs,
297
           struct object *obj,
298
           const char *name, unsigned mode,
299
           const char *path)
300
0
{
301
0
  struct interpret_branch_name_options options = { 0 };
302
0
  if (!obj)
303
0
    return;
304
0
  if (revs->no_walk && (obj->flags & UNINTERESTING))
305
0
    revs->no_walk = 0;
306
0
  if (revs->reflog_info && obj->type == OBJ_COMMIT) {
307
0
    struct strbuf buf = STRBUF_INIT;
308
0
    size_t namelen = strlen(name);
309
0
    int len = repo_interpret_branch_name(the_repository, name,
310
0
                 namelen, &buf, &options);
311
312
0
    if (0 < len && len < namelen && buf.len)
313
0
      strbuf_addstr(&buf, name + len);
314
0
    add_reflog_for_walk(revs->reflog_info,
315
0
            (struct commit *)obj,
316
0
            buf.buf[0] ? buf.buf: name);
317
0
    strbuf_release(&buf);
318
0
    return; /* do not add the commit itself */
319
0
  }
320
0
  add_object_array_with_path(obj, name, &revs->pending, mode, path);
321
0
}
322
323
static void add_pending_object_with_mode(struct rev_info *revs,
324
           struct object *obj,
325
           const char *name, unsigned mode)
326
0
{
327
0
  add_pending_object_with_path(revs, obj, name, mode, NULL);
328
0
}
329
330
void add_pending_object(struct rev_info *revs,
331
      struct object *obj, const char *name)
332
0
{
333
0
  add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
334
0
}
335
336
void add_head_to_pending(struct rev_info *revs)
337
0
{
338
0
  struct object_id oid;
339
0
  struct object *obj;
340
0
  if (repo_get_oid(the_repository, "HEAD", &oid))
341
0
    return;
342
0
  obj = parse_object(revs->repo, &oid);
343
0
  if (!obj)
344
0
    return;
345
0
  add_pending_object(revs, obj, "HEAD");
346
0
}
347
348
static struct object *get_reference(struct rev_info *revs, const char *name,
349
            const struct object_id *oid,
350
            unsigned int flags)
351
0
{
352
0
  struct object *object;
353
354
0
  object = parse_object_with_flags(revs->repo, oid,
355
0
           revs->verify_objects ? 0 :
356
0
           PARSE_OBJECT_SKIP_HASH_CHECK |
357
0
           PARSE_OBJECT_DISCARD_TREE);
358
359
0
  if (!object) {
360
0
    if (revs->ignore_missing)
361
0
      return NULL;
362
0
    if (revs->exclude_promisor_objects &&
363
0
        is_promisor_object(revs->repo, oid))
364
0
      return NULL;
365
0
    if (revs->do_not_die_on_missing_objects) {
366
0
      oidset_insert(&revs->missing_commits, oid);
367
0
      return NULL;
368
0
    }
369
0
    die("bad object %s", name);
370
0
  }
371
0
  object->flags |= flags;
372
0
  return object;
373
0
}
374
375
void add_pending_oid(struct rev_info *revs, const char *name,
376
          const struct object_id *oid, unsigned int flags)
377
0
{
378
0
  struct object *object = get_reference(revs, name, oid, flags);
379
0
  add_pending_object(revs, object, name);
380
0
}
381
382
static struct commit *handle_commit(struct rev_info *revs,
383
            struct object_array_entry *entry)
384
0
{
385
0
  struct object *object = entry->item;
386
0
  const char *name = entry->name;
387
0
  const char *path = entry->path;
388
0
  unsigned int mode = entry->mode;
389
0
  unsigned long flags = object->flags;
390
391
  /*
392
   * Tag object? Look what it points to..
393
   */
394
0
  while (object->type == OBJ_TAG) {
395
0
    struct tag *tag = (struct tag *) object;
396
0
    struct object_id *oid;
397
0
    if (revs->tag_objects && !(flags & UNINTERESTING))
398
0
      add_pending_object(revs, object, tag->tag);
399
0
    oid = get_tagged_oid(tag);
400
0
    object = parse_object(revs->repo, oid);
401
0
    if (!object) {
402
0
      if (revs->ignore_missing_links || (flags & UNINTERESTING))
403
0
        return NULL;
404
0
      if (revs->exclude_promisor_objects &&
405
0
          is_promisor_object(revs->repo, &tag->tagged->oid))
406
0
        return NULL;
407
0
      if (revs->do_not_die_on_missing_objects && oid) {
408
0
        oidset_insert(&revs->missing_commits, oid);
409
0
        return NULL;
410
0
      }
411
0
      die("bad object %s", oid_to_hex(&tag->tagged->oid));
412
0
    }
413
0
    object->flags |= flags;
414
    /*
415
     * We'll handle the tagged object by looping or dropping
416
     * through to the non-tag handlers below. Do not
417
     * propagate path data from the tag's pending entry.
418
     */
419
0
    path = NULL;
420
0
    mode = 0;
421
0
  }
422
423
  /*
424
   * Commit object? Just return it, we'll do all the complex
425
   * reachability crud.
426
   */
427
0
  if (object->type == OBJ_COMMIT) {
428
0
    struct commit *commit = (struct commit *)object;
429
430
0
    if (repo_parse_commit(revs->repo, commit) < 0)
431
0
      die("unable to parse commit %s", name);
432
0
    if (flags & UNINTERESTING) {
433
0
      mark_parents_uninteresting(revs, commit);
434
435
0
      if (!revs->topo_order || !generation_numbers_enabled(the_repository))
436
0
        revs->limited = 1;
437
0
    }
438
0
    if (revs->sources) {
439
0
      char **slot = revision_sources_at(revs->sources, commit);
440
441
0
      if (!*slot)
442
0
        *slot = xstrdup(name);
443
0
    }
444
0
    return commit;
445
0
  }
446
447
  /*
448
   * Tree object? Either mark it uninteresting, or add it
449
   * to the list of objects to look at later..
450
   */
451
0
  if (object->type == OBJ_TREE) {
452
0
    struct tree *tree = (struct tree *)object;
453
0
    if (!revs->tree_objects)
454
0
      return NULL;
455
0
    if (flags & UNINTERESTING) {
456
0
      mark_tree_contents_uninteresting(revs->repo, tree);
457
0
      return NULL;
458
0
    }
459
0
    add_pending_object_with_path(revs, object, name, mode, path);
460
0
    return NULL;
461
0
  }
462
463
  /*
464
   * Blob object? You know the drill by now..
465
   */
466
0
  if (object->type == OBJ_BLOB) {
467
0
    if (!revs->blob_objects)
468
0
      return NULL;
469
0
    if (flags & UNINTERESTING)
470
0
      return NULL;
471
0
    add_pending_object_with_path(revs, object, name, mode, path);
472
0
    return NULL;
473
0
  }
474
0
  die("%s is unknown object", name);
475
0
}
476
477
static int everybody_uninteresting(struct prio_queue *orig,
478
           struct commit **interesting_cache)
479
0
{
480
0
  struct commit *commit;
481
482
0
  if (*interesting_cache) {
483
0
    commit = *interesting_cache;
484
0
    if (!(commit->object.flags & UNINTERESTING))
485
0
      return 0;
486
0
  }
487
488
0
  prio_queue_for_each(orig, commit) {
489
0
    if (commit->object.flags & UNINTERESTING)
490
0
      continue;
491
492
0
    *interesting_cache = commit;
493
0
    return 0;
494
0
  }
495
0
  return 1;
496
0
}
497
498
/*
499
 * A definition of "relevant" commit that we can use to simplify limited graphs
500
 * by eliminating side branches.
501
 *
502
 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
503
 * in our list), or that is a specified BOTTOM commit. Then after computing
504
 * a limited list, during processing we can generally ignore boundary merges
505
 * coming from outside the graph, (ie from irrelevant parents), and treat
506
 * those merges as if they were single-parent. TREESAME is defined to consider
507
 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
508
 * we don't care if we were !TREESAME to non-graph parents.
509
 *
510
 * Treating bottom commits as relevant ensures that a limited graph's
511
 * connection to the actual bottom commit is not viewed as a side branch, but
512
 * treated as part of the graph. For example:
513
 *
514
 *   ....Z...A---X---o---o---B
515
 *        .     /
516
 *         W---Y
517
 *
518
 * When computing "A..B", the A-X connection is at least as important as
519
 * Y-X, despite A being flagged UNINTERESTING.
520
 *
521
 * And when computing --ancestry-path "A..B", the A-X connection is more
522
 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
523
 */
524
static inline int relevant_commit(struct commit *commit)
525
0
{
526
0
  return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
527
0
}
528
529
/*
530
 * Return a single relevant commit from a parent list. If we are a TREESAME
531
 * commit, and this selects one of our parents, then we can safely simplify to
532
 * that parent.
533
 */
534
static struct commit *one_relevant_parent(const struct rev_info *revs,
535
            struct commit_list *orig)
536
0
{
537
0
  struct commit_list *list = orig;
538
0
  struct commit *relevant = NULL;
539
540
0
  if (!orig)
541
0
    return NULL;
542
543
  /*
544
   * For 1-parent commits, or if first-parent-only, then return that
545
   * first parent (even if not "relevant" by the above definition).
546
   * TREESAME will have been set purely on that parent.
547
   */
548
0
  if (revs->first_parent_only || !orig->next)
549
0
    return orig->item;
550
551
  /*
552
   * For multi-parent commits, identify a sole relevant parent, if any.
553
   * If we have only one relevant parent, then TREESAME will be set purely
554
   * with regard to that parent, and we can simplify accordingly.
555
   *
556
   * If we have more than one relevant parent, or no relevant parents
557
   * (and multiple irrelevant ones), then we can't select a parent here
558
   * and return NULL.
559
   */
560
0
  while (list) {
561
0
    struct commit *commit = list->item;
562
0
    list = list->next;
563
0
    if (relevant_commit(commit)) {
564
0
      if (relevant)
565
0
        return NULL;
566
0
      relevant = commit;
567
0
    }
568
0
  }
569
0
  return relevant;
570
0
}
571
572
/*
573
 * The goal is to get REV_TREE_NEW as the result only if the
574
 * diff consists of all '+' (and no other changes), REV_TREE_OLD
575
 * if the whole diff is removal of old data, and otherwise
576
 * REV_TREE_DIFFERENT (of course if the trees are the same we
577
 * want REV_TREE_SAME).
578
 *
579
 * The only time we care about the distinction is when
580
 * remove_empty_trees is in effect, in which case we care only about
581
 * whether the whole change is REV_TREE_NEW, or if there's another type
582
 * of change. Which means we can stop the diff early in either of these
583
 * cases:
584
 *
585
 *   1. We're not using remove_empty_trees at all.
586
 *
587
 *   2. We saw anything except REV_TREE_NEW.
588
 */
589
0
#define REV_TREE_SAME   0
590
0
#define REV_TREE_NEW    1  /* Only new files */
591
0
#define REV_TREE_OLD    2  /* Only files removed */
592
0
#define REV_TREE_DIFFERENT  3  /* Mixed changes */
593
static int tree_difference = REV_TREE_SAME;
594
595
static void file_add_remove(struct diff_options *options,
596
        int addremove,
597
        unsigned mode UNUSED,
598
        const struct object_id *oid UNUSED,
599
        int oid_valid UNUSED,
600
        const char *fullpath UNUSED,
601
        unsigned dirty_submodule UNUSED)
602
0
{
603
0
  int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
604
0
  struct rev_info *revs = options->change_fn_data;
605
606
0
  tree_difference |= diff;
607
0
  if (!revs->remove_empty_trees || tree_difference != REV_TREE_NEW)
608
0
    options->flags.has_changes = 1;
609
0
}
610
611
static void file_change(struct diff_options *options,
612
     unsigned old_mode UNUSED,
613
     unsigned new_mode UNUSED,
614
     const struct object_id *old_oid UNUSED,
615
     const struct object_id *new_oid UNUSED,
616
     int old_oid_valid UNUSED,
617
     int new_oid_valid UNUSED,
618
     const char *fullpath UNUSED,
619
     unsigned old_dirty_submodule UNUSED,
620
     unsigned new_dirty_submodule UNUSED)
621
0
{
622
0
  tree_difference = REV_TREE_DIFFERENT;
623
0
  options->flags.has_changes = 1;
624
0
}
625
626
static int bloom_filter_atexit_registered;
627
static unsigned int count_bloom_filter_maybe;
628
static unsigned int count_bloom_filter_definitely_not;
629
static unsigned int count_bloom_filter_false_positive;
630
static unsigned int count_bloom_filter_not_present;
631
632
static void trace2_bloom_filter_statistics_atexit(void)
633
0
{
634
0
  struct json_writer jw = JSON_WRITER_INIT;
635
636
0
  jw_object_begin(&jw, 0);
637
0
  jw_object_intmax(&jw, "filter_not_present", count_bloom_filter_not_present);
638
0
  jw_object_intmax(&jw, "maybe", count_bloom_filter_maybe);
639
0
  jw_object_intmax(&jw, "definitely_not", count_bloom_filter_definitely_not);
640
0
  jw_object_intmax(&jw, "false_positive", count_bloom_filter_false_positive);
641
0
  jw_end(&jw);
642
643
0
  trace2_data_json("bloom", the_repository, "statistics", &jw);
644
645
0
  jw_release(&jw);
646
0
}
647
648
static int forbid_bloom_filters(struct pathspec *spec)
649
0
{
650
0
  unsigned int allowed_magic =
651
0
    PATHSPEC_FROMTOP |
652
0
    PATHSPEC_MAXDEPTH |
653
0
    PATHSPEC_LITERAL |
654
0
    PATHSPEC_GLOB |
655
0
    PATHSPEC_ATTR;
656
657
0
  if (spec->magic & ~allowed_magic)
658
0
    return 1;
659
0
  for (size_t nr = 0; nr < spec->nr; nr++)
660
0
    if (spec->items[nr].magic & ~allowed_magic)
661
0
      return 1;
662
663
0
  return 0;
664
0
}
665
666
static void release_revisions_bloom_keyvecs(struct rev_info *revs);
667
668
static int convert_pathspec_to_bloom_keyvec(struct bloom_keyvec **out,
669
              const struct pathspec_item *pi,
670
              const struct bloom_filter_settings *settings)
671
0
{
672
0
  char *path_alloc = NULL;
673
0
  const char *path;
674
0
  size_t len;
675
0
  int res = -1;
676
677
0
  len = pi->nowildcard_len;
678
0
  if (len != pi->len) {
679
    /*
680
     * for path like "dir/file*", nowildcard part would be
681
     * "dir/file", but only "dir" should be used for the
682
     * bloom filter.
683
     */
684
0
    while (len > 0 && pi->match[len - 1] != '/')
685
0
      len--;
686
0
  }
687
  /* remove single trailing slash from path, if needed */
688
0
  if (len > 0 && pi->match[len - 1] == '/')
689
0
    len--;
690
691
0
  if (!len)
692
0
    goto cleanup;
693
694
0
  if (len != pi->len) {
695
0
    path_alloc = xmemdupz(pi->match, len);
696
0
    path = path_alloc;
697
0
  } else
698
0
    path = pi->match;
699
700
0
  *out = bloom_keyvec_new(path, len, settings);
701
702
0
  res = 0;
703
0
cleanup:
704
0
  free(path_alloc);
705
0
  return res;
706
0
}
707
708
static void prepare_to_use_bloom_filter(struct rev_info *revs)
709
0
{
710
0
  release_revisions_bloom_keyvecs(revs);
711
712
0
  if (!revs->commits)
713
0
    return;
714
715
0
  if (forbid_bloom_filters(&revs->prune_data))
716
0
    return;
717
718
0
  repo_parse_commit(revs->repo, revs->commits->item);
719
720
0
  revs->bloom_filter_settings = get_bloom_filter_settings(revs->repo);
721
0
  if (!revs->bloom_filter_settings)
722
0
    return;
723
724
0
  if (!revs->pruning.pathspec.nr)
725
0
    return;
726
727
0
  revs->bloom_keyvecs_nr = revs->pruning.pathspec.nr;
728
0
  CALLOC_ARRAY(revs->bloom_keyvecs, revs->bloom_keyvecs_nr);
729
730
0
  for (int i = 0; i < revs->pruning.pathspec.nr; i++) {
731
0
    if (convert_pathspec_to_bloom_keyvec(&revs->bloom_keyvecs[i],
732
0
                 &revs->pruning.pathspec.items[i],
733
0
                 revs->bloom_filter_settings))
734
0
      goto fail;
735
0
  }
736
737
0
  if (trace2_is_enabled() && !bloom_filter_atexit_registered) {
738
0
    atexit(trace2_bloom_filter_statistics_atexit);
739
0
    bloom_filter_atexit_registered = 1;
740
0
  }
741
742
0
  return;
743
744
0
fail:
745
0
  revs->bloom_filter_settings = NULL;
746
0
  release_revisions_bloom_keyvecs(revs);
747
0
}
748
749
static int check_maybe_different_in_bloom_filter(struct rev_info *revs,
750
             struct commit *commit)
751
0
{
752
0
  struct bloom_filter *filter;
753
0
  int result = 0;
754
755
0
  if (commit_graph_generation(commit) == GENERATION_NUMBER_INFINITY)
756
0
    return -1;
757
758
0
  filter = get_bloom_filter(revs->repo, commit);
759
760
0
  if (!filter) {
761
0
    count_bloom_filter_not_present++;
762
0
    return -1;
763
0
  }
764
765
0
  for (size_t nr = 0; !result && nr < revs->bloom_keyvecs_nr; nr++) {
766
0
    result = bloom_filter_contains_vec(filter,
767
0
               revs->bloom_keyvecs[nr],
768
0
               revs->bloom_filter_settings);
769
0
  }
770
771
0
  if (result)
772
0
    count_bloom_filter_maybe++;
773
0
  else
774
0
    count_bloom_filter_definitely_not++;
775
776
0
  return result;
777
0
}
778
779
static int rev_compare_tree(struct rev_info *revs,
780
          struct commit *parent, struct commit *commit, int nth_parent)
781
0
{
782
0
  struct tree *t1 = repo_get_commit_tree(the_repository, parent);
783
0
  struct tree *t2 = repo_get_commit_tree(the_repository, commit);
784
0
  int bloom_ret = 1;
785
786
0
  if (!t1)
787
0
    return REV_TREE_NEW;
788
0
  if (!t2)
789
0
    return REV_TREE_OLD;
790
791
0
  if (revs->simplify_by_decoration) {
792
    /*
793
     * If we are simplifying by decoration, then the commit
794
     * is worth showing if it has a tag pointing at it.
795
     */
796
0
    if (get_name_decoration(&commit->object))
797
0
      return REV_TREE_DIFFERENT;
798
    /*
799
     * A commit that is not pointed by a tag is uninteresting
800
     * if we are not limited by path.  This means that you will
801
     * see the usual "commits that touch the paths" plus any
802
     * tagged commit by specifying both --simplify-by-decoration
803
     * and pathspec.
804
     */
805
0
    if (!revs->prune_data.nr)
806
0
      return REV_TREE_SAME;
807
0
  }
808
809
0
  if (revs->bloom_keyvecs_nr && !nth_parent) {
810
0
    bloom_ret = check_maybe_different_in_bloom_filter(revs, commit);
811
812
0
    if (bloom_ret == 0)
813
0
      return REV_TREE_SAME;
814
0
  }
815
816
0
  tree_difference = REV_TREE_SAME;
817
0
  revs->pruning.flags.has_changes = 0;
818
0
  diff_tree_oid(&t1->object.oid, &t2->object.oid, "", &revs->pruning);
819
820
0
  if (!nth_parent)
821
0
    if (bloom_ret == 1 && tree_difference == REV_TREE_SAME)
822
0
      count_bloom_filter_false_positive++;
823
824
0
  return tree_difference;
825
0
}
826
827
static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit,
828
          int nth_parent)
829
0
{
830
0
  struct tree *t1 = repo_get_commit_tree(the_repository, commit);
831
0
  int bloom_ret = -1;
832
833
0
  if (!t1)
834
0
    return 0;
835
836
0
  if (!nth_parent && revs->bloom_keyvecs_nr) {
837
0
    bloom_ret = check_maybe_different_in_bloom_filter(revs, commit);
838
0
    if (!bloom_ret)
839
0
      return 1;
840
0
  }
841
842
0
  tree_difference = REV_TREE_SAME;
843
0
  revs->pruning.flags.has_changes = 0;
844
0
  diff_tree_oid(NULL, &t1->object.oid, "", &revs->pruning);
845
846
0
  if (bloom_ret == 1 && tree_difference == REV_TREE_SAME)
847
0
    count_bloom_filter_false_positive++;
848
849
0
  return tree_difference == REV_TREE_SAME;
850
0
}
851
852
struct treesame_state {
853
  unsigned int nparents;
854
  unsigned char treesame[FLEX_ARRAY];
855
};
856
857
static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
858
0
{
859
0
  unsigned n = commit_list_count(commit->parents);
860
0
  struct treesame_state *st = xcalloc(1, st_add(sizeof(*st), n));
861
0
  st->nparents = n;
862
0
  add_decoration(&revs->treesame, &commit->object, st);
863
0
  return st;
864
0
}
865
866
/*
867
 * Must be called immediately after removing the nth_parent from a commit's
868
 * parent list, if we are maintaining the per-parent treesame[] decoration.
869
 * This does not recalculate the master TREESAME flag - update_treesame()
870
 * should be called to update it after a sequence of treesame[] modifications
871
 * that may have affected it.
872
 */
873
static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
874
0
{
875
0
  struct treesame_state *st;
876
0
  int old_same;
877
878
0
  if (!commit->parents) {
879
    /*
880
     * Have just removed the only parent from a non-merge.
881
     * Different handling, as we lack decoration.
882
     */
883
0
    if (nth_parent != 0)
884
0
      die("compact_treesame %u", nth_parent);
885
0
    old_same = !!(commit->object.flags & TREESAME);
886
0
    if (rev_same_tree_as_empty(revs, commit, nth_parent))
887
0
      commit->object.flags |= TREESAME;
888
0
    else
889
0
      commit->object.flags &= ~TREESAME;
890
0
    return old_same;
891
0
  }
892
893
0
  st = lookup_decoration(&revs->treesame, &commit->object);
894
0
  if (!st || nth_parent >= st->nparents)
895
0
    die("compact_treesame %u", nth_parent);
896
897
0
  old_same = st->treesame[nth_parent];
898
0
  memmove(st->treesame + nth_parent,
899
0
    st->treesame + nth_parent + 1,
900
0
    st->nparents - nth_parent - 1);
901
902
  /*
903
   * If we've just become a non-merge commit, update TREESAME
904
   * immediately, and remove the no-longer-needed decoration.
905
   * If still a merge, defer update until update_treesame().
906
   */
907
0
  if (--st->nparents == 1) {
908
0
    if (commit->parents->next)
909
0
      die("compact_treesame parents mismatch");
910
0
    if (st->treesame[0] && revs->dense)
911
0
      commit->object.flags |= TREESAME;
912
0
    else
913
0
      commit->object.flags &= ~TREESAME;
914
0
    free(add_decoration(&revs->treesame, &commit->object, NULL));
915
0
  }
916
917
0
  return old_same;
918
0
}
919
920
static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
921
0
{
922
0
  if (commit->parents && commit->parents->next) {
923
0
    unsigned n;
924
0
    struct treesame_state *st;
925
0
    struct commit_list *p;
926
0
    unsigned relevant_parents;
927
0
    unsigned relevant_change, irrelevant_change;
928
929
0
    st = lookup_decoration(&revs->treesame, &commit->object);
930
0
    if (!st)
931
0
      die("update_treesame %s", oid_to_hex(&commit->object.oid));
932
0
    relevant_parents = 0;
933
0
    relevant_change = irrelevant_change = 0;
934
0
    for (p = commit->parents, n = 0; p; n++, p = p->next) {
935
0
      if (relevant_commit(p->item)) {
936
0
        relevant_change |= !st->treesame[n];
937
0
        relevant_parents++;
938
0
      } else
939
0
        irrelevant_change |= !st->treesame[n];
940
0
    }
941
0
    if (relevant_parents ? relevant_change : irrelevant_change)
942
0
      commit->object.flags &= ~TREESAME;
943
0
    else
944
0
      commit->object.flags |= TREESAME;
945
0
  }
946
947
0
  return commit->object.flags & TREESAME;
948
0
}
949
950
static inline int limiting_can_increase_treesame(const struct rev_info *revs)
951
0
{
952
  /*
953
   * TREESAME is irrelevant unless prune && dense;
954
   * if simplify_history is set, we can't have a mixture of TREESAME and
955
   *    !TREESAME INTERESTING parents (and we don't have treesame[]
956
   *    decoration anyway);
957
   * if first_parent_only is set, then the TREESAME flag is locked
958
   *    against the first parent (and again we lack treesame[] decoration).
959
   */
960
0
  return revs->prune && revs->dense &&
961
0
         !revs->simplify_history &&
962
0
         !revs->first_parent_only;
963
0
}
964
965
static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
966
0
{
967
0
  struct commit_list **pp, *parent;
968
0
  struct treesame_state *ts = NULL;
969
0
  int relevant_change = 0, irrelevant_change = 0;
970
0
  int relevant_parents, nth_parent;
971
972
  /*
973
   * If we don't do pruning, everything is interesting
974
   */
975
0
  if (!revs->prune)
976
0
    return;
977
978
0
  if (!repo_get_commit_tree(the_repository, commit))
979
0
    return;
980
981
0
  if (!commit->parents) {
982
    /*
983
     * Pretend as if we are comparing ourselves to the
984
     * (non-existent) first parent of this commit object. Even
985
     * though no such parent exists, its changed-path Bloom filter
986
     * (if one exists) is relative to the empty tree, using Bloom
987
     * filters is allowed here.
988
     */
989
0
    if (rev_same_tree_as_empty(revs, commit, 0))
990
0
      commit->object.flags |= TREESAME;
991
0
    return;
992
0
  }
993
994
  /*
995
   * Normal non-merge commit? If we don't want to make the
996
   * history dense, we consider it always to be a change..
997
   */
998
0
  if (!revs->dense && !commit->parents->next)
999
0
    return;
1000
1001
0
  for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
1002
0
       (parent = *pp) != NULL;
1003
0
       pp = &parent->next, nth_parent++) {
1004
0
    struct commit *p = parent->item;
1005
0
    if (relevant_commit(p))
1006
0
      relevant_parents++;
1007
1008
0
    if (nth_parent == 1) {
1009
      /*
1010
       * This our second loop iteration - so we now know
1011
       * we're dealing with a merge.
1012
       *
1013
       * Do not compare with later parents when we care only about
1014
       * the first parent chain, in order to avoid derailing the
1015
       * traversal to follow a side branch that brought everything
1016
       * in the path we are limited to by the pathspec.
1017
       */
1018
0
      if (revs->first_parent_only)
1019
0
        break;
1020
      /*
1021
       * If this will remain a potentially-simplifiable
1022
       * merge, remember per-parent treesame if needed.
1023
       * Initialise the array with the comparison from our
1024
       * first iteration.
1025
       */
1026
0
      if (revs->treesame.name &&
1027
0
          !revs->simplify_history &&
1028
0
          !(commit->object.flags & UNINTERESTING)) {
1029
0
        ts = initialise_treesame(revs, commit);
1030
0
        if (!(irrelevant_change || relevant_change))
1031
0
          ts->treesame[0] = 1;
1032
0
      }
1033
0
    }
1034
0
    if (repo_parse_commit(revs->repo, p) < 0)
1035
0
      die("cannot simplify commit %s (because of %s)",
1036
0
          oid_to_hex(&commit->object.oid),
1037
0
          oid_to_hex(&p->object.oid));
1038
0
    switch (rev_compare_tree(revs, p, commit, nth_parent)) {
1039
0
    case REV_TREE_SAME:
1040
0
      if (!revs->simplify_history || !relevant_commit(p)) {
1041
        /* Even if a merge with an uninteresting
1042
         * side branch brought the entire change
1043
         * we are interested in, we do not want
1044
         * to lose the other branches of this
1045
         * merge, so we just keep going.
1046
         */
1047
0
        if (ts)
1048
0
          ts->treesame[nth_parent] = 1;
1049
0
        continue;
1050
0
      }
1051
1052
0
      commit_list_free(parent->next);
1053
0
      parent->next = NULL;
1054
0
      while (commit->parents != parent)
1055
0
        pop_commit(&commit->parents);
1056
0
      commit->parents = parent;
1057
1058
      /*
1059
       * A merge commit is a "diversion" if it is not
1060
       * TREESAME to its first parent but is TREESAME
1061
       * to a later parent. In the simplified history,
1062
       * we "divert" the history walk to the later
1063
       * parent. These commits are shown when "show_pulls"
1064
       * is enabled, so do not mark the object as
1065
       * TREESAME here.
1066
       */
1067
0
      if (!revs->show_pulls || !nth_parent)
1068
0
        commit->object.flags |= TREESAME;
1069
1070
0
      return;
1071
1072
0
    case REV_TREE_NEW:
1073
0
      if (revs->remove_empty_trees &&
1074
0
          rev_same_tree_as_empty(revs, p, nth_parent)) {
1075
        /* We are adding all the specified
1076
         * paths from this parent, so the
1077
         * history beyond this parent is not
1078
         * interesting.  Remove its parents
1079
         * (they are grandparents for us).
1080
         * IOW, we pretend this parent is a
1081
         * "root" commit.
1082
         */
1083
0
        if (repo_parse_commit(revs->repo, p) < 0)
1084
0
          die("cannot simplify commit %s (invalid %s)",
1085
0
              oid_to_hex(&commit->object.oid),
1086
0
              oid_to_hex(&p->object.oid));
1087
0
        commit_list_free(p->parents);
1088
0
        p->parents = NULL;
1089
0
      }
1090
    /* fallthrough */
1091
0
    case REV_TREE_OLD:
1092
0
    case REV_TREE_DIFFERENT:
1093
0
      if (relevant_commit(p))
1094
0
        relevant_change = 1;
1095
0
      else
1096
0
        irrelevant_change = 1;
1097
1098
0
      if (!nth_parent)
1099
0
        commit->object.flags |= PULL_MERGE;
1100
1101
0
      continue;
1102
0
    }
1103
0
    die("bad tree compare for commit %s", oid_to_hex(&commit->object.oid));
1104
0
  }
1105
1106
  /*
1107
   * TREESAME is straightforward for single-parent commits. For merge
1108
   * commits, it is most useful to define it so that "irrelevant"
1109
   * parents cannot make us !TREESAME - if we have any relevant
1110
   * parents, then we only consider TREESAMEness with respect to them,
1111
   * allowing irrelevant merges from uninteresting branches to be
1112
   * simplified away. Only if we have only irrelevant parents do we
1113
   * base TREESAME on them. Note that this logic is replicated in
1114
   * update_treesame, which should be kept in sync.
1115
   */
1116
0
  if (relevant_parents ? !relevant_change : !irrelevant_change)
1117
0
    commit->object.flags |= TREESAME;
1118
0
}
1119
1120
static int process_parents(struct rev_info *revs, struct commit *commit,
1121
         struct prio_queue *queue)
1122
0
{
1123
0
  struct commit_list *parent = commit->parents;
1124
0
  unsigned pass_flags;
1125
1126
0
  if (commit->object.flags & ADDED)
1127
0
    return 0;
1128
0
  if (revs->do_not_die_on_missing_objects &&
1129
0
      oidset_contains(&revs->missing_commits, &commit->object.oid))
1130
0
    return 0;
1131
0
  commit->object.flags |= ADDED;
1132
1133
0
  if (revs->include_check &&
1134
0
      !revs->include_check(commit, revs->include_check_data))
1135
0
    return 0;
1136
1137
  /*
1138
   * If the commit is uninteresting, don't try to
1139
   * prune parents - we want the maximal uninteresting
1140
   * set.
1141
   *
1142
   * Normally we haven't parsed the parent
1143
   * yet, so we won't have a parent of a parent
1144
   * here. However, it may turn out that we've
1145
   * reached this commit some other way (where it
1146
   * wasn't uninteresting), in which case we need
1147
   * to mark its parents recursively too..
1148
   */
1149
0
  if (commit->object.flags & UNINTERESTING) {
1150
0
    while (parent) {
1151
0
      struct commit *p = parent->item;
1152
0
      parent = parent->next;
1153
0
      if (p)
1154
0
        p->object.flags |= UNINTERESTING |
1155
0
               CHILD_VISITED;
1156
0
      if (repo_parse_commit_gently(revs->repo, p, 1) < 0) {
1157
0
        if (revs->exclude_first_parent_only)
1158
0
          break;
1159
0
        continue;
1160
0
      }
1161
0
      if (p->parents)
1162
0
        mark_parents_uninteresting(revs, p);
1163
0
      if (p->object.flags & SEEN) {
1164
0
        if (revs->exclude_first_parent_only)
1165
0
          break;
1166
0
        continue;
1167
0
      }
1168
0
      p->object.flags |= (SEEN | NOT_USER_GIVEN);
1169
0
      if (queue)
1170
0
        prio_queue_put(queue, p);
1171
0
      if (revs->exclude_first_parent_only)
1172
0
        break;
1173
0
    }
1174
0
    return 0;
1175
0
  }
1176
1177
  /*
1178
   * Ok, the commit wasn't uninteresting. Try to
1179
   * simplify the commit history and find the parent
1180
   * that has no differences in the path set if one exists.
1181
   */
1182
0
  try_to_simplify_commit(revs, commit);
1183
1184
0
  if (revs->no_walk)
1185
0
    return 0;
1186
1187
0
  pass_flags = (commit->object.flags & (SYMMETRIC_LEFT | ANCESTRY_PATH));
1188
1189
0
  for (parent = commit->parents; parent; parent = parent->next) {
1190
0
    struct commit *p = parent->item;
1191
0
    int gently = revs->ignore_missing_links ||
1192
0
           revs->exclude_promisor_objects ||
1193
0
           revs->do_not_die_on_missing_objects;
1194
0
    if (repo_parse_commit_gently(revs->repo, p, gently) < 0) {
1195
0
      if (revs->exclude_promisor_objects &&
1196
0
          is_promisor_object(revs->repo, &p->object.oid)) {
1197
0
        if (revs->first_parent_only)
1198
0
          break;
1199
0
        continue;
1200
0
      }
1201
1202
0
      if (revs->do_not_die_on_missing_objects)
1203
0
        oidset_insert(&revs->missing_commits, &p->object.oid);
1204
0
      else
1205
0
        return -1; /* corrupt repository */
1206
0
    }
1207
0
    if (revs->sources) {
1208
0
      char **slot = revision_sources_at(revs->sources, p);
1209
1210
0
      if (!*slot)
1211
0
        *slot = *revision_sources_at(revs->sources, commit);
1212
0
    }
1213
0
    p->object.flags |= pass_flags | CHILD_VISITED;
1214
0
    if (!(p->object.flags & SEEN)) {
1215
0
      p->object.flags |= (SEEN | NOT_USER_GIVEN);
1216
0
      if (queue)
1217
0
        prio_queue_put(queue, p);
1218
0
    }
1219
0
    if (revs->first_parent_only)
1220
0
      break;
1221
0
  }
1222
0
  return 0;
1223
0
}
1224
1225
static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
1226
0
{
1227
0
  struct commit_list *p;
1228
0
  int left_count = 0, right_count = 0;
1229
0
  int left_first;
1230
0
  struct patch_ids ids;
1231
0
  unsigned cherry_flag;
1232
1233
  /* First count the commits on the left and on the right */
1234
0
  for (p = list; p; p = p->next) {
1235
0
    struct commit *commit = p->item;
1236
0
    unsigned flags = commit->object.flags;
1237
0
    if (flags & BOUNDARY)
1238
0
      ;
1239
0
    else if (flags & SYMMETRIC_LEFT)
1240
0
      left_count++;
1241
0
    else
1242
0
      right_count++;
1243
0
  }
1244
1245
0
  if (!left_count || !right_count)
1246
0
    return;
1247
1248
0
  left_first = left_count < right_count;
1249
0
  init_patch_ids(revs->repo, &ids);
1250
0
  ids.diffopts.pathspec = revs->diffopt.pathspec;
1251
1252
  /* Compute patch-ids for one side */
1253
0
  for (p = list; p; p = p->next) {
1254
0
    struct commit *commit = p->item;
1255
0
    unsigned flags = commit->object.flags;
1256
1257
0
    if (flags & BOUNDARY)
1258
0
      continue;
1259
    /*
1260
     * If we have fewer left, left_first is set and we omit
1261
     * commits on the right branch in this loop.  If we have
1262
     * fewer right, we skip the left ones.
1263
     */
1264
0
    if (left_first != !!(flags & SYMMETRIC_LEFT))
1265
0
      continue;
1266
0
    add_commit_patch_id(commit, &ids);
1267
0
  }
1268
1269
  /* either cherry_mark or cherry_pick are true */
1270
0
  cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
1271
1272
  /* Check the other side */
1273
0
  for (p = list; p; p = p->next) {
1274
0
    struct commit *commit = p->item;
1275
0
    struct patch_id *id;
1276
0
    unsigned flags = commit->object.flags;
1277
1278
0
    if (flags & BOUNDARY)
1279
0
      continue;
1280
    /*
1281
     * If we have fewer left, left_first is set and we omit
1282
     * commits on the left branch in this loop.
1283
     */
1284
0
    if (left_first == !!(flags & SYMMETRIC_LEFT))
1285
0
      continue;
1286
1287
    /*
1288
     * Have we seen the same patch id?
1289
     */
1290
0
    id = patch_id_iter_first(commit, &ids);
1291
0
    if (!id)
1292
0
      continue;
1293
1294
0
    commit->object.flags |= cherry_flag;
1295
0
    do {
1296
0
      id->commit->object.flags |= cherry_flag;
1297
0
    } while ((id = patch_id_iter_next(id, &ids)));
1298
0
  }
1299
1300
0
  free_patch_ids(&ids);
1301
0
}
1302
1303
/* How many extra uninteresting commits we want to see.. */
1304
0
#define SLOP 5
1305
1306
static int still_interesting(struct prio_queue *src, timestamp_t date, int slop,
1307
           struct commit **interesting_cache)
1308
0
{
1309
  /*
1310
   * Since src is sorted by date, it is enough to peek at the
1311
   * first entry to compare dates.  No entry at all means done.
1312
   */
1313
0
  struct commit *commit = prio_queue_peek(src);
1314
0
  if (!commit)
1315
0
    return 0;
1316
0
  if (date <= commit->date)
1317
0
    return SLOP;
1318
1319
  /*
1320
   * Does the source list still have interesting commits in
1321
   * it? Definitely not done..
1322
   */
1323
0
  if (!everybody_uninteresting(src, interesting_cache))
1324
0
    return SLOP;
1325
1326
  /* Ok, we're closing in.. */
1327
0
  return slop-1;
1328
0
}
1329
1330
/*
1331
 * "rev-list --ancestry-path=C_0 [--ancestry-path=C_1 ...] A..B"
1332
 * computes commits that are ancestors of B but not ancestors of A but
1333
 * further limits the result to those that have any of C in their
1334
 * ancestry path (i.e. are either ancestors of any of C, descendants
1335
 * of any of C, or are any of C). If --ancestry-path is specified with
1336
 * no commit, we use all bottom commits for C.
1337
 *
1338
 * Before this function is called, ancestors of C will have already
1339
 * been marked with ANCESTRY_PATH previously.
1340
 *
1341
 * This takes the list of bottom commits and the result of "A..B"
1342
 * without --ancestry-path, and limits the latter further to the ones
1343
 * that have any of C in their ancestry path. Since the ancestors of C
1344
 * have already been marked (a prerequisite of this function), we just
1345
 * need to mark the descendants, then exclude any commit that does not
1346
 * have any of these marks.
1347
 */
1348
static void limit_to_ancestry(struct commit_list *bottoms, struct commit_list *list)
1349
0
{
1350
0
  struct commit_list *p;
1351
0
  struct commit_list *rlist = NULL;
1352
0
  int made_progress;
1353
1354
  /*
1355
   * Reverse the list so that it will be likely that we would
1356
   * process parents before children.
1357
   */
1358
0
  for (p = list; p; p = p->next)
1359
0
    commit_list_insert(p->item, &rlist);
1360
1361
0
  for (p = bottoms; p; p = p->next)
1362
0
    p->item->object.flags |= TMP_MARK;
1363
1364
  /*
1365
   * Mark the ones that can reach bottom commits in "list",
1366
   * in a bottom-up fashion.
1367
   */
1368
0
  do {
1369
0
    made_progress = 0;
1370
0
    for (p = rlist; p; p = p->next) {
1371
0
      struct commit *c = p->item;
1372
0
      struct commit_list *parents;
1373
0
      if (c->object.flags & (TMP_MARK | UNINTERESTING))
1374
0
        continue;
1375
0
      for (parents = c->parents;
1376
0
           parents;
1377
0
           parents = parents->next) {
1378
0
        if (!(parents->item->object.flags & TMP_MARK))
1379
0
          continue;
1380
0
        c->object.flags |= TMP_MARK;
1381
0
        made_progress = 1;
1382
0
        break;
1383
0
      }
1384
0
    }
1385
0
  } while (made_progress);
1386
1387
  /*
1388
   * NEEDSWORK: decide if we want to remove parents that are
1389
   * not marked with TMP_MARK from commit->parents for commits
1390
   * in the resulting list.  We may not want to do that, though.
1391
   */
1392
1393
  /*
1394
   * The ones that are not marked with either TMP_MARK or
1395
   * ANCESTRY_PATH are uninteresting
1396
   */
1397
0
  for (p = list; p; p = p->next) {
1398
0
    struct commit *c = p->item;
1399
0
    if (c->object.flags & (TMP_MARK | ANCESTRY_PATH))
1400
0
      continue;
1401
0
    c->object.flags |= UNINTERESTING;
1402
0
  }
1403
1404
  /* We are done with TMP_MARK and ANCESTRY_PATH */
1405
0
  for (p = list; p; p = p->next)
1406
0
    p->item->object.flags &= ~(TMP_MARK | ANCESTRY_PATH);
1407
0
  for (p = bottoms; p; p = p->next)
1408
0
    p->item->object.flags &= ~(TMP_MARK | ANCESTRY_PATH);
1409
0
  commit_list_free(rlist);
1410
0
}
1411
1412
/*
1413
 * Before walking the history, add the set of "negative" refs the
1414
 * caller has asked to exclude to the bottom list.
1415
 *
1416
 * This is used to compute "rev-list --ancestry-path A..B", as we need
1417
 * to filter the result of "A..B" further to the ones that can actually
1418
 * reach A.
1419
 */
1420
static void collect_bottom_commits(struct commit_list *list,
1421
           struct commit_list **bottom)
1422
0
{
1423
0
  struct commit_list *elem;
1424
0
  for (elem = list; elem; elem = elem->next)
1425
0
    if (elem->item->object.flags & BOTTOM)
1426
0
      commit_list_insert(elem->item, bottom);
1427
0
}
1428
1429
/* Assumes either left_only or right_only is set */
1430
static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1431
0
{
1432
0
  struct commit_list *p;
1433
1434
0
  for (p = list; p; p = p->next) {
1435
0
    struct commit *commit = p->item;
1436
1437
0
    if (revs->right_only) {
1438
0
      if (commit->object.flags & SYMMETRIC_LEFT)
1439
0
        commit->object.flags |= SHOWN;
1440
0
    } else  /* revs->left_only is set */
1441
0
      if (!(commit->object.flags & SYMMETRIC_LEFT))
1442
0
        commit->object.flags |= SHOWN;
1443
0
  }
1444
0
}
1445
1446
static int limit_list(struct rev_info *revs)
1447
0
{
1448
0
  int slop = SLOP;
1449
0
  timestamp_t date = TIME_MAX;
1450
0
  struct commit_list *original_list = revs->commits;
1451
0
  struct commit_list *newlist = NULL;
1452
0
  struct commit_list **p = &newlist;
1453
0
  struct commit *commit, *interesting_cache = NULL;
1454
0
  struct prio_queue queue = { .compare = compare_commits_by_commit_date };
1455
1456
0
  if (revs->ancestry_path_implicit_bottoms) {
1457
0
    collect_bottom_commits(original_list,
1458
0
               &revs->ancestry_path_bottoms);
1459
0
    if (!revs->ancestry_path_bottoms)
1460
0
      die("--ancestry-path given but there are no bottom commits");
1461
0
  }
1462
1463
0
  while (original_list) {
1464
0
    struct commit *commit = pop_commit(&original_list);
1465
0
    prio_queue_put(&queue, commit);
1466
0
  }
1467
1468
0
  while ((commit = prio_queue_get(&queue))) {
1469
0
    struct object *obj = &commit->object;
1470
1471
0
    if (commit == interesting_cache)
1472
0
      interesting_cache = NULL;
1473
1474
0
    if (revs->max_age != -1 && (commit->date < revs->max_age))
1475
0
      obj->flags |= UNINTERESTING;
1476
0
    if (process_parents(revs, commit, &queue) < 0) {
1477
0
      clear_prio_queue(&queue);
1478
0
      return -1;
1479
0
    }
1480
0
    if (obj->flags & UNINTERESTING) {
1481
0
      mark_parents_uninteresting(revs, commit);
1482
0
      slop = still_interesting(&queue, date, slop, &interesting_cache);
1483
0
      if (slop)
1484
0
        continue;
1485
0
      break;
1486
0
    }
1487
0
    if (revs->min_age != -1 && (commit->date > revs->min_age) &&
1488
0
        !revs->line_level_traverse)
1489
0
      continue;
1490
0
    if (revs->max_age_as_filter != -1 &&
1491
0
      (commit->date < revs->max_age_as_filter) && !revs->line_level_traverse)
1492
0
      continue;
1493
0
    date = commit->date;
1494
0
    p = &commit_list_insert(commit, p)->next;
1495
0
  }
1496
0
  if (revs->cherry_pick || revs->cherry_mark)
1497
0
    cherry_pick_list(newlist, revs);
1498
1499
0
  if (revs->left_only || revs->right_only)
1500
0
    limit_left_right(newlist, revs);
1501
1502
0
  if (revs->ancestry_path)
1503
0
    limit_to_ancestry(revs->ancestry_path_bottoms, newlist);
1504
1505
  /*
1506
   * Check if any commits have become TREESAME by some of their parents
1507
   * becoming UNINTERESTING.
1508
   */
1509
0
  if (limiting_can_increase_treesame(revs)) {
1510
0
    struct commit_list *list = NULL;
1511
0
    for (list = newlist; list; list = list->next) {
1512
0
      struct commit *c = list->item;
1513
0
      if (c->object.flags & (UNINTERESTING | TREESAME))
1514
0
        continue;
1515
0
      update_treesame(revs, c);
1516
0
    }
1517
0
  }
1518
1519
0
  clear_prio_queue(&queue);
1520
0
  revs->commits = newlist;
1521
0
  return 0;
1522
0
}
1523
1524
/*
1525
 * Add an entry to refs->cmdline with the specified information.
1526
 * *name is copied.
1527
 */
1528
static void add_rev_cmdline(struct rev_info *revs,
1529
          struct object *item,
1530
          const char *name,
1531
          int whence,
1532
          unsigned flags)
1533
0
{
1534
0
  struct rev_cmdline_info *info = &revs->cmdline;
1535
0
  unsigned int nr = info->nr;
1536
1537
0
  ALLOC_GROW(info->rev, nr + 1, info->alloc);
1538
0
  info->rev[nr].item = item;
1539
0
  info->rev[nr].name = xstrdup(name);
1540
0
  info->rev[nr].whence = whence;
1541
0
  info->rev[nr].flags = flags;
1542
0
  info->nr++;
1543
0
}
1544
1545
static void add_rev_cmdline_list(struct rev_info *revs,
1546
         struct commit_list *commit_list,
1547
         int whence,
1548
         unsigned flags)
1549
0
{
1550
0
  while (commit_list) {
1551
0
    struct object *object = &commit_list->item->object;
1552
0
    add_rev_cmdline(revs, object, oid_to_hex(&object->oid),
1553
0
        whence, flags);
1554
0
    commit_list = commit_list->next;
1555
0
  }
1556
0
}
1557
1558
int ref_excluded(const struct ref_exclusions *exclusions, const char *path)
1559
0
{
1560
0
  const char *stripped_path = strip_namespace(path);
1561
0
  struct string_list_item *item;
1562
1563
0
  for_each_string_list_item(item, &exclusions->excluded_refs) {
1564
0
    if (!wildmatch(item->string, path, 0))
1565
0
      return 1;
1566
0
  }
1567
1568
0
  if (ref_is_hidden(stripped_path, path, &exclusions->hidden_refs))
1569
0
    return 1;
1570
1571
0
  return 0;
1572
0
}
1573
1574
void init_ref_exclusions(struct ref_exclusions *exclusions)
1575
0
{
1576
0
  struct ref_exclusions blank = REF_EXCLUSIONS_INIT;
1577
0
  memcpy(exclusions, &blank, sizeof(*exclusions));
1578
0
}
1579
1580
void clear_ref_exclusions(struct ref_exclusions *exclusions)
1581
0
{
1582
0
  string_list_clear(&exclusions->excluded_refs, 0);
1583
0
  strvec_clear(&exclusions->hidden_refs);
1584
0
  exclusions->hidden_refs_configured = 0;
1585
0
}
1586
1587
void add_ref_exclusion(struct ref_exclusions *exclusions, const char *exclude)
1588
0
{
1589
0
  string_list_append(&exclusions->excluded_refs, exclude);
1590
0
}
1591
1592
struct exclude_hidden_refs_cb {
1593
  struct ref_exclusions *exclusions;
1594
  const char *section;
1595
};
1596
1597
static int hide_refs_config(const char *var, const char *value,
1598
          const struct config_context *ctx UNUSED,
1599
          void *cb_data)
1600
0
{
1601
0
  struct exclude_hidden_refs_cb *cb = cb_data;
1602
0
  cb->exclusions->hidden_refs_configured = 1;
1603
0
  return parse_hide_refs_config(var, value, cb->section,
1604
0
              &cb->exclusions->hidden_refs);
1605
0
}
1606
1607
void exclude_hidden_refs(struct ref_exclusions *exclusions, const char *section)
1608
0
{
1609
0
  struct exclude_hidden_refs_cb cb;
1610
1611
0
  if (strcmp(section, "fetch") && strcmp(section, "receive") &&
1612
0
      strcmp(section, "uploadpack"))
1613
0
    die(_("unsupported section for hidden refs: %s"), section);
1614
1615
0
  if (exclusions->hidden_refs_configured)
1616
0
    die(_("--exclude-hidden= passed more than once"));
1617
1618
0
  cb.exclusions = exclusions;
1619
0
  cb.section = section;
1620
1621
0
  repo_config(the_repository, hide_refs_config, &cb);
1622
0
}
1623
1624
struct all_refs_cb {
1625
  int all_flags;
1626
  int warned_bad_reflog;
1627
  struct rev_info *all_revs;
1628
  const char *name_for_errormsg;
1629
  struct worktree *wt;
1630
};
1631
1632
static int handle_one_ref(const struct reference *ref, void *cb_data)
1633
0
{
1634
0
  struct all_refs_cb *cb = cb_data;
1635
0
  struct object *object;
1636
1637
0
  if (ref_excluded(&cb->all_revs->ref_excludes, ref->name))
1638
0
      return 0;
1639
1640
0
  object = get_reference(cb->all_revs, ref->name, ref->oid, cb->all_flags);
1641
0
  add_rev_cmdline(cb->all_revs, object, ref->name, REV_CMD_REF, cb->all_flags);
1642
0
  add_pending_object(cb->all_revs, object, ref->name);
1643
0
  return 0;
1644
0
}
1645
1646
static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1647
  unsigned flags)
1648
0
{
1649
0
  cb->all_revs = revs;
1650
0
  cb->all_flags = flags;
1651
0
  revs->rev_input_given = 1;
1652
0
  cb->wt = NULL;
1653
0
}
1654
1655
static void handle_refs(struct ref_store *refs,
1656
      struct rev_info *revs, unsigned flags,
1657
      int (*for_each)(struct ref_store *, refs_for_each_cb, void *))
1658
0
{
1659
0
  struct all_refs_cb cb;
1660
1661
0
  if (!refs) {
1662
    /* this could happen with uninitialized submodules */
1663
0
    return;
1664
0
  }
1665
1666
0
  init_all_refs_cb(&cb, revs, flags);
1667
0
  for_each(refs, handle_one_ref, &cb);
1668
0
}
1669
1670
static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
1671
0
{
1672
0
  struct all_refs_cb *cb = cb_data;
1673
0
  if (!is_null_oid(oid)) {
1674
0
    struct object *o = parse_object(cb->all_revs->repo, oid);
1675
0
    if (o) {
1676
0
      o->flags |= cb->all_flags;
1677
      /* ??? CMDLINEFLAGS ??? */
1678
0
      add_pending_object(cb->all_revs, o, "");
1679
0
    }
1680
0
    else if (!cb->warned_bad_reflog) {
1681
0
      warning("reflog of '%s' references pruned commits",
1682
0
        cb->name_for_errormsg);
1683
0
      cb->warned_bad_reflog = 1;
1684
0
    }
1685
0
  }
1686
0
}
1687
1688
static int handle_one_reflog_ent(const char *refname UNUSED,
1689
         struct object_id *ooid, struct object_id *noid,
1690
         const char *email UNUSED,
1691
         timestamp_t timestamp UNUSED,
1692
         int tz UNUSED,
1693
         const char *message UNUSED,
1694
         void *cb_data)
1695
0
{
1696
0
  handle_one_reflog_commit(ooid, cb_data);
1697
0
  handle_one_reflog_commit(noid, cb_data);
1698
0
  return 0;
1699
0
}
1700
1701
static int handle_one_reflog(const char *refname_in_wt, void *cb_data)
1702
0
{
1703
0
  struct all_refs_cb *cb = cb_data;
1704
0
  struct strbuf refname = STRBUF_INIT;
1705
1706
0
  cb->warned_bad_reflog = 0;
1707
0
  strbuf_worktree_ref(cb->wt, &refname, refname_in_wt);
1708
0
  cb->name_for_errormsg = refname.buf;
1709
0
  refs_for_each_reflog_ent(get_main_ref_store(the_repository),
1710
0
         refname.buf,
1711
0
         handle_one_reflog_ent, cb_data);
1712
0
  strbuf_release(&refname);
1713
0
  return 0;
1714
0
}
1715
1716
static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
1717
0
{
1718
0
  struct worktree **worktrees, **p;
1719
1720
0
  worktrees = get_worktrees(the_repository);
1721
0
  for (p = worktrees; *p; p++) {
1722
0
    struct worktree *wt = *p;
1723
1724
0
    if (wt->is_current)
1725
0
      continue;
1726
1727
0
    cb->wt = wt;
1728
0
    refs_for_each_reflog(get_worktree_ref_store(wt),
1729
0
             handle_one_reflog,
1730
0
             cb);
1731
0
  }
1732
0
  free_worktrees(worktrees);
1733
0
}
1734
1735
void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
1736
0
{
1737
0
  struct all_refs_cb cb;
1738
1739
0
  cb.all_revs = revs;
1740
0
  cb.all_flags = flags;
1741
0
  cb.wt = NULL;
1742
0
  refs_for_each_reflog(get_main_ref_store(the_repository),
1743
0
           handle_one_reflog, &cb);
1744
1745
0
  if (!revs->single_worktree)
1746
0
    add_other_reflogs_to_pending(&cb);
1747
0
}
1748
1749
static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
1750
         struct strbuf *path, unsigned int flags)
1751
0
{
1752
0
  size_t baselen = path->len;
1753
0
  int i;
1754
1755
0
  if (it->entry_count >= 0) {
1756
0
    struct tree *tree = lookup_tree(revs->repo, &it->oid);
1757
0
    tree->object.flags |= flags;
1758
0
    add_pending_object_with_path(revs, &tree->object, "",
1759
0
               040000, path->buf);
1760
0
  }
1761
1762
0
  for (i = 0; i < it->subtree_nr; i++) {
1763
0
    struct cache_tree_sub *sub = it->down[i];
1764
0
    strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
1765
0
    add_cache_tree(sub->cache_tree, revs, path, flags);
1766
0
    strbuf_setlen(path, baselen);
1767
0
  }
1768
1769
0
}
1770
1771
static void add_resolve_undo_to_pending(struct index_state *istate, struct rev_info *revs)
1772
0
{
1773
0
  struct string_list_item *item;
1774
0
  struct string_list *resolve_undo = istate->resolve_undo;
1775
1776
0
  if (!resolve_undo)
1777
0
    return;
1778
1779
0
  for_each_string_list_item(item, resolve_undo) {
1780
0
    const char *path = item->string;
1781
0
    struct resolve_undo_info *ru = item->util;
1782
0
    int i;
1783
1784
0
    if (!ru)
1785
0
      continue;
1786
0
    for (i = 0; i < 3; i++) {
1787
0
      struct blob *blob;
1788
1789
0
      if (!ru->mode[i] || !S_ISREG(ru->mode[i]))
1790
0
        continue;
1791
1792
0
      blob = lookup_blob(revs->repo, &ru->oid[i]);
1793
0
      if (!blob) {
1794
0
        warning(_("resolve-undo records `%s` which is missing"),
1795
0
          oid_to_hex(&ru->oid[i]));
1796
0
        continue;
1797
0
      }
1798
0
      add_pending_object_with_path(revs, &blob->object, "",
1799
0
                 ru->mode[i], path);
1800
0
    }
1801
0
  }
1802
0
}
1803
1804
static void do_add_index_objects_to_pending(struct rev_info *revs,
1805
              struct index_state *istate,
1806
              unsigned int flags)
1807
0
{
1808
0
  int i;
1809
1810
  /* TODO: audit for interaction with sparse-index. */
1811
0
  ensure_full_index(istate);
1812
0
  for (i = 0; i < istate->cache_nr; i++) {
1813
0
    struct cache_entry *ce = istate->cache[i];
1814
0
    struct blob *blob;
1815
1816
0
    if (S_ISGITLINK(ce->ce_mode))
1817
0
      continue;
1818
1819
0
    blob = lookup_blob(revs->repo, &ce->oid);
1820
0
    if (!blob)
1821
0
      die("unable to add index blob to traversal");
1822
0
    blob->object.flags |= flags;
1823
0
    add_pending_object_with_path(revs, &blob->object, "",
1824
0
               ce->ce_mode, ce->name);
1825
0
  }
1826
1827
0
  if (istate->cache_tree) {
1828
0
    struct strbuf path = STRBUF_INIT;
1829
0
    add_cache_tree(istate->cache_tree, revs, &path, flags);
1830
0
    strbuf_release(&path);
1831
0
  }
1832
1833
0
  add_resolve_undo_to_pending(istate, revs);
1834
0
}
1835
1836
void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
1837
0
{
1838
0
  struct worktree **worktrees, **p;
1839
1840
0
  repo_read_index(revs->repo);
1841
0
  do_add_index_objects_to_pending(revs, revs->repo->index, flags);
1842
1843
0
  if (revs->single_worktree)
1844
0
    return;
1845
1846
0
  worktrees = get_worktrees(the_repository);
1847
0
  for (p = worktrees; *p; p++) {
1848
0
    struct worktree *wt = *p;
1849
0
    struct index_state istate = INDEX_STATE_INIT(revs->repo);
1850
0
    char *wt_gitdir;
1851
1852
0
    if (wt->is_current)
1853
0
      continue; /* current index already taken care of */
1854
1855
0
    wt_gitdir = get_worktree_git_dir(wt);
1856
1857
0
    if (read_index_from(&istate,
1858
0
            worktree_git_path(wt, "index"),
1859
0
            wt_gitdir) > 0)
1860
0
      do_add_index_objects_to_pending(revs, &istate, flags);
1861
1862
0
    discard_index(&istate);
1863
0
    free(wt_gitdir);
1864
0
  }
1865
0
  free_worktrees(worktrees);
1866
0
}
1867
1868
struct add_alternate_refs_data {
1869
  struct rev_info *revs;
1870
  unsigned int flags;
1871
};
1872
1873
static void add_one_alternate_ref(const struct object_id *oid,
1874
          void *vdata)
1875
0
{
1876
0
  const char *name = ".alternate";
1877
0
  struct add_alternate_refs_data *data = vdata;
1878
0
  struct object *obj;
1879
1880
0
  obj = get_reference(data->revs, name, oid, data->flags);
1881
0
  add_rev_cmdline(data->revs, obj, name, REV_CMD_REV, data->flags);
1882
0
  add_pending_object(data->revs, obj, name);
1883
0
}
1884
1885
static void add_alternate_refs_to_pending(struct rev_info *revs,
1886
            unsigned int flags)
1887
0
{
1888
0
  struct add_alternate_refs_data data;
1889
0
  data.revs = revs;
1890
0
  data.flags = flags;
1891
0
  odb_for_each_alternate_ref(the_repository->objects,
1892
0
           add_one_alternate_ref, &data);
1893
0
}
1894
1895
static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
1896
          int exclude_parent)
1897
0
{
1898
0
  struct object_id oid;
1899
0
  struct object *it;
1900
0
  struct commit *commit;
1901
0
  struct commit_list *parents;
1902
0
  int parent_number;
1903
0
  const char *arg = arg_;
1904
1905
0
  if (*arg == '^') {
1906
0
    flags ^= UNINTERESTING | BOTTOM;
1907
0
    arg++;
1908
0
  }
1909
0
  if (repo_get_oid_committish(the_repository, arg, &oid))
1910
0
    return 0;
1911
0
  while (1) {
1912
0
    it = get_reference(revs, arg, &oid, 0);
1913
0
    if (!it) {
1914
0
      if (revs->ignore_missing)
1915
0
        return 0;
1916
0
      if (revs->do_not_die_on_missing_objects)
1917
0
        return 0;
1918
0
      return -1;
1919
0
    }
1920
0
    if (it->type != OBJ_TAG)
1921
0
      break;
1922
0
    if (!((struct tag*)it)->tagged)
1923
0
      return 0;
1924
0
    oidcpy(&oid, &((struct tag*)it)->tagged->oid);
1925
0
  }
1926
0
  if (it->type != OBJ_COMMIT)
1927
0
    return 0;
1928
0
  commit = (struct commit *)it;
1929
0
  if (exclude_parent &&
1930
0
      exclude_parent > commit_list_count(commit->parents))
1931
0
    return 0;
1932
0
  for (parents = commit->parents, parent_number = 1;
1933
0
       parents;
1934
0
       parents = parents->next, parent_number++) {
1935
0
    if (exclude_parent && parent_number != exclude_parent)
1936
0
      continue;
1937
1938
0
    it = &parents->item->object;
1939
0
    it->flags |= flags;
1940
0
    add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1941
0
    add_pending_object(revs, it, arg);
1942
0
  }
1943
0
  return 1;
1944
0
}
1945
1946
void repo_init_revisions(struct repository *r,
1947
       struct rev_info *revs,
1948
       const char *prefix)
1949
0
{
1950
0
  struct rev_info blank = REV_INFO_INIT;
1951
0
  memcpy(revs, &blank, sizeof(*revs));
1952
1953
0
  revs->repo = r;
1954
0
  revs->pruning.repo = r;
1955
0
  revs->pruning.add_remove = file_add_remove;
1956
0
  revs->pruning.change = file_change;
1957
0
  revs->pruning.change_fn_data = revs;
1958
0
  revs->prefix = prefix;
1959
1960
0
  grep_init(&revs->grep_filter, revs->repo);
1961
0
  revs->grep_filter.status_only = 1;
1962
1963
0
  repo_diff_setup(revs->repo, &revs->diffopt);
1964
0
  if (prefix && !revs->diffopt.prefix) {
1965
0
    revs->diffopt.prefix = prefix;
1966
0
    revs->diffopt.prefix_length = strlen(prefix);
1967
0
  }
1968
1969
0
  init_display_notes(&revs->notes_opt);
1970
0
  list_objects_filter_init(&revs->filter);
1971
0
  init_ref_exclusions(&revs->ref_excludes);
1972
0
  oidset_init(&revs->missing_commits, 0);
1973
0
}
1974
1975
static void add_pending_commit_list(struct rev_info *revs,
1976
            struct commit_list *commit_list,
1977
            unsigned int flags)
1978
0
{
1979
0
  while (commit_list) {
1980
0
    struct object *object = &commit_list->item->object;
1981
0
    object->flags |= flags;
1982
0
    add_pending_object(revs, object, oid_to_hex(&object->oid));
1983
0
    commit_list = commit_list->next;
1984
0
  }
1985
0
}
1986
1987
static const char *lookup_other_head(struct object_id *oid)
1988
0
{
1989
0
  int i;
1990
0
  static const char *const other_head[] = {
1991
0
    "MERGE_HEAD", "CHERRY_PICK_HEAD", "REVERT_HEAD", "REBASE_HEAD"
1992
0
  };
1993
1994
0
  for (i = 0; i < ARRAY_SIZE(other_head); i++)
1995
0
    if (!refs_read_ref_full(get_main_ref_store(the_repository), other_head[i],
1996
0
          RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
1997
0
          oid, NULL)) {
1998
0
      if (is_null_oid(oid))
1999
0
        die(_("%s exists but is a symbolic ref"), other_head[i]);
2000
0
      return other_head[i];
2001
0
    }
2002
2003
0
  die(_("--merge requires an ongoing merge, cherry-pick, revert or rebase"));
2004
0
}
2005
2006
static void prepare_show_merge(struct rev_info *revs)
2007
0
{
2008
0
  struct commit_list *bases = NULL;
2009
0
  struct commit *head, *other;
2010
0
  struct object_id oid;
2011
0
  const char *other_name;
2012
0
  const char **prune = NULL;
2013
0
  int i, prune_num = 1; /* counting terminating NULL */
2014
0
  struct index_state *istate = revs->repo->index;
2015
2016
0
  if (repo_get_oid(the_repository, "HEAD", &oid))
2017
0
    die("--merge without HEAD?");
2018
0
  head = lookup_commit_or_die(&oid, "HEAD");
2019
0
  other_name = lookup_other_head(&oid);
2020
0
  other = lookup_commit_or_die(&oid, other_name);
2021
0
  add_pending_object(revs, &head->object, "HEAD");
2022
0
  add_pending_object(revs, &other->object, other_name);
2023
0
  if (repo_get_merge_bases(the_repository, head, other, &bases) < 0)
2024
0
    exit(128);
2025
0
  add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
2026
0
  add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
2027
0
  commit_list_free(bases);
2028
0
  head->object.flags |= SYMMETRIC_LEFT;
2029
2030
0
  if (!istate->cache_nr)
2031
0
    repo_read_index(revs->repo);
2032
0
  for (i = 0; i < istate->cache_nr; i++) {
2033
0
    const struct cache_entry *ce = istate->cache[i];
2034
0
    if (!ce_stage(ce))
2035
0
      continue;
2036
0
    if (ce_path_match(istate, ce, &revs->prune_data, NULL)) {
2037
0
      prune_num++;
2038
0
      REALLOC_ARRAY(prune, prune_num);
2039
0
      prune[prune_num-2] = ce->name;
2040
0
      prune[prune_num-1] = NULL;
2041
0
    }
2042
0
    while ((i+1 < istate->cache_nr) &&
2043
0
           ce_same_name(ce, istate->cache[i+1]))
2044
0
      i++;
2045
0
  }
2046
0
  clear_pathspec(&revs->prune_data);
2047
0
  parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
2048
0
           PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
2049
0
  revs->limited = 1;
2050
0
  free(prune);
2051
0
}
2052
2053
static int dotdot_missing(const char *full_name,
2054
        struct rev_info *revs, int symmetric)
2055
0
{
2056
0
  if (revs->ignore_missing)
2057
0
    return 0;
2058
0
  die(symmetric
2059
0
      ? "Invalid symmetric difference expression %s"
2060
0
      : "Invalid revision range %s", full_name);
2061
0
}
2062
2063
static int handle_dotdot_1(const char *a_name, const char *b_name,
2064
         const char *full_name, int symmetric,
2065
         struct rev_info *revs, int flags,
2066
         int cant_be_filename,
2067
         struct object_context *a_oc,
2068
         struct object_context *b_oc)
2069
0
{
2070
0
  struct object_id a_oid, b_oid;
2071
0
  struct object *a_obj, *b_obj;
2072
0
  unsigned int a_flags, b_flags;
2073
0
  unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
2074
0
  unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
2075
2076
0
  if (!*a_name)
2077
0
    a_name = "HEAD";
2078
2079
0
  if (!*b_name)
2080
0
    b_name = "HEAD";
2081
2082
0
  if (get_oid_with_context(revs->repo, a_name, oc_flags, &a_oid, a_oc) ||
2083
0
      get_oid_with_context(revs->repo, b_name, oc_flags, &b_oid, b_oc))
2084
0
    return -1;
2085
2086
0
  if (!cant_be_filename) {
2087
0
    verify_non_filename(the_repository, revs->prefix, full_name);
2088
0
  }
2089
2090
0
  a_obj = parse_object(revs->repo, &a_oid);
2091
0
  b_obj = parse_object(revs->repo, &b_oid);
2092
0
  if (!a_obj || !b_obj)
2093
0
    return dotdot_missing(full_name, revs, symmetric);
2094
2095
0
  if (!symmetric) {
2096
    /* just A..B */
2097
0
    b_flags = flags;
2098
0
    a_flags = flags_exclude;
2099
0
  } else {
2100
    /* A...B -- find merge bases between the two */
2101
0
    struct commit *a, *b;
2102
0
    struct commit_list *exclude = NULL;
2103
2104
0
    a = lookup_commit_reference(revs->repo, &a_obj->oid);
2105
0
    b = lookup_commit_reference(revs->repo, &b_obj->oid);
2106
0
    if (!a || !b)
2107
0
      return dotdot_missing(full_name, revs, symmetric);
2108
2109
0
    if (repo_get_merge_bases(the_repository, a, b, &exclude) < 0) {
2110
0
      commit_list_free(exclude);
2111
0
      return -1;
2112
0
    }
2113
0
    add_rev_cmdline_list(revs, exclude, REV_CMD_MERGE_BASE,
2114
0
             flags_exclude);
2115
0
    add_pending_commit_list(revs, exclude, flags_exclude);
2116
0
    commit_list_free(exclude);
2117
2118
0
    b_flags = flags;
2119
0
    a_flags = flags | SYMMETRIC_LEFT;
2120
0
  }
2121
2122
0
  a_obj->flags |= a_flags;
2123
0
  b_obj->flags |= b_flags;
2124
0
  add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags);
2125
0
  add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags);
2126
0
  add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path);
2127
0
  add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path);
2128
0
  return 0;
2129
0
}
2130
2131
static int handle_dotdot(const char *arg,
2132
       struct rev_info *revs, int flags,
2133
       int cant_be_filename)
2134
0
{
2135
0
  struct object_context a_oc = {0}, b_oc = {0};
2136
0
  const char *dotdot = strstr(arg, "..");
2137
0
  char *tmp;
2138
0
  int symmetric = 0;
2139
0
  int ret;
2140
2141
0
  if (!dotdot)
2142
0
    return -1;
2143
2144
0
  tmp = xmemdupz(arg, dotdot - arg);
2145
0
  dotdot += 2;
2146
0
  if (*dotdot == '.') {
2147
0
    symmetric = 1;
2148
0
    dotdot++;
2149
0
  }
2150
0
  ret = handle_dotdot_1(tmp, dotdot, arg, symmetric, revs, flags,
2151
0
            cant_be_filename, &a_oc, &b_oc);
2152
0
  free(tmp);
2153
2154
0
  object_context_release(&a_oc);
2155
0
  object_context_release(&b_oc);
2156
0
  return ret;
2157
0
}
2158
2159
static int handle_revision_arg_1(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
2160
0
{
2161
0
  struct object_context oc = {0};
2162
0
  const char *mark;
2163
0
  char *arg_minus_at = NULL;
2164
0
  char *arg_minus_excl = NULL;
2165
0
  char *arg_minus_dash = NULL;
2166
0
  struct object *object;
2167
0
  struct object_id oid;
2168
0
  int local_flags;
2169
0
  const char *arg = arg_;
2170
0
  int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
2171
0
  unsigned get_sha1_flags = GET_OID_RECORD_PATH;
2172
0
  int ret;
2173
2174
0
  flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
2175
2176
0
  if (!cant_be_filename && !strcmp(arg, "..")) {
2177
    /*
2178
     * Just ".."?  That is not a range but the
2179
     * pathspec for the parent directory.
2180
     */
2181
0
    ret = -1;
2182
0
    goto out;
2183
0
  }
2184
2185
0
  if (!handle_dotdot(arg, revs, flags, revarg_opt)) {
2186
0
    ret = 0;
2187
0
    goto out;
2188
0
  }
2189
2190
0
  mark = strstr(arg, "^@");
2191
0
  if (mark && !mark[2]) {
2192
0
    arg_minus_at = xmemdupz(arg, mark - arg);
2193
0
    if (add_parents_only(revs, arg_minus_at, flags, 0)) {
2194
0
      ret = 0;
2195
0
      goto out;
2196
0
    }
2197
0
  }
2198
0
  mark = strstr(arg, "^!");
2199
0
  if (mark && !mark[2]) {
2200
0
    arg_minus_excl = xmemdupz(arg, mark - arg);
2201
0
    if (add_parents_only(revs, arg_minus_excl, flags ^ (UNINTERESTING | BOTTOM), 0))
2202
0
      arg = arg_minus_excl;
2203
0
  }
2204
0
  mark = strstr(arg, "^-");
2205
0
  if (mark) {
2206
0
    int exclude_parent = 1;
2207
2208
0
    if (mark[2]) {
2209
0
      if (strtol_i(mark + 2, 10, &exclude_parent) ||
2210
0
          exclude_parent < 1) {
2211
0
        ret = -1;
2212
0
        goto out;
2213
0
      }
2214
0
    }
2215
2216
0
    arg_minus_dash = xmemdupz(arg, mark - arg);
2217
0
    if (add_parents_only(revs, arg_minus_dash, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
2218
0
      arg = arg_minus_dash;
2219
0
  }
2220
2221
0
  local_flags = 0;
2222
0
  if (*arg == '^') {
2223
0
    local_flags = UNINTERESTING | BOTTOM;
2224
0
    arg++;
2225
0
  }
2226
2227
0
  if (revarg_opt & REVARG_COMMITTISH)
2228
0
    get_sha1_flags |= GET_OID_COMMITTISH;
2229
2230
  /*
2231
   * Even if revs->do_not_die_on_missing_objects is set, we
2232
   * should error out if we can't even get an oid, as
2233
   * `--missing=print` should be able to report missing oids.
2234
   */
2235
0
  if (get_oid_with_context(revs->repo, arg, get_sha1_flags, &oid, &oc)) {
2236
0
    ret = revs->ignore_missing ? 0 : -1;
2237
0
    goto out;
2238
0
  }
2239
0
  if (!cant_be_filename)
2240
0
    verify_non_filename(the_repository, revs->prefix, arg);
2241
0
  object = get_reference(revs, arg, &oid, flags ^ local_flags);
2242
0
  if (!object) {
2243
0
    ret = (revs->ignore_missing || revs->do_not_die_on_missing_objects) ? 0 : -1;
2244
0
    goto out;
2245
0
  }
2246
0
  add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
2247
0
  add_pending_object_with_path(revs, object, arg, oc.mode, oc.path);
2248
2249
0
  ret = 0;
2250
2251
0
out:
2252
0
  object_context_release(&oc);
2253
0
  free(arg_minus_at);
2254
0
  free(arg_minus_excl);
2255
0
  free(arg_minus_dash);
2256
0
  return ret;
2257
0
}
2258
2259
int handle_revision_arg(const char *arg, struct rev_info *revs, int flags, unsigned revarg_opt)
2260
0
{
2261
0
  int ret = handle_revision_arg_1(arg, revs, flags, revarg_opt);
2262
0
  if (!ret)
2263
0
    revs->rev_input_given = 1;
2264
0
  return ret;
2265
0
}
2266
2267
static void read_pathspec_from_stdin(struct strbuf *sb,
2268
             struct strvec *prune)
2269
0
{
2270
0
  while (strbuf_getline(sb, stdin) != EOF)
2271
0
    strvec_push(prune, sb->buf);
2272
0
}
2273
2274
static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
2275
0
{
2276
0
  append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
2277
0
}
2278
2279
static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
2280
0
{
2281
0
  append_header_grep_pattern(&revs->grep_filter, field, pattern);
2282
0
}
2283
2284
static void add_message_grep(struct rev_info *revs, const char *pattern)
2285
0
{
2286
0
  add_grep(revs, pattern, GREP_PATTERN_BODY);
2287
0
}
2288
2289
static int parse_count(const char *arg)
2290
0
{
2291
0
  int count;
2292
2293
0
  if (strtol_i(arg, 10, &count) < 0)
2294
0
    die("'%s': not an integer", arg);
2295
0
  return count;
2296
0
}
2297
2298
static timestamp_t parse_age(const char *arg)
2299
0
{
2300
0
  timestamp_t num;
2301
0
  char *p;
2302
2303
0
  errno = 0;
2304
0
  num = parse_timestamp(arg, &p, 10);
2305
0
  if (errno || *p || p == arg)
2306
0
    die("'%s': not a number of seconds since epoch", arg);
2307
0
  return num;
2308
0
}
2309
2310
/*
2311
 * When asked to free argv strings, we should not do so immediately. Some
2312
 * option parsing may have stored a reference to the string (either the whole
2313
 * thing, or a substring inside it). We should keep it valid until the rev_info
2314
 * struct itself is freed.
2315
 *
2316
 * Note that we take a const str for the convenience of callers (who have the
2317
 * usual const argv array, even when opt->free_removed_argv_elements is set).
2318
 * We cast away the const on their behalf.
2319
 */
2320
static void mark_argv_for_free(const struct setup_revision_opt *opt,
2321
             struct rev_info *revs, const char *str)
2322
0
{
2323
0
  if (!opt || !opt->free_removed_argv_elements)
2324
0
    return;
2325
0
  if (!str)
2326
0
    return;
2327
0
  strvec_push_nodup(&revs->argv_to_free, (char *)str);
2328
0
}
2329
2330
static void overwrite_argv(int *argc, const char **argv,
2331
         const char **value,
2332
         const struct setup_revision_opt *opt,
2333
         struct rev_info *revs)
2334
0
{
2335
  /*
2336
   * Detect the case when we are overwriting ourselves. The assignment
2337
   * itself would be a noop either way, but this lets us avoid corner
2338
   * cases around the free() and NULL operations.
2339
   */
2340
0
  if (*value != argv[*argc]) {
2341
0
    mark_argv_for_free(opt, revs, argv[*argc]);
2342
0
    argv[*argc] = *value;
2343
0
    *value = NULL;
2344
0
  }
2345
0
  (*argc)++;
2346
0
}
2347
2348
static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
2349
             int *unkc, const char **unkv,
2350
             const struct setup_revision_opt* opt)
2351
0
{
2352
0
  const char *arg = argv[0];
2353
0
  const char *optarg = NULL;
2354
0
  int argcount;
2355
0
  const unsigned hexsz = the_hash_algo->hexsz;
2356
2357
  /* pseudo revision arguments */
2358
0
  if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
2359
0
      !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
2360
0
      !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
2361
0
      !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
2362
0
      !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
2363
0
      !strcmp(arg, "--indexed-objects") ||
2364
0
      !strcmp(arg, "--alternate-refs") ||
2365
0
      starts_with(arg, "--exclude=") || starts_with(arg, "--exclude-hidden=") ||
2366
0
      starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
2367
0
      starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
2368
0
  {
2369
0
    overwrite_argv(unkc, unkv, &argv[0], opt, revs);
2370
0
    return 1;
2371
0
  }
2372
2373
0
  if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
2374
0
    if (revs->max_count_type == 1)
2375
0
      die_for_incompatible_opt2(1, "--max-count", 1,
2376
0
              "--max-count-oldest");
2377
0
    revs->max_count = parse_count(optarg);
2378
0
    revs->no_walk = 0;
2379
0
    revs->max_count_type = 0;
2380
0
    return argcount;
2381
0
  } else if ((argcount = parse_long_opt("max-count-oldest", argv, &optarg))) {
2382
0
    if (revs->max_count_type == 0 && revs->max_count != -1)
2383
0
      die_for_incompatible_opt2(1, "--max-count", 1,
2384
0
              "--max-count-oldest");
2385
0
    if (revs->skip_count > 0)
2386
0
      die_for_incompatible_opt2(1, "--skip", 1,
2387
0
              "--max-count-oldest");
2388
0
    revs->max_count = parse_count(optarg);
2389
0
    revs->no_walk = 0;
2390
0
    revs->max_count_type = 1;
2391
0
    revs->max_count_stage = 0;
2392
0
  } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
2393
0
    if (revs->max_count_type == 1)
2394
0
      die_for_incompatible_opt2(1, "--skip", 1,
2395
0
              "--max-count-oldest");
2396
0
    revs->skip_count = parse_count(optarg);
2397
0
    return argcount;
2398
0
  } else if ((*arg == '-') && isdigit(arg[1])) {
2399
    /* accept -<digit>, like traditional "head" */
2400
0
    revs->max_count = parse_count(arg + 1);
2401
0
    revs->no_walk = 0;
2402
0
  } else if (!strcmp(arg, "-n")) {
2403
0
    if (argc <= 1)
2404
0
      return error("-n requires an argument");
2405
0
    revs->max_count = parse_count(argv[1]);
2406
0
    revs->no_walk = 0;
2407
0
    return 2;
2408
0
  } else if (skip_prefix(arg, "-n", &optarg)) {
2409
0
    revs->max_count = parse_count(optarg);
2410
0
    revs->no_walk = 0;
2411
0
  } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
2412
0
    revs->max_age = parse_age(optarg);
2413
0
    return argcount;
2414
0
  } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
2415
0
    revs->max_age = approxidate(optarg);
2416
0
    return argcount;
2417
0
  } else if ((argcount = parse_long_opt("since-as-filter", argv, &optarg))) {
2418
0
    revs->max_age_as_filter = approxidate(optarg);
2419
0
    return argcount;
2420
0
  } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
2421
0
    revs->max_age = approxidate(optarg);
2422
0
    return argcount;
2423
0
  } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
2424
0
    revs->min_age = parse_age(optarg);
2425
0
    return argcount;
2426
0
  } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
2427
0
    revs->min_age = approxidate(optarg);
2428
0
    return argcount;
2429
0
  } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
2430
0
    revs->min_age = approxidate(optarg);
2431
0
    return argcount;
2432
0
  } else if (!strcmp(arg, "--maximal-only")) {
2433
0
    revs->maximal_only = 1;
2434
0
  } else if (!strcmp(arg, "--first-parent")) {
2435
0
    revs->first_parent_only = 1;
2436
0
  } else if (!strcmp(arg, "--exclude-first-parent-only")) {
2437
0
    revs->exclude_first_parent_only = 1;
2438
0
  } else if (!strcmp(arg, "--ancestry-path")) {
2439
0
    revs->ancestry_path = 1;
2440
0
    revs->simplify_history = 0;
2441
0
    revs->limited = 1;
2442
0
    revs->ancestry_path_implicit_bottoms = 1;
2443
0
  } else if (skip_prefix(arg, "--ancestry-path=", &optarg)) {
2444
0
    struct commit *c;
2445
0
    struct object_id oid;
2446
0
    const char *msg = _("could not get commit for --ancestry-path argument %s");
2447
2448
0
    revs->ancestry_path = 1;
2449
0
    revs->simplify_history = 0;
2450
0
    revs->limited = 1;
2451
2452
0
    if (repo_get_oid_committish(revs->repo, optarg, &oid))
2453
0
      return error(msg, optarg);
2454
0
    get_reference(revs, optarg, &oid, ANCESTRY_PATH);
2455
0
    c = lookup_commit_reference(revs->repo, &oid);
2456
0
    if (!c)
2457
0
      return error(msg, optarg);
2458
0
    commit_list_insert(c, &revs->ancestry_path_bottoms);
2459
0
  } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
2460
0
    init_reflog_walk(&revs->reflog_info);
2461
0
  } else if (!strcmp(arg, "--default")) {
2462
0
    if (argc <= 1)
2463
0
      return error("bad --default argument");
2464
0
    revs->def = argv[1];
2465
0
    return 2;
2466
0
  } else if (!strcmp(arg, "--merge")) {
2467
0
    revs->show_merge = 1;
2468
0
  } else if (!strcmp(arg, "--topo-order")) {
2469
0
    revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
2470
0
    revs->topo_order = 1;
2471
0
  } else if (!strcmp(arg, "--simplify-merges")) {
2472
0
    revs->simplify_merges = 1;
2473
0
    revs->topo_order = 1;
2474
0
    revs->rewrite_parents = 1;
2475
0
    revs->simplify_history = 0;
2476
0
    revs->limited = 1;
2477
0
  } else if (!strcmp(arg, "--simplify-by-decoration")) {
2478
0
    revs->simplify_merges = 1;
2479
0
    revs->topo_order = 1;
2480
0
    revs->rewrite_parents = 1;
2481
0
    revs->simplify_history = 0;
2482
0
    revs->simplify_by_decoration = 1;
2483
0
    revs->limited = 1;
2484
0
    revs->prune = 1;
2485
0
  } else if (!strcmp(arg, "--date-order")) {
2486
0
    revs->sort_order = REV_SORT_BY_COMMIT_DATE;
2487
0
    revs->topo_order = 1;
2488
0
  } else if (!strcmp(arg, "--author-date-order")) {
2489
0
    revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
2490
0
    revs->topo_order = 1;
2491
0
  } else if (!strcmp(arg, "--parents")) {
2492
0
    revs->rewrite_parents = 1;
2493
0
    revs->print_parents = 1;
2494
0
  } else if (!strcmp(arg, "--dense")) {
2495
0
    revs->dense = 1;
2496
0
  } else if (!strcmp(arg, "--sparse")) {
2497
0
    revs->dense = 0;
2498
0
  } else if (!strcmp(arg, "--in-commit-order")) {
2499
0
    revs->tree_blobs_in_commit_order = 1;
2500
0
  } else if (!strcmp(arg, "--remove-empty")) {
2501
0
    revs->remove_empty_trees = 1;
2502
0
  } else if (!strcmp(arg, "--merges")) {
2503
0
    revs->min_parents = 2;
2504
0
  } else if (!strcmp(arg, "--no-merges")) {
2505
0
    revs->max_parents = 1;
2506
0
  } else if (skip_prefix(arg, "--min-parents=", &optarg)) {
2507
0
    revs->min_parents = parse_count(optarg);
2508
0
  } else if (!strcmp(arg, "--no-min-parents")) {
2509
0
    revs->min_parents = 0;
2510
0
  } else if (skip_prefix(arg, "--max-parents=", &optarg)) {
2511
0
    revs->max_parents = parse_count(optarg);
2512
0
  } else if (!strcmp(arg, "--no-max-parents")) {
2513
0
    revs->max_parents = -1;
2514
0
  } else if (!strcmp(arg, "--boundary")) {
2515
0
    revs->boundary = 1;
2516
0
  } else if (!strcmp(arg, "--left-right")) {
2517
0
    revs->left_right = 1;
2518
0
  } else if (!strcmp(arg, "--left-only")) {
2519
0
    if (revs->right_only)
2520
0
      die(_("options '%s' and '%s' cannot be used together"),
2521
0
          "--left-only", "--right-only/--cherry");
2522
0
    revs->left_only = 1;
2523
0
    revs->limited = 1;
2524
0
  } else if (!strcmp(arg, "--right-only")) {
2525
0
    if (revs->left_only)
2526
0
      die(_("options '%s' and '%s' cannot be used together"), "--right-only", "--left-only");
2527
0
    revs->right_only = 1;
2528
0
    revs->limited = 1;
2529
0
  } else if (!strcmp(arg, "--cherry")) {
2530
0
    if (revs->left_only)
2531
0
      die(_("options '%s' and '%s' cannot be used together"), "--cherry", "--left-only");
2532
0
    revs->cherry_mark = 1;
2533
0
    revs->right_only = 1;
2534
0
    revs->max_parents = 1;
2535
0
    revs->limited = 1;
2536
0
  } else if (!strcmp(arg, "--count")) {
2537
0
    revs->count = 1;
2538
0
  } else if (!strcmp(arg, "--cherry-mark")) {
2539
0
    if (revs->cherry_pick)
2540
0
      die(_("options '%s' and '%s' cannot be used together"), "--cherry-mark", "--cherry-pick");
2541
0
    revs->cherry_mark = 1;
2542
0
    revs->limited = 1; /* needs limit_list() */
2543
0
  } else if (!strcmp(arg, "--cherry-pick")) {
2544
0
    if (revs->cherry_mark)
2545
0
      die(_("options '%s' and '%s' cannot be used together"), "--cherry-pick", "--cherry-mark");
2546
0
    revs->cherry_pick = 1;
2547
0
    revs->limited = 1;
2548
0
  } else if (!strcmp(arg, "--objects")) {
2549
0
    revs->tag_objects = 1;
2550
0
    revs->tree_objects = 1;
2551
0
    revs->blob_objects = 1;
2552
0
  } else if (!strcmp(arg, "--objects-edge")) {
2553
0
    revs->tag_objects = 1;
2554
0
    revs->tree_objects = 1;
2555
0
    revs->blob_objects = 1;
2556
0
    revs->edge_hint = 1;
2557
0
  } else if (!strcmp(arg, "--objects-edge-aggressive")) {
2558
0
    revs->tag_objects = 1;
2559
0
    revs->tree_objects = 1;
2560
0
    revs->blob_objects = 1;
2561
0
    revs->edge_hint = 1;
2562
0
    revs->edge_hint_aggressive = 1;
2563
0
  } else if (!strcmp(arg, "--verify-objects")) {
2564
0
    revs->tag_objects = 1;
2565
0
    revs->tree_objects = 1;
2566
0
    revs->blob_objects = 1;
2567
0
    revs->verify_objects = 1;
2568
0
    disable_commit_graph(revs->repo);
2569
0
  } else if (!strcmp(arg, "--unpacked")) {
2570
0
    revs->unpacked = 1;
2571
0
  } else if (starts_with(arg, "--unpacked=")) {
2572
0
    die(_("--unpacked=<packfile> no longer supported"));
2573
0
  } else if (!strcmp(arg, "--no-kept-objects")) {
2574
0
    revs->no_kept_objects = 1;
2575
0
    revs->keep_pack_cache_flags |= KEPT_PACK_IN_CORE;
2576
0
    revs->keep_pack_cache_flags |= KEPT_PACK_ON_DISK;
2577
0
  } else if (skip_prefix(arg, "--no-kept-objects=", &optarg)) {
2578
0
    revs->no_kept_objects = 1;
2579
0
    if (!strcmp(optarg, "in-core"))
2580
0
      revs->keep_pack_cache_flags |= KEPT_PACK_IN_CORE;
2581
0
    if (!strcmp(optarg, "on-disk"))
2582
0
      revs->keep_pack_cache_flags |= KEPT_PACK_ON_DISK;
2583
0
  } else if (!strcmp(arg, "-r")) {
2584
0
    revs->diff = 1;
2585
0
    revs->diffopt.flags.recursive = 1;
2586
0
  } else if (!strcmp(arg, "-t")) {
2587
0
    revs->diff = 1;
2588
0
    revs->diffopt.flags.recursive = 1;
2589
0
    revs->diffopt.flags.tree_in_recursive = 1;
2590
0
  } else if ((argcount = diff_merges_parse_opts(revs, argv))) {
2591
0
    return argcount;
2592
0
  } else if (!strcmp(arg, "-v")) {
2593
0
    revs->verbose_header = 1;
2594
0
  } else if (!strcmp(arg, "--pretty")) {
2595
0
    revs->verbose_header = 1;
2596
0
    revs->pretty_given = 1;
2597
0
    get_commit_format(NULL, revs);
2598
0
  } else if (skip_prefix(arg, "--pretty=", &optarg) ||
2599
0
       skip_prefix(arg, "--format=", &optarg)) {
2600
    /*
2601
     * Detached form ("--pretty X" as opposed to "--pretty=X")
2602
     * not allowed, since the argument is optional.
2603
     */
2604
0
    revs->verbose_header = 1;
2605
0
    revs->pretty_given = 1;
2606
0
    get_commit_format(optarg, revs);
2607
0
  } else if (!strcmp(arg, "--expand-tabs")) {
2608
0
    revs->expand_tabs_in_log = 8;
2609
0
  } else if (!strcmp(arg, "--no-expand-tabs")) {
2610
0
    revs->expand_tabs_in_log = 0;
2611
0
  } else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
2612
0
    int val;
2613
0
    if (strtol_i(arg, 10, &val) < 0 || val < 0)
2614
0
      die("'%s': not a non-negative integer", arg);
2615
0
    revs->expand_tabs_in_log = val;
2616
0
  } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
2617
0
    enable_default_display_notes(&revs->notes_opt, &revs->show_notes);
2618
0
    revs->show_notes_given = 1;
2619
0
  } else if (!strcmp(arg, "--show-signature")) {
2620
0
    revs->show_signature = 1;
2621
0
  } else if (!strcmp(arg, "--no-show-signature")) {
2622
0
    revs->show_signature = 0;
2623
0
  } else if (!strcmp(arg, "--show-linear-break")) {
2624
0
    revs->break_bar = "                    ..........";
2625
0
    revs->track_linear = 1;
2626
0
    revs->track_first_time = 1;
2627
0
  } else if (skip_prefix(arg, "--show-linear-break=", &optarg)) {
2628
0
    revs->break_bar = xstrdup(optarg);
2629
0
    revs->track_linear = 1;
2630
0
    revs->track_first_time = 1;
2631
0
  } else if (!strcmp(arg, "--show-notes-by-default")) {
2632
0
    revs->show_notes_by_default = 1;
2633
0
  } else if (skip_prefix(arg, "--show-notes=", &optarg) ||
2634
0
       skip_prefix(arg, "--notes=", &optarg)) {
2635
0
    if (starts_with(arg, "--show-notes=") &&
2636
0
        revs->notes_opt.use_default_notes < 0)
2637
0
      revs->notes_opt.use_default_notes = 1;
2638
0
    enable_ref_display_notes(&revs->notes_opt, &revs->show_notes, optarg);
2639
0
    revs->show_notes_given = 1;
2640
0
  } else if (!strcmp(arg, "--no-notes")) {
2641
0
    disable_display_notes(&revs->notes_opt, &revs->show_notes);
2642
0
    revs->show_notes_given = 1;
2643
0
  } else if (!strcmp(arg, "--standard-notes")) {
2644
0
    revs->show_notes_given = 1;
2645
0
    revs->notes_opt.use_default_notes = 1;
2646
0
  } else if (!strcmp(arg, "--no-standard-notes")) {
2647
0
    revs->notes_opt.use_default_notes = 0;
2648
0
  } else if (!strcmp(arg, "--oneline")) {
2649
0
    revs->verbose_header = 1;
2650
0
    get_commit_format("oneline", revs);
2651
0
    revs->pretty_given = 1;
2652
0
    revs->abbrev_commit = 1;
2653
0
  } else if (!strcmp(arg, "--graph")) {
2654
0
    graph_clear(revs->graph);
2655
0
    revs->graph = graph_init(revs);
2656
0
  } else if (!strcmp(arg, "--no-graph")) {
2657
0
    graph_clear(revs->graph);
2658
0
    revs->graph = NULL;
2659
0
  } else if (skip_prefix(arg, "--graph-lane-limit=", &optarg)) {
2660
0
    revs->graph_max_lanes = parse_count(optarg);
2661
0
  } else if (!strcmp(arg, "--graph-indent")) {
2662
0
    revs->no_graph_indent = 0;
2663
0
    revs->graph_indent_set = 1;
2664
0
  } else if (!strcmp(arg, "--no-graph-indent")) {
2665
0
    revs->no_graph_indent = 1;
2666
0
    revs->graph_indent_set = 1;
2667
0
  } else if (!strcmp(arg, "--encode-email-headers")) {
2668
0
    revs->encode_email_headers = 1;
2669
0
  } else if (!strcmp(arg, "--no-encode-email-headers")) {
2670
0
    revs->encode_email_headers = 0;
2671
0
  } else if (!strcmp(arg, "--root")) {
2672
0
    revs->show_root_diff = 1;
2673
0
  } else if (!strcmp(arg, "--no-commit-id")) {
2674
0
    revs->no_commit_id = 1;
2675
0
  } else if (!strcmp(arg, "--always")) {
2676
0
    revs->always_show_header = 1;
2677
0
  } else if (!strcmp(arg, "--no-abbrev")) {
2678
0
    revs->abbrev = 0;
2679
0
  } else if (!strcmp(arg, "--abbrev")) {
2680
0
    revs->abbrev = DEFAULT_ABBREV;
2681
0
  } else if (skip_prefix(arg, "--abbrev=", &optarg)) {
2682
0
    revs->abbrev = strtoul(optarg, NULL, 10);
2683
0
    if (revs->abbrev < MINIMUM_ABBREV)
2684
0
      revs->abbrev = MINIMUM_ABBREV;
2685
0
    else if (revs->abbrev > hexsz)
2686
0
      revs->abbrev = hexsz;
2687
0
  } else if (!strcmp(arg, "--abbrev-commit")) {
2688
0
    revs->abbrev_commit = 1;
2689
0
    revs->abbrev_commit_given = 1;
2690
0
  } else if (!strcmp(arg, "--no-abbrev-commit")) {
2691
0
    revs->abbrev_commit = 0;
2692
0
  } else if (!strcmp(arg, "--full-diff")) {
2693
0
    revs->diff = 1;
2694
0
    revs->full_diff = 1;
2695
0
  } else if (!strcmp(arg, "--show-pulls")) {
2696
0
    revs->show_pulls = 1;
2697
0
  } else if (!strcmp(arg, "--full-history")) {
2698
0
    revs->simplify_history = 0;
2699
0
  } else if (!strcmp(arg, "--relative-date")) {
2700
0
    revs->date_mode.type = DATE_RELATIVE;
2701
0
    revs->date_mode_explicit = 1;
2702
0
  } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
2703
0
    parse_date_format(optarg, &revs->date_mode);
2704
0
    revs->date_mode_explicit = 1;
2705
0
    return argcount;
2706
0
  } else if (!strcmp(arg, "--log-size")) {
2707
0
    revs->show_log_size = 1;
2708
0
  }
2709
  /*
2710
   * Grepping the commit log
2711
   */
2712
0
  else if ((argcount = parse_long_opt("author", argv, &optarg))) {
2713
0
    add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
2714
0
    return argcount;
2715
0
  } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
2716
0
    add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
2717
0
    return argcount;
2718
0
  } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
2719
0
    add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
2720
0
    return argcount;
2721
0
  } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
2722
0
    add_message_grep(revs, optarg);
2723
0
    return argcount;
2724
0
  } else if (!strcmp(arg, "--basic-regexp")) {
2725
0
    revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE;
2726
0
  } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
2727
0
    revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
2728
0
  } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
2729
0
    revs->grep_filter.ignore_case = 1;
2730
0
    revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE;
2731
0
  } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
2732
0
    revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
2733
0
  } else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) {
2734
0
    revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;
2735
0
  } else if (!strcmp(arg, "--all-match")) {
2736
0
    revs->grep_filter.all_match = 1;
2737
0
  } else if (!strcmp(arg, "--invert-grep")) {
2738
0
    revs->grep_filter.no_body_match = 1;
2739
0
  } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
2740
0
    free(git_log_output_encoding);
2741
0
    if (strcmp(optarg, "none"))
2742
0
      git_log_output_encoding = xstrdup(optarg);
2743
0
    else
2744
0
      git_log_output_encoding = xstrdup("");
2745
0
    return argcount;
2746
0
  } else if (!strcmp(arg, "--reverse")) {
2747
0
    revs->reverse ^= 1;
2748
0
  } else if (!strcmp(arg, "--children")) {
2749
0
    revs->children.name = "children";
2750
0
    revs->limited = 1;
2751
0
  } else if (!strcmp(arg, "--ignore-missing")) {
2752
0
    revs->ignore_missing = 1;
2753
0
  } else if (opt && opt->allow_exclude_promisor_objects &&
2754
0
       !strcmp(arg, "--exclude-promisor-objects")) {
2755
0
    if (revs->repo->fetch_if_missing)
2756
0
      BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
2757
0
    revs->exclude_promisor_objects = 1;
2758
0
  } else {
2759
0
    int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
2760
0
    if (!opts)
2761
0
      overwrite_argv(unkc, unkv, &argv[0], opt, revs);
2762
0
    return opts;
2763
0
  }
2764
2765
0
  return 1;
2766
0
}
2767
2768
void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2769
      const struct option *options,
2770
      const char * const usagestr[])
2771
0
{
2772
0
  int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
2773
0
            &ctx->cpidx, ctx->out, NULL);
2774
0
  if (n < 0) {
2775
    /* handle_revision_opt() has already reported the error. */
2776
0
    usage_with_options(usagestr, options);
2777
0
  } else if (!n) {
2778
0
    error("unknown option `%s'", ctx->out[ctx->cpidx - 1]);
2779
0
    usage_with_options(usagestr, options);
2780
0
  }
2781
0
  ctx->argv += n;
2782
0
  ctx->argc -= n;
2783
0
}
2784
2785
void revision_opts_finish(struct rev_info *revs)
2786
0
{
2787
0
  if (revs->graph && revs->track_linear)
2788
0
    die(_("options '%s' and '%s' cannot be used together"), "--show-linear-break", "--graph");
2789
2790
0
  if (revs->graph) {
2791
0
    revs->topo_order = 1;
2792
0
    revs->rewrite_parents = 1;
2793
0
  }
2794
0
}
2795
2796
static int for_each_bisect_ref(struct ref_store *refs, refs_for_each_cb fn,
2797
             void *cb_data, const char *term)
2798
0
{
2799
0
  struct refs_for_each_ref_options opts = { 0 };
2800
0
  struct strbuf bisect_refs = STRBUF_INIT;
2801
0
  int status;
2802
0
  strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
2803
0
  opts.prefix = bisect_refs.buf;
2804
0
  status = refs_for_each_ref_ext(refs, fn, cb_data, &opts);
2805
0
  strbuf_release(&bisect_refs);
2806
0
  return status;
2807
0
}
2808
2809
static int for_each_bad_bisect_ref(struct ref_store *refs, refs_for_each_cb fn, void *cb_data)
2810
0
{
2811
0
  return for_each_bisect_ref(refs, fn, cb_data, term_bad);
2812
0
}
2813
2814
static int for_each_good_bisect_ref(struct ref_store *refs, refs_for_each_cb fn, void *cb_data)
2815
0
{
2816
0
  return for_each_bisect_ref(refs, fn, cb_data, term_good);
2817
0
}
2818
2819
static int handle_revision_pseudo_opt(struct rev_info *revs,
2820
              const char **argv, int *flags)
2821
0
{
2822
0
  const char *arg = argv[0];
2823
0
  const char *optarg;
2824
0
  struct ref_store *refs;
2825
0
  int argcount;
2826
2827
0
  if (revs->repo != the_repository) {
2828
    /*
2829
     * We need some something like get_submodule_worktrees()
2830
     * before we can go through all worktrees of a submodule,
2831
     * .e.g with adding all HEADs from --all, which is not
2832
     * supported right now, so stick to single worktree.
2833
     */
2834
0
    if (!revs->single_worktree)
2835
0
      BUG("--single-worktree cannot be used together with submodule");
2836
0
  }
2837
0
  refs = get_main_ref_store(revs->repo);
2838
2839
  /*
2840
   * NOTE!
2841
   *
2842
   * Commands like "git shortlog" will not accept the options below
2843
   * unless parse_revision_opt queues them (as opposed to erroring
2844
   * out).
2845
   *
2846
   * When implementing your new pseudo-option, remember to
2847
   * register it in the list at the top of handle_revision_opt.
2848
   */
2849
0
  if (!strcmp(arg, "--all")) {
2850
0
    handle_refs(refs, revs, *flags, refs_for_each_ref);
2851
0
    handle_refs(refs, revs, *flags, refs_head_ref);
2852
0
    if (!revs->single_worktree) {
2853
0
      struct all_refs_cb cb;
2854
2855
0
      init_all_refs_cb(&cb, revs, *flags);
2856
0
      other_head_refs(the_repository, handle_one_ref, &cb);
2857
0
    }
2858
0
    clear_ref_exclusions(&revs->ref_excludes);
2859
0
  } else if (!strcmp(arg, "--branches")) {
2860
0
    if (revs->ref_excludes.hidden_refs_configured)
2861
0
      return error(_("options '%s' and '%s' cannot be used together"),
2862
0
             "--exclude-hidden", "--branches");
2863
0
    handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
2864
0
    clear_ref_exclusions(&revs->ref_excludes);
2865
0
  } else if (!strcmp(arg, "--bisect")) {
2866
0
    read_bisect_terms(&term_bad, &term_good);
2867
0
    handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
2868
0
    handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
2869
0
          for_each_good_bisect_ref);
2870
0
    revs->bisect = 1;
2871
0
  } else if (!strcmp(arg, "--tags")) {
2872
0
    if (revs->ref_excludes.hidden_refs_configured)
2873
0
      return error(_("options '%s' and '%s' cannot be used together"),
2874
0
             "--exclude-hidden", "--tags");
2875
0
    handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
2876
0
    clear_ref_exclusions(&revs->ref_excludes);
2877
0
  } else if (!strcmp(arg, "--remotes")) {
2878
0
    if (revs->ref_excludes.hidden_refs_configured)
2879
0
      return error(_("options '%s' and '%s' cannot be used together"),
2880
0
             "--exclude-hidden", "--remotes");
2881
0
    handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
2882
0
    clear_ref_exclusions(&revs->ref_excludes);
2883
0
  } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2884
0
    struct refs_for_each_ref_options opts = {
2885
0
      .pattern = optarg,
2886
0
    };
2887
0
    struct all_refs_cb cb;
2888
0
    init_all_refs_cb(&cb, revs, *flags);
2889
0
    refs_for_each_ref_ext(get_main_ref_store(the_repository),
2890
0
              handle_one_ref, &cb, &opts);
2891
0
    clear_ref_exclusions(&revs->ref_excludes);
2892
0
    return argcount;
2893
0
  } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2894
0
    add_ref_exclusion(&revs->ref_excludes, optarg);
2895
0
    return argcount;
2896
0
  } else if ((argcount = parse_long_opt("exclude-hidden", argv, &optarg))) {
2897
0
    exclude_hidden_refs(&revs->ref_excludes, optarg);
2898
0
    return argcount;
2899
0
  } else if (skip_prefix(arg, "--branches=", &optarg)) {
2900
0
    struct refs_for_each_ref_options opts = {
2901
0
      .prefix = "refs/heads/",
2902
0
      .trim_prefix = strlen("refs/heads/"),
2903
0
      .pattern = optarg,
2904
0
    };
2905
0
    struct all_refs_cb cb;
2906
0
    if (revs->ref_excludes.hidden_refs_configured)
2907
0
      return error(_("options '%s' and '%s' cannot be used together"),
2908
0
             "--exclude-hidden", "--branches");
2909
0
    init_all_refs_cb(&cb, revs, *flags);
2910
0
    refs_for_each_ref_ext(get_main_ref_store(the_repository),
2911
0
              handle_one_ref, &cb, &opts);
2912
0
    clear_ref_exclusions(&revs->ref_excludes);
2913
0
  } else if (skip_prefix(arg, "--tags=", &optarg)) {
2914
0
    struct refs_for_each_ref_options opts = {
2915
0
      .prefix = "refs/tags/",
2916
0
      .trim_prefix = strlen("refs/tags/"),
2917
0
      .pattern = optarg,
2918
0
    };
2919
0
    struct all_refs_cb cb;
2920
0
    if (revs->ref_excludes.hidden_refs_configured)
2921
0
      return error(_("options '%s' and '%s' cannot be used together"),
2922
0
             "--exclude-hidden", "--tags");
2923
0
    init_all_refs_cb(&cb, revs, *flags);
2924
0
    refs_for_each_ref_ext(get_main_ref_store(the_repository),
2925
0
              handle_one_ref, &cb, &opts);
2926
0
    clear_ref_exclusions(&revs->ref_excludes);
2927
0
  } else if (skip_prefix(arg, "--remotes=", &optarg)) {
2928
0
    struct refs_for_each_ref_options opts = {
2929
0
      .prefix = "refs/remotes/",
2930
0
      .trim_prefix = strlen("refs/remotes/"),
2931
0
      .pattern = optarg,
2932
0
    };
2933
0
    struct all_refs_cb cb;
2934
0
    if (revs->ref_excludes.hidden_refs_configured)
2935
0
      return error(_("options '%s' and '%s' cannot be used together"),
2936
0
             "--exclude-hidden", "--remotes");
2937
0
    init_all_refs_cb(&cb, revs, *flags);
2938
0
    refs_for_each_ref_ext(get_main_ref_store(the_repository),
2939
0
              handle_one_ref, &cb, &opts);
2940
0
    clear_ref_exclusions(&revs->ref_excludes);
2941
0
  } else if (!strcmp(arg, "--reflog")) {
2942
0
    add_reflogs_to_pending(revs, *flags);
2943
0
  } else if (!strcmp(arg, "--indexed-objects")) {
2944
0
    add_index_objects_to_pending(revs, *flags);
2945
0
  } else if (!strcmp(arg, "--alternate-refs")) {
2946
0
    add_alternate_refs_to_pending(revs, *flags);
2947
0
  } else if (!strcmp(arg, "--not")) {
2948
0
    *flags ^= UNINTERESTING | BOTTOM;
2949
0
  } else if (!strcmp(arg, "--no-walk")) {
2950
0
    revs->no_walk = 1;
2951
0
  } else if (skip_prefix(arg, "--no-walk=", &optarg)) {
2952
    /*
2953
     * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2954
     * not allowed, since the argument is optional.
2955
     */
2956
0
    revs->no_walk = 1;
2957
0
    if (!strcmp(optarg, "sorted"))
2958
0
      revs->unsorted_input = 0;
2959
0
    else if (!strcmp(optarg, "unsorted"))
2960
0
      revs->unsorted_input = 1;
2961
0
    else
2962
0
      return error("invalid argument to --no-walk");
2963
0
  } else if (!strcmp(arg, "--do-walk")) {
2964
0
    revs->no_walk = 0;
2965
0
  } else if (!strcmp(arg, "--single-worktree")) {
2966
0
    revs->single_worktree = 1;
2967
0
  } else if (skip_prefix(arg, ("--filter="), &arg)) {
2968
0
    parse_list_objects_filter(&revs->filter, arg);
2969
0
  } else if (!strcmp(arg, ("--no-filter"))) {
2970
0
    list_objects_filter_set_no_filter(&revs->filter);
2971
0
  } else {
2972
0
    return 0;
2973
0
  }
2974
2975
0
  return 1;
2976
0
}
2977
2978
static void read_revisions_from_stdin(struct rev_info *revs,
2979
              struct strvec *prune)
2980
0
{
2981
0
  struct strbuf sb;
2982
0
  int seen_dashdash = 0;
2983
0
  int seen_end_of_options = 0;
2984
0
  int save_warning;
2985
0
  int flags = 0;
2986
0
  struct repo_config_values *cfg = repo_config_values(the_repository);
2987
2988
0
  save_warning = cfg->warn_on_object_refname_ambiguity;
2989
0
  cfg->warn_on_object_refname_ambiguity = 0;
2990
2991
0
  strbuf_init(&sb, 1000);
2992
0
  while (strbuf_getline(&sb, stdin) != EOF) {
2993
0
    if (!sb.len)
2994
0
      break;
2995
2996
0
    if (!strcmp(sb.buf, "--")) {
2997
0
      seen_dashdash = 1;
2998
0
      break;
2999
0
    }
3000
3001
0
    if (!seen_end_of_options && sb.buf[0] == '-') {
3002
0
      const char *argv[] = { sb.buf, NULL };
3003
3004
0
      if (!strcmp(sb.buf, "--end-of-options")) {
3005
0
        seen_end_of_options = 1;
3006
0
        continue;
3007
0
      }
3008
3009
0
      if (handle_revision_pseudo_opt(revs, argv, &flags) > 0)
3010
0
        continue;
3011
3012
0
      die(_("invalid option '%s' in --stdin mode"), sb.buf);
3013
0
    }
3014
3015
0
    if (handle_revision_arg(sb.buf, revs, flags,
3016
0
          REVARG_CANNOT_BE_FILENAME))
3017
0
      die("bad revision '%s'", sb.buf);
3018
0
  }
3019
0
  if (seen_dashdash)
3020
0
    read_pathspec_from_stdin(&sb, prune);
3021
3022
0
  strbuf_release(&sb);
3023
0
  cfg->warn_on_object_refname_ambiguity = save_warning;
3024
0
}
3025
3026
static void NORETURN diagnose_missing_default(const char *def)
3027
0
{
3028
0
  int flags;
3029
0
  const char *refname;
3030
3031
0
  refname = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
3032
0
            def, 0, NULL, &flags);
3033
0
  if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
3034
0
    die(_("your current branch appears to be broken"));
3035
3036
0
  skip_prefix(refname, "refs/heads/", &refname);
3037
0
  die(_("your current branch '%s' does not have any commits yet"),
3038
0
      refname);
3039
0
}
3040
3041
/*
3042
 * Parse revision information, filling in the "rev_info" structure,
3043
 * and removing the used arguments from the argument list.
3044
 *
3045
 * Returns the number of arguments left that weren't recognized
3046
 * (which are also moved to the head of the argument list)
3047
 */
3048
int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
3049
0
{
3050
0
  int i, flags, left, seen_dashdash, revarg_opt;
3051
0
  struct strvec prune_data = STRVEC_INIT;
3052
0
  int seen_end_of_options = 0;
3053
3054
  /* First, search for "--" */
3055
0
  if (opt && opt->assume_dashdash) {
3056
0
    seen_dashdash = 1;
3057
0
  } else {
3058
0
    seen_dashdash = 0;
3059
0
    for (i = 1; i < argc; i++) {
3060
0
      const char *arg = argv[i];
3061
0
      if (strcmp(arg, "--"))
3062
0
        continue;
3063
0
      mark_argv_for_free(opt, revs, argv[i]);
3064
0
      argv[i] = NULL;
3065
0
      argc = i;
3066
0
      if (argv[i + 1])
3067
0
        strvec_pushv(&prune_data, argv + i + 1);
3068
0
      seen_dashdash = 1;
3069
0
      break;
3070
0
    }
3071
0
  }
3072
3073
  /* Second, deal with arguments and options */
3074
0
  flags = 0;
3075
0
  revarg_opt = opt ? opt->revarg_opt : 0;
3076
0
  if (seen_dashdash)
3077
0
    revarg_opt |= REVARG_CANNOT_BE_FILENAME;
3078
0
  for (left = i = 1; i < argc; i++) {
3079
0
    const char *arg = argv[i];
3080
0
    if (!seen_end_of_options && *arg == '-') {
3081
0
      int opts;
3082
3083
0
      opts = handle_revision_pseudo_opt(
3084
0
            revs, argv + i,
3085
0
            &flags);
3086
0
      if (opts > 0) {
3087
0
        i += opts - 1;
3088
0
        continue;
3089
0
      }
3090
3091
0
      if (!strcmp(arg, "--stdin")) {
3092
0
        if (revs->disable_stdin) {
3093
0
          overwrite_argv(&left, argv, &argv[i],
3094
0
                   opt, revs);
3095
0
          continue;
3096
0
        }
3097
0
        if (revs->read_from_stdin++)
3098
0
          die("--stdin given twice?");
3099
0
        read_revisions_from_stdin(revs, &prune_data);
3100
0
        continue;
3101
0
      }
3102
3103
0
      if (!strcmp(arg, "--end-of-options")) {
3104
0
        seen_end_of_options = 1;
3105
0
        continue;
3106
0
      }
3107
3108
0
      opts = handle_revision_opt(revs, argc - i, argv + i,
3109
0
               &left, argv, opt);
3110
0
      if (opts > 0) {
3111
0
        i += opts - 1;
3112
0
        continue;
3113
0
      }
3114
0
      if (opts < 0)
3115
0
        exit(128);
3116
0
      continue;
3117
0
    }
3118
3119
3120
0
    if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
3121
0
      int j;
3122
0
      if (seen_dashdash || *arg == '^')
3123
0
        die("bad revision '%s'", arg);
3124
3125
      /* If we didn't have a "--":
3126
       * (1) all filenames must exist;
3127
       * (2) all rev-args must not be interpretable
3128
       *     as a valid filename.
3129
       * but the latter we have checked in the main loop.
3130
       */
3131
0
      for (j = i; j < argc; j++)
3132
0
        verify_filename(the_repository, revs->prefix, argv[j], j == i);
3133
3134
0
      strvec_pushv(&prune_data, argv + i);
3135
0
      break;
3136
0
    }
3137
0
  }
3138
0
  revision_opts_finish(revs);
3139
3140
0
  if (prune_data.nr) {
3141
    /*
3142
     * If we need to introduce the magic "a lone ':' means no
3143
     * pathspec whatsoever", here is the place to do so.
3144
     *
3145
     * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
3146
     *  prune_data.nr = 0;
3147
     *  prune_data.alloc = 0;
3148
     *  free(prune_data.path);
3149
     *  prune_data.path = NULL;
3150
     * } else {
3151
     *  terminate prune_data.alloc with NULL and
3152
     *  call init_pathspec() to set revs->prune_data here.
3153
     * }
3154
     */
3155
0
    parse_pathspec(&revs->prune_data, 0, 0,
3156
0
             revs->prefix, prune_data.v);
3157
0
  }
3158
0
  strvec_clear(&prune_data);
3159
3160
0
  if (!revs->def)
3161
0
    revs->def = opt ? opt->def : NULL;
3162
0
  if (opt && opt->tweak)
3163
0
    opt->tweak(revs);
3164
0
  if (revs->show_merge)
3165
0
    prepare_show_merge(revs);
3166
0
  if (revs->def && !revs->pending.nr && !revs->rev_input_given) {
3167
0
    struct object_id oid;
3168
0
    struct object *object;
3169
0
    struct object_context oc;
3170
0
    if (get_oid_with_context(revs->repo, revs->def, 0, &oid, &oc))
3171
0
      diagnose_missing_default(revs->def);
3172
0
    object = get_reference(revs, revs->def, &oid, 0);
3173
0
    add_pending_object_with_mode(revs, object, revs->def, oc.mode);
3174
0
    object_context_release(&oc);
3175
0
  }
3176
3177
0
  if (revs->line_level_traverse) {
3178
0
    if (want_ancestry(revs))
3179
0
      revs->limited = 1;
3180
0
    revs->topo_order = 1;
3181
0
    if (!revs->diffopt.output_format)
3182
0
      revs->diffopt.output_format = DIFF_FORMAT_PATCH;
3183
0
  }
3184
3185
  /* Did the user ask for any diff output? Run the diff! */
3186
0
  if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
3187
0
    revs->diff = 1;
3188
3189
  /* Pickaxe, diff-filter and rename following need diffs */
3190
0
  if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
3191
0
      revs->diffopt.filter || revs->diffopt.filter_not ||
3192
0
      revs->diffopt.flags.follow_renames)
3193
0
    revs->diff = 1;
3194
3195
0
  if (revs->diffopt.objfind)
3196
0
    revs->simplify_history = 0;
3197
3198
0
  if (revs->topo_order && !generation_numbers_enabled(the_repository))
3199
0
    revs->limited = 1;
3200
3201
0
  if (revs->prune_data.nr) {
3202
0
    copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
3203
    /* Can't prune commits with rename following: the paths change.. */
3204
0
    if (!revs->diffopt.flags.follow_renames)
3205
0
      revs->prune = 1;
3206
0
    if (!revs->full_diff)
3207
0
      copy_pathspec(&revs->diffopt.pathspec,
3208
0
              &revs->prune_data);
3209
0
  }
3210
3211
0
  diff_merges_setup_revs(revs);
3212
3213
0
  revs->diffopt.abbrev = revs->abbrev;
3214
3215
0
  diff_setup_done(&revs->diffopt);
3216
3217
0
  if (!is_encoding_utf8(get_log_output_encoding()))
3218
0
    revs->grep_filter.ignore_locale = 1;
3219
0
  compile_grep_patterns(&revs->grep_filter);
3220
3221
0
  if (revs->reflog_info && revs->limited)
3222
0
    die("cannot combine --walk-reflogs with history-limiting options");
3223
0
  if (revs->rewrite_parents && revs->children.name)
3224
0
    die(_("options '%s' and '%s' cannot be used together"), "--parents", "--children");
3225
0
  if (revs->filter.choice && !revs->blob_objects)
3226
0
    die(_("object filtering requires --objects"));
3227
3228
  /*
3229
   * Limitations on the graph functionality
3230
   */
3231
0
  die_for_incompatible_opt3(!!revs->graph, "--graph",
3232
0
          !!revs->reverse, "--reverse",
3233
0
          !!revs->reflog_info, "--walk-reflogs");
3234
3235
0
  die_for_incompatible_opt2(!!revs->boundary, "--boundary",
3236
0
          !!revs->maximal_only, "--maximal-only");
3237
3238
0
  if (revs->no_walk && revs->graph)
3239
0
    die(_("options '%s' and '%s' cannot be used together"), "--no-walk", "--graph");
3240
3241
0
  if (revs->graph_max_lanes > 0 && !revs->graph)
3242
0
    die(_("the option '%s' requires '%s'"), "--graph-lane-limit", "--graph");
3243
3244
0
  if (revs->graph_indent_set && !revs->graph)
3245
0
    die(_("the option '%s' requires '%s'"), "--[no-]graph-indent", "--graph");
3246
3247
0
  if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
3248
0
    die(_("the option '%s' requires '%s'"), "--grep-reflog", "--walk-reflogs");
3249
3250
0
  if (revs->line_level_traverse &&
3251
0
      (revs->full_diff ||
3252
0
       (revs->diffopt.output_format &
3253
0
        ~(DIFF_FORMAT_PATCH | DIFF_FORMAT_NO_OUTPUT |
3254
0
    DIFF_FORMAT_RAW | DIFF_FORMAT_NAME |
3255
0
    DIFF_FORMAT_NAME_STATUS | DIFF_FORMAT_SUMMARY))))
3256
0
    die(_("-L does not yet support the requested diff format"));
3257
3258
0
  if (revs->expand_tabs_in_log < 0)
3259
0
    revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
3260
3261
0
  if (!revs->show_notes_given && revs->show_notes_by_default) {
3262
0
    enable_default_display_notes(&revs->notes_opt, &revs->show_notes);
3263
0
    revs->show_notes_given = 1;
3264
0
  }
3265
3266
0
  if (argv) {
3267
0
    mark_argv_for_free(opt, revs, argv[left]);
3268
0
    argv[left] = NULL;
3269
0
  }
3270
3271
0
  return left;
3272
0
}
3273
3274
void setup_revisions_from_strvec(struct strvec *argv, struct rev_info *revs,
3275
         struct setup_revision_opt *opt)
3276
0
{
3277
0
  struct setup_revision_opt fallback_opt;
3278
0
  int ret;
3279
3280
0
  if (!opt) {
3281
0
    memset(&fallback_opt, 0, sizeof(fallback_opt));
3282
0
    opt = &fallback_opt;
3283
0
  }
3284
0
  opt->free_removed_argv_elements = 1;
3285
3286
0
  ret = setup_revisions(argv->nr, argv->v, revs, opt);
3287
3288
0
  for (size_t i = ret; i < argv->nr; i++)
3289
0
    mark_argv_for_free(opt, revs, argv->v[i]);
3290
0
  argv->nr = ret;
3291
0
}
3292
3293
static void release_revisions_cmdline(struct rev_cmdline_info *cmdline)
3294
0
{
3295
0
  unsigned int i;
3296
3297
0
  for (i = 0; i < cmdline->nr; i++)
3298
0
    free((char *)cmdline->rev[i].name);
3299
0
  free(cmdline->rev);
3300
0
}
3301
3302
static void release_revisions_mailmap(struct string_list *mailmap)
3303
0
{
3304
0
  if (!mailmap)
3305
0
    return;
3306
0
  clear_mailmap(mailmap);
3307
0
  free(mailmap);
3308
0
}
3309
3310
static void release_revisions_topo_walk_info(struct topo_walk_info *info);
3311
3312
static void release_revisions_bloom_keyvecs(struct rev_info *revs)
3313
0
{
3314
0
  for (size_t nr = 0; nr < revs->bloom_keyvecs_nr; nr++)
3315
0
    bloom_keyvec_free(revs->bloom_keyvecs[nr]);
3316
0
  FREE_AND_NULL(revs->bloom_keyvecs);
3317
0
  revs->bloom_keyvecs_nr = 0;
3318
0
}
3319
3320
static void free_void_commit_list(void *list)
3321
0
{
3322
0
  commit_list_free(list);
3323
0
}
3324
3325
void release_revisions(struct rev_info *revs)
3326
0
{
3327
0
  commit_list_free(revs->commits);
3328
0
  clear_prio_queue(&revs->commit_queue);
3329
0
  commit_list_free(revs->ancestry_path_bottoms);
3330
0
  release_display_notes(&revs->notes_opt);
3331
0
  object_array_clear(&revs->pending);
3332
0
  object_array_clear(&revs->boundary_commits);
3333
0
  release_revisions_cmdline(&revs->cmdline);
3334
0
  list_objects_filter_release(&revs->filter);
3335
0
  clear_pathspec(&revs->prune_data);
3336
0
  date_mode_release(&revs->date_mode);
3337
0
  release_revisions_mailmap(revs->mailmap);
3338
0
  free_grep_patterns(&revs->grep_filter);
3339
0
  graph_clear(revs->graph);
3340
0
  diff_free(&revs->diffopt);
3341
0
  diff_free(&revs->pruning);
3342
0
  reflog_walk_info_release(revs->reflog_info);
3343
0
  release_revisions_topo_walk_info(revs->topo_walk_info);
3344
0
  clear_decoration(&revs->children, free_void_commit_list);
3345
0
  clear_decoration(&revs->merge_simplification, free);
3346
0
  clear_decoration(&revs->treesame, free);
3347
0
  line_log_free(revs);
3348
0
  oidset_clear(&revs->missing_commits);
3349
0
  release_revisions_bloom_keyvecs(revs);
3350
0
  release_follow_pathspec_slab(revs);
3351
0
  strvec_clear(&revs->argv_to_free);
3352
0
}
3353
3354
static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
3355
0
{
3356
0
  struct commit_list *l = xcalloc(1, sizeof(*l));
3357
3358
0
  l->item = child;
3359
0
  l->next = add_decoration(&revs->children, &parent->object, l);
3360
0
}
3361
3362
static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
3363
0
{
3364
0
  struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
3365
0
  struct commit_list **pp, *p;
3366
0
  int surviving_parents;
3367
3368
  /* Examine existing parents while marking ones we have seen... */
3369
0
  pp = &commit->parents;
3370
0
  surviving_parents = 0;
3371
0
  while ((p = *pp) != NULL) {
3372
0
    struct commit *parent = p->item;
3373
0
    if (parent->object.flags & TMP_MARK) {
3374
0
      *pp = p->next;
3375
0
      free(p);
3376
0
      if (ts)
3377
0
        compact_treesame(revs, commit, surviving_parents);
3378
0
      continue;
3379
0
    }
3380
0
    parent->object.flags |= TMP_MARK;
3381
0
    surviving_parents++;
3382
0
    pp = &p->next;
3383
0
  }
3384
  /* clear the temporary mark */
3385
0
  for (p = commit->parents; p; p = p->next) {
3386
0
    p->item->object.flags &= ~TMP_MARK;
3387
0
  }
3388
  /* no update_treesame() - removing duplicates can't affect TREESAME */
3389
0
  return surviving_parents;
3390
0
}
3391
3392
struct merge_simplify_state {
3393
  struct commit *simplified;
3394
};
3395
3396
static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
3397
0
{
3398
0
  struct merge_simplify_state *st;
3399
3400
0
  st = lookup_decoration(&revs->merge_simplification, &commit->object);
3401
0
  if (!st) {
3402
0
    CALLOC_ARRAY(st, 1);
3403
0
    add_decoration(&revs->merge_simplification, &commit->object, st);
3404
0
  }
3405
0
  return st;
3406
0
}
3407
3408
static int mark_redundant_parents(struct commit *commit)
3409
0
{
3410
0
  struct commit_list *h = reduce_heads(commit->parents);
3411
0
  int i = 0, marked = 0;
3412
0
  struct commit_list *po, *pn;
3413
3414
  /* Want these for sanity-checking only */
3415
0
  int orig_cnt = commit_list_count(commit->parents);
3416
0
  int cnt = commit_list_count(h);
3417
3418
  /*
3419
   * Not ready to remove items yet, just mark them for now, based
3420
   * on the output of reduce_heads(). reduce_heads outputs the reduced
3421
   * set in its original order, so this isn't too hard.
3422
   */
3423
0
  po = commit->parents;
3424
0
  pn = h;
3425
0
  while (po) {
3426
0
    if (pn && po->item == pn->item) {
3427
0
      pn = pn->next;
3428
0
      i++;
3429
0
    } else {
3430
0
      po->item->object.flags |= TMP_MARK;
3431
0
      marked++;
3432
0
    }
3433
0
    po=po->next;
3434
0
  }
3435
3436
0
  if (i != cnt || cnt+marked != orig_cnt)
3437
0
    die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
3438
3439
0
  commit_list_free(h);
3440
3441
0
  return marked;
3442
0
}
3443
3444
static int mark_treesame_root_parents(struct commit *commit)
3445
0
{
3446
0
  struct commit_list *p;
3447
0
  int marked = 0;
3448
3449
0
  for (p = commit->parents; p; p = p->next) {
3450
0
    struct commit *parent = p->item;
3451
0
    if (!parent->parents && (parent->object.flags & TREESAME)) {
3452
0
      parent->object.flags |= TMP_MARK;
3453
0
      marked++;
3454
0
    }
3455
0
  }
3456
3457
0
  return marked;
3458
0
}
3459
3460
/*
3461
 * Awkward naming - this means one parent we are TREESAME to.
3462
 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
3463
 * empty tree). Better name suggestions?
3464
 */
3465
static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
3466
0
{
3467
0
  struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
3468
0
  struct commit *unmarked = NULL, *marked = NULL;
3469
0
  struct commit_list *p;
3470
0
  unsigned n;
3471
3472
0
  for (p = commit->parents, n = 0; p; p = p->next, n++) {
3473
0
    if (ts->treesame[n]) {
3474
0
      if (p->item->object.flags & TMP_MARK) {
3475
0
        if (!marked)
3476
0
          marked = p->item;
3477
0
      } else {
3478
0
        if (!unmarked) {
3479
0
          unmarked = p->item;
3480
0
          break;
3481
0
        }
3482
0
      }
3483
0
    }
3484
0
  }
3485
3486
  /*
3487
   * If we are TREESAME to a marked-for-deletion parent, but not to any
3488
   * unmarked parents, unmark the first TREESAME parent. This is the
3489
   * parent that the default simplify_history==1 scan would have followed,
3490
   * and it doesn't make sense to omit that path when asking for a
3491
   * simplified full history. Retaining it improves the chances of
3492
   * understanding odd missed merges that took an old version of a file.
3493
   *
3494
   * Example:
3495
   *
3496
   *   I--------*X       A modified the file, but mainline merge X used
3497
   *    \       /        "-s ours", so took the version from I. X is
3498
   *     `-*A--'         TREESAME to I and !TREESAME to A.
3499
   *
3500
   * Default log from X would produce "I". Without this check,
3501
   * --full-history --simplify-merges would produce "I-A-X", showing
3502
   * the merge commit X and that it changed A, but not making clear that
3503
   * it had just taken the I version. With this check, the topology above
3504
   * is retained.
3505
   *
3506
   * Note that it is possible that the simplification chooses a different
3507
   * TREESAME parent from the default, in which case this test doesn't
3508
   * activate, and we _do_ drop the default parent. Example:
3509
   *
3510
   *   I------X         A modified the file, but it was reverted in B,
3511
   *    \    /          meaning mainline merge X is TREESAME to both
3512
   *    *A-*B           parents.
3513
   *
3514
   * Default log would produce "I" by following the first parent;
3515
   * --full-history --simplify-merges will produce "I-A-B". But this is a
3516
   * reasonable result - it presents a logical full history leading from
3517
   * I to X, and X is not an important merge.
3518
   */
3519
0
  if (!unmarked && marked) {
3520
0
    marked->object.flags &= ~TMP_MARK;
3521
0
    return 1;
3522
0
  }
3523
3524
0
  return 0;
3525
0
}
3526
3527
static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
3528
0
{
3529
0
  struct commit_list **pp, *p;
3530
0
  int nth_parent, removed = 0;
3531
3532
0
  pp = &commit->parents;
3533
0
  nth_parent = 0;
3534
0
  while ((p = *pp) != NULL) {
3535
0
    struct commit *parent = p->item;
3536
0
    if (parent->object.flags & TMP_MARK) {
3537
0
      parent->object.flags &= ~TMP_MARK;
3538
0
      *pp = p->next;
3539
0
      free(p);
3540
0
      removed++;
3541
0
      compact_treesame(revs, commit, nth_parent);
3542
0
      continue;
3543
0
    }
3544
0
    pp = &p->next;
3545
0
    nth_parent++;
3546
0
  }
3547
3548
  /* Removing parents can only increase TREESAMEness */
3549
0
  if (removed && !(commit->object.flags & TREESAME))
3550
0
    update_treesame(revs, commit);
3551
3552
0
  return nth_parent;
3553
0
}
3554
3555
static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
3556
0
{
3557
0
  struct commit_list *p;
3558
0
  struct commit *parent;
3559
0
  struct merge_simplify_state *st, *pst;
3560
0
  int cnt;
3561
3562
0
  st = locate_simplify_state(revs, commit);
3563
3564
  /*
3565
   * Have we handled this one?
3566
   */
3567
0
  if (st->simplified)
3568
0
    return tail;
3569
3570
  /*
3571
   * An UNINTERESTING commit simplifies to itself, so does a
3572
   * root commit.  We do not rewrite parents of such commit
3573
   * anyway.
3574
   */
3575
0
  if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
3576
0
    st->simplified = commit;
3577
0
    return tail;
3578
0
  }
3579
3580
  /*
3581
   * Do we know what commit all of our parents that matter
3582
   * should be rewritten to?  Otherwise we are not ready to
3583
   * rewrite this one yet.
3584
   */
3585
0
  for (cnt = 0, p = commit->parents; p; p = p->next) {
3586
0
    pst = locate_simplify_state(revs, p->item);
3587
0
    if (!pst->simplified) {
3588
0
      tail = &commit_list_insert(p->item, tail)->next;
3589
0
      cnt++;
3590
0
    }
3591
0
    if (revs->first_parent_only)
3592
0
      break;
3593
0
  }
3594
0
  if (cnt) {
3595
0
    tail = &commit_list_insert(commit, tail)->next;
3596
0
    return tail;
3597
0
  }
3598
3599
  /*
3600
   * Rewrite our list of parents. Note that this cannot
3601
   * affect our TREESAME flags in any way - a commit is
3602
   * always TREESAME to its simplification.
3603
   */
3604
0
  for (p = commit->parents; p; p = p->next) {
3605
0
    pst = locate_simplify_state(revs, p->item);
3606
0
    p->item = pst->simplified;
3607
0
    if (revs->first_parent_only)
3608
0
      break;
3609
0
  }
3610
3611
0
  if (revs->first_parent_only)
3612
0
    cnt = 1;
3613
0
  else
3614
0
    cnt = remove_duplicate_parents(revs, commit);
3615
3616
  /*
3617
   * It is possible that we are a merge and one side branch
3618
   * does not have any commit that touches the given paths;
3619
   * in such a case, the immediate parent from that branch
3620
   * will be rewritten to be the merge base.
3621
   *
3622
   *      o----X    X: the commit we are looking at;
3623
   *     /    /   o: a commit that touches the paths;
3624
   * ---o----'
3625
   *
3626
   * Further, a merge of an independent branch that doesn't
3627
   * touch the path will reduce to a treesame root parent:
3628
   *
3629
   *  ----o----X    X: the commit we are looking at;
3630
   *          /   o: a commit that touches the paths;
3631
   *         r    r: a root commit not touching the paths
3632
   *
3633
   * Detect and simplify both cases.
3634
   */
3635
0
  if (1 < cnt) {
3636
0
    int marked = mark_redundant_parents(commit);
3637
0
    marked += mark_treesame_root_parents(commit);
3638
0
    if (marked)
3639
0
      marked -= leave_one_treesame_to_parent(revs, commit);
3640
0
    if (marked)
3641
0
      cnt = remove_marked_parents(revs, commit);
3642
0
  }
3643
3644
  /*
3645
   * A commit simplifies to itself if it is a root, if it is
3646
   * UNINTERESTING, if it touches the given paths, or if it is a
3647
   * merge and its parents don't simplify to one relevant commit
3648
   * (the first two cases are already handled at the beginning of
3649
   * this function).
3650
   *
3651
   * Otherwise, it simplifies to what its sole relevant parent
3652
   * simplifies to.
3653
   */
3654
0
  if (!cnt ||
3655
0
      (commit->object.flags & UNINTERESTING) ||
3656
0
      !(commit->object.flags & TREESAME) ||
3657
0
      (parent = one_relevant_parent(revs, commit->parents)) == NULL ||
3658
0
      (revs->show_pulls && (commit->object.flags & PULL_MERGE)))
3659
0
    st->simplified = commit;
3660
0
  else {
3661
0
    pst = locate_simplify_state(revs, parent);
3662
0
    st->simplified = pst->simplified;
3663
0
  }
3664
0
  return tail;
3665
0
}
3666
3667
static void simplify_merges(struct rev_info *revs)
3668
0
{
3669
0
  struct commit_list *list, *next;
3670
0
  struct commit_list *yet_to_do, **tail;
3671
0
  struct commit *commit;
3672
3673
0
  if (!revs->prune)
3674
0
    return;
3675
3676
  /* feed the list reversed */
3677
0
  yet_to_do = NULL;
3678
0
  for (list = revs->commits; list; list = next) {
3679
0
    commit = list->item;
3680
0
    next = list->next;
3681
    /*
3682
     * Do not free(list) here yet; the original list
3683
     * is used later in this function.
3684
     */
3685
0
    commit_list_insert(commit, &yet_to_do);
3686
0
  }
3687
0
  while (yet_to_do) {
3688
0
    list = yet_to_do;
3689
0
    yet_to_do = NULL;
3690
0
    tail = &yet_to_do;
3691
0
    while (list) {
3692
0
      commit = pop_commit(&list);
3693
0
      tail = simplify_one(revs, commit, tail);
3694
0
    }
3695
0
  }
3696
3697
  /* clean up the result, removing the simplified ones */
3698
0
  list = revs->commits;
3699
0
  revs->commits = NULL;
3700
0
  tail = &revs->commits;
3701
0
  while (list) {
3702
0
    struct merge_simplify_state *st;
3703
3704
0
    commit = pop_commit(&list);
3705
0
    st = locate_simplify_state(revs, commit);
3706
0
    if (st->simplified == commit)
3707
0
      tail = &commit_list_insert(commit, tail)->next;
3708
0
  }
3709
0
}
3710
3711
static void set_children(struct rev_info *revs)
3712
0
{
3713
0
  struct commit_list *l;
3714
0
  for (l = revs->commits; l; l = l->next) {
3715
0
    struct commit *commit = l->item;
3716
0
    struct commit_list *p;
3717
3718
0
    for (p = commit->parents; p; p = p->next)
3719
0
      add_child(revs, p->item, commit);
3720
0
  }
3721
0
}
3722
3723
void reset_revision_walk(void)
3724
0
{
3725
0
  clear_object_flags(the_repository,
3726
0
         SEEN | ADDED | SHOWN | TOPO_WALK_EXPLORED | TOPO_WALK_INDEGREE);
3727
0
}
3728
3729
static int mark_uninteresting(const struct object_id *oid,
3730
            struct object_info *oi UNUSED,
3731
            void *cb)
3732
0
{
3733
0
  struct rev_info *revs = cb;
3734
0
  struct object *o = lookup_unknown_object(revs->repo, oid);
3735
0
  o->flags |= UNINTERESTING | SEEN;
3736
0
  return 0;
3737
0
}
3738
3739
0
define_commit_slab(indegree_slab, int);
Unexecuted instantiation: revision.c:clear_indegree_slab
Unexecuted instantiation: revision.c:init_indegree_slab_with_stride
Unexecuted instantiation: revision.c:indegree_slab_at_peek
3740
0
define_commit_slab(author_date_slab, timestamp_t);
Unexecuted instantiation: revision.c:clear_author_date_slab
Unexecuted instantiation: revision.c:init_author_date_slab_with_stride
3741
0
3742
0
struct topo_walk_info {
3743
0
  timestamp_t min_generation;
3744
0
  struct prio_queue explore_queue;
3745
0
  struct prio_queue indegree_queue;
3746
0
  struct prio_queue topo_queue;
3747
0
  struct indegree_slab indegree;
3748
0
  struct author_date_slab author_date;
3749
0
};
3750
0
3751
0
static int topo_walk_atexit_registered;
3752
0
static unsigned int count_explore_walked;
3753
0
static unsigned int count_indegree_walked;
3754
0
static unsigned int count_topo_walked;
3755
0
3756
0
static void trace2_topo_walk_statistics_atexit(void)
3757
0
{
3758
0
  struct json_writer jw = JSON_WRITER_INIT;
3759
3760
0
  jw_object_begin(&jw, 0);
3761
0
  jw_object_intmax(&jw, "count_explore_walked", count_explore_walked);
3762
0
  jw_object_intmax(&jw, "count_indegree_walked", count_indegree_walked);
3763
0
  jw_object_intmax(&jw, "count_topo_walked", count_topo_walked);
3764
0
  jw_end(&jw);
3765
3766
0
  trace2_data_json("topo_walk", the_repository, "statistics", &jw);
3767
3768
0
  jw_release(&jw);
3769
0
}
3770
3771
static inline void test_flag_and_insert(struct prio_queue *q, struct commit *c, int flag)
3772
0
{
3773
0
  if (c->object.flags & flag)
3774
0
    return;
3775
3776
0
  c->object.flags |= flag;
3777
0
  prio_queue_put(q, c);
3778
0
}
3779
3780
static void explore_walk_step(struct rev_info *revs)
3781
0
{
3782
0
  struct topo_walk_info *info = revs->topo_walk_info;
3783
0
  struct commit_list *p;
3784
0
  struct commit *c = prio_queue_get(&info->explore_queue);
3785
3786
0
  if (!c)
3787
0
    return;
3788
3789
0
  if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3790
0
    return;
3791
3792
0
  count_explore_walked++;
3793
3794
0
  if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3795
0
    record_author_date(&info->author_date, c);
3796
3797
0
  if (revs->max_age != -1 && (c->date < revs->max_age))
3798
0
    c->object.flags |= UNINTERESTING;
3799
3800
0
  if (process_parents(revs, c, NULL) < 0)
3801
0
    return;
3802
3803
0
  if (c->object.flags & UNINTERESTING)
3804
0
    mark_parents_uninteresting(revs, c);
3805
3806
0
  for (p = c->parents; p; p = p->next)
3807
0
    test_flag_and_insert(&info->explore_queue, p->item, TOPO_WALK_EXPLORED);
3808
0
}
3809
3810
static void explore_to_depth(struct rev_info *revs,
3811
           timestamp_t gen_cutoff)
3812
0
{
3813
0
  struct topo_walk_info *info = revs->topo_walk_info;
3814
0
  struct commit *c;
3815
0
  while ((c = prio_queue_peek(&info->explore_queue)) &&
3816
0
         commit_graph_generation(c) >= gen_cutoff)
3817
0
    explore_walk_step(revs);
3818
0
}
3819
3820
static void indegree_walk_step(struct rev_info *revs)
3821
0
{
3822
0
  struct commit_list *p;
3823
0
  struct topo_walk_info *info = revs->topo_walk_info;
3824
0
  struct commit *c = prio_queue_get(&info->indegree_queue);
3825
3826
0
  if (!c)
3827
0
    return;
3828
3829
0
  if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3830
0
    return;
3831
3832
0
  count_indegree_walked++;
3833
3834
0
  explore_to_depth(revs, commit_graph_generation(c));
3835
3836
0
  for (p = c->parents; p; p = p->next) {
3837
0
    struct commit *parent = p->item;
3838
0
    int *pi = indegree_slab_at(&info->indegree, parent);
3839
3840
0
    if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3841
0
      return;
3842
3843
0
    if (*pi)
3844
0
      (*pi)++;
3845
0
    else
3846
0
      *pi = 2;
3847
3848
0
    test_flag_and_insert(&info->indegree_queue, parent, TOPO_WALK_INDEGREE);
3849
3850
0
    if (revs->first_parent_only)
3851
0
      return;
3852
0
  }
3853
0
}
3854
3855
static void compute_indegrees_to_depth(struct rev_info *revs,
3856
               timestamp_t gen_cutoff)
3857
0
{
3858
0
  struct topo_walk_info *info = revs->topo_walk_info;
3859
0
  struct commit *c;
3860
0
  while ((c = prio_queue_peek(&info->indegree_queue)) &&
3861
0
         commit_graph_generation(c) >= gen_cutoff)
3862
0
    indegree_walk_step(revs);
3863
0
}
3864
3865
static void release_revisions_topo_walk_info(struct topo_walk_info *info)
3866
0
{
3867
0
  if (!info)
3868
0
    return;
3869
0
  clear_prio_queue(&info->explore_queue);
3870
0
  clear_prio_queue(&info->indegree_queue);
3871
0
  clear_prio_queue(&info->topo_queue);
3872
0
  clear_indegree_slab(&info->indegree);
3873
0
  clear_author_date_slab(&info->author_date);
3874
0
  free(info);
3875
0
}
3876
3877
static void reset_topo_walk(struct rev_info *revs)
3878
0
{
3879
0
  release_revisions_topo_walk_info(revs->topo_walk_info);
3880
0
  revs->topo_walk_info = NULL;
3881
0
}
3882
3883
static void init_topo_walk(struct rev_info *revs)
3884
0
{
3885
0
  struct topo_walk_info *info;
3886
0
  struct commit_list *list;
3887
0
  if (revs->topo_walk_info)
3888
0
    reset_topo_walk(revs);
3889
3890
0
  revs->topo_walk_info = xmalloc(sizeof(struct topo_walk_info));
3891
0
  info = revs->topo_walk_info;
3892
0
  memset(info, 0, sizeof(struct topo_walk_info));
3893
3894
0
  init_indegree_slab(&info->indegree);
3895
0
  memset(&info->explore_queue, 0, sizeof(info->explore_queue));
3896
0
  memset(&info->indegree_queue, 0, sizeof(info->indegree_queue));
3897
0
  memset(&info->topo_queue, 0, sizeof(info->topo_queue));
3898
3899
0
  switch (revs->sort_order) {
3900
0
  default: /* REV_SORT_IN_GRAPH_ORDER */
3901
0
    info->topo_queue.compare = NULL;
3902
0
    break;
3903
0
  case REV_SORT_BY_COMMIT_DATE:
3904
0
    info->topo_queue.compare = compare_commits_by_commit_date;
3905
0
    break;
3906
0
  case REV_SORT_BY_AUTHOR_DATE:
3907
0
    init_author_date_slab(&info->author_date);
3908
0
    info->topo_queue.compare = compare_commits_by_author_date;
3909
0
    info->topo_queue.cb_data = &info->author_date;
3910
0
    break;
3911
0
  }
3912
3913
0
  info->explore_queue.compare = compare_commits_by_gen_then_commit_date;
3914
0
  info->indegree_queue.compare = compare_commits_by_gen_then_commit_date;
3915
3916
0
  info->min_generation = GENERATION_NUMBER_INFINITY;
3917
0
  for (list = revs->commits; list; list = list->next) {
3918
0
    struct commit *c = list->item;
3919
0
    timestamp_t generation;
3920
3921
0
    if (repo_parse_commit_gently(revs->repo, c, 1))
3922
0
      continue;
3923
3924
0
    test_flag_and_insert(&info->explore_queue, c, TOPO_WALK_EXPLORED);
3925
0
    test_flag_and_insert(&info->indegree_queue, c, TOPO_WALK_INDEGREE);
3926
3927
0
    generation = commit_graph_generation(c);
3928
0
    if (generation < info->min_generation)
3929
0
      info->min_generation = generation;
3930
3931
0
    *(indegree_slab_at(&info->indegree, c)) = 1;
3932
3933
0
    if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3934
0
      record_author_date(&info->author_date, c);
3935
0
  }
3936
0
  compute_indegrees_to_depth(revs, info->min_generation);
3937
3938
0
  for (list = revs->commits; list; list = list->next) {
3939
0
    struct commit *c = list->item;
3940
3941
0
    if (*(indegree_slab_at(&info->indegree, c)) == 1)
3942
0
      prio_queue_put(&info->topo_queue, c);
3943
0
  }
3944
3945
  /*
3946
   * This is unfortunate; the initial tips need to be shown
3947
   * in the order given from the revision traversal machinery.
3948
   */
3949
0
  if (revs->sort_order == REV_SORT_IN_GRAPH_ORDER)
3950
0
    prio_queue_reverse(&info->topo_queue);
3951
3952
0
  if (trace2_is_enabled() && !topo_walk_atexit_registered) {
3953
0
    atexit(trace2_topo_walk_statistics_atexit);
3954
0
    topo_walk_atexit_registered = 1;
3955
0
  }
3956
0
}
3957
3958
static struct commit *next_topo_commit(struct rev_info *revs)
3959
0
{
3960
0
  struct commit *c;
3961
0
  struct topo_walk_info *info = revs->topo_walk_info;
3962
3963
  /* pop next off of topo_queue */
3964
0
  c = prio_queue_get(&info->topo_queue);
3965
3966
0
  if (c)
3967
0
    *(indegree_slab_at(&info->indegree, c)) = 0;
3968
3969
0
  return c;
3970
0
}
3971
3972
static void expand_topo_walk(struct rev_info *revs, struct commit *commit)
3973
0
{
3974
0
  struct commit_list *p;
3975
0
  struct topo_walk_info *info = revs->topo_walk_info;
3976
0
  if (process_parents(revs, commit, NULL) < 0) {
3977
0
    if (!revs->ignore_missing_links)
3978
0
      die("Failed to traverse parents of commit %s",
3979
0
          oid_to_hex(&commit->object.oid));
3980
0
  }
3981
3982
0
  count_topo_walked++;
3983
3984
0
  for (p = commit->parents; p; p = p->next) {
3985
0
    struct commit *parent = p->item;
3986
0
    int *pi;
3987
0
    timestamp_t generation;
3988
3989
0
    if (parent->object.flags & UNINTERESTING)
3990
0
      continue;
3991
3992
0
    if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3993
0
      continue;
3994
3995
0
    generation = commit_graph_generation(parent);
3996
0
    if (generation < info->min_generation) {
3997
0
      info->min_generation = generation;
3998
0
      compute_indegrees_to_depth(revs, info->min_generation);
3999
0
    }
4000
4001
0
    pi = indegree_slab_at(&info->indegree, parent);
4002
4003
0
    (*pi)--;
4004
0
    if (*pi == 1)
4005
0
      prio_queue_put(&info->topo_queue, parent);
4006
4007
0
    if (revs->first_parent_only)
4008
0
      return;
4009
0
  }
4010
0
}
4011
4012
void rev_info_commit_list_to_queue(struct rev_info *revs)
4013
0
{
4014
0
  while (revs->commits)
4015
0
    prio_queue_put(&revs->commit_queue, pop_commit(&revs->commits));
4016
0
}
4017
4018
4019
int prepare_revision_walk(struct rev_info *revs)
4020
0
{
4021
0
  int i;
4022
0
  struct object_array old_pending;
4023
0
  struct commit_list **next = &revs->commits;
4024
4025
0
  memcpy(&old_pending, &revs->pending, sizeof(old_pending));
4026
0
  revs->pending.nr = 0;
4027
0
  revs->pending.alloc = 0;
4028
0
  revs->pending.objects = NULL;
4029
0
  for (i = 0; i < old_pending.nr; i++) {
4030
0
    struct object_array_entry *e = old_pending.objects + i;
4031
0
    struct commit *commit = handle_commit(revs, e);
4032
0
    if (commit) {
4033
0
      if (!(commit->object.flags & SEEN)) {
4034
0
        commit->object.flags |= SEEN;
4035
0
        next = commit_list_append(commit, next);
4036
0
      }
4037
0
    }
4038
0
  }
4039
0
  object_array_clear(&old_pending);
4040
4041
  /* Signal whether we need per-parent treesame decoration */
4042
0
  if (revs->simplify_merges ||
4043
0
      (revs->limited && limiting_can_increase_treesame(revs)))
4044
0
    revs->treesame.name = "treesame";
4045
4046
0
  if (revs->exclude_promisor_objects)
4047
0
    odb_for_each_object(revs->repo->objects, NULL, mark_uninteresting,
4048
0
            revs, ODB_FOR_EACH_OBJECT_PROMISOR_ONLY);
4049
4050
0
  if (!revs->reflog_info)
4051
0
    prepare_to_use_bloom_filter(revs);
4052
0
  if (!revs->unsorted_input)
4053
0
    commit_list_sort_by_date(&revs->commits);
4054
0
  if (revs->no_walk)
4055
0
    return 0;
4056
0
  if (revs->limited) {
4057
0
    if (limit_list(revs) < 0)
4058
0
      return -1;
4059
0
    if (revs->topo_order)
4060
0
      sort_in_topological_order(&revs->commits, revs->sort_order);
4061
0
  } else if (revs->topo_order)
4062
0
    init_topo_walk(revs);
4063
0
  if (revs->line_level_traverse && want_ancestry(revs))
4064
    /*
4065
     * At the moment we can only do line-level log with parent
4066
     * rewriting by performing this expensive pre-filtering step.
4067
     * If parent rewriting is not requested, then we rather
4068
     * perform the line-level log filtering during the regular
4069
     * history traversal.
4070
     */
4071
0
    line_log_filter(revs);
4072
0
  if (revs->simplify_merges)
4073
0
    simplify_merges(revs);
4074
0
  if (revs->children.name)
4075
0
    set_children(revs);
4076
4077
0
  return 0;
4078
0
}
4079
4080
static enum rewrite_result rewrite_one_1(struct rev_info *revs,
4081
           struct commit **pp,
4082
           struct prio_queue *queue)
4083
0
{
4084
0
  for (;;) {
4085
0
    struct commit *p = *pp;
4086
0
    if (!revs->limited)
4087
0
      if (process_parents(revs, p, queue) < 0)
4088
0
        return rewrite_one_error;
4089
0
    if (p->object.flags & UNINTERESTING)
4090
0
      return rewrite_one_ok;
4091
0
    if (!(p->object.flags & TREESAME))
4092
0
      return rewrite_one_ok;
4093
0
    if (!p->parents)
4094
0
      return rewrite_one_noparents;
4095
0
    if (!(p = one_relevant_parent(revs, p->parents)))
4096
0
      return rewrite_one_ok;
4097
0
    *pp = p;
4098
0
  }
4099
0
}
4100
4101
static void merge_queue_into_prio_queue(struct prio_queue *from,
4102
          struct prio_queue *to)
4103
0
{
4104
0
  struct commit *item;
4105
0
  while ((item = prio_queue_get(from)))
4106
0
    prio_queue_put(to, item);
4107
0
}
4108
4109
static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
4110
0
{
4111
0
  struct prio_queue queue = { compare_commits_by_commit_date };
4112
0
  enum rewrite_result ret = rewrite_one_1(revs, pp, &queue);
4113
0
  merge_queue_into_prio_queue(&queue, &revs->commit_queue);
4114
0
  clear_prio_queue(&queue);
4115
0
  return ret;
4116
0
}
4117
4118
int rewrite_parents(struct rev_info *revs, struct commit *commit,
4119
  rewrite_parent_fn_t rewrite_parent)
4120
0
{
4121
0
  struct commit_list **pp = &commit->parents;
4122
0
  while (*pp) {
4123
0
    struct commit_list *parent = *pp;
4124
0
    switch (rewrite_parent(revs, &parent->item)) {
4125
0
    case rewrite_one_ok:
4126
0
      break;
4127
0
    case rewrite_one_noparents:
4128
0
      *pp = parent->next;
4129
0
      free(parent);
4130
0
      continue;
4131
0
    case rewrite_one_error:
4132
0
      return -1;
4133
0
    }
4134
0
    pp = &parent->next;
4135
0
  }
4136
0
  remove_duplicate_parents(revs, commit);
4137
0
  return 0;
4138
0
}
4139
4140
static int commit_match(struct commit *commit, struct rev_info *opt)
4141
0
{
4142
0
  int retval;
4143
0
  const char *encoding;
4144
0
  const char *message;
4145
0
  struct strbuf buf = STRBUF_INIT;
4146
4147
0
  if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
4148
0
    return 1;
4149
4150
  /* Prepend "fake" headers as needed */
4151
0
  if (opt->grep_filter.use_reflog_filter) {
4152
0
    strbuf_addstr(&buf, "reflog ");
4153
0
    get_reflog_message(&buf, opt->reflog_info);
4154
0
    strbuf_addch(&buf, '\n');
4155
0
  }
4156
4157
  /*
4158
   * We grep in the user's output encoding, under the assumption that it
4159
   * is the encoding they are most likely to write their grep pattern
4160
   * for. In addition, it means we will match the "notes" encoding below,
4161
   * so we will not end up with a buffer that has two different encodings
4162
   * in it.
4163
   */
4164
0
  encoding = get_log_output_encoding();
4165
0
  message = repo_logmsg_reencode(the_repository, commit, NULL, encoding);
4166
4167
  /* Copy the commit to temporary if we are using "fake" headers */
4168
0
  if (buf.len)
4169
0
    strbuf_addstr(&buf, message);
4170
4171
0
  if (opt->grep_filter.header_list && opt->mailmap) {
4172
0
    const char *commit_headers[] = { "author ", "committer ", NULL };
4173
4174
0
    if (!buf.len)
4175
0
      strbuf_addstr(&buf, message);
4176
4177
0
    apply_mailmap_to_header(&buf, commit_headers, opt->mailmap);
4178
0
  }
4179
4180
  /* Append "fake" message parts as needed */
4181
0
  if (opt->show_notes) {
4182
0
    if (!buf.len)
4183
0
      strbuf_addstr(&buf, message);
4184
0
    format_display_notes(&commit->object.oid, &buf, encoding, 1);
4185
0
  }
4186
4187
  /*
4188
   * Find either in the original commit message, or in the temporary.
4189
   * Note that we cast away the constness of "message" here. It is
4190
   * const because it may come from the cached commit buffer. That's OK,
4191
   * because we know that it is modifiable heap memory, and that while
4192
   * grep_buffer may modify it for speed, it will restore any
4193
   * changes before returning.
4194
   */
4195
0
  if (buf.len)
4196
0
    retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
4197
0
  else
4198
0
    retval = grep_buffer(&opt->grep_filter,
4199
0
             (char *)message, strlen(message));
4200
0
  strbuf_release(&buf);
4201
0
  repo_unuse_commit_buffer(the_repository, commit, message);
4202
0
  return retval;
4203
0
}
4204
4205
static inline int want_ancestry(const struct rev_info *revs)
4206
0
{
4207
0
  return (revs->rewrite_parents || revs->children.name);
4208
0
}
4209
4210
/*
4211
 * Return a timestamp to be used for --since/--until comparisons for this
4212
 * commit, based on the revision options.
4213
 */
4214
static timestamp_t comparison_date(const struct rev_info *revs,
4215
           struct commit *commit)
4216
0
{
4217
0
  return revs->reflog_info ?
4218
0
    get_reflog_timestamp(revs->reflog_info) :
4219
0
    commit->date;
4220
0
}
4221
4222
/*
4223
 * Whether the commit is ignored by the cheap checks that read only its
4224
 * traversal flags and pack membership (e.g. already shown, or marked
4225
 * uninteresting), before any check that examines the commit's date,
4226
 * parents, message, or diff.
4227
 */
4228
static int commit_early_ignore(struct rev_info *revs, struct commit *commit)
4229
0
{
4230
0
  if (commit->object.flags & SHOWN)
4231
0
    return 1;
4232
0
  if (revs->maximal_only && (commit->object.flags & CHILD_VISITED))
4233
0
    return 1;
4234
0
  if (revs->unpacked && has_object_pack(revs->repo, &commit->object.oid))
4235
0
    return 1;
4236
0
  if (revs->no_kept_objects &&
4237
0
      has_object_kept_pack(revs->repo, &commit->object.oid,
4238
0
         revs->keep_pack_cache_flags))
4239
0
    return 1;
4240
0
  if (commit->object.flags & UNINTERESTING)
4241
0
    return 1;
4242
0
  return 0;
4243
0
}
4244
4245
/*
4246
 * Decide whether this commit is shown or ignored.  Keep it a pure
4247
 * predicate: callers such as the commit graph depend on it having no
4248
 * side effects, so per-commit mutations (such as -L range tracking)
4249
 * belong in the caller, simplify_commit(), not here.
4250
 */
4251
enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
4252
0
{
4253
0
  if (commit_early_ignore(revs, commit))
4254
0
    return commit_ignore;
4255
0
  if (revs->min_age != -1 &&
4256
0
      comparison_date(revs, commit) > revs->min_age)
4257
0
      return commit_ignore;
4258
0
  if (revs->max_age_as_filter != -1 &&
4259
0
      comparison_date(revs, commit) < revs->max_age_as_filter)
4260
0
      return commit_ignore;
4261
0
  if (revs->min_parents || (revs->max_parents >= 0)) {
4262
0
    int n = commit_list_count(commit->parents);
4263
0
    if ((n < revs->min_parents) ||
4264
0
        ((revs->max_parents >= 0) && (n > revs->max_parents)))
4265
0
      return commit_ignore;
4266
0
  }
4267
0
  if (!commit_match(commit, revs))
4268
0
    return commit_ignore;
4269
0
  if (revs->prune && revs->dense) {
4270
    /* Commit without changes? */
4271
0
    if (commit->object.flags & TREESAME) {
4272
0
      int n;
4273
0
      struct commit_list *p;
4274
      /* drop merges unless we want parenthood */
4275
0
      if (!want_ancestry(revs))
4276
0
        return commit_ignore;
4277
4278
0
      if (revs->show_pulls && (commit->object.flags & PULL_MERGE))
4279
0
        return commit_show;
4280
4281
      /*
4282
       * If we want ancestry, then need to keep any merges
4283
       * between relevant commits to tie together topology.
4284
       * For consistency with TREESAME and simplification
4285
       * use "relevant" here rather than just INTERESTING,
4286
       * to treat bottom commit(s) as part of the topology.
4287
       */
4288
0
      for (n = 0, p = commit->parents; p; p = p->next)
4289
0
        if (relevant_commit(p->item))
4290
0
          if (++n >= 2)
4291
0
            return commit_show;
4292
0
      return commit_ignore;
4293
0
    }
4294
0
  }
4295
0
  return commit_show;
4296
0
}
4297
4298
0
define_commit_slab(saved_parents, struct commit_list *);
Unexecuted instantiation: revision.c:saved_parents_at_peek
Unexecuted instantiation: revision.c:init_saved_parents_with_stride
Unexecuted instantiation: revision.c:clear_saved_parents
4299
0
4300
0
#define EMPTY_PARENT_LIST ((struct commit_list *)-1)
4301
4302
/*
4303
 * You may only call save_parents() once per commit (this is checked
4304
 * for non-root commits).
4305
 */
4306
static void save_parents(struct rev_info *revs, struct commit *commit)
4307
0
{
4308
0
  struct commit_list **pp;
4309
4310
0
  if (!revs->saved_parents_slab) {
4311
0
    revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
4312
0
    init_saved_parents(revs->saved_parents_slab);
4313
0
  }
4314
4315
0
  pp = saved_parents_at(revs->saved_parents_slab, commit);
4316
4317
  /*
4318
   * When walking with reflogs, we may visit the same commit
4319
   * several times: once for each appearance in the reflog.
4320
   *
4321
   * In this case, save_parents() will be called multiple times.
4322
   * We want to keep only the first set of parents.  We need to
4323
   * store a sentinel value for an empty (i.e., NULL) parent
4324
   * list to distinguish it from a not-yet-saved list, however.
4325
   */
4326
0
  if (*pp)
4327
0
    return;
4328
0
  if (commit->parents)
4329
0
    *pp = commit_list_copy(commit->parents);
4330
0
  else
4331
0
    *pp = EMPTY_PARENT_LIST;
4332
0
}
4333
4334
static void free_saved_parent(struct commit_list **parents)
4335
0
{
4336
0
  if (*parents != EMPTY_PARENT_LIST)
4337
0
    commit_list_free(*parents);
4338
0
}
4339
4340
static void free_saved_parents(struct rev_info *revs)
4341
0
{
4342
0
  if (!revs->saved_parents_slab)
4343
0
    return;
4344
0
  deep_clear_saved_parents(revs->saved_parents_slab, free_saved_parent);
4345
0
  FREE_AND_NULL(revs->saved_parents_slab);
4346
0
}
4347
4348
struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
4349
0
{
4350
0
  struct commit_list *parents;
4351
4352
0
  if (!revs->saved_parents_slab)
4353
0
    return commit->parents;
4354
4355
0
  parents = *saved_parents_at(revs->saved_parents_slab, commit);
4356
0
  if (parents == EMPTY_PARENT_LIST)
4357
0
    return NULL;
4358
0
  return parents;
4359
0
}
4360
4361
enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
4362
0
{
4363
0
  enum commit_action action;
4364
4365
  /*
4366
   * For a line-level log without parent rewriting, fold each commit's
4367
   * ranges as the walk reaches it (parent rewriting does this eagerly in
4368
   * prepare_revision_walk()).  Fold before get_commit_action() so the
4369
   * ranges carry across a commit that a later, cheaper check ignores;
4370
   * the commit_early_ignore() guard skips a commit get_commit_action()
4371
   * would ignore outright.
4372
   */
4373
0
  if (revs->line_level_traverse && !want_ancestry(revs) &&
4374
0
      !commit_early_ignore(revs, commit)) {
4375
0
    if (!line_log_process_ranges_arbitrary_commit(revs, commit))
4376
0
      return commit_ignore;
4377
0
  }
4378
4379
0
  action = get_commit_action(revs, commit);
4380
4381
0
  if (action == commit_show &&
4382
0
      revs->prune && revs->dense && want_ancestry(revs)) {
4383
    /*
4384
     * --full-diff on simplified parents is no good: it
4385
     * will show spurious changes from the commits that
4386
     * were elided.  So we save the parents on the side
4387
     * when --full-diff is in effect.
4388
     */
4389
0
    if (revs->full_diff)
4390
0
      save_parents(revs, commit);
4391
0
    if (rewrite_parents(revs, commit, rewrite_one) < 0)
4392
0
      return commit_error;
4393
0
  }
4394
0
  return action;
4395
0
}
4396
4397
static void track_linear(struct rev_info *revs, struct commit *commit)
4398
0
{
4399
0
  if (revs->track_first_time) {
4400
0
    revs->linear = 1;
4401
0
    revs->track_first_time = 0;
4402
0
  } else {
4403
0
    struct commit_list *p;
4404
0
    for (p = revs->previous_parents; p; p = p->next)
4405
0
      if (p->item == NULL || /* first commit */
4406
0
          oideq(&p->item->object.oid, &commit->object.oid))
4407
0
        break;
4408
0
    revs->linear = p != NULL;
4409
0
  }
4410
0
  if (revs->reverse) {
4411
0
    if (revs->linear)
4412
0
      commit->object.flags |= TRACK_LINEAR;
4413
0
  }
4414
0
  commit_list_free(revs->previous_parents);
4415
0
  revs->previous_parents = commit_list_copy(commit->parents);
4416
0
}
4417
4418
enum rev_walk_mode {
4419
  REV_WALK_REFLOG,
4420
  REV_WALK_TOPO,
4421
  REV_WALK_LIMITED,
4422
  REV_WALK_NO_WALK,
4423
  REV_WALK_STREAMING,
4424
};
4425
4426
static enum rev_walk_mode get_walk_mode(struct rev_info *revs)
4427
0
{
4428
0
  if (revs->reflog_info)
4429
0
    return REV_WALK_REFLOG;
4430
0
  if (revs->topo_walk_info)
4431
0
    return REV_WALK_TOPO;
4432
0
  if (revs->limited)
4433
0
    return REV_WALK_LIMITED;
4434
0
  if (revs->no_walk)
4435
0
    return REV_WALK_NO_WALK;
4436
0
  return REV_WALK_STREAMING;
4437
0
}
4438
4439
static struct commit *get_revision_1(struct rev_info *revs)
4440
0
{
4441
0
  enum rev_walk_mode mode = get_walk_mode(revs);
4442
4443
0
  if (mode == REV_WALK_STREAMING && revs->commits)
4444
0
    rev_info_commit_list_to_queue(revs);
4445
4446
0
  while (1) {
4447
0
    struct commit *commit;
4448
4449
0
    switch (mode) {
4450
0
    case REV_WALK_REFLOG:
4451
0
      commit = next_reflog_entry(revs->reflog_info);
4452
0
      break;
4453
0
    case REV_WALK_TOPO:
4454
0
      commit = next_topo_commit(revs);
4455
0
      break;
4456
0
    case REV_WALK_LIMITED:
4457
0
    case REV_WALK_NO_WALK:
4458
0
      commit = pop_commit(&revs->commits);
4459
0
      break;
4460
0
    case REV_WALK_STREAMING:
4461
0
      commit = prio_queue_get(&revs->commit_queue);
4462
0
      break;
4463
0
    }
4464
4465
0
    if (!commit)
4466
0
      return NULL;
4467
4468
0
    if (mode == REV_WALK_REFLOG)
4469
0
      commit->object.flags &= ~(ADDED | SEEN | SHOWN);
4470
4471
    /*
4472
     * If we haven't done the list limiting, we need to look at
4473
     * the parents here. We also need to do the date-based limiting
4474
     * that we'd otherwise have done in limit_list().
4475
     */
4476
0
    if (mode != REV_WALK_LIMITED &&
4477
0
        revs->max_age != -1 &&
4478
0
        comparison_date(revs, commit) < revs->max_age)
4479
0
      continue;
4480
4481
0
    switch (mode) {
4482
0
    case REV_WALK_REFLOG:
4483
0
    case REV_WALK_NO_WALK:
4484
0
      try_to_simplify_commit(revs, commit);
4485
0
      break;
4486
0
    case REV_WALK_TOPO:
4487
0
      expand_topo_walk(revs, commit);
4488
0
      break;
4489
0
    case REV_WALK_STREAMING:
4490
0
      if (process_parents(revs, commit,
4491
0
              &revs->commit_queue) < 0) {
4492
0
        if (!revs->ignore_missing_links)
4493
0
          die("Failed to traverse parents of commit %s",
4494
0
              oid_to_hex(&commit->object.oid));
4495
0
      }
4496
0
      break;
4497
0
    case REV_WALK_LIMITED:
4498
0
      break;
4499
0
    }
4500
4501
0
    switch (simplify_commit(revs, commit)) {
4502
0
    case commit_ignore:
4503
0
      continue;
4504
0
    case commit_error:
4505
0
      die("Failed to simplify parents of commit %s",
4506
0
          oid_to_hex(&commit->object.oid));
4507
0
    default:
4508
0
      if (revs->track_linear)
4509
0
        track_linear(revs, commit);
4510
0
      return commit;
4511
0
    }
4512
0
  }
4513
0
}
4514
4515
/*
4516
 * Return true for entries that have not yet been shown.  (This is an
4517
 * object_array_each_func_t.)
4518
 */
4519
static int entry_unshown(struct object_array_entry *entry, void *cb_data UNUSED)
4520
0
{
4521
0
  return !(entry->item->flags & SHOWN);
4522
0
}
4523
4524
/*
4525
 * If array is on the verge of a realloc, garbage-collect any entries
4526
 * that have already been shown to try to free up some space.
4527
 */
4528
static void gc_boundary(struct object_array *array)
4529
0
{
4530
0
  if (array->nr == array->alloc)
4531
0
    object_array_filter(array, entry_unshown, NULL);
4532
0
}
4533
4534
static void create_boundary_commit_list(struct rev_info *revs)
4535
0
{
4536
0
  unsigned i;
4537
0
  struct commit *c;
4538
0
  struct object_array *array = &revs->boundary_commits;
4539
0
  struct object_array_entry *objects = array->objects;
4540
4541
  /*
4542
   * If revs->commits is non-NULL at this point, an error occurred in
4543
   * get_revision_1().  Ignore the error and continue printing the
4544
   * boundary commits anyway.  (This is what the code has always
4545
   * done.)
4546
   */
4547
0
  commit_list_free(revs->commits);
4548
0
  revs->commits = NULL;
4549
4550
  /*
4551
   * Put all of the actual boundary commits from revs->boundary_commits
4552
   * into revs->commits
4553
   */
4554
0
  for (i = 0; i < array->nr; i++) {
4555
0
    c = (struct commit *)(objects[i].item);
4556
0
    if (!c)
4557
0
      continue;
4558
0
    if (!(c->object.flags & CHILD_SHOWN))
4559
0
      continue;
4560
0
    if (c->object.flags & (SHOWN | BOUNDARY))
4561
0
      continue;
4562
0
    c->object.flags |= BOUNDARY;
4563
0
    commit_list_insert(c, &revs->commits);
4564
0
  }
4565
4566
  /*
4567
   * If revs->topo_order is set, sort the boundary commits
4568
   * in topological order
4569
   */
4570
0
  sort_in_topological_order(&revs->commits, revs->sort_order);
4571
0
}
4572
4573
static struct commit *get_revision_internal(struct rev_info *revs)
4574
0
{
4575
0
  struct commit *c = NULL;
4576
0
  struct commit_list *l;
4577
4578
0
  if (revs->boundary == 2) {
4579
    /*
4580
     * All of the normal commits have already been returned,
4581
     * and we are now returning boundary commits.
4582
     * create_boundary_commit_list() has populated
4583
     * revs->commits with the remaining commits to return.
4584
     */
4585
0
    c = pop_commit(&revs->commits);
4586
0
    if (c)
4587
0
      c->object.flags |= SHOWN;
4588
0
    return c;
4589
0
  }
4590
4591
  /*
4592
   * If our max_count counter has reached zero, then we are done. We
4593
   * don't simply return NULL because we still might need to show
4594
   * boundary commits. But we want to avoid calling get_revision_1, which
4595
   * might do a considerable amount of work finding the next commit only
4596
   * for us to throw it away.
4597
   *
4598
   * If it is non-zero, then either we don't have a max_count at all
4599
   * (-1), or it is still counting, in which case we decrement.
4600
   */
4601
0
  if (revs->max_count) {
4602
0
    c = get_revision_1(revs);
4603
0
    if (c) {
4604
0
      while (revs->skip_count > 0) {
4605
0
        revs->skip_count--;
4606
0
        c = get_revision_1(revs);
4607
0
        if (!c)
4608
0
          break;
4609
0
        free_commit_buffer(revs->repo->parsed_objects, c);
4610
0
      }
4611
0
    }
4612
4613
0
    if (revs->max_count > 0)
4614
0
      revs->max_count--;
4615
0
  }
4616
4617
0
  if (c)
4618
0
    c->object.flags |= SHOWN;
4619
4620
0
  if (!revs->boundary)
4621
0
    return c;
4622
4623
0
  if (!c) {
4624
    /*
4625
     * get_revision_1() runs out the commits, and
4626
     * we are done computing the boundaries.
4627
     * switch to boundary commits output mode.
4628
     */
4629
0
    revs->boundary = 2;
4630
4631
    /*
4632
     * Update revs->commits to contain the list of
4633
     * boundary commits.
4634
     */
4635
0
    create_boundary_commit_list(revs);
4636
4637
0
    return get_revision_internal(revs);
4638
0
  }
4639
4640
  /*
4641
   * boundary commits are the commits that are parents of the
4642
   * ones we got from get_revision_1() but they themselves are
4643
   * not returned from get_revision_1().  Before returning
4644
   * 'c', we need to mark its parents that they could be boundaries.
4645
   */
4646
4647
0
  for (l = c->parents; l; l = l->next) {
4648
0
    struct object *p;
4649
0
    p = &(l->item->object);
4650
0
    if (p->flags & (CHILD_SHOWN | SHOWN))
4651
0
      continue;
4652
0
    p->flags |= CHILD_SHOWN;
4653
0
    gc_boundary(&revs->boundary_commits);
4654
0
    add_object_array(p, NULL, &revs->boundary_commits);
4655
0
  }
4656
4657
0
  return c;
4658
0
}
4659
4660
static void retrieve_oldest_commits(struct rev_info *revs,
4661
            struct commit_list **queue)
4662
0
{
4663
0
  struct commit *c;
4664
0
  int max_count = revs->max_count;
4665
0
  int queuei_count = 0;
4666
0
  int queueo_count = 0;
4667
0
  struct commit_list *queueo = NULL;
4668
0
  struct commit_list *queuei = NULL;
4669
0
  struct commit_list *reversed_queue = NULL;
4670
0
  struct commit_list *p;
4671
4672
0
  revs->max_count = -1;
4673
0
  while ((c = get_revision_internal(revs))) {
4674
    /*
4675
     * We need to reset SHOWN status otherwise --graph breaks.
4676
     * It is fine to do, get_revision_internal() doesn't consider
4677
     * children commits as they have been already processed and the
4678
     * traversal happens only child to parent.
4679
     *
4680
     * We do this because the --graph machinery relies on the status
4681
     * of the parents to decide how the printing will happen.
4682
     *
4683
     * We can't simply replace this instruction with a
4684
     * graph_update() as it doesn't do the actualy printing, we'd
4685
     * have to remove any commit that goes over the
4686
     * --max-count-oldest limit from revs->graph.
4687
     */
4688
0
    c->object.flags &= ~(SHOWN | CHILD_SHOWN);
4689
0
    commit_list_insert(c, &queuei);
4690
0
    if (!(c->object.flags & BOUNDARY))
4691
0
      queuei_count++;
4692
0
    while (queuei_count + queueo_count > max_count) {
4693
0
      if (!queueo_count) {
4694
0
        while ((c = pop_commit(&queuei))) {
4695
0
          commit_list_insert(c, &queueo);
4696
0
          queueo_count++;
4697
0
        }
4698
0
        queuei_count = 0;
4699
0
      }
4700
0
      c = pop_commit(&queueo);
4701
0
      queueo_count--;
4702
      /* We need to do this otherwise we'll discard the
4703
       * commits that go over the --max-count-oldest limit but
4704
       * not their respective boundaries. This matters only if
4705
       * we're discarding the commit right before the boundary.
4706
       */
4707
0
      for (p = c->parents; p; p = p->next)
4708
0
        p->item->object.flags &= ~CHILD_SHOWN;
4709
0
    }
4710
0
  }
4711
4712
0
  while ((c = pop_commit(&queueo)))
4713
0
    commit_list_insert(c, &reversed_queue);
4714
0
  while ((c = pop_commit(&queuei)))
4715
0
    commit_list_insert(c, &queueo);
4716
0
  while ((c = pop_commit(&queueo)))
4717
0
    commit_list_insert(c, &reversed_queue);
4718
4719
0
  while ((c = pop_commit(&reversed_queue)))
4720
0
    commit_list_insert(c, queue);
4721
0
}
4722
4723
/*
4724
 * Returns the next commit that will be shown, regardless of whether it comes
4725
 * directly from the revision walk or from the list saved by the staged output
4726
 * of --max-count-oldest.
4727
 */
4728
static struct commit *next_commit_to_show(struct rev_info *revs)
4729
0
{
4730
0
  struct commit *c;
4731
0
  struct commit_list *p;
4732
4733
0
  if (!revs->max_count_stage)
4734
0
    return get_revision_internal(revs);
4735
4736
0
  c = pop_commit(&revs->commits);
4737
0
  if (c) {
4738
0
    c->object.flags |= SHOWN;
4739
0
    if (!(c->object.flags & BOUNDARY))
4740
0
      for (p = c->parents; p; p = p->next)
4741
0
        p->item->object.flags |= CHILD_SHOWN;
4742
0
  }
4743
0
  return c;
4744
0
}
4745
4746
struct commit *get_revision(struct rev_info *revs)
4747
0
{
4748
0
  struct commit *c;
4749
0
  struct commit_list *reversed;
4750
0
  struct commit_list *queue = NULL;
4751
4752
0
  if (revs->max_count_type == 1 && !revs->max_count_stage) {
4753
0
    retrieve_oldest_commits(revs, &queue);
4754
0
    commit_list_free(revs->commits);
4755
0
    revs->commits = queue;
4756
0
    revs->max_count_stage = 1;
4757
0
  }
4758
4759
0
  if (revs->reverse) {
4760
0
    reversed = NULL;
4761
0
    if (revs->max_count_type == 1)
4762
0
      while ((c = pop_commit(&revs->commits)))
4763
0
        commit_list_insert(c, &reversed);
4764
0
    else
4765
0
      while ((c = get_revision_internal(revs)))
4766
0
        commit_list_insert(c, &reversed);
4767
0
    commit_list_free(revs->commits);
4768
0
    revs->commits = reversed;
4769
0
    revs->reverse = 0;
4770
0
    revs->reverse_output_stage = 1;
4771
0
  }
4772
4773
0
  if (revs->reverse_output_stage) {
4774
0
    c = pop_commit(&revs->commits);
4775
0
    if (revs->track_linear)
4776
0
      revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
4777
0
    return c;
4778
0
  }
4779
4780
0
  if (revs->graph) {
4781
0
    c = graph_pop_lookahead(revs->graph);
4782
0
    if (!c)
4783
0
      c = next_commit_to_show(revs);
4784
0
  } else {
4785
0
    c = next_commit_to_show(revs);
4786
0
  }
4787
4788
0
  if (c && revs->graph) {
4789
0
    while (graph_get_lookahead_room(revs->graph)) {
4790
0
      struct commit *next = next_commit_to_show(revs);
4791
0
      if (!next)
4792
0
        break;
4793
0
      graph_push_lookahead(revs->graph, next);
4794
0
    }
4795
0
    graph_update(revs->graph, c);
4796
0
  }
4797
4798
0
  if (!c) {
4799
0
    free_saved_parents(revs);
4800
0
    commit_list_free(revs->previous_parents);
4801
0
    revs->previous_parents = NULL;
4802
0
  }
4803
0
  return c;
4804
0
}
4805
4806
const char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
4807
0
{
4808
0
  if (commit->object.flags & BOUNDARY)
4809
0
    return "-";
4810
0
  else if (commit->object.flags & UNINTERESTING)
4811
0
    return "^";
4812
0
  else if (commit->object.flags & PATCHSAME)
4813
0
    return "=";
4814
0
  else if (!revs || revs->left_right) {
4815
0
    if (commit->object.flags & SYMMETRIC_LEFT)
4816
0
      return "<";
4817
0
    else
4818
0
      return ">";
4819
0
  } else if (revs->graph)
4820
0
    return "*";
4821
0
  else if (revs->cherry_mark)
4822
0
    return "+";
4823
0
  return "";
4824
0
}
4825
4826
void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
4827
0
{
4828
0
  const char *mark = get_revision_mark(revs, commit);
4829
0
  if (!strlen(mark))
4830
0
    return;
4831
0
  fputs(mark, stdout);
4832
0
  putchar(' ');
4833
0
}