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