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