merge: don't translate literal commands
[git] / builtin / bundle.c
1 #include "builtin.h"
2 #include "strvec.h"
3 #include "parse-options.h"
4 #include "cache.h"
5 #include "bundle.h"
6
7 /*
8  * Basic handler for bundle files to connect repositories via sneakernet.
9  * Invocation must include action.
10  * This function can create a bundle or provide information on an existing
11  * bundle supporting "fetch", "pull", and "ls-remote".
12  */
13
14 static const char * const builtin_bundle_usage[] = {
15   N_("git bundle create [<options>] <file> <git-rev-list args>"),
16   N_("git bundle verify [<options>] <file>"),
17   N_("git bundle list-heads <file> [<refname>...]"),
18   N_("git bundle unbundle <file> [<refname>...]"),
19   NULL
20 };
21
22 static const char * const builtin_bundle_create_usage[] = {
23   N_("git bundle create [<options>] <file> <git-rev-list args>"),
24   NULL
25 };
26
27 static const char * const builtin_bundle_verify_usage[] = {
28   N_("git bundle verify [<options>] <file>"),
29   NULL
30 };
31
32 static const char * const builtin_bundle_list_heads_usage[] = {
33   N_("git bundle list-heads <file> [<refname>...]"),
34   NULL
35 };
36
37 static const char * const builtin_bundle_unbundle_usage[] = {
38   N_("git bundle unbundle <file> [<refname>...]"),
39   NULL
40 };
41
42 static int verbose;
43
44 static int parse_options_cmd_bundle(int argc,
45                 const char **argv,
46                 const char* prefix,
47                 const char * const usagestr[],
48                 const struct option options[],
49                 const char **bundle_file) {
50         int newargc;
51         newargc = parse_options(argc, argv, NULL, options, usagestr,
52                              PARSE_OPT_STOP_AT_NON_OPTION);
53         if (argc < 1)
54                 usage_with_options(usagestr, options);
55         *bundle_file = prefix_filename(prefix, argv[0]);
56         return newargc;
57 }
58
59 static int cmd_bundle_create(int argc, const char **argv, const char *prefix) {
60         int all_progress_implied = 0;
61         int progress = isatty(STDERR_FILENO);
62         struct strvec pack_opts;
63         int version = -1;
64
65         struct option options[] = {
66                 OPT_SET_INT('q', "quiet", &progress,
67                             N_("do not show progress meter"), 0),
68                 OPT_SET_INT(0, "progress", &progress,
69                             N_("show progress meter"), 1),
70                 OPT_SET_INT(0, "all-progress", &progress,
71                             N_("show progress meter during object writing phase"), 2),
72                 OPT_BOOL(0, "all-progress-implied",
73                          &all_progress_implied,
74                          N_("similar to --all-progress when progress meter is shown")),
75                 OPT_INTEGER(0, "version", &version,
76                             N_("specify bundle format version")),
77                 OPT_END()
78         };
79         const char* bundle_file;
80
81         argc = parse_options_cmd_bundle(argc, argv, prefix,
82                         builtin_bundle_create_usage, options, &bundle_file);
83         /* bundle internals use argv[1] as further parameters */
84
85         strvec_init(&pack_opts);
86         if (progress == 0)
87                 strvec_push(&pack_opts, "--quiet");
88         else if (progress == 1)
89                 strvec_push(&pack_opts, "--progress");
90         else if (progress == 2)
91                 strvec_push(&pack_opts, "--all-progress");
92         if (progress && all_progress_implied)
93                 strvec_push(&pack_opts, "--all-progress-implied");
94
95         if (!startup_info->have_repository)
96                 die(_("Need a repository to create a bundle."));
97         return !!create_bundle(the_repository, bundle_file, argc, argv, &pack_opts, version);
98 }
99
100 static int cmd_bundle_verify(int argc, const char **argv, const char *prefix) {
101         struct bundle_header header;
102         int bundle_fd = -1;
103         int quiet = 0;
104
105         struct option options[] = {
106                 OPT_BOOL('q', "quiet", &quiet,
107                             N_("do not show bundle details")),
108                 OPT_END()
109         };
110         const char* bundle_file;
111
112         argc = parse_options_cmd_bundle(argc, argv, prefix,
113                         builtin_bundle_verify_usage, options, &bundle_file);
114         /* bundle internals use argv[1] as further parameters */
115
116         memset(&header, 0, sizeof(header));
117         if ((bundle_fd = read_bundle_header(bundle_file, &header)) < 0)
118                 return 1;
119         close(bundle_fd);
120         if (verify_bundle(the_repository, &header, !quiet))
121                 return 1;
122         fprintf(stderr, _("%s is okay\n"), bundle_file);
123         return 0;
124 }
125
126 static int cmd_bundle_list_heads(int argc, const char **argv, const char *prefix) {
127         struct bundle_header header;
128         int bundle_fd = -1;
129
130         struct option options[] = {
131                 OPT_END()
132         };
133         const char* bundle_file;
134
135         argc = parse_options_cmd_bundle(argc, argv, prefix,
136                         builtin_bundle_list_heads_usage, options, &bundle_file);
137         /* bundle internals use argv[1] as further parameters */
138
139         memset(&header, 0, sizeof(header));
140         if ((bundle_fd = read_bundle_header(bundle_file, &header)) < 0)
141                 return 1;
142         close(bundle_fd);
143         return !!list_bundle_refs(&header, argc, argv);
144 }
145
146 static int cmd_bundle_unbundle(int argc, const char **argv, const char *prefix) {
147         struct bundle_header header;
148         int bundle_fd = -1;
149
150         struct option options[] = {
151                 OPT_END()
152         };
153         const char* bundle_file;
154
155         argc = parse_options_cmd_bundle(argc, argv, prefix,
156                         builtin_bundle_unbundle_usage, options, &bundle_file);
157         /* bundle internals use argv[1] as further parameters */
158
159         memset(&header, 0, sizeof(header));
160         if ((bundle_fd = read_bundle_header(bundle_file, &header)) < 0)
161                 return 1;
162         if (!startup_info->have_repository)
163                 die(_("Need a repository to unbundle."));
164         return !!unbundle(the_repository, &header, bundle_fd, 0) ||
165                 list_bundle_refs(&header, argc, argv);
166 }
167
168 int cmd_bundle(int argc, const char **argv, const char *prefix)
169 {
170         struct option options[] = {
171                 OPT__VERBOSE(&verbose, N_("be verbose; must be placed before a subcommand")),
172                 OPT_END()
173         };
174         int result;
175
176         argc = parse_options(argc, argv, prefix, options, builtin_bundle_usage,
177                 PARSE_OPT_STOP_AT_NON_OPTION);
178
179         packet_trace_identity("bundle");
180
181         if (argc < 2)
182                 usage_with_options(builtin_bundle_usage, options);
183
184         else if (!strcmp(argv[0], "create"))
185                 result = cmd_bundle_create(argc, argv, prefix);
186         else if (!strcmp(argv[0], "verify"))
187                 result = cmd_bundle_verify(argc, argv, prefix);
188         else if (!strcmp(argv[0], "list-heads"))
189                 result = cmd_bundle_list_heads(argc, argv, prefix);
190         else if (!strcmp(argv[0], "unbundle"))
191                 result = cmd_bundle_unbundle(argc, argv, prefix);
192         else {
193                 error(_("Unknown subcommand: %s"), argv[0]);
194                 usage_with_options(builtin_bundle_usage, options);
195         }
196         return result ? 1 : 0;
197 }