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
,
21 if (!parse_oid_hex(name
, &oid
, &p
)) {
23 /* <oid> <ref>, find refname */
25 } else if (*p
== '\0') {
26 ; /* <oid>, leave oid as name */
28 /* <ref>, clear cruft from oid */
32 /* <ref>, clear cruft from get_oid_hex */
36 ref
= alloc_ref(name
);
37 oidcpy(&ref
->old_oid
, &oid
);
39 ALLOC_GROW(*sought
, *nr
, *alloc
);
40 (*sought
)[*nr
- 1] = ref
;
43 int cmd_fetch_pack(int argc
, const char **argv
, const char *prefix
)
46 struct ref
*ref
= NULL
;
47 const char *dest
= NULL
;
48 struct ref
**sought
= NULL
;
49 int nr_sought
= 0, alloc_sought
= 0;
51 char *pack_lockfile
= NULL
;
52 char **pack_lockfile_ptr
= NULL
;
53 struct child_process
*conn
;
54 struct fetch_pack_args args
;
55 struct oid_array shallow
= OID_ARRAY_INIT
;
56 struct string_list deepen_not
= STRING_LIST_INIT_DUP
;
57 struct packet_reader reader
;
61 packet_trace_identity("fetch-pack");
63 memset(&args
, 0, sizeof(args
));
64 args
.uploadpack
= "git-upload-pack";
66 for (i
= 1; i
< argc
&& *argv
[i
] == '-'; i
++) {
67 const char *arg
= argv
[i
];
69 if (skip_prefix(arg
, "--upload-pack=", &arg
)) {
70 args
.uploadpack
= arg
;
73 if (skip_prefix(arg
, "--exec=", &arg
)) {
74 args
.uploadpack
= arg
;
77 if (!strcmp("--quiet", arg
) || !strcmp("-q", arg
)) {
81 if (!strcmp("--keep", arg
) || !strcmp("-k", arg
)) {
82 args
.lock_pack
= args
.keep_pack
;
86 if (!strcmp("--thin", arg
)) {
87 args
.use_thin_pack
= 1;
90 if (!strcmp("--include-tag", arg
)) {
94 if (!strcmp("--all", arg
)) {
98 if (!strcmp("--stdin", arg
)) {
102 if (!strcmp("--diag-url", arg
)) {
106 if (!strcmp("-v", arg
)) {
110 if (skip_prefix(arg
, "--depth=", &arg
)) {
111 args
.depth
= strtol(arg
, NULL
, 0);
114 if (skip_prefix(arg
, "--shallow-since=", &arg
)) {
115 args
.deepen_since
= xstrdup(arg
);
118 if (skip_prefix(arg
, "--shallow-exclude=", &arg
)) {
119 string_list_append(&deepen_not
, arg
);
122 if (!strcmp(arg
, "--deepen-relative")) {
123 args
.deepen_relative
= 1;
126 if (!strcmp("--no-progress", arg
)) {
127 args
.no_progress
= 1;
130 if (!strcmp("--stateless-rpc", arg
)) {
131 args
.stateless_rpc
= 1;
134 if (!strcmp("--lock-pack", arg
)) {
136 pack_lockfile_ptr
= &pack_lockfile
;
139 if (!strcmp("--check-self-contained-and-connected", arg
)) {
140 args
.check_self_contained_and_connected
= 1;
143 if (!strcmp("--cloning", arg
)) {
147 if (!strcmp("--update-shallow", arg
)) {
148 args
.update_shallow
= 1;
151 if (!strcmp("--from-promisor", arg
)) {
152 args
.from_promisor
= 1;
155 if (!strcmp("--no-dependents", arg
)) {
156 args
.no_dependents
= 1;
159 if (skip_prefix(arg
, ("--" CL_ARG__FILTER
"="), &arg
)) {
160 parse_list_objects_filter(&args
.filter_options
, arg
);
163 if (!strcmp(arg
, ("--no-" CL_ARG__FILTER
))) {
164 list_objects_filter_set_no_filter(&args
.filter_options
);
167 usage(fetch_pack_usage
);
170 args
.deepen_not
= &deepen_not
;
175 usage(fetch_pack_usage
);
178 * Copy refs from cmdline to growable list, then append any
179 * refs from the standard input:
181 for (; i
< argc
; i
++)
182 add_sought_entry(&sought
, &nr_sought
, &alloc_sought
, argv
[i
]);
183 if (args
.stdin_refs
) {
184 if (args
.stateless_rpc
) {
185 /* in stateless RPC mode we use pkt-line to read
186 * from stdin, until we get a flush packet
189 char *line
= packet_read_line(0, NULL
);
192 add_sought_entry(&sought
, &nr_sought
, &alloc_sought
, line
);
196 /* read from stdin one ref per line, until EOF */
197 struct strbuf line
= STRBUF_INIT
;
198 while (strbuf_getline_lf(&line
, stdin
) != EOF
)
199 add_sought_entry(&sought
, &nr_sought
, &alloc_sought
, line
.buf
);
200 strbuf_release(&line
);
204 if (args
.stateless_rpc
) {
209 int flags
= args
.verbose
? CONNECT_VERBOSE
: 0;
211 flags
|= CONNECT_DIAG_URL
;
212 conn
= git_connect(fd
, dest
, args
.uploadpack
,
215 return args
.diag_url
? 0 : 1;
218 packet_reader_init(&reader
, fd
[0], NULL
, 0,
219 PACKET_READ_CHOMP_NEWLINE
|
220 PACKET_READ_GENTLE_ON_EOF
);
222 switch (discover_version(&reader
)) {
224 die("support for protocol v2 not implemented yet");
227 get_remote_heads(&reader
, &ref
, 0, NULL
, &shallow
);
229 case protocol_unknown_version
:
230 BUG("unknown protocol version");
233 ref
= fetch_pack(&args
, fd
, conn
, ref
, dest
, sought
, nr_sought
,
234 &shallow
, pack_lockfile_ptr
, protocol_v0
);
236 printf("lock %s\n", pack_lockfile
);
239 if (args
.check_self_contained_and_connected
&&
240 args
.self_contained_and_connected
) {
241 printf("connectivity-ok\n");
246 if (finish_connect(conn
))
252 * If the heads to pull were given, we should have consumed
253 * all of them by matching the remote. Otherwise, 'git fetch
254 * remote no-such-ref' would silently succeed without issuing
257 ret
|= report_unmatched_refs(sought
, nr_sought
);
261 oid_to_hex(&ref
->old_oid
), ref
->name
);