fetch.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944
  1. /*
  2. * "git fetch"
  3. */
  4. #include "cache.h"
  5. #include "config.h"
  6. #include "repository.h"
  7. #include "refs.h"
  8. #include "refspec.h"
  9. #include "object-store.h"
  10. #include "oidset.h"
  11. #include "commit.h"
  12. #include "builtin.h"
  13. #include "string-list.h"
  14. #include "remote.h"
  15. #include "transport.h"
  16. #include "run-command.h"
  17. #include "parse-options.h"
  18. #include "sigchain.h"
  19. #include "submodule-config.h"
  20. #include "submodule.h"
  21. #include "connected.h"
  22. #include "strvec.h"
  23. #include "utf8.h"
  24. #include "packfile.h"
  25. #include "list-objects-filter-options.h"
  26. #include "commit-reach.h"
  27. #include "branch.h"
  28. #include "promisor-remote.h"
  29. #include "commit-graph.h"
  30. #include "shallow.h"
  31. #define FORCED_UPDATES_DELAY_WARNING_IN_MS (10 * 1000)
  32. static const char * const builtin_fetch_usage[] = {
  33. N_("git fetch [<options>] [<repository> [<refspec>...]]"),
  34. N_("git fetch [<options>] <group>"),
  35. N_("git fetch --multiple [<options>] [(<repository> | <group>)...]"),
  36. N_("git fetch --all [<options>]"),
  37. NULL
  38. };
  39. enum {
  40. TAGS_UNSET = 0,
  41. TAGS_DEFAULT = 1,
  42. TAGS_SET = 2
  43. };
  44. static int fetch_prune_config = -1; /* unspecified */
  45. static int fetch_show_forced_updates = 1;
  46. static uint64_t forced_updates_ms = 0;
  47. static int prune = -1; /* unspecified */
  48. #define PRUNE_BY_DEFAULT 0 /* do we prune by default? */
  49. static int fetch_prune_tags_config = -1; /* unspecified */
  50. static int prune_tags = -1; /* unspecified */
  51. #define PRUNE_TAGS_BY_DEFAULT 0 /* do we prune tags by default? */
  52. static int all, append, dry_run, force, keep, multiple, update_head_ok;
  53. static int write_fetch_head = 1;
  54. static int verbosity, deepen_relative, set_upstream;
  55. static int progress = -1;
  56. static int enable_auto_gc = 1;
  57. static int tags = TAGS_DEFAULT, unshallow, update_shallow, deepen;
  58. static int max_jobs = -1, submodule_fetch_jobs_config = -1;
  59. static int fetch_parallel_config = 1;
  60. static enum transport_family family;
  61. static const char *depth;
  62. static const char *deepen_since;
  63. static const char *upload_pack;
  64. static struct string_list deepen_not = STRING_LIST_INIT_NODUP;
  65. static struct strbuf default_rla = STRBUF_INIT;
  66. static struct transport *gtransport;
  67. static struct transport *gsecondary;
  68. static const char *submodule_prefix = "";
  69. static int recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
  70. static int recurse_submodules_default = RECURSE_SUBMODULES_ON_DEMAND;
  71. static int shown_url = 0;
  72. static struct refspec refmap = REFSPEC_INIT_FETCH;
  73. static struct list_objects_filter_options filter_options;
  74. static struct string_list server_options = STRING_LIST_INIT_DUP;
  75. static struct string_list negotiation_tip = STRING_LIST_INIT_NODUP;
  76. static int fetch_write_commit_graph = -1;
  77. static int stdin_refspecs = 0;
  78. static int git_fetch_config(const char *k, const char *v, void *cb)
  79. {
  80. if (!strcmp(k, "fetch.prune")) {
  81. fetch_prune_config = git_config_bool(k, v);
  82. return 0;
  83. }
  84. if (!strcmp(k, "fetch.prunetags")) {
  85. fetch_prune_tags_config = git_config_bool(k, v);
  86. return 0;
  87. }
  88. if (!strcmp(k, "fetch.showforcedupdates")) {
  89. fetch_show_forced_updates = git_config_bool(k, v);
  90. return 0;
  91. }
  92. if (!strcmp(k, "submodule.recurse")) {
  93. int r = git_config_bool(k, v) ?
  94. RECURSE_SUBMODULES_ON : RECURSE_SUBMODULES_OFF;
  95. recurse_submodules = r;
  96. }
  97. if (!strcmp(k, "submodule.fetchjobs")) {
  98. submodule_fetch_jobs_config = parse_submodule_fetchjobs(k, v);
  99. return 0;
  100. } else if (!strcmp(k, "fetch.recursesubmodules")) {
  101. recurse_submodules = parse_fetch_recurse_submodules_arg(k, v);
  102. return 0;
  103. }
  104. if (!strcmp(k, "fetch.parallel")) {
  105. fetch_parallel_config = git_config_int(k, v);
  106. if (fetch_parallel_config < 0)
  107. die(_("fetch.parallel cannot be negative"));
  108. return 0;
  109. }
  110. return git_default_config(k, v, cb);
  111. }
  112. static int parse_refmap_arg(const struct option *opt, const char *arg, int unset)
  113. {
  114. BUG_ON_OPT_NEG(unset);
  115. /*
  116. * "git fetch --refmap='' origin foo"
  117. * can be used to tell the command not to store anywhere
  118. */
  119. refspec_append(&refmap, arg);
  120. return 0;
  121. }
  122. static struct option builtin_fetch_options[] = {
  123. OPT__VERBOSITY(&verbosity),
  124. OPT_BOOL(0, "all", &all,
  125. N_("fetch from all remotes")),
  126. OPT_BOOL(0, "set-upstream", &set_upstream,
  127. N_("set upstream for git pull/fetch")),
  128. OPT_BOOL('a', "append", &append,
  129. N_("append to .git/FETCH_HEAD instead of overwriting")),
  130. OPT_STRING(0, "upload-pack", &upload_pack, N_("path"),
  131. N_("path to upload pack on remote end")),
  132. OPT__FORCE(&force, N_("force overwrite of local reference"), 0),
  133. OPT_BOOL('m', "multiple", &multiple,
  134. N_("fetch from multiple remotes")),
  135. OPT_SET_INT('t', "tags", &tags,
  136. N_("fetch all tags and associated objects"), TAGS_SET),
  137. OPT_SET_INT('n', NULL, &tags,
  138. N_("do not fetch all tags (--no-tags)"), TAGS_UNSET),
  139. OPT_INTEGER('j', "jobs", &max_jobs,
  140. N_("number of submodules fetched in parallel")),
  141. OPT_BOOL('p', "prune", &prune,
  142. N_("prune remote-tracking branches no longer on remote")),
  143. OPT_BOOL('P', "prune-tags", &prune_tags,
  144. N_("prune local tags no longer on remote and clobber changed tags")),
  145. OPT_CALLBACK_F(0, "recurse-submodules", &recurse_submodules, N_("on-demand"),
  146. N_("control recursive fetching of submodules"),
  147. PARSE_OPT_OPTARG, option_fetch_parse_recurse_submodules),
  148. OPT_BOOL(0, "dry-run", &dry_run,
  149. N_("dry run")),
  150. OPT_BOOL(0, "write-fetch-head", &write_fetch_head,
  151. N_("write fetched references to the FETCH_HEAD file")),
  152. OPT_BOOL('k', "keep", &keep, N_("keep downloaded pack")),
  153. OPT_BOOL('u', "update-head-ok", &update_head_ok,
  154. N_("allow updating of HEAD ref")),
  155. OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
  156. OPT_STRING(0, "depth", &depth, N_("depth"),
  157. N_("deepen history of shallow clone")),
  158. OPT_STRING(0, "shallow-since", &deepen_since, N_("time"),
  159. N_("deepen history of shallow repository based on time")),
  160. OPT_STRING_LIST(0, "shallow-exclude", &deepen_not, N_("revision"),
  161. N_("deepen history of shallow clone, excluding rev")),
  162. OPT_INTEGER(0, "deepen", &deepen_relative,
  163. N_("deepen history of shallow clone")),
  164. OPT_SET_INT_F(0, "unshallow", &unshallow,
  165. N_("convert to a complete repository"),
  166. 1, PARSE_OPT_NONEG),
  167. { OPTION_STRING, 0, "submodule-prefix", &submodule_prefix, N_("dir"),
  168. N_("prepend this to submodule path output"), PARSE_OPT_HIDDEN },
  169. OPT_CALLBACK_F(0, "recurse-submodules-default",
  170. &recurse_submodules_default, N_("on-demand"),
  171. N_("default for recursive fetching of submodules "
  172. "(lower priority than config files)"),
  173. PARSE_OPT_HIDDEN, option_fetch_parse_recurse_submodules),
  174. OPT_BOOL(0, "update-shallow", &update_shallow,
  175. N_("accept refs that update .git/shallow")),
  176. OPT_CALLBACK_F(0, "refmap", NULL, N_("refmap"),
  177. N_("specify fetch refmap"), PARSE_OPT_NONEG, parse_refmap_arg),
  178. OPT_STRING_LIST('o', "server-option", &server_options, N_("server-specific"), N_("option to transmit")),
  179. OPT_SET_INT('4', "ipv4", &family, N_("use IPv4 addresses only"),
  180. TRANSPORT_FAMILY_IPV4),
  181. OPT_SET_INT('6', "ipv6", &family, N_("use IPv6 addresses only"),
  182. TRANSPORT_FAMILY_IPV6),
  183. OPT_STRING_LIST(0, "negotiation-tip", &negotiation_tip, N_("revision"),
  184. N_("report that we have only objects reachable from this object")),
  185. OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
  186. OPT_BOOL(0, "auto-maintenance", &enable_auto_gc,
  187. N_("run 'maintenance --auto' after fetching")),
  188. OPT_BOOL(0, "auto-gc", &enable_auto_gc,
  189. N_("run 'maintenance --auto' after fetching")),
  190. OPT_BOOL(0, "show-forced-updates", &fetch_show_forced_updates,
  191. N_("check for forced-updates on all updated branches")),
  192. OPT_BOOL(0, "write-commit-graph", &fetch_write_commit_graph,
  193. N_("write the commit-graph after fetching")),
  194. OPT_BOOL(0, "stdin", &stdin_refspecs,
  195. N_("accept refspecs from stdin")),
  196. OPT_END()
  197. };
  198. static void unlock_pack(void)
  199. {
  200. if (gtransport)
  201. transport_unlock_pack(gtransport);
  202. if (gsecondary)
  203. transport_unlock_pack(gsecondary);
  204. }
  205. static void unlock_pack_on_signal(int signo)
  206. {
  207. unlock_pack();
  208. sigchain_pop(signo);
  209. raise(signo);
  210. }
  211. static void add_merge_config(struct ref **head,
  212. const struct ref *remote_refs,
  213. struct branch *branch,
  214. struct ref ***tail)
  215. {
  216. int i;
  217. for (i = 0; i < branch->merge_nr; i++) {
  218. struct ref *rm, **old_tail = *tail;
  219. struct refspec_item refspec;
  220. for (rm = *head; rm; rm = rm->next) {
  221. if (branch_merge_matches(branch, i, rm->name)) {
  222. rm->fetch_head_status = FETCH_HEAD_MERGE;
  223. break;
  224. }
  225. }
  226. if (rm)
  227. continue;
  228. /*
  229. * Not fetched to a remote-tracking branch? We need to fetch
  230. * it anyway to allow this branch's "branch.$name.merge"
  231. * to be honored by 'git pull', but we do not have to
  232. * fail if branch.$name.merge is misconfigured to point
  233. * at a nonexisting branch. If we were indeed called by
  234. * 'git pull', it will notice the misconfiguration because
  235. * there is no entry in the resulting FETCH_HEAD marked
  236. * for merging.
  237. */
  238. memset(&refspec, 0, sizeof(refspec));
  239. refspec.src = branch->merge[i]->src;
  240. get_fetch_map(remote_refs, &refspec, tail, 1);
  241. for (rm = *old_tail; rm; rm = rm->next)
  242. rm->fetch_head_status = FETCH_HEAD_MERGE;
  243. }
  244. }
  245. static void create_fetch_oidset(struct ref **head, struct oidset *out)
  246. {
  247. struct ref *rm = *head;
  248. while (rm) {
  249. oidset_insert(out, &rm->old_oid);
  250. rm = rm->next;
  251. }
  252. }
  253. struct refname_hash_entry {
  254. struct hashmap_entry ent;
  255. struct object_id oid;
  256. int ignore;
  257. char refname[FLEX_ARRAY];
  258. };
  259. static int refname_hash_entry_cmp(const void *hashmap_cmp_fn_data,
  260. const struct hashmap_entry *eptr,
  261. const struct hashmap_entry *entry_or_key,
  262. const void *keydata)
  263. {
  264. const struct refname_hash_entry *e1, *e2;
  265. e1 = container_of(eptr, const struct refname_hash_entry, ent);
  266. e2 = container_of(entry_or_key, const struct refname_hash_entry, ent);
  267. return strcmp(e1->refname, keydata ? keydata : e2->refname);
  268. }
  269. static struct refname_hash_entry *refname_hash_add(struct hashmap *map,
  270. const char *refname,
  271. const struct object_id *oid)
  272. {
  273. struct refname_hash_entry *ent;
  274. size_t len = strlen(refname);
  275. FLEX_ALLOC_MEM(ent, refname, refname, len);
  276. hashmap_entry_init(&ent->ent, strhash(refname));
  277. oidcpy(&ent->oid, oid);
  278. hashmap_add(map, &ent->ent);
  279. return ent;
  280. }
  281. static int add_one_refname(const char *refname,
  282. const struct object_id *oid,
  283. int flag, void *cbdata)
  284. {
  285. struct hashmap *refname_map = cbdata;
  286. (void) refname_hash_add(refname_map, refname, oid);
  287. return 0;
  288. }
  289. static void refname_hash_init(struct hashmap *map)
  290. {
  291. hashmap_init(map, refname_hash_entry_cmp, NULL, 0);
  292. }
  293. static int refname_hash_exists(struct hashmap *map, const char *refname)
  294. {
  295. return !!hashmap_get_from_hash(map, strhash(refname), refname);
  296. }
  297. static void clear_item(struct refname_hash_entry *item)
  298. {
  299. item->ignore = 1;
  300. }
  301. static void find_non_local_tags(const struct ref *refs,
  302. struct ref **head,
  303. struct ref ***tail)
  304. {
  305. struct hashmap existing_refs;
  306. struct hashmap remote_refs;
  307. struct oidset fetch_oids = OIDSET_INIT;
  308. struct string_list remote_refs_list = STRING_LIST_INIT_NODUP;
  309. struct string_list_item *remote_ref_item;
  310. const struct ref *ref;
  311. struct refname_hash_entry *item = NULL;
  312. const int quick_flags = OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT;
  313. refname_hash_init(&existing_refs);
  314. refname_hash_init(&remote_refs);
  315. create_fetch_oidset(head, &fetch_oids);
  316. for_each_ref(add_one_refname, &existing_refs);
  317. for (ref = refs; ref; ref = ref->next) {
  318. if (!starts_with(ref->name, "refs/tags/"))
  319. continue;
  320. /*
  321. * The peeled ref always follows the matching base
  322. * ref, so if we see a peeled ref that we don't want
  323. * to fetch then we can mark the ref entry in the list
  324. * as one to ignore by setting util to NULL.
  325. */
  326. if (ends_with(ref->name, "^{}")) {
  327. if (item &&
  328. !has_object_file_with_flags(&ref->old_oid, quick_flags) &&
  329. !oidset_contains(&fetch_oids, &ref->old_oid) &&
  330. !has_object_file_with_flags(&item->oid, quick_flags) &&
  331. !oidset_contains(&fetch_oids, &item->oid))
  332. clear_item(item);
  333. item = NULL;
  334. continue;
  335. }
  336. /*
  337. * If item is non-NULL here, then we previously saw a
  338. * ref not followed by a peeled reference, so we need
  339. * to check if it is a lightweight tag that we want to
  340. * fetch.
  341. */
  342. if (item &&
  343. !has_object_file_with_flags(&item->oid, quick_flags) &&
  344. !oidset_contains(&fetch_oids, &item->oid))
  345. clear_item(item);
  346. item = NULL;
  347. /* skip duplicates and refs that we already have */
  348. if (refname_hash_exists(&remote_refs, ref->name) ||
  349. refname_hash_exists(&existing_refs, ref->name))
  350. continue;
  351. item = refname_hash_add(&remote_refs, ref->name, &ref->old_oid);
  352. string_list_insert(&remote_refs_list, ref->name);
  353. }
  354. hashmap_free_entries(&existing_refs, struct refname_hash_entry, ent);
  355. /*
  356. * We may have a final lightweight tag that needs to be
  357. * checked to see if it needs fetching.
  358. */
  359. if (item &&
  360. !has_object_file_with_flags(&item->oid, quick_flags) &&
  361. !oidset_contains(&fetch_oids, &item->oid))
  362. clear_item(item);
  363. /*
  364. * For all the tags in the remote_refs_list,
  365. * add them to the list of refs to be fetched
  366. */
  367. for_each_string_list_item(remote_ref_item, &remote_refs_list) {
  368. const char *refname = remote_ref_item->string;
  369. struct ref *rm;
  370. unsigned int hash = strhash(refname);
  371. item = hashmap_get_entry_from_hash(&remote_refs, hash, refname,
  372. struct refname_hash_entry, ent);
  373. if (!item)
  374. BUG("unseen remote ref?");
  375. /* Unless we have already decided to ignore this item... */
  376. if (item->ignore)
  377. continue;
  378. rm = alloc_ref(item->refname);
  379. rm->peer_ref = alloc_ref(item->refname);
  380. oidcpy(&rm->old_oid, &item->oid);
  381. **tail = rm;
  382. *tail = &rm->next;
  383. }
  384. hashmap_free_entries(&remote_refs, struct refname_hash_entry, ent);
  385. string_list_clear(&remote_refs_list, 0);
  386. oidset_clear(&fetch_oids);
  387. }
  388. static struct ref *get_ref_map(struct remote *remote,
  389. const struct ref *remote_refs,
  390. struct refspec *rs,
  391. int tags, int *autotags)
  392. {
  393. int i;
  394. struct ref *rm;
  395. struct ref *ref_map = NULL;
  396. struct ref **tail = &ref_map;
  397. /* opportunistically-updated references: */
  398. struct ref *orefs = NULL, **oref_tail = &orefs;
  399. struct hashmap existing_refs;
  400. int existing_refs_populated = 0;
  401. if (rs->nr) {
  402. struct refspec *fetch_refspec;
  403. for (i = 0; i < rs->nr; i++) {
  404. get_fetch_map(remote_refs, &rs->items[i], &tail, 0);
  405. if (rs->items[i].dst && rs->items[i].dst[0])
  406. *autotags = 1;
  407. }
  408. /* Merge everything on the command line (but not --tags) */
  409. for (rm = ref_map; rm; rm = rm->next)
  410. rm->fetch_head_status = FETCH_HEAD_MERGE;
  411. /*
  412. * For any refs that we happen to be fetching via
  413. * command-line arguments, the destination ref might
  414. * have been missing or have been different than the
  415. * remote-tracking ref that would be derived from the
  416. * configured refspec. In these cases, we want to
  417. * take the opportunity to update their configured
  418. * remote-tracking reference. However, we do not want
  419. * to mention these entries in FETCH_HEAD at all, as
  420. * they would simply be duplicates of existing
  421. * entries, so we set them FETCH_HEAD_IGNORE below.
  422. *
  423. * We compute these entries now, based only on the
  424. * refspecs specified on the command line. But we add
  425. * them to the list following the refspecs resulting
  426. * from the tags option so that one of the latter,
  427. * which has FETCH_HEAD_NOT_FOR_MERGE, is not removed
  428. * by ref_remove_duplicates() in favor of one of these
  429. * opportunistic entries with FETCH_HEAD_IGNORE.
  430. */
  431. if (refmap.nr)
  432. fetch_refspec = &refmap;
  433. else
  434. fetch_refspec = &remote->fetch;
  435. for (i = 0; i < fetch_refspec->nr; i++)
  436. get_fetch_map(ref_map, &fetch_refspec->items[i], &oref_tail, 1);
  437. } else if (refmap.nr) {
  438. die("--refmap option is only meaningful with command-line refspec(s).");
  439. } else {
  440. /* Use the defaults */
  441. struct branch *branch = branch_get(NULL);
  442. int has_merge = branch_has_merge_config(branch);
  443. if (remote &&
  444. (remote->fetch.nr ||
  445. /* Note: has_merge implies non-NULL branch->remote_name */
  446. (has_merge && !strcmp(branch->remote_name, remote->name)))) {
  447. for (i = 0; i < remote->fetch.nr; i++) {
  448. get_fetch_map(remote_refs, &remote->fetch.items[i], &tail, 0);
  449. if (remote->fetch.items[i].dst &&
  450. remote->fetch.items[i].dst[0])
  451. *autotags = 1;
  452. if (!i && !has_merge && ref_map &&
  453. !remote->fetch.items[0].pattern)
  454. ref_map->fetch_head_status = FETCH_HEAD_MERGE;
  455. }
  456. /*
  457. * if the remote we're fetching from is the same
  458. * as given in branch.<name>.remote, we add the
  459. * ref given in branch.<name>.merge, too.
  460. *
  461. * Note: has_merge implies non-NULL branch->remote_name
  462. */
  463. if (has_merge &&
  464. !strcmp(branch->remote_name, remote->name))
  465. add_merge_config(&ref_map, remote_refs, branch, &tail);
  466. } else {
  467. ref_map = get_remote_ref(remote_refs, "HEAD");
  468. if (!ref_map)
  469. die(_("Couldn't find remote ref HEAD"));
  470. ref_map->fetch_head_status = FETCH_HEAD_MERGE;
  471. tail = &ref_map->next;
  472. }
  473. }
  474. if (tags == TAGS_SET)
  475. /* also fetch all tags */
  476. get_fetch_map(remote_refs, tag_refspec, &tail, 0);
  477. else if (tags == TAGS_DEFAULT && *autotags)
  478. find_non_local_tags(remote_refs, &ref_map, &tail);
  479. /* Now append any refs to be updated opportunistically: */
  480. *tail = orefs;
  481. for (rm = orefs; rm; rm = rm->next) {
  482. rm->fetch_head_status = FETCH_HEAD_IGNORE;
  483. tail = &rm->next;
  484. }
  485. /*
  486. * apply negative refspecs first, before we remove duplicates. This is
  487. * necessary as negative refspecs might remove an otherwise conflicting
  488. * duplicate.
  489. */
  490. if (rs->nr)
  491. ref_map = apply_negative_refspecs(ref_map, rs);
  492. else
  493. ref_map = apply_negative_refspecs(ref_map, &remote->fetch);
  494. ref_map = ref_remove_duplicates(ref_map);
  495. for (rm = ref_map; rm; rm = rm->next) {
  496. if (rm->peer_ref) {
  497. const char *refname = rm->peer_ref->name;
  498. struct refname_hash_entry *peer_item;
  499. unsigned int hash = strhash(refname);
  500. if (!existing_refs_populated) {
  501. refname_hash_init(&existing_refs);
  502. for_each_ref(add_one_refname, &existing_refs);
  503. existing_refs_populated = 1;
  504. }
  505. peer_item = hashmap_get_entry_from_hash(&existing_refs,
  506. hash, refname,
  507. struct refname_hash_entry, ent);
  508. if (peer_item) {
  509. struct object_id *old_oid = &peer_item->oid;
  510. oidcpy(&rm->peer_ref->old_oid, old_oid);
  511. }
  512. }
  513. }
  514. if (existing_refs_populated)
  515. hashmap_free_entries(&existing_refs, struct refname_hash_entry, ent);
  516. return ref_map;
  517. }
  518. #define STORE_REF_ERROR_OTHER 1
  519. #define STORE_REF_ERROR_DF_CONFLICT 2
  520. static int s_update_ref(const char *action,
  521. struct ref *ref,
  522. int check_old)
  523. {
  524. char *msg;
  525. char *rla = getenv("GIT_REFLOG_ACTION");
  526. struct ref_transaction *transaction;
  527. struct strbuf err = STRBUF_INIT;
  528. int ret, df_conflict = 0;
  529. if (dry_run)
  530. return 0;
  531. if (!rla)
  532. rla = default_rla.buf;
  533. msg = xstrfmt("%s: %s", rla, action);
  534. transaction = ref_transaction_begin(&err);
  535. if (!transaction ||
  536. ref_transaction_update(transaction, ref->name,
  537. &ref->new_oid,
  538. check_old ? &ref->old_oid : NULL,
  539. 0, msg, &err))
  540. goto fail;
  541. ret = ref_transaction_commit(transaction, &err);
  542. if (ret) {
  543. df_conflict = (ret == TRANSACTION_NAME_CONFLICT);
  544. goto fail;
  545. }
  546. ref_transaction_free(transaction);
  547. strbuf_release(&err);
  548. free(msg);
  549. return 0;
  550. fail:
  551. ref_transaction_free(transaction);
  552. error("%s", err.buf);
  553. strbuf_release(&err);
  554. free(msg);
  555. return df_conflict ? STORE_REF_ERROR_DF_CONFLICT
  556. : STORE_REF_ERROR_OTHER;
  557. }
  558. static int refcol_width = 10;
  559. static int compact_format;
  560. static void adjust_refcol_width(const struct ref *ref)
  561. {
  562. int max, rlen, llen, len;
  563. /* uptodate lines are only shown on high verbosity level */
  564. if (!verbosity && oideq(&ref->peer_ref->old_oid, &ref->old_oid))
  565. return;
  566. max = term_columns();
  567. rlen = utf8_strwidth(prettify_refname(ref->name));
  568. llen = utf8_strwidth(prettify_refname(ref->peer_ref->name));
  569. /*
  570. * rough estimation to see if the output line is too long and
  571. * should not be counted (we can't do precise calculation
  572. * anyway because we don't know if the error explanation part
  573. * will be printed in update_local_ref)
  574. */
  575. if (compact_format) {
  576. llen = 0;
  577. max = max * 2 / 3;
  578. }
  579. len = 21 /* flag and summary */ + rlen + 4 /* -> */ + llen;
  580. if (len >= max)
  581. return;
  582. /*
  583. * Not precise calculation for compact mode because '*' can
  584. * appear on the left hand side of '->' and shrink the column
  585. * back.
  586. */
  587. if (refcol_width < rlen)
  588. refcol_width = rlen;
  589. }
  590. static void prepare_format_display(struct ref *ref_map)
  591. {
  592. struct ref *rm;
  593. const char *format = "full";
  594. git_config_get_string_tmp("fetch.output", &format);
  595. if (!strcasecmp(format, "full"))
  596. compact_format = 0;
  597. else if (!strcasecmp(format, "compact"))
  598. compact_format = 1;
  599. else
  600. die(_("configuration fetch.output contains invalid value %s"),
  601. format);
  602. for (rm = ref_map; rm; rm = rm->next) {
  603. if (rm->status == REF_STATUS_REJECT_SHALLOW ||
  604. !rm->peer_ref ||
  605. !strcmp(rm->name, "HEAD"))
  606. continue;
  607. adjust_refcol_width(rm);
  608. }
  609. }
  610. static void print_remote_to_local(struct strbuf *display,
  611. const char *remote, const char *local)
  612. {
  613. strbuf_addf(display, "%-*s -> %s", refcol_width, remote, local);
  614. }
  615. static int find_and_replace(struct strbuf *haystack,
  616. const char *needle,
  617. const char *placeholder)
  618. {
  619. const char *p = NULL;
  620. int plen, nlen;
  621. nlen = strlen(needle);
  622. if (ends_with(haystack->buf, needle))
  623. p = haystack->buf + haystack->len - nlen;
  624. else
  625. p = strstr(haystack->buf, needle);
  626. if (!p)
  627. return 0;
  628. if (p > haystack->buf && p[-1] != '/')
  629. return 0;
  630. plen = strlen(p);
  631. if (plen > nlen && p[nlen] != '/')
  632. return 0;
  633. strbuf_splice(haystack, p - haystack->buf, nlen,
  634. placeholder, strlen(placeholder));
  635. return 1;
  636. }
  637. static void print_compact(struct strbuf *display,
  638. const char *remote, const char *local)
  639. {
  640. struct strbuf r = STRBUF_INIT;
  641. struct strbuf l = STRBUF_INIT;
  642. if (!strcmp(remote, local)) {
  643. strbuf_addf(display, "%-*s -> *", refcol_width, remote);
  644. return;
  645. }
  646. strbuf_addstr(&r, remote);
  647. strbuf_addstr(&l, local);
  648. if (!find_and_replace(&r, local, "*"))
  649. find_and_replace(&l, remote, "*");
  650. print_remote_to_local(display, r.buf, l.buf);
  651. strbuf_release(&r);
  652. strbuf_release(&l);
  653. }
  654. static void format_display(struct strbuf *display, char code,
  655. const char *summary, const char *error,
  656. const char *remote, const char *local,
  657. int summary_width)
  658. {
  659. int width = (summary_width + strlen(summary) - gettext_width(summary));
  660. strbuf_addf(display, "%c %-*s ", code, width, summary);
  661. if (!compact_format)
  662. print_remote_to_local(display, remote, local);
  663. else
  664. print_compact(display, remote, local);
  665. if (error)
  666. strbuf_addf(display, " (%s)", error);
  667. }
  668. static int update_local_ref(struct ref *ref,
  669. const char *remote,
  670. const struct ref *remote_ref,
  671. struct strbuf *display,
  672. int summary_width)
  673. {
  674. struct commit *current = NULL, *updated;
  675. enum object_type type;
  676. struct branch *current_branch = branch_get(NULL);
  677. const char *pretty_ref = prettify_refname(ref->name);
  678. int fast_forward = 0;
  679. type = oid_object_info(the_repository, &ref->new_oid, NULL);
  680. if (type < 0)
  681. die(_("object %s not found"), oid_to_hex(&ref->new_oid));
  682. if (oideq(&ref->old_oid, &ref->new_oid)) {
  683. if (verbosity > 0)
  684. format_display(display, '=', _("[up to date]"), NULL,
  685. remote, pretty_ref, summary_width);
  686. return 0;
  687. }
  688. if (current_branch &&
  689. !strcmp(ref->name, current_branch->name) &&
  690. !(update_head_ok || is_bare_repository()) &&
  691. !is_null_oid(&ref->old_oid)) {
  692. /*
  693. * If this is the head, and it's not okay to update
  694. * the head, and the old value of the head isn't empty...
  695. */
  696. format_display(display, '!', _("[rejected]"),
  697. _("can't fetch in current branch"),
  698. remote, pretty_ref, summary_width);
  699. return 1;
  700. }
  701. if (!is_null_oid(&ref->old_oid) &&
  702. starts_with(ref->name, "refs/tags/")) {
  703. if (force || ref->force) {
  704. int r;
  705. r = s_update_ref("updating tag", ref, 0);
  706. format_display(display, r ? '!' : 't', _("[tag update]"),
  707. r ? _("unable to update local ref") : NULL,
  708. remote, pretty_ref, summary_width);
  709. return r;
  710. } else {
  711. format_display(display, '!', _("[rejected]"), _("would clobber existing tag"),
  712. remote, pretty_ref, summary_width);
  713. return 1;
  714. }
  715. }
  716. current = lookup_commit_reference_gently(the_repository,
  717. &ref->old_oid, 1);
  718. updated = lookup_commit_reference_gently(the_repository,
  719. &ref->new_oid, 1);
  720. if (!current || !updated) {
  721. const char *msg;
  722. const char *what;
  723. int r;
  724. /*
  725. * Nicely describe the new ref we're fetching.
  726. * Base this on the remote's ref name, as it's
  727. * more likely to follow a standard layout.
  728. */
  729. const char *name = remote_ref ? remote_ref->name : "";
  730. if (starts_with(name, "refs/tags/")) {
  731. msg = "storing tag";
  732. what = _("[new tag]");
  733. } else if (starts_with(name, "refs/heads/")) {
  734. msg = "storing head";
  735. what = _("[new branch]");
  736. } else {
  737. msg = "storing ref";
  738. what = _("[new ref]");
  739. }
  740. r = s_update_ref(msg, ref, 0);
  741. format_display(display, r ? '!' : '*', what,
  742. r ? _("unable to update local ref") : NULL,
  743. remote, pretty_ref, summary_width);
  744. return r;
  745. }
  746. if (fetch_show_forced_updates) {
  747. uint64_t t_before = getnanotime();
  748. fast_forward = in_merge_bases(current, updated);
  749. forced_updates_ms += (getnanotime() - t_before) / 1000000;
  750. } else {
  751. fast_forward = 1;
  752. }
  753. if (fast_forward) {
  754. struct strbuf quickref = STRBUF_INIT;
  755. int r;
  756. strbuf_add_unique_abbrev(&quickref, &current->object.oid, DEFAULT_ABBREV);
  757. strbuf_addstr(&quickref, "..");
  758. strbuf_add_unique_abbrev(&quickref, &ref->new_oid, DEFAULT_ABBREV);
  759. r = s_update_ref("fast-forward", ref, 1);
  760. format_display(display, r ? '!' : ' ', quickref.buf,
  761. r ? _("unable to update local ref") : NULL,
  762. remote, pretty_ref, summary_width);
  763. strbuf_release(&quickref);
  764. return r;
  765. } else if (force || ref->force) {
  766. struct strbuf quickref = STRBUF_INIT;
  767. int r;
  768. strbuf_add_unique_abbrev(&quickref, &current->object.oid, DEFAULT_ABBREV);
  769. strbuf_addstr(&quickref, "...");
  770. strbuf_add_unique_abbrev(&quickref, &ref->new_oid, DEFAULT_ABBREV);
  771. r = s_update_ref("forced-update", ref, 1);
  772. format_display(display, r ? '!' : '+', quickref.buf,
  773. r ? _("unable to update local ref") : _("forced update"),
  774. remote, pretty_ref, summary_width);
  775. strbuf_release(&quickref);
  776. return r;
  777. } else {
  778. format_display(display, '!', _("[rejected]"), _("non-fast-forward"),
  779. remote, pretty_ref, summary_width);
  780. return 1;
  781. }
  782. }
  783. static int iterate_ref_map(void *cb_data, struct object_id *oid)
  784. {
  785. struct ref **rm = cb_data;
  786. struct ref *ref = *rm;
  787. while (ref && ref->status == REF_STATUS_REJECT_SHALLOW)
  788. ref = ref->next;
  789. if (!ref)
  790. return -1; /* end of the list */
  791. *rm = ref->next;
  792. oidcpy(oid, &ref->old_oid);
  793. return 0;
  794. }
  795. static const char warn_show_forced_updates[] =
  796. N_("Fetch normally indicates which branches had a forced update,\n"
  797. "but that check has been disabled. To re-enable, use '--show-forced-updates'\n"
  798. "flag or run 'git config fetch.showForcedUpdates true'.");
  799. static const char warn_time_show_forced_updates[] =
  800. N_("It took %.2f seconds to check forced updates. You can use\n"
  801. "'--no-show-forced-updates' or run 'git config fetch.showForcedUpdates false'\n"
  802. " to avoid this check.\n");
  803. static int store_updated_refs(const char *raw_url, const char *remote_name,
  804. int connectivity_checked, struct ref *ref_map)
  805. {
  806. FILE *fp;
  807. struct commit *commit;
  808. int url_len, i, rc = 0;
  809. struct strbuf note = STRBUF_INIT;
  810. const char *what, *kind;
  811. struct ref *rm;
  812. char *url;
  813. const char *filename = (!write_fetch_head
  814. ? "/dev/null"
  815. : git_path_fetch_head(the_repository));
  816. int want_status;
  817. int summary_width = transport_summary_width(ref_map);
  818. fp = fopen(filename, "a");
  819. if (!fp)
  820. return error_errno(_("cannot open %s"), filename);
  821. if (raw_url)
  822. url = transport_anonymize_url(raw_url);
  823. else
  824. url = xstrdup("foreign");
  825. if (!connectivity_checked) {
  826. struct check_connected_options opt = CHECK_CONNECTED_INIT;
  827. rm = ref_map;
  828. if (check_connected(iterate_ref_map, &rm, &opt)) {
  829. rc = error(_("%s did not send all necessary objects\n"), url);
  830. goto abort;
  831. }
  832. }
  833. prepare_format_display(ref_map);
  834. /*
  835. * We do a pass for each fetch_head_status type in their enum order, so
  836. * merged entries are written before not-for-merge. That lets readers
  837. * use FETCH_HEAD as a refname to refer to the ref to be merged.
  838. */
  839. for (want_status = FETCH_HEAD_MERGE;
  840. want_status <= FETCH_HEAD_IGNORE;
  841. want_status++) {
  842. for (rm = ref_map; rm; rm = rm->next) {
  843. struct ref *ref = NULL;
  844. const char *merge_status_marker = "";
  845. if (rm->status == REF_STATUS_REJECT_SHALLOW) {
  846. if (want_status == FETCH_HEAD_MERGE)
  847. warning(_("reject %s because shallow roots are not allowed to be updated"),
  848. rm->peer_ref ? rm->peer_ref->name : rm->name);
  849. continue;
  850. }
  851. commit = lookup_commit_reference_gently(the_repository,
  852. &rm->old_oid,
  853. 1);
  854. if (!commit)
  855. rm->fetch_head_status = FETCH_HEAD_NOT_FOR_MERGE;
  856. if (rm->fetch_head_status != want_status)
  857. continue;
  858. if (rm->peer_ref) {
  859. ref = alloc_ref(rm->peer_ref->name);
  860. oidcpy(&ref->old_oid, &rm->peer_ref->old_oid);
  861. oidcpy(&ref->new_oid, &rm->old_oid);
  862. ref->force = rm->peer_ref->force;
  863. }
  864. if (recurse_submodules != RECURSE_SUBMODULES_OFF &&
  865. (!rm->peer_ref || !oideq(&ref->old_oid, &ref->new_oid))) {
  866. check_for_new_submodule_commits(&rm->old_oid);
  867. }
  868. if (!strcmp(rm->name, "HEAD")) {
  869. kind = "";
  870. what = "";
  871. }
  872. else if (skip_prefix(rm->name, "refs/heads/", &what))
  873. kind = "branch";
  874. else if (skip_prefix(rm->name, "refs/tags/", &what))
  875. kind = "tag";
  876. else if (skip_prefix(rm->name, "refs/remotes/", &what))
  877. kind = "remote-tracking branch";
  878. else {
  879. kind = "";
  880. what = rm->name;
  881. }
  882. url_len = strlen(url);
  883. for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
  884. ;
  885. url_len = i + 1;
  886. if (4 < i && !strncmp(".git", url + i - 3, 4))
  887. url_len = i - 3;
  888. strbuf_reset(&note);
  889. if (*what) {
  890. if (*kind)
  891. strbuf_addf(&note, "%s ", kind);
  892. strbuf_addf(&note, "'%s' of ", what);
  893. }
  894. switch (rm->fetch_head_status) {
  895. case FETCH_HEAD_NOT_FOR_MERGE:
  896. merge_status_marker = "not-for-merge";
  897. /* fall-through */
  898. case FETCH_HEAD_MERGE:
  899. fprintf(fp, "%s\t%s\t%s",
  900. oid_to_hex(&rm->old_oid),
  901. merge_status_marker,
  902. note.buf);
  903. for (i = 0; i < url_len; ++i)
  904. if ('\n' == url[i])
  905. fputs("\\n", fp);
  906. else
  907. fputc(url[i], fp);
  908. fputc('\n', fp);
  909. break;
  910. default:
  911. /* do not write anything to FETCH_HEAD */
  912. break;
  913. }
  914. strbuf_reset(&note);
  915. if (ref) {
  916. rc |= update_local_ref(ref, what, rm, &note,
  917. summary_width);
  918. free(ref);
  919. } else if (write_fetch_head || dry_run) {
  920. /*
  921. * Display fetches written to FETCH_HEAD (or
  922. * would be written to FETCH_HEAD, if --dry-run
  923. * is set).
  924. */
  925. format_display(&note, '*',
  926. *kind ? kind : "branch", NULL,
  927. *what ? what : "HEAD",
  928. "FETCH_HEAD", summary_width);
  929. }
  930. if (note.len) {
  931. if (verbosity >= 0 && !shown_url) {
  932. fprintf(stderr, _("From %.*s\n"),
  933. url_len, url);
  934. shown_url = 1;
  935. }
  936. if (verbosity >= 0)
  937. fprintf(stderr, " %s\n", note.buf);
  938. }
  939. }
  940. }
  941. if (rc & STORE_REF_ERROR_DF_CONFLICT)
  942. error(_("some local refs could not be updated; try running\n"
  943. " 'git remote prune %s' to remove any old, conflicting "
  944. "branches"), remote_name);
  945. if (advice_fetch_show_forced_updates) {
  946. if (!fetch_show_forced_updates) {
  947. warning(_(warn_show_forced_updates));
  948. } else if (forced_updates_ms > FORCED_UPDATES_DELAY_WARNING_IN_MS) {
  949. warning(_(warn_time_show_forced_updates),
  950. forced_updates_ms / 1000.0);
  951. }
  952. }
  953. abort:
  954. strbuf_release(&note);
  955. free(url);
  956. fclose(fp);
  957. return rc;
  958. }
  959. /*
  960. * We would want to bypass the object transfer altogether if
  961. * everything we are going to fetch already exists and is connected
  962. * locally.
  963. */
  964. static int check_exist_and_connected(struct ref *ref_map)
  965. {
  966. struct ref *rm = ref_map;
  967. struct check_connected_options opt = CHECK_CONNECTED_INIT;
  968. struct ref *r;
  969. /*
  970. * If we are deepening a shallow clone we already have these
  971. * objects reachable. Running rev-list here will return with
  972. * a good (0) exit status and we'll bypass the fetch that we
  973. * really need to perform. Claiming failure now will ensure
  974. * we perform the network exchange to deepen our history.
  975. */
  976. if (deepen)
  977. return -1;
  978. /*
  979. * check_connected() allows objects to merely be promised, but
  980. * we need all direct targets to exist.
  981. */
  982. for (r = rm; r; r = r->next) {
  983. if (!has_object_file_with_flags(&r->old_oid,
  984. OBJECT_INFO_SKIP_FETCH_OBJECT))
  985. return -1;
  986. }
  987. opt.quiet = 1;
  988. return check_connected(iterate_ref_map, &rm, &opt);
  989. }
  990. static int fetch_refs(struct transport *transport, struct ref *ref_map)
  991. {
  992. int ret = check_exist_and_connected(ref_map);
  993. if (ret) {
  994. trace2_region_enter("fetch", "fetch_refs", the_repository);
  995. ret = transport_fetch_refs(transport, ref_map);
  996. trace2_region_leave("fetch", "fetch_refs", the_repository);
  997. }
  998. if (!ret)
  999. /*
  1000. * Keep the new pack's ".keep" file around to allow the caller
  1001. * time to update refs to reference the new objects.
  1002. */
  1003. return 0;
  1004. transport_unlock_pack(transport);
  1005. return ret;
  1006. }
  1007. /* Update local refs based on the ref values fetched from a remote */
  1008. static int consume_refs(struct transport *transport, struct ref *ref_map)
  1009. {
  1010. int connectivity_checked = transport->smart_options
  1011. ? transport->smart_options->connectivity_checked : 0;
  1012. int ret;
  1013. trace2_region_enter("fetch", "consume_refs", the_repository);
  1014. ret = store_updated_refs(transport->url,
  1015. transport->remote->name,
  1016. connectivity_checked,
  1017. ref_map);
  1018. transport_unlock_pack(transport);
  1019. trace2_region_leave("fetch", "consume_refs", the_repository);
  1020. return ret;
  1021. }
  1022. static int prune_refs(struct refspec *rs, struct ref *ref_map,
  1023. const char *raw_url)
  1024. {
  1025. int url_len, i, result = 0;
  1026. struct ref *ref, *stale_refs = get_stale_heads(rs, ref_map);
  1027. char *url;
  1028. int summary_width = transport_summary_width(stale_refs);
  1029. const char *dangling_msg = dry_run
  1030. ? _(" (%s will become dangling)")
  1031. : _(" (%s has become dangling)");
  1032. if (raw_url)
  1033. url = transport_anonymize_url(raw_url);
  1034. else
  1035. url = xstrdup("foreign");
  1036. url_len = strlen(url);
  1037. for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
  1038. ;
  1039. url_len = i + 1;
  1040. if (4 < i && !strncmp(".git", url + i - 3, 4))
  1041. url_len = i - 3;
  1042. if (!dry_run) {
  1043. struct string_list refnames = STRING_LIST_INIT_NODUP;
  1044. for (ref = stale_refs; ref; ref = ref->next)
  1045. string_list_append(&refnames, ref->name);
  1046. result = delete_refs("fetch: prune", &refnames, 0);
  1047. string_list_clear(&refnames, 0);
  1048. }
  1049. if (verbosity >= 0) {
  1050. for (ref = stale_refs; ref; ref = ref->next) {
  1051. struct strbuf sb = STRBUF_INIT;
  1052. if (!shown_url) {
  1053. fprintf(stderr, _("From %.*s\n"), url_len, url);
  1054. shown_url = 1;
  1055. }
  1056. format_display(&sb, '-', _("[deleted]"), NULL,
  1057. _("(none)"), prettify_refname(ref->name),
  1058. summary_width);
  1059. fprintf(stderr, " %s\n",sb.buf);
  1060. strbuf_release(&sb);
  1061. warn_dangling_symref(stderr, dangling_msg, ref->name);
  1062. }
  1063. }
  1064. free(url);
  1065. free_refs(stale_refs);
  1066. return result;
  1067. }
  1068. static void check_not_current_branch(struct ref *ref_map)
  1069. {
  1070. struct branch *current_branch = branch_get(NULL);
  1071. if (is_bare_repository() || !current_branch)
  1072. return;
  1073. for (; ref_map; ref_map = ref_map->next)
  1074. if (ref_map->peer_ref && !strcmp(current_branch->refname,
  1075. ref_map->peer_ref->name))
  1076. die(_("Refusing to fetch into current branch %s "
  1077. "of non-bare repository"), current_branch->refname);
  1078. }
  1079. static int truncate_fetch_head(void)
  1080. {
  1081. const char *filename = git_path_fetch_head(the_repository);
  1082. FILE *fp = fopen_for_writing(filename);
  1083. if (!fp)
  1084. return error_errno(_("cannot open %s"), filename);
  1085. fclose(fp);
  1086. return 0;
  1087. }
  1088. static void set_option(struct transport *transport, const char *name, const char *value)
  1089. {
  1090. int r = transport_set_option(transport, name, value);
  1091. if (r < 0)
  1092. die(_("Option \"%s\" value \"%s\" is not valid for %s"),
  1093. name, value, transport->url);
  1094. if (r > 0)
  1095. warning(_("Option \"%s\" is ignored for %s\n"),
  1096. name, transport->url);
  1097. }
  1098. static int add_oid(const char *refname, const struct object_id *oid, int flags,
  1099. void *cb_data)
  1100. {
  1101. struct oid_array *oids = cb_data;
  1102. oid_array_append(oids, oid);
  1103. return 0;
  1104. }
  1105. static void add_negotiation_tips(struct git_transport_options *smart_options)
  1106. {
  1107. struct oid_array *oids = xcalloc(1, sizeof(*oids));
  1108. int i;
  1109. for (i = 0; i < negotiation_tip.nr; i++) {
  1110. const char *s = negotiation_tip.items[i].string;
  1111. int old_nr;
  1112. if (!has_glob_specials(s)) {
  1113. struct object_id oid;
  1114. if (get_oid(s, &oid))
  1115. die("%s is not a valid object", s);
  1116. oid_array_append(oids, &oid);
  1117. continue;
  1118. }
  1119. old_nr = oids->nr;
  1120. for_each_glob_ref(add_oid, s, oids);
  1121. if (old_nr == oids->nr)
  1122. warning("Ignoring --negotiation-tip=%s because it does not match any refs",
  1123. s);
  1124. }
  1125. smart_options->negotiation_tips = oids;
  1126. }
  1127. static struct transport *prepare_transport(struct remote *remote, int deepen)
  1128. {
  1129. struct transport *transport;
  1130. transport = transport_get(remote, NULL);
  1131. transport_set_verbosity(transport, verbosity, progress);
  1132. transport->family = family;
  1133. if (upload_pack)
  1134. set_option(transport, TRANS_OPT_UPLOADPACK, upload_pack);
  1135. if (keep)
  1136. set_option(transport, TRANS_OPT_KEEP, "yes");
  1137. if (depth)
  1138. set_option(transport, TRANS_OPT_DEPTH, depth);
  1139. if (deepen && deepen_since)
  1140. set_option(transport, TRANS_OPT_DEEPEN_SINCE, deepen_since);
  1141. if (deepen && deepen_not.nr)
  1142. set_option(transport, TRANS_OPT_DEEPEN_NOT,
  1143. (const char *)&deepen_not);
  1144. if (deepen_relative)
  1145. set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, "yes");
  1146. if (update_shallow)
  1147. set_option(transport, TRANS_OPT_UPDATE_SHALLOW, "yes");
  1148. if (filter_options.choice) {
  1149. const char *spec =
  1150. expand_list_objects_filter_spec(&filter_options);
  1151. set_option(transport, TRANS_OPT_LIST_OBJECTS_FILTER, spec);
  1152. set_option(transport, TRANS_OPT_FROM_PROMISOR, "1");
  1153. }
  1154. if (negotiation_tip.nr) {
  1155. if (transport->smart_options)
  1156. add_negotiation_tips(transport->smart_options);
  1157. else
  1158. warning("Ignoring --negotiation-tip because the protocol does not support it.");
  1159. }
  1160. return transport;
  1161. }
  1162. static void backfill_tags(struct transport *transport, struct ref *ref_map)
  1163. {
  1164. int cannot_reuse;
  1165. /*
  1166. * Once we have set TRANS_OPT_DEEPEN_SINCE, we can't unset it
  1167. * when remote helper is used (setting it to an empty string
  1168. * is not unsetting). We could extend the remote helper
  1169. * protocol for that, but for now, just force a new connection
  1170. * without deepen-since. Similar story for deepen-not.
  1171. */
  1172. cannot_reuse = transport->cannot_reuse ||
  1173. deepen_since || deepen_not.nr;
  1174. if (cannot_reuse) {
  1175. gsecondary = prepare_transport(transport->remote, 0);
  1176. transport = gsecondary;
  1177. }
  1178. transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);
  1179. transport_set_option(transport, TRANS_OPT_DEPTH, "0");
  1180. transport_set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, NULL);
  1181. if (!fetch_refs(transport, ref_map))
  1182. consume_refs(transport, ref_map);
  1183. if (gsecondary) {
  1184. transport_disconnect(gsecondary);
  1185. gsecondary = NULL;
  1186. }
  1187. }
  1188. static int do_fetch(struct transport *transport,
  1189. struct refspec *rs)
  1190. {
  1191. struct ref *ref_map;
  1192. int autotags = (transport->remote->fetch_tags == 1);
  1193. int retcode = 0;
  1194. const struct ref *remote_refs;
  1195. struct strvec ref_prefixes = STRVEC_INIT;
  1196. int must_list_refs = 1;
  1197. if (tags == TAGS_DEFAULT) {
  1198. if (transport->remote->fetch_tags == 2)
  1199. tags = TAGS_SET;
  1200. if (transport->remote->fetch_tags == -1)
  1201. tags = TAGS_UNSET;
  1202. }
  1203. /* if not appending, truncate FETCH_HEAD */
  1204. if (!append && write_fetch_head) {
  1205. retcode = truncate_fetch_head();
  1206. if (retcode)
  1207. goto cleanup;
  1208. }
  1209. if (rs->nr) {
  1210. int i;
  1211. refspec_ref_prefixes(rs, &ref_prefixes);
  1212. /*
  1213. * We can avoid listing refs if all of them are exact
  1214. * OIDs
  1215. */
  1216. must_list_refs = 0;
  1217. for (i = 0; i < rs->nr; i++) {
  1218. if (!rs->items[i].exact_sha1) {
  1219. must_list_refs = 1;
  1220. break;
  1221. }
  1222. }
  1223. } else if (transport->remote && transport->remote->fetch.nr)
  1224. refspec_ref_prefixes(&transport->remote->fetch, &ref_prefixes);
  1225. if (tags == TAGS_SET || tags == TAGS_DEFAULT) {
  1226. must_list_refs = 1;
  1227. if (ref_prefixes.nr)
  1228. strvec_push(&ref_prefixes, "refs/tags/");
  1229. }
  1230. if (must_list_refs) {
  1231. trace2_region_enter("fetch", "remote_refs", the_repository);
  1232. remote_refs = transport_get_remote_refs(transport, &ref_prefixes);
  1233. trace2_region_leave("fetch", "remote_refs", the_repository);
  1234. } else
  1235. remote_refs = NULL;
  1236. strvec_clear(&ref_prefixes);
  1237. ref_map = get_ref_map(transport->remote, remote_refs, rs,
  1238. tags, &autotags);
  1239. if (!update_head_ok)
  1240. check_not_current_branch(ref_map);
  1241. if (tags == TAGS_DEFAULT && autotags)
  1242. transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
  1243. if (prune) {
  1244. /*
  1245. * We only prune based on refspecs specified
  1246. * explicitly (via command line or configuration); we
  1247. * don't care whether --tags was specified.
  1248. */
  1249. if (rs->nr) {
  1250. prune_refs(rs, ref_map, transport->url);
  1251. } else {
  1252. prune_refs(&transport->remote->fetch,
  1253. ref_map,
  1254. transport->url);
  1255. }
  1256. }
  1257. if (fetch_refs(transport, ref_map) || consume_refs(transport, ref_map)) {
  1258. free_refs(ref_map);
  1259. retcode = 1;
  1260. goto cleanup;
  1261. }
  1262. if (set_upstream) {
  1263. struct branch *branch = branch_get("HEAD");
  1264. struct ref *rm;
  1265. struct ref *source_ref = NULL;
  1266. /*
  1267. * We're setting the upstream configuration for the
  1268. * current branch. The relevant upstream is the
  1269. * fetched branch that is meant to be merged with the
  1270. * current one, i.e. the one fetched to FETCH_HEAD.
  1271. *
  1272. * When there are several such branches, consider the
  1273. * request ambiguous and err on the safe side by doing
  1274. * nothing and just emit a warning.
  1275. */
  1276. for (rm = ref_map; rm; rm = rm->next) {
  1277. if (!rm->peer_ref) {
  1278. if (source_ref) {
  1279. warning(_("multiple branches detected, incompatible with --set-upstream"));
  1280. goto skip;
  1281. } else {
  1282. source_ref = rm;
  1283. }
  1284. }
  1285. }
  1286. if (source_ref) {
  1287. if (!strcmp(source_ref->name, "HEAD") ||
  1288. starts_with(source_ref->name, "refs/heads/"))
  1289. install_branch_config(0,
  1290. branch->name,
  1291. transport->remote->name,
  1292. source_ref->name);
  1293. else if (starts_with(source_ref->name, "refs/remotes/"))
  1294. warning(_("not setting upstream for a remote remote-tracking branch"));
  1295. else if (starts_with(source_ref->name, "refs/tags/"))
  1296. warning(_("not setting upstream for a remote tag"));
  1297. else
  1298. warning(_("unknown branch type"));
  1299. } else {
  1300. warning(_("no source branch found.\n"
  1301. "you need to specify exactly one branch with the --set-upstream option."));
  1302. }
  1303. }
  1304. skip:
  1305. free_refs(ref_map);
  1306. /* if neither --no-tags nor --tags was specified, do automated tag
  1307. * following ... */
  1308. if (tags == TAGS_DEFAULT && autotags) {
  1309. struct ref **tail = &ref_map;
  1310. ref_map = NULL;
  1311. find_non_local_tags(remote_refs, &ref_map, &tail);
  1312. if (ref_map)
  1313. backfill_tags(transport, ref_map);
  1314. free_refs(ref_map);
  1315. }
  1316. cleanup:
  1317. return retcode;
  1318. }
  1319. static int get_one_remote_for_fetch(struct remote *remote, void *priv)
  1320. {
  1321. struct string_list *list = priv;
  1322. if (!remote->skip_default_update)
  1323. string_list_append(list, remote->name);
  1324. return 0;
  1325. }
  1326. struct remote_group_data {
  1327. const char *name;
  1328. struct string_list *list;
  1329. };
  1330. static int get_remote_group(const char *key, const char *value, void *priv)
  1331. {
  1332. struct remote_group_data *g = priv;
  1333. if (skip_prefix(key, "remotes.", &key) && !strcmp(key, g->name)) {
  1334. /* split list by white space */
  1335. while (*value) {
  1336. size_t wordlen = strcspn(value, " \t\n");
  1337. if (wordlen >= 1)
  1338. string_list_append_nodup(g->list,
  1339. xstrndup(value, wordlen));
  1340. value += wordlen + (value[wordlen] != '\0');
  1341. }
  1342. }
  1343. return 0;
  1344. }
  1345. static int add_remote_or_group(const char *name, struct string_list *list)
  1346. {
  1347. int prev_nr = list->nr;
  1348. struct remote_group_data g;
  1349. g.name = name; g.list = list;
  1350. git_config(get_remote_group, &g);
  1351. if (list->nr == prev_nr) {
  1352. struct remote *remote = remote_get(name);
  1353. if (!remote_is_configured(remote, 0))
  1354. return 0;
  1355. string_list_append(list, remote->name);
  1356. }
  1357. return 1;
  1358. }
  1359. static void add_options_to_argv(struct strvec *argv)
  1360. {
  1361. if (dry_run)
  1362. strvec_push(argv, "--dry-run");
  1363. if (prune != -1)
  1364. strvec_push(argv, prune ? "--prune" : "--no-prune");
  1365. if (prune_tags != -1)
  1366. strvec_push(argv, prune_tags ? "--prune-tags" : "--no-prune-tags");
  1367. if (update_head_ok)
  1368. strvec_push(argv, "--update-head-ok");
  1369. if (force)
  1370. strvec_push(argv, "--force");
  1371. if (keep)
  1372. strvec_push(argv, "--keep");
  1373. if (recurse_submodules == RECURSE_SUBMODULES_ON)
  1374. strvec_push(argv, "--recurse-submodules");
  1375. else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)
  1376. strvec_push(argv, "--recurse-submodules=on-demand");
  1377. if (tags == TAGS_SET)
  1378. strvec_push(argv, "--tags");
  1379. else if (tags == TAGS_UNSET)
  1380. strvec_push(argv, "--no-tags");
  1381. if (verbosity >= 2)
  1382. strvec_push(argv, "-v");
  1383. if (verbosity >= 1)
  1384. strvec_push(argv, "-v");
  1385. else if (verbosity < 0)
  1386. strvec_push(argv, "-q");
  1387. if (family == TRANSPORT_FAMILY_IPV4)
  1388. strvec_push(argv, "--ipv4");
  1389. else if (family == TRANSPORT_FAMILY_IPV6)
  1390. strvec_push(argv, "--ipv6");
  1391. }
  1392. /* Fetch multiple remotes in parallel */
  1393. struct parallel_fetch_state {
  1394. const char **argv;
  1395. struct string_list *remotes;
  1396. int next, result;
  1397. };
  1398. static int fetch_next_remote(struct child_process *cp, struct strbuf *out,
  1399. void *cb, void **task_cb)
  1400. {
  1401. struct parallel_fetch_state *state = cb;
  1402. char *remote;
  1403. if (state->next < 0 || state->next >= state->remotes->nr)
  1404. return 0;
  1405. remote = state->remotes->items[state->next++].string;
  1406. *task_cb = remote;
  1407. strvec_pushv(&cp->args, state->argv);
  1408. strvec_push(&cp->args, remote);
  1409. cp->git_cmd = 1;
  1410. if (verbosity >= 0)
  1411. printf(_("Fetching %s\n"), remote);
  1412. return 1;
  1413. }
  1414. static int fetch_failed_to_start(struct strbuf *out, void *cb, void *task_cb)
  1415. {
  1416. struct parallel_fetch_state *state = cb;
  1417. const char *remote = task_cb;
  1418. state->result = error(_("Could not fetch %s"), remote);
  1419. return 0;
  1420. }
  1421. static int fetch_finished(int result, struct strbuf *out,
  1422. void *cb, void *task_cb)
  1423. {
  1424. struct parallel_fetch_state *state = cb;
  1425. const char *remote = task_cb;
  1426. if (result) {
  1427. strbuf_addf(out, _("could not fetch '%s' (exit code: %d)\n"),
  1428. remote, result);
  1429. state->result = -1;
  1430. }
  1431. return 0;
  1432. }
  1433. static int fetch_multiple(struct string_list *list, int max_children)
  1434. {
  1435. int i, result = 0;
  1436. struct strvec argv = STRVEC_INIT;
  1437. if (!append && write_fetch_head) {
  1438. int errcode = truncate_fetch_head();
  1439. if (errcode)
  1440. return errcode;
  1441. }
  1442. strvec_pushl(&argv, "fetch", "--append", "--no-auto-gc",
  1443. "--no-write-commit-graph", NULL);
  1444. add_options_to_argv(&argv);
  1445. if (max_children != 1 && list->nr != 1) {
  1446. struct parallel_fetch_state state = { argv.v, list, 0, 0 };
  1447. strvec_push(&argv, "--end-of-options");
  1448. result = run_processes_parallel_tr2(max_children,
  1449. &fetch_next_remote,
  1450. &fetch_failed_to_start,
  1451. &fetch_finished,
  1452. &state,
  1453. "fetch", "parallel/fetch");
  1454. if (!result)
  1455. result = state.result;
  1456. } else
  1457. for (i = 0; i < list->nr; i++) {
  1458. const char *name = list->items[i].string;
  1459. strvec_push(&argv, name);
  1460. if (verbosity >= 0)
  1461. printf(_("Fetching %s\n"), name);
  1462. if (run_command_v_opt(argv.v, RUN_GIT_CMD)) {
  1463. error(_("Could not fetch %s"), name);
  1464. result = 1;
  1465. }
  1466. strvec_pop(&argv);
  1467. }
  1468. strvec_clear(&argv);
  1469. return !!result;
  1470. }
  1471. /*
  1472. * Fetching from the promisor remote should use the given filter-spec
  1473. * or inherit the default filter-spec from the config.
  1474. */
  1475. static inline void fetch_one_setup_partial(struct remote *remote)
  1476. {
  1477. /*
  1478. * Explicit --no-filter argument overrides everything, regardless
  1479. * of any prior partial clones and fetches.
  1480. */
  1481. if (filter_options.no_filter)
  1482. return;
  1483. /*
  1484. * If no prior partial clone/fetch and the current fetch DID NOT
  1485. * request a partial-fetch, do a normal fetch.
  1486. */
  1487. if (!has_promisor_remote() && !filter_options.choice)
  1488. return;
  1489. /*
  1490. * If this is a partial-fetch request, we enable partial on
  1491. * this repo if not already enabled and remember the given
  1492. * filter-spec as the default for subsequent fetches to this
  1493. * remote if there is currently no default filter-spec.
  1494. */
  1495. if (filter_options.choice) {
  1496. partial_clone_register(remote->name, &filter_options);
  1497. return;
  1498. }
  1499. /*
  1500. * Do a partial-fetch from the promisor remote using either the
  1501. * explicitly given filter-spec or inherit the filter-spec from
  1502. * the config.
  1503. */
  1504. if (!filter_options.choice)
  1505. partial_clone_get_default_filter_spec(&filter_options, remote->name);
  1506. return;
  1507. }
  1508. static int fetch_one(struct remote *remote, int argc, const char **argv,
  1509. int prune_tags_ok, int use_stdin_refspecs)
  1510. {
  1511. struct refspec rs = REFSPEC_INIT_FETCH;
  1512. int i;
  1513. int exit_code;
  1514. int maybe_prune_tags;
  1515. int remote_via_config = remote_is_configured(remote, 0);
  1516. if (!remote)
  1517. die(_("No remote repository specified. Please, specify either a URL or a\n"
  1518. "remote name from which new revisions should be fetched."));
  1519. gtransport = prepare_transport(remote, 1);
  1520. if (prune < 0) {
  1521. /* no command line request */
  1522. if (0 <= remote->prune)
  1523. prune = remote->prune;
  1524. else if (0 <= fetch_prune_config)
  1525. prune = fetch_prune_config;
  1526. else
  1527. prune = PRUNE_BY_DEFAULT;
  1528. }
  1529. if (prune_tags < 0) {
  1530. /* no command line request */
  1531. if (0 <= remote->prune_tags)
  1532. prune_tags = remote->prune_tags;
  1533. else if (0 <= fetch_prune_tags_config)
  1534. prune_tags = fetch_prune_tags_config;
  1535. else
  1536. prune_tags = PRUNE_TAGS_BY_DEFAULT;
  1537. }
  1538. maybe_prune_tags = prune_tags_ok && prune_tags;
  1539. if (maybe_prune_tags && remote_via_config)
  1540. refspec_append(&remote->fetch, TAG_REFSPEC);
  1541. if (maybe_prune_tags && (argc || !remote_via_config))
  1542. refspec_append(&rs, TAG_REFSPEC);
  1543. for (i = 0; i < argc; i++) {
  1544. if (!strcmp(argv[i], "tag")) {
  1545. i++;
  1546. if (i >= argc)
  1547. die(_("You need to specify a tag name."));
  1548. refspec_appendf(&rs, "refs/tags/%s:refs/tags/%s",
  1549. argv[i], argv[i]);
  1550. } else {
  1551. refspec_append(&rs, argv[i]);
  1552. }
  1553. }
  1554. if (use_stdin_refspecs) {
  1555. struct strbuf line = STRBUF_INIT;
  1556. while (strbuf_getline_lf(&line, stdin) != EOF)
  1557. refspec_append(&rs, line.buf);
  1558. strbuf_release(&line);
  1559. }
  1560. if (server_options.nr)
  1561. gtransport->server_options = &server_options;
  1562. sigchain_push_common(unlock_pack_on_signal);
  1563. atexit(unlock_pack);
  1564. sigchain_push(SIGPIPE, SIG_IGN);
  1565. exit_code = do_fetch(gtransport, &rs);
  1566. sigchain_pop(SIGPIPE);
  1567. refspec_clear(&rs);
  1568. transport_disconnect(gtransport);
  1569. gtransport = NULL;
  1570. return exit_code;
  1571. }
  1572. int cmd_fetch(int argc, const char **argv, const char *prefix)
  1573. {
  1574. int i;
  1575. struct string_list list = STRING_LIST_INIT_DUP;
  1576. struct remote *remote = NULL;
  1577. int result = 0;
  1578. int prune_tags_ok = 1;
  1579. packet_trace_identity("fetch");
  1580. /* Record the command line for the reflog */
  1581. strbuf_addstr(&default_rla, "fetch");
  1582. for (i = 1; i < argc; i++) {
  1583. /* This handles non-URLs gracefully */
  1584. char *anon = transport_anonymize_url(argv[i]);
  1585. strbuf_addf(&default_rla, " %s", anon);
  1586. free(anon);
  1587. }
  1588. git_config(git_fetch_config, NULL);
  1589. argc = parse_options(argc, argv, prefix,
  1590. builtin_fetch_options, builtin_fetch_usage, 0);
  1591. if (recurse_submodules != RECURSE_SUBMODULES_OFF) {
  1592. int *sfjc = submodule_fetch_jobs_config == -1
  1593. ? &submodule_fetch_jobs_config : NULL;
  1594. int *rs = recurse_submodules == RECURSE_SUBMODULES_DEFAULT
  1595. ? &recurse_submodules : NULL;
  1596. fetch_config_from_gitmodules(sfjc, rs);
  1597. }
  1598. if (deepen_relative) {
  1599. if (deepen_relative < 0)
  1600. die(_("Negative depth in --deepen is not supported"));
  1601. if (depth)
  1602. die(_("--deepen and --depth are mutually exclusive"));
  1603. depth = xstrfmt("%d", deepen_relative);
  1604. }
  1605. if (unshallow) {
  1606. if (depth)
  1607. die(_("--depth and --unshallow cannot be used together"));
  1608. else if (!is_repository_shallow(the_repository))
  1609. die(_("--unshallow on a complete repository does not make sense"));
  1610. else
  1611. depth = xstrfmt("%d", INFINITE_DEPTH);
  1612. }
  1613. /* no need to be strict, transport_set_option() will validate it again */
  1614. if (depth && atoi(depth) < 1)
  1615. die(_("depth %s is not a positive number"), depth);
  1616. if (depth || deepen_since || deepen_not.nr)
  1617. deepen = 1;
  1618. /* FETCH_HEAD never gets updated in --dry-run mode */
  1619. if (dry_run)
  1620. write_fetch_head = 0;
  1621. if (all) {
  1622. if (argc == 1)
  1623. die(_("fetch --all does not take a repository argument"));
  1624. else if (argc > 1)
  1625. die(_("fetch --all does not make sense with refspecs"));
  1626. (void) for_each_remote(get_one_remote_for_fetch, &list);
  1627. } else if (argc == 0) {
  1628. /* No arguments -- use default remote */
  1629. remote = remote_get(NULL);
  1630. } else if (multiple) {
  1631. /* All arguments are assumed to be remotes or groups */
  1632. for (i = 0; i < argc; i++)
  1633. if (!add_remote_or_group(argv[i], &list))
  1634. die(_("No such remote or remote group: %s"), argv[i]);
  1635. } else {
  1636. /* Single remote or group */
  1637. (void) add_remote_or_group(argv[0], &list);
  1638. if (list.nr > 1) {
  1639. /* More than one remote */
  1640. if (argc > 1)
  1641. die(_("Fetching a group and specifying refspecs does not make sense"));
  1642. } else {
  1643. /* Zero or one remotes */
  1644. remote = remote_get(argv[0]);
  1645. prune_tags_ok = (argc == 1);
  1646. argc--;
  1647. argv++;
  1648. }
  1649. }
  1650. if (remote) {
  1651. if (filter_options.choice || has_promisor_remote())
  1652. fetch_one_setup_partial(remote);
  1653. result = fetch_one(remote, argc, argv, prune_tags_ok, stdin_refspecs);
  1654. } else {
  1655. int max_children = max_jobs;
  1656. if (filter_options.choice)
  1657. die(_("--filter can only be used with the remote "
  1658. "configured in extensions.partialclone"));
  1659. if (stdin_refspecs)
  1660. die(_("--stdin can only be used when fetching "
  1661. "from one remote"));
  1662. if (max_children < 0)
  1663. max_children = fetch_parallel_config;
  1664. /* TODO should this also die if we have a previous partial-clone? */
  1665. result = fetch_multiple(&list, max_children);
  1666. }
  1667. if (!result && (recurse_submodules != RECURSE_SUBMODULES_OFF)) {
  1668. struct strvec options = STRVEC_INIT;
  1669. int max_children = max_jobs;
  1670. if (max_children < 0)
  1671. max_children = submodule_fetch_jobs_config;
  1672. if (max_children < 0)
  1673. max_children = fetch_parallel_config;
  1674. add_options_to_argv(&options);
  1675. result = fetch_populated_submodules(the_repository,
  1676. &options,
  1677. submodule_prefix,
  1678. recurse_submodules,
  1679. recurse_submodules_default,
  1680. verbosity < 0,
  1681. max_children);
  1682. strvec_clear(&options);
  1683. }
  1684. string_list_clear(&list, 0);
  1685. prepare_repo_settings(the_repository);
  1686. if (fetch_write_commit_graph > 0 ||
  1687. (fetch_write_commit_graph < 0 &&
  1688. the_repository->settings.fetch_write_commit_graph)) {
  1689. int commit_graph_flags = COMMIT_GRAPH_WRITE_SPLIT;
  1690. if (progress)
  1691. commit_graph_flags |= COMMIT_GRAPH_WRITE_PROGRESS;
  1692. write_commit_graph_reachable(the_repository->objects->odb,
  1693. commit_graph_flags,
  1694. NULL);
  1695. }
  1696. close_object_store(the_repository->objects);
  1697. if (enable_auto_gc)
  1698. run_auto_maintenance(verbosity < 0);
  1699. return result;
  1700. }