1 #define USE_THE_REPOSITORY_VARIABLE
2 #define DISABLE_SIGN_COMPARE_WARNINGS
7 #include "object-file.h"
9 #include "fetch-pack.h"
12 #include "oid-array.h"
15 static const char fetch_pack_usage
[] =
16 "git fetch-pack [--all] [--stdin] [--quiet | -q] [--keep | -k] [--thin] "
17 "[--include-tag] [--upload-pack=<git-upload-pack>] [--depth=<n>] "
18 "[--no-progress] [--diag-url] [-v] [<host>:]<directory> [<refs>...]";
20 static void add_sought_entry(struct ref
***sought
, int *nr
, int *alloc
,
27 if (!parse_oid_hex(name
, &oid
, &p
)) {
29 /* <oid> <ref>, find refname */
31 } else if (*p
== '\0') {
32 ; /* <oid>, leave oid as name */
34 /* <ref>, clear cruft from oid */
35 oidclr(&oid
, the_repository
->hash_algo
);
38 /* <ref>, clear cruft from get_oid_hex */
39 oidclr(&oid
, the_repository
->hash_algo
);
42 ref
= alloc_ref(name
);
43 oidcpy(&ref
->old_oid
, &oid
);
45 ALLOC_GROW(*sought
, *nr
, *alloc
);
46 (*sought
)[*nr
- 1] = ref
;
49 int cmd_fetch_pack(int argc
,
51 const char *prefix UNUSED
,
52 struct repository
*repo UNUSED
)
55 struct ref
*fetched_refs
= NULL
, *remote_refs
= NULL
;
56 const char *dest
= NULL
;
57 struct ref
**sought
= NULL
;
58 struct ref
**sought_to_free
= NULL
;
59 int nr_sought
= 0, alloc_sought
= 0;
61 struct string_list pack_lockfiles
= STRING_LIST_INIT_DUP
;
62 struct string_list
*pack_lockfiles_ptr
= NULL
;
63 struct child_process
*conn
;
64 struct fetch_pack_args args
;
65 struct oid_array shallow
= OID_ARRAY_INIT
;
66 struct string_list deepen_not
= STRING_LIST_INIT_DUP
;
67 struct packet_reader reader
;
68 enum protocol_version version
;
72 packet_trace_identity("fetch-pack");
74 memset(&args
, 0, sizeof(args
));
75 list_objects_filter_init(&args
.filter_options
);
76 args
.uploadpack
= "git-upload-pack";
78 show_usage_if_asked(argc
, argv
, fetch_pack_usage
);
80 for (i
= 1; i
< argc
&& *argv
[i
] == '-'; i
++) {
81 const char *arg
= argv
[i
];
83 if (skip_prefix(arg
, "--upload-pack=", &arg
)) {
84 args
.uploadpack
= arg
;
87 if (skip_prefix(arg
, "--exec=", &arg
)) {
88 args
.uploadpack
= arg
;
91 if (!strcmp("--quiet", arg
) || !strcmp("-q", arg
)) {
95 if (!strcmp("--keep", arg
) || !strcmp("-k", arg
)) {
96 args
.lock_pack
= args
.keep_pack
;
100 if (!strcmp("--thin", arg
)) {
101 args
.use_thin_pack
= 1;
104 if (!strcmp("--include-tag", arg
)) {
105 args
.include_tag
= 1;
108 if (!strcmp("--all", arg
)) {
112 if (!strcmp("--stdin", arg
)) {
116 if (!strcmp("--diag-url", arg
)) {
120 if (!strcmp("-v", arg
)) {
124 if (skip_prefix(arg
, "--depth=", &arg
)) {
125 args
.depth
= strtol(arg
, NULL
, 0);
128 if (skip_prefix(arg
, "--shallow-since=", &arg
)) {
129 args
.deepen_since
= xstrdup(arg
);
132 if (skip_prefix(arg
, "--shallow-exclude=", &arg
)) {
133 string_list_append(&deepen_not
, arg
);
136 if (!strcmp(arg
, "--deepen-relative")) {
137 args
.deepen_relative
= 1;
140 if (!strcmp("--no-progress", arg
)) {
141 args
.no_progress
= 1;
144 if (!strcmp("--stateless-rpc", arg
)) {
145 args
.stateless_rpc
= 1;
148 if (!strcmp("--lock-pack", arg
)) {
150 pack_lockfiles_ptr
= &pack_lockfiles
;
153 if (!strcmp("--check-self-contained-and-connected", arg
)) {
154 args
.check_self_contained_and_connected
= 1;
157 if (!strcmp("--cloning", arg
)) {
161 if (!strcmp("--update-shallow", arg
)) {
162 args
.update_shallow
= 1;
165 if (!strcmp("--from-promisor", arg
)) {
166 args
.from_promisor
= 1;
169 if (!strcmp("--refetch", arg
)) {
173 if (skip_prefix(arg
, ("--filter="), &arg
)) {
174 parse_list_objects_filter(&args
.filter_options
, arg
);
177 if (!strcmp(arg
, ("--no-filter"))) {
178 list_objects_filter_set_no_filter(&args
.filter_options
);
181 usage(fetch_pack_usage
);
184 args
.deepen_not
= &deepen_not
;
189 usage(fetch_pack_usage
);
192 * Copy refs from cmdline to growable list, then append any
193 * refs from the standard input:
195 for (; i
< argc
; i
++)
196 add_sought_entry(&sought
, &nr_sought
, &alloc_sought
, argv
[i
]);
197 if (args
.stdin_refs
) {
198 if (args
.stateless_rpc
) {
199 /* in stateless RPC mode we use pkt-line to read
200 * from stdin, until we get a flush packet
203 char *line
= packet_read_line(0, NULL
);
206 add_sought_entry(&sought
, &nr_sought
, &alloc_sought
, line
);
210 /* read from stdin one ref per line, until EOF */
211 struct strbuf line
= STRBUF_INIT
;
212 while (strbuf_getline_lf(&line
, stdin
) != EOF
)
213 add_sought_entry(&sought
, &nr_sought
, &alloc_sought
, line
.buf
);
214 strbuf_release(&line
);
218 if (args
.stateless_rpc
) {
223 int flags
= args
.verbose
? CONNECT_VERBOSE
: 0;
225 flags
|= CONNECT_DIAG_URL
;
226 conn
= git_connect(fd
, dest
, "git-upload-pack",
227 args
.uploadpack
, flags
);
229 return args
.diag_url
? 0 : 1;
232 packet_reader_init(&reader
, fd
[0], NULL
, 0,
233 PACKET_READ_CHOMP_NEWLINE
|
234 PACKET_READ_GENTLE_ON_EOF
|
235 PACKET_READ_DIE_ON_ERR_PACKET
);
237 version
= discover_version(&reader
);
240 get_remote_refs(fd
[1], &reader
, &remote_refs
, 0, NULL
, NULL
,
245 get_remote_heads(&reader
, &remote_refs
, 0, NULL
, &shallow
);
247 case protocol_unknown_version
:
248 BUG("unknown protocol version");
252 * Create a shallow copy of `sought` so that we can free all of its entries.
253 * This is because `fetch_pack()` will modify the array to evict some
254 * entries, but won't free those.
256 DUP_ARRAY(sought_to_free
, sought
, nr_sought
);
258 fetched_refs
= fetch_pack(&args
, fd
, remote_refs
, sought
, nr_sought
,
259 &shallow
, pack_lockfiles_ptr
, version
);
261 if (pack_lockfiles
.nr
) {
264 printf("lock %s\n", pack_lockfiles
.items
[0].string
);
266 for (i
= 1; i
< pack_lockfiles
.nr
; i
++)
267 warning(_("Lockfile created but not reported: %s"),
268 pack_lockfiles
.items
[i
].string
);
270 if (args
.check_self_contained_and_connected
&&
271 args
.self_contained_and_connected
) {
272 printf("connectivity-ok\n");
277 if (finish_connect(conn
)) {
285 * If the heads to pull were given, we should have consumed
286 * all of them by matching the remote. Otherwise, 'git fetch
287 * remote no-such-ref' would silently succeed without issuing
290 ret
|= report_unmatched_refs(sought
, nr_sought
);
292 for (struct ref
*ref
= fetched_refs
; ref
; ref
= ref
->next
)
294 oid_to_hex(&ref
->old_oid
), ref
->name
);
297 for (size_t i
= 0; i
< nr_sought
; i
++)
298 free_one_ref(sought_to_free
[i
]);
299 free(sought_to_free
);
301 free_refs(fetched_refs
);
302 free_refs(remote_refs
);
303 list_objects_filter_release(&args
.filter_options
);
304 oid_array_clear(&shallow
);
305 string_list_clear(&pack_lockfiles
, 0);