4 #include "object-file.h"
6 #include "fetch-pack.h"
12 static const char fetch_pack_usage
[] =
13 "git fetch-pack [--all] [--stdin] [--quiet | -q] [--keep | -k] [--thin] "
14 "[--include-tag] [--upload-pack=<git-upload-pack>] [--depth=<n>] "
15 "[--no-progress] [--diag-url] [-v] [<host>:]<directory> [<refs>...]";
17 static void add_sought_entry(struct ref
***sought
, int *nr
, int *alloc
,
24 if (!parse_oid_hex(name
, &oid
, &p
)) {
26 /* <oid> <ref>, find refname */
28 } else if (*p
== '\0') {
29 ; /* <oid>, leave oid as name */
31 /* <ref>, clear cruft from oid */
32 oidclr(&oid
, the_repository
->hash_algo
);
35 /* <ref>, clear cruft from get_oid_hex */
36 oidclr(&oid
, the_repository
->hash_algo
);
39 ref
= alloc_ref(name
);
40 oidcpy(&ref
->old_oid
, &oid
);
42 ALLOC_GROW(*sought
, *nr
, *alloc
);
43 (*sought
)[*nr
- 1] = ref
;
46 int cmd_fetch_pack(int argc
, const char **argv
, const char *prefix UNUSED
)
49 struct ref
*fetched_refs
= NULL
, *remote_refs
= NULL
;
50 const char *dest
= NULL
;
51 struct ref
**sought
= NULL
;
52 int nr_sought
= 0, alloc_sought
= 0;
54 struct string_list pack_lockfiles
= STRING_LIST_INIT_DUP
;
55 struct string_list
*pack_lockfiles_ptr
= NULL
;
56 struct child_process
*conn
;
57 struct fetch_pack_args args
;
58 struct oid_array shallow
= OID_ARRAY_INIT
;
59 struct string_list deepen_not
= STRING_LIST_INIT_DUP
;
60 struct packet_reader reader
;
61 enum protocol_version version
;
65 packet_trace_identity("fetch-pack");
67 memset(&args
, 0, sizeof(args
));
68 list_objects_filter_init(&args
.filter_options
);
69 args
.uploadpack
= "git-upload-pack";
71 for (i
= 1; i
< argc
&& *argv
[i
] == '-'; i
++) {
72 const char *arg
= argv
[i
];
74 if (skip_prefix(arg
, "--upload-pack=", &arg
)) {
75 args
.uploadpack
= arg
;
78 if (skip_prefix(arg
, "--exec=", &arg
)) {
79 args
.uploadpack
= arg
;
82 if (!strcmp("--quiet", arg
) || !strcmp("-q", arg
)) {
86 if (!strcmp("--keep", arg
) || !strcmp("-k", arg
)) {
87 args
.lock_pack
= args
.keep_pack
;
91 if (!strcmp("--thin", arg
)) {
92 args
.use_thin_pack
= 1;
95 if (!strcmp("--include-tag", arg
)) {
99 if (!strcmp("--all", arg
)) {
103 if (!strcmp("--stdin", arg
)) {
107 if (!strcmp("--diag-url", arg
)) {
111 if (!strcmp("-v", arg
)) {
115 if (skip_prefix(arg
, "--depth=", &arg
)) {
116 args
.depth
= strtol(arg
, NULL
, 0);
119 if (skip_prefix(arg
, "--shallow-since=", &arg
)) {
120 args
.deepen_since
= xstrdup(arg
);
123 if (skip_prefix(arg
, "--shallow-exclude=", &arg
)) {
124 string_list_append(&deepen_not
, arg
);
127 if (!strcmp(arg
, "--deepen-relative")) {
128 args
.deepen_relative
= 1;
131 if (!strcmp("--no-progress", arg
)) {
132 args
.no_progress
= 1;
135 if (!strcmp("--stateless-rpc", arg
)) {
136 args
.stateless_rpc
= 1;
139 if (!strcmp("--lock-pack", arg
)) {
141 pack_lockfiles_ptr
= &pack_lockfiles
;
144 if (!strcmp("--check-self-contained-and-connected", arg
)) {
145 args
.check_self_contained_and_connected
= 1;
148 if (!strcmp("--cloning", arg
)) {
152 if (!strcmp("--update-shallow", arg
)) {
153 args
.update_shallow
= 1;
156 if (!strcmp("--from-promisor", arg
)) {
157 args
.from_promisor
= 1;
160 if (!strcmp("--refetch", arg
)) {
164 if (skip_prefix(arg
, ("--filter="), &arg
)) {
165 parse_list_objects_filter(&args
.filter_options
, arg
);
168 if (!strcmp(arg
, ("--no-filter"))) {
169 list_objects_filter_set_no_filter(&args
.filter_options
);
172 usage(fetch_pack_usage
);
175 args
.deepen_not
= &deepen_not
;
180 usage(fetch_pack_usage
);
183 * Copy refs from cmdline to growable list, then append any
184 * refs from the standard input:
186 for (; i
< argc
; i
++)
187 add_sought_entry(&sought
, &nr_sought
, &alloc_sought
, argv
[i
]);
188 if (args
.stdin_refs
) {
189 if (args
.stateless_rpc
) {
190 /* in stateless RPC mode we use pkt-line to read
191 * from stdin, until we get a flush packet
194 char *line
= packet_read_line(0, NULL
);
197 add_sought_entry(&sought
, &nr_sought
, &alloc_sought
, line
);
201 /* read from stdin one ref per line, until EOF */
202 struct strbuf line
= STRBUF_INIT
;
203 while (strbuf_getline_lf(&line
, stdin
) != EOF
)
204 add_sought_entry(&sought
, &nr_sought
, &alloc_sought
, line
.buf
);
205 strbuf_release(&line
);
209 if (args
.stateless_rpc
) {
214 int flags
= args
.verbose
? CONNECT_VERBOSE
: 0;
216 flags
|= CONNECT_DIAG_URL
;
217 conn
= git_connect(fd
, dest
, "git-upload-pack",
218 args
.uploadpack
, flags
);
220 return args
.diag_url
? 0 : 1;
223 packet_reader_init(&reader
, fd
[0], NULL
, 0,
224 PACKET_READ_CHOMP_NEWLINE
|
225 PACKET_READ_GENTLE_ON_EOF
|
226 PACKET_READ_DIE_ON_ERR_PACKET
);
228 version
= discover_version(&reader
);
231 get_remote_refs(fd
[1], &reader
, &remote_refs
, 0, NULL
, NULL
,
236 get_remote_heads(&reader
, &remote_refs
, 0, NULL
, &shallow
);
238 case protocol_unknown_version
:
239 BUG("unknown protocol version");
242 fetched_refs
= fetch_pack(&args
, fd
, remote_refs
, sought
, nr_sought
,
243 &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
);
274 for (struct ref
*ref
= fetched_refs
; ref
; ref
= ref
->next
)
276 oid_to_hex(&ref
->old_oid
), ref
->name
);
278 for (size_t i
= 0; i
< nr_sought
; i
++)
279 free_one_ref(sought
[i
]);
281 free_refs(fetched_refs
);
282 free_refs(remote_refs
);