8 #include "string-list.h"
10 #include "transport.h"
11 #include "run-command.h"
12 #include "parse-options.h"
14 #include "transport.h"
15 #include "submodule.h"
16 #include "connected.h"
17 #include "argv-array.h"
19 static const char * const builtin_fetch_usage
[] = {
20 N_("git fetch [<options>] [<repository> [<refspec>...]]"),
21 N_("git fetch [<options>] <group>"),
22 N_("git fetch --multiple [<options>] [(<repository> | <group>)...]"),
23 N_("git fetch --all [<options>]"),
33 static int fetch_prune_config
= -1; /* unspecified */
34 static int prune
= -1; /* unspecified */
35 #define PRUNE_BY_DEFAULT 0 /* do we prune by default? */
37 static int all
, append
, dry_run
, force
, keep
, multiple
, update_head_ok
, verbosity
;
38 static int progress
= -1, recurse_submodules
= RECURSE_SUBMODULES_DEFAULT
;
39 static int tags
= TAGS_DEFAULT
, unshallow
, update_shallow
;
40 static const char *depth
;
41 static const char *upload_pack
;
42 static struct strbuf default_rla
= STRBUF_INIT
;
43 static struct transport
*gtransport
;
44 static struct transport
*gsecondary
;
45 static const char *submodule_prefix
= "";
46 static const char *recurse_submodules_default
;
47 static int shown_url
= 0;
48 static int refmap_alloc
, refmap_nr
;
49 static const char **refmap_array
;
51 static int option_parse_recurse_submodules(const struct option
*opt
,
52 const char *arg
, int unset
)
55 recurse_submodules
= RECURSE_SUBMODULES_OFF
;
58 recurse_submodules
= parse_fetch_recurse_submodules_arg(opt
->long_name
, arg
);
60 recurse_submodules
= RECURSE_SUBMODULES_ON
;
65 static int git_fetch_config(const char *k
, const char *v
, void *cb
)
67 if (!strcmp(k
, "fetch.prune")) {
68 fetch_prune_config
= git_config_bool(k
, v
);
74 static int parse_refmap_arg(const struct option
*opt
, const char *arg
, int unset
)
76 ALLOC_GROW(refmap_array
, refmap_nr
+ 1, refmap_alloc
);
79 * "git fetch --refmap='' origin foo"
80 * can be used to tell the command not to store anywhere
83 refmap_array
[refmap_nr
++] = arg
;
87 static struct option builtin_fetch_options
[] = {
88 OPT__VERBOSITY(&verbosity
),
89 OPT_BOOL(0, "all", &all
,
90 N_("fetch from all remotes")),
91 OPT_BOOL('a', "append", &append
,
92 N_("append to .git/FETCH_HEAD instead of overwriting")),
93 OPT_STRING(0, "upload-pack", &upload_pack
, N_("path"),
94 N_("path to upload pack on remote end")),
95 OPT__FORCE(&force
, N_("force overwrite of local branch")),
96 OPT_BOOL('m', "multiple", &multiple
,
97 N_("fetch from multiple remotes")),
98 OPT_SET_INT('t', "tags", &tags
,
99 N_("fetch all tags and associated objects"), TAGS_SET
),
100 OPT_SET_INT('n', NULL
, &tags
,
101 N_("do not fetch all tags (--no-tags)"), TAGS_UNSET
),
102 OPT_BOOL('p', "prune", &prune
,
103 N_("prune remote-tracking branches no longer on remote")),
104 { OPTION_CALLBACK
, 0, "recurse-submodules", NULL
, N_("on-demand"),
105 N_("control recursive fetching of submodules"),
106 PARSE_OPT_OPTARG
, option_parse_recurse_submodules
},
107 OPT_BOOL(0, "dry-run", &dry_run
,
109 OPT_BOOL('k', "keep", &keep
, N_("keep downloaded pack")),
110 OPT_BOOL('u', "update-head-ok", &update_head_ok
,
111 N_("allow updating of HEAD ref")),
112 OPT_BOOL(0, "progress", &progress
, N_("force progress reporting")),
113 OPT_STRING(0, "depth", &depth
, N_("depth"),
114 N_("deepen history of shallow clone")),
115 { OPTION_SET_INT
, 0, "unshallow", &unshallow
, NULL
,
116 N_("convert to a complete repository"),
117 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
, NULL
, 1 },
118 { OPTION_STRING
, 0, "submodule-prefix", &submodule_prefix
, N_("dir"),
119 N_("prepend this to submodule path output"), PARSE_OPT_HIDDEN
},
120 { OPTION_STRING
, 0, "recurse-submodules-default",
121 &recurse_submodules_default
, NULL
,
122 N_("default mode for recursion"), PARSE_OPT_HIDDEN
},
123 OPT_BOOL(0, "update-shallow", &update_shallow
,
124 N_("accept refs that update .git/shallow")),
125 { OPTION_CALLBACK
, 0, "refmap", NULL
, N_("refmap"),
126 N_("specify fetch refmap"), PARSE_OPT_NONEG
, parse_refmap_arg
},
130 static void unlock_pack(void)
133 transport_unlock_pack(gtransport
);
135 transport_unlock_pack(gsecondary
);
138 static void unlock_pack_on_signal(int signo
)
145 static void add_merge_config(struct ref
**head
,
146 const struct ref
*remote_refs
,
147 struct branch
*branch
,
152 for (i
= 0; i
< branch
->merge_nr
; i
++) {
153 struct ref
*rm
, **old_tail
= *tail
;
154 struct refspec refspec
;
156 for (rm
= *head
; rm
; rm
= rm
->next
) {
157 if (branch_merge_matches(branch
, i
, rm
->name
)) {
158 rm
->fetch_head_status
= FETCH_HEAD_MERGE
;
166 * Not fetched to a remote-tracking branch? We need to fetch
167 * it anyway to allow this branch's "branch.$name.merge"
168 * to be honored by 'git pull', but we do not have to
169 * fail if branch.$name.merge is misconfigured to point
170 * at a nonexisting branch. If we were indeed called by
171 * 'git pull', it will notice the misconfiguration because
172 * there is no entry in the resulting FETCH_HEAD marked
175 memset(&refspec
, 0, sizeof(refspec
));
176 refspec
.src
= branch
->merge
[i
]->src
;
177 get_fetch_map(remote_refs
, &refspec
, tail
, 1);
178 for (rm
= *old_tail
; rm
; rm
= rm
->next
)
179 rm
->fetch_head_status
= FETCH_HEAD_MERGE
;
183 static int add_existing(const char *refname
, const unsigned char *sha1
,
184 int flag
, void *cbdata
)
186 struct string_list
*list
= (struct string_list
*)cbdata
;
187 struct string_list_item
*item
= string_list_insert(list
, refname
);
188 item
->util
= xmalloc(20);
189 hashcpy(item
->util
, sha1
);
193 static int will_fetch(struct ref
**head
, const unsigned char *sha1
)
195 struct ref
*rm
= *head
;
197 if (!hashcmp(rm
->old_sha1
, sha1
))
204 static void find_non_local_tags(struct transport
*transport
,
208 struct string_list existing_refs
= STRING_LIST_INIT_DUP
;
209 struct string_list remote_refs
= STRING_LIST_INIT_NODUP
;
210 const struct ref
*ref
;
211 struct string_list_item
*item
= NULL
;
213 for_each_ref(add_existing
, &existing_refs
);
214 for (ref
= transport_get_remote_refs(transport
); ref
; ref
= ref
->next
) {
215 if (!starts_with(ref
->name
, "refs/tags/"))
219 * The peeled ref always follows the matching base
220 * ref, so if we see a peeled ref that we don't want
221 * to fetch then we can mark the ref entry in the list
222 * as one to ignore by setting util to NULL.
224 if (ends_with(ref
->name
, "^{}")) {
225 if (item
&& !has_sha1_file(ref
->old_sha1
) &&
226 !will_fetch(head
, ref
->old_sha1
) &&
227 !has_sha1_file(item
->util
) &&
228 !will_fetch(head
, item
->util
))
235 * If item is non-NULL here, then we previously saw a
236 * ref not followed by a peeled reference, so we need
237 * to check if it is a lightweight tag that we want to
240 if (item
&& !has_sha1_file(item
->util
) &&
241 !will_fetch(head
, item
->util
))
246 /* skip duplicates and refs that we already have */
247 if (string_list_has_string(&remote_refs
, ref
->name
) ||
248 string_list_has_string(&existing_refs
, ref
->name
))
251 item
= string_list_insert(&remote_refs
, ref
->name
);
252 item
->util
= (void *)ref
->old_sha1
;
254 string_list_clear(&existing_refs
, 1);
257 * We may have a final lightweight tag that needs to be
258 * checked to see if it needs fetching.
260 if (item
&& !has_sha1_file(item
->util
) &&
261 !will_fetch(head
, item
->util
))
265 * For all the tags in the remote_refs string list,
266 * add them to the list of refs to be fetched
268 for_each_string_list_item(item
, &remote_refs
) {
269 /* Unless we have already decided to ignore this item... */
272 struct ref
*rm
= alloc_ref(item
->string
);
273 rm
->peer_ref
= alloc_ref(item
->string
);
274 hashcpy(rm
->old_sha1
, item
->util
);
280 string_list_clear(&remote_refs
, 0);
283 static struct ref
*get_ref_map(struct transport
*transport
,
284 struct refspec
*refspecs
, int refspec_count
,
285 int tags
, int *autotags
)
289 struct ref
*ref_map
= NULL
;
290 struct ref
**tail
= &ref_map
;
292 /* opportunistically-updated references: */
293 struct ref
*orefs
= NULL
, **oref_tail
= &orefs
;
295 const struct ref
*remote_refs
= transport_get_remote_refs(transport
);
298 struct refspec
*fetch_refspec
;
299 int fetch_refspec_nr
;
301 for (i
= 0; i
< refspec_count
; i
++) {
302 get_fetch_map(remote_refs
, &refspecs
[i
], &tail
, 0);
303 if (refspecs
[i
].dst
&& refspecs
[i
].dst
[0])
306 /* Merge everything on the command line (but not --tags) */
307 for (rm
= ref_map
; rm
; rm
= rm
->next
)
308 rm
->fetch_head_status
= FETCH_HEAD_MERGE
;
311 * For any refs that we happen to be fetching via
312 * command-line arguments, the destination ref might
313 * have been missing or have been different than the
314 * remote-tracking ref that would be derived from the
315 * configured refspec. In these cases, we want to
316 * take the opportunity to update their configured
317 * remote-tracking reference. However, we do not want
318 * to mention these entries in FETCH_HEAD at all, as
319 * they would simply be duplicates of existing
320 * entries, so we set them FETCH_HEAD_IGNORE below.
322 * We compute these entries now, based only on the
323 * refspecs specified on the command line. But we add
324 * them to the list following the refspecs resulting
325 * from the tags option so that one of the latter,
326 * which has FETCH_HEAD_NOT_FOR_MERGE, is not removed
327 * by ref_remove_duplicates() in favor of one of these
328 * opportunistic entries with FETCH_HEAD_IGNORE.
331 fetch_refspec
= parse_fetch_refspec(refmap_nr
, refmap_array
);
332 fetch_refspec_nr
= refmap_nr
;
334 fetch_refspec
= transport
->remote
->fetch
;
335 fetch_refspec_nr
= transport
->remote
->fetch_refspec_nr
;
338 for (i
= 0; i
< fetch_refspec_nr
; i
++)
339 get_fetch_map(ref_map
, &fetch_refspec
[i
], &oref_tail
, 1);
341 if (tags
== TAGS_SET
)
342 get_fetch_map(remote_refs
, tag_refspec
, &tail
, 0);
343 } else if (refmap_array
) {
344 die("--refmap option is only meaningful with command-line refspec(s).");
346 /* Use the defaults */
347 struct remote
*remote
= transport
->remote
;
348 struct branch
*branch
= branch_get(NULL
);
349 int has_merge
= branch_has_merge_config(branch
);
351 (remote
->fetch_refspec_nr
||
352 /* Note: has_merge implies non-NULL branch->remote_name */
353 (has_merge
&& !strcmp(branch
->remote_name
, remote
->name
)))) {
354 for (i
= 0; i
< remote
->fetch_refspec_nr
; i
++) {
355 get_fetch_map(remote_refs
, &remote
->fetch
[i
], &tail
, 0);
356 if (remote
->fetch
[i
].dst
&&
357 remote
->fetch
[i
].dst
[0])
359 if (!i
&& !has_merge
&& ref_map
&&
360 !remote
->fetch
[0].pattern
)
361 ref_map
->fetch_head_status
= FETCH_HEAD_MERGE
;
364 * if the remote we're fetching from is the same
365 * as given in branch.<name>.remote, we add the
366 * ref given in branch.<name>.merge, too.
368 * Note: has_merge implies non-NULL branch->remote_name
371 !strcmp(branch
->remote_name
, remote
->name
))
372 add_merge_config(&ref_map
, remote_refs
, branch
, &tail
);
374 ref_map
= get_remote_ref(remote_refs
, "HEAD");
376 die(_("Couldn't find remote ref HEAD"));
377 ref_map
->fetch_head_status
= FETCH_HEAD_MERGE
;
378 tail
= &ref_map
->next
;
382 if (tags
== TAGS_SET
)
383 /* also fetch all tags */
384 get_fetch_map(remote_refs
, tag_refspec
, &tail
, 0);
385 else if (tags
== TAGS_DEFAULT
&& *autotags
)
386 find_non_local_tags(transport
, &ref_map
, &tail
);
388 /* Now append any refs to be updated opportunistically: */
390 for (rm
= orefs
; rm
; rm
= rm
->next
) {
391 rm
->fetch_head_status
= FETCH_HEAD_IGNORE
;
395 return ref_remove_duplicates(ref_map
);
398 #define STORE_REF_ERROR_OTHER 1
399 #define STORE_REF_ERROR_DF_CONFLICT 2
401 static int s_update_ref(const char *action
,
406 char *rla
= getenv("GIT_REFLOG_ACTION");
407 static struct ref_lock
*lock
;
412 rla
= default_rla
.buf
;
413 snprintf(msg
, sizeof(msg
), "%s: %s", rla
, action
);
414 lock
= lock_any_ref_for_update(ref
->name
,
415 check_old
? ref
->old_sha1
: NULL
,
418 return errno
== ENOTDIR
? STORE_REF_ERROR_DF_CONFLICT
:
419 STORE_REF_ERROR_OTHER
;
420 if (write_ref_sha1(lock
, ref
->new_sha1
, msg
) < 0)
421 return errno
== ENOTDIR
? STORE_REF_ERROR_DF_CONFLICT
:
422 STORE_REF_ERROR_OTHER
;
426 #define REFCOL_WIDTH 10
428 static int update_local_ref(struct ref
*ref
,
430 const struct ref
*remote_ref
,
431 struct strbuf
*display
)
433 struct commit
*current
= NULL
, *updated
;
434 enum object_type type
;
435 struct branch
*current_branch
= branch_get(NULL
);
436 const char *pretty_ref
= prettify_refname(ref
->name
);
438 type
= sha1_object_info(ref
->new_sha1
, NULL
);
440 die(_("object %s not found"), sha1_to_hex(ref
->new_sha1
));
442 if (!hashcmp(ref
->old_sha1
, ref
->new_sha1
)) {
444 strbuf_addf(display
, "= %-*s %-*s -> %s",
445 TRANSPORT_SUMMARY(_("[up to date]")),
446 REFCOL_WIDTH
, remote
, pretty_ref
);
450 if (current_branch
&&
451 !strcmp(ref
->name
, current_branch
->name
) &&
452 !(update_head_ok
|| is_bare_repository()) &&
453 !is_null_sha1(ref
->old_sha1
)) {
455 * If this is the head, and it's not okay to update
456 * the head, and the old value of the head isn't empty...
459 _("! %-*s %-*s -> %s (can't fetch in current branch)"),
460 TRANSPORT_SUMMARY(_("[rejected]")),
461 REFCOL_WIDTH
, remote
, pretty_ref
);
465 if (!is_null_sha1(ref
->old_sha1
) &&
466 starts_with(ref
->name
, "refs/tags/")) {
468 r
= s_update_ref("updating tag", ref
, 0);
469 strbuf_addf(display
, "%c %-*s %-*s -> %s%s",
471 TRANSPORT_SUMMARY(_("[tag update]")),
472 REFCOL_WIDTH
, remote
, pretty_ref
,
473 r
? _(" (unable to update local ref)") : "");
477 current
= lookup_commit_reference_gently(ref
->old_sha1
, 1);
478 updated
= lookup_commit_reference_gently(ref
->new_sha1
, 1);
479 if (!current
|| !updated
) {
484 * Nicely describe the new ref we're fetching.
485 * Base this on the remote's ref name, as it's
486 * more likely to follow a standard layout.
488 const char *name
= remote_ref
? remote_ref
->name
: "";
489 if (starts_with(name
, "refs/tags/")) {
491 what
= _("[new tag]");
492 } else if (starts_with(name
, "refs/heads/")) {
493 msg
= "storing head";
494 what
= _("[new branch]");
497 what
= _("[new ref]");
500 if ((recurse_submodules
!= RECURSE_SUBMODULES_OFF
) &&
501 (recurse_submodules
!= RECURSE_SUBMODULES_ON
))
502 check_for_new_submodule_commits(ref
->new_sha1
);
503 r
= s_update_ref(msg
, ref
, 0);
504 strbuf_addf(display
, "%c %-*s %-*s -> %s%s",
506 TRANSPORT_SUMMARY(what
),
507 REFCOL_WIDTH
, remote
, pretty_ref
,
508 r
? _(" (unable to update local ref)") : "");
512 if (in_merge_bases(current
, updated
)) {
515 strcpy(quickref
, find_unique_abbrev(current
->object
.sha1
, DEFAULT_ABBREV
));
516 strcat(quickref
, "..");
517 strcat(quickref
, find_unique_abbrev(ref
->new_sha1
, DEFAULT_ABBREV
));
518 if ((recurse_submodules
!= RECURSE_SUBMODULES_OFF
) &&
519 (recurse_submodules
!= RECURSE_SUBMODULES_ON
))
520 check_for_new_submodule_commits(ref
->new_sha1
);
521 r
= s_update_ref("fast-forward", ref
, 1);
522 strbuf_addf(display
, "%c %-*s %-*s -> %s%s",
524 TRANSPORT_SUMMARY_WIDTH
, quickref
,
525 REFCOL_WIDTH
, remote
, pretty_ref
,
526 r
? _(" (unable to update local ref)") : "");
528 } else if (force
|| ref
->force
) {
531 strcpy(quickref
, find_unique_abbrev(current
->object
.sha1
, DEFAULT_ABBREV
));
532 strcat(quickref
, "...");
533 strcat(quickref
, find_unique_abbrev(ref
->new_sha1
, DEFAULT_ABBREV
));
534 if ((recurse_submodules
!= RECURSE_SUBMODULES_OFF
) &&
535 (recurse_submodules
!= RECURSE_SUBMODULES_ON
))
536 check_for_new_submodule_commits(ref
->new_sha1
);
537 r
= s_update_ref("forced-update", ref
, 1);
538 strbuf_addf(display
, "%c %-*s %-*s -> %s (%s)",
540 TRANSPORT_SUMMARY_WIDTH
, quickref
,
541 REFCOL_WIDTH
, remote
, pretty_ref
,
542 r
? _("unable to update local ref") : _("forced update"));
545 strbuf_addf(display
, "! %-*s %-*s -> %s %s",
546 TRANSPORT_SUMMARY(_("[rejected]")),
547 REFCOL_WIDTH
, remote
, pretty_ref
,
548 _("(non-fast-forward)"));
553 static int iterate_ref_map(void *cb_data
, unsigned char sha1
[20])
555 struct ref
**rm
= cb_data
;
556 struct ref
*ref
= *rm
;
558 while (ref
&& ref
->status
== REF_STATUS_REJECT_SHALLOW
)
561 return -1; /* end of the list */
563 hashcpy(sha1
, ref
->old_sha1
);
567 static int store_updated_refs(const char *raw_url
, const char *remote_name
,
571 struct commit
*commit
;
572 int url_len
, i
, rc
= 0;
573 struct strbuf note
= STRBUF_INIT
;
574 const char *what
, *kind
;
576 char *url
, *filename
= dry_run
? "/dev/null" : git_path("FETCH_HEAD");
579 fp
= fopen(filename
, "a");
581 return error(_("cannot open %s: %s\n"), filename
, strerror(errno
));
584 url
= transport_anonymize_url(raw_url
);
586 url
= xstrdup("foreign");
589 if (check_everything_connected(iterate_ref_map
, 0, &rm
)) {
590 rc
= error(_("%s did not send all necessary objects\n"), url
);
595 * We do a pass for each fetch_head_status type in their enum order, so
596 * merged entries are written before not-for-merge. That lets readers
597 * use FETCH_HEAD as a refname to refer to the ref to be merged.
599 for (want_status
= FETCH_HEAD_MERGE
;
600 want_status
<= FETCH_HEAD_IGNORE
;
602 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
603 struct ref
*ref
= NULL
;
604 const char *merge_status_marker
= "";
606 if (rm
->status
== REF_STATUS_REJECT_SHALLOW
) {
607 if (want_status
== FETCH_HEAD_MERGE
)
608 warning(_("reject %s because shallow roots are not allowed to be updated"),
609 rm
->peer_ref
? rm
->peer_ref
->name
: rm
->name
);
613 commit
= lookup_commit_reference_gently(rm
->old_sha1
, 1);
615 rm
->fetch_head_status
= FETCH_HEAD_NOT_FOR_MERGE
;
617 if (rm
->fetch_head_status
!= want_status
)
621 ref
= xcalloc(1, sizeof(*ref
) + strlen(rm
->peer_ref
->name
) + 1);
622 strcpy(ref
->name
, rm
->peer_ref
->name
);
623 hashcpy(ref
->old_sha1
, rm
->peer_ref
->old_sha1
);
624 hashcpy(ref
->new_sha1
, rm
->old_sha1
);
625 ref
->force
= rm
->peer_ref
->force
;
629 if (!strcmp(rm
->name
, "HEAD")) {
633 else if (starts_with(rm
->name
, "refs/heads/")) {
635 what
= rm
->name
+ 11;
637 else if (starts_with(rm
->name
, "refs/tags/")) {
639 what
= rm
->name
+ 10;
641 else if (starts_with(rm
->name
, "refs/remotes/")) {
642 kind
= "remote-tracking branch";
643 what
= rm
->name
+ 13;
650 url_len
= strlen(url
);
651 for (i
= url_len
- 1; url
[i
] == '/' && 0 <= i
; i
--)
654 if (4 < i
&& !strncmp(".git", url
+ i
- 3, 4))
660 strbuf_addf(¬e
, "%s ", kind
);
661 strbuf_addf(¬e
, "'%s' of ", what
);
663 switch (rm
->fetch_head_status
) {
664 case FETCH_HEAD_NOT_FOR_MERGE
:
665 merge_status_marker
= "not-for-merge";
667 case FETCH_HEAD_MERGE
:
668 fprintf(fp
, "%s\t%s\t%s",
669 sha1_to_hex(rm
->old_sha1
),
672 for (i
= 0; i
< url_len
; ++i
)
680 /* do not write anything to FETCH_HEAD */
686 rc
|= update_local_ref(ref
, what
, rm
, ¬e
);
689 strbuf_addf(¬e
, "* %-*s %-*s -> FETCH_HEAD",
690 TRANSPORT_SUMMARY_WIDTH
,
691 *kind
? kind
: "branch",
693 *what
? what
: "HEAD");
695 if (verbosity
>= 0 && !shown_url
) {
696 fprintf(stderr
, _("From %.*s\n"),
701 fprintf(stderr
, " %s\n", note
.buf
);
706 if (rc
& STORE_REF_ERROR_DF_CONFLICT
)
707 error(_("some local refs could not be updated; try running\n"
708 " 'git remote prune %s' to remove any old, conflicting "
709 "branches"), remote_name
);
712 strbuf_release(¬e
);
719 * We would want to bypass the object transfer altogether if
720 * everything we are going to fetch already exists and is connected
723 static int quickfetch(struct ref
*ref_map
)
725 struct ref
*rm
= ref_map
;
728 * If we are deepening a shallow clone we already have these
729 * objects reachable. Running rev-list here will return with
730 * a good (0) exit status and we'll bypass the fetch that we
731 * really need to perform. Claiming failure now will ensure
732 * we perform the network exchange to deepen our history.
736 return check_everything_connected(iterate_ref_map
, 1, &rm
);
739 static int fetch_refs(struct transport
*transport
, struct ref
*ref_map
)
741 int ret
= quickfetch(ref_map
);
743 ret
= transport_fetch_refs(transport
, ref_map
);
745 ret
|= store_updated_refs(transport
->url
,
746 transport
->remote
->name
,
748 transport_unlock_pack(transport
);
752 static int prune_refs(struct refspec
*refs
, int ref_count
, struct ref
*ref_map
,
755 int url_len
, i
, result
= 0;
756 struct ref
*ref
, *stale_refs
= get_stale_heads(refs
, ref_count
, ref_map
);
758 const char *dangling_msg
= dry_run
759 ? _(" (%s will become dangling)")
760 : _(" (%s has become dangling)");
763 url
= transport_anonymize_url(raw_url
);
765 url
= xstrdup("foreign");
767 url_len
= strlen(url
);
768 for (i
= url_len
- 1; url
[i
] == '/' && 0 <= i
; i
--)
772 if (4 < i
&& !strncmp(".git", url
+ i
- 3, 4))
775 for (ref
= stale_refs
; ref
; ref
= ref
->next
) {
777 result
|= delete_ref(ref
->name
, NULL
, 0);
778 if (verbosity
>= 0 && !shown_url
) {
779 fprintf(stderr
, _("From %.*s\n"), url_len
, url
);
782 if (verbosity
>= 0) {
783 fprintf(stderr
, " x %-*s %-*s -> %s\n",
784 TRANSPORT_SUMMARY(_("[deleted]")),
785 REFCOL_WIDTH
, _("(none)"), prettify_refname(ref
->name
));
786 warn_dangling_symref(stderr
, dangling_msg
, ref
->name
);
790 free_refs(stale_refs
);
794 static void check_not_current_branch(struct ref
*ref_map
)
796 struct branch
*current_branch
= branch_get(NULL
);
798 if (is_bare_repository() || !current_branch
)
801 for (; ref_map
; ref_map
= ref_map
->next
)
802 if (ref_map
->peer_ref
&& !strcmp(current_branch
->refname
,
803 ref_map
->peer_ref
->name
))
804 die(_("Refusing to fetch into current branch %s "
805 "of non-bare repository"), current_branch
->refname
);
808 static int truncate_fetch_head(void)
810 char *filename
= git_path("FETCH_HEAD");
811 FILE *fp
= fopen(filename
, "w");
814 return error(_("cannot open %s: %s\n"), filename
, strerror(errno
));
819 static void set_option(struct transport
*transport
, const char *name
, const char *value
)
821 int r
= transport_set_option(transport
, name
, value
);
823 die(_("Option \"%s\" value \"%s\" is not valid for %s"),
824 name
, value
, transport
->url
);
826 warning(_("Option \"%s\" is ignored for %s\n"),
827 name
, transport
->url
);
830 static struct transport
*prepare_transport(struct remote
*remote
)
832 struct transport
*transport
;
833 transport
= transport_get(remote
, NULL
);
834 transport_set_verbosity(transport
, verbosity
, progress
);
836 set_option(transport
, TRANS_OPT_UPLOADPACK
, upload_pack
);
838 set_option(transport
, TRANS_OPT_KEEP
, "yes");
840 set_option(transport
, TRANS_OPT_DEPTH
, depth
);
842 set_option(transport
, TRANS_OPT_UPDATE_SHALLOW
, "yes");
846 static void backfill_tags(struct transport
*transport
, struct ref
*ref_map
)
848 if (transport
->cannot_reuse
) {
849 gsecondary
= prepare_transport(transport
->remote
);
850 transport
= gsecondary
;
853 transport_set_option(transport
, TRANS_OPT_FOLLOWTAGS
, NULL
);
854 transport_set_option(transport
, TRANS_OPT_DEPTH
, "0");
855 fetch_refs(transport
, ref_map
);
858 transport_disconnect(gsecondary
);
863 static int do_fetch(struct transport
*transport
,
864 struct refspec
*refs
, int ref_count
)
866 struct string_list existing_refs
= STRING_LIST_INIT_DUP
;
869 int autotags
= (transport
->remote
->fetch_tags
== 1);
872 for_each_ref(add_existing
, &existing_refs
);
874 if (tags
== TAGS_DEFAULT
) {
875 if (transport
->remote
->fetch_tags
== 2)
877 if (transport
->remote
->fetch_tags
== -1)
881 if (!transport
->get_refs_list
|| !transport
->fetch
)
882 die(_("Don't know how to fetch from %s"), transport
->url
);
884 /* if not appending, truncate FETCH_HEAD */
885 if (!append
&& !dry_run
) {
886 retcode
= truncate_fetch_head();
891 ref_map
= get_ref_map(transport
, refs
, ref_count
, tags
, &autotags
);
893 check_not_current_branch(ref_map
);
895 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
897 struct string_list_item
*peer_item
=
898 string_list_lookup(&existing_refs
,
901 hashcpy(rm
->peer_ref
->old_sha1
,
906 if (tags
== TAGS_DEFAULT
&& autotags
)
907 transport_set_option(transport
, TRANS_OPT_FOLLOWTAGS
, "1");
910 * We only prune based on refspecs specified
911 * explicitly (via command line or configuration); we
912 * don't care whether --tags was specified.
915 prune_refs(refs
, ref_count
, ref_map
, transport
->url
);
917 prune_refs(transport
->remote
->fetch
,
918 transport
->remote
->fetch_refspec_nr
,
923 if (fetch_refs(transport
, ref_map
)) {
930 /* if neither --no-tags nor --tags was specified, do automated tag
932 if (tags
== TAGS_DEFAULT
&& autotags
) {
933 struct ref
**tail
= &ref_map
;
935 find_non_local_tags(transport
, &ref_map
, &tail
);
937 backfill_tags(transport
, ref_map
);
942 string_list_clear(&existing_refs
, 1);
946 static int get_one_remote_for_fetch(struct remote
*remote
, void *priv
)
948 struct string_list
*list
= priv
;
949 if (!remote
->skip_default_update
)
950 string_list_append(list
, remote
->name
);
954 struct remote_group_data
{
956 struct string_list
*list
;
959 static int get_remote_group(const char *key
, const char *value
, void *priv
)
961 struct remote_group_data
*g
= priv
;
963 if (starts_with(key
, "remotes.") &&
964 !strcmp(key
+ 8, g
->name
)) {
965 /* split list by white space */
966 int space
= strcspn(value
, " \t\n");
969 string_list_append(g
->list
,
970 xstrndup(value
, space
));
972 value
+= space
+ (value
[space
] != '\0');
973 space
= strcspn(value
, " \t\n");
980 static int add_remote_or_group(const char *name
, struct string_list
*list
)
982 int prev_nr
= list
->nr
;
983 struct remote_group_data g
;
984 g
.name
= name
; g
.list
= list
;
986 git_config(get_remote_group
, &g
);
987 if (list
->nr
== prev_nr
) {
988 struct remote
*remote
;
989 if (!remote_is_configured(name
))
991 remote
= remote_get(name
);
992 string_list_append(list
, remote
->name
);
997 static void add_options_to_argv(struct argv_array
*argv
)
1000 argv_array_push(argv
, "--dry-run");
1002 argv_array_push(argv
, prune
? "--prune" : "--no-prune");
1004 argv_array_push(argv
, "--update-head-ok");
1006 argv_array_push(argv
, "--force");
1008 argv_array_push(argv
, "--keep");
1009 if (recurse_submodules
== RECURSE_SUBMODULES_ON
)
1010 argv_array_push(argv
, "--recurse-submodules");
1011 else if (recurse_submodules
== RECURSE_SUBMODULES_ON_DEMAND
)
1012 argv_array_push(argv
, "--recurse-submodules=on-demand");
1013 if (tags
== TAGS_SET
)
1014 argv_array_push(argv
, "--tags");
1015 else if (tags
== TAGS_UNSET
)
1016 argv_array_push(argv
, "--no-tags");
1018 argv_array_push(argv
, "-v");
1020 argv_array_push(argv
, "-v");
1021 else if (verbosity
< 0)
1022 argv_array_push(argv
, "-q");
1026 static int fetch_multiple(struct string_list
*list
)
1029 struct argv_array argv
= ARGV_ARRAY_INIT
;
1031 if (!append
&& !dry_run
) {
1032 int errcode
= truncate_fetch_head();
1037 argv_array_pushl(&argv
, "fetch", "--append", NULL
);
1038 add_options_to_argv(&argv
);
1040 for (i
= 0; i
< list
->nr
; i
++) {
1041 const char *name
= list
->items
[i
].string
;
1042 argv_array_push(&argv
, name
);
1044 printf(_("Fetching %s\n"), name
);
1045 if (run_command_v_opt(argv
.argv
, RUN_GIT_CMD
)) {
1046 error(_("Could not fetch %s"), name
);
1049 argv_array_pop(&argv
);
1052 argv_array_clear(&argv
);
1056 static int fetch_one(struct remote
*remote
, int argc
, const char **argv
)
1058 static const char **refs
= NULL
;
1059 struct refspec
*refspec
;
1064 die(_("No remote repository specified. Please, specify either a URL or a\n"
1065 "remote name from which new revisions should be fetched."));
1067 gtransport
= prepare_transport(remote
);
1070 /* no command line request */
1071 if (0 <= gtransport
->remote
->prune
)
1072 prune
= gtransport
->remote
->prune
;
1073 else if (0 <= fetch_prune_config
)
1074 prune
= fetch_prune_config
;
1076 prune
= PRUNE_BY_DEFAULT
;
1082 refs
= xcalloc(argc
+ 1, sizeof(const char *));
1083 for (i
= 0; i
< argc
; i
++) {
1084 if (!strcmp(argv
[i
], "tag")) {
1087 die(_("You need to specify a tag name."));
1088 refs
[j
++] = xstrfmt("refs/tags/%s:refs/tags/%s",
1091 refs
[j
++] = argv
[i
];
1097 sigchain_push_common(unlock_pack_on_signal
);
1098 atexit(unlock_pack
);
1099 refspec
= parse_fetch_refspec(ref_nr
, refs
);
1100 exit_code
= do_fetch(gtransport
, refspec
, ref_nr
);
1101 free_refspec(ref_nr
, refspec
);
1102 transport_disconnect(gtransport
);
1107 int cmd_fetch(int argc
, const char **argv
, const char *prefix
)
1110 struct string_list list
= STRING_LIST_INIT_NODUP
;
1111 struct remote
*remote
;
1113 struct argv_array argv_gc_auto
= ARGV_ARRAY_INIT
;
1115 packet_trace_identity("fetch");
1117 /* Record the command line for the reflog */
1118 strbuf_addstr(&default_rla
, "fetch");
1119 for (i
= 1; i
< argc
; i
++)
1120 strbuf_addf(&default_rla
, " %s", argv
[i
]);
1122 git_config(git_fetch_config
, NULL
);
1124 argc
= parse_options(argc
, argv
, prefix
,
1125 builtin_fetch_options
, builtin_fetch_usage
, 0);
1129 die(_("--depth and --unshallow cannot be used together"));
1130 else if (!is_repository_shallow())
1131 die(_("--unshallow on a complete repository does not make sense"));
1133 static char inf_depth
[12];
1134 sprintf(inf_depth
, "%d", INFINITE_DEPTH
);
1139 /* no need to be strict, transport_set_option() will validate it again */
1140 if (depth
&& atoi(depth
) < 1)
1141 die(_("depth %s is not a positive number"), depth
);
1143 if (recurse_submodules
!= RECURSE_SUBMODULES_OFF
) {
1144 if (recurse_submodules_default
) {
1145 int arg
= parse_fetch_recurse_submodules_arg("--recurse-submodules-default", recurse_submodules_default
);
1146 set_config_fetch_recurse_submodules(arg
);
1148 gitmodules_config();
1149 git_config(submodule_config
, NULL
);
1154 die(_("fetch --all does not take a repository argument"));
1156 die(_("fetch --all does not make sense with refspecs"));
1157 (void) for_each_remote(get_one_remote_for_fetch
, &list
);
1158 result
= fetch_multiple(&list
);
1159 } else if (argc
== 0) {
1160 /* No arguments -- use default remote */
1161 remote
= remote_get(NULL
);
1162 result
= fetch_one(remote
, argc
, argv
);
1163 } else if (multiple
) {
1164 /* All arguments are assumed to be remotes or groups */
1165 for (i
= 0; i
< argc
; i
++)
1166 if (!add_remote_or_group(argv
[i
], &list
))
1167 die(_("No such remote or remote group: %s"), argv
[i
]);
1168 result
= fetch_multiple(&list
);
1170 /* Single remote or group */
1171 (void) add_remote_or_group(argv
[0], &list
);
1173 /* More than one remote */
1175 die(_("Fetching a group and specifying refspecs does not make sense"));
1176 result
= fetch_multiple(&list
);
1178 /* Zero or one remotes */
1179 remote
= remote_get(argv
[0]);
1180 result
= fetch_one(remote
, argc
-1, argv
+1);
1184 if (!result
&& (recurse_submodules
!= RECURSE_SUBMODULES_OFF
)) {
1185 struct argv_array options
= ARGV_ARRAY_INIT
;
1187 add_options_to_argv(&options
);
1188 result
= fetch_populated_submodules(&options
,
1192 argv_array_clear(&options
);
1195 /* All names were strdup()ed or strndup()ed */
1196 list
.strdup_strings
= 1;
1197 string_list_clear(&list
, 0);
1199 argv_array_pushl(&argv_gc_auto
, "gc", "--auto", NULL
);
1201 argv_array_push(&argv_gc_auto
, "--quiet");
1202 run_command_v_opt(argv_gc_auto
.argv
, RUN_GIT_CMD
);
1203 argv_array_clear(&argv_gc_auto
);