5 #include "object-file.h"
7 #include "fetch-pack.h"
10 #include "oid-array.h"
13 static const char fetch_pack_usage
[] =
14 "git fetch-pack [--all] [--stdin] [--quiet | -q] [--keep | -k] [--thin] "
15 "[--include-tag] [--upload-pack=<git-upload-pack>] [--depth=<n>] "
16 "[--no-progress] [--diag-url] [-v] [<host>:]<directory> [<refs>...]";
18 static void add_sought_entry(struct ref
***sought
, int *nr
, int *alloc
,
25 if (!parse_oid_hex(name
, &oid
, &p
)) {
27 /* <oid> <ref>, find refname */
29 } else if (*p
== '\0') {
30 ; /* <oid>, leave oid as name */
32 /* <ref>, clear cruft from oid */
36 /* <ref>, clear cruft from get_oid_hex */
40 ref
= alloc_ref(name
);
41 oidcpy(&ref
->old_oid
, &oid
);
43 ALLOC_GROW(*sought
, *nr
, *alloc
);
44 (*sought
)[*nr
- 1] = ref
;
47 int cmd_fetch_pack(int argc
, const char **argv
, const char *prefix UNUSED
)
50 struct ref
*ref
= NULL
;
51 const char *dest
= NULL
;
52 struct ref
**sought
= NULL
;
53 int nr_sought
= 0, alloc_sought
= 0;
55 struct string_list pack_lockfiles
= STRING_LIST_INIT_DUP
;
56 struct string_list
*pack_lockfiles_ptr
= NULL
;
57 struct child_process
*conn
;
58 struct fetch_pack_args args
;
59 struct oid_array shallow
= OID_ARRAY_INIT
;
60 struct string_list deepen_not
= STRING_LIST_INIT_DUP
;
61 struct packet_reader reader
;
62 enum protocol_version version
;
66 packet_trace_identity("fetch-pack");
68 memset(&args
, 0, sizeof(args
));
69 list_objects_filter_init(&args
.filter_options
);
70 args
.uploadpack
= "git-upload-pack";
72 for (i
= 1; i
< argc
&& *argv
[i
] == '-'; i
++) {
73 const char *arg
= argv
[i
];
75 if (skip_prefix(arg
, "--upload-pack=", &arg
)) {
76 args
.uploadpack
= arg
;
79 if (skip_prefix(arg
, "--exec=", &arg
)) {
80 args
.uploadpack
= arg
;
83 if (!strcmp("--quiet", arg
) || !strcmp("-q", arg
)) {
87 if (!strcmp("--keep", arg
) || !strcmp("-k", arg
)) {
88 args
.lock_pack
= args
.keep_pack
;
92 if (!strcmp("--thin", arg
)) {
93 args
.use_thin_pack
= 1;
96 if (!strcmp("--include-tag", arg
)) {
100 if (!strcmp("--all", arg
)) {
104 if (!strcmp("--stdin", arg
)) {
108 if (!strcmp("--diag-url", arg
)) {
112 if (!strcmp("-v", arg
)) {
116 if (skip_prefix(arg
, "--depth=", &arg
)) {
117 args
.depth
= strtol(arg
, NULL
, 0);
120 if (skip_prefix(arg
, "--shallow-since=", &arg
)) {
121 args
.deepen_since
= xstrdup(arg
);
124 if (skip_prefix(arg
, "--shallow-exclude=", &arg
)) {
125 string_list_append(&deepen_not
, arg
);
128 if (!strcmp(arg
, "--deepen-relative")) {
129 args
.deepen_relative
= 1;
132 if (!strcmp("--no-progress", arg
)) {
133 args
.no_progress
= 1;
136 if (!strcmp("--stateless-rpc", arg
)) {
137 args
.stateless_rpc
= 1;
140 if (!strcmp("--lock-pack", arg
)) {
142 pack_lockfiles_ptr
= &pack_lockfiles
;
145 if (!strcmp("--check-self-contained-and-connected", arg
)) {
146 args
.check_self_contained_and_connected
= 1;
149 if (!strcmp("--cloning", arg
)) {
153 if (!strcmp("--update-shallow", arg
)) {
154 args
.update_shallow
= 1;
157 if (!strcmp("--from-promisor", arg
)) {
158 args
.from_promisor
= 1;
161 if (!strcmp("--refetch", arg
)) {
165 if (skip_prefix(arg
, ("--filter="), &arg
)) {
166 parse_list_objects_filter(&args
.filter_options
, arg
);
169 if (!strcmp(arg
, ("--no-filter"))) {
170 list_objects_filter_set_no_filter(&args
.filter_options
);
173 usage(fetch_pack_usage
);
176 args
.deepen_not
= &deepen_not
;
181 usage(fetch_pack_usage
);
184 * Copy refs from cmdline to growable list, then append any
185 * refs from the standard input:
187 for (; i
< argc
; i
++)
188 add_sought_entry(&sought
, &nr_sought
, &alloc_sought
, argv
[i
]);
189 if (args
.stdin_refs
) {
190 if (args
.stateless_rpc
) {
191 /* in stateless RPC mode we use pkt-line to read
192 * from stdin, until we get a flush packet
195 char *line
= packet_read_line(0, NULL
);
198 add_sought_entry(&sought
, &nr_sought
, &alloc_sought
, line
);
202 /* read from stdin one ref per line, until EOF */
203 struct strbuf line
= STRBUF_INIT
;
204 while (strbuf_getline_lf(&line
, stdin
) != EOF
)
205 add_sought_entry(&sought
, &nr_sought
, &alloc_sought
, line
.buf
);
206 strbuf_release(&line
);
210 if (args
.stateless_rpc
) {
215 int flags
= args
.verbose
? CONNECT_VERBOSE
: 0;
217 flags
|= CONNECT_DIAG_URL
;
218 conn
= git_connect(fd
, dest
, "git-upload-pack",
219 args
.uploadpack
, flags
);
221 return args
.diag_url
? 0 : 1;
224 packet_reader_init(&reader
, fd
[0], NULL
, 0,
225 PACKET_READ_CHOMP_NEWLINE
|
226 PACKET_READ_GENTLE_ON_EOF
|
227 PACKET_READ_DIE_ON_ERR_PACKET
);
229 version
= discover_version(&reader
);
232 get_remote_refs(fd
[1], &reader
, &ref
, 0, NULL
, NULL
,
237 get_remote_heads(&reader
, &ref
, 0, NULL
, &shallow
);
239 case protocol_unknown_version
:
240 BUG("unknown protocol version");
243 ref
= fetch_pack(&args
, fd
, ref
, sought
, nr_sought
,
244 &shallow
, pack_lockfiles_ptr
, version
);
245 if (pack_lockfiles
.nr
) {
248 printf("lock %s\n", pack_lockfiles
.items
[0].string
);
250 for (i
= 1; i
< pack_lockfiles
.nr
; i
++)
251 warning(_("Lockfile created but not reported: %s"),
252 pack_lockfiles
.items
[i
].string
);
254 if (args
.check_self_contained_and_connected
&&
255 args
.self_contained_and_connected
) {
256 printf("connectivity-ok\n");
261 if (finish_connect(conn
))
267 * If the heads to pull were given, we should have consumed
268 * all of them by matching the remote. Otherwise, 'git fetch
269 * remote no-such-ref' would silently succeed without issuing
272 ret
|= report_unmatched_refs(sought
, nr_sought
);
276 oid_to_hex(&ref
->old_oid
), ref
->name
);