]>
Commit | Line | Data |
---|---|---|
b888d61c DB |
1 | /* |
2 | * "git fetch" | |
3 | */ | |
4 | #include "cache.h" | |
5 | #include "refs.h" | |
6 | #include "commit.h" | |
7 | #include "builtin.h" | |
c455c87c | 8 | #include "string-list.h" |
b888d61c DB |
9 | #include "remote.h" |
10 | #include "transport.h" | |
4191c356 | 11 | #include "run-command.h" |
83201998 | 12 | #include "parse-options.h" |
4a16d072 | 13 | #include "sigchain.h" |
f1863d0d | 14 | #include "transport.h" |
7dce19d3 | 15 | #include "submodule.h" |
f96400cb | 16 | #include "connected.h" |
85556d4e | 17 | #include "argv-array.h" |
b888d61c | 18 | |
83201998 | 19 | static const char * const builtin_fetch_usage[] = { |
719acedb NTND |
20 | N_("git fetch [<options>] [<repository> [<refspec>...]]"), |
21 | N_("git fetch [<options>] <group>"), | |
22 | N_("git fetch --multiple [<options>] [(<repository> | <group>)...]"), | |
23 | N_("git fetch --all [<options>]"), | |
83201998 KH |
24 | NULL |
25 | }; | |
b888d61c | 26 | |
83201998 KH |
27 | enum { |
28 | TAGS_UNSET = 0, | |
29 | TAGS_DEFAULT = 1, | |
30 | TAGS_SET = 2 | |
31 | }; | |
32 | ||
737c5a9c MS |
33 | static int fetch_prune_config = -1; /* unspecified */ |
34 | static int prune = -1; /* unspecified */ | |
35 | #define PRUNE_BY_DEFAULT 0 /* do we prune by default? */ | |
36 | ||
37 | static int all, append, dry_run, force, keep, multiple, update_head_ok, verbosity; | |
01fdc21f | 38 | static int progress = -1, recurse_submodules = RECURSE_SUBMODULES_DEFAULT; |
48d25cae | 39 | static int tags = TAGS_DEFAULT, unshallow, update_shallow; |
4191c356 | 40 | static const char *depth; |
83201998 | 41 | static const char *upload_pack; |
2d324efa | 42 | static struct strbuf default_rla = STRBUF_INIT; |
af234459 | 43 | static struct transport *gtransport; |
b26ed430 | 44 | static struct transport *gsecondary; |
7dce19d3 | 45 | static const char *submodule_prefix = ""; |
88a21979 | 46 | static const char *recurse_submodules_default; |
4b3b33a7 | 47 | static int shown_url = 0; |
c5558f80 JH |
48 | static int refmap_alloc, refmap_nr; |
49 | static const char **refmap_array; | |
e4022ed2 | 50 | |
8f0700dd JL |
51 | static int option_parse_recurse_submodules(const struct option *opt, |
52 | const char *arg, int unset) | |
53 | { | |
54 | if (unset) { | |
55 | recurse_submodules = RECURSE_SUBMODULES_OFF; | |
56 | } else { | |
57 | if (arg) | |
58 | recurse_submodules = parse_fetch_recurse_submodules_arg(opt->long_name, arg); | |
59 | else | |
60 | recurse_submodules = RECURSE_SUBMODULES_ON; | |
61 | } | |
62 | return 0; | |
63 | } | |
e4022ed2 | 64 | |
737c5a9c MS |
65 | static int git_fetch_config(const char *k, const char *v, void *cb) |
66 | { | |
67 | if (!strcmp(k, "fetch.prune")) { | |
68 | fetch_prune_config = git_config_bool(k, v); | |
69 | return 0; | |
70 | } | |
72549dfd | 71 | return git_default_config(k, v, cb); |
737c5a9c MS |
72 | } |
73 | ||
c5558f80 JH |
74 | static int parse_refmap_arg(const struct option *opt, const char *arg, int unset) |
75 | { | |
76 | ALLOC_GROW(refmap_array, refmap_nr + 1, refmap_alloc); | |
77 | ||
78 | /* | |
79 | * "git fetch --refmap='' origin foo" | |
80 | * can be used to tell the command not to store anywhere | |
81 | */ | |
82 | if (*arg) | |
83 | refmap_array[refmap_nr++] = arg; | |
84 | return 0; | |
85 | } | |
86 | ||
83201998 | 87 | static struct option builtin_fetch_options[] = { |
7f87aff2 | 88 | OPT__VERBOSITY(&verbosity), |
d5d09d47 SB |
89 | OPT_BOOL(0, "all", &all, |
90 | N_("fetch from all remotes")), | |
91 | OPT_BOOL('a', "append", &append, | |
92 | N_("append to .git/FETCH_HEAD instead of overwriting")), | |
719acedb NTND |
93 | OPT_STRING(0, "upload-pack", &upload_pack, N_("path"), |
94 | N_("path to upload pack on remote end")), | |
95 | OPT__FORCE(&force, N_("force overwrite of local branch")), | |
d5d09d47 SB |
96 | OPT_BOOL('m', "multiple", &multiple, |
97 | N_("fetch from multiple remotes")), | |
83201998 | 98 | OPT_SET_INT('t', "tags", &tags, |
719acedb | 99 | N_("fetch all tags and associated objects"), TAGS_SET), |
e7951290 | 100 | OPT_SET_INT('n', NULL, &tags, |
719acedb | 101 | N_("do not fetch all tags (--no-tags)"), TAGS_UNSET), |
d5d09d47 SB |
102 | OPT_BOOL('p', "prune", &prune, |
103 | N_("prune remote-tracking branches no longer on remote")), | |
719acedb NTND |
104 | { OPTION_CALLBACK, 0, "recurse-submodules", NULL, N_("on-demand"), |
105 | N_("control recursive fetching of submodules"), | |
8f0700dd | 106 | PARSE_OPT_OPTARG, option_parse_recurse_submodules }, |
d5d09d47 SB |
107 | OPT_BOOL(0, "dry-run", &dry_run, |
108 | N_("dry run")), | |
109 | OPT_BOOL('k', "keep", &keep, N_("keep downloaded pack")), | |
110 | OPT_BOOL('u', "update-head-ok", &update_head_ok, | |
719acedb NTND |
111 | N_("allow updating of HEAD ref")), |
112 | OPT_BOOL(0, "progress", &progress, N_("force progress reporting")), | |
113 | OPT_STRING(0, "depth", &depth, N_("depth"), | |
114 | N_("deepen history of shallow clone")), | |
4dcb167f NTND |
115 | { OPTION_SET_INT, 0, "unshallow", &unshallow, NULL, |
116 | N_("convert to a complete repository"), | |
117 | PARSE_OPT_NONEG | PARSE_OPT_NOARG, NULL, 1 }, | |
719acedb NTND |
118 | { OPTION_STRING, 0, "submodule-prefix", &submodule_prefix, N_("dir"), |
119 | N_("prepend this to submodule path output"), PARSE_OPT_HIDDEN }, | |
88a21979 JL |
120 | { OPTION_STRING, 0, "recurse-submodules-default", |
121 | &recurse_submodules_default, NULL, | |
719acedb | 122 | N_("default mode for recursion"), PARSE_OPT_HIDDEN }, |
48d25cae NTND |
123 | OPT_BOOL(0, "update-shallow", &update_shallow, |
124 | N_("accept refs that update .git/shallow")), | |
c5558f80 JH |
125 | { OPTION_CALLBACK, 0, "refmap", NULL, N_("refmap"), |
126 | N_("specify fetch refmap"), PARSE_OPT_NONEG, parse_refmap_arg }, | |
83201998 KH |
127 | OPT_END() |
128 | }; | |
129 | ||
e4022ed2 SP |
130 | static void unlock_pack(void) |
131 | { | |
af234459 JH |
132 | if (gtransport) |
133 | transport_unlock_pack(gtransport); | |
b26ed430 JH |
134 | if (gsecondary) |
135 | transport_unlock_pack(gsecondary); | |
e4022ed2 SP |
136 | } |
137 | ||
138 | static void unlock_pack_on_signal(int signo) | |
139 | { | |
140 | unlock_pack(); | |
4a16d072 | 141 | sigchain_pop(signo); |
e4022ed2 SP |
142 | raise(signo); |
143 | } | |
b888d61c | 144 | |
85682c19 | 145 | static void add_merge_config(struct ref **head, |
4577370e | 146 | const struct ref *remote_refs, |
85682c19 SP |
147 | struct branch *branch, |
148 | struct ref ***tail) | |
b888d61c | 149 | { |
85682c19 | 150 | int i; |
b888d61c | 151 | |
85682c19 SP |
152 | for (i = 0; i < branch->merge_nr; i++) { |
153 | struct ref *rm, **old_tail = *tail; | |
154 | struct refspec refspec; | |
155 | ||
156 | for (rm = *head; rm; rm = rm->next) { | |
157 | if (branch_merge_matches(branch, i, rm->name)) { | |
900f2814 | 158 | rm->fetch_head_status = FETCH_HEAD_MERGE; |
85682c19 SP |
159 | break; |
160 | } | |
b888d61c | 161 | } |
85682c19 SP |
162 | if (rm) |
163 | continue; | |
164 | ||
9ad7c5ae | 165 | /* |
8b3f3f84 | 166 | * Not fetched to a remote-tracking branch? We need to fetch |
85682c19 | 167 | * it anyway to allow this branch's "branch.$name.merge" |
05207a28 | 168 | * to be honored by 'git pull', but we do not have to |
9ad7c5ae JH |
169 | * fail if branch.$name.merge is misconfigured to point |
170 | * at a nonexisting branch. If we were indeed called by | |
05207a28 | 171 | * 'git pull', it will notice the misconfiguration because |
9ad7c5ae JH |
172 | * there is no entry in the resulting FETCH_HEAD marked |
173 | * for merging. | |
85682c19 | 174 | */ |
8da61a2a | 175 | memset(&refspec, 0, sizeof(refspec)); |
85682c19 | 176 | refspec.src = branch->merge[i]->src; |
9ad7c5ae | 177 | get_fetch_map(remote_refs, &refspec, tail, 1); |
85682c19 | 178 | for (rm = *old_tail; rm; rm = rm->next) |
900f2814 | 179 | rm->fetch_head_status = FETCH_HEAD_MERGE; |
b888d61c DB |
180 | } |
181 | } | |
182 | ||
a0fbb5a3 MH |
183 | static int add_existing(const char *refname, const unsigned char *sha1, |
184 | int flag, void *cbdata) | |
185 | { | |
186 | struct string_list *list = (struct string_list *)cbdata; | |
187 | struct string_list_item *item = string_list_insert(list, refname); | |
188 | item->util = xmalloc(20); | |
189 | hashcpy(item->util, sha1); | |
190 | return 0; | |
191 | } | |
192 | ||
193 | static int will_fetch(struct ref **head, const unsigned char *sha1) | |
194 | { | |
195 | struct ref *rm = *head; | |
196 | while (rm) { | |
197 | if (!hashcmp(rm->old_sha1, sha1)) | |
198 | return 1; | |
199 | rm = rm->next; | |
200 | } | |
201 | return 0; | |
202 | } | |
203 | ||
767f176a SP |
204 | static void find_non_local_tags(struct transport *transport, |
205 | struct ref **head, | |
a0fbb5a3 MH |
206 | struct ref ***tail) |
207 | { | |
208 | struct string_list existing_refs = STRING_LIST_INIT_DUP; | |
209 | struct string_list remote_refs = STRING_LIST_INIT_NODUP; | |
210 | const struct ref *ref; | |
211 | struct string_list_item *item = NULL; | |
212 | ||
213 | for_each_ref(add_existing, &existing_refs); | |
214 | for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) { | |
ad704485 | 215 | if (!starts_with(ref->name, "refs/tags/")) |
a0fbb5a3 MH |
216 | continue; |
217 | ||
218 | /* | |
219 | * The peeled ref always follows the matching base | |
220 | * ref, so if we see a peeled ref that we don't want | |
221 | * to fetch then we can mark the ref entry in the list | |
222 | * as one to ignore by setting util to NULL. | |
223 | */ | |
ad704485 | 224 | if (ends_with(ref->name, "^{}")) { |
a0fbb5a3 MH |
225 | if (item && !has_sha1_file(ref->old_sha1) && |
226 | !will_fetch(head, ref->old_sha1) && | |
227 | !has_sha1_file(item->util) && | |
228 | !will_fetch(head, item->util)) | |
229 | item->util = NULL; | |
230 | item = NULL; | |
231 | continue; | |
232 | } | |
233 | ||
234 | /* | |
235 | * If item is non-NULL here, then we previously saw a | |
236 | * ref not followed by a peeled reference, so we need | |
237 | * to check if it is a lightweight tag that we want to | |
238 | * fetch. | |
239 | */ | |
240 | if (item && !has_sha1_file(item->util) && | |
241 | !will_fetch(head, item->util)) | |
242 | item->util = NULL; | |
243 | ||
244 | item = NULL; | |
245 | ||
246 | /* skip duplicates and refs that we already have */ | |
247 | if (string_list_has_string(&remote_refs, ref->name) || | |
248 | string_list_has_string(&existing_refs, ref->name)) | |
249 | continue; | |
250 | ||
251 | item = string_list_insert(&remote_refs, ref->name); | |
252 | item->util = (void *)ref->old_sha1; | |
253 | } | |
254 | string_list_clear(&existing_refs, 1); | |
255 | ||
256 | /* | |
257 | * We may have a final lightweight tag that needs to be | |
258 | * checked to see if it needs fetching. | |
259 | */ | |
260 | if (item && !has_sha1_file(item->util) && | |
261 | !will_fetch(head, item->util)) | |
262 | item->util = NULL; | |
263 | ||
264 | /* | |
265 | * For all the tags in the remote_refs string list, | |
266 | * add them to the list of refs to be fetched | |
267 | */ | |
268 | for_each_string_list_item(item, &remote_refs) { | |
269 | /* Unless we have already decided to ignore this item... */ | |
270 | if (item->util) | |
271 | { | |
272 | struct ref *rm = alloc_ref(item->string); | |
273 | rm->peer_ref = alloc_ref(item->string); | |
274 | hashcpy(rm->old_sha1, item->util); | |
275 | **tail = rm; | |
276 | *tail = &rm->next; | |
277 | } | |
278 | } | |
279 | ||
280 | string_list_clear(&remote_refs, 0); | |
281 | } | |
767f176a | 282 | |
b888d61c | 283 | static struct ref *get_ref_map(struct transport *transport, |
f137a45e MH |
284 | struct refspec *refspecs, int refspec_count, |
285 | int tags, int *autotags) | |
b888d61c DB |
286 | { |
287 | int i; | |
288 | struct ref *rm; | |
289 | struct ref *ref_map = NULL; | |
290 | struct ref **tail = &ref_map; | |
291 | ||
c5a84e92 MH |
292 | /* opportunistically-updated references: */ |
293 | struct ref *orefs = NULL, **oref_tail = &orefs; | |
b888d61c | 294 | |
c5a84e92 | 295 | const struct ref *remote_refs = transport_get_remote_refs(transport); |
f2690487 | 296 | |
c5a84e92 | 297 | if (refspec_count) { |
c5558f80 JH |
298 | struct refspec *fetch_refspec; |
299 | int fetch_refspec_nr; | |
300 | ||
f137a45e MH |
301 | for (i = 0; i < refspec_count; i++) { |
302 | get_fetch_map(remote_refs, &refspecs[i], &tail, 0); | |
303 | if (refspecs[i].dst && refspecs[i].dst[0]) | |
b888d61c DB |
304 | *autotags = 1; |
305 | } | |
0281c930 | 306 | /* Merge everything on the command line (but not --tags) */ |
b888d61c | 307 | for (rm = ref_map; rm; rm = rm->next) |
900f2814 | 308 | rm->fetch_head_status = FETCH_HEAD_MERGE; |
f2690487 JK |
309 | |
310 | /* | |
0281c930 MH |
311 | * For any refs that we happen to be fetching via |
312 | * command-line arguments, the destination ref might | |
313 | * have been missing or have been different than the | |
314 | * remote-tracking ref that would be derived from the | |
315 | * configured refspec. In these cases, we want to | |
316 | * take the opportunity to update their configured | |
317 | * remote-tracking reference. However, we do not want | |
318 | * to mention these entries in FETCH_HEAD at all, as | |
319 | * they would simply be duplicates of existing | |
320 | * entries, so we set them FETCH_HEAD_IGNORE below. | |
321 | * | |
322 | * We compute these entries now, based only on the | |
323 | * refspecs specified on the command line. But we add | |
324 | * them to the list following the refspecs resulting | |
325 | * from the tags option so that one of the latter, | |
326 | * which has FETCH_HEAD_NOT_FOR_MERGE, is not removed | |
327 | * by ref_remove_duplicates() in favor of one of these | |
328 | * opportunistic entries with FETCH_HEAD_IGNORE. | |
f2690487 | 329 | */ |
c5558f80 JH |
330 | if (refmap_array) { |
331 | fetch_refspec = parse_fetch_refspec(refmap_nr, refmap_array); | |
332 | fetch_refspec_nr = refmap_nr; | |
333 | } else { | |
334 | fetch_refspec = transport->remote->fetch; | |
335 | fetch_refspec_nr = transport->remote->fetch_refspec_nr; | |
336 | } | |
337 | ||
338 | for (i = 0; i < fetch_refspec_nr; i++) | |
339 | get_fetch_map(ref_map, &fetch_refspec[i], &oref_tail, 1); | |
0281c930 MH |
340 | |
341 | if (tags == TAGS_SET) | |
342 | get_fetch_map(remote_refs, tag_refspec, &tail, 0); | |
c5558f80 JH |
343 | } else if (refmap_array) { |
344 | die("--refmap option is only meaningful with command-line refspec(s)."); | |
b888d61c DB |
345 | } else { |
346 | /* Use the defaults */ | |
347 | struct remote *remote = transport->remote; | |
85682c19 SP |
348 | struct branch *branch = branch_get(NULL); |
349 | int has_merge = branch_has_merge_config(branch); | |
3ee1757b BC |
350 | if (remote && |
351 | (remote->fetch_refspec_nr || | |
f31dbdc7 | 352 | /* Note: has_merge implies non-NULL branch->remote_name */ |
3ee1757b | 353 | (has_merge && !strcmp(branch->remote_name, remote->name)))) { |
b888d61c | 354 | for (i = 0; i < remote->fetch_refspec_nr; i++) { |
9ad7c5ae | 355 | get_fetch_map(remote_refs, &remote->fetch[i], &tail, 0); |
b888d61c DB |
356 | if (remote->fetch[i].dst && |
357 | remote->fetch[i].dst[0]) | |
358 | *autotags = 1; | |
85682c19 | 359 | if (!i && !has_merge && ref_map && |
cfb8f898 | 360 | !remote->fetch[0].pattern) |
900f2814 | 361 | ref_map->fetch_head_status = FETCH_HEAD_MERGE; |
b888d61c | 362 | } |
da0204df JS |
363 | /* |
364 | * if the remote we're fetching from is the same | |
365 | * as given in branch.<name>.remote, we add the | |
366 | * ref given in branch.<name>.merge, too. | |
f31dbdc7 BC |
367 | * |
368 | * Note: has_merge implies non-NULL branch->remote_name | |
da0204df | 369 | */ |
9ad7c5ae JH |
370 | if (has_merge && |
371 | !strcmp(branch->remote_name, remote->name)) | |
85682c19 | 372 | add_merge_config(&ref_map, remote_refs, branch, &tail); |
b888d61c DB |
373 | } else { |
374 | ref_map = get_remote_ref(remote_refs, "HEAD"); | |
9ad7c5ae | 375 | if (!ref_map) |
bd4a51fc | 376 | die(_("Couldn't find remote ref HEAD")); |
900f2814 | 377 | ref_map->fetch_head_status = FETCH_HEAD_MERGE; |
5aaf7f2a | 378 | tail = &ref_map->next; |
b888d61c DB |
379 | } |
380 | } | |
0281c930 | 381 | |
c5a84e92 MH |
382 | if (tags == TAGS_SET) |
383 | /* also fetch all tags */ | |
384 | get_fetch_map(remote_refs, tag_refspec, &tail, 0); | |
385 | else if (tags == TAGS_DEFAULT && *autotags) | |
767f176a | 386 | find_non_local_tags(transport, &ref_map, &tail); |
0281c930 | 387 | |
c5a84e92 MH |
388 | /* Now append any refs to be updated opportunistically: */ |
389 | *tail = orefs; | |
390 | for (rm = orefs; rm; rm = rm->next) { | |
391 | rm->fetch_head_status = FETCH_HEAD_IGNORE; | |
392 | tail = &rm->next; | |
393 | } | |
394 | ||
b9afe665 | 395 | return ref_remove_duplicates(ref_map); |
b888d61c DB |
396 | } |
397 | ||
fa250759 JK |
398 | #define STORE_REF_ERROR_OTHER 1 |
399 | #define STORE_REF_ERROR_DF_CONFLICT 2 | |
400 | ||
b888d61c DB |
401 | static int s_update_ref(const char *action, |
402 | struct ref *ref, | |
403 | int check_old) | |
404 | { | |
405 | char msg[1024]; | |
406 | char *rla = getenv("GIT_REFLOG_ACTION"); | |
cd94f765 RS |
407 | struct ref_transaction *transaction; |
408 | struct strbuf err = STRBUF_INIT; | |
409 | int ret, df_conflict = 0; | |
b888d61c | 410 | |
28a15401 JS |
411 | if (dry_run) |
412 | return 0; | |
b888d61c | 413 | if (!rla) |
2d324efa | 414 | rla = default_rla.buf; |
b888d61c | 415 | snprintf(msg, sizeof(msg), "%s: %s", rla, action); |
cd94f765 RS |
416 | |
417 | transaction = ref_transaction_begin(&err); | |
418 | if (!transaction || | |
419 | ref_transaction_update(transaction, ref->name, ref->new_sha1, | |
420 | ref->old_sha1, 0, check_old, msg, &err)) | |
421 | goto fail; | |
422 | ||
423 | ret = ref_transaction_commit(transaction, &err); | |
424 | if (ret) { | |
425 | df_conflict = (ret == TRANSACTION_NAME_CONFLICT); | |
426 | goto fail; | |
427 | } | |
428 | ||
429 | ref_transaction_free(transaction); | |
430 | strbuf_release(&err); | |
b888d61c | 431 | return 0; |
cd94f765 RS |
432 | fail: |
433 | ref_transaction_free(transaction); | |
434 | error("%s", err.buf); | |
435 | strbuf_release(&err); | |
436 | return df_conflict ? STORE_REF_ERROR_DF_CONFLICT | |
437 | : STORE_REF_ERROR_OTHER; | |
b888d61c DB |
438 | } |
439 | ||
9ef4272b | 440 | #define REFCOL_WIDTH 10 |
165f3902 | 441 | |
b888d61c | 442 | static int update_local_ref(struct ref *ref, |
165f3902 | 443 | const char *remote, |
6da618d5 | 444 | const struct ref *remote_ref, |
5914f2d0 | 445 | struct strbuf *display) |
b888d61c | 446 | { |
b888d61c DB |
447 | struct commit *current = NULL, *updated; |
448 | enum object_type type; | |
449 | struct branch *current_branch = branch_get(NULL); | |
4577e483 | 450 | const char *pretty_ref = prettify_refname(ref->name); |
b888d61c DB |
451 | |
452 | type = sha1_object_info(ref->new_sha1, NULL); | |
453 | if (type < 0) | |
bd4a51fc | 454 | die(_("object %s not found"), sha1_to_hex(ref->new_sha1)); |
b888d61c | 455 | |
b888d61c | 456 | if (!hashcmp(ref->old_sha1, ref->new_sha1)) { |
7f87aff2 | 457 | if (verbosity > 0) |
5914f2d0 | 458 | strbuf_addf(display, "= %-*s %-*s -> %s", |
754395d3 NTND |
459 | TRANSPORT_SUMMARY(_("[up to date]")), |
460 | REFCOL_WIDTH, remote, pretty_ref); | |
b888d61c DB |
461 | return 0; |
462 | } | |
463 | ||
b3abdd9d SP |
464 | if (current_branch && |
465 | !strcmp(ref->name, current_branch->name) && | |
b888d61c DB |
466 | !(update_head_ok || is_bare_repository()) && |
467 | !is_null_sha1(ref->old_sha1)) { | |
468 | /* | |
469 | * If this is the head, and it's not okay to update | |
470 | * the head, and the old value of the head isn't empty... | |
471 | */ | |
5914f2d0 JK |
472 | strbuf_addf(display, |
473 | _("! %-*s %-*s -> %s (can't fetch in current branch)"), | |
754395d3 | 474 | TRANSPORT_SUMMARY(_("[rejected]")), |
5914f2d0 | 475 | REFCOL_WIDTH, remote, pretty_ref); |
b888d61c DB |
476 | return 1; |
477 | } | |
478 | ||
479 | if (!is_null_sha1(ref->old_sha1) && | |
59556548 | 480 | starts_with(ref->name, "refs/tags/")) { |
6315472e JK |
481 | int r; |
482 | r = s_update_ref("updating tag", ref, 0); | |
5914f2d0 JK |
483 | strbuf_addf(display, "%c %-*s %-*s -> %s%s", |
484 | r ? '!' : '-', | |
754395d3 | 485 | TRANSPORT_SUMMARY(_("[tag update]")), |
5914f2d0 JK |
486 | REFCOL_WIDTH, remote, pretty_ref, |
487 | r ? _(" (unable to update local ref)") : ""); | |
6315472e | 488 | return r; |
b888d61c DB |
489 | } |
490 | ||
cfa5b2b7 SP |
491 | current = lookup_commit_reference_gently(ref->old_sha1, 1); |
492 | updated = lookup_commit_reference_gently(ref->new_sha1, 1); | |
b888d61c | 493 | if (!current || !updated) { |
165f3902 NP |
494 | const char *msg; |
495 | const char *what; | |
6315472e | 496 | int r; |
0997adaa MB |
497 | /* |
498 | * Nicely describe the new ref we're fetching. | |
499 | * Base this on the remote's ref name, as it's | |
500 | * more likely to follow a standard layout. | |
501 | */ | |
502 | const char *name = remote_ref ? remote_ref->name : ""; | |
59556548 | 503 | if (starts_with(name, "refs/tags/")) { |
b888d61c | 504 | msg = "storing tag"; |
f7b3742a | 505 | what = _("[new tag]"); |
59556548 | 506 | } else if (starts_with(name, "refs/heads/")) { |
b888d61c | 507 | msg = "storing head"; |
f7b3742a | 508 | what = _("[new branch]"); |
0997adaa MB |
509 | } else { |
510 | msg = "storing ref"; | |
511 | what = _("[new ref]"); | |
165f3902 NP |
512 | } |
513 | ||
a6801adc JL |
514 | if ((recurse_submodules != RECURSE_SUBMODULES_OFF) && |
515 | (recurse_submodules != RECURSE_SUBMODULES_ON)) | |
516 | check_for_new_submodule_commits(ref->new_sha1); | |
6315472e | 517 | r = s_update_ref(msg, ref, 0); |
5914f2d0 JK |
518 | strbuf_addf(display, "%c %-*s %-*s -> %s%s", |
519 | r ? '!' : '*', | |
754395d3 | 520 | TRANSPORT_SUMMARY(what), |
5914f2d0 JK |
521 | REFCOL_WIDTH, remote, pretty_ref, |
522 | r ? _(" (unable to update local ref)") : ""); | |
6315472e | 523 | return r; |
b888d61c DB |
524 | } |
525 | ||
a20efee9 | 526 | if (in_merge_bases(current, updated)) { |
165f3902 | 527 | char quickref[83]; |
6315472e | 528 | int r; |
165f3902 NP |
529 | strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV)); |
530 | strcat(quickref, ".."); | |
531 | strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV)); | |
88a21979 JL |
532 | if ((recurse_submodules != RECURSE_SUBMODULES_OFF) && |
533 | (recurse_submodules != RECURSE_SUBMODULES_ON)) | |
534 | check_for_new_submodule_commits(ref->new_sha1); | |
a75d7b54 | 535 | r = s_update_ref("fast-forward", ref, 1); |
5914f2d0 JK |
536 | strbuf_addf(display, "%c %-*s %-*s -> %s%s", |
537 | r ? '!' : ' ', | |
538 | TRANSPORT_SUMMARY_WIDTH, quickref, | |
539 | REFCOL_WIDTH, remote, pretty_ref, | |
540 | r ? _(" (unable to update local ref)") : ""); | |
6315472e | 541 | return r; |
165f3902 NP |
542 | } else if (force || ref->force) { |
543 | char quickref[84]; | |
6315472e | 544 | int r; |
165f3902 NP |
545 | strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV)); |
546 | strcat(quickref, "..."); | |
547 | strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV)); | |
88a21979 JL |
548 | if ((recurse_submodules != RECURSE_SUBMODULES_OFF) && |
549 | (recurse_submodules != RECURSE_SUBMODULES_ON)) | |
550 | check_for_new_submodule_commits(ref->new_sha1); | |
6315472e | 551 | r = s_update_ref("forced-update", ref, 1); |
5914f2d0 JK |
552 | strbuf_addf(display, "%c %-*s %-*s -> %s (%s)", |
553 | r ? '!' : '+', | |
554 | TRANSPORT_SUMMARY_WIDTH, quickref, | |
555 | REFCOL_WIDTH, remote, pretty_ref, | |
556 | r ? _("unable to update local ref") : _("forced update")); | |
6315472e | 557 | return r; |
165f3902 | 558 | } else { |
5914f2d0 | 559 | strbuf_addf(display, "! %-*s %-*s -> %s %s", |
754395d3 | 560 | TRANSPORT_SUMMARY(_("[rejected]")), |
5914f2d0 JK |
561 | REFCOL_WIDTH, remote, pretty_ref, |
562 | _("(non-fast-forward)")); | |
b888d61c DB |
563 | return 1; |
564 | } | |
b888d61c DB |
565 | } |
566 | ||
f0e278b1 | 567 | static int iterate_ref_map(void *cb_data, unsigned char sha1[20]) |
6b67e0dc | 568 | { |
f0e278b1 JH |
569 | struct ref **rm = cb_data; |
570 | struct ref *ref = *rm; | |
6b67e0dc | 571 | |
4820a33b NTND |
572 | while (ref && ref->status == REF_STATUS_REJECT_SHALLOW) |
573 | ref = ref->next; | |
f0e278b1 JH |
574 | if (!ref) |
575 | return -1; /* end of the list */ | |
576 | *rm = ref->next; | |
577 | hashcpy(sha1, ref->old_sha1); | |
578 | return 0; | |
6b67e0dc JH |
579 | } |
580 | ||
47abd85b | 581 | static int store_updated_refs(const char *raw_url, const char *remote_name, |
f3cb169b | 582 | struct ref *ref_map) |
b888d61c DB |
583 | { |
584 | FILE *fp; | |
585 | struct commit *commit; | |
4b3b33a7 | 586 | int url_len, i, rc = 0; |
5914f2d0 | 587 | struct strbuf note = STRBUF_INIT; |
b888d61c DB |
588 | const char *what, *kind; |
589 | struct ref *rm; | |
28a15401 | 590 | char *url, *filename = dry_run ? "/dev/null" : git_path("FETCH_HEAD"); |
900f2814 | 591 | int want_status; |
b888d61c | 592 | |
d6617c7c AGR |
593 | fp = fopen(filename, "a"); |
594 | if (!fp) | |
bd4a51fc | 595 | return error(_("cannot open %s: %s\n"), filename, strerror(errno)); |
47abd85b | 596 | |
fb0cc87e DB |
597 | if (raw_url) |
598 | url = transport_anonymize_url(raw_url); | |
599 | else | |
600 | url = xstrdup("foreign"); | |
6b67e0dc | 601 | |
f0e278b1 | 602 | rm = ref_map; |
3f7d11c4 | 603 | if (check_everything_connected(iterate_ref_map, 0, &rm)) { |
9516a598 TRC |
604 | rc = error(_("%s did not send all necessary objects\n"), url); |
605 | goto abort; | |
606 | } | |
6b67e0dc | 607 | |
96890f4c | 608 | /* |
900f2814 JK |
609 | * We do a pass for each fetch_head_status type in their enum order, so |
610 | * merged entries are written before not-for-merge. That lets readers | |
611 | * use FETCH_HEAD as a refname to refer to the ref to be merged. | |
96890f4c | 612 | */ |
900f2814 JK |
613 | for (want_status = FETCH_HEAD_MERGE; |
614 | want_status <= FETCH_HEAD_IGNORE; | |
615 | want_status++) { | |
96890f4c JH |
616 | for (rm = ref_map; rm; rm = rm->next) { |
617 | struct ref *ref = NULL; | |
900f2814 | 618 | const char *merge_status_marker = ""; |
96890f4c | 619 | |
4820a33b NTND |
620 | if (rm->status == REF_STATUS_REJECT_SHALLOW) { |
621 | if (want_status == FETCH_HEAD_MERGE) | |
622 | warning(_("reject %s because shallow roots are not allowed to be updated"), | |
623 | rm->peer_ref ? rm->peer_ref->name : rm->name); | |
624 | continue; | |
625 | } | |
626 | ||
96890f4c JH |
627 | commit = lookup_commit_reference_gently(rm->old_sha1, 1); |
628 | if (!commit) | |
900f2814 | 629 | rm->fetch_head_status = FETCH_HEAD_NOT_FOR_MERGE; |
96890f4c | 630 | |
900f2814 | 631 | if (rm->fetch_head_status != want_status) |
96890f4c JH |
632 | continue; |
633 | ||
634 | if (rm->peer_ref) { | |
635 | ref = xcalloc(1, sizeof(*ref) + strlen(rm->peer_ref->name) + 1); | |
636 | strcpy(ref->name, rm->peer_ref->name); | |
637 | hashcpy(ref->old_sha1, rm->peer_ref->old_sha1); | |
638 | hashcpy(ref->new_sha1, rm->old_sha1); | |
639 | ref->force = rm->peer_ref->force; | |
640 | } | |
b888d61c | 641 | |
b888d61c | 642 | |
96890f4c JH |
643 | if (!strcmp(rm->name, "HEAD")) { |
644 | kind = ""; | |
645 | what = ""; | |
646 | } | |
59556548 | 647 | else if (starts_with(rm->name, "refs/heads/")) { |
96890f4c JH |
648 | kind = "branch"; |
649 | what = rm->name + 11; | |
650 | } | |
59556548 | 651 | else if (starts_with(rm->name, "refs/tags/")) { |
96890f4c JH |
652 | kind = "tag"; |
653 | what = rm->name + 10; | |
654 | } | |
59556548 | 655 | else if (starts_with(rm->name, "refs/remotes/")) { |
96890f4c JH |
656 | kind = "remote-tracking branch"; |
657 | what = rm->name + 13; | |
658 | } | |
659 | else { | |
660 | kind = ""; | |
661 | what = rm->name; | |
662 | } | |
b888d61c | 663 | |
96890f4c JH |
664 | url_len = strlen(url); |
665 | for (i = url_len - 1; url[i] == '/' && 0 <= i; i--) | |
666 | ; | |
667 | url_len = i + 1; | |
668 | if (4 < i && !strncmp(".git", url + i - 3, 4)) | |
669 | url_len = i - 3; | |
670 | ||
671 | strbuf_reset(¬e); | |
672 | if (*what) { | |
673 | if (*kind) | |
674 | strbuf_addf(¬e, "%s ", kind); | |
675 | strbuf_addf(¬e, "'%s' of ", what); | |
676 | } | |
900f2814 JK |
677 | switch (rm->fetch_head_status) { |
678 | case FETCH_HEAD_NOT_FOR_MERGE: | |
679 | merge_status_marker = "not-for-merge"; | |
680 | /* fall-through */ | |
681 | case FETCH_HEAD_MERGE: | |
682 | fprintf(fp, "%s\t%s\t%s", | |
683 | sha1_to_hex(rm->old_sha1), | |
684 | merge_status_marker, | |
685 | note.buf); | |
686 | for (i = 0; i < url_len; ++i) | |
687 | if ('\n' == url[i]) | |
688 | fputs("\\n", fp); | |
689 | else | |
690 | fputc(url[i], fp); | |
691 | fputc('\n', fp); | |
692 | break; | |
693 | default: | |
694 | /* do not write anything to FETCH_HEAD */ | |
695 | break; | |
696 | } | |
96890f4c JH |
697 | |
698 | strbuf_reset(¬e); | |
699 | if (ref) { | |
6da618d5 | 700 | rc |= update_local_ref(ref, what, rm, ¬e); |
96890f4c JH |
701 | free(ref); |
702 | } else | |
703 | strbuf_addf(¬e, "* %-*s %-*s -> FETCH_HEAD", | |
704 | TRANSPORT_SUMMARY_WIDTH, | |
705 | *kind ? kind : "branch", | |
706 | REFCOL_WIDTH, | |
707 | *what ? what : "HEAD"); | |
708 | if (note.len) { | |
709 | if (verbosity >= 0 && !shown_url) { | |
710 | fprintf(stderr, _("From %.*s\n"), | |
711 | url_len, url); | |
712 | shown_url = 1; | |
713 | } | |
714 | if (verbosity >= 0) | |
715 | fprintf(stderr, " %s\n", note.buf); | |
165f3902 NP |
716 | } |
717 | } | |
b888d61c | 718 | } |
9516a598 | 719 | |
fa250759 | 720 | if (rc & STORE_REF_ERROR_DF_CONFLICT) |
bd4a51fc | 721 | error(_("some local refs could not be updated; try running\n" |
f3cb169b | 722 | " 'git remote prune %s' to remove any old, conflicting " |
bd4a51fc | 723 | "branches"), remote_name); |
9516a598 TRC |
724 | |
725 | abort: | |
5914f2d0 | 726 | strbuf_release(¬e); |
9516a598 TRC |
727 | free(url); |
728 | fclose(fp); | |
efb98b44 | 729 | return rc; |
b888d61c DB |
730 | } |
731 | ||
4191c356 SP |
732 | /* |
733 | * We would want to bypass the object transfer altogether if | |
d9eb0205 | 734 | * everything we are going to fetch already exists and is connected |
4191c356 | 735 | * locally. |
4191c356 SP |
736 | */ |
737 | static int quickfetch(struct ref *ref_map) | |
738 | { | |
f0e278b1 JH |
739 | struct ref *rm = ref_map; |
740 | ||
4191c356 SP |
741 | /* |
742 | * If we are deepening a shallow clone we already have these | |
743 | * objects reachable. Running rev-list here will return with | |
744 | * a good (0) exit status and we'll bypass the fetch that we | |
745 | * really need to perform. Claiming failure now will ensure | |
746 | * we perform the network exchange to deepen our history. | |
747 | */ | |
748 | if (depth) | |
749 | return -1; | |
f0e278b1 | 750 | return check_everything_connected(iterate_ref_map, 1, &rm); |
4191c356 SP |
751 | } |
752 | ||
b888d61c DB |
753 | static int fetch_refs(struct transport *transport, struct ref *ref_map) |
754 | { | |
4191c356 SP |
755 | int ret = quickfetch(ref_map); |
756 | if (ret) | |
757 | ret = transport_fetch_refs(transport, ref_map); | |
b888d61c | 758 | if (!ret) |
f3cb169b JK |
759 | ret |= store_updated_refs(transport->url, |
760 | transport->remote->name, | |
761 | ref_map); | |
1788c39c | 762 | transport_unlock_pack(transport); |
b888d61c DB |
763 | return ret; |
764 | } | |
765 | ||
4b3b33a7 TM |
766 | static int prune_refs(struct refspec *refs, int ref_count, struct ref *ref_map, |
767 | const char *raw_url) | |
f360d844 | 768 | { |
4b3b33a7 | 769 | int url_len, i, result = 0; |
ed43de6e | 770 | struct ref *ref, *stale_refs = get_stale_heads(refs, ref_count, ref_map); |
4b3b33a7 | 771 | char *url; |
f360d844 | 772 | const char *dangling_msg = dry_run |
18986d53 NTND |
773 | ? _(" (%s will become dangling)") |
774 | : _(" (%s has become dangling)"); | |
f360d844 | 775 | |
4b3b33a7 TM |
776 | if (raw_url) |
777 | url = transport_anonymize_url(raw_url); | |
778 | else | |
779 | url = xstrdup("foreign"); | |
780 | ||
781 | url_len = strlen(url); | |
782 | for (i = url_len - 1; url[i] == '/' && 0 <= i; i--) | |
783 | ; | |
784 | ||
785 | url_len = i + 1; | |
786 | if (4 < i && !strncmp(".git", url + i - 3, 4)) | |
787 | url_len = i - 3; | |
788 | ||
f360d844 JS |
789 | for (ref = stale_refs; ref; ref = ref->next) { |
790 | if (!dry_run) | |
791 | result |= delete_ref(ref->name, NULL, 0); | |
4b3b33a7 TM |
792 | if (verbosity >= 0 && !shown_url) { |
793 | fprintf(stderr, _("From %.*s\n"), url_len, url); | |
794 | shown_url = 1; | |
795 | } | |
f360d844 JS |
796 | if (verbosity >= 0) { |
797 | fprintf(stderr, " x %-*s %-*s -> %s\n", | |
754395d3 | 798 | TRANSPORT_SUMMARY(_("[deleted]")), |
502681cd | 799 | REFCOL_WIDTH, _("(none)"), prettify_refname(ref->name)); |
f360d844 JS |
800 | warn_dangling_symref(stderr, dangling_msg, ref->name); |
801 | } | |
802 | } | |
4b3b33a7 | 803 | free(url); |
f360d844 JS |
804 | free_refs(stale_refs); |
805 | return result; | |
806 | } | |
807 | ||
8ee5d731 JS |
808 | static void check_not_current_branch(struct ref *ref_map) |
809 | { | |
810 | struct branch *current_branch = branch_get(NULL); | |
811 | ||
812 | if (is_bare_repository() || !current_branch) | |
813 | return; | |
814 | ||
815 | for (; ref_map; ref_map = ref_map->next) | |
816 | if (ref_map->peer_ref && !strcmp(current_branch->refname, | |
817 | ref_map->peer_ref->name)) | |
bd4a51fc ÆAB |
818 | die(_("Refusing to fetch into current branch %s " |
819 | "of non-bare repository"), current_branch->refname); | |
8ee5d731 JS |
820 | } |
821 | ||
e6cc5104 JH |
822 | static int truncate_fetch_head(void) |
823 | { | |
824 | char *filename = git_path("FETCH_HEAD"); | |
825 | FILE *fp = fopen(filename, "w"); | |
826 | ||
827 | if (!fp) | |
bd4a51fc | 828 | return error(_("cannot open %s: %s\n"), filename, strerror(errno)); |
e6cc5104 JH |
829 | fclose(fp); |
830 | return 0; | |
831 | } | |
832 | ||
db5723c6 JH |
833 | static void set_option(struct transport *transport, const char *name, const char *value) |
834 | { | |
835 | int r = transport_set_option(transport, name, value); | |
836 | if (r < 0) | |
837 | die(_("Option \"%s\" value \"%s\" is not valid for %s"), | |
838 | name, value, transport->url); | |
839 | if (r > 0) | |
840 | warning(_("Option \"%s\" is ignored for %s\n"), | |
841 | name, transport->url); | |
842 | } | |
843 | ||
0f73f8bd | 844 | static struct transport *prepare_transport(struct remote *remote) |
db5723c6 JH |
845 | { |
846 | struct transport *transport; | |
847 | transport = transport_get(remote, NULL); | |
848 | transport_set_verbosity(transport, verbosity, progress); | |
849 | if (upload_pack) | |
850 | set_option(transport, TRANS_OPT_UPLOADPACK, upload_pack); | |
851 | if (keep) | |
852 | set_option(transport, TRANS_OPT_KEEP, "yes"); | |
853 | if (depth) | |
854 | set_option(transport, TRANS_OPT_DEPTH, depth); | |
48d25cae NTND |
855 | if (update_shallow) |
856 | set_option(transport, TRANS_OPT_UPDATE_SHALLOW, "yes"); | |
db5723c6 JH |
857 | return transport; |
858 | } | |
859 | ||
069d5032 JH |
860 | static void backfill_tags(struct transport *transport, struct ref *ref_map) |
861 | { | |
b26ed430 JH |
862 | if (transport->cannot_reuse) { |
863 | gsecondary = prepare_transport(transport->remote); | |
864 | transport = gsecondary; | |
865 | } | |
866 | ||
069d5032 JH |
867 | transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL); |
868 | transport_set_option(transport, TRANS_OPT_DEPTH, "0"); | |
869 | fetch_refs(transport, ref_map); | |
b26ed430 JH |
870 | |
871 | if (gsecondary) { | |
872 | transport_disconnect(gsecondary); | |
873 | gsecondary = NULL; | |
874 | } | |
069d5032 JH |
875 | } |
876 | ||
b888d61c DB |
877 | static int do_fetch(struct transport *transport, |
878 | struct refspec *refs, int ref_count) | |
879 | { | |
b87dbcc8 | 880 | struct string_list existing_refs = STRING_LIST_INIT_DUP; |
7f98428d | 881 | struct ref *ref_map; |
b888d61c DB |
882 | struct ref *rm; |
883 | int autotags = (transport->remote->fetch_tags == 1); | |
5b87d8d3 | 884 | int retcode = 0; |
b1a01e1c JP |
885 | |
886 | for_each_ref(add_existing, &existing_refs); | |
887 | ||
ed368546 DJ |
888 | if (tags == TAGS_DEFAULT) { |
889 | if (transport->remote->fetch_tags == 2) | |
890 | tags = TAGS_SET; | |
891 | if (transport->remote->fetch_tags == -1) | |
892 | tags = TAGS_UNSET; | |
893 | } | |
b888d61c | 894 | |
824d5776 | 895 | if (!transport->get_refs_list || !transport->fetch) |
bd4a51fc | 896 | die(_("Don't know how to fetch from %s"), transport->url); |
b888d61c DB |
897 | |
898 | /* if not appending, truncate FETCH_HEAD */ | |
28a15401 | 899 | if (!append && !dry_run) { |
5b87d8d3 MH |
900 | retcode = truncate_fetch_head(); |
901 | if (retcode) | |
902 | goto cleanup; | |
d6617c7c | 903 | } |
b888d61c DB |
904 | |
905 | ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags); | |
8ee5d731 JS |
906 | if (!update_head_ok) |
907 | check_not_current_branch(ref_map); | |
b888d61c DB |
908 | |
909 | for (rm = ref_map; rm; rm = rm->next) { | |
b1a01e1c | 910 | if (rm->peer_ref) { |
6f64a16f MH |
911 | struct string_list_item *peer_item = |
912 | string_list_lookup(&existing_refs, | |
913 | rm->peer_ref->name); | |
b1a01e1c JP |
914 | if (peer_item) |
915 | hashcpy(rm->peer_ref->old_sha1, | |
916 | peer_item->util); | |
917 | } | |
b888d61c DB |
918 | } |
919 | ||
41fa7d2e SP |
920 | if (tags == TAGS_DEFAULT && autotags) |
921 | transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1"); | |
ed43de6e | 922 | if (prune) { |
0838bf47 MH |
923 | /* |
924 | * We only prune based on refspecs specified | |
925 | * explicitly (via command line or configuration); we | |
926 | * don't care whether --tags was specified. | |
927 | */ | |
c5a84e92 | 928 | if (ref_count) { |
4b3b33a7 | 929 | prune_refs(refs, ref_count, ref_map, transport->url); |
e8c1e6c7 | 930 | } else { |
0838bf47 MH |
931 | prune_refs(transport->remote->fetch, |
932 | transport->remote->fetch_refspec_nr, | |
4b3b33a7 TM |
933 | ref_map, |
934 | transport->url); | |
e8c1e6c7 | 935 | } |
ed43de6e | 936 | } |
10a6cc88 TM |
937 | if (fetch_refs(transport, ref_map)) { |
938 | free_refs(ref_map); | |
939 | retcode = 1; | |
940 | goto cleanup; | |
941 | } | |
7f98428d | 942 | free_refs(ref_map); |
b888d61c DB |
943 | |
944 | /* if neither --no-tags nor --tags was specified, do automated tag | |
945 | * following ... */ | |
83201998 | 946 | if (tags == TAGS_DEFAULT && autotags) { |
c50b2b47 SP |
947 | struct ref **tail = &ref_map; |
948 | ref_map = NULL; | |
949 | find_non_local_tags(transport, &ref_map, &tail); | |
069d5032 JH |
950 | if (ref_map) |
951 | backfill_tags(transport, ref_map); | |
b888d61c DB |
952 | free_refs(ref_map); |
953 | } | |
954 | ||
5b87d8d3 | 955 | cleanup: |
f83918ed | 956 | string_list_clear(&existing_refs, 1); |
5b87d8d3 | 957 | return retcode; |
b888d61c DB |
958 | } |
959 | ||
9c4a036b BG |
960 | static int get_one_remote_for_fetch(struct remote *remote, void *priv) |
961 | { | |
962 | struct string_list *list = priv; | |
7cc91a2f | 963 | if (!remote->skip_default_update) |
1d2f80fa | 964 | string_list_append(list, remote->name); |
9c4a036b BG |
965 | return 0; |
966 | } | |
967 | ||
968 | struct remote_group_data { | |
969 | const char *name; | |
970 | struct string_list *list; | |
971 | }; | |
972 | ||
973 | static int get_remote_group(const char *key, const char *value, void *priv) | |
974 | { | |
975 | struct remote_group_data *g = priv; | |
976 | ||
59556548 | 977 | if (starts_with(key, "remotes.") && |
9c4a036b BG |
978 | !strcmp(key + 8, g->name)) { |
979 | /* split list by white space */ | |
980 | int space = strcspn(value, " \t\n"); | |
981 | while (*value) { | |
982 | if (space > 1) { | |
1d2f80fa JP |
983 | string_list_append(g->list, |
984 | xstrndup(value, space)); | |
9c4a036b BG |
985 | } |
986 | value += space + (value[space] != '\0'); | |
987 | space = strcspn(value, " \t\n"); | |
988 | } | |
989 | } | |
990 | ||
991 | return 0; | |
992 | } | |
993 | ||
994 | static int add_remote_or_group(const char *name, struct string_list *list) | |
995 | { | |
996 | int prev_nr = list->nr; | |
66dbfd55 GV |
997 | struct remote_group_data g; |
998 | g.name = name; g.list = list; | |
9c4a036b BG |
999 | |
1000 | git_config(get_remote_group, &g); | |
1001 | if (list->nr == prev_nr) { | |
1002 | struct remote *remote; | |
1003 | if (!remote_is_configured(name)) | |
1004 | return 0; | |
1005 | remote = remote_get(name); | |
1d2f80fa | 1006 | string_list_append(list, remote->name); |
9c4a036b BG |
1007 | } |
1008 | return 1; | |
1009 | } | |
1010 | ||
85556d4e | 1011 | static void add_options_to_argv(struct argv_array *argv) |
9c4a036b | 1012 | { |
28a15401 | 1013 | if (dry_run) |
85556d4e | 1014 | argv_array_push(argv, "--dry-run"); |
90765fa3 MH |
1015 | if (prune != -1) |
1016 | argv_array_push(argv, prune ? "--prune" : "--no-prune"); | |
bba5322a | 1017 | if (update_head_ok) |
85556d4e | 1018 | argv_array_push(argv, "--update-head-ok"); |
bba5322a | 1019 | if (force) |
85556d4e | 1020 | argv_array_push(argv, "--force"); |
bba5322a | 1021 | if (keep) |
85556d4e | 1022 | argv_array_push(argv, "--keep"); |
be254a0e | 1023 | if (recurse_submodules == RECURSE_SUBMODULES_ON) |
85556d4e | 1024 | argv_array_push(argv, "--recurse-submodules"); |
8f0700dd | 1025 | else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) |
85556d4e | 1026 | argv_array_push(argv, "--recurse-submodules=on-demand"); |
85566460 DJ |
1027 | if (tags == TAGS_SET) |
1028 | argv_array_push(argv, "--tags"); | |
1029 | else if (tags == TAGS_UNSET) | |
1030 | argv_array_push(argv, "--no-tags"); | |
9c4a036b | 1031 | if (verbosity >= 2) |
85556d4e | 1032 | argv_array_push(argv, "-v"); |
9c4a036b | 1033 | if (verbosity >= 1) |
85556d4e | 1034 | argv_array_push(argv, "-v"); |
9c4a036b | 1035 | else if (verbosity < 0) |
85556d4e | 1036 | argv_array_push(argv, "-q"); |
7dce19d3 JL |
1037 | |
1038 | } | |
1039 | ||
1040 | static int fetch_multiple(struct string_list *list) | |
1041 | { | |
1042 | int i, result = 0; | |
85556d4e | 1043 | struct argv_array argv = ARGV_ARRAY_INIT; |
9c4a036b | 1044 | |
e6cc5104 JH |
1045 | if (!append && !dry_run) { |
1046 | int errcode = truncate_fetch_head(); | |
1047 | if (errcode) | |
1048 | return errcode; | |
1049 | } | |
1050 | ||
85556d4e JK |
1051 | argv_array_pushl(&argv, "fetch", "--append", NULL); |
1052 | add_options_to_argv(&argv); | |
1053 | ||
9c4a036b BG |
1054 | for (i = 0; i < list->nr; i++) { |
1055 | const char *name = list->items[i].string; | |
85556d4e | 1056 | argv_array_push(&argv, name); |
9c4a036b | 1057 | if (verbosity >= 0) |
bd4a51fc | 1058 | printf(_("Fetching %s\n"), name); |
85556d4e | 1059 | if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) { |
bd4a51fc | 1060 | error(_("Could not fetch %s"), name); |
9c4a036b BG |
1061 | result = 1; |
1062 | } | |
85556d4e | 1063 | argv_array_pop(&argv); |
9c4a036b BG |
1064 | } |
1065 | ||
85556d4e | 1066 | argv_array_clear(&argv); |
9c4a036b BG |
1067 | return result; |
1068 | } | |
1069 | ||
1070 | static int fetch_one(struct remote *remote, int argc, const char **argv) | |
b888d61c | 1071 | { |
b888d61c | 1072 | static const char **refs = NULL; |
d8ead159 | 1073 | struct refspec *refspec; |
b888d61c | 1074 | int ref_nr = 0; |
7b7f39ea | 1075 | int exit_code; |
b888d61c | 1076 | |
fa685bdf | 1077 | if (!remote) |
bd4a51fc ÆAB |
1078 | die(_("No remote repository specified. Please, specify either a URL or a\n" |
1079 | "remote name from which new revisions should be fetched.")); | |
fa685bdf | 1080 | |
db5723c6 | 1081 | gtransport = prepare_transport(remote); |
737c5a9c MS |
1082 | |
1083 | if (prune < 0) { | |
1084 | /* no command line request */ | |
20419de9 JH |
1085 | if (0 <= gtransport->remote->prune) |
1086 | prune = gtransport->remote->prune; | |
737c5a9c MS |
1087 | else if (0 <= fetch_prune_config) |
1088 | prune = fetch_prune_config; | |
1089 | else | |
1090 | prune = PRUNE_BY_DEFAULT; | |
1091 | } | |
1092 | ||
9c4a036b | 1093 | if (argc > 0) { |
b888d61c | 1094 | int j = 0; |
bf7e645c | 1095 | int i; |
83201998 | 1096 | refs = xcalloc(argc + 1, sizeof(const char *)); |
9c4a036b | 1097 | for (i = 0; i < argc; i++) { |
b888d61c | 1098 | if (!strcmp(argv[i], "tag")) { |
b888d61c | 1099 | i++; |
f53423b0 | 1100 | if (i >= argc) |
bd4a51fc | 1101 | die(_("You need to specify a tag name.")); |
b2724c87 JK |
1102 | refs[j++] = xstrfmt("refs/tags/%s:refs/tags/%s", |
1103 | argv[i], argv[i]); | |
b888d61c DB |
1104 | } else |
1105 | refs[j++] = argv[i]; | |
b888d61c DB |
1106 | } |
1107 | refs[j] = NULL; | |
1108 | ref_nr = j; | |
b888d61c DB |
1109 | } |
1110 | ||
57b235a4 | 1111 | sigchain_push_common(unlock_pack_on_signal); |
e4022ed2 | 1112 | atexit(unlock_pack); |
d8ead159 | 1113 | refspec = parse_fetch_refspec(ref_nr, refs); |
af234459 | 1114 | exit_code = do_fetch(gtransport, refspec, ref_nr); |
5caf1973 | 1115 | free_refspec(ref_nr, refspec); |
af234459 JH |
1116 | transport_disconnect(gtransport); |
1117 | gtransport = NULL; | |
7b7f39ea | 1118 | return exit_code; |
b888d61c | 1119 | } |
9c4a036b BG |
1120 | |
1121 | int cmd_fetch(int argc, const char **argv, const char *prefix) | |
1122 | { | |
1123 | int i; | |
183113a5 | 1124 | struct string_list list = STRING_LIST_INIT_NODUP; |
9c4a036b BG |
1125 | struct remote *remote; |
1126 | int result = 0; | |
1991006c | 1127 | struct argv_array argv_gc_auto = ARGV_ARRAY_INIT; |
9c4a036b | 1128 | |
bbc30f99 JK |
1129 | packet_trace_identity("fetch"); |
1130 | ||
9c4a036b BG |
1131 | /* Record the command line for the reflog */ |
1132 | strbuf_addstr(&default_rla, "fetch"); | |
1133 | for (i = 1; i < argc; i++) | |
1134 | strbuf_addf(&default_rla, " %s", argv[i]); | |
1135 | ||
737c5a9c MS |
1136 | git_config(git_fetch_config, NULL); |
1137 | ||
9c4a036b BG |
1138 | argc = parse_options(argc, argv, prefix, |
1139 | builtin_fetch_options, builtin_fetch_usage, 0); | |
1140 | ||
4dcb167f NTND |
1141 | if (unshallow) { |
1142 | if (depth) | |
1143 | die(_("--depth and --unshallow cannot be used together")); | |
1144 | else if (!is_repository_shallow()) | |
1145 | die(_("--unshallow on a complete repository does not make sense")); | |
1146 | else { | |
1147 | static char inf_depth[12]; | |
1148 | sprintf(inf_depth, "%d", INFINITE_DEPTH); | |
1149 | depth = inf_depth; | |
1150 | } | |
1151 | } | |
1152 | ||
5594bcad NTND |
1153 | /* no need to be strict, transport_set_option() will validate it again */ |
1154 | if (depth && atoi(depth) < 1) | |
1155 | die(_("depth %s is not a positive number"), depth); | |
1156 | ||
18322bad JL |
1157 | if (recurse_submodules != RECURSE_SUBMODULES_OFF) { |
1158 | if (recurse_submodules_default) { | |
1159 | int arg = parse_fetch_recurse_submodules_arg("--recurse-submodules-default", recurse_submodules_default); | |
1160 | set_config_fetch_recurse_submodules(arg); | |
1161 | } | |
1162 | gitmodules_config(); | |
1163 | git_config(submodule_config, NULL); | |
1164 | } | |
1165 | ||
9c4a036b BG |
1166 | if (all) { |
1167 | if (argc == 1) | |
bd4a51fc | 1168 | die(_("fetch --all does not take a repository argument")); |
9c4a036b | 1169 | else if (argc > 1) |
bd4a51fc | 1170 | die(_("fetch --all does not make sense with refspecs")); |
9c4a036b BG |
1171 | (void) for_each_remote(get_one_remote_for_fetch, &list); |
1172 | result = fetch_multiple(&list); | |
1173 | } else if (argc == 0) { | |
1174 | /* No arguments -- use default remote */ | |
1175 | remote = remote_get(NULL); | |
1176 | result = fetch_one(remote, argc, argv); | |
16679e37 BG |
1177 | } else if (multiple) { |
1178 | /* All arguments are assumed to be remotes or groups */ | |
1179 | for (i = 0; i < argc; i++) | |
1180 | if (!add_remote_or_group(argv[i], &list)) | |
bd4a51fc | 1181 | die(_("No such remote or remote group: %s"), argv[i]); |
16679e37 | 1182 | result = fetch_multiple(&list); |
9c4a036b BG |
1183 | } else { |
1184 | /* Single remote or group */ | |
1185 | (void) add_remote_or_group(argv[0], &list); | |
1186 | if (list.nr > 1) { | |
1187 | /* More than one remote */ | |
1188 | if (argc > 1) | |
bd4a51fc | 1189 | die(_("Fetching a group and specifying refspecs does not make sense")); |
9c4a036b BG |
1190 | result = fetch_multiple(&list); |
1191 | } else { | |
1192 | /* Zero or one remotes */ | |
1193 | remote = remote_get(argv[0]); | |
1194 | result = fetch_one(remote, argc-1, argv+1); | |
1195 | } | |
1196 | } | |
1197 | ||
be254a0e | 1198 | if (!result && (recurse_submodules != RECURSE_SUBMODULES_OFF)) { |
85556d4e JK |
1199 | struct argv_array options = ARGV_ARRAY_INIT; |
1200 | ||
1201 | add_options_to_argv(&options); | |
50d89ad6 | 1202 | result = fetch_populated_submodules(&options, |
7dce19d3 | 1203 | submodule_prefix, |
8f0700dd | 1204 | recurse_submodules, |
7dce19d3 | 1205 | verbosity < 0); |
85556d4e | 1206 | argv_array_clear(&options); |
7dce19d3 JL |
1207 | } |
1208 | ||
9c4a036b BG |
1209 | /* All names were strdup()ed or strndup()ed */ |
1210 | list.strdup_strings = 1; | |
1211 | string_list_clear(&list, 0); | |
1212 | ||
1991006c | 1213 | argv_array_pushl(&argv_gc_auto, "gc", "--auto", NULL); |
6fceed3b NTND |
1214 | if (verbosity < 0) |
1215 | argv_array_push(&argv_gc_auto, "--quiet"); | |
1991006c NTND |
1216 | run_command_v_opt(argv_gc_auto.argv, RUN_GIT_CMD); |
1217 | argv_array_clear(&argv_gc_auto); | |
131b8fcb | 1218 | |
9c4a036b BG |
1219 | return result; |
1220 | } |