blob: bbc9bc78f9d5a32ed9d167a79426ba0329bdea95 [file] [log] [blame]
Linus Torvalds8bc9a0c2005-04-07 15:16:10 -07001/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
Linus Torvaldse83c5162005-04-07 15:13:13 -07006#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07007#include "config.h"
Michael Haggertyfb58c8d2015-06-22 16:03:05 +02008#include "refs.h"
Timo Hirvonenc3c88352006-05-19 13:03:57 +03009#include "builtin.h"
Stefan Bellerd807c4a2018-04-10 14:26:18 -070010#include "exec-cmd.h"
Michał Kiedrowicz596f91e2009-07-12 12:24:32 +020011#include "parse-options.h"
Linus Torvaldse83c5162005-04-07 15:13:13 -070012
Junio C Hamanod3af6212005-08-06 12:50:14 -070013#ifndef DEFAULT_GIT_TEMPLATE_DIR
Johannes Sixtd52fd422007-06-11 11:10:47 +020014#define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates"
Junio C Hamanod3af6212005-08-06 12:50:14 -070015#endif
16
Shawn O. Pearcec8697532006-12-30 23:53:55 -050017#ifdef NO_TRUSTABLE_FILEMODE
18#define TEST_FILEMODE 0
19#else
20#define TEST_FILEMODE 1
21#endif
22
brian m. carlson3c9331a2020-02-22 20:17:39 +000023#define GIT_DEFAULT_HASH_ENVIRONMENT "GIT_DEFAULT_HASH"
24
Deskin Miller0a2c7ee2008-10-07 01:37:48 -040025static int init_is_bare_repository = 0;
26static int init_shared_repository = -1;
Steven Drake90b45182010-02-17 12:42:31 +130027static const char *init_db_template_dir;
Deskin Miller0a2c7ee2008-10-07 01:37:48 -040028
Brandon Williamsfa0fcca2018-02-14 10:59:52 -080029static void copy_templates_1(struct strbuf *path, struct strbuf *template_path,
Junio C Hamano8d5afef2005-08-02 16:45:21 -070030 DIR *dir)
31{
Jeff King9c283902015-10-04 23:46:04 -040032 size_t path_baselen = path->len;
Brandon Williamsfa0fcca2018-02-14 10:59:52 -080033 size_t template_baselen = template_path->len;
Junio C Hamano8d5afef2005-08-02 16:45:21 -070034 struct dirent *de;
35
36 /* Note: if ".git/hooks" file exists in the repository being
37 * re-initialized, /etc/core-git/templates/hooks/update would
Heikki Orsilaf18d2442008-09-13 20:18:36 +030038 * cause "git init" to fail here. I think this is sane but
Junio C Hamano8d5afef2005-08-02 16:45:21 -070039 * it means that the set of templates we ship by default, along
40 * with the way the namespace under .git/ is organized, should
41 * be really carefully chosen.
42 */
Jeff King9c283902015-10-04 23:46:04 -040043 safe_create_dir(path->buf, 1);
Junio C Hamano8d5afef2005-08-02 16:45:21 -070044 while ((de = readdir(dir)) != NULL) {
45 struct stat st_git, st_template;
Junio C Hamano8d5afef2005-08-02 16:45:21 -070046 int exists = 0;
47
Jeff King9c283902015-10-04 23:46:04 -040048 strbuf_setlen(path, path_baselen);
Brandon Williamsfa0fcca2018-02-14 10:59:52 -080049 strbuf_setlen(template_path, template_baselen);
Jeff King9c283902015-10-04 23:46:04 -040050
Junio C Hamano8d5afef2005-08-02 16:45:21 -070051 if (de->d_name[0] == '.')
52 continue;
Jeff King9c283902015-10-04 23:46:04 -040053 strbuf_addstr(path, de->d_name);
Brandon Williamsfa0fcca2018-02-14 10:59:52 -080054 strbuf_addstr(template_path, de->d_name);
Jeff King9c283902015-10-04 23:46:04 -040055 if (lstat(path->buf, &st_git)) {
Junio C Hamano8d5afef2005-08-02 16:45:21 -070056 if (errno != ENOENT)
Jeff King9c283902015-10-04 23:46:04 -040057 die_errno(_("cannot stat '%s'"), path->buf);
Junio C Hamano8d5afef2005-08-02 16:45:21 -070058 }
59 else
60 exists = 1;
61
Brandon Williamsfa0fcca2018-02-14 10:59:52 -080062 if (lstat(template_path->buf, &st_template))
63 die_errno(_("cannot stat template '%s'"), template_path->buf);
Junio C Hamano8d5afef2005-08-02 16:45:21 -070064
65 if (S_ISDIR(st_template.st_mode)) {
Brandon Williamsfa0fcca2018-02-14 10:59:52 -080066 DIR *subdir = opendir(template_path->buf);
Junio C Hamano8d5afef2005-08-02 16:45:21 -070067 if (!subdir)
Brandon Williamsfa0fcca2018-02-14 10:59:52 -080068 die_errno(_("cannot opendir '%s'"), template_path->buf);
Jeff King9c283902015-10-04 23:46:04 -040069 strbuf_addch(path, '/');
Brandon Williamsfa0fcca2018-02-14 10:59:52 -080070 strbuf_addch(template_path, '/');
71 copy_templates_1(path, template_path, subdir);
Junio C Hamano8d5afef2005-08-02 16:45:21 -070072 closedir(subdir);
73 }
74 else if (exists)
75 continue;
76 else if (S_ISLNK(st_template.st_mode)) {
Jeff King9c283902015-10-04 23:46:04 -040077 struct strbuf lnk = STRBUF_INIT;
Jeff King765b4962018-07-24 06:51:39 -040078 if (strbuf_readlink(&lnk, template_path->buf,
79 st_template.st_size) < 0)
Brandon Williamsfa0fcca2018-02-14 10:59:52 -080080 die_errno(_("cannot readlink '%s'"), template_path->buf);
Jeff King9c283902015-10-04 23:46:04 -040081 if (symlink(lnk.buf, path->buf))
82 die_errno(_("cannot symlink '%s' '%s'"),
83 lnk.buf, path->buf);
84 strbuf_release(&lnk);
Junio C Hamano8d5afef2005-08-02 16:45:21 -070085 }
86 else if (S_ISREG(st_template.st_mode)) {
Brandon Williamsfa0fcca2018-02-14 10:59:52 -080087 if (copy_file(path->buf, template_path->buf, st_template.st_mode))
Jeff King9c283902015-10-04 23:46:04 -040088 die_errno(_("cannot copy '%s' to '%s'"),
Brandon Williamsfa0fcca2018-02-14 10:59:52 -080089 template_path->buf, path->buf);
Junio C Hamano8d5afef2005-08-02 16:45:21 -070090 }
91 else
Brandon Williamsfa0fcca2018-02-14 10:59:52 -080092 error(_("ignoring template %s"), template_path->buf);
Junio C Hamano8d5afef2005-08-02 16:45:21 -070093 }
94}
95
Daniel Barkalowf225aeb2008-04-27 13:39:27 -040096static void copy_templates(const char *template_dir)
Junio C Hamano8d5afef2005-08-02 16:45:21 -070097{
Jeff King9c283902015-10-04 23:46:04 -040098 struct strbuf path = STRBUF_INIT;
99 struct strbuf template_path = STRBUF_INIT;
100 size_t template_len;
Martin Ågrene8805af2019-02-28 21:36:28 +0100101 struct repository_format template_format = REPOSITORY_FORMAT_INIT;
Jeff King94ce1672016-03-11 17:37:11 -0500102 struct strbuf err = STRBUF_INIT;
Junio C Hamano8d5afef2005-08-02 16:45:21 -0700103 DIR *dir;
Junio C Hamano59362e52014-11-24 11:33:54 -0800104 char *to_free = NULL;
Junio C Hamano8d5afef2005-08-02 16:45:21 -0700105
Johannes Sixta47d1812007-11-13 21:05:04 +0100106 if (!template_dir)
Junio C Hamanod4ebc362006-12-19 01:28:15 -0800107 template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT);
Steffen Prohaska2de9de52008-07-13 22:31:18 +0200108 if (!template_dir)
Steven Drake90b45182010-02-17 12:42:31 +1300109 template_dir = init_db_template_dir;
110 if (!template_dir)
Junio C Hamano59362e52014-11-24 11:33:54 -0800111 template_dir = to_free = system_path(DEFAULT_GIT_TEMPLATE_DIR);
112 if (!template_dir[0]) {
113 free(to_free);
Jeff King172035f2008-07-28 02:02:04 -0400114 return;
Junio C Hamano59362e52014-11-24 11:33:54 -0800115 }
Jeff King9c283902015-10-04 23:46:04 -0400116
117 strbuf_addstr(&template_path, template_dir);
118 strbuf_complete(&template_path, '/');
119 template_len = template_path.len;
120
121 dir = opendir(template_path.buf);
Junio C Hamanod3af6212005-08-06 12:50:14 -0700122 if (!dir) {
Robert P. J. Day44f560f2018-05-29 08:14:35 -0400123 warning(_("templates not found in %s"), template_dir);
Junio C Hamano59362e52014-11-24 11:33:54 -0800124 goto free_return;
Junio C Hamanod3af6212005-08-06 12:50:14 -0700125 }
126
Junio C Hamano4f629532005-11-25 16:03:56 -0800127 /* Make sure that template is from the correct vintage */
Jeff King9c283902015-10-04 23:46:04 -0400128 strbuf_addstr(&template_path, "config");
Jeff King94ce1672016-03-11 17:37:11 -0500129 read_repository_format(&template_format, template_path.buf);
Jeff King9c283902015-10-04 23:46:04 -0400130 strbuf_setlen(&template_path, template_len);
Junio C Hamano4f629532005-11-25 16:03:56 -0800131
Jeff King94ce1672016-03-11 17:37:11 -0500132 /*
133 * No mention of version at all is OK, but anything else should be
134 * verified.
135 */
136 if (template_format.version >= 0 &&
137 verify_repository_format(&template_format, &err) < 0) {
138 warning(_("not copying templates from '%s': %s"),
139 template_dir, err.buf);
140 strbuf_release(&err);
Junio C Hamano59362e52014-11-24 11:33:54 -0800141 goto close_free_return;
Junio C Hamano4f629532005-11-25 16:03:56 -0800142 }
143
Nguyễn Thái Ngọc Duyfe9aa0b2016-09-25 10:14:36 +0700144 strbuf_addstr(&path, get_git_common_dir());
Jeff King9c283902015-10-04 23:46:04 -0400145 strbuf_complete(&path, '/');
146 copy_templates_1(&path, &template_path, dir);
Junio C Hamano59362e52014-11-24 11:33:54 -0800147close_free_return:
Junio C Hamano8d5afef2005-08-02 16:45:21 -0700148 closedir(dir);
Junio C Hamano59362e52014-11-24 11:33:54 -0800149free_return:
150 free(to_free);
Jeff King9c283902015-10-04 23:46:04 -0400151 strbuf_release(&path);
152 strbuf_release(&template_path);
Martin Ågrene8805af2019-02-28 21:36:28 +0100153 clear_repository_format(&template_format);
Junio C Hamano8d5afef2005-08-02 16:45:21 -0700154}
155
Steven Drake90b45182010-02-17 12:42:31 +1300156static int git_init_db_config(const char *k, const char *v, void *cb)
157{
Steven Drake90b45182010-02-17 12:42:31 +1300158 if (!strcmp(k, "init.templatedir"))
159 return git_config_pathname(&init_db_template_dir, k, v);
160
Johannes Schindelin28785332019-03-11 13:10:58 -0700161 if (starts_with(k, "core."))
162 return platform_core_config(k, v, cb);
163
Steven Drake90b45182010-02-17 12:42:31 +1300164 return 0;
165}
166
Jeff King84ccad82015-04-02 14:37:40 -0400167/*
168 * If the git_dir is not directly inside the working tree, then git will not
169 * find it by default, and we need to set the worktree explicitly.
170 */
171static int needs_work_tree_config(const char *git_dir, const char *work_tree)
172{
173 if (!strcmp(work_tree, "/") && !strcmp(git_dir, "/.git"))
174 return 0;
175 if (skip_prefix(git_dir, work_tree, &git_dir) &&
176 !strcmp(git_dir, "/.git"))
177 return 0;
178 return 1;
179}
180
brian m. carlsonefa7ae32020-02-22 20:17:40 +0000181void initialize_repository_version(int hash_algo)
182{
183 char repo_version_string[10];
184 int repo_version = GIT_REPO_VERSION;
185
brian m. carlsonefa7ae32020-02-22 20:17:40 +0000186 if (hash_algo != GIT_HASH_SHA1)
187 repo_version = GIT_REPO_VERSION_READ;
188
189 /* This forces creation of new config file */
190 xsnprintf(repo_version_string, sizeof(repo_version_string),
191 "%d", repo_version);
192 git_config_set("core.repositoryformatversion", repo_version_string);
193
194 if (hash_algo != GIT_HASH_SHA1)
195 git_config_set("extensions.objectformat",
196 hash_algos[hash_algo].name);
197}
198
Nguyễn Thái Ngọc Duy6311cfa2016-09-25 10:14:39 +0700199static int create_default_files(const char *template_path,
brian m. carlson8b8f7182020-02-22 20:17:38 +0000200 const char *original_git_dir,
Johannes Schindelin32ba12d2020-06-24 14:46:32 +0000201 const char *initial_branch,
brian m. carlson8b8f7182020-02-22 20:17:38 +0000202 const struct repository_format *fmt)
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700203{
Junio C Hamano4f629532005-11-25 16:03:56 -0800204 struct stat st1;
Jeff King9c283902015-10-04 23:46:04 -0400205 struct strbuf buf = STRBUF_INIT;
206 char *path;
Johannes Schindelin5cc8f372008-03-24 16:14:52 +0100207 char junk[2];
Shawn O. Pearceef0a89a2006-12-15 00:44:58 -0500208 int reinit;
Shawn O. Pearcec8697532006-12-30 23:53:55 -0500209 int filemode;
David Turner6fb5acf2016-09-04 18:08:41 +0200210 struct strbuf err = STRBUF_INIT;
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700211
Steven Drake90b45182010-02-17 12:42:31 +1300212 /* Just look for `init.templatedir` */
Johannes Schindelin28785332019-03-11 13:10:58 -0700213 init_db_template_dir = NULL; /* re-set in case it was set before */
Steven Drake90b45182010-02-17 12:42:31 +1300214 git_config(git_init_db_config, NULL);
215
Jeff King7c0a8422016-09-12 20:24:19 -0700216 /*
217 * First copy the templates -- we might have the default
Junio C Hamano4f629532005-11-25 16:03:56 -0800218 * config file there, in which case we would want to read
219 * from it after installing.
Jeff King45439262016-09-12 20:24:23 -0700220 *
221 * Before reading that config, we also need to clear out any cached
222 * values (since we've just potentially changed what's available on
223 * disk).
Junio C Hamano4f629532005-11-25 16:03:56 -0800224 */
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400225 copy_templates(template_path);
Jeff King45439262016-09-12 20:24:23 -0700226 git_config_clear();
227 reset_shared_repository();
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100228 git_config(git_default_config, NULL);
Junio C Hamano5a688fe2009-03-25 16:19:36 -0700229
Jeff King7c0a8422016-09-12 20:24:19 -0700230 /*
231 * We must make sure command-line options continue to override any
232 * values we might have just re-read from the config.
233 */
234 is_bare_repository_cfg = init_is_bare_repository;
Deskin Miller0a2c7ee2008-10-07 01:37:48 -0400235 if (init_shared_repository != -1)
Jeff King7875acb2016-03-11 17:36:49 -0500236 set_shared_repository(init_shared_repository);
Junio C Hamano4f629532005-11-25 16:03:56 -0800237
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700238 /*
Junio C Hamano138086a2006-06-09 22:07:23 -0700239 * We would have created the above under user's umask -- under
240 * shared-repository settings, we would need to fix them up.
241 */
Jeff King7875acb2016-03-11 17:36:49 -0500242 if (get_shared_repository()) {
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400243 adjust_shared_perm(get_git_dir());
Junio C Hamano138086a2006-06-09 22:07:23 -0700244 }
245
246 /*
David Turner6fb5acf2016-09-04 18:08:41 +0200247 * We need to create a "refs" dir in any case so that older
248 * versions of git can tell that this is a repository.
249 */
250 safe_create_dir(git_path("refs"), 1);
251 adjust_shared_perm(git_path("refs"));
252
253 if (refs_init_db(&err))
254 die("failed to set up refs db: %s", err.buf);
255
256 /*
Johannes Schindelin32ba12d2020-06-24 14:46:32 +0000257 * Point the HEAD symref to the initial branch with if HEAD does
258 * not yet exist.
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700259 */
Jeff King9c283902015-10-04 23:46:04 -0400260 path = git_path_buf(&buf, "HEAD");
Johannes Schindelin5cc8f372008-03-24 16:14:52 +0100261 reinit = (!access(path, R_OK)
262 || readlink(path, junk, sizeof(junk)-1) != -1);
Shawn O. Pearceef0a89a2006-12-15 00:44:58 -0500263 if (!reinit) {
Johannes Schindelin32ba12d2020-06-24 14:46:32 +0000264 char *ref;
265
266 if (!initial_branch)
Don Goodman-Wilson8747ebb2020-06-24 14:46:33 +0000267 initial_branch = git_default_branch_name();
Johannes Schindelin32ba12d2020-06-24 14:46:32 +0000268
269 ref = xstrfmt("refs/heads/%s", initial_branch);
270 if (check_refname_format(ref, 0) < 0)
271 die(_("invalid initial branch name: '%s'"),
272 initial_branch);
273
274 if (create_symref("HEAD", ref, NULL) < 0)
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700275 exit(1);
Johannes Schindelin32ba12d2020-06-24 14:46:32 +0000276 free(ref);
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700277 }
Junio C Hamano4f629532005-11-25 16:03:56 -0800278
brian m. carlsonefa7ae32020-02-22 20:17:40 +0000279 initialize_repository_version(fmt->hash_algo);
Junio C Hamano4f629532005-11-25 16:03:56 -0800280
Junio C Hamano4f629532005-11-25 16:03:56 -0800281 /* Check filemode trustability */
Jeff King9c283902015-10-04 23:46:04 -0400282 path = git_path_buf(&buf, "config");
Shawn O. Pearcec8697532006-12-30 23:53:55 -0500283 filemode = TEST_FILEMODE;
284 if (TEST_FILEMODE && !lstat(path, &st1)) {
Junio C Hamano4f629532005-11-25 16:03:56 -0800285 struct stat st2;
Shawn O. Pearcec8697532006-12-30 23:53:55 -0500286 filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) &&
Junio C Hamano4f629532005-11-25 16:03:56 -0800287 !lstat(path, &st2) &&
Michael Haggerty1f32ecf2014-11-18 14:50:24 +0100288 st1.st_mode != st2.st_mode &&
289 !chmod(path, st1.st_mode));
Torsten Bögershausenc7bf68d2014-11-21 10:34:54 +0100290 if (filemode && !reinit && (st1.st_mode & S_IXUSR))
291 filemode = 0;
Johannes Schindeline24317b2005-10-26 01:43:03 +0200292 }
Patrick Steinhardt3d180642016-02-22 12:23:36 +0100293 git_config_set("core.filemode", filemode ? "true" : "false");
Shawn O. Pearceef0a89a2006-12-15 00:44:58 -0500294
Johannes Schindeline90fdc32007-08-01 01:30:14 +0100295 if (is_bare_repository())
Patrick Steinhardt3d180642016-02-22 12:23:36 +0100296 git_config_set("core.bare", "true");
Junio C Hamano7d1864c2007-01-07 02:00:28 -0800297 else {
Johannes Schindeline90fdc32007-08-01 01:30:14 +0100298 const char *work_tree = get_git_work_tree();
Patrick Steinhardt3d180642016-02-22 12:23:36 +0100299 git_config_set("core.bare", "false");
Alex Riesen196055c2007-01-23 16:51:18 +0100300 /* allow template config file to override the default */
Cornelius Weig341fb282017-01-27 11:09:47 +0100301 if (log_all_ref_updates == LOG_REFS_UNSET)
Patrick Steinhardt3d180642016-02-22 12:23:36 +0100302 git_config_set("core.logallrefupdates", "true");
Nguyễn Thái Ngọc Duy6311cfa2016-09-25 10:14:39 +0700303 if (needs_work_tree_config(original_git_dir, work_tree))
Patrick Steinhardt3d180642016-02-22 12:23:36 +0100304 git_config_set("core.worktree", work_tree);
Junio C Hamano7d1864c2007-01-07 02:00:28 -0800305 }
Junio C Hamano75d24492007-08-31 00:25:04 -0700306
Junio C Hamano75d24492007-08-31 00:25:04 -0700307 if (!reinit) {
Dmitry Potapov24554062008-05-11 18:16:39 +0200308 /* Check if symlink is supported in the work tree */
Jeff King9c283902015-10-04 23:46:04 -0400309 path = git_path_buf(&buf, "tXXXXXX");
Junio C Hamano75d24492007-08-31 00:25:04 -0700310 if (!close(xmkstemp(path)) &&
311 !unlink(path) &&
312 !symlink("testing", path) &&
313 !lstat(path, &st1) &&
314 S_ISLNK(st1.st_mode))
315 unlink(path); /* good */
316 else
Patrick Steinhardt3d180642016-02-22 12:23:36 +0100317 git_config_set("core.symlinks", "false");
Dmitry Potapov24554062008-05-11 18:16:39 +0200318
319 /* Check if the filesystem is case-insensitive */
Jeff King9c283902015-10-04 23:46:04 -0400320 path = git_path_buf(&buf, "CoNfIg");
Dmitry Potapov24554062008-05-11 18:16:39 +0200321 if (!access(path, F_OK))
Patrick Steinhardt3d180642016-02-22 12:23:36 +0100322 git_config_set("core.ignorecase", "true");
Jeff Kingfdf72962015-10-04 23:45:26 -0400323 probe_utf8_pathname_composition();
Junio C Hamano75d24492007-08-31 00:25:04 -0700324 }
325
Jeff King9c283902015-10-04 23:46:04 -0400326 strbuf_release(&buf);
Shawn O. Pearceef0a89a2006-12-15 00:44:58 -0500327 return reinit;
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700328}
329
Jonathan Nieder91739122010-10-03 23:34:27 -0500330static void create_object_directory(void)
331{
Jeff King9c283902015-10-04 23:46:04 -0400332 struct strbuf path = STRBUF_INIT;
333 size_t baselen;
Jonathan Nieder91739122010-10-03 23:34:27 -0500334
Jeff King9c283902015-10-04 23:46:04 -0400335 strbuf_addstr(&path, get_object_directory());
336 baselen = path.len;
Jonathan Nieder91739122010-10-03 23:34:27 -0500337
Jeff King9c283902015-10-04 23:46:04 -0400338 safe_create_dir(path.buf, 1);
Jonathan Nieder91739122010-10-03 23:34:27 -0500339
Jeff King9c283902015-10-04 23:46:04 -0400340 strbuf_setlen(&path, baselen);
341 strbuf_addstr(&path, "/pack");
342 safe_create_dir(path.buf, 1);
343
344 strbuf_setlen(&path, baselen);
345 strbuf_addstr(&path, "/info");
346 safe_create_dir(path.buf, 1);
347
348 strbuf_release(&path);
Jonathan Nieder91739122010-10-03 23:34:27 -0500349}
350
Nguyễn Thái Ngọc Duy822d9402016-09-25 10:14:40 +0700351static void separate_git_dir(const char *git_dir, const char *git_link)
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700352{
353 struct stat st;
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700354
355 if (!stat(git_link, &st)) {
356 const char *src;
357
358 if (S_ISREG(st.st_mode))
Junio C Hamano13d6ec92011-08-22 14:04:56 -0700359 src = read_gitfile(git_link);
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700360 else if (S_ISDIR(st.st_mode))
361 src = git_link;
362 else
Ævar Arnfjörð Bjarmason97f261b2011-12-20 23:27:41 +0000363 die(_("unable to handle file type %d"), (int)st.st_mode);
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700364
365 if (rename(src, git_dir))
Ævar Arnfjörð Bjarmason2c050e02011-04-10 19:34:02 +0000366 die_errno(_("unable to move %s to %s"), src, git_dir);
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700367 }
368
Junio C Hamano1f76a102015-08-24 13:20:39 -0700369 write_file(git_link, "gitdir: %s", git_dir);
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700370}
371
brian m. carlson8b8f7182020-02-22 20:17:38 +0000372static void validate_hash_algorithm(struct repository_format *repo_fmt, int hash)
373{
brian m. carlson3c9331a2020-02-22 20:17:39 +0000374 const char *env = getenv(GIT_DEFAULT_HASH_ENVIRONMENT);
brian m. carlson8b8f7182020-02-22 20:17:38 +0000375 /*
376 * If we already have an initialized repo, don't allow the user to
377 * specify a different algorithm, as that could cause corruption.
378 * Otherwise, if the user has specified one on the command line, use it.
379 */
380 if (repo_fmt->version >= 0 && hash != GIT_HASH_UNKNOWN && hash != repo_fmt->hash_algo)
381 die(_("attempt to reinitialize repository with different hash"));
382 else if (hash != GIT_HASH_UNKNOWN)
383 repo_fmt->hash_algo = hash;
brian m. carlson3c9331a2020-02-22 20:17:39 +0000384 else if (env) {
385 int env_algo = hash_algo_by_name(env);
386 if (env_algo == GIT_HASH_UNKNOWN)
387 die(_("unknown hash algorithm '%s'"), env);
388 repo_fmt->hash_algo = env_algo;
389 }
brian m. carlson8b8f7182020-02-22 20:17:38 +0000390}
391
Nguyễn Thái Ngọc Duy33158702016-09-25 10:14:37 +0700392int init_db(const char *git_dir, const char *real_git_dir,
Johannes Schindelin32ba12d2020-06-24 14:46:32 +0000393 const char *template_dir, int hash, const char *initial_branch,
394 unsigned int flags)
Junio C Hamano6adcca32007-08-27 00:58:06 -0700395{
Jonathan Nieder91739122010-10-03 23:34:27 -0500396 int reinit;
Nguyễn Thái Ngọc Duy1bd19072016-09-25 10:14:38 +0700397 int exist_ok = flags & INIT_DB_EXIST_OK;
Johannes Schindelince83ead2017-03-08 16:43:40 +0100398 char *original_git_dir = real_pathdup(git_dir, 1);
brian m. carlson8b8f7182020-02-22 20:17:38 +0000399 struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT;
Linus Torvaldse83c5162005-04-07 15:13:13 -0700400
Nguyễn Thái Ngọc Duy1bd19072016-09-25 10:14:38 +0700401 if (real_git_dir) {
402 struct stat st;
Nguyễn Thái Ngọc Duy33158702016-09-25 10:14:37 +0700403
Nguyễn Thái Ngọc Duy1bd19072016-09-25 10:14:38 +0700404 if (!exist_ok && !stat(git_dir, &st))
405 die(_("%s already exists"), git_dir);
406
407 if (!exist_ok && !stat(real_git_dir, &st))
408 die(_("%s already exists"), real_git_dir);
409
Alexandr Miloslavskiy0915a5b2020-03-06 19:03:13 +0000410 set_git_dir(real_git_dir, 1);
Nguyễn Thái Ngọc Duy822d9402016-09-25 10:14:40 +0700411 git_dir = get_git_dir();
412 separate_git_dir(git_dir, original_git_dir);
Nguyễn Thái Ngọc Duy1bd19072016-09-25 10:14:38 +0700413 }
414 else {
Alexandr Miloslavskiy0915a5b2020-03-06 19:03:13 +0000415 set_git_dir(git_dir, 1);
Nguyễn Thái Ngọc Duy822d9402016-09-25 10:14:40 +0700416 git_dir = get_git_dir();
Nguyễn Thái Ngọc Duy1bd19072016-09-25 10:14:38 +0700417 }
418 startup_info->have_repository = 1;
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700419
Johannes Schindelin28785332019-03-11 13:10:58 -0700420 /* Just look for `core.hidedotfiles` */
421 git_config(git_init_db_config, NULL);
422
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700423 safe_create_dir(git_dir, 0);
Junio C Hamano4f629532005-11-25 16:03:56 -0800424
Deskin Miller0a2c7ee2008-10-07 01:37:48 -0400425 init_is_bare_repository = is_bare_repository();
426
Junio C Hamano4f629532005-11-25 16:03:56 -0800427 /* Check to see if the repository version is right.
428 * Note that a newly created repository does not have
429 * config file, so this will not fail. What we are catching
430 * is an attempt to reinitialize new repository with an old tool.
431 */
brian m. carlson8b8f7182020-02-22 20:17:38 +0000432 check_repository_format(&repo_fmt);
Junio C Hamano4f629532005-11-25 16:03:56 -0800433
brian m. carlson8b8f7182020-02-22 20:17:38 +0000434 validate_hash_algorithm(&repo_fmt, hash);
435
Johannes Schindelin32ba12d2020-06-24 14:46:32 +0000436 reinit = create_default_files(template_dir, original_git_dir,
437 initial_branch, &repo_fmt);
438 if (reinit && initial_branch)
439 warning(_("re-init: ignored --initial-branch=%s"),
440 initial_branch);
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700441
Jonathan Nieder91739122010-10-03 23:34:27 -0500442 create_object_directory();
Johannes Schindelinaf6e2772005-12-22 23:19:37 +0100443
Jeff King7875acb2016-03-11 17:36:49 -0500444 if (get_shared_repository()) {
Junio C Hamano94df2502006-06-09 23:09:49 -0700445 char buf[10];
446 /* We do not spell "group" and such, so that
447 * the configuration can be read by older version
Heikki Orsila06cbe852008-04-16 11:34:24 +0300448 * of git. Note, we use octal numbers for new share modes,
449 * and compatibility values for PERM_GROUP and
450 * PERM_EVERYBODY.
Junio C Hamano94df2502006-06-09 23:09:49 -0700451 */
Jeff King7875acb2016-03-11 17:36:49 -0500452 if (get_shared_repository() < 0)
Junio C Hamano5a688fe2009-03-25 16:19:36 -0700453 /* force to the mode value */
Jeff King7875acb2016-03-11 17:36:49 -0500454 xsnprintf(buf, sizeof(buf), "0%o", -get_shared_repository());
455 else if (get_shared_repository() == PERM_GROUP)
Jeff King5096d492015-09-24 17:06:08 -0400456 xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_GROUP);
Jeff King7875acb2016-03-11 17:36:49 -0500457 else if (get_shared_repository() == PERM_EVERYBODY)
Jeff King5096d492015-09-24 17:06:08 -0400458 xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_EVERYBODY);
Heikki Orsila06cbe852008-04-16 11:34:24 +0300459 else
Johannes Schindelin033abf92018-05-02 11:38:39 +0200460 BUG("invalid value for shared_repository");
Patrick Steinhardt3d180642016-02-22 12:23:36 +0100461 git_config_set("core.sharedrepository", buf);
462 git_config_set("receive.denyNonFastforwards", "true");
Junio C Hamano94df2502006-06-09 23:09:49 -0700463 }
Johannes Schindelinaf6e2772005-12-22 23:19:37 +0100464
Nguyễn Thái Ngọc Duyd06f15d2010-02-14 22:44:42 +0700465 if (!(flags & INIT_DB_QUIET)) {
Nguyễn Thái Ngọc Duyd06f15d2010-02-14 22:44:42 +0700466 int len = strlen(git_dir);
Ævar Arnfjörð Bjarmason3e5dd7e2011-02-22 23:41:25 +0000467
Vasco Almeidac30364d2016-06-17 21:54:11 +0000468 if (reinit)
469 printf(get_shared_repository()
470 ? _("Reinitialized existing shared Git repository in %s%s\n")
471 : _("Reinitialized existing Git repository in %s%s\n"),
472 git_dir, len && git_dir[len-1] != '/' ? "/" : "");
473 else
474 printf(get_shared_repository()
475 ? _("Initialized empty shared Git repository in %s%s\n")
476 : _("Initialized empty Git repository in %s%s\n"),
477 git_dir, len && git_dir[len-1] != '/' ? "/" : "");
Nguyễn Thái Ngọc Duyd06f15d2010-02-14 22:44:42 +0700478 }
Shawn O. Pearceef0a89a2006-12-15 00:44:58 -0500479
Nguyễn Thái Ngọc Duy6311cfa2016-09-25 10:14:39 +0700480 free(original_git_dir);
Linus Torvaldse83c5162005-04-07 15:13:13 -0700481 return 0;
482}
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400483
484static int guess_repository_type(const char *git_dir)
485{
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400486 const char *slash;
René Scharfe56b9f6e2014-07-28 20:30:39 +0200487 char *cwd;
488 int cwd_is_git_dir;
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400489
490 /*
491 * "GIT_DIR=. git init" is always bare.
492 * "GIT_DIR=`pwd` git init" too.
493 */
494 if (!strcmp(".", git_dir))
495 return 1;
René Scharfe56b9f6e2014-07-28 20:30:39 +0200496 cwd = xgetcwd();
497 cwd_is_git_dir = !strcmp(git_dir, cwd);
498 free(cwd);
499 if (cwd_is_git_dir)
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400500 return 1;
501 /*
502 * "GIT_DIR=.git or GIT_DIR=something/.git is usually not.
503 */
504 if (!strcmp(git_dir, ".git"))
505 return 0;
506 slash = strrchr(git_dir, '/');
507 if (slash && !strcmp(slash, "/.git"))
508 return 0;
509
510 /*
511 * Otherwise it is often bare. At this point
512 * we are just guessing.
513 */
514 return 1;
515}
516
Michał Kiedrowicz596f91e2009-07-12 12:24:32 +0200517static int shared_callback(const struct option *opt, const char *arg, int unset)
518{
Jeff King517fe802018-11-05 01:45:42 -0500519 BUG_ON_OPT_NEG(unset);
Michał Kiedrowicz596f91e2009-07-12 12:24:32 +0200520 *((int *) opt->value) = (arg) ? git_config_perm("arg", arg) : PERM_GROUP;
521 return 0;
522}
523
524static const char *const init_db_usage[] = {
Alex Henrie9c9b4f22015-01-13 00:44:47 -0700525 N_("git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]] [<directory>]"),
Michał Kiedrowicz596f91e2009-07-12 12:24:32 +0200526 NULL
527};
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400528
529/*
530 * If you want to, you can share the DB area with any number of branches.
531 * That has advantages: you can save space by sharing all the SHA1 objects.
532 * On the other hand, it might just make lookup slower and messier. You
533 * be the judge. The default case is to have one DB per managed directory.
534 */
535int cmd_init_db(int argc, const char **argv, const char *prefix)
536{
537 const char *git_dir;
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700538 const char *real_git_dir = NULL;
Nguyễn Thái Ngọc Duy83518362010-11-26 22:32:40 +0700539 const char *work_tree;
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400540 const char *template_dir = NULL;
541 unsigned int flags = 0;
brian m. carlson8b8f7182020-02-22 20:17:38 +0000542 const char *object_format = NULL;
Johannes Schindelin32ba12d2020-06-24 14:46:32 +0000543 const char *initial_branch = NULL;
brian m. carlson8b8f7182020-02-22 20:17:38 +0000544 int hash_algo = GIT_HASH_UNKNOWN;
Michał Kiedrowicz596f91e2009-07-12 12:24:32 +0200545 const struct option init_db_options[] = {
Nguyễn Thái Ngọc Duyce4a5e52012-08-20 19:32:18 +0700546 OPT_STRING(0, "template", &template_dir, N_("template-directory"),
547 N_("directory from which templates will be used")),
Michał Kiedrowicz596f91e2009-07-12 12:24:32 +0200548 OPT_SET_INT(0, "bare", &is_bare_repository_cfg,
Nguyễn Thái Ngọc Duyce4a5e52012-08-20 19:32:18 +0700549 N_("create a bare repository"), 1),
Michał Kiedrowicz596f91e2009-07-12 12:24:32 +0200550 { OPTION_CALLBACK, 0, "shared", &init_shared_repository,
Nguyễn Thái Ngọc Duyce4a5e52012-08-20 19:32:18 +0700551 N_("permissions"),
552 N_("specify that the git repository is to be shared amongst several users"),
Michał Kiedrowicz596f91e2009-07-12 12:24:32 +0200553 PARSE_OPT_OPTARG | PARSE_OPT_NONEG, shared_callback, 0},
Nguyễn Thái Ngọc Duyce4a5e52012-08-20 19:32:18 +0700554 OPT_BIT('q', "quiet", &flags, N_("be quiet"), INIT_DB_QUIET),
555 OPT_STRING(0, "separate-git-dir", &real_git_dir, N_("gitdir"),
556 N_("separate git dir from working tree")),
Johannes Schindelin32ba12d2020-06-24 14:46:32 +0000557 OPT_STRING('b', "initial-branch", &initial_branch, N_("name"),
558 N_("override the name of the initial branch")),
brian m. carlson8b8f7182020-02-22 20:17:38 +0000559 OPT_STRING(0, "object-format", &object_format, N_("hash"),
560 N_("specify the hash algorithm to use")),
Michał Kiedrowicz596f91e2009-07-12 12:24:32 +0200561 OPT_END()
562 };
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400563
Junio C Hamano0397ff22009-08-05 12:39:33 -0700564 argc = parse_options(argc, argv, prefix, init_db_options, init_db_usage, 0);
Michał Kiedrowicz596f91e2009-07-12 12:24:32 +0200565
Eric Sunshineccf236a2020-08-09 18:53:16 -0400566 if (real_git_dir && is_bare_repository_cfg == 1)
567 die(_("--separate-git-dir and --bare are mutually exclusive"));
568
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700569 if (real_git_dir && !is_absolute_path(real_git_dir))
Johannes Schindelince83ead2017-03-08 16:43:40 +0100570 real_git_dir = real_pathdup(real_git_dir, 1);
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700571
Nguyễn Thái Ngọc Duye1df7fe2019-05-10 17:46:57 +0700572 if (template_dir && *template_dir && !is_absolute_path(template_dir))
573 template_dir = absolute_pathdup(template_dir);
574
Junio C Hamano0397ff22009-08-05 12:39:33 -0700575 if (argc == 1) {
Nanako Shiraishi53d48882009-07-25 06:59:28 +0900576 int mkdir_tried = 0;
577 retry:
Junio C Hamano0397ff22009-08-05 12:39:33 -0700578 if (chdir(argv[0]) < 0) {
Nanako Shiraishi53d48882009-07-25 06:59:28 +0900579 if (!mkdir_tried) {
580 int saved;
581 /*
582 * At this point we haven't read any configuration,
583 * and we know shared_repository should always be 0;
584 * but just in case we play safe.
585 */
Jeff King7875acb2016-03-11 17:36:49 -0500586 saved = get_shared_repository();
587 set_shared_repository(0);
Junio C Hamano0397ff22009-08-05 12:39:33 -0700588 switch (safe_create_leading_directories_const(argv[0])) {
Michael Haggertyf3565c02014-01-06 14:45:26 +0100589 case SCLD_OK:
590 case SCLD_PERMS:
591 break;
Michael Haggerty0be05212014-01-06 14:45:25 +0100592 case SCLD_EXISTS:
Nanako Shiraishi53d48882009-07-25 06:59:28 +0900593 errno = EEXIST;
594 /* fallthru */
Nanako Shiraishi53d48882009-07-25 06:59:28 +0900595 default:
Michael Haggertyf3565c02014-01-06 14:45:26 +0100596 die_errno(_("cannot mkdir %s"), argv[0]);
Nanako Shiraishi53d48882009-07-25 06:59:28 +0900597 break;
598 }
Jeff King7875acb2016-03-11 17:36:49 -0500599 set_shared_repository(saved);
Junio C Hamano0397ff22009-08-05 12:39:33 -0700600 if (mkdir(argv[0], 0777) < 0)
Ævar Arnfjörð Bjarmason33e92e42011-02-22 23:41:24 +0000601 die_errno(_("cannot mkdir %s"), argv[0]);
Nanako Shiraishi53d48882009-07-25 06:59:28 +0900602 mkdir_tried = 1;
603 goto retry;
604 }
Ævar Arnfjörð Bjarmason33e92e42011-02-22 23:41:24 +0000605 die_errno(_("cannot chdir to %s"), argv[0]);
Nanako Shiraishi53d48882009-07-25 06:59:28 +0900606 }
Junio C Hamano0397ff22009-08-05 12:39:33 -0700607 } else if (0 < argc) {
608 usage(init_db_usage[0]);
Nanako Shiraishi53d48882009-07-25 06:59:28 +0900609 }
Junio C Hamano0397ff22009-08-05 12:39:33 -0700610 if (is_bare_repository_cfg == 1) {
René Scharfe4d3ab442014-07-28 20:31:57 +0200611 char *cwd = xgetcwd();
612 setenv(GIT_DIR_ENVIRONMENT, cwd, argc > 0);
613 free(cwd);
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400614 }
615
brian m. carlson8b8f7182020-02-22 20:17:38 +0000616 if (object_format) {
617 hash_algo = hash_algo_by_name(object_format);
618 if (hash_algo == GIT_HASH_UNKNOWN)
619 die(_("unknown hash algorithm '%s'"), object_format);
620 }
621
Junio C Hamano5a688fe2009-03-25 16:19:36 -0700622 if (init_shared_repository != -1)
Jeff King7875acb2016-03-11 17:36:49 -0500623 set_shared_repository(init_shared_repository);
Junio C Hamano5a688fe2009-03-25 16:19:36 -0700624
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400625 /*
626 * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
627 * without --bare. Catch the error early.
628 */
Jeff Kinge5b07c52019-01-11 17:16:31 -0500629 git_dir = xstrdup_or_null(getenv(GIT_DIR_ENVIRONMENT));
630 work_tree = xstrdup_or_null(getenv(GIT_WORK_TREE_ENVIRONMENT));
Nguyễn Thái Ngọc Duy83518362010-11-26 22:32:40 +0700631 if ((!git_dir || is_bare_repository_cfg == 1) && work_tree)
Ævar Arnfjörð Bjarmason33e92e42011-02-22 23:41:24 +0000632 die(_("%s (or --work-tree=<directory>) not allowed without "
633 "specifying %s (or --git-dir=<directory>)"),
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400634 GIT_WORK_TREE_ENVIRONMENT,
635 GIT_DIR_ENVIRONMENT);
636
637 /*
638 * Set up the default .git directory contents
639 */
640 if (!git_dir)
641 git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
642
643 if (is_bare_repository_cfg < 0)
644 is_bare_repository_cfg = guess_repository_type(git_dir);
645
646 if (!is_bare_repository_cfg) {
Nguyễn Thái Ngọc Duyb31d2022011-03-03 19:34:51 +0700647 const char *git_dir_parent = strrchr(git_dir, '/');
648 if (git_dir_parent) {
649 char *rel = xstrndup(git_dir, git_dir_parent - git_dir);
Johannes Schindelince83ead2017-03-08 16:43:40 +0100650 git_work_tree_cfg = real_pathdup(rel, 1);
Nguyễn Thái Ngọc Duyb31d2022011-03-03 19:34:51 +0700651 free(rel);
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400652 }
René Scharfe56b9f6e2014-07-28 20:30:39 +0200653 if (!git_work_tree_cfg)
654 git_work_tree_cfg = xgetcwd();
Nguyễn Thái Ngọc Duy83518362010-11-26 22:32:40 +0700655 if (work_tree)
René Scharfe2d186c82014-07-28 20:42:05 +0200656 set_git_work_tree(work_tree);
Nguyễn Thái Ngọc Duy83518362010-11-26 22:32:40 +0700657 else
658 set_git_work_tree(git_work_tree_cfg);
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400659 if (access(get_git_work_tree(), X_OK))
Ævar Arnfjörð Bjarmason33e92e42011-02-22 23:41:24 +0000660 die_errno (_("Cannot access work tree '%s'"),
Thomas Rast0721c312009-06-27 17:58:47 +0200661 get_git_work_tree());
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400662 }
Nguyễn Thái Ngọc Duy83518362010-11-26 22:32:40 +0700663 else {
Eric Sunshineccf236a2020-08-09 18:53:16 -0400664 if (real_git_dir)
665 die(_("--separate-git-dir incompatible with bare repository"));
Nguyễn Thái Ngọc Duy83518362010-11-26 22:32:40 +0700666 if (work_tree)
René Scharfe2d186c82014-07-28 20:42:05 +0200667 set_git_work_tree(work_tree);
Nguyễn Thái Ngọc Duy83518362010-11-26 22:32:40 +0700668 }
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400669
Jeff King0e5bba52017-09-08 02:38:41 -0400670 UNLEAK(real_git_dir);
Jeff Kinge5b07c52019-01-11 17:16:31 -0500671 UNLEAK(git_dir);
672 UNLEAK(work_tree);
Jeff King0e5bba52017-09-08 02:38:41 -0400673
Nguyễn Thái Ngọc Duy33158702016-09-25 10:14:37 +0700674 flags |= INIT_DB_EXIST_OK;
Johannes Schindelin32ba12d2020-06-24 14:46:32 +0000675 return init_db(git_dir, real_git_dir, template_dir, hash_algo,
676 initial_branch, flags);
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400677}