Coverage Report

Created: 2026-03-31 06:24

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 (repo_parse_tree_gently(the_repository, 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 (repo_parse_tree_gently(the_repository, 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
static void mark_one_parent_uninteresting(struct rev_info *revs, struct commit *commit,
254
            struct commit_stack *pending)
255
0
{
256
0
  struct commit_list *l;
257
258
0
  if (commit->object.flags & UNINTERESTING)
259
0
    return;
260
0
  commit->object.flags |= UNINTERESTING;
261
262
  /*
263
   * Normally we haven't parsed the parent
264
   * yet, so we won't have a parent of a parent
265
   * here. However, it may turn out that we've
266
   * reached this commit some other way (where it
267
   * wasn't uninteresting), in which case we need
268
   * to mark its parents recursively too..
269
   */
270
0
  for (l = commit->parents; l; l = l->next) {
271
0
    commit_stack_push(pending, l->item);
272
0
    if (revs && revs->exclude_first_parent_only)
273
0
      break;
274
0
  }
275
0
}
276
277
void mark_parents_uninteresting(struct rev_info *revs, struct commit *commit)
278
0
{
279
0
  struct commit_stack pending = COMMIT_STACK_INIT;
280
0
  struct commit_list *l;
281
282
0
  for (l = commit->parents; l; l = l->next) {
283
0
    mark_one_parent_uninteresting(revs, l->item, &pending);
284
0
    if (revs && revs->exclude_first_parent_only)
285
0
      break;
286
0
  }
287
288
0
  while (pending.nr > 0)
289
0
    mark_one_parent_uninteresting(revs, commit_stack_pop(&pending),
290
0
                &pending);
291
292
0
  commit_stack_clear(&pending);
293
0
}
294
295
static void add_pending_object_with_path(struct rev_info *revs,
296
           struct object *obj,
297
           const char *name, unsigned mode,
298
           const char *path)
299
0
{
300
0
  struct interpret_branch_name_options options = { 0 };
301
0
  if (!obj)
302
0
    return;
303
0
  if (revs->no_walk && (obj->flags & UNINTERESTING))
304
0
    revs->no_walk = 0;
305
0
  if (revs->reflog_info && obj->type == OBJ_COMMIT) {
306
0
    struct strbuf buf = STRBUF_INIT;
307
0
    size_t namelen = strlen(name);
308
0
    int len = repo_interpret_branch_name(the_repository, name,
309
0
                 namelen, &buf, &options);
310
311
0
    if (0 < len && len < namelen && buf.len)
312
0
      strbuf_addstr(&buf, name + len);
313
0
    add_reflog_for_walk(revs->reflog_info,
314
0
            (struct commit *)obj,
315
0
            buf.buf[0] ? buf.buf: name);
316
0
    strbuf_release(&buf);
317
0
    return; /* do not add the commit itself */
318
0
  }
319
0
  add_object_array_with_path(obj, name, &revs->pending, mode, path);
320
0
}
321
322
static void add_pending_object_with_mode(struct rev_info *revs,
323
           struct object *obj,
324
           const char *name, unsigned mode)
325
0
{
326
0
  add_pending_object_with_path(revs, obj, name, mode, NULL);
327
0
}
328
329
void add_pending_object(struct rev_info *revs,
330
      struct object *obj, const char *name)
331
0
{
332
0
  add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
333
0
}
334
335
void add_head_to_pending(struct rev_info *revs)
336
0
{
337
0
  struct object_id oid;
338
0
  struct object *obj;
339
0
  if (repo_get_oid(the_repository, "HEAD", &oid))
340
0
    return;
341
0
  obj = parse_object(revs->repo, &oid);
342
0
  if (!obj)
343
0
    return;
344
0
  add_pending_object(revs, obj, "HEAD");
345
0
}
346
347
static struct object *get_reference(struct rev_info *revs, const char *name,
348
            const struct object_id *oid,
349
            unsigned int flags)
350
0
{
351
0
  struct object *object;
352
353
0
  object = parse_object_with_flags(revs->repo, oid,
354
0
           revs->verify_objects ? 0 :
355
0
           PARSE_OBJECT_SKIP_HASH_CHECK |
356
0
           PARSE_OBJECT_DISCARD_TREE);
357
358
0
  if (!object) {
359
0
    if (revs->ignore_missing)
360
0
      return NULL;
361
0
    if (revs->exclude_promisor_objects &&
362
0
        is_promisor_object(revs->repo, oid))
363
0
      return NULL;
364
0
    if (revs->do_not_die_on_missing_objects) {
365
0
      oidset_insert(&revs->missing_commits, oid);
366
0
      return NULL;
367
0
    }
368
0
    die("bad object %s", name);
369
0
  }
370
0
  object->flags |= flags;
371
0
  return object;
372
0
}
373
374
void add_pending_oid(struct rev_info *revs, const char *name,
375
          const struct object_id *oid, unsigned int flags)
376
0
{
377
0
  struct object *object = get_reference(revs, name, oid, flags);
378
0
  add_pending_object(revs, object, name);
379
0
}
380
381
static struct commit *handle_commit(struct rev_info *revs,
382
            struct object_array_entry *entry)
383
0
{
384
0
  struct object *object = entry->item;
385
0
  const char *name = entry->name;
386
0
  const char *path = entry->path;
387
0
  unsigned int mode = entry->mode;
388
0
  unsigned long flags = object->flags;
389
390
  /*
391
   * Tag object? Look what it points to..
392
   */
393
0
  while (object->type == OBJ_TAG) {
394
0
    struct tag *tag = (struct tag *) object;
395
0
    struct object_id *oid;
396
0
    if (revs->tag_objects && !(flags & UNINTERESTING))
397
0
      add_pending_object(revs, object, tag->tag);
398
0
    oid = get_tagged_oid(tag);
399
0
    object = parse_object(revs->repo, oid);
400
0
    if (!object) {
401
0
      if (revs->ignore_missing_links || (flags & UNINTERESTING))
402
0
        return NULL;
403
0
      if (revs->exclude_promisor_objects &&
404
0
          is_promisor_object(revs->repo, &tag->tagged->oid))
405
0
        return NULL;
406
0
      if (revs->do_not_die_on_missing_objects && oid) {
407
0
        oidset_insert(&revs->missing_commits, oid);
408
0
        return NULL;
409
0
      }
410
0
      die("bad object %s", oid_to_hex(&tag->tagged->oid));
411
0
    }
412
0
    object->flags |= flags;
413
    /*
414
     * We'll handle the tagged object by looping or dropping
415
     * through to the non-tag handlers below. Do not
416
     * propagate path data from the tag's pending entry.
417
     */
418
0
    path = NULL;
419
0
    mode = 0;
420
0
  }
421
422
  /*
423
   * Commit object? Just return it, we'll do all the complex
424
   * reachability crud.
425
   */
426
0
  if (object->type == OBJ_COMMIT) {
427
0
    struct commit *commit = (struct commit *)object;
428
429
0
    if (repo_parse_commit(revs->repo, commit) < 0)
430
0
      die("unable to parse commit %s", name);
431
0
    if (flags & UNINTERESTING) {
432
0
      mark_parents_uninteresting(revs, commit);
433
434
0
      if (!revs->topo_order || !generation_numbers_enabled(the_repository))
435
0
        revs->limited = 1;
436
0
    }
437
0
    if (revs->sources) {
438
0
      char **slot = revision_sources_at(revs->sources, commit);
439
440
0
      if (!*slot)
441
0
        *slot = xstrdup(name);
442
0
    }
443
0
    return commit;
444
0
  }
445
446
  /*
447
   * Tree object? Either mark it uninteresting, or add it
448
   * to the list of objects to look at later..
449
   */
450
0
  if (object->type == OBJ_TREE) {
451
0
    struct tree *tree = (struct tree *)object;
452
0
    if (!revs->tree_objects)
453
0
      return NULL;
454
0
    if (flags & UNINTERESTING) {
455
0
      mark_tree_contents_uninteresting(revs->repo, tree);
456
0
      return NULL;
457
0
    }
458
0
    add_pending_object_with_path(revs, object, name, mode, path);
459
0
    return NULL;
460
0
  }
461
462
  /*
463
   * Blob object? You know the drill by now..
464
   */
465
0
  if (object->type == OBJ_BLOB) {
466
0
    if (!revs->blob_objects)
467
0
      return NULL;
468
0
    if (flags & UNINTERESTING)
469
0
      return NULL;
470
0
    add_pending_object_with_path(revs, object, name, mode, path);
471
0
    return NULL;
472
0
  }
473
0
  die("%s is unknown object", name);
474
0
}
475
476
static int everybody_uninteresting(struct commit_list *orig,
477
           struct commit **interesting_cache)
478
0
{
479
0
  struct commit_list *list = orig;
480
481
0
  if (*interesting_cache) {
482
0
    struct commit *commit = *interesting_cache;
483
0
    if (!(commit->object.flags & UNINTERESTING))
484
0
      return 0;
485
0
  }
486
487
0
  while (list) {
488
0
    struct commit *commit = list->item;
489
0
    list = list->next;
490
0
    if (commit->object.flags & UNINTERESTING)
491
0
      continue;
492
493
0
    *interesting_cache = commit;
494
0
    return 0;
495
0
  }
496
0
  return 1;
497
0
}
498
499
/*
500
 * A definition of "relevant" commit that we can use to simplify limited graphs
501
 * by eliminating side branches.
502
 *
503
 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
504
 * in our list), or that is a specified BOTTOM commit. Then after computing
505
 * a limited list, during processing we can generally ignore boundary merges
506
 * coming from outside the graph, (ie from irrelevant parents), and treat
507
 * those merges as if they were single-parent. TREESAME is defined to consider
508
 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
509
 * we don't care if we were !TREESAME to non-graph parents.
510
 *
511
 * Treating bottom commits as relevant ensures that a limited graph's
512
 * connection to the actual bottom commit is not viewed as a side branch, but
513
 * treated as part of the graph. For example:
514
 *
515
 *   ....Z...A---X---o---o---B
516
 *        .     /
517
 *         W---Y
518
 *
519
 * When computing "A..B", the A-X connection is at least as important as
520
 * Y-X, despite A being flagged UNINTERESTING.
521
 *
522
 * And when computing --ancestry-path "A..B", the A-X connection is more
523
 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
524
 */
525
static inline int relevant_commit(struct commit *commit)
526
0
{
527
0
  return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
528
0
}
529
530
/*
531
 * Return a single relevant commit from a parent list. If we are a TREESAME
532
 * commit, and this selects one of our parents, then we can safely simplify to
533
 * that parent.
534
 */
535
static struct commit *one_relevant_parent(const struct rev_info *revs,
536
            struct commit_list *orig)
537
0
{
538
0
  struct commit_list *list = orig;
539
0
  struct commit *relevant = NULL;
540
541
0
  if (!orig)
542
0
    return NULL;
543
544
  /*
545
   * For 1-parent commits, or if first-parent-only, then return that
546
   * first parent (even if not "relevant" by the above definition).
547
   * TREESAME will have been set purely on that parent.
548
   */
549
0
  if (revs->first_parent_only || !orig->next)
550
0
    return orig->item;
551
552
  /*
553
   * For multi-parent commits, identify a sole relevant parent, if any.
554
   * If we have only one relevant parent, then TREESAME will be set purely
555
   * with regard to that parent, and we can simplify accordingly.
556
   *
557
   * If we have more than one relevant parent, or no relevant parents
558
   * (and multiple irrelevant ones), then we can't select a parent here
559
   * and return NULL.
560
   */
561
0
  while (list) {
562
0
    struct commit *commit = list->item;
563
0
    list = list->next;
564
0
    if (relevant_commit(commit)) {
565
0
      if (relevant)
566
0
        return NULL;
567
0
      relevant = commit;
568
0
    }
569
0
  }
570
0
  return relevant;
571
0
}
572
573
/*
574
 * The goal is to get REV_TREE_NEW as the result only if the
575
 * diff consists of all '+' (and no other changes), REV_TREE_OLD
576
 * if the whole diff is removal of old data, and otherwise
577
 * REV_TREE_DIFFERENT (of course if the trees are the same we
578
 * want REV_TREE_SAME).
579
 *
580
 * The only time we care about the distinction is when
581
 * remove_empty_trees is in effect, in which case we care only about
582
 * whether the whole change is REV_TREE_NEW, or if there's another type
583
 * of change. Which means we can stop the diff early in either of these
584
 * cases:
585
 *
586
 *   1. We're not using remove_empty_trees at all.
587
 *
588
 *   2. We saw anything except REV_TREE_NEW.
589
 */
590
0
#define REV_TREE_SAME   0
591
0
#define REV_TREE_NEW    1  /* Only new files */
592
0
#define REV_TREE_OLD    2  /* Only files removed */
593
0
#define REV_TREE_DIFFERENT  3  /* Mixed changes */
594
static int tree_difference = REV_TREE_SAME;
595
596
static void file_add_remove(struct diff_options *options,
597
        int addremove,
598
        unsigned mode UNUSED,
599
        const struct object_id *oid UNUSED,
600
        int oid_valid UNUSED,
601
        const char *fullpath UNUSED,
602
        unsigned dirty_submodule UNUSED)
603
0
{
604
0
  int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
605
0
  struct rev_info *revs = options->change_fn_data;
606
607
0
  tree_difference |= diff;
608
0
  if (!revs->remove_empty_trees || tree_difference != REV_TREE_NEW)
609
0
    options->flags.has_changes = 1;
610
0
}
611
612
static void file_change(struct diff_options *options,
613
     unsigned old_mode UNUSED,
614
     unsigned new_mode UNUSED,
615
     const struct object_id *old_oid UNUSED,
616
     const struct object_id *new_oid UNUSED,
617
     int old_oid_valid UNUSED,
618
     int new_oid_valid UNUSED,
619
     const char *fullpath UNUSED,
620
     unsigned old_dirty_submodule UNUSED,
621
     unsigned new_dirty_submodule UNUSED)
622
0
{
623
0
  tree_difference = REV_TREE_DIFFERENT;
624
0
  options->flags.has_changes = 1;
625
0
}
626
627
static int bloom_filter_atexit_registered;
628
static unsigned int count_bloom_filter_maybe;
629
static unsigned int count_bloom_filter_definitely_not;
630
static unsigned int count_bloom_filter_false_positive;
631
static unsigned int count_bloom_filter_not_present;
632
633
static void trace2_bloom_filter_statistics_atexit(void)
634
0
{
635
0
  struct json_writer jw = JSON_WRITER_INIT;
636
637
0
  jw_object_begin(&jw, 0);
638
0
  jw_object_intmax(&jw, "filter_not_present", count_bloom_filter_not_present);
639
0
  jw_object_intmax(&jw, "maybe", count_bloom_filter_maybe);
640
0
  jw_object_intmax(&jw, "definitely_not", count_bloom_filter_definitely_not);
641
0
  jw_object_intmax(&jw, "false_positive", count_bloom_filter_false_positive);
642
0
  jw_end(&jw);
643
644
0
  trace2_data_json("bloom", the_repository, "statistics", &jw);
645
646
0
  jw_release(&jw);
647
0
}
648
649
static int forbid_bloom_filters(struct pathspec *spec)
650
0
{
651
0
  unsigned int allowed_magic =
652
0
    PATHSPEC_FROMTOP |
653
0
    PATHSPEC_MAXDEPTH |
654
0
    PATHSPEC_LITERAL |
655
0
    PATHSPEC_GLOB |
656
0
    PATHSPEC_ATTR;
657
658
0
  if (spec->magic & ~allowed_magic)
659
0
    return 1;
660
0
  for (size_t nr = 0; nr < spec->nr; nr++)
661
0
    if (spec->items[nr].magic & ~allowed_magic)
662
0
      return 1;
663
664
0
  return 0;
665
0
}
666
667
static void release_revisions_bloom_keyvecs(struct rev_info *revs);
668
669
static int convert_pathspec_to_bloom_keyvec(struct bloom_keyvec **out,
670
              const struct pathspec_item *pi,
671
              const struct bloom_filter_settings *settings)
672
0
{
673
0
  char *path_alloc = NULL;
674
0
  const char *path;
675
0
  size_t len;
676
0
  int res = -1;
677
678
0
  len = pi->nowildcard_len;
679
0
  if (len != pi->len) {
680
    /*
681
     * for path like "dir/file*", nowildcard part would be
682
     * "dir/file", but only "dir" should be used for the
683
     * bloom filter.
684
     */
685
0
    while (len > 0 && pi->match[len - 1] != '/')
686
0
      len--;
687
0
  }
688
  /* remove single trailing slash from path, if needed */
689
0
  if (len > 0 && pi->match[len - 1] == '/')
690
0
    len--;
691
692
0
  if (!len)
693
0
    goto cleanup;
694
695
0
  if (len != pi->len) {
696
0
    path_alloc = xmemdupz(pi->match, len);
697
0
    path = path_alloc;
698
0
  } else
699
0
    path = pi->match;
700
701
0
  *out = bloom_keyvec_new(path, len, settings);
702
703
0
  res = 0;
704
0
cleanup:
705
0
  free(path_alloc);
706
0
  return res;
707
0
}
708
709
static void prepare_to_use_bloom_filter(struct rev_info *revs)
710
0
{
711
0
  if (!revs->commits)
712
0
    return;
713
714
0
  if (forbid_bloom_filters(&revs->prune_data))
715
0
    return;
716
717
0
  repo_parse_commit(revs->repo, revs->commits->item);
718
719
0
  revs->bloom_filter_settings = get_bloom_filter_settings(revs->repo);
720
0
  if (!revs->bloom_filter_settings)
721
0
    return;
722
723
0
  if (!revs->pruning.pathspec.nr)
724
0
    return;
725
726
0
  revs->bloom_keyvecs_nr = revs->pruning.pathspec.nr;
727
0
  CALLOC_ARRAY(revs->bloom_keyvecs, revs->bloom_keyvecs_nr);
728
729
0
  for (int i = 0; i < revs->pruning.pathspec.nr; i++) {
730
0
    if (convert_pathspec_to_bloom_keyvec(&revs->bloom_keyvecs[i],
731
0
                 &revs->pruning.pathspec.items[i],
732
0
                 revs->bloom_filter_settings))
733
0
      goto fail;
734
0
  }
735
736
0
  if (trace2_is_enabled() && !bloom_filter_atexit_registered) {
737
0
    atexit(trace2_bloom_filter_statistics_atexit);
738
0
    bloom_filter_atexit_registered = 1;
739
0
  }
740
741
0
  return;
742
743
0
fail:
744
0
  revs->bloom_filter_settings = NULL;
745
0
  release_revisions_bloom_keyvecs(revs);
746
0
}
747
748
static int check_maybe_different_in_bloom_filter(struct rev_info *revs,
749
             struct commit *commit)
750
0
{
751
0
  struct bloom_filter *filter;
752
0
  int result = 0;
753
754
0
  if (commit_graph_generation(commit) == GENERATION_NUMBER_INFINITY)
755
0
    return -1;
756
757
0
  filter = get_bloom_filter(revs->repo, commit);
758
759
0
  if (!filter) {
760
0
    count_bloom_filter_not_present++;
761
0
    return -1;
762
0
  }
763
764
0
  for (size_t nr = 0; !result && nr < revs->bloom_keyvecs_nr; nr++) {
765
0
    result = bloom_filter_contains_vec(filter,
766
0
               revs->bloom_keyvecs[nr],
767
0
               revs->bloom_filter_settings);
768
0
  }
769
770
0
  if (result)
771
0
    count_bloom_filter_maybe++;
772
0
  else
773
0
    count_bloom_filter_definitely_not++;
774
775
0
  return result;
776
0
}
777
778
static int rev_compare_tree(struct rev_info *revs,
779
          struct commit *parent, struct commit *commit, int nth_parent)
780
0
{
781
0
  struct tree *t1 = repo_get_commit_tree(the_repository, parent);
782
0
  struct tree *t2 = repo_get_commit_tree(the_repository, commit);
783
0
  int bloom_ret = 1;
784
785
0
  if (!t1)
786
0
    return REV_TREE_NEW;
787
0
  if (!t2)
788
0
    return REV_TREE_OLD;
789
790
0
  if (revs->simplify_by_decoration) {
791
    /*
792
     * If we are simplifying by decoration, then the commit
793
     * is worth showing if it has a tag pointing at it.
794
     */
795
0
    if (get_name_decoration(&commit->object))
796
0
      return REV_TREE_DIFFERENT;
797
    /*
798
     * A commit that is not pointed by a tag is uninteresting
799
     * if we are not limited by path.  This means that you will
800
     * see the usual "commits that touch the paths" plus any
801
     * tagged commit by specifying both --simplify-by-decoration
802
     * and pathspec.
803
     */
804
0
    if (!revs->prune_data.nr)
805
0
      return REV_TREE_SAME;
806
0
  }
807
808
0
  if (revs->bloom_keyvecs_nr && !nth_parent) {
809
0
    bloom_ret = check_maybe_different_in_bloom_filter(revs, commit);
810
811
0
    if (bloom_ret == 0)
812
0
      return REV_TREE_SAME;
813
0
  }
814
815
0
  tree_difference = REV_TREE_SAME;
816
0
  revs->pruning.flags.has_changes = 0;
817
0
  diff_tree_oid(&t1->object.oid, &t2->object.oid, "", &revs->pruning);
818
819
0
  if (!nth_parent)
820
0
    if (bloom_ret == 1 && tree_difference == REV_TREE_SAME)
821
0
      count_bloom_filter_false_positive++;
822
823
0
  return tree_difference;
824
0
}
825
826
static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit,
827
          int nth_parent)
828
0
{
829
0
  struct tree *t1 = repo_get_commit_tree(the_repository, commit);
830
0
  int bloom_ret = -1;
831
832
0
  if (!t1)
833
0
    return 0;
834
835
0
  if (!nth_parent && revs->bloom_keyvecs_nr) {
836
0
    bloom_ret = check_maybe_different_in_bloom_filter(revs, commit);
837
0
    if (!bloom_ret)
838
0
      return 1;
839
0
  }
840
841
0
  tree_difference = REV_TREE_SAME;
842
0
  revs->pruning.flags.has_changes = 0;
843
0
  diff_tree_oid(NULL, &t1->object.oid, "", &revs->pruning);
844
845
0
  if (bloom_ret == 1 && tree_difference == REV_TREE_SAME)
846
0
    count_bloom_filter_false_positive++;
847
848
0
  return tree_difference == REV_TREE_SAME;
849
0
}
850
851
struct treesame_state {
852
  unsigned int nparents;
853
  unsigned char treesame[FLEX_ARRAY];
854
};
855
856
static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
857
0
{
858
0
  unsigned n = commit_list_count(commit->parents);
859
0
  struct treesame_state *st = xcalloc(1, st_add(sizeof(*st), n));
860
0
  st->nparents = n;
861
0
  add_decoration(&revs->treesame, &commit->object, st);
862
0
  return st;
863
0
}
864
865
/*
866
 * Must be called immediately after removing the nth_parent from a commit's
867
 * parent list, if we are maintaining the per-parent treesame[] decoration.
868
 * This does not recalculate the master TREESAME flag - update_treesame()
869
 * should be called to update it after a sequence of treesame[] modifications
870
 * that may have affected it.
871
 */
872
static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
873
0
{
874
0
  struct treesame_state *st;
875
0
  int old_same;
876
877
0
  if (!commit->parents) {
878
    /*
879
     * Have just removed the only parent from a non-merge.
880
     * Different handling, as we lack decoration.
881
     */
882
0
    if (nth_parent != 0)
883
0
      die("compact_treesame %u", nth_parent);
884
0
    old_same = !!(commit->object.flags & TREESAME);
885
0
    if (rev_same_tree_as_empty(revs, commit, nth_parent))
886
0
      commit->object.flags |= TREESAME;
887
0
    else
888
0
      commit->object.flags &= ~TREESAME;
889
0
    return old_same;
890
0
  }
891
892
0
  st = lookup_decoration(&revs->treesame, &commit->object);
893
0
  if (!st || nth_parent >= st->nparents)
894
0
    die("compact_treesame %u", nth_parent);
895
896
0
  old_same = st->treesame[nth_parent];
897
0
  memmove(st->treesame + nth_parent,
898
0
    st->treesame + nth_parent + 1,
899
0
    st->nparents - nth_parent - 1);
900
901
  /*
902
   * If we've just become a non-merge commit, update TREESAME
903
   * immediately, and remove the no-longer-needed decoration.
904
   * If still a merge, defer update until update_treesame().
905
   */
906
0
  if (--st->nparents == 1) {
907
0
    if (commit->parents->next)
908
0
      die("compact_treesame parents mismatch");
909
0
    if (st->treesame[0] && revs->dense)
910
0
      commit->object.flags |= TREESAME;
911
0
    else
912
0
      commit->object.flags &= ~TREESAME;
913
0
    free(add_decoration(&revs->treesame, &commit->object, NULL));
914
0
  }
915
916
0
  return old_same;
917
0
}
918
919
static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
920
0
{
921
0
  if (commit->parents && commit->parents->next) {
922
0
    unsigned n;
923
0
    struct treesame_state *st;
924
0
    struct commit_list *p;
925
0
    unsigned relevant_parents;
926
0
    unsigned relevant_change, irrelevant_change;
927
928
0
    st = lookup_decoration(&revs->treesame, &commit->object);
929
0
    if (!st)
930
0
      die("update_treesame %s", oid_to_hex(&commit->object.oid));
931
0
    relevant_parents = 0;
932
0
    relevant_change = irrelevant_change = 0;
933
0
    for (p = commit->parents, n = 0; p; n++, p = p->next) {
934
0
      if (relevant_commit(p->item)) {
935
0
        relevant_change |= !st->treesame[n];
936
0
        relevant_parents++;
937
0
      } else
938
0
        irrelevant_change |= !st->treesame[n];
939
0
    }
940
0
    if (relevant_parents ? relevant_change : irrelevant_change)
941
0
      commit->object.flags &= ~TREESAME;
942
0
    else
943
0
      commit->object.flags |= TREESAME;
944
0
  }
945
946
0
  return commit->object.flags & TREESAME;
947
0
}
948
949
static inline int limiting_can_increase_treesame(const struct rev_info *revs)
950
0
{
951
  /*
952
   * TREESAME is irrelevant unless prune && dense;
953
   * if simplify_history is set, we can't have a mixture of TREESAME and
954
   *    !TREESAME INTERESTING parents (and we don't have treesame[]
955
   *    decoration anyway);
956
   * if first_parent_only is set, then the TREESAME flag is locked
957
   *    against the first parent (and again we lack treesame[] decoration).
958
   */
959
0
  return revs->prune && revs->dense &&
960
0
         !revs->simplify_history &&
961
0
         !revs->first_parent_only;
962
0
}
963
964
static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
965
0
{
966
0
  struct commit_list **pp, *parent;
967
0
  struct treesame_state *ts = NULL;
968
0
  int relevant_change = 0, irrelevant_change = 0;
969
0
  int relevant_parents, nth_parent;
970
971
  /*
972
   * If we don't do pruning, everything is interesting
973
   */
974
0
  if (!revs->prune)
975
0
    return;
976
977
0
  if (!repo_get_commit_tree(the_repository, commit))
978
0
    return;
979
980
0
  if (!commit->parents) {
981
    /*
982
     * Pretend as if we are comparing ourselves to the
983
     * (non-existent) first parent of this commit object. Even
984
     * though no such parent exists, its changed-path Bloom filter
985
     * (if one exists) is relative to the empty tree, using Bloom
986
     * filters is allowed here.
987
     */
988
0
    if (rev_same_tree_as_empty(revs, commit, 0))
989
0
      commit->object.flags |= TREESAME;
990
0
    return;
991
0
  }
992
993
  /*
994
   * Normal non-merge commit? If we don't want to make the
995
   * history dense, we consider it always to be a change..
996
   */
997
0
  if (!revs->dense && !commit->parents->next)
998
0
    return;
999
1000
0
  for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
1001
0
       (parent = *pp) != NULL;
1002
0
       pp = &parent->next, nth_parent++) {
1003
0
    struct commit *p = parent->item;
1004
0
    if (relevant_commit(p))
1005
0
      relevant_parents++;
1006
1007
0
    if (nth_parent == 1) {
1008
      /*
1009
       * This our second loop iteration - so we now know
1010
       * we're dealing with a merge.
1011
       *
1012
       * Do not compare with later parents when we care only about
1013
       * the first parent chain, in order to avoid derailing the
1014
       * traversal to follow a side branch that brought everything
1015
       * in the path we are limited to by the pathspec.
1016
       */
1017
0
      if (revs->first_parent_only)
1018
0
        break;
1019
      /*
1020
       * If this will remain a potentially-simplifiable
1021
       * merge, remember per-parent treesame if needed.
1022
       * Initialise the array with the comparison from our
1023
       * first iteration.
1024
       */
1025
0
      if (revs->treesame.name &&
1026
0
          !revs->simplify_history &&
1027
0
          !(commit->object.flags & UNINTERESTING)) {
1028
0
        ts = initialise_treesame(revs, commit);
1029
0
        if (!(irrelevant_change || relevant_change))
1030
0
          ts->treesame[0] = 1;
1031
0
      }
1032
0
    }
1033
0
    if (repo_parse_commit(revs->repo, p) < 0)
1034
0
      die("cannot simplify commit %s (because of %s)",
1035
0
          oid_to_hex(&commit->object.oid),
1036
0
          oid_to_hex(&p->object.oid));
1037
0
    switch (rev_compare_tree(revs, p, commit, nth_parent)) {
1038
0
    case REV_TREE_SAME:
1039
0
      if (!revs->simplify_history || !relevant_commit(p)) {
1040
        /* Even if a merge with an uninteresting
1041
         * side branch brought the entire change
1042
         * we are interested in, we do not want
1043
         * to lose the other branches of this
1044
         * merge, so we just keep going.
1045
         */
1046
0
        if (ts)
1047
0
          ts->treesame[nth_parent] = 1;
1048
0
        continue;
1049
0
      }
1050
1051
0
      commit_list_free(parent->next);
1052
0
      parent->next = NULL;
1053
0
      while (commit->parents != parent)
1054
0
        pop_commit(&commit->parents);
1055
0
      commit->parents = parent;
1056
1057
      /*
1058
       * A merge commit is a "diversion" if it is not
1059
       * TREESAME to its first parent but is TREESAME
1060
       * to a later parent. In the simplified history,
1061
       * we "divert" the history walk to the later
1062
       * parent. These commits are shown when "show_pulls"
1063
       * is enabled, so do not mark the object as
1064
       * TREESAME here.
1065
       */
1066
0
      if (!revs->show_pulls || !nth_parent)
1067
0
        commit->object.flags |= TREESAME;
1068
1069
0
      return;
1070
1071
0
    case REV_TREE_NEW:
1072
0
      if (revs->remove_empty_trees &&
1073
0
          rev_same_tree_as_empty(revs, p, nth_parent)) {
1074
        /* We are adding all the specified
1075
         * paths from this parent, so the
1076
         * history beyond this parent is not
1077
         * interesting.  Remove its parents
1078
         * (they are grandparents for us).
1079
         * IOW, we pretend this parent is a
1080
         * "root" commit.
1081
         */
1082
0
        if (repo_parse_commit(revs->repo, p) < 0)
1083
0
          die("cannot simplify commit %s (invalid %s)",
1084
0
              oid_to_hex(&commit->object.oid),
1085
0
              oid_to_hex(&p->object.oid));
1086
0
        commit_list_free(p->parents);
1087
0
        p->parents = NULL;
1088
0
      }
1089
    /* fallthrough */
1090
0
    case REV_TREE_OLD:
1091
0
    case REV_TREE_DIFFERENT:
1092
0
      if (relevant_commit(p))
1093
0
        relevant_change = 1;
1094
0
      else
1095
0
        irrelevant_change = 1;
1096
1097
0
      if (!nth_parent)
1098
0
        commit->object.flags |= PULL_MERGE;
1099
1100
0
      continue;
1101
0
    }
1102
0
    die("bad tree compare for commit %s", oid_to_hex(&commit->object.oid));
1103
0
  }
1104
1105
  /*
1106
   * TREESAME is straightforward for single-parent commits. For merge
1107
   * commits, it is most useful to define it so that "irrelevant"
1108
   * parents cannot make us !TREESAME - if we have any relevant
1109
   * parents, then we only consider TREESAMEness with respect to them,
1110
   * allowing irrelevant merges from uninteresting branches to be
1111
   * simplified away. Only if we have only irrelevant parents do we
1112
   * base TREESAME on them. Note that this logic is replicated in
1113
   * update_treesame, which should be kept in sync.
1114
   */
1115
0
  if (relevant_parents ? !relevant_change : !irrelevant_change)
1116
0
    commit->object.flags |= TREESAME;
1117
0
}
1118
1119
static int process_parents(struct rev_info *revs, struct commit *commit,
1120
         struct commit_list **list, struct prio_queue *queue)
1121
0
{
1122
0
  struct commit_list *parent = commit->parents;
1123
0
  unsigned pass_flags;
1124
1125
0
  if (commit->object.flags & ADDED)
1126
0
    return 0;
1127
0
  if (revs->do_not_die_on_missing_objects &&
1128
0
      oidset_contains(&revs->missing_commits, &commit->object.oid))
1129
0
    return 0;
1130
0
  commit->object.flags |= ADDED;
1131
1132
0
  if (revs->include_check &&
1133
0
      !revs->include_check(commit, revs->include_check_data))
1134
0
    return 0;
1135
1136
  /*
1137
   * If the commit is uninteresting, don't try to
1138
   * prune parents - we want the maximal uninteresting
1139
   * set.
1140
   *
1141
   * Normally we haven't parsed the parent
1142
   * yet, so we won't have a parent of a parent
1143
   * here. However, it may turn out that we've
1144
   * reached this commit some other way (where it
1145
   * wasn't uninteresting), in which case we need
1146
   * to mark its parents recursively too..
1147
   */
1148
0
  if (commit->object.flags & UNINTERESTING) {
1149
0
    while (parent) {
1150
0
      struct commit *p = parent->item;
1151
0
      parent = parent->next;
1152
0
      if (p)
1153
0
        p->object.flags |= UNINTERESTING |
1154
0
               CHILD_VISITED;
1155
0
      if (repo_parse_commit_gently(revs->repo, p, 1) < 0)
1156
0
        continue;
1157
0
      if (p->parents)
1158
0
        mark_parents_uninteresting(revs, p);
1159
0
      if (p->object.flags & SEEN)
1160
0
        continue;
1161
0
      p->object.flags |= (SEEN | NOT_USER_GIVEN);
1162
0
      if (list)
1163
0
        commit_list_insert_by_date(p, list);
1164
0
      if (queue)
1165
0
        prio_queue_put(queue, p);
1166
0
      if (revs->exclude_first_parent_only)
1167
0
        break;
1168
0
    }
1169
0
    return 0;
1170
0
  }
1171
1172
  /*
1173
   * Ok, the commit wasn't uninteresting. Try to
1174
   * simplify the commit history and find the parent
1175
   * that has no differences in the path set if one exists.
1176
   */
1177
0
  try_to_simplify_commit(revs, commit);
1178
1179
0
  if (revs->no_walk)
1180
0
    return 0;
1181
1182
0
  pass_flags = (commit->object.flags & (SYMMETRIC_LEFT | ANCESTRY_PATH));
1183
1184
0
  for (parent = commit->parents; parent; parent = parent->next) {
1185
0
    struct commit *p = parent->item;
1186
0
    int gently = revs->ignore_missing_links ||
1187
0
           revs->exclude_promisor_objects ||
1188
0
           revs->do_not_die_on_missing_objects;
1189
0
    if (repo_parse_commit_gently(revs->repo, p, gently) < 0) {
1190
0
      if (revs->exclude_promisor_objects &&
1191
0
          is_promisor_object(revs->repo, &p->object.oid)) {
1192
0
        if (revs->first_parent_only)
1193
0
          break;
1194
0
        continue;
1195
0
      }
1196
1197
0
      if (revs->do_not_die_on_missing_objects)
1198
0
        oidset_insert(&revs->missing_commits, &p->object.oid);
1199
0
      else
1200
0
        return -1; /* corrupt repository */
1201
0
    }
1202
0
    if (revs->sources) {
1203
0
      char **slot = revision_sources_at(revs->sources, p);
1204
1205
0
      if (!*slot)
1206
0
        *slot = *revision_sources_at(revs->sources, commit);
1207
0
    }
1208
0
    p->object.flags |= pass_flags | CHILD_VISITED;
1209
0
    if (!(p->object.flags & SEEN)) {
1210
0
      p->object.flags |= (SEEN | NOT_USER_GIVEN);
1211
0
      if (list)
1212
0
        commit_list_insert_by_date(p, list);
1213
0
      if (queue)
1214
0
        prio_queue_put(queue, p);
1215
0
    }
1216
0
    if (revs->first_parent_only)
1217
0
      break;
1218
0
  }
1219
0
  return 0;
1220
0
}
1221
1222
static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
1223
0
{
1224
0
  struct commit_list *p;
1225
0
  int left_count = 0, right_count = 0;
1226
0
  int left_first;
1227
0
  struct patch_ids ids;
1228
0
  unsigned cherry_flag;
1229
1230
  /* First count the commits on the left and on the right */
1231
0
  for (p = list; p; p = p->next) {
1232
0
    struct commit *commit = p->item;
1233
0
    unsigned flags = commit->object.flags;
1234
0
    if (flags & BOUNDARY)
1235
0
      ;
1236
0
    else if (flags & SYMMETRIC_LEFT)
1237
0
      left_count++;
1238
0
    else
1239
0
      right_count++;
1240
0
  }
1241
1242
0
  if (!left_count || !right_count)
1243
0
    return;
1244
1245
0
  left_first = left_count < right_count;
1246
0
  init_patch_ids(revs->repo, &ids);
1247
0
  ids.diffopts.pathspec = revs->diffopt.pathspec;
1248
1249
  /* Compute patch-ids for one side */
1250
0
  for (p = list; p; p = p->next) {
1251
0
    struct commit *commit = p->item;
1252
0
    unsigned flags = commit->object.flags;
1253
1254
0
    if (flags & BOUNDARY)
1255
0
      continue;
1256
    /*
1257
     * If we have fewer left, left_first is set and we omit
1258
     * commits on the right branch in this loop.  If we have
1259
     * fewer right, we skip the left ones.
1260
     */
1261
0
    if (left_first != !!(flags & SYMMETRIC_LEFT))
1262
0
      continue;
1263
0
    add_commit_patch_id(commit, &ids);
1264
0
  }
1265
1266
  /* either cherry_mark or cherry_pick are true */
1267
0
  cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
1268
1269
  /* Check the other side */
1270
0
  for (p = list; p; p = p->next) {
1271
0
    struct commit *commit = p->item;
1272
0
    struct patch_id *id;
1273
0
    unsigned flags = commit->object.flags;
1274
1275
0
    if (flags & BOUNDARY)
1276
0
      continue;
1277
    /*
1278
     * If we have fewer left, left_first is set and we omit
1279
     * commits on the left branch in this loop.
1280
     */
1281
0
    if (left_first == !!(flags & SYMMETRIC_LEFT))
1282
0
      continue;
1283
1284
    /*
1285
     * Have we seen the same patch id?
1286
     */
1287
0
    id = patch_id_iter_first(commit, &ids);
1288
0
    if (!id)
1289
0
      continue;
1290
1291
0
    commit->object.flags |= cherry_flag;
1292
0
    do {
1293
0
      id->commit->object.flags |= cherry_flag;
1294
0
    } while ((id = patch_id_iter_next(id, &ids)));
1295
0
  }
1296
1297
0
  free_patch_ids(&ids);
1298
0
}
1299
1300
/* How many extra uninteresting commits we want to see.. */
1301
0
#define SLOP 5
1302
1303
static int still_interesting(struct commit_list *src, timestamp_t date, int slop,
1304
           struct commit **interesting_cache)
1305
0
{
1306
  /*
1307
   * No source list at all? We're definitely done..
1308
   */
1309
0
  if (!src)
1310
0
    return 0;
1311
1312
  /*
1313
   * Does the destination list contain entries with a date
1314
   * before the source list? Definitely _not_ done.
1315
   */
1316
0
  if (date <= src->item->date)
1317
0
    return SLOP;
1318
1319
  /*
1320
   * Does the source list still have interesting commits in
1321
   * it? Definitely not done..
1322
   */
1323
0
  if (!everybody_uninteresting(src, interesting_cache))
1324
0
    return SLOP;
1325
1326
  /* Ok, we're closing in.. */
1327
0
  return slop-1;
1328
0
}
1329
1330
/*
1331
 * "rev-list --ancestry-path=C_0 [--ancestry-path=C_1 ...] A..B"
1332
 * computes commits that are ancestors of B but not ancestors of A but
1333
 * further limits the result to those that have any of C in their
1334
 * ancestry path (i.e. are either ancestors of any of C, descendants
1335
 * of any of C, or are any of C). If --ancestry-path is specified with
1336
 * no commit, we use all bottom commits for C.
1337
 *
1338
 * Before this function is called, ancestors of C will have already
1339
 * been marked with ANCESTRY_PATH previously.
1340
 *
1341
 * This takes the list of bottom commits and the result of "A..B"
1342
 * without --ancestry-path, and limits the latter further to the ones
1343
 * that have any of C in their ancestry path. Since the ancestors of C
1344
 * have already been marked (a prerequisite of this function), we just
1345
 * need to mark the descendants, then exclude any commit that does not
1346
 * have any of these marks.
1347
 */
1348
static void limit_to_ancestry(struct commit_list *bottoms, struct commit_list *list)
1349
0
{
1350
0
  struct commit_list *p;
1351
0
  struct commit_list *rlist = NULL;
1352
0
  int made_progress;
1353
1354
  /*
1355
   * Reverse the list so that it will be likely that we would
1356
   * process parents before children.
1357
   */
1358
0
  for (p = list; p; p = p->next)
1359
0
    commit_list_insert(p->item, &rlist);
1360
1361
0
  for (p = bottoms; p; p = p->next)
1362
0
    p->item->object.flags |= TMP_MARK;
1363
1364
  /*
1365
   * Mark the ones that can reach bottom commits in "list",
1366
   * in a bottom-up fashion.
1367
   */
1368
0
  do {
1369
0
    made_progress = 0;
1370
0
    for (p = rlist; p; p = p->next) {
1371
0
      struct commit *c = p->item;
1372
0
      struct commit_list *parents;
1373
0
      if (c->object.flags & (TMP_MARK | UNINTERESTING))
1374
0
        continue;
1375
0
      for (parents = c->parents;
1376
0
           parents;
1377
0
           parents = parents->next) {
1378
0
        if (!(parents->item->object.flags & TMP_MARK))
1379
0
          continue;
1380
0
        c->object.flags |= TMP_MARK;
1381
0
        made_progress = 1;
1382
0
        break;
1383
0
      }
1384
0
    }
1385
0
  } while (made_progress);
1386
1387
  /*
1388
   * NEEDSWORK: decide if we want to remove parents that are
1389
   * not marked with TMP_MARK from commit->parents for commits
1390
   * in the resulting list.  We may not want to do that, though.
1391
   */
1392
1393
  /*
1394
   * The ones that are not marked with either TMP_MARK or
1395
   * ANCESTRY_PATH are uninteresting
1396
   */
1397
0
  for (p = list; p; p = p->next) {
1398
0
    struct commit *c = p->item;
1399
0
    if (c->object.flags & (TMP_MARK | ANCESTRY_PATH))
1400
0
      continue;
1401
0
    c->object.flags |= UNINTERESTING;
1402
0
  }
1403
1404
  /* We are done with TMP_MARK and ANCESTRY_PATH */
1405
0
  for (p = list; p; p = p->next)
1406
0
    p->item->object.flags &= ~(TMP_MARK | ANCESTRY_PATH);
1407
0
  for (p = bottoms; p; p = p->next)
1408
0
    p->item->object.flags &= ~(TMP_MARK | ANCESTRY_PATH);
1409
0
  commit_list_free(rlist);
1410
0
}
1411
1412
/*
1413
 * Before walking the history, add the set of "negative" refs the
1414
 * caller has asked to exclude to the bottom list.
1415
 *
1416
 * This is used to compute "rev-list --ancestry-path A..B", as we need
1417
 * to filter the result of "A..B" further to the ones that can actually
1418
 * reach A.
1419
 */
1420
static void collect_bottom_commits(struct commit_list *list,
1421
           struct commit_list **bottom)
1422
0
{
1423
0
  struct commit_list *elem;
1424
0
  for (elem = list; elem; elem = elem->next)
1425
0
    if (elem->item->object.flags & BOTTOM)
1426
0
      commit_list_insert(elem->item, bottom);
1427
0
}
1428
1429
/* Assumes either left_only or right_only is set */
1430
static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1431
0
{
1432
0
  struct commit_list *p;
1433
1434
0
  for (p = list; p; p = p->next) {
1435
0
    struct commit *commit = p->item;
1436
1437
0
    if (revs->right_only) {
1438
0
      if (commit->object.flags & SYMMETRIC_LEFT)
1439
0
        commit->object.flags |= SHOWN;
1440
0
    } else  /* revs->left_only is set */
1441
0
      if (!(commit->object.flags & SYMMETRIC_LEFT))
1442
0
        commit->object.flags |= SHOWN;
1443
0
  }
1444
0
}
1445
1446
static int limit_list(struct rev_info *revs)
1447
0
{
1448
0
  int slop = SLOP;
1449
0
  timestamp_t date = TIME_MAX;
1450
0
  struct commit_list *original_list = revs->commits;
1451
0
  struct commit_list *newlist = NULL;
1452
0
  struct commit_list **p = &newlist;
1453
0
  struct commit *interesting_cache = NULL;
1454
1455
0
  if (revs->ancestry_path_implicit_bottoms) {
1456
0
    collect_bottom_commits(original_list,
1457
0
               &revs->ancestry_path_bottoms);
1458
0
    if (!revs->ancestry_path_bottoms)
1459
0
      die("--ancestry-path given but there are no bottom commits");
1460
0
  }
1461
1462
0
  while (original_list) {
1463
0
    struct commit *commit = pop_commit(&original_list);
1464
0
    struct object *obj = &commit->object;
1465
1466
0
    if (commit == interesting_cache)
1467
0
      interesting_cache = NULL;
1468
1469
0
    if (revs->max_age != -1 && (commit->date < revs->max_age))
1470
0
      obj->flags |= UNINTERESTING;
1471
0
    if (process_parents(revs, commit, &original_list, NULL) < 0)
1472
0
      return -1;
1473
0
    if (obj->flags & UNINTERESTING) {
1474
0
      mark_parents_uninteresting(revs, commit);
1475
0
      slop = still_interesting(original_list, date, slop, &interesting_cache);
1476
0
      if (slop)
1477
0
        continue;
1478
0
      break;
1479
0
    }
1480
0
    if (revs->min_age != -1 && (commit->date > revs->min_age) &&
1481
0
        !revs->line_level_traverse)
1482
0
      continue;
1483
0
    if (revs->max_age_as_filter != -1 &&
1484
0
      (commit->date < revs->max_age_as_filter) && !revs->line_level_traverse)
1485
0
      continue;
1486
0
    date = commit->date;
1487
0
    p = &commit_list_insert(commit, p)->next;
1488
0
  }
1489
0
  if (revs->cherry_pick || revs->cherry_mark)
1490
0
    cherry_pick_list(newlist, revs);
1491
1492
0
  if (revs->left_only || revs->right_only)
1493
0
    limit_left_right(newlist, revs);
1494
1495
0
  if (revs->ancestry_path)
1496
0
    limit_to_ancestry(revs->ancestry_path_bottoms, newlist);
1497
1498
  /*
1499
   * Check if any commits have become TREESAME by some of their parents
1500
   * becoming UNINTERESTING.
1501
   */
1502
0
  if (limiting_can_increase_treesame(revs)) {
1503
0
    struct commit_list *list = NULL;
1504
0
    for (list = newlist; list; list = list->next) {
1505
0
      struct commit *c = list->item;
1506
0
      if (c->object.flags & (UNINTERESTING | TREESAME))
1507
0
        continue;
1508
0
      update_treesame(revs, c);
1509
0
    }
1510
0
  }
1511
1512
0
  commit_list_free(original_list);
1513
0
  revs->commits = newlist;
1514
0
  return 0;
1515
0
}
1516
1517
/*
1518
 * Add an entry to refs->cmdline with the specified information.
1519
 * *name is copied.
1520
 */
1521
static void add_rev_cmdline(struct rev_info *revs,
1522
          struct object *item,
1523
          const char *name,
1524
          int whence,
1525
          unsigned flags)
1526
0
{
1527
0
  struct rev_cmdline_info *info = &revs->cmdline;
1528
0
  unsigned int nr = info->nr;
1529
1530
0
  ALLOC_GROW(info->rev, nr + 1, info->alloc);
1531
0
  info->rev[nr].item = item;
1532
0
  info->rev[nr].name = xstrdup(name);
1533
0
  info->rev[nr].whence = whence;
1534
0
  info->rev[nr].flags = flags;
1535
0
  info->nr++;
1536
0
}
1537
1538
static void add_rev_cmdline_list(struct rev_info *revs,
1539
         struct commit_list *commit_list,
1540
         int whence,
1541
         unsigned flags)
1542
0
{
1543
0
  while (commit_list) {
1544
0
    struct object *object = &commit_list->item->object;
1545
0
    add_rev_cmdline(revs, object, oid_to_hex(&object->oid),
1546
0
        whence, flags);
1547
0
    commit_list = commit_list->next;
1548
0
  }
1549
0
}
1550
1551
int ref_excluded(const struct ref_exclusions *exclusions, const char *path)
1552
0
{
1553
0
  const char *stripped_path = strip_namespace(path);
1554
0
  struct string_list_item *item;
1555
1556
0
  for_each_string_list_item(item, &exclusions->excluded_refs) {
1557
0
    if (!wildmatch(item->string, path, 0))
1558
0
      return 1;
1559
0
  }
1560
1561
0
  if (ref_is_hidden(stripped_path, path, &exclusions->hidden_refs))
1562
0
    return 1;
1563
1564
0
  return 0;
1565
0
}
1566
1567
void init_ref_exclusions(struct ref_exclusions *exclusions)
1568
0
{
1569
0
  struct ref_exclusions blank = REF_EXCLUSIONS_INIT;
1570
0
  memcpy(exclusions, &blank, sizeof(*exclusions));
1571
0
}
1572
1573
void clear_ref_exclusions(struct ref_exclusions *exclusions)
1574
0
{
1575
0
  string_list_clear(&exclusions->excluded_refs, 0);
1576
0
  strvec_clear(&exclusions->hidden_refs);
1577
0
  exclusions->hidden_refs_configured = 0;
1578
0
}
1579
1580
void add_ref_exclusion(struct ref_exclusions *exclusions, const char *exclude)
1581
0
{
1582
0
  string_list_append(&exclusions->excluded_refs, exclude);
1583
0
}
1584
1585
struct exclude_hidden_refs_cb {
1586
  struct ref_exclusions *exclusions;
1587
  const char *section;
1588
};
1589
1590
static int hide_refs_config(const char *var, const char *value,
1591
          const struct config_context *ctx UNUSED,
1592
          void *cb_data)
1593
0
{
1594
0
  struct exclude_hidden_refs_cb *cb = cb_data;
1595
0
  cb->exclusions->hidden_refs_configured = 1;
1596
0
  return parse_hide_refs_config(var, value, cb->section,
1597
0
              &cb->exclusions->hidden_refs);
1598
0
}
1599
1600
void exclude_hidden_refs(struct ref_exclusions *exclusions, const char *section)
1601
0
{
1602
0
  struct exclude_hidden_refs_cb cb;
1603
1604
0
  if (strcmp(section, "fetch") && strcmp(section, "receive") &&
1605
0
      strcmp(section, "uploadpack"))
1606
0
    die(_("unsupported section for hidden refs: %s"), section);
1607
1608
0
  if (exclusions->hidden_refs_configured)
1609
0
    die(_("--exclude-hidden= passed more than once"));
1610
1611
0
  cb.exclusions = exclusions;
1612
0
  cb.section = section;
1613
1614
0
  repo_config(the_repository, hide_refs_config, &cb);
1615
0
}
1616
1617
struct all_refs_cb {
1618
  int all_flags;
1619
  int warned_bad_reflog;
1620
  struct rev_info *all_revs;
1621
  const char *name_for_errormsg;
1622
  struct worktree *wt;
1623
};
1624
1625
static int handle_one_ref(const struct reference *ref, void *cb_data)
1626
0
{
1627
0
  struct all_refs_cb *cb = cb_data;
1628
0
  struct object *object;
1629
1630
0
  if (ref_excluded(&cb->all_revs->ref_excludes, ref->name))
1631
0
      return 0;
1632
1633
0
  object = get_reference(cb->all_revs, ref->name, ref->oid, cb->all_flags);
1634
0
  add_rev_cmdline(cb->all_revs, object, ref->name, REV_CMD_REF, cb->all_flags);
1635
0
  add_pending_object(cb->all_revs, object, ref->name);
1636
0
  return 0;
1637
0
}
1638
1639
static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1640
  unsigned flags)
1641
0
{
1642
0
  cb->all_revs = revs;
1643
0
  cb->all_flags = flags;
1644
0
  revs->rev_input_given = 1;
1645
0
  cb->wt = NULL;
1646
0
}
1647
1648
static void handle_refs(struct ref_store *refs,
1649
      struct rev_info *revs, unsigned flags,
1650
      int (*for_each)(struct ref_store *, refs_for_each_cb, void *))
1651
0
{
1652
0
  struct all_refs_cb cb;
1653
1654
0
  if (!refs) {
1655
    /* this could happen with uninitialized submodules */
1656
0
    return;
1657
0
  }
1658
1659
0
  init_all_refs_cb(&cb, revs, flags);
1660
0
  for_each(refs, handle_one_ref, &cb);
1661
0
}
1662
1663
static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
1664
0
{
1665
0
  struct all_refs_cb *cb = cb_data;
1666
0
  if (!is_null_oid(oid)) {
1667
0
    struct object *o = parse_object(cb->all_revs->repo, oid);
1668
0
    if (o) {
1669
0
      o->flags |= cb->all_flags;
1670
      /* ??? CMDLINEFLAGS ??? */
1671
0
      add_pending_object(cb->all_revs, o, "");
1672
0
    }
1673
0
    else if (!cb->warned_bad_reflog) {
1674
0
      warning("reflog of '%s' references pruned commits",
1675
0
        cb->name_for_errormsg);
1676
0
      cb->warned_bad_reflog = 1;
1677
0
    }
1678
0
  }
1679
0
}
1680
1681
static int handle_one_reflog_ent(const char *refname UNUSED,
1682
         struct object_id *ooid, struct object_id *noid,
1683
         const char *email UNUSED,
1684
         timestamp_t timestamp UNUSED,
1685
         int tz UNUSED,
1686
         const char *message UNUSED,
1687
         void *cb_data)
1688
0
{
1689
0
  handle_one_reflog_commit(ooid, cb_data);
1690
0
  handle_one_reflog_commit(noid, cb_data);
1691
0
  return 0;
1692
0
}
1693
1694
static int handle_one_reflog(const char *refname_in_wt, void *cb_data)
1695
0
{
1696
0
  struct all_refs_cb *cb = cb_data;
1697
0
  struct strbuf refname = STRBUF_INIT;
1698
1699
0
  cb->warned_bad_reflog = 0;
1700
0
  strbuf_worktree_ref(cb->wt, &refname, refname_in_wt);
1701
0
  cb->name_for_errormsg = refname.buf;
1702
0
  refs_for_each_reflog_ent(get_main_ref_store(the_repository),
1703
0
         refname.buf,
1704
0
         handle_one_reflog_ent, cb_data);
1705
0
  strbuf_release(&refname);
1706
0
  return 0;
1707
0
}
1708
1709
static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
1710
0
{
1711
0
  struct worktree **worktrees, **p;
1712
1713
0
  worktrees = get_worktrees();
1714
0
  for (p = worktrees; *p; p++) {
1715
0
    struct worktree *wt = *p;
1716
1717
0
    if (wt->is_current)
1718
0
      continue;
1719
1720
0
    cb->wt = wt;
1721
0
    refs_for_each_reflog(get_worktree_ref_store(wt),
1722
0
             handle_one_reflog,
1723
0
             cb);
1724
0
  }
1725
0
  free_worktrees(worktrees);
1726
0
}
1727
1728
void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
1729
0
{
1730
0
  struct all_refs_cb cb;
1731
1732
0
  cb.all_revs = revs;
1733
0
  cb.all_flags = flags;
1734
0
  cb.wt = NULL;
1735
0
  refs_for_each_reflog(get_main_ref_store(the_repository),
1736
0
           handle_one_reflog, &cb);
1737
1738
0
  if (!revs->single_worktree)
1739
0
    add_other_reflogs_to_pending(&cb);
1740
0
}
1741
1742
static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
1743
         struct strbuf *path, unsigned int flags)
1744
0
{
1745
0
  size_t baselen = path->len;
1746
0
  int i;
1747
1748
0
  if (it->entry_count >= 0) {
1749
0
    struct tree *tree = lookup_tree(revs->repo, &it->oid);
1750
0
    tree->object.flags |= flags;
1751
0
    add_pending_object_with_path(revs, &tree->object, "",
1752
0
               040000, path->buf);
1753
0
  }
1754
1755
0
  for (i = 0; i < it->subtree_nr; i++) {
1756
0
    struct cache_tree_sub *sub = it->down[i];
1757
0
    strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
1758
0
    add_cache_tree(sub->cache_tree, revs, path, flags);
1759
0
    strbuf_setlen(path, baselen);
1760
0
  }
1761
1762
0
}
1763
1764
static void add_resolve_undo_to_pending(struct index_state *istate, struct rev_info *revs)
1765
0
{
1766
0
  struct string_list_item *item;
1767
0
  struct string_list *resolve_undo = istate->resolve_undo;
1768
1769
0
  if (!resolve_undo)
1770
0
    return;
1771
1772
0
  for_each_string_list_item(item, resolve_undo) {
1773
0
    const char *path = item->string;
1774
0
    struct resolve_undo_info *ru = item->util;
1775
0
    int i;
1776
1777
0
    if (!ru)
1778
0
      continue;
1779
0
    for (i = 0; i < 3; i++) {
1780
0
      struct blob *blob;
1781
1782
0
      if (!ru->mode[i] || !S_ISREG(ru->mode[i]))
1783
0
        continue;
1784
1785
0
      blob = lookup_blob(revs->repo, &ru->oid[i]);
1786
0
      if (!blob) {
1787
0
        warning(_("resolve-undo records `%s` which is missing"),
1788
0
          oid_to_hex(&ru->oid[i]));
1789
0
        continue;
1790
0
      }
1791
0
      add_pending_object_with_path(revs, &blob->object, "",
1792
0
                 ru->mode[i], path);
1793
0
    }
1794
0
  }
1795
0
}
1796
1797
static void do_add_index_objects_to_pending(struct rev_info *revs,
1798
              struct index_state *istate,
1799
              unsigned int flags)
1800
0
{
1801
0
  int i;
1802
1803
  /* TODO: audit for interaction with sparse-index. */
1804
0
  ensure_full_index(istate);
1805
0
  for (i = 0; i < istate->cache_nr; i++) {
1806
0
    struct cache_entry *ce = istate->cache[i];
1807
0
    struct blob *blob;
1808
1809
0
    if (S_ISGITLINK(ce->ce_mode))
1810
0
      continue;
1811
1812
0
    blob = lookup_blob(revs->repo, &ce->oid);
1813
0
    if (!blob)
1814
0
      die("unable to add index blob to traversal");
1815
0
    blob->object.flags |= flags;
1816
0
    add_pending_object_with_path(revs, &blob->object, "",
1817
0
               ce->ce_mode, ce->name);
1818
0
  }
1819
1820
0
  if (istate->cache_tree) {
1821
0
    struct strbuf path = STRBUF_INIT;
1822
0
    add_cache_tree(istate->cache_tree, revs, &path, flags);
1823
0
    strbuf_release(&path);
1824
0
  }
1825
1826
0
  add_resolve_undo_to_pending(istate, revs);
1827
0
}
1828
1829
void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
1830
0
{
1831
0
  struct worktree **worktrees, **p;
1832
1833
0
  repo_read_index(revs->repo);
1834
0
  do_add_index_objects_to_pending(revs, revs->repo->index, flags);
1835
1836
0
  if (revs->single_worktree)
1837
0
    return;
1838
1839
0
  worktrees = get_worktrees();
1840
0
  for (p = worktrees; *p; p++) {
1841
0
    struct worktree *wt = *p;
1842
0
    struct index_state istate = INDEX_STATE_INIT(revs->repo);
1843
0
    char *wt_gitdir;
1844
1845
0
    if (wt->is_current)
1846
0
      continue; /* current index already taken care of */
1847
1848
0
    wt_gitdir = get_worktree_git_dir(wt);
1849
1850
0
    if (read_index_from(&istate,
1851
0
            worktree_git_path(wt, "index"),
1852
0
            wt_gitdir) > 0)
1853
0
      do_add_index_objects_to_pending(revs, &istate, flags);
1854
1855
0
    discard_index(&istate);
1856
0
    free(wt_gitdir);
1857
0
  }
1858
0
  free_worktrees(worktrees);
1859
0
}
1860
1861
struct add_alternate_refs_data {
1862
  struct rev_info *revs;
1863
  unsigned int flags;
1864
};
1865
1866
static void add_one_alternate_ref(const struct object_id *oid,
1867
          void *vdata)
1868
0
{
1869
0
  const char *name = ".alternate";
1870
0
  struct add_alternate_refs_data *data = vdata;
1871
0
  struct object *obj;
1872
1873
0
  obj = get_reference(data->revs, name, oid, data->flags);
1874
0
  add_rev_cmdline(data->revs, obj, name, REV_CMD_REV, data->flags);
1875
0
  add_pending_object(data->revs, obj, name);
1876
0
}
1877
1878
static void add_alternate_refs_to_pending(struct rev_info *revs,
1879
            unsigned int flags)
1880
0
{
1881
0
  struct add_alternate_refs_data data;
1882
0
  data.revs = revs;
1883
0
  data.flags = flags;
1884
0
  odb_for_each_alternate_ref(the_repository->objects,
1885
0
           add_one_alternate_ref, &data);
1886
0
}
1887
1888
static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
1889
          int exclude_parent)
1890
0
{
1891
0
  struct object_id oid;
1892
0
  struct object *it;
1893
0
  struct commit *commit;
1894
0
  struct commit_list *parents;
1895
0
  int parent_number;
1896
0
  const char *arg = arg_;
1897
1898
0
  if (*arg == '^') {
1899
0
    flags ^= UNINTERESTING | BOTTOM;
1900
0
    arg++;
1901
0
  }
1902
0
  if (repo_get_oid_committish(the_repository, arg, &oid))
1903
0
    return 0;
1904
0
  while (1) {
1905
0
    it = get_reference(revs, arg, &oid, 0);
1906
0
    if (!it && revs->ignore_missing)
1907
0
      return 0;
1908
0
    if (it->type != OBJ_TAG)
1909
0
      break;
1910
0
    if (!((struct tag*)it)->tagged)
1911
0
      return 0;
1912
0
    oidcpy(&oid, &((struct tag*)it)->tagged->oid);
1913
0
  }
1914
0
  if (it->type != OBJ_COMMIT)
1915
0
    return 0;
1916
0
  commit = (struct commit *)it;
1917
0
  if (exclude_parent &&
1918
0
      exclude_parent > commit_list_count(commit->parents))
1919
0
    return 0;
1920
0
  for (parents = commit->parents, parent_number = 1;
1921
0
       parents;
1922
0
       parents = parents->next, parent_number++) {
1923
0
    if (exclude_parent && parent_number != exclude_parent)
1924
0
      continue;
1925
1926
0
    it = &parents->item->object;
1927
0
    it->flags |= flags;
1928
0
    add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1929
0
    add_pending_object(revs, it, arg);
1930
0
  }
1931
0
  return 1;
1932
0
}
1933
1934
void repo_init_revisions(struct repository *r,
1935
       struct rev_info *revs,
1936
       const char *prefix)
1937
0
{
1938
0
  struct rev_info blank = REV_INFO_INIT;
1939
0
  memcpy(revs, &blank, sizeof(*revs));
1940
1941
0
  revs->repo = r;
1942
0
  revs->pruning.repo = r;
1943
0
  revs->pruning.add_remove = file_add_remove;
1944
0
  revs->pruning.change = file_change;
1945
0
  revs->pruning.change_fn_data = revs;
1946
0
  revs->prefix = prefix;
1947
1948
0
  grep_init(&revs->grep_filter, revs->repo);
1949
0
  revs->grep_filter.status_only = 1;
1950
1951
0
  repo_diff_setup(revs->repo, &revs->diffopt);
1952
0
  if (prefix && !revs->diffopt.prefix) {
1953
0
    revs->diffopt.prefix = prefix;
1954
0
    revs->diffopt.prefix_length = strlen(prefix);
1955
0
  }
1956
1957
0
  init_display_notes(&revs->notes_opt);
1958
0
  list_objects_filter_init(&revs->filter);
1959
0
  init_ref_exclusions(&revs->ref_excludes);
1960
0
  oidset_init(&revs->missing_commits, 0);
1961
0
}
1962
1963
static void add_pending_commit_list(struct rev_info *revs,
1964
            struct commit_list *commit_list,
1965
            unsigned int flags)
1966
0
{
1967
0
  while (commit_list) {
1968
0
    struct object *object = &commit_list->item->object;
1969
0
    object->flags |= flags;
1970
0
    add_pending_object(revs, object, oid_to_hex(&object->oid));
1971
0
    commit_list = commit_list->next;
1972
0
  }
1973
0
}
1974
1975
static const char *lookup_other_head(struct object_id *oid)
1976
0
{
1977
0
  int i;
1978
0
  static const char *const other_head[] = {
1979
0
    "MERGE_HEAD", "CHERRY_PICK_HEAD", "REVERT_HEAD", "REBASE_HEAD"
1980
0
  };
1981
1982
0
  for (i = 0; i < ARRAY_SIZE(other_head); i++)
1983
0
    if (!refs_read_ref_full(get_main_ref_store(the_repository), other_head[i],
1984
0
          RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
1985
0
          oid, NULL)) {
1986
0
      if (is_null_oid(oid))
1987
0
        die(_("%s exists but is a symbolic ref"), other_head[i]);
1988
0
      return other_head[i];
1989
0
    }
1990
1991
0
  die(_("--merge requires one of the pseudorefs MERGE_HEAD, CHERRY_PICK_HEAD, REVERT_HEAD or REBASE_HEAD"));
1992
0
}
1993
1994
static void prepare_show_merge(struct rev_info *revs)
1995
0
{
1996
0
  struct commit_list *bases = NULL;
1997
0
  struct commit *head, *other;
1998
0
  struct object_id oid;
1999
0
  const char *other_name;
2000
0
  const char **prune = NULL;
2001
0
  int i, prune_num = 1; /* counting terminating NULL */
2002
0
  struct index_state *istate = revs->repo->index;
2003
2004
0
  if (repo_get_oid(the_repository, "HEAD", &oid))
2005
0
    die("--merge without HEAD?");
2006
0
  head = lookup_commit_or_die(&oid, "HEAD");
2007
0
  other_name = lookup_other_head(&oid);
2008
0
  other = lookup_commit_or_die(&oid, other_name);
2009
0
  add_pending_object(revs, &head->object, "HEAD");
2010
0
  add_pending_object(revs, &other->object, other_name);
2011
0
  if (repo_get_merge_bases(the_repository, head, other, &bases) < 0)
2012
0
    exit(128);
2013
0
  add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
2014
0
  add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
2015
0
  commit_list_free(bases);
2016
0
  head->object.flags |= SYMMETRIC_LEFT;
2017
2018
0
  if (!istate->cache_nr)
2019
0
    repo_read_index(revs->repo);
2020
0
  for (i = 0; i < istate->cache_nr; i++) {
2021
0
    const struct cache_entry *ce = istate->cache[i];
2022
0
    if (!ce_stage(ce))
2023
0
      continue;
2024
0
    if (ce_path_match(istate, ce, &revs->prune_data, NULL)) {
2025
0
      prune_num++;
2026
0
      REALLOC_ARRAY(prune, prune_num);
2027
0
      prune[prune_num-2] = ce->name;
2028
0
      prune[prune_num-1] = NULL;
2029
0
    }
2030
0
    while ((i+1 < istate->cache_nr) &&
2031
0
           ce_same_name(ce, istate->cache[i+1]))
2032
0
      i++;
2033
0
  }
2034
0
  clear_pathspec(&revs->prune_data);
2035
0
  parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
2036
0
           PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
2037
0
  revs->limited = 1;
2038
0
  free(prune);
2039
0
}
2040
2041
static int dotdot_missing(const char *arg, char *dotdot,
2042
        struct rev_info *revs, int symmetric)
2043
0
{
2044
0
  if (revs->ignore_missing)
2045
0
    return 0;
2046
  /* de-munge so we report the full argument */
2047
0
  *dotdot = '.';
2048
0
  die(symmetric
2049
0
      ? "Invalid symmetric difference expression %s"
2050
0
      : "Invalid revision range %s", arg);
2051
0
}
2052
2053
static int handle_dotdot_1(const char *arg, char *dotdot,
2054
         struct rev_info *revs, int flags,
2055
         int cant_be_filename,
2056
         struct object_context *a_oc,
2057
         struct object_context *b_oc)
2058
0
{
2059
0
  const char *a_name, *b_name;
2060
0
  struct object_id a_oid, b_oid;
2061
0
  struct object *a_obj, *b_obj;
2062
0
  unsigned int a_flags, b_flags;
2063
0
  int symmetric = 0;
2064
0
  unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
2065
0
  unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
2066
2067
0
  a_name = arg;
2068
0
  if (!*a_name)
2069
0
    a_name = "HEAD";
2070
2071
0
  b_name = dotdot + 2;
2072
0
  if (*b_name == '.') {
2073
0
    symmetric = 1;
2074
0
    b_name++;
2075
0
  }
2076
0
  if (!*b_name)
2077
0
    b_name = "HEAD";
2078
2079
0
  if (get_oid_with_context(revs->repo, a_name, oc_flags, &a_oid, a_oc) ||
2080
0
      get_oid_with_context(revs->repo, b_name, oc_flags, &b_oid, b_oc))
2081
0
    return -1;
2082
2083
0
  if (!cant_be_filename) {
2084
0
    *dotdot = '.';
2085
0
    verify_non_filename(revs->prefix, arg);
2086
0
    *dotdot = '\0';
2087
0
  }
2088
2089
0
  a_obj = parse_object(revs->repo, &a_oid);
2090
0
  b_obj = parse_object(revs->repo, &b_oid);
2091
0
  if (!a_obj || !b_obj)
2092
0
    return dotdot_missing(arg, dotdot, revs, symmetric);
2093
2094
0
  if (!symmetric) {
2095
    /* just A..B */
2096
0
    b_flags = flags;
2097
0
    a_flags = flags_exclude;
2098
0
  } else {
2099
    /* A...B -- find merge bases between the two */
2100
0
    struct commit *a, *b;
2101
0
    struct commit_list *exclude = NULL;
2102
2103
0
    a = lookup_commit_reference(revs->repo, &a_obj->oid);
2104
0
    b = lookup_commit_reference(revs->repo, &b_obj->oid);
2105
0
    if (!a || !b)
2106
0
      return dotdot_missing(arg, dotdot, revs, symmetric);
2107
2108
0
    if (repo_get_merge_bases(the_repository, a, b, &exclude) < 0) {
2109
0
      commit_list_free(exclude);
2110
0
      return -1;
2111
0
    }
2112
0
    add_rev_cmdline_list(revs, exclude, REV_CMD_MERGE_BASE,
2113
0
             flags_exclude);
2114
0
    add_pending_commit_list(revs, exclude, flags_exclude);
2115
0
    commit_list_free(exclude);
2116
2117
0
    b_flags = flags;
2118
0
    a_flags = flags | SYMMETRIC_LEFT;
2119
0
  }
2120
2121
0
  a_obj->flags |= a_flags;
2122
0
  b_obj->flags |= b_flags;
2123
0
  add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags);
2124
0
  add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags);
2125
0
  add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path);
2126
0
  add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path);
2127
0
  return 0;
2128
0
}
2129
2130
static int handle_dotdot(const char *arg,
2131
       struct rev_info *revs, int flags,
2132
       int cant_be_filename)
2133
0
{
2134
0
  struct object_context a_oc = {0}, b_oc = {0};
2135
0
  char *dotdot = strstr(arg, "..");
2136
0
  int ret;
2137
2138
0
  if (!dotdot)
2139
0
    return -1;
2140
2141
0
  *dotdot = '\0';
2142
0
  ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename,
2143
0
            &a_oc, &b_oc);
2144
0
  *dotdot = '.';
2145
2146
0
  object_context_release(&a_oc);
2147
0
  object_context_release(&b_oc);
2148
0
  return ret;
2149
0
}
2150
2151
static int handle_revision_arg_1(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
2152
0
{
2153
0
  struct object_context oc = {0};
2154
0
  char *mark;
2155
0
  struct object *object;
2156
0
  struct object_id oid;
2157
0
  int local_flags;
2158
0
  const char *arg = arg_;
2159
0
  int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
2160
0
  unsigned get_sha1_flags = GET_OID_RECORD_PATH;
2161
0
  int ret;
2162
2163
0
  flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
2164
2165
0
  if (!cant_be_filename && !strcmp(arg, "..")) {
2166
    /*
2167
     * Just ".."?  That is not a range but the
2168
     * pathspec for the parent directory.
2169
     */
2170
0
    ret = -1;
2171
0
    goto out;
2172
0
  }
2173
2174
0
  if (!handle_dotdot(arg, revs, flags, revarg_opt)) {
2175
0
    ret = 0;
2176
0
    goto out;
2177
0
  }
2178
2179
0
  mark = strstr(arg, "^@");
2180
0
  if (mark && !mark[2]) {
2181
0
    *mark = 0;
2182
0
    if (add_parents_only(revs, arg, flags, 0)) {
2183
0
      ret = 0;
2184
0
      goto out;
2185
0
    }
2186
0
    *mark = '^';
2187
0
  }
2188
0
  mark = strstr(arg, "^!");
2189
0
  if (mark && !mark[2]) {
2190
0
    *mark = 0;
2191
0
    if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0))
2192
0
      *mark = '^';
2193
0
  }
2194
0
  mark = strstr(arg, "^-");
2195
0
  if (mark) {
2196
0
    int exclude_parent = 1;
2197
2198
0
    if (mark[2]) {
2199
0
      if (strtol_i(mark + 2, 10, &exclude_parent) ||
2200
0
          exclude_parent < 1) {
2201
0
        ret = -1;
2202
0
        goto out;
2203
0
      }
2204
0
    }
2205
2206
0
    *mark = 0;
2207
0
    if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
2208
0
      *mark = '^';
2209
0
  }
2210
2211
0
  local_flags = 0;
2212
0
  if (*arg == '^') {
2213
0
    local_flags = UNINTERESTING | BOTTOM;
2214
0
    arg++;
2215
0
  }
2216
2217
0
  if (revarg_opt & REVARG_COMMITTISH)
2218
0
    get_sha1_flags |= GET_OID_COMMITTISH;
2219
2220
  /*
2221
   * Even if revs->do_not_die_on_missing_objects is set, we
2222
   * should error out if we can't even get an oid, as
2223
   * `--missing=print` should be able to report missing oids.
2224
   */
2225
0
  if (get_oid_with_context(revs->repo, arg, get_sha1_flags, &oid, &oc)) {
2226
0
    ret = revs->ignore_missing ? 0 : -1;
2227
0
    goto out;
2228
0
  }
2229
0
  if (!cant_be_filename)
2230
0
    verify_non_filename(revs->prefix, arg);
2231
0
  object = get_reference(revs, arg, &oid, flags ^ local_flags);
2232
0
  if (!object) {
2233
0
    ret = (revs->ignore_missing || revs->do_not_die_on_missing_objects) ? 0 : -1;
2234
0
    goto out;
2235
0
  }
2236
0
  add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
2237
0
  add_pending_object_with_path(revs, object, arg, oc.mode, oc.path);
2238
2239
0
  ret = 0;
2240
2241
0
out:
2242
0
  object_context_release(&oc);
2243
0
  return ret;
2244
0
}
2245
2246
int handle_revision_arg(const char *arg, struct rev_info *revs, int flags, unsigned revarg_opt)
2247
0
{
2248
0
  int ret = handle_revision_arg_1(arg, revs, flags, revarg_opt);
2249
0
  if (!ret)
2250
0
    revs->rev_input_given = 1;
2251
0
  return ret;
2252
0
}
2253
2254
static void read_pathspec_from_stdin(struct strbuf *sb,
2255
             struct strvec *prune)
2256
0
{
2257
0
  while (strbuf_getline(sb, stdin) != EOF)
2258
0
    strvec_push(prune, sb->buf);
2259
0
}
2260
2261
static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
2262
0
{
2263
0
  append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
2264
0
}
2265
2266
static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
2267
0
{
2268
0
  append_header_grep_pattern(&revs->grep_filter, field, pattern);
2269
0
}
2270
2271
static void add_message_grep(struct rev_info *revs, const char *pattern)
2272
0
{
2273
0
  add_grep(revs, pattern, GREP_PATTERN_BODY);
2274
0
}
2275
2276
static int parse_count(const char *arg)
2277
0
{
2278
0
  int count;
2279
2280
0
  if (strtol_i(arg, 10, &count) < 0)
2281
0
    die("'%s': not an integer", arg);
2282
0
  return count;
2283
0
}
2284
2285
static timestamp_t parse_age(const char *arg)
2286
0
{
2287
0
  timestamp_t num;
2288
0
  char *p;
2289
2290
0
  errno = 0;
2291
0
  num = parse_timestamp(arg, &p, 10);
2292
0
  if (errno || *p || p == arg)
2293
0
    die("'%s': not a number of seconds since epoch", arg);
2294
0
  return num;
2295
0
}
2296
2297
static void overwrite_argv(int *argc, const char **argv,
2298
         const char **value,
2299
         const struct setup_revision_opt *opt)
2300
0
{
2301
  /*
2302
   * Detect the case when we are overwriting ourselves. The assignment
2303
   * itself would be a noop either way, but this lets us avoid corner
2304
   * cases around the free() and NULL operations.
2305
   */
2306
0
  if (*value != argv[*argc]) {
2307
0
    if (opt && opt->free_removed_argv_elements)
2308
0
      free((char *)argv[*argc]);
2309
0
    argv[*argc] = *value;
2310
0
    *value = NULL;
2311
0
  }
2312
0
  (*argc)++;
2313
0
}
2314
2315
static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
2316
             int *unkc, const char **unkv,
2317
             const struct setup_revision_opt* opt)
2318
0
{
2319
0
  const char *arg = argv[0];
2320
0
  const char *optarg = NULL;
2321
0
  int argcount;
2322
0
  const unsigned hexsz = the_hash_algo->hexsz;
2323
2324
  /* pseudo revision arguments */
2325
0
  if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
2326
0
      !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
2327
0
      !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
2328
0
      !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
2329
0
      !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
2330
0
      !strcmp(arg, "--indexed-objects") ||
2331
0
      !strcmp(arg, "--alternate-refs") ||
2332
0
      starts_with(arg, "--exclude=") || starts_with(arg, "--exclude-hidden=") ||
2333
0
      starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
2334
0
      starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
2335
0
  {
2336
0
    overwrite_argv(unkc, unkv, &argv[0], opt);
2337
0
    return 1;
2338
0
  }
2339
2340
0
  if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
2341
0
    revs->max_count = parse_count(optarg);
2342
0
    revs->no_walk = 0;
2343
0
    return argcount;
2344
0
  } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
2345
0
    revs->skip_count = parse_count(optarg);
2346
0
    return argcount;
2347
0
  } else if ((*arg == '-') && isdigit(arg[1])) {
2348
    /* accept -<digit>, like traditional "head" */
2349
0
    revs->max_count = parse_count(arg + 1);
2350
0
    revs->no_walk = 0;
2351
0
  } else if (!strcmp(arg, "-n")) {
2352
0
    if (argc <= 1)
2353
0
      return error("-n requires an argument");
2354
0
    revs->max_count = parse_count(argv[1]);
2355
0
    revs->no_walk = 0;
2356
0
    return 2;
2357
0
  } else if (skip_prefix(arg, "-n", &optarg)) {
2358
0
    revs->max_count = parse_count(optarg);
2359
0
    revs->no_walk = 0;
2360
0
  } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
2361
0
    revs->max_age = parse_age(optarg);
2362
0
    return argcount;
2363
0
  } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
2364
0
    revs->max_age = approxidate(optarg);
2365
0
    return argcount;
2366
0
  } else if ((argcount = parse_long_opt("since-as-filter", argv, &optarg))) {
2367
0
    revs->max_age_as_filter = approxidate(optarg);
2368
0
    return argcount;
2369
0
  } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
2370
0
    revs->max_age = approxidate(optarg);
2371
0
    return argcount;
2372
0
  } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
2373
0
    revs->min_age = parse_age(optarg);
2374
0
    return argcount;
2375
0
  } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
2376
0
    revs->min_age = approxidate(optarg);
2377
0
    return argcount;
2378
0
  } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
2379
0
    revs->min_age = approxidate(optarg);
2380
0
    return argcount;
2381
0
  } else if (!strcmp(arg, "--maximal-only")) {
2382
0
    revs->maximal_only = 1;
2383
0
  } else if (!strcmp(arg, "--first-parent")) {
2384
0
    revs->first_parent_only = 1;
2385
0
  } else if (!strcmp(arg, "--exclude-first-parent-only")) {
2386
0
    revs->exclude_first_parent_only = 1;
2387
0
  } else if (!strcmp(arg, "--ancestry-path")) {
2388
0
    revs->ancestry_path = 1;
2389
0
    revs->simplify_history = 0;
2390
0
    revs->limited = 1;
2391
0
    revs->ancestry_path_implicit_bottoms = 1;
2392
0
  } else if (skip_prefix(arg, "--ancestry-path=", &optarg)) {
2393
0
    struct commit *c;
2394
0
    struct object_id oid;
2395
0
    const char *msg = _("could not get commit for --ancestry-path argument %s");
2396
2397
0
    revs->ancestry_path = 1;
2398
0
    revs->simplify_history = 0;
2399
0
    revs->limited = 1;
2400
2401
0
    if (repo_get_oid_committish(revs->repo, optarg, &oid))
2402
0
      return error(msg, optarg);
2403
0
    get_reference(revs, optarg, &oid, ANCESTRY_PATH);
2404
0
    c = lookup_commit_reference(revs->repo, &oid);
2405
0
    if (!c)
2406
0
      return error(msg, optarg);
2407
0
    commit_list_insert(c, &revs->ancestry_path_bottoms);
2408
0
  } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
2409
0
    init_reflog_walk(&revs->reflog_info);
2410
0
  } else if (!strcmp(arg, "--default")) {
2411
0
    if (argc <= 1)
2412
0
      return error("bad --default argument");
2413
0
    revs->def = argv[1];
2414
0
    return 2;
2415
0
  } else if (!strcmp(arg, "--merge")) {
2416
0
    revs->show_merge = 1;
2417
0
  } else if (!strcmp(arg, "--topo-order")) {
2418
0
    revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
2419
0
    revs->topo_order = 1;
2420
0
  } else if (!strcmp(arg, "--simplify-merges")) {
2421
0
    revs->simplify_merges = 1;
2422
0
    revs->topo_order = 1;
2423
0
    revs->rewrite_parents = 1;
2424
0
    revs->simplify_history = 0;
2425
0
    revs->limited = 1;
2426
0
  } else if (!strcmp(arg, "--simplify-by-decoration")) {
2427
0
    revs->simplify_merges = 1;
2428
0
    revs->topo_order = 1;
2429
0
    revs->rewrite_parents = 1;
2430
0
    revs->simplify_history = 0;
2431
0
    revs->simplify_by_decoration = 1;
2432
0
    revs->limited = 1;
2433
0
    revs->prune = 1;
2434
0
  } else if (!strcmp(arg, "--date-order")) {
2435
0
    revs->sort_order = REV_SORT_BY_COMMIT_DATE;
2436
0
    revs->topo_order = 1;
2437
0
  } else if (!strcmp(arg, "--author-date-order")) {
2438
0
    revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
2439
0
    revs->topo_order = 1;
2440
0
  } else if (!strcmp(arg, "--parents")) {
2441
0
    revs->rewrite_parents = 1;
2442
0
    revs->print_parents = 1;
2443
0
  } else if (!strcmp(arg, "--dense")) {
2444
0
    revs->dense = 1;
2445
0
  } else if (!strcmp(arg, "--sparse")) {
2446
0
    revs->dense = 0;
2447
0
  } else if (!strcmp(arg, "--in-commit-order")) {
2448
0
    revs->tree_blobs_in_commit_order = 1;
2449
0
  } else if (!strcmp(arg, "--remove-empty")) {
2450
0
    revs->remove_empty_trees = 1;
2451
0
  } else if (!strcmp(arg, "--merges")) {
2452
0
    revs->min_parents = 2;
2453
0
  } else if (!strcmp(arg, "--no-merges")) {
2454
0
    revs->max_parents = 1;
2455
0
  } else if (skip_prefix(arg, "--min-parents=", &optarg)) {
2456
0
    revs->min_parents = parse_count(optarg);
2457
0
  } else if (!strcmp(arg, "--no-min-parents")) {
2458
0
    revs->min_parents = 0;
2459
0
  } else if (skip_prefix(arg, "--max-parents=", &optarg)) {
2460
0
    revs->max_parents = parse_count(optarg);
2461
0
  } else if (!strcmp(arg, "--no-max-parents")) {
2462
0
    revs->max_parents = -1;
2463
0
  } else if (!strcmp(arg, "--boundary")) {
2464
0
    revs->boundary = 1;
2465
0
  } else if (!strcmp(arg, "--left-right")) {
2466
0
    revs->left_right = 1;
2467
0
  } else if (!strcmp(arg, "--left-only")) {
2468
0
    if (revs->right_only)
2469
0
      die(_("options '%s' and '%s' cannot be used together"),
2470
0
          "--left-only", "--right-only/--cherry");
2471
0
    revs->left_only = 1;
2472
0
    revs->limited = 1;
2473
0
  } else if (!strcmp(arg, "--right-only")) {
2474
0
    if (revs->left_only)
2475
0
      die(_("options '%s' and '%s' cannot be used together"), "--right-only", "--left-only");
2476
0
    revs->right_only = 1;
2477
0
    revs->limited = 1;
2478
0
  } else if (!strcmp(arg, "--cherry")) {
2479
0
    if (revs->left_only)
2480
0
      die(_("options '%s' and '%s' cannot be used together"), "--cherry", "--left-only");
2481
0
    revs->cherry_mark = 1;
2482
0
    revs->right_only = 1;
2483
0
    revs->max_parents = 1;
2484
0
    revs->limited = 1;
2485
0
  } else if (!strcmp(arg, "--count")) {
2486
0
    revs->count = 1;
2487
0
  } else if (!strcmp(arg, "--cherry-mark")) {
2488
0
    if (revs->cherry_pick)
2489
0
      die(_("options '%s' and '%s' cannot be used together"), "--cherry-mark", "--cherry-pick");
2490
0
    revs->cherry_mark = 1;
2491
0
    revs->limited = 1; /* needs limit_list() */
2492
0
  } else if (!strcmp(arg, "--cherry-pick")) {
2493
0
    if (revs->cherry_mark)
2494
0
      die(_("options '%s' and '%s' cannot be used together"), "--cherry-pick", "--cherry-mark");
2495
0
    revs->cherry_pick = 1;
2496
0
    revs->limited = 1;
2497
0
  } else if (!strcmp(arg, "--objects")) {
2498
0
    revs->tag_objects = 1;
2499
0
    revs->tree_objects = 1;
2500
0
    revs->blob_objects = 1;
2501
0
  } else if (!strcmp(arg, "--objects-edge")) {
2502
0
    revs->tag_objects = 1;
2503
0
    revs->tree_objects = 1;
2504
0
    revs->blob_objects = 1;
2505
0
    revs->edge_hint = 1;
2506
0
  } else if (!strcmp(arg, "--objects-edge-aggressive")) {
2507
0
    revs->tag_objects = 1;
2508
0
    revs->tree_objects = 1;
2509
0
    revs->blob_objects = 1;
2510
0
    revs->edge_hint = 1;
2511
0
    revs->edge_hint_aggressive = 1;
2512
0
  } else if (!strcmp(arg, "--verify-objects")) {
2513
0
    revs->tag_objects = 1;
2514
0
    revs->tree_objects = 1;
2515
0
    revs->blob_objects = 1;
2516
0
    revs->verify_objects = 1;
2517
0
    disable_commit_graph(revs->repo);
2518
0
  } else if (!strcmp(arg, "--unpacked")) {
2519
0
    revs->unpacked = 1;
2520
0
  } else if (starts_with(arg, "--unpacked=")) {
2521
0
    die(_("--unpacked=<packfile> no longer supported"));
2522
0
  } else if (!strcmp(arg, "--no-kept-objects")) {
2523
0
    revs->no_kept_objects = 1;
2524
0
    revs->keep_pack_cache_flags |= KEPT_PACK_IN_CORE;
2525
0
    revs->keep_pack_cache_flags |= KEPT_PACK_ON_DISK;
2526
0
  } else if (skip_prefix(arg, "--no-kept-objects=", &optarg)) {
2527
0
    revs->no_kept_objects = 1;
2528
0
    if (!strcmp(optarg, "in-core"))
2529
0
      revs->keep_pack_cache_flags |= KEPT_PACK_IN_CORE;
2530
0
    if (!strcmp(optarg, "on-disk"))
2531
0
      revs->keep_pack_cache_flags |= KEPT_PACK_ON_DISK;
2532
0
  } else if (!strcmp(arg, "-r")) {
2533
0
    revs->diff = 1;
2534
0
    revs->diffopt.flags.recursive = 1;
2535
0
  } else if (!strcmp(arg, "-t")) {
2536
0
    revs->diff = 1;
2537
0
    revs->diffopt.flags.recursive = 1;
2538
0
    revs->diffopt.flags.tree_in_recursive = 1;
2539
0
  } else if ((argcount = diff_merges_parse_opts(revs, argv))) {
2540
0
    return argcount;
2541
0
  } else if (!strcmp(arg, "-v")) {
2542
0
    revs->verbose_header = 1;
2543
0
  } else if (!strcmp(arg, "--pretty")) {
2544
0
    revs->verbose_header = 1;
2545
0
    revs->pretty_given = 1;
2546
0
    get_commit_format(NULL, revs);
2547
0
  } else if (skip_prefix(arg, "--pretty=", &optarg) ||
2548
0
       skip_prefix(arg, "--format=", &optarg)) {
2549
    /*
2550
     * Detached form ("--pretty X" as opposed to "--pretty=X")
2551
     * not allowed, since the argument is optional.
2552
     */
2553
0
    revs->verbose_header = 1;
2554
0
    revs->pretty_given = 1;
2555
0
    get_commit_format(optarg, revs);
2556
0
  } else if (!strcmp(arg, "--expand-tabs")) {
2557
0
    revs->expand_tabs_in_log = 8;
2558
0
  } else if (!strcmp(arg, "--no-expand-tabs")) {
2559
0
    revs->expand_tabs_in_log = 0;
2560
0
  } else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
2561
0
    int val;
2562
0
    if (strtol_i(arg, 10, &val) < 0 || val < 0)
2563
0
      die("'%s': not a non-negative integer", arg);
2564
0
    revs->expand_tabs_in_log = val;
2565
0
  } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
2566
0
    enable_default_display_notes(&revs->notes_opt, &revs->show_notes);
2567
0
    revs->show_notes_given = 1;
2568
0
  } else if (!strcmp(arg, "--show-signature")) {
2569
0
    revs->show_signature = 1;
2570
0
  } else if (!strcmp(arg, "--no-show-signature")) {
2571
0
    revs->show_signature = 0;
2572
0
  } else if (!strcmp(arg, "--show-linear-break")) {
2573
0
    revs->break_bar = "                    ..........";
2574
0
    revs->track_linear = 1;
2575
0
    revs->track_first_time = 1;
2576
0
  } else if (skip_prefix(arg, "--show-linear-break=", &optarg)) {
2577
0
    revs->break_bar = xstrdup(optarg);
2578
0
    revs->track_linear = 1;
2579
0
    revs->track_first_time = 1;
2580
0
  } else if (!strcmp(arg, "--show-notes-by-default")) {
2581
0
    revs->show_notes_by_default = 1;
2582
0
  } else if (skip_prefix(arg, "--show-notes=", &optarg) ||
2583
0
       skip_prefix(arg, "--notes=", &optarg)) {
2584
0
    if (starts_with(arg, "--show-notes=") &&
2585
0
        revs->notes_opt.use_default_notes < 0)
2586
0
      revs->notes_opt.use_default_notes = 1;
2587
0
    enable_ref_display_notes(&revs->notes_opt, &revs->show_notes, optarg);
2588
0
    revs->show_notes_given = 1;
2589
0
  } else if (!strcmp(arg, "--no-notes")) {
2590
0
    disable_display_notes(&revs->notes_opt, &revs->show_notes);
2591
0
    revs->show_notes_given = 1;
2592
0
  } else if (!strcmp(arg, "--standard-notes")) {
2593
0
    revs->show_notes_given = 1;
2594
0
    revs->notes_opt.use_default_notes = 1;
2595
0
  } else if (!strcmp(arg, "--no-standard-notes")) {
2596
0
    revs->notes_opt.use_default_notes = 0;
2597
0
  } else if (!strcmp(arg, "--oneline")) {
2598
0
    revs->verbose_header = 1;
2599
0
    get_commit_format("oneline", revs);
2600
0
    revs->pretty_given = 1;
2601
0
    revs->abbrev_commit = 1;
2602
0
  } else if (!strcmp(arg, "--graph")) {
2603
0
    graph_clear(revs->graph);
2604
0
    revs->graph = graph_init(revs);
2605
0
  } else if (!strcmp(arg, "--no-graph")) {
2606
0
    graph_clear(revs->graph);
2607
0
    revs->graph = NULL;
2608
0
  } else if (!strcmp(arg, "--encode-email-headers")) {
2609
0
    revs->encode_email_headers = 1;
2610
0
  } else if (!strcmp(arg, "--no-encode-email-headers")) {
2611
0
    revs->encode_email_headers = 0;
2612
0
  } else if (!strcmp(arg, "--root")) {
2613
0
    revs->show_root_diff = 1;
2614
0
  } else if (!strcmp(arg, "--no-commit-id")) {
2615
0
    revs->no_commit_id = 1;
2616
0
  } else if (!strcmp(arg, "--always")) {
2617
0
    revs->always_show_header = 1;
2618
0
  } else if (!strcmp(arg, "--no-abbrev")) {
2619
0
    revs->abbrev = 0;
2620
0
  } else if (!strcmp(arg, "--abbrev")) {
2621
0
    revs->abbrev = DEFAULT_ABBREV;
2622
0
  } else if (skip_prefix(arg, "--abbrev=", &optarg)) {
2623
0
    revs->abbrev = strtoul(optarg, NULL, 10);
2624
0
    if (revs->abbrev < MINIMUM_ABBREV)
2625
0
      revs->abbrev = MINIMUM_ABBREV;
2626
0
    else if (revs->abbrev > hexsz)
2627
0
      revs->abbrev = hexsz;
2628
0
  } else if (!strcmp(arg, "--abbrev-commit")) {
2629
0
    revs->abbrev_commit = 1;
2630
0
    revs->abbrev_commit_given = 1;
2631
0
  } else if (!strcmp(arg, "--no-abbrev-commit")) {
2632
0
    revs->abbrev_commit = 0;
2633
0
  } else if (!strcmp(arg, "--full-diff")) {
2634
0
    revs->diff = 1;
2635
0
    revs->full_diff = 1;
2636
0
  } else if (!strcmp(arg, "--show-pulls")) {
2637
0
    revs->show_pulls = 1;
2638
0
  } else if (!strcmp(arg, "--full-history")) {
2639
0
    revs->simplify_history = 0;
2640
0
  } else if (!strcmp(arg, "--relative-date")) {
2641
0
    revs->date_mode.type = DATE_RELATIVE;
2642
0
    revs->date_mode_explicit = 1;
2643
0
  } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
2644
0
    parse_date_format(optarg, &revs->date_mode);
2645
0
    revs->date_mode_explicit = 1;
2646
0
    return argcount;
2647
0
  } else if (!strcmp(arg, "--log-size")) {
2648
0
    revs->show_log_size = 1;
2649
0
  }
2650
  /*
2651
   * Grepping the commit log
2652
   */
2653
0
  else if ((argcount = parse_long_opt("author", argv, &optarg))) {
2654
0
    add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
2655
0
    return argcount;
2656
0
  } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
2657
0
    add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
2658
0
    return argcount;
2659
0
  } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
2660
0
    add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
2661
0
    return argcount;
2662
0
  } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
2663
0
    add_message_grep(revs, optarg);
2664
0
    return argcount;
2665
0
  } else if (!strcmp(arg, "--basic-regexp")) {
2666
0
    revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE;
2667
0
  } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
2668
0
    revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
2669
0
  } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
2670
0
    revs->grep_filter.ignore_case = 1;
2671
0
    revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE;
2672
0
  } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
2673
0
    revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
2674
0
  } else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) {
2675
0
    revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;
2676
0
  } else if (!strcmp(arg, "--all-match")) {
2677
0
    revs->grep_filter.all_match = 1;
2678
0
  } else if (!strcmp(arg, "--invert-grep")) {
2679
0
    revs->grep_filter.no_body_match = 1;
2680
0
  } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
2681
0
    free(git_log_output_encoding);
2682
0
    if (strcmp(optarg, "none"))
2683
0
      git_log_output_encoding = xstrdup(optarg);
2684
0
    else
2685
0
      git_log_output_encoding = xstrdup("");
2686
0
    return argcount;
2687
0
  } else if (!strcmp(arg, "--reverse")) {
2688
0
    revs->reverse ^= 1;
2689
0
  } else if (!strcmp(arg, "--children")) {
2690
0
    revs->children.name = "children";
2691
0
    revs->limited = 1;
2692
0
  } else if (!strcmp(arg, "--ignore-missing")) {
2693
0
    revs->ignore_missing = 1;
2694
0
  } else if (opt && opt->allow_exclude_promisor_objects &&
2695
0
       !strcmp(arg, "--exclude-promisor-objects")) {
2696
0
    if (fetch_if_missing)
2697
0
      BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
2698
0
    revs->exclude_promisor_objects = 1;
2699
0
  } else {
2700
0
    int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
2701
0
    if (!opts)
2702
0
      overwrite_argv(unkc, unkv, &argv[0], opt);
2703
0
    return opts;
2704
0
  }
2705
2706
0
  return 1;
2707
0
}
2708
2709
void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2710
      const struct option *options,
2711
      const char * const usagestr[])
2712
0
{
2713
0
  int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
2714
0
            &ctx->cpidx, ctx->out, NULL);
2715
0
  if (n <= 0) {
2716
0
    error("unknown option `%s'", ctx->argv[0]);
2717
0
    usage_with_options(usagestr, options);
2718
0
  }
2719
0
  ctx->argv += n;
2720
0
  ctx->argc -= n;
2721
0
}
2722
2723
void revision_opts_finish(struct rev_info *revs)
2724
0
{
2725
0
  if (revs->graph && revs->track_linear)
2726
0
    die(_("options '%s' and '%s' cannot be used together"), "--show-linear-break", "--graph");
2727
2728
0
  if (revs->graph) {
2729
0
    revs->topo_order = 1;
2730
0
    revs->rewrite_parents = 1;
2731
0
  }
2732
0
}
2733
2734
static int for_each_bisect_ref(struct ref_store *refs, refs_for_each_cb fn,
2735
             void *cb_data, const char *term)
2736
0
{
2737
0
  struct refs_for_each_ref_options opts = { 0 };
2738
0
  struct strbuf bisect_refs = STRBUF_INIT;
2739
0
  int status;
2740
0
  strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
2741
0
  opts.prefix = bisect_refs.buf;
2742
0
  status = refs_for_each_ref_ext(refs, fn, cb_data, &opts);
2743
0
  strbuf_release(&bisect_refs);
2744
0
  return status;
2745
0
}
2746
2747
static int for_each_bad_bisect_ref(struct ref_store *refs, refs_for_each_cb fn, void *cb_data)
2748
0
{
2749
0
  return for_each_bisect_ref(refs, fn, cb_data, term_bad);
2750
0
}
2751
2752
static int for_each_good_bisect_ref(struct ref_store *refs, refs_for_each_cb fn, void *cb_data)
2753
0
{
2754
0
  return for_each_bisect_ref(refs, fn, cb_data, term_good);
2755
0
}
2756
2757
static int handle_revision_pseudo_opt(struct rev_info *revs,
2758
              const char **argv, int *flags)
2759
0
{
2760
0
  const char *arg = argv[0];
2761
0
  const char *optarg;
2762
0
  struct ref_store *refs;
2763
0
  int argcount;
2764
2765
0
  if (revs->repo != the_repository) {
2766
    /*
2767
     * We need some something like get_submodule_worktrees()
2768
     * before we can go through all worktrees of a submodule,
2769
     * .e.g with adding all HEADs from --all, which is not
2770
     * supported right now, so stick to single worktree.
2771
     */
2772
0
    if (!revs->single_worktree)
2773
0
      BUG("--single-worktree cannot be used together with submodule");
2774
0
  }
2775
0
  refs = get_main_ref_store(revs->repo);
2776
2777
  /*
2778
   * NOTE!
2779
   *
2780
   * Commands like "git shortlog" will not accept the options below
2781
   * unless parse_revision_opt queues them (as opposed to erroring
2782
   * out).
2783
   *
2784
   * When implementing your new pseudo-option, remember to
2785
   * register it in the list at the top of handle_revision_opt.
2786
   */
2787
0
  if (!strcmp(arg, "--all")) {
2788
0
    handle_refs(refs, revs, *flags, refs_for_each_ref);
2789
0
    handle_refs(refs, revs, *flags, refs_head_ref);
2790
0
    if (!revs->single_worktree) {
2791
0
      struct all_refs_cb cb;
2792
2793
0
      init_all_refs_cb(&cb, revs, *flags);
2794
0
      other_head_refs(handle_one_ref, &cb);
2795
0
    }
2796
0
    clear_ref_exclusions(&revs->ref_excludes);
2797
0
  } else if (!strcmp(arg, "--branches")) {
2798
0
    if (revs->ref_excludes.hidden_refs_configured)
2799
0
      return error(_("options '%s' and '%s' cannot be used together"),
2800
0
             "--exclude-hidden", "--branches");
2801
0
    handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
2802
0
    clear_ref_exclusions(&revs->ref_excludes);
2803
0
  } else if (!strcmp(arg, "--bisect")) {
2804
0
    read_bisect_terms(&term_bad, &term_good);
2805
0
    handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
2806
0
    handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
2807
0
          for_each_good_bisect_ref);
2808
0
    revs->bisect = 1;
2809
0
  } else if (!strcmp(arg, "--tags")) {
2810
0
    if (revs->ref_excludes.hidden_refs_configured)
2811
0
      return error(_("options '%s' and '%s' cannot be used together"),
2812
0
             "--exclude-hidden", "--tags");
2813
0
    handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
2814
0
    clear_ref_exclusions(&revs->ref_excludes);
2815
0
  } else if (!strcmp(arg, "--remotes")) {
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", "--remotes");
2819
0
    handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
2820
0
    clear_ref_exclusions(&revs->ref_excludes);
2821
0
  } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2822
0
    struct refs_for_each_ref_options opts = {
2823
0
      .pattern = optarg,
2824
0
    };
2825
0
    struct all_refs_cb cb;
2826
0
    init_all_refs_cb(&cb, revs, *flags);
2827
0
    refs_for_each_ref_ext(get_main_ref_store(the_repository),
2828
0
              handle_one_ref, &cb, &opts);
2829
0
    clear_ref_exclusions(&revs->ref_excludes);
2830
0
    return argcount;
2831
0
  } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2832
0
    add_ref_exclusion(&revs->ref_excludes, optarg);
2833
0
    return argcount;
2834
0
  } else if ((argcount = parse_long_opt("exclude-hidden", argv, &optarg))) {
2835
0
    exclude_hidden_refs(&revs->ref_excludes, optarg);
2836
0
    return argcount;
2837
0
  } else if (skip_prefix(arg, "--branches=", &optarg)) {
2838
0
    struct refs_for_each_ref_options opts = {
2839
0
      .prefix = "refs/heads/",
2840
0
      .trim_prefix = strlen("refs/heads/"),
2841
0
      .pattern = optarg,
2842
0
    };
2843
0
    struct all_refs_cb cb;
2844
0
    if (revs->ref_excludes.hidden_refs_configured)
2845
0
      return error(_("options '%s' and '%s' cannot be used together"),
2846
0
             "--exclude-hidden", "--branches");
2847
0
    init_all_refs_cb(&cb, revs, *flags);
2848
0
    refs_for_each_ref_ext(get_main_ref_store(the_repository),
2849
0
              handle_one_ref, &cb, &opts);
2850
0
    clear_ref_exclusions(&revs->ref_excludes);
2851
0
  } else if (skip_prefix(arg, "--tags=", &optarg)) {
2852
0
    struct refs_for_each_ref_options opts = {
2853
0
      .prefix = "refs/tags/",
2854
0
      .trim_prefix = strlen("refs/tags/"),
2855
0
      .pattern = optarg,
2856
0
    };
2857
0
    struct all_refs_cb cb;
2858
0
    if (revs->ref_excludes.hidden_refs_configured)
2859
0
      return error(_("options '%s' and '%s' cannot be used together"),
2860
0
             "--exclude-hidden", "--tags");
2861
0
    init_all_refs_cb(&cb, revs, *flags);
2862
0
    refs_for_each_ref_ext(get_main_ref_store(the_repository),
2863
0
              handle_one_ref, &cb, &opts);
2864
0
    clear_ref_exclusions(&revs->ref_excludes);
2865
0
  } else if (skip_prefix(arg, "--remotes=", &optarg)) {
2866
0
    struct refs_for_each_ref_options opts = {
2867
0
      .prefix = "refs/remotes/",
2868
0
      .trim_prefix = strlen("refs/remotes/"),
2869
0
      .pattern = optarg,
2870
0
    };
2871
0
    struct all_refs_cb cb;
2872
0
    if (revs->ref_excludes.hidden_refs_configured)
2873
0
      return error(_("options '%s' and '%s' cannot be used together"),
2874
0
             "--exclude-hidden", "--remotes");
2875
0
    init_all_refs_cb(&cb, revs, *flags);
2876
0
    refs_for_each_ref_ext(get_main_ref_store(the_repository),
2877
0
              handle_one_ref, &cb, &opts);
2878
0
    clear_ref_exclusions(&revs->ref_excludes);
2879
0
  } else if (!strcmp(arg, "--reflog")) {
2880
0
    add_reflogs_to_pending(revs, *flags);
2881
0
  } else if (!strcmp(arg, "--indexed-objects")) {
2882
0
    add_index_objects_to_pending(revs, *flags);
2883
0
  } else if (!strcmp(arg, "--alternate-refs")) {
2884
0
    add_alternate_refs_to_pending(revs, *flags);
2885
0
  } else if (!strcmp(arg, "--not")) {
2886
0
    *flags ^= UNINTERESTING | BOTTOM;
2887
0
  } else if (!strcmp(arg, "--no-walk")) {
2888
0
    revs->no_walk = 1;
2889
0
  } else if (skip_prefix(arg, "--no-walk=", &optarg)) {
2890
    /*
2891
     * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2892
     * not allowed, since the argument is optional.
2893
     */
2894
0
    revs->no_walk = 1;
2895
0
    if (!strcmp(optarg, "sorted"))
2896
0
      revs->unsorted_input = 0;
2897
0
    else if (!strcmp(optarg, "unsorted"))
2898
0
      revs->unsorted_input = 1;
2899
0
    else
2900
0
      return error("invalid argument to --no-walk");
2901
0
  } else if (!strcmp(arg, "--do-walk")) {
2902
0
    revs->no_walk = 0;
2903
0
  } else if (!strcmp(arg, "--single-worktree")) {
2904
0
    revs->single_worktree = 1;
2905
0
  } else if (skip_prefix(arg, ("--filter="), &arg)) {
2906
0
    parse_list_objects_filter(&revs->filter, arg);
2907
0
  } else if (!strcmp(arg, ("--no-filter"))) {
2908
0
    list_objects_filter_set_no_filter(&revs->filter);
2909
0
  } else {
2910
0
    return 0;
2911
0
  }
2912
2913
0
  return 1;
2914
0
}
2915
2916
static void read_revisions_from_stdin(struct rev_info *revs,
2917
              struct strvec *prune)
2918
0
{
2919
0
  struct strbuf sb;
2920
0
  int seen_dashdash = 0;
2921
0
  int seen_end_of_options = 0;
2922
0
  int save_warning;
2923
0
  int flags = 0;
2924
2925
0
  save_warning = warn_on_object_refname_ambiguity;
2926
0
  warn_on_object_refname_ambiguity = 0;
2927
2928
0
  strbuf_init(&sb, 1000);
2929
0
  while (strbuf_getline(&sb, stdin) != EOF) {
2930
0
    if (!sb.len)
2931
0
      break;
2932
2933
0
    if (!strcmp(sb.buf, "--")) {
2934
0
      seen_dashdash = 1;
2935
0
      break;
2936
0
    }
2937
2938
0
    if (!seen_end_of_options && sb.buf[0] == '-') {
2939
0
      const char *argv[] = { sb.buf, NULL };
2940
2941
0
      if (!strcmp(sb.buf, "--end-of-options")) {
2942
0
        seen_end_of_options = 1;
2943
0
        continue;
2944
0
      }
2945
2946
0
      if (handle_revision_pseudo_opt(revs, argv, &flags) > 0)
2947
0
        continue;
2948
2949
0
      die(_("invalid option '%s' in --stdin mode"), sb.buf);
2950
0
    }
2951
2952
0
    if (handle_revision_arg(sb.buf, revs, flags,
2953
0
          REVARG_CANNOT_BE_FILENAME))
2954
0
      die("bad revision '%s'", sb.buf);
2955
0
  }
2956
0
  if (seen_dashdash)
2957
0
    read_pathspec_from_stdin(&sb, prune);
2958
2959
0
  strbuf_release(&sb);
2960
0
  warn_on_object_refname_ambiguity = save_warning;
2961
0
}
2962
2963
static void NORETURN diagnose_missing_default(const char *def)
2964
0
{
2965
0
  int flags;
2966
0
  const char *refname;
2967
2968
0
  refname = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
2969
0
            def, 0, NULL, &flags);
2970
0
  if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2971
0
    die(_("your current branch appears to be broken"));
2972
2973
0
  skip_prefix(refname, "refs/heads/", &refname);
2974
0
  die(_("your current branch '%s' does not have any commits yet"),
2975
0
      refname);
2976
0
}
2977
2978
/*
2979
 * Parse revision information, filling in the "rev_info" structure,
2980
 * and removing the used arguments from the argument list.
2981
 *
2982
 * Returns the number of arguments left that weren't recognized
2983
 * (which are also moved to the head of the argument list)
2984
 */
2985
int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2986
0
{
2987
0
  int i, flags, left, seen_dashdash, revarg_opt;
2988
0
  struct strvec prune_data = STRVEC_INIT;
2989
0
  int seen_end_of_options = 0;
2990
2991
  /* First, search for "--" */
2992
0
  if (opt && opt->assume_dashdash) {
2993
0
    seen_dashdash = 1;
2994
0
  } else {
2995
0
    seen_dashdash = 0;
2996
0
    for (i = 1; i < argc; i++) {
2997
0
      const char *arg = argv[i];
2998
0
      if (strcmp(arg, "--"))
2999
0
        continue;
3000
0
      if (opt && opt->free_removed_argv_elements)
3001
0
        free((char *)argv[i]);
3002
0
      argv[i] = NULL;
3003
0
      argc = i;
3004
0
      if (argv[i + 1])
3005
0
        strvec_pushv(&prune_data, argv + i + 1);
3006
0
      seen_dashdash = 1;
3007
0
      break;
3008
0
    }
3009
0
  }
3010
3011
  /* Second, deal with arguments and options */
3012
0
  flags = 0;
3013
0
  revarg_opt = opt ? opt->revarg_opt : 0;
3014
0
  if (seen_dashdash)
3015
0
    revarg_opt |= REVARG_CANNOT_BE_FILENAME;
3016
0
  for (left = i = 1; i < argc; i++) {
3017
0
    const char *arg = argv[i];
3018
0
    if (!seen_end_of_options && *arg == '-') {
3019
0
      int opts;
3020
3021
0
      opts = handle_revision_pseudo_opt(
3022
0
            revs, argv + i,
3023
0
            &flags);
3024
0
      if (opts > 0) {
3025
0
        i += opts - 1;
3026
0
        continue;
3027
0
      }
3028
3029
0
      if (!strcmp(arg, "--stdin")) {
3030
0
        if (revs->disable_stdin) {
3031
0
          overwrite_argv(&left, argv, &argv[i], opt);
3032
0
          continue;
3033
0
        }
3034
0
        if (revs->read_from_stdin++)
3035
0
          die("--stdin given twice?");
3036
0
        read_revisions_from_stdin(revs, &prune_data);
3037
0
        continue;
3038
0
      }
3039
3040
0
      if (!strcmp(arg, "--end-of-options")) {
3041
0
        seen_end_of_options = 1;
3042
0
        continue;
3043
0
      }
3044
3045
0
      opts = handle_revision_opt(revs, argc - i, argv + i,
3046
0
               &left, argv, opt);
3047
0
      if (opts > 0) {
3048
0
        i += opts - 1;
3049
0
        continue;
3050
0
      }
3051
0
      if (opts < 0)
3052
0
        exit(128);
3053
0
      continue;
3054
0
    }
3055
3056
3057
0
    if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
3058
0
      int j;
3059
0
      if (seen_dashdash || *arg == '^')
3060
0
        die("bad revision '%s'", arg);
3061
3062
      /* If we didn't have a "--":
3063
       * (1) all filenames must exist;
3064
       * (2) all rev-args must not be interpretable
3065
       *     as a valid filename.
3066
       * but the latter we have checked in the main loop.
3067
       */
3068
0
      for (j = i; j < argc; j++)
3069
0
        verify_filename(revs->prefix, argv[j], j == i);
3070
3071
0
      strvec_pushv(&prune_data, argv + i);
3072
0
      break;
3073
0
    }
3074
0
  }
3075
0
  revision_opts_finish(revs);
3076
3077
0
  if (prune_data.nr) {
3078
    /*
3079
     * If we need to introduce the magic "a lone ':' means no
3080
     * pathspec whatsoever", here is the place to do so.
3081
     *
3082
     * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
3083
     *  prune_data.nr = 0;
3084
     *  prune_data.alloc = 0;
3085
     *  free(prune_data.path);
3086
     *  prune_data.path = NULL;
3087
     * } else {
3088
     *  terminate prune_data.alloc with NULL and
3089
     *  call init_pathspec() to set revs->prune_data here.
3090
     * }
3091
     */
3092
0
    parse_pathspec(&revs->prune_data, 0, 0,
3093
0
             revs->prefix, prune_data.v);
3094
0
  }
3095
0
  strvec_clear(&prune_data);
3096
3097
0
  if (!revs->def)
3098
0
    revs->def = opt ? opt->def : NULL;
3099
0
  if (opt && opt->tweak)
3100
0
    opt->tweak(revs);
3101
0
  if (revs->show_merge)
3102
0
    prepare_show_merge(revs);
3103
0
  if (revs->def && !revs->pending.nr && !revs->rev_input_given) {
3104
0
    struct object_id oid;
3105
0
    struct object *object;
3106
0
    struct object_context oc;
3107
0
    if (get_oid_with_context(revs->repo, revs->def, 0, &oid, &oc))
3108
0
      diagnose_missing_default(revs->def);
3109
0
    object = get_reference(revs, revs->def, &oid, 0);
3110
0
    add_pending_object_with_mode(revs, object, revs->def, oc.mode);
3111
0
    object_context_release(&oc);
3112
0
  }
3113
3114
  /* Did the user ask for any diff output? Run the diff! */
3115
0
  if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
3116
0
    revs->diff = 1;
3117
3118
  /* Pickaxe, diff-filter and rename following need diffs */
3119
0
  if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
3120
0
      revs->diffopt.filter || revs->diffopt.filter_not ||
3121
0
      revs->diffopt.flags.follow_renames)
3122
0
    revs->diff = 1;
3123
3124
0
  if (revs->diffopt.objfind)
3125
0
    revs->simplify_history = 0;
3126
3127
0
  if (revs->line_level_traverse) {
3128
0
    if (want_ancestry(revs))
3129
0
      revs->limited = 1;
3130
0
    revs->topo_order = 1;
3131
0
  }
3132
3133
0
  if (revs->topo_order && !generation_numbers_enabled(the_repository))
3134
0
    revs->limited = 1;
3135
3136
0
  if (revs->prune_data.nr) {
3137
0
    copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
3138
    /* Can't prune commits with rename following: the paths change.. */
3139
0
    if (!revs->diffopt.flags.follow_renames)
3140
0
      revs->prune = 1;
3141
0
    if (!revs->full_diff)
3142
0
      copy_pathspec(&revs->diffopt.pathspec,
3143
0
              &revs->prune_data);
3144
0
  }
3145
3146
0
  diff_merges_setup_revs(revs);
3147
3148
0
  revs->diffopt.abbrev = revs->abbrev;
3149
3150
0
  diff_setup_done(&revs->diffopt);
3151
3152
0
  if (!is_encoding_utf8(get_log_output_encoding()))
3153
0
    revs->grep_filter.ignore_locale = 1;
3154
0
  compile_grep_patterns(&revs->grep_filter);
3155
3156
0
  if (revs->reflog_info && revs->limited)
3157
0
    die("cannot combine --walk-reflogs with history-limiting options");
3158
0
  if (revs->rewrite_parents && revs->children.name)
3159
0
    die(_("options '%s' and '%s' cannot be used together"), "--parents", "--children");
3160
0
  if (revs->filter.choice && !revs->blob_objects)
3161
0
    die(_("object filtering requires --objects"));
3162
3163
  /*
3164
   * Limitations on the graph functionality
3165
   */
3166
0
  die_for_incompatible_opt3(!!revs->graph, "--graph",
3167
0
          !!revs->reverse, "--reverse",
3168
0
          !!revs->reflog_info, "--walk-reflogs");
3169
3170
0
  die_for_incompatible_opt2(!!revs->boundary, "--boundary",
3171
0
          !!revs->maximal_only, "--maximal-only");
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
  commit_list_free(list);
3248
0
}
3249
3250
void release_revisions(struct rev_info *revs)
3251
0
{
3252
0
  commit_list_free(revs->commits);
3253
0
  commit_list_free(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
  commit_list_free(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 object_info *oi UNUSED,
3653
            void *cb)
3654
0
{
3655
0
  struct rev_info *revs = cb;
3656
0
  struct object *o = lookup_unknown_object(revs->repo, oid);
3657
0
  o->flags |= UNINTERESTING | SEEN;
3658
0
  return 0;
3659
0
}
3660
3661
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
3662
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
3663
0
3664
0
struct topo_walk_info {
3665
0
  timestamp_t min_generation;
3666
0
  struct prio_queue explore_queue;
3667
0
  struct prio_queue indegree_queue;
3668
0
  struct prio_queue topo_queue;
3669
0
  struct indegree_slab indegree;
3670
0
  struct author_date_slab author_date;
3671
0
};
3672
0
3673
0
static int topo_walk_atexit_registered;
3674
0
static unsigned int count_explore_walked;
3675
0
static unsigned int count_indegree_walked;
3676
0
static unsigned int count_topo_walked;
3677
0
3678
0
static void trace2_topo_walk_statistics_atexit(void)
3679
0
{
3680
0
  struct json_writer jw = JSON_WRITER_INIT;
3681
3682
0
  jw_object_begin(&jw, 0);
3683
0
  jw_object_intmax(&jw, "count_explore_walked", count_explore_walked);
3684
0
  jw_object_intmax(&jw, "count_indegree_walked", count_indegree_walked);
3685
0
  jw_object_intmax(&jw, "count_topo_walked", count_topo_walked);
3686
0
  jw_end(&jw);
3687
3688
0
  trace2_data_json("topo_walk", the_repository, "statistics", &jw);
3689
3690
0
  jw_release(&jw);
3691
0
}
3692
3693
static inline void test_flag_and_insert(struct prio_queue *q, struct commit *c, int flag)
3694
0
{
3695
0
  if (c->object.flags & flag)
3696
0
    return;
3697
3698
0
  c->object.flags |= flag;
3699
0
  prio_queue_put(q, c);
3700
0
}
3701
3702
static void explore_walk_step(struct rev_info *revs)
3703
0
{
3704
0
  struct topo_walk_info *info = revs->topo_walk_info;
3705
0
  struct commit_list *p;
3706
0
  struct commit *c = prio_queue_get(&info->explore_queue);
3707
3708
0
  if (!c)
3709
0
    return;
3710
3711
0
  if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3712
0
    return;
3713
3714
0
  count_explore_walked++;
3715
3716
0
  if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3717
0
    record_author_date(&info->author_date, c);
3718
3719
0
  if (revs->max_age != -1 && (c->date < revs->max_age))
3720
0
    c->object.flags |= UNINTERESTING;
3721
3722
0
  if (process_parents(revs, c, NULL, NULL) < 0)
3723
0
    return;
3724
3725
0
  if (c->object.flags & UNINTERESTING)
3726
0
    mark_parents_uninteresting(revs, c);
3727
3728
0
  for (p = c->parents; p; p = p->next)
3729
0
    test_flag_and_insert(&info->explore_queue, p->item, TOPO_WALK_EXPLORED);
3730
0
}
3731
3732
static void explore_to_depth(struct rev_info *revs,
3733
           timestamp_t gen_cutoff)
3734
0
{
3735
0
  struct topo_walk_info *info = revs->topo_walk_info;
3736
0
  struct commit *c;
3737
0
  while ((c = prio_queue_peek(&info->explore_queue)) &&
3738
0
         commit_graph_generation(c) >= gen_cutoff)
3739
0
    explore_walk_step(revs);
3740
0
}
3741
3742
static void indegree_walk_step(struct rev_info *revs)
3743
0
{
3744
0
  struct commit_list *p;
3745
0
  struct topo_walk_info *info = revs->topo_walk_info;
3746
0
  struct commit *c = prio_queue_get(&info->indegree_queue);
3747
3748
0
  if (!c)
3749
0
    return;
3750
3751
0
  if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3752
0
    return;
3753
3754
0
  count_indegree_walked++;
3755
3756
0
  explore_to_depth(revs, commit_graph_generation(c));
3757
3758
0
  for (p = c->parents; p; p = p->next) {
3759
0
    struct commit *parent = p->item;
3760
0
    int *pi = indegree_slab_at(&info->indegree, parent);
3761
3762
0
    if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3763
0
      return;
3764
3765
0
    if (*pi)
3766
0
      (*pi)++;
3767
0
    else
3768
0
      *pi = 2;
3769
3770
0
    test_flag_and_insert(&info->indegree_queue, parent, TOPO_WALK_INDEGREE);
3771
3772
0
    if (revs->first_parent_only)
3773
0
      return;
3774
0
  }
3775
0
}
3776
3777
static void compute_indegrees_to_depth(struct rev_info *revs,
3778
               timestamp_t gen_cutoff)
3779
0
{
3780
0
  struct topo_walk_info *info = revs->topo_walk_info;
3781
0
  struct commit *c;
3782
0
  while ((c = prio_queue_peek(&info->indegree_queue)) &&
3783
0
         commit_graph_generation(c) >= gen_cutoff)
3784
0
    indegree_walk_step(revs);
3785
0
}
3786
3787
static void release_revisions_topo_walk_info(struct topo_walk_info *info)
3788
0
{
3789
0
  if (!info)
3790
0
    return;
3791
0
  clear_prio_queue(&info->explore_queue);
3792
0
  clear_prio_queue(&info->indegree_queue);
3793
0
  clear_prio_queue(&info->topo_queue);
3794
0
  clear_indegree_slab(&info->indegree);
3795
0
  clear_author_date_slab(&info->author_date);
3796
0
  free(info);
3797
0
}
3798
3799
static void reset_topo_walk(struct rev_info *revs)
3800
0
{
3801
0
  release_revisions_topo_walk_info(revs->topo_walk_info);
3802
0
  revs->topo_walk_info = NULL;
3803
0
}
3804
3805
static void init_topo_walk(struct rev_info *revs)
3806
0
{
3807
0
  struct topo_walk_info *info;
3808
0
  struct commit_list *list;
3809
0
  if (revs->topo_walk_info)
3810
0
    reset_topo_walk(revs);
3811
3812
0
  revs->topo_walk_info = xmalloc(sizeof(struct topo_walk_info));
3813
0
  info = revs->topo_walk_info;
3814
0
  memset(info, 0, sizeof(struct topo_walk_info));
3815
3816
0
  init_indegree_slab(&info->indegree);
3817
0
  memset(&info->explore_queue, 0, sizeof(info->explore_queue));
3818
0
  memset(&info->indegree_queue, 0, sizeof(info->indegree_queue));
3819
0
  memset(&info->topo_queue, 0, sizeof(info->topo_queue));
3820
3821
0
  switch (revs->sort_order) {
3822
0
  default: /* REV_SORT_IN_GRAPH_ORDER */
3823
0
    info->topo_queue.compare = NULL;
3824
0
    break;
3825
0
  case REV_SORT_BY_COMMIT_DATE:
3826
0
    info->topo_queue.compare = compare_commits_by_commit_date;
3827
0
    break;
3828
0
  case REV_SORT_BY_AUTHOR_DATE:
3829
0
    init_author_date_slab(&info->author_date);
3830
0
    info->topo_queue.compare = compare_commits_by_author_date;
3831
0
    info->topo_queue.cb_data = &info->author_date;
3832
0
    break;
3833
0
  }
3834
3835
0
  info->explore_queue.compare = compare_commits_by_gen_then_commit_date;
3836
0
  info->indegree_queue.compare = compare_commits_by_gen_then_commit_date;
3837
3838
0
  info->min_generation = GENERATION_NUMBER_INFINITY;
3839
0
  for (list = revs->commits; list; list = list->next) {
3840
0
    struct commit *c = list->item;
3841
0
    timestamp_t generation;
3842
3843
0
    if (repo_parse_commit_gently(revs->repo, c, 1))
3844
0
      continue;
3845
3846
0
    test_flag_and_insert(&info->explore_queue, c, TOPO_WALK_EXPLORED);
3847
0
    test_flag_and_insert(&info->indegree_queue, c, TOPO_WALK_INDEGREE);
3848
3849
0
    generation = commit_graph_generation(c);
3850
0
    if (generation < info->min_generation)
3851
0
      info->min_generation = generation;
3852
3853
0
    *(indegree_slab_at(&info->indegree, c)) = 1;
3854
3855
0
    if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3856
0
      record_author_date(&info->author_date, c);
3857
0
  }
3858
0
  compute_indegrees_to_depth(revs, info->min_generation);
3859
3860
0
  for (list = revs->commits; list; list = list->next) {
3861
0
    struct commit *c = list->item;
3862
3863
0
    if (*(indegree_slab_at(&info->indegree, c)) == 1)
3864
0
      prio_queue_put(&info->topo_queue, c);
3865
0
  }
3866
3867
  /*
3868
   * This is unfortunate; the initial tips need to be shown
3869
   * in the order given from the revision traversal machinery.
3870
   */
3871
0
  if (revs->sort_order == REV_SORT_IN_GRAPH_ORDER)
3872
0
    prio_queue_reverse(&info->topo_queue);
3873
3874
0
  if (trace2_is_enabled() && !topo_walk_atexit_registered) {
3875
0
    atexit(trace2_topo_walk_statistics_atexit);
3876
0
    topo_walk_atexit_registered = 1;
3877
0
  }
3878
0
}
3879
3880
static struct commit *next_topo_commit(struct rev_info *revs)
3881
0
{
3882
0
  struct commit *c;
3883
0
  struct topo_walk_info *info = revs->topo_walk_info;
3884
3885
  /* pop next off of topo_queue */
3886
0
  c = prio_queue_get(&info->topo_queue);
3887
3888
0
  if (c)
3889
0
    *(indegree_slab_at(&info->indegree, c)) = 0;
3890
3891
0
  return c;
3892
0
}
3893
3894
static void expand_topo_walk(struct rev_info *revs, struct commit *commit)
3895
0
{
3896
0
  struct commit_list *p;
3897
0
  struct topo_walk_info *info = revs->topo_walk_info;
3898
0
  if (process_parents(revs, commit, NULL, NULL) < 0) {
3899
0
    if (!revs->ignore_missing_links)
3900
0
      die("Failed to traverse parents of commit %s",
3901
0
          oid_to_hex(&commit->object.oid));
3902
0
  }
3903
3904
0
  count_topo_walked++;
3905
3906
0
  for (p = commit->parents; p; p = p->next) {
3907
0
    struct commit *parent = p->item;
3908
0
    int *pi;
3909
0
    timestamp_t generation;
3910
3911
0
    if (parent->object.flags & UNINTERESTING)
3912
0
      continue;
3913
3914
0
    if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3915
0
      continue;
3916
3917
0
    generation = commit_graph_generation(parent);
3918
0
    if (generation < info->min_generation) {
3919
0
      info->min_generation = generation;
3920
0
      compute_indegrees_to_depth(revs, info->min_generation);
3921
0
    }
3922
3923
0
    pi = indegree_slab_at(&info->indegree, parent);
3924
3925
0
    (*pi)--;
3926
0
    if (*pi == 1)
3927
0
      prio_queue_put(&info->topo_queue, parent);
3928
3929
0
    if (revs->first_parent_only)
3930
0
      return;
3931
0
  }
3932
0
}
3933
3934
int prepare_revision_walk(struct rev_info *revs)
3935
0
{
3936
0
  int i;
3937
0
  struct object_array old_pending;
3938
0
  struct commit_list **next = &revs->commits;
3939
3940
0
  memcpy(&old_pending, &revs->pending, sizeof(old_pending));
3941
0
  revs->pending.nr = 0;
3942
0
  revs->pending.alloc = 0;
3943
0
  revs->pending.objects = NULL;
3944
0
  for (i = 0; i < old_pending.nr; i++) {
3945
0
    struct object_array_entry *e = old_pending.objects + i;
3946
0
    struct commit *commit = handle_commit(revs, e);
3947
0
    if (commit) {
3948
0
      if (!(commit->object.flags & SEEN)) {
3949
0
        commit->object.flags |= SEEN;
3950
0
        next = commit_list_append(commit, next);
3951
0
      }
3952
0
    }
3953
0
  }
3954
0
  object_array_clear(&old_pending);
3955
3956
  /* Signal whether we need per-parent treesame decoration */
3957
0
  if (revs->simplify_merges ||
3958
0
      (revs->limited && limiting_can_increase_treesame(revs)))
3959
0
    revs->treesame.name = "treesame";
3960
3961
0
  if (revs->exclude_promisor_objects)
3962
0
    odb_for_each_object(revs->repo->objects, NULL, mark_uninteresting,
3963
0
            revs, ODB_FOR_EACH_OBJECT_PROMISOR_ONLY);
3964
3965
0
  if (!revs->reflog_info)
3966
0
    prepare_to_use_bloom_filter(revs);
3967
0
  if (!revs->unsorted_input)
3968
0
    commit_list_sort_by_date(&revs->commits);
3969
0
  if (revs->no_walk)
3970
0
    return 0;
3971
0
  if (revs->limited) {
3972
0
    if (limit_list(revs) < 0)
3973
0
      return -1;
3974
0
    if (revs->topo_order)
3975
0
      sort_in_topological_order(&revs->commits, revs->sort_order);
3976
0
  } else if (revs->topo_order)
3977
0
    init_topo_walk(revs);
3978
0
  if (revs->line_level_traverse && want_ancestry(revs))
3979
    /*
3980
     * At the moment we can only do line-level log with parent
3981
     * rewriting by performing this expensive pre-filtering step.
3982
     * If parent rewriting is not requested, then we rather
3983
     * perform the line-level log filtering during the regular
3984
     * history traversal.
3985
     */
3986
0
    line_log_filter(revs);
3987
0
  if (revs->simplify_merges)
3988
0
    simplify_merges(revs);
3989
0
  if (revs->children.name)
3990
0
    set_children(revs);
3991
3992
0
  return 0;
3993
0
}
3994
3995
static enum rewrite_result rewrite_one_1(struct rev_info *revs,
3996
           struct commit **pp,
3997
           struct prio_queue *queue)
3998
0
{
3999
0
  for (;;) {
4000
0
    struct commit *p = *pp;
4001
0
    if (!revs->limited)
4002
0
      if (process_parents(revs, p, NULL, queue) < 0)
4003
0
        return rewrite_one_error;
4004
0
    if (p->object.flags & UNINTERESTING)
4005
0
      return rewrite_one_ok;
4006
0
    if (!(p->object.flags & TREESAME))
4007
0
      return rewrite_one_ok;
4008
0
    if (!p->parents)
4009
0
      return rewrite_one_noparents;
4010
0
    if (!(p = one_relevant_parent(revs, p->parents)))
4011
0
      return rewrite_one_ok;
4012
0
    *pp = p;
4013
0
  }
4014
0
}
4015
4016
static void merge_queue_into_list(struct prio_queue *q, struct commit_list **list)
4017
0
{
4018
0
  while (q->nr) {
4019
0
    struct commit *item = prio_queue_peek(q);
4020
0
    struct commit_list *p = *list;
4021
4022
0
    if (p && p->item->date >= item->date)
4023
0
      list = &p->next;
4024
0
    else {
4025
0
      p = commit_list_insert(item, list);
4026
0
      list = &p->next; /* skip newly added item */
4027
0
      prio_queue_get(q); /* pop item */
4028
0
    }
4029
0
  }
4030
0
}
4031
4032
static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
4033
0
{
4034
0
  struct prio_queue queue = { compare_commits_by_commit_date };
4035
0
  enum rewrite_result ret = rewrite_one_1(revs, pp, &queue);
4036
0
  merge_queue_into_list(&queue, &revs->commits);
4037
0
  clear_prio_queue(&queue);
4038
0
  return ret;
4039
0
}
4040
4041
int rewrite_parents(struct rev_info *revs, struct commit *commit,
4042
  rewrite_parent_fn_t rewrite_parent)
4043
0
{
4044
0
  struct commit_list **pp = &commit->parents;
4045
0
  while (*pp) {
4046
0
    struct commit_list *parent = *pp;
4047
0
    switch (rewrite_parent(revs, &parent->item)) {
4048
0
    case rewrite_one_ok:
4049
0
      break;
4050
0
    case rewrite_one_noparents:
4051
0
      *pp = parent->next;
4052
0
      free(parent);
4053
0
      continue;
4054
0
    case rewrite_one_error:
4055
0
      return -1;
4056
0
    }
4057
0
    pp = &parent->next;
4058
0
  }
4059
0
  remove_duplicate_parents(revs, commit);
4060
0
  return 0;
4061
0
}
4062
4063
static int commit_match(struct commit *commit, struct rev_info *opt)
4064
0
{
4065
0
  int retval;
4066
0
  const char *encoding;
4067
0
  const char *message;
4068
0
  struct strbuf buf = STRBUF_INIT;
4069
4070
0
  if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
4071
0
    return 1;
4072
4073
  /* Prepend "fake" headers as needed */
4074
0
  if (opt->grep_filter.use_reflog_filter) {
4075
0
    strbuf_addstr(&buf, "reflog ");
4076
0
    get_reflog_message(&buf, opt->reflog_info);
4077
0
    strbuf_addch(&buf, '\n');
4078
0
  }
4079
4080
  /*
4081
   * We grep in the user's output encoding, under the assumption that it
4082
   * is the encoding they are most likely to write their grep pattern
4083
   * for. In addition, it means we will match the "notes" encoding below,
4084
   * so we will not end up with a buffer that has two different encodings
4085
   * in it.
4086
   */
4087
0
  encoding = get_log_output_encoding();
4088
0
  message = repo_logmsg_reencode(the_repository, commit, NULL, encoding);
4089
4090
  /* Copy the commit to temporary if we are using "fake" headers */
4091
0
  if (buf.len)
4092
0
    strbuf_addstr(&buf, message);
4093
4094
0
  if (opt->grep_filter.header_list && opt->mailmap) {
4095
0
    const char *commit_headers[] = { "author ", "committer ", NULL };
4096
4097
0
    if (!buf.len)
4098
0
      strbuf_addstr(&buf, message);
4099
4100
0
    apply_mailmap_to_header(&buf, commit_headers, opt->mailmap);
4101
0
  }
4102
4103
  /* Append "fake" message parts as needed */
4104
0
  if (opt->show_notes) {
4105
0
    if (!buf.len)
4106
0
      strbuf_addstr(&buf, message);
4107
0
    format_display_notes(&commit->object.oid, &buf, encoding, 1);
4108
0
  }
4109
4110
  /*
4111
   * Find either in the original commit message, or in the temporary.
4112
   * Note that we cast away the constness of "message" here. It is
4113
   * const because it may come from the cached commit buffer. That's OK,
4114
   * because we know that it is modifiable heap memory, and that while
4115
   * grep_buffer may modify it for speed, it will restore any
4116
   * changes before returning.
4117
   */
4118
0
  if (buf.len)
4119
0
    retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
4120
0
  else
4121
0
    retval = grep_buffer(&opt->grep_filter,
4122
0
             (char *)message, strlen(message));
4123
0
  strbuf_release(&buf);
4124
0
  repo_unuse_commit_buffer(the_repository, commit, message);
4125
0
  return retval;
4126
0
}
4127
4128
static inline int want_ancestry(const struct rev_info *revs)
4129
0
{
4130
0
  return (revs->rewrite_parents || revs->children.name);
4131
0
}
4132
4133
/*
4134
 * Return a timestamp to be used for --since/--until comparisons for this
4135
 * commit, based on the revision options.
4136
 */
4137
static timestamp_t comparison_date(const struct rev_info *revs,
4138
           struct commit *commit)
4139
0
{
4140
0
  return revs->reflog_info ?
4141
0
    get_reflog_timestamp(revs->reflog_info) :
4142
0
    commit->date;
4143
0
}
4144
4145
enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
4146
0
{
4147
0
  if (commit->object.flags & SHOWN)
4148
0
    return commit_ignore;
4149
0
  if (revs->maximal_only && (commit->object.flags & CHILD_VISITED))
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 = commit_list_copy(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
    commit_list_free(*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
  commit_list_free(revs->previous_parents);
4320
0
  revs->previous_parents = commit_list_copy(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
  commit_list_free(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
    commit_list_free(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
    commit_list_free(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
}