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);
17 unsigned char sha1[20];
19 if (namelen > 41 && name[40] == ' ' && !get_sha1_hex(name, sha1)) {
20 hashcpy(ref->old_sha1, sha1);
25 memcpy(ref->name, name, namelen);
26 ref->name[namelen] = '\0';
28 ALLOC_GROW(*sought, *nr, *alloc);
29 (*sought)[*nr - 1] = ref;
32 static void add_sought_entry(struct ref ***sought, int *nr, int *alloc,
35 add_sought_entry_mem(sought, nr, alloc, string, strlen(string));
38 int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
41 struct ref *ref = NULL;
42 const char *dest = NULL;
43 struct ref **sought = NULL;
44 int nr_sought = 0, alloc_sought = 0;
46 char *pack_lockfile = NULL;
47 char **pack_lockfile_ptr = NULL;
48 struct child_process *conn;
49 struct fetch_pack_args args;
50 struct sha1_array shallow = SHA1_ARRAY_INIT;
52 packet_trace_identity("fetch-pack");
54 memset(&args, 0, sizeof(args));
55 args.uploadpack = "git-upload-pack";
57 for (i = 1; i < argc && *argv[i] == '-'; i++) {
58 const char *arg = argv[i];
60 if (starts_with(arg, "--upload-pack=")) {
61 args.uploadpack = arg + 14;
64 if (starts_with(arg, "--exec=")) {
65 args.uploadpack = arg + 7;
68 if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
72 if (!strcmp("--keep", arg) || !strcmp("-k", arg)) {
73 args.lock_pack = args.keep_pack;
77 if (!strcmp("--thin", arg)) {
78 args.use_thin_pack = 1;
81 if (!strcmp("--include-tag", arg)) {
85 if (!strcmp("--all", arg)) {
89 if (!strcmp("--stdin", arg)) {
93 if (!strcmp("--diag-url", arg)) {
97 if (!strcmp("-v", arg)) {
101 if (starts_with(arg, "--depth=")) {
102 args.depth = strtol(arg + 8, NULL, 0);
105 if (!strcmp("--no-progress", arg)) {
106 args.no_progress = 1;
109 if (!strcmp("--stateless-rpc", arg)) {
110 args.stateless_rpc = 1;
113 if (!strcmp("--lock-pack", arg)) {
115 pack_lockfile_ptr = &pack_lockfile;
118 if (!strcmp("--check-self-contained-and-connected", arg)) {
119 args.check_self_contained_and_connected = 1;
122 if (!strcmp("--cloning", arg)) {
126 if (!strcmp("--update-shallow", arg)) {
127 args.update_shallow = 1;
130 usage(fetch_pack_usage);
136 usage(fetch_pack_usage);
139 * Copy refs from cmdline to growable list, then append any
140 * refs from the standard input:
142 for (; i < argc; i++)
143 add_sought_entry(&sought, &nr_sought, &alloc_sought, argv[i]);
144 if (args.stdin_refs) {
145 if (args.stateless_rpc) {
146 /* in stateless RPC mode we use pkt-line to read
147 * from stdin, until we get a flush packet
150 char *line = packet_read_line(0, NULL);
153 add_sought_entry(&sought, &nr_sought, &alloc_sought, line);
157 /* read from stdin one ref per line, until EOF */
158 struct strbuf line = STRBUF_INIT;
159 while (strbuf_getline(&line, stdin, '\n') != EOF)
160 add_sought_entry(&sought, &nr_sought, &alloc_sought, line.buf);
161 strbuf_release(&line);
165 if (args.stateless_rpc) {
170 int flags = args.verbose ? CONNECT_VERBOSE : 0;
172 flags |= CONNECT_DIAG_URL;
173 conn = git_connect(fd, dest, args.uploadpack,
176 return args.diag_url ? 0 : 1;
178 get_remote_heads(fd[0], NULL, 0, &ref, 0, NULL, &shallow);
180 ref = fetch_pack(&args, fd, conn, ref, dest, sought, nr_sought,
181 &shallow, pack_lockfile_ptr);
183 printf("lock %s\n", pack_lockfile);
186 if (args.check_self_contained_and_connected &&
187 args.self_contained_and_connected) {
188 printf("connectivity-ok\n");
193 if (finish_connect(conn))
199 * If the heads to pull were given, we should have consumed
200 * all of them by matching the remote. Otherwise, 'git fetch
201 * remote no-such-ref' would silently succeed without issuing
204 for (i = 0; i < nr_sought; i++) {
205 if (!sought[i] || sought[i]->matched)
207 error("no such remote ref %s", sought[i]->name);
213 sha1_to_hex(ref->old_sha1), ref->name);