Skip to content

Commit 283e68c

Browse files
derrickstoleegitster
authored andcommitted
commit-graph: add 'verify' subcommand
If the commit-graph file becomes corrupt, we need a way to verify that its contents match the object database. In the manner of 'git fsck' we will implement a 'git commit-graph verify' subcommand to report all issues with the file. Add the 'verify' subcommand to the 'commit-graph' builtin and its documentation. The subcommand is currently a no-op except for loading the commit-graph into memory, which may trigger run-time errors that would be caught by normal use. Add a simple test that ensures the command returns a zero error code. If no commit-graph file exists, this is an acceptable state. Do not report any errors. Helped-by: Ramsay Jones <[email protected]> Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0cbef8f commit 283e68c

File tree

5 files changed

+81
-0
lines changed

5 files changed

+81
-0
lines changed

Documentation/git-commit-graph.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ SYNOPSIS
1010
--------
1111
[verse]
1212
'git commit-graph read' [--object-dir <dir>]
13+
'git commit-graph verify' [--object-dir <dir>]
1314
'git commit-graph write' <options> [--object-dir <dir>]
1415

1516

@@ -52,6 +53,11 @@ existing commit-graph file.
5253
Read a graph file given by the commit-graph file and output basic
5354
details about the graph file. Used for debugging purposes.
5455

56+
'verify'::
57+
58+
Read the commit-graph file and verify its contents against the object
59+
database. Used to check for corrupted data.
60+
5561

5662
EXAMPLES
5763
--------

builtin/commit-graph.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,22 @@
33
#include "dir.h"
44
#include "lockfile.h"
55
#include "parse-options.h"
6+
#include "repository.h"
67
#include "commit-graph.h"
78

89
static char const * const builtin_commit_graph_usage[] = {
910
N_("git commit-graph [--object-dir <objdir>]"),
1011
N_("git commit-graph read [--object-dir <objdir>]"),
12+
N_("git commit-graph verify [--object-dir <objdir>]"),
1113
N_("git commit-graph write [--object-dir <objdir>] [--append] [--stdin-packs|--stdin-commits]"),
1214
NULL
1315
};
1416

17+
static const char * const builtin_commit_graph_verify_usage[] = {
18+
N_("git commit-graph verify [--object-dir <objdir>]"),
19+
NULL
20+
};
21+
1522
static const char * const builtin_commit_graph_read_usage[] = {
1623
N_("git commit-graph read [--object-dir <objdir>]"),
1724
NULL
@@ -29,6 +36,36 @@ static struct opts_commit_graph {
2936
int append;
3037
} opts;
3138

39+
40+
static int graph_verify(int argc, const char **argv)
41+
{
42+
struct commit_graph *graph = NULL;
43+
char *graph_name;
44+
45+
static struct option builtin_commit_graph_verify_options[] = {
46+
OPT_STRING(0, "object-dir", &opts.obj_dir,
47+
N_("dir"),
48+
N_("The object directory to store the graph")),
49+
OPT_END(),
50+
};
51+
52+
argc = parse_options(argc, argv, NULL,
53+
builtin_commit_graph_verify_options,
54+
builtin_commit_graph_verify_usage, 0);
55+
56+
if (!opts.obj_dir)
57+
opts.obj_dir = get_object_directory();
58+
59+
graph_name = get_commit_graph_filename(opts.obj_dir);
60+
graph = load_commit_graph_one(graph_name);
61+
FREE_AND_NULL(graph_name);
62+
63+
if (!graph)
64+
return 0;
65+
66+
return verify_commit_graph(the_repository, graph);
67+
}
68+
3269
static int graph_read(int argc, const char **argv)
3370
{
3471
struct commit_graph *graph = NULL;
@@ -165,6 +202,8 @@ int cmd_commit_graph(int argc, const char **argv, const char *prefix)
165202
if (argc > 0) {
166203
if (!strcmp(argv[0], "read"))
167204
return graph_read(argc, argv);
205+
if (!strcmp(argv[0], "verify"))
206+
return graph_verify(argc, argv);
168207
if (!strcmp(argv[0], "write"))
169208
return graph_write(argc, argv);
170209
}

commit-graph.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,3 +827,26 @@ void write_commit_graph(const char *obj_dir,
827827
oids.alloc = 0;
828828
oids.nr = 0;
829829
}
830+
831+
static int verify_commit_graph_error;
832+
833+
static void graph_report(const char *fmt, ...)
834+
{
835+
va_list ap;
836+
837+
verify_commit_graph_error = 1;
838+
va_start(ap, fmt);
839+
vfprintf(stderr, fmt, ap);
840+
fprintf(stderr, "\n");
841+
va_end(ap);
842+
}
843+
844+
int verify_commit_graph(struct repository *r, struct commit_graph *g)
845+
{
846+
if (!g) {
847+
graph_report("no commit-graph file loaded");
848+
return 1;
849+
}
850+
851+
return verify_commit_graph_error;
852+
}

commit-graph.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define COMMIT_GRAPH_H
33

44
#include "git-compat-util.h"
5+
#include "repository.h"
56

67
char *get_commit_graph_filename(const char *obj_dir);
78

@@ -53,4 +54,6 @@ void write_commit_graph(const char *obj_dir,
5354
int nr_commits,
5455
int append);
5556

57+
int verify_commit_graph(struct repository *r, struct commit_graph *g);
58+
5659
#endif

t/t5318-commit-graph.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ test_expect_success 'setup full repo' '
1111
objdir=".git/objects"
1212
'
1313

14+
test_expect_success 'verify graph with no graph file' '
15+
cd "$TRASH_DIRECTORY/full" &&
16+
git commit-graph verify
17+
'
18+
1419
test_expect_success 'write graph with no packs' '
1520
cd "$TRASH_DIRECTORY/full" &&
1621
git commit-graph write --object-dir . &&
@@ -230,4 +235,9 @@ test_expect_success 'perform fast-forward merge in full repo' '
230235
test_cmp expect output
231236
'
232237

238+
test_expect_success 'git commit-graph verify' '
239+
cd "$TRASH_DIRECTORY/full" &&
240+
git commit-graph verify >output
241+
'
242+
233243
test_done

0 commit comments

Comments
 (0)