]> asedeno.scripts.mit.edu Git - git.git/blob - builtin-fetch.c
c1930aaa0e00c1c68658370d05c8c2902b123161
[git.git] / builtin-fetch.c
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 #include "run-command.h"
12
13 static 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>...]";
14
15 static int append, force, tags, no_tags, update_head_ok, verbose, quiet;
16 static const char *depth;
17 static char *default_rla = NULL;
18 static struct transport *transport;
19
20 static void unlock_pack(void)
21 {
22         if (transport)
23                 transport_unlock_pack(transport);
24 }
25
26 static void unlock_pack_on_signal(int signo)
27 {
28         unlock_pack();
29         signal(SIGINT, SIG_DFL);
30         raise(signo);
31 }
32
33 static void add_merge_config(struct ref **head,
34                            struct ref *remote_refs,
35                            struct branch *branch,
36                            struct ref ***tail)
37 {
38         int i;
39
40         for (i = 0; i < branch->merge_nr; i++) {
41                 struct ref *rm, **old_tail = *tail;
42                 struct refspec refspec;
43
44                 for (rm = *head; rm; rm = rm->next) {
45                         if (branch_merge_matches(branch, i, rm->name)) {
46                                 rm->merge = 1;
47                                 break;
48                         }
49                 }
50                 if (rm)
51                         continue;
52
53                 /*
54                  * Not fetched to a tracking branch?  We need to fetch
55                  * it anyway to allow this branch's "branch.$name.merge"
56                  * to be honored by git-pull, but we do not have to
57                  * fail if branch.$name.merge is misconfigured to point
58                  * at a nonexisting branch.  If we were indeed called by
59                  * git-pull, it will notice the misconfiguration because
60                  * there is no entry in the resulting FETCH_HEAD marked
61                  * for merging.
62                  */
63                 refspec.src = branch->merge[i]->src;
64                 refspec.dst = NULL;
65                 refspec.pattern = 0;
66                 refspec.force = 0;
67                 get_fetch_map(remote_refs, &refspec, tail, 1);
68                 for (rm = *old_tail; rm; rm = rm->next)
69                         rm->merge = 1;
70         }
71 }
72
73 static struct ref *get_ref_map(struct transport *transport,
74                                struct refspec *refs, int ref_count, int tags,
75                                int *autotags)
76 {
77         int i;
78         struct ref *rm;
79         struct ref *ref_map = NULL;
80         struct ref **tail = &ref_map;
81
82         struct ref *remote_refs = transport_get_remote_refs(transport);
83
84         if (ref_count || tags) {
85                 for (i = 0; i < ref_count; i++) {
86                         get_fetch_map(remote_refs, &refs[i], &tail, 0);
87                         if (refs[i].dst && refs[i].dst[0])
88                                 *autotags = 1;
89                 }
90                 /* Merge everything on the command line, but not --tags */
91                 for (rm = ref_map; rm; rm = rm->next)
92                         rm->merge = 1;
93                 if (tags) {
94                         struct refspec refspec;
95                         refspec.src = "refs/tags/";
96                         refspec.dst = "refs/tags/";
97                         refspec.pattern = 1;
98                         refspec.force = 0;
99                         get_fetch_map(remote_refs, &refspec, &tail, 0);
100                 }
101         } else {
102                 /* Use the defaults */
103                 struct remote *remote = transport->remote;
104                 struct branch *branch = branch_get(NULL);
105                 int has_merge = branch_has_merge_config(branch);
106                 if (remote && (remote->fetch_refspec_nr || has_merge)) {
107                         for (i = 0; i < remote->fetch_refspec_nr; i++) {
108                                 get_fetch_map(remote_refs, &remote->fetch[i], &tail, 0);
109                                 if (remote->fetch[i].dst &&
110                                     remote->fetch[i].dst[0])
111                                         *autotags = 1;
112                                 if (!i && !has_merge && ref_map &&
113                                     !remote->fetch[0].pattern)
114                                         ref_map->merge = 1;
115                         }
116                         /*
117                          * if the remote we're fetching from is the same
118                          * as given in branch.<name>.remote, we add the
119                          * ref given in branch.<name>.merge, too.
120                          */
121                         if (has_merge &&
122                             !strcmp(branch->remote_name, remote->name))
123                                 add_merge_config(&ref_map, remote_refs, branch, &tail);
124                 } else {
125                         ref_map = get_remote_ref(remote_refs, "HEAD");
126                         if (!ref_map)
127                                 die("Couldn't find remote ref HEAD");
128                         ref_map->merge = 1;
129                 }
130         }
131         ref_remove_duplicates(ref_map);
132
133         return ref_map;
134 }
135
136 static int s_update_ref(const char *action,
137                         struct ref *ref,
138                         int check_old)
139 {
140         char msg[1024];
141         char *rla = getenv("GIT_REFLOG_ACTION");
142         static struct ref_lock *lock;
143
144         if (!rla)
145                 rla = default_rla;
146         snprintf(msg, sizeof(msg), "%s: %s", rla, action);
147         lock = lock_any_ref_for_update(ref->name,
148                                        check_old ? ref->old_sha1 : NULL, 0);
149         if (!lock)
150                 return 1;
151         if (write_ref_sha1(lock, ref->new_sha1, msg) < 0)
152                 return 1;
153         return 0;
154 }
155
156 #define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
157
158 static int update_local_ref(struct ref *ref,
159                             const char *remote,
160                             int verbose,
161                             char *display)
162 {
163         struct commit *current = NULL, *updated;
164         enum object_type type;
165         struct branch *current_branch = branch_get(NULL);
166         const char *pretty_ref = ref->name + (
167                 !prefixcmp(ref->name, "refs/heads/") ? 11 :
168                 !prefixcmp(ref->name, "refs/tags/") ? 10 :
169                 !prefixcmp(ref->name, "refs/remotes/") ? 13 :
170                 0);
171
172         *display = 0;
173         type = sha1_object_info(ref->new_sha1, NULL);
174         if (type < 0)
175                 die("object %s not found", sha1_to_hex(ref->new_sha1));
176
177         if (!*ref->name) {
178                 /* Not storing */
179                 if (verbose)
180                         sprintf(display, "* branch %s -> FETCH_HEAD", remote);
181                 return 0;
182         }
183
184         if (!hashcmp(ref->old_sha1, ref->new_sha1)) {
185                 if (verbose)
186                         sprintf(display, "= %-*s %s -> %s", SUMMARY_WIDTH,
187                                 "[up to date]", remote, pretty_ref);
188                 return 0;
189         }
190
191         if (current_branch &&
192             !strcmp(ref->name, current_branch->name) &&
193             !(update_head_ok || is_bare_repository()) &&
194             !is_null_sha1(ref->old_sha1)) {
195                 /*
196                  * If this is the head, and it's not okay to update
197                  * the head, and the old value of the head isn't empty...
198                  */
199                 sprintf(display, "! %-*s %s -> %s  (can't  fetch in current branch)",
200                         SUMMARY_WIDTH, "[rejected]", remote, pretty_ref);
201                 return 1;
202         }
203
204         if (!is_null_sha1(ref->old_sha1) &&
205             !prefixcmp(ref->name, "refs/tags/")) {
206                 sprintf(display, "- %-*s %s -> %s",
207                         SUMMARY_WIDTH, "[tag update]", remote, pretty_ref);
208                 return s_update_ref("updating tag", ref, 0);
209         }
210
211         current = lookup_commit_reference_gently(ref->old_sha1, 1);
212         updated = lookup_commit_reference_gently(ref->new_sha1, 1);
213         if (!current || !updated) {
214                 const char *msg;
215                 const char *what;
216                 if (!strncmp(ref->name, "refs/tags/", 10)) {
217                         msg = "storing tag";
218                         what = "[new tag]";
219                 }
220                 else {
221                         msg = "storing head";
222                         what = "[new branch]";
223                 }
224
225                 sprintf(display, "* %-*s %s -> %s",
226                         SUMMARY_WIDTH, what, remote, pretty_ref);
227                 return s_update_ref(msg, ref, 0);
228         }
229
230         if (in_merge_bases(current, &updated, 1)) {
231                 char quickref[83];
232                 strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
233                 strcat(quickref, "..");
234                 strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
235                 sprintf(display, "  %-*s %s -> %s  (fast forward)",
236                         SUMMARY_WIDTH, quickref, remote, pretty_ref);
237                 return s_update_ref("fast forward", ref, 1);
238         } else if (force || ref->force) {
239                 char quickref[84];
240                 strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
241                 strcat(quickref, "...");
242                 strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
243                 sprintf(display, "+ %-*s %s -> %s  (forced update)",
244                         SUMMARY_WIDTH, quickref, remote, pretty_ref);
245                 return s_update_ref("forced-update", ref, 1);
246         } else {
247                 sprintf(display, "! %-*s %s -> %s  (non fast forward)",
248                         SUMMARY_WIDTH, "[rejected]", remote, pretty_ref);
249                 return 1;
250         }
251 }
252
253 static void store_updated_refs(const char *url, struct ref *ref_map)
254 {
255         FILE *fp;
256         struct commit *commit;
257         int url_len, i, note_len, shown_url = 0;
258         char note[1024];
259         const char *what, *kind;
260         struct ref *rm;
261
262         fp = fopen(git_path("FETCH_HEAD"), "a");
263         for (rm = ref_map; rm; rm = rm->next) {
264                 struct ref *ref = NULL;
265
266                 if (rm->peer_ref) {
267                         ref = xcalloc(1, sizeof(*ref) + strlen(rm->peer_ref->name) + 1);
268                         strcpy(ref->name, rm->peer_ref->name);
269                         hashcpy(ref->old_sha1, rm->peer_ref->old_sha1);
270                         hashcpy(ref->new_sha1, rm->old_sha1);
271                         ref->force = rm->peer_ref->force;
272                 }
273
274                 commit = lookup_commit_reference_gently(rm->old_sha1, 1);
275                 if (!commit)
276                         rm->merge = 0;
277
278                 if (!strcmp(rm->name, "HEAD")) {
279                         kind = "";
280                         what = "";
281                 }
282                 else if (!prefixcmp(rm->name, "refs/heads/")) {
283                         kind = "branch";
284                         what = rm->name + 11;
285                 }
286                 else if (!prefixcmp(rm->name, "refs/tags/")) {
287                         kind = "tag";
288                         what = rm->name + 10;
289                 }
290                 else if (!prefixcmp(rm->name, "refs/remotes/")) {
291                         kind = "remote branch";
292                         what = rm->name + 13;
293                 }
294                 else {
295                         kind = "";
296                         what = rm->name;
297                 }
298
299                 url_len = strlen(url);
300                 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
301                         ;
302                 url_len = i + 1;
303                 if (4 < i && !strncmp(".git", url + i - 3, 4))
304                         url_len = i - 3;
305
306                 note_len = 0;
307                 if (*what) {
308                         if (*kind)
309                                 note_len += sprintf(note + note_len, "%s ",
310                                                     kind);
311                         note_len += sprintf(note + note_len, "'%s' of ", what);
312                 }
313                 note_len += sprintf(note + note_len, "%.*s", url_len, url);
314                 fprintf(fp, "%s\t%s\t%s\n",
315                         sha1_to_hex(commit ? commit->object.sha1 :
316                                     rm->old_sha1),
317                         rm->merge ? "" : "not-for-merge",
318                         note);
319
320                 if (ref) {
321                         update_local_ref(ref, what, verbose, note);
322                         if (*note) {
323                                 if (!shown_url) {
324                                         fprintf(stderr, "From %.*s\n",
325                                                         url_len, url);
326                                         shown_url = 1;
327                                 }
328                                 fprintf(stderr, " %s\n", note);
329                         }
330                 }
331         }
332         fclose(fp);
333 }
334
335 /*
336  * We would want to bypass the object transfer altogether if
337  * everything we are going to fetch already exists and connected
338  * locally.
339  *
340  * The refs we are going to fetch are in to_fetch (nr_heads in
341  * total).  If running
342  *
343  *  $ git-rev-list --objects to_fetch[0] to_fetch[1] ... --not --all
344  *
345  * does not error out, that means everything reachable from the
346  * refs we are going to fetch exists and is connected to some of
347  * our existing refs.
348  */
349 static int quickfetch(struct ref *ref_map)
350 {
351         struct child_process revlist;
352         struct ref *ref;
353         char **argv;
354         int i, err;
355
356         /*
357          * If we are deepening a shallow clone we already have these
358          * objects reachable.  Running rev-list here will return with
359          * a good (0) exit status and we'll bypass the fetch that we
360          * really need to perform.  Claiming failure now will ensure
361          * we perform the network exchange to deepen our history.
362          */
363         if (depth)
364                 return -1;
365
366         for (i = 0, ref = ref_map; ref; ref = ref->next)
367                 i++;
368         if (!i)
369                 return 0;
370
371         argv = xmalloc(sizeof(*argv) * (i + 6));
372         i = 0;
373         argv[i++] = xstrdup("rev-list");
374         argv[i++] = xstrdup("--quiet");
375         argv[i++] = xstrdup("--objects");
376         for (ref = ref_map; ref; ref = ref->next)
377                 argv[i++] = xstrdup(sha1_to_hex(ref->old_sha1));
378         argv[i++] = xstrdup("--not");
379         argv[i++] = xstrdup("--all");
380         argv[i++] = NULL;
381
382         memset(&revlist, 0, sizeof(revlist));
383         revlist.argv = (const char**)argv;
384         revlist.git_cmd = 1;
385         revlist.no_stdin = 1;
386         revlist.no_stdout = 1;
387         revlist.no_stderr = 1;
388         err = run_command(&revlist);
389
390         for (i = 0; argv[i]; i++)
391                 free(argv[i]);
392         free(argv);
393         return err;
394 }
395
396 static int fetch_refs(struct transport *transport, struct ref *ref_map)
397 {
398         int ret = quickfetch(ref_map);
399         if (ret)
400                 ret = transport_fetch_refs(transport, ref_map);
401         if (!ret)
402                 store_updated_refs(transport->url, ref_map);
403         transport_unlock_pack(transport);
404         return ret;
405 }
406
407 static int add_existing(const char *refname, const unsigned char *sha1,
408                         int flag, void *cbdata)
409 {
410         struct path_list *list = (struct path_list *)cbdata;
411         path_list_insert(refname, list);
412         return 0;
413 }
414
415 static struct ref *find_non_local_tags(struct transport *transport,
416                                        struct ref *fetch_map)
417 {
418         static struct path_list existing_refs = { NULL, 0, 0, 0 };
419         struct path_list new_refs = { NULL, 0, 0, 1 };
420         char *ref_name;
421         int ref_name_len;
422         unsigned char *ref_sha1;
423         struct ref *tag_ref;
424         struct ref *rm = NULL;
425         struct ref *ref_map = NULL;
426         struct ref **tail = &ref_map;
427         struct ref *ref;
428
429         for_each_ref(add_existing, &existing_refs);
430         for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
431                 if (prefixcmp(ref->name, "refs/tags"))
432                         continue;
433
434                 ref_name = xstrdup(ref->name);
435                 ref_name_len = strlen(ref_name);
436                 ref_sha1 = ref->old_sha1;
437
438                 if (!strcmp(ref_name + ref_name_len - 3, "^{}")) {
439                         ref_name[ref_name_len - 3] = 0;
440                         tag_ref = transport_get_remote_refs(transport);
441                         while (tag_ref) {
442                                 if (!strcmp(tag_ref->name, ref_name)) {
443                                         ref_sha1 = tag_ref->old_sha1;
444                                         break;
445                                 }
446                                 tag_ref = tag_ref->next;
447                         }
448                 }
449
450                 if (!path_list_has_path(&existing_refs, ref_name) &&
451                     !path_list_has_path(&new_refs, ref_name) &&
452                     has_sha1_file(ref->old_sha1)) {
453                         path_list_insert(ref_name, &new_refs);
454
455                         rm = alloc_ref(strlen(ref_name) + 1);
456                         strcpy(rm->name, ref_name);
457                         rm->peer_ref = alloc_ref(strlen(ref_name) + 1);
458                         strcpy(rm->peer_ref->name, ref_name);
459                         hashcpy(rm->old_sha1, ref_sha1);
460
461                         *tail = rm;
462                         tail = &rm->next;
463                 }
464                 free(ref_name);
465         }
466
467         return ref_map;
468 }
469
470 static int do_fetch(struct transport *transport,
471                     struct refspec *refs, int ref_count)
472 {
473         struct ref *ref_map, *fetch_map;
474         struct ref *rm;
475         int autotags = (transport->remote->fetch_tags == 1);
476         if (transport->remote->fetch_tags == 2 && !no_tags)
477                 tags = 1;
478         if (transport->remote->fetch_tags == -1)
479                 no_tags = 1;
480
481         if (!transport->get_refs_list || !transport->fetch)
482                 die("Don't know how to fetch from %s", transport->url);
483
484         /* if not appending, truncate FETCH_HEAD */
485         if (!append)
486                 fclose(fopen(git_path("FETCH_HEAD"), "w"));
487
488         ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
489
490         for (rm = ref_map; rm; rm = rm->next) {
491                 if (rm->peer_ref)
492                         read_ref(rm->peer_ref->name, rm->peer_ref->old_sha1);
493         }
494
495         if (fetch_refs(transport, ref_map)) {
496                 free_refs(ref_map);
497                 return 1;
498         }
499
500         fetch_map = ref_map;
501
502         /* if neither --no-tags nor --tags was specified, do automated tag
503          * following ... */
504         if (!(tags || no_tags) && autotags) {
505                 ref_map = find_non_local_tags(transport, fetch_map);
506                 if (ref_map) {
507                         transport_set_option(transport, TRANS_OPT_DEPTH, "0");
508                         fetch_refs(transport, ref_map);
509                 }
510                 free_refs(ref_map);
511         }
512
513         free_refs(fetch_map);
514
515         return 0;
516 }
517
518 static void set_option(const char *name, const char *value)
519 {
520         int r = transport_set_option(transport, name, value);
521         if (r < 0)
522                 die("Option \"%s\" value \"%s\" is not valid for %s\n",
523                         name, value, transport->url);
524         if (r > 0)
525                 warning("Option \"%s\" is ignored for %s\n",
526                         name, transport->url);
527 }
528
529 int cmd_fetch(int argc, const char **argv, const char *prefix)
530 {
531         struct remote *remote;
532         int i, j, rla_offset;
533         static const char **refs = NULL;
534         int ref_nr = 0;
535         int cmd_len = 0;
536         const char *upload_pack = NULL;
537         int keep = 0;
538
539         for (i = 1; i < argc; i++) {
540                 const char *arg = argv[i];
541                 cmd_len += strlen(arg);
542
543                 if (arg[0] != '-')
544                         break;
545                 if (!strcmp(arg, "--append") || !strcmp(arg, "-a")) {
546                         append = 1;
547                         continue;
548                 }
549                 if (!prefixcmp(arg, "--upload-pack=")) {
550                         upload_pack = arg + 14;
551                         continue;
552                 }
553                 if (!strcmp(arg, "--upload-pack")) {
554                         i++;
555                         if (i == argc)
556                                 usage(fetch_usage);
557                         upload_pack = argv[i];
558                         continue;
559                 }
560                 if (!strcmp(arg, "--force") || !strcmp(arg, "-f")) {
561                         force = 1;
562                         continue;
563                 }
564                 if (!strcmp(arg, "--no-tags")) {
565                         no_tags = 1;
566                         continue;
567                 }
568                 if (!strcmp(arg, "--tags") || !strcmp(arg, "-t")) {
569                         tags = 1;
570                         continue;
571                 }
572                 if (!strcmp(arg, "--keep") || !strcmp(arg, "-k")) {
573                         keep = 1;
574                         continue;
575                 }
576                 if (!strcmp(arg, "--update-head-ok") || !strcmp(arg, "-u")) {
577                         update_head_ok = 1;
578                         continue;
579                 }
580                 if (!prefixcmp(arg, "--depth=")) {
581                         depth = arg + 8;
582                         continue;
583                 }
584                 if (!strcmp(arg, "--depth")) {
585                         i++;
586                         if (i == argc)
587                                 usage(fetch_usage);
588                         depth = argv[i];
589                         continue;
590                 }
591                 if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) {
592                         quiet = 1;
593                         continue;
594                 }
595                 if (!strcmp(arg, "--verbose") || !strcmp(arg, "-v")) {
596                         verbose++;
597                         continue;
598                 }
599                 usage(fetch_usage);
600         }
601
602         for (j = i; j < argc; j++)
603                 cmd_len += strlen(argv[j]);
604
605         default_rla = xmalloc(cmd_len + 5 + argc + 1);
606         sprintf(default_rla, "fetch");
607         rla_offset = strlen(default_rla);
608         for (j = 1; j < argc; j++) {
609                 sprintf(default_rla + rla_offset, " %s", argv[j]);
610                 rla_offset += strlen(argv[j]) + 1;
611         }
612
613         if (i == argc)
614                 remote = remote_get(NULL);
615         else
616                 remote = remote_get(argv[i++]);
617
618         transport = transport_get(remote, remote->url[0]);
619         if (verbose >= 2)
620                 transport->verbose = 1;
621         if (quiet)
622                 transport->verbose = -1;
623         if (upload_pack)
624                 set_option(TRANS_OPT_UPLOADPACK, upload_pack);
625         if (keep)
626                 set_option(TRANS_OPT_KEEP, "yes");
627         if (depth)
628                 set_option(TRANS_OPT_DEPTH, depth);
629
630         if (!transport->url)
631                 die("Where do you want to fetch from today?");
632
633         if (i < argc) {
634                 int j = 0;
635                 refs = xcalloc(argc - i + 1, sizeof(const char *));
636                 while (i < argc) {
637                         if (!strcmp(argv[i], "tag")) {
638                                 char *ref;
639                                 i++;
640                                 ref = xmalloc(strlen(argv[i]) * 2 + 22);
641                                 strcpy(ref, "refs/tags/");
642                                 strcat(ref, argv[i]);
643                                 strcat(ref, ":refs/tags/");
644                                 strcat(ref, argv[i]);
645                                 refs[j++] = ref;
646                         } else
647                                 refs[j++] = argv[i];
648                         i++;
649                 }
650                 refs[j] = NULL;
651                 ref_nr = j;
652         }
653
654         signal(SIGINT, unlock_pack_on_signal);
655         atexit(unlock_pack);
656         return do_fetch(transport, parse_ref_spec(ref_nr, refs), ref_nr);
657 }