3 #include "fetch-pack.h"
5 static const char fetch_pack_usage[] =
6 "git fetch-pack [--all] [--stdin] [--quiet|-q] [--keep|-k] [--thin] "
7 "[--include-tag] [--upload-pack=<git-upload-pack>] [--depth=<n>] "
8 "[--no-progress] [-v] [<host>:]<directory> [<refs>...]";
10 static void add_sought_entry_mem(struct ref ***sought, int *nr, int *alloc,
11 const char *name, int namelen)
13 struct ref *ref = xcalloc(1, sizeof(*ref) + namelen + 1);
15 memcpy(ref->name, name, namelen);
16 ref->name[namelen] = '\0';
18 ALLOC_GROW(*sought, *nr, *alloc);
19 (*sought)[*nr - 1] = ref;
22 static void add_sought_entry(struct ref ***sought, int *nr, int *alloc,
25 add_sought_entry_mem(sought, nr, alloc, string, strlen(string));
28 int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
31 struct ref *ref = NULL;
32 const char *dest = NULL;
33 struct ref **sought = NULL;
34 int nr_sought = 0, alloc_sought = 0;
36 char *pack_lockfile = NULL;
37 char **pack_lockfile_ptr = NULL;
38 struct child_process *conn;
39 struct fetch_pack_args args;
41 packet_trace_identity("fetch-pack");
43 memset(&args, 0, sizeof(args));
44 args.uploadpack = "git-upload-pack";
46 for (i = 1; i < argc && *argv[i] == '-'; i++) {
47 const char *arg = argv[i];
49 if (!prefixcmp(arg, "--upload-pack=")) {
50 args.uploadpack = arg + 14;
53 if (!prefixcmp(arg, "--exec=")) {
54 args.uploadpack = arg + 7;
57 if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
61 if (!strcmp("--keep", arg) || !strcmp("-k", arg)) {
62 args.lock_pack = args.keep_pack;
66 if (!strcmp("--thin", arg)) {
67 args.use_thin_pack = 1;
70 if (!strcmp("--include-tag", arg)) {
74 if (!strcmp("--all", arg)) {
78 if (!strcmp("--stdin", arg)) {
82 if (!strcmp("-v", arg)) {
86 if (!prefixcmp(arg, "--depth=")) {
87 args.depth = strtol(arg + 8, NULL, 0);
90 if (!strcmp("--no-progress", arg)) {
94 if (!strcmp("--stateless-rpc", arg)) {
95 args.stateless_rpc = 1;
98 if (!strcmp("--lock-pack", arg)) {
100 pack_lockfile_ptr = &pack_lockfile;
103 usage(fetch_pack_usage);
109 usage(fetch_pack_usage);
112 * Copy refs from cmdline to growable list, then append any
113 * refs from the standard input:
115 for (; i < argc; i++)
116 add_sought_entry(&sought, &nr_sought, &alloc_sought, argv[i]);
117 if (args.stdin_refs) {
118 if (args.stateless_rpc) {
119 /* in stateless RPC mode we use pkt-line to read
120 * from stdin, until we get a flush packet
123 char *line = packet_read_line(0, NULL);
126 add_sought_entry(&sought, &nr_sought, &alloc_sought, line);
130 /* read from stdin one ref per line, until EOF */
131 struct strbuf line = STRBUF_INIT;
132 while (strbuf_getline(&line, stdin, '\n') != EOF)
133 add_sought_entry(&sought, &nr_sought, &alloc_sought, line.buf);
134 strbuf_release(&line);
138 if (args.stateless_rpc) {
143 conn = git_connect(fd, dest, args.uploadpack,
144 args.verbose ? CONNECT_VERBOSE : 0);
147 get_remote_heads(fd[0], NULL, 0, &ref, 0, NULL);
149 ref = fetch_pack(&args, fd, conn, ref, dest,
150 sought, nr_sought, pack_lockfile_ptr);
152 printf("lock %s\n", pack_lockfile);
157 if (finish_connect(conn))
163 * If the heads to pull were given, we should have consumed
164 * all of them by matching the remote. Otherwise, 'git fetch
165 * remote no-such-ref' would silently succeed without issuing
168 for (i = 0; i < nr_sought; i++) {
169 if (!sought[i] || sought[i]->matched)
171 error("no such remote ref %s", sought[i]->name);
177 sha1_to_hex(ref->old_sha1), ref->name);