Skip to content

Commit 3afc679

Browse files
derrickstoleegitster
authored andcommitted
commit: use generations in paint_down_to_common()
Define compare_commits_by_gen_then_commit_date(), which uses generation numbers as a primary comparison and commit date to break ties (or as a comparison when both commits do not have computed generation numbers). Since the commit-graph file is closed under reachability, we know that all commits in the file have generation at most GENERATION_NUMBER_MAX which is less than GENERATION_NUMBER_INFINITY. This change does not affect the number of commits that are walked during the execution of paint_down_to_common(), only the order that those commits are inspected. In the case that commit dates violate topological order (i.e. a parent is "newer" than a child), the previous code could walk a commit twice: if a commit is reached with the PARENT1 bit, but later is re-visited with the PARENT2 bit, then that PARENT2 bit must be propagated to its parents. Using generation numbers avoids this extra effort, even if it is somewhat rare. Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3258c66 commit 3afc679

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

commit.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,24 @@ static int compare_commits_by_author_date(const void *a_, const void *b_,
640640
return 0;
641641
}
642642

643+
int compare_commits_by_gen_then_commit_date(const void *a_, const void *b_, void *unused)
644+
{
645+
const struct commit *a = a_, *b = b_;
646+
647+
/* newer commits first */
648+
if (a->generation < b->generation)
649+
return 1;
650+
else if (a->generation > b->generation)
651+
return -1;
652+
653+
/* use date as a heuristic when generations are equal */
654+
if (a->date < b->date)
655+
return 1;
656+
else if (a->date > b->date)
657+
return -1;
658+
return 0;
659+
}
660+
643661
int compare_commits_by_commit_date(const void *a_, const void *b_, void *unused)
644662
{
645663
const struct commit *a = a_, *b = b_;
@@ -789,7 +807,7 @@ static int queue_has_nonstale(struct prio_queue *queue)
789807
/* all input commits in one and twos[] must have been parsed! */
790808
static struct commit_list *paint_down_to_common(struct commit *one, int n, struct commit **twos)
791809
{
792-
struct prio_queue queue = { compare_commits_by_commit_date };
810+
struct prio_queue queue = { compare_commits_by_gen_then_commit_date };
793811
struct commit_list *result = NULL;
794812
int i;
795813

commit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ extern int remove_signature(struct strbuf *buf);
341341
extern int check_commit_signature(const struct commit *commit, struct signature_check *sigc);
342342

343343
int compare_commits_by_commit_date(const void *a_, const void *b_, void *unused);
344+
int compare_commits_by_gen_then_commit_date(const void *a_, const void *b_, void *unused);
344345

345346
LAST_ARG_MUST_BE_NULL
346347
extern int run_commit_hook(int editor_is_used, const char *index_file, const char *name, ...);

0 commit comments

Comments
 (0)