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
;
16 static int unpacklimit
;
18 static char *default_rla
= NULL
;
20 static void find_merge_config(struct ref
*ref_map
, struct remote
*remote
)
22 struct ref
*rm
= ref_map
;
23 struct branch
*branch
= branch_get(NULL
);
25 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
26 if (!branch_has_merge_config(branch
)) {
27 if (remote
&& remote
->fetch
&&
28 !strcmp(remote
->fetch
[0].src
, rm
->name
))
31 if (branch_merges(branch
, rm
->name
))
37 static struct ref
*get_ref_map(struct transport
*transport
,
38 struct refspec
*refs
, int ref_count
, int tags
,
43 struct ref
*ref_map
= NULL
;
44 struct ref
**tail
= &ref_map
;
46 struct ref
*remote_refs
= transport_get_remote_refs(transport
);
48 if (ref_count
|| tags
) {
49 for (i
= 0; i
< ref_count
; i
++) {
50 get_fetch_map(remote_refs
, &refs
[i
], &tail
);
51 if (refs
[i
].dst
&& refs
[i
].dst
[0])
54 /* Merge everything on the command line, but not --tags */
55 for (rm
= ref_map
; rm
; rm
= rm
->next
)
58 struct refspec refspec
;
59 refspec
.src
= "refs/tags/";
60 refspec
.dst
= "refs/tags/";
63 get_fetch_map(remote_refs
, &refspec
, &tail
);
66 /* Use the defaults */
67 struct remote
*remote
= transport
->remote
;
68 if (remote
->fetch_refspec_nr
) {
69 for (i
= 0; i
< remote
->fetch_refspec_nr
; i
++) {
70 get_fetch_map(remote_refs
, &remote
->fetch
[i
], &tail
);
71 if (remote
->fetch
[i
].dst
&&
72 remote
->fetch
[i
].dst
[0])
75 find_merge_config(ref_map
, remote
);
77 ref_map
= get_remote_ref(remote_refs
, "HEAD");
86 static void show_new(enum object_type type
, unsigned char *sha1_new
)
88 fprintf(stderr
, " %s: %s\n", typename(type
),
89 find_unique_abbrev(sha1_new
, DEFAULT_ABBREV
));
92 static int s_update_ref(const char *action
,
97 char *rla
= getenv("GIT_REFLOG_ACTION");
98 static struct ref_lock
*lock
;
102 snprintf(msg
, sizeof(msg
), "%s: %s", rla
, action
);
103 lock
= lock_any_ref_for_update(ref
->name
,
104 check_old
? ref
->old_sha1
: NULL
, 0);
107 if (write_ref_sha1(lock
, ref
->new_sha1
, msg
) < 0)
112 static int update_local_ref(struct ref
*ref
,
116 char oldh
[41], newh
[41];
117 struct commit
*current
= NULL
, *updated
;
118 enum object_type type
;
119 struct branch
*current_branch
= branch_get(NULL
);
121 type
= sha1_object_info(ref
->new_sha1
, NULL
);
123 die("object %s not found", sha1_to_hex(ref
->new_sha1
));
128 fprintf(stderr
, "* fetched %s\n", note
);
129 show_new(type
, ref
->new_sha1
);
134 if (!hashcmp(ref
->old_sha1
, ref
->new_sha1
)) {
136 fprintf(stderr
, "* %s: same as %s\n",
138 show_new(type
, ref
->new_sha1
);
143 if (!strcmp(ref
->name
, current_branch
->name
) &&
144 !(update_head_ok
|| is_bare_repository()) &&
145 !is_null_sha1(ref
->old_sha1
)) {
147 * If this is the head, and it's not okay to update
148 * the head, and the old value of the head isn't empty...
151 " * %s: Cannot fetch into the current branch.\n",
156 if (!is_null_sha1(ref
->old_sha1
) &&
157 !prefixcmp(ref
->name
, "refs/tags/")) {
158 fprintf(stderr
, "* %s: updating with %s\n",
160 show_new(type
, ref
->new_sha1
);
161 return s_update_ref("updating tag", ref
, 0);
164 current
= lookup_commit_reference(ref
->old_sha1
);
165 updated
= lookup_commit_reference(ref
->new_sha1
);
166 if (!current
|| !updated
) {
168 if (!strncmp(ref
->name
, "refs/tags/", 10))
171 msg
= "storing head";
172 fprintf(stderr
, "* %s: storing %s\n",
174 show_new(type
, ref
->new_sha1
);
175 return s_update_ref(msg
, ref
, 0);
178 strcpy(oldh
, find_unique_abbrev(current
->object
.sha1
, DEFAULT_ABBREV
));
179 strcpy(newh
, find_unique_abbrev(ref
->new_sha1
, DEFAULT_ABBREV
));
181 if (in_merge_bases(current
, &updated
, 1)) {
182 fprintf(stderr
, "* %s: fast forward to %s\n",
184 fprintf(stderr
, " old..new: %s..%s\n", oldh
, newh
);
185 return s_update_ref("fast forward", ref
, 1);
187 if (!force
&& !ref
->force
) {
189 "* %s: not updating to non-fast forward %s\n",
192 " old...new: %s...%s\n", oldh
, newh
);
196 "* %s: forcing update to non-fast forward %s\n",
198 fprintf(stderr
, " old...new: %s...%s\n", oldh
, newh
);
199 return s_update_ref("forced-update", ref
, 1);
202 static void store_updated_refs(const char *url
, struct ref
*ref_map
)
205 struct commit
*commit
;
206 int url_len
, i
, note_len
;
208 const char *what
, *kind
;
211 fp
= fopen(git_path("FETCH_HEAD"), "a");
212 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
213 struct ref
*ref
= NULL
;
216 ref
= xcalloc(1, sizeof(*ref
) + strlen(rm
->peer_ref
->name
) + 1);
217 strcpy(ref
->name
, rm
->peer_ref
->name
);
218 hashcpy(ref
->old_sha1
, rm
->peer_ref
->old_sha1
);
219 hashcpy(ref
->new_sha1
, rm
->old_sha1
);
220 ref
->force
= rm
->force
;
223 commit
= lookup_commit_reference(rm
->old_sha1
);
227 if (!strcmp(rm
->name
, "HEAD")) {
231 else if (!prefixcmp(rm
->name
, "refs/heads/")) {
233 what
= rm
->name
+ 11;
235 else if (!prefixcmp(rm
->name
, "refs/tags/")) {
237 what
= rm
->name
+ 10;
239 else if (!prefixcmp(rm
->name
, "refs/remotes/")) {
240 kind
= "remote branch";
241 what
= rm
->name
+ 13;
248 url_len
= strlen(url
);
249 for (i
= url_len
- 1; url
[i
] == '/' && 0 <= i
; i
--)
252 if (4 < i
&& !strncmp(".git", url
+ i
- 3, 4))
258 note_len
+= sprintf(note
+ note_len
, "%s ",
260 note_len
+= sprintf(note
+ note_len
, "'%s' of ", what
);
262 note_len
+= sprintf(note
+ note_len
, "%.*s", url_len
, url
);
263 fprintf(fp
, "%s\t%s\t%s\n",
264 sha1_to_hex(commit
? commit
->object
.sha1
:
266 rm
->merge
? "" : "not-for-merge",
270 update_local_ref(ref
, note
, verbose
);
275 static int fetch_refs(struct transport
*transport
, struct ref
*ref_map
)
277 int ret
= transport_fetch_refs(transport
, ref_map
);
279 store_updated_refs(transport
->url
, ref_map
);
283 static int add_existing(const char *refname
, const unsigned char *sha1
,
284 int flag
, void *cbdata
)
286 struct path_list
*list
= (struct path_list
*)cbdata
;
287 path_list_insert(refname
, list
);
291 static struct ref
*find_non_local_tags(struct transport
*transport
,
292 struct ref
*fetch_map
)
294 static struct path_list existing_refs
= { NULL
, 0, 0, 0 };
295 struct path_list new_refs
= { NULL
, 0, 0, 1 };
298 unsigned char *ref_sha1
;
300 struct ref
*rm
= NULL
;
301 struct ref
*ref_map
= NULL
;
302 struct ref
**tail
= &ref_map
;
305 for_each_ref(add_existing
, &existing_refs
);
306 for (ref
= transport_get_remote_refs(transport
); ref
; ref
= ref
->next
) {
307 if (prefixcmp(ref
->name
, "refs/tags"))
310 ref_name
= xstrdup(ref
->name
);
311 ref_name_len
= strlen(ref_name
);
312 ref_sha1
= ref
->old_sha1
;
314 if (!strcmp(ref_name
+ ref_name_len
- 3, "^{}")) {
315 ref_name
[ref_name_len
- 3] = 0;
316 tag_ref
= transport_get_remote_refs(transport
);
318 if (!strcmp(tag_ref
->name
, ref_name
)) {
319 ref_sha1
= tag_ref
->old_sha1
;
322 tag_ref
= tag_ref
->next
;
326 if (!path_list_has_path(&existing_refs
, ref_name
) &&
327 !path_list_has_path(&new_refs
, ref_name
) &&
328 lookup_object(ref
->old_sha1
)) {
329 fprintf(stderr
, "Auto-following %s\n",
332 path_list_insert(ref_name
, &new_refs
);
334 rm
= alloc_ref(strlen(ref_name
) + 1);
335 strcpy(rm
->name
, ref_name
);
336 rm
->peer_ref
= alloc_ref(strlen(ref_name
) + 1);
337 strcpy(rm
->peer_ref
->name
, ref_name
);
338 hashcpy(rm
->old_sha1
, ref_sha1
);
349 static int do_fetch(struct transport
*transport
,
350 struct refspec
*refs
, int ref_count
)
352 struct ref
*ref_map
, *fetch_map
;
354 int autotags
= (transport
->remote
->fetch_tags
== 1);
355 if (transport
->remote
->fetch_tags
== 2 && !no_tags
)
357 if (transport
->remote
->fetch_tags
== -1)
360 if (!transport
->ops
|| !transport
->ops
->get_refs_list
||
361 !(transport
->ops
->fetch_refs
|| transport
->ops
->fetch_objs
))
362 die("Don't know how to fetch from %s", transport
->url
);
364 /* if not appending, truncate FETCH_HEAD */
366 fclose(fopen(git_path("FETCH_HEAD"), "w"));
368 ref_map
= get_ref_map(transport
, refs
, ref_count
, tags
, &autotags
);
370 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
372 read_ref(rm
->peer_ref
->name
, rm
->peer_ref
->old_sha1
);
374 printf("%s : %s\n", rm
->name
, rm
->peer_ref
? rm
->peer_ref
->name
: NULL
);
375 printf(" < %s\n", sha1_to_hex(rm
->old_sha1
));
377 printf(" > %s\n", sha1_to_hex(rm
->peer_ref
->old_sha1
));
379 hashcmp(rm
->old_sha1
, rm
->peer_ref
->old_sha1
)) {
380 printf("%s needs update.\n", rm
->name
);
384 if (fetch_refs(transport
, ref_map
)) {
391 /* if neither --no-tags nor --tags was specified, do automated tag
393 if (!(tags
|| no_tags
) && autotags
) {
394 ref_map
= find_non_local_tags(transport
, fetch_map
);
396 transport_set_option(transport
, TRANS_OPT_DEPTH
, "0");
397 fetch_refs(transport
, ref_map
);
402 free_refs(fetch_map
);
407 static int fetch_config(const char *var
, const char *value
)
409 if (strcmp(var
, "fetch.unpacklimit") == 0) {
410 unpacklimit
= git_config_int(var
, value
);
414 if (strcmp(var
, "transfer.unpacklimit") == 0) {
415 unpacklimit
= git_config_int(var
, value
);
419 return git_default_config(var
, value
);
422 int cmd_fetch(int argc
, const char **argv
, const char *prefix
)
424 struct remote
*remote
;
425 struct transport
*transport
;
426 int i
, j
, rla_offset
;
427 static const char **refs
= NULL
;
430 const char *depth
= NULL
, *upload_pack
= NULL
;
433 git_config(fetch_config
);
435 for (i
= 1; i
< argc
; i
++) {
436 const char *arg
= argv
[i
];
437 cmd_len
+= strlen(arg
);
441 if (!strcmp(arg
, "--append") || !strcmp(arg
, "-a")) {
445 if (!prefixcmp(arg
, "--upload-pack=")) {
446 upload_pack
= arg
+ 14;
449 if (!strcmp(arg
, "--upload-pack")) {
453 upload_pack
= argv
[i
];
456 if (!strcmp(arg
, "--force") || !strcmp(arg
, "-f")) {
460 if (!strcmp(arg
, "--no-tags")) {
464 if (!strcmp(arg
, "--tags") || !strcmp(arg
, "-t")) {
468 if (!strcmp(arg
, "--keep") || !strcmp(arg
, "-k")) {
472 if (!strcmp(arg
, "--update-head-ok") || !strcmp(arg
, "-u")) {
476 if (!prefixcmp(arg
, "--depth=")) {
480 if (!strcmp(arg
, "--depth")) {
487 if (!strcmp(arg
, "--quiet")) {
491 if (!strcmp(arg
, "--verbose") || !strcmp(arg
, "-v")) {
498 for (j
= i
; j
< argc
; j
++)
499 cmd_len
+= strlen(argv
[j
]);
501 default_rla
= xmalloc(cmd_len
+ 5 + argc
+ 1);
502 sprintf(default_rla
, "fetch");
503 rla_offset
= strlen(default_rla
);
504 for (j
= 1; j
< argc
; j
++) {
505 sprintf(default_rla
+ rla_offset
, " %s", argv
[j
]);
506 rla_offset
+= strlen(argv
[j
]);
510 remote
= remote_get(NULL
);
512 remote
= remote_get(argv
[i
++]);
514 transport
= transport_get(remote
, remote
->uri
[0], 1);
516 transport
->verbose
= 1;
518 transport
->verbose
= 0;
520 transport_set_option(transport
, TRANS_OPT_UPLOADPACK
, upload_pack
);
522 transport_set_option(transport
, TRANS_OPT_KEEP
, "yes");
523 transport_set_option(transport
, TRANS_OPT_DEPTH
, depth
);
526 die("Where do you want to fetch from today?");
530 refs
= xcalloc(argc
- i
+ 1, sizeof(const char *));
532 if (!strcmp(argv
[i
], "tag")) {
535 ref
= xmalloc(strlen(argv
[i
]) * 2 + 22);
536 strcpy(ref
, "refs/tags/");
537 strcat(ref
, argv
[i
]);
538 strcat(ref
, ":refs/tags/");
539 strcat(ref
, argv
[i
]);
547 for (j
= 0; refs
[j
]; j
++)
548 printf("ref: %s\n", refs
[j
]);
551 return do_fetch(transport
, parse_ref_spec(ref_nr
, refs
), ref_nr
);