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 | 7547b95 | 2018-04-10 08:56:08 -0400 | [diff] [blame] | 13 | N_("git commit-graph write [--object-dir <objdir>] [--append] [--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 | 7547b95 | 2018-04-10 08:56:08 -0400 | [diff] [blame] | 28 | N_("git commit-graph write [--object-dir <objdir>] [--append] [--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 | 049d51a | 2018-04-10 08:56:06 -0400 | [diff] [blame] | 34 | int stdin_packs; |
Derrick Stolee | 3d5df01 | 2018-04-10 08:56:07 -0400 | [diff] [blame] | 35 | int stdin_commits; |
Derrick Stolee | 7547b95 | 2018-04-10 08:56:08 -0400 | [diff] [blame] | 36 | int append; |
Derrick Stolee | 4ce58ee | 2018-04-02 16:34:18 -0400 | [diff] [blame] | 37 | } opts; |
| 38 | |
Derrick Stolee | 283e68c | 2018-06-27 09:24:32 -0400 | [diff] [blame] | 39 | |
| 40 | static int graph_verify(int argc, const char **argv) |
| 41 | { |
| 42 | struct commit_graph *graph = NULL; |
| 43 | char *graph_name; |
| 44 | |
| 45 | static struct option builtin_commit_graph_verify_options[] = { |
| 46 | OPT_STRING(0, "object-dir", &opts.obj_dir, |
| 47 | N_("dir"), |
| 48 | N_("The object directory to store the graph")), |
| 49 | OPT_END(), |
| 50 | }; |
| 51 | |
| 52 | argc = parse_options(argc, argv, NULL, |
| 53 | builtin_commit_graph_verify_options, |
| 54 | builtin_commit_graph_verify_usage, 0); |
| 55 | |
| 56 | if (!opts.obj_dir) |
| 57 | opts.obj_dir = get_object_directory(); |
| 58 | |
| 59 | graph_name = get_commit_graph_filename(opts.obj_dir); |
| 60 | graph = load_commit_graph_one(graph_name); |
| 61 | FREE_AND_NULL(graph_name); |
| 62 | |
| 63 | if (!graph) |
| 64 | return 0; |
| 65 | |
| 66 | return verify_commit_graph(the_repository, graph); |
| 67 | } |
| 68 | |
Derrick Stolee | 2a2e32b | 2018-04-10 08:56:02 -0400 | [diff] [blame] | 69 | static int graph_read(int argc, const char **argv) |
| 70 | { |
| 71 | struct commit_graph *graph = NULL; |
| 72 | char *graph_name; |
| 73 | |
| 74 | static struct option builtin_commit_graph_read_options[] = { |
| 75 | OPT_STRING(0, "object-dir", &opts.obj_dir, |
| 76 | N_("dir"), |
| 77 | N_("The object directory to store the graph")), |
| 78 | OPT_END(), |
| 79 | }; |
| 80 | |
| 81 | argc = parse_options(argc, argv, NULL, |
| 82 | builtin_commit_graph_read_options, |
| 83 | builtin_commit_graph_read_usage, 0); |
| 84 | |
| 85 | if (!opts.obj_dir) |
| 86 | opts.obj_dir = get_object_directory(); |
| 87 | |
| 88 | graph_name = get_commit_graph_filename(opts.obj_dir); |
| 89 | graph = load_commit_graph_one(graph_name); |
| 90 | |
Derrick Stolee | 883e5c7 | 2018-06-27 09:24:27 -0400 | [diff] [blame] | 91 | if (!graph) { |
| 92 | UNLEAK(graph_name); |
Derrick Stolee | 2a2e32b | 2018-04-10 08:56:02 -0400 | [diff] [blame] | 93 | die("graph file %s does not exist", graph_name); |
Derrick Stolee | 883e5c7 | 2018-06-27 09:24:27 -0400 | [diff] [blame] | 94 | } |
| 95 | |
Derrick Stolee | 2a2e32b | 2018-04-10 08:56:02 -0400 | [diff] [blame] | 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 | return 0; |
| 118 | } |
| 119 | |
Derrick Stolee | f237c8b | 2018-04-02 16:34:20 -0400 | [diff] [blame] | 120 | static int graph_write(int argc, const char **argv) |
| 121 | { |
Derrick Stolee | 049d51a | 2018-04-10 08:56:06 -0400 | [diff] [blame] | 122 | const char **pack_indexes = NULL; |
| 123 | int packs_nr = 0; |
Derrick Stolee | 3d5df01 | 2018-04-10 08:56:07 -0400 | [diff] [blame] | 124 | const char **commit_hex = NULL; |
| 125 | int commits_nr = 0; |
Derrick Stolee | 049d51a | 2018-04-10 08:56:06 -0400 | [diff] [blame] | 126 | const char **lines = NULL; |
| 127 | int lines_nr = 0; |
| 128 | int lines_alloc = 0; |
| 129 | |
Derrick Stolee | f237c8b | 2018-04-02 16:34:20 -0400 | [diff] [blame] | 130 | static struct option builtin_commit_graph_write_options[] = { |
| 131 | OPT_STRING(0, "object-dir", &opts.obj_dir, |
| 132 | N_("dir"), |
| 133 | N_("The object directory to store the graph")), |
Derrick Stolee | 049d51a | 2018-04-10 08:56:06 -0400 | [diff] [blame] | 134 | OPT_BOOL(0, "stdin-packs", &opts.stdin_packs, |
| 135 | N_("scan pack-indexes listed by stdin for commits")), |
Derrick Stolee | 3d5df01 | 2018-04-10 08:56:07 -0400 | [diff] [blame] | 136 | OPT_BOOL(0, "stdin-commits", &opts.stdin_commits, |
| 137 | N_("start walk at commits listed by stdin")), |
Derrick Stolee | 7547b95 | 2018-04-10 08:56:08 -0400 | [diff] [blame] | 138 | OPT_BOOL(0, "append", &opts.append, |
| 139 | N_("include all commits already in the commit-graph file")), |
Derrick Stolee | f237c8b | 2018-04-02 16:34:20 -0400 | [diff] [blame] | 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 | |
Derrick Stolee | 3d5df01 | 2018-04-10 08:56:07 -0400 | [diff] [blame] | 147 | if (opts.stdin_packs && opts.stdin_commits) |
| 148 | die(_("cannot use both --stdin-commits and --stdin-packs")); |
Derrick Stolee | f237c8b | 2018-04-02 16:34:20 -0400 | [diff] [blame] | 149 | if (!opts.obj_dir) |
| 150 | opts.obj_dir = get_object_directory(); |
| 151 | |
Derrick Stolee | 3d5df01 | 2018-04-10 08:56:07 -0400 | [diff] [blame] | 152 | if (opts.stdin_packs || opts.stdin_commits) { |
Derrick Stolee | 049d51a | 2018-04-10 08:56:06 -0400 | [diff] [blame] | 153 | struct strbuf buf = STRBUF_INIT; |
| 154 | lines_nr = 0; |
| 155 | lines_alloc = 128; |
| 156 | ALLOC_ARRAY(lines, lines_alloc); |
| 157 | |
| 158 | while (strbuf_getline(&buf, stdin) != EOF) { |
| 159 | ALLOC_GROW(lines, lines_nr + 1, lines_alloc); |
| 160 | lines[lines_nr++] = strbuf_detach(&buf, NULL); |
| 161 | } |
| 162 | |
Derrick Stolee | 3d5df01 | 2018-04-10 08:56:07 -0400 | [diff] [blame] | 163 | if (opts.stdin_packs) { |
| 164 | pack_indexes = lines; |
| 165 | packs_nr = lines_nr; |
| 166 | } |
| 167 | if (opts.stdin_commits) { |
| 168 | commit_hex = lines; |
| 169 | commits_nr = lines_nr; |
| 170 | } |
Derrick Stolee | 049d51a | 2018-04-10 08:56:06 -0400 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | write_commit_graph(opts.obj_dir, |
| 174 | pack_indexes, |
Derrick Stolee | 3d5df01 | 2018-04-10 08:56:07 -0400 | [diff] [blame] | 175 | packs_nr, |
| 176 | commit_hex, |
Derrick Stolee | 7547b95 | 2018-04-10 08:56:08 -0400 | [diff] [blame] | 177 | commits_nr, |
| 178 | opts.append); |
Derrick Stolee | 049d51a | 2018-04-10 08:56:06 -0400 | [diff] [blame] | 179 | |
Derrick Stolee | f237c8b | 2018-04-02 16:34:20 -0400 | [diff] [blame] | 180 | return 0; |
| 181 | } |
Derrick Stolee | 4ce58ee | 2018-04-02 16:34:18 -0400 | [diff] [blame] | 182 | |
| 183 | int cmd_commit_graph(int argc, const char **argv, const char *prefix) |
| 184 | { |
| 185 | static struct option builtin_commit_graph_options[] = { |
| 186 | OPT_STRING(0, "object-dir", &opts.obj_dir, |
| 187 | N_("dir"), |
| 188 | N_("The object directory to store the graph")), |
| 189 | OPT_END(), |
| 190 | }; |
| 191 | |
| 192 | if (argc == 2 && !strcmp(argv[1], "-h")) |
| 193 | usage_with_options(builtin_commit_graph_usage, |
| 194 | builtin_commit_graph_options); |
| 195 | |
| 196 | git_config(git_default_config, NULL); |
| 197 | argc = parse_options(argc, argv, prefix, |
| 198 | builtin_commit_graph_options, |
| 199 | builtin_commit_graph_usage, |
| 200 | PARSE_OPT_STOP_AT_NON_OPTION); |
| 201 | |
Derrick Stolee | f237c8b | 2018-04-02 16:34:20 -0400 | [diff] [blame] | 202 | if (argc > 0) { |
Derrick Stolee | 2a2e32b | 2018-04-10 08:56:02 -0400 | [diff] [blame] | 203 | if (!strcmp(argv[0], "read")) |
| 204 | return graph_read(argc, argv); |
Derrick Stolee | 283e68c | 2018-06-27 09:24:32 -0400 | [diff] [blame] | 205 | if (!strcmp(argv[0], "verify")) |
| 206 | return graph_verify(argc, argv); |
Derrick Stolee | f237c8b | 2018-04-02 16:34:20 -0400 | [diff] [blame] | 207 | if (!strcmp(argv[0], "write")) |
| 208 | return graph_write(argc, argv); |
| 209 | } |
| 210 | |
Derrick Stolee | 4ce58ee | 2018-04-02 16:34:18 -0400 | [diff] [blame] | 211 | usage_with_options(builtin_commit_graph_usage, |
| 212 | builtin_commit_graph_options); |
| 213 | } |