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