git
[Ported from github.com/git/git] Sync with Git 2.56.1
Brought by drakestapleton
| 1 | #define USE_THE_REPOSITORY_VARIABLE |
| 2 | #include "builtin.h" |
| 3 | #include "abspath.h" |
| 4 | #include "gettext.h" |
| 5 | #include "setup.h" |
| 6 | #include "strvec.h" |
| 7 | #include "parse-options.h" |
| 8 | #include "pkt-line.h" |
| 9 | #include "bundle.h" |
| 10 | |
| 11 | /* |
| 12 | * Basic handler for bundle files to connect repositories via sneakernet. |
| 13 | * Invocation must include action. |
| 14 | * This function can create a bundle or provide information on an existing |
| 15 | * bundle supporting "fetch", "pull", and "ls-remote". |
| 16 | */ |
| 17 | |
| 18 | #define BUILTIN_BUNDLE_CREATE_USAGE \ |
| 19 | N_("git bundle create [-q | --quiet | --progress]\n" \ |
| 20 | " [--version=<version>] <file> <git-rev-list-args>") |
| 21 | #define BUILTIN_BUNDLE_VERIFY_USAGE \ |
| 22 | N_("git bundle verify [-q | --quiet] <file>") |
| 23 | #define BUILTIN_BUNDLE_LIST_HEADS_USAGE \ |
| 24 | N_("git bundle list-heads <file> [<refname>...]") |
| 25 | #define BUILTIN_BUNDLE_UNBUNDLE_USAGE \ |
| 26 | N_("git bundle unbundle [--progress] <file> [<refname>...]") |
| 27 | |
| 28 | static char const * const builtin_bundle_usage[] = { |
| 29 | BUILTIN_BUNDLE_CREATE_USAGE, |
| 30 | BUILTIN_BUNDLE_VERIFY_USAGE, |
| 31 | BUILTIN_BUNDLE_LIST_HEADS_USAGE, |
| 32 | BUILTIN_BUNDLE_UNBUNDLE_USAGE, |
| 33 | NULL, |
| 34 | }; |
| 35 | |
| 36 | static const char * const builtin_bundle_create_usage[] = { |
| 37 | BUILTIN_BUNDLE_CREATE_USAGE, |
| 38 | NULL |
| 39 | }; |
| 40 | |
| 41 | static const char * const builtin_bundle_verify_usage[] = { |
| 42 | BUILTIN_BUNDLE_VERIFY_USAGE, |
| 43 | NULL |
| 44 | }; |
| 45 | |
| 46 | static const char * const builtin_bundle_list_heads_usage[] = { |
| 47 | BUILTIN_BUNDLE_LIST_HEADS_USAGE, |
| 48 | NULL |
| 49 | }; |
| 50 | |
| 51 | static const char * const builtin_bundle_unbundle_usage[] = { |
| 52 | BUILTIN_BUNDLE_UNBUNDLE_USAGE, |
| 53 | NULL |
| 54 | }; |
| 55 | |
| 56 | static int parse_options_cmd_bundle(int argc, |
| 57 | const char **argv, |
| 58 | const char* prefix, |
| 59 | const char * const usagestr[], |
| 60 | const struct option options[], |
| 61 | char **bundle_file) { |
| 62 | argc = parse_options(argc, argv, NULL, options, usagestr, |
| 63 | PARSE_OPT_STOP_AT_NON_OPTION); |
| 64 | if (!argc) |
| 65 | usage_msg_opt(_("need a <file> argument"), usagestr, options); |
| 66 | *bundle_file = prefix_filename_except_for_dash(prefix, argv[0]); |
| 67 | return argc; |
| 68 | } |
| 69 | |
| 70 | static int cmd_bundle_create(int argc, const char **argv, const char *prefix, |
| 71 | struct repository *repo UNUSED) |
| 72 | { |
| 73 | int progress = isatty(STDERR_FILENO); |
| 74 | int version = -1; |
| 75 | struct option options[] = { |
| 76 | OPT_NEGBIT('q', "quiet", &progress, |
| 77 | N_("do not show progress meter"), 1), |
| 78 | OPT_BIT(0, "progress", &progress, |
| 79 | N_("show progress meter"), 1), |
| 80 | OPT_BIT_F(0, "all-progress", &progress, |
| 81 | N_("historical; same as --progress"), 1, |
| 82 | PARSE_OPT_HIDDEN), |
| 83 | OPT_NOOP_NOARG(0, "all-progress-implied"), |
| 84 | OPT_INTEGER(0, "version", &version, |
| 85 | N_("specify bundle format version")), |
| 86 | OPT_END() |
| 87 | }; |
| 88 | char *bundle_file; |
| 89 | int ret; |
| 90 | |
| 91 | argc = parse_options_cmd_bundle(argc, argv, prefix, |
| 92 | builtin_bundle_create_usage, options, &bundle_file); |
| 93 | /* bundle internals use argv[1] as further parameters */ |
| 94 | |
| 95 | if (!startup_info->have_repository) |
| 96 | die(_("Need a repository to create a bundle.")); |
| 97 | ret = !!create_bundle(the_repository, bundle_file, argc, argv, version, progress); |
| 98 | free(bundle_file); |
| 99 | return ret; |
| 100 | } |
| 101 | |
| 102 | /* |
| 103 | * Similar to read_bundle_header(), but handle "-" as stdin. |
| 104 | */ |
| 105 | static int open_bundle(const char *path, struct bundle_header *header, |
| 106 | const char **name) |
| 107 | { |
| 108 | if (!strcmp(path, "-")) { |
| 109 | if (name) |
| 110 | *name = "<stdin>"; |
| 111 | return read_bundle_header_fd(0, header, "<stdin>"); |
| 112 | } |
| 113 | |
| 114 | if (name) |
| 115 | *name = path; |
| 116 | return read_bundle_header(path, header); |
| 117 | } |
| 118 | |
| 119 | static int cmd_bundle_verify(int argc, const char **argv, const char *prefix, |
| 120 | struct repository *repo UNUSED) { |
| 121 | struct bundle_header header = BUNDLE_HEADER_INIT; |
| 122 | int bundle_fd = -1; |
| 123 | int quiet = 0; |
| 124 | int ret; |
| 125 | struct option options[] = { |
| 126 | OPT_BOOL('q', "quiet", &quiet, |
| 127 | N_("do not show bundle details")), |
| 128 | OPT_END() |
| 129 | }; |
| 130 | char *bundle_file; |
| 131 | const char *name; |
| 132 | |
| 133 | argc = parse_options_cmd_bundle(argc, argv, prefix, |
| 134 | builtin_bundle_verify_usage, options, &bundle_file); |
| 135 | /* bundle internals use argv[1] as further parameters */ |
| 136 | |
| 137 | if (!startup_info->have_repository) { |
| 138 | ret = error(_("need a repository to verify a bundle")); |
| 139 | goto cleanup; |
| 140 | } |
| 141 | |
| 142 | if ((bundle_fd = open_bundle(bundle_file, &header, &name)) < 0) { |
| 143 | ret = 1; |
| 144 | goto cleanup; |
| 145 | } |
| 146 | close(bundle_fd); |
| 147 | if (verify_bundle(the_repository, &header, |
| 148 | quiet ? VERIFY_BUNDLE_QUIET : VERIFY_BUNDLE_VERBOSE)) { |
| 149 | ret = 1; |
| 150 | goto cleanup; |
| 151 | } |
| 152 | |
| 153 | fprintf(stderr, _("%s is okay\n"), name); |
| 154 | ret = 0; |
| 155 | cleanup: |
| 156 | free(bundle_file); |
| 157 | bundle_header_release(&header); |
| 158 | return ret; |
| 159 | } |
| 160 | |
| 161 | static int cmd_bundle_list_heads(int argc, const char **argv, const char *prefix, |
| 162 | struct repository *repo UNUSED) { |
| 163 | struct bundle_header header = BUNDLE_HEADER_INIT; |
| 164 | int bundle_fd = -1; |
| 165 | int ret; |
| 166 | struct option options[] = { |
| 167 | OPT_END() |
| 168 | }; |
| 169 | char *bundle_file; |
| 170 | |
| 171 | argc = parse_options_cmd_bundle(argc, argv, prefix, |
| 172 | builtin_bundle_list_heads_usage, options, &bundle_file); |
| 173 | /* bundle internals use argv[1] as further parameters */ |
| 174 | |
| 175 | if ((bundle_fd = open_bundle(bundle_file, &header, NULL)) < 0) { |
| 176 | ret = 1; |
| 177 | goto cleanup; |
| 178 | } |
| 179 | close(bundle_fd); |
| 180 | ret = !!list_bundle_refs(&header, argc, argv); |
| 181 | cleanup: |
| 182 | free(bundle_file); |
| 183 | bundle_header_release(&header); |
| 184 | return ret; |
| 185 | } |
| 186 | |
| 187 | static int cmd_bundle_unbundle(int argc, const char **argv, const char *prefix, |
| 188 | struct repository *repo UNUSED) { |
| 189 | struct bundle_header header = BUNDLE_HEADER_INIT; |
| 190 | int bundle_fd = -1; |
| 191 | int ret; |
| 192 | int progress = isatty(2); |
| 193 | |
| 194 | struct option options[] = { |
| 195 | OPT_BOOL(0, "progress", &progress, |
| 196 | N_("show progress meter")), |
| 197 | OPT_END() |
| 198 | }; |
| 199 | char *bundle_file; |
| 200 | struct strvec extra_index_pack_args = STRVEC_INIT; |
| 201 | |
| 202 | argc = parse_options_cmd_bundle(argc, argv, prefix, |
| 203 | builtin_bundle_unbundle_usage, options, &bundle_file); |
| 204 | /* bundle internals use argv[1] as further parameters */ |
| 205 | |
| 206 | if (!startup_info->have_repository) |
| 207 | die(_("Need a repository to unbundle.")); |
| 208 | |
| 209 | if ((bundle_fd = open_bundle(bundle_file, &header, NULL)) < 0) { |
| 210 | ret = 1; |
| 211 | goto cleanup; |
| 212 | } |
| 213 | if (progress) |
| 214 | strvec_pushl(&extra_index_pack_args, "-v", "--progress-title", |
| 215 | _("Unbundling objects"), NULL); |
| 216 | ret = !!unbundle(the_repository, &header, bundle_fd, |
| 217 | &extra_index_pack_args, NULL) || |
| 218 | list_bundle_refs(&header, argc, argv); |
| 219 | bundle_header_release(&header); |
| 220 | |
| 221 | cleanup: |
| 222 | strvec_clear(&extra_index_pack_args); |
| 223 | free(bundle_file); |
| 224 | return ret; |
| 225 | } |
| 226 | |
| 227 | int cmd_bundle(int argc, |
| 228 | const char **argv, |
| 229 | const char *prefix, |
| 230 | struct repository *repo) |
| 231 | { |
| 232 | parse_opt_subcommand_fn *fn = NULL; |
| 233 | struct option options[] = { |
| 234 | OPT_SUBCOMMAND("create", &fn, cmd_bundle_create), |
| 235 | OPT_SUBCOMMAND("verify", &fn, cmd_bundle_verify), |
| 236 | OPT_SUBCOMMAND("list-heads", &fn, cmd_bundle_list_heads), |
| 237 | OPT_SUBCOMMAND("unbundle", &fn, cmd_bundle_unbundle), |
| 238 | OPT_END() |
| 239 | }; |
| 240 | |
| 241 | argc = parse_options(argc, argv, prefix, options, builtin_bundle_usage, |
| 242 | 0); |
| 243 | |
| 244 | packet_trace_identity("bundle"); |
| 245 | |
| 246 | return !!fn(argc, argv, prefix, repo); |
| 247 | } |