builtin / fetch-pack.con commit fetch-pack: support protocol version 2 (4316ff3)
   1#include "builtin.h"
   2#include "pkt-line.h"
   3#include "fetch-pack.h"
   4#include "remote.h"
   5#include "connect.h"
   6#include "sha1-array.h"
   7#include "protocol.h"
   8
   9static const char fetch_pack_usage[] =
  10"git fetch-pack [--all] [--stdin] [--quiet | -q] [--keep | -k] [--thin] "
  11"[--include-tag] [--upload-pack=<git-upload-pack>] [--depth=<n>] "
  12"[--no-progress] [--diag-url] [-v] [<host>:]<directory> [<refs>...]";
  13
  14static void add_sought_entry(struct ref ***sought, int *nr, int *alloc,
  15                             const char *name)
  16{
  17        struct ref *ref;
  18        struct object_id oid;
  19
  20        if (!get_oid_hex(name, &oid)) {
  21                if (name[GIT_SHA1_HEXSZ] == ' ') {
  22                        /* <sha1> <ref>, find refname */
  23                        name += GIT_SHA1_HEXSZ + 1;
  24                } else if (name[GIT_SHA1_HEXSZ] == '\0') {
  25                        ; /* <sha1>, leave sha1 as name */
  26                } else {
  27                        /* <ref>, clear cruft from oid */
  28                        oidclr(&oid);
  29                }
  30        } else {
  31                /* <ref>, clear cruft from get_oid_hex */
  32                oidclr(&oid);
  33        }
  34
  35        ref = alloc_ref(name);
  36        oidcpy(&ref->old_oid, &oid);
  37        (*nr)++;
  38        ALLOC_GROW(*sought, *nr, *alloc);
  39        (*sought)[*nr - 1] = ref;
  40}
  41
  42int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
  43{
  44        int i, ret;
  45        struct ref *ref = NULL;
  46        const char *dest = NULL;
  47        struct ref **sought = NULL;
  48        int nr_sought = 0, alloc_sought = 0;
  49        int fd[2];
  50        char *pack_lockfile = NULL;
  51        char **pack_lockfile_ptr = NULL;
  52        struct child_process *conn;
  53        struct fetch_pack_args args;
  54        struct oid_array shallow = OID_ARRAY_INIT;
  55        struct string_list deepen_not = STRING_LIST_INIT_DUP;
  56        struct packet_reader reader;
  57        enum protocol_version version;
  58
  59        fetch_if_missing = 0;
  60
  61        packet_trace_identity("fetch-pack");
  62
  63        memset(&args, 0, sizeof(args));
  64        args.uploadpack = "git-upload-pack";
  65
  66        for (i = 1; i < argc && *argv[i] == '-'; i++) {
  67                const char *arg = argv[i];
  68
  69                if (skip_prefix(arg, "--upload-pack=", &arg)) {
  70                        args.uploadpack = arg;
  71                        continue;
  72                }
  73                if (skip_prefix(arg, "--exec=", &arg)) {
  74                        args.uploadpack = arg;
  75                        continue;
  76                }
  77                if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
  78                        args.quiet = 1;
  79                        continue;
  80                }
  81                if (!strcmp("--keep", arg) || !strcmp("-k", arg)) {
  82                        args.lock_pack = args.keep_pack;
  83                        args.keep_pack = 1;
  84                        continue;
  85                }
  86                if (!strcmp("--thin", arg)) {
  87                        args.use_thin_pack = 1;
  88                        continue;
  89                }
  90                if (!strcmp("--include-tag", arg)) {
  91                        args.include_tag = 1;
  92                        continue;
  93                }
  94                if (!strcmp("--all", arg)) {
  95                        args.fetch_all = 1;
  96                        continue;
  97                }
  98                if (!strcmp("--stdin", arg)) {
  99                        args.stdin_refs = 1;
 100                        continue;
 101                }
 102                if (!strcmp("--diag-url", arg)) {
 103                        args.diag_url = 1;
 104                        continue;
 105                }
 106                if (!strcmp("-v", arg)) {
 107                        args.verbose = 1;
 108                        continue;
 109                }
 110                if (skip_prefix(arg, "--depth=", &arg)) {
 111                        args.depth = strtol(arg, NULL, 0);
 112                        continue;
 113                }
 114                if (skip_prefix(arg, "--shallow-since=", &arg)) {
 115                        args.deepen_since = xstrdup(arg);
 116                        continue;
 117                }
 118                if (skip_prefix(arg, "--shallow-exclude=", &arg)) {
 119                        string_list_append(&deepen_not, arg);
 120                        continue;
 121                }
 122                if (!strcmp(arg, "--deepen-relative")) {
 123                        args.deepen_relative = 1;
 124                        continue;
 125                }
 126                if (!strcmp("--no-progress", arg)) {
 127                        args.no_progress = 1;
 128                        continue;
 129                }
 130                if (!strcmp("--stateless-rpc", arg)) {
 131                        args.stateless_rpc = 1;
 132                        continue;
 133                }
 134                if (!strcmp("--lock-pack", arg)) {
 135                        args.lock_pack = 1;
 136                        pack_lockfile_ptr = &pack_lockfile;
 137                        continue;
 138                }
 139                if (!strcmp("--check-self-contained-and-connected", arg)) {
 140                        args.check_self_contained_and_connected = 1;
 141                        continue;
 142                }
 143                if (!strcmp("--cloning", arg)) {
 144                        args.cloning = 1;
 145                        continue;
 146                }
 147                if (!strcmp("--update-shallow", arg)) {
 148                        args.update_shallow = 1;
 149                        continue;
 150                }
 151                if (!strcmp("--from-promisor", arg)) {
 152                        args.from_promisor = 1;
 153                        continue;
 154                }
 155                if (!strcmp("--no-dependents", arg)) {
 156                        args.no_dependents = 1;
 157                        continue;
 158                }
 159                if (skip_prefix(arg, ("--" CL_ARG__FILTER "="), &arg)) {
 160                        parse_list_objects_filter(&args.filter_options, arg);
 161                        continue;
 162                }
 163                if (!strcmp(arg, ("--no-" CL_ARG__FILTER))) {
 164                        list_objects_filter_set_no_filter(&args.filter_options);
 165                        continue;
 166                }
 167                usage(fetch_pack_usage);
 168        }
 169        if (deepen_not.nr)
 170                args.deepen_not = &deepen_not;
 171
 172        if (i < argc)
 173                dest = argv[i++];
 174        else
 175                usage(fetch_pack_usage);
 176
 177        /*
 178         * Copy refs from cmdline to growable list, then append any
 179         * refs from the standard input:
 180         */
 181        for (; i < argc; i++)
 182                add_sought_entry(&sought, &nr_sought, &alloc_sought, argv[i]);
 183        if (args.stdin_refs) {
 184                if (args.stateless_rpc) {
 185                        /* in stateless RPC mode we use pkt-line to read
 186                         * from stdin, until we get a flush packet
 187                         */
 188                        for (;;) {
 189                                char *line = packet_read_line(0, NULL);
 190                                if (!line)
 191                                        break;
 192                                add_sought_entry(&sought, &nr_sought,  &alloc_sought, line);
 193                        }
 194                }
 195                else {
 196                        /* read from stdin one ref per line, until EOF */
 197                        struct strbuf line = STRBUF_INIT;
 198                        while (strbuf_getline_lf(&line, stdin) != EOF)
 199                                add_sought_entry(&sought, &nr_sought, &alloc_sought, line.buf);
 200                        strbuf_release(&line);
 201                }
 202        }
 203
 204        if (args.stateless_rpc) {
 205                conn = NULL;
 206                fd[0] = 0;
 207                fd[1] = 1;
 208        } else {
 209                int flags = args.verbose ? CONNECT_VERBOSE : 0;
 210                if (args.diag_url)
 211                        flags |= CONNECT_DIAG_URL;
 212                conn = git_connect(fd, dest, args.uploadpack,
 213                                   flags);
 214                if (!conn)
 215                        return args.diag_url ? 0 : 1;
 216        }
 217
 218        packet_reader_init(&reader, fd[0], NULL, 0,
 219                           PACKET_READ_CHOMP_NEWLINE |
 220                           PACKET_READ_GENTLE_ON_EOF);
 221
 222        version = discover_version(&reader);
 223        switch (version) {
 224        case protocol_v2:
 225                get_remote_refs(fd[1], &reader, &ref, 0, NULL, NULL);
 226                break;
 227        case protocol_v1:
 228        case protocol_v0:
 229                get_remote_heads(&reader, &ref, 0, NULL, &shallow);
 230                break;
 231        case protocol_unknown_version:
 232                BUG("unknown protocol version");
 233        }
 234
 235        ref = fetch_pack(&args, fd, conn, ref, dest, sought, nr_sought,
 236                         &shallow, pack_lockfile_ptr, version);
 237        if (pack_lockfile) {
 238                printf("lock %s\n", pack_lockfile);
 239                fflush(stdout);
 240        }
 241        if (args.check_self_contained_and_connected &&
 242            args.self_contained_and_connected) {
 243                printf("connectivity-ok\n");
 244                fflush(stdout);
 245        }
 246        close(fd[0]);
 247        close(fd[1]);
 248        if (finish_connect(conn))
 249                return 1;
 250
 251        ret = !ref;
 252
 253        /*
 254         * If the heads to pull were given, we should have consumed
 255         * all of them by matching the remote.  Otherwise, 'git fetch
 256         * remote no-such-ref' would silently succeed without issuing
 257         * an error.
 258         */
 259        ret |= report_unmatched_refs(sought, nr_sought);
 260
 261        while (ref) {
 262                printf("%s %s\n",
 263                       oid_to_hex(&ref->old_oid), ref->name);
 264                ref = ref->next;
 265        }
 266
 267        return ret;
 268}