Skip to content

Commit 3da4b60

Browse files
derrickstoleegitster
authored andcommitted
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]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c2bc6e6 commit 3da4b60

File tree

5 files changed

+101
-14
lines changed

5 files changed

+101
-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
@@ -428,7 +428,7 @@ static struct commit_graph *load_commit_graph_chain(struct repository *r, const
428428
return graph_chain;
429429
}
430430

431-
static struct commit_graph *read_commit_graph_one(struct repository *r, const char *obj_dir)
431+
struct commit_graph *read_commit_graph_one(struct repository *r, const char *obj_dir)
432432
{
433433
struct commit_graph *g = load_commit_graph_v1(r, obj_dir);
434434

@@ -1887,14 +1887,15 @@ static void graph_report(const char *fmt, ...)
18871887
#define GENERATION_ZERO_EXISTS 1
18881888
#define GENERATION_NUMBER_EXISTS 2
18891889

1890-
int verify_commit_graph(struct repository *r, struct commit_graph *g)
1890+
int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
18911891
{
18921892
uint32_t i, cur_fanout_pos = 0;
18931893
struct object_id prev_oid, cur_oid, checksum;
18941894
int generation_zero = 0;
18951895
struct hashfile *f;
18961896
int devnull;
18971897
struct progress *progress = NULL;
1898+
int local_error = 0;
18981899

18991900
if (!g) {
19001901
graph_report("no commit-graph file loaded");
@@ -1989,6 +1990,9 @@ int verify_commit_graph(struct repository *r, struct commit_graph *g)
19891990
break;
19901991
}
19911992

1993+
/* parse parent in case it is in a base graph */
1994+
parse_commit_in_graph_one(r, g, graph_parents->item);
1995+
19921996
if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
19931997
graph_report(_("commit-graph parent for %s is %s != %s"),
19941998
oid_to_hex(&cur_oid),
@@ -2040,7 +2044,12 @@ int verify_commit_graph(struct repository *r, struct commit_graph *g)
20402044
}
20412045
stop_progress(&progress);
20422046

2043-
return verify_commit_graph_error;
2047+
local_error = verify_commit_graph_error;
2048+
2049+
if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW) && g->base_graph)
2050+
local_error |= verify_commit_graph(r, g->base_graph, flags);
2051+
2052+
return local_error;
20442053
}
20452054

20462055
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

@@ -95,7 +95,9 @@ int write_commit_graph(const char *obj_dir,
9595
unsigned int flags,
9696
const struct split_commit_graph_opts *split_opts);
9797

98-
int verify_commit_graph(struct repository *r, struct commit_graph *g);
98+
#define COMMIT_GRAPH_VERIFY_SHALLOW (1 << 0)
99+
100+
int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags);
99101

100102
void close_commit_graph(struct raw_object_store *);
101103
void free_commit_graph(struct commit_graph *);

t/t5324-split-commit-graph.sh

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,4 +216,66 @@ test_expect_success 'test merge stragety constants' '
216216
)
217217
'
218218

219+
corrupt_file() {
220+
file=$1
221+
pos=$2
222+
data="${3:-\0}"
223+
printf "$data" | dd of="$file" bs=1 seek="$pos" conv=notrunc
224+
}
225+
226+
test_expect_success 'verify hashes along chain, even in shallow' '
227+
git clone --no-hardlinks . verify &&
228+
(
229+
cd verify &&
230+
git commit-graph verify &&
231+
base_file=$graphdir/graph-$(head -n 1 $graphdir/commit-graph-chain).graph &&
232+
corrupt_file "$base_file" 1760 "\01" &&
233+
test_must_fail git commit-graph verify --shallow 2>test_err &&
234+
grep -v "^+" test_err >err &&
235+
test_i18ngrep "incorrect checksum" err
236+
)
237+
'
238+
239+
test_expect_success 'verify --shallow does not check base contents' '
240+
git clone --no-hardlinks . verify-shallow &&
241+
(
242+
cd verify-shallow &&
243+
git commit-graph verify &&
244+
base_file=$graphdir/graph-$(head -n 1 $graphdir/commit-graph-chain).graph &&
245+
corrupt_file "$base_file" 1000 "\01" &&
246+
git commit-graph verify --shallow &&
247+
test_must_fail git commit-graph verify 2>test_err &&
248+
grep -v "^+" test_err >err &&
249+
test_i18ngrep "incorrect checksum" err
250+
)
251+
'
252+
253+
test_expect_success 'warn on base graph chunk incorrect' '
254+
git clone --no-hardlinks . base-chunk &&
255+
(
256+
cd base-chunk &&
257+
git commit-graph verify &&
258+
base_file=$graphdir/graph-$(tail -n 1 $graphdir/commit-graph-chain).graph &&
259+
corrupt_file "$base_file" 1376 "\01" &&
260+
git commit-graph verify --shallow 2>test_err &&
261+
grep -v "^+" test_err >err &&
262+
test_i18ngrep "commit-graph chain does not match" err
263+
)
264+
'
265+
266+
test_expect_success 'verify after commit-graph-chain corruption' '
267+
git clone --no-hardlinks . verify-chain &&
268+
(
269+
cd verify-chain &&
270+
corrupt_file "$graphdir/commit-graph-chain" 60 "G" &&
271+
git commit-graph verify 2>test_err &&
272+
grep -v "^+" test_err >err &&
273+
test_i18ngrep "invalid commit-graph chain" err &&
274+
corrupt_file "$graphdir/commit-graph-chain" 60 "A" &&
275+
git commit-graph verify 2>test_err &&
276+
grep -v "^+" test_err >err &&
277+
test_i18ngrep "unable to find all commit-graph files" err
278+
)
279+
'
280+
219281
test_done

0 commit comments

Comments
 (0)