commit-graph: verify chains with --shallow mode
authorDerrick Stolee <dstolee@microsoft.com>
Tue, 18 Jun 2019 18:14:32 +0000 (11:14 -0700)
committerJunio C Hamano <gitster@pobox.com>
Thu, 20 Jun 2019 03:46:26 +0000 (20:46 -0700)
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 <dstolee@microsoft.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/git-commit-graph.txt
builtin/commit-graph.c
commit-graph.c
commit-graph.h
t/t5324-split-commit-graph.sh

index 365e145..eb5e786 100644 (file)
@@ -10,7 +10,7 @@ SYNOPSIS
 --------
 [verse]
 'git commit-graph read' [--object-dir <dir>]
-'git commit-graph verify' [--object-dir <dir>]
+'git commit-graph verify' [--object-dir <dir>] [--shallow]
 'git commit-graph write' <options> [--object-dir <dir>]
 
 
@@ -80,6 +80,9 @@ Used for debugging purposes.
 
 Read the commit-graph file and verify its contents against the object
 database. Used to check for corrupted data.
++
+With the `--shallow` option, only check the tip commit-graph file in
+a chain of split commit-graphs.
 
 
 EXAMPLES
index 6c0b5b1..38027b8 100644 (file)
@@ -5,17 +5,18 @@
 #include "parse-options.h"
 #include "repository.h"
 #include "commit-graph.h"
+#include "object-store.h"
 
 static char const * const builtin_commit_graph_usage[] = {
        N_("git commit-graph [--object-dir <objdir>]"),
        N_("git commit-graph read [--object-dir <objdir>]"),
-       N_("git commit-graph verify [--object-dir <objdir>]"),
+       N_("git commit-graph verify [--object-dir <objdir>] [--shallow]"),
        N_("git commit-graph write [--object-dir <objdir>] [--append|--split] [--reachable|--stdin-packs|--stdin-commits] <split options>"),
        NULL
 };
 
 static const char * const builtin_commit_graph_verify_usage[] = {
-       N_("git commit-graph verify [--object-dir <objdir>]"),
+       N_("git commit-graph verify [--object-dir <objdir>] [--shallow]"),
        NULL
 };
 
@@ -36,6 +37,7 @@ static struct opts_commit_graph {
        int stdin_commits;
        int append;
        int split;
+       int shallow;
 } opts;
 
 static int graph_verify(int argc, const char **argv)
@@ -45,11 +47,14 @@ static int graph_verify(int argc, const char **argv)
        int open_ok;
        int fd;
        struct stat st;
+       int flags = 0;
 
        static struct option builtin_commit_graph_verify_options[] = {
                OPT_STRING(0, "object-dir", &opts.obj_dir,
                           N_("dir"),
                           N_("The object directory to store the graph")),
+               OPT_BOOL(0, "shallow", &opts.shallow,
+                        N_("if the commit-graph is split, only verify the tip file")),
                OPT_END(),
        };
 
@@ -59,21 +64,27 @@ static int graph_verify(int argc, const char **argv)
 
        if (!opts.obj_dir)
                opts.obj_dir = get_object_directory();
+       if (opts.shallow)
+               flags |= COMMIT_GRAPH_VERIFY_SHALLOW;
 
        graph_name = get_commit_graph_filename(opts.obj_dir);
        open_ok = open_commit_graph(graph_name, &fd, &st);
-       if (!open_ok && errno == ENOENT)
-               return 0;
-       if (!open_ok)
+       if (!open_ok && errno != ENOENT)
                die_errno(_("Could not open commit-graph '%s'"), graph_name);
-       graph = load_commit_graph_one_fd_st(fd, &st);
+
        FREE_AND_NULL(graph_name);
 
+       if (open_ok)
+               graph = load_commit_graph_one_fd_st(fd, &st);
+        else
+               graph = read_commit_graph_one(the_repository, opts.obj_dir);
+
+       /* Return failure if open_ok predicted success */
        if (!graph)
-               return 1;
+               return !!open_ok;
 
        UNLEAK(graph);
-       return verify_commit_graph(the_repository, graph);
+       return verify_commit_graph(the_repository, graph, flags);
 }
 
 static int graph_read(int argc, const char **argv)
index 315088d..f33f4fe 100644 (file)
@@ -428,7 +428,7 @@ static struct commit_graph *load_commit_graph_chain(struct repository *r, const
        return graph_chain;
 }
 
-static struct commit_graph *read_commit_graph_one(struct repository *r, const char *obj_dir)
+struct commit_graph *read_commit_graph_one(struct repository *r, const char *obj_dir)
 {
        struct commit_graph *g = load_commit_graph_v1(r, obj_dir);
 
@@ -1887,7 +1887,7 @@ static void graph_report(const char *fmt, ...)
 #define GENERATION_ZERO_EXISTS 1
 #define GENERATION_NUMBER_EXISTS 2
 
-int verify_commit_graph(struct repository *r, struct commit_graph *g)
+int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
 {
        uint32_t i, cur_fanout_pos = 0;
        struct object_id prev_oid, cur_oid, checksum;
@@ -1895,6 +1895,7 @@ int verify_commit_graph(struct repository *r, struct commit_graph *g)
        struct hashfile *f;
        int devnull;
        struct progress *progress = NULL;
+       int local_error = 0;
 
        if (!g) {
                graph_report("no commit-graph file loaded");
@@ -1989,6 +1990,9 @@ int verify_commit_graph(struct repository *r, struct commit_graph *g)
                                break;
                        }
 
+                       /* parse parent in case it is in a base graph */
+                       parse_commit_in_graph_one(r, g, graph_parents->item);
+
                        if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
                                graph_report(_("commit-graph parent for %s is %s != %s"),
                                             oid_to_hex(&cur_oid),
@@ -2040,7 +2044,12 @@ int verify_commit_graph(struct repository *r, struct commit_graph *g)
        }
        stop_progress(&progress);
 
-       return verify_commit_graph_error;
+       local_error = verify_commit_graph_error;
+
+       if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW) && g->base_graph)
+               local_error |= verify_commit_graph(r, g->base_graph, flags);
+
+       return local_error;
 }
 
 void free_commit_graph(struct commit_graph *g)
index a84c22a..df9a3b2 100644 (file)
@@ -61,7 +61,7 @@ struct commit_graph {
 };
 
 struct commit_graph *load_commit_graph_one_fd_st(int fd, struct stat *st);
-
+struct commit_graph *read_commit_graph_one(struct repository *r, const char *obj_dir);
 struct commit_graph *parse_commit_graph(void *graph_map, int fd,
                                        size_t graph_size);
 
@@ -95,7 +95,9 @@ int write_commit_graph(const char *obj_dir,
                       unsigned int flags,
                       const struct split_commit_graph_opts *split_opts);
 
-int verify_commit_graph(struct repository *r, struct commit_graph *g);
+#define COMMIT_GRAPH_VERIFY_SHALLOW    (1 << 0)
+
+int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags);
 
 void close_commit_graph(struct raw_object_store *);
 void free_commit_graph(struct commit_graph *);
index 1b699a5..3df90ae 100755 (executable)
@@ -216,4 +216,66 @@ test_expect_success 'test merge stragety constants' '
        )
 '
 
+corrupt_file() {
+       file=$1
+       pos=$2
+       data="${3:-\0}"
+       printf "$data" | dd of="$file" bs=1 seek="$pos" conv=notrunc
+}
+
+test_expect_success 'verify hashes along chain, even in shallow' '
+       git clone --no-hardlinks . verify &&
+       (
+               cd verify &&
+               git commit-graph verify &&
+               base_file=$graphdir/graph-$(head -n 1 $graphdir/commit-graph-chain).graph &&
+               corrupt_file "$base_file" 1760 "\01" &&
+               test_must_fail git commit-graph verify --shallow 2>test_err &&
+               grep -v "^+" test_err >err &&
+               test_i18ngrep "incorrect checksum" err
+       )
+'
+
+test_expect_success 'verify --shallow does not check base contents' '
+       git clone --no-hardlinks . verify-shallow &&
+       (
+               cd verify-shallow &&
+               git commit-graph verify &&
+               base_file=$graphdir/graph-$(head -n 1 $graphdir/commit-graph-chain).graph &&
+               corrupt_file "$base_file" 1000 "\01" &&
+               git commit-graph verify --shallow &&
+               test_must_fail git commit-graph verify 2>test_err &&
+               grep -v "^+" test_err >err &&
+               test_i18ngrep "incorrect checksum" err
+       )
+'
+
+test_expect_success 'warn on base graph chunk incorrect' '
+       git clone --no-hardlinks . base-chunk &&
+       (
+               cd base-chunk &&
+               git commit-graph verify &&
+               base_file=$graphdir/graph-$(tail -n 1 $graphdir/commit-graph-chain).graph &&
+               corrupt_file "$base_file" 1376 "\01" &&
+               git commit-graph verify --shallow 2>test_err &&
+               grep -v "^+" test_err >err &&
+               test_i18ngrep "commit-graph chain does not match" err
+       )
+'
+
+test_expect_success 'verify after commit-graph-chain corruption' '
+       git clone --no-hardlinks . verify-chain &&
+       (
+               cd verify-chain &&
+               corrupt_file "$graphdir/commit-graph-chain" 60 "G" &&
+               git commit-graph verify 2>test_err &&
+               grep -v "^+" test_err >err &&
+               test_i18ngrep "invalid commit-graph chain" err &&
+               corrupt_file "$graphdir/commit-graph-chain" 60 "A" &&
+               git commit-graph verify 2>test_err &&
+               grep -v "^+" test_err >err &&
+               test_i18ngrep "unable to find all commit-graph files" err
+       )
+'
+
 test_done