builtin / fetch-pack.con commit remote-curl: pass ref SHA-1 to fetch-pack as well (58f2ed0)
   1#include "builtin.h"
   2#include "pkt-line.h"
   3#include "fetch-pack.h"
   4#include "remote.h"
   5#include "connect.h"
   6
   7static const char fetch_pack_usage[] =
   8"git fetch-pack [--all] [--stdin] [--quiet|-q] [--keep|-k] [--thin] "
   9"[--include-tag] [--upload-pack=<git-upload-pack>] [--depth=<n>] "
  10"[--no-progress] [-v] [<host>:]<directory> [<refs>...]";
  11
  12static void add_sought_entry_mem(struct ref ***sought, int *nr, int *alloc,
  13                                 const char *name, int namelen)
  14{
  15        struct ref *ref = xcalloc(1, sizeof(*ref) + namelen + 1);
  16        unsigned char sha1[20];
  17
  18        if (namelen > 41 && name[40] == ' ' && !get_sha1_hex(name, sha1)) {
  19                hashcpy(ref->old_sha1, sha1);
  20                name += 41;
  21                namelen -= 41;
  22        }
  23
  24        memcpy(ref->name, name, namelen);
  25        ref->name[namelen] = '\0';
  26        (*nr)++;
  27        ALLOC_GROW(*sought, *nr, *alloc);
  28        (*sought)[*nr - 1] = ref;
  29}
  30
  31static void add_sought_entry(struct ref ***sought, int *nr, int *alloc,
  32                             const char *string)
  33{
  34        add_sought_entry_mem(sought, nr, alloc, string, strlen(string));
  35}
  36
  37int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
  38{
  39        int i, ret;
  40        struct ref *ref = NULL;
  41        const char *dest = NULL;
  42        struct ref **sought = NULL;
  43        int nr_sought = 0, alloc_sought = 0;
  44        int fd[2];
  45        char *pack_lockfile = NULL;
  46        char **pack_lockfile_ptr = NULL;
  47        struct child_process *conn;
  48        struct fetch_pack_args args;
  49
  50        packet_trace_identity("fetch-pack");
  51
  52        memset(&args, 0, sizeof(args));
  53        args.uploadpack = "git-upload-pack";
  54
  55        for (i = 1; i < argc && *argv[i] == '-'; i++) {
  56                const char *arg = argv[i];
  57
  58                if (!prefixcmp(arg, "--upload-pack=")) {
  59                        args.uploadpack = arg + 14;
  60                        continue;
  61                }
  62                if (!prefixcmp(arg, "--exec=")) {
  63                        args.uploadpack = arg + 7;
  64                        continue;
  65                }
  66                if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
  67                        args.quiet = 1;
  68                        continue;
  69                }
  70                if (!strcmp("--keep", arg) || !strcmp("-k", arg)) {
  71                        args.lock_pack = args.keep_pack;
  72                        args.keep_pack = 1;
  73                        continue;
  74                }
  75                if (!strcmp("--thin", arg)) {
  76                        args.use_thin_pack = 1;
  77                        continue;
  78                }
  79                if (!strcmp("--include-tag", arg)) {
  80                        args.include_tag = 1;
  81                        continue;
  82                }
  83                if (!strcmp("--all", arg)) {
  84                        args.fetch_all = 1;
  85                        continue;
  86                }
  87                if (!strcmp("--stdin", arg)) {
  88                        args.stdin_refs = 1;
  89                        continue;
  90                }
  91                if (!strcmp("-v", arg)) {
  92                        args.verbose = 1;
  93                        continue;
  94                }
  95                if (!prefixcmp(arg, "--depth=")) {
  96                        args.depth = strtol(arg + 8, NULL, 0);
  97                        continue;
  98                }
  99                if (!strcmp("--no-progress", arg)) {
 100                        args.no_progress = 1;
 101                        continue;
 102                }
 103                if (!strcmp("--stateless-rpc", arg)) {
 104                        args.stateless_rpc = 1;
 105                        continue;
 106                }
 107                if (!strcmp("--lock-pack", arg)) {
 108                        args.lock_pack = 1;
 109                        pack_lockfile_ptr = &pack_lockfile;
 110                        continue;
 111                }
 112                if (!strcmp("--check-self-contained-and-connected", arg)) {
 113                        args.check_self_contained_and_connected = 1;
 114                        continue;
 115                }
 116                usage(fetch_pack_usage);
 117        }
 118
 119        if (i < argc)
 120                dest = argv[i++];
 121        else
 122                usage(fetch_pack_usage);
 123
 124        /*
 125         * Copy refs from cmdline to growable list, then append any
 126         * refs from the standard input:
 127         */
 128        for (; i < argc; i++)
 129                add_sought_entry(&sought, &nr_sought, &alloc_sought, argv[i]);
 130        if (args.stdin_refs) {
 131                if (args.stateless_rpc) {
 132                        /* in stateless RPC mode we use pkt-line to read
 133                         * from stdin, until we get a flush packet
 134                         */
 135                        for (;;) {
 136                                char *line = packet_read_line(0, NULL);
 137                                if (!line)
 138                                        break;
 139                                add_sought_entry(&sought, &nr_sought,  &alloc_sought, line);
 140                        }
 141                }
 142                else {
 143                        /* read from stdin one ref per line, until EOF */
 144                        struct strbuf line = STRBUF_INIT;
 145                        while (strbuf_getline(&line, stdin, '\n') != EOF)
 146                                add_sought_entry(&sought, &nr_sought, &alloc_sought, line.buf);
 147                        strbuf_release(&line);
 148                }
 149        }
 150
 151        if (args.stateless_rpc) {
 152                conn = NULL;
 153                fd[0] = 0;
 154                fd[1] = 1;
 155        } else {
 156                conn = git_connect(fd, dest, args.uploadpack,
 157                                   args.verbose ? CONNECT_VERBOSE : 0);
 158        }
 159
 160        get_remote_heads(fd[0], NULL, 0, &ref, 0, NULL, NULL);
 161
 162        ref = fetch_pack(&args, fd, conn, ref, dest,
 163                         sought, nr_sought, NULL, pack_lockfile_ptr);
 164        if (pack_lockfile) {
 165                printf("lock %s\n", pack_lockfile);
 166                fflush(stdout);
 167        }
 168        if (args.check_self_contained_and_connected &&
 169            args.self_contained_and_connected) {
 170                printf("connectivity-ok\n");
 171                fflush(stdout);
 172        }
 173        close(fd[0]);
 174        close(fd[1]);
 175        if (finish_connect(conn))
 176                return 1;
 177
 178        ret = !ref;
 179
 180        /*
 181         * If the heads to pull were given, we should have consumed
 182         * all of them by matching the remote.  Otherwise, 'git fetch
 183         * remote no-such-ref' would silently succeed without issuing
 184         * an error.
 185         */
 186        for (i = 0; i < nr_sought; i++) {
 187                if (!sought[i] || sought[i]->matched)
 188                        continue;
 189                error("no such remote ref %s", sought[i]->name);
 190                ret = 1;
 191        }
 192
 193        while (ref) {
 194                printf("%s %s\n",
 195                       sha1_to_hex(ref->old_sha1), ref->name);
 196                ref = ref->next;
 197        }
 198
 199        return ret;
 200}