]>
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" |
b888d61c | 14 | |
83201998 | 15 | static const char * const builtin_fetch_usage[] = { |
1b1dd23f | 16 | "git fetch [options] [<repository> <refspec>...]", |
9c4a036b | 17 | "git fetch [options] <group>", |
16679e37 | 18 | "git fetch --multiple [options] [<repository> | <group>]...", |
9c4a036b | 19 | "git fetch --all [options]", |
83201998 KH |
20 | NULL |
21 | }; | |
b888d61c | 22 | |
83201998 KH |
23 | enum { |
24 | TAGS_UNSET = 0, | |
25 | TAGS_DEFAULT = 1, | |
26 | TAGS_SET = 2 | |
27 | }; | |
28 | ||
16679e37 | 29 | static int all, append, force, keep, multiple, update_head_ok, verbosity; |
83201998 | 30 | static int tags = TAGS_DEFAULT; |
4191c356 | 31 | static const char *depth; |
83201998 | 32 | static const char *upload_pack; |
2d324efa | 33 | static struct strbuf default_rla = STRBUF_INIT; |
e4022ed2 SP |
34 | static struct transport *transport; |
35 | ||
83201998 | 36 | static struct option builtin_fetch_options[] = { |
7f87aff2 | 37 | OPT__VERBOSITY(&verbosity), |
9c4a036b BG |
38 | OPT_BOOLEAN(0, "all", &all, |
39 | "fetch from all remotes"), | |
83201998 KH |
40 | OPT_BOOLEAN('a', "append", &append, |
41 | "append to .git/FETCH_HEAD instead of overwriting"), | |
42 | OPT_STRING(0, "upload-pack", &upload_pack, "PATH", | |
43 | "path to upload pack on remote end"), | |
44 | OPT_BOOLEAN('f', "force", &force, | |
45 | "force overwrite of local branch"), | |
16679e37 BG |
46 | OPT_BOOLEAN('m', "multiple", &multiple, |
47 | "fetch from multiple remotes"), | |
83201998 KH |
48 | OPT_SET_INT('t', "tags", &tags, |
49 | "fetch all tags and associated objects", TAGS_SET), | |
e7951290 JS |
50 | OPT_SET_INT('n', NULL, &tags, |
51 | "do not fetch all tags (--no-tags)", TAGS_UNSET), | |
83201998 KH |
52 | OPT_BOOLEAN('k', "keep", &keep, "keep downloaded pack"), |
53 | OPT_BOOLEAN('u', "update-head-ok", &update_head_ok, | |
54 | "allow updating of HEAD ref"), | |
55 | OPT_STRING(0, "depth", &depth, "DEPTH", | |
56 | "deepen history of shallow clone"), | |
57 | OPT_END() | |
58 | }; | |
59 | ||
e4022ed2 SP |
60 | static void unlock_pack(void) |
61 | { | |
62 | if (transport) | |
63 | transport_unlock_pack(transport); | |
64 | } | |
65 | ||
66 | static void unlock_pack_on_signal(int signo) | |
67 | { | |
68 | unlock_pack(); | |
4a16d072 | 69 | sigchain_pop(signo); |
e4022ed2 SP |
70 | raise(signo); |
71 | } | |
b888d61c | 72 | |
85682c19 | 73 | static void add_merge_config(struct ref **head, |
4577370e | 74 | const struct ref *remote_refs, |
85682c19 SP |
75 | struct branch *branch, |
76 | struct ref ***tail) | |
b888d61c | 77 | { |
85682c19 | 78 | int i; |
b888d61c | 79 | |
85682c19 SP |
80 | for (i = 0; i < branch->merge_nr; i++) { |
81 | struct ref *rm, **old_tail = *tail; | |
82 | struct refspec refspec; | |
83 | ||
84 | for (rm = *head; rm; rm = rm->next) { | |
85 | if (branch_merge_matches(branch, i, rm->name)) { | |
b888d61c | 86 | rm->merge = 1; |
85682c19 SP |
87 | break; |
88 | } | |
b888d61c | 89 | } |
85682c19 SP |
90 | if (rm) |
91 | continue; | |
92 | ||
9ad7c5ae JH |
93 | /* |
94 | * Not fetched to a tracking branch? We need to fetch | |
85682c19 | 95 | * it anyway to allow this branch's "branch.$name.merge" |
05207a28 | 96 | * to be honored by 'git pull', but we do not have to |
9ad7c5ae JH |
97 | * fail if branch.$name.merge is misconfigured to point |
98 | * at a nonexisting branch. If we were indeed called by | |
05207a28 | 99 | * 'git pull', it will notice the misconfiguration because |
9ad7c5ae JH |
100 | * there is no entry in the resulting FETCH_HEAD marked |
101 | * for merging. | |
85682c19 SP |
102 | */ |
103 | refspec.src = branch->merge[i]->src; | |
104 | refspec.dst = NULL; | |
105 | refspec.pattern = 0; | |
106 | refspec.force = 0; | |
9ad7c5ae | 107 | get_fetch_map(remote_refs, &refspec, tail, 1); |
85682c19 SP |
108 | for (rm = *old_tail; rm; rm = rm->next) |
109 | rm->merge = 1; | |
b888d61c DB |
110 | } |
111 | } | |
112 | ||
767f176a SP |
113 | static void find_non_local_tags(struct transport *transport, |
114 | struct ref **head, | |
115 | struct ref ***tail); | |
116 | ||
b888d61c DB |
117 | static struct ref *get_ref_map(struct transport *transport, |
118 | struct refspec *refs, int ref_count, int tags, | |
119 | int *autotags) | |
120 | { | |
121 | int i; | |
122 | struct ref *rm; | |
123 | struct ref *ref_map = NULL; | |
124 | struct ref **tail = &ref_map; | |
125 | ||
4577370e | 126 | const struct ref *remote_refs = transport_get_remote_refs(transport); |
b888d61c | 127 | |
83201998 | 128 | if (ref_count || tags == TAGS_SET) { |
b888d61c | 129 | for (i = 0; i < ref_count; i++) { |
9ad7c5ae | 130 | get_fetch_map(remote_refs, &refs[i], &tail, 0); |
b888d61c DB |
131 | if (refs[i].dst && refs[i].dst[0]) |
132 | *autotags = 1; | |
133 | } | |
134 | /* Merge everything on the command line, but not --tags */ | |
135 | for (rm = ref_map; rm; rm = rm->next) | |
136 | rm->merge = 1; | |
e0aaa29f DB |
137 | if (tags == TAGS_SET) |
138 | get_fetch_map(remote_refs, tag_refspec, &tail, 0); | |
b888d61c DB |
139 | } else { |
140 | /* Use the defaults */ | |
141 | struct remote *remote = transport->remote; | |
85682c19 SP |
142 | struct branch *branch = branch_get(NULL); |
143 | int has_merge = branch_has_merge_config(branch); | |
144 | if (remote && (remote->fetch_refspec_nr || has_merge)) { | |
b888d61c | 145 | for (i = 0; i < remote->fetch_refspec_nr; i++) { |
9ad7c5ae | 146 | get_fetch_map(remote_refs, &remote->fetch[i], &tail, 0); |
b888d61c DB |
147 | if (remote->fetch[i].dst && |
148 | remote->fetch[i].dst[0]) | |
149 | *autotags = 1; | |
85682c19 | 150 | if (!i && !has_merge && ref_map && |
cfb8f898 | 151 | !remote->fetch[0].pattern) |
85682c19 | 152 | ref_map->merge = 1; |
b888d61c | 153 | } |
da0204df JS |
154 | /* |
155 | * if the remote we're fetching from is the same | |
156 | * as given in branch.<name>.remote, we add the | |
157 | * ref given in branch.<name>.merge, too. | |
158 | */ | |
9ad7c5ae JH |
159 | if (has_merge && |
160 | !strcmp(branch->remote_name, remote->name)) | |
85682c19 | 161 | add_merge_config(&ref_map, remote_refs, branch, &tail); |
b888d61c DB |
162 | } else { |
163 | ref_map = get_remote_ref(remote_refs, "HEAD"); | |
9ad7c5ae JH |
164 | if (!ref_map) |
165 | die("Couldn't find remote ref HEAD"); | |
b888d61c | 166 | ref_map->merge = 1; |
5aaf7f2a | 167 | tail = &ref_map->next; |
b888d61c DB |
168 | } |
169 | } | |
767f176a SP |
170 | if (tags == TAGS_DEFAULT && *autotags) |
171 | find_non_local_tags(transport, &ref_map, &tail); | |
2467a4fa | 172 | ref_remove_duplicates(ref_map); |
b888d61c DB |
173 | |
174 | return ref_map; | |
175 | } | |
176 | ||
fa250759 JK |
177 | #define STORE_REF_ERROR_OTHER 1 |
178 | #define STORE_REF_ERROR_DF_CONFLICT 2 | |
179 | ||
b888d61c DB |
180 | static int s_update_ref(const char *action, |
181 | struct ref *ref, | |
182 | int check_old) | |
183 | { | |
184 | char msg[1024]; | |
185 | char *rla = getenv("GIT_REFLOG_ACTION"); | |
186 | static struct ref_lock *lock; | |
187 | ||
188 | if (!rla) | |
2d324efa | 189 | rla = default_rla.buf; |
b888d61c DB |
190 | snprintf(msg, sizeof(msg), "%s: %s", rla, action); |
191 | lock = lock_any_ref_for_update(ref->name, | |
192 | check_old ? ref->old_sha1 : NULL, 0); | |
193 | if (!lock) | |
fa250759 JK |
194 | return errno == ENOTDIR ? STORE_REF_ERROR_DF_CONFLICT : |
195 | STORE_REF_ERROR_OTHER; | |
b888d61c | 196 | if (write_ref_sha1(lock, ref->new_sha1, msg) < 0) |
fa250759 JK |
197 | return errno == ENOTDIR ? STORE_REF_ERROR_DF_CONFLICT : |
198 | STORE_REF_ERROR_OTHER; | |
b888d61c DB |
199 | return 0; |
200 | } | |
201 | ||
165f3902 | 202 | #define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3) |
9ef4272b | 203 | #define REFCOL_WIDTH 10 |
165f3902 | 204 | |
b888d61c | 205 | static int update_local_ref(struct ref *ref, |
165f3902 | 206 | const char *remote, |
165f3902 | 207 | char *display) |
b888d61c | 208 | { |
b888d61c DB |
209 | struct commit *current = NULL, *updated; |
210 | enum object_type type; | |
211 | struct branch *current_branch = branch_get(NULL); | |
4577e483 | 212 | const char *pretty_ref = prettify_refname(ref->name); |
b888d61c | 213 | |
165f3902 | 214 | *display = 0; |
b888d61c DB |
215 | type = sha1_object_info(ref->new_sha1, NULL); |
216 | if (type < 0) | |
217 | die("object %s not found", sha1_to_hex(ref->new_sha1)); | |
218 | ||
b888d61c | 219 | if (!hashcmp(ref->old_sha1, ref->new_sha1)) { |
7f87aff2 | 220 | if (verbosity > 0) |
9ef4272b PH |
221 | sprintf(display, "= %-*s %-*s -> %s", SUMMARY_WIDTH, |
222 | "[up to date]", REFCOL_WIDTH, remote, | |
223 | pretty_ref); | |
b888d61c DB |
224 | return 0; |
225 | } | |
226 | ||
b3abdd9d SP |
227 | if (current_branch && |
228 | !strcmp(ref->name, current_branch->name) && | |
b888d61c DB |
229 | !(update_head_ok || is_bare_repository()) && |
230 | !is_null_sha1(ref->old_sha1)) { | |
231 | /* | |
232 | * If this is the head, and it's not okay to update | |
233 | * the head, and the old value of the head isn't empty... | |
234 | */ | |
9ef4272b PH |
235 | sprintf(display, "! %-*s %-*s -> %s (can't fetch in current branch)", |
236 | SUMMARY_WIDTH, "[rejected]", REFCOL_WIDTH, remote, | |
237 | pretty_ref); | |
b888d61c DB |
238 | return 1; |
239 | } | |
240 | ||
241 | if (!is_null_sha1(ref->old_sha1) && | |
242 | !prefixcmp(ref->name, "refs/tags/")) { | |
6315472e JK |
243 | int r; |
244 | r = s_update_ref("updating tag", ref, 0); | |
245 | sprintf(display, "%c %-*s %-*s -> %s%s", r ? '!' : '-', | |
9ef4272b | 246 | SUMMARY_WIDTH, "[tag update]", REFCOL_WIDTH, remote, |
6315472e JK |
247 | pretty_ref, r ? " (unable to update local ref)" : ""); |
248 | return r; | |
b888d61c DB |
249 | } |
250 | ||
cfa5b2b7 SP |
251 | current = lookup_commit_reference_gently(ref->old_sha1, 1); |
252 | updated = lookup_commit_reference_gently(ref->new_sha1, 1); | |
b888d61c | 253 | if (!current || !updated) { |
165f3902 NP |
254 | const char *msg; |
255 | const char *what; | |
6315472e | 256 | int r; |
165f3902 | 257 | if (!strncmp(ref->name, "refs/tags/", 10)) { |
b888d61c | 258 | msg = "storing tag"; |
165f3902 NP |
259 | what = "[new tag]"; |
260 | } | |
261 | else { | |
b888d61c | 262 | msg = "storing head"; |
165f3902 NP |
263 | what = "[new branch]"; |
264 | } | |
265 | ||
6315472e JK |
266 | r = s_update_ref(msg, ref, 0); |
267 | sprintf(display, "%c %-*s %-*s -> %s%s", r ? '!' : '*', | |
268 | SUMMARY_WIDTH, what, REFCOL_WIDTH, remote, pretty_ref, | |
269 | r ? " (unable to update local ref)" : ""); | |
270 | return r; | |
b888d61c DB |
271 | } |
272 | ||
b888d61c | 273 | if (in_merge_bases(current, &updated, 1)) { |
165f3902 | 274 | char quickref[83]; |
6315472e | 275 | int r; |
165f3902 NP |
276 | strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV)); |
277 | strcat(quickref, ".."); | |
278 | strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV)); | |
6315472e JK |
279 | r = s_update_ref("fast forward", ref, 1); |
280 | sprintf(display, "%c %-*s %-*s -> %s%s", r ? '!' : ' ', | |
281 | SUMMARY_WIDTH, quickref, REFCOL_WIDTH, remote, | |
282 | pretty_ref, r ? " (unable to update local ref)" : ""); | |
283 | return r; | |
165f3902 NP |
284 | } else if (force || ref->force) { |
285 | char quickref[84]; | |
6315472e | 286 | int r; |
165f3902 NP |
287 | strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV)); |
288 | strcat(quickref, "..."); | |
289 | strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV)); | |
6315472e JK |
290 | r = s_update_ref("forced-update", ref, 1); |
291 | sprintf(display, "%c %-*s %-*s -> %s (%s)", r ? '!' : '+', | |
292 | SUMMARY_WIDTH, quickref, REFCOL_WIDTH, remote, | |
293 | pretty_ref, | |
294 | r ? "unable to update local ref" : "forced update"); | |
295 | return r; | |
165f3902 | 296 | } else { |
9ef4272b PH |
297 | sprintf(display, "! %-*s %-*s -> %s (non fast forward)", |
298 | SUMMARY_WIDTH, "[rejected]", REFCOL_WIDTH, remote, | |
299 | pretty_ref); | |
b888d61c DB |
300 | return 1; |
301 | } | |
b888d61c DB |
302 | } |
303 | ||
47abd85b | 304 | static int store_updated_refs(const char *raw_url, const char *remote_name, |
f3cb169b | 305 | struct ref *ref_map) |
b888d61c DB |
306 | { |
307 | FILE *fp; | |
308 | struct commit *commit; | |
efb98b44 | 309 | int url_len, i, note_len, shown_url = 0, rc = 0; |
b888d61c DB |
310 | char note[1024]; |
311 | const char *what, *kind; | |
312 | struct ref *rm; | |
47abd85b | 313 | char *url, *filename = git_path("FETCH_HEAD"); |
b888d61c | 314 | |
d6617c7c AGR |
315 | fp = fopen(filename, "a"); |
316 | if (!fp) | |
317 | return error("cannot open %s: %s\n", filename, strerror(errno)); | |
47abd85b AE |
318 | |
319 | url = transport_anonymize_url(raw_url); | |
b888d61c DB |
320 | for (rm = ref_map; rm; rm = rm->next) { |
321 | struct ref *ref = NULL; | |
322 | ||
323 | if (rm->peer_ref) { | |
324 | ref = xcalloc(1, sizeof(*ref) + strlen(rm->peer_ref->name) + 1); | |
325 | strcpy(ref->name, rm->peer_ref->name); | |
326 | hashcpy(ref->old_sha1, rm->peer_ref->old_sha1); | |
327 | hashcpy(ref->new_sha1, rm->old_sha1); | |
1aad91f5 | 328 | ref->force = rm->peer_ref->force; |
b888d61c DB |
329 | } |
330 | ||
cfa5b2b7 | 331 | commit = lookup_commit_reference_gently(rm->old_sha1, 1); |
b888d61c DB |
332 | if (!commit) |
333 | rm->merge = 0; | |
334 | ||
335 | if (!strcmp(rm->name, "HEAD")) { | |
336 | kind = ""; | |
337 | what = ""; | |
338 | } | |
339 | else if (!prefixcmp(rm->name, "refs/heads/")) { | |
340 | kind = "branch"; | |
341 | what = rm->name + 11; | |
342 | } | |
343 | else if (!prefixcmp(rm->name, "refs/tags/")) { | |
344 | kind = "tag"; | |
345 | what = rm->name + 10; | |
346 | } | |
347 | else if (!prefixcmp(rm->name, "refs/remotes/")) { | |
348 | kind = "remote branch"; | |
349 | what = rm->name + 13; | |
350 | } | |
351 | else { | |
352 | kind = ""; | |
353 | what = rm->name; | |
354 | } | |
355 | ||
356 | url_len = strlen(url); | |
357 | for (i = url_len - 1; url[i] == '/' && 0 <= i; i--) | |
358 | ; | |
359 | url_len = i + 1; | |
360 | if (4 < i && !strncmp(".git", url + i - 3, 4)) | |
361 | url_len = i - 3; | |
362 | ||
363 | note_len = 0; | |
364 | if (*what) { | |
365 | if (*kind) | |
366 | note_len += sprintf(note + note_len, "%s ", | |
367 | kind); | |
368 | note_len += sprintf(note + note_len, "'%s' of ", what); | |
369 | } | |
95405ba6 AR |
370 | note[note_len] = '\0'; |
371 | fprintf(fp, "%s\t%s\t%s", | |
b888d61c DB |
372 | sha1_to_hex(commit ? commit->object.sha1 : |
373 | rm->old_sha1), | |
374 | rm->merge ? "" : "not-for-merge", | |
375 | note); | |
95405ba6 AR |
376 | for (i = 0; i < url_len; ++i) |
377 | if ('\n' == url[i]) | |
378 | fputs("\\n", fp); | |
379 | else | |
380 | fputc(url[i], fp); | |
381 | fputc('\n', fp); | |
b888d61c | 382 | |
f59774ad | 383 | if (ref) |
7f87aff2 | 384 | rc |= update_local_ref(ref, what, note); |
e5c49826 | 385 | else |
f59774ad JK |
386 | sprintf(note, "* %-*s %-*s -> FETCH_HEAD", |
387 | SUMMARY_WIDTH, *kind ? kind : "branch", | |
388 | REFCOL_WIDTH, *what ? what : "HEAD"); | |
f59774ad | 389 | if (*note) { |
7f87aff2 | 390 | if (verbosity >= 0 && !shown_url) { |
f59774ad JK |
391 | fprintf(stderr, "From %.*s\n", |
392 | url_len, url); | |
393 | shown_url = 1; | |
165f3902 | 394 | } |
7f87aff2 TA |
395 | if (verbosity >= 0) |
396 | fprintf(stderr, " %s\n", note); | |
165f3902 | 397 | } |
b888d61c | 398 | } |
47abd85b | 399 | free(url); |
b888d61c | 400 | fclose(fp); |
fa250759 | 401 | if (rc & STORE_REF_ERROR_DF_CONFLICT) |
f3cb169b JK |
402 | error("some local refs could not be updated; try running\n" |
403 | " 'git remote prune %s' to remove any old, conflicting " | |
404 | "branches", remote_name); | |
efb98b44 | 405 | return rc; |
b888d61c DB |
406 | } |
407 | ||
4191c356 SP |
408 | /* |
409 | * We would want to bypass the object transfer altogether if | |
d9eb0205 | 410 | * everything we are going to fetch already exists and is connected |
4191c356 SP |
411 | * locally. |
412 | * | |
d9eb0205 | 413 | * The refs we are going to fetch are in ref_map. If running |
4191c356 | 414 | * |
d9eb0205 | 415 | * $ git rev-list --objects --stdin --not --all |
4191c356 | 416 | * |
d9eb0205 | 417 | * (feeding all the refs in ref_map on its standard input) |
4191c356 SP |
418 | * does not error out, that means everything reachable from the |
419 | * refs we are going to fetch exists and is connected to some of | |
420 | * our existing refs. | |
421 | */ | |
422 | static int quickfetch(struct ref *ref_map) | |
423 | { | |
424 | struct child_process revlist; | |
425 | struct ref *ref; | |
d9eb0205 JH |
426 | int err; |
427 | const char *argv[] = {"rev-list", | |
428 | "--quiet", "--objects", "--stdin", "--not", "--all", NULL}; | |
4191c356 SP |
429 | |
430 | /* | |
431 | * If we are deepening a shallow clone we already have these | |
432 | * objects reachable. Running rev-list here will return with | |
433 | * a good (0) exit status and we'll bypass the fetch that we | |
434 | * really need to perform. Claiming failure now will ensure | |
435 | * we perform the network exchange to deepen our history. | |
436 | */ | |
437 | if (depth) | |
438 | return -1; | |
439 | ||
d9eb0205 | 440 | if (!ref_map) |
4191c356 SP |
441 | return 0; |
442 | ||
4191c356 | 443 | memset(&revlist, 0, sizeof(revlist)); |
d9eb0205 | 444 | revlist.argv = argv; |
4191c356 | 445 | revlist.git_cmd = 1; |
4191c356 SP |
446 | revlist.no_stdout = 1; |
447 | revlist.no_stderr = 1; | |
d9eb0205 JH |
448 | revlist.in = -1; |
449 | ||
450 | err = start_command(&revlist); | |
451 | if (err) { | |
452 | error("could not run rev-list"); | |
453 | return err; | |
454 | } | |
455 | ||
456 | /* | |
457 | * If rev-list --stdin encounters an unknown commit, it terminates, | |
458 | * which will cause SIGPIPE in the write loop below. | |
459 | */ | |
460 | sigchain_push(SIGPIPE, SIG_IGN); | |
461 | ||
462 | for (ref = ref_map; ref; ref = ref->next) { | |
463 | if (write_in_full(revlist.in, sha1_to_hex(ref->old_sha1), 40) < 0 || | |
2b7ca830 | 464 | write_str_in_full(revlist.in, "\n") < 0) { |
d9eb0205 JH |
465 | if (errno != EPIPE && errno != EINVAL) |
466 | error("failed write to rev-list: %s", strerror(errno)); | |
467 | err = -1; | |
468 | break; | |
469 | } | |
470 | } | |
471 | ||
472 | if (close(revlist.in)) { | |
473 | error("failed to close rev-list's stdin: %s", strerror(errno)); | |
474 | err = -1; | |
475 | } | |
476 | ||
477 | sigchain_pop(SIGPIPE); | |
4191c356 | 478 | |
d9eb0205 | 479 | return finish_command(&revlist) || err; |
4191c356 SP |
480 | } |
481 | ||
b888d61c DB |
482 | static int fetch_refs(struct transport *transport, struct ref *ref_map) |
483 | { | |
4191c356 SP |
484 | int ret = quickfetch(ref_map); |
485 | if (ret) | |
486 | ret = transport_fetch_refs(transport, ref_map); | |
b888d61c | 487 | if (!ret) |
f3cb169b JK |
488 | ret |= store_updated_refs(transport->url, |
489 | transport->remote->name, | |
490 | ref_map); | |
1788c39c | 491 | transport_unlock_pack(transport); |
b888d61c DB |
492 | return ret; |
493 | } | |
494 | ||
495 | static int add_existing(const char *refname, const unsigned char *sha1, | |
496 | int flag, void *cbdata) | |
497 | { | |
c455c87c JS |
498 | struct string_list *list = (struct string_list *)cbdata; |
499 | string_list_insert(refname, list); | |
b888d61c DB |
500 | return 0; |
501 | } | |
502 | ||
cf7f929a SP |
503 | static int will_fetch(struct ref **head, const unsigned char *sha1) |
504 | { | |
505 | struct ref *rm = *head; | |
506 | while (rm) { | |
507 | if (!hashcmp(rm->old_sha1, sha1)) | |
508 | return 1; | |
509 | rm = rm->next; | |
510 | } | |
511 | return 0; | |
512 | } | |
513 | ||
c50b2b47 SP |
514 | static void find_non_local_tags(struct transport *transport, |
515 | struct ref **head, | |
516 | struct ref ***tail) | |
b888d61c | 517 | { |
c455c87c JS |
518 | struct string_list existing_refs = { NULL, 0, 0, 0 }; |
519 | struct string_list new_refs = { NULL, 0, 0, 1 }; | |
b888d61c DB |
520 | char *ref_name; |
521 | int ref_name_len; | |
4577370e DB |
522 | const unsigned char *ref_sha1; |
523 | const struct ref *tag_ref; | |
b888d61c | 524 | struct ref *rm = NULL; |
4577370e | 525 | const struct ref *ref; |
b888d61c DB |
526 | |
527 | for_each_ref(add_existing, &existing_refs); | |
528 | for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) { | |
529 | if (prefixcmp(ref->name, "refs/tags")) | |
530 | continue; | |
531 | ||
532 | ref_name = xstrdup(ref->name); | |
533 | ref_name_len = strlen(ref_name); | |
534 | ref_sha1 = ref->old_sha1; | |
535 | ||
536 | if (!strcmp(ref_name + ref_name_len - 3, "^{}")) { | |
537 | ref_name[ref_name_len - 3] = 0; | |
538 | tag_ref = transport_get_remote_refs(transport); | |
539 | while (tag_ref) { | |
540 | if (!strcmp(tag_ref->name, ref_name)) { | |
541 | ref_sha1 = tag_ref->old_sha1; | |
542 | break; | |
543 | } | |
544 | tag_ref = tag_ref->next; | |
545 | } | |
546 | } | |
547 | ||
c455c87c JS |
548 | if (!string_list_has_string(&existing_refs, ref_name) && |
549 | !string_list_has_string(&new_refs, ref_name) && | |
cf7f929a SP |
550 | (has_sha1_file(ref->old_sha1) || |
551 | will_fetch(head, ref->old_sha1))) { | |
c455c87c | 552 | string_list_insert(ref_name, &new_refs); |
b888d61c | 553 | |
59c69c0c RS |
554 | rm = alloc_ref(ref_name); |
555 | rm->peer_ref = alloc_ref(ref_name); | |
b888d61c DB |
556 | hashcpy(rm->old_sha1, ref_sha1); |
557 | ||
c50b2b47 SP |
558 | **tail = rm; |
559 | *tail = &rm->next; | |
b888d61c DB |
560 | } |
561 | free(ref_name); | |
562 | } | |
c455c87c JS |
563 | string_list_clear(&existing_refs, 0); |
564 | string_list_clear(&new_refs, 0); | |
b888d61c DB |
565 | } |
566 | ||
8ee5d731 JS |
567 | static void check_not_current_branch(struct ref *ref_map) |
568 | { | |
569 | struct branch *current_branch = branch_get(NULL); | |
570 | ||
571 | if (is_bare_repository() || !current_branch) | |
572 | return; | |
573 | ||
574 | for (; ref_map; ref_map = ref_map->next) | |
575 | if (ref_map->peer_ref && !strcmp(current_branch->refname, | |
576 | ref_map->peer_ref->name)) | |
26284f93 AR |
577 | die("Refusing to fetch into current branch %s " |
578 | "of non-bare repository", current_branch->refname); | |
8ee5d731 JS |
579 | } |
580 | ||
b888d61c DB |
581 | static int do_fetch(struct transport *transport, |
582 | struct refspec *refs, int ref_count) | |
583 | { | |
7f98428d | 584 | struct ref *ref_map; |
b888d61c DB |
585 | struct ref *rm; |
586 | int autotags = (transport->remote->fetch_tags == 1); | |
83201998 KH |
587 | if (transport->remote->fetch_tags == 2 && tags != TAGS_UNSET) |
588 | tags = TAGS_SET; | |
b888d61c | 589 | if (transport->remote->fetch_tags == -1) |
83201998 | 590 | tags = TAGS_UNSET; |
b888d61c | 591 | |
824d5776 | 592 | if (!transport->get_refs_list || !transport->fetch) |
b888d61c DB |
593 | die("Don't know how to fetch from %s", transport->url); |
594 | ||
595 | /* if not appending, truncate FETCH_HEAD */ | |
d6617c7c AGR |
596 | if (!append) { |
597 | char *filename = git_path("FETCH_HEAD"); | |
598 | FILE *fp = fopen(filename, "w"); | |
599 | if (!fp) | |
600 | return error("cannot open %s: %s\n", filename, strerror(errno)); | |
601 | fclose(fp); | |
602 | } | |
b888d61c DB |
603 | |
604 | ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags); | |
8ee5d731 JS |
605 | if (!update_head_ok) |
606 | check_not_current_branch(ref_map); | |
b888d61c DB |
607 | |
608 | for (rm = ref_map; rm; rm = rm->next) { | |
609 | if (rm->peer_ref) | |
610 | read_ref(rm->peer_ref->name, rm->peer_ref->old_sha1); | |
b888d61c DB |
611 | } |
612 | ||
41fa7d2e SP |
613 | if (tags == TAGS_DEFAULT && autotags) |
614 | transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1"); | |
b888d61c DB |
615 | if (fetch_refs(transport, ref_map)) { |
616 | free_refs(ref_map); | |
617 | return 1; | |
618 | } | |
7f98428d | 619 | free_refs(ref_map); |
b888d61c DB |
620 | |
621 | /* if neither --no-tags nor --tags was specified, do automated tag | |
622 | * following ... */ | |
83201998 | 623 | if (tags == TAGS_DEFAULT && autotags) { |
c50b2b47 SP |
624 | struct ref **tail = &ref_map; |
625 | ref_map = NULL; | |
626 | find_non_local_tags(transport, &ref_map, &tail); | |
b888d61c | 627 | if (ref_map) { |
41fa7d2e | 628 | transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL); |
b888d61c DB |
629 | transport_set_option(transport, TRANS_OPT_DEPTH, "0"); |
630 | fetch_refs(transport, ref_map); | |
631 | } | |
632 | free_refs(ref_map); | |
633 | } | |
634 | ||
b888d61c DB |
635 | return 0; |
636 | } | |
637 | ||
ab865e6e SP |
638 | static void set_option(const char *name, const char *value) |
639 | { | |
640 | int r = transport_set_option(transport, name, value); | |
641 | if (r < 0) | |
d7530708 | 642 | die("Option \"%s\" value \"%s\" is not valid for %s", |
ab865e6e SP |
643 | name, value, transport->url); |
644 | if (r > 0) | |
645 | warning("Option \"%s\" is ignored for %s\n", | |
646 | name, transport->url); | |
647 | } | |
648 | ||
9c4a036b BG |
649 | static int get_one_remote_for_fetch(struct remote *remote, void *priv) |
650 | { | |
651 | struct string_list *list = priv; | |
7cc91a2f BG |
652 | if (!remote->skip_default_update) |
653 | string_list_append(remote->name, list); | |
9c4a036b BG |
654 | return 0; |
655 | } | |
656 | ||
657 | struct remote_group_data { | |
658 | const char *name; | |
659 | struct string_list *list; | |
660 | }; | |
661 | ||
662 | static int get_remote_group(const char *key, const char *value, void *priv) | |
663 | { | |
664 | struct remote_group_data *g = priv; | |
665 | ||
666 | if (!prefixcmp(key, "remotes.") && | |
667 | !strcmp(key + 8, g->name)) { | |
668 | /* split list by white space */ | |
669 | int space = strcspn(value, " \t\n"); | |
670 | while (*value) { | |
671 | if (space > 1) { | |
672 | string_list_append(xstrndup(value, space), | |
673 | g->list); | |
674 | } | |
675 | value += space + (value[space] != '\0'); | |
676 | space = strcspn(value, " \t\n"); | |
677 | } | |
678 | } | |
679 | ||
680 | return 0; | |
681 | } | |
682 | ||
683 | static int add_remote_or_group(const char *name, struct string_list *list) | |
684 | { | |
685 | int prev_nr = list->nr; | |
686 | struct remote_group_data g = { name, list }; | |
687 | ||
688 | git_config(get_remote_group, &g); | |
689 | if (list->nr == prev_nr) { | |
690 | struct remote *remote; | |
691 | if (!remote_is_configured(name)) | |
692 | return 0; | |
693 | remote = remote_get(name); | |
694 | string_list_append(remote->name, list); | |
695 | } | |
696 | return 1; | |
697 | } | |
698 | ||
699 | static int fetch_multiple(struct string_list *list) | |
700 | { | |
701 | int i, result = 0; | |
702 | const char *argv[] = { "fetch", NULL, NULL, NULL, NULL }; | |
703 | int argc = 1; | |
704 | ||
705 | if (verbosity >= 2) | |
706 | argv[argc++] = "-v"; | |
707 | if (verbosity >= 1) | |
708 | argv[argc++] = "-v"; | |
709 | else if (verbosity < 0) | |
710 | argv[argc++] = "-q"; | |
711 | ||
712 | for (i = 0; i < list->nr; i++) { | |
713 | const char *name = list->items[i].string; | |
714 | argv[argc] = name; | |
715 | if (verbosity >= 0) | |
716 | printf("Fetching %s\n", name); | |
717 | if (run_command_v_opt(argv, RUN_GIT_CMD)) { | |
718 | error("Could not fetch %s", name); | |
719 | result = 1; | |
720 | } | |
721 | } | |
722 | ||
723 | return result; | |
724 | } | |
725 | ||
726 | static int fetch_one(struct remote *remote, int argc, const char **argv) | |
b888d61c | 727 | { |
2d324efa | 728 | int i; |
b888d61c DB |
729 | static const char **refs = NULL; |
730 | int ref_nr = 0; | |
7b7f39ea | 731 | int exit_code; |
b888d61c | 732 | |
fa685bdf DB |
733 | if (!remote) |
734 | die("Where do you want to fetch from today?"); | |
735 | ||
28b91f8a | 736 | transport = transport_get(remote, remote->url[0]); |
7f87aff2 | 737 | if (verbosity >= 2) |
b888d61c | 738 | transport->verbose = 1; |
7f87aff2 | 739 | if (verbosity < 0) |
2b5a06ed | 740 | transport->verbose = -1; |
b888d61c | 741 | if (upload_pack) |
ab865e6e | 742 | set_option(TRANS_OPT_UPLOADPACK, upload_pack); |
b888d61c | 743 | if (keep) |
ab865e6e SP |
744 | set_option(TRANS_OPT_KEEP, "yes"); |
745 | if (depth) | |
746 | set_option(TRANS_OPT_DEPTH, depth); | |
b888d61c | 747 | |
9c4a036b | 748 | if (argc > 0) { |
b888d61c | 749 | int j = 0; |
83201998 | 750 | refs = xcalloc(argc + 1, sizeof(const char *)); |
9c4a036b | 751 | for (i = 0; i < argc; i++) { |
b888d61c DB |
752 | if (!strcmp(argv[i], "tag")) { |
753 | char *ref; | |
754 | i++; | |
f53423b0 KB |
755 | if (i >= argc) |
756 | die("You need to specify a tag name."); | |
b888d61c DB |
757 | ref = xmalloc(strlen(argv[i]) * 2 + 22); |
758 | strcpy(ref, "refs/tags/"); | |
759 | strcat(ref, argv[i]); | |
760 | strcat(ref, ":refs/tags/"); | |
761 | strcat(ref, argv[i]); | |
762 | refs[j++] = ref; | |
763 | } else | |
764 | refs[j++] = argv[i]; | |
b888d61c DB |
765 | } |
766 | refs[j] = NULL; | |
767 | ref_nr = j; | |
b888d61c DB |
768 | } |
769 | ||
57b235a4 | 770 | sigchain_push_common(unlock_pack_on_signal); |
e4022ed2 | 771 | atexit(unlock_pack); |
7b7f39ea | 772 | exit_code = do_fetch(transport, |
46220ca1 | 773 | parse_fetch_refspec(ref_nr, refs), ref_nr); |
7b7f39ea AR |
774 | transport_disconnect(transport); |
775 | transport = NULL; | |
776 | return exit_code; | |
b888d61c | 777 | } |
9c4a036b BG |
778 | |
779 | int cmd_fetch(int argc, const char **argv, const char *prefix) | |
780 | { | |
781 | int i; | |
782 | struct string_list list = { NULL, 0, 0, 0 }; | |
783 | struct remote *remote; | |
784 | int result = 0; | |
785 | ||
786 | /* Record the command line for the reflog */ | |
787 | strbuf_addstr(&default_rla, "fetch"); | |
788 | for (i = 1; i < argc; i++) | |
789 | strbuf_addf(&default_rla, " %s", argv[i]); | |
790 | ||
791 | argc = parse_options(argc, argv, prefix, | |
792 | builtin_fetch_options, builtin_fetch_usage, 0); | |
793 | ||
794 | if (all) { | |
795 | if (argc == 1) | |
796 | die("fetch --all does not take a repository argument"); | |
797 | else if (argc > 1) | |
798 | die("fetch --all does not make sense with refspecs"); | |
799 | (void) for_each_remote(get_one_remote_for_fetch, &list); | |
800 | result = fetch_multiple(&list); | |
801 | } else if (argc == 0) { | |
802 | /* No arguments -- use default remote */ | |
803 | remote = remote_get(NULL); | |
804 | result = fetch_one(remote, argc, argv); | |
16679e37 BG |
805 | } else if (multiple) { |
806 | /* All arguments are assumed to be remotes or groups */ | |
807 | for (i = 0; i < argc; i++) | |
808 | if (!add_remote_or_group(argv[i], &list)) | |
809 | die("No such remote or remote group: %s", argv[i]); | |
810 | result = fetch_multiple(&list); | |
9c4a036b BG |
811 | } else { |
812 | /* Single remote or group */ | |
813 | (void) add_remote_or_group(argv[0], &list); | |
814 | if (list.nr > 1) { | |
815 | /* More than one remote */ | |
816 | if (argc > 1) | |
817 | die("Fetching a group and specifying refspecs does not make sense"); | |
818 | result = fetch_multiple(&list); | |
819 | } else { | |
820 | /* Zero or one remotes */ | |
821 | remote = remote_get(argv[0]); | |
822 | result = fetch_one(remote, argc-1, argv+1); | |
823 | } | |
824 | } | |
825 | ||
826 | /* All names were strdup()ed or strndup()ed */ | |
827 | list.strdup_strings = 1; | |
828 | string_list_clear(&list, 0); | |
829 | ||
830 | return result; | |
831 | } |