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
;
17 static void find_merge_config(struct ref
*ref_map
, struct remote
*remote
)
19 struct ref
*rm
= ref_map
;
20 struct branch
*branch
= branch_get(NULL
);
22 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
23 if (!branch_has_merge_config(branch
)) {
24 if (remote
&& remote
->fetch
&&
25 !strcmp(remote
->fetch
[0].src
, rm
->name
))
28 if (branch_merges(branch
, rm
->name
))
34 static struct ref
*get_ref_map(struct transport
*transport
,
35 struct refspec
*refs
, int ref_count
, int tags
,
40 struct ref
*ref_map
= NULL
;
41 struct ref
**tail
= &ref_map
;
43 struct ref
*remote_refs
= transport_get_remote_refs(transport
);
45 if (ref_count
|| tags
) {
46 for (i
= 0; i
< ref_count
; i
++) {
47 get_fetch_map(remote_refs
, &refs
[i
], &tail
);
48 if (refs
[i
].dst
&& refs
[i
].dst
[0])
51 /* Merge everything on the command line, but not --tags */
52 for (rm
= ref_map
; rm
; rm
= rm
->next
)
55 struct refspec refspec
;
56 refspec
.src
= "refs/tags/";
57 refspec
.dst
= "refs/tags/";
60 get_fetch_map(remote_refs
, &refspec
, &tail
);
63 /* Use the defaults */
64 struct remote
*remote
= transport
->remote
;
65 if (remote
->fetch_refspec_nr
) {
66 for (i
= 0; i
< remote
->fetch_refspec_nr
; i
++) {
67 get_fetch_map(remote_refs
, &remote
->fetch
[i
], &tail
);
68 if (remote
->fetch
[i
].dst
&&
69 remote
->fetch
[i
].dst
[0])
72 find_merge_config(ref_map
, remote
);
74 ref_map
= get_remote_ref(remote_refs
, "HEAD");
83 static void show_new(enum object_type type
, unsigned char *sha1_new
)
85 fprintf(stderr
, " %s: %s\n", typename(type
),
86 find_unique_abbrev(sha1_new
, DEFAULT_ABBREV
));
89 static int s_update_ref(const char *action
,
94 char *rla
= getenv("GIT_REFLOG_ACTION");
95 static struct ref_lock
*lock
;
99 snprintf(msg
, sizeof(msg
), "%s: %s", rla
, action
);
100 lock
= lock_any_ref_for_update(ref
->name
,
101 check_old
? ref
->old_sha1
: NULL
, 0);
104 if (write_ref_sha1(lock
, ref
->new_sha1
, msg
) < 0)
109 static int update_local_ref(struct ref
*ref
,
113 char oldh
[41], newh
[41];
114 struct commit
*current
= NULL
, *updated
;
115 enum object_type type
;
116 struct branch
*current_branch
= branch_get(NULL
);
118 type
= sha1_object_info(ref
->new_sha1
, NULL
);
120 die("object %s not found", sha1_to_hex(ref
->new_sha1
));
125 fprintf(stderr
, "* fetched %s\n", note
);
126 show_new(type
, ref
->new_sha1
);
131 if (!hashcmp(ref
->old_sha1
, ref
->new_sha1
)) {
133 fprintf(stderr
, "* %s: same as %s\n",
135 show_new(type
, ref
->new_sha1
);
140 if (!strcmp(ref
->name
, current_branch
->name
) &&
141 !(update_head_ok
|| is_bare_repository()) &&
142 !is_null_sha1(ref
->old_sha1
)) {
144 * If this is the head, and it's not okay to update
145 * the head, and the old value of the head isn't empty...
148 " * %s: Cannot fetch into the current branch.\n",
153 if (!is_null_sha1(ref
->old_sha1
) &&
154 !prefixcmp(ref
->name
, "refs/tags/")) {
155 fprintf(stderr
, "* %s: updating with %s\n",
157 show_new(type
, ref
->new_sha1
);
158 return s_update_ref("updating tag", ref
, 0);
161 current
= lookup_commit_reference(ref
->old_sha1
);
162 updated
= lookup_commit_reference(ref
->new_sha1
);
163 if (!current
|| !updated
) {
165 if (!strncmp(ref
->name
, "refs/tags/", 10))
168 msg
= "storing head";
169 fprintf(stderr
, "* %s: storing %s\n",
171 show_new(type
, ref
->new_sha1
);
172 return s_update_ref(msg
, ref
, 0);
175 strcpy(oldh
, find_unique_abbrev(current
->object
.sha1
, DEFAULT_ABBREV
));
176 strcpy(newh
, find_unique_abbrev(ref
->new_sha1
, DEFAULT_ABBREV
));
178 if (in_merge_bases(current
, &updated
, 1)) {
179 fprintf(stderr
, "* %s: fast forward to %s\n",
181 fprintf(stderr
, " old..new: %s..%s\n", oldh
, newh
);
182 return s_update_ref("fast forward", ref
, 1);
184 if (!force
&& !ref
->force
) {
186 "* %s: not updating to non-fast forward %s\n",
189 " old...new: %s...%s\n", oldh
, newh
);
193 "* %s: forcing update to non-fast forward %s\n",
195 fprintf(stderr
, " old...new: %s...%s\n", oldh
, newh
);
196 return s_update_ref("forced-update", ref
, 1);
199 static void store_updated_refs(const char *url
, struct ref
*ref_map
)
202 struct commit
*commit
;
203 int url_len
, i
, note_len
;
205 const char *what
, *kind
;
208 fp
= fopen(git_path("FETCH_HEAD"), "a");
209 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
210 struct ref
*ref
= NULL
;
213 ref
= xcalloc(1, sizeof(*ref
) + strlen(rm
->peer_ref
->name
) + 1);
214 strcpy(ref
->name
, rm
->peer_ref
->name
);
215 hashcpy(ref
->old_sha1
, rm
->peer_ref
->old_sha1
);
216 hashcpy(ref
->new_sha1
, rm
->old_sha1
);
217 ref
->force
= rm
->peer_ref
->force
;
220 commit
= lookup_commit_reference(rm
->old_sha1
);
224 if (!strcmp(rm
->name
, "HEAD")) {
228 else if (!prefixcmp(rm
->name
, "refs/heads/")) {
230 what
= rm
->name
+ 11;
232 else if (!prefixcmp(rm
->name
, "refs/tags/")) {
234 what
= rm
->name
+ 10;
236 else if (!prefixcmp(rm
->name
, "refs/remotes/")) {
237 kind
= "remote branch";
238 what
= rm
->name
+ 13;
245 url_len
= strlen(url
);
246 for (i
= url_len
- 1; url
[i
] == '/' && 0 <= i
; i
--)
249 if (4 < i
&& !strncmp(".git", url
+ i
- 3, 4))
255 note_len
+= sprintf(note
+ note_len
, "%s ",
257 note_len
+= sprintf(note
+ note_len
, "'%s' of ", what
);
259 note_len
+= sprintf(note
+ note_len
, "%.*s", url_len
, url
);
260 fprintf(fp
, "%s\t%s\t%s\n",
261 sha1_to_hex(commit
? commit
->object
.sha1
:
263 rm
->merge
? "" : "not-for-merge",
267 update_local_ref(ref
, note
, verbose
);
272 static int fetch_refs(struct transport
*transport
, struct ref
*ref_map
)
274 int ret
= transport_fetch_refs(transport
, ref_map
);
276 store_updated_refs(transport
->url
, ref_map
);
280 static int add_existing(const char *refname
, const unsigned char *sha1
,
281 int flag
, void *cbdata
)
283 struct path_list
*list
= (struct path_list
*)cbdata
;
284 path_list_insert(refname
, list
);
288 static struct ref
*find_non_local_tags(struct transport
*transport
,
289 struct ref
*fetch_map
)
291 static struct path_list existing_refs
= { NULL
, 0, 0, 0 };
292 struct path_list new_refs
= { NULL
, 0, 0, 1 };
295 unsigned char *ref_sha1
;
297 struct ref
*rm
= NULL
;
298 struct ref
*ref_map
= NULL
;
299 struct ref
**tail
= &ref_map
;
302 for_each_ref(add_existing
, &existing_refs
);
303 for (ref
= transport_get_remote_refs(transport
); ref
; ref
= ref
->next
) {
304 if (prefixcmp(ref
->name
, "refs/tags"))
307 ref_name
= xstrdup(ref
->name
);
308 ref_name_len
= strlen(ref_name
);
309 ref_sha1
= ref
->old_sha1
;
311 if (!strcmp(ref_name
+ ref_name_len
- 3, "^{}")) {
312 ref_name
[ref_name_len
- 3] = 0;
313 tag_ref
= transport_get_remote_refs(transport
);
315 if (!strcmp(tag_ref
->name
, ref_name
)) {
316 ref_sha1
= tag_ref
->old_sha1
;
319 tag_ref
= tag_ref
->next
;
323 if (!path_list_has_path(&existing_refs
, ref_name
) &&
324 !path_list_has_path(&new_refs
, ref_name
) &&
325 lookup_object(ref
->old_sha1
)) {
326 fprintf(stderr
, "Auto-following %s\n",
329 path_list_insert(ref_name
, &new_refs
);
331 rm
= alloc_ref(strlen(ref_name
) + 1);
332 strcpy(rm
->name
, ref_name
);
333 rm
->peer_ref
= alloc_ref(strlen(ref_name
) + 1);
334 strcpy(rm
->peer_ref
->name
, ref_name
);
335 hashcpy(rm
->old_sha1
, ref_sha1
);
346 static int do_fetch(struct transport
*transport
,
347 struct refspec
*refs
, int ref_count
)
349 struct ref
*ref_map
, *fetch_map
;
351 int autotags
= (transport
->remote
->fetch_tags
== 1);
352 if (transport
->remote
->fetch_tags
== 2 && !no_tags
)
354 if (transport
->remote
->fetch_tags
== -1)
357 if (!transport
->ops
|| !transport
->ops
->get_refs_list
||
358 !(transport
->ops
->fetch_refs
|| transport
->ops
->fetch_objs
))
359 die("Don't know how to fetch from %s", transport
->url
);
361 /* if not appending, truncate FETCH_HEAD */
363 fclose(fopen(git_path("FETCH_HEAD"), "w"));
365 ref_map
= get_ref_map(transport
, refs
, ref_count
, tags
, &autotags
);
367 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
369 read_ref(rm
->peer_ref
->name
, rm
->peer_ref
->old_sha1
);
372 if (fetch_refs(transport
, ref_map
)) {
379 /* if neither --no-tags nor --tags was specified, do automated tag
381 if (!(tags
|| no_tags
) && autotags
) {
382 ref_map
= find_non_local_tags(transport
, fetch_map
);
384 transport_set_option(transport
, TRANS_OPT_DEPTH
, "0");
385 fetch_refs(transport
, ref_map
);
390 free_refs(fetch_map
);
395 int cmd_fetch(int argc
, const char **argv
, const char *prefix
)
397 struct remote
*remote
;
398 struct transport
*transport
;
399 int i
, j
, rla_offset
;
400 static const char **refs
= NULL
;
403 const char *depth
= NULL
, *upload_pack
= NULL
;
406 for (i
= 1; i
< argc
; i
++) {
407 const char *arg
= argv
[i
];
408 cmd_len
+= strlen(arg
);
412 if (!strcmp(arg
, "--append") || !strcmp(arg
, "-a")) {
416 if (!prefixcmp(arg
, "--upload-pack=")) {
417 upload_pack
= arg
+ 14;
420 if (!strcmp(arg
, "--upload-pack")) {
424 upload_pack
= argv
[i
];
427 if (!strcmp(arg
, "--force") || !strcmp(arg
, "-f")) {
431 if (!strcmp(arg
, "--no-tags")) {
435 if (!strcmp(arg
, "--tags") || !strcmp(arg
, "-t")) {
439 if (!strcmp(arg
, "--keep") || !strcmp(arg
, "-k")) {
443 if (!strcmp(arg
, "--update-head-ok") || !strcmp(arg
, "-u")) {
447 if (!prefixcmp(arg
, "--depth=")) {
451 if (!strcmp(arg
, "--depth")) {
458 if (!strcmp(arg
, "--quiet")) {
462 if (!strcmp(arg
, "--verbose") || !strcmp(arg
, "-v")) {
469 for (j
= i
; j
< argc
; j
++)
470 cmd_len
+= strlen(argv
[j
]);
472 default_rla
= xmalloc(cmd_len
+ 5 + argc
+ 1);
473 sprintf(default_rla
, "fetch");
474 rla_offset
= strlen(default_rla
);
475 for (j
= 1; j
< argc
; j
++) {
476 sprintf(default_rla
+ rla_offset
, " %s", argv
[j
]);
477 rla_offset
+= strlen(argv
[j
]) + 1;
481 remote
= remote_get(NULL
);
483 remote
= remote_get(argv
[i
++]);
485 transport
= transport_get(remote
, remote
->uri
[0], 1);
487 transport
->verbose
= 1;
489 transport
->verbose
= 0;
491 transport_set_option(transport
, TRANS_OPT_UPLOADPACK
, upload_pack
);
493 transport_set_option(transport
, TRANS_OPT_KEEP
, "yes");
494 transport_set_option(transport
, TRANS_OPT_DEPTH
, depth
);
497 die("Where do you want to fetch from today?");
501 refs
= xcalloc(argc
- i
+ 1, sizeof(const char *));
503 if (!strcmp(argv
[i
], "tag")) {
506 ref
= xmalloc(strlen(argv
[i
]) * 2 + 22);
507 strcpy(ref
, "refs/tags/");
508 strcat(ref
, argv
[i
]);
509 strcat(ref
, ":refs/tags/");
510 strcat(ref
, argv
[i
]);
518 for (j
= 0; refs
[j
]; j
++)
519 printf("ref: %s\n", refs
[j
]);
522 return do_fetch(transport
, parse_ref_spec(ref_nr
, refs
), ref_nr
);