3 #include "fetch-pack.h"
6 #include "sha1-array.h"
8 static 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>...]";
13 static void add_sought_entry_mem(struct ref ***sought, int *nr, int *alloc,
14 const char *name, int namelen)
16 struct ref *ref = xcalloc(1, sizeof(*ref) + namelen + 1);
18 const int chunksz = GIT_SHA1_HEXSZ + 1;
20 if (namelen > chunksz && name[chunksz - 1] == ' ' &&
21 !get_oid_hex(name, &oid)) {
22 oidcpy(&ref->old_oid, &oid);
27 memcpy(ref->name, name, namelen);
28 ref->name[namelen] = '\0';
30 ALLOC_GROW(*sought, *nr, *alloc);
31 (*sought)[*nr - 1] = ref;
34 static void add_sought_entry(struct ref ***sought, int *nr, int *alloc,
37 add_sought_entry_mem(sought, nr, alloc, string, strlen(string));
40 int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
43 struct ref *ref = NULL;
44 const char *dest = NULL;
45 struct ref **sought = NULL;
46 int nr_sought = 0, alloc_sought = 0;
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;
55 packet_trace_identity("fetch-pack");
57 memset(&args, 0, sizeof(args));
58 args.uploadpack = "git-upload-pack";
60 for (i = 1; i < argc && *argv[i] == '-'; i++) {
61 const char *arg = argv[i];
63 if (skip_prefix(arg, "--upload-pack=", &arg)) {
64 args.uploadpack = arg;
67 if (skip_prefix(arg, "--exec=", &arg)) {
68 args.uploadpack = arg;
71 if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
75 if (!strcmp("--keep", arg) || !strcmp("-k", arg)) {
76 args.lock_pack = args.keep_pack;
80 if (!strcmp("--thin", arg)) {
81 args.use_thin_pack = 1;
84 if (!strcmp("--include-tag", arg)) {
88 if (!strcmp("--all", arg)) {
92 if (!strcmp("--stdin", arg)) {
96 if (!strcmp("--diag-url", arg)) {
100 if (!strcmp("-v", arg)) {
104 if (skip_prefix(arg, "--depth=", &arg)) {
105 args.depth = strtol(arg, NULL, 0);
108 if (skip_prefix(arg, "--shallow-since=", &arg)) {
109 args.deepen_since = xstrdup(arg);
112 if (skip_prefix(arg, "--shallow-exclude=", &arg)) {
113 string_list_append(&deepen_not, arg);
116 if (!strcmp(arg, "--deepen-relative")) {
117 args.deepen_relative = 1;
120 if (!strcmp("--no-progress", arg)) {
121 args.no_progress = 1;
124 if (!strcmp("--stateless-rpc", arg)) {
125 args.stateless_rpc = 1;
128 if (!strcmp("--lock-pack", arg)) {
130 pack_lockfile_ptr = &pack_lockfile;
133 if (!strcmp("--check-self-contained-and-connected", arg)) {
134 args.check_self_contained_and_connected = 1;
137 if (!strcmp("--cloning", arg)) {
141 if (!strcmp("--update-shallow", arg)) {
142 args.update_shallow = 1;
145 usage(fetch_pack_usage);
148 args.deepen_not = &deepen_not;
153 usage(fetch_pack_usage);
156 * Copy refs from cmdline to growable list, then append any
157 * refs from the standard input:
159 for (; i < argc; i++)
160 add_sought_entry(&sought, &nr_sought, &alloc_sought, argv[i]);
161 if (args.stdin_refs) {
162 if (args.stateless_rpc) {
163 /* in stateless RPC mode we use pkt-line to read
164 * from stdin, until we get a flush packet
167 char *line = packet_read_line(0, NULL);
170 add_sought_entry(&sought, &nr_sought, &alloc_sought, line);
174 /* read from stdin one ref per line, until EOF */
175 struct strbuf line = STRBUF_INIT;
176 while (strbuf_getline_lf(&line, stdin) != EOF)
177 add_sought_entry(&sought, &nr_sought, &alloc_sought, line.buf);
178 strbuf_release(&line);
182 if (args.stateless_rpc) {
187 int flags = args.verbose ? CONNECT_VERBOSE : 0;
189 flags |= CONNECT_DIAG_URL;
190 conn = git_connect(fd, dest, args.uploadpack,
193 return args.diag_url ? 0 : 1;
195 get_remote_heads(fd[0], NULL, 0, &ref, 0, NULL, &shallow);
197 ref = fetch_pack(&args, fd, conn, ref, dest, sought, nr_sought,
198 &shallow, pack_lockfile_ptr);
200 printf("lock %s\n", pack_lockfile);
203 if (args.check_self_contained_and_connected &&
204 args.self_contained_and_connected) {
205 printf("connectivity-ok\n");
210 if (finish_connect(conn))
216 * If the heads to pull were given, we should have consumed
217 * all of them by matching the remote. Otherwise, 'git fetch
218 * remote no-such-ref' would silently succeed without issuing
221 for (i = 0; i < nr_sought; i++) {
222 if (!sought[i] || sought[i]->matched)
224 error("no such remote ref %s", sought[i]->name);
230 oid_to_hex(&ref->old_oid), ref->name);