3 #include "fetch-pack.h"
7 static const char fetch_pack_usage[] =
8 "git fetch-pack [--all] [--stdin] [--quiet|-q] [--keep|-k] [--thin] "
9 "[--include-tag] [--upload-pack=<git-upload-pack>] [--depth=<n>] "
10 "[--no-progress] [-v] [<host>:]<directory> [<refs>...]";
12 static void add_sought_entry_mem(struct ref ***sought, int *nr, int *alloc,
13 const char *name, int namelen)
15 struct ref *ref = xcalloc(1, sizeof(*ref) + namelen + 1);
17 memcpy(ref->name, name, namelen);
18 ref->name[namelen] = '\0';
20 ALLOC_GROW(*sought, *nr, *alloc);
21 (*sought)[*nr - 1] = ref;
24 static void add_sought_entry(struct ref ***sought, int *nr, int *alloc,
27 add_sought_entry_mem(sought, nr, alloc, string, strlen(string));
30 int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
33 struct ref *ref = NULL;
34 const char *dest = NULL;
35 struct ref **sought = NULL;
36 int nr_sought = 0, alloc_sought = 0;
38 char *pack_lockfile = NULL;
39 char **pack_lockfile_ptr = NULL;
40 struct child_process *conn;
41 struct fetch_pack_args args;
43 packet_trace_identity("fetch-pack");
45 memset(&args, 0, sizeof(args));
46 args.uploadpack = "git-upload-pack";
48 for (i = 1; i < argc && *argv[i] == '-'; i++) {
49 const char *arg = argv[i];
51 if (!prefixcmp(arg, "--upload-pack=")) {
52 args.uploadpack = arg + 14;
55 if (!prefixcmp(arg, "--exec=")) {
56 args.uploadpack = arg + 7;
59 if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
63 if (!strcmp("--keep", arg) || !strcmp("-k", arg)) {
64 args.lock_pack = args.keep_pack;
68 if (!strcmp("--thin", arg)) {
69 args.use_thin_pack = 1;
72 if (!strcmp("--include-tag", arg)) {
76 if (!strcmp("--all", arg)) {
80 if (!strcmp("--stdin", arg)) {
84 if (!strcmp("-v", arg)) {
88 if (!prefixcmp(arg, "--depth=")) {
89 args.depth = strtol(arg + 8, NULL, 0);
92 if (!strcmp("--no-progress", arg)) {
96 if (!strcmp("--stateless-rpc", arg)) {
97 args.stateless_rpc = 1;
100 if (!strcmp("--lock-pack", arg)) {
102 pack_lockfile_ptr = &pack_lockfile;
105 if (!strcmp("--check-self-contained-and-connected", arg)) {
106 args.check_self_contained_and_connected = 1;
109 usage(fetch_pack_usage);
115 usage(fetch_pack_usage);
118 * Copy refs from cmdline to growable list, then append any
119 * refs from the standard input:
121 for (; i < argc; i++)
122 add_sought_entry(&sought, &nr_sought, &alloc_sought, argv[i]);
123 if (args.stdin_refs) {
124 if (args.stateless_rpc) {
125 /* in stateless RPC mode we use pkt-line to read
126 * from stdin, until we get a flush packet
129 char *line = packet_read_line(0, NULL);
132 add_sought_entry(&sought, &nr_sought, &alloc_sought, line);
136 /* read from stdin one ref per line, until EOF */
137 struct strbuf line = STRBUF_INIT;
138 while (strbuf_getline(&line, stdin, '\n') != EOF)
139 add_sought_entry(&sought, &nr_sought, &alloc_sought, line.buf);
140 strbuf_release(&line);
144 if (args.stateless_rpc) {
149 conn = git_connect(fd, dest, args.uploadpack,
150 args.verbose ? CONNECT_VERBOSE : 0);
153 get_remote_heads(fd[0], NULL, 0, &ref, 0, NULL);
155 ref = fetch_pack(&args, fd, conn, ref, dest,
156 sought, nr_sought, pack_lockfile_ptr);
158 printf("lock %s\n", pack_lockfile);
161 if (args.check_self_contained_and_connected &&
162 args.self_contained_and_connected) {
163 printf("connectivity-ok\n");
168 if (finish_connect(conn))
174 * If the heads to pull were given, we should have consumed
175 * all of them by matching the remote. Otherwise, 'git fetch
176 * remote no-such-ref' would silently succeed without issuing
179 for (i = 0; i < nr_sought; i++) {
180 if (!sought[i] || sought[i]->matched)
182 error("no such remote ref %s", sought[i]->name);
188 sha1_to_hex(ref->old_sha1), ref->name);