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(struct ref ***sought, int *nr, int *alloc,
19 if (!get_oid_hex(name, &oid) && name[GIT_SHA1_HEXSZ] == ' ')
20 name += GIT_SHA1_HEXSZ + 1;
24 ref = alloc_ref(name);
25 oidcpy(&ref->old_oid, &oid);
27 ALLOC_GROW(*sought, *nr, *alloc);
28 (*sought)[*nr - 1] = ref;
31 int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
34 struct ref *ref = NULL;
35 const char *dest = NULL;
36 struct ref **sought = NULL;
37 int nr_sought = 0, alloc_sought = 0;
39 char *pack_lockfile = NULL;
40 char **pack_lockfile_ptr = NULL;
41 struct child_process *conn;
42 struct fetch_pack_args args;
43 struct sha1_array shallow = SHA1_ARRAY_INIT;
45 packet_trace_identity("fetch-pack");
47 memset(&args, 0, sizeof(args));
48 args.uploadpack = "git-upload-pack";
50 for (i = 1; i < argc && *argv[i] == '-'; i++) {
51 const char *arg = argv[i];
53 if (starts_with(arg, "--upload-pack=")) {
54 args.uploadpack = arg + 14;
57 if (starts_with(arg, "--exec=")) {
58 args.uploadpack = arg + 7;
61 if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
65 if (!strcmp("--keep", arg) || !strcmp("-k", arg)) {
66 args.lock_pack = args.keep_pack;
70 if (!strcmp("--thin", arg)) {
71 args.use_thin_pack = 1;
74 if (!strcmp("--include-tag", arg)) {
78 if (!strcmp("--all", arg)) {
82 if (!strcmp("--stdin", arg)) {
86 if (!strcmp("--diag-url", arg)) {
90 if (!strcmp("-v", arg)) {
94 if (starts_with(arg, "--depth=")) {
95 args.depth = strtol(arg + 8, NULL, 0);
98 if (!strcmp("--no-progress", arg)) {
102 if (!strcmp("--stateless-rpc", arg)) {
103 args.stateless_rpc = 1;
106 if (!strcmp("--lock-pack", arg)) {
108 pack_lockfile_ptr = &pack_lockfile;
111 if (!strcmp("--check-self-contained-and-connected", arg)) {
112 args.check_self_contained_and_connected = 1;
115 if (!strcmp("--cloning", arg)) {
119 if (!strcmp("--update-shallow", arg)) {
120 args.update_shallow = 1;
123 usage(fetch_pack_usage);
129 usage(fetch_pack_usage);
132 * Copy refs from cmdline to growable list, then append any
133 * refs from the standard input:
135 for (; i < argc; i++)
136 add_sought_entry(&sought, &nr_sought, &alloc_sought, argv[i]);
137 if (args.stdin_refs) {
138 if (args.stateless_rpc) {
139 /* in stateless RPC mode we use pkt-line to read
140 * from stdin, until we get a flush packet
143 char *line = packet_read_line(0, NULL);
146 add_sought_entry(&sought, &nr_sought, &alloc_sought, line);
150 /* read from stdin one ref per line, until EOF */
151 struct strbuf line = STRBUF_INIT;
152 while (strbuf_getline_lf(&line, stdin) != EOF)
153 add_sought_entry(&sought, &nr_sought, &alloc_sought, line.buf);
154 strbuf_release(&line);
158 if (args.stateless_rpc) {
163 int flags = args.verbose ? CONNECT_VERBOSE : 0;
165 flags |= CONNECT_DIAG_URL;
166 conn = git_connect(fd, dest, args.uploadpack,
169 return args.diag_url ? 0 : 1;
171 get_remote_heads(fd[0], NULL, 0, &ref, 0, NULL, &shallow);
173 ref = fetch_pack(&args, fd, conn, ref, dest, sought, nr_sought,
174 &shallow, pack_lockfile_ptr);
176 printf("lock %s\n", pack_lockfile);
179 if (args.check_self_contained_and_connected &&
180 args.self_contained_and_connected) {
181 printf("connectivity-ok\n");
186 if (finish_connect(conn))
192 * If the heads to pull were given, we should have consumed
193 * all of them by matching the remote. Otherwise, 'git fetch
194 * remote no-such-ref' would silently succeed without issuing
197 for (i = 0; i < nr_sought; i++) {
198 if (!sought[i] || sought[i]->matched)
200 error("no such remote ref %s", sought[i]->name);
206 oid_to_hex(&ref->old_oid), ref->name);