blob: 78fb80eab6b323c6a4395ce3b65f5a6d0ab6d2a3 [file] [log] [blame]
Daniel Barkalow8434c2f2008-04-27 13:39:30 -04001/*
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
Nguyễn Thái Ngọc Duyf8adbec2019-01-24 15:29:12 +070011#define USE_THE_INDEX_COMPATIBILITY_MACROS
Stephen Boydc2e86ad2011-03-22 00:51:05 -070012#include "builtin.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -070013#include "config.h"
Michael Haggerty697cc8e2014-10-01 12:28:42 +020014#include "lockfile.h"
Daniel Barkalow8434c2f2008-04-27 13:39:30 -040015#include "parse-options.h"
16#include "fetch-pack.h"
17#include "refs.h"
Brandon Williamsec0cb492018-05-16 15:57:48 -070018#include "refspec.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -070019#include "object-store.h"
Daniel Barkalow8434c2f2008-04-27 13:39:30 -040020#include "tree.h"
21#include "tree-walk.h"
22#include "unpack-trees.h"
23#include "transport.h"
24#include "strbuf.h"
25#include "dir.h"
Matheus Tavaresff7ccc82019-07-10 20:59:03 -030026#include "dir-iterator.h"
27#include "iterator.h"
Jeff King4a16d072009-01-22 01:02:35 -050028#include "sigchain.h"
Junio C Hamanoa9f2c132009-03-03 22:29:55 -080029#include "branch.h"
Jay Soffian8ef51732009-02-25 03:32:13 -050030#include "remote.h"
Jeff Kingdfa7a6c2009-03-03 00:37:51 -050031#include "run-command.h"
Jeff King0433ad12013-03-25 16:26:27 -040032#include "connected.h"
Jonathan Tan3836d882017-08-18 15:20:21 -070033#include "packfile.h"
Jonathan Tan548719f2017-12-08 15:58:46 +000034#include "list-objects-filter-options.h"
Emily Shaffer72ddf342021-12-22 04:59:35 +010035#include "hook.h"
Derrick Stolee86fdd942022-03-09 16:01:43 +000036#include "bundle.h"
Daniel Barkalow8434c2f2008-04-27 13:39:30 -040037
38/*
39 * Overall FIXMEs:
40 * - respect DB_ENVIRONMENT for .git/objects.
41 *
42 * Implementation notes:
43 * - dropping use-separate-remote and no-separate-remote compatibility
44 *
45 */
46static const char * const builtin_clone_usage[] = {
Alex Henrie9c9b4f22015-01-13 00:44:47 -070047 N_("git clone [<options>] [--] <repo> [<dir>]"),
Daniel Barkalow8434c2f2008-04-27 13:39:30 -040048 NULL
49};
50
Nguyễn Thái Ngọc Duy3e6e0ed2012-01-07 21:45:59 +070051static int option_no_checkout, option_bare, option_mirror, option_single_branch = -1;
Brandon Williamsbb62e0a2017-03-17 15:38:03 -070052static int option_local = -1, option_no_hardlinks, option_shared;
Ævar Arnfjörð Bjarmason0dab2462017-04-26 23:12:33 +000053static int option_no_tags;
Junio C Hamano18a74a02016-06-19 13:51:56 -070054static int option_shallow_submodules;
Li Linchao4fe788b2021-04-01 10:46:59 +000055static int option_reject_shallow = -1; /* unspecified */
56static int config_reject_shallow = -1; /* unspecified */
Nguyễn Thái Ngọc Duy994c2aa2016-06-12 17:54:00 +070057static int deepen;
58static char *option_template, *option_depth, *option_since;
Daniel Barkalow8434c2f2008-04-27 13:39:30 -040059static char *option_origin = NULL;
Sean Baragde9ed3e2020-10-01 03:46:16 +000060static char *remote_name = NULL;
Jeff King7a4ee282009-08-26 15:05:08 -040061static char *option_branch = NULL;
Nguyễn Thái Ngọc Duy859e5df2016-06-12 17:54:05 +070062static struct string_list option_not = STRING_LIST_INIT_NODUP;
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +070063static const char *real_git_dir;
Daniel Barkalow8434c2f2008-04-27 13:39:30 -040064static char *option_upload_pack = "git-upload-pack";
Tay Ray Chuan5bd631b2010-02-24 20:50:25 +080065static int option_verbosity;
Clemens Buchacher01fdc212012-02-13 21:17:15 +010066static int option_progress = -1;
Derrick Stoleed89f09c2019-11-21 22:04:35 +000067static int option_sparse_checkout;
Eric Wongc915f112016-02-03 04:09:14 +000068static enum transport_family family;
Jeff King2721ce22016-06-13 06:04:20 -040069static struct string_list option_config = STRING_LIST_INIT_NODUP;
Stefan Beller5e408002016-08-15 14:53:25 -070070static struct string_list option_required_reference = STRING_LIST_INIT_NODUP;
Stefan Bellerf7415b42016-08-15 14:53:26 -070071static struct string_list option_optional_reference = STRING_LIST_INIT_NODUP;
Junio C Hamanofb1d6da2014-10-14 12:38:52 -070072static int option_dissociate;
Stefan Beller72290d62016-02-29 18:07:20 -080073static int max_jobs = -1;
Brandon Williamsbb62e0a2017-03-17 15:38:03 -070074static struct string_list option_recurse_submodules = STRING_LIST_INIT_NODUP;
Jeff King2a01bde2022-09-11 01:03:07 -040075static struct list_objects_filter_options filter_options = LIST_OBJECTS_FILTER_INIT;
Josh Steadmonf05da2b2022-02-04 21:00:49 -080076static int option_filter_submodules = -1; /* unspecified */
77static int config_filter_submodules = -1; /* unspecified */
Jonathan Tan6e983052019-04-12 12:51:22 -070078static struct string_list server_options = STRING_LIST_INIT_NODUP;
Ben Avison4c691012019-05-19 15:26:49 +010079static int option_remote_submodules;
Brandon Williamsbb62e0a2017-03-17 15:38:03 -070080
81static int recurse_submodules_cb(const struct option *opt,
82 const char *arg, int unset)
83{
84 if (unset)
85 string_list_clear((struct string_list *)opt->value, 0);
86 else if (arg)
87 string_list_append((struct string_list *)opt->value, arg);
88 else
89 string_list_append((struct string_list *)opt->value,
90 (const char *)opt->defval);
91
92 return 0;
93}
Junio C Hamanodbc92b02011-08-22 18:05:15 -070094
Daniel Barkalow8434c2f2008-04-27 13:39:30 -040095static struct option builtin_clone_options[] = {
Tay Ray Chuan5bd631b2010-02-24 20:50:25 +080096 OPT__VERBOSITY(&option_verbosity),
Clemens Buchacher01fdc212012-02-13 21:17:15 +010097 OPT_BOOL(0, "progress", &option_progress,
Nguyễn Thái Ngọc Duy32b77ad2012-08-20 19:32:02 +070098 N_("force progress reporting")),
Li Linchao4fe788b2021-04-01 10:46:59 +000099 OPT_BOOL(0, "reject-shallow", &option_reject_shallow,
100 N_("don't clone shallow repository")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200101 OPT_BOOL('n', "no-checkout", &option_no_checkout,
102 N_("don't create a checkout")),
Stefan Beller4741edd2013-08-03 13:51:18 +0200103 OPT_BOOL(0, "bare", &option_bare, N_("create a bare repository")),
104 OPT_HIDDEN_BOOL(0, "naked", &option_bare,
105 N_("create a bare repository")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200106 OPT_BOOL(0, "mirror", &option_mirror,
107 N_("create a mirror repository (implies bare)")),
Jeff King189260b2012-05-30 07:10:16 -0400108 OPT_BOOL('l', "local", &option_local,
Nguyễn Thái Ngọc Duy32b77ad2012-08-20 19:32:02 +0700109 N_("to clone from a local repository")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200110 OPT_BOOL(0, "no-hardlinks", &option_no_hardlinks,
Nguyễn Thái Ngọc Duy32b77ad2012-08-20 19:32:02 +0700111 N_("don't use local hardlinks, always copy")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200112 OPT_BOOL('s', "shared", &option_shared,
Nguyễn Thái Ngọc Duy32b77ad2012-08-20 19:32:02 +0700113 N_("setup as shared repository")),
Brandon Williamsbb62e0a2017-03-17 15:38:03 -0700114 { OPTION_CALLBACK, 0, "recurse-submodules", &option_recurse_submodules,
115 N_("pathspec"), N_("initialize submodules in the clone"),
116 PARSE_OPT_OPTARG, recurse_submodules_cb, (intptr_t)"." },
Junio C Hamanoc28b0362020-03-16 13:27:43 -0700117 OPT_ALIAS(0, "recursive", "recurse-submodules"),
Stefan Beller72290d62016-02-29 18:07:20 -0800118 OPT_INTEGER('j', "jobs", &max_jobs,
119 N_("number of submodules cloned in parallel")),
Nguyễn Thái Ngọc Duy32b77ad2012-08-20 19:32:02 +0700120 OPT_STRING(0, "template", &option_template, N_("template-directory"),
121 N_("directory from which templates will be used")),
Stefan Beller5e408002016-08-15 14:53:25 -0700122 OPT_STRING_LIST(0, "reference", &option_required_reference, N_("repo"),
Jeff King8ade0092015-05-21 00:15:19 -0400123 N_("reference repository")),
Stefan Bellerf7415b42016-08-15 14:53:26 -0700124 OPT_STRING_LIST(0, "reference-if-able", &option_optional_reference,
125 N_("repo"), N_("reference repository")),
Jeff King14f8b9b2015-05-21 00:16:04 -0400126 OPT_BOOL(0, "dissociate", &option_dissociate,
127 N_("use --reference only while cloning")),
Nguyễn Thái Ngọc Duy32b77ad2012-08-20 19:32:02 +0700128 OPT_STRING('o', "origin", &option_origin, N_("name"),
129 N_("use <name> instead of 'origin' to track upstream")),
130 OPT_STRING('b', "branch", &option_branch, N_("branch"),
131 N_("checkout <branch> instead of the remote's HEAD")),
132 OPT_STRING('u', "upload-pack", &option_upload_pack, N_("path"),
133 N_("path to git-upload-pack on the remote")),
134 OPT_STRING(0, "depth", &option_depth, N_("depth"),
135 N_("create a shallow clone of that depth")),
Nguyễn Thái Ngọc Duy994c2aa2016-06-12 17:54:00 +0700136 OPT_STRING(0, "shallow-since", &option_since, N_("time"),
137 N_("create a shallow clone since a specific time")),
Nguyễn Thái Ngọc Duy859e5df2016-06-12 17:54:05 +0700138 OPT_STRING_LIST(0, "shallow-exclude", &option_not, N_("revision"),
Alex Henrie6d873862016-12-04 15:03:59 -0700139 N_("deepen history of shallow clone, excluding rev")),
Nguyễn Thái Ngọc Duy3e6e0ed2012-01-07 21:45:59 +0700140 OPT_BOOL(0, "single-branch", &option_single_branch,
Nguyễn Thái Ngọc Duy32b77ad2012-08-20 19:32:02 +0700141 N_("clone only one branch, HEAD or --branch")),
Ævar Arnfjörð Bjarmason0dab2462017-04-26 23:12:33 +0000142 OPT_BOOL(0, "no-tags", &option_no_tags,
143 N_("don't clone any tags, and make later fetches not to follow them")),
Stefan Bellerd22eb042016-04-25 18:12:27 -0700144 OPT_BOOL(0, "shallow-submodules", &option_shallow_submodules,
145 N_("any cloned submodules will be shallow")),
Nguyễn Thái Ngọc Duy32b77ad2012-08-20 19:32:02 +0700146 OPT_STRING(0, "separate-git-dir", &real_git_dir, N_("gitdir"),
147 N_("separate git dir from working tree")),
148 OPT_STRING_LIST('c', "config", &option_config, N_("key=value"),
149 N_("set config inside the new repository")),
Jonathan Tan6e983052019-04-12 12:51:22 -0700150 OPT_STRING_LIST(0, "server-option", &server_options,
151 N_("server-specific"), N_("option to transmit")),
Eric Wongc915f112016-02-03 04:09:14 +0000152 OPT_SET_INT('4', "ipv4", &family, N_("use IPv4 addresses only"),
153 TRANSPORT_FAMILY_IPV4),
154 OPT_SET_INT('6', "ipv6", &family, N_("use IPv6 addresses only"),
155 TRANSPORT_FAMILY_IPV6),
Jonathan Tan548719f2017-12-08 15:58:46 +0000156 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
Josh Steadmonf05da2b2022-02-04 21:00:49 -0800157 OPT_BOOL(0, "also-filter-submodules", &option_filter_submodules,
158 N_("apply partial clone filters to submodules")),
Ben Avison4c691012019-05-19 15:26:49 +0100159 OPT_BOOL(0, "remote-submodules", &option_remote_submodules,
160 N_("any cloned submodules will use their remote-tracking branch")),
Derrick Stoleed89f09c2019-11-21 22:04:35 +0000161 OPT_BOOL(0, "sparse", &option_sparse_checkout,
162 N_("initialize sparse-checkout file to include only files at root")),
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400163 OPT_END()
164};
165
Jeff King0ea68e42015-08-10 05:37:55 -0400166static const char *get_repo_path_1(struct strbuf *path, int *is_bundle)
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400167{
Jeff Kingb3256eb2012-02-02 16:59:13 -0500168 static char *suffix[] = { "/.git", "", ".git/.git", ".git" };
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400169 static char *bundle_suffix[] = { ".bundle", "" };
Jeff King0ea68e42015-08-10 05:37:55 -0400170 size_t baselen = path->len;
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400171 struct stat st;
172 int i;
173
174 for (i = 0; i < ARRAY_SIZE(suffix); i++) {
Jeff King0ea68e42015-08-10 05:37:55 -0400175 strbuf_setlen(path, baselen);
176 strbuf_addstr(path, suffix[i]);
177 if (stat(path->buf, &st))
Nguyễn Thái Ngọc Duy9b0ebc72011-08-21 18:58:09 +0700178 continue;
Jeff King0ea68e42015-08-10 05:37:55 -0400179 if (S_ISDIR(st.st_mode) && is_git_directory(path->buf)) {
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400180 *is_bundle = 0;
Jeff King0ea68e42015-08-10 05:37:55 -0400181 return path->buf;
Nguyễn Thái Ngọc Duy9b0ebc72011-08-21 18:58:09 +0700182 } else if (S_ISREG(st.st_mode) && st.st_size > 8) {
183 /* Is it a "gitfile"? */
184 char signature[8];
Jeff King0ea68e42015-08-10 05:37:55 -0400185 const char *dst;
186 int len, fd = open(path->buf, O_RDONLY);
Nguyễn Thái Ngọc Duy9b0ebc72011-08-21 18:58:09 +0700187 if (fd < 0)
188 continue;
189 len = read_in_full(fd, signature, 8);
190 close(fd);
191 if (len != 8 || strncmp(signature, "gitdir: ", 8))
192 continue;
Jeff King0ea68e42015-08-10 05:37:55 -0400193 dst = read_gitfile(path->buf);
194 if (dst) {
Nguyễn Thái Ngọc Duy9b0ebc72011-08-21 18:58:09 +0700195 *is_bundle = 0;
Jeff King0ea68e42015-08-10 05:37:55 -0400196 return dst;
Nguyễn Thái Ngọc Duy9b0ebc72011-08-21 18:58:09 +0700197 }
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400198 }
199 }
200
201 for (i = 0; i < ARRAY_SIZE(bundle_suffix); i++) {
Jeff King0ea68e42015-08-10 05:37:55 -0400202 strbuf_setlen(path, baselen);
203 strbuf_addstr(path, bundle_suffix[i]);
204 if (!stat(path->buf, &st) && S_ISREG(st.st_mode)) {
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400205 *is_bundle = 1;
Jeff King0ea68e42015-08-10 05:37:55 -0400206 return path->buf;
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400207 }
208 }
209
210 return NULL;
211}
212
Jeff King0ea68e42015-08-10 05:37:55 -0400213static char *get_repo_path(const char *repo, int *is_bundle)
214{
215 struct strbuf path = STRBUF_INIT;
216 const char *raw;
217 char *canon;
218
219 strbuf_addstr(&path, repo);
220 raw = get_repo_path_1(&path, is_bundle);
René Scharfe0aaad412017-01-26 18:54:23 +0100221 canon = raw ? absolute_pathdup(raw) : NULL;
Jeff King0ea68e42015-08-10 05:37:55 -0400222 strbuf_release(&path);
223 return canon;
224}
225
Junio C Hamanodbc92b02011-08-22 18:05:15 -0700226static int add_one_reference(struct string_list_item *item, void *cb_data)
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400227{
Stefan Beller9eeea7d2016-08-15 14:53:24 -0700228 struct strbuf err = STRBUF_INIT;
Stefan Bellerf7415b42016-08-15 14:53:26 -0700229 int *required = cb_data;
Stefan Beller9eeea7d2016-08-15 14:53:24 -0700230 char *ref_git = compute_alternate_path(item->string, &err);
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400231
Stefan Bellerf7415b42016-08-15 14:53:26 -0700232 if (!ref_git) {
233 if (*required)
234 die("%s", err.buf);
235 else
236 fprintf(stderr,
237 _("info: Could not add alternate for '%s': %s\n"),
238 item->string, err.buf);
239 } else {
240 struct strbuf sb = STRBUF_INIT;
241 strbuf_addf(&sb, "%s/objects", ref_git);
242 add_to_alternates_file(sb.buf);
243 strbuf_release(&sb);
244 }
Aaron Schrabb552b562013-04-09 18:22:00 -0400245
Stefan Beller9eeea7d2016-08-15 14:53:24 -0700246 strbuf_release(&err);
Stefan Bellerf7415b42016-08-15 14:53:26 -0700247 free(ref_git);
Junio C Hamanodbc92b02011-08-22 18:05:15 -0700248 return 0;
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400249}
250
Junio C Hamanodbc92b02011-08-22 18:05:15 -0700251static void setup_reference(void)
252{
Stefan Bellerf7415b42016-08-15 14:53:26 -0700253 int required = 1;
254 for_each_string_list(&option_required_reference,
255 add_one_reference, &required);
256 required = 0;
257 for_each_string_list(&option_optional_reference,
258 add_one_reference, &required);
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400259}
260
Jeff King3c1dce82019-05-09 17:29:22 -0400261static void copy_alternates(struct strbuf *src, const char *src_repo)
Junio C Hamanoe6baf4a2011-08-22 18:05:16 -0700262{
263 /*
264 * Read from the source objects/info/alternates file
265 * and copy the entries to corresponding file in the
266 * destination repository with add_to_alternates_file().
267 * Both src and dst have "$path/objects/info/alternates".
268 *
269 * Instead of copying bit-for-bit from the original,
270 * we need to append to existing one so that the already
271 * created entry via "clone -s" is not lost, and also
272 * to turn entries with paths relative to the original
273 * absolute, so that they can be used in the new repository.
274 */
Nguyễn Thái Ngọc Duy02912f42017-05-03 17:16:47 +0700275 FILE *in = xfopen(src->buf, "r");
Junio C Hamanoe6baf4a2011-08-22 18:05:16 -0700276 struct strbuf line = STRBUF_INIT;
277
Junio C Hamano3f163962015-10-28 13:29:24 -0700278 while (strbuf_getline(&line, in) != EOF) {
Nguyễn Thái Ngọc Duydcf69262014-11-30 15:24:27 +0700279 char *abs_path;
Junio C Hamanoe6baf4a2011-08-22 18:05:16 -0700280 if (!line.len || line.buf[0] == '#')
281 continue;
282 if (is_absolute_path(line.buf)) {
283 add_to_alternates_file(line.buf);
284 continue;
285 }
Nguyễn Thái Ngọc Duydcf69262014-11-30 15:24:27 +0700286 abs_path = mkpathdup("%s/objects/%s", src_repo, line.buf);
Jeff King22d3b8d2016-10-05 10:29:29 -0400287 if (!normalize_path_copy(abs_path, abs_path))
288 add_to_alternates_file(abs_path);
289 else
290 warning("skipping invalid relative alternate: %s/%s",
291 src_repo, line.buf);
Nguyễn Thái Ngọc Duydcf69262014-11-30 15:24:27 +0700292 free(abs_path);
Junio C Hamanoe6baf4a2011-08-22 18:05:16 -0700293 }
294 strbuf_release(&line);
295 fclose(in);
296}
297
Matheus Tavares14954b72019-07-10 20:59:02 -0300298static void mkdir_if_missing(const char *pathname, mode_t mode)
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400299{
Matheus Tavares14954b72019-07-10 20:59:02 -0300300 struct stat st;
301
302 if (!mkdir(pathname, mode))
303 return;
304
305 if (errno != EEXIST)
306 die_errno(_("failed to create directory '%s'"), pathname);
307 else if (stat(pathname, &st))
308 die_errno(_("failed to stat '%s'"), pathname);
309 else if (!S_ISDIR(st.st_mode))
310 die(_("%s exists and is not a directory"), pathname);
311}
312
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400313static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest,
Matheus Tavaresff7ccc82019-07-10 20:59:03 -0300314 const char *src_repo)
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400315{
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400316 int src_len, dest_len;
Matheus Tavaresff7ccc82019-07-10 20:59:03 -0300317 struct dir_iterator *iter;
318 int iter_status;
319 unsigned int flags;
Alexandr Miloslavskiy3d7747e2020-03-10 13:11:22 +0000320 struct strbuf realpath = STRBUF_INIT;
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400321
Matheus Tavares14954b72019-07-10 20:59:02 -0300322 mkdir_if_missing(dest->buf, 0777);
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400323
Matheus Tavaresff7ccc82019-07-10 20:59:03 -0300324 flags = DIR_ITERATOR_PEDANTIC | DIR_ITERATOR_FOLLOW_SYMLINKS;
325 iter = dir_iterator_begin(src->buf, flags);
326
327 if (!iter)
328 die_errno(_("failed to start iterator over '%s'"), src->buf);
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400329
Miklos Vajnab9e125e2008-11-21 01:45:00 +0100330 strbuf_addch(src, '/');
331 src_len = src->len;
332 strbuf_addch(dest, '/');
333 dest_len = dest->len;
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400334
Matheus Tavaresff7ccc82019-07-10 20:59:03 -0300335 while ((iter_status = dir_iterator_advance(iter)) == ITER_OK) {
Miklos Vajnab9e125e2008-11-21 01:45:00 +0100336 strbuf_setlen(src, src_len);
Matheus Tavaresff7ccc82019-07-10 20:59:03 -0300337 strbuf_addstr(src, iter->relative_path);
Miklos Vajnab9e125e2008-11-21 01:45:00 +0100338 strbuf_setlen(dest, dest_len);
Matheus Tavaresff7ccc82019-07-10 20:59:03 -0300339 strbuf_addstr(dest, iter->relative_path);
340
341 if (S_ISDIR(iter->st.st_mode)) {
342 mkdir_if_missing(dest->buf, 0777);
Junio C Hamanoe6baf4a2011-08-22 18:05:16 -0700343 continue;
344 }
345
346 /* Files that cannot be copied bit-for-bit... */
Matheus Tavaresc4d9c502019-07-10 20:59:04 -0300347 if (!fspathcmp(iter->relative_path, "info/alternates")) {
Jeff King3c1dce82019-05-09 17:29:22 -0400348 copy_alternates(src, src_repo);
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400349 continue;
350 }
351
Miklos Vajnab9e125e2008-11-21 01:45:00 +0100352 if (unlink(dest->buf) && errno != ENOENT)
Ævar Arnfjörð Bjarmasone84d7b72011-02-22 23:41:26 +0000353 die_errno(_("failed to unlink '%s'"), dest->buf);
Daniel Barkalowfdabc242008-05-20 14:15:14 -0400354 if (!option_no_hardlinks) {
Alexandr Miloslavskiy3d7747e2020-03-10 13:11:22 +0000355 strbuf_realpath(&realpath, src->buf, 1);
356 if (!link(realpath.buf, dest->buf))
Daniel Barkalowfdabc242008-05-20 14:15:14 -0400357 continue;
Jeff King189260b2012-05-30 07:10:16 -0400358 if (option_local > 0)
Ævar Arnfjörð Bjarmasone84d7b72011-02-22 23:41:26 +0000359 die_errno(_("failed to create link '%s'"), dest->buf);
Daniel Barkalowfdabc242008-05-20 14:15:14 -0400360 option_no_hardlinks = 1;
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400361 }
Clemens Buchacherf7835a22009-09-12 11:03:48 +0200362 if (copy_file_with_time(dest->buf, src->buf, 0666))
Ævar Arnfjörð Bjarmasone84d7b72011-02-22 23:41:26 +0000363 die_errno(_("failed to copy file to '%s'"), dest->buf);
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400364 }
Matheus Tavaresff7ccc82019-07-10 20:59:03 -0300365
366 if (iter_status != ITER_DONE) {
367 strbuf_setlen(src, src_len);
368 die(_("failed to iterate over '%s'"), src->buf);
369 }
Alexandr Miloslavskiy3d7747e2020-03-10 13:11:22 +0000370
371 strbuf_release(&realpath);
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400372}
373
Nguyễn Thái Ngọc Duy6f48d392012-01-16 16:46:12 +0700374static void clone_local(const char *src_repo, const char *dest_repo)
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400375{
Junio C Hamanoe6baf4a2011-08-22 18:05:16 -0700376 if (option_shared) {
377 struct strbuf alt = STRBUF_INIT;
Eric Sunshineb3b05972017-12-11 18:16:12 -0500378 get_common_dir(&alt, src_repo);
379 strbuf_addstr(&alt, "/objects");
Junio C Hamanoe6baf4a2011-08-22 18:05:16 -0700380 add_to_alternates_file(alt.buf);
381 strbuf_release(&alt);
382 } else {
383 struct strbuf src = STRBUF_INIT;
384 struct strbuf dest = STRBUF_INIT;
Nguyễn Thái Ngọc Duy744e4692015-09-28 20:06:15 +0700385 get_common_dir(&src, src_repo);
386 get_common_dir(&dest, dest_repo);
387 strbuf_addstr(&src, "/objects");
388 strbuf_addstr(&dest, "/objects");
Matheus Tavaresff7ccc82019-07-10 20:59:03 -0300389 copy_or_link_directory(&src, &dest, src_repo);
Miklos Vajnab9e125e2008-11-21 01:45:00 +0100390 strbuf_release(&src);
391 strbuf_release(&dest);
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400392 }
393
Junio C Hamano28ba96a2010-04-23 14:37:22 +0200394 if (0 <= option_verbosity)
Jeff King68b939b2013-09-18 16:05:13 -0400395 fprintf(stderr, _("done.\n"));
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400396}
397
398static const char *junk_work_tree;
Jeff Kingd45420c2018-01-02 16:11:39 -0500399static int junk_work_tree_flags;
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400400static const char *junk_git_dir;
Jeff Kingd45420c2018-01-02 16:11:39 -0500401static int junk_git_dir_flags;
Ramsay Jones85064632013-04-27 19:39:04 +0100402static enum {
Jeff Kingd3b34622013-03-26 18:22:09 -0400403 JUNK_LEAVE_NONE,
404 JUNK_LEAVE_REPO,
405 JUNK_LEAVE_ALL
406} junk_mode = JUNK_LEAVE_NONE;
407
408static const char junk_leave_repo_msg[] =
409N_("Clone succeeded, but checkout failed.\n"
410 "You can inspect what was checked out with 'git status'\n"
Nguyễn Thái Ngọc Duy80f537f2019-04-25 16:45:58 +0700411 "and retry with 'git restore --source=HEAD :/'\n");
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400412
413static void remove_junk(void)
414{
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500415 struct strbuf sb = STRBUF_INIT;
Jeff Kingd3b34622013-03-26 18:22:09 -0400416
417 switch (junk_mode) {
418 case JUNK_LEAVE_REPO:
419 warning("%s", _(junk_leave_repo_msg));
420 /* fall-through */
421 case JUNK_LEAVE_ALL:
422 return;
423 default:
424 /* proceed to removal */
425 break;
426 }
427
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400428 if (junk_git_dir) {
429 strbuf_addstr(&sb, junk_git_dir);
Jeff Kingd45420c2018-01-02 16:11:39 -0500430 remove_dir_recursively(&sb, junk_git_dir_flags);
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400431 strbuf_reset(&sb);
432 }
433 if (junk_work_tree) {
434 strbuf_addstr(&sb, junk_work_tree);
Jeff Kingd45420c2018-01-02 16:11:39 -0500435 remove_dir_recursively(&sb, junk_work_tree_flags);
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400436 }
Rene Scharfe9c18b542017-08-30 19:49:37 +0200437 strbuf_release(&sb);
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400438}
439
440static void remove_junk_on_signal(int signo)
441{
442 remove_junk();
Jeff King4a16d072009-01-22 01:02:35 -0500443 sigchain_pop(signo);
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400444 raise(signo);
445}
446
Nguyễn Thái Ngọc Duy9e585042012-01-16 16:46:13 +0700447static struct ref *find_remote_branch(const struct ref *refs, const char *branch)
448{
449 struct ref *ref;
450 struct strbuf head = STRBUF_INIT;
451 strbuf_addstr(&head, "refs/heads/");
452 strbuf_addstr(&head, branch);
453 ref = find_ref_by_name(refs, head.buf);
454 strbuf_release(&head);
Nguyễn Thái Ngọc Duy5a7d5b62012-01-16 16:46:15 +0700455
456 if (ref)
457 return ref;
458
459 strbuf_addstr(&head, "refs/tags/");
460 strbuf_addstr(&head, branch);
461 ref = find_ref_by_name(refs, head.buf);
462 strbuf_release(&head);
463
Nguyễn Thái Ngọc Duy9e585042012-01-16 16:46:13 +0700464 return ref;
465}
466
Nicolas Pitre5bdc32d2009-09-25 23:54:42 -0400467static struct ref *wanted_peer_refs(const struct ref *refs,
SZEDER Gábor515be832018-11-14 11:46:19 +0100468 struct refspec *refspec)
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400469{
Jeff Kingc1921c12011-06-07 19:03:22 -0400470 struct ref *head = copy_ref(find_ref_by_name(refs, "HEAD"));
471 struct ref *local_refs = head;
472 struct ref **tail = head ? &head->next : &local_refs;
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400473
Nguyễn Thái Ngọc Duy3e6e0ed2012-01-07 21:45:59 +0700474 if (option_single_branch) {
475 struct ref *remote_head = NULL;
476
477 if (!option_branch)
478 remote_head = guess_remote_head(head, refs, 0);
Nguyễn Thái Ngọc Duy0ec4b162012-06-22 16:35:47 +0700479 else {
480 local_refs = NULL;
481 tail = &local_refs;
482 remote_head = copy_ref(find_remote_branch(refs, option_branch));
483 }
Nguyễn Thái Ngọc Duy3e6e0ed2012-01-07 21:45:59 +0700484
485 if (!remote_head && option_branch)
486 warning(_("Could not find remote branch %s to clone."),
487 option_branch);
Nguyễn Thái Ngọc Duy5a7d5b62012-01-16 16:46:15 +0700488 else {
SZEDER Gábor515be832018-11-14 11:46:19 +0100489 int i;
490 for (i = 0; i < refspec->nr; i++)
491 get_fetch_map(remote_head, &refspec->items[i],
492 &tail, 0);
Nguyễn Thái Ngọc Duy5a7d5b62012-01-16 16:46:15 +0700493
494 /* if --branch=tag, pull the requested tag explicitly */
495 get_fetch_map(remote_head, tag_refspec, &tail, 0);
496 }
SZEDER Gábor515be832018-11-14 11:46:19 +0100497 } else {
498 int i;
499 for (i = 0; i < refspec->nr; i++)
500 get_fetch_map(refs, &refspec->items[i], &tail, 0);
501 }
Nguyễn Thái Ngọc Duy3e6e0ed2012-01-07 21:45:59 +0700502
Ævar Arnfjörð Bjarmason0dab2462017-04-26 23:12:33 +0000503 if (!option_mirror && !option_single_branch && !option_no_tags)
Johannes Schindelin468386a2008-08-08 04:29:35 +0200504 get_fetch_map(refs, tag_refspec, &tail, 0);
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400505
Nicolas Pitre5bdc32d2009-09-25 23:54:42 -0400506 return local_refs;
507}
508
509static void write_remote_refs(const struct ref *local_refs)
510{
511 const struct ref *r;
512
Michael Haggerty58f233c2015-06-22 16:03:01 +0200513 struct ref_transaction *t;
514 struct strbuf err = STRBUF_INIT;
515
516 t = ref_transaction_begin(&err);
517 if (!t)
518 die("%s", err.buf);
Michael Haggerty9f69d292013-06-20 10:37:46 +0200519
Jeff Kingc1921c12011-06-07 19:03:22 -0400520 for (r = local_refs; r; r = r->next) {
521 if (!r->peer_ref)
522 continue;
brian m. carlson89f3bbd2017-10-15 22:06:53 +0000523 if (ref_transaction_create(t, r->peer_ref->name, &r->old_oid,
Michael Haggerty58f233c2015-06-22 16:03:01 +0200524 0, NULL, &err))
525 die("%s", err.buf);
Jeff Kingc1921c12011-06-07 19:03:22 -0400526 }
Johan Herland3e8aded2008-06-15 16:06:16 +0200527
Michael Haggerty58f233c2015-06-22 16:03:01 +0200528 if (initial_ref_transaction_commit(t, &err))
529 die("%s", err.buf);
530
531 strbuf_release(&err);
532 ref_transaction_free(t);
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400533}
534
Nguyễn Thái Ngọc Duy3e6e0ed2012-01-07 21:45:59 +0700535static void write_followtags(const struct ref *refs, const char *msg)
536{
537 const struct ref *ref;
538 for (ref = refs; ref; ref = ref->next) {
Christian Couder59556542013-11-30 21:55:40 +0100539 if (!starts_with(ref->name, "refs/tags/"))
Nguyễn Thái Ngọc Duy3e6e0ed2012-01-07 21:45:59 +0700540 continue;
Christian Couder59556542013-11-30 21:55:40 +0100541 if (ends_with(ref->name, "^{}"))
Nguyễn Thái Ngọc Duy3e6e0ed2012-01-07 21:45:59 +0700542 continue;
Jeff King167a5752020-04-01 08:15:37 -0400543 if (!has_object_file_with_flags(&ref->old_oid,
544 OBJECT_INFO_QUICK |
545 OBJECT_INFO_SKIP_FETCH_OBJECT))
Nguyễn Thái Ngọc Duy3e6e0ed2012-01-07 21:45:59 +0700546 continue;
brian m. carlsonae077772017-10-15 22:06:51 +0000547 update_ref(msg, ref->name, &ref->old_oid, NULL, 0,
548 UPDATE_REFS_DIE_ON_ERR);
Nguyễn Thái Ngọc Duy3e6e0ed2012-01-07 21:45:59 +0700549 }
550}
551
Patrick Steinhardt9fec7b22021-09-01 15:09:50 +0200552static const struct object_id *iterate_ref_map(void *cb_data)
Jeff King0433ad12013-03-25 16:26:27 -0400553{
554 struct ref **rm = cb_data;
555 struct ref *ref = *rm;
556
557 /*
558 * Skip anything missing a peer_ref, which we are not
559 * actually going to write a ref for.
560 */
561 while (ref && !ref->peer_ref)
562 ref = ref->next;
Jeff King0433ad12013-03-25 16:26:27 -0400563 if (!ref)
Patrick Steinhardt9fec7b22021-09-01 15:09:50 +0200564 return NULL;
Jeff King0433ad12013-03-25 16:26:27 -0400565
Jeff King0433ad12013-03-25 16:26:27 -0400566 *rm = ref->next;
Patrick Steinhardt9fec7b22021-09-01 15:09:50 +0200567 return &ref->old_oid;
Jeff King0433ad12013-03-25 16:26:27 -0400568}
569
Nguyễn Thái Ngọc Duy960b7d12012-01-16 16:46:11 +0700570static void update_remote_refs(const struct ref *refs,
571 const struct ref *mapped_refs,
572 const struct ref *remote_head_points_at,
573 const char *branch_top,
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +0700574 const char *msg,
Junio C Hamano45ed4af2013-07-18 12:48:28 -0700575 struct transport *transport,
Jonathan Tan2b984782020-03-20 15:00:45 -0700576 int check_connectivity)
Nguyễn Thái Ngọc Duy960b7d12012-01-16 16:46:11 +0700577{
Jeff King0433ad12013-03-25 16:26:27 -0400578 const struct ref *rm = mapped_refs;
579
Jeff King125a05f2013-07-08 03:30:41 -0400580 if (check_connectivity) {
Jeff King7043c702016-07-15 06:30:40 -0400581 struct check_connected_options opt = CHECK_CONNECTED_INIT;
582
583 opt.transport = transport;
Jeff King38e590e2016-07-15 06:33:18 -0400584 opt.progress = transport->progress;
Jeff King7043c702016-07-15 06:30:40 -0400585
Jeff King7043c702016-07-15 06:30:40 -0400586 if (check_connected(iterate_ref_map, &rm, &opt))
Jeff King125a05f2013-07-08 03:30:41 -0400587 die(_("remote did not send all necessary objects"));
588 }
Jeff King0433ad12013-03-25 16:26:27 -0400589
Nguyễn Thái Ngọc Duy960b7d12012-01-16 16:46:11 +0700590 if (refs) {
Nguyễn Thái Ngọc Duy960b7d12012-01-16 16:46:11 +0700591 write_remote_refs(mapped_refs);
Ævar Arnfjörð Bjarmason0dab2462017-04-26 23:12:33 +0000592 if (option_single_branch && !option_no_tags)
Nguyễn Thái Ngọc Duy960b7d12012-01-16 16:46:11 +0700593 write_followtags(refs, msg);
594 }
595
596 if (remote_head_points_at && !option_bare) {
597 struct strbuf head_ref = STRBUF_INIT;
598 strbuf_addstr(&head_ref, branch_top);
599 strbuf_addstr(&head_ref, "HEAD");
Jeff King4be49d72016-01-12 04:57:34 -0500600 if (create_symref(head_ref.buf,
601 remote_head_points_at->peer_ref->name,
602 msg) < 0)
Nguyễn Thái Ngọc Duy39ad4f32016-02-27 13:41:55 +0700603 die(_("unable to update %s"), head_ref.buf);
Jeff King4be49d72016-01-12 04:57:34 -0500604 strbuf_release(&head_ref);
Nguyễn Thái Ngọc Duy960b7d12012-01-16 16:46:11 +0700605 }
606}
607
Nguyễn Thái Ngọc Duyf034d352012-01-16 16:46:10 +0700608static void update_head(const struct ref *our, const struct ref *remote,
Jeff Kingdaf78982022-07-11 05:21:52 -0400609 const char *unborn, const char *msg)
Nguyễn Thái Ngọc Duyf034d352012-01-16 16:46:10 +0700610{
Jeff Kingcf4fff52014-06-18 15:44:19 -0400611 const char *head;
612 if (our && skip_prefix(our->name, "refs/heads/", &head)) {
Nguyễn Thái Ngọc Duyf034d352012-01-16 16:46:10 +0700613 /* Local default branch link */
Jeff King4be49d72016-01-12 04:57:34 -0500614 if (create_symref("HEAD", our->name, NULL) < 0)
Nguyễn Thái Ngọc Duy39ad4f32016-02-27 13:41:55 +0700615 die(_("unable to update HEAD"));
Nguyễn Thái Ngọc Duyf034d352012-01-16 16:46:10 +0700616 if (!option_bare) {
brian m. carlsonae077772017-10-15 22:06:51 +0000617 update_ref(msg, "HEAD", &our->old_oid, NULL, 0,
Michael Haggertyf4124112014-04-07 15:47:56 +0200618 UPDATE_REFS_DIE_ON_ERR);
Sean Barag75ca3902020-10-01 03:46:15 +0000619 install_branch_config(0, head, remote_name, our->name);
Nguyễn Thái Ngọc Duyf034d352012-01-16 16:46:10 +0700620 }
Nguyễn Thái Ngọc Duy5a7d5b62012-01-16 16:46:15 +0700621 } else if (our) {
Stefan Beller2122f672018-06-28 18:21:58 -0700622 struct commit *c = lookup_commit_reference(the_repository,
623 &our->old_oid);
Nguyễn Thái Ngọc Duy5a7d5b62012-01-16 16:46:15 +0700624 /* --branch specifies a non-branch (i.e. tags), detach HEAD */
Michael Haggerty91774af2017-11-05 09:42:06 +0100625 update_ref(msg, "HEAD", &c->object.oid, NULL, REF_NO_DEREF,
brian m. carlsonae077772017-10-15 22:06:51 +0000626 UPDATE_REFS_DIE_ON_ERR);
Nguyễn Thái Ngọc Duyf034d352012-01-16 16:46:10 +0700627 } else if (remote) {
628 /*
629 * We know remote HEAD points to a non-branch, or
Nguyễn Thái Ngọc Duy920b6912012-01-16 16:46:14 +0700630 * HEAD points to a branch but we don't know which one.
Nguyễn Thái Ngọc Duyf034d352012-01-16 16:46:10 +0700631 * Detach HEAD in all these cases.
632 */
Michael Haggerty91774af2017-11-05 09:42:06 +0100633 update_ref(msg, "HEAD", &remote->old_oid, NULL, REF_NO_DEREF,
brian m. carlsonae077772017-10-15 22:06:51 +0000634 UPDATE_REFS_DIE_ON_ERR);
Jeff Kingdaf78982022-07-11 05:21:52 -0400635 } else if (unborn && skip_prefix(unborn, "refs/heads/", &head)) {
636 /*
637 * Unborn head from remote; same as "our" case above except
638 * that we have no ref to update.
639 */
640 if (create_symref("HEAD", unborn, NULL) < 0)
641 die(_("unable to update HEAD"));
642 if (!option_bare)
643 install_branch_config(0, head, remote_name, unborn);
Nguyễn Thái Ngọc Duyf034d352012-01-16 16:46:10 +0700644 }
645}
646
Derrick Stoleed89f09c2019-11-21 22:04:35 +0000647static int git_sparse_checkout_init(const char *repo)
648{
Jeff King22f9b7f2020-07-28 16:24:27 -0400649 struct strvec argv = STRVEC_INIT;
Derrick Stoleed89f09c2019-11-21 22:04:35 +0000650 int result = 0;
Elijah Newrend3595412021-12-14 04:09:12 +0000651 strvec_pushl(&argv, "-C", repo, "sparse-checkout", "set", NULL);
Derrick Stoleed89f09c2019-11-21 22:04:35 +0000652
653 /*
654 * We must apply the setting in the current process
655 * for the later checkout to use the sparse-checkout file.
656 */
657 core_apply_sparse_checkout = 1;
658
Jeff Kingd70a9eb2020-07-28 20:37:20 -0400659 if (run_command_v_opt(argv.v, RUN_GIT_CMD)) {
Derrick Stoleed89f09c2019-11-21 22:04:35 +0000660 error(_("failed to initialize sparse-checkout"));
661 result = 1;
662 }
663
Jeff King22f9b7f2020-07-28 16:24:27 -0400664 strvec_clear(&argv);
Derrick Stoleed89f09c2019-11-21 22:04:35 +0000665 return result;
666}
667
Josh Steadmonf05da2b2022-02-04 21:00:49 -0800668static int checkout(int submodule_progress, int filter_submodules)
Nguyễn Thái Ngọc Duyc39852c2012-01-16 16:46:09 +0700669{
brian m. carlsonddc2cc62017-02-21 23:47:27 +0000670 struct object_id oid;
Nguyễn Thái Ngọc Duyc39852c2012-01-16 16:46:09 +0700671 char *head;
Martin Ågren837e34e2017-10-05 22:32:04 +0200672 struct lock_file lock_file = LOCK_INIT;
Nguyễn Thái Ngọc Duyc39852c2012-01-16 16:46:09 +0700673 struct unpack_trees_options opts;
674 struct tree *tree;
675 struct tree_desc t;
Nguyễn Thái Ngọc Duy03b86642014-06-13 19:19:23 +0700676 int err = 0;
Nguyễn Thái Ngọc Duyc39852c2012-01-16 16:46:09 +0700677
678 if (option_no_checkout)
679 return 0;
680
brian m. carlson0f2dc722017-10-15 22:06:55 +0000681 head = resolve_refdup("HEAD", RESOLVE_REF_READING, &oid, NULL);
Nguyễn Thái Ngọc Duyc39852c2012-01-16 16:46:09 +0700682 if (!head) {
683 warning(_("remote HEAD refers to nonexistent ref, "
Jeff Kingf77710c2022-07-07 19:54:51 -0400684 "unable to checkout"));
Nguyễn Thái Ngọc Duyc39852c2012-01-16 16:46:09 +0700685 return 0;
686 }
Nguyễn Thái Ngọc Duy28570932012-01-16 16:46:16 +0700687 if (!strcmp(head, "HEAD")) {
Ben Boeckeled9bff02021-08-23 12:44:00 +0200688 if (advice_enabled(ADVICE_DETACHED_HEAD))
brian m. carlsonddc2cc62017-02-21 23:47:27 +0000689 detach_advice(oid_to_hex(&oid));
brian m. carlsondfc8cdc2020-03-16 18:05:05 +0000690 FREE_AND_NULL(head);
Nguyễn Thái Ngọc Duy28570932012-01-16 16:46:16 +0700691 } else {
Christian Couder59556542013-11-30 21:55:40 +0100692 if (!starts_with(head, "refs/heads/"))
Nguyễn Thái Ngọc Duyc39852c2012-01-16 16:46:09 +0700693 die(_("HEAD not found below refs/heads!"));
694 }
Nguyễn Thái Ngọc Duyc39852c2012-01-16 16:46:09 +0700695
696 /* We need to be in the new work tree for the checkout */
697 setup_work_tree();
698
Martin Ågren837e34e2017-10-05 22:32:04 +0200699 hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
Nguyễn Thái Ngọc Duyc39852c2012-01-16 16:46:09 +0700700
701 memset(&opts, 0, sizeof opts);
702 opts.update = 1;
703 opts.merge = 1;
Duy Nguyenb8785792018-08-17 20:00:39 +0200704 opts.clone = 1;
Elijah Newren1b5f3732021-09-27 16:33:43 +0000705 opts.preserve_ignored = 0;
Nguyễn Thái Ngọc Duyc39852c2012-01-16 16:46:09 +0700706 opts.fn = oneway_merge;
Junio C Hamano8f63da12012-05-07 12:35:36 -0700707 opts.verbose_update = (option_verbosity >= 0);
Nguyễn Thái Ngọc Duyc39852c2012-01-16 16:46:09 +0700708 opts.src_index = &the_index;
709 opts.dst_index = &the_index;
brian m. carlsondfc8cdc2020-03-16 18:05:05 +0000710 init_checkout_metadata(&opts.meta, head, &oid, NULL);
Nguyễn Thái Ngọc Duyc39852c2012-01-16 16:46:09 +0700711
brian m. carlsona9dbc172017-05-06 22:10:37 +0000712 tree = parse_tree_indirect(&oid);
Glen Choo8d2eaf62022-03-01 16:36:13 -0800713 if (!tree)
714 die(_("unable to parse commit %s"), oid_to_hex(&oid));
Nguyễn Thái Ngọc Duyc39852c2012-01-16 16:46:09 +0700715 parse_tree(tree);
716 init_tree_desc(&t, tree->buffer, tree->size);
Jeff King0aac7bb2013-03-25 16:23:59 -0400717 if (unpack_trees(1, &t, &opts) < 0)
718 die(_("unable to checkout working tree"));
Nguyễn Thái Ngọc Duyc39852c2012-01-16 16:46:09 +0700719
brian m. carlsondfc8cdc2020-03-16 18:05:05 +0000720 free(head);
721
Martin Ågren837e34e2017-10-05 22:32:04 +0200722 if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
Nguyễn Thái Ngọc Duyc39852c2012-01-16 16:46:09 +0700723 die(_("unable to write new index file"));
724
Emily Shaffer72ddf342021-12-22 04:59:35 +0100725 err |= run_hooks_l("post-checkout", oid_to_hex(null_oid()),
brian m. carlsonddc2cc62017-02-21 23:47:27 +0000726 oid_to_hex(&oid), "1", NULL);
Nguyễn Thái Ngọc Duyc39852c2012-01-16 16:46:09 +0700727
Brandon Williamsbb62e0a2017-03-17 15:38:03 -0700728 if (!err && (option_recurse_submodules.nr > 0)) {
Jeff King22f9b7f2020-07-28 16:24:27 -0400729 struct strvec args = STRVEC_INIT;
730 strvec_pushl(&args, "submodule", "update", "--require-init", "--recursive", NULL);
Stefan Beller72290d62016-02-29 18:07:20 -0800731
Junio C Hamano18a74a02016-06-19 13:51:56 -0700732 if (option_shallow_submodules == 1)
Jeff King22f9b7f2020-07-28 16:24:27 -0400733 strvec_push(&args, "--depth=1");
Stefan Bellerd22eb042016-04-25 18:12:27 -0700734
Stefan Beller72290d62016-02-29 18:07:20 -0800735 if (max_jobs != -1)
Jeff King22f9b7f2020-07-28 16:24:27 -0400736 strvec_pushf(&args, "--jobs=%d", max_jobs);
Stefan Beller72290d62016-02-29 18:07:20 -0800737
Jeff King72c5f882016-09-22 01:24:46 -0400738 if (submodule_progress)
Jeff King22f9b7f2020-07-28 16:24:27 -0400739 strvec_push(&args, "--progress");
Jeff King72c5f882016-09-22 01:24:46 -0400740
Brandon Williams03c004c2017-08-03 15:25:44 -0700741 if (option_verbosity < 0)
Jeff King22f9b7f2020-07-28 16:24:27 -0400742 strvec_push(&args, "--quiet");
Brandon Williams03c004c2017-08-03 15:25:44 -0700743
Ben Avison4c691012019-05-19 15:26:49 +0100744 if (option_remote_submodules) {
Jeff King22f9b7f2020-07-28 16:24:27 -0400745 strvec_push(&args, "--remote");
746 strvec_push(&args, "--no-fetch");
Ben Avison4c691012019-05-19 15:26:49 +0100747 }
748
Josh Steadmonf05da2b2022-02-04 21:00:49 -0800749 if (filter_submodules && filter_options.choice)
750 strvec_pushf(&args, "--filter=%s",
751 expand_list_objects_filter_spec(&filter_options));
752
Emily Shaffer132f6002020-02-20 19:10:27 -0800753 if (option_single_branch >= 0)
Jeff King22f9b7f2020-07-28 16:24:27 -0400754 strvec_push(&args, option_single_branch ?
Emily Shaffer132f6002020-02-20 19:10:27 -0800755 "--single-branch" :
756 "--no-single-branch");
757
Jeff Kingd70a9eb2020-07-28 20:37:20 -0400758 err = run_command_v_opt(args.v, RUN_GIT_CMD);
Jeff King22f9b7f2020-07-28 16:24:27 -0400759 strvec_clear(&args);
Stefan Beller72290d62016-02-29 18:07:20 -0800760 }
Nguyễn Thái Ngọc Duyc39852c2012-01-16 16:46:09 +0700761
762 return err;
763}
764
Sean Barag552955e2020-10-01 03:46:11 +0000765static int git_clone_config(const char *k, const char *v, void *cb)
766{
Sean Baragde9ed3e2020-10-01 03:46:16 +0000767 if (!strcmp(k, "clone.defaultremotename")) {
768 free(remote_name);
769 remote_name = xstrdup(v);
770 }
Li Linchao4fe788b2021-04-01 10:46:59 +0000771 if (!strcmp(k, "clone.rejectshallow"))
772 config_reject_shallow = git_config_bool(k, v);
Josh Steadmonf05da2b2022-02-04 21:00:49 -0800773 if (!strcmp(k, "clone.filtersubmodules"))
774 config_filter_submodules = git_config_bool(k, v);
Li Linchao4fe788b2021-04-01 10:46:59 +0000775
Sean Barag552955e2020-10-01 03:46:11 +0000776 return git_default_config(k, v, cb);
777}
778
Jeff King84054f72011-06-09 16:56:19 -0400779static int write_one_config(const char *key, const char *value, void *data)
780{
Sean Barag552955e2020-10-01 03:46:11 +0000781 /*
782 * give git_clone_config a chance to write config values back to the
783 * environment, since git_config_set_multivar_gently only deals with
784 * config-file writes
785 */
786 int apply_failed = git_clone_config(key, value, data);
787 if (apply_failed)
788 return apply_failed;
789
Jonathan Niederdb4eca12017-05-01 17:05:15 -0700790 return git_config_set_multivar_gently(key,
791 value ? value : "true",
792 CONFIG_REGEX_NONE, 0);
Jeff King84054f72011-06-09 16:56:19 -0400793}
794
795static void write_config(struct string_list *config)
796{
797 int i;
798
799 for (i = 0; i < config->nr; i++) {
800 if (git_config_parse_parameter(config->items[i].string,
801 write_one_config, NULL) < 0)
Nguyễn Thái Ngọc Duy39ad4f32016-02-27 13:41:55 +0700802 die(_("unable to write parameters to config file"));
Jeff King84054f72011-06-09 16:56:19 -0400803 }
804}
805
David Aguilar24d36f12014-08-31 13:11:31 -0700806static void write_refspec_config(const char *src_ref_prefix,
807 const struct ref *our_head_points_at,
808 const struct ref *remote_head_points_at,
809 struct strbuf *branch_top)
Ralf Thielow31b808a2012-09-20 20:04:08 +0200810{
811 struct strbuf key = STRBUF_INIT;
812 struct strbuf value = STRBUF_INIT;
813
814 if (option_mirror || !option_bare) {
815 if (option_single_branch && !option_mirror) {
816 if (option_branch) {
Junio C Hamano60a5f5f2014-06-23 14:27:36 -0700817 if (starts_with(our_head_points_at->name, "refs/tags/"))
Ralf Thielow31b808a2012-09-20 20:04:08 +0200818 strbuf_addf(&value, "+%s:%s", our_head_points_at->name,
819 our_head_points_at->name);
820 else
821 strbuf_addf(&value, "+%s:%s%s", our_head_points_at->name,
822 branch_top->buf, option_branch);
823 } else if (remote_head_points_at) {
Jeff Kingcf4fff52014-06-18 15:44:19 -0400824 const char *head = remote_head_points_at->name;
825 if (!skip_prefix(head, "refs/heads/", &head))
Johannes Schindelin033abf92018-05-02 11:38:39 +0200826 BUG("remote HEAD points at non-head?");
Jeff Kingcf4fff52014-06-18 15:44:19 -0400827
Ralf Thielow31b808a2012-09-20 20:04:08 +0200828 strbuf_addf(&value, "+%s:%s%s", remote_head_points_at->name,
Jeff Kingcf4fff52014-06-18 15:44:19 -0400829 branch_top->buf, head);
Ralf Thielow31b808a2012-09-20 20:04:08 +0200830 }
831 /*
832 * otherwise, the next "git fetch" will
833 * simply fetch from HEAD without updating
Michael Schubertd6ac1d22013-07-03 11:12:34 +0200834 * any remote-tracking branch, which is what
Ralf Thielow31b808a2012-09-20 20:04:08 +0200835 * we want.
836 */
837 } else {
838 strbuf_addf(&value, "+%s*:%s*", src_ref_prefix, branch_top->buf);
839 }
840 /* Configure the remote */
841 if (value.len) {
Sean Barag75ca3902020-10-01 03:46:15 +0000842 strbuf_addf(&key, "remote.%s.fetch", remote_name);
Ralf Thielow31b808a2012-09-20 20:04:08 +0200843 git_config_set_multivar(key.buf, value.buf, "^$", 0);
844 strbuf_reset(&key);
845
846 if (option_mirror) {
Sean Barag75ca3902020-10-01 03:46:15 +0000847 strbuf_addf(&key, "remote.%s.mirror", remote_name);
Ralf Thielow31b808a2012-09-20 20:04:08 +0200848 git_config_set(key.buf, "true");
849 strbuf_reset(&key);
850 }
851 }
852 }
853
854 strbuf_release(&key);
855 strbuf_release(&value);
856}
857
Junio C Hamanofb1d6da2014-10-14 12:38:52 -0700858static void dissociate_from_references(void)
859{
860 static const char* argv[] = { "repack", "-a", "-d", NULL };
Alex Riesen01816812015-10-22 18:41:17 +0200861 char *alternates = git_pathdup("objects/info/alternates");
Junio C Hamanofb1d6da2014-10-14 12:38:52 -0700862
Alex Riesen01816812015-10-22 18:41:17 +0200863 if (!access(alternates, F_OK)) {
864 if (run_command_v_opt(argv, RUN_GIT_CMD|RUN_COMMAND_NO_STDIN))
865 die(_("cannot repack to clean up"));
866 if (unlink(alternates) && errno != ENOENT)
867 die_errno(_("cannot unlink temporary alternates file"));
868 }
869 free(alternates);
Junio C Hamanofb1d6da2014-10-14 12:38:52 -0700870}
871
Miriam Rubio6c020422019-10-28 17:55:23 +0100872static int path_exists(const char *path)
Jeff Kingf9e377a2018-01-02 16:10:14 -0500873{
874 struct stat sb;
875 return !stat(path, &sb);
876}
877
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400878int cmd_clone(int argc, const char **argv, const char *prefix)
879{
Nguyễn Thái Ngọc Duy24c61c42010-08-23 22:08:22 +1000880 int is_bundle = 0, is_local;
Li Linchao4fe788b2021-04-01 10:46:59 +0000881 int reject_shallow = 0;
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400882 const char *repo_name, *repo, *work_tree, *git_dir;
Andrzej Hunt0c454272021-03-14 18:47:36 +0000883 char *path = NULL, *dir, *display_repo = NULL;
Ben Wijendfaa2092020-07-10 10:47:32 +0200884 int dest_exists, real_dest_exists = 0;
Daniel Barkalow37148312009-11-18 02:42:24 +0100885 const struct ref *refs, *remote_head;
Andrzej Hunt0c454272021-03-14 18:47:36 +0000886 struct ref *remote_head_points_at = NULL;
Jeff King7a4ee282009-08-26 15:05:08 -0400887 const struct ref *our_head_points_at;
Jeff Kingdaf78982022-07-11 05:21:52 -0400888 char *unborn_head = NULL;
Jonathan Tandccea602022-01-24 10:09:09 -0800889 struct ref *mapped_refs = NULL;
Nguyễn Thái Ngọc Duy90498162012-01-24 18:10:38 +0700890 const struct ref *ref;
SZEDER Gábor3e42cb32018-11-14 11:46:18 +0100891 struct strbuf key = STRBUF_INIT;
Miklos Vajnab5ff37a2008-11-21 01:45:01 +0100892 struct strbuf branch_top = STRBUF_INIT, reflog_msg = STRBUF_INIT;
Shawn O. Pearce1db4a752008-07-08 04:46:06 +0000893 struct transport *transport = NULL;
Nguyễn Thái Ngọc Duy9e585042012-01-16 16:46:13 +0700894 const char *src_ref_prefix = "refs/heads/";
Nguyễn Thái Ngọc Duy6f48d392012-01-16 16:46:12 +0700895 struct remote *remote;
Nguyễn Thái Ngọc Duy90498162012-01-24 18:10:38 +0700896 int err = 0, complete_refs_before_fetch = 1;
Jeff King72c5f882016-09-22 01:24:46 -0400897 int submodule_progress;
Josh Steadmonf05da2b2022-02-04 21:00:49 -0800898 int filter_submodules = 0;
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400899
Jonathan Tan39835402021-02-05 12:48:48 -0800900 struct transport_ls_refs_options transport_ls_refs_options =
901 TRANSPORT_LS_REFS_OPTIONS_INIT;
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400902
Jeff Kingbbc30f92011-02-24 09:30:19 -0500903 packet_trace_identity("clone");
Sean Barag552955e2020-10-01 03:46:11 +0000904
905 git_config(git_clone_config, NULL);
906
Stephen Boyd37782922009-05-23 11:53:12 -0700907 argc = parse_options(argc, argv, prefix, builtin_clone_options,
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400908 builtin_clone_usage, 0);
909
Jonathan Niederd52dc4b2009-10-29 03:10:30 -0500910 if (argc > 2)
Ævar Arnfjörð Bjarmasone84d7b72011-02-22 23:41:26 +0000911 usage_msg_opt(_("Too many arguments."),
Jonathan Niederd52dc4b2009-10-29 03:10:30 -0500912 builtin_clone_usage, builtin_clone_options);
913
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400914 if (argc == 0)
Ævar Arnfjörð Bjarmasone84d7b72011-02-22 23:41:26 +0000915 usage_msg_opt(_("You must specify a repository to clone."),
Jonathan Niederd52dc4b2009-10-29 03:10:30 -0500916 builtin_clone_usage, builtin_clone_options);
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400917
Nguyễn Thái Ngọc Duy859e5df2016-06-12 17:54:05 +0700918 if (option_depth || option_since || option_not.nr)
Nguyễn Thái Ngọc Duy994c2aa2016-06-12 17:54:00 +0700919 deepen = 1;
Nguyễn Thái Ngọc Duy3e6e0ed2012-01-07 21:45:59 +0700920 if (option_single_branch == -1)
Nguyễn Thái Ngọc Duy994c2aa2016-06-12 17:54:00 +0700921 option_single_branch = deepen ? 1 : 0;
Nguyễn Thái Ngọc Duy3e6e0ed2012-01-07 21:45:59 +0700922
Johannes Schindelinbc699af2008-08-02 21:38:56 +0200923 if (option_mirror)
924 option_bare = 1;
925
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400926 if (option_bare) {
927 if (option_origin)
Jean-Noël Avila246cac82022-01-05 20:02:24 +0000928 die(_("options '%s' and '%s %s' cannot be used together"),
929 "--bare", "--origin", option_origin);
Nguyễn Thái Ngọc Duy95b63f12013-01-11 10:09:59 +0700930 if (real_git_dir)
Jean-Noël Avila12909b62022-01-05 20:02:16 +0000931 die(_("options '%s' and '%s' cannot be used together"), "--bare", "--separate-git-dir");
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400932 option_no_checkout = 1;
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400933 }
934
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400935 repo_name = argv[0];
936
937 path = get_repo_path(repo_name, &is_bundle);
Andrzej Hunt0c454272021-03-14 18:47:36 +0000938 if (path) {
939 FREE_AND_NULL(path);
René Scharfe0aaad412017-01-26 18:54:23 +0100940 repo = absolute_pathdup(repo_name);
Andrzej Hunt0c454272021-03-14 18:47:36 +0000941 } else if (strchr(repo_name, ':')) {
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400942 repo = repo_name;
Johannes Schindelin46da2952020-06-04 20:08:29 +0000943 display_repo = transport_anonymize_url(repo);
944 } else
945 die(_("repository '%s' does not exist"), repo_name);
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400946
Nguyễn Thái Ngọc Duy5594bca2013-12-05 10:31:11 +0700947 /* no need to be strict, transport_set_option() will validate it again */
948 if (option_depth && atoi(option_depth) < 1)
949 die(_("depth %s is not a positive number"), option_depth);
950
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400951 if (argc == 2)
952 dir = xstrdup(argv[1]);
953 else
Atharva Raykared863012021-08-10 17:16:36 +0530954 dir = git_url_basename(repo_name, is_bundle, option_bare);
955 strip_dir_trailing_slashes(dir);
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400956
Miriam Rubio6c020422019-10-28 17:55:23 +0100957 dest_exists = path_exists(dir);
Alexander Potashev55892d22009-01-11 15:19:12 +0300958 if (dest_exists && !is_empty_dir(dir))
Ævar Arnfjörð Bjarmasone84d7b72011-02-22 23:41:26 +0000959 die(_("destination path '%s' already exists and is not "
960 "an empty directory."), dir);
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400961
Ben Wijendfaa2092020-07-10 10:47:32 +0200962 if (real_git_dir) {
963 real_dest_exists = path_exists(real_git_dir);
964 if (real_dest_exists && !is_empty_dir(real_git_dir))
965 die(_("repository path '%s' already exists and is not "
966 "an empty directory."), real_git_dir);
967 }
968
969
Johannes Schindelin46da2952020-06-04 20:08:29 +0000970 strbuf_addf(&reflog_msg, "clone: from %s",
971 display_repo ? display_repo : repo);
972 free(display_repo);
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400973
974 if (option_bare)
975 work_tree = NULL;
976 else {
977 work_tree = getenv("GIT_WORK_TREE");
Miriam Rubio6c020422019-10-28 17:55:23 +0100978 if (work_tree && path_exists(work_tree))
Ævar Arnfjörð Bjarmasone84d7b72011-02-22 23:41:26 +0000979 die(_("working tree '%s' already exists."), work_tree);
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400980 }
981
982 if (option_bare || work_tree)
983 git_dir = xstrdup(dir);
984 else {
985 work_tree = dir;
Ramsay Jones4e2d0942012-09-04 18:31:14 +0100986 git_dir = mkpathdup("%s/.git", dir);
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400987 }
988
Jeff Kingee0e3872015-03-18 14:55:32 -0400989 atexit(remove_junk);
990 sigchain_push_common(remove_junk_on_signal);
991
Daniel Barkalow8434c2f2008-04-27 13:39:30 -0400992 if (!option_bare) {
Jeff King8e21d632008-06-25 01:41:34 -0400993 if (safe_create_leading_directories_const(work_tree) < 0)
Ævar Arnfjörð Bjarmasone84d7b72011-02-22 23:41:26 +0000994 die_errno(_("could not create leading directories of '%s'"),
Thomas Rastd824cbb2009-06-27 17:58:46 +0200995 work_tree);
Jeff Kingd45420c2018-01-02 16:11:39 -0500996 if (dest_exists)
997 junk_work_tree_flags |= REMOVE_DIR_KEEP_TOPLEVEL;
998 else if (mkdir(work_tree, 0777))
Jeff King16eff6c2015-03-18 15:02:01 -0400999 die_errno(_("could not create work tree dir '%s'"),
Thomas Rastd824cbb2009-06-27 17:58:46 +02001000 work_tree);
Jeff Kingee0e3872015-03-18 14:55:32 -04001001 junk_work_tree = work_tree;
Daniel Barkalow8434c2f2008-04-27 13:39:30 -04001002 set_git_work_tree(work_tree);
1003 }
Daniel Barkalow8434c2f2008-04-27 13:39:30 -04001004
Jeff Kingd45420c2018-01-02 16:11:39 -05001005 if (real_git_dir) {
Ben Wijendfaa2092020-07-10 10:47:32 +02001006 if (real_dest_exists)
Jeff Kingd45420c2018-01-02 16:11:39 -05001007 junk_git_dir_flags |= REMOVE_DIR_KEEP_TOPLEVEL;
1008 junk_git_dir = real_git_dir;
1009 } else {
1010 if (dest_exists)
1011 junk_git_dir_flags |= REMOVE_DIR_KEEP_TOPLEVEL;
1012 junk_git_dir = git_dir;
1013 }
Jeff King8e21d632008-06-25 01:41:34 -04001014 if (safe_create_leading_directories_const(git_dir) < 0)
Ævar Arnfjörð Bjarmasone84d7b72011-02-22 23:41:26 +00001015 die(_("could not create leading directories of '%s'"), git_dir);
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +07001016
Ævar Arnfjörð Bjarmason3781fcc2011-02-22 23:41:27 +00001017 if (0 <= option_verbosity) {
1018 if (option_bare)
Jeff King68b939b2013-09-18 16:05:13 -04001019 fprintf(stderr, _("Cloning into bare repository '%s'...\n"), dir);
Ævar Arnfjörð Bjarmason3781fcc2011-02-22 23:41:27 +00001020 else
Jeff King68b939b2013-09-18 16:05:13 -04001021 fprintf(stderr, _("Cloning into '%s'...\n"), dir);
Ævar Arnfjörð Bjarmason3781fcc2011-02-22 23:41:27 +00001022 }
Stefan Beller31224cb2016-08-17 15:45:35 -07001023
Brandon Williamsbb62e0a2017-03-17 15:38:03 -07001024 if (option_recurse_submodules.nr > 0) {
1025 struct string_list_item *item;
1026 struct strbuf sb = STRBUF_INIT;
Mahi Kolla48072e32021-08-14 01:09:56 +00001027 int val;
Brandon Williamsbb62e0a2017-03-17 15:38:03 -07001028
1029 /* remove duplicates */
1030 string_list_sort(&option_recurse_submodules);
1031 string_list_remove_duplicates(&option_recurse_submodules, 0);
1032
1033 /*
1034 * NEEDSWORK: In a multi-working-tree world, this needs to be
1035 * set in the per-worktree config.
1036 */
1037 for_each_string_list_item(item, &option_recurse_submodules) {
1038 strbuf_addf(&sb, "submodule.active=%s",
1039 item->string);
1040 string_list_append(&option_config,
1041 strbuf_detach(&sb, NULL));
1042 }
1043
Mahi Kolla48072e32021-08-14 01:09:56 +00001044 if (!git_config_get_bool("submodule.stickyRecursiveClone", &val) &&
1045 val)
1046 string_list_append(&option_config, "submodule.recurse=true");
1047
Stefan Beller31224cb2016-08-17 15:45:35 -07001048 if (option_required_reference.nr &&
1049 option_optional_reference.nr)
1050 die(_("clone --recursive is not compatible with "
1051 "both --reference and --reference-if-able"));
1052 else if (option_required_reference.nr) {
1053 string_list_append(&option_config,
1054 "submodule.alternateLocation=superproject");
1055 string_list_append(&option_config,
1056 "submodule.alternateErrorStrategy=die");
1057 } else if (option_optional_reference.nr) {
1058 string_list_append(&option_config,
1059 "submodule.alternateLocation=superproject");
1060 string_list_append(&option_config,
1061 "submodule.alternateErrorStrategy=info");
1062 }
1063 }
1064
Johannes Schindelin32ba12d2020-06-24 14:46:32 +00001065 init_db(git_dir, real_git_dir, option_template, GIT_HASH_UNKNOWN, NULL,
1066 INIT_DB_QUIET);
Nguyễn Thái Ngọc Duy33158702016-09-25 10:14:37 +07001067
Ævar Arnfjörð Bjarmason27ff1fb2021-10-22 10:55:42 +02001068 if (real_git_dir) {
1069 free((char *)git_dir);
Nguyễn Thái Ngọc Duy33158702016-09-25 10:14:37 +07001070 git_dir = real_git_dir;
Ævar Arnfjörð Bjarmason27ff1fb2021-10-22 10:55:42 +02001071 }
Nguyễn Thái Ngọc Duy33158702016-09-25 10:14:37 +07001072
Sean Barag552955e2020-10-01 03:46:11 +00001073 /*
1074 * additional config can be injected with -c, make sure it's included
1075 * after init_db, which clears the entire config environment.
1076 */
Jeff King84054f72011-06-09 16:56:19 -04001077 write_config(&option_config);
Daniel Barkalow8434c2f2008-04-27 13:39:30 -04001078
Sean Barag552955e2020-10-01 03:46:11 +00001079 /*
1080 * re-read config after init_db and write_config to pick up any config
1081 * injected by --template and --config, respectively.
1082 */
1083 git_config(git_clone_config, NULL);
Daniel Barkalow8434c2f2008-04-27 13:39:30 -04001084
Sean Baragde9ed3e2020-10-01 03:46:16 +00001085 /*
Li Linchao4fe788b2021-04-01 10:46:59 +00001086 * If option_reject_shallow is specified from CLI option,
1087 * ignore config_reject_shallow from git_clone_config.
1088 */
1089 if (config_reject_shallow != -1)
1090 reject_shallow = config_reject_shallow;
1091 if (option_reject_shallow != -1)
1092 reject_shallow = option_reject_shallow;
1093
1094 /*
Josh Steadmonf05da2b2022-02-04 21:00:49 -08001095 * If option_filter_submodules is specified from CLI option,
1096 * ignore config_filter_submodules from git_clone_config.
1097 */
1098 if (config_filter_submodules != -1)
1099 filter_submodules = config_filter_submodules;
1100 if (option_filter_submodules != -1)
1101 filter_submodules = option_filter_submodules;
1102
1103 /*
1104 * Exit if the user seems to be doing something silly with submodule
1105 * filter flags (but not with filter configs, as those should be
1106 * set-and-forget).
1107 */
1108 if (option_filter_submodules > 0 && !filter_options.choice)
1109 die(_("the option '%s' requires '%s'"),
1110 "--also-filter-submodules", "--filter");
1111 if (option_filter_submodules > 0 && !option_recurse_submodules.nr)
1112 die(_("the option '%s' requires '%s'"),
1113 "--also-filter-submodules", "--recurse-submodules");
1114
1115 /*
Sean Baragde9ed3e2020-10-01 03:46:16 +00001116 * apply the remote name provided by --origin only after this second
1117 * call to git_config, to ensure it overrides all config-based values.
1118 */
Junio C Hamano538dc452022-05-20 15:26:59 -07001119 if (option_origin) {
Junio C Hamano6dfadc82022-04-30 22:17:15 -07001120 free(remote_name);
Sean Baragde9ed3e2020-10-01 03:46:16 +00001121 remote_name = xstrdup(option_origin);
Junio C Hamano6dfadc82022-04-30 22:17:15 -07001122 }
Sean Baragde9ed3e2020-10-01 03:46:16 +00001123
Junio C Hamanoafe8a902022-05-02 09:50:37 -07001124 if (!remote_name)
Sean Baragde9ed3e2020-10-01 03:46:16 +00001125 remote_name = xstrdup("origin");
1126
1127 if (!valid_remote_name(remote_name))
1128 die(_("'%s' is not a valid remote name"), remote_name);
Daniel Barkalow8434c2f2008-04-27 13:39:30 -04001129
1130 if (option_bare) {
Johannes Schindelinbc699af2008-08-02 21:38:56 +02001131 if (option_mirror)
1132 src_ref_prefix = "refs/";
Miklos Vajnab5ff37a2008-11-21 01:45:01 +01001133 strbuf_addstr(&branch_top, src_ref_prefix);
Daniel Barkalow8434c2f2008-04-27 13:39:30 -04001134
1135 git_config_set("core.bare", "true");
1136 } else {
Sean Barag75ca3902020-10-01 03:46:15 +00001137 strbuf_addf(&branch_top, "refs/remotes/%s/", remote_name);
Johannes Schindelinbc699af2008-08-02 21:38:56 +02001138 }
Daniel Barkalow8434c2f2008-04-27 13:39:30 -04001139
Sean Barag75ca3902020-10-01 03:46:15 +00001140 strbuf_addf(&key, "remote.%s.url", remote_name);
Sverre Rabbelierdf61c882010-03-29 11:48:24 -05001141 git_config_set(key.buf, repo);
1142 strbuf_reset(&key);
1143
Ævar Arnfjörð Bjarmason0dab2462017-04-26 23:12:33 +00001144 if (option_no_tags) {
Sean Barag75ca3902020-10-01 03:46:15 +00001145 strbuf_addf(&key, "remote.%s.tagOpt", remote_name);
Ævar Arnfjörð Bjarmason0dab2462017-04-26 23:12:33 +00001146 git_config_set(key.buf, "--no-tags");
1147 strbuf_reset(&key);
1148 }
1149
Stefan Bellerf7415b42016-08-15 14:53:26 -07001150 if (option_required_reference.nr || option_optional_reference.nr)
Junio C Hamanodbc92b02011-08-22 18:05:15 -07001151 setup_reference();
Sverre Rabbelier766ac6a2010-03-29 11:48:23 -05001152
Derrick Stolee47dbf102020-01-24 21:19:34 +00001153 if (option_sparse_checkout && git_sparse_checkout_init(dir))
Derrick Stoleed89f09c2019-11-21 22:04:35 +00001154 return 1;
1155
Sean Barag75ca3902020-10-01 03:46:15 +00001156 remote = remote_get(remote_name);
SZEDER Gábor515be832018-11-14 11:46:19 +01001157
René Scharfe1af8b8c2020-09-05 16:49:30 +02001158 refspec_appendf(&remote->fetch, "+%s*:%s*", src_ref_prefix,
1159 branch_top.buf);
Daniel Barkalow8434c2f2008-04-27 13:39:30 -04001160
Nguyễn Thái Ngọc Duy6f48d392012-01-16 16:46:12 +07001161 transport = transport_get(remote, remote->url[0]);
Mike Hommey822f0c42015-05-12 13:30:16 +09001162 transport_set_verbosity(transport, option_verbosity, option_progress);
Eric Wongc915f112016-02-03 04:09:14 +00001163 transport->family = family;
Mike Hommey822f0c42015-05-12 13:30:16 +09001164
Michael Barabanovf38aa832014-07-17 00:09:32 -07001165 path = get_repo_path(remote->url[0], &is_bundle);
1166 is_local = option_local != 0 && path && !is_bundle;
1167 if (is_local) {
1168 if (option_depth)
1169 warning(_("--depth is ignored in local clones; use file:// instead."));
Nguyễn Thái Ngọc Duy994c2aa2016-06-12 17:54:00 +07001170 if (option_since)
1171 warning(_("--shallow-since is ignored in local clones; use file:// instead."));
Nguyễn Thái Ngọc Duy859e5df2016-06-12 17:54:05 +07001172 if (option_not.nr)
1173 warning(_("--shallow-exclude is ignored in local clones; use file:// instead."));
Jonathan Tan548719f2017-12-08 15:58:46 +00001174 if (filter_options.choice)
1175 warning(_("--filter is ignored in local clones; use file:// instead."));
Michael Barabanovf38aa832014-07-17 00:09:32 -07001176 if (!access(mkpath("%s/shallow", path), F_OK)) {
Li Linchao4fe788b2021-04-01 10:46:59 +00001177 if (reject_shallow)
1178 die(_("source repository is shallow, reject to clone."));
Michael Barabanovf38aa832014-07-17 00:09:32 -07001179 if (option_local > 0)
1180 warning(_("source repository is shallow, ignoring --local"));
1181 is_local = 0;
1182 }
1183 }
1184 if (option_local > 0 && !is_local)
1185 warning(_("--local is ignored"));
Nguyễn Thái Ngọc Duybeea4152013-12-05 20:02:39 +07001186 transport->cloning = 1;
Daniel Barkalow8434c2f2008-04-27 13:39:30 -04001187
Derrick Stolee86fdd942022-03-09 16:01:43 +00001188 if (is_bundle) {
1189 struct bundle_header header = BUNDLE_HEADER_INIT;
1190 int fd = read_bundle_header(path, &header);
1191 int has_filter = header.filter.choice != LOFC_DISABLED;
1192
1193 if (fd > 0)
1194 close(fd);
1195 bundle_header_release(&header);
1196 if (has_filter)
1197 die(_("cannot clone from filtered bundle"));
1198 }
1199
Jeff King643f9182013-09-18 16:35:13 -04001200 transport_set_option(transport, TRANS_OPT_KEEP, "yes");
Daniel Barkalow8434c2f2008-04-27 13:39:30 -04001201
Li Linchao4fe788b2021-04-01 10:46:59 +00001202 if (reject_shallow)
1203 transport_set_option(transport, TRANS_OPT_REJECT_SHALLOW, "1");
Jeff King643f9182013-09-18 16:35:13 -04001204 if (option_depth)
1205 transport_set_option(transport, TRANS_OPT_DEPTH,
1206 option_depth);
Nguyễn Thái Ngọc Duy994c2aa2016-06-12 17:54:00 +07001207 if (option_since)
1208 transport_set_option(transport, TRANS_OPT_DEEPEN_SINCE,
1209 option_since);
Nguyễn Thái Ngọc Duy859e5df2016-06-12 17:54:05 +07001210 if (option_not.nr)
1211 transport_set_option(transport, TRANS_OPT_DEEPEN_NOT,
1212 (const char *)&option_not);
Jeff King643f9182013-09-18 16:35:13 -04001213 if (option_single_branch)
1214 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
Daniel Barkalow8434c2f2008-04-27 13:39:30 -04001215
Jeff King643f9182013-09-18 16:35:13 -04001216 if (option_upload_pack)
1217 transport_set_option(transport, TRANS_OPT_UPLOADPACK,
1218 option_upload_pack);
Nguyễn Thái Ngọc Duyc6807a42013-05-26 08:16:17 +07001219
Jonathan Tan6e983052019-04-12 12:51:22 -07001220 if (server_options.nr)
1221 transport->server_options = &server_options;
1222
Jonathan Tan548719f2017-12-08 15:58:46 +00001223 if (filter_options.choice) {
Matthew DeVorecf9ceb52019-06-27 15:54:10 -07001224 const char *spec =
1225 expand_list_objects_filter_spec(&filter_options);
Jonathan Tan548719f2017-12-08 15:58:46 +00001226 transport_set_option(transport, TRANS_OPT_LIST_OBJECTS_FILTER,
Matthew DeVorecf9ceb52019-06-27 15:54:10 -07001227 spec);
Jonathan Tan548719f2017-12-08 15:58:46 +00001228 transport_set_option(transport, TRANS_OPT_FROM_PROMISOR, "1");
1229 }
1230
1231 if (transport->smart_options && !deepen && !filter_options.choice)
Jeff King643f9182013-09-18 16:35:13 -04001232 transport->smart_options->check_self_contained_and_connected = 1;
Daniel Barkalow8434c2f2008-04-27 13:39:30 -04001233
Brandon Williams402c47d2018-07-20 15:07:54 -07001234
Jonathan Tan39835402021-02-05 12:48:48 -08001235 strvec_push(&transport_ls_refs_options.ref_prefixes, "HEAD");
1236 refspec_ref_prefixes(&remote->fetch,
1237 &transport_ls_refs_options.ref_prefixes);
Brandon Williams402c47d2018-07-20 15:07:54 -07001238 if (option_branch)
Jonathan Tan39835402021-02-05 12:48:48 -08001239 expand_ref_prefix(&transport_ls_refs_options.ref_prefixes,
1240 option_branch);
Brandon Williams402c47d2018-07-20 15:07:54 -07001241 if (!option_no_tags)
Jonathan Tan39835402021-02-05 12:48:48 -08001242 strvec_push(&transport_ls_refs_options.ref_prefixes,
1243 "refs/tags/");
Brandon Williams402c47d2018-07-20 15:07:54 -07001244
Jonathan Tan39835402021-02-05 12:48:48 -08001245 refs = transport_get_remote_refs(transport, &transport_ls_refs_options);
Nguyễn Thái Ngọc Duy6f48d392012-01-16 16:46:12 +07001246
Jonathan Tandccea602022-01-24 10:09:09 -08001247 if (refs)
1248 mapped_refs = wanted_peer_refs(refs, &remote->fetch);
1249
1250 if (mapped_refs) {
brian m. carlsonb65dc2c2020-05-25 19:59:05 +00001251 int hash_algo = hash_algo_by_ptr(transport_get_hash_algo(transport));
1252
1253 /*
1254 * Now that we know what algorithm the remote side is using,
1255 * let's set ours to the same thing.
1256 */
brian m. carlson47ac9702020-09-20 22:35:41 +00001257 initialize_repository_version(hash_algo, 1);
brian m. carlsonb65dc2c2020-05-25 19:59:05 +00001258 repo_set_hash_algo(the_repository, hash_algo);
Michael Haggerty5b057952012-02-11 07:20:56 +01001259 /*
1260 * transport_get_remote_refs() may return refs with null sha-1
1261 * in mapped_refs (see struct transport->get_refs_list
1262 * comment). In that case we need fetch it early because
1263 * remote_head code below relies on it.
1264 *
1265 * for normal clones, transport_get_remote_refs() should
1266 * return reliable ref set, we can delay cloning until after
1267 * remote HEAD check.
1268 */
1269 for (ref = refs; ref; ref = ref->next)
brian m. carlsonf4e54d02015-11-10 02:22:20 +00001270 if (is_null_oid(&ref->old_oid)) {
Michael Haggerty5b057952012-02-11 07:20:56 +01001271 complete_refs_before_fetch = 0;
1272 break;
1273 }
1274
Taylor Blauaab179d2020-12-03 13:55:13 -05001275 if (!is_local && !complete_refs_before_fetch) {
Jeff King6aacb7d2021-05-19 07:17:15 -04001276 if (transport_fetch_refs(transport, mapped_refs))
1277 die(_("remote transport reported error"));
Taylor Blauaab179d2020-12-03 13:55:13 -05001278 }
Sverre Rabbelier86ac7512009-01-23 01:07:32 +01001279 }
Jeff King6b58df52021-09-20 15:04:10 -04001280
Jeff King3d8314f2022-07-07 19:57:45 -04001281 remote_head = find_ref_by_name(refs, "HEAD");
1282 remote_head_points_at = guess_remote_head(remote_head, mapped_refs, 0);
1283
1284 if (option_branch) {
1285 our_head_points_at = find_remote_branch(mapped_refs, option_branch);
1286 if (!our_head_points_at)
Ralf Thielowa3552ab2013-10-11 18:49:02 +02001287 die(_("Remote branch %s not found in upstream %s"),
Jeff King3d8314f2022-07-07 19:57:45 -04001288 option_branch, remote_name);
1289 } else if (remote_head_points_at) {
1290 our_head_points_at = remote_head_points_at;
1291 } else if (remote_head) {
Jeff King7a4ee282009-08-26 15:05:08 -04001292 our_head_points_at = NULL;
Jeff King3d8314f2022-07-07 19:57:45 -04001293 } else {
Jeff King6b58df52021-09-20 15:04:10 -04001294 const char *branch;
Jeff King6b58df52021-09-20 15:04:10 -04001295
Jeff King3d8314f2022-07-07 19:57:45 -04001296 if (!mapped_refs) {
1297 warning(_("You appear to have cloned an empty repository."));
1298 option_no_checkout = 1;
1299 }
Jonathan Tan4f37d452021-02-05 12:48:49 -08001300
Jeff King6b58df52021-09-20 15:04:10 -04001301 if (transport_ls_refs_options.unborn_head_target &&
1302 skip_prefix(transport_ls_refs_options.unborn_head_target,
1303 "refs/heads/", &branch)) {
Jeff Kingdaf78982022-07-11 05:21:52 -04001304 unborn_head = xstrdup(transport_ls_refs_options.unborn_head_target);
Jeff King6b58df52021-09-20 15:04:10 -04001305 } else {
1306 branch = git_default_branch_name(0);
Jeff Kingdaf78982022-07-11 05:21:52 -04001307 unborn_head = xstrfmt("refs/heads/%s", branch);
Johannes Schindelin0cc1b472020-06-24 14:46:34 +00001308 }
Jeff King6b58df52021-09-20 15:04:10 -04001309
Jeff Kingcc8fcd12022-07-07 19:59:35 -04001310 /*
1311 * We may have selected a local default branch name "foo",
1312 * and even though the remote's HEAD does not point there,
1313 * it may still have a "foo" branch. If so, set it up so
1314 * that we can follow the usual checkout code later.
1315 *
1316 * Note that for an empty repo we'll already have set
1317 * option_no_checkout above, which would work against us here.
1318 * But for an empty repo, find_remote_branch() can never find
1319 * a match.
1320 */
1321 our_head_points_at = find_remote_branch(mapped_refs, branch);
Sverre Rabbelier86ac7512009-01-23 01:07:32 +01001322 }
Daniel Barkalow8434c2f2008-04-27 13:39:30 -04001323
Ralf Thielow31b808a2012-09-20 20:04:08 +02001324 write_refspec_config(src_ref_prefix, our_head_points_at,
1325 remote_head_points_at, &branch_top);
1326
Jonathan Tan548719f2017-12-08 15:58:46 +00001327 if (filter_options.choice)
Sean Barag75ca3902020-10-01 03:46:15 +00001328 partial_clone_register(remote_name, &filter_options);
Jonathan Tan548719f2017-12-08 15:58:46 +00001329
Nguyễn Thái Ngọc Duy6f48d392012-01-16 16:46:12 +07001330 if (is_local)
1331 clone_local(path, git_dir);
Jonathan Tandccea602022-01-24 10:09:09 -08001332 else if (mapped_refs && complete_refs_before_fetch) {
Jeff King6aacb7d2021-05-19 07:17:15 -04001333 if (transport_fetch_refs(transport, mapped_refs))
1334 die(_("remote transport reported error"));
Taylor Blauaab179d2020-12-03 13:55:13 -05001335 }
Jeff King7a4ee282009-08-26 15:05:08 -04001336
Nguyễn Thái Ngọc Duy960b7d12012-01-16 16:46:11 +07001337 update_remote_refs(refs, mapped_refs, remote_head_points_at,
Jonathan Tan548719f2017-12-08 15:58:46 +00001338 branch_top.buf, reflog_msg.buf, transport,
Jonathan Tan2b984782020-03-20 15:00:45 -07001339 !is_local);
Daniel Barkalow8434c2f2008-04-27 13:39:30 -04001340
Jeff Kingdaf78982022-07-11 05:21:52 -04001341 update_head(our_head_points_at, remote_head, unborn_head, reflog_msg.buf);
Shawn O. Pearce1db4a752008-07-08 04:46:06 +00001342
Jeff King72c5f882016-09-22 01:24:46 -04001343 /*
1344 * We want to show progress for recursive submodule clones iff
1345 * we did so for the main clone. But only the transport knows
1346 * the final decision for this flag, so we need to rescue the value
1347 * before we free the transport.
1348 */
1349 submodule_progress = transport->progress;
1350
Patrick Steinhardt58d4d7f2022-01-07 11:55:47 +01001351 transport_unlock_pack(transport, 0);
Nguyễn Thái Ngọc Duy6f48d392012-01-16 16:46:12 +07001352 transport_disconnect(transport);
Daniel Barkalow8434c2f2008-04-27 13:39:30 -04001353
Johannes Schindelin786b1502015-10-06 15:18:47 +02001354 if (option_dissociate) {
Derrick Stolee2d511cf2019-05-17 11:41:49 -07001355 close_object_store(the_repository->objects);
Junio C Hamanofb1d6da2014-10-14 12:38:52 -07001356 dissociate_from_references();
Johannes Schindelin786b1502015-10-06 15:18:47 +02001357 }
Junio C Hamanofb1d6da2014-10-14 12:38:52 -07001358
Jeff Kingd3b34622013-03-26 18:22:09 -04001359 junk_mode = JUNK_LEAVE_REPO;
Josh Steadmonf05da2b2022-02-04 21:00:49 -08001360 err = checkout(submodule_progress, filter_submodules);
Daniel Barkalow8434c2f2008-04-27 13:39:30 -04001361
Sean Baragde9ed3e2020-10-01 03:46:16 +00001362 free(remote_name);
Daniel Barkalow8434c2f2008-04-27 13:39:30 -04001363 strbuf_release(&reflog_msg);
Miklos Vajnab5ff37a2008-11-21 01:45:01 +01001364 strbuf_release(&branch_top);
1365 strbuf_release(&key);
Andrzej Hunt0c454272021-03-14 18:47:36 +00001366 free_refs(mapped_refs);
1367 free_refs(remote_head_points_at);
Jeff Kingdaf78982022-07-11 05:21:52 -04001368 free(unborn_head);
Andrzej Hunt0c454272021-03-14 18:47:36 +00001369 free(dir);
1370 free(path);
1371 UNLEAK(repo);
Jeff Kingd3b34622013-03-26 18:22:09 -04001372 junk_mode = JUNK_LEAVE_ALL;
Stefan Beller50b67732014-08-10 15:57:56 +02001373
Ævar Arnfjörð Bjarmasonf36d4f82022-02-05 01:08:14 +01001374 transport_ls_refs_options_release(&transport_ls_refs_options);
Jeff Kingdfa7a6c2009-03-03 00:37:51 -05001375 return err;
Daniel Barkalow8434c2f2008-04-27 13:39:30 -04001376}