3 #include "fetch-pack.h"
6 #include "sha1-array.h"
9 static const char fetch_pack_usage[] =
10 "git fetch-pack [--all] [--stdin] [--quiet | -q] [--keep | -k] [--thin] "
11 "[--include-tag] [--upload-pack=<git-upload-pack>] [--depth=<n>] "
12 "[--no-progress] [--diag-url] [-v] [<host>:]<directory> [<refs>...]";
14 static void add_sought_entry(struct ref ***sought, int *nr, int *alloc,
20 if (!get_oid_hex(name, &oid)) {
21 if (name[GIT_SHA1_HEXSZ] == ' ') {
22 /* <sha1> <ref>, find refname */
23 name += GIT_SHA1_HEXSZ + 1;
24 } else if (name[GIT_SHA1_HEXSZ] == '\0') {
25 ; /* <sha1>, leave sha1 as name */
27 /* <ref>, clear cruft from oid */
31 /* <ref>, clear cruft from get_oid_hex */
35 ref = alloc_ref(name);
36 oidcpy(&ref->old_oid, &oid);
38 ALLOC_GROW(*sought, *nr, *alloc);
39 (*sought)[*nr - 1] = ref;
42 int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
45 struct ref *ref = NULL;
46 const char *dest = NULL;
47 struct ref **sought = NULL;
48 int nr_sought = 0, alloc_sought = 0;
50 char *pack_lockfile = NULL;
51 char **pack_lockfile_ptr = NULL;
52 struct child_process *conn;
53 struct fetch_pack_args args;
54 struct oid_array shallow = OID_ARRAY_INIT;
55 struct string_list deepen_not = STRING_LIST_INIT_DUP;
56 struct packet_reader reader;
60 packet_trace_identity("fetch-pack");
62 memset(&args, 0, sizeof(args));
63 args.uploadpack = "git-upload-pack";
65 for (i = 1; i < argc && *argv[i] == '-'; i++) {
66 const char *arg = argv[i];
68 if (skip_prefix(arg, "--upload-pack=", &arg)) {
69 args.uploadpack = arg;
72 if (skip_prefix(arg, "--exec=", &arg)) {
73 args.uploadpack = arg;
76 if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
80 if (!strcmp("--keep", arg) || !strcmp("-k", arg)) {
81 args.lock_pack = args.keep_pack;
85 if (!strcmp("--thin", arg)) {
86 args.use_thin_pack = 1;
89 if (!strcmp("--include-tag", arg)) {
93 if (!strcmp("--all", arg)) {
97 if (!strcmp("--stdin", arg)) {
101 if (!strcmp("--diag-url", arg)) {
105 if (!strcmp("-v", arg)) {
109 if (skip_prefix(arg, "--depth=", &arg)) {
110 args.depth = strtol(arg, NULL, 0);
113 if (skip_prefix(arg, "--shallow-since=", &arg)) {
114 args.deepen_since = xstrdup(arg);
117 if (skip_prefix(arg, "--shallow-exclude=", &arg)) {
118 string_list_append(&deepen_not, arg);
121 if (!strcmp(arg, "--deepen-relative")) {
122 args.deepen_relative = 1;
125 if (!strcmp("--no-progress", arg)) {
126 args.no_progress = 1;
129 if (!strcmp("--stateless-rpc", arg)) {
130 args.stateless_rpc = 1;
133 if (!strcmp("--lock-pack", arg)) {
135 pack_lockfile_ptr = &pack_lockfile;
138 if (!strcmp("--check-self-contained-and-connected", arg)) {
139 args.check_self_contained_and_connected = 1;
142 if (!strcmp("--cloning", arg)) {
146 if (!strcmp("--update-shallow", arg)) {
147 args.update_shallow = 1;
150 if (!strcmp("--from-promisor", arg)) {
151 args.from_promisor = 1;
154 if (!strcmp("--no-dependents", arg)) {
155 args.no_dependents = 1;
158 if (skip_prefix(arg, ("--" CL_ARG__FILTER "="), &arg)) {
159 parse_list_objects_filter(&args.filter_options, arg);
162 if (!strcmp(arg, ("--no-" CL_ARG__FILTER))) {
163 list_objects_filter_set_no_filter(&args.filter_options);
166 usage(fetch_pack_usage);
169 args.deepen_not = &deepen_not;
174 usage(fetch_pack_usage);
177 * Copy refs from cmdline to growable list, then append any
178 * refs from the standard input:
180 for (; i < argc; i++)
181 add_sought_entry(&sought, &nr_sought, &alloc_sought, argv[i]);
182 if (args.stdin_refs) {
183 if (args.stateless_rpc) {
184 /* in stateless RPC mode we use pkt-line to read
185 * from stdin, until we get a flush packet
188 char *line = packet_read_line(0, NULL);
191 add_sought_entry(&sought, &nr_sought, &alloc_sought, line);
195 /* read from stdin one ref per line, until EOF */
196 struct strbuf line = STRBUF_INIT;
197 while (strbuf_getline_lf(&line, stdin) != EOF)
198 add_sought_entry(&sought, &nr_sought, &alloc_sought, line.buf);
199 strbuf_release(&line);
203 if (args.stateless_rpc) {
208 int flags = args.verbose ? CONNECT_VERBOSE : 0;
210 flags |= CONNECT_DIAG_URL;
211 conn = git_connect(fd, dest, args.uploadpack,
214 return args.diag_url ? 0 : 1;
217 packet_reader_init(&reader, fd[0], NULL, 0,
218 PACKET_READ_CHOMP_NEWLINE |
219 PACKET_READ_GENTLE_ON_EOF);
221 switch (discover_version(&reader)) {
223 die("support for protocol v2 not implemented yet");
226 get_remote_heads(&reader, &ref, 0, NULL, &shallow);
228 case protocol_unknown_version:
229 BUG("unknown protocol version");
232 ref = fetch_pack(&args, fd, conn, ref, dest, sought, nr_sought,
233 &shallow, pack_lockfile_ptr, protocol_v0);
235 printf("lock %s\n", pack_lockfile);
238 if (args.check_self_contained_and_connected &&
239 args.self_contained_and_connected) {
240 printf("connectivity-ok\n");
245 if (finish_connect(conn))
251 * If the heads to pull were given, we should have consumed
252 * all of them by matching the remote. Otherwise, 'git fetch
253 * remote no-such-ref' would silently succeed without issuing
256 ret |= report_unmatched_refs(sought, nr_sought);
260 oid_to_hex(&ref->old_oid), ref->name);