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 int s_update_ref(const char *action
,
139 char *rla
= getenv("GIT_REFLOG_ACTION");
140 static struct ref_lock
*lock
;
144 snprintf(msg
, sizeof(msg
), "%s: %s", rla
, action
);
145 lock
= lock_any_ref_for_update(ref
->name
,
146 check_old
? ref
->old_sha1
: NULL
, 0);
149 if (write_ref_sha1(lock
, ref
->new_sha1
, msg
) < 0)
154 #define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
155 #define REFCOL_WIDTH 10
157 static int update_local_ref(struct ref
*ref
,
162 struct commit
*current
= NULL
, *updated
;
163 enum object_type type
;
164 struct branch
*current_branch
= branch_get(NULL
);
165 const char *pretty_ref
= ref
->name
+ (
166 !prefixcmp(ref
->name
, "refs/heads/") ? 11 :
167 !prefixcmp(ref
->name
, "refs/tags/") ? 10 :
168 !prefixcmp(ref
->name
, "refs/remotes/") ? 13 :
172 type
= sha1_object_info(ref
->new_sha1
, NULL
);
174 die("object %s not found", sha1_to_hex(ref
->new_sha1
));
179 sprintf(display
, "* branch %s -> FETCH_HEAD", remote
);
183 if (!hashcmp(ref
->old_sha1
, ref
->new_sha1
)) {
185 sprintf(display
, "= %-*s %-*s -> %s", SUMMARY_WIDTH
,
186 "[up to date]", REFCOL_WIDTH
, remote
,
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...
199 sprintf(display
, "! %-*s %-*s -> %s (can't fetch in current branch)",
200 SUMMARY_WIDTH
, "[rejected]", REFCOL_WIDTH
, remote
,
205 if (!is_null_sha1(ref
->old_sha1
) &&
206 !prefixcmp(ref
->name
, "refs/tags/")) {
207 sprintf(display
, "- %-*s %-*s -> %s",
208 SUMMARY_WIDTH
, "[tag update]", REFCOL_WIDTH
, remote
,
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
) {
218 if (!strncmp(ref
->name
, "refs/tags/", 10)) {
223 msg
= "storing head";
224 what
= "[new branch]";
227 sprintf(display
, "* %-*s %-*s -> %s", SUMMARY_WIDTH
, what
,
228 REFCOL_WIDTH
, remote
, pretty_ref
);
229 return s_update_ref(msg
, ref
, 0);
232 if (in_merge_bases(current
, &updated
, 1)) {
234 strcpy(quickref
, find_unique_abbrev(current
->object
.sha1
, DEFAULT_ABBREV
));
235 strcat(quickref
, "..");
236 strcat(quickref
, find_unique_abbrev(ref
->new_sha1
, DEFAULT_ABBREV
));
237 sprintf(display
, " %-*s %-*s -> %s", SUMMARY_WIDTH
, quickref
,
238 REFCOL_WIDTH
, remote
, pretty_ref
);
239 return s_update_ref("fast forward", ref
, 1);
240 } else if (force
|| ref
->force
) {
242 strcpy(quickref
, find_unique_abbrev(current
->object
.sha1
, DEFAULT_ABBREV
));
243 strcat(quickref
, "...");
244 strcat(quickref
, find_unique_abbrev(ref
->new_sha1
, DEFAULT_ABBREV
));
245 sprintf(display
, "+ %-*s %-*s -> %s (forced update)",
246 SUMMARY_WIDTH
, quickref
, REFCOL_WIDTH
, remote
, pretty_ref
);
247 return s_update_ref("forced-update", ref
, 1);
249 sprintf(display
, "! %-*s %-*s -> %s (non fast forward)",
250 SUMMARY_WIDTH
, "[rejected]", REFCOL_WIDTH
, remote
,
256 static void store_updated_refs(const char *url
, struct ref
*ref_map
)
259 struct commit
*commit
;
260 int url_len
, i
, note_len
, shown_url
= 0;
262 const char *what
, *kind
;
265 fp
= fopen(git_path("FETCH_HEAD"), "a");
266 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
267 struct ref
*ref
= NULL
;
270 ref
= xcalloc(1, sizeof(*ref
) + strlen(rm
->peer_ref
->name
) + 1);
271 strcpy(ref
->name
, rm
->peer_ref
->name
);
272 hashcpy(ref
->old_sha1
, rm
->peer_ref
->old_sha1
);
273 hashcpy(ref
->new_sha1
, rm
->old_sha1
);
274 ref
->force
= rm
->peer_ref
->force
;
277 commit
= lookup_commit_reference_gently(rm
->old_sha1
, 1);
281 if (!strcmp(rm
->name
, "HEAD")) {
285 else if (!prefixcmp(rm
->name
, "refs/heads/")) {
287 what
= rm
->name
+ 11;
289 else if (!prefixcmp(rm
->name
, "refs/tags/")) {
291 what
= rm
->name
+ 10;
293 else if (!prefixcmp(rm
->name
, "refs/remotes/")) {
294 kind
= "remote branch";
295 what
= rm
->name
+ 13;
302 url_len
= strlen(url
);
303 for (i
= url_len
- 1; url
[i
] == '/' && 0 <= i
; i
--)
306 if (4 < i
&& !strncmp(".git", url
+ i
- 3, 4))
312 note_len
+= sprintf(note
+ note_len
, "%s ",
314 note_len
+= sprintf(note
+ note_len
, "'%s' of ", what
);
316 note_len
+= sprintf(note
+ note_len
, "%.*s", url_len
, url
);
317 fprintf(fp
, "%s\t%s\t%s\n",
318 sha1_to_hex(commit
? commit
->object
.sha1
:
320 rm
->merge
? "" : "not-for-merge",
324 update_local_ref(ref
, what
, verbose
, note
);
327 fprintf(stderr
, "From %.*s\n",
331 fprintf(stderr
, " %s\n", note
);
338 static int fetch_refs(struct transport
*transport
, struct ref
*ref_map
)
340 int ret
= transport_fetch_refs(transport
, ref_map
);
342 store_updated_refs(transport
->url
, ref_map
);
343 transport_unlock_pack(transport
);
347 static int add_existing(const char *refname
, const unsigned char *sha1
,
348 int flag
, void *cbdata
)
350 struct path_list
*list
= (struct path_list
*)cbdata
;
351 path_list_insert(refname
, list
);
355 static struct ref
*find_non_local_tags(struct transport
*transport
,
356 struct ref
*fetch_map
)
358 static struct path_list existing_refs
= { NULL
, 0, 0, 0 };
359 struct path_list new_refs
= { NULL
, 0, 0, 1 };
362 unsigned char *ref_sha1
;
364 struct ref
*rm
= NULL
;
365 struct ref
*ref_map
= NULL
;
366 struct ref
**tail
= &ref_map
;
369 for_each_ref(add_existing
, &existing_refs
);
370 for (ref
= transport_get_remote_refs(transport
); ref
; ref
= ref
->next
) {
371 if (prefixcmp(ref
->name
, "refs/tags"))
374 ref_name
= xstrdup(ref
->name
);
375 ref_name_len
= strlen(ref_name
);
376 ref_sha1
= ref
->old_sha1
;
378 if (!strcmp(ref_name
+ ref_name_len
- 3, "^{}")) {
379 ref_name
[ref_name_len
- 3] = 0;
380 tag_ref
= transport_get_remote_refs(transport
);
382 if (!strcmp(tag_ref
->name
, ref_name
)) {
383 ref_sha1
= tag_ref
->old_sha1
;
386 tag_ref
= tag_ref
->next
;
390 if (!path_list_has_path(&existing_refs
, ref_name
) &&
391 !path_list_has_path(&new_refs
, ref_name
) &&
392 lookup_object(ref
->old_sha1
)) {
393 path_list_insert(ref_name
, &new_refs
);
395 rm
= alloc_ref(strlen(ref_name
) + 1);
396 strcpy(rm
->name
, ref_name
);
397 rm
->peer_ref
= alloc_ref(strlen(ref_name
) + 1);
398 strcpy(rm
->peer_ref
->name
, ref_name
);
399 hashcpy(rm
->old_sha1
, ref_sha1
);
410 static int do_fetch(struct transport
*transport
,
411 struct refspec
*refs
, int ref_count
)
413 struct ref
*ref_map
, *fetch_map
;
415 int autotags
= (transport
->remote
->fetch_tags
== 1);
416 if (transport
->remote
->fetch_tags
== 2 && !no_tags
)
418 if (transport
->remote
->fetch_tags
== -1)
421 if (!transport
->get_refs_list
|| !transport
->fetch
)
422 die("Don't know how to fetch from %s", transport
->url
);
424 /* if not appending, truncate FETCH_HEAD */
426 fclose(fopen(git_path("FETCH_HEAD"), "w"));
428 ref_map
= get_ref_map(transport
, refs
, ref_count
, tags
, &autotags
);
430 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
432 read_ref(rm
->peer_ref
->name
, rm
->peer_ref
->old_sha1
);
435 if (fetch_refs(transport
, ref_map
)) {
442 /* if neither --no-tags nor --tags was specified, do automated tag
444 if (!(tags
|| no_tags
) && autotags
) {
445 ref_map
= find_non_local_tags(transport
, fetch_map
);
447 transport_set_option(transport
, TRANS_OPT_DEPTH
, "0");
448 fetch_refs(transport
, ref_map
);
453 free_refs(fetch_map
);
458 static void set_option(const char *name
, const char *value
)
460 int r
= transport_set_option(transport
, name
, value
);
462 die("Option \"%s\" value \"%s\" is not valid for %s\n",
463 name
, value
, transport
->url
);
465 warning("Option \"%s\" is ignored for %s\n",
466 name
, transport
->url
);
469 int cmd_fetch(int argc
, const char **argv
, const char *prefix
)
471 struct remote
*remote
;
472 int i
, j
, rla_offset
;
473 static const char **refs
= NULL
;
476 const char *depth
= NULL
, *upload_pack
= NULL
;
479 for (i
= 1; i
< argc
; i
++) {
480 const char *arg
= argv
[i
];
481 cmd_len
+= strlen(arg
);
485 if (!strcmp(arg
, "--append") || !strcmp(arg
, "-a")) {
489 if (!prefixcmp(arg
, "--upload-pack=")) {
490 upload_pack
= arg
+ 14;
493 if (!strcmp(arg
, "--upload-pack")) {
497 upload_pack
= argv
[i
];
500 if (!strcmp(arg
, "--force") || !strcmp(arg
, "-f")) {
504 if (!strcmp(arg
, "--no-tags")) {
508 if (!strcmp(arg
, "--tags") || !strcmp(arg
, "-t")) {
512 if (!strcmp(arg
, "--keep") || !strcmp(arg
, "-k")) {
516 if (!strcmp(arg
, "--update-head-ok") || !strcmp(arg
, "-u")) {
520 if (!prefixcmp(arg
, "--depth=")) {
524 if (!strcmp(arg
, "--depth")) {
531 if (!strcmp(arg
, "--quiet") || !strcmp(arg
, "-q")) {
535 if (!strcmp(arg
, "--verbose") || !strcmp(arg
, "-v")) {
542 for (j
= i
; j
< argc
; j
++)
543 cmd_len
+= strlen(argv
[j
]);
545 default_rla
= xmalloc(cmd_len
+ 5 + argc
+ 1);
546 sprintf(default_rla
, "fetch");
547 rla_offset
= strlen(default_rla
);
548 for (j
= 1; j
< argc
; j
++) {
549 sprintf(default_rla
+ rla_offset
, " %s", argv
[j
]);
550 rla_offset
+= strlen(argv
[j
]) + 1;
554 remote
= remote_get(NULL
);
556 remote
= remote_get(argv
[i
++]);
558 transport
= transport_get(remote
, remote
->url
[0]);
560 transport
->verbose
= 1;
562 transport
->verbose
= -1;
564 set_option(TRANS_OPT_UPLOADPACK
, upload_pack
);
566 set_option(TRANS_OPT_KEEP
, "yes");
568 set_option(TRANS_OPT_DEPTH
, depth
);
571 die("Where do you want to fetch from today?");
575 refs
= xcalloc(argc
- i
+ 1, sizeof(const char *));
577 if (!strcmp(argv
[i
], "tag")) {
580 ref
= xmalloc(strlen(argv
[i
]) * 2 + 22);
581 strcpy(ref
, "refs/tags/");
582 strcat(ref
, argv
[i
]);
583 strcat(ref
, ":refs/tags/");
584 strcat(ref
, argv
[i
]);
594 signal(SIGINT
, unlock_pack_on_signal
);
596 return do_fetch(transport
, parse_ref_spec(ref_nr
, refs
), ref_nr
);