blob: 2542c5244e91b68692c7ed371fa9976997ee8ff5 [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
Deskin Miller0a2c7ee2008-10-07 01:37:48 -040023static int init_is_bare_repository = 0;
24static int init_shared_repository = -1;
Steven Drake90b45182010-02-17 12:42:31 +130025static const char *init_db_template_dir;
Deskin Miller0a2c7ee2008-10-07 01:37:48 -040026
Brandon Williamsfa0fcca2018-02-14 10:59:52 -080027static void copy_templates_1(struct strbuf *path, struct strbuf *template_path,
Junio C Hamano8d5afef2005-08-02 16:45:21 -070028 DIR *dir)
29{
Jeff King9c283902015-10-04 23:46:04 -040030 size_t path_baselen = path->len;
Brandon Williamsfa0fcca2018-02-14 10:59:52 -080031 size_t template_baselen = template_path->len;
Junio C Hamano8d5afef2005-08-02 16:45:21 -070032 struct dirent *de;
33
34 /* Note: if ".git/hooks" file exists in the repository being
35 * re-initialized, /etc/core-git/templates/hooks/update would
Heikki Orsilaf18d2442008-09-13 20:18:36 +030036 * cause "git init" to fail here. I think this is sane but
Junio C Hamano8d5afef2005-08-02 16:45:21 -070037 * it means that the set of templates we ship by default, along
38 * with the way the namespace under .git/ is organized, should
39 * be really carefully chosen.
40 */
Jeff King9c283902015-10-04 23:46:04 -040041 safe_create_dir(path->buf, 1);
Junio C Hamano8d5afef2005-08-02 16:45:21 -070042 while ((de = readdir(dir)) != NULL) {
43 struct stat st_git, st_template;
Junio C Hamano8d5afef2005-08-02 16:45:21 -070044 int exists = 0;
45
Jeff King9c283902015-10-04 23:46:04 -040046 strbuf_setlen(path, path_baselen);
Brandon Williamsfa0fcca2018-02-14 10:59:52 -080047 strbuf_setlen(template_path, template_baselen);
Jeff King9c283902015-10-04 23:46:04 -040048
Junio C Hamano8d5afef2005-08-02 16:45:21 -070049 if (de->d_name[0] == '.')
50 continue;
Jeff King9c283902015-10-04 23:46:04 -040051 strbuf_addstr(path, de->d_name);
Brandon Williamsfa0fcca2018-02-14 10:59:52 -080052 strbuf_addstr(template_path, de->d_name);
Jeff King9c283902015-10-04 23:46:04 -040053 if (lstat(path->buf, &st_git)) {
Junio C Hamano8d5afef2005-08-02 16:45:21 -070054 if (errno != ENOENT)
Jeff King9c283902015-10-04 23:46:04 -040055 die_errno(_("cannot stat '%s'"), path->buf);
Junio C Hamano8d5afef2005-08-02 16:45:21 -070056 }
57 else
58 exists = 1;
59
Brandon Williamsfa0fcca2018-02-14 10:59:52 -080060 if (lstat(template_path->buf, &st_template))
61 die_errno(_("cannot stat template '%s'"), template_path->buf);
Junio C Hamano8d5afef2005-08-02 16:45:21 -070062
63 if (S_ISDIR(st_template.st_mode)) {
Brandon Williamsfa0fcca2018-02-14 10:59:52 -080064 DIR *subdir = opendir(template_path->buf);
Junio C Hamano8d5afef2005-08-02 16:45:21 -070065 if (!subdir)
Brandon Williamsfa0fcca2018-02-14 10:59:52 -080066 die_errno(_("cannot opendir '%s'"), template_path->buf);
Jeff King9c283902015-10-04 23:46:04 -040067 strbuf_addch(path, '/');
Brandon Williamsfa0fcca2018-02-14 10:59:52 -080068 strbuf_addch(template_path, '/');
69 copy_templates_1(path, template_path, subdir);
Junio C Hamano8d5afef2005-08-02 16:45:21 -070070 closedir(subdir);
71 }
72 else if (exists)
73 continue;
74 else if (S_ISLNK(st_template.st_mode)) {
Jeff King9c283902015-10-04 23:46:04 -040075 struct strbuf lnk = STRBUF_INIT;
Brandon Williamsfa0fcca2018-02-14 10:59:52 -080076 if (strbuf_readlink(&lnk, template_path->buf, 0) < 0)
77 die_errno(_("cannot readlink '%s'"), template_path->buf);
Jeff King9c283902015-10-04 23:46:04 -040078 if (symlink(lnk.buf, path->buf))
79 die_errno(_("cannot symlink '%s' '%s'"),
80 lnk.buf, path->buf);
81 strbuf_release(&lnk);
Junio C Hamano8d5afef2005-08-02 16:45:21 -070082 }
83 else if (S_ISREG(st_template.st_mode)) {
Brandon Williamsfa0fcca2018-02-14 10:59:52 -080084 if (copy_file(path->buf, template_path->buf, st_template.st_mode))
Jeff King9c283902015-10-04 23:46:04 -040085 die_errno(_("cannot copy '%s' to '%s'"),
Brandon Williamsfa0fcca2018-02-14 10:59:52 -080086 template_path->buf, path->buf);
Junio C Hamano8d5afef2005-08-02 16:45:21 -070087 }
88 else
Brandon Williamsfa0fcca2018-02-14 10:59:52 -080089 error(_("ignoring template %s"), template_path->buf);
Junio C Hamano8d5afef2005-08-02 16:45:21 -070090 }
91}
92
Daniel Barkalowf225aeb2008-04-27 13:39:27 -040093static void copy_templates(const char *template_dir)
Junio C Hamano8d5afef2005-08-02 16:45:21 -070094{
Jeff King9c283902015-10-04 23:46:04 -040095 struct strbuf path = STRBUF_INIT;
96 struct strbuf template_path = STRBUF_INIT;
97 size_t template_len;
Jeff King94ce1672016-03-11 17:37:11 -050098 struct repository_format template_format;
99 struct strbuf err = STRBUF_INIT;
Junio C Hamano8d5afef2005-08-02 16:45:21 -0700100 DIR *dir;
Junio C Hamano59362e52014-11-24 11:33:54 -0800101 char *to_free = NULL;
Junio C Hamano8d5afef2005-08-02 16:45:21 -0700102
Johannes Sixta47d1812007-11-13 21:05:04 +0100103 if (!template_dir)
Junio C Hamanod4ebc362006-12-19 01:28:15 -0800104 template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT);
Steffen Prohaska2de9de52008-07-13 22:31:18 +0200105 if (!template_dir)
Steven Drake90b45182010-02-17 12:42:31 +1300106 template_dir = init_db_template_dir;
107 if (!template_dir)
Junio C Hamano59362e52014-11-24 11:33:54 -0800108 template_dir = to_free = system_path(DEFAULT_GIT_TEMPLATE_DIR);
109 if (!template_dir[0]) {
110 free(to_free);
Jeff King172035f2008-07-28 02:02:04 -0400111 return;
Junio C Hamano59362e52014-11-24 11:33:54 -0800112 }
Jeff King9c283902015-10-04 23:46:04 -0400113
114 strbuf_addstr(&template_path, template_dir);
115 strbuf_complete(&template_path, '/');
116 template_len = template_path.len;
117
118 dir = opendir(template_path.buf);
Junio C Hamanod3af6212005-08-06 12:50:14 -0700119 if (!dir) {
Ævar Arnfjörð Bjarmason33e92e42011-02-22 23:41:24 +0000120 warning(_("templates not found %s"), template_dir);
Junio C Hamano59362e52014-11-24 11:33:54 -0800121 goto free_return;
Junio C Hamanod3af6212005-08-06 12:50:14 -0700122 }
123
Junio C Hamano4f629532005-11-25 16:03:56 -0800124 /* Make sure that template is from the correct vintage */
Jeff King9c283902015-10-04 23:46:04 -0400125 strbuf_addstr(&template_path, "config");
Jeff King94ce1672016-03-11 17:37:11 -0500126 read_repository_format(&template_format, template_path.buf);
Jeff King9c283902015-10-04 23:46:04 -0400127 strbuf_setlen(&template_path, template_len);
Junio C Hamano4f629532005-11-25 16:03:56 -0800128
Jeff King94ce1672016-03-11 17:37:11 -0500129 /*
130 * No mention of version at all is OK, but anything else should be
131 * verified.
132 */
133 if (template_format.version >= 0 &&
134 verify_repository_format(&template_format, &err) < 0) {
135 warning(_("not copying templates from '%s': %s"),
136 template_dir, err.buf);
137 strbuf_release(&err);
Junio C Hamano59362e52014-11-24 11:33:54 -0800138 goto close_free_return;
Junio C Hamano4f629532005-11-25 16:03:56 -0800139 }
140
Nguyễn Thái Ngọc Duyfe9aa0b2016-09-25 10:14:36 +0700141 strbuf_addstr(&path, get_git_common_dir());
Jeff King9c283902015-10-04 23:46:04 -0400142 strbuf_complete(&path, '/');
143 copy_templates_1(&path, &template_path, dir);
Junio C Hamano59362e52014-11-24 11:33:54 -0800144close_free_return:
Junio C Hamano8d5afef2005-08-02 16:45:21 -0700145 closedir(dir);
Junio C Hamano59362e52014-11-24 11:33:54 -0800146free_return:
147 free(to_free);
Jeff King9c283902015-10-04 23:46:04 -0400148 strbuf_release(&path);
149 strbuf_release(&template_path);
Junio C Hamano8d5afef2005-08-02 16:45:21 -0700150}
151
Steven Drake90b45182010-02-17 12:42:31 +1300152static int git_init_db_config(const char *k, const char *v, void *cb)
153{
Steven Drake90b45182010-02-17 12:42:31 +1300154 if (!strcmp(k, "init.templatedir"))
155 return git_config_pathname(&init_db_template_dir, k, v);
156
157 return 0;
158}
159
Jeff King84ccad82015-04-02 14:37:40 -0400160/*
161 * If the git_dir is not directly inside the working tree, then git will not
162 * find it by default, and we need to set the worktree explicitly.
163 */
164static int needs_work_tree_config(const char *git_dir, const char *work_tree)
165{
166 if (!strcmp(work_tree, "/") && !strcmp(git_dir, "/.git"))
167 return 0;
168 if (skip_prefix(git_dir, work_tree, &git_dir) &&
169 !strcmp(git_dir, "/.git"))
170 return 0;
171 return 1;
172}
173
Nguyễn Thái Ngọc Duy6311cfa2016-09-25 10:14:39 +0700174static int create_default_files(const char *template_path,
175 const char *original_git_dir)
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700176{
Junio C Hamano4f629532005-11-25 16:03:56 -0800177 struct stat st1;
Jeff King9c283902015-10-04 23:46:04 -0400178 struct strbuf buf = STRBUF_INIT;
179 char *path;
Junio C Hamano4f629532005-11-25 16:03:56 -0800180 char repo_version_string[10];
Johannes Schindelin5cc8f372008-03-24 16:14:52 +0100181 char junk[2];
Shawn O. Pearceef0a89a2006-12-15 00:44:58 -0500182 int reinit;
Shawn O. Pearcec8697532006-12-30 23:53:55 -0500183 int filemode;
David Turner6fb5acf2016-09-04 18:08:41 +0200184 struct strbuf err = STRBUF_INIT;
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700185
Steven Drake90b45182010-02-17 12:42:31 +1300186 /* Just look for `init.templatedir` */
187 git_config(git_init_db_config, NULL);
188
Jeff King7c0a8422016-09-12 20:24:19 -0700189 /*
190 * First copy the templates -- we might have the default
Junio C Hamano4f629532005-11-25 16:03:56 -0800191 * config file there, in which case we would want to read
192 * from it after installing.
Jeff King45439262016-09-12 20:24:23 -0700193 *
194 * Before reading that config, we also need to clear out any cached
195 * values (since we've just potentially changed what's available on
196 * disk).
Junio C Hamano4f629532005-11-25 16:03:56 -0800197 */
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400198 copy_templates(template_path);
Jeff King45439262016-09-12 20:24:23 -0700199 git_config_clear();
200 reset_shared_repository();
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100201 git_config(git_default_config, NULL);
Junio C Hamano5a688fe2009-03-25 16:19:36 -0700202
Jeff King7c0a8422016-09-12 20:24:19 -0700203 /*
204 * We must make sure command-line options continue to override any
205 * values we might have just re-read from the config.
206 */
207 is_bare_repository_cfg = init_is_bare_repository;
Deskin Miller0a2c7ee2008-10-07 01:37:48 -0400208 if (init_shared_repository != -1)
Jeff King7875acb2016-03-11 17:36:49 -0500209 set_shared_repository(init_shared_repository);
Junio C Hamano4f629532005-11-25 16:03:56 -0800210
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700211 /*
Junio C Hamano138086a2006-06-09 22:07:23 -0700212 * We would have created the above under user's umask -- under
213 * shared-repository settings, we would need to fix them up.
214 */
Jeff King7875acb2016-03-11 17:36:49 -0500215 if (get_shared_repository()) {
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400216 adjust_shared_perm(get_git_dir());
Junio C Hamano138086a2006-06-09 22:07:23 -0700217 }
218
219 /*
David Turner6fb5acf2016-09-04 18:08:41 +0200220 * We need to create a "refs" dir in any case so that older
221 * versions of git can tell that this is a repository.
222 */
223 safe_create_dir(git_path("refs"), 1);
224 adjust_shared_perm(git_path("refs"));
225
226 if (refs_init_db(&err))
227 die("failed to set up refs db: %s", err.buf);
228
229 /*
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700230 * Create the default symlink from ".git/HEAD" to the "master"
Junio C Hamano8098a172005-09-30 14:26:57 -0700231 * branch, if it does not exist yet.
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700232 */
Jeff King9c283902015-10-04 23:46:04 -0400233 path = git_path_buf(&buf, "HEAD");
Johannes Schindelin5cc8f372008-03-24 16:14:52 +0100234 reinit = (!access(path, R_OK)
235 || readlink(path, junk, sizeof(junk)-1) != -1);
Shawn O. Pearceef0a89a2006-12-15 00:44:58 -0500236 if (!reinit) {
Nicolas Pitre8b5157e2007-01-26 17:26:10 -0500237 if (create_symref("HEAD", "refs/heads/master", NULL) < 0)
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700238 exit(1);
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700239 }
Junio C Hamano4f629532005-11-25 16:03:56 -0800240
241 /* This forces creation of new config file */
Jeff King5096d492015-09-24 17:06:08 -0400242 xsnprintf(repo_version_string, sizeof(repo_version_string),
243 "%d", GIT_REPO_VERSION);
Patrick Steinhardt3d180642016-02-22 12:23:36 +0100244 git_config_set("core.repositoryformatversion", repo_version_string);
Junio C Hamano4f629532005-11-25 16:03:56 -0800245
Junio C Hamano4f629532005-11-25 16:03:56 -0800246 /* Check filemode trustability */
Jeff King9c283902015-10-04 23:46:04 -0400247 path = git_path_buf(&buf, "config");
Shawn O. Pearcec8697532006-12-30 23:53:55 -0500248 filemode = TEST_FILEMODE;
249 if (TEST_FILEMODE && !lstat(path, &st1)) {
Junio C Hamano4f629532005-11-25 16:03:56 -0800250 struct stat st2;
Shawn O. Pearcec8697532006-12-30 23:53:55 -0500251 filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) &&
Junio C Hamano4f629532005-11-25 16:03:56 -0800252 !lstat(path, &st2) &&
Michael Haggerty1f32ecf2014-11-18 14:50:24 +0100253 st1.st_mode != st2.st_mode &&
254 !chmod(path, st1.st_mode));
Torsten Bögershausenc7bf68d2014-11-21 10:34:54 +0100255 if (filemode && !reinit && (st1.st_mode & S_IXUSR))
256 filemode = 0;
Johannes Schindeline24317b2005-10-26 01:43:03 +0200257 }
Patrick Steinhardt3d180642016-02-22 12:23:36 +0100258 git_config_set("core.filemode", filemode ? "true" : "false");
Shawn O. Pearceef0a89a2006-12-15 00:44:58 -0500259
Johannes Schindeline90fdc32007-08-01 01:30:14 +0100260 if (is_bare_repository())
Patrick Steinhardt3d180642016-02-22 12:23:36 +0100261 git_config_set("core.bare", "true");
Junio C Hamano7d1864c2007-01-07 02:00:28 -0800262 else {
Johannes Schindeline90fdc32007-08-01 01:30:14 +0100263 const char *work_tree = get_git_work_tree();
Patrick Steinhardt3d180642016-02-22 12:23:36 +0100264 git_config_set("core.bare", "false");
Alex Riesen196055c2007-01-23 16:51:18 +0100265 /* allow template config file to override the default */
Cornelius Weig341fb282017-01-27 11:09:47 +0100266 if (log_all_ref_updates == LOG_REFS_UNSET)
Patrick Steinhardt3d180642016-02-22 12:23:36 +0100267 git_config_set("core.logallrefupdates", "true");
Nguyễn Thái Ngọc Duy6311cfa2016-09-25 10:14:39 +0700268 if (needs_work_tree_config(original_git_dir, work_tree))
Patrick Steinhardt3d180642016-02-22 12:23:36 +0100269 git_config_set("core.worktree", work_tree);
Junio C Hamano7d1864c2007-01-07 02:00:28 -0800270 }
Junio C Hamano75d24492007-08-31 00:25:04 -0700271
Junio C Hamano75d24492007-08-31 00:25:04 -0700272 if (!reinit) {
Dmitry Potapov24554062008-05-11 18:16:39 +0200273 /* Check if symlink is supported in the work tree */
Jeff King9c283902015-10-04 23:46:04 -0400274 path = git_path_buf(&buf, "tXXXXXX");
Junio C Hamano75d24492007-08-31 00:25:04 -0700275 if (!close(xmkstemp(path)) &&
276 !unlink(path) &&
277 !symlink("testing", path) &&
278 !lstat(path, &st1) &&
279 S_ISLNK(st1.st_mode))
280 unlink(path); /* good */
281 else
Patrick Steinhardt3d180642016-02-22 12:23:36 +0100282 git_config_set("core.symlinks", "false");
Dmitry Potapov24554062008-05-11 18:16:39 +0200283
284 /* Check if the filesystem is case-insensitive */
Jeff King9c283902015-10-04 23:46:04 -0400285 path = git_path_buf(&buf, "CoNfIg");
Dmitry Potapov24554062008-05-11 18:16:39 +0200286 if (!access(path, F_OK))
Patrick Steinhardt3d180642016-02-22 12:23:36 +0100287 git_config_set("core.ignorecase", "true");
Jeff Kingfdf72962015-10-04 23:45:26 -0400288 probe_utf8_pathname_composition();
Junio C Hamano75d24492007-08-31 00:25:04 -0700289 }
290
Jeff King9c283902015-10-04 23:46:04 -0400291 strbuf_release(&buf);
Shawn O. Pearceef0a89a2006-12-15 00:44:58 -0500292 return reinit;
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700293}
294
Jonathan Nieder91739122010-10-03 23:34:27 -0500295static void create_object_directory(void)
296{
Jeff King9c283902015-10-04 23:46:04 -0400297 struct strbuf path = STRBUF_INIT;
298 size_t baselen;
Jonathan Nieder91739122010-10-03 23:34:27 -0500299
Jeff King9c283902015-10-04 23:46:04 -0400300 strbuf_addstr(&path, get_object_directory());
301 baselen = path.len;
Jonathan Nieder91739122010-10-03 23:34:27 -0500302
Jeff King9c283902015-10-04 23:46:04 -0400303 safe_create_dir(path.buf, 1);
Jonathan Nieder91739122010-10-03 23:34:27 -0500304
Jeff King9c283902015-10-04 23:46:04 -0400305 strbuf_setlen(&path, baselen);
306 strbuf_addstr(&path, "/pack");
307 safe_create_dir(path.buf, 1);
308
309 strbuf_setlen(&path, baselen);
310 strbuf_addstr(&path, "/info");
311 safe_create_dir(path.buf, 1);
312
313 strbuf_release(&path);
Jonathan Nieder91739122010-10-03 23:34:27 -0500314}
315
Nguyễn Thái Ngọc Duy822d9402016-09-25 10:14:40 +0700316static void separate_git_dir(const char *git_dir, const char *git_link)
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700317{
318 struct stat st;
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700319
320 if (!stat(git_link, &st)) {
321 const char *src;
322
323 if (S_ISREG(st.st_mode))
Junio C Hamano13d6ec92011-08-22 14:04:56 -0700324 src = read_gitfile(git_link);
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700325 else if (S_ISDIR(st.st_mode))
326 src = git_link;
327 else
Ævar Arnfjörð Bjarmason97f261b2011-12-20 23:27:41 +0000328 die(_("unable to handle file type %d"), (int)st.st_mode);
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700329
330 if (rename(src, git_dir))
Ævar Arnfjörð Bjarmason2c050e02011-04-10 19:34:02 +0000331 die_errno(_("unable to move %s to %s"), src, git_dir);
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700332 }
333
Junio C Hamano1f76a102015-08-24 13:20:39 -0700334 write_file(git_link, "gitdir: %s", git_dir);
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700335}
336
Nguyễn Thái Ngọc Duy33158702016-09-25 10:14:37 +0700337int init_db(const char *git_dir, const char *real_git_dir,
338 const char *template_dir, unsigned int flags)
Junio C Hamano6adcca32007-08-27 00:58:06 -0700339{
Jonathan Nieder91739122010-10-03 23:34:27 -0500340 int reinit;
Nguyễn Thái Ngọc Duy1bd19072016-09-25 10:14:38 +0700341 int exist_ok = flags & INIT_DB_EXIST_OK;
Johannes Schindelince83ead2017-03-08 16:43:40 +0100342 char *original_git_dir = real_pathdup(git_dir, 1);
Linus Torvaldse83c5162005-04-07 15:13:13 -0700343
Nguyễn Thái Ngọc Duy1bd19072016-09-25 10:14:38 +0700344 if (real_git_dir) {
345 struct stat st;
Nguyễn Thái Ngọc Duy33158702016-09-25 10:14:37 +0700346
Nguyễn Thái Ngọc Duy1bd19072016-09-25 10:14:38 +0700347 if (!exist_ok && !stat(git_dir, &st))
348 die(_("%s already exists"), git_dir);
349
350 if (!exist_ok && !stat(real_git_dir, &st))
351 die(_("%s already exists"), real_git_dir);
352
Nguyễn Thái Ngọc Duy1bd19072016-09-25 10:14:38 +0700353 set_git_dir(real_path(real_git_dir));
Nguyễn Thái Ngọc Duy822d9402016-09-25 10:14:40 +0700354 git_dir = get_git_dir();
355 separate_git_dir(git_dir, original_git_dir);
Nguyễn Thái Ngọc Duy1bd19072016-09-25 10:14:38 +0700356 }
357 else {
358 set_git_dir(real_path(git_dir));
Nguyễn Thái Ngọc Duy822d9402016-09-25 10:14:40 +0700359 git_dir = get_git_dir();
Nguyễn Thái Ngọc Duy1bd19072016-09-25 10:14:38 +0700360 }
361 startup_info->have_repository = 1;
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700362
363 safe_create_dir(git_dir, 0);
Junio C Hamano4f629532005-11-25 16:03:56 -0800364
Deskin Miller0a2c7ee2008-10-07 01:37:48 -0400365 init_is_bare_repository = is_bare_repository();
366
Junio C Hamano4f629532005-11-25 16:03:56 -0800367 /* Check to see if the repository version is right.
368 * Note that a newly created repository does not have
369 * config file, so this will not fail. What we are catching
370 * is an attempt to reinitialize new repository with an old tool.
371 */
372 check_repository_format();
373
Nguyễn Thái Ngọc Duy6311cfa2016-09-25 10:14:39 +0700374 reinit = create_default_files(template_dir, original_git_dir);
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700375
Jonathan Nieder91739122010-10-03 23:34:27 -0500376 create_object_directory();
Johannes Schindelinaf6e2772005-12-22 23:19:37 +0100377
Jeff King7875acb2016-03-11 17:36:49 -0500378 if (get_shared_repository()) {
Junio C Hamano94df2502006-06-09 23:09:49 -0700379 char buf[10];
380 /* We do not spell "group" and such, so that
381 * the configuration can be read by older version
Heikki Orsila06cbe852008-04-16 11:34:24 +0300382 * of git. Note, we use octal numbers for new share modes,
383 * and compatibility values for PERM_GROUP and
384 * PERM_EVERYBODY.
Junio C Hamano94df2502006-06-09 23:09:49 -0700385 */
Jeff King7875acb2016-03-11 17:36:49 -0500386 if (get_shared_repository() < 0)
Junio C Hamano5a688fe2009-03-25 16:19:36 -0700387 /* force to the mode value */
Jeff King7875acb2016-03-11 17:36:49 -0500388 xsnprintf(buf, sizeof(buf), "0%o", -get_shared_repository());
389 else if (get_shared_repository() == PERM_GROUP)
Jeff King5096d492015-09-24 17:06:08 -0400390 xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_GROUP);
Jeff King7875acb2016-03-11 17:36:49 -0500391 else if (get_shared_repository() == PERM_EVERYBODY)
Jeff King5096d492015-09-24 17:06:08 -0400392 xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_EVERYBODY);
Heikki Orsila06cbe852008-04-16 11:34:24 +0300393 else
Jeff King5096d492015-09-24 17:06:08 -0400394 die("BUG: invalid value for shared_repository");
Patrick Steinhardt3d180642016-02-22 12:23:36 +0100395 git_config_set("core.sharedrepository", buf);
396 git_config_set("receive.denyNonFastforwards", "true");
Junio C Hamano94df2502006-06-09 23:09:49 -0700397 }
Johannes Schindelinaf6e2772005-12-22 23:19:37 +0100398
Nguyễn Thái Ngọc Duyd06f15d2010-02-14 22:44:42 +0700399 if (!(flags & INIT_DB_QUIET)) {
Nguyễn Thái Ngọc Duyd06f15d2010-02-14 22:44:42 +0700400 int len = strlen(git_dir);
Ævar Arnfjörð Bjarmason3e5dd7e2011-02-22 23:41:25 +0000401
Vasco Almeidac30364d2016-06-17 21:54:11 +0000402 if (reinit)
403 printf(get_shared_repository()
404 ? _("Reinitialized existing shared Git repository in %s%s\n")
405 : _("Reinitialized existing Git repository in %s%s\n"),
406 git_dir, len && git_dir[len-1] != '/' ? "/" : "");
407 else
408 printf(get_shared_repository()
409 ? _("Initialized empty shared Git repository in %s%s\n")
410 : _("Initialized empty Git repository in %s%s\n"),
411 git_dir, len && git_dir[len-1] != '/' ? "/" : "");
Nguyễn Thái Ngọc Duyd06f15d2010-02-14 22:44:42 +0700412 }
Shawn O. Pearceef0a89a2006-12-15 00:44:58 -0500413
Nguyễn Thái Ngọc Duy6311cfa2016-09-25 10:14:39 +0700414 free(original_git_dir);
Linus Torvaldse83c5162005-04-07 15:13:13 -0700415 return 0;
416}
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400417
418static int guess_repository_type(const char *git_dir)
419{
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400420 const char *slash;
René Scharfe56b9f6e2014-07-28 20:30:39 +0200421 char *cwd;
422 int cwd_is_git_dir;
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400423
424 /*
425 * "GIT_DIR=. git init" is always bare.
426 * "GIT_DIR=`pwd` git init" too.
427 */
428 if (!strcmp(".", git_dir))
429 return 1;
René Scharfe56b9f6e2014-07-28 20:30:39 +0200430 cwd = xgetcwd();
431 cwd_is_git_dir = !strcmp(git_dir, cwd);
432 free(cwd);
433 if (cwd_is_git_dir)
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400434 return 1;
435 /*
436 * "GIT_DIR=.git or GIT_DIR=something/.git is usually not.
437 */
438 if (!strcmp(git_dir, ".git"))
439 return 0;
440 slash = strrchr(git_dir, '/');
441 if (slash && !strcmp(slash, "/.git"))
442 return 0;
443
444 /*
445 * Otherwise it is often bare. At this point
446 * we are just guessing.
447 */
448 return 1;
449}
450
Michał Kiedrowicz596f91e2009-07-12 12:24:32 +0200451static int shared_callback(const struct option *opt, const char *arg, int unset)
452{
453 *((int *) opt->value) = (arg) ? git_config_perm("arg", arg) : PERM_GROUP;
454 return 0;
455}
456
457static const char *const init_db_usage[] = {
Alex Henrie9c9b4f22015-01-13 00:44:47 -0700458 N_("git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]] [<directory>]"),
Michał Kiedrowicz596f91e2009-07-12 12:24:32 +0200459 NULL
460};
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400461
462/*
463 * If you want to, you can share the DB area with any number of branches.
464 * That has advantages: you can save space by sharing all the SHA1 objects.
465 * On the other hand, it might just make lookup slower and messier. You
466 * be the judge. The default case is to have one DB per managed directory.
467 */
468int cmd_init_db(int argc, const char **argv, const char *prefix)
469{
470 const char *git_dir;
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700471 const char *real_git_dir = NULL;
Nguyễn Thái Ngọc Duy83518362010-11-26 22:32:40 +0700472 const char *work_tree;
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400473 const char *template_dir = NULL;
474 unsigned int flags = 0;
Michał Kiedrowicz596f91e2009-07-12 12:24:32 +0200475 const struct option init_db_options[] = {
Nguyễn Thái Ngọc Duyce4a5e52012-08-20 19:32:18 +0700476 OPT_STRING(0, "template", &template_dir, N_("template-directory"),
477 N_("directory from which templates will be used")),
Michał Kiedrowicz596f91e2009-07-12 12:24:32 +0200478 OPT_SET_INT(0, "bare", &is_bare_repository_cfg,
Nguyễn Thái Ngọc Duyce4a5e52012-08-20 19:32:18 +0700479 N_("create a bare repository"), 1),
Michał Kiedrowicz596f91e2009-07-12 12:24:32 +0200480 { OPTION_CALLBACK, 0, "shared", &init_shared_repository,
Nguyễn Thái Ngọc Duyce4a5e52012-08-20 19:32:18 +0700481 N_("permissions"),
482 N_("specify that the git repository is to be shared amongst several users"),
Michał Kiedrowicz596f91e2009-07-12 12:24:32 +0200483 PARSE_OPT_OPTARG | PARSE_OPT_NONEG, shared_callback, 0},
Nguyễn Thái Ngọc Duyce4a5e52012-08-20 19:32:18 +0700484 OPT_BIT('q', "quiet", &flags, N_("be quiet"), INIT_DB_QUIET),
485 OPT_STRING(0, "separate-git-dir", &real_git_dir, N_("gitdir"),
486 N_("separate git dir from working tree")),
Michał Kiedrowicz596f91e2009-07-12 12:24:32 +0200487 OPT_END()
488 };
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400489
Junio C Hamano0397ff22009-08-05 12:39:33 -0700490 argc = parse_options(argc, argv, prefix, init_db_options, init_db_usage, 0);
Michał Kiedrowicz596f91e2009-07-12 12:24:32 +0200491
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700492 if (real_git_dir && !is_absolute_path(real_git_dir))
Johannes Schindelince83ead2017-03-08 16:43:40 +0100493 real_git_dir = real_pathdup(real_git_dir, 1);
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700494
Junio C Hamano0397ff22009-08-05 12:39:33 -0700495 if (argc == 1) {
Nanako Shiraishi53d48882009-07-25 06:59:28 +0900496 int mkdir_tried = 0;
497 retry:
Junio C Hamano0397ff22009-08-05 12:39:33 -0700498 if (chdir(argv[0]) < 0) {
Nanako Shiraishi53d48882009-07-25 06:59:28 +0900499 if (!mkdir_tried) {
500 int saved;
501 /*
502 * At this point we haven't read any configuration,
503 * and we know shared_repository should always be 0;
504 * but just in case we play safe.
505 */
Jeff King7875acb2016-03-11 17:36:49 -0500506 saved = get_shared_repository();
507 set_shared_repository(0);
Junio C Hamano0397ff22009-08-05 12:39:33 -0700508 switch (safe_create_leading_directories_const(argv[0])) {
Michael Haggertyf3565c02014-01-06 14:45:26 +0100509 case SCLD_OK:
510 case SCLD_PERMS:
511 break;
Michael Haggerty0be05212014-01-06 14:45:25 +0100512 case SCLD_EXISTS:
Nanako Shiraishi53d48882009-07-25 06:59:28 +0900513 errno = EEXIST;
514 /* fallthru */
Nanako Shiraishi53d48882009-07-25 06:59:28 +0900515 default:
Michael Haggertyf3565c02014-01-06 14:45:26 +0100516 die_errno(_("cannot mkdir %s"), argv[0]);
Nanako Shiraishi53d48882009-07-25 06:59:28 +0900517 break;
518 }
Jeff King7875acb2016-03-11 17:36:49 -0500519 set_shared_repository(saved);
Junio C Hamano0397ff22009-08-05 12:39:33 -0700520 if (mkdir(argv[0], 0777) < 0)
Ævar Arnfjörð Bjarmason33e92e42011-02-22 23:41:24 +0000521 die_errno(_("cannot mkdir %s"), argv[0]);
Nanako Shiraishi53d48882009-07-25 06:59:28 +0900522 mkdir_tried = 1;
523 goto retry;
524 }
Ævar Arnfjörð Bjarmason33e92e42011-02-22 23:41:24 +0000525 die_errno(_("cannot chdir to %s"), argv[0]);
Nanako Shiraishi53d48882009-07-25 06:59:28 +0900526 }
Junio C Hamano0397ff22009-08-05 12:39:33 -0700527 } else if (0 < argc) {
528 usage(init_db_usage[0]);
Nanako Shiraishi53d48882009-07-25 06:59:28 +0900529 }
Junio C Hamano0397ff22009-08-05 12:39:33 -0700530 if (is_bare_repository_cfg == 1) {
René Scharfe4d3ab442014-07-28 20:31:57 +0200531 char *cwd = xgetcwd();
532 setenv(GIT_DIR_ENVIRONMENT, cwd, argc > 0);
533 free(cwd);
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400534 }
535
Junio C Hamano5a688fe2009-03-25 16:19:36 -0700536 if (init_shared_repository != -1)
Jeff King7875acb2016-03-11 17:36:49 -0500537 set_shared_repository(init_shared_repository);
Junio C Hamano5a688fe2009-03-25 16:19:36 -0700538
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400539 /*
540 * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
541 * without --bare. Catch the error early.
542 */
543 git_dir = getenv(GIT_DIR_ENVIRONMENT);
Nguyễn Thái Ngọc Duy83518362010-11-26 22:32:40 +0700544 work_tree = getenv(GIT_WORK_TREE_ENVIRONMENT);
545 if ((!git_dir || is_bare_repository_cfg == 1) && work_tree)
Ævar Arnfjörð Bjarmason33e92e42011-02-22 23:41:24 +0000546 die(_("%s (or --work-tree=<directory>) not allowed without "
547 "specifying %s (or --git-dir=<directory>)"),
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400548 GIT_WORK_TREE_ENVIRONMENT,
549 GIT_DIR_ENVIRONMENT);
550
551 /*
552 * Set up the default .git directory contents
553 */
554 if (!git_dir)
555 git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
556
557 if (is_bare_repository_cfg < 0)
558 is_bare_repository_cfg = guess_repository_type(git_dir);
559
560 if (!is_bare_repository_cfg) {
Nguyễn Thái Ngọc Duyb31d2022011-03-03 19:34:51 +0700561 const char *git_dir_parent = strrchr(git_dir, '/');
562 if (git_dir_parent) {
563 char *rel = xstrndup(git_dir, git_dir_parent - git_dir);
Johannes Schindelince83ead2017-03-08 16:43:40 +0100564 git_work_tree_cfg = real_pathdup(rel, 1);
Nguyễn Thái Ngọc Duyb31d2022011-03-03 19:34:51 +0700565 free(rel);
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400566 }
René Scharfe56b9f6e2014-07-28 20:30:39 +0200567 if (!git_work_tree_cfg)
568 git_work_tree_cfg = xgetcwd();
Nguyễn Thái Ngọc Duy83518362010-11-26 22:32:40 +0700569 if (work_tree)
René Scharfe2d186c82014-07-28 20:42:05 +0200570 set_git_work_tree(work_tree);
Nguyễn Thái Ngọc Duy83518362010-11-26 22:32:40 +0700571 else
572 set_git_work_tree(git_work_tree_cfg);
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400573 if (access(get_git_work_tree(), X_OK))
Ævar Arnfjörð Bjarmason33e92e42011-02-22 23:41:24 +0000574 die_errno (_("Cannot access work tree '%s'"),
Thomas Rast0721c312009-06-27 17:58:47 +0200575 get_git_work_tree());
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400576 }
Nguyễn Thái Ngọc Duy83518362010-11-26 22:32:40 +0700577 else {
578 if (work_tree)
René Scharfe2d186c82014-07-28 20:42:05 +0200579 set_git_work_tree(work_tree);
Nguyễn Thái Ngọc Duy83518362010-11-26 22:32:40 +0700580 }
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400581
Jeff King0e5bba52017-09-08 02:38:41 -0400582 UNLEAK(real_git_dir);
583
Nguyễn Thái Ngọc Duy33158702016-09-25 10:14:37 +0700584 flags |= INIT_DB_EXIST_OK;
585 return init_db(git_dir, real_git_dir, template_dir, flags);
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400586}