builtin / commit-graph.con commit builtin/commit-graph.c: UNLEAK variables (0bfb48e)
   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        UNLEAK(graph);
  68        return verify_commit_graph(the_repository, graph);
  69}
  70
  71static int graph_read(int argc, const char **argv)
  72{
  73        struct commit_graph *graph = NULL;
  74        char *graph_name;
  75
  76        static struct option builtin_commit_graph_read_options[] = {
  77                OPT_STRING(0, "object-dir", &opts.obj_dir,
  78                        N_("dir"),
  79                        N_("The object directory to store the graph")),
  80                OPT_END(),
  81        };
  82
  83        argc = parse_options(argc, argv, NULL,
  84                             builtin_commit_graph_read_options,
  85                             builtin_commit_graph_read_usage, 0);
  86
  87        if (!opts.obj_dir)
  88                opts.obj_dir = get_object_directory();
  89
  90        graph_name = get_commit_graph_filename(opts.obj_dir);
  91        graph = load_commit_graph_one(graph_name);
  92
  93        if (!graph)
  94                die("graph file %s does not exist", graph_name);
  95
  96        FREE_AND_NULL(graph_name);
  97
  98        printf("header: %08x %d %d %d %d\n",
  99                ntohl(*(uint32_t*)graph->data),
 100                *(unsigned char*)(graph->data + 4),
 101                *(unsigned char*)(graph->data + 5),
 102                *(unsigned char*)(graph->data + 6),
 103                *(unsigned char*)(graph->data + 7));
 104        printf("num_commits: %u\n", graph->num_commits);
 105        printf("chunks:");
 106
 107        if (graph->chunk_oid_fanout)
 108                printf(" oid_fanout");
 109        if (graph->chunk_oid_lookup)
 110                printf(" oid_lookup");
 111        if (graph->chunk_commit_data)
 112                printf(" commit_metadata");
 113        if (graph->chunk_large_edges)
 114                printf(" large_edges");
 115        printf("\n");
 116
 117        UNLEAK(graph);
 118
 119        return 0;
 120}
 121
 122static int graph_write(int argc, const char **argv)
 123{
 124        struct string_list *pack_indexes = NULL;
 125        struct string_list *commit_hex = NULL;
 126        struct string_list lines;
 127
 128        static struct option builtin_commit_graph_write_options[] = {
 129                OPT_STRING(0, "object-dir", &opts.obj_dir,
 130                        N_("dir"),
 131                        N_("The object directory to store the graph")),
 132                OPT_BOOL(0, "reachable", &opts.reachable,
 133                        N_("start walk at all refs")),
 134                OPT_BOOL(0, "stdin-packs", &opts.stdin_packs,
 135                        N_("scan pack-indexes listed by stdin for commits")),
 136                OPT_BOOL(0, "stdin-commits", &opts.stdin_commits,
 137                        N_("start walk at commits listed by stdin")),
 138                OPT_BOOL(0, "append", &opts.append,
 139                        N_("include all commits already in the commit-graph file")),
 140                OPT_END(),
 141        };
 142
 143        argc = parse_options(argc, argv, NULL,
 144                             builtin_commit_graph_write_options,
 145                             builtin_commit_graph_write_usage, 0);
 146
 147        if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1)
 148                die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs"));
 149        if (!opts.obj_dir)
 150                opts.obj_dir = get_object_directory();
 151
 152        if (opts.reachable) {
 153                write_commit_graph_reachable(opts.obj_dir, opts.append, 1);
 154                return 0;
 155        }
 156
 157        string_list_init(&lines, 0);
 158        if (opts.stdin_packs || opts.stdin_commits) {
 159                struct strbuf buf = STRBUF_INIT;
 160
 161                while (strbuf_getline(&buf, stdin) != EOF)
 162                        string_list_append(&lines, strbuf_detach(&buf, NULL));
 163
 164                if (opts.stdin_packs)
 165                        pack_indexes = &lines;
 166                if (opts.stdin_commits)
 167                        commit_hex = &lines;
 168
 169                UNLEAK(buf);
 170        }
 171
 172        write_commit_graph(opts.obj_dir,
 173                           pack_indexes,
 174                           commit_hex,
 175                           opts.append,
 176                           1);
 177
 178        UNLEAK(lines);
 179        return 0;
 180}
 181
 182int cmd_commit_graph(int argc, const char **argv, const char *prefix)
 183{
 184        static struct option builtin_commit_graph_options[] = {
 185                OPT_STRING(0, "object-dir", &opts.obj_dir,
 186                        N_("dir"),
 187                        N_("The object directory to store the graph")),
 188                OPT_END(),
 189        };
 190
 191        if (argc == 2 && !strcmp(argv[1], "-h"))
 192                usage_with_options(builtin_commit_graph_usage,
 193                                   builtin_commit_graph_options);
 194
 195        git_config(git_default_config, NULL);
 196        argc = parse_options(argc, argv, prefix,
 197                             builtin_commit_graph_options,
 198                             builtin_commit_graph_usage,
 199                             PARSE_OPT_STOP_AT_NON_OPTION);
 200
 201        if (argc > 0) {
 202                if (!strcmp(argv[0], "read"))
 203                        return graph_read(argc, argv);
 204                if (!strcmp(argv[0], "verify"))
 205                        return graph_verify(argc, argv);
 206                if (!strcmp(argv[0], "write"))
 207                        return graph_write(argc, argv);
 208        }
 209
 210        usage_with_options(builtin_commit_graph_usage,
 211                           builtin_commit_graph_options);
 212}