Coverage Report

Created: 2026-01-09 07:10

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