8 #include "string-list.h"
10 #include "transport.h"
11 #include "run-command.h"
12 #include "parse-options.h"
15 static const char * const builtin_fetch_usage
[] = {
16 "git fetch [options] [<repository> <refspec>...]",
17 "git fetch [options] <group>",
18 "git fetch --multiple [options] [<repository> | <group>]...",
19 "git fetch --all [options]",
29 static int all
, append
, force
, keep
, multiple
, update_head_ok
, verbosity
;
30 static int tags
= TAGS_DEFAULT
;
31 static const char *depth
;
32 static const char *upload_pack
;
33 static struct strbuf default_rla
= STRBUF_INIT
;
34 static struct transport
*transport
;
36 static struct option builtin_fetch_options
[] = {
37 OPT__VERBOSITY(&verbosity
),
38 OPT_BOOLEAN(0, "all", &all
,
39 "fetch from all remotes"),
40 OPT_BOOLEAN('a', "append", &append
,
41 "append to .git/FETCH_HEAD instead of overwriting"),
42 OPT_STRING(0, "upload-pack", &upload_pack
, "PATH",
43 "path to upload pack on remote end"),
44 OPT_BOOLEAN('f', "force", &force
,
45 "force overwrite of local branch"),
46 OPT_BOOLEAN('m', "multiple", &multiple
,
47 "fetch from multiple remotes"),
48 OPT_SET_INT('t', "tags", &tags
,
49 "fetch all tags and associated objects", TAGS_SET
),
50 OPT_SET_INT('n', NULL
, &tags
,
51 "do not fetch all tags (--no-tags)", TAGS_UNSET
),
52 OPT_BOOLEAN('k', "keep", &keep
, "keep downloaded pack"),
53 OPT_BOOLEAN('u', "update-head-ok", &update_head_ok
,
54 "allow updating of HEAD ref"),
55 OPT_STRING(0, "depth", &depth
, "DEPTH",
56 "deepen history of shallow clone"),
60 static void unlock_pack(void)
63 transport_unlock_pack(transport
);
66 static void unlock_pack_on_signal(int signo
)
73 static void add_merge_config(struct ref
**head
,
74 const struct ref
*remote_refs
,
75 struct branch
*branch
,
80 for (i
= 0; i
< branch
->merge_nr
; i
++) {
81 struct ref
*rm
, **old_tail
= *tail
;
82 struct refspec refspec
;
84 for (rm
= *head
; rm
; rm
= rm
->next
) {
85 if (branch_merge_matches(branch
, i
, rm
->name
)) {
94 * Not fetched to a tracking branch? We need to fetch
95 * it anyway to allow this branch's "branch.$name.merge"
96 * to be honored by 'git pull', but we do not have to
97 * fail if branch.$name.merge is misconfigured to point
98 * at a nonexisting branch. If we were indeed called by
99 * 'git pull', it will notice the misconfiguration because
100 * there is no entry in the resulting FETCH_HEAD marked
103 refspec
.src
= branch
->merge
[i
]->src
;
107 get_fetch_map(remote_refs
, &refspec
, tail
, 1);
108 for (rm
= *old_tail
; rm
; rm
= rm
->next
)
113 static void find_non_local_tags(struct transport
*transport
,
117 static struct ref
*get_ref_map(struct transport
*transport
,
118 struct refspec
*refs
, int ref_count
, int tags
,
123 struct ref
*ref_map
= NULL
;
124 struct ref
**tail
= &ref_map
;
126 const struct ref
*remote_refs
= transport_get_remote_refs(transport
);
128 if (ref_count
|| tags
== TAGS_SET
) {
129 for (i
= 0; i
< ref_count
; i
++) {
130 get_fetch_map(remote_refs
, &refs
[i
], &tail
, 0);
131 if (refs
[i
].dst
&& refs
[i
].dst
[0])
134 /* Merge everything on the command line, but not --tags */
135 for (rm
= ref_map
; rm
; rm
= rm
->next
)
137 if (tags
== TAGS_SET
)
138 get_fetch_map(remote_refs
, tag_refspec
, &tail
, 0);
140 /* Use the defaults */
141 struct remote
*remote
= transport
->remote
;
142 struct branch
*branch
= branch_get(NULL
);
143 int has_merge
= branch_has_merge_config(branch
);
144 if (remote
&& (remote
->fetch_refspec_nr
|| has_merge
)) {
145 for (i
= 0; i
< remote
->fetch_refspec_nr
; i
++) {
146 get_fetch_map(remote_refs
, &remote
->fetch
[i
], &tail
, 0);
147 if (remote
->fetch
[i
].dst
&&
148 remote
->fetch
[i
].dst
[0])
150 if (!i
&& !has_merge
&& ref_map
&&
151 !remote
->fetch
[0].pattern
)
155 * if the remote we're fetching from is the same
156 * as given in branch.<name>.remote, we add the
157 * ref given in branch.<name>.merge, too.
160 !strcmp(branch
->remote_name
, remote
->name
))
161 add_merge_config(&ref_map
, remote_refs
, branch
, &tail
);
163 ref_map
= get_remote_ref(remote_refs
, "HEAD");
165 die("Couldn't find remote ref HEAD");
167 tail
= &ref_map
->next
;
170 if (tags
== TAGS_DEFAULT
&& *autotags
)
171 find_non_local_tags(transport
, &ref_map
, &tail
);
172 ref_remove_duplicates(ref_map
);
177 #define STORE_REF_ERROR_OTHER 1
178 #define STORE_REF_ERROR_DF_CONFLICT 2
180 static int s_update_ref(const char *action
,
185 char *rla
= getenv("GIT_REFLOG_ACTION");
186 static struct ref_lock
*lock
;
189 rla
= default_rla
.buf
;
190 snprintf(msg
, sizeof(msg
), "%s: %s", rla
, action
);
191 lock
= lock_any_ref_for_update(ref
->name
,
192 check_old
? ref
->old_sha1
: NULL
, 0);
194 return errno
== ENOTDIR
? STORE_REF_ERROR_DF_CONFLICT
:
195 STORE_REF_ERROR_OTHER
;
196 if (write_ref_sha1(lock
, ref
->new_sha1
, msg
) < 0)
197 return errno
== ENOTDIR
? STORE_REF_ERROR_DF_CONFLICT
:
198 STORE_REF_ERROR_OTHER
;
202 #define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
203 #define REFCOL_WIDTH 10
205 static int update_local_ref(struct ref
*ref
,
209 struct commit
*current
= NULL
, *updated
;
210 enum object_type type
;
211 struct branch
*current_branch
= branch_get(NULL
);
212 const char *pretty_ref
= prettify_refname(ref
->name
);
215 type
= sha1_object_info(ref
->new_sha1
, NULL
);
217 die("object %s not found", sha1_to_hex(ref
->new_sha1
));
219 if (!hashcmp(ref
->old_sha1
, ref
->new_sha1
)) {
221 sprintf(display
, "= %-*s %-*s -> %s", SUMMARY_WIDTH
,
222 "[up to date]", REFCOL_WIDTH
, remote
,
227 if (current_branch
&&
228 !strcmp(ref
->name
, current_branch
->name
) &&
229 !(update_head_ok
|| is_bare_repository()) &&
230 !is_null_sha1(ref
->old_sha1
)) {
232 * If this is the head, and it's not okay to update
233 * the head, and the old value of the head isn't empty...
235 sprintf(display
, "! %-*s %-*s -> %s (can't fetch in current branch)",
236 SUMMARY_WIDTH
, "[rejected]", REFCOL_WIDTH
, remote
,
241 if (!is_null_sha1(ref
->old_sha1
) &&
242 !prefixcmp(ref
->name
, "refs/tags/")) {
244 r
= s_update_ref("updating tag", ref
, 0);
245 sprintf(display
, "%c %-*s %-*s -> %s%s", r
? '!' : '-',
246 SUMMARY_WIDTH
, "[tag update]", REFCOL_WIDTH
, remote
,
247 pretty_ref
, r
? " (unable to update local ref)" : "");
251 current
= lookup_commit_reference_gently(ref
->old_sha1
, 1);
252 updated
= lookup_commit_reference_gently(ref
->new_sha1
, 1);
253 if (!current
|| !updated
) {
257 if (!strncmp(ref
->name
, "refs/tags/", 10)) {
262 msg
= "storing head";
263 what
= "[new branch]";
266 r
= s_update_ref(msg
, ref
, 0);
267 sprintf(display
, "%c %-*s %-*s -> %s%s", r
? '!' : '*',
268 SUMMARY_WIDTH
, what
, REFCOL_WIDTH
, remote
, pretty_ref
,
269 r
? " (unable to update local ref)" : "");
273 if (in_merge_bases(current
, &updated
, 1)) {
276 strcpy(quickref
, find_unique_abbrev(current
->object
.sha1
, DEFAULT_ABBREV
));
277 strcat(quickref
, "..");
278 strcat(quickref
, find_unique_abbrev(ref
->new_sha1
, DEFAULT_ABBREV
));
279 r
= s_update_ref("fast forward", ref
, 1);
280 sprintf(display
, "%c %-*s %-*s -> %s%s", r
? '!' : ' ',
281 SUMMARY_WIDTH
, quickref
, REFCOL_WIDTH
, remote
,
282 pretty_ref
, r
? " (unable to update local ref)" : "");
284 } else if (force
|| ref
->force
) {
287 strcpy(quickref
, find_unique_abbrev(current
->object
.sha1
, DEFAULT_ABBREV
));
288 strcat(quickref
, "...");
289 strcat(quickref
, find_unique_abbrev(ref
->new_sha1
, DEFAULT_ABBREV
));
290 r
= s_update_ref("forced-update", ref
, 1);
291 sprintf(display
, "%c %-*s %-*s -> %s (%s)", r
? '!' : '+',
292 SUMMARY_WIDTH
, quickref
, REFCOL_WIDTH
, remote
,
294 r
? "unable to update local ref" : "forced update");
297 sprintf(display
, "! %-*s %-*s -> %s (non fast forward)",
298 SUMMARY_WIDTH
, "[rejected]", REFCOL_WIDTH
, remote
,
304 static int store_updated_refs(const char *raw_url
, const char *remote_name
,
308 struct commit
*commit
;
309 int url_len
, i
, note_len
, shown_url
= 0, rc
= 0;
311 const char *what
, *kind
;
313 char *url
, *filename
= git_path("FETCH_HEAD");
315 fp
= fopen(filename
, "a");
317 return error("cannot open %s: %s\n", filename
, strerror(errno
));
319 url
= transport_anonymize_url(raw_url
);
320 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
321 struct ref
*ref
= NULL
;
324 ref
= xcalloc(1, sizeof(*ref
) + strlen(rm
->peer_ref
->name
) + 1);
325 strcpy(ref
->name
, rm
->peer_ref
->name
);
326 hashcpy(ref
->old_sha1
, rm
->peer_ref
->old_sha1
);
327 hashcpy(ref
->new_sha1
, rm
->old_sha1
);
328 ref
->force
= rm
->peer_ref
->force
;
331 commit
= lookup_commit_reference_gently(rm
->old_sha1
, 1);
335 if (!strcmp(rm
->name
, "HEAD")) {
339 else if (!prefixcmp(rm
->name
, "refs/heads/")) {
341 what
= rm
->name
+ 11;
343 else if (!prefixcmp(rm
->name
, "refs/tags/")) {
345 what
= rm
->name
+ 10;
347 else if (!prefixcmp(rm
->name
, "refs/remotes/")) {
348 kind
= "remote branch";
349 what
= rm
->name
+ 13;
356 url_len
= strlen(url
);
357 for (i
= url_len
- 1; url
[i
] == '/' && 0 <= i
; i
--)
360 if (4 < i
&& !strncmp(".git", url
+ i
- 3, 4))
366 note_len
+= sprintf(note
+ note_len
, "%s ",
368 note_len
+= sprintf(note
+ note_len
, "'%s' of ", what
);
370 note
[note_len
] = '\0';
371 fprintf(fp
, "%s\t%s\t%s",
372 sha1_to_hex(commit
? commit
->object
.sha1
:
374 rm
->merge
? "" : "not-for-merge",
376 for (i
= 0; i
< url_len
; ++i
)
384 rc
|= update_local_ref(ref
, what
, note
);
386 sprintf(note
, "* %-*s %-*s -> FETCH_HEAD",
387 SUMMARY_WIDTH
, *kind
? kind
: "branch",
388 REFCOL_WIDTH
, *what
? what
: "HEAD");
390 if (verbosity
>= 0 && !shown_url
) {
391 fprintf(stderr
, "From %.*s\n",
396 fprintf(stderr
, " %s\n", note
);
401 if (rc
& STORE_REF_ERROR_DF_CONFLICT
)
402 error("some local refs could not be updated; try running\n"
403 " 'git remote prune %s' to remove any old, conflicting "
404 "branches", remote_name
);
409 * We would want to bypass the object transfer altogether if
410 * everything we are going to fetch already exists and is connected
413 * The refs we are going to fetch are in ref_map. If running
415 * $ git rev-list --objects --stdin --not --all
417 * (feeding all the refs in ref_map on its standard input)
418 * does not error out, that means everything reachable from the
419 * refs we are going to fetch exists and is connected to some of
422 static int quickfetch(struct ref
*ref_map
)
424 struct child_process revlist
;
427 const char *argv
[] = {"rev-list",
428 "--quiet", "--objects", "--stdin", "--not", "--all", NULL
};
431 * If we are deepening a shallow clone we already have these
432 * objects reachable. Running rev-list here will return with
433 * a good (0) exit status and we'll bypass the fetch that we
434 * really need to perform. Claiming failure now will ensure
435 * we perform the network exchange to deepen our history.
443 memset(&revlist
, 0, sizeof(revlist
));
446 revlist
.no_stdout
= 1;
447 revlist
.no_stderr
= 1;
450 err
= start_command(&revlist
);
452 error("could not run rev-list");
457 * If rev-list --stdin encounters an unknown commit, it terminates,
458 * which will cause SIGPIPE in the write loop below.
460 sigchain_push(SIGPIPE
, SIG_IGN
);
462 for (ref
= ref_map
; ref
; ref
= ref
->next
) {
463 if (write_in_full(revlist
.in
, sha1_to_hex(ref
->old_sha1
), 40) < 0 ||
464 write_str_in_full(revlist
.in
, "\n") < 0) {
465 if (errno
!= EPIPE
&& errno
!= EINVAL
)
466 error("failed write to rev-list: %s", strerror(errno
));
472 if (close(revlist
.in
)) {
473 error("failed to close rev-list's stdin: %s", strerror(errno
));
477 sigchain_pop(SIGPIPE
);
479 return finish_command(&revlist
) || err
;
482 static int fetch_refs(struct transport
*transport
, struct ref
*ref_map
)
484 int ret
= quickfetch(ref_map
);
486 ret
= transport_fetch_refs(transport
, ref_map
);
488 ret
|= store_updated_refs(transport
->url
,
489 transport
->remote
->name
,
491 transport_unlock_pack(transport
);
495 static int add_existing(const char *refname
, const unsigned char *sha1
,
496 int flag
, void *cbdata
)
498 struct string_list
*list
= (struct string_list
*)cbdata
;
499 string_list_insert(refname
, list
);
503 static int will_fetch(struct ref
**head
, const unsigned char *sha1
)
505 struct ref
*rm
= *head
;
507 if (!hashcmp(rm
->old_sha1
, sha1
))
514 static void find_non_local_tags(struct transport
*transport
,
518 struct string_list existing_refs
= { NULL
, 0, 0, 0 };
519 struct string_list new_refs
= { NULL
, 0, 0, 1 };
522 const unsigned char *ref_sha1
;
523 const struct ref
*tag_ref
;
524 struct ref
*rm
= NULL
;
525 const struct ref
*ref
;
527 for_each_ref(add_existing
, &existing_refs
);
528 for (ref
= transport_get_remote_refs(transport
); ref
; ref
= ref
->next
) {
529 if (prefixcmp(ref
->name
, "refs/tags"))
532 ref_name
= xstrdup(ref
->name
);
533 ref_name_len
= strlen(ref_name
);
534 ref_sha1
= ref
->old_sha1
;
536 if (!strcmp(ref_name
+ ref_name_len
- 3, "^{}")) {
537 ref_name
[ref_name_len
- 3] = 0;
538 tag_ref
= transport_get_remote_refs(transport
);
540 if (!strcmp(tag_ref
->name
, ref_name
)) {
541 ref_sha1
= tag_ref
->old_sha1
;
544 tag_ref
= tag_ref
->next
;
548 if (!string_list_has_string(&existing_refs
, ref_name
) &&
549 !string_list_has_string(&new_refs
, ref_name
) &&
550 (has_sha1_file(ref
->old_sha1
) ||
551 will_fetch(head
, ref
->old_sha1
))) {
552 string_list_insert(ref_name
, &new_refs
);
554 rm
= alloc_ref(ref_name
);
555 rm
->peer_ref
= alloc_ref(ref_name
);
556 hashcpy(rm
->old_sha1
, ref_sha1
);
563 string_list_clear(&existing_refs
, 0);
564 string_list_clear(&new_refs
, 0);
567 static void check_not_current_branch(struct ref
*ref_map
)
569 struct branch
*current_branch
= branch_get(NULL
);
571 if (is_bare_repository() || !current_branch
)
574 for (; ref_map
; ref_map
= ref_map
->next
)
575 if (ref_map
->peer_ref
&& !strcmp(current_branch
->refname
,
576 ref_map
->peer_ref
->name
))
577 die("Refusing to fetch into current branch %s "
578 "of non-bare repository", current_branch
->refname
);
581 static int do_fetch(struct transport
*transport
,
582 struct refspec
*refs
, int ref_count
)
586 int autotags
= (transport
->remote
->fetch_tags
== 1);
587 if (transport
->remote
->fetch_tags
== 2 && tags
!= TAGS_UNSET
)
589 if (transport
->remote
->fetch_tags
== -1)
592 if (!transport
->get_refs_list
|| !transport
->fetch
)
593 die("Don't know how to fetch from %s", transport
->url
);
595 /* if not appending, truncate FETCH_HEAD */
597 char *filename
= git_path("FETCH_HEAD");
598 FILE *fp
= fopen(filename
, "w");
600 return error("cannot open %s: %s\n", filename
, strerror(errno
));
604 ref_map
= get_ref_map(transport
, refs
, ref_count
, tags
, &autotags
);
606 check_not_current_branch(ref_map
);
608 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
610 read_ref(rm
->peer_ref
->name
, rm
->peer_ref
->old_sha1
);
613 if (tags
== TAGS_DEFAULT
&& autotags
)
614 transport_set_option(transport
, TRANS_OPT_FOLLOWTAGS
, "1");
615 if (fetch_refs(transport
, ref_map
)) {
621 /* if neither --no-tags nor --tags was specified, do automated tag
623 if (tags
== TAGS_DEFAULT
&& autotags
) {
624 struct ref
**tail
= &ref_map
;
626 find_non_local_tags(transport
, &ref_map
, &tail
);
628 transport_set_option(transport
, TRANS_OPT_FOLLOWTAGS
, NULL
);
629 transport_set_option(transport
, TRANS_OPT_DEPTH
, "0");
630 fetch_refs(transport
, ref_map
);
638 static void set_option(const char *name
, const char *value
)
640 int r
= transport_set_option(transport
, name
, value
);
642 die("Option \"%s\" value \"%s\" is not valid for %s",
643 name
, value
, transport
->url
);
645 warning("Option \"%s\" is ignored for %s\n",
646 name
, transport
->url
);
649 static int get_one_remote_for_fetch(struct remote
*remote
, void *priv
)
651 struct string_list
*list
= priv
;
652 if (!remote
->skip_default_update
)
653 string_list_append(remote
->name
, list
);
657 struct remote_group_data
{
659 struct string_list
*list
;
662 static int get_remote_group(const char *key
, const char *value
, void *priv
)
664 struct remote_group_data
*g
= priv
;
666 if (!prefixcmp(key
, "remotes.") &&
667 !strcmp(key
+ 8, g
->name
)) {
668 /* split list by white space */
669 int space
= strcspn(value
, " \t\n");
672 string_list_append(xstrndup(value
, space
),
675 value
+= space
+ (value
[space
] != '\0');
676 space
= strcspn(value
, " \t\n");
683 static int add_remote_or_group(const char *name
, struct string_list
*list
)
685 int prev_nr
= list
->nr
;
686 struct remote_group_data g
= { name
, list
};
688 git_config(get_remote_group
, &g
);
689 if (list
->nr
== prev_nr
) {
690 struct remote
*remote
;
691 if (!remote_is_configured(name
))
693 remote
= remote_get(name
);
694 string_list_append(remote
->name
, list
);
699 static int fetch_multiple(struct string_list
*list
)
702 const char *argv
[] = { "fetch", NULL
, NULL
, NULL
, NULL
};
709 else if (verbosity
< 0)
712 for (i
= 0; i
< list
->nr
; i
++) {
713 const char *name
= list
->items
[i
].string
;
716 printf("Fetching %s\n", name
);
717 if (run_command_v_opt(argv
, RUN_GIT_CMD
)) {
718 error("Could not fetch %s", name
);
726 static int fetch_one(struct remote
*remote
, int argc
, const char **argv
)
729 static const char **refs
= NULL
;
734 die("Where do you want to fetch from today?");
736 transport
= transport_get(remote
, remote
->url
[0]);
738 transport
->verbose
= 1;
740 transport
->verbose
= -1;
742 set_option(TRANS_OPT_UPLOADPACK
, upload_pack
);
744 set_option(TRANS_OPT_KEEP
, "yes");
746 set_option(TRANS_OPT_DEPTH
, depth
);
750 refs
= xcalloc(argc
+ 1, sizeof(const char *));
751 for (i
= 0; i
< argc
; i
++) {
752 if (!strcmp(argv
[i
], "tag")) {
756 die("You need to specify a tag name.");
757 ref
= xmalloc(strlen(argv
[i
]) * 2 + 22);
758 strcpy(ref
, "refs/tags/");
759 strcat(ref
, argv
[i
]);
760 strcat(ref
, ":refs/tags/");
761 strcat(ref
, argv
[i
]);
770 sigchain_push_common(unlock_pack_on_signal
);
772 exit_code
= do_fetch(transport
,
773 parse_fetch_refspec(ref_nr
, refs
), ref_nr
);
774 transport_disconnect(transport
);
779 int cmd_fetch(int argc
, const char **argv
, const char *prefix
)
782 struct string_list list
= { NULL
, 0, 0, 0 };
783 struct remote
*remote
;
786 /* Record the command line for the reflog */
787 strbuf_addstr(&default_rla
, "fetch");
788 for (i
= 1; i
< argc
; i
++)
789 strbuf_addf(&default_rla
, " %s", argv
[i
]);
791 argc
= parse_options(argc
, argv
, prefix
,
792 builtin_fetch_options
, builtin_fetch_usage
, 0);
796 die("fetch --all does not take a repository argument");
798 die("fetch --all does not make sense with refspecs");
799 (void) for_each_remote(get_one_remote_for_fetch
, &list
);
800 result
= fetch_multiple(&list
);
801 } else if (argc
== 0) {
802 /* No arguments -- use default remote */
803 remote
= remote_get(NULL
);
804 result
= fetch_one(remote
, argc
, argv
);
805 } else if (multiple
) {
806 /* All arguments are assumed to be remotes or groups */
807 for (i
= 0; i
< argc
; i
++)
808 if (!add_remote_or_group(argv
[i
], &list
))
809 die("No such remote or remote group: %s", argv
[i
]);
810 result
= fetch_multiple(&list
);
812 /* Single remote or group */
813 (void) add_remote_or_group(argv
[0], &list
);
815 /* More than one remote */
817 die("Fetching a group and specifying refspecs does not make sense");
818 result
= fetch_multiple(&list
);
820 /* Zero or one remotes */
821 remote
= remote_get(argv
[0]);
822 result
= fetch_one(remote
, argc
-1, argv
+1);
826 /* All names were strdup()ed or strndup()ed */
827 list
.strdup_strings
= 1;
828 string_list_clear(&list
, 0);