builtin / fetch-pack.con commit mingw: ensure temporary file handles are not inherited by child processes (05d1ed6)
   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 sha1_array shallow = SHA1_ARRAY_INIT;
  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 (starts_with(arg, "--upload-pack=")) {
  64                        args.uploadpack = arg + 14;
  65                        continue;
  66                }
  67                if (starts_with(arg, "--exec=")) {
  68                        args.uploadpack = arg + 7;
  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 (starts_with(arg, "--depth=")) {
 105                        args.depth = strtol(arg + 8, NULL, 0);
 106                        continue;
 107                }
 108                if (!strcmp("--no-progress", arg)) {
 109                        args.no_progress = 1;
 110                        continue;
 111                }
 112                if (!strcmp("--stateless-rpc", arg)) {
 113                        args.stateless_rpc = 1;
 114                        continue;
 115                }
 116                if (!strcmp("--lock-pack", arg)) {
 117                        args.lock_pack = 1;
 118                        pack_lockfile_ptr = &pack_lockfile;
 119                        continue;
 120                }
 121                if (!strcmp("--check-self-contained-and-connected", arg)) {
 122                        args.check_self_contained_and_connected = 1;
 123                        continue;
 124                }
 125                if (!strcmp("--cloning", arg)) {
 126                        args.cloning = 1;
 127                        continue;
 128                }
 129                if (!strcmp("--update-shallow", arg)) {
 130                        args.update_shallow = 1;
 131                        continue;
 132                }
 133                usage(fetch_pack_usage);
 134        }
 135
 136        if (i < argc)
 137                dest = argv[i++];
 138        else
 139                usage(fetch_pack_usage);
 140
 141        /*
 142         * Copy refs from cmdline to growable list, then append any
 143         * refs from the standard input:
 144         */
 145        for (; i < argc; i++)
 146                add_sought_entry(&sought, &nr_sought, &alloc_sought, argv[i]);
 147        if (args.stdin_refs) {
 148                if (args.stateless_rpc) {
 149                        /* in stateless RPC mode we use pkt-line to read
 150                         * from stdin, until we get a flush packet
 151                         */
 152                        for (;;) {
 153                                char *line = packet_read_line(0, NULL);
 154                                if (!line)
 155                                        break;
 156                                add_sought_entry(&sought, &nr_sought,  &alloc_sought, line);
 157                        }
 158                }
 159                else {
 160                        /* read from stdin one ref per line, until EOF */
 161                        struct strbuf line = STRBUF_INIT;
 162                        while (strbuf_getline_lf(&line, stdin) != EOF)
 163                                add_sought_entry(&sought, &nr_sought, &alloc_sought, line.buf);
 164                        strbuf_release(&line);
 165                }
 166        }
 167
 168        if (args.stateless_rpc) {
 169                conn = NULL;
 170                fd[0] = 0;
 171                fd[1] = 1;
 172        } else {
 173                int flags = args.verbose ? CONNECT_VERBOSE : 0;
 174                if (args.diag_url)
 175                        flags |= CONNECT_DIAG_URL;
 176                conn = git_connect(fd, dest, args.uploadpack,
 177                                   flags);
 178                if (!conn)
 179                        return args.diag_url ? 0 : 1;
 180        }
 181        get_remote_heads(fd[0], NULL, 0, &ref, 0, NULL, &shallow);
 182
 183        ref = fetch_pack(&args, fd, conn, ref, dest, sought, nr_sought,
 184                         &shallow, pack_lockfile_ptr);
 185        if (pack_lockfile) {
 186                printf("lock %s\n", pack_lockfile);
 187                fflush(stdout);
 188        }
 189        if (args.check_self_contained_and_connected &&
 190            args.self_contained_and_connected) {
 191                printf("connectivity-ok\n");
 192                fflush(stdout);
 193        }
 194        close(fd[0]);
 195        close(fd[1]);
 196        if (finish_connect(conn))
 197                return 1;
 198
 199        ret = !ref;
 200
 201        /*
 202         * If the heads to pull were given, we should have consumed
 203         * all of them by matching the remote.  Otherwise, 'git fetch
 204         * remote no-such-ref' would silently succeed without issuing
 205         * an error.
 206         */
 207        for (i = 0; i < nr_sought; i++) {
 208                if (!sought[i] || sought[i]->matched)
 209                        continue;
 210                error("no such remote ref %s", sought[i]->name);
 211                ret = 1;
 212        }
 213
 214        while (ref) {
 215                printf("%s %s\n",
 216                       oid_to_hex(&ref->old_oid), ref->name);
 217                ref = ref->next;
 218        }
 219
 220        return ret;
 221}