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