Code

Allow builtin-fetch to work on a detached HEAD
[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"
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>...]";
14 static int append, force, tags, no_tags, update_head_ok, verbose, quiet;
15 static char *default_rla = NULL;
16 static struct transport *transport;
18 static void unlock_pack(void)
19 {
20         if (transport)
21                 transport_unlock_pack(transport);
22 }
24 static void unlock_pack_on_signal(int signo)
25 {
26         unlock_pack();
27         signal(SIGINT, SIG_DFL);
28         raise(signo);
29 }
31 static 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);
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 }
48 static 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;
57         struct ref *remote_refs = transport_get_remote_refs(transport);
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");
90                         ref_map->merge = 1;
91                 }
92         }
94         return ref_map;
95 }
97 static 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));
103 static int s_update_ref(const char *action,
104                         struct ref *ref,
105                         int check_old)
107         char msg[1024];
108         char *rla = getenv("GIT_REFLOG_ACTION");
109         static struct ref_lock *lock;
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;
123 static int update_local_ref(struct ref *ref,
124                             const char *note,
125                             int verbose)
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);
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));
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         }
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         }
154         if (current_branch &&
155             !strcmp(ref->name, current_branch->name) &&
156             !(update_head_ok || is_bare_repository()) &&
157             !is_null_sha1(ref->old_sha1)) {
158                 /*
159                  * If this is the head, and it's not okay to update
160                  * the head, and the old value of the head isn't empty...
161                  */
162                 fprintf(stderr,
163                         " * %s: Cannot fetch into the current branch.\n",
164                         ref->name);
165                 return 1;
166         }
168         if (!is_null_sha1(ref->old_sha1) &&
169             !prefixcmp(ref->name, "refs/tags/")) {
170                 fprintf(stderr, "* %s: updating with %s\n",
171                         ref->name, note);
172                 show_new(type, ref->new_sha1);
173                 return s_update_ref("updating tag", ref, 0);
174         }
176         current = lookup_commit_reference(ref->old_sha1);
177         updated = lookup_commit_reference(ref->new_sha1);
178         if (!current || !updated) {
179                 char *msg;
180                 if (!strncmp(ref->name, "refs/tags/", 10))
181                         msg = "storing tag";
182                 else
183                         msg = "storing head";
184                 fprintf(stderr, "* %s: storing %s\n",
185                         ref->name, note);
186                 show_new(type, ref->new_sha1);
187                 return s_update_ref(msg, ref, 0);
188         }
190         strcpy(oldh, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
191         strcpy(newh, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
193         if (in_merge_bases(current, &updated, 1)) {
194                 fprintf(stderr, "* %s: fast forward to %s\n",
195                         ref->name, note);
196                 fprintf(stderr, "  old..new: %s..%s\n", oldh, newh);
197                 return s_update_ref("fast forward", ref, 1);
198         }
199         if (!force && !ref->force) {
200                 fprintf(stderr,
201                         "* %s: not updating to non-fast forward %s\n",
202                         ref->name, note);
203                 fprintf(stderr,
204                         "  old...new: %s...%s\n", oldh, newh);
205                 return 1;
206         }
207         fprintf(stderr,
208                 "* %s: forcing update to non-fast forward %s\n",
209                 ref->name, note);
210         fprintf(stderr, "  old...new: %s...%s\n", oldh, newh);
211         return s_update_ref("forced-update", ref, 1);
214 static void store_updated_refs(const char *url, struct ref *ref_map)
216         FILE *fp;
217         struct commit *commit;
218         int url_len, i, note_len;
219         char note[1024];
220         const char *what, *kind;
221         struct ref *rm;
223         fp = fopen(git_path("FETCH_HEAD"), "a");
224         for (rm = ref_map; rm; rm = rm->next) {
225                 struct ref *ref = NULL;
227                 if (rm->peer_ref) {
228                         ref = xcalloc(1, sizeof(*ref) + strlen(rm->peer_ref->name) + 1);
229                         strcpy(ref->name, rm->peer_ref->name);
230                         hashcpy(ref->old_sha1, rm->peer_ref->old_sha1);
231                         hashcpy(ref->new_sha1, rm->old_sha1);
232                         ref->force = rm->peer_ref->force;
233                 }
235                 commit = lookup_commit_reference(rm->old_sha1);
236                 if (!commit)
237                         rm->merge = 0;
239                 if (!strcmp(rm->name, "HEAD")) {
240                         kind = "";
241                         what = "";
242                 }
243                 else if (!prefixcmp(rm->name, "refs/heads/")) {
244                         kind = "branch";
245                         what = rm->name + 11;
246                 }
247                 else if (!prefixcmp(rm->name, "refs/tags/")) {
248                         kind = "tag";
249                         what = rm->name + 10;
250                 }
251                 else if (!prefixcmp(rm->name, "refs/remotes/")) {
252                         kind = "remote branch";
253                         what = rm->name + 13;
254                 }
255                 else {
256                         kind = "";
257                         what = rm->name;
258                 }
260                 url_len = strlen(url);
261                 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
262                         ;
263                 url_len = i + 1;
264                 if (4 < i && !strncmp(".git", url + i - 3, 4))
265                         url_len = i - 3;
267                 note_len = 0;
268                 if (*what) {
269                         if (*kind)
270                                 note_len += sprintf(note + note_len, "%s ",
271                                                     kind);
272                         note_len += sprintf(note + note_len, "'%s' of ", what);
273                 }
274                 note_len += sprintf(note + note_len, "%.*s", url_len, url);
275                 fprintf(fp, "%s\t%s\t%s\n",
276                         sha1_to_hex(commit ? commit->object.sha1 :
277                                     rm->old_sha1),
278                         rm->merge ? "" : "not-for-merge",
279                         note);
281                 if (ref)
282                         update_local_ref(ref, note, verbose);
283         }
284         fclose(fp);
287 static int fetch_refs(struct transport *transport, struct ref *ref_map)
289         int ret = transport_fetch_refs(transport, ref_map);
290         if (!ret)
291                 store_updated_refs(transport->url, ref_map);
292         transport_unlock_pack(transport);
293         return ret;
296 static int add_existing(const char *refname, const unsigned char *sha1,
297                         int flag, void *cbdata)
299         struct path_list *list = (struct path_list *)cbdata;
300         path_list_insert(refname, list);
301         return 0;
304 static struct ref *find_non_local_tags(struct transport *transport,
305                                        struct ref *fetch_map)
307         static struct path_list existing_refs = { NULL, 0, 0, 0 };
308         struct path_list new_refs = { NULL, 0, 0, 1 };
309         char *ref_name;
310         int ref_name_len;
311         unsigned char *ref_sha1;
312         struct ref *tag_ref;
313         struct ref *rm = NULL;
314         struct ref *ref_map = NULL;
315         struct ref **tail = &ref_map;
316         struct ref *ref;
318         for_each_ref(add_existing, &existing_refs);
319         for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
320                 if (prefixcmp(ref->name, "refs/tags"))
321                         continue;
323                 ref_name = xstrdup(ref->name);
324                 ref_name_len = strlen(ref_name);
325                 ref_sha1 = ref->old_sha1;
327                 if (!strcmp(ref_name + ref_name_len - 3, "^{}")) {
328                         ref_name[ref_name_len - 3] = 0;
329                         tag_ref = transport_get_remote_refs(transport);
330                         while (tag_ref) {
331                                 if (!strcmp(tag_ref->name, ref_name)) {
332                                         ref_sha1 = tag_ref->old_sha1;
333                                         break;
334                                 }
335                                 tag_ref = tag_ref->next;
336                         }
337                 }
339                 if (!path_list_has_path(&existing_refs, ref_name) &&
340                     !path_list_has_path(&new_refs, ref_name) &&
341                     lookup_object(ref->old_sha1)) {
342                         fprintf(stderr, "Auto-following %s\n",
343                                 ref_name);
345                         path_list_insert(ref_name, &new_refs);
347                         rm = alloc_ref(strlen(ref_name) + 1);
348                         strcpy(rm->name, ref_name);
349                         rm->peer_ref = alloc_ref(strlen(ref_name) + 1);
350                         strcpy(rm->peer_ref->name, ref_name);
351                         hashcpy(rm->old_sha1, ref_sha1);
353                         *tail = rm;
354                         tail = &rm->next;
355                 }
356                 free(ref_name);
357         }
359         return ref_map;
362 static int do_fetch(struct transport *transport,
363                     struct refspec *refs, int ref_count)
365         struct ref *ref_map, *fetch_map;
366         struct ref *rm;
367         int autotags = (transport->remote->fetch_tags == 1);
368         if (transport->remote->fetch_tags == 2 && !no_tags)
369                 tags = 1;
370         if (transport->remote->fetch_tags == -1)
371                 no_tags = 1;
373         if (!transport->ops || !transport->ops->get_refs_list ||
374             !transport->ops->fetch)
375                 die("Don't know how to fetch from %s", transport->url);
377         /* if not appending, truncate FETCH_HEAD */
378         if (!append)
379                 fclose(fopen(git_path("FETCH_HEAD"), "w"));
381         ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
383         for (rm = ref_map; rm; rm = rm->next) {
384                 if (rm->peer_ref)
385                         read_ref(rm->peer_ref->name, rm->peer_ref->old_sha1);
386         }
388         if (fetch_refs(transport, ref_map)) {
389                 free_refs(ref_map);
390                 return 1;
391         }
393         fetch_map = ref_map;
395         /* if neither --no-tags nor --tags was specified, do automated tag
396          * following ... */
397         if (!(tags || no_tags) && autotags) {
398                 ref_map = find_non_local_tags(transport, fetch_map);
399                 if (ref_map) {
400                         transport_set_option(transport, TRANS_OPT_DEPTH, "0");
401                         fetch_refs(transport, ref_map);
402                 }
403                 free_refs(ref_map);
404         }
406         free_refs(fetch_map);
408         return 0;
411 int cmd_fetch(int argc, const char **argv, const char *prefix)
413         struct remote *remote;
414         int i, j, rla_offset;
415         static const char **refs = NULL;
416         int ref_nr = 0;
417         int cmd_len = 0;
418         const char *depth = NULL, *upload_pack = NULL;
419         int keep = 0;
421         for (i = 1; i < argc; i++) {
422                 const char *arg = argv[i];
423                 cmd_len += strlen(arg);
425                 if (arg[0] != '-')
426                         break;
427                 if (!strcmp(arg, "--append") || !strcmp(arg, "-a")) {
428                         append = 1;
429                         continue;
430                 }
431                 if (!prefixcmp(arg, "--upload-pack=")) {
432                         upload_pack = arg + 14;
433                         continue;
434                 }
435                 if (!strcmp(arg, "--upload-pack")) {
436                         i++;
437                         if (i == argc)
438                                 usage(fetch_usage);
439                         upload_pack = argv[i];
440                         continue;
441                 }
442                 if (!strcmp(arg, "--force") || !strcmp(arg, "-f")) {
443                         force = 1;
444                         continue;
445                 }
446                 if (!strcmp(arg, "--no-tags")) {
447                         no_tags = 1;
448                         continue;
449                 }
450                 if (!strcmp(arg, "--tags") || !strcmp(arg, "-t")) {
451                         tags = 1;
452                         continue;
453                 }
454                 if (!strcmp(arg, "--keep") || !strcmp(arg, "-k")) {
455                         keep = 1;
456                         continue;
457                 }
458                 if (!strcmp(arg, "--update-head-ok") || !strcmp(arg, "-u")) {
459                         update_head_ok = 1;
460                         continue;
461                 }
462                 if (!prefixcmp(arg, "--depth=")) {
463                         depth = arg + 8;
464                         continue;
465                 }
466                 if (!strcmp(arg, "--depth")) {
467                         i++;
468                         if (i == argc)
469                                 usage(fetch_usage);
470                         depth = argv[i];
471                         continue;
472                 }
473                 if (!strcmp(arg, "--quiet")) {
474                         quiet = 1;
475                         continue;
476                 }
477                 if (!strcmp(arg, "--verbose") || !strcmp(arg, "-v")) {
478                         verbose++;
479                         continue;
480                 }
481                 usage(fetch_usage);
482         }
484         for (j = i; j < argc; j++)
485                 cmd_len += strlen(argv[j]);
487         default_rla = xmalloc(cmd_len + 5 + argc + 1);
488         sprintf(default_rla, "fetch");
489         rla_offset = strlen(default_rla);
490         for (j = 1; j < argc; j++) {
491                 sprintf(default_rla + rla_offset, " %s", argv[j]);
492                 rla_offset += strlen(argv[j]) + 1;
493         }
495         if (i == argc)
496                 remote = remote_get(NULL);
497         else
498                 remote = remote_get(argv[i++]);
500         transport = transport_get(remote, remote->uri[0]);
501         if (verbose >= 2)
502                 transport->verbose = 1;
503         if (quiet)
504                 transport->verbose = 0;
505         if (upload_pack)
506                 transport_set_option(transport, TRANS_OPT_UPLOADPACK, upload_pack);
507         if (keep)
508                 transport_set_option(transport, TRANS_OPT_KEEP, "yes");
509         transport_set_option(transport, TRANS_OPT_DEPTH, depth);
511         if (!transport->url)
512                 die("Where do you want to fetch from today?");
514         if (i < argc) {
515                 int j = 0;
516                 refs = xcalloc(argc - i + 1, sizeof(const char *));
517                 while (i < argc) {
518                         if (!strcmp(argv[i], "tag")) {
519                                 char *ref;
520                                 i++;
521                                 ref = xmalloc(strlen(argv[i]) * 2 + 22);
522                                 strcpy(ref, "refs/tags/");
523                                 strcat(ref, argv[i]);
524                                 strcat(ref, ":refs/tags/");
525                                 strcat(ref, argv[i]);
526                                 refs[j++] = ref;
527                         } else
528                                 refs[j++] = argv[i];
529                         i++;
530                 }
531                 refs[j] = NULL;
532                 ref_nr = j;
533                 for (j = 0; refs[j]; j++)
534                         printf("ref: %s\n", refs[j]);
535         }
537         signal(SIGINT, unlock_pack_on_signal);
538         atexit(unlock_pack);
539         return do_fetch(transport, parse_ref_spec(ref_nr, refs), ref_nr);