54
static int topo_order = 0;
55
static int no_merges = 0;
56
static const char **paths = NULL;
57
+static int remove_empty_trees = 0;
58
59
static void show_commit(struct commit *commit)
60
{
425
}
426
}
427
427
-static int is_different = 0;
428
+#define TREE_SAME 0
429
+#define TREE_NEW 1
430
+#define TREE_DIFFERENT 2
431
+static int tree_difference = TREE_SAME;
432
433
static void file_add_remove(struct diff_options *options,
434
int addremove, unsigned mode,
435
const unsigned char *sha1,
436
const char *base, const char *path)
437
{
434
- is_different = 1;
438
+ int diff = TREE_DIFFERENT;
439
+
440
+ /*
441
+ * Is it an add of a new file? It means that
442
+ * the old tree didn't have it at all, so we
443
+ * will turn "TREE_SAME" -> "TREE_NEW", but
444
+ * leave any "TREE_DIFFERENT" alone (and if
445
+ * it already was "TREE_NEW", we'll keep it
446
+ * "TREE_NEW" of course).
447
+ */
448
+ if (addremove == '+') {
449
+ diff = tree_difference;
450
+ if (diff != TREE_SAME)
451
+ return;
452
+ diff = TREE_NEW;
453
+ }
454
+ tree_difference = diff;
455
}
456
457
static void file_change(struct diff_options *options,
460
const unsigned char *new_sha1,
461
const char *base, const char *path)
462
{
443
- is_different = 1;
463
+ tree_difference = TREE_DIFFERENT;
464
}
465
466
static struct diff_options diff_opt = {
469
.change = file_change,
470
};
471
452
-static int same_tree(struct tree *t1, struct tree *t2)
472
+static int compare_tree(struct tree *t1, struct tree *t2)
473
{
454
- is_different = 0;
474
+ if (!t1)
475
+ return TREE_NEW;
476
+ if (!t2)
477
+ return TREE_DIFFERENT;
478
+ tree_difference = TREE_SAME;
479
if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "", &diff_opt) < 0)
456
- return 0;
457
- return !is_different;
480
+ return TREE_DIFFERENT;
481
+ return tree_difference;
482
}
483
484
static int same_tree_as_empty(struct tree *t1)
498
empty.buf = "";
499
empty.size = 0;
500
477
- is_different = 0;
501
+ tree_difference = 0;
502
retval = diff_tree(&empty, &real, "", &diff_opt);
503
free(tree);
504
481
- return retval >= 0 && !is_different;
505
+ return retval >= 0 && !tree_difference;
506
}
507
484
-static struct commit *try_to_simplify_merge(struct commit *commit, struct commit_list *parent)
508
+static void try_to_simplify_commit(struct commit *commit)
509
{
510
+ struct commit_list **pp, *parent;
511
+
512
if (!commit->tree)
487
- return NULL;
513
+ return;
514
489
- while (parent) {
515
+ if (!commit->parents) {
516
+ if (!same_tree_as_empty(commit->tree))
517
+ commit->object.flags |= TREECHANGE;
518
+ return;
519
+ }
520
+
521
+ pp = &commit->parents;
522
+ while ((parent = *pp) != NULL) {
523
struct commit *p = parent->item;
491
- parent = parent->next;
524
+
525
+ if (p->object.flags & UNINTERESTING) {
526
+ pp = &parent->next;
527
+ continue;
528
+ }
529
+
530
parse_commit(p);
493
- if (!p->tree)
531
+ switch (compare_tree(p->tree, commit->tree)) {
532
+ case TREE_SAME:
533
+ parent->next = NULL;
534
+ commit->parents = parent;
535
+ return;
536
+
537
+ case TREE_NEW:
538
+ if (remove_empty_trees && same_tree_as_empty(p->tree)) {
539
+ *pp = parent->next;
540
+ continue;
541
+ }
542
+ /* fallthrough */
543
+ case TREE_DIFFERENT:
544
+ pp = &parent->next;
545
continue;
495
- if (same_tree(commit->tree, p->tree))
496
- return p;
546
+ }
547
+ die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
548
}
498
- return NULL;
549
+ commit->object.flags |= TREECHANGE;
550
}
551
552
static void add_parents_to_list(struct commit *commit, struct commit_list **list)
582
}
583
584
/*
534
- * Ok, the commit wasn't uninteresting. If it
535
- * is a merge, try to find the parent that has
536
- * no differences in the path set if one exists.
585
+ * Ok, the commit wasn't uninteresting. Try to
586
+ * simplify the commit history and find the parent
587
+ * that has no differences in the path set if one exists.
588
*/
538
- if (paths && parent && parent->next) {
539
- struct commit *preferred;
540
-
541
- preferred = try_to_simplify_merge(commit, parent);
542
- if (preferred) {
543
- parent->item = preferred;
544
- parent->next = NULL;
545
- }
546
- }
589
+ if (paths)
590
+ try_to_simplify_commit(commit);
591
592
+ parent = commit->parents;
593
while (parent) {
594
struct commit *p = parent->item;
595
603
}
604
}
605
561
-static void compress_list(struct commit_list *list)
562
-{
563
- while (list) {
564
- struct commit *commit = list->item;
565
- struct commit_list *parent = commit->parents;
566
- list = list->next;
567
-
568
- if (!parent) {
569
- if (!same_tree_as_empty(commit->tree))
570
- commit->object.flags |= TREECHANGE;
571
- continue;
572
- }
573
-
574
- /*
575
- * Exactly one parent? Check if it leaves the tree
576
- * unchanged
577
- */
578
- if (!parent->next) {
579
- struct tree *t1 = commit->tree;
580
- struct tree *t2 = parent->item->tree;
581
- if (!t1 || !t2 || same_tree(t1, t2))
582
- continue;
583
- }
584
- commit->object.flags |= TREECHANGE;
585
- }
586
-}
587
-
606
static struct commit_list *limit_list(struct commit_list *list)
607
{
608
struct commit_list *newlist = NULL;
632
}
633
if (tree_objects)
634
mark_edges_uninteresting(newlist);
617
- if (paths && dense)
618
- compress_list(newlist);
635
if (bisect_list)
636
newlist = find_bisection(newlist);
637
return newlist;
824
dense = 0;
825
continue;
826
}
827
+ if (!strcmp(arg, "--remove-empty")) {
828
+ remove_empty_trees = 1;
829
+ continue;
830
+ }
831
if (!strcmp(arg, "--")) {
832
i++;
833
break;