Skip to content

Commit 59fb877

Browse files
derrickstoleegitster
authored andcommitted
commit-graph: add '--reachable' option
When writing commit-graph files, it can be convenient to ask for all reachable commits (starting at the ref set) in the resulting file. This is particularly helpful when writing to stdin is complicated, such as a future integration with 'git gc'. Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d88b14b commit 59fb877

File tree

5 files changed

+49
-6
lines changed

5 files changed

+49
-6
lines changed

Documentation/git-commit-graph.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,16 @@ Write a commit graph file based on the commits found in packfiles.
3838
+
3939
With the `--stdin-packs` option, generate the new commit graph by
4040
walking objects only in the specified pack-indexes. (Cannot be combined
41-
with --stdin-commits.)
41+
with `--stdin-commits` or `--reachable`.)
4242
+
4343
With the `--stdin-commits` option, generate the new commit graph by
4444
walking commits starting at the commits specified in stdin as a list
4545
of OIDs in hex, one OID per line. (Cannot be combined with
46-
--stdin-packs.)
46+
`--stdin-packs` or `--reachable`.)
47+
+
48+
With the `--reachable` option, generate the new commit graph by walking
49+
commits starting at all refs. (Cannot be combined with `--stdin-commits`
50+
or `--stdin-packs`.)
4751
+
4852
With the `--append` option, include all commits that are present in the
4953
existing commit-graph file.

builtin/commit-graph.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ static char const * const builtin_commit_graph_usage[] = {
1010
N_("git commit-graph [--object-dir <objdir>]"),
1111
N_("git commit-graph read [--object-dir <objdir>]"),
1212
N_("git commit-graph verify [--object-dir <objdir>]"),
13-
N_("git commit-graph write [--object-dir <objdir>] [--append] [--stdin-packs|--stdin-commits]"),
13+
N_("git commit-graph write [--object-dir <objdir>] [--append] [--reachable|--stdin-packs|--stdin-commits]"),
1414
NULL
1515
};
1616

@@ -25,12 +25,13 @@ static const char * const builtin_commit_graph_read_usage[] = {
2525
};
2626

2727
static const char * const builtin_commit_graph_write_usage[] = {
28-
N_("git commit-graph write [--object-dir <objdir>] [--append] [--stdin-packs|--stdin-commits]"),
28+
N_("git commit-graph write [--object-dir <objdir>] [--append] [--reachable|--stdin-packs|--stdin-commits]"),
2929
NULL
3030
};
3131

3232
static struct opts_commit_graph {
3333
const char *obj_dir;
34+
int reachable;
3435
int stdin_packs;
3536
int stdin_commits;
3637
int append;
@@ -127,6 +128,8 @@ static int graph_write(int argc, const char **argv)
127128
OPT_STRING(0, "object-dir", &opts.obj_dir,
128129
N_("dir"),
129130
N_("The object directory to store the graph")),
131+
OPT_BOOL(0, "reachable", &opts.reachable,
132+
N_("start walk at all refs")),
130133
OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
131134
N_("scan pack-indexes listed by stdin for commits")),
132135
OPT_BOOL(0, "stdin-commits", &opts.stdin_commits,
@@ -140,11 +143,16 @@ static int graph_write(int argc, const char **argv)
140143
builtin_commit_graph_write_options,
141144
builtin_commit_graph_write_usage, 0);
142145

143-
if (opts.stdin_packs && opts.stdin_commits)
144-
die(_("cannot use both --stdin-commits and --stdin-packs"));
146+
if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1)
147+
die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
145148
if (!opts.obj_dir)
146149
opts.obj_dir = get_object_directory();
147150

151+
if (opts.reachable) {
152+
write_commit_graph_reachable(opts.obj_dir, opts.append);
153+
return 0;
154+
}
155+
148156
string_list_init(&lines, 0);
149157
if (opts.stdin_packs || opts.stdin_commits) {
150158
struct strbuf buf = STRBUF_INIT;

commit-graph.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "packfile.h"
88
#include "commit.h"
99
#include "object.h"
10+
#include "refs.h"
1011
#include "revision.h"
1112
#include "sha1-lookup.h"
1213
#include "commit-graph.h"
@@ -656,6 +657,25 @@ static void compute_generation_numbers(struct packed_commit_list* commits)
656657
}
657658
}
658659

660+
static int add_ref_to_list(const char *refname,
661+
const struct object_id *oid,
662+
int flags, void *cb_data)
663+
{
664+
struct string_list *list = (struct string_list *)cb_data;
665+
666+
string_list_append(list, oid_to_hex(oid));
667+
return 0;
668+
}
669+
670+
void write_commit_graph_reachable(const char *obj_dir, int append)
671+
{
672+
struct string_list list;
673+
674+
string_list_init(&list, 1);
675+
for_each_ref(add_ref_to_list, &list);
676+
write_commit_graph(obj_dir, NULL, &list, append);
677+
}
678+
659679
void write_commit_graph(const char *obj_dir,
660680
struct string_list *pack_indexes,
661681
struct string_list *commit_hex,

commit-graph.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ struct commit_graph {
4848

4949
struct commit_graph *load_commit_graph_one(const char *graph_file);
5050

51+
void write_commit_graph_reachable(const char *obj_dir, int append);
5152
void write_commit_graph(const char *obj_dir,
5253
struct string_list *pack_indexes,
5354
struct string_list *commit_hex,

t/t5318-commit-graph.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,16 @@ test_expect_success 'build graph from commits with append' '
205205
graph_git_behavior 'append graph, commit 8 vs merge 1' full commits/8 merge/1
206206
graph_git_behavior 'append graph, commit 8 vs merge 2' full commits/8 merge/2
207207

208+
test_expect_success 'build graph using --reachable' '
209+
cd "$TRASH_DIRECTORY/full" &&
210+
git commit-graph write --reachable &&
211+
test_path_is_file $objdir/info/commit-graph &&
212+
graph_read_expect "11" "large_edges"
213+
'
214+
215+
graph_git_behavior 'append graph, commit 8 vs merge 1' full commits/8 merge/1
216+
graph_git_behavior 'append graph, commit 8 vs merge 2' full commits/8 merge/2
217+
208218
test_expect_success 'setup bare repo' '
209219
cd "$TRASH_DIRECTORY" &&
210220
git clone --bare --no-local full bare &&

0 commit comments

Comments
 (0)