commit-graph: don't early exit(1) on e.g. "git status"

Make the commit-graph loading code work as a library that returns an error code instead of calling exit(1) when the commit-graph is corrupt. This means that e.g. "status" will now report commit-graph corruption as an "error: [...]" at the top of its output, but then proceed to work normally. This required splitting up the load_commit_graph_one() function so that the code that deals with open()-ing and stat()-ing the graph can now be called independently as open_commit_graph(). This is needed because "commit-graph verify" where the graph doesn't exist isn't an error. See the third paragraph in 283e68c72f ("commit-graph: add 'verify' subcommand", 2018-06-27). There's a bug in that logic where we conflate the intended ENOENT with other errno values (e.g. EACCES), but this change doesn't address that. That'll be addressed in a follow-up change. I'm then splitting most of the logic out of load_commit_graph_one() into load_commit_graph_one_fd_st(), which allows for providing an existing file descriptor and stat information to the loading code. This isn't strictly needed, but it would be redundant and confusing to open() and stat() the file twice for some of the codepaths, this allows for calling open_commit_graph() followed by load_commit_graph_one_fd_st(). The "graph_file" still needs to be passed to that function for the the "graph file %s is too small" error message. This leaves load_commit_graph_one() unused by everything except the internal prepare_commit_graph_one() function, so let's mark it as "static". If someone needs it in the future we can remove the "static" attribute. I could also rewrite its sole remaining user ("prepare_commit_graph_one()") to use load_commit_graph_one_fd_st() instead, but let's leave it at this. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Ramsay Jones <ramsay@ramsayjones.plus.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Mar 25, 2019 at 13:08 UTC 61df89c8e5559c2b524968f492d445421913bfdb
4 files changed +51 -18
builtin/commit-graph.c
+17 -4
@@ -42,6 +42,9 @@ static int graph_verify(int argc, const char **argv)
42 {
43 struct commit_graph *graph = NULL;
44 char *graph_name;
45 + int open_ok;
46 + int fd;
47 + struct stat st;
48
49 static struct option builtin_commit_graph_verify_options[] = {
50 OPT_STRING(0, "object-dir", &opts.obj_dir,
@@ -58,11 +61,14 @@ static int graph_verify(int argc, const char **argv)
61 opts.obj_dir = get_object_directory();
62
63 graph_name = get_commit_graph_filename(opts.obj_dir);
61 - graph = load_commit_graph_one(graph_name);
64 + open_ok = open_commit_graph(graph_name, &fd, &st);
65 + if (!open_ok)
66 + return 0;
67 + graph = load_commit_graph_one_fd_st(graph_name, fd, &st);
68 FREE_AND_NULL(graph_name);
69
70 if (!graph)
65 - return 0;
71 + return 1;
72
73 UNLEAK(graph);
74 return verify_commit_graph(the_repository, graph);
@@ -72,6 +78,9 @@ static int graph_read(int argc, const char **argv)
78 {
79 struct commit_graph *graph = NULL;
80 char *graph_name;
81 + int open_ok;
82 + int fd;
83 + struct stat st;
84
85 static struct option builtin_commit_graph_read_options[] = {
86 OPT_STRING(0, "object-dir", &opts.obj_dir,
@@ -88,10 +97,14 @@ static int graph_read(int argc, const char **argv)
97 opts.obj_dir = get_object_directory();
98
99 graph_name = get_commit_graph_filename(opts.obj_dir);
91 - graph = load_commit_graph_one(graph_name);
100
101 + open_ok = open_commit_graph(graph_name, &fd, &st);
102 + if (!open_ok)
103 + die_errno(_("Could not open commit-graph '%s'"), graph_name);
104 +
105 + graph = load_commit_graph_one_fd_st(graph_name, fd, &st);
106 if (!graph)
94 - die("graph file %s does not exist", graph_name);
107 + return 1;
108
109 FREE_AND_NULL(graph_name);
110
commit-graph.c
+30 -12
@@ -80,25 +80,31 @@ static int commit_graph_compatible(struct repository *r)
80 return 1;
81 }
82
83 -struct commit_graph *load_commit_graph_one(const char *graph_file)
83 +int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
84 +{
85 + *fd = git_open(graph_file);
86 + if (*fd < 0)
87 + return 0;
88 + if (fstat(*fd, st)) {
89 + close(*fd);
90 + return 0;
91 + }
92 + return 1;
93 +}
94 +
95 +struct commit_graph *load_commit_graph_one_fd_st(const char *graph_file,
96 + int fd, struct stat *st)
97 {
98 void *graph_map;
99 size_t graph_size;
87 - struct stat st;
100 struct commit_graph *ret;
89 - int fd = git_open(graph_file);
101
91 - if (fd < 0)
92 - return NULL;
93 - if (fstat(fd, &st)) {
94 - close(fd);
95 - return NULL;
96 - }
97 - graph_size = xsize_t(st.st_size);
102 + graph_size = xsize_t(st->st_size);
103
104 if (graph_size < GRAPH_MIN_SIZE) {
105 close(fd);
101 - die(_("graph file %s is too small"), graph_file);
106 + error(_("graph file %s is too small"), graph_file);
107 + return NULL;
108 }
109 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
110 ret = parse_commit_graph(graph_map, fd, graph_size);
@@ -106,7 +112,6 @@ struct commit_graph *load_commit_graph_one(const char *graph_file)
112 if (!ret) {
113 munmap(graph_map, graph_size);
114 close(fd);
109 - exit(1);
115 }
116
117 return ret;
@@ -269,6 +274,19 @@ struct commit_graph *parse_commit_graph(void *graph_map, int fd,
274 return graph;
275 }
276
277 +static struct commit_graph *load_commit_graph_one(const char *graph_file)
278 +{
279 +
280 + struct stat st;
281 + int fd;
282 + int open_ok = open_commit_graph(graph_file, &fd, &st);
283 +
284 + if (!open_ok)
285 + return NULL;
286 +
287 + return load_commit_graph_one_fd_st(graph_file, fd, &st);
288 +}
289 +
290 static void prepare_commit_graph_one(struct repository *r, const char *obj_dir)
291 {
292 char *graph_name;
commit-graph.h
+3 -1
@@ -11,6 +11,7 @@
11 struct commit;
12
13 char *get_commit_graph_filename(const char *obj_dir);
14 +int open_commit_graph(const char *graph_file, int *fd, struct stat *st);
15
16 /*
17 * Given a commit struct, try to fill the commit struct info, including:
@@ -52,7 +53,8 @@ struct commit_graph {
53 const unsigned char *chunk_extra_edges;
54 };
55
55 -struct commit_graph *load_commit_graph_one(const char *graph_file);
56 +struct commit_graph *load_commit_graph_one_fd_st(const char *graph_file,
57 + int fd, struct stat *st);
58
59 struct commit_graph *parse_commit_graph(void *graph_map, int fd,
60 size_t graph_size);
t/t5318-commit-graph.sh
+1 -1
@@ -377,7 +377,7 @@ corrupt_graph_verify() {
377 test_must_fail git commit-graph verify 2>test_err &&
378 grep -v "^+" test_err >err &&
379 test_i18ngrep "$grepstr" err &&
380 - test_might_fail git status --short
380 + git status --short
381 }
382
383 # usage: corrupt_graph_and_verify <position> <data> <string> [<zero_pos>]