blob: 3ad11dc5d0548727da0f27e04563a8d947207f74 [file] [log] [blame]
Peter Hagervallbaffc0e2007-07-15 01:14:45 +02001#include "builtin.h"
Elijah Newren0b027f62023-03-21 06:25:58 +00002#include "abspath.h"
Elijah Newrenf394e092023-03-21 06:25:54 +00003#include "gettext.h"
Elijah Newrene38da482023-03-21 06:26:05 +00004#include "setup.h"
Jeff Kingdbbcd442020-07-28 16:23:39 -04005#include "strvec.h"
Robin H. Johnson73c32532019-11-10 12:41:24 -08006#include "parse-options.h"
Elijah Newrenb3886332023-04-22 20:17:14 +00007#include "pkt-line.h"
Elijah Newrend1cbe1e2023-04-22 20:17:20 +00008#include "repository.h"
Johannes Schindelin30415d52007-09-10 23:03:15 -04009#include "bundle.h"
Johannes Schindelin2e0afaf2007-02-22 01:59:14 +010010
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
Heikki Orsila34baebc2008-08-30 14:12:53 +030015 * bundle supporting "fetch", "pull", and "ls-remote".
Johannes Schindelin2e0afaf2007-02-22 01:59:14 +010016 */
17
Ævar Arnfjörð Bjarmasonf587d162022-10-13 17:38:58 +020018#define BUILTIN_BUNDLE_CREATE_USAGE \
Jeff King8b955212023-03-04 05:55:13 -050019 N_("git bundle create [-q | --quiet | --progress]\n" \
Ævar Arnfjörð Bjarmasona5748672022-10-13 17:39:16 +020020 " [--version=<version>] <file> <git-rev-list-args>")
Ævar Arnfjörð Bjarmasonf587d162022-10-13 17:38:58 +020021#define BUILTIN_BUNDLE_VERIFY_USAGE \
Ævar Arnfjörð Bjarmasona5748672022-10-13 17:39:16 +020022 N_("git bundle verify [-q | --quiet] <file>")
Ævar Arnfjörð Bjarmasonf587d162022-10-13 17:38:58 +020023#define BUILTIN_BUNDLE_LIST_HEADS_USAGE \
24 N_("git bundle list-heads <file> [<refname>...]")
25#define BUILTIN_BUNDLE_UNBUNDLE_USAGE \
Ævar Arnfjörð Bjarmasona5748672022-10-13 17:39:16 +020026 N_("git bundle unbundle [--progress] <file> [<refname>...]")
Ævar Arnfjörð Bjarmasonf587d162022-10-13 17:38:58 +020027
28static 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,
Robin H. Johnson73c32532019-11-10 12:41:24 -080034};
35
36static const char * const builtin_bundle_create_usage[] = {
Ævar Arnfjörð Bjarmasonf587d162022-10-13 17:38:58 +020037 BUILTIN_BUNDLE_CREATE_USAGE,
Ævar Arnfjörð Bjarmason968a04e2022-10-13 17:38:57 +020038 NULL
Robin H. Johnson73c32532019-11-10 12:41:24 -080039};
40
41static const char * const builtin_bundle_verify_usage[] = {
Ævar Arnfjörð Bjarmasonf587d162022-10-13 17:38:58 +020042 BUILTIN_BUNDLE_VERIFY_USAGE,
Ævar Arnfjörð Bjarmason968a04e2022-10-13 17:38:57 +020043 NULL
Robin H. Johnson73c32532019-11-10 12:41:24 -080044};
45
46static const char * const builtin_bundle_list_heads_usage[] = {
Ævar Arnfjörð Bjarmasonf587d162022-10-13 17:38:58 +020047 BUILTIN_BUNDLE_LIST_HEADS_USAGE,
Ævar Arnfjörð Bjarmason968a04e2022-10-13 17:38:57 +020048 NULL
Robin H. Johnson73c32532019-11-10 12:41:24 -080049};
50
51static const char * const builtin_bundle_unbundle_usage[] = {
Ævar Arnfjörð Bjarmasonf587d162022-10-13 17:38:58 +020052 BUILTIN_BUNDLE_UNBUNDLE_USAGE,
Ævar Arnfjörð Bjarmason968a04e2022-10-13 17:38:57 +020053 NULL
Robin H. Johnson73c32532019-11-10 12:41:24 -080054};
55
Robin H. Johnson73c32532019-11-10 12:41:24 -080056static 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[],
Ævar Arnfjörð Bjarmasondb6bfb92021-07-02 11:57:30 +020061 char **bundle_file) {
Ævar Arnfjörð Bjarmasone778ecb2022-12-27 19:39:09 +010062 argc = parse_options(argc, argv, NULL, options, usagestr,
Robin H. Johnson73c32532019-11-10 12:41:24 -080063 PARSE_OPT_STOP_AT_NON_OPTION);
Ævar Arnfjörð Bjarmasone778ecb2022-12-27 19:39:09 +010064 if (!argc)
Ævar Arnfjörð Bjarmason6d5e9e52022-12-27 19:39:10 +010065 usage_msg_opt(_("need a <file> argument"), usagestr, options);
Junio C Hamanoa8bfa992023-03-04 05:27:56 -050066 *bundle_file = prefix_filename_except_for_dash(prefix, argv[0]);
Ævar Arnfjörð Bjarmasone778ecb2022-12-27 19:39:09 +010067 return argc;
Robin H. Johnson73c32532019-11-10 12:41:24 -080068}
69
70static int cmd_bundle_create(int argc, const char **argv, const char *prefix) {
René Scharfed089a062023-07-29 22:40:27 +020071 struct strvec pack_opts = STRVEC_INIT;
brian m. carlsonc5aecfc2020-07-29 23:14:20 +000072 int version = -1;
Ævar Arnfjörð Bjarmasondb6bfb92021-07-02 11:57:30 +020073 int ret;
Robin H. Johnson73c32532019-11-10 12:41:24 -080074 struct option options[] = {
René Scharfed089a062023-07-29 22:40:27 +020075 OPT_PASSTHRU_ARGV('q', "quiet", &pack_opts, NULL,
76 N_("do not show progress meter"),
77 PARSE_OPT_NOARG),
78 OPT_PASSTHRU_ARGV(0, "progress", &pack_opts, NULL,
79 N_("show progress meter"),
80 PARSE_OPT_NOARG),
81 OPT_PASSTHRU_ARGV(0, "all-progress", &pack_opts, NULL,
82 N_("historical; same as --progress"),
83 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN),
84 OPT_PASSTHRU_ARGV(0, "all-progress-implied", &pack_opts, NULL,
85 N_("historical; does nothing"),
86 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN),
brian m. carlsonc5aecfc2020-07-29 23:14:20 +000087 OPT_INTEGER(0, "version", &version,
88 N_("specify bundle format version")),
Robin H. Johnson73c32532019-11-10 12:41:24 -080089 OPT_END()
90 };
Ævar Arnfjörð Bjarmasondb6bfb92021-07-02 11:57:30 +020091 char *bundle_file;
Robin H. Johnson73c32532019-11-10 12:41:24 -080092
René Scharfed089a062023-07-29 22:40:27 +020093 if (isatty(STDERR_FILENO))
94 strvec_push(&pack_opts, "--progress");
95 strvec_push(&pack_opts, "--all-progress-implied");
96
Robin H. Johnson73c32532019-11-10 12:41:24 -080097 argc = parse_options_cmd_bundle(argc, argv, prefix,
98 builtin_bundle_create_usage, options, &bundle_file);
99 /* bundle internals use argv[1] as further parameters */
100
101 if (!startup_info->have_repository)
102 die(_("Need a repository to create a bundle."));
Ævar Arnfjörð Bjarmasondb6bfb92021-07-02 11:57:30 +0200103 ret = !!create_bundle(the_repository, bundle_file, argc, argv, &pack_opts, version);
Ævar Arnfjörð Bjarmasonbf67dd82022-03-04 19:32:09 +0100104 strvec_clear(&pack_opts);
Ævar Arnfjörð Bjarmasondb6bfb92021-07-02 11:57:30 +0200105 free(bundle_file);
106 return ret;
Robin H. Johnson73c32532019-11-10 12:41:24 -0800107}
108
Jeff Kingbf8b1e02023-03-04 05:26:14 -0500109/*
110 * Similar to read_bundle_header(), but handle "-" as stdin.
111 */
112static int open_bundle(const char *path, struct bundle_header *header,
113 const char **name)
114{
115 if (!strcmp(path, "-")) {
116 if (name)
117 *name = "<stdin>";
118 return read_bundle_header_fd(0, header, "<stdin>");
119 }
120
121 if (name)
122 *name = path;
123 return read_bundle_header(path, header);
124}
125
Robin H. Johnson73c32532019-11-10 12:41:24 -0800126static int cmd_bundle_verify(int argc, const char **argv, const char *prefix) {
Ævar Arnfjörð Bjarmason10b635b2021-07-02 11:57:32 +0200127 struct bundle_header header = BUNDLE_HEADER_INIT;
Robin H. Johnson73c32532019-11-10 12:41:24 -0800128 int bundle_fd = -1;
Robin H. Johnsone0eba642019-11-10 12:41:26 -0800129 int quiet = 0;
Ævar Arnfjörð Bjarmasondb6bfb92021-07-02 11:57:30 +0200130 int ret;
Robin H. Johnson73c32532019-11-10 12:41:24 -0800131 struct option options[] = {
Robin H. Johnsone0eba642019-11-10 12:41:26 -0800132 OPT_BOOL('q', "quiet", &quiet,
133 N_("do not show bundle details")),
Robin H. Johnson73c32532019-11-10 12:41:24 -0800134 OPT_END()
135 };
Ævar Arnfjörð Bjarmasondb6bfb92021-07-02 11:57:30 +0200136 char *bundle_file;
Jeff Kingbf8b1e02023-03-04 05:26:14 -0500137 const char *name;
Robin H. Johnson73c32532019-11-10 12:41:24 -0800138
139 argc = parse_options_cmd_bundle(argc, argv, prefix,
140 builtin_bundle_verify_usage, options, &bundle_file);
141 /* bundle internals use argv[1] as further parameters */
142
Jeff Kingbf8b1e02023-03-04 05:26:14 -0500143 if ((bundle_fd = open_bundle(bundle_file, &header, &name)) < 0) {
Ævar Arnfjörð Bjarmasondb6bfb92021-07-02 11:57:30 +0200144 ret = 1;
145 goto cleanup;
146 }
Robin H. Johnson73c32532019-11-10 12:41:24 -0800147 close(bundle_fd);
Derrick Stolee89bd7fe2022-10-12 12:52:37 +0000148 if (verify_bundle(the_repository, &header,
Derrick Stolee70334fc2022-10-12 12:52:38 +0000149 quiet ? VERIFY_BUNDLE_QUIET : VERIFY_BUNDLE_VERBOSE)) {
Ævar Arnfjörð Bjarmasondb6bfb92021-07-02 11:57:30 +0200150 ret = 1;
151 goto cleanup;
152 }
153
Jeff Kingbf8b1e02023-03-04 05:26:14 -0500154 fprintf(stderr, _("%s is okay\n"), name);
Ævar Arnfjörð Bjarmasondb6bfb92021-07-02 11:57:30 +0200155 ret = 0;
156cleanup:
157 free(bundle_file);
Ævar Arnfjörð Bjarmason10b635b2021-07-02 11:57:32 +0200158 bundle_header_release(&header);
Ævar Arnfjörð Bjarmasondb6bfb92021-07-02 11:57:30 +0200159 return ret;
Robin H. Johnson73c32532019-11-10 12:41:24 -0800160}
161
162static int cmd_bundle_list_heads(int argc, const char **argv, const char *prefix) {
Ævar Arnfjörð Bjarmason10b635b2021-07-02 11:57:32 +0200163 struct bundle_header header = BUNDLE_HEADER_INIT;
Robin H. Johnson73c32532019-11-10 12:41:24 -0800164 int bundle_fd = -1;
Ævar Arnfjörð Bjarmasondb6bfb92021-07-02 11:57:30 +0200165 int ret;
Robin H. Johnson73c32532019-11-10 12:41:24 -0800166 struct option options[] = {
167 OPT_END()
168 };
Ævar Arnfjörð Bjarmasondb6bfb92021-07-02 11:57:30 +0200169 char *bundle_file;
Robin H. Johnson73c32532019-11-10 12:41:24 -0800170
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
Jeff Kingbf8b1e02023-03-04 05:26:14 -0500175 if ((bundle_fd = open_bundle(bundle_file, &header, NULL)) < 0) {
Ævar Arnfjörð Bjarmasondb6bfb92021-07-02 11:57:30 +0200176 ret = 1;
177 goto cleanup;
178 }
Robin H. Johnson73c32532019-11-10 12:41:24 -0800179 close(bundle_fd);
Ævar Arnfjörð Bjarmasondb6bfb92021-07-02 11:57:30 +0200180 ret = !!list_bundle_refs(&header, argc, argv);
181cleanup:
182 free(bundle_file);
Ævar Arnfjörð Bjarmason10b635b2021-07-02 11:57:32 +0200183 bundle_header_release(&header);
Ævar Arnfjörð Bjarmasondb6bfb92021-07-02 11:57:30 +0200184 return ret;
Robin H. Johnson73c32532019-11-10 12:41:24 -0800185}
186
187static int cmd_bundle_unbundle(int argc, const char **argv, const char *prefix) {
Ævar Arnfjörð Bjarmason10b635b2021-07-02 11:57:32 +0200188 struct bundle_header header = BUNDLE_HEADER_INIT;
Robin H. Johnson73c32532019-11-10 12:41:24 -0800189 int bundle_fd = -1;
Ævar Arnfjörð Bjarmasondb6bfb92021-07-02 11:57:30 +0200190 int ret;
Ævar Arnfjörð Bjarmasond941cc42021-09-05 09:34:45 +0200191 int progress = isatty(2);
192
Robin H. Johnson73c32532019-11-10 12:41:24 -0800193 struct option options[] = {
Ævar Arnfjörð Bjarmasond941cc42021-09-05 09:34:45 +0200194 OPT_BOOL(0, "progress", &progress,
195 N_("show progress meter")),
Robin H. Johnson73c32532019-11-10 12:41:24 -0800196 OPT_END()
197 };
Ævar Arnfjörð Bjarmasondb6bfb92021-07-02 11:57:30 +0200198 char *bundle_file;
Ævar Arnfjörð Bjarmason73660962021-09-05 09:34:43 +0200199 struct strvec extra_index_pack_args = STRVEC_INIT;
Robin H. Johnson73c32532019-11-10 12:41:24 -0800200
201 argc = parse_options_cmd_bundle(argc, argv, prefix,
202 builtin_bundle_unbundle_usage, options, &bundle_file);
203 /* bundle internals use argv[1] as further parameters */
204
Jeff Kingbf8b1e02023-03-04 05:26:14 -0500205 if ((bundle_fd = open_bundle(bundle_file, &header, NULL)) < 0) {
Ævar Arnfjörð Bjarmasondb6bfb92021-07-02 11:57:30 +0200206 ret = 1;
207 goto cleanup;
208 }
Robin H. Johnson73c32532019-11-10 12:41:24 -0800209 if (!startup_info->have_repository)
210 die(_("Need a repository to unbundle."));
Ævar Arnfjörð Bjarmasond941cc42021-09-05 09:34:45 +0200211 if (progress)
212 strvec_pushl(&extra_index_pack_args, "-v", "--progress-title",
213 _("Unbundling objects"), NULL);
Ævar Arnfjörð Bjarmason73660962021-09-05 09:34:43 +0200214 ret = !!unbundle(the_repository, &header, bundle_fd,
Derrick Stolee89bd7fe2022-10-12 12:52:37 +0000215 &extra_index_pack_args, 0) ||
Robin H. Johnson73c32532019-11-10 12:41:24 -0800216 list_bundle_refs(&header, argc, argv);
Ævar Arnfjörð Bjarmason10b635b2021-07-02 11:57:32 +0200217 bundle_header_release(&header);
Ævar Arnfjörð Bjarmasondb6bfb92021-07-02 11:57:30 +0200218cleanup:
219 free(bundle_file);
220 return ret;
Robin H. Johnson73c32532019-11-10 12:41:24 -0800221}
Johannes Schindelin2e0afaf2007-02-22 01:59:14 +0100222
Johannes Schindelin2e0afaf2007-02-22 01:59:14 +0100223int cmd_bundle(int argc, const char **argv, const char *prefix)
224{
SZEDER Gáboraef7d752022-08-19 18:04:01 +0200225 parse_opt_subcommand_fn *fn = NULL;
Robin H. Johnson73c32532019-11-10 12:41:24 -0800226 struct option options[] = {
SZEDER Gáboraef7d752022-08-19 18:04:01 +0200227 OPT_SUBCOMMAND("create", &fn, cmd_bundle_create),
228 OPT_SUBCOMMAND("verify", &fn, cmd_bundle_verify),
229 OPT_SUBCOMMAND("list-heads", &fn, cmd_bundle_list_heads),
230 OPT_SUBCOMMAND("unbundle", &fn, cmd_bundle_unbundle),
Robin H. Johnson73c32532019-11-10 12:41:24 -0800231 OPT_END()
232 };
Johannes Schindelin2e0afaf2007-02-22 01:59:14 +0100233
Robin H. Johnson73c32532019-11-10 12:41:24 -0800234 argc = parse_options(argc, argv, prefix, options, builtin_bundle_usage,
SZEDER Gáboraef7d752022-08-19 18:04:01 +0200235 0);
Johannes Schindelin2e0afaf2007-02-22 01:59:14 +0100236
Robin H. Johnson73c32532019-11-10 12:41:24 -0800237 packet_trace_identity("bundle");
Johannes Schindelin2e0afaf2007-02-22 01:59:14 +0100238
SZEDER Gáboraef7d752022-08-19 18:04:01 +0200239 return !!fn(argc, argv, prefix);
Johannes Schindelin2e0afaf2007-02-22 01:59:14 +0100240}