| 1 | #define USE_THE_REPOSITORY_VARIABLE |
| 2 | #define DISABLE_SIGN_COMPARE_WARNINGS |
| 3 | |
| 4 | #include "git-compat-util.h" |
| 5 | #include "abspath.h" |
| 6 | #include "config.h" |
| 7 | #include "convert.h" |
| 8 | #include "environment.h" |
| 9 | #include "gettext.h" |
| 10 | #include "git-zlib.h" |
| 11 | #include "hex.h" |
| 12 | #include "object-name.h" |
| 13 | #include "path.h" |
| 14 | #include "pretty.h" |
| 15 | #include "setup.h" |
| 16 | #include "refs.h" |
| 17 | #include "odb.h" |
| 18 | #include "commit.h" |
| 19 | #include "tree.h" |
| 20 | #include "tree-walk.h" |
| 21 | #include "attr.h" |
| 22 | #include "archive.h" |
| 23 | #include "parse-options.h" |
| 24 | #include "unpack-trees.h" |
| 25 | #include "quote.h" |
| 26 | |
| 27 | static char const * const archive_usage[] = { |
| 28 | N_("git archive [<options>] <tree-ish> [<path>...]"), |
| 29 | "git archive --list", |
| 30 | N_("git archive --remote <repo> [--exec <cmd>] [<options>] <tree-ish> [<path>...]"), |
| 31 | N_("git archive --remote <repo> [--exec <cmd>] --list"), |
| 32 | NULL |
| 33 | }; |
| 34 | |
| 35 | static const struct archiver **archivers; |
| 36 | static int nr_archivers; |
| 37 | static int alloc_archivers; |
| 38 | static int remote_allow_unreachable; |
| 39 | |
| 40 | void register_archiver(struct archiver *ar) |
| 41 | { |
| 42 | ALLOC_GROW(archivers, nr_archivers + 1, alloc_archivers); |
| 43 | archivers[nr_archivers++] = ar; |
| 44 | } |
| 45 | |
| 46 | void init_archivers(void) |
| 47 | { |
| 48 | init_tar_archiver(); |
| 49 | init_zip_archiver(); |
| 50 | } |
| 51 | |
| 52 | static void format_subst(const struct commit *commit, |
| 53 | const char *src, size_t len, |
| 54 | struct strbuf *buf, struct pretty_print_context *ctx) |
| 55 | { |
| 56 | char *to_free = NULL; |
| 57 | struct strbuf fmt = STRBUF_INIT; |
| 58 | |
| 59 | if (src == buf->buf) |
| 60 | to_free = strbuf_detach(buf, NULL); |
| 61 | for (;;) { |
| 62 | const char *b, *c; |
| 63 | |
| 64 | b = memmem(src, len, "$Format:", 8); |
| 65 | if (!b) |
| 66 | break; |
| 67 | c = memchr(b + 8, '$', (src + len) - b - 8); |
| 68 | if (!c) |
| 69 | break; |
| 70 | |
| 71 | strbuf_reset(&fmt); |
| 72 | strbuf_add(&fmt, b + 8, c - b - 8); |
| 73 | |
| 74 | strbuf_add(buf, src, b - src); |
| 75 | repo_format_commit_message(the_repository, commit, fmt.buf, |
| 76 | buf, ctx); |
| 77 | len -= c + 1 - src; |
| 78 | src = c + 1; |
| 79 | } |
| 80 | strbuf_add(buf, src, len); |
| 81 | strbuf_release(&fmt); |
| 82 | free(to_free); |
| 83 | } |
| 84 | |
| 85 | static void *object_file_to_archive(const struct archiver_args *args, |
| 86 | const char *path, |
| 87 | const struct object_id *oid, |
| 88 | unsigned int mode, |
| 89 | enum object_type *type, |
| 90 | size_t *sizep) |
| 91 | { |
| 92 | void *buffer; |
| 93 | const struct commit *commit = args->convert ? args->commit : NULL; |
| 94 | struct checkout_metadata meta; |
| 95 | |
| 96 | init_checkout_metadata(&meta, args->refname, |
| 97 | args->commit_oid ? args->commit_oid : |
| 98 | (args->tree ? &args->tree->object.oid : NULL), oid); |
| 99 | |
| 100 | path += args->baselen; |
| 101 | buffer = odb_read_object(the_repository->objects, oid, type, sizep); |
| 102 | if (buffer && S_ISREG(mode)) { |
| 103 | struct strbuf buf = STRBUF_INIT; |
| 104 | size_t size = 0; |
| 105 | |
| 106 | strbuf_attach(&buf, buffer, *sizep, *sizep + 1); |
| 107 | convert_to_working_tree(args->repo->index, path, buf.buf, buf.len, &buf, &meta); |
| 108 | if (commit) |
| 109 | format_subst(commit, buf.buf, buf.len, &buf, args->pretty_ctx); |
| 110 | buffer = strbuf_detach(&buf, &size); |
| 111 | *sizep = size; |
| 112 | } |
| 113 | |
| 114 | return buffer; |
| 115 | } |
| 116 | |
| 117 | struct directory { |
| 118 | struct directory *up; |
| 119 | struct object_id oid; |
| 120 | int baselen, len; |
| 121 | unsigned mode; |
| 122 | char path[FLEX_ARRAY]; |
| 123 | }; |
| 124 | |
| 125 | struct archiver_context { |
| 126 | struct archiver_args *args; |
| 127 | write_archive_entry_fn_t write_entry; |
| 128 | struct directory *bottom; |
| 129 | }; |
| 130 | |
| 131 | static const struct attr_check *get_archive_attrs(struct index_state *istate, |
| 132 | const char *path) |
| 133 | { |
| 134 | static struct attr_check *check; |
| 135 | if (!check) |
| 136 | check = attr_check_initl("export-ignore", "export-subst", NULL); |
| 137 | git_check_attr(istate, path, check); |
| 138 | return check; |
| 139 | } |
| 140 | |
| 141 | static int check_attr_export_ignore(const struct attr_check *check) |
| 142 | { |
| 143 | return check && ATTR_TRUE(check->items[0].value); |
| 144 | } |
| 145 | |
| 146 | static int check_attr_export_subst(const struct attr_check *check) |
| 147 | { |
| 148 | return check && ATTR_TRUE(check->items[1].value); |
| 149 | } |
| 150 | |
| 151 | static int write_archive_entry(const struct object_id *oid, const char *base, |
| 152 | int baselen, const char *filename, unsigned mode, |
| 153 | void *context) |
| 154 | { |
| 155 | static struct strbuf path = STRBUF_INIT; |
| 156 | struct archiver_context *c = context; |
| 157 | struct archiver_args *args = c->args; |
| 158 | write_archive_entry_fn_t write_entry = c->write_entry; |
| 159 | int err; |
| 160 | const char *path_without_prefix; |
| 161 | size_t size; |
| 162 | void *buffer; |
| 163 | enum object_type type; |
| 164 | |
| 165 | args->convert = 0; |
| 166 | strbuf_reset(&path); |
| 167 | strbuf_grow(&path, PATH_MAX); |
| 168 | strbuf_add(&path, args->base, args->baselen); |
| 169 | strbuf_add(&path, base, baselen); |
| 170 | strbuf_addstr(&path, filename); |
| 171 | if (S_ISDIR(mode) || S_ISGITLINK(mode)) |
| 172 | strbuf_addch(&path, '/'); |
| 173 | path_without_prefix = path.buf + args->baselen; |
| 174 | |
| 175 | if (!S_ISDIR(mode)) { |
| 176 | const struct attr_check *check; |
| 177 | check = get_archive_attrs(args->repo->index, path_without_prefix); |
| 178 | if (check_attr_export_ignore(check)) |
| 179 | return 0; |
| 180 | args->convert = check_attr_export_subst(check); |
| 181 | } |
| 182 | |
| 183 | if (args->prefix) { |
| 184 | static struct strbuf new_path = STRBUF_INIT; |
| 185 | static struct strbuf buf = STRBUF_INIT; |
| 186 | const char *rel; |
| 187 | |
| 188 | rel = relative_path(path_without_prefix, args->prefix, &buf); |
| 189 | |
| 190 | /* |
| 191 | * We don't add an entry for the current working |
| 192 | * directory when we are at the root; skip it also when |
| 193 | * we're in a subdirectory or submodule. Skip entries |
| 194 | * higher up as well. |
| 195 | */ |
| 196 | if (!strcmp(rel, "./") || starts_with(rel, "../")) |
| 197 | return S_ISDIR(mode) ? READ_TREE_RECURSIVE : 0; |
| 198 | |
| 199 | /* rel can refer to path, so don't edit it in place */ |
| 200 | strbuf_reset(&new_path); |
| 201 | strbuf_add(&new_path, args->base, args->baselen); |
| 202 | strbuf_addstr(&new_path, rel); |
| 203 | strbuf_swap(&path, &new_path); |
| 204 | } |
| 205 | |
| 206 | if (args->verbose) |
| 207 | fprintf(stderr, "%.*s\n", (int)path.len, path.buf); |
| 208 | |
| 209 | if (S_ISDIR(mode) || S_ISGITLINK(mode)) { |
| 210 | err = write_entry(args, oid, path.buf, path.len, mode, NULL, 0); |
| 211 | if (err) |
| 212 | return err; |
| 213 | return (S_ISDIR(mode) ? READ_TREE_RECURSIVE : 0); |
| 214 | } |
| 215 | |
| 216 | /* Stream it? */ |
| 217 | if (S_ISREG(mode) && !args->convert && |
| 218 | odb_read_object_info(args->repo->objects, oid, &size) == OBJ_BLOB && |
| 219 | size > repo_settings_get_big_file_threshold(the_repository)) |
| 220 | return write_entry(args, oid, path.buf, path.len, mode, NULL, size); |
| 221 | |
| 222 | buffer = object_file_to_archive(args, path.buf, oid, mode, &type, &size); |
| 223 | if (!buffer) |
| 224 | return error(_("cannot read '%s'"), oid_to_hex(oid)); |
| 225 | err = write_entry(args, oid, path.buf, path.len, mode, buffer, size); |
| 226 | free(buffer); |
| 227 | return err; |
| 228 | } |
| 229 | |
| 230 | static void queue_directory(const struct object_id *oid, |
| 231 | struct strbuf *base, const char *filename, |
| 232 | unsigned mode, struct archiver_context *c) |
| 233 | { |
| 234 | struct directory *d; |
| 235 | size_t len = st_add4(base->len, 1, strlen(filename), 1); |
| 236 | d = xmalloc(st_add(sizeof(*d), len)); |
| 237 | d->up = c->bottom; |
| 238 | d->baselen = base->len; |
| 239 | d->mode = mode; |
| 240 | c->bottom = d; |
| 241 | d->len = xsnprintf(d->path, len, "%.*s%s/", (int)base->len, base->buf, filename); |
| 242 | oidcpy(&d->oid, oid); |
| 243 | } |
| 244 | |
| 245 | static int write_directory(struct archiver_context *c) |
| 246 | { |
| 247 | struct directory *d = c->bottom; |
| 248 | int ret; |
| 249 | |
| 250 | if (!d) |
| 251 | return 0; |
| 252 | c->bottom = d->up; |
| 253 | d->path[d->len - 1] = '\0'; /* no trailing slash */ |
| 254 | ret = |
| 255 | write_directory(c) || |
| 256 | write_archive_entry(&d->oid, d->path, d->baselen, |
| 257 | d->path + d->baselen, d->mode, |
| 258 | c) != READ_TREE_RECURSIVE; |
| 259 | free(d); |
| 260 | return ret ? -1 : 0; |
| 261 | } |
| 262 | |
| 263 | static int queue_or_write_archive_entry(const struct object_id *oid, |
| 264 | struct strbuf *base, const char *filename, |
| 265 | unsigned mode, void *context) |
| 266 | { |
| 267 | struct archiver_context *c = context; |
| 268 | |
| 269 | while (c->bottom && |
| 270 | !(base->len >= c->bottom->len && |
| 271 | !strncmp(base->buf, c->bottom->path, c->bottom->len))) { |
| 272 | struct directory *next = c->bottom->up; |
| 273 | free(c->bottom); |
| 274 | c->bottom = next; |
| 275 | } |
| 276 | |
| 277 | if (S_ISDIR(mode)) { |
| 278 | size_t baselen = base->len; |
| 279 | const struct attr_check *check; |
| 280 | |
| 281 | /* Borrow base, but restore its original value when done. */ |
| 282 | strbuf_addstr(base, filename); |
| 283 | strbuf_addch(base, '/'); |
| 284 | check = get_archive_attrs(c->args->repo->index, base->buf); |
| 285 | strbuf_setlen(base, baselen); |
| 286 | |
| 287 | if (check_attr_export_ignore(check)) |
| 288 | return 0; |
| 289 | queue_directory(oid, base, filename, mode, c); |
| 290 | return READ_TREE_RECURSIVE; |
| 291 | } |
| 292 | |
| 293 | if (write_directory(c)) |
| 294 | return -1; |
| 295 | return write_archive_entry(oid, base->buf, base->len, filename, mode, |
| 296 | context); |
| 297 | } |
| 298 | |
| 299 | struct extra_file_info { |
| 300 | char *base; |
| 301 | struct stat stat; |
| 302 | void *content; |
| 303 | }; |
| 304 | |
| 305 | int write_archive_entries(struct archiver_args *args, |
| 306 | write_archive_entry_fn_t write_entry) |
| 307 | { |
| 308 | struct archiver_context context; |
| 309 | int err; |
| 310 | struct strbuf path_in_archive = STRBUF_INIT; |
| 311 | struct strbuf content = STRBUF_INIT; |
| 312 | struct object_id fake_oid; |
| 313 | int i; |
| 314 | |
| 315 | oidcpy(&fake_oid, null_oid(the_hash_algo)); |
| 316 | |
| 317 | if (args->baselen > 0 && args->base[args->baselen - 1] == '/') { |
| 318 | size_t len = args->baselen; |
| 319 | |
| 320 | while (len > 1 && args->base[len - 2] == '/') |
| 321 | len--; |
| 322 | if (args->verbose) |
| 323 | fprintf(stderr, "%.*s\n", (int)len, args->base); |
| 324 | err = write_entry(args, &args->tree->object.oid, args->base, |
| 325 | len, 040777, NULL, 0); |
| 326 | if (err) |
| 327 | return err; |
| 328 | } |
| 329 | |
| 330 | memset(&context, 0, sizeof(context)); |
| 331 | context.args = args; |
| 332 | context.write_entry = write_entry; |
| 333 | |
| 334 | err = read_tree(args->repo, args->tree, |
| 335 | &args->pathspec, |
| 336 | queue_or_write_archive_entry, |
| 337 | &context); |
| 338 | if (err == READ_TREE_RECURSIVE) |
| 339 | err = 0; |
| 340 | while (context.bottom) { |
| 341 | struct directory *next = context.bottom->up; |
| 342 | free(context.bottom); |
| 343 | context.bottom = next; |
| 344 | } |
| 345 | |
| 346 | for (i = 0; i < args->extra_files.nr; i++) { |
| 347 | struct string_list_item *item = args->extra_files.items + i; |
| 348 | char *path = item->string; |
| 349 | struct extra_file_info *info = item->util; |
| 350 | |
| 351 | put_be64(fake_oid.hash, i + 1); |
| 352 | |
| 353 | if (!info->content) { |
| 354 | strbuf_reset(&path_in_archive); |
| 355 | if (info->base) |
| 356 | strbuf_addstr(&path_in_archive, info->base); |
| 357 | strbuf_addstr(&path_in_archive, basename(path)); |
| 358 | |
| 359 | strbuf_reset(&content); |
| 360 | if (strbuf_read_file(&content, path, info->stat.st_size) < 0) |
| 361 | err = error_errno(_("cannot read '%s'"), path); |
| 362 | else |
| 363 | err = write_entry(args, &fake_oid, path_in_archive.buf, |
| 364 | path_in_archive.len, |
| 365 | canon_mode(info->stat.st_mode), |
| 366 | content.buf, content.len); |
| 367 | } else { |
| 368 | err = write_entry(args, &fake_oid, |
| 369 | path, strlen(path), |
| 370 | canon_mode(info->stat.st_mode), |
| 371 | info->content, info->stat.st_size); |
| 372 | } |
| 373 | |
| 374 | if (err) |
| 375 | break; |
| 376 | } |
| 377 | strbuf_release(&path_in_archive); |
| 378 | strbuf_release(&content); |
| 379 | |
| 380 | return err; |
| 381 | } |
| 382 | |
| 383 | static const struct archiver *lookup_archiver(const char *name) |
| 384 | { |
| 385 | int i; |
| 386 | |
| 387 | if (!name) |
| 388 | return NULL; |
| 389 | |
| 390 | for (i = 0; i < nr_archivers; i++) { |
| 391 | if (!strcmp(name, archivers[i]->name)) |
| 392 | return archivers[i]; |
| 393 | } |
| 394 | return NULL; |
| 395 | } |
| 396 | |
| 397 | struct path_exists_context { |
| 398 | struct pathspec pathspec; |
| 399 | struct archiver_args *args; |
| 400 | }; |
| 401 | |
| 402 | static int reject_entry(const struct object_id *oid UNUSED, |
| 403 | struct strbuf *base, |
| 404 | const char *filename, unsigned mode, |
| 405 | void *context) |
| 406 | { |
| 407 | int ret = -1; |
| 408 | struct path_exists_context *ctx = context; |
| 409 | |
| 410 | if (S_ISDIR(mode)) { |
| 411 | struct strbuf sb = STRBUF_INIT; |
| 412 | strbuf_addbuf(&sb, base); |
| 413 | strbuf_addstr(&sb, filename); |
| 414 | if (!match_pathspec(ctx->args->repo->index, |
| 415 | &ctx->pathspec, |
| 416 | sb.buf, sb.len, 0, NULL, 1)) |
| 417 | ret = READ_TREE_RECURSIVE; |
| 418 | strbuf_release(&sb); |
| 419 | } |
| 420 | return ret; |
| 421 | } |
| 422 | |
| 423 | static int reject_outside(const struct object_id *oid UNUSED, |
| 424 | struct strbuf *base, const char *filename, |
| 425 | unsigned mode, void *context) |
| 426 | { |
| 427 | struct archiver_args *args = context; |
| 428 | struct strbuf buf = STRBUF_INIT; |
| 429 | struct strbuf path = STRBUF_INIT; |
| 430 | int ret = 0; |
| 431 | |
| 432 | if (S_ISDIR(mode)) |
| 433 | return READ_TREE_RECURSIVE; |
| 434 | |
| 435 | strbuf_addbuf(&path, base); |
| 436 | strbuf_addstr(&path, filename); |
| 437 | if (starts_with(relative_path(path.buf, args->prefix, &buf), "../")) |
| 438 | ret = -1; |
| 439 | strbuf_release(&buf); |
| 440 | strbuf_release(&path); |
| 441 | return ret; |
| 442 | } |
| 443 | |
| 444 | static int path_exists(struct archiver_args *args, const char *path) |
| 445 | { |
| 446 | const char *paths[] = { path, NULL }; |
| 447 | struct path_exists_context ctx; |
| 448 | int ret; |
| 449 | |
| 450 | ctx.args = args; |
| 451 | parse_pathspec(&ctx.pathspec, 0, PATHSPEC_PREFER_CWD, |
| 452 | args->prefix, paths); |
| 453 | ctx.pathspec.recursive = 1; |
| 454 | if (args->prefix && read_tree(args->repo, args->tree, &ctx.pathspec, |
| 455 | reject_outside, args)) |
| 456 | die(_("pathspec '%s' matches files outside the " |
| 457 | "current directory"), path); |
| 458 | ret = read_tree(args->repo, args->tree, |
| 459 | &ctx.pathspec, |
| 460 | reject_entry, &ctx); |
| 461 | clear_pathspec(&ctx.pathspec); |
| 462 | return ret != 0; |
| 463 | } |
| 464 | |
| 465 | static void parse_pathspec_arg(const char **pathspec, |
| 466 | struct archiver_args *ar_args) |
| 467 | { |
| 468 | /* |
| 469 | * must be consistent with parse_pathspec in path_exists() |
| 470 | * Also if pathspec patterns are dependent, we're in big |
| 471 | * trouble as we test each one separately |
| 472 | */ |
| 473 | parse_pathspec(&ar_args->pathspec, 0, PATHSPEC_PREFER_CWD, |
| 474 | ar_args->prefix, pathspec); |
| 475 | ar_args->pathspec.recursive = 1; |
| 476 | if (pathspec) { |
| 477 | while (*pathspec) { |
| 478 | if (**pathspec && !path_exists(ar_args, *pathspec)) |
| 479 | die(_("pathspec '%s' did not match any files"), *pathspec); |
| 480 | pathspec++; |
| 481 | } |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | static void parse_treeish_arg(const char **argv, |
| 486 | struct archiver_args *ar_args, int remote) |
| 487 | { |
| 488 | const char *name = argv[0]; |
| 489 | const struct object_id *commit_oid; |
| 490 | time_t archive_time; |
| 491 | struct tree *tree; |
| 492 | const struct commit *commit; |
| 493 | struct object_id oid; |
| 494 | char *ref = NULL; |
| 495 | |
| 496 | /* Remotes are only allowed to fetch actual refs */ |
| 497 | if (remote && !remote_allow_unreachable) { |
| 498 | const char *colon = strchrnul(name, ':'); |
| 499 | int refnamelen = colon - name; |
| 500 | |
| 501 | if (!repo_dwim_ref(the_repository, name, refnamelen, &oid, &ref, 0)) |
| 502 | die(_("no such ref: %.*s"), refnamelen, name); |
| 503 | } else { |
| 504 | repo_dwim_ref(the_repository, name, strlen(name), &oid, &ref, |
| 505 | 0); |
| 506 | } |
| 507 | |
| 508 | if (repo_get_oid(the_repository, name, &oid)) |
| 509 | die(_("not a valid object name: %s"), name); |
| 510 | |
| 511 | commit = lookup_commit_reference_gently(ar_args->repo, &oid, 1); |
| 512 | if (commit) { |
| 513 | commit_oid = &commit->object.oid; |
| 514 | archive_time = commit->date; |
| 515 | } else { |
| 516 | commit_oid = NULL; |
| 517 | archive_time = time(NULL); |
| 518 | } |
| 519 | if (ar_args->mtime_option) |
| 520 | archive_time = approxidate(ar_args->mtime_option); |
| 521 | |
| 522 | tree = repo_parse_tree_indirect(the_repository, &oid); |
| 523 | if (!tree) |
| 524 | die(_("not a tree object: %s"), oid_to_hex(&oid)); |
| 525 | |
| 526 | /* |
| 527 | * Setup index and instruct attr to read index only |
| 528 | */ |
| 529 | if (!ar_args->worktree_attributes) { |
| 530 | struct unpack_trees_options opts; |
| 531 | struct tree_desc t; |
| 532 | |
| 533 | memset(&opts, 0, sizeof(opts)); |
| 534 | opts.index_only = 1; |
| 535 | opts.head_idx = -1; |
| 536 | opts.src_index = ar_args->repo->index; |
| 537 | opts.dst_index = ar_args->repo->index; |
| 538 | opts.fn = oneway_merge; |
| 539 | init_tree_desc(&t, &tree->object.oid, tree->buffer, tree->size); |
| 540 | if (unpack_trees(1, &t, &opts)) |
| 541 | die(_("failed to unpack tree object %s"), |
| 542 | oid_to_hex(&tree->object.oid)); |
| 543 | |
| 544 | git_attr_set_direction(GIT_ATTR_INDEX); |
| 545 | } |
| 546 | |
| 547 | ar_args->refname = ref; |
| 548 | ar_args->tree = tree; |
| 549 | ar_args->commit_oid = commit_oid; |
| 550 | ar_args->commit = commit; |
| 551 | ar_args->time = archive_time; |
| 552 | } |
| 553 | |
| 554 | static void extra_file_info_clear(void *util, const char *str UNUSED) |
| 555 | { |
| 556 | struct extra_file_info *info = util; |
| 557 | free(info->base); |
| 558 | free(info->content); |
| 559 | free(info); |
| 560 | } |
| 561 | |
| 562 | static int add_file_cb(const struct option *opt, const char *arg, int unset) |
| 563 | { |
| 564 | struct archiver_args *args = opt->value; |
| 565 | const char **basep = (const char **)opt->defval; |
| 566 | const char *base = *basep; |
| 567 | char *path; |
| 568 | struct string_list_item *item; |
| 569 | struct extra_file_info *info; |
| 570 | |
| 571 | if (unset) { |
| 572 | string_list_clear_func(&args->extra_files, |
| 573 | extra_file_info_clear); |
| 574 | return 0; |
| 575 | } |
| 576 | |
| 577 | if (!arg) |
| 578 | return -1; |
| 579 | |
| 580 | info = xmalloc(sizeof(*info)); |
| 581 | info->base = xstrdup_or_null(base); |
| 582 | |
| 583 | if (!strcmp(opt->long_name, "add-file")) { |
| 584 | path = prefix_filename(args->prefix, arg); |
| 585 | if (stat(path, &info->stat)) |
| 586 | die(_("File not found: %s"), path); |
| 587 | if (!S_ISREG(info->stat.st_mode)) |
| 588 | die(_("Not a regular file: %s"), path); |
| 589 | info->content = NULL; /* read the file later */ |
| 590 | } else if (!strcmp(opt->long_name, "add-virtual-file")) { |
| 591 | struct strbuf buf = STRBUF_INIT; |
| 592 | const char *p = arg; |
| 593 | |
| 594 | if (*p != '"') |
| 595 | p = strchr(p, ':'); |
| 596 | else if (unquote_c_style(&buf, p, &p) < 0) |
| 597 | die(_("unclosed quote: '%s'"), arg); |
| 598 | |
| 599 | if (!p || *p != ':') |
| 600 | die(_("missing colon: '%s'"), arg); |
| 601 | |
| 602 | if (p == arg) |
| 603 | die(_("empty file name: '%s'"), arg); |
| 604 | |
| 605 | path = buf.len ? |
| 606 | strbuf_detach(&buf, NULL) : xstrndup(arg, p - arg); |
| 607 | |
| 608 | if (args->prefix) { |
| 609 | char *save = path; |
| 610 | path = prefix_filename(args->prefix, path); |
| 611 | free(save); |
| 612 | } |
| 613 | memset(&info->stat, 0, sizeof(info->stat)); |
| 614 | info->stat.st_mode = S_IFREG | 0644; |
| 615 | info->content = xstrdup(p + 1); |
| 616 | info->stat.st_size = strlen(info->content); |
| 617 | } else { |
| 618 | BUG("add_file_cb() called for %s", opt->long_name); |
| 619 | } |
| 620 | item = string_list_append_nodup(&args->extra_files, path); |
| 621 | item->util = info; |
| 622 | |
| 623 | return 0; |
| 624 | } |
| 625 | |
| 626 | static int number_callback(const struct option *opt, const char *arg, int unset) |
| 627 | { |
| 628 | BUG_ON_OPT_NEG(unset); |
| 629 | *(int *)opt->value = strtol(arg, NULL, 10); |
| 630 | return 0; |
| 631 | } |
| 632 | |
| 633 | static int parse_archive_args(int argc, const char **argv, |
| 634 | const struct archiver **ar, struct archiver_args *args, |
| 635 | const char *name_hint, int is_remote) |
| 636 | { |
| 637 | const char *format = NULL; |
| 638 | const char *base = NULL; |
| 639 | const char *remote = NULL; |
| 640 | const char *exec = NULL; |
| 641 | const char *output = NULL; |
| 642 | const char *mtime_option = NULL; |
| 643 | int compression_level = -1; |
| 644 | int verbose = 0; |
| 645 | int i; |
| 646 | int list = 0; |
| 647 | int worktree_attributes = 0; |
| 648 | struct option opts[] = { |
| 649 | OPT_GROUP(""), |
| 650 | OPT_STRING(0, "format", &format, N_("fmt"), N_("archive format")), |
| 651 | OPT_STRING(0, "prefix", &base, N_("prefix"), |
| 652 | N_("prepend prefix to each pathname in the archive")), |
| 653 | { |
| 654 | .type = OPTION_CALLBACK, |
| 655 | .long_name = "add-file", |
| 656 | .value = args, |
| 657 | .argh = N_("file"), |
| 658 | .help = N_("add untracked file to archive"), |
| 659 | .callback = add_file_cb, |
| 660 | .defval = (intptr_t) &base, |
| 661 | }, |
| 662 | { |
| 663 | .type = OPTION_CALLBACK, |
| 664 | .long_name = "add-virtual-file", |
| 665 | .value = args, |
| 666 | .argh = N_("path:content"), |
| 667 | .help = N_("add untracked file to archive"), |
| 668 | .callback = add_file_cb, |
| 669 | .defval = (intptr_t) &base, |
| 670 | }, |
| 671 | OPT_STRING('o', "output", &output, N_("file"), |
| 672 | N_("write the archive to this file")), |
| 673 | OPT_BOOL(0, "worktree-attributes", &worktree_attributes, |
| 674 | N_("read .gitattributes in working directory")), |
| 675 | OPT__VERBOSE(&verbose, N_("report archived files on stderr")), |
| 676 | { |
| 677 | .type = OPTION_STRING, |
| 678 | .long_name = "mtime", |
| 679 | .value = &mtime_option, |
| 680 | .argh = N_("time"), |
| 681 | .help = N_("set modification time of archive entries"), |
| 682 | .flags = PARSE_OPT_NONEG, |
| 683 | }, |
| 684 | OPT_NUMBER_CALLBACK(&compression_level, |
| 685 | N_("set compression level"), number_callback), |
| 686 | OPT_GROUP(""), |
| 687 | OPT_BOOL('l', "list", &list, |
| 688 | N_("list supported archive formats")), |
| 689 | OPT_GROUP(""), |
| 690 | OPT_STRING(0, "remote", &remote, N_("repo"), |
| 691 | N_("retrieve the archive from remote repository <repo>")), |
| 692 | OPT_STRING(0, "exec", &exec, N_("command"), |
| 693 | N_("path to the remote git-upload-archive command")), |
| 694 | OPT_END() |
| 695 | }; |
| 696 | |
| 697 | argc = parse_options(argc, argv, NULL, opts, archive_usage, 0); |
| 698 | |
| 699 | if (remote) |
| 700 | die(_("Unexpected option --remote")); |
| 701 | if (exec) |
| 702 | die(_("the option '%s' requires '%s'"), "--exec", "--remote"); |
| 703 | if (output) |
| 704 | die(_("Unexpected option --output")); |
| 705 | if (is_remote && args->extra_files.nr) |
| 706 | die(_("options '%s' and '%s' cannot be used together"), "--add-file", "--remote"); |
| 707 | |
| 708 | if (!base) |
| 709 | base = ""; |
| 710 | |
| 711 | if (list) { |
| 712 | if (argc) |
| 713 | die(_("extra command line parameter '%s'"), *argv); |
| 714 | for (i = 0; i < nr_archivers; i++) |
| 715 | if (!is_remote || archivers[i]->flags & ARCHIVER_REMOTE) |
| 716 | printf("%s\n", archivers[i]->name); |
| 717 | exit(0); |
| 718 | } |
| 719 | |
| 720 | if (!format && name_hint) |
| 721 | format = archive_format_from_filename(name_hint); |
| 722 | if (!format) |
| 723 | format = "tar"; |
| 724 | |
| 725 | /* We need at least one parameter -- tree-ish */ |
| 726 | if (argc < 1) |
| 727 | usage_with_options(archive_usage, opts); |
| 728 | *ar = lookup_archiver(format); |
| 729 | if (!*ar || (is_remote && !((*ar)->flags & ARCHIVER_REMOTE))) |
| 730 | die(_("Unknown archive format '%s'"), format); |
| 731 | |
| 732 | args->compression_level = Z_DEFAULT_COMPRESSION; |
| 733 | if (compression_level != -1) { |
| 734 | int levels_ok = (*ar)->flags & ARCHIVER_WANT_COMPRESSION_LEVELS; |
| 735 | int high_ok = (*ar)->flags & ARCHIVER_HIGH_COMPRESSION_LEVELS; |
| 736 | if (levels_ok && (compression_level <= 9 || high_ok)) |
| 737 | args->compression_level = compression_level; |
| 738 | else { |
| 739 | die(_("Argument not supported for format '%s': -%d"), |
| 740 | format, compression_level); |
| 741 | } |
| 742 | } |
| 743 | args->verbose = verbose; |
| 744 | args->base = base; |
| 745 | args->baselen = strlen(base); |
| 746 | args->worktree_attributes = worktree_attributes; |
| 747 | args->mtime_option = mtime_option; |
| 748 | |
| 749 | return argc; |
| 750 | } |
| 751 | |
| 752 | int write_archive(int argc, const char **argv, const char *prefix, |
| 753 | struct repository *repo, |
| 754 | const char *name_hint, int remote) |
| 755 | { |
| 756 | const struct archiver *ar = NULL; |
| 757 | struct pretty_print_describe_status describe_status = {0}; |
| 758 | struct pretty_print_context ctx = {0}; |
| 759 | struct archiver_args args; |
| 760 | const char **argv_copy; |
| 761 | int rc; |
| 762 | |
| 763 | repo_config_get_bool(the_repository, "uploadarchive.allowunreachable", &remote_allow_unreachable); |
| 764 | repo_config(the_repository, git_default_config, NULL); |
| 765 | |
| 766 | describe_status.max_invocations = 1; |
| 767 | ctx.date_mode.type = DATE_NORMAL; |
| 768 | ctx.abbrev = DEFAULT_ABBREV; |
| 769 | ctx.describe_status = &describe_status; |
| 770 | args.pretty_ctx = &ctx; |
| 771 | args.repo = repo; |
| 772 | args.prefix = prefix; |
| 773 | string_list_init_dup(&args.extra_files); |
| 774 | |
| 775 | /* |
| 776 | * `parse_archive_args()` modifies contents of `argv`, which is what we |
| 777 | * want. Our callers may not want it though, so we create a copy here. |
| 778 | */ |
| 779 | DUP_ARRAY(argv_copy, argv, argc); |
| 780 | argv = argv_copy; |
| 781 | |
| 782 | argc = parse_archive_args(argc, argv, &ar, &args, name_hint, remote); |
| 783 | if (!startup_info->have_repository) { |
| 784 | /* |
| 785 | * We know this will die() with an error, so we could just |
| 786 | * die ourselves; but its error message will be more specific |
| 787 | * than what we could write here. |
| 788 | */ |
| 789 | setup_git_directory(the_repository); |
| 790 | } |
| 791 | |
| 792 | parse_treeish_arg(argv, &args, remote); |
| 793 | parse_pathspec_arg(argv + 1, &args); |
| 794 | |
| 795 | rc = ar->write_archive(ar, &args); |
| 796 | |
| 797 | string_list_clear_func(&args.extra_files, extra_file_info_clear); |
| 798 | free(args.refname); |
| 799 | clear_pathspec(&args.pathspec); |
| 800 | free(argv_copy); |
| 801 | |
| 802 | return rc; |
| 803 | } |
| 804 | |
| 805 | static int match_extension(const char *filename, const char *ext) |
| 806 | { |
| 807 | int prefixlen = strlen(filename) - strlen(ext); |
| 808 | |
| 809 | /* |
| 810 | * We need 1 character for the '.', and 1 character to ensure that the |
| 811 | * prefix is non-empty (k.e., we don't match .tar.gz with no actual |
| 812 | * filename). |
| 813 | */ |
| 814 | if (prefixlen < 2 || filename[prefixlen - 1] != '.') |
| 815 | return 0; |
| 816 | return !strcmp(filename + prefixlen, ext); |
| 817 | } |
| 818 | |
| 819 | const char *archive_format_from_filename(const char *filename) |
| 820 | { |
| 821 | int i; |
| 822 | |
| 823 | for (i = 0; i < nr_archivers; i++) |
| 824 | if (match_extension(filename, archivers[i]->name)) |
| 825 | return archivers[i]->name; |
| 826 | return NULL; |
| 827 | } |