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 find_merge_config(struct ref *ref_map, struct remote *remote)
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))
42 if (branch_merges(branch, rm->name))
48 static struct ref *get_ref_map(struct transport *transport,
49 struct refspec *refs, int ref_count, int tags,
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])
65 /* Merge everything on the command line, but not --tags */
66 for (rm = ref_map; rm; rm = rm->next)
69 struct refspec refspec;
70 refspec.src = "refs/tags/";
71 refspec.dst = "refs/tags/";
74 get_fetch_map(remote_refs, &refspec, &tail);
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])
86 find_merge_config(ref_map, remote);
88 ref_map = get_remote_ref(remote_refs, "HEAD");
97 static void show_new(enum object_type type, unsigned char *sha1_new)
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,
108 char *rla = getenv("GIT_REFLOG_ACTION");
109 static struct ref_lock *lock;
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);
118 if (write_ref_sha1(lock, ref->new_sha1, msg) < 0)
123 static int update_local_ref(struct ref *ref,
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);
134 die("object %s not found", sha1_to_hex(ref->new_sha1));
139 fprintf(stderr, "* fetched %s\n", note);
140 show_new(type, ref->new_sha1);
145 if (!hashcmp(ref->old_sha1, ref->new_sha1)) {
147 fprintf(stderr, "* %s: same as %s\n",
149 show_new(type, ref->new_sha1);
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)) {
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...
163 " * %s: Cannot fetch into the current branch.\n",
168 if (!is_null_sha1(ref->old_sha1) &&
169 !prefixcmp(ref->name, "refs/tags/")) {
170 fprintf(stderr, "* %s: updating with %s\n",
172 show_new(type, ref->new_sha1);
173 return s_update_ref("updating tag", ref, 0);
176 current = lookup_commit_reference(ref->old_sha1);
177 updated = lookup_commit_reference(ref->new_sha1);
178 if (!current || !updated) {
180 if (!strncmp(ref->name, "refs/tags/", 10))
183 msg = "storing head";
184 fprintf(stderr, "* %s: storing %s\n",
186 show_new(type, ref->new_sha1);
187 return s_update_ref(msg, ref, 0);
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",
196 fprintf(stderr, " old..new: %s..%s\n", oldh, newh);
197 return s_update_ref("fast forward", ref, 1);
199 if (!force && !ref->force) {
201 "* %s: not updating to non-fast forward %s\n",
204 " old...new: %s...%s\n", oldh, newh);
208 "* %s: forcing update to non-fast forward %s\n",
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)
217 struct commit *commit;
218 int url_len, i, note_len;
220 const char *what, *kind;
223 fp = fopen(git_path("FETCH_HEAD"), "a");
224 for (rm = ref_map; rm; rm = rm->next) {
225 struct ref *ref = NULL;
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;
235 commit = lookup_commit_reference(rm->old_sha1);
239 if (!strcmp(rm->name, "HEAD")) {
243 else if (!prefixcmp(rm->name, "refs/heads/")) {
245 what = rm->name + 11;
247 else if (!prefixcmp(rm->name, "refs/tags/")) {
249 what = rm->name + 10;
251 else if (!prefixcmp(rm->name, "refs/remotes/")) {
252 kind = "remote branch";
253 what = rm->name + 13;
260 url_len = strlen(url);
261 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
264 if (4 < i && !strncmp(".git", url + i - 3, 4))
270 note_len += sprintf(note + note_len, "%s ",
272 note_len += sprintf(note + note_len, "'%s' of ", what);
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 :
278 rm->merge ? "" : "not-for-merge",
282 update_local_ref(ref, note, verbose);
287 static int fetch_refs(struct transport *transport, struct ref *ref_map)
289 int ret = transport_fetch_refs(transport, ref_map);
291 store_updated_refs(transport->url, ref_map);
292 transport_unlock_pack(transport);
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);
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 };
311 unsigned char *ref_sha1;
313 struct ref *rm = NULL;
314 struct ref *ref_map = NULL;
315 struct ref **tail = &ref_map;
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"))
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);
331 if (!strcmp(tag_ref->name, ref_name)) {
332 ref_sha1 = tag_ref->old_sha1;
335 tag_ref = tag_ref->next;
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",
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);
362 static int do_fetch(struct transport *transport,
363 struct refspec *refs, int ref_count)
365 struct ref *ref_map, *fetch_map;
367 int autotags = (transport->remote->fetch_tags == 1);
368 if (transport->remote->fetch_tags == 2 && !no_tags)
370 if (transport->remote->fetch_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 */
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) {
385 read_ref(rm->peer_ref->name, rm->peer_ref->old_sha1);
388 if (fetch_refs(transport, ref_map)) {
395 /* if neither --no-tags nor --tags was specified, do automated tag
397 if (!(tags || no_tags) && autotags) {
398 ref_map = find_non_local_tags(transport, fetch_map);
400 transport_set_option(transport, TRANS_OPT_DEPTH, "0");
401 fetch_refs(transport, ref_map);
406 free_refs(fetch_map);
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;
418 const char *depth = NULL, *upload_pack = NULL;
421 for (i = 1; i < argc; i++) {
422 const char *arg = argv[i];
423 cmd_len += strlen(arg);
427 if (!strcmp(arg, "--append") || !strcmp(arg, "-a")) {
431 if (!prefixcmp(arg, "--upload-pack=")) {
432 upload_pack = arg + 14;
435 if (!strcmp(arg, "--upload-pack")) {
439 upload_pack = argv[i];
442 if (!strcmp(arg, "--force") || !strcmp(arg, "-f")) {
446 if (!strcmp(arg, "--no-tags")) {
450 if (!strcmp(arg, "--tags") || !strcmp(arg, "-t")) {
454 if (!strcmp(arg, "--keep") || !strcmp(arg, "-k")) {
458 if (!strcmp(arg, "--update-head-ok") || !strcmp(arg, "-u")) {
462 if (!prefixcmp(arg, "--depth=")) {
466 if (!strcmp(arg, "--depth")) {
473 if (!strcmp(arg, "--quiet")) {
477 if (!strcmp(arg, "--verbose") || !strcmp(arg, "-v")) {
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;
496 remote = remote_get(NULL);
498 remote = remote_get(argv[i++]);
500 transport = transport_get(remote, remote->uri[0]);
502 transport->verbose = 1;
504 transport->verbose = 0;
506 transport_set_option(transport, TRANS_OPT_UPLOADPACK, upload_pack);
508 transport_set_option(transport, TRANS_OPT_KEEP, "yes");
509 transport_set_option(transport, TRANS_OPT_DEPTH, depth);
512 die("Where do you want to fetch from today?");
516 refs = xcalloc(argc - i + 1, sizeof(const char *));
518 if (!strcmp(argv[i], "tag")) {
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]);
533 for (j = 0; refs[j]; j++)
534 printf("ref: %s\n", refs[j]);
537 signal(SIGINT, unlock_pack_on_signal);
539 return do_fetch(transport, parse_ref_spec(ref_nr, refs), ref_nr);