Skip to content

Commit fe91ff5

Browse files
committed
commit-graph: verify chains with --shallow mode
If we wrote a commit-graph chain, we only modified the tip file in the chain. It is valuable to verify what we wrote, but not waste time checking files we did not write. Add a '--shallow' option to the 'git commit-graph verify' subcommand and check that it does not read the base graph in a two-file chain. Making the verify subcommand read from a chain of commit-graphs takes some rearranging of the builtin code. Signed-off-by: Derrick Stolee <[email protected]>
1 parent 4de4bfb commit fe91ff5

File tree

5 files changed

+60
-14
lines changed

5 files changed

+60
-14
lines changed

Documentation/git-commit-graph.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ SYNOPSIS
1010
--------
1111
[verse]
1212
'git commit-graph read' [--object-dir <dir>]
13-
'git commit-graph verify' [--object-dir <dir>]
13+
'git commit-graph verify' [--object-dir <dir>] [--shallow]
1414
'git commit-graph write' <options> [--object-dir <dir>]
1515

1616

@@ -80,6 +80,9 @@ Used for debugging purposes.
8080

8181
Read the commit-graph file and verify its contents against the object
8282
database. Used to check for corrupted data.
83+
+
84+
With the `--shallow` option, only check the tip commit-graph file in
85+
a chain of split commit-graphs.
8386

8487

8588
EXAMPLES

builtin/commit-graph.c

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@
55
#include "parse-options.h"
66
#include "repository.h"
77
#include "commit-graph.h"
8+
#include "object-store.h"
89

910
static char const * const builtin_commit_graph_usage[] = {
1011
N_("git commit-graph [--object-dir <objdir>]"),
1112
N_("git commit-graph read [--object-dir <objdir>]"),
12-
N_("git commit-graph verify [--object-dir <objdir>]"),
13+
N_("git commit-graph verify [--object-dir <objdir>] [--shallow]"),
1314
N_("git commit-graph write [--object-dir <objdir>] [--append|--split] [--reachable|--stdin-packs|--stdin-commits] <split options>"),
1415
NULL
1516
};
1617

1718
static const char * const builtin_commit_graph_verify_usage[] = {
18-
N_("git commit-graph verify [--object-dir <objdir>]"),
19+
N_("git commit-graph verify [--object-dir <objdir>] [--shallow]"),
1920
NULL
2021
};
2122

@@ -36,6 +37,7 @@ static struct opts_commit_graph {
3637
int stdin_commits;
3738
int append;
3839
int split;
40+
int shallow;
3941
} opts;
4042

4143
static int graph_verify(int argc, const char **argv)
@@ -45,11 +47,14 @@ static int graph_verify(int argc, const char **argv)
4547
int open_ok;
4648
int fd;
4749
struct stat st;
50+
int flags = 0;
4851

4952
static struct option builtin_commit_graph_verify_options[] = {
5053
OPT_STRING(0, "object-dir", &opts.obj_dir,
5154
N_("dir"),
5255
N_("The object directory to store the graph")),
56+
OPT_BOOL(0, "shallow", &opts.shallow,
57+
N_("if the commit-graph is split, only verify the tip file")),
5358
OPT_END(),
5459
};
5560

@@ -59,21 +64,27 @@ static int graph_verify(int argc, const char **argv)
5964

6065
if (!opts.obj_dir)
6166
opts.obj_dir = get_object_directory();
67+
if (opts.shallow)
68+
flags |= COMMIT_GRAPH_VERIFY_SHALLOW;
6269

6370
graph_name = get_commit_graph_filename(opts.obj_dir);
6471
open_ok = open_commit_graph(graph_name, &fd, &st);
65-
if (!open_ok && errno == ENOENT)
66-
return 0;
67-
if (!open_ok)
72+
if (!open_ok && errno != ENOENT)
6873
die_errno(_("Could not open commit-graph '%s'"), graph_name);
69-
graph = load_commit_graph_one_fd_st(fd, &st);
74+
7075
FREE_AND_NULL(graph_name);
7176

77+
if (open_ok)
78+
graph = load_commit_graph_one_fd_st(fd, &st);
79+
else
80+
graph = read_commit_graph_one(the_repository, opts.obj_dir);
81+
82+
/* Return failure if open_ok predicted success */
7283
if (!graph)
73-
return 1;
84+
return !!open_ok;
7485

7586
UNLEAK(graph);
76-
return verify_commit_graph(the_repository, graph);
87+
return verify_commit_graph(the_repository, graph, flags);
7788
}
7889

7990
static int graph_read(int argc, const char **argv)

commit-graph.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ static struct commit_graph *load_commit_graph_chain(struct repository *r, const
422422
return graph_chain;
423423
}
424424

425-
static struct commit_graph *read_commit_graph_one(struct repository *r, const char *obj_dir)
425+
struct commit_graph *read_commit_graph_one(struct repository *r, const char *obj_dir)
426426
{
427427
struct commit_graph *g = load_commit_graph_v1(r, obj_dir);
428428

@@ -1872,14 +1872,15 @@ static void graph_report(const char *fmt, ...)
18721872
#define GENERATION_ZERO_EXISTS 1
18731873
#define GENERATION_NUMBER_EXISTS 2
18741874

1875-
int verify_commit_graph(struct repository *r, struct commit_graph *g)
1875+
int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
18761876
{
18771877
uint32_t i, cur_fanout_pos = 0;
18781878
struct object_id prev_oid, cur_oid, checksum;
18791879
int generation_zero = 0;
18801880
struct hashfile *f;
18811881
int devnull;
18821882
struct progress *progress = NULL;
1883+
int local_error = 0;
18831884

18841885
if (!g) {
18851886
graph_report("no commit-graph file loaded");
@@ -1974,6 +1975,9 @@ int verify_commit_graph(struct repository *r, struct commit_graph *g)
19741975
break;
19751976
}
19761977

1978+
/* parse parent in case it is in a base graph */
1979+
parse_commit_in_graph_one(r, g, graph_parents->item);
1980+
19771981
if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
19781982
graph_report(_("commit-graph parent for %s is %s != %s"),
19791983
oid_to_hex(&cur_oid),
@@ -2025,7 +2029,12 @@ int verify_commit_graph(struct repository *r, struct commit_graph *g)
20252029
}
20262030
stop_progress(&progress);
20272031

2028-
return verify_commit_graph_error;
2032+
local_error = verify_commit_graph_error;
2033+
2034+
if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW) && g->base_graph)
2035+
local_error |= verify_commit_graph(r, g->base_graph, flags);
2036+
2037+
return local_error;
20292038
}
20302039

20312040
void free_commit_graph(struct commit_graph *g)

commit-graph.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ struct commit_graph {
6161
};
6262

6363
struct commit_graph *load_commit_graph_one_fd_st(int fd, struct stat *st);
64-
64+
struct commit_graph *read_commit_graph_one(struct repository *r, const char *obj_dir);
6565
struct commit_graph *parse_commit_graph(void *graph_map, int fd,
6666
size_t graph_size);
6767

@@ -89,7 +89,9 @@ int write_commit_graph(const char *obj_dir,
8989
unsigned int flags,
9090
const struct split_commit_graph_opts *split_opts);
9191

92-
int verify_commit_graph(struct repository *r, struct commit_graph *g);
92+
#define COMMIT_GRAPH_VERIFY_SHALLOW (1 << 0)
93+
94+
int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags);
9395

9496
void close_commit_graph(struct repository *);
9597
void free_commit_graph(struct commit_graph *);

t/t5323-split-commit-graph.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,25 @@ test_expect_success 'test merge stragety constants' '
204204
)
205205
'
206206

207+
corrupt_file() {
208+
file=$1
209+
pos=$2
210+
data="${3:-\0}"
211+
printf "$data" | dd of="$file" bs=1 seek="$pos" conv=notrunc
212+
}
213+
214+
test_expect_success 'verify shallow' '
215+
git clone . verify &&
216+
(
217+
cd verify &&
218+
git commit-graph verify &&
219+
base_file=$graphdir/graph-$(head -n 1 $graphdir/commit-graph-chain).graph &&
220+
corrupt_file "$base_file" 1760 "\01" &&
221+
git commit-graph verify --shallow &&
222+
test_must_fail git commit-graph verify 2>test_err &&
223+
grep -v "^+" test_err >err &&
224+
test_i18ngrep "incorrect checksum" err
225+
)
226+
'
227+
207228
test_done

0 commit comments

Comments
 (0)