10 #include "transport.h"
11 #include "run-command.h"
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>...]";
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;
20 static void unlock_pack(void)
23 transport_unlock_pack(transport);
26 static void unlock_pack_on_signal(int signo)
29 signal(SIGINT, SIG_DFL);
33 static void add_merge_config(struct ref **head,
34 struct ref *remote_refs,
35 struct branch *branch,
40 for (i = 0; i < branch->merge_nr; i++) {
41 struct ref *rm, **old_tail = *tail;
42 struct refspec refspec;
44 for (rm = *head; rm; rm = rm->next) {
45 if (branch_merge_matches(branch, i, rm->name)) {
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
63 refspec.src = branch->merge[i]->src;
67 get_fetch_map(remote_refs, &refspec, tail, 1);
68 for (rm = *old_tail; rm; rm = rm->next)
73 static struct ref *get_ref_map(struct transport *transport,
74 struct refspec *refs, int ref_count, int tags,
79 struct ref *ref_map = NULL;
80 struct ref **tail = &ref_map;
82 struct ref *remote_refs = transport_get_remote_refs(transport);
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])
90 /* Merge everything on the command line, but not --tags */
91 for (rm = ref_map; rm; rm = rm->next)
94 struct refspec refspec;
95 refspec.src = "refs/tags/";
96 refspec.dst = "refs/tags/";
99 get_fetch_map(remote_refs, &refspec, &tail, 0);
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])
112 if (!i && !has_merge && ref_map &&
113 !remote->fetch[0].pattern)
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.
122 !strcmp(branch->remote_name, remote->name))
123 add_merge_config(&ref_map, remote_refs, branch, &tail);
125 ref_map = get_remote_ref(remote_refs, "HEAD");
127 die("Couldn't find remote ref HEAD");
131 ref_remove_duplicates(ref_map);
136 static int s_update_ref(const char *action,
141 char *rla = getenv("GIT_REFLOG_ACTION");
142 static struct ref_lock *lock;
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);
151 if (write_ref_sha1(lock, ref->new_sha1, msg) < 0)
156 #define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
157 #define REFCOL_WIDTH 10
159 static int update_local_ref(struct ref *ref,
164 struct commit *current = NULL, *updated;
165 enum object_type type;
166 struct branch *current_branch = branch_get(NULL);
167 const char *pretty_ref = ref->name + (
168 !prefixcmp(ref->name, "refs/heads/") ? 11 :
169 !prefixcmp(ref->name, "refs/tags/") ? 10 :
170 !prefixcmp(ref->name, "refs/remotes/") ? 13 :
174 type = sha1_object_info(ref->new_sha1, NULL);
176 die("object %s not found", sha1_to_hex(ref->new_sha1));
181 sprintf(display, "* branch %s -> FETCH_HEAD", remote);
185 if (!hashcmp(ref->old_sha1, ref->new_sha1)) {
187 sprintf(display, "= %-*s %-*s -> %s", SUMMARY_WIDTH,
188 "[up to date]", REFCOL_WIDTH, remote,
193 if (current_branch &&
194 !strcmp(ref->name, current_branch->name) &&
195 !(update_head_ok || is_bare_repository()) &&
196 !is_null_sha1(ref->old_sha1)) {
198 * If this is the head, and it's not okay to update
199 * the head, and the old value of the head isn't empty...
201 sprintf(display, "! %-*s %-*s -> %s (can't fetch in current branch)",
202 SUMMARY_WIDTH, "[rejected]", REFCOL_WIDTH, remote,
207 if (!is_null_sha1(ref->old_sha1) &&
208 !prefixcmp(ref->name, "refs/tags/")) {
209 sprintf(display, "- %-*s %-*s -> %s",
210 SUMMARY_WIDTH, "[tag update]", REFCOL_WIDTH, remote,
212 return s_update_ref("updating tag", ref, 0);
215 current = lookup_commit_reference_gently(ref->old_sha1, 1);
216 updated = lookup_commit_reference_gently(ref->new_sha1, 1);
217 if (!current || !updated) {
220 if (!strncmp(ref->name, "refs/tags/", 10)) {
225 msg = "storing head";
226 what = "[new branch]";
229 sprintf(display, "* %-*s %-*s -> %s", SUMMARY_WIDTH, what,
230 REFCOL_WIDTH, remote, pretty_ref);
231 return s_update_ref(msg, ref, 0);
234 if (in_merge_bases(current, &updated, 1)) {
236 strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
237 strcat(quickref, "..");
238 strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
239 sprintf(display, " %-*s %-*s -> %s", SUMMARY_WIDTH, quickref,
240 REFCOL_WIDTH, remote, pretty_ref);
241 return s_update_ref("fast forward", ref, 1);
242 } else if (force || ref->force) {
244 strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
245 strcat(quickref, "...");
246 strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
247 sprintf(display, "+ %-*s %-*s -> %s (forced update)",
248 SUMMARY_WIDTH, quickref, REFCOL_WIDTH, remote, pretty_ref);
249 return s_update_ref("forced-update", ref, 1);
251 sprintf(display, "! %-*s %-*s -> %s (non fast forward)",
252 SUMMARY_WIDTH, "[rejected]", REFCOL_WIDTH, remote,
258 static void store_updated_refs(const char *url, struct ref *ref_map)
261 struct commit *commit;
262 int url_len, i, note_len, shown_url = 0;
264 const char *what, *kind;
267 fp = fopen(git_path("FETCH_HEAD"), "a");
268 for (rm = ref_map; rm; rm = rm->next) {
269 struct ref *ref = NULL;
272 ref = xcalloc(1, sizeof(*ref) + strlen(rm->peer_ref->name) + 1);
273 strcpy(ref->name, rm->peer_ref->name);
274 hashcpy(ref->old_sha1, rm->peer_ref->old_sha1);
275 hashcpy(ref->new_sha1, rm->old_sha1);
276 ref->force = rm->peer_ref->force;
279 commit = lookup_commit_reference_gently(rm->old_sha1, 1);
283 if (!strcmp(rm->name, "HEAD")) {
287 else if (!prefixcmp(rm->name, "refs/heads/")) {
289 what = rm->name + 11;
291 else if (!prefixcmp(rm->name, "refs/tags/")) {
293 what = rm->name + 10;
295 else if (!prefixcmp(rm->name, "refs/remotes/")) {
296 kind = "remote branch";
297 what = rm->name + 13;
304 url_len = strlen(url);
305 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
308 if (4 < i && !strncmp(".git", url + i - 3, 4))
314 note_len += sprintf(note + note_len, "%s ",
316 note_len += sprintf(note + note_len, "'%s' of ", what);
318 note_len += sprintf(note + note_len, "%.*s", url_len, url);
319 fprintf(fp, "%s\t%s\t%s\n",
320 sha1_to_hex(commit ? commit->object.sha1 :
322 rm->merge ? "" : "not-for-merge",
326 update_local_ref(ref, what, verbose, note);
329 fprintf(stderr, "From %.*s\n",
333 fprintf(stderr, " %s\n", note);
341 * We would want to bypass the object transfer altogether if
342 * everything we are going to fetch already exists and connected
345 * The refs we are going to fetch are in to_fetch (nr_heads in
348 * $ git-rev-list --objects to_fetch[0] to_fetch[1] ... --not --all
350 * does not error out, that means everything reachable from the
351 * refs we are going to fetch exists and is connected to some of
354 static int quickfetch(struct ref *ref_map)
356 struct child_process revlist;
362 * If we are deepening a shallow clone we already have these
363 * objects reachable. Running rev-list here will return with
364 * a good (0) exit status and we'll bypass the fetch that we
365 * really need to perform. Claiming failure now will ensure
366 * we perform the network exchange to deepen our history.
371 for (i = 0, ref = ref_map; ref; ref = ref->next)
376 argv = xmalloc(sizeof(*argv) * (i + 6));
378 argv[i++] = xstrdup("rev-list");
379 argv[i++] = xstrdup("--quiet");
380 argv[i++] = xstrdup("--objects");
381 for (ref = ref_map; ref; ref = ref->next)
382 argv[i++] = xstrdup(sha1_to_hex(ref->old_sha1));
383 argv[i++] = xstrdup("--not");
384 argv[i++] = xstrdup("--all");
387 memset(&revlist, 0, sizeof(revlist));
388 revlist.argv = (const char**)argv;
390 revlist.no_stdin = 1;
391 revlist.no_stdout = 1;
392 revlist.no_stderr = 1;
393 err = run_command(&revlist);
395 for (i = 0; argv[i]; i++)
401 static int fetch_refs(struct transport *transport, struct ref *ref_map)
403 int ret = quickfetch(ref_map);
405 ret = transport_fetch_refs(transport, ref_map);
407 store_updated_refs(transport->url, ref_map);
408 transport_unlock_pack(transport);
412 static int add_existing(const char *refname, const unsigned char *sha1,
413 int flag, void *cbdata)
415 struct path_list *list = (struct path_list *)cbdata;
416 path_list_insert(refname, list);
420 static struct ref *find_non_local_tags(struct transport *transport,
421 struct ref *fetch_map)
423 static struct path_list existing_refs = { NULL, 0, 0, 0 };
424 struct path_list new_refs = { NULL, 0, 0, 1 };
427 unsigned char *ref_sha1;
429 struct ref *rm = NULL;
430 struct ref *ref_map = NULL;
431 struct ref **tail = &ref_map;
434 for_each_ref(add_existing, &existing_refs);
435 for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
436 if (prefixcmp(ref->name, "refs/tags"))
439 ref_name = xstrdup(ref->name);
440 ref_name_len = strlen(ref_name);
441 ref_sha1 = ref->old_sha1;
443 if (!strcmp(ref_name + ref_name_len - 3, "^{}")) {
444 ref_name[ref_name_len - 3] = 0;
445 tag_ref = transport_get_remote_refs(transport);
447 if (!strcmp(tag_ref->name, ref_name)) {
448 ref_sha1 = tag_ref->old_sha1;
451 tag_ref = tag_ref->next;
455 if (!path_list_has_path(&existing_refs, ref_name) &&
456 !path_list_has_path(&new_refs, ref_name) &&
457 has_sha1_file(ref->old_sha1)) {
458 path_list_insert(ref_name, &new_refs);
460 rm = alloc_ref(strlen(ref_name) + 1);
461 strcpy(rm->name, ref_name);
462 rm->peer_ref = alloc_ref(strlen(ref_name) + 1);
463 strcpy(rm->peer_ref->name, ref_name);
464 hashcpy(rm->old_sha1, ref_sha1);
475 static int do_fetch(struct transport *transport,
476 struct refspec *refs, int ref_count)
478 struct ref *ref_map, *fetch_map;
480 int autotags = (transport->remote->fetch_tags == 1);
481 if (transport->remote->fetch_tags == 2 && !no_tags)
483 if (transport->remote->fetch_tags == -1)
486 if (!transport->get_refs_list || !transport->fetch)
487 die("Don't know how to fetch from %s", transport->url);
489 /* if not appending, truncate FETCH_HEAD */
491 fclose(fopen(git_path("FETCH_HEAD"), "w"));
493 ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
495 for (rm = ref_map; rm; rm = rm->next) {
497 read_ref(rm->peer_ref->name, rm->peer_ref->old_sha1);
500 if (fetch_refs(transport, ref_map)) {
507 /* if neither --no-tags nor --tags was specified, do automated tag
509 if (!(tags || no_tags) && autotags) {
510 ref_map = find_non_local_tags(transport, fetch_map);
512 transport_set_option(transport, TRANS_OPT_DEPTH, "0");
513 fetch_refs(transport, ref_map);
518 free_refs(fetch_map);
523 static void set_option(const char *name, const char *value)
525 int r = transport_set_option(transport, name, value);
527 die("Option \"%s\" value \"%s\" is not valid for %s\n",
528 name, value, transport->url);
530 warning("Option \"%s\" is ignored for %s\n",
531 name, transport->url);
534 int cmd_fetch(int argc, const char **argv, const char *prefix)
536 struct remote *remote;
537 int i, j, rla_offset;
538 static const char **refs = NULL;
541 const char *upload_pack = NULL;
544 for (i = 1; i < argc; i++) {
545 const char *arg = argv[i];
546 cmd_len += strlen(arg);
550 if (!strcmp(arg, "--append") || !strcmp(arg, "-a")) {
554 if (!prefixcmp(arg, "--upload-pack=")) {
555 upload_pack = arg + 14;
558 if (!strcmp(arg, "--upload-pack")) {
562 upload_pack = argv[i];
565 if (!strcmp(arg, "--force") || !strcmp(arg, "-f")) {
569 if (!strcmp(arg, "--no-tags")) {
573 if (!strcmp(arg, "--tags") || !strcmp(arg, "-t")) {
577 if (!strcmp(arg, "--keep") || !strcmp(arg, "-k")) {
581 if (!strcmp(arg, "--update-head-ok") || !strcmp(arg, "-u")) {
585 if (!prefixcmp(arg, "--depth=")) {
589 if (!strcmp(arg, "--depth")) {
596 if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) {
600 if (!strcmp(arg, "--verbose") || !strcmp(arg, "-v")) {
607 for (j = i; j < argc; j++)
608 cmd_len += strlen(argv[j]);
610 default_rla = xmalloc(cmd_len + 5 + argc + 1);
611 sprintf(default_rla, "fetch");
612 rla_offset = strlen(default_rla);
613 for (j = 1; j < argc; j++) {
614 sprintf(default_rla + rla_offset, " %s", argv[j]);
615 rla_offset += strlen(argv[j]) + 1;
619 remote = remote_get(NULL);
621 remote = remote_get(argv[i++]);
623 transport = transport_get(remote, remote->url[0]);
625 transport->verbose = 1;
627 transport->verbose = -1;
629 set_option(TRANS_OPT_UPLOADPACK, upload_pack);
631 set_option(TRANS_OPT_KEEP, "yes");
633 set_option(TRANS_OPT_DEPTH, depth);
636 die("Where do you want to fetch from today?");
640 refs = xcalloc(argc - i + 1, sizeof(const char *));
642 if (!strcmp(argv[i], "tag")) {
645 ref = xmalloc(strlen(argv[i]) * 2 + 22);
646 strcpy(ref, "refs/tags/");
647 strcat(ref, argv[i]);
648 strcat(ref, ":refs/tags/");
649 strcat(ref, argv[i]);
659 signal(SIGINT, unlock_pack_on_signal);
661 return do_fetch(transport, parse_ref_spec(ref_nr, refs), ref_nr);