blob: 357f3cdbbfd601e2ce3f1261a4d6b30c1257cda4 [file] [log] [blame]
Daniel Barkalowb888d612007-09-10 23:03:25 -04001/*
2 * "git fetch"
3 */
4#include "cache.h"
5#include "refs.h"
6#include "commit.h"
7#include "builtin.h"
Johannes Schindelinc455c872008-07-21 19:03:49 +01008#include "string-list.h"
Daniel Barkalowb888d612007-09-10 23:03:25 -04009#include "remote.h"
10#include "transport.h"
Shawn O. Pearce4191c352007-11-11 02:29:47 -050011#include "run-command.h"
Kristian Høgsberg83201992007-12-04 02:25:47 -050012#include "parse-options.h"
Jeff King4a16d072009-01-22 01:02:35 -050013#include "sigchain.h"
Michael Lukashovf1863d02010-02-16 23:42:52 +000014#include "transport.h"
Jens Lehmann7dce19d2010-11-12 13:54:52 +010015#include "submodule.h"
Daniel Barkalowb888d612007-09-10 23:03:25 -040016
Kristian Høgsberg83201992007-12-04 02:25:47 -050017static const char * const builtin_fetch_usage[] = {
Tay Ray Chuane3163c72010-04-10 10:50:19 +080018 "git fetch [<options>] [<repository> [<refspec>...]]",
19 "git fetch [<options>] <group>",
Štěpán Němec0adda932010-10-08 19:31:17 +020020 "git fetch --multiple [<options>] [(<repository> | <group>)...]",
Tay Ray Chuane3163c72010-04-10 10:50:19 +080021 "git fetch --all [<options>]",
Kristian Høgsberg83201992007-12-04 02:25:47 -050022 NULL
23};
Daniel Barkalowb888d612007-09-10 23:03:25 -040024
Kristian Høgsberg83201992007-12-04 02:25:47 -050025enum {
26 TAGS_UNSET = 0,
27 TAGS_DEFAULT = 1,
28 TAGS_SET = 2
29};
30
Jens Lehmannbe254a02010-11-11 00:55:02 +010031enum {
32 RECURSE_SUBMODULES_OFF = 0,
33 RECURSE_SUBMODULES_DEFAULT = 1,
34 RECURSE_SUBMODULES_ON = 2
35};
36
Jay Soffian28a15402009-11-10 09:19:43 +010037static int all, append, dry_run, force, keep, multiple, prune, update_head_ok, verbosity;
Jens Lehmannbe254a02010-11-11 00:55:02 +010038static int progress, recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
Kristian Høgsberg83201992007-12-04 02:25:47 -050039static int tags = TAGS_DEFAULT;
Shawn O. Pearce4191c352007-11-11 02:29:47 -050040static const char *depth;
Kristian Høgsberg83201992007-12-04 02:25:47 -050041static const char *upload_pack;
Kristian Høgsberg2d324ef2007-12-04 02:25:46 -050042static struct strbuf default_rla = STRBUF_INIT;
Shawn O. Pearcee4022ed2007-09-14 03:31:25 -040043static struct transport *transport;
Jens Lehmann7dce19d2010-11-12 13:54:52 +010044static const char *submodule_prefix = "";
Shawn O. Pearcee4022ed2007-09-14 03:31:25 -040045
Kristian Høgsberg83201992007-12-04 02:25:47 -050046static struct option builtin_fetch_options[] = {
Tuncer Ayaz7f87aff2008-11-15 01:14:24 +010047 OPT__VERBOSITY(&verbosity),
Björn Gustavsson9c4a0362009-11-09 21:09:56 +010048 OPT_BOOLEAN(0, "all", &all,
49 "fetch from all remotes"),
Kristian Høgsberg83201992007-12-04 02:25:47 -050050 OPT_BOOLEAN('a', "append", &append,
51 "append to .git/FETCH_HEAD instead of overwriting"),
52 OPT_STRING(0, "upload-pack", &upload_pack, "PATH",
53 "path to upload pack on remote end"),
René Scharfe76946b72010-11-08 19:01:54 +010054 OPT__FORCE(&force, "force overwrite of local branch"),
Björn Gustavsson16679e32009-11-09 21:10:32 +010055 OPT_BOOLEAN('m', "multiple", &multiple,
56 "fetch from multiple remotes"),
Kristian Høgsberg83201992007-12-04 02:25:47 -050057 OPT_SET_INT('t', "tags", &tags,
58 "fetch all tags and associated objects", TAGS_SET),
Johannes Schindeline7951292008-03-13 08:13:15 +010059 OPT_SET_INT('n', NULL, &tags,
60 "do not fetch all tags (--no-tags)", TAGS_UNSET),
Jay Soffianf360d842009-11-10 09:15:47 +010061 OPT_BOOLEAN('p', "prune", &prune,
Matthieu Moy8b3f3f82010-11-02 16:31:23 +010062 "prune remote-tracking branches no longer on remote"),
Jens Lehmannbe254a02010-11-11 00:55:02 +010063 OPT_SET_INT(0, "recurse-submodules", &recurse_submodules,
64 "control recursive fetching of submodules",
65 RECURSE_SUBMODULES_ON),
Jay Soffian28a15402009-11-10 09:19:43 +010066 OPT_BOOLEAN(0, "dry-run", &dry_run,
67 "dry run"),
Kristian Høgsberg83201992007-12-04 02:25:47 -050068 OPT_BOOLEAN('k', "keep", &keep, "keep downloaded pack"),
69 OPT_BOOLEAN('u', "update-head-ok", &update_head_ok,
70 "allow updating of HEAD ref"),
Tay Ray Chuan98390182010-02-24 20:50:28 +080071 OPT_BOOLEAN(0, "progress", &progress, "force progress reporting"),
Kristian Høgsberg83201992007-12-04 02:25:47 -050072 OPT_STRING(0, "depth", &depth, "DEPTH",
73 "deepen history of shallow clone"),
Jens Lehmann7dce19d2010-11-12 13:54:52 +010074 { OPTION_STRING, 0, "submodule-prefix", &submodule_prefix, "DIR",
75 "prepend this to submodule path output", PARSE_OPT_HIDDEN },
Kristian Høgsberg83201992007-12-04 02:25:47 -050076 OPT_END()
77};
78
Shawn O. Pearcee4022ed2007-09-14 03:31:25 -040079static void unlock_pack(void)
80{
81 if (transport)
82 transport_unlock_pack(transport);
83}
84
85static void unlock_pack_on_signal(int signo)
86{
87 unlock_pack();
Jeff King4a16d072009-01-22 01:02:35 -050088 sigchain_pop(signo);
Shawn O. Pearcee4022ed2007-09-14 03:31:25 -040089 raise(signo);
90}
Daniel Barkalowb888d612007-09-10 23:03:25 -040091
Shawn O. Pearce85682c12007-09-18 04:54:53 -040092static void add_merge_config(struct ref **head,
Daniel Barkalow45773702007-10-29 21:05:40 -040093 const struct ref *remote_refs,
Shawn O. Pearce85682c12007-09-18 04:54:53 -040094 struct branch *branch,
95 struct ref ***tail)
Daniel Barkalowb888d612007-09-10 23:03:25 -040096{
Shawn O. Pearce85682c12007-09-18 04:54:53 -040097 int i;
Daniel Barkalowb888d612007-09-10 23:03:25 -040098
Shawn O. Pearce85682c12007-09-18 04:54:53 -040099 for (i = 0; i < branch->merge_nr; i++) {
100 struct ref *rm, **old_tail = *tail;
101 struct refspec refspec;
102
103 for (rm = *head; rm; rm = rm->next) {
104 if (branch_merge_matches(branch, i, rm->name)) {
Daniel Barkalowb888d612007-09-10 23:03:25 -0400105 rm->merge = 1;
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400106 break;
107 }
Daniel Barkalowb888d612007-09-10 23:03:25 -0400108 }
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400109 if (rm)
110 continue;
111
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700112 /*
Matthieu Moy8b3f3f82010-11-02 16:31:23 +0100113 * Not fetched to a remote-tracking branch? We need to fetch
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400114 * it anyway to allow this branch's "branch.$name.merge"
Heikki Orsila05207a22008-09-09 13:28:30 +0300115 * to be honored by 'git pull', but we do not have to
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700116 * fail if branch.$name.merge is misconfigured to point
117 * at a nonexisting branch. If we were indeed called by
Heikki Orsila05207a22008-09-09 13:28:30 +0300118 * 'git pull', it will notice the misconfiguration because
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700119 * there is no entry in the resulting FETCH_HEAD marked
120 * for merging.
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400121 */
Andreas Gruenbacher8da61a22010-03-12 23:27:33 +0100122 memset(&refspec, 0, sizeof(refspec));
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400123 refspec.src = branch->merge[i]->src;
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700124 get_fetch_map(remote_refs, &refspec, tail, 1);
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400125 for (rm = *old_tail; rm; rm = rm->next)
126 rm->merge = 1;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400127 }
128}
129
Shawn O. Pearce767f1762008-03-02 21:35:25 -0500130static void find_non_local_tags(struct transport *transport,
131 struct ref **head,
132 struct ref ***tail);
133
Daniel Barkalowb888d612007-09-10 23:03:25 -0400134static struct ref *get_ref_map(struct transport *transport,
135 struct refspec *refs, int ref_count, int tags,
136 int *autotags)
137{
138 int i;
139 struct ref *rm;
140 struct ref *ref_map = NULL;
141 struct ref **tail = &ref_map;
142
Daniel Barkalow45773702007-10-29 21:05:40 -0400143 const struct ref *remote_refs = transport_get_remote_refs(transport);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400144
Kristian Høgsberg83201992007-12-04 02:25:47 -0500145 if (ref_count || tags == TAGS_SET) {
Daniel Barkalowb888d612007-09-10 23:03:25 -0400146 for (i = 0; i < ref_count; i++) {
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700147 get_fetch_map(remote_refs, &refs[i], &tail, 0);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400148 if (refs[i].dst && refs[i].dst[0])
149 *autotags = 1;
150 }
151 /* Merge everything on the command line, but not --tags */
152 for (rm = ref_map; rm; rm = rm->next)
153 rm->merge = 1;
Daniel Barkalowe0aaa292008-04-17 19:32:35 -0400154 if (tags == TAGS_SET)
155 get_fetch_map(remote_refs, tag_refspec, &tail, 0);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400156 } else {
157 /* Use the defaults */
158 struct remote *remote = transport->remote;
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400159 struct branch *branch = branch_get(NULL);
160 int has_merge = branch_has_merge_config(branch);
Brandon Casey3ee17572010-08-25 12:52:56 -0500161 if (remote &&
162 (remote->fetch_refspec_nr ||
Brandon Caseyf31dbdc2010-09-09 13:56:36 -0500163 /* Note: has_merge implies non-NULL branch->remote_name */
Brandon Casey3ee17572010-08-25 12:52:56 -0500164 (has_merge && !strcmp(branch->remote_name, remote->name)))) {
Daniel Barkalowb888d612007-09-10 23:03:25 -0400165 for (i = 0; i < remote->fetch_refspec_nr; i++) {
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700166 get_fetch_map(remote_refs, &remote->fetch[i], &tail, 0);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400167 if (remote->fetch[i].dst &&
168 remote->fetch[i].dst[0])
169 *autotags = 1;
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400170 if (!i && !has_merge && ref_map &&
Daniel Barkalowcfb8f892007-09-28 19:34:17 -0400171 !remote->fetch[0].pattern)
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400172 ref_map->merge = 1;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400173 }
Johannes Schindelinda0204d2007-10-11 01:47:55 +0100174 /*
175 * if the remote we're fetching from is the same
176 * as given in branch.<name>.remote, we add the
177 * ref given in branch.<name>.merge, too.
Brandon Caseyf31dbdc2010-09-09 13:56:36 -0500178 *
179 * Note: has_merge implies non-NULL branch->remote_name
Johannes Schindelinda0204d2007-10-11 01:47:55 +0100180 */
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700181 if (has_merge &&
182 !strcmp(branch->remote_name, remote->name))
Shawn O. Pearce85682c12007-09-18 04:54:53 -0400183 add_merge_config(&ref_map, remote_refs, branch, &tail);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400184 } else {
185 ref_map = get_remote_ref(remote_refs, "HEAD");
Junio C Hamano9ad7c5a2007-10-26 23:09:48 -0700186 if (!ref_map)
187 die("Couldn't find remote ref HEAD");
Daniel Barkalowb888d612007-09-10 23:03:25 -0400188 ref_map->merge = 1;
Shawn O. Pearce5aaf7f22008-03-02 21:34:51 -0500189 tail = &ref_map->next;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400190 }
191 }
Shawn O. Pearce767f1762008-03-02 21:35:25 -0500192 if (tags == TAGS_DEFAULT && *autotags)
193 find_non_local_tags(transport, &ref_map, &tail);
Daniel Barkalow2467a4f2007-10-08 00:25:07 -0400194 ref_remove_duplicates(ref_map);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400195
196 return ref_map;
197}
198
Jeff Kingfa250752009-05-25 06:40:54 -0400199#define STORE_REF_ERROR_OTHER 1
200#define STORE_REF_ERROR_DF_CONFLICT 2
201
Daniel Barkalowb888d612007-09-10 23:03:25 -0400202static int s_update_ref(const char *action,
203 struct ref *ref,
204 int check_old)
205{
206 char msg[1024];
207 char *rla = getenv("GIT_REFLOG_ACTION");
208 static struct ref_lock *lock;
209
Jay Soffian28a15402009-11-10 09:19:43 +0100210 if (dry_run)
211 return 0;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400212 if (!rla)
Kristian Høgsberg2d324ef2007-12-04 02:25:46 -0500213 rla = default_rla.buf;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400214 snprintf(msg, sizeof(msg), "%s: %s", rla, action);
215 lock = lock_any_ref_for_update(ref->name,
216 check_old ? ref->old_sha1 : NULL, 0);
217 if (!lock)
Jeff Kingfa250752009-05-25 06:40:54 -0400218 return errno == ENOTDIR ? STORE_REF_ERROR_DF_CONFLICT :
219 STORE_REF_ERROR_OTHER;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400220 if (write_ref_sha1(lock, ref->new_sha1, msg) < 0)
Jeff Kingfa250752009-05-25 06:40:54 -0400221 return errno == ENOTDIR ? STORE_REF_ERROR_DF_CONFLICT :
222 STORE_REF_ERROR_OTHER;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400223 return 0;
224}
225
Pierre Habouzit9ef42722007-11-04 15:05:45 -0800226#define REFCOL_WIDTH 10
Nicolas Pitre165f3902007-11-03 01:32:48 -0400227
Daniel Barkalowb888d612007-09-10 23:03:25 -0400228static int update_local_ref(struct ref *ref,
Nicolas Pitre165f3902007-11-03 01:32:48 -0400229 const char *remote,
Nicolas Pitre165f3902007-11-03 01:32:48 -0400230 char *display)
Daniel Barkalowb888d612007-09-10 23:03:25 -0400231{
Daniel Barkalowb888d612007-09-10 23:03:25 -0400232 struct commit *current = NULL, *updated;
233 enum object_type type;
234 struct branch *current_branch = branch_get(NULL);
Felipe Contreras4577e482009-05-14 00:22:04 +0300235 const char *pretty_ref = prettify_refname(ref->name);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400236
Nicolas Pitre165f3902007-11-03 01:32:48 -0400237 *display = 0;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400238 type = sha1_object_info(ref->new_sha1, NULL);
239 if (type < 0)
240 die("object %s not found", sha1_to_hex(ref->new_sha1));
241
Daniel Barkalowb888d612007-09-10 23:03:25 -0400242 if (!hashcmp(ref->old_sha1, ref->new_sha1)) {
Tuncer Ayaz7f87aff2008-11-15 01:14:24 +0100243 if (verbosity > 0)
Michael Lukashovf1863d02010-02-16 23:42:52 +0000244 sprintf(display, "= %-*s %-*s -> %s", TRANSPORT_SUMMARY_WIDTH,
Pierre Habouzit9ef42722007-11-04 15:05:45 -0800245 "[up to date]", REFCOL_WIDTH, remote,
246 pretty_ref);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400247 return 0;
248 }
249
Shawn O. Pearceb3abdd92007-09-16 02:31:26 -0400250 if (current_branch &&
251 !strcmp(ref->name, current_branch->name) &&
Daniel Barkalowb888d612007-09-10 23:03:25 -0400252 !(update_head_ok || is_bare_repository()) &&
253 !is_null_sha1(ref->old_sha1)) {
254 /*
255 * If this is the head, and it's not okay to update
256 * the head, and the old value of the head isn't empty...
257 */
Pierre Habouzit9ef42722007-11-04 15:05:45 -0800258 sprintf(display, "! %-*s %-*s -> %s (can't fetch in current branch)",
Michael Lukashovf1863d02010-02-16 23:42:52 +0000259 TRANSPORT_SUMMARY_WIDTH, "[rejected]", REFCOL_WIDTH, remote,
Pierre Habouzit9ef42722007-11-04 15:05:45 -0800260 pretty_ref);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400261 return 1;
262 }
263
264 if (!is_null_sha1(ref->old_sha1) &&
265 !prefixcmp(ref->name, "refs/tags/")) {
Jeff King63154722008-06-26 23:59:50 -0400266 int r;
267 r = s_update_ref("updating tag", ref, 0);
268 sprintf(display, "%c %-*s %-*s -> %s%s", r ? '!' : '-',
Michael Lukashovf1863d02010-02-16 23:42:52 +0000269 TRANSPORT_SUMMARY_WIDTH, "[tag update]", REFCOL_WIDTH, remote,
Jeff King63154722008-06-26 23:59:50 -0400270 pretty_ref, r ? " (unable to update local ref)" : "");
271 return r;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400272 }
273
Shawn O. Pearcecfa5b2b2007-10-19 00:54:59 -0400274 current = lookup_commit_reference_gently(ref->old_sha1, 1);
275 updated = lookup_commit_reference_gently(ref->new_sha1, 1);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400276 if (!current || !updated) {
Nicolas Pitre165f3902007-11-03 01:32:48 -0400277 const char *msg;
278 const char *what;
Jeff King63154722008-06-26 23:59:50 -0400279 int r;
Nicolas Pitre165f3902007-11-03 01:32:48 -0400280 if (!strncmp(ref->name, "refs/tags/", 10)) {
Daniel Barkalowb888d612007-09-10 23:03:25 -0400281 msg = "storing tag";
Nicolas Pitre165f3902007-11-03 01:32:48 -0400282 what = "[new tag]";
283 }
284 else {
Daniel Barkalowb888d612007-09-10 23:03:25 -0400285 msg = "storing head";
Nicolas Pitre165f3902007-11-03 01:32:48 -0400286 what = "[new branch]";
287 }
288
Jeff King63154722008-06-26 23:59:50 -0400289 r = s_update_ref(msg, ref, 0);
290 sprintf(display, "%c %-*s %-*s -> %s%s", r ? '!' : '*',
Michael Lukashovf1863d02010-02-16 23:42:52 +0000291 TRANSPORT_SUMMARY_WIDTH, what, REFCOL_WIDTH, remote, pretty_ref,
Jeff King63154722008-06-26 23:59:50 -0400292 r ? " (unable to update local ref)" : "");
293 return r;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400294 }
295
Daniel Barkalowb888d612007-09-10 23:03:25 -0400296 if (in_merge_bases(current, &updated, 1)) {
Nicolas Pitre165f3902007-11-03 01:32:48 -0400297 char quickref[83];
Jeff King63154722008-06-26 23:59:50 -0400298 int r;
Nicolas Pitre165f3902007-11-03 01:32:48 -0400299 strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
300 strcat(quickref, "..");
301 strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
Felipe Contrerasa75d7b52009-10-24 11:31:32 +0300302 r = s_update_ref("fast-forward", ref, 1);
Jeff King63154722008-06-26 23:59:50 -0400303 sprintf(display, "%c %-*s %-*s -> %s%s", r ? '!' : ' ',
Michael Lukashovf1863d02010-02-16 23:42:52 +0000304 TRANSPORT_SUMMARY_WIDTH, quickref, REFCOL_WIDTH, remote,
Jeff King63154722008-06-26 23:59:50 -0400305 pretty_ref, r ? " (unable to update local ref)" : "");
306 return r;
Nicolas Pitre165f3902007-11-03 01:32:48 -0400307 } else if (force || ref->force) {
308 char quickref[84];
Jeff King63154722008-06-26 23:59:50 -0400309 int r;
Nicolas Pitre165f3902007-11-03 01:32:48 -0400310 strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
311 strcat(quickref, "...");
312 strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
Jeff King63154722008-06-26 23:59:50 -0400313 r = s_update_ref("forced-update", ref, 1);
314 sprintf(display, "%c %-*s %-*s -> %s (%s)", r ? '!' : '+',
Michael Lukashovf1863d02010-02-16 23:42:52 +0000315 TRANSPORT_SUMMARY_WIDTH, quickref, REFCOL_WIDTH, remote,
Jeff King63154722008-06-26 23:59:50 -0400316 pretty_ref,
317 r ? "unable to update local ref" : "forced update");
318 return r;
Nicolas Pitre165f3902007-11-03 01:32:48 -0400319 } else {
Felipe Contrerasa75d7b52009-10-24 11:31:32 +0300320 sprintf(display, "! %-*s %-*s -> %s (non-fast-forward)",
Michael Lukashovf1863d02010-02-16 23:42:52 +0000321 TRANSPORT_SUMMARY_WIDTH, "[rejected]", REFCOL_WIDTH, remote,
Pierre Habouzit9ef42722007-11-04 15:05:45 -0800322 pretty_ref);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400323 return 1;
324 }
Daniel Barkalowb888d612007-09-10 23:03:25 -0400325}
326
Andreas Ericsson47abd852009-04-17 10:20:11 +0200327static int store_updated_refs(const char *raw_url, const char *remote_name,
Jeff Kingf3cb1692008-06-27 00:01:41 -0400328 struct ref *ref_map)
Daniel Barkalowb888d612007-09-10 23:03:25 -0400329{
330 FILE *fp;
331 struct commit *commit;
Dmitry V. Levinefb98b42008-05-28 19:29:36 +0400332 int url_len, i, note_len, shown_url = 0, rc = 0;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400333 char note[1024];
334 const char *what, *kind;
335 struct ref *rm;
Jay Soffian28a15402009-11-10 09:19:43 +0100336 char *url, *filename = dry_run ? "/dev/null" : git_path("FETCH_HEAD");
Daniel Barkalowb888d612007-09-10 23:03:25 -0400337
André Goddard Rosad6617c72007-11-22 20:22:23 -0200338 fp = fopen(filename, "a");
339 if (!fp)
340 return error("cannot open %s: %s\n", filename, strerror(errno));
Andreas Ericsson47abd852009-04-17 10:20:11 +0200341
Daniel Barkalowfb0cc872009-11-18 02:42:22 +0100342 if (raw_url)
343 url = transport_anonymize_url(raw_url);
344 else
345 url = xstrdup("foreign");
Daniel Barkalowb888d612007-09-10 23:03:25 -0400346 for (rm = ref_map; rm; rm = rm->next) {
347 struct ref *ref = NULL;
348
349 if (rm->peer_ref) {
350 ref = xcalloc(1, sizeof(*ref) + strlen(rm->peer_ref->name) + 1);
351 strcpy(ref->name, rm->peer_ref->name);
352 hashcpy(ref->old_sha1, rm->peer_ref->old_sha1);
353 hashcpy(ref->new_sha1, rm->old_sha1);
Shawn O. Pearce1aad91f2007-09-14 03:31:07 -0400354 ref->force = rm->peer_ref->force;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400355 }
356
Shawn O. Pearcecfa5b2b2007-10-19 00:54:59 -0400357 commit = lookup_commit_reference_gently(rm->old_sha1, 1);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400358 if (!commit)
359 rm->merge = 0;
360
361 if (!strcmp(rm->name, "HEAD")) {
362 kind = "";
363 what = "";
364 }
365 else if (!prefixcmp(rm->name, "refs/heads/")) {
366 kind = "branch";
367 what = rm->name + 11;
368 }
369 else if (!prefixcmp(rm->name, "refs/tags/")) {
370 kind = "tag";
371 what = rm->name + 10;
372 }
373 else if (!prefixcmp(rm->name, "refs/remotes/")) {
Matthieu Moy13931232010-11-02 16:31:25 +0100374 kind = "remote-tracking branch";
Daniel Barkalowb888d612007-09-10 23:03:25 -0400375 what = rm->name + 13;
376 }
377 else {
378 kind = "";
379 what = rm->name;
380 }
381
382 url_len = strlen(url);
383 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
384 ;
385 url_len = i + 1;
386 if (4 < i && !strncmp(".git", url + i - 3, 4))
387 url_len = i - 3;
388
389 note_len = 0;
390 if (*what) {
391 if (*kind)
392 note_len += sprintf(note + note_len, "%s ",
393 kind);
394 note_len += sprintf(note + note_len, "'%s' of ", what);
395 }
Alex Riesen95405ba2009-05-13 20:08:53 +0200396 note[note_len] = '\0';
397 fprintf(fp, "%s\t%s\t%s",
Daniel Barkalowb888d612007-09-10 23:03:25 -0400398 sha1_to_hex(commit ? commit->object.sha1 :
399 rm->old_sha1),
400 rm->merge ? "" : "not-for-merge",
401 note);
Alex Riesen95405ba2009-05-13 20:08:53 +0200402 for (i = 0; i < url_len; ++i)
403 if ('\n' == url[i])
404 fputs("\\n", fp);
405 else
406 fputc(url[i], fp);
407 fputc('\n', fp);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400408
Andreas Gruenbacher730b0202010-03-15 23:18:48 +0100409 if (ref) {
Tuncer Ayaz7f87aff2008-11-15 01:14:24 +0100410 rc |= update_local_ref(ref, what, note);
Andreas Gruenbacher730b0202010-03-15 23:18:48 +0100411 free(ref);
412 } else
Jeff Kingf59774a2008-04-09 20:03:49 -0400413 sprintf(note, "* %-*s %-*s -> FETCH_HEAD",
Michael Lukashovf1863d02010-02-16 23:42:52 +0000414 TRANSPORT_SUMMARY_WIDTH, *kind ? kind : "branch",
Jeff Kingf59774a2008-04-09 20:03:49 -0400415 REFCOL_WIDTH, *what ? what : "HEAD");
Jeff Kingf59774a2008-04-09 20:03:49 -0400416 if (*note) {
Tuncer Ayaz7f87aff2008-11-15 01:14:24 +0100417 if (verbosity >= 0 && !shown_url) {
Jeff Kingf59774a2008-04-09 20:03:49 -0400418 fprintf(stderr, "From %.*s\n",
419 url_len, url);
420 shown_url = 1;
Nicolas Pitre165f3902007-11-03 01:32:48 -0400421 }
Tuncer Ayaz7f87aff2008-11-15 01:14:24 +0100422 if (verbosity >= 0)
423 fprintf(stderr, " %s\n", note);
Nicolas Pitre165f3902007-11-03 01:32:48 -0400424 }
Daniel Barkalowb888d612007-09-10 23:03:25 -0400425 }
Andreas Ericsson47abd852009-04-17 10:20:11 +0200426 free(url);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400427 fclose(fp);
Jeff Kingfa250752009-05-25 06:40:54 -0400428 if (rc & STORE_REF_ERROR_DF_CONFLICT)
Jeff Kingf3cb1692008-06-27 00:01:41 -0400429 error("some local refs could not be updated; try running\n"
430 " 'git remote prune %s' to remove any old, conflicting "
431 "branches", remote_name);
Dmitry V. Levinefb98b42008-05-28 19:29:36 +0400432 return rc;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400433}
434
Shawn O. Pearce4191c352007-11-11 02:29:47 -0500435/*
436 * We would want to bypass the object transfer altogether if
Johan Herlandd9eb0202009-07-10 01:52:30 +0200437 * everything we are going to fetch already exists and is connected
Shawn O. Pearce4191c352007-11-11 02:29:47 -0500438 * locally.
439 *
Johan Herlandd9eb0202009-07-10 01:52:30 +0200440 * The refs we are going to fetch are in ref_map. If running
Shawn O. Pearce4191c352007-11-11 02:29:47 -0500441 *
Johan Herlandd9eb0202009-07-10 01:52:30 +0200442 * $ git rev-list --objects --stdin --not --all
Shawn O. Pearce4191c352007-11-11 02:29:47 -0500443 *
Johan Herlandd9eb0202009-07-10 01:52:30 +0200444 * (feeding all the refs in ref_map on its standard input)
Shawn O. Pearce4191c352007-11-11 02:29:47 -0500445 * does not error out, that means everything reachable from the
446 * refs we are going to fetch exists and is connected to some of
447 * our existing refs.
448 */
449static int quickfetch(struct ref *ref_map)
450{
451 struct child_process revlist;
452 struct ref *ref;
Johan Herlandd9eb0202009-07-10 01:52:30 +0200453 int err;
454 const char *argv[] = {"rev-list",
455 "--quiet", "--objects", "--stdin", "--not", "--all", NULL};
Shawn O. Pearce4191c352007-11-11 02:29:47 -0500456
457 /*
458 * If we are deepening a shallow clone we already have these
459 * objects reachable. Running rev-list here will return with
460 * a good (0) exit status and we'll bypass the fetch that we
461 * really need to perform. Claiming failure now will ensure
462 * we perform the network exchange to deepen our history.
463 */
464 if (depth)
465 return -1;
466
Johan Herlandd9eb0202009-07-10 01:52:30 +0200467 if (!ref_map)
Shawn O. Pearce4191c352007-11-11 02:29:47 -0500468 return 0;
469
Shawn O. Pearce4191c352007-11-11 02:29:47 -0500470 memset(&revlist, 0, sizeof(revlist));
Johan Herlandd9eb0202009-07-10 01:52:30 +0200471 revlist.argv = argv;
Shawn O. Pearce4191c352007-11-11 02:29:47 -0500472 revlist.git_cmd = 1;
Shawn O. Pearce4191c352007-11-11 02:29:47 -0500473 revlist.no_stdout = 1;
474 revlist.no_stderr = 1;
Johan Herlandd9eb0202009-07-10 01:52:30 +0200475 revlist.in = -1;
Shawn O. Pearce4191c352007-11-11 02:29:47 -0500476
Johan Herlandd9eb0202009-07-10 01:52:30 +0200477 err = start_command(&revlist);
478 if (err) {
479 error("could not run rev-list");
480 return err;
481 }
482
483 /*
484 * If rev-list --stdin encounters an unknown commit, it terminates,
485 * which will cause SIGPIPE in the write loop below.
486 */
487 sigchain_push(SIGPIPE, SIG_IGN);
488
489 for (ref = ref_map; ref; ref = ref->next) {
490 if (write_in_full(revlist.in, sha1_to_hex(ref->old_sha1), 40) < 0 ||
Jim Meyering2b7ca832009-09-12 10:54:32 +0200491 write_str_in_full(revlist.in, "\n") < 0) {
Johan Herlandd9eb0202009-07-10 01:52:30 +0200492 if (errno != EPIPE && errno != EINVAL)
493 error("failed write to rev-list: %s", strerror(errno));
494 err = -1;
495 break;
496 }
497 }
498
499 if (close(revlist.in)) {
500 error("failed to close rev-list's stdin: %s", strerror(errno));
501 err = -1;
502 }
503
504 sigchain_pop(SIGPIPE);
505
506 return finish_command(&revlist) || err;
Shawn O. Pearce4191c352007-11-11 02:29:47 -0500507}
508
Daniel Barkalowb888d612007-09-10 23:03:25 -0400509static int fetch_refs(struct transport *transport, struct ref *ref_map)
510{
Shawn O. Pearce4191c352007-11-11 02:29:47 -0500511 int ret = quickfetch(ref_map);
512 if (ret)
513 ret = transport_fetch_refs(transport, ref_map);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400514 if (!ret)
Jeff Kingf3cb1692008-06-27 00:01:41 -0400515 ret |= store_updated_refs(transport->url,
516 transport->remote->name,
517 ref_map);
Shawn O. Pearce1788c392007-09-14 03:31:23 -0400518 transport_unlock_pack(transport);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400519 return ret;
520}
521
Jay Soffianf360d842009-11-10 09:15:47 +0100522static int prune_refs(struct transport *transport, struct ref *ref_map)
523{
524 int result = 0;
525 struct ref *ref, *stale_refs = get_stale_heads(transport->remote, ref_map);
526 const char *dangling_msg = dry_run
527 ? " (%s will become dangling)\n"
528 : " (%s has become dangling)\n";
529
530 for (ref = stale_refs; ref; ref = ref->next) {
531 if (!dry_run)
532 result |= delete_ref(ref->name, NULL, 0);
533 if (verbosity >= 0) {
534 fprintf(stderr, " x %-*s %-*s -> %s\n",
Michael Lukashovf1863d02010-02-16 23:42:52 +0000535 TRANSPORT_SUMMARY_WIDTH, "[deleted]",
Jay Soffianf360d842009-11-10 09:15:47 +0100536 REFCOL_WIDTH, "(none)", prettify_refname(ref->name));
537 warn_dangling_symref(stderr, dangling_msg, ref->name);
538 }
539 }
540 free_refs(stale_refs);
541 return result;
542}
543
Daniel Barkalowb888d612007-09-10 23:03:25 -0400544static int add_existing(const char *refname, const unsigned char *sha1,
545 int flag, void *cbdata)
546{
Johannes Schindelinc455c872008-07-21 19:03:49 +0100547 struct string_list *list = (struct string_list *)cbdata;
Julian Phillips78a395d2010-06-26 00:41:35 +0100548 struct string_list_item *item = string_list_insert(list, refname);
Julian Phillipsb1a01e12009-10-25 21:28:12 +0000549 item->util = (void *)sha1;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400550 return 0;
551}
552
Shawn O. Pearcecf7f9292008-03-02 21:35:33 -0500553static int will_fetch(struct ref **head, const unsigned char *sha1)
554{
555 struct ref *rm = *head;
556 while (rm) {
557 if (!hashcmp(rm->old_sha1, sha1))
558 return 1;
559 rm = rm->next;
560 }
561 return 0;
562}
563
Shawn O. Pearcec50b2b42008-03-02 21:35:00 -0500564static void find_non_local_tags(struct transport *transport,
565 struct ref **head,
566 struct ref ***tail)
Daniel Barkalowb888d612007-09-10 23:03:25 -0400567{
Thiago Farina183113a2010-07-04 16:46:19 -0300568 struct string_list existing_refs = STRING_LIST_INIT_NODUP;
569 struct string_list remote_refs = STRING_LIST_INIT_NODUP;
Daniel Barkalow45773702007-10-29 21:05:40 -0400570 const struct ref *ref;
Julian Phillipse984c542009-09-17 08:33:19 +0100571 struct string_list_item *item = NULL;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400572
573 for_each_ref(add_existing, &existing_refs);
574 for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
575 if (prefixcmp(ref->name, "refs/tags"))
576 continue;
577
Julian Phillipse984c542009-09-17 08:33:19 +0100578 /*
579 * The peeled ref always follows the matching base
580 * ref, so if we see a peeled ref that we don't want
581 * to fetch then we can mark the ref entry in the list
582 * as one to ignore by setting util to NULL.
583 */
Andreas Gruenbacheraac1d7b2010-03-13 18:17:04 +0100584 if (!suffixcmp(ref->name, "^{}")) {
Julian Phillipse984c542009-09-17 08:33:19 +0100585 if (item && !has_sha1_file(ref->old_sha1) &&
586 !will_fetch(head, ref->old_sha1) &&
587 !has_sha1_file(item->util) &&
588 !will_fetch(head, item->util))
589 item->util = NULL;
590 item = NULL;
591 continue;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400592 }
593
Julian Phillipse984c542009-09-17 08:33:19 +0100594 /*
595 * If item is non-NULL here, then we previously saw a
596 * ref not followed by a peeled reference, so we need
597 * to check if it is a lightweight tag that we want to
598 * fetch.
599 */
600 if (item && !has_sha1_file(item->util) &&
601 !will_fetch(head, item->util))
602 item->util = NULL;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400603
Julian Phillipse984c542009-09-17 08:33:19 +0100604 item = NULL;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400605
Julian Phillipse984c542009-09-17 08:33:19 +0100606 /* skip duplicates and refs that we already have */
607 if (string_list_has_string(&remote_refs, ref->name) ||
608 string_list_has_string(&existing_refs, ref->name))
609 continue;
610
Julian Phillips78a395d2010-06-26 00:41:35 +0100611 item = string_list_insert(&remote_refs, ref->name);
Julian Phillipse984c542009-09-17 08:33:19 +0100612 item->util = (void *)ref->old_sha1;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400613 }
Johannes Schindelinc455c872008-07-21 19:03:49 +0100614 string_list_clear(&existing_refs, 0);
Julian Phillipse984c542009-09-17 08:33:19 +0100615
616 /*
617 * We may have a final lightweight tag that needs to be
618 * checked to see if it needs fetching.
619 */
620 if (item && !has_sha1_file(item->util) &&
621 !will_fetch(head, item->util))
622 item->util = NULL;
623
624 /*
Alex Riesen8a57c6e2010-07-03 14:41:54 +0200625 * For all the tags in the remote_refs string list,
626 * add them to the list of refs to be fetched
Julian Phillipse984c542009-09-17 08:33:19 +0100627 */
Alex Riesen8a57c6e2010-07-03 14:41:54 +0200628 for_each_string_list_item(item, &remote_refs) {
629 /* Unless we have already decided to ignore this item... */
630 if (item->util)
631 {
632 struct ref *rm = alloc_ref(item->string);
633 rm->peer_ref = alloc_ref(item->string);
634 hashcpy(rm->old_sha1, item->util);
635 **tail = rm;
636 *tail = &rm->next;
637 }
638 }
Julian Phillipse984c542009-09-17 08:33:19 +0100639
640 string_list_clear(&remote_refs, 0);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400641}
642
Johannes Schindelin8ee5d732008-10-13 11:36:52 +0200643static void check_not_current_branch(struct ref *ref_map)
644{
645 struct branch *current_branch = branch_get(NULL);
646
647 if (is_bare_repository() || !current_branch)
648 return;
649
650 for (; ref_map; ref_map = ref_map->next)
651 if (ref_map->peer_ref && !strcmp(current_branch->refname,
652 ref_map->peer_ref->name))
Alex Riesen26284f92009-03-22 23:07:33 +0100653 die("Refusing to fetch into current branch %s "
654 "of non-bare repository", current_branch->refname);
Johannes Schindelin8ee5d732008-10-13 11:36:52 +0200655}
656
Junio C Hamanoe6cc5102010-02-24 11:02:05 -0800657static int truncate_fetch_head(void)
658{
659 char *filename = git_path("FETCH_HEAD");
660 FILE *fp = fopen(filename, "w");
661
662 if (!fp)
663 return error("cannot open %s: %s\n", filename, strerror(errno));
664 fclose(fp);
665 return 0;
666}
667
Daniel Barkalowb888d612007-09-10 23:03:25 -0400668static int do_fetch(struct transport *transport,
669 struct refspec *refs, int ref_count)
670{
Thiago Farina183113a2010-07-04 16:46:19 -0300671 struct string_list existing_refs = STRING_LIST_INIT_NODUP;
Julian Phillipsb1a01e12009-10-25 21:28:12 +0000672 struct string_list_item *peer_item = NULL;
Shawn O. Pearce7f984282008-03-02 21:34:43 -0500673 struct ref *ref_map;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400674 struct ref *rm;
675 int autotags = (transport->remote->fetch_tags == 1);
Julian Phillipsb1a01e12009-10-25 21:28:12 +0000676
677 for_each_ref(add_existing, &existing_refs);
678
Daniel Johnsoned368542010-08-11 18:57:20 -0400679 if (tags == TAGS_DEFAULT) {
680 if (transport->remote->fetch_tags == 2)
681 tags = TAGS_SET;
682 if (transport->remote->fetch_tags == -1)
683 tags = TAGS_UNSET;
684 }
Daniel Barkalowb888d612007-09-10 23:03:25 -0400685
Shawn O. Pearce824d5772007-09-19 00:49:31 -0400686 if (!transport->get_refs_list || !transport->fetch)
Daniel Barkalowb888d612007-09-10 23:03:25 -0400687 die("Don't know how to fetch from %s", transport->url);
688
689 /* if not appending, truncate FETCH_HEAD */
Jay Soffian28a15402009-11-10 09:19:43 +0100690 if (!append && !dry_run) {
Junio C Hamanoe6cc5102010-02-24 11:02:05 -0800691 int errcode = truncate_fetch_head();
692 if (errcode)
693 return errcode;
André Goddard Rosad6617c72007-11-22 20:22:23 -0200694 }
Daniel Barkalowb888d612007-09-10 23:03:25 -0400695
696 ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
Johannes Schindelin8ee5d732008-10-13 11:36:52 +0200697 if (!update_head_ok)
698 check_not_current_branch(ref_map);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400699
700 for (rm = ref_map; rm; rm = rm->next) {
Julian Phillipsb1a01e12009-10-25 21:28:12 +0000701 if (rm->peer_ref) {
Julian Phillipse8c8b712010-06-26 00:41:37 +0100702 peer_item = string_list_lookup(&existing_refs,
703 rm->peer_ref->name);
Julian Phillipsb1a01e12009-10-25 21:28:12 +0000704 if (peer_item)
705 hashcpy(rm->peer_ref->old_sha1,
706 peer_item->util);
707 }
Daniel Barkalowb888d612007-09-10 23:03:25 -0400708 }
709
Shawn O. Pearce41fa7d22008-03-03 22:27:40 -0500710 if (tags == TAGS_DEFAULT && autotags)
711 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
Daniel Barkalowb888d612007-09-10 23:03:25 -0400712 if (fetch_refs(transport, ref_map)) {
713 free_refs(ref_map);
714 return 1;
715 }
Jay Soffianf360d842009-11-10 09:15:47 +0100716 if (prune)
717 prune_refs(transport, ref_map);
Shawn O. Pearce7f984282008-03-02 21:34:43 -0500718 free_refs(ref_map);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400719
720 /* if neither --no-tags nor --tags was specified, do automated tag
721 * following ... */
Kristian Høgsberg83201992007-12-04 02:25:47 -0500722 if (tags == TAGS_DEFAULT && autotags) {
Shawn O. Pearcec50b2b42008-03-02 21:35:00 -0500723 struct ref **tail = &ref_map;
724 ref_map = NULL;
725 find_non_local_tags(transport, &ref_map, &tail);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400726 if (ref_map) {
Shawn O. Pearce41fa7d22008-03-03 22:27:40 -0500727 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400728 transport_set_option(transport, TRANS_OPT_DEPTH, "0");
729 fetch_refs(transport, ref_map);
730 }
731 free_refs(ref_map);
732 }
733
Daniel Barkalowb888d612007-09-10 23:03:25 -0400734 return 0;
735}
736
Shawn O. Pearceab865e62007-09-18 04:54:57 -0400737static void set_option(const char *name, const char *value)
738{
739 int r = transport_set_option(transport, name, value);
740 if (r < 0)
Alexander Potashevd7530702009-01-04 21:38:41 +0300741 die("Option \"%s\" value \"%s\" is not valid for %s",
Shawn O. Pearceab865e62007-09-18 04:54:57 -0400742 name, value, transport->url);
743 if (r > 0)
744 warning("Option \"%s\" is ignored for %s\n",
745 name, transport->url);
746}
747
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100748static int get_one_remote_for_fetch(struct remote *remote, void *priv)
Daniel Barkalowb888d612007-09-10 23:03:25 -0400749{
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100750 struct string_list *list = priv;
Björn Gustavsson7cc91a22009-11-09 21:11:06 +0100751 if (!remote->skip_default_update)
Julian Phillips1d2f80f2010-06-26 00:41:38 +0100752 string_list_append(list, remote->name);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100753 return 0;
754}
755
756struct remote_group_data {
757 const char *name;
758 struct string_list *list;
759};
760
761static int get_remote_group(const char *key, const char *value, void *priv)
762{
763 struct remote_group_data *g = priv;
764
765 if (!prefixcmp(key, "remotes.") &&
766 !strcmp(key + 8, g->name)) {
767 /* split list by white space */
768 int space = strcspn(value, " \t\n");
769 while (*value) {
770 if (space > 1) {
Julian Phillips1d2f80f2010-06-26 00:41:38 +0100771 string_list_append(g->list,
772 xstrndup(value, space));
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100773 }
774 value += space + (value[space] != '\0');
775 space = strcspn(value, " \t\n");
776 }
777 }
778
779 return 0;
780}
781
782static int add_remote_or_group(const char *name, struct string_list *list)
783{
784 int prev_nr = list->nr;
Gary V. Vaughan66dbfd52010-05-14 09:31:33 +0000785 struct remote_group_data g;
786 g.name = name; g.list = list;
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100787
788 git_config(get_remote_group, &g);
789 if (list->nr == prev_nr) {
790 struct remote *remote;
791 if (!remote_is_configured(name))
792 return 0;
793 remote = remote_get(name);
Julian Phillips1d2f80f2010-06-26 00:41:38 +0100794 string_list_append(list, remote->name);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100795 }
796 return 1;
797}
798
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100799static void add_options_to_argv(int *argc, const char **argv)
800{
801 if (dry_run)
802 argv[(*argc)++] = "--dry-run";
803 if (prune)
804 argv[(*argc)++] = "--prune";
805 if (update_head_ok)
806 argv[(*argc)++] = "--update-head-ok";
807 if (force)
808 argv[(*argc)++] = "--force";
809 if (keep)
810 argv[(*argc)++] = "--keep";
Jens Lehmannbe254a02010-11-11 00:55:02 +0100811 if (recurse_submodules == RECURSE_SUBMODULES_ON)
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100812 argv[(*argc)++] = "--recurse-submodules";
813 if (verbosity >= 2)
814 argv[(*argc)++] = "-v";
815 if (verbosity >= 1)
816 argv[(*argc)++] = "-v";
817 else if (verbosity < 0)
818 argv[(*argc)++] = "-q";
819
820}
821
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100822static int fetch_multiple(struct string_list *list)
823{
824 int i, result = 0;
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100825 const char *argv[12] = { "fetch", "--append" };
Junio C Hamanoe6cc5102010-02-24 11:02:05 -0800826 int argc = 2;
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100827
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100828 add_options_to_argv(&argc, argv);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100829
Junio C Hamanoe6cc5102010-02-24 11:02:05 -0800830 if (!append && !dry_run) {
831 int errcode = truncate_fetch_head();
832 if (errcode)
833 return errcode;
834 }
835
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100836 for (i = 0; i < list->nr; i++) {
837 const char *name = list->items[i].string;
838 argv[argc] = name;
Junio C Hamanobba53222010-02-24 10:22:06 -0800839 argv[argc + 1] = NULL;
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100840 if (verbosity >= 0)
841 printf("Fetching %s\n", name);
842 if (run_command_v_opt(argv, RUN_GIT_CMD)) {
843 error("Could not fetch %s", name);
844 result = 1;
845 }
846 }
847
848 return result;
849}
850
851static int fetch_one(struct remote *remote, int argc, const char **argv)
852{
Kristian Høgsberg2d324ef2007-12-04 02:25:46 -0500853 int i;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400854 static const char **refs = NULL;
855 int ref_nr = 0;
Alex Riesen7b7f39e2008-04-28 22:23:35 +0200856 int exit_code;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400857
Daniel Barkalowfa685bd2009-03-11 01:47:20 -0400858 if (!remote)
Matthieu Moyd3b9dd12010-07-26 18:32:09 +0200859 die("No remote repository specified. Please, specify either a URL or a\n"
860 "remote name from which new revisions should be fetched.");
Daniel Barkalowfa685bd2009-03-11 01:47:20 -0400861
Daniel Barkalowfb0cc872009-11-18 02:42:22 +0100862 transport = transport_get(remote, NULL);
Tay Ray Chuan98390182010-02-24 20:50:28 +0800863 transport_set_verbosity(transport, verbosity, progress);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400864 if (upload_pack)
Shawn O. Pearceab865e62007-09-18 04:54:57 -0400865 set_option(TRANS_OPT_UPLOADPACK, upload_pack);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400866 if (keep)
Shawn O. Pearceab865e62007-09-18 04:54:57 -0400867 set_option(TRANS_OPT_KEEP, "yes");
868 if (depth)
869 set_option(TRANS_OPT_DEPTH, depth);
Daniel Barkalowb888d612007-09-10 23:03:25 -0400870
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100871 if (argc > 0) {
Daniel Barkalowb888d612007-09-10 23:03:25 -0400872 int j = 0;
Kristian Høgsberg83201992007-12-04 02:25:47 -0500873 refs = xcalloc(argc + 1, sizeof(const char *));
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100874 for (i = 0; i < argc; i++) {
Daniel Barkalowb888d612007-09-10 23:03:25 -0400875 if (!strcmp(argv[i], "tag")) {
876 char *ref;
877 i++;
Kevin Ballardf53423b2008-04-05 14:28:53 -0400878 if (i >= argc)
879 die("You need to specify a tag name.");
Daniel Barkalowb888d612007-09-10 23:03:25 -0400880 ref = xmalloc(strlen(argv[i]) * 2 + 22);
881 strcpy(ref, "refs/tags/");
882 strcat(ref, argv[i]);
883 strcat(ref, ":refs/tags/");
884 strcat(ref, argv[i]);
885 refs[j++] = ref;
886 } else
887 refs[j++] = argv[i];
Daniel Barkalowb888d612007-09-10 23:03:25 -0400888 }
889 refs[j] = NULL;
890 ref_nr = j;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400891 }
892
Jeff King57b235a2009-01-22 01:03:08 -0500893 sigchain_push_common(unlock_pack_on_signal);
Shawn O. Pearcee4022ed2007-09-14 03:31:25 -0400894 atexit(unlock_pack);
Alex Riesen7b7f39e2008-04-28 22:23:35 +0200895 exit_code = do_fetch(transport,
Junio C Hamano46220ca2008-03-20 23:34:37 -0700896 parse_fetch_refspec(ref_nr, refs), ref_nr);
Alex Riesen7b7f39e2008-04-28 22:23:35 +0200897 transport_disconnect(transport);
898 transport = NULL;
899 return exit_code;
Daniel Barkalowb888d612007-09-10 23:03:25 -0400900}
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100901
902int cmd_fetch(int argc, const char **argv, const char *prefix)
903{
904 int i;
Thiago Farina183113a2010-07-04 16:46:19 -0300905 struct string_list list = STRING_LIST_INIT_NODUP;
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100906 struct remote *remote;
907 int result = 0;
908
909 /* Record the command line for the reflog */
910 strbuf_addstr(&default_rla, "fetch");
911 for (i = 1; i < argc; i++)
912 strbuf_addf(&default_rla, " %s", argv[i]);
913
914 argc = parse_options(argc, argv, prefix,
915 builtin_fetch_options, builtin_fetch_usage, 0);
916
917 if (all) {
918 if (argc == 1)
919 die("fetch --all does not take a repository argument");
920 else if (argc > 1)
921 die("fetch --all does not make sense with refspecs");
922 (void) for_each_remote(get_one_remote_for_fetch, &list);
923 result = fetch_multiple(&list);
924 } else if (argc == 0) {
925 /* No arguments -- use default remote */
926 remote = remote_get(NULL);
927 result = fetch_one(remote, argc, argv);
Björn Gustavsson16679e32009-11-09 21:10:32 +0100928 } else if (multiple) {
929 /* All arguments are assumed to be remotes or groups */
930 for (i = 0; i < argc; i++)
931 if (!add_remote_or_group(argv[i], &list))
932 die("No such remote or remote group: %s", argv[i]);
933 result = fetch_multiple(&list);
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100934 } else {
935 /* Single remote or group */
936 (void) add_remote_or_group(argv[0], &list);
937 if (list.nr > 1) {
938 /* More than one remote */
939 if (argc > 1)
940 die("Fetching a group and specifying refspecs does not make sense");
941 result = fetch_multiple(&list);
942 } else {
943 /* Zero or one remotes */
944 remote = remote_get(argv[0]);
945 result = fetch_one(remote, argc-1, argv+1);
946 }
947 }
948
Jens Lehmannbe254a02010-11-11 00:55:02 +0100949 if (!result && (recurse_submodules != RECURSE_SUBMODULES_OFF)) {
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100950 const char *options[10];
951 int num_options = 0;
Jens Lehmannbe254a02010-11-11 00:55:02 +0100952 /* Set recursion as default when we already are recursing */
953 if (submodule_prefix[0])
954 set_config_fetch_recurse_submodules(1);
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100955 gitmodules_config();
956 git_config(submodule_config, NULL);
957 add_options_to_argv(&num_options, options);
958 result = fetch_populated_submodules(num_options, options,
959 submodule_prefix,
Jens Lehmannbe254a02010-11-11 00:55:02 +0100960 recurse_submodules == RECURSE_SUBMODULES_ON,
Jens Lehmann7dce19d2010-11-12 13:54:52 +0100961 verbosity < 0);
962 }
963
Björn Gustavsson9c4a0362009-11-09 21:09:56 +0100964 /* All names were strdup()ed or strndup()ed */
965 list.strdup_strings = 1;
966 string_list_clear(&list, 0);
967
968 return result;
969}