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 const 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 const 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)
156 static int update_local_ref(struct ref
*ref
,
161 struct commit
*current
= NULL
, *updated
;
162 enum object_type type
;
163 struct branch
*current_branch
= branch_get(NULL
);
164 const char *pretty_ref
= ref
->name
+ (
165 !prefixcmp(ref
->name
, "refs/heads/") ? 11 :
166 !prefixcmp(ref
->name
, "refs/tags/") ? 10 :
167 !prefixcmp(ref
->name
, "refs/remotes/") ? 13 :
171 type
= sha1_object_info(ref
->new_sha1
, NULL
);
173 die("object %s not found", sha1_to_hex(ref
->new_sha1
));
178 sprintf(display
, "* branch %s -> FETCH_HEAD", remote
);
182 if (!hashcmp(ref
->old_sha1
, ref
->new_sha1
)) {
184 sprintf(display
, "= %-*s %s -> %s", SUMMARY_WIDTH
,
185 "[up to date]", remote
, pretty_ref
);
189 if (current_branch
&&
190 !strcmp(ref
->name
, current_branch
->name
) &&
191 !(update_head_ok
|| is_bare_repository()) &&
192 !is_null_sha1(ref
->old_sha1
)) {
194 * If this is the head, and it's not okay to update
195 * the head, and the old value of the head isn't empty...
197 sprintf(display
, "! %-*s %s -> %s (can't fetch in current branch)",
198 SUMMARY_WIDTH
, "[rejected]", remote
, pretty_ref
);
202 if (!is_null_sha1(ref
->old_sha1
) &&
203 !prefixcmp(ref
->name
, "refs/tags/")) {
204 sprintf(display
, "- %-*s %s -> %s",
205 SUMMARY_WIDTH
, "[tag update]", remote
, pretty_ref
);
206 return s_update_ref("updating tag", ref
, 0);
209 current
= lookup_commit_reference_gently(ref
->old_sha1
, 1);
210 updated
= lookup_commit_reference_gently(ref
->new_sha1
, 1);
211 if (!current
|| !updated
) {
214 if (!strncmp(ref
->name
, "refs/tags/", 10)) {
219 msg
= "storing head";
220 what
= "[new branch]";
223 sprintf(display
, "* %-*s %s -> %s",
224 SUMMARY_WIDTH
, what
, remote
, pretty_ref
);
225 return s_update_ref(msg
, ref
, 0);
228 if (in_merge_bases(current
, &updated
, 1)) {
230 strcpy(quickref
, find_unique_abbrev(current
->object
.sha1
, DEFAULT_ABBREV
));
231 strcat(quickref
, "..");
232 strcat(quickref
, find_unique_abbrev(ref
->new_sha1
, DEFAULT_ABBREV
));
233 sprintf(display
, " %-*s %s -> %s (fast forward)",
234 SUMMARY_WIDTH
, quickref
, remote
, pretty_ref
);
235 return s_update_ref("fast forward", ref
, 1);
236 } else if (force
|| ref
->force
) {
238 strcpy(quickref
, find_unique_abbrev(current
->object
.sha1
, DEFAULT_ABBREV
));
239 strcat(quickref
, "...");
240 strcat(quickref
, find_unique_abbrev(ref
->new_sha1
, DEFAULT_ABBREV
));
241 sprintf(display
, "+ %-*s %s -> %s (forced update)",
242 SUMMARY_WIDTH
, quickref
, remote
, pretty_ref
);
243 return s_update_ref("forced-update", ref
, 1);
245 sprintf(display
, "! %-*s %s -> %s (non fast forward)",
246 SUMMARY_WIDTH
, "[rejected]", remote
, pretty_ref
);
251 static void store_updated_refs(const char *url
, struct ref
*ref_map
)
254 struct commit
*commit
;
255 int url_len
, i
, note_len
, shown_url
= 0;
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
, what
, verbose
, note
);
322 fprintf(stderr
, "From %.*s\n",
326 fprintf(stderr
, " %s\n", note
);
333 static int fetch_refs(struct transport
*transport
, struct ref
*ref_map
)
335 int ret
= transport_fetch_refs(transport
, ref_map
);
337 store_updated_refs(transport
->url
, ref_map
);
338 transport_unlock_pack(transport
);
342 static int add_existing(const char *refname
, const unsigned char *sha1
,
343 int flag
, void *cbdata
)
345 struct path_list
*list
= (struct path_list
*)cbdata
;
346 path_list_insert(refname
, list
);
350 static struct ref
*find_non_local_tags(struct transport
*transport
,
351 struct ref
*fetch_map
)
353 static struct path_list existing_refs
= { NULL
, 0, 0, 0 };
354 struct path_list new_refs
= { NULL
, 0, 0, 1 };
357 const unsigned char *ref_sha1
;
358 const struct ref
*tag_ref
;
359 struct ref
*rm
= NULL
;
360 struct ref
*ref_map
= NULL
;
361 struct ref
**tail
= &ref_map
;
362 const struct ref
*ref
;
364 for_each_ref(add_existing
, &existing_refs
);
365 for (ref
= transport_get_remote_refs(transport
); ref
; ref
= ref
->next
) {
366 if (prefixcmp(ref
->name
, "refs/tags"))
369 ref_name
= xstrdup(ref
->name
);
370 ref_name_len
= strlen(ref_name
);
371 ref_sha1
= ref
->old_sha1
;
373 if (!strcmp(ref_name
+ ref_name_len
- 3, "^{}")) {
374 ref_name
[ref_name_len
- 3] = 0;
375 tag_ref
= transport_get_remote_refs(transport
);
377 if (!strcmp(tag_ref
->name
, ref_name
)) {
378 ref_sha1
= tag_ref
->old_sha1
;
381 tag_ref
= tag_ref
->next
;
385 if (!path_list_has_path(&existing_refs
, ref_name
) &&
386 !path_list_has_path(&new_refs
, ref_name
) &&
387 lookup_object(ref
->old_sha1
)) {
388 path_list_insert(ref_name
, &new_refs
);
390 rm
= alloc_ref(strlen(ref_name
) + 1);
391 strcpy(rm
->name
, ref_name
);
392 rm
->peer_ref
= alloc_ref(strlen(ref_name
) + 1);
393 strcpy(rm
->peer_ref
->name
, ref_name
);
394 hashcpy(rm
->old_sha1
, ref_sha1
);
405 static int do_fetch(struct transport
*transport
,
406 struct refspec
*refs
, int ref_count
)
408 struct ref
*ref_map
, *fetch_map
;
410 int autotags
= (transport
->remote
->fetch_tags
== 1);
411 if (transport
->remote
->fetch_tags
== 2 && !no_tags
)
413 if (transport
->remote
->fetch_tags
== -1)
416 if (!transport
->get_refs_list
|| !transport
->fetch
)
417 die("Don't know how to fetch from %s", transport
->url
);
419 /* if not appending, truncate FETCH_HEAD */
421 fclose(fopen(git_path("FETCH_HEAD"), "w"));
423 ref_map
= get_ref_map(transport
, refs
, ref_count
, tags
, &autotags
);
425 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
427 read_ref(rm
->peer_ref
->name
, rm
->peer_ref
->old_sha1
);
430 if (fetch_refs(transport
, ref_map
)) {
437 /* if neither --no-tags nor --tags was specified, do automated tag
439 if (!(tags
|| no_tags
) && autotags
) {
440 ref_map
= find_non_local_tags(transport
, fetch_map
);
442 transport_set_option(transport
, TRANS_OPT_DEPTH
, "0");
443 fetch_refs(transport
, ref_map
);
448 free_refs(fetch_map
);
453 static void set_option(const char *name
, const char *value
)
455 int r
= transport_set_option(transport
, name
, value
);
457 die("Option \"%s\" value \"%s\" is not valid for %s\n",
458 name
, value
, transport
->url
);
460 warning("Option \"%s\" is ignored for %s\n",
461 name
, transport
->url
);
464 int cmd_fetch(int argc
, const char **argv
, const char *prefix
)
466 struct remote
*remote
;
467 int i
, j
, rla_offset
;
468 static const char **refs
= NULL
;
471 const char *depth
= NULL
, *upload_pack
= NULL
;
474 for (i
= 1; i
< argc
; i
++) {
475 const char *arg
= argv
[i
];
476 cmd_len
+= strlen(arg
);
480 if (!strcmp(arg
, "--append") || !strcmp(arg
, "-a")) {
484 if (!prefixcmp(arg
, "--upload-pack=")) {
485 upload_pack
= arg
+ 14;
488 if (!strcmp(arg
, "--upload-pack")) {
492 upload_pack
= argv
[i
];
495 if (!strcmp(arg
, "--force") || !strcmp(arg
, "-f")) {
499 if (!strcmp(arg
, "--no-tags")) {
503 if (!strcmp(arg
, "--tags") || !strcmp(arg
, "-t")) {
507 if (!strcmp(arg
, "--keep") || !strcmp(arg
, "-k")) {
511 if (!strcmp(arg
, "--update-head-ok") || !strcmp(arg
, "-u")) {
515 if (!prefixcmp(arg
, "--depth=")) {
519 if (!strcmp(arg
, "--depth")) {
526 if (!strcmp(arg
, "--quiet") || !strcmp(arg
, "-q")) {
530 if (!strcmp(arg
, "--verbose") || !strcmp(arg
, "-v")) {
537 for (j
= i
; j
< argc
; j
++)
538 cmd_len
+= strlen(argv
[j
]);
540 default_rla
= xmalloc(cmd_len
+ 5 + argc
+ 1);
541 sprintf(default_rla
, "fetch");
542 rla_offset
= strlen(default_rla
);
543 for (j
= 1; j
< argc
; j
++) {
544 sprintf(default_rla
+ rla_offset
, " %s", argv
[j
]);
545 rla_offset
+= strlen(argv
[j
]) + 1;
549 remote
= remote_get(NULL
);
551 remote
= remote_get(argv
[i
++]);
553 transport
= transport_get(remote
, remote
->url
[0]);
555 transport
->verbose
= 1;
557 transport
->verbose
= -1;
559 set_option(TRANS_OPT_UPLOADPACK
, upload_pack
);
561 set_option(TRANS_OPT_KEEP
, "yes");
563 set_option(TRANS_OPT_DEPTH
, depth
);
566 die("Where do you want to fetch from today?");
570 refs
= xcalloc(argc
- i
+ 1, sizeof(const char *));
572 if (!strcmp(argv
[i
], "tag")) {
575 ref
= xmalloc(strlen(argv
[i
]) * 2 + 22);
576 strcpy(ref
, "refs/tags/");
577 strcat(ref
, argv
[i
]);
578 strcat(ref
, ":refs/tags/");
579 strcat(ref
, argv
[i
]);
589 signal(SIGINT
, unlock_pack_on_signal
);
591 return do_fetch(transport
, parse_ref_spec(ref_nr
, refs
), ref_nr
);