]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-fetch.c
Remove unnecessary 'fetch' argument from transport_get API
[thirdparty/git.git] / builtin-fetch.c
CommitLineData
b888d61c
DB
1/*
2 * "git fetch"
3 */
4#include "cache.h"
5#include "refs.h"
6#include "commit.h"
7#include "builtin.h"
8#include "path-list.h"
9#include "remote.h"
10#include "transport.h"
11
12static const char fetch_usage[] = "git-fetch [-a | --append] [--upload-pack <upload-pack>] [-f | --force] [--no-tags] [-t | --tags] [-k | --keep] [-u | --update-head-ok] [--depth <depth>] [-v | --verbose] [<repository> <refspec>...]";
13
14static int append, force, tags, no_tags, update_head_ok, verbose, quiet;
b888d61c 15static char *default_rla = NULL;
e4022ed2
SP
16static struct transport *transport;
17
18static void unlock_pack(void)
19{
20 if (transport)
21 transport_unlock_pack(transport);
22}
23
24static void unlock_pack_on_signal(int signo)
25{
26 unlock_pack();
27 signal(SIGINT, SIG_DFL);
28 raise(signo);
29}
b888d61c
DB
30
31static void find_merge_config(struct ref *ref_map, struct remote *remote)
32{
33 struct ref *rm = ref_map;
34 struct branch *branch = branch_get(NULL);
35
36 for (rm = ref_map; rm; rm = rm->next) {
37 if (!branch_has_merge_config(branch)) {
38 if (remote && remote->fetch &&
39 !strcmp(remote->fetch[0].src, rm->name))
40 rm->merge = 1;
41 } else {
42 if (branch_merges(branch, rm->name))
43 rm->merge = 1;
44 }
45 }
46}
47
48static struct ref *get_ref_map(struct transport *transport,
49 struct refspec *refs, int ref_count, int tags,
50 int *autotags)
51{
52 int i;
53 struct ref *rm;
54 struct ref *ref_map = NULL;
55 struct ref **tail = &ref_map;
56
57 struct ref *remote_refs = transport_get_remote_refs(transport);
58
59 if (ref_count || tags) {
60 for (i = 0; i < ref_count; i++) {
61 get_fetch_map(remote_refs, &refs[i], &tail);
62 if (refs[i].dst && refs[i].dst[0])
63 *autotags = 1;
64 }
65 /* Merge everything on the command line, but not --tags */
66 for (rm = ref_map; rm; rm = rm->next)
67 rm->merge = 1;
68 if (tags) {
69 struct refspec refspec;
70 refspec.src = "refs/tags/";
71 refspec.dst = "refs/tags/";
72 refspec.pattern = 1;
73 refspec.force = 0;
74 get_fetch_map(remote_refs, &refspec, &tail);
75 }
76 } else {
77 /* Use the defaults */
78 struct remote *remote = transport->remote;
79 if (remote->fetch_refspec_nr) {
80 for (i = 0; i < remote->fetch_refspec_nr; i++) {
81 get_fetch_map(remote_refs, &remote->fetch[i], &tail);
82 if (remote->fetch[i].dst &&
83 remote->fetch[i].dst[0])
84 *autotags = 1;
85 }
86 find_merge_config(ref_map, remote);
87 } else {
88 ref_map = get_remote_ref(remote_refs, "HEAD");
89
90 ref_map->merge = 1;
91 }
92 }
93
94 return ref_map;
95}
96
97static void show_new(enum object_type type, unsigned char *sha1_new)
98{
99 fprintf(stderr, " %s: %s\n", typename(type),
100 find_unique_abbrev(sha1_new, DEFAULT_ABBREV));
101}
102
103static int s_update_ref(const char *action,
104 struct ref *ref,
105 int check_old)
106{
107 char msg[1024];
108 char *rla = getenv("GIT_REFLOG_ACTION");
109 static struct ref_lock *lock;
110
111 if (!rla)
112 rla = default_rla;
113 snprintf(msg, sizeof(msg), "%s: %s", rla, action);
114 lock = lock_any_ref_for_update(ref->name,
115 check_old ? ref->old_sha1 : NULL, 0);
116 if (!lock)
117 return 1;
118 if (write_ref_sha1(lock, ref->new_sha1, msg) < 0)
119 return 1;
120 return 0;
121}
122
123static int update_local_ref(struct ref *ref,
124 const char *note,
125 int verbose)
126{
127 char oldh[41], newh[41];
128 struct commit *current = NULL, *updated;
129 enum object_type type;
130 struct branch *current_branch = branch_get(NULL);
131
132 type = sha1_object_info(ref->new_sha1, NULL);
133 if (type < 0)
134 die("object %s not found", sha1_to_hex(ref->new_sha1));
135
136 if (!*ref->name) {
137 /* Not storing */
138 if (verbose) {
139 fprintf(stderr, "* fetched %s\n", note);
140 show_new(type, ref->new_sha1);
141 }
142 return 0;
143 }
144
145 if (!hashcmp(ref->old_sha1, ref->new_sha1)) {
146 if (verbose) {
147 fprintf(stderr, "* %s: same as %s\n",
148 ref->name, note);
149 show_new(type, ref->new_sha1);
150 }
151 return 0;
152 }
153
154 if (!strcmp(ref->name, current_branch->name) &&
155 !(update_head_ok || is_bare_repository()) &&
156 !is_null_sha1(ref->old_sha1)) {
157 /*
158 * If this is the head, and it's not okay to update
159 * the head, and the old value of the head isn't empty...
160 */
161 fprintf(stderr,
162 " * %s: Cannot fetch into the current branch.\n",
163 ref->name);
164 return 1;
165 }
166
167 if (!is_null_sha1(ref->old_sha1) &&
168 !prefixcmp(ref->name, "refs/tags/")) {
169 fprintf(stderr, "* %s: updating with %s\n",
170 ref->name, note);
171 show_new(type, ref->new_sha1);
172 return s_update_ref("updating tag", ref, 0);
173 }
174
175 current = lookup_commit_reference(ref->old_sha1);
176 updated = lookup_commit_reference(ref->new_sha1);
177 if (!current || !updated) {
178 char *msg;
179 if (!strncmp(ref->name, "refs/tags/", 10))
180 msg = "storing tag";
181 else
182 msg = "storing head";
183 fprintf(stderr, "* %s: storing %s\n",
184 ref->name, note);
185 show_new(type, ref->new_sha1);
186 return s_update_ref(msg, ref, 0);
187 }
188
189 strcpy(oldh, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
190 strcpy(newh, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
191
192 if (in_merge_bases(current, &updated, 1)) {
193 fprintf(stderr, "* %s: fast forward to %s\n",
194 ref->name, note);
195 fprintf(stderr, " old..new: %s..%s\n", oldh, newh);
196 return s_update_ref("fast forward", ref, 1);
197 }
198 if (!force && !ref->force) {
199 fprintf(stderr,
200 "* %s: not updating to non-fast forward %s\n",
201 ref->name, note);
202 fprintf(stderr,
203 " old...new: %s...%s\n", oldh, newh);
204 return 1;
205 }
206 fprintf(stderr,
207 "* %s: forcing update to non-fast forward %s\n",
208 ref->name, note);
209 fprintf(stderr, " old...new: %s...%s\n", oldh, newh);
210 return s_update_ref("forced-update", ref, 1);
211}
212
213static void store_updated_refs(const char *url, struct ref *ref_map)
214{
215 FILE *fp;
216 struct commit *commit;
217 int url_len, i, note_len;
218 char note[1024];
219 const char *what, *kind;
220 struct ref *rm;
221
222 fp = fopen(git_path("FETCH_HEAD"), "a");
223 for (rm = ref_map; rm; rm = rm->next) {
224 struct ref *ref = NULL;
225
226 if (rm->peer_ref) {
227 ref = xcalloc(1, sizeof(*ref) + strlen(rm->peer_ref->name) + 1);
228 strcpy(ref->name, rm->peer_ref->name);
229 hashcpy(ref->old_sha1, rm->peer_ref->old_sha1);
230 hashcpy(ref->new_sha1, rm->old_sha1);
1aad91f5 231 ref->force = rm->peer_ref->force;
b888d61c
DB
232 }
233
234 commit = lookup_commit_reference(rm->old_sha1);
235 if (!commit)
236 rm->merge = 0;
237
238 if (!strcmp(rm->name, "HEAD")) {
239 kind = "";
240 what = "";
241 }
242 else if (!prefixcmp(rm->name, "refs/heads/")) {
243 kind = "branch";
244 what = rm->name + 11;
245 }
246 else if (!prefixcmp(rm->name, "refs/tags/")) {
247 kind = "tag";
248 what = rm->name + 10;
249 }
250 else if (!prefixcmp(rm->name, "refs/remotes/")) {
251 kind = "remote branch";
252 what = rm->name + 13;
253 }
254 else {
255 kind = "";
256 what = rm->name;
257 }
258
259 url_len = strlen(url);
260 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
261 ;
262 url_len = i + 1;
263 if (4 < i && !strncmp(".git", url + i - 3, 4))
264 url_len = i - 3;
265
266 note_len = 0;
267 if (*what) {
268 if (*kind)
269 note_len += sprintf(note + note_len, "%s ",
270 kind);
271 note_len += sprintf(note + note_len, "'%s' of ", what);
272 }
273 note_len += sprintf(note + note_len, "%.*s", url_len, url);
274 fprintf(fp, "%s\t%s\t%s\n",
275 sha1_to_hex(commit ? commit->object.sha1 :
276 rm->old_sha1),
277 rm->merge ? "" : "not-for-merge",
278 note);
279
280 if (ref)
281 update_local_ref(ref, note, verbose);
282 }
283 fclose(fp);
284}
285
286static int fetch_refs(struct transport *transport, struct ref *ref_map)
287{
288 int ret = transport_fetch_refs(transport, ref_map);
289 if (!ret)
290 store_updated_refs(transport->url, ref_map);
1788c39c 291 transport_unlock_pack(transport);
b888d61c
DB
292 return ret;
293}
294
295static int add_existing(const char *refname, const unsigned char *sha1,
296 int flag, void *cbdata)
297{
298 struct path_list *list = (struct path_list *)cbdata;
299 path_list_insert(refname, list);
300 return 0;
301}
302
303static struct ref *find_non_local_tags(struct transport *transport,
304 struct ref *fetch_map)
305{
306 static struct path_list existing_refs = { NULL, 0, 0, 0 };
307 struct path_list new_refs = { NULL, 0, 0, 1 };
308 char *ref_name;
309 int ref_name_len;
310 unsigned char *ref_sha1;
311 struct ref *tag_ref;
312 struct ref *rm = NULL;
313 struct ref *ref_map = NULL;
314 struct ref **tail = &ref_map;
315 struct ref *ref;
316
317 for_each_ref(add_existing, &existing_refs);
318 for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
319 if (prefixcmp(ref->name, "refs/tags"))
320 continue;
321
322 ref_name = xstrdup(ref->name);
323 ref_name_len = strlen(ref_name);
324 ref_sha1 = ref->old_sha1;
325
326 if (!strcmp(ref_name + ref_name_len - 3, "^{}")) {
327 ref_name[ref_name_len - 3] = 0;
328 tag_ref = transport_get_remote_refs(transport);
329 while (tag_ref) {
330 if (!strcmp(tag_ref->name, ref_name)) {
331 ref_sha1 = tag_ref->old_sha1;
332 break;
333 }
334 tag_ref = tag_ref->next;
335 }
336 }
337
338 if (!path_list_has_path(&existing_refs, ref_name) &&
339 !path_list_has_path(&new_refs, ref_name) &&
340 lookup_object(ref->old_sha1)) {
341 fprintf(stderr, "Auto-following %s\n",
342 ref_name);
343
344 path_list_insert(ref_name, &new_refs);
345
346 rm = alloc_ref(strlen(ref_name) + 1);
347 strcpy(rm->name, ref_name);
348 rm->peer_ref = alloc_ref(strlen(ref_name) + 1);
349 strcpy(rm->peer_ref->name, ref_name);
350 hashcpy(rm->old_sha1, ref_sha1);
351
352 *tail = rm;
353 tail = &rm->next;
354 }
355 free(ref_name);
356 }
357
358 return ref_map;
359}
360
361static int do_fetch(struct transport *transport,
362 struct refspec *refs, int ref_count)
363{
364 struct ref *ref_map, *fetch_map;
365 struct ref *rm;
366 int autotags = (transport->remote->fetch_tags == 1);
367 if (transport->remote->fetch_tags == 2 && !no_tags)
368 tags = 1;
369 if (transport->remote->fetch_tags == -1)
370 no_tags = 1;
371
372 if (!transport->ops || !transport->ops->get_refs_list ||
425b1393 373 !transport->ops->fetch)
b888d61c
DB
374 die("Don't know how to fetch from %s", transport->url);
375
376 /* if not appending, truncate FETCH_HEAD */
377 if (!append)
378 fclose(fopen(git_path("FETCH_HEAD"), "w"));
379
380 ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
381
382 for (rm = ref_map; rm; rm = rm->next) {
383 if (rm->peer_ref)
384 read_ref(rm->peer_ref->name, rm->peer_ref->old_sha1);
b888d61c
DB
385 }
386
387 if (fetch_refs(transport, ref_map)) {
388 free_refs(ref_map);
389 return 1;
390 }
391
392 fetch_map = ref_map;
393
394 /* if neither --no-tags nor --tags was specified, do automated tag
395 * following ... */
396 if (!(tags || no_tags) && autotags) {
397 ref_map = find_non_local_tags(transport, fetch_map);
398 if (ref_map) {
399 transport_set_option(transport, TRANS_OPT_DEPTH, "0");
400 fetch_refs(transport, ref_map);
401 }
402 free_refs(ref_map);
403 }
404
405 free_refs(fetch_map);
406
407 return 0;
408}
409
b888d61c
DB
410int cmd_fetch(int argc, const char **argv, const char *prefix)
411{
412 struct remote *remote;
b888d61c
DB
413 int i, j, rla_offset;
414 static const char **refs = NULL;
415 int ref_nr = 0;
416 int cmd_len = 0;
417 const char *depth = NULL, *upload_pack = NULL;
418 int keep = 0;
419
b888d61c
DB
420 for (i = 1; i < argc; i++) {
421 const char *arg = argv[i];
422 cmd_len += strlen(arg);
423
424 if (arg[0] != '-')
425 break;
426 if (!strcmp(arg, "--append") || !strcmp(arg, "-a")) {
427 append = 1;
428 continue;
429 }
430 if (!prefixcmp(arg, "--upload-pack=")) {
431 upload_pack = arg + 14;
432 continue;
433 }
434 if (!strcmp(arg, "--upload-pack")) {
435 i++;
436 if (i == argc)
437 usage(fetch_usage);
438 upload_pack = argv[i];
439 continue;
440 }
441 if (!strcmp(arg, "--force") || !strcmp(arg, "-f")) {
442 force = 1;
443 continue;
444 }
445 if (!strcmp(arg, "--no-tags")) {
446 no_tags = 1;
447 continue;
448 }
449 if (!strcmp(arg, "--tags") || !strcmp(arg, "-t")) {
450 tags = 1;
451 continue;
452 }
453 if (!strcmp(arg, "--keep") || !strcmp(arg, "-k")) {
454 keep = 1;
455 continue;
456 }
457 if (!strcmp(arg, "--update-head-ok") || !strcmp(arg, "-u")) {
458 update_head_ok = 1;
459 continue;
460 }
461 if (!prefixcmp(arg, "--depth=")) {
462 depth = arg + 8;
463 continue;
464 }
465 if (!strcmp(arg, "--depth")) {
466 i++;
467 if (i == argc)
468 usage(fetch_usage);
469 depth = argv[i];
470 continue;
471 }
472 if (!strcmp(arg, "--quiet")) {
473 quiet = 1;
474 continue;
475 }
476 if (!strcmp(arg, "--verbose") || !strcmp(arg, "-v")) {
477 verbose++;
478 continue;
479 }
480 usage(fetch_usage);
481 }
482
483 for (j = i; j < argc; j++)
484 cmd_len += strlen(argv[j]);
485
486 default_rla = xmalloc(cmd_len + 5 + argc + 1);
487 sprintf(default_rla, "fetch");
488 rla_offset = strlen(default_rla);
489 for (j = 1; j < argc; j++) {
490 sprintf(default_rla + rla_offset, " %s", argv[j]);
4ad1eada 491 rla_offset += strlen(argv[j]) + 1;
b888d61c
DB
492 }
493
494 if (i == argc)
495 remote = remote_get(NULL);
496 else
497 remote = remote_get(argv[i++]);
498
e5f4e214 499 transport = transport_get(remote, remote->uri[0]);
b888d61c
DB
500 if (verbose >= 2)
501 transport->verbose = 1;
502 if (quiet)
503 transport->verbose = 0;
504 if (upload_pack)
505 transport_set_option(transport, TRANS_OPT_UPLOADPACK, upload_pack);
506 if (keep)
507 transport_set_option(transport, TRANS_OPT_KEEP, "yes");
508 transport_set_option(transport, TRANS_OPT_DEPTH, depth);
509
510 if (!transport->url)
511 die("Where do you want to fetch from today?");
512
513 if (i < argc) {
514 int j = 0;
515 refs = xcalloc(argc - i + 1, sizeof(const char *));
516 while (i < argc) {
517 if (!strcmp(argv[i], "tag")) {
518 char *ref;
519 i++;
520 ref = xmalloc(strlen(argv[i]) * 2 + 22);
521 strcpy(ref, "refs/tags/");
522 strcat(ref, argv[i]);
523 strcat(ref, ":refs/tags/");
524 strcat(ref, argv[i]);
525 refs[j++] = ref;
526 } else
527 refs[j++] = argv[i];
528 i++;
529 }
530 refs[j] = NULL;
531 ref_nr = j;
532 for (j = 0; refs[j]; j++)
533 printf("ref: %s\n", refs[j]);
534 }
535
e4022ed2
SP
536 signal(SIGINT, unlock_pack_on_signal);
537 atexit(unlock_pack);
b888d61c
DB
538 return do_fetch(transport, parse_ref_spec(ref_nr, refs), ref_nr);
539}