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
)) {
52 * Not fetched to a tracking branch? We need to fetch
53 * it anyway to allow this branch's "branch.$name.merge"
54 * to be honored by git-pull, but we do not have to
55 * fail if branch.$name.merge is misconfigured to point
56 * at a nonexisting branch. If we were indeed called by
57 * git-pull, it will notice the misconfiguration because
58 * there is no entry in the resulting FETCH_HEAD marked
61 refspec
.src
= branch
->merge
[i
]->src
;
65 get_fetch_map(remote_refs
, &refspec
, tail
, 1);
66 for (rm
= *old_tail
; rm
; rm
= rm
->next
)
71 static struct ref
*get_ref_map(struct transport
*transport
,
72 struct refspec
*refs
, int ref_count
, int tags
,
77 struct ref
*ref_map
= NULL
;
78 struct ref
**tail
= &ref_map
;
80 struct ref
*remote_refs
= transport_get_remote_refs(transport
);
82 if (ref_count
|| tags
) {
83 for (i
= 0; i
< ref_count
; i
++) {
84 get_fetch_map(remote_refs
, &refs
[i
], &tail
, 0);
85 if (refs
[i
].dst
&& refs
[i
].dst
[0])
88 /* Merge everything on the command line, but not --tags */
89 for (rm
= ref_map
; rm
; rm
= rm
->next
)
92 struct refspec refspec
;
93 refspec
.src
= "refs/tags/";
94 refspec
.dst
= "refs/tags/";
97 get_fetch_map(remote_refs
, &refspec
, &tail
, 0);
100 /* Use the defaults */
101 struct remote
*remote
= transport
->remote
;
102 struct branch
*branch
= branch_get(NULL
);
103 int has_merge
= branch_has_merge_config(branch
);
104 if (remote
&& (remote
->fetch_refspec_nr
|| has_merge
)) {
105 for (i
= 0; i
< remote
->fetch_refspec_nr
; i
++) {
106 get_fetch_map(remote_refs
, &remote
->fetch
[i
], &tail
, 0);
107 if (remote
->fetch
[i
].dst
&&
108 remote
->fetch
[i
].dst
[0])
110 if (!i
&& !has_merge
&& ref_map
&&
111 !remote
->fetch
[0].pattern
)
115 * if the remote we're fetching from is the same
116 * as given in branch.<name>.remote, we add the
117 * ref given in branch.<name>.merge, too.
120 !strcmp(branch
->remote_name
, remote
->name
))
121 add_merge_config(&ref_map
, remote_refs
, branch
, &tail
);
123 ref_map
= get_remote_ref(remote_refs
, "HEAD");
125 die("Couldn't find remote ref HEAD");
129 ref_remove_duplicates(ref_map
);
134 static void show_new(enum object_type type
, unsigned char *sha1_new
)
136 fprintf(stderr
, " %s: %s\n", typename(type
),
137 find_unique_abbrev(sha1_new
, DEFAULT_ABBREV
));
140 static int s_update_ref(const char *action
,
145 char *rla
= getenv("GIT_REFLOG_ACTION");
146 static struct ref_lock
*lock
;
150 snprintf(msg
, sizeof(msg
), "%s: %s", rla
, action
);
151 lock
= lock_any_ref_for_update(ref
->name
,
152 check_old
? ref
->old_sha1
: NULL
, 0);
155 if (write_ref_sha1(lock
, ref
->new_sha1
, msg
) < 0)
160 static int update_local_ref(struct ref
*ref
,
164 char oldh
[41], newh
[41];
165 struct commit
*current
= NULL
, *updated
;
166 enum object_type type
;
167 struct branch
*current_branch
= branch_get(NULL
);
169 type
= sha1_object_info(ref
->new_sha1
, NULL
);
171 die("object %s not found", sha1_to_hex(ref
->new_sha1
));
176 fprintf(stderr
, "* fetched %s\n", note
);
177 show_new(type
, ref
->new_sha1
);
182 if (!hashcmp(ref
->old_sha1
, ref
->new_sha1
)) {
184 fprintf(stderr
, "* %s: same as %s\n",
186 show_new(type
, ref
->new_sha1
);
191 if (current_branch
&&
192 !strcmp(ref
->name
, current_branch
->name
) &&
193 !(update_head_ok
|| is_bare_repository()) &&
194 !is_null_sha1(ref
->old_sha1
)) {
196 * If this is the head, and it's not okay to update
197 * the head, and the old value of the head isn't empty...
200 " * %s: Cannot fetch into the current branch.\n",
205 if (!is_null_sha1(ref
->old_sha1
) &&
206 !prefixcmp(ref
->name
, "refs/tags/")) {
207 fprintf(stderr
, "* %s: updating with %s\n",
209 show_new(type
, ref
->new_sha1
);
210 return s_update_ref("updating tag", ref
, 0);
213 current
= lookup_commit_reference_gently(ref
->old_sha1
, 1);
214 updated
= lookup_commit_reference_gently(ref
->new_sha1
, 1);
215 if (!current
|| !updated
) {
217 if (!strncmp(ref
->name
, "refs/tags/", 10))
220 msg
= "storing head";
221 fprintf(stderr
, "* %s: storing %s\n",
223 show_new(type
, ref
->new_sha1
);
224 return s_update_ref(msg
, ref
, 0);
227 strcpy(oldh
, find_unique_abbrev(current
->object
.sha1
, DEFAULT_ABBREV
));
228 strcpy(newh
, find_unique_abbrev(ref
->new_sha1
, DEFAULT_ABBREV
));
230 if (in_merge_bases(current
, &updated
, 1)) {
231 fprintf(stderr
, "* %s: fast forward to %s\n",
233 fprintf(stderr
, " old..new: %s..%s\n", oldh
, newh
);
234 return s_update_ref("fast forward", ref
, 1);
236 if (!force
&& !ref
->force
) {
238 "* %s: not updating to non-fast forward %s\n",
241 " old...new: %s...%s\n", oldh
, newh
);
245 "* %s: forcing update to non-fast forward %s\n",
247 fprintf(stderr
, " old...new: %s...%s\n", oldh
, newh
);
248 return s_update_ref("forced-update", ref
, 1);
251 static void store_updated_refs(const char *url
, struct ref
*ref_map
)
254 struct commit
*commit
;
255 int url_len
, i
, note_len
;
257 const char *what
, *kind
;
260 fp
= fopen(git_path("FETCH_HEAD"), "a");
261 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
262 struct ref
*ref
= NULL
;
265 ref
= xcalloc(1, sizeof(*ref
) + strlen(rm
->peer_ref
->name
) + 1);
266 strcpy(ref
->name
, rm
->peer_ref
->name
);
267 hashcpy(ref
->old_sha1
, rm
->peer_ref
->old_sha1
);
268 hashcpy(ref
->new_sha1
, rm
->old_sha1
);
269 ref
->force
= rm
->peer_ref
->force
;
272 commit
= lookup_commit_reference_gently(rm
->old_sha1
, 1);
276 if (!strcmp(rm
->name
, "HEAD")) {
280 else if (!prefixcmp(rm
->name
, "refs/heads/")) {
282 what
= rm
->name
+ 11;
284 else if (!prefixcmp(rm
->name
, "refs/tags/")) {
286 what
= rm
->name
+ 10;
288 else if (!prefixcmp(rm
->name
, "refs/remotes/")) {
289 kind
= "remote branch";
290 what
= rm
->name
+ 13;
297 url_len
= strlen(url
);
298 for (i
= url_len
- 1; url
[i
] == '/' && 0 <= i
; i
--)
301 if (4 < i
&& !strncmp(".git", url
+ i
- 3, 4))
307 note_len
+= sprintf(note
+ note_len
, "%s ",
309 note_len
+= sprintf(note
+ note_len
, "'%s' of ", what
);
311 note_len
+= sprintf(note
+ note_len
, "%.*s", url_len
, url
);
312 fprintf(fp
, "%s\t%s\t%s\n",
313 sha1_to_hex(commit
? commit
->object
.sha1
:
315 rm
->merge
? "" : "not-for-merge",
319 update_local_ref(ref
, note
, verbose
);
324 static int fetch_refs(struct transport
*transport
, struct ref
*ref_map
)
326 int ret
= transport_fetch_refs(transport
, ref_map
);
328 store_updated_refs(transport
->url
, ref_map
);
329 transport_unlock_pack(transport
);
333 static int add_existing(const char *refname
, const unsigned char *sha1
,
334 int flag
, void *cbdata
)
336 struct path_list
*list
= (struct path_list
*)cbdata
;
337 path_list_insert(refname
, list
);
341 static struct ref
*find_non_local_tags(struct transport
*transport
,
342 struct ref
*fetch_map
)
344 static struct path_list existing_refs
= { NULL
, 0, 0, 0 };
345 struct path_list new_refs
= { NULL
, 0, 0, 1 };
348 unsigned char *ref_sha1
;
350 struct ref
*rm
= NULL
;
351 struct ref
*ref_map
= NULL
;
352 struct ref
**tail
= &ref_map
;
355 for_each_ref(add_existing
, &existing_refs
);
356 for (ref
= transport_get_remote_refs(transport
); ref
; ref
= ref
->next
) {
357 if (prefixcmp(ref
->name
, "refs/tags"))
360 ref_name
= xstrdup(ref
->name
);
361 ref_name_len
= strlen(ref_name
);
362 ref_sha1
= ref
->old_sha1
;
364 if (!strcmp(ref_name
+ ref_name_len
- 3, "^{}")) {
365 ref_name
[ref_name_len
- 3] = 0;
366 tag_ref
= transport_get_remote_refs(transport
);
368 if (!strcmp(tag_ref
->name
, ref_name
)) {
369 ref_sha1
= tag_ref
->old_sha1
;
372 tag_ref
= tag_ref
->next
;
376 if (!path_list_has_path(&existing_refs
, ref_name
) &&
377 !path_list_has_path(&new_refs
, ref_name
) &&
378 lookup_object(ref
->old_sha1
)) {
379 fprintf(stderr
, "Auto-following %s\n",
382 path_list_insert(ref_name
, &new_refs
);
384 rm
= alloc_ref(strlen(ref_name
) + 1);
385 strcpy(rm
->name
, ref_name
);
386 rm
->peer_ref
= alloc_ref(strlen(ref_name
) + 1);
387 strcpy(rm
->peer_ref
->name
, ref_name
);
388 hashcpy(rm
->old_sha1
, ref_sha1
);
399 static int do_fetch(struct transport
*transport
,
400 struct refspec
*refs
, int ref_count
)
402 struct ref
*ref_map
, *fetch_map
;
404 int autotags
= (transport
->remote
->fetch_tags
== 1);
405 if (transport
->remote
->fetch_tags
== 2 && !no_tags
)
407 if (transport
->remote
->fetch_tags
== -1)
410 if (!transport
->get_refs_list
|| !transport
->fetch
)
411 die("Don't know how to fetch from %s", transport
->url
);
413 /* if not appending, truncate FETCH_HEAD */
415 fclose(fopen(git_path("FETCH_HEAD"), "w"));
417 ref_map
= get_ref_map(transport
, refs
, ref_count
, tags
, &autotags
);
419 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
421 read_ref(rm
->peer_ref
->name
, rm
->peer_ref
->old_sha1
);
424 if (fetch_refs(transport
, ref_map
)) {
431 /* if neither --no-tags nor --tags was specified, do automated tag
433 if (!(tags
|| no_tags
) && autotags
) {
434 ref_map
= find_non_local_tags(transport
, fetch_map
);
436 transport_set_option(transport
, TRANS_OPT_DEPTH
, "0");
437 fetch_refs(transport
, ref_map
);
442 free_refs(fetch_map
);
447 static void set_option(const char *name
, const char *value
)
449 int r
= transport_set_option(transport
, name
, value
);
451 die("Option \"%s\" value \"%s\" is not valid for %s\n",
452 name
, value
, transport
->url
);
454 warning("Option \"%s\" is ignored for %s\n",
455 name
, transport
->url
);
458 int cmd_fetch(int argc
, const char **argv
, const char *prefix
)
460 struct remote
*remote
;
461 int i
, j
, rla_offset
;
462 static const char **refs
= NULL
;
465 const char *depth
= NULL
, *upload_pack
= NULL
;
468 for (i
= 1; i
< argc
; i
++) {
469 const char *arg
= argv
[i
];
470 cmd_len
+= strlen(arg
);
474 if (!strcmp(arg
, "--append") || !strcmp(arg
, "-a")) {
478 if (!prefixcmp(arg
, "--upload-pack=")) {
479 upload_pack
= arg
+ 14;
482 if (!strcmp(arg
, "--upload-pack")) {
486 upload_pack
= argv
[i
];
489 if (!strcmp(arg
, "--force") || !strcmp(arg
, "-f")) {
493 if (!strcmp(arg
, "--no-tags")) {
497 if (!strcmp(arg
, "--tags") || !strcmp(arg
, "-t")) {
501 if (!strcmp(arg
, "--keep") || !strcmp(arg
, "-k")) {
505 if (!strcmp(arg
, "--update-head-ok") || !strcmp(arg
, "-u")) {
509 if (!prefixcmp(arg
, "--depth=")) {
513 if (!strcmp(arg
, "--depth")) {
520 if (!strcmp(arg
, "--quiet")) {
524 if (!strcmp(arg
, "--verbose") || !strcmp(arg
, "-v")) {
531 for (j
= i
; j
< argc
; j
++)
532 cmd_len
+= strlen(argv
[j
]);
534 default_rla
= xmalloc(cmd_len
+ 5 + argc
+ 1);
535 sprintf(default_rla
, "fetch");
536 rla_offset
= strlen(default_rla
);
537 for (j
= 1; j
< argc
; j
++) {
538 sprintf(default_rla
+ rla_offset
, " %s", argv
[j
]);
539 rla_offset
+= strlen(argv
[j
]) + 1;
543 remote
= remote_get(NULL
);
545 remote
= remote_get(argv
[i
++]);
547 transport
= transport_get(remote
, remote
->url
[0]);
549 transport
->verbose
= 1;
551 transport
->verbose
= -1;
553 set_option(TRANS_OPT_UPLOADPACK
, upload_pack
);
555 set_option(TRANS_OPT_KEEP
, "yes");
557 set_option(TRANS_OPT_DEPTH
, depth
);
560 die("Where do you want to fetch from today?");
564 refs
= xcalloc(argc
- i
+ 1, sizeof(const char *));
566 if (!strcmp(argv
[i
], "tag")) {
569 ref
= xmalloc(strlen(argv
[i
]) * 2 + 22);
570 strcpy(ref
, "refs/tags/");
571 strcat(ref
, argv
[i
]);
572 strcat(ref
, ":refs/tags/");
573 strcat(ref
, argv
[i
]);
583 signal(SIGINT
, unlock_pack_on_signal
);
585 return do_fetch(transport
, parse_ref_spec(ref_nr
, refs
), ref_nr
);