Derrick Stolee | 4ce58ee | 2018-04-02 16:34:18 -0400 | [diff] [blame] | 1 | #include "builtin.h" |
| 2 | #include "config.h" |
Derrick Stolee | f237c8b | 2018-04-02 16:34:20 -0400 | [diff] [blame] | 3 | #include "dir.h" |
| 4 | #include "lockfile.h" |
Derrick Stolee | 4ce58ee | 2018-04-02 16:34:18 -0400 | [diff] [blame] | 5 | #include "parse-options.h" |
Derrick Stolee | 283e68c | 2018-06-27 09:24:32 -0400 | [diff] [blame] | 6 | #include "repository.h" |
Derrick Stolee | f237c8b | 2018-04-02 16:34:20 -0400 | [diff] [blame] | 7 | #include "commit-graph.h" |
Derrick Stolee | 4ce58ee | 2018-04-02 16:34:18 -0400 | [diff] [blame] | 8 | |
| 9 | static char const * const builtin_commit_graph_usage[] = { |
| 10 | N_("git commit-graph [--object-dir <objdir>]"), |
Derrick Stolee | 2a2e32b | 2018-04-10 08:56:02 -0400 | [diff] [blame] | 11 | N_("git commit-graph read [--object-dir <objdir>]"), |
Derrick Stolee | 283e68c | 2018-06-27 09:24:32 -0400 | [diff] [blame] | 12 | N_("git commit-graph verify [--object-dir <objdir>]"), |
Derrick Stolee | 59fb877 | 2018-06-27 09:24:45 -0400 | [diff] [blame] | 13 | N_("git commit-graph write [--object-dir <objdir>] [--append] [--reachable|--stdin-packs|--stdin-commits]"), |
Derrick Stolee | f237c8b | 2018-04-02 16:34:20 -0400 | [diff] [blame] | 14 | NULL |
| 15 | }; |
| 16 | |
Derrick Stolee | 283e68c | 2018-06-27 09:24:32 -0400 | [diff] [blame] | 17 | static const char * const builtin_commit_graph_verify_usage[] = { |
| 18 | N_("git commit-graph verify [--object-dir <objdir>]"), |
| 19 | NULL |
| 20 | }; |
| 21 | |
Derrick Stolee | 2a2e32b | 2018-04-10 08:56:02 -0400 | [diff] [blame] | 22 | static const char * const builtin_commit_graph_read_usage[] = { |
| 23 | N_("git commit-graph read [--object-dir <objdir>]"), |
| 24 | NULL |
| 25 | }; |
| 26 | |
Derrick Stolee | f237c8b | 2018-04-02 16:34:20 -0400 | [diff] [blame] | 27 | static const char * const builtin_commit_graph_write_usage[] = { |
Derrick Stolee | 59fb877 | 2018-06-27 09:24:45 -0400 | [diff] [blame] | 28 | N_("git commit-graph write [--object-dir <objdir>] [--append] [--reachable|--stdin-packs|--stdin-commits]"), |
Derrick Stolee | 4ce58ee | 2018-04-02 16:34:18 -0400 | [diff] [blame] | 29 | NULL |
| 30 | }; |
| 31 | |
| 32 | static struct opts_commit_graph { |
| 33 | const char *obj_dir; |
Derrick Stolee | 59fb877 | 2018-06-27 09:24:45 -0400 | [diff] [blame] | 34 | int reachable; |
Derrick Stolee | 049d51a | 2018-04-10 08:56:06 -0400 | [diff] [blame] | 35 | int stdin_packs; |
Derrick Stolee | 3d5df01 | 2018-04-10 08:56:07 -0400 | [diff] [blame] | 36 | int stdin_commits; |
Derrick Stolee | 7547b95 | 2018-04-10 08:56:08 -0400 | [diff] [blame] | 37 | int append; |
Derrick Stolee | 4ce58ee | 2018-04-02 16:34:18 -0400 | [diff] [blame] | 38 | } opts; |
| 39 | |
Derrick Stolee | 283e68c | 2018-06-27 09:24:32 -0400 | [diff] [blame] | 40 | |
| 41 | static 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 | |
Derrick Stolee | 2a2e32b | 2018-04-10 08:56:02 -0400 | [diff] [blame] | 70 | static 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 | |
Derrick Stolee | 883e5c7 | 2018-06-27 09:24:27 -0400 | [diff] [blame] | 92 | if (!graph) { |
| 93 | UNLEAK(graph_name); |
Derrick Stolee | 2a2e32b | 2018-04-10 08:56:02 -0400 | [diff] [blame] | 94 | die("graph file %s does not exist", graph_name); |
Derrick Stolee | 883e5c7 | 2018-06-27 09:24:27 -0400 | [diff] [blame] | 95 | } |
| 96 | |
Derrick Stolee | 2a2e32b | 2018-04-10 08:56:02 -0400 | [diff] [blame] | 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 | |
Jonathan Tan | c3756d5 | 2018-07-11 15:42:40 -0700 | [diff] [blame] | 118 | free_commit_graph(graph); |
| 119 | |
Derrick Stolee | 2a2e32b | 2018-04-10 08:56:02 -0400 | [diff] [blame] | 120 | return 0; |
| 121 | } |
| 122 | |
Derrick Stolee | f237c8b | 2018-04-02 16:34:20 -0400 | [diff] [blame] | 123 | static int graph_write(int argc, const char **argv) |
| 124 | { |
Derrick Stolee | d88b14b | 2018-06-27 09:24:44 -0400 | [diff] [blame] | 125 | struct string_list *pack_indexes = NULL; |
| 126 | struct string_list *commit_hex = NULL; |
| 127 | struct string_list lines; |
Derrick Stolee | 049d51a | 2018-04-10 08:56:06 -0400 | [diff] [blame] | 128 | |
Derrick Stolee | f237c8b | 2018-04-02 16:34:20 -0400 | [diff] [blame] | 129 | static struct option builtin_commit_graph_write_options[] = { |
| 130 | OPT_STRING(0, "object-dir", &opts.obj_dir, |
| 131 | N_("dir"), |
| 132 | N_("The object directory to store the graph")), |
Derrick Stolee | 59fb877 | 2018-06-27 09:24:45 -0400 | [diff] [blame] | 133 | OPT_BOOL(0, "reachable", &opts.reachable, |
| 134 | N_("start walk at all refs")), |
Derrick Stolee | 049d51a | 2018-04-10 08:56:06 -0400 | [diff] [blame] | 135 | OPT_BOOL(0, "stdin-packs", &opts.stdin_packs, |
| 136 | N_("scan pack-indexes listed by stdin for commits")), |
Derrick Stolee | 3d5df01 | 2018-04-10 08:56:07 -0400 | [diff] [blame] | 137 | OPT_BOOL(0, "stdin-commits", &opts.stdin_commits, |
| 138 | N_("start walk at commits listed by stdin")), |
Derrick Stolee | 7547b95 | 2018-04-10 08:56:08 -0400 | [diff] [blame] | 139 | OPT_BOOL(0, "append", &opts.append, |
| 140 | N_("include all commits already in the commit-graph file")), |
Derrick Stolee | f237c8b | 2018-04-02 16:34:20 -0400 | [diff] [blame] | 141 | OPT_END(), |
| 142 | }; |
| 143 | |
| 144 | argc = parse_options(argc, argv, NULL, |
| 145 | builtin_commit_graph_write_options, |
| 146 | builtin_commit_graph_write_usage, 0); |
| 147 | |
Derrick Stolee | 59fb877 | 2018-06-27 09:24:45 -0400 | [diff] [blame] | 148 | if (opts.reachable + opts.stdin_packs + opts.stdin_commits > 1) |
| 149 | die(_("use at most one of --reachable, --stdin-commits, or --stdin-packs")); |
Derrick Stolee | f237c8b | 2018-04-02 16:34:20 -0400 | [diff] [blame] | 150 | if (!opts.obj_dir) |
| 151 | opts.obj_dir = get_object_directory(); |
| 152 | |
Derrick Stolee | 59fb877 | 2018-06-27 09:24:45 -0400 | [diff] [blame] | 153 | if (opts.reachable) { |
| 154 | write_commit_graph_reachable(opts.obj_dir, opts.append); |
| 155 | return 0; |
| 156 | } |
| 157 | |
Derrick Stolee | d88b14b | 2018-06-27 09:24:44 -0400 | [diff] [blame] | 158 | string_list_init(&lines, 0); |
Derrick Stolee | 3d5df01 | 2018-04-10 08:56:07 -0400 | [diff] [blame] | 159 | if (opts.stdin_packs || opts.stdin_commits) { |
Derrick Stolee | 049d51a | 2018-04-10 08:56:06 -0400 | [diff] [blame] | 160 | struct strbuf buf = STRBUF_INIT; |
Derrick Stolee | 049d51a | 2018-04-10 08:56:06 -0400 | [diff] [blame] | 161 | |
Derrick Stolee | d88b14b | 2018-06-27 09:24:44 -0400 | [diff] [blame] | 162 | while (strbuf_getline(&buf, stdin) != EOF) |
| 163 | string_list_append(&lines, strbuf_detach(&buf, NULL)); |
Derrick Stolee | 049d51a | 2018-04-10 08:56:06 -0400 | [diff] [blame] | 164 | |
Derrick Stolee | d88b14b | 2018-06-27 09:24:44 -0400 | [diff] [blame] | 165 | if (opts.stdin_packs) |
| 166 | pack_indexes = &lines; |
| 167 | if (opts.stdin_commits) |
| 168 | commit_hex = &lines; |
Derrick Stolee | 049d51a | 2018-04-10 08:56:06 -0400 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | write_commit_graph(opts.obj_dir, |
| 172 | pack_indexes, |
Derrick Stolee | 3d5df01 | 2018-04-10 08:56:07 -0400 | [diff] [blame] | 173 | commit_hex, |
Derrick Stolee | 7547b95 | 2018-04-10 08:56:08 -0400 | [diff] [blame] | 174 | opts.append); |
Derrick Stolee | 049d51a | 2018-04-10 08:56:06 -0400 | [diff] [blame] | 175 | |
Derrick Stolee | d88b14b | 2018-06-27 09:24:44 -0400 | [diff] [blame] | 176 | string_list_clear(&lines, 0); |
Derrick Stolee | f237c8b | 2018-04-02 16:34:20 -0400 | [diff] [blame] | 177 | return 0; |
| 178 | } |
Derrick Stolee | 4ce58ee | 2018-04-02 16:34:18 -0400 | [diff] [blame] | 179 | |
| 180 | int cmd_commit_graph(int argc, const char **argv, const char *prefix) |
| 181 | { |
| 182 | static struct option builtin_commit_graph_options[] = { |
| 183 | OPT_STRING(0, "object-dir", &opts.obj_dir, |
| 184 | N_("dir"), |
| 185 | N_("The object directory to store the graph")), |
| 186 | OPT_END(), |
| 187 | }; |
| 188 | |
| 189 | if (argc == 2 && !strcmp(argv[1], "-h")) |
| 190 | usage_with_options(builtin_commit_graph_usage, |
| 191 | builtin_commit_graph_options); |
| 192 | |
| 193 | git_config(git_default_config, NULL); |
| 194 | argc = parse_options(argc, argv, prefix, |
| 195 | builtin_commit_graph_options, |
| 196 | builtin_commit_graph_usage, |
| 197 | PARSE_OPT_STOP_AT_NON_OPTION); |
| 198 | |
Derrick Stolee | f237c8b | 2018-04-02 16:34:20 -0400 | [diff] [blame] | 199 | if (argc > 0) { |
Derrick Stolee | 2a2e32b | 2018-04-10 08:56:02 -0400 | [diff] [blame] | 200 | if (!strcmp(argv[0], "read")) |
| 201 | return graph_read(argc, argv); |
Derrick Stolee | 283e68c | 2018-06-27 09:24:32 -0400 | [diff] [blame] | 202 | if (!strcmp(argv[0], "verify")) |
| 203 | return graph_verify(argc, argv); |
Derrick Stolee | f237c8b | 2018-04-02 16:34:20 -0400 | [diff] [blame] | 204 | if (!strcmp(argv[0], "write")) |
| 205 | return graph_write(argc, argv); |
| 206 | } |
| 207 | |
Derrick Stolee | 4ce58ee | 2018-04-02 16:34:18 -0400 | [diff] [blame] | 208 | usage_with_options(builtin_commit_graph_usage, |
| 209 | builtin_commit_graph_options); |
| 210 | } |