Skip to content

Commit 9084424

Browse files
committed
revision.c: begin refactoring --topo-order logic
When running 'git rev-list --topo-order' and its kin, the topo_order setting in struct rev_info implies the limited setting. This means that the following things happen during prepare_revision_walk(): * revs->limited implies we run limit_list() to walk the entire reachable set. There are some short-cuts here, such as if we perform a range query like 'git rev-list COMPARE..HEAD' and we can stop limit_list() when all queued commits are uninteresting. * revs->topo_order implies we run sort_in_topological_order(). See the implementation of that method in commit.c. It implies that the full set of commits to order is in the given commit_list. These two methods imply that a 'git rev-list --topo-order HEAD' command must walk the entire reachable set of commits _twice_ before returning a single result. If we have a commit-graph file with generation numbers computed, then there is a better way. This patch introduces some necessary logic redirection when we are in this situation. In v2.18.0, the commit-graph file contains zero-valued bytes in the positions where the generation number is stored in v2.19.0 and later. Thus, we use generation_numbers_enabled() to check if the commit-graph is available and has non-zero generation numbers. When setting revs->limited only because revs->topo_order is true, only do so if generation numbers are not available. There is no reason to use the new logic as it will behave similarly when all generation numbers are INFINITY or ZERO. In prepare_revision_walk(), if we have revs->topo_order but not revs->limited, then we trigger the new logic. It breaks the logic into three pieces, to fit with the existing framework: 1. init_topo_walk() fills a new struct topo_walk_info in the rev_info struct. We use the presence of this struct as a signal to use the new methods during our walk. In this patch, this method simply calls limit_list() and sort_in_topological_order(). In the future, this method will set up a new data structure to perform that logic in-line. 2. next_topo_commit() provides get_revision_1() with the next topo- ordered commit in the list. Currently, this simply pops the commit from revs->commits. 3. expand_topo_walk() provides get_revision_1() with a way to signal walking beyond the latest commit. Currently, this calls add_parents_to_list() exactly like the old logic. While this commit presents method redirection for performing the exact same logic as before, it allows the next commit to focus only on the new logic. Signed-off-by: Derrick Stolee <[email protected]>
1 parent 708b455 commit 9084424

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

revision.c

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "worktree.h"
2626
#include "argv-array.h"
2727
#include "commit-reach.h"
28+
#include "commit-graph.h"
2829

2930
volatile show_early_output_fn_t show_early_output;
3031

@@ -2451,7 +2452,7 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s
24512452
if (revs->diffopt.objfind)
24522453
revs->simplify_history = 0;
24532454

2454-
if (revs->topo_order)
2455+
if (revs->topo_order && !generation_numbers_enabled(the_repository))
24552456
revs->limited = 1;
24562457

24572458
if (revs->prune_data.nr) {
@@ -2889,6 +2890,33 @@ static int mark_uninteresting(const struct object_id *oid,
28892890
return 0;
28902891
}
28912892

2893+
struct topo_walk_info {};
2894+
2895+
static void init_topo_walk(struct rev_info *revs)
2896+
{
2897+
struct topo_walk_info *info;
2898+
revs->topo_walk_info = xmalloc(sizeof(struct topo_walk_info));
2899+
info = revs->topo_walk_info;
2900+
memset(info, 0, sizeof(struct topo_walk_info));
2901+
2902+
limit_list(revs);
2903+
sort_in_topological_order(&revs->commits, revs->sort_order);
2904+
}
2905+
2906+
static struct commit *next_topo_commit(struct rev_info *revs)
2907+
{
2908+
return pop_commit(&revs->commits);
2909+
}
2910+
2911+
static void expand_topo_walk(struct rev_info *revs, struct commit *commit)
2912+
{
2913+
if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0) {
2914+
if (!revs->ignore_missing_links)
2915+
die("Failed to traverse parents of commit %s",
2916+
oid_to_hex(&commit->object.oid));
2917+
}
2918+
}
2919+
28922920
int prepare_revision_walk(struct rev_info *revs)
28932921
{
28942922
int i;
@@ -2925,11 +2953,13 @@ int prepare_revision_walk(struct rev_info *revs)
29252953
commit_list_sort_by_date(&revs->commits);
29262954
if (revs->no_walk)
29272955
return 0;
2928-
if (revs->limited)
2956+
if (revs->limited) {
29292957
if (limit_list(revs) < 0)
29302958
return -1;
2931-
if (revs->topo_order)
2932-
sort_in_topological_order(&revs->commits, revs->sort_order);
2959+
if (revs->topo_order)
2960+
sort_in_topological_order(&revs->commits, revs->sort_order);
2961+
} else if (revs->topo_order)
2962+
init_topo_walk(revs);
29332963
if (revs->line_level_traverse)
29342964
line_log_filter(revs);
29352965
if (revs->simplify_merges)
@@ -3254,6 +3284,8 @@ static struct commit *get_revision_1(struct rev_info *revs)
32543284

32553285
if (revs->reflog_info)
32563286
commit = next_reflog_entry(revs->reflog_info);
3287+
else if (revs->topo_walk_info)
3288+
commit = next_topo_commit(revs);
32573289
else
32583290
commit = pop_commit(&revs->commits);
32593291

@@ -3275,6 +3307,8 @@ static struct commit *get_revision_1(struct rev_info *revs)
32753307

32763308
if (revs->reflog_info)
32773309
try_to_simplify_commit(revs, commit);
3310+
else if (revs->topo_walk_info)
3311+
expand_topo_walk(revs, commit);
32783312
else if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0) {
32793313
if (!revs->ignore_missing_links)
32803314
die("Failed to traverse parents of commit %s",

revision.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ struct rev_cmdline_info {
5454
#define REVISION_WALK_NO_WALK_SORTED 1
5555
#define REVISION_WALK_NO_WALK_UNSORTED 2
5656

57+
struct topo_walk_info;
58+
5759
struct rev_info {
5860
/* Starting list */
5961
struct commit_list *commits;
@@ -227,6 +229,8 @@ struct rev_info {
227229
const char *break_bar;
228230

229231
struct revision_sources *sources;
232+
233+
struct topo_walk_info *topo_walk_info;
230234
};
231235

232236
extern int ref_excluded(struct string_list *, const char *path);

0 commit comments

Comments
 (0)