clone.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338
  1. /*
  2. * Builtin "git clone"
  3. *
  4. * Copyright (c) 2007 Kristian Høgsberg <krh@redhat.com>,
  5. * 2008 Daniel Barkalow <barkalow@iabervon.org>
  6. * Based on git-commit.sh by Junio C Hamano and Linus Torvalds
  7. *
  8. * Clone a repository into a different directory that does not yet exist.
  9. */
  10. #define USE_THE_INDEX_COMPATIBILITY_MACROS
  11. #include "builtin.h"
  12. #include "config.h"
  13. #include "lockfile.h"
  14. #include "parse-options.h"
  15. #include "fetch-pack.h"
  16. #include "refs.h"
  17. #include "refspec.h"
  18. #include "object-store.h"
  19. #include "tree.h"
  20. #include "tree-walk.h"
  21. #include "unpack-trees.h"
  22. #include "transport.h"
  23. #include "strbuf.h"
  24. #include "dir.h"
  25. #include "dir-iterator.h"
  26. #include "iterator.h"
  27. #include "sigchain.h"
  28. #include "branch.h"
  29. #include "remote.h"
  30. #include "run-command.h"
  31. #include "connected.h"
  32. #include "packfile.h"
  33. #include "list-objects-filter-options.h"
  34. /*
  35. * Overall FIXMEs:
  36. * - respect DB_ENVIRONMENT for .git/objects.
  37. *
  38. * Implementation notes:
  39. * - dropping use-separate-remote and no-separate-remote compatibility
  40. *
  41. */
  42. static const char * const builtin_clone_usage[] = {
  43. N_("git clone [<options>] [--] <repo> [<dir>]"),
  44. NULL
  45. };
  46. static int option_no_checkout, option_bare, option_mirror, option_single_branch = -1;
  47. static int option_local = -1, option_no_hardlinks, option_shared;
  48. static int option_no_tags;
  49. static int option_shallow_submodules;
  50. static int deepen;
  51. static char *option_template, *option_depth, *option_since;
  52. static char *option_origin = NULL;
  53. static char *option_branch = NULL;
  54. static struct string_list option_not = STRING_LIST_INIT_NODUP;
  55. static const char *real_git_dir;
  56. static char *option_upload_pack = "git-upload-pack";
  57. static int option_verbosity;
  58. static int option_progress = -1;
  59. static int option_sparse_checkout;
  60. static enum transport_family family;
  61. static struct string_list option_config = STRING_LIST_INIT_NODUP;
  62. static struct string_list option_required_reference = STRING_LIST_INIT_NODUP;
  63. static struct string_list option_optional_reference = STRING_LIST_INIT_NODUP;
  64. static int option_dissociate;
  65. static int max_jobs = -1;
  66. static struct string_list option_recurse_submodules = STRING_LIST_INIT_NODUP;
  67. static struct list_objects_filter_options filter_options;
  68. static struct string_list server_options = STRING_LIST_INIT_NODUP;
  69. static int option_remote_submodules;
  70. static int recurse_submodules_cb(const struct option *opt,
  71. const char *arg, int unset)
  72. {
  73. if (unset)
  74. string_list_clear((struct string_list *)opt->value, 0);
  75. else if (arg)
  76. string_list_append((struct string_list *)opt->value, arg);
  77. else
  78. string_list_append((struct string_list *)opt->value,
  79. (const char *)opt->defval);
  80. return 0;
  81. }
  82. static struct option builtin_clone_options[] = {
  83. OPT__VERBOSITY(&option_verbosity),
  84. OPT_BOOL(0, "progress", &option_progress,
  85. N_("force progress reporting")),
  86. OPT_BOOL('n', "no-checkout", &option_no_checkout,
  87. N_("don't create a checkout")),
  88. OPT_BOOL(0, "bare", &option_bare, N_("create a bare repository")),
  89. OPT_HIDDEN_BOOL(0, "naked", &option_bare,
  90. N_("create a bare repository")),
  91. OPT_BOOL(0, "mirror", &option_mirror,
  92. N_("create a mirror repository (implies bare)")),
  93. OPT_BOOL('l', "local", &option_local,
  94. N_("to clone from a local repository")),
  95. OPT_BOOL(0, "no-hardlinks", &option_no_hardlinks,
  96. N_("don't use local hardlinks, always copy")),
  97. OPT_BOOL('s', "shared", &option_shared,
  98. N_("setup as shared repository")),
  99. { OPTION_CALLBACK, 0, "recurse-submodules", &option_recurse_submodules,
  100. N_("pathspec"), N_("initialize submodules in the clone"),
  101. PARSE_OPT_OPTARG, recurse_submodules_cb, (intptr_t)"." },
  102. OPT_ALIAS(0, "recursive", "recurse-submodules"),
  103. OPT_INTEGER('j', "jobs", &max_jobs,
  104. N_("number of submodules cloned in parallel")),
  105. OPT_STRING(0, "template", &option_template, N_("template-directory"),
  106. N_("directory from which templates will be used")),
  107. OPT_STRING_LIST(0, "reference", &option_required_reference, N_("repo"),
  108. N_("reference repository")),
  109. OPT_STRING_LIST(0, "reference-if-able", &option_optional_reference,
  110. N_("repo"), N_("reference repository")),
  111. OPT_BOOL(0, "dissociate", &option_dissociate,
  112. N_("use --reference only while cloning")),
  113. OPT_STRING('o', "origin", &option_origin, N_("name"),
  114. N_("use <name> instead of 'origin' to track upstream")),
  115. OPT_STRING('b', "branch", &option_branch, N_("branch"),
  116. N_("checkout <branch> instead of the remote's HEAD")),
  117. OPT_STRING('u', "upload-pack", &option_upload_pack, N_("path"),
  118. N_("path to git-upload-pack on the remote")),
  119. OPT_STRING(0, "depth", &option_depth, N_("depth"),
  120. N_("create a shallow clone of that depth")),
  121. OPT_STRING(0, "shallow-since", &option_since, N_("time"),
  122. N_("create a shallow clone since a specific time")),
  123. OPT_STRING_LIST(0, "shallow-exclude", &option_not, N_("revision"),
  124. N_("deepen history of shallow clone, excluding rev")),
  125. OPT_BOOL(0, "single-branch", &option_single_branch,
  126. N_("clone only one branch, HEAD or --branch")),
  127. OPT_BOOL(0, "no-tags", &option_no_tags,
  128. N_("don't clone any tags, and make later fetches not to follow them")),
  129. OPT_BOOL(0, "shallow-submodules", &option_shallow_submodules,
  130. N_("any cloned submodules will be shallow")),
  131. OPT_STRING(0, "separate-git-dir", &real_git_dir, N_("gitdir"),
  132. N_("separate git dir from working tree")),
  133. OPT_STRING_LIST('c', "config", &option_config, N_("key=value"),
  134. N_("set config inside the new repository")),
  135. OPT_STRING_LIST(0, "server-option", &server_options,
  136. N_("server-specific"), N_("option to transmit")),
  137. OPT_SET_INT('4', "ipv4", &family, N_("use IPv4 addresses only"),
  138. TRANSPORT_FAMILY_IPV4),
  139. OPT_SET_INT('6', "ipv6", &family, N_("use IPv6 addresses only"),
  140. TRANSPORT_FAMILY_IPV6),
  141. OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
  142. OPT_BOOL(0, "remote-submodules", &option_remote_submodules,
  143. N_("any cloned submodules will use their remote-tracking branch")),
  144. OPT_BOOL(0, "sparse", &option_sparse_checkout,
  145. N_("initialize sparse-checkout file to include only files at root")),
  146. OPT_END()
  147. };
  148. static const char *get_repo_path_1(struct strbuf *path, int *is_bundle)
  149. {
  150. static char *suffix[] = { "/.git", "", ".git/.git", ".git" };
  151. static char *bundle_suffix[] = { ".bundle", "" };
  152. size_t baselen = path->len;
  153. struct stat st;
  154. int i;
  155. for (i = 0; i < ARRAY_SIZE(suffix); i++) {
  156. strbuf_setlen(path, baselen);
  157. strbuf_addstr(path, suffix[i]);
  158. if (stat(path->buf, &st))
  159. continue;
  160. if (S_ISDIR(st.st_mode) && is_git_directory(path->buf)) {
  161. *is_bundle = 0;
  162. return path->buf;
  163. } else if (S_ISREG(st.st_mode) && st.st_size > 8) {
  164. /* Is it a "gitfile"? */
  165. char signature[8];
  166. const char *dst;
  167. int len, fd = open(path->buf, O_RDONLY);
  168. if (fd < 0)
  169. continue;
  170. len = read_in_full(fd, signature, 8);
  171. close(fd);
  172. if (len != 8 || strncmp(signature, "gitdir: ", 8))
  173. continue;
  174. dst = read_gitfile(path->buf);
  175. if (dst) {
  176. *is_bundle = 0;
  177. return dst;
  178. }
  179. }
  180. }
  181. for (i = 0; i < ARRAY_SIZE(bundle_suffix); i++) {
  182. strbuf_setlen(path, baselen);
  183. strbuf_addstr(path, bundle_suffix[i]);
  184. if (!stat(path->buf, &st) && S_ISREG(st.st_mode)) {
  185. *is_bundle = 1;
  186. return path->buf;
  187. }
  188. }
  189. return NULL;
  190. }
  191. static char *get_repo_path(const char *repo, int *is_bundle)
  192. {
  193. struct strbuf path = STRBUF_INIT;
  194. const char *raw;
  195. char *canon;
  196. strbuf_addstr(&path, repo);
  197. raw = get_repo_path_1(&path, is_bundle);
  198. canon = raw ? absolute_pathdup(raw) : NULL;
  199. strbuf_release(&path);
  200. return canon;
  201. }
  202. static char *guess_dir_name(const char *repo, int is_bundle, int is_bare)
  203. {
  204. const char *end = repo + strlen(repo), *start, *ptr;
  205. size_t len;
  206. char *dir;
  207. /*
  208. * Skip scheme.
  209. */
  210. start = strstr(repo, "://");
  211. if (start == NULL)
  212. start = repo;
  213. else
  214. start += 3;
  215. /*
  216. * Skip authentication data. The stripping does happen
  217. * greedily, such that we strip up to the last '@' inside
  218. * the host part.
  219. */
  220. for (ptr = start; ptr < end && !is_dir_sep(*ptr); ptr++) {
  221. if (*ptr == '@')
  222. start = ptr + 1;
  223. }
  224. /*
  225. * Strip trailing spaces, slashes and /.git
  226. */
  227. while (start < end && (is_dir_sep(end[-1]) || isspace(end[-1])))
  228. end--;
  229. if (end - start > 5 && is_dir_sep(end[-5]) &&
  230. !strncmp(end - 4, ".git", 4)) {
  231. end -= 5;
  232. while (start < end && is_dir_sep(end[-1]))
  233. end--;
  234. }
  235. /*
  236. * Strip trailing port number if we've got only a
  237. * hostname (that is, there is no dir separator but a
  238. * colon). This check is required such that we do not
  239. * strip URI's like '/foo/bar:2222.git', which should
  240. * result in a dir '2222' being guessed due to backwards
  241. * compatibility.
  242. */
  243. if (memchr(start, '/', end - start) == NULL
  244. && memchr(start, ':', end - start) != NULL) {
  245. ptr = end;
  246. while (start < ptr && isdigit(ptr[-1]) && ptr[-1] != ':')
  247. ptr--;
  248. if (start < ptr && ptr[-1] == ':')
  249. end = ptr - 1;
  250. }
  251. /*
  252. * Find last component. To remain backwards compatible we
  253. * also regard colons as path separators, such that
  254. * cloning a repository 'foo:bar.git' would result in a
  255. * directory 'bar' being guessed.
  256. */
  257. ptr = end;
  258. while (start < ptr && !is_dir_sep(ptr[-1]) && ptr[-1] != ':')
  259. ptr--;
  260. start = ptr;
  261. /*
  262. * Strip .{bundle,git}.
  263. */
  264. len = end - start;
  265. strip_suffix_mem(start, &len, is_bundle ? ".bundle" : ".git");
  266. if (!len || (len == 1 && *start == '/'))
  267. die(_("No directory name could be guessed.\n"
  268. "Please specify a directory on the command line"));
  269. if (is_bare)
  270. dir = xstrfmt("%.*s.git", (int)len, start);
  271. else
  272. dir = xstrndup(start, len);
  273. /*
  274. * Replace sequences of 'control' characters and whitespace
  275. * with one ascii space, remove leading and trailing spaces.
  276. */
  277. if (*dir) {
  278. char *out = dir;
  279. int prev_space = 1 /* strip leading whitespace */;
  280. for (end = dir; *end; ++end) {
  281. char ch = *end;
  282. if ((unsigned char)ch < '\x20')
  283. ch = '\x20';
  284. if (isspace(ch)) {
  285. if (prev_space)
  286. continue;
  287. prev_space = 1;
  288. } else
  289. prev_space = 0;
  290. *out++ = ch;
  291. }
  292. *out = '\0';
  293. if (out > dir && prev_space)
  294. out[-1] = '\0';
  295. }
  296. return dir;
  297. }
  298. static void strip_trailing_slashes(char *dir)
  299. {
  300. char *end = dir + strlen(dir);
  301. while (dir < end - 1 && is_dir_sep(end[-1]))
  302. end--;
  303. *end = '\0';
  304. }
  305. static int add_one_reference(struct string_list_item *item, void *cb_data)
  306. {
  307. struct strbuf err = STRBUF_INIT;
  308. int *required = cb_data;
  309. char *ref_git = compute_alternate_path(item->string, &err);
  310. if (!ref_git) {
  311. if (*required)
  312. die("%s", err.buf);
  313. else
  314. fprintf(stderr,
  315. _("info: Could not add alternate for '%s': %s\n"),
  316. item->string, err.buf);
  317. } else {
  318. struct strbuf sb = STRBUF_INIT;
  319. strbuf_addf(&sb, "%s/objects", ref_git);
  320. add_to_alternates_file(sb.buf);
  321. strbuf_release(&sb);
  322. }
  323. strbuf_release(&err);
  324. free(ref_git);
  325. return 0;
  326. }
  327. static void setup_reference(void)
  328. {
  329. int required = 1;
  330. for_each_string_list(&option_required_reference,
  331. add_one_reference, &required);
  332. required = 0;
  333. for_each_string_list(&option_optional_reference,
  334. add_one_reference, &required);
  335. }
  336. static void copy_alternates(struct strbuf *src, const char *src_repo)
  337. {
  338. /*
  339. * Read from the source objects/info/alternates file
  340. * and copy the entries to corresponding file in the
  341. * destination repository with add_to_alternates_file().
  342. * Both src and dst have "$path/objects/info/alternates".
  343. *
  344. * Instead of copying bit-for-bit from the original,
  345. * we need to append to existing one so that the already
  346. * created entry via "clone -s" is not lost, and also
  347. * to turn entries with paths relative to the original
  348. * absolute, so that they can be used in the new repository.
  349. */
  350. FILE *in = xfopen(src->buf, "r");
  351. struct strbuf line = STRBUF_INIT;
  352. while (strbuf_getline(&line, in) != EOF) {
  353. char *abs_path;
  354. if (!line.len || line.buf[0] == '#')
  355. continue;
  356. if (is_absolute_path(line.buf)) {
  357. add_to_alternates_file(line.buf);
  358. continue;
  359. }
  360. abs_path = mkpathdup("%s/objects/%s", src_repo, line.buf);
  361. if (!normalize_path_copy(abs_path, abs_path))
  362. add_to_alternates_file(abs_path);
  363. else
  364. warning("skipping invalid relative alternate: %s/%s",
  365. src_repo, line.buf);
  366. free(abs_path);
  367. }
  368. strbuf_release(&line);
  369. fclose(in);
  370. }
  371. static void mkdir_if_missing(const char *pathname, mode_t mode)
  372. {
  373. struct stat st;
  374. if (!mkdir(pathname, mode))
  375. return;
  376. if (errno != EEXIST)
  377. die_errno(_("failed to create directory '%s'"), pathname);
  378. else if (stat(pathname, &st))
  379. die_errno(_("failed to stat '%s'"), pathname);
  380. else if (!S_ISDIR(st.st_mode))
  381. die(_("%s exists and is not a directory"), pathname);
  382. }
  383. static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest,
  384. const char *src_repo)
  385. {
  386. int src_len, dest_len;
  387. struct dir_iterator *iter;
  388. int iter_status;
  389. unsigned int flags;
  390. struct strbuf realpath = STRBUF_INIT;
  391. mkdir_if_missing(dest->buf, 0777);
  392. flags = DIR_ITERATOR_PEDANTIC | DIR_ITERATOR_FOLLOW_SYMLINKS;
  393. iter = dir_iterator_begin(src->buf, flags);
  394. if (!iter)
  395. die_errno(_("failed to start iterator over '%s'"), src->buf);
  396. strbuf_addch(src, '/');
  397. src_len = src->len;
  398. strbuf_addch(dest, '/');
  399. dest_len = dest->len;
  400. while ((iter_status = dir_iterator_advance(iter)) == ITER_OK) {
  401. strbuf_setlen(src, src_len);
  402. strbuf_addstr(src, iter->relative_path);
  403. strbuf_setlen(dest, dest_len);
  404. strbuf_addstr(dest, iter->relative_path);
  405. if (S_ISDIR(iter->st.st_mode)) {
  406. mkdir_if_missing(dest->buf, 0777);
  407. continue;
  408. }
  409. /* Files that cannot be copied bit-for-bit... */
  410. if (!fspathcmp(iter->relative_path, "info/alternates")) {
  411. copy_alternates(src, src_repo);
  412. continue;
  413. }
  414. if (unlink(dest->buf) && errno != ENOENT)
  415. die_errno(_("failed to unlink '%s'"), dest->buf);
  416. if (!option_no_hardlinks) {
  417. strbuf_realpath(&realpath, src->buf, 1);
  418. if (!link(realpath.buf, dest->buf))
  419. continue;
  420. if (option_local > 0)
  421. die_errno(_("failed to create link '%s'"), dest->buf);
  422. option_no_hardlinks = 1;
  423. }
  424. if (copy_file_with_time(dest->buf, src->buf, 0666))
  425. die_errno(_("failed to copy file to '%s'"), dest->buf);
  426. }
  427. if (iter_status != ITER_DONE) {
  428. strbuf_setlen(src, src_len);
  429. die(_("failed to iterate over '%s'"), src->buf);
  430. }
  431. strbuf_release(&realpath);
  432. }
  433. static void clone_local(const char *src_repo, const char *dest_repo)
  434. {
  435. if (option_shared) {
  436. struct strbuf alt = STRBUF_INIT;
  437. get_common_dir(&alt, src_repo);
  438. strbuf_addstr(&alt, "/objects");
  439. add_to_alternates_file(alt.buf);
  440. strbuf_release(&alt);
  441. } else {
  442. struct strbuf src = STRBUF_INIT;
  443. struct strbuf dest = STRBUF_INIT;
  444. get_common_dir(&src, src_repo);
  445. get_common_dir(&dest, dest_repo);
  446. strbuf_addstr(&src, "/objects");
  447. strbuf_addstr(&dest, "/objects");
  448. copy_or_link_directory(&src, &dest, src_repo);
  449. strbuf_release(&src);
  450. strbuf_release(&dest);
  451. }
  452. if (0 <= option_verbosity)
  453. fprintf(stderr, _("done.\n"));
  454. }
  455. static const char *junk_work_tree;
  456. static int junk_work_tree_flags;
  457. static const char *junk_git_dir;
  458. static int junk_git_dir_flags;
  459. static enum {
  460. JUNK_LEAVE_NONE,
  461. JUNK_LEAVE_REPO,
  462. JUNK_LEAVE_ALL
  463. } junk_mode = JUNK_LEAVE_NONE;
  464. static const char junk_leave_repo_msg[] =
  465. N_("Clone succeeded, but checkout failed.\n"
  466. "You can inspect what was checked out with 'git status'\n"
  467. "and retry with 'git restore --source=HEAD :/'\n");
  468. static void remove_junk(void)
  469. {
  470. struct strbuf sb = STRBUF_INIT;
  471. switch (junk_mode) {
  472. case JUNK_LEAVE_REPO:
  473. warning("%s", _(junk_leave_repo_msg));
  474. /* fall-through */
  475. case JUNK_LEAVE_ALL:
  476. return;
  477. default:
  478. /* proceed to removal */
  479. break;
  480. }
  481. if (junk_git_dir) {
  482. strbuf_addstr(&sb, junk_git_dir);
  483. remove_dir_recursively(&sb, junk_git_dir_flags);
  484. strbuf_reset(&sb);
  485. }
  486. if (junk_work_tree) {
  487. strbuf_addstr(&sb, junk_work_tree);
  488. remove_dir_recursively(&sb, junk_work_tree_flags);
  489. }
  490. strbuf_release(&sb);
  491. }
  492. static void remove_junk_on_signal(int signo)
  493. {
  494. remove_junk();
  495. sigchain_pop(signo);
  496. raise(signo);
  497. }
  498. static struct ref *find_remote_branch(const struct ref *refs, const char *branch)
  499. {
  500. struct ref *ref;
  501. struct strbuf head = STRBUF_INIT;
  502. strbuf_addstr(&head, "refs/heads/");
  503. strbuf_addstr(&head, branch);
  504. ref = find_ref_by_name(refs, head.buf);
  505. strbuf_release(&head);
  506. if (ref)
  507. return ref;
  508. strbuf_addstr(&head, "refs/tags/");
  509. strbuf_addstr(&head, branch);
  510. ref = find_ref_by_name(refs, head.buf);
  511. strbuf_release(&head);
  512. return ref;
  513. }
  514. static struct ref *wanted_peer_refs(const struct ref *refs,
  515. struct refspec *refspec)
  516. {
  517. struct ref *head = copy_ref(find_ref_by_name(refs, "HEAD"));
  518. struct ref *local_refs = head;
  519. struct ref **tail = head ? &head->next : &local_refs;
  520. if (option_single_branch) {
  521. struct ref *remote_head = NULL;
  522. if (!option_branch)
  523. remote_head = guess_remote_head(head, refs, 0);
  524. else {
  525. local_refs = NULL;
  526. tail = &local_refs;
  527. remote_head = copy_ref(find_remote_branch(refs, option_branch));
  528. }
  529. if (!remote_head && option_branch)
  530. warning(_("Could not find remote branch %s to clone."),
  531. option_branch);
  532. else {
  533. int i;
  534. for (i = 0; i < refspec->nr; i++)
  535. get_fetch_map(remote_head, &refspec->items[i],
  536. &tail, 0);
  537. /* if --branch=tag, pull the requested tag explicitly */
  538. get_fetch_map(remote_head, tag_refspec, &tail, 0);
  539. }
  540. } else {
  541. int i;
  542. for (i = 0; i < refspec->nr; i++)
  543. get_fetch_map(refs, &refspec->items[i], &tail, 0);
  544. }
  545. if (!option_mirror && !option_single_branch && !option_no_tags)
  546. get_fetch_map(refs, tag_refspec, &tail, 0);
  547. return local_refs;
  548. }
  549. static void write_remote_refs(const struct ref *local_refs)
  550. {
  551. const struct ref *r;
  552. struct ref_transaction *t;
  553. struct strbuf err = STRBUF_INIT;
  554. t = ref_transaction_begin(&err);
  555. if (!t)
  556. die("%s", err.buf);
  557. for (r = local_refs; r; r = r->next) {
  558. if (!r->peer_ref)
  559. continue;
  560. if (ref_transaction_create(t, r->peer_ref->name, &r->old_oid,
  561. 0, NULL, &err))
  562. die("%s", err.buf);
  563. }
  564. if (initial_ref_transaction_commit(t, &err))
  565. die("%s", err.buf);
  566. strbuf_release(&err);
  567. ref_transaction_free(t);
  568. }
  569. static void write_followtags(const struct ref *refs, const char *msg)
  570. {
  571. const struct ref *ref;
  572. for (ref = refs; ref; ref = ref->next) {
  573. if (!starts_with(ref->name, "refs/tags/"))
  574. continue;
  575. if (ends_with(ref->name, "^{}"))
  576. continue;
  577. if (!has_object_file_with_flags(&ref->old_oid,
  578. OBJECT_INFO_QUICK |
  579. OBJECT_INFO_SKIP_FETCH_OBJECT))
  580. continue;
  581. update_ref(msg, ref->name, &ref->old_oid, NULL, 0,
  582. UPDATE_REFS_DIE_ON_ERR);
  583. }
  584. }
  585. static int iterate_ref_map(void *cb_data, struct object_id *oid)
  586. {
  587. struct ref **rm = cb_data;
  588. struct ref *ref = *rm;
  589. /*
  590. * Skip anything missing a peer_ref, which we are not
  591. * actually going to write a ref for.
  592. */
  593. while (ref && !ref->peer_ref)
  594. ref = ref->next;
  595. /* Returning -1 notes "end of list" to the caller. */
  596. if (!ref)
  597. return -1;
  598. oidcpy(oid, &ref->old_oid);
  599. *rm = ref->next;
  600. return 0;
  601. }
  602. static void update_remote_refs(const struct ref *refs,
  603. const struct ref *mapped_refs,
  604. const struct ref *remote_head_points_at,
  605. const char *branch_top,
  606. const char *msg,
  607. struct transport *transport,
  608. int check_connectivity)
  609. {
  610. const struct ref *rm = mapped_refs;
  611. if (check_connectivity) {
  612. struct check_connected_options opt = CHECK_CONNECTED_INIT;
  613. opt.transport = transport;
  614. opt.progress = transport->progress;
  615. if (check_connected(iterate_ref_map, &rm, &opt))
  616. die(_("remote did not send all necessary objects"));
  617. }
  618. if (refs) {
  619. write_remote_refs(mapped_refs);
  620. if (option_single_branch && !option_no_tags)
  621. write_followtags(refs, msg);
  622. }
  623. if (remote_head_points_at && !option_bare) {
  624. struct strbuf head_ref = STRBUF_INIT;
  625. strbuf_addstr(&head_ref, branch_top);
  626. strbuf_addstr(&head_ref, "HEAD");
  627. if (create_symref(head_ref.buf,
  628. remote_head_points_at->peer_ref->name,
  629. msg) < 0)
  630. die(_("unable to update %s"), head_ref.buf);
  631. strbuf_release(&head_ref);
  632. }
  633. }
  634. static void update_head(const struct ref *our, const struct ref *remote,
  635. const char *msg)
  636. {
  637. const char *head;
  638. if (our && skip_prefix(our->name, "refs/heads/", &head)) {
  639. /* Local default branch link */
  640. if (create_symref("HEAD", our->name, NULL) < 0)
  641. die(_("unable to update HEAD"));
  642. if (!option_bare) {
  643. update_ref(msg, "HEAD", &our->old_oid, NULL, 0,
  644. UPDATE_REFS_DIE_ON_ERR);
  645. install_branch_config(0, head, option_origin, our->name);
  646. }
  647. } else if (our) {
  648. struct commit *c = lookup_commit_reference(the_repository,
  649. &our->old_oid);
  650. /* --branch specifies a non-branch (i.e. tags), detach HEAD */
  651. update_ref(msg, "HEAD", &c->object.oid, NULL, REF_NO_DEREF,
  652. UPDATE_REFS_DIE_ON_ERR);
  653. } else if (remote) {
  654. /*
  655. * We know remote HEAD points to a non-branch, or
  656. * HEAD points to a branch but we don't know which one.
  657. * Detach HEAD in all these cases.
  658. */
  659. update_ref(msg, "HEAD", &remote->old_oid, NULL, REF_NO_DEREF,
  660. UPDATE_REFS_DIE_ON_ERR);
  661. }
  662. }
  663. static int git_sparse_checkout_init(const char *repo)
  664. {
  665. struct strvec argv = STRVEC_INIT;
  666. int result = 0;
  667. strvec_pushl(&argv, "-C", repo, "sparse-checkout", "init", NULL);
  668. /*
  669. * We must apply the setting in the current process
  670. * for the later checkout to use the sparse-checkout file.
  671. */
  672. core_apply_sparse_checkout = 1;
  673. if (run_command_v_opt(argv.v, RUN_GIT_CMD)) {
  674. error(_("failed to initialize sparse-checkout"));
  675. result = 1;
  676. }
  677. strvec_clear(&argv);
  678. return result;
  679. }
  680. static int checkout(int submodule_progress)
  681. {
  682. struct object_id oid;
  683. char *head;
  684. struct lock_file lock_file = LOCK_INIT;
  685. struct unpack_trees_options opts;
  686. struct tree *tree;
  687. struct tree_desc t;
  688. int err = 0;
  689. if (option_no_checkout)
  690. return 0;
  691. head = resolve_refdup("HEAD", RESOLVE_REF_READING, &oid, NULL);
  692. if (!head) {
  693. warning(_("remote HEAD refers to nonexistent ref, "
  694. "unable to checkout.\n"));
  695. return 0;
  696. }
  697. if (!strcmp(head, "HEAD")) {
  698. if (advice_detached_head)
  699. detach_advice(oid_to_hex(&oid));
  700. FREE_AND_NULL(head);
  701. } else {
  702. if (!starts_with(head, "refs/heads/"))
  703. die(_("HEAD not found below refs/heads!"));
  704. }
  705. /* We need to be in the new work tree for the checkout */
  706. setup_work_tree();
  707. hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
  708. memset(&opts, 0, sizeof opts);
  709. opts.update = 1;
  710. opts.merge = 1;
  711. opts.clone = 1;
  712. opts.fn = oneway_merge;
  713. opts.verbose_update = (option_verbosity >= 0);
  714. opts.src_index = &the_index;
  715. opts.dst_index = &the_index;
  716. init_checkout_metadata(&opts.meta, head, &oid, NULL);
  717. tree = parse_tree_indirect(&oid);
  718. parse_tree(tree);
  719. init_tree_desc(&t, tree->buffer, tree->size);
  720. if (unpack_trees(1, &t, &opts) < 0)
  721. die(_("unable to checkout working tree"));
  722. free(head);
  723. if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
  724. die(_("unable to write new index file"));
  725. err |= run_hook_le(NULL, "post-checkout", oid_to_hex(&null_oid),
  726. oid_to_hex(&oid), "1", NULL);
  727. if (!err && (option_recurse_submodules.nr > 0)) {
  728. struct strvec args = STRVEC_INIT;
  729. strvec_pushl(&args, "submodule", "update", "--require-init", "--recursive", NULL);
  730. if (option_shallow_submodules == 1)
  731. strvec_push(&args, "--depth=1");
  732. if (max_jobs != -1)
  733. strvec_pushf(&args, "--jobs=%d", max_jobs);
  734. if (submodule_progress)
  735. strvec_push(&args, "--progress");
  736. if (option_verbosity < 0)
  737. strvec_push(&args, "--quiet");
  738. if (option_remote_submodules) {
  739. strvec_push(&args, "--remote");
  740. strvec_push(&args, "--no-fetch");
  741. }
  742. if (option_single_branch >= 0)
  743. strvec_push(&args, option_single_branch ?
  744. "--single-branch" :
  745. "--no-single-branch");
  746. err = run_command_v_opt(args.v, RUN_GIT_CMD);
  747. strvec_clear(&args);
  748. }
  749. return err;
  750. }
  751. static int write_one_config(const char *key, const char *value, void *data)
  752. {
  753. return git_config_set_multivar_gently(key,
  754. value ? value : "true",
  755. CONFIG_REGEX_NONE, 0);
  756. }
  757. static void write_config(struct string_list *config)
  758. {
  759. int i;
  760. for (i = 0; i < config->nr; i++) {
  761. if (git_config_parse_parameter(config->items[i].string,
  762. write_one_config, NULL) < 0)
  763. die(_("unable to write parameters to config file"));
  764. }
  765. }
  766. static void write_refspec_config(const char *src_ref_prefix,
  767. const struct ref *our_head_points_at,
  768. const struct ref *remote_head_points_at,
  769. struct strbuf *branch_top)
  770. {
  771. struct strbuf key = STRBUF_INIT;
  772. struct strbuf value = STRBUF_INIT;
  773. if (option_mirror || !option_bare) {
  774. if (option_single_branch && !option_mirror) {
  775. if (option_branch) {
  776. if (starts_with(our_head_points_at->name, "refs/tags/"))
  777. strbuf_addf(&value, "+%s:%s", our_head_points_at->name,
  778. our_head_points_at->name);
  779. else
  780. strbuf_addf(&value, "+%s:%s%s", our_head_points_at->name,
  781. branch_top->buf, option_branch);
  782. } else if (remote_head_points_at) {
  783. const char *head = remote_head_points_at->name;
  784. if (!skip_prefix(head, "refs/heads/", &head))
  785. BUG("remote HEAD points at non-head?");
  786. strbuf_addf(&value, "+%s:%s%s", remote_head_points_at->name,
  787. branch_top->buf, head);
  788. }
  789. /*
  790. * otherwise, the next "git fetch" will
  791. * simply fetch from HEAD without updating
  792. * any remote-tracking branch, which is what
  793. * we want.
  794. */
  795. } else {
  796. strbuf_addf(&value, "+%s*:%s*", src_ref_prefix, branch_top->buf);
  797. }
  798. /* Configure the remote */
  799. if (value.len) {
  800. strbuf_addf(&key, "remote.%s.fetch", option_origin);
  801. git_config_set_multivar(key.buf, value.buf, "^$", 0);
  802. strbuf_reset(&key);
  803. if (option_mirror) {
  804. strbuf_addf(&key, "remote.%s.mirror", option_origin);
  805. git_config_set(key.buf, "true");
  806. strbuf_reset(&key);
  807. }
  808. }
  809. }
  810. strbuf_release(&key);
  811. strbuf_release(&value);
  812. }
  813. static void dissociate_from_references(void)
  814. {
  815. static const char* argv[] = { "repack", "-a", "-d", NULL };
  816. char *alternates = git_pathdup("objects/info/alternates");
  817. if (!access(alternates, F_OK)) {
  818. if (run_command_v_opt(argv, RUN_GIT_CMD|RUN_COMMAND_NO_STDIN))
  819. die(_("cannot repack to clean up"));
  820. if (unlink(alternates) && errno != ENOENT)
  821. die_errno(_("cannot unlink temporary alternates file"));
  822. }
  823. free(alternates);
  824. }
  825. static int path_exists(const char *path)
  826. {
  827. struct stat sb;
  828. return !stat(path, &sb);
  829. }
  830. int cmd_clone(int argc, const char **argv, const char *prefix)
  831. {
  832. int is_bundle = 0, is_local;
  833. const char *repo_name, *repo, *work_tree, *git_dir;
  834. char *path, *dir, *display_repo = NULL;
  835. int dest_exists, real_dest_exists = 0;
  836. const struct ref *refs, *remote_head;
  837. const struct ref *remote_head_points_at;
  838. const struct ref *our_head_points_at;
  839. struct ref *mapped_refs;
  840. const struct ref *ref;
  841. struct strbuf key = STRBUF_INIT;
  842. struct strbuf branch_top = STRBUF_INIT, reflog_msg = STRBUF_INIT;
  843. struct transport *transport = NULL;
  844. const char *src_ref_prefix = "refs/heads/";
  845. struct remote *remote;
  846. int err = 0, complete_refs_before_fetch = 1;
  847. int submodule_progress;
  848. struct strvec ref_prefixes = STRVEC_INIT;
  849. packet_trace_identity("clone");
  850. argc = parse_options(argc, argv, prefix, builtin_clone_options,
  851. builtin_clone_usage, 0);
  852. if (argc > 2)
  853. usage_msg_opt(_("Too many arguments."),
  854. builtin_clone_usage, builtin_clone_options);
  855. if (argc == 0)
  856. usage_msg_opt(_("You must specify a repository to clone."),
  857. builtin_clone_usage, builtin_clone_options);
  858. if (option_depth || option_since || option_not.nr)
  859. deepen = 1;
  860. if (option_single_branch == -1)
  861. option_single_branch = deepen ? 1 : 0;
  862. if (option_mirror)
  863. option_bare = 1;
  864. if (option_bare) {
  865. if (option_origin)
  866. die(_("--bare and --origin %s options are incompatible."),
  867. option_origin);
  868. if (real_git_dir)
  869. die(_("--bare and --separate-git-dir are incompatible."));
  870. option_no_checkout = 1;
  871. }
  872. if (!option_origin)
  873. option_origin = "origin";
  874. repo_name = argv[0];
  875. path = get_repo_path(repo_name, &is_bundle);
  876. if (path)
  877. repo = absolute_pathdup(repo_name);
  878. else if (strchr(repo_name, ':')) {
  879. repo = repo_name;
  880. display_repo = transport_anonymize_url(repo);
  881. } else
  882. die(_("repository '%s' does not exist"), repo_name);
  883. /* no need to be strict, transport_set_option() will validate it again */
  884. if (option_depth && atoi(option_depth) < 1)
  885. die(_("depth %s is not a positive number"), option_depth);
  886. if (argc == 2)
  887. dir = xstrdup(argv[1]);
  888. else
  889. dir = guess_dir_name(repo_name, is_bundle, option_bare);
  890. strip_trailing_slashes(dir);
  891. dest_exists = path_exists(dir);
  892. if (dest_exists && !is_empty_dir(dir))
  893. die(_("destination path '%s' already exists and is not "
  894. "an empty directory."), dir);
  895. if (real_git_dir) {
  896. real_dest_exists = path_exists(real_git_dir);
  897. if (real_dest_exists && !is_empty_dir(real_git_dir))
  898. die(_("repository path '%s' already exists and is not "
  899. "an empty directory."), real_git_dir);
  900. }
  901. strbuf_addf(&reflog_msg, "clone: from %s",
  902. display_repo ? display_repo : repo);
  903. free(display_repo);
  904. if (option_bare)
  905. work_tree = NULL;
  906. else {
  907. work_tree = getenv("GIT_WORK_TREE");
  908. if (work_tree && path_exists(work_tree))
  909. die(_("working tree '%s' already exists."), work_tree);
  910. }
  911. if (option_bare || work_tree)
  912. git_dir = xstrdup(dir);
  913. else {
  914. work_tree = dir;
  915. git_dir = mkpathdup("%s/.git", dir);
  916. }
  917. atexit(remove_junk);
  918. sigchain_push_common(remove_junk_on_signal);
  919. if (!option_bare) {
  920. if (safe_create_leading_directories_const(work_tree) < 0)
  921. die_errno(_("could not create leading directories of '%s'"),
  922. work_tree);
  923. if (dest_exists)
  924. junk_work_tree_flags |= REMOVE_DIR_KEEP_TOPLEVEL;
  925. else if (mkdir(work_tree, 0777))
  926. die_errno(_("could not create work tree dir '%s'"),
  927. work_tree);
  928. junk_work_tree = work_tree;
  929. set_git_work_tree(work_tree);
  930. }
  931. if (real_git_dir) {
  932. if (real_dest_exists)
  933. junk_git_dir_flags |= REMOVE_DIR_KEEP_TOPLEVEL;
  934. junk_git_dir = real_git_dir;
  935. } else {
  936. if (dest_exists)
  937. junk_git_dir_flags |= REMOVE_DIR_KEEP_TOPLEVEL;
  938. junk_git_dir = git_dir;
  939. }
  940. if (safe_create_leading_directories_const(git_dir) < 0)
  941. die(_("could not create leading directories of '%s'"), git_dir);
  942. if (0 <= option_verbosity) {
  943. if (option_bare)
  944. fprintf(stderr, _("Cloning into bare repository '%s'...\n"), dir);
  945. else
  946. fprintf(stderr, _("Cloning into '%s'...\n"), dir);
  947. }
  948. if (option_recurse_submodules.nr > 0) {
  949. struct string_list_item *item;
  950. struct strbuf sb = STRBUF_INIT;
  951. /* remove duplicates */
  952. string_list_sort(&option_recurse_submodules);
  953. string_list_remove_duplicates(&option_recurse_submodules, 0);
  954. /*
  955. * NEEDSWORK: In a multi-working-tree world, this needs to be
  956. * set in the per-worktree config.
  957. */
  958. for_each_string_list_item(item, &option_recurse_submodules) {
  959. strbuf_addf(&sb, "submodule.active=%s",
  960. item->string);
  961. string_list_append(&option_config,
  962. strbuf_detach(&sb, NULL));
  963. }
  964. if (option_required_reference.nr &&
  965. option_optional_reference.nr)
  966. die(_("clone --recursive is not compatible with "
  967. "both --reference and --reference-if-able"));
  968. else if (option_required_reference.nr) {
  969. string_list_append(&option_config,
  970. "submodule.alternateLocation=superproject");
  971. string_list_append(&option_config,
  972. "submodule.alternateErrorStrategy=die");
  973. } else if (option_optional_reference.nr) {
  974. string_list_append(&option_config,
  975. "submodule.alternateLocation=superproject");
  976. string_list_append(&option_config,
  977. "submodule.alternateErrorStrategy=info");
  978. }
  979. }
  980. init_db(git_dir, real_git_dir, option_template, GIT_HASH_UNKNOWN, NULL,
  981. INIT_DB_QUIET);
  982. if (real_git_dir)
  983. git_dir = real_git_dir;
  984. write_config(&option_config);
  985. git_config(git_default_config, NULL);
  986. if (option_bare) {
  987. if (option_mirror)
  988. src_ref_prefix = "refs/";
  989. strbuf_addstr(&branch_top, src_ref_prefix);
  990. git_config_set("core.bare", "true");
  991. } else {
  992. strbuf_addf(&branch_top, "refs/remotes/%s/", option_origin);
  993. }
  994. strbuf_addf(&key, "remote.%s.url", option_origin);
  995. git_config_set(key.buf, repo);
  996. strbuf_reset(&key);
  997. if (option_no_tags) {
  998. strbuf_addf(&key, "remote.%s.tagOpt", option_origin);
  999. git_config_set(key.buf, "--no-tags");
  1000. strbuf_reset(&key);
  1001. }
  1002. if (option_required_reference.nr || option_optional_reference.nr)
  1003. setup_reference();
  1004. if (option_sparse_checkout && git_sparse_checkout_init(dir))
  1005. return 1;
  1006. remote = remote_get(option_origin);
  1007. refspec_appendf(&remote->fetch, "+%s*:%s*", src_ref_prefix,
  1008. branch_top.buf);
  1009. transport = transport_get(remote, remote->url[0]);
  1010. transport_set_verbosity(transport, option_verbosity, option_progress);
  1011. transport->family = family;
  1012. path = get_repo_path(remote->url[0], &is_bundle);
  1013. is_local = option_local != 0 && path && !is_bundle;
  1014. if (is_local) {
  1015. if (option_depth)
  1016. warning(_("--depth is ignored in local clones; use file:// instead."));
  1017. if (option_since)
  1018. warning(_("--shallow-since is ignored in local clones; use file:// instead."));
  1019. if (option_not.nr)
  1020. warning(_("--shallow-exclude is ignored in local clones; use file:// instead."));
  1021. if (filter_options.choice)
  1022. warning(_("--filter is ignored in local clones; use file:// instead."));
  1023. if (!access(mkpath("%s/shallow", path), F_OK)) {
  1024. if (option_local > 0)
  1025. warning(_("source repository is shallow, ignoring --local"));
  1026. is_local = 0;
  1027. }
  1028. }
  1029. if (option_local > 0 && !is_local)
  1030. warning(_("--local is ignored"));
  1031. transport->cloning = 1;
  1032. transport_set_option(transport, TRANS_OPT_KEEP, "yes");
  1033. if (option_depth)
  1034. transport_set_option(transport, TRANS_OPT_DEPTH,
  1035. option_depth);
  1036. if (option_since)
  1037. transport_set_option(transport, TRANS_OPT_DEEPEN_SINCE,
  1038. option_since);
  1039. if (option_not.nr)
  1040. transport_set_option(transport, TRANS_OPT_DEEPEN_NOT,
  1041. (const char *)&option_not);
  1042. if (option_single_branch)
  1043. transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
  1044. if (option_upload_pack)
  1045. transport_set_option(transport, TRANS_OPT_UPLOADPACK,
  1046. option_upload_pack);
  1047. if (server_options.nr)
  1048. transport->server_options = &server_options;
  1049. if (filter_options.choice) {
  1050. const char *spec =
  1051. expand_list_objects_filter_spec(&filter_options);
  1052. transport_set_option(transport, TRANS_OPT_LIST_OBJECTS_FILTER,
  1053. spec);
  1054. transport_set_option(transport, TRANS_OPT_FROM_PROMISOR, "1");
  1055. }
  1056. if (transport->smart_options && !deepen && !filter_options.choice)
  1057. transport->smart_options->check_self_contained_and_connected = 1;
  1058. strvec_push(&ref_prefixes, "HEAD");
  1059. refspec_ref_prefixes(&remote->fetch, &ref_prefixes);
  1060. if (option_branch)
  1061. expand_ref_prefix(&ref_prefixes, option_branch);
  1062. if (!option_no_tags)
  1063. strvec_push(&ref_prefixes, "refs/tags/");
  1064. refs = transport_get_remote_refs(transport, &ref_prefixes);
  1065. if (refs) {
  1066. int hash_algo = hash_algo_by_ptr(transport_get_hash_algo(transport));
  1067. /*
  1068. * Now that we know what algorithm the remote side is using,
  1069. * let's set ours to the same thing.
  1070. */
  1071. initialize_repository_version(hash_algo, 1);
  1072. repo_set_hash_algo(the_repository, hash_algo);
  1073. mapped_refs = wanted_peer_refs(refs, &remote->fetch);
  1074. /*
  1075. * transport_get_remote_refs() may return refs with null sha-1
  1076. * in mapped_refs (see struct transport->get_refs_list
  1077. * comment). In that case we need fetch it early because
  1078. * remote_head code below relies on it.
  1079. *
  1080. * for normal clones, transport_get_remote_refs() should
  1081. * return reliable ref set, we can delay cloning until after
  1082. * remote HEAD check.
  1083. */
  1084. for (ref = refs; ref; ref = ref->next)
  1085. if (is_null_oid(&ref->old_oid)) {
  1086. complete_refs_before_fetch = 0;
  1087. break;
  1088. }
  1089. if (!is_local && !complete_refs_before_fetch)
  1090. transport_fetch_refs(transport, mapped_refs);
  1091. remote_head = find_ref_by_name(refs, "HEAD");
  1092. remote_head_points_at =
  1093. guess_remote_head(remote_head, mapped_refs, 0);
  1094. if (option_branch) {
  1095. our_head_points_at =
  1096. find_remote_branch(mapped_refs, option_branch);
  1097. if (!our_head_points_at)
  1098. die(_("Remote branch %s not found in upstream %s"),
  1099. option_branch, option_origin);
  1100. }
  1101. else
  1102. our_head_points_at = remote_head_points_at;
  1103. }
  1104. else {
  1105. if (option_branch)
  1106. die(_("Remote branch %s not found in upstream %s"),
  1107. option_branch, option_origin);
  1108. warning(_("You appear to have cloned an empty repository."));
  1109. mapped_refs = NULL;
  1110. our_head_points_at = NULL;
  1111. remote_head_points_at = NULL;
  1112. remote_head = NULL;
  1113. option_no_checkout = 1;
  1114. if (!option_bare) {
  1115. const char *branch = git_default_branch_name();
  1116. char *ref = xstrfmt("refs/heads/%s", branch);
  1117. install_branch_config(0, branch, option_origin, ref);
  1118. free(ref);
  1119. }
  1120. }
  1121. write_refspec_config(src_ref_prefix, our_head_points_at,
  1122. remote_head_points_at, &branch_top);
  1123. if (filter_options.choice)
  1124. partial_clone_register(option_origin, &filter_options);
  1125. if (is_local)
  1126. clone_local(path, git_dir);
  1127. else if (refs && complete_refs_before_fetch)
  1128. transport_fetch_refs(transport, mapped_refs);
  1129. update_remote_refs(refs, mapped_refs, remote_head_points_at,
  1130. branch_top.buf, reflog_msg.buf, transport,
  1131. !is_local);
  1132. update_head(our_head_points_at, remote_head, reflog_msg.buf);
  1133. /*
  1134. * We want to show progress for recursive submodule clones iff
  1135. * we did so for the main clone. But only the transport knows
  1136. * the final decision for this flag, so we need to rescue the value
  1137. * before we free the transport.
  1138. */
  1139. submodule_progress = transport->progress;
  1140. transport_unlock_pack(transport);
  1141. transport_disconnect(transport);
  1142. if (option_dissociate) {
  1143. close_object_store(the_repository->objects);
  1144. dissociate_from_references();
  1145. }
  1146. junk_mode = JUNK_LEAVE_REPO;
  1147. err = checkout(submodule_progress);
  1148. strbuf_release(&reflog_msg);
  1149. strbuf_release(&branch_top);
  1150. strbuf_release(&key);
  1151. junk_mode = JUNK_LEAVE_ALL;
  1152. strvec_clear(&ref_prefixes);
  1153. return err;
  1154. }