builtin-upload-archive.con commit Teach --exec to git-archive --remote (fe5ab76)
   1/*
   2 * Copyright (c) 2006 Franck Bui-Huu
   3 */
   4#include <time.h>
   5#include "cache.h"
   6#include "builtin.h"
   7#include "archive.h"
   8#include "pkt-line.h"
   9
  10static const char upload_archive_usage[] =
  11        "git-upload-archive <repo>";
  12
  13
  14int cmd_upload_archive(int argc, const char **argv, const char *prefix)
  15{
  16        struct archiver ar;
  17        const char *sent_argv[MAX_ARGS];
  18        const char *arg_cmd = "argument ";
  19        char *p, buf[4096];
  20        int treeish_idx;
  21        int sent_argc;
  22        int len;
  23
  24        if (argc != 2)
  25                usage(upload_archive_usage);
  26
  27        if (strlen(argv[1]) > sizeof(buf))
  28                die("insanely long repository name");
  29
  30        strcpy(buf, argv[1]); /* enter-repo smudges its argument */
  31
  32        if (!enter_repo(buf, 0))
  33                die("not a git archive");
  34
  35        /* put received options in sent_argv[] */
  36        sent_argc = 1;
  37        sent_argv[0] = "git-upload-archive";
  38        for (p = buf;;) {
  39                /* This will die if not enough free space in buf */
  40                len = packet_read_line(0, p, (buf + sizeof buf) - p);
  41                if (len == 0)
  42                        break;  /* got a flush */
  43                if (sent_argc > MAX_ARGS - 2)
  44                        die("Too many options (>29)");
  45
  46                if (p[len-1] == '\n') {
  47                        p[--len] = 0;
  48                }
  49                if (len < strlen(arg_cmd) ||
  50                    strncmp(arg_cmd, p, strlen(arg_cmd)))
  51                        die("'argument' token or flush expected");
  52
  53                len -= strlen(arg_cmd);
  54                memmove(p, p + strlen(arg_cmd), len);
  55                sent_argv[sent_argc++] = p;
  56                p += len;
  57                *p++ = 0;
  58        }
  59        sent_argv[sent_argc] = NULL;
  60
  61        /* parse all options sent by the client */
  62        treeish_idx = parse_archive_args(sent_argc, sent_argv, &ar);
  63
  64        parse_treeish_arg(sent_argv + treeish_idx, &ar.args, prefix);
  65        parse_pathspec_arg(sent_argv + treeish_idx + 1, &ar.args);
  66
  67        packet_write(1, "ACK\n");
  68        packet_flush(1);
  69
  70        return ar.write_archive(&ar.args);
  71}
  72