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)
21 transport_unlock_pack(transport);
24 static void unlock_pack_on_signal(int signo)
27 signal(SIGINT, SIG_DFL);
31 static void add_merge_config(struct ref **head,
32 struct ref *remote_refs,
33 struct branch *branch,
38 for (i = 0; i < branch->merge_nr; i++) {
39 struct ref *rm, **old_tail = *tail;
40 struct refspec refspec;
42 for (rm = *head; rm; rm = rm->next) {
43 if (branch_merge_matches(branch, i, rm->name)) {
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.
55 refspec.src = branch->merge[i]->src;
59 get_fetch_map(remote_refs, &refspec, tail);
60 for (rm = *old_tail; rm; rm = rm->next)
65 static struct ref *get_ref_map(struct transport *transport,
66 struct refspec *refs, int ref_count, int tags,
71 struct ref *ref_map = NULL;
72 struct ref **tail = &ref_map;
74 struct ref *remote_refs = transport_get_remote_refs(transport);
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])
82 /* Merge everything on the command line, but not --tags */
83 for (rm = ref_map; rm; rm = rm->next)
86 struct refspec refspec;
87 refspec.src = "refs/tags/";
88 refspec.dst = "refs/tags/";
91 get_fetch_map(remote_refs, &refspec, &tail);
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])
104 if (!i && !has_merge && ref_map &&
105 !strcmp(remote->fetch[0].src, ref_map->name))
109 add_merge_config(&ref_map, remote_refs, branch, &tail);
111 ref_map = get_remote_ref(remote_refs, "HEAD");
119 static void show_new(enum object_type type, unsigned char *sha1_new)
121 fprintf(stderr, " %s: %s\n", typename(type),
122 find_unique_abbrev(sha1_new, DEFAULT_ABBREV));
125 static int s_update_ref(const char *action,
130 char *rla = getenv("GIT_REFLOG_ACTION");
131 static struct ref_lock *lock;
135 snprintf(msg, sizeof(msg), "%s: %s", rla, action);
136 lock = lock_any_ref_for_update(ref->name,
137 check_old ? ref->old_sha1 : NULL, 0);
140 if (write_ref_sha1(lock, ref->new_sha1, msg) < 0)
145 static int update_local_ref(struct ref *ref,
149 char oldh[41], newh[41];
150 struct commit *current = NULL, *updated;
151 enum object_type type;
152 struct branch *current_branch = branch_get(NULL);
154 type = sha1_object_info(ref->new_sha1, NULL);
156 die("object %s not found", sha1_to_hex(ref->new_sha1));
161 fprintf(stderr, "* fetched %s\n", note);
162 show_new(type, ref->new_sha1);
167 if (!hashcmp(ref->old_sha1, ref->new_sha1)) {
169 fprintf(stderr, "* %s: same as %s\n",
171 show_new(type, ref->new_sha1);
176 if (current_branch &&
177 !strcmp(ref->name, current_branch->name) &&
178 !(update_head_ok || is_bare_repository()) &&
179 !is_null_sha1(ref->old_sha1)) {
181 * If this is the head, and it's not okay to update
182 * the head, and the old value of the head isn't empty...
185 " * %s: Cannot fetch into the current branch.\n",
190 if (!is_null_sha1(ref->old_sha1) &&
191 !prefixcmp(ref->name, "refs/tags/")) {
192 fprintf(stderr, "* %s: updating with %s\n",
194 show_new(type, ref->new_sha1);
195 return s_update_ref("updating tag", ref, 0);
198 current = lookup_commit_reference(ref->old_sha1);
199 updated = lookup_commit_reference(ref->new_sha1);
200 if (!current || !updated) {
202 if (!strncmp(ref->name, "refs/tags/", 10))
205 msg = "storing head";
206 fprintf(stderr, "* %s: storing %s\n",
208 show_new(type, ref->new_sha1);
209 return s_update_ref(msg, ref, 0);
212 strcpy(oldh, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
213 strcpy(newh, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
215 if (in_merge_bases(current, &updated, 1)) {
216 fprintf(stderr, "* %s: fast forward to %s\n",
218 fprintf(stderr, " old..new: %s..%s\n", oldh, newh);
219 return s_update_ref("fast forward", ref, 1);
221 if (!force && !ref->force) {
223 "* %s: not updating to non-fast forward %s\n",
226 " old...new: %s...%s\n", oldh, newh);
230 "* %s: forcing update to non-fast forward %s\n",
232 fprintf(stderr, " old...new: %s...%s\n", oldh, newh);
233 return s_update_ref("forced-update", ref, 1);
236 static void store_updated_refs(const char *url, struct ref *ref_map)
239 struct commit *commit;
240 int url_len, i, note_len;
242 const char *what, *kind;
245 fp = fopen(git_path("FETCH_HEAD"), "a");
246 for (rm = ref_map; rm; rm = rm->next) {
247 struct ref *ref = NULL;
250 ref = xcalloc(1, sizeof(*ref) + strlen(rm->peer_ref->name) + 1);
251 strcpy(ref->name, rm->peer_ref->name);
252 hashcpy(ref->old_sha1, rm->peer_ref->old_sha1);
253 hashcpy(ref->new_sha1, rm->old_sha1);
254 ref->force = rm->peer_ref->force;
257 commit = lookup_commit_reference(rm->old_sha1);
261 if (!strcmp(rm->name, "HEAD")) {
265 else if (!prefixcmp(rm->name, "refs/heads/")) {
267 what = rm->name + 11;
269 else if (!prefixcmp(rm->name, "refs/tags/")) {
271 what = rm->name + 10;
273 else if (!prefixcmp(rm->name, "refs/remotes/")) {
274 kind = "remote branch";
275 what = rm->name + 13;
282 url_len = strlen(url);
283 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
286 if (4 < i && !strncmp(".git", url + i - 3, 4))
292 note_len += sprintf(note + note_len, "%s ",
294 note_len += sprintf(note + note_len, "'%s' of ", what);
296 note_len += sprintf(note + note_len, "%.*s", url_len, url);
297 fprintf(fp, "%s\t%s\t%s\n",
298 sha1_to_hex(commit ? commit->object.sha1 :
300 rm->merge ? "" : "not-for-merge",
304 update_local_ref(ref, note, verbose);
309 static int fetch_refs(struct transport *transport, struct ref *ref_map)
311 int ret = transport_fetch_refs(transport, ref_map);
313 store_updated_refs(transport->url, ref_map);
314 transport_unlock_pack(transport);
318 static int add_existing(const char *refname, const unsigned char *sha1,
319 int flag, void *cbdata)
321 struct path_list *list = (struct path_list *)cbdata;
322 path_list_insert(refname, list);
326 static struct ref *find_non_local_tags(struct transport *transport,
327 struct ref *fetch_map)
329 static struct path_list existing_refs = { NULL, 0, 0, 0 };
330 struct path_list new_refs = { NULL, 0, 0, 1 };
333 unsigned char *ref_sha1;
335 struct ref *rm = NULL;
336 struct ref *ref_map = NULL;
337 struct ref **tail = &ref_map;
340 for_each_ref(add_existing, &existing_refs);
341 for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
342 if (prefixcmp(ref->name, "refs/tags"))
345 ref_name = xstrdup(ref->name);
346 ref_name_len = strlen(ref_name);
347 ref_sha1 = ref->old_sha1;
349 if (!strcmp(ref_name + ref_name_len - 3, "^{}")) {
350 ref_name[ref_name_len - 3] = 0;
351 tag_ref = transport_get_remote_refs(transport);
353 if (!strcmp(tag_ref->name, ref_name)) {
354 ref_sha1 = tag_ref->old_sha1;
357 tag_ref = tag_ref->next;
361 if (!path_list_has_path(&existing_refs, ref_name) &&
362 !path_list_has_path(&new_refs, ref_name) &&
363 lookup_object(ref->old_sha1)) {
364 fprintf(stderr, "Auto-following %s\n",
367 path_list_insert(ref_name, &new_refs);
369 rm = alloc_ref(strlen(ref_name) + 1);
370 strcpy(rm->name, ref_name);
371 rm->peer_ref = alloc_ref(strlen(ref_name) + 1);
372 strcpy(rm->peer_ref->name, ref_name);
373 hashcpy(rm->old_sha1, ref_sha1);
384 static int do_fetch(struct transport *transport,
385 struct refspec *refs, int ref_count)
387 struct ref *ref_map, *fetch_map;
389 int autotags = (transport->remote->fetch_tags == 1);
390 if (transport->remote->fetch_tags == 2 && !no_tags)
392 if (transport->remote->fetch_tags == -1)
395 if (!transport->get_refs_list || !transport->fetch)
396 die("Don't know how to fetch from %s", transport->url);
398 /* if not appending, truncate FETCH_HEAD */
400 fclose(fopen(git_path("FETCH_HEAD"), "w"));
402 ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
404 for (rm = ref_map; rm; rm = rm->next) {
406 read_ref(rm->peer_ref->name, rm->peer_ref->old_sha1);
409 if (fetch_refs(transport, ref_map)) {
416 /* if neither --no-tags nor --tags was specified, do automated tag
418 if (!(tags || no_tags) && autotags) {
419 ref_map = find_non_local_tags(transport, fetch_map);
421 transport_set_option(transport, TRANS_OPT_DEPTH, "0");
422 fetch_refs(transport, ref_map);
427 free_refs(fetch_map);
432 static void set_option(const char *name, const char *value)
434 int r = transport_set_option(transport, name, value);
436 die("Option \"%s\" value \"%s\" is not valid for %s\n",
437 name, value, transport->url);
439 warning("Option \"%s\" is ignored for %s\n",
440 name, transport->url);
443 int cmd_fetch(int argc, const char **argv, const char *prefix)
445 struct remote *remote;
446 int i, j, rla_offset;
447 static const char **refs = NULL;
450 const char *depth = NULL, *upload_pack = NULL;
453 for (i = 1; i < argc; i++) {
454 const char *arg = argv[i];
455 cmd_len += strlen(arg);
459 if (!strcmp(arg, "--append") || !strcmp(arg, "-a")) {
463 if (!prefixcmp(arg, "--upload-pack=")) {
464 upload_pack = arg + 14;
467 if (!strcmp(arg, "--upload-pack")) {
471 upload_pack = argv[i];
474 if (!strcmp(arg, "--force") || !strcmp(arg, "-f")) {
478 if (!strcmp(arg, "--no-tags")) {
482 if (!strcmp(arg, "--tags") || !strcmp(arg, "-t")) {
486 if (!strcmp(arg, "--keep") || !strcmp(arg, "-k")) {
490 if (!strcmp(arg, "--update-head-ok") || !strcmp(arg, "-u")) {
494 if (!prefixcmp(arg, "--depth=")) {
498 if (!strcmp(arg, "--depth")) {
505 if (!strcmp(arg, "--quiet")) {
509 if (!strcmp(arg, "--verbose") || !strcmp(arg, "-v")) {
516 for (j = i; j < argc; j++)
517 cmd_len += strlen(argv[j]);
519 default_rla = xmalloc(cmd_len + 5 + argc + 1);
520 sprintf(default_rla, "fetch");
521 rla_offset = strlen(default_rla);
522 for (j = 1; j < argc; j++) {
523 sprintf(default_rla + rla_offset, " %s", argv[j]);
524 rla_offset += strlen(argv[j]) + 1;
528 remote = remote_get(NULL);
530 remote = remote_get(argv[i++]);
532 transport = transport_get(remote, remote->url[0]);
534 transport->verbose = 1;
536 transport->verbose = 0;
538 set_option(TRANS_OPT_UPLOADPACK, upload_pack);
540 set_option(TRANS_OPT_KEEP, "yes");
542 set_option(TRANS_OPT_DEPTH, depth);
545 die("Where do you want to fetch from today?");
549 refs = xcalloc(argc - i + 1, sizeof(const char *));
551 if (!strcmp(argv[i], "tag")) {
554 ref = xmalloc(strlen(argv[i]) * 2 + 22);
555 strcpy(ref, "refs/tags/");
556 strcat(ref, argv[i]);
557 strcat(ref, ":refs/tags/");
558 strcat(ref, argv[i]);
568 signal(SIGINT, unlock_pack_on_signal);
570 return do_fetch(transport, parse_ref_spec(ref_nr, refs), ref_nr);