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 find_merge_config(struct ref
*ref_map
, struct remote
*remote
)
33 struct ref
*rm
= ref_map
;
34 struct branch
*branch
= branch_get(NULL
);
36 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
37 if (!branch_has_merge_config(branch
)) {
38 if (remote
&& remote
->fetch
&&
39 !strcmp(remote
->fetch
[0].src
, rm
->name
))
42 if (branch_merges(branch
, rm
->name
))
48 static struct ref
*get_ref_map(struct transport
*transport
,
49 struct refspec
*refs
, int ref_count
, int tags
,
54 struct ref
*ref_map
= NULL
;
55 struct ref
**tail
= &ref_map
;
57 struct ref
*remote_refs
= transport_get_remote_refs(transport
);
59 if (ref_count
|| tags
) {
60 for (i
= 0; i
< ref_count
; i
++) {
61 get_fetch_map(remote_refs
, &refs
[i
], &tail
);
62 if (refs
[i
].dst
&& refs
[i
].dst
[0])
65 /* Merge everything on the command line, but not --tags */
66 for (rm
= ref_map
; rm
; rm
= rm
->next
)
69 struct refspec refspec
;
70 refspec
.src
= "refs/tags/";
71 refspec
.dst
= "refs/tags/";
74 get_fetch_map(remote_refs
, &refspec
, &tail
);
77 /* Use the defaults */
78 struct remote
*remote
= transport
->remote
;
79 if (remote
->fetch_refspec_nr
) {
80 for (i
= 0; i
< remote
->fetch_refspec_nr
; i
++) {
81 get_fetch_map(remote_refs
, &remote
->fetch
[i
], &tail
);
82 if (remote
->fetch
[i
].dst
&&
83 remote
->fetch
[i
].dst
[0])
86 find_merge_config(ref_map
, remote
);
88 ref_map
= get_remote_ref(remote_refs
, "HEAD");
97 static void show_new(enum object_type type
, unsigned char *sha1_new
)
99 fprintf(stderr
, " %s: %s\n", typename(type
),
100 find_unique_abbrev(sha1_new
, DEFAULT_ABBREV
));
103 static int s_update_ref(const char *action
,
108 char *rla
= getenv("GIT_REFLOG_ACTION");
109 static struct ref_lock
*lock
;
113 snprintf(msg
, sizeof(msg
), "%s: %s", rla
, action
);
114 lock
= lock_any_ref_for_update(ref
->name
,
115 check_old
? ref
->old_sha1
: NULL
, 0);
118 if (write_ref_sha1(lock
, ref
->new_sha1
, msg
) < 0)
123 static int update_local_ref(struct ref
*ref
,
127 char oldh
[41], newh
[41];
128 struct commit
*current
= NULL
, *updated
;
129 enum object_type type
;
130 struct branch
*current_branch
= branch_get(NULL
);
132 type
= sha1_object_info(ref
->new_sha1
, NULL
);
134 die("object %s not found", sha1_to_hex(ref
->new_sha1
));
139 fprintf(stderr
, "* fetched %s\n", note
);
140 show_new(type
, ref
->new_sha1
);
145 if (!hashcmp(ref
->old_sha1
, ref
->new_sha1
)) {
147 fprintf(stderr
, "* %s: same as %s\n",
149 show_new(type
, ref
->new_sha1
);
154 if (!strcmp(ref
->name
, current_branch
->name
) &&
155 !(update_head_ok
|| is_bare_repository()) &&
156 !is_null_sha1(ref
->old_sha1
)) {
158 * If this is the head, and it's not okay to update
159 * the head, and the old value of the head isn't empty...
162 " * %s: Cannot fetch into the current branch.\n",
167 if (!is_null_sha1(ref
->old_sha1
) &&
168 !prefixcmp(ref
->name
, "refs/tags/")) {
169 fprintf(stderr
, "* %s: updating with %s\n",
171 show_new(type
, ref
->new_sha1
);
172 return s_update_ref("updating tag", ref
, 0);
175 current
= lookup_commit_reference(ref
->old_sha1
);
176 updated
= lookup_commit_reference(ref
->new_sha1
);
177 if (!current
|| !updated
) {
179 if (!strncmp(ref
->name
, "refs/tags/", 10))
182 msg
= "storing head";
183 fprintf(stderr
, "* %s: storing %s\n",
185 show_new(type
, ref
->new_sha1
);
186 return s_update_ref(msg
, ref
, 0);
189 strcpy(oldh
, find_unique_abbrev(current
->object
.sha1
, DEFAULT_ABBREV
));
190 strcpy(newh
, find_unique_abbrev(ref
->new_sha1
, DEFAULT_ABBREV
));
192 if (in_merge_bases(current
, &updated
, 1)) {
193 fprintf(stderr
, "* %s: fast forward to %s\n",
195 fprintf(stderr
, " old..new: %s..%s\n", oldh
, newh
);
196 return s_update_ref("fast forward", ref
, 1);
198 if (!force
&& !ref
->force
) {
200 "* %s: not updating to non-fast forward %s\n",
203 " old...new: %s...%s\n", oldh
, newh
);
207 "* %s: forcing update to non-fast forward %s\n",
209 fprintf(stderr
, " old...new: %s...%s\n", oldh
, newh
);
210 return s_update_ref("forced-update", ref
, 1);
213 static void store_updated_refs(const char *url
, struct ref
*ref_map
)
216 struct commit
*commit
;
217 int url_len
, i
, note_len
;
219 const char *what
, *kind
;
222 fp
= fopen(git_path("FETCH_HEAD"), "a");
223 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
224 struct ref
*ref
= NULL
;
227 ref
= xcalloc(1, sizeof(*ref
) + strlen(rm
->peer_ref
->name
) + 1);
228 strcpy(ref
->name
, rm
->peer_ref
->name
);
229 hashcpy(ref
->old_sha1
, rm
->peer_ref
->old_sha1
);
230 hashcpy(ref
->new_sha1
, rm
->old_sha1
);
231 ref
->force
= rm
->peer_ref
->force
;
234 commit
= lookup_commit_reference(rm
->old_sha1
);
238 if (!strcmp(rm
->name
, "HEAD")) {
242 else if (!prefixcmp(rm
->name
, "refs/heads/")) {
244 what
= rm
->name
+ 11;
246 else if (!prefixcmp(rm
->name
, "refs/tags/")) {
248 what
= rm
->name
+ 10;
250 else if (!prefixcmp(rm
->name
, "refs/remotes/")) {
251 kind
= "remote branch";
252 what
= rm
->name
+ 13;
259 url_len
= strlen(url
);
260 for (i
= url_len
- 1; url
[i
] == '/' && 0 <= i
; i
--)
263 if (4 < i
&& !strncmp(".git", url
+ i
- 3, 4))
269 note_len
+= sprintf(note
+ note_len
, "%s ",
271 note_len
+= sprintf(note
+ note_len
, "'%s' of ", what
);
273 note_len
+= sprintf(note
+ note_len
, "%.*s", url_len
, url
);
274 fprintf(fp
, "%s\t%s\t%s\n",
275 sha1_to_hex(commit
? commit
->object
.sha1
:
277 rm
->merge
? "" : "not-for-merge",
281 update_local_ref(ref
, note
, verbose
);
286 static int fetch_refs(struct transport
*transport
, struct ref
*ref_map
)
288 int ret
= transport_fetch_refs(transport
, ref_map
);
290 store_updated_refs(transport
->url
, ref_map
);
291 transport_unlock_pack(transport
);
295 static int add_existing(const char *refname
, const unsigned char *sha1
,
296 int flag
, void *cbdata
)
298 struct path_list
*list
= (struct path_list
*)cbdata
;
299 path_list_insert(refname
, list
);
303 static struct ref
*find_non_local_tags(struct transport
*transport
,
304 struct ref
*fetch_map
)
306 static struct path_list existing_refs
= { NULL
, 0, 0, 0 };
307 struct path_list new_refs
= { NULL
, 0, 0, 1 };
310 unsigned char *ref_sha1
;
312 struct ref
*rm
= NULL
;
313 struct ref
*ref_map
= NULL
;
314 struct ref
**tail
= &ref_map
;
317 for_each_ref(add_existing
, &existing_refs
);
318 for (ref
= transport_get_remote_refs(transport
); ref
; ref
= ref
->next
) {
319 if (prefixcmp(ref
->name
, "refs/tags"))
322 ref_name
= xstrdup(ref
->name
);
323 ref_name_len
= strlen(ref_name
);
324 ref_sha1
= ref
->old_sha1
;
326 if (!strcmp(ref_name
+ ref_name_len
- 3, "^{}")) {
327 ref_name
[ref_name_len
- 3] = 0;
328 tag_ref
= transport_get_remote_refs(transport
);
330 if (!strcmp(tag_ref
->name
, ref_name
)) {
331 ref_sha1
= tag_ref
->old_sha1
;
334 tag_ref
= tag_ref
->next
;
338 if (!path_list_has_path(&existing_refs
, ref_name
) &&
339 !path_list_has_path(&new_refs
, ref_name
) &&
340 lookup_object(ref
->old_sha1
)) {
341 fprintf(stderr
, "Auto-following %s\n",
344 path_list_insert(ref_name
, &new_refs
);
346 rm
= alloc_ref(strlen(ref_name
) + 1);
347 strcpy(rm
->name
, ref_name
);
348 rm
->peer_ref
= alloc_ref(strlen(ref_name
) + 1);
349 strcpy(rm
->peer_ref
->name
, ref_name
);
350 hashcpy(rm
->old_sha1
, ref_sha1
);
361 static int do_fetch(struct transport
*transport
,
362 struct refspec
*refs
, int ref_count
)
364 struct ref
*ref_map
, *fetch_map
;
366 int autotags
= (transport
->remote
->fetch_tags
== 1);
367 if (transport
->remote
->fetch_tags
== 2 && !no_tags
)
369 if (transport
->remote
->fetch_tags
== -1)
372 if (!transport
->ops
|| !transport
->ops
->get_refs_list
||
373 !transport
->ops
->fetch
)
374 die("Don't know how to fetch from %s", transport
->url
);
376 /* if not appending, truncate FETCH_HEAD */
378 fclose(fopen(git_path("FETCH_HEAD"), "w"));
380 ref_map
= get_ref_map(transport
, refs
, ref_count
, tags
, &autotags
);
382 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
384 read_ref(rm
->peer_ref
->name
, rm
->peer_ref
->old_sha1
);
387 if (fetch_refs(transport
, ref_map
)) {
394 /* if neither --no-tags nor --tags was specified, do automated tag
396 if (!(tags
|| no_tags
) && autotags
) {
397 ref_map
= find_non_local_tags(transport
, fetch_map
);
399 transport_set_option(transport
, TRANS_OPT_DEPTH
, "0");
400 fetch_refs(transport
, ref_map
);
405 free_refs(fetch_map
);
410 int cmd_fetch(int argc
, const char **argv
, const char *prefix
)
412 struct remote
*remote
;
413 int i
, j
, rla_offset
;
414 static const char **refs
= NULL
;
417 const char *depth
= NULL
, *upload_pack
= NULL
;
420 for (i
= 1; i
< argc
; i
++) {
421 const char *arg
= argv
[i
];
422 cmd_len
+= strlen(arg
);
426 if (!strcmp(arg
, "--append") || !strcmp(arg
, "-a")) {
430 if (!prefixcmp(arg
, "--upload-pack=")) {
431 upload_pack
= arg
+ 14;
434 if (!strcmp(arg
, "--upload-pack")) {
438 upload_pack
= argv
[i
];
441 if (!strcmp(arg
, "--force") || !strcmp(arg
, "-f")) {
445 if (!strcmp(arg
, "--no-tags")) {
449 if (!strcmp(arg
, "--tags") || !strcmp(arg
, "-t")) {
453 if (!strcmp(arg
, "--keep") || !strcmp(arg
, "-k")) {
457 if (!strcmp(arg
, "--update-head-ok") || !strcmp(arg
, "-u")) {
461 if (!prefixcmp(arg
, "--depth=")) {
465 if (!strcmp(arg
, "--depth")) {
472 if (!strcmp(arg
, "--quiet")) {
476 if (!strcmp(arg
, "--verbose") || !strcmp(arg
, "-v")) {
483 for (j
= i
; j
< argc
; j
++)
484 cmd_len
+= strlen(argv
[j
]);
486 default_rla
= xmalloc(cmd_len
+ 5 + argc
+ 1);
487 sprintf(default_rla
, "fetch");
488 rla_offset
= strlen(default_rla
);
489 for (j
= 1; j
< argc
; j
++) {
490 sprintf(default_rla
+ rla_offset
, " %s", argv
[j
]);
491 rla_offset
+= strlen(argv
[j
]) + 1;
495 remote
= remote_get(NULL
);
497 remote
= remote_get(argv
[i
++]);
499 transport
= transport_get(remote
, remote
->uri
[0], 1);
501 transport
->verbose
= 1;
503 transport
->verbose
= 0;
505 transport_set_option(transport
, TRANS_OPT_UPLOADPACK
, upload_pack
);
507 transport_set_option(transport
, TRANS_OPT_KEEP
, "yes");
508 transport_set_option(transport
, TRANS_OPT_DEPTH
, depth
);
511 die("Where do you want to fetch from today?");
515 refs
= xcalloc(argc
- i
+ 1, sizeof(const char *));
517 if (!strcmp(argv
[i
], "tag")) {
520 ref
= xmalloc(strlen(argv
[i
]) * 2 + 22);
521 strcpy(ref
, "refs/tags/");
522 strcat(ref
, argv
[i
]);
523 strcat(ref
, ":refs/tags/");
524 strcat(ref
, argv
[i
]);
532 for (j
= 0; refs
[j
]; j
++)
533 printf("ref: %s\n", refs
[j
]);
536 signal(SIGINT
, unlock_pack_on_signal
);
538 return do_fetch(transport
, parse_ref_spec(ref_nr
, refs
), ref_nr
);