builtin / tar-tree.con commit remote-hg: add tests for special filenames (8d784da)
   1/*
   2 * Copyright (c) 2005, 2006 Rene Scharfe
   3 */
   4#include "cache.h"
   5#include "commit.h"
   6#include "tar.h"
   7#include "builtin.h"
   8#include "quote.h"
   9
  10static const char tar_tree_usage[] =
  11"git tar-tree [--remote=<repo>] <tree-ish> [basedir]\n"
  12"*** Note that this command is now deprecated; use \"git archive\" instead.";
  13
  14static const char builtin_get_tar_commit_id_usage[] =
  15"git get-tar-commit-id < <tarfile>";
  16
  17int cmd_tar_tree(int argc, const char **argv, const char *prefix)
  18{
  19        /*
  20         * "git tar-tree" is now a wrapper around "git archive --format=tar"
  21         *
  22         * $0 --remote=<repo> arg... ==>
  23         *      git archive --format=tar --remote=<repo> arg...
  24         * $0 tree-ish ==>
  25         *      git archive --format=tar tree-ish
  26         * $0 tree-ish basedir ==>
  27         *      git archive --format-tar --prefix=basedir tree-ish
  28         */
  29        const char **nargv = xcalloc(sizeof(*nargv), argc + 3);
  30        struct strbuf sb = STRBUF_INIT;
  31        char *basedir_arg;
  32        int nargc = 0;
  33
  34        nargv[nargc++] = "archive";
  35        nargv[nargc++] = "--format=tar";
  36
  37        if (2 <= argc && !prefixcmp(argv[1], "--remote=")) {
  38                nargv[nargc++] = argv[1];
  39                argv++;
  40                argc--;
  41        }
  42
  43        /*
  44         * Because it's just a compatibility wrapper, tar-tree supports only
  45         * the old behaviour of reading attributes from the work tree.
  46         */
  47        nargv[nargc++] = "--worktree-attributes";
  48
  49        switch (argc) {
  50        default:
  51                usage(tar_tree_usage);
  52                break;
  53        case 3:
  54                /* base-path */
  55                basedir_arg = xmalloc(strlen(argv[2]) + 11);
  56                sprintf(basedir_arg, "--prefix=%s/", argv[2]);
  57                nargv[nargc++] = basedir_arg;
  58                /* fallthru */
  59        case 2:
  60                /* tree-ish */
  61                nargv[nargc++] = argv[1];
  62        }
  63        nargv[nargc] = NULL;
  64
  65        fprintf(stderr,
  66                "*** \"git tar-tree\" is now deprecated.\n"
  67                "*** Running \"git archive\" instead.\n***");
  68        sq_quote_argv(&sb, nargv, 0);
  69        strbuf_addch(&sb, '\n');
  70        fputs(sb.buf, stderr);
  71        strbuf_release(&sb);
  72        return cmd_archive(nargc, nargv, prefix);
  73}
  74
  75/* ustar header + extended global header content */
  76#define RECORDSIZE      (512)
  77#define HEADERSIZE (2 * RECORDSIZE)
  78
  79int cmd_get_tar_commit_id(int argc, const char **argv, const char *prefix)
  80{
  81        char buffer[HEADERSIZE];
  82        struct ustar_header *header = (struct ustar_header *)buffer;
  83        char *content = buffer + RECORDSIZE;
  84        ssize_t n;
  85
  86        if (argc != 1)
  87                usage(builtin_get_tar_commit_id_usage);
  88
  89        n = read_in_full(0, buffer, HEADERSIZE);
  90        if (n < HEADERSIZE)
  91                die("git get-tar-commit-id: read error");
  92        if (header->typeflag[0] != 'g')
  93                return 1;
  94        if (memcmp(content, "52 comment=", 11))
  95                return 1;
  96
  97        n = write_in_full(1, content + 11, 41);
  98        if (n < 41)
  99                die_errno("git get-tar-commit-id: write error");
 100
 101        return 0;
 102}