@matheusbsbs / matheus-bagliano / commits / c088543553
Fork 0

archive: move parameter parsing code to archive.c

write_archive() in archive.c is the only callsite for the command line parsing functions located in builtin-archive.c. Move them to the place where they are used, un-export them and make them static, as hinted at by Stephan. Cc: Stephan Beyer <s-beyer@gmx.net> Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Rene Scharfe committed Jul 25, 2008 at 12:41 UTC c0885435537e4b93709d2bf39ce36454186057a1
3 files changed +137 -145
archive.c
+137
@@ -1,8 +1,19 @@
1 #include "cache.h"
2 #include "commit.h"
3 +#include "tree-walk.h"
4 #include "attr.h"
5 #include "archive.h"
6
7 +static const char archive_usage[] = \
8 +"git archive --format=<fmt> [--prefix=<prefix>/] [--verbose] [<extra>] <tree-ish> [path...]";
9 +
10 +#define USES_ZLIB_COMPRESSION 1
11 +
12 +const struct archiver archivers[] = {
13 + { "tar", write_tar_archive },
14 + { "zip", write_zip_archive, USES_ZLIB_COMPRESSION },
15 +};
16 +
17 static void format_subst(const struct commit *commit,
18 const char *src, size_t len,
19 struct strbuf *buf)
@@ -156,6 +167,132 @@ int write_archive_entries(struct archiver_args *args,
167 return err;
168 }
169
170 +static const struct archiver *lookup_archiver(const char *name)
171 +{
172 + int i;
173 +
174 + for (i = 0; i < ARRAY_SIZE(archivers); i++) {
175 + if (!strcmp(name, archivers[i].name))
176 + return &archivers[i];
177 + }
178 + return NULL;
179 +}
180 +
181 +static void parse_pathspec_arg(const char **pathspec,
182 + struct archiver_args *ar_args)
183 +{
184 + ar_args->pathspec = get_pathspec(ar_args->base, pathspec);
185 +}
186 +
187 +static void parse_treeish_arg(const char **argv,
188 + struct archiver_args *ar_args, const char *prefix)
189 +{
190 + const char *name = argv[0];
191 + const unsigned char *commit_sha1;
192 + time_t archive_time;
193 + struct tree *tree;
194 + const struct commit *commit;
195 + unsigned char sha1[20];
196 +
197 + if (get_sha1(name, sha1))
198 + die("Not a valid object name");
199 +
200 + commit = lookup_commit_reference_gently(sha1, 1);
201 + if (commit) {
202 + commit_sha1 = commit->object.sha1;
203 + archive_time = commit->date;
204 + } else {
205 + commit_sha1 = NULL;
206 + archive_time = time(NULL);
207 + }
208 +
209 + tree = parse_tree_indirect(sha1);
210 + if (tree == NULL)
211 + die("not a tree object");
212 +
213 + if (prefix) {
214 + unsigned char tree_sha1[20];
215 + unsigned int mode;
216 + int err;
217 +
218 + err = get_tree_entry(tree->object.sha1, prefix,
219 + tree_sha1, &mode);
220 + if (err || !S_ISDIR(mode))
221 + die("current working directory is untracked");
222 +
223 + tree = parse_tree_indirect(tree_sha1);
224 + }
225 + ar_args->tree = tree;
226 + ar_args->commit_sha1 = commit_sha1;
227 + ar_args->commit = commit;
228 + ar_args->time = archive_time;
229 +}
230 +
231 +static int parse_archive_args(int argc, const char **argv,
232 + const struct archiver **ar, struct archiver_args *args)
233 +{
234 + const char *format = "tar";
235 + const char *base = "";
236 + int compression_level = -1;
237 + int verbose = 0;
238 + int i;
239 +
240 + for (i = 1; i < argc; i++) {
241 + const char *arg = argv[i];
242 +
243 + if (!strcmp(arg, "--list") || !strcmp(arg, "-l")) {
244 + for (i = 0; i < ARRAY_SIZE(archivers); i++)
245 + printf("%s\n", archivers[i].name);
246 + exit(0);
247 + }
248 + if (!strcmp(arg, "--verbose") || !strcmp(arg, "-v")) {
249 + verbose = 1;
250 + continue;
251 + }
252 + if (!prefixcmp(arg, "--format=")) {
253 + format = arg + 9;
254 + continue;
255 + }
256 + if (!prefixcmp(arg, "--prefix=")) {
257 + base = arg + 9;
258 + continue;
259 + }
260 + if (!strcmp(arg, "--")) {
261 + i++;
262 + break;
263 + }
264 + if (arg[0] == '-' && isdigit(arg[1]) && arg[2] == '\0') {
265 + compression_level = arg[1] - '0';
266 + continue;
267 + }
268 + if (arg[0] == '-')
269 + die("Unknown argument: %s", arg);
270 + break;
271 + }
272 +
273 + /* We need at least one parameter -- tree-ish */
274 + if (argc - 1 < i)
275 + usage(archive_usage);
276 + *ar = lookup_archiver(format);
277 + if (!*ar)
278 + die("Unknown archive format '%s'", format);
279 +
280 + args->compression_level = Z_DEFAULT_COMPRESSION;
281 + if (compression_level != -1) {
282 + if ((*ar)->flags & USES_ZLIB_COMPRESSION)
283 + args->compression_level = compression_level;
284 + else {
285 + die("Argument not supported for format '%s': -%d",
286 + format, compression_level);
287 + }
288 + }
289 + args->verbose = verbose;
290 + args->base = base;
291 + args->baselen = strlen(base);
292 +
293 + return i;
294 +}
295 +
296 int write_archive(int argc, const char **argv, const char *prefix,
297 int setup_prefix)
298 {
archive.h
-8
@@ -26,14 +26,6 @@ struct archiver {
26 unsigned int flags;
27 };
28
29 -extern int parse_archive_args(int argc, const char **argv, const struct archiver **ar, struct archiver_args *args);
30 -
31 -extern void parse_treeish_arg(const char **treeish,
32 - struct archiver_args *ar_args,
33 - const char *prefix);
34 -
35 -extern void parse_pathspec_arg(const char **pathspec,
36 - struct archiver_args *args);
29 /*
30 * Archive-format specific backends.
31 */
builtin-archive.c
-137
@@ -5,21 +5,9 @@
5 #include "cache.h"
6 #include "builtin.h"
7 #include "archive.h"
8 -#include "commit.h"
9 -#include "tree-walk.h"
8 #include "pkt-line.h"
9 #include "sideband.h"
10
13 -static const char archive_usage[] = \
14 -"git archive --format=<fmt> [--prefix=<prefix>/] [--verbose] [<extra>] <tree-ish> [path...]";
15 -
16 -#define USES_ZLIB_COMPRESSION 1
17 -
18 -const struct archiver archivers[] = {
19 - { "tar", write_tar_archive },
20 - { "zip", write_zip_archive, USES_ZLIB_COMPRESSION },
21 -};
22 -
11 static int run_remote_archiver(const char *remote, int argc,
12 const char **argv)
13 {
@@ -74,131 +62,6 @@ static int run_remote_archiver(const char *remote, int argc,
62 return !!rv;
63 }
64
77 -static const struct archiver *lookup_archiver(const char *name)
78 -{
79 - int i;
80 -
81 - for (i = 0; i < ARRAY_SIZE(archivers); i++) {
82 - if (!strcmp(name, archivers[i].name))
83 - return &archivers[i];
84 - }
85 - return NULL;
86 -}
87 -
88 -void parse_pathspec_arg(const char **pathspec, struct archiver_args *ar_args)
89 -{
90 - ar_args->pathspec = get_pathspec(ar_args->base, pathspec);
91 -}
92 -
93 -void parse_treeish_arg(const char **argv, struct archiver_args *ar_args,
94 - const char *prefix)
95 -{
96 - const char *name = argv[0];
97 - const unsigned char *commit_sha1;
98 - time_t archive_time;
99 - struct tree *tree;
100 - const struct commit *commit;
101 - unsigned char sha1[20];
102 -
103 - if (get_sha1(name, sha1))
104 - die("Not a valid object name");
105 -
106 - commit = lookup_commit_reference_gently(sha1, 1);
107 - if (commit) {
108 - commit_sha1 = commit->object.sha1;
109 - archive_time = commit->date;
110 - } else {
111 - commit_sha1 = NULL;
112 - archive_time = time(NULL);
113 - }
114 -
115 - tree = parse_tree_indirect(sha1);
116 - if (tree == NULL)
117 - die("not a tree object");
118 -
119 - if (prefix) {
120 - unsigned char tree_sha1[20];
121 - unsigned int mode;
122 - int err;
123 -
124 - err = get_tree_entry(tree->object.sha1, prefix,
125 - tree_sha1, &mode);
126 - if (err || !S_ISDIR(mode))
127 - die("current working directory is untracked");
128 -
129 - tree = parse_tree_indirect(tree_sha1);
130 - }
131 - ar_args->tree = tree;
132 - ar_args->commit_sha1 = commit_sha1;
133 - ar_args->commit = commit;
134 - ar_args->time = archive_time;
135 -}
136 -
137 -int parse_archive_args(int argc, const char **argv, const struct archiver **ar,
138 - struct archiver_args *args)
139 -{
140 - const char *format = "tar";
141 - const char *base = "";
142 - int compression_level = -1;
143 - int verbose = 0;
144 - int i;
145 -
146 - for (i = 1; i < argc; i++) {
147 - const char *arg = argv[i];
148 -
149 - if (!strcmp(arg, "--list") || !strcmp(arg, "-l")) {
150 - for (i = 0; i < ARRAY_SIZE(archivers); i++)
151 - printf("%s\n", archivers[i].name);
152 - exit(0);
153 - }
154 - if (!strcmp(arg, "--verbose") || !strcmp(arg, "-v")) {
155 - verbose = 1;
156 - continue;
157 - }
158 - if (!prefixcmp(arg, "--format=")) {
159 - format = arg + 9;
160 - continue;
161 - }
162 - if (!prefixcmp(arg, "--prefix=")) {
163 - base = arg + 9;
164 - continue;
165 - }
166 - if (!strcmp(arg, "--")) {
167 - i++;
168 - break;
169 - }
170 - if (arg[0] == '-' && isdigit(arg[1]) && arg[2] == '\0') {
171 - compression_level = arg[1] - '0';
172 - continue;
173 - }
174 - if (arg[0] == '-')
175 - die("Unknown argument: %s", arg);
176 - break;
177 - }
178 -
179 - /* We need at least one parameter -- tree-ish */
180 - if (argc - 1 < i)
181 - usage(archive_usage);
182 - *ar = lookup_archiver(format);
183 - if (!*ar)
184 - die("Unknown archive format '%s'", format);
185 -
186 - args->compression_level = Z_DEFAULT_COMPRESSION;
187 - if (compression_level != -1) {
188 - if ((*ar)->flags & USES_ZLIB_COMPRESSION)
189 - args->compression_level = compression_level;
190 - else {
191 - die("Argument not supported for format '%s': -%d",
192 - format, compression_level);
193 - }
194 - }
195 - args->verbose = verbose;
196 - args->base = base;
197 - args->baselen = strlen(base);
198 -
199 - return i;
200 -}
201 -
65 static const char *extract_remote_arg(int *ac, const char **av)
66 {
67 int ix, iy, cnt = *ac;