builtin / commit-graph.con commit Merge branch 'js/pack-objects-mutex-init-fix' into maint (e3c18aa)
   1#include "builtin.h"
   2#include "config.h"
   3#include "dir.h"
   4#include "lockfile.h"
   5#include "parse-options.h"
   6#include "repository.h"
   7#include "commit-graph.h"
   8
   9static char const * const builtin_commit_graph_usage[] = {
  10        N_("git commit-graph [--object-dir <objdir>]"),
  11        N_("git commit-graph read [--object-dir <objdir>]"),
  12        N_("git commit-graph verify [--object-dir <objdir>]"),
  13        N_("git commit-graph write [--object-dir <objdir>] [--append] [--reachable|--stdin-packs|--stdin-commits]"),
  14        NULL
  15};
  16
  17static const char * const builtin_commit_graph_verify_usage[] = {
  18        N_("git commit-graph verify [--object-dir <objdir>]"),
  19        NULL
  20};
  21
  22static const char * const builtin_commit_graph_read_usage[] = {
  23        N_("git commit-graph read [--object-dir <objdir>]"),
  24        NULL
  25};
  26
  27static const char * const builtin_commit_graph_write_usage[] = {
  28        N_("git commit-graph write [--object-dir <objdir>] [--append] [--reachable|--stdin-packs|--stdin-commits]"),
  29        NULL
  30};
  31
  32static struct opts_commit_graph {
  33        const char *obj_dir;
  34        int reachable;
  35        int stdin_packs;
  36        int stdin_commits;
  37        int append;
  38} opts;
  39
  40
  41static int graph_verify(int argc, const char **argv)
  42{
  43        struct commit_graph *graph = NULL;
  44        char *graph_name;
  45
  46        static struct option builtin_commit_graph_verify_options[] = {
  47                OPT_STRING(0, "object-dir", &opts.obj_dir,
  48                           N_("dir"),
  49                           N_("The object directory to store the graph")),
  50                OPT_END(),
  51        };
  52
  53        argc = parse_options(argc, argv, NULL,
  54                             builtin_commit_graph_verify_options,
  55                             builtin_commit_graph_verify_usage, 0);
  56
  57        if (!opts.obj_dir)
  58                opts.obj_dir = get_object_directory();
  59
  60        graph_name = get_commit_graph_filename(opts.obj_dir);
  61        graph = load_commit_graph_one(graph_name);
  62        FREE_AND_NULL(graph_name);
  63
  64        if (!graph)
  65                return 0;
  66
  67        return verify_commit_graph(the_repository, graph);
  68}
  69
  70static int graph_read(int argc, const char **argv)
  71{
  72        struct commit_graph *graph = NULL;
  73        char *graph_name;
  74
  75        static struct option builtin_commit_graph_read_options[] = {
  76                OPT_STRING(0, "object-dir", &opts.obj_dir,
  77                        N_("dir"),
  78                        N_("The object directory to store the graph")),
  79                OPT_END(),
  80        };
  81
  82        argc = parse_options(argc, argv, NULL,
  83                             builtin_commit_graph_read_options,
  84                             builtin_commit_graph_read_usage, 0);
  85
  86        if (!opts.obj_dir)
  87                opts.obj_dir = get_object_directory();
  88
  89        graph_name = get_commit_graph_filename(opts.obj_dir);
  90        graph = load_commit_graph_one(graph_name);
  91
  92        if (!graph) {
  93                UNLEAK(graph_name);
  94                die("graph file %s does not exist", graph_name);
  95        }
  96
  97        FREE_AND_NULL(graph_name);
  98
  99        printf("header: %08x %d %d %d %d\n",
 100                ntohl(*(uint32_t*)graph->data),
 101                *(unsigned char*)(graph->data + 4),
 102                *(unsigned char*)(graph->data + 5),
 103                *(unsigned char*)(graph->data + 6),
 104                *(unsigned char*)(graph->data + 7));
 105        printf("num_commits: %u\n", graph->num_commits);
 106        printf("chunks:");
 107
 108        if (graph->chunk_oid_fanout)
 109                printf(" oid_fanout");
 110        if (graph->chunk_oid_lookup)
 111                printf(" oid_lookup");
 112        if (graph->chunk_commit_data)
 113                printf(" commit_metadata");
 114        if (graph->chunk_large_edges)
 115                printf(" large_edges");
 116        printf("\n");
 117
 118        free_commit_graph(graph);
 119
 120        return 0;
 121}
 122
 123extern int read_replace_refs;
 124
 125static int graph_write(int argc, const char **argv)
 126{
 127        struct string_list *pack_indexes = NULL;
 128        struct string_list *commit_hex = NULL;
 129        struct string_list lines;
 130
 131        static struct option builtin_commit_graph_write_options[] = {
 132                OPT_STRING(0, "object-dir", &opts.obj_dir,
 133                        N_("dir"),
 134                        N_("The object directory to store the graph")),
 135                OPT_BOOL(0, "reachable", &opts.reachable,
 136                        N_("start walk at all refs")),
 137                OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
 138                        N_("scan pack-indexes listed by stdin for commits")),
 139                OPT_BOOL(0, "stdin-commits", &opts.stdin_commits,
 140                        N_("start walk at commits listed by stdin")),
 141                OPT_BOOL(0, "append", &opts.append,
 142                        N_("include all commits already in the commit-graph file")),
 143                OPT_END(),
 144        };
 145
 146        argc = parse_options(argc, argv, NULL,
 147                             builtin_commit_graph_write_options,
 148                             builtin_commit_graph_write_usage, 0);
 149
 150        if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1)
 151                die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
 152        if (!opts.obj_dir)
 153                opts.obj_dir = get_object_directory();
 154
 155        read_replace_refs = 0;
 156
 157        if (opts.reachable) {
 158                write_commit_graph_reachable(opts.obj_dir, opts.append);
 159                return 0;
 160        }
 161
 162        string_list_init(&lines, 0);
 163        if (opts.stdin_packs || opts.stdin_commits) {
 164                struct strbuf buf = STRBUF_INIT;
 165
 166                while (strbuf_getline(&buf, stdin) != EOF)
 167                        string_list_append(&lines, strbuf_detach(&buf, NULL));
 168
 169                if (opts.stdin_packs)
 170                        pack_indexes = &lines;
 171                if (opts.stdin_commits)
 172                        commit_hex = &lines;
 173        }
 174
 175        write_commit_graph(opts.obj_dir,
 176                           pack_indexes,
 177                           commit_hex,
 178                           opts.append);
 179
 180        string_list_clear(&lines, 0);
 181        return 0;
 182}
 183
 184int cmd_commit_graph(int argc, const char **argv, const char *prefix)
 185{
 186        static struct option builtin_commit_graph_options[] = {
 187                OPT_STRING(0, "object-dir", &opts.obj_dir,
 188                        N_("dir"),
 189                        N_("The object directory to store the graph")),
 190                OPT_END(),
 191        };
 192
 193        if (argc == 2 && !strcmp(argv[1], "-h"))
 194                usage_with_options(builtin_commit_graph_usage,
 195                                   builtin_commit_graph_options);
 196
 197        git_config(git_default_config, NULL);
 198        argc = parse_options(argc, argv, prefix,
 199                             builtin_commit_graph_options,
 200                             builtin_commit_graph_usage,
 201                             PARSE_OPT_STOP_AT_NON_OPTION);
 202
 203        if (argc > 0) {
 204                if (!strcmp(argv[0], "read"))
 205                        return graph_read(argc, argv);
 206                if (!strcmp(argv[0], "verify"))
 207                        return graph_verify(argc, argv);
 208                if (!strcmp(argv[0], "write"))
 209                        return graph_write(argc, argv);
 210        }
 211
 212        usage_with_options(builtin_commit_graph_usage,
 213                           builtin_commit_graph_options);
 214}