blob: 12ddda7e7bac51f74b2f7ca8a2b64ffe90fd8683 [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;
Jeff King765b4962018-07-24 06:51:39 -040076 if (strbuf_readlink(&lnk, template_path->buf,
77 st_template.st_size) < 0)
Brandon Williamsfa0fcca2018-02-14 10:59:52 -080078 die_errno(_("cannot readlink '%s'"), template_path->buf);
Jeff King9c283902015-10-04 23:46:04 -040079 if (symlink(lnk.buf, path->buf))
80 die_errno(_("cannot symlink '%s' '%s'"),
81 lnk.buf, path->buf);
82 strbuf_release(&lnk);
Junio C Hamano8d5afef2005-08-02 16:45:21 -070083 }
84 else if (S_ISREG(st_template.st_mode)) {
Brandon Williamsfa0fcca2018-02-14 10:59:52 -080085 if (copy_file(path->buf, template_path->buf, st_template.st_mode))
Jeff King9c283902015-10-04 23:46:04 -040086 die_errno(_("cannot copy '%s' to '%s'"),
Brandon Williamsfa0fcca2018-02-14 10:59:52 -080087 template_path->buf, path->buf);
Junio C Hamano8d5afef2005-08-02 16:45:21 -070088 }
89 else
Brandon Williamsfa0fcca2018-02-14 10:59:52 -080090 error(_("ignoring template %s"), template_path->buf);
Junio C Hamano8d5afef2005-08-02 16:45:21 -070091 }
92}
93
Daniel Barkalowf225aeb2008-04-27 13:39:27 -040094static void copy_templates(const char *template_dir)
Junio C Hamano8d5afef2005-08-02 16:45:21 -070095{
Jeff King9c283902015-10-04 23:46:04 -040096 struct strbuf path = STRBUF_INIT;
97 struct strbuf template_path = STRBUF_INIT;
98 size_t template_len;
Jeff King94ce1672016-03-11 17:37:11 -050099 struct repository_format template_format;
100 struct strbuf err = STRBUF_INIT;
Junio C Hamano8d5afef2005-08-02 16:45:21 -0700101 DIR *dir;
Junio C Hamano59362e52014-11-24 11:33:54 -0800102 char *to_free = NULL;
Junio C Hamano8d5afef2005-08-02 16:45:21 -0700103
Johannes Sixta47d1812007-11-13 21:05:04 +0100104 if (!template_dir)
Junio C Hamanod4ebc362006-12-19 01:28:15 -0800105 template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT);
Steffen Prohaska2de9de52008-07-13 22:31:18 +0200106 if (!template_dir)
Steven Drake90b45182010-02-17 12:42:31 +1300107 template_dir = init_db_template_dir;
108 if (!template_dir)
Junio C Hamano59362e52014-11-24 11:33:54 -0800109 template_dir = to_free = system_path(DEFAULT_GIT_TEMPLATE_DIR);
110 if (!template_dir[0]) {
111 free(to_free);
Jeff King172035f2008-07-28 02:02:04 -0400112 return;
Junio C Hamano59362e52014-11-24 11:33:54 -0800113 }
Jeff King9c283902015-10-04 23:46:04 -0400114
115 strbuf_addstr(&template_path, template_dir);
116 strbuf_complete(&template_path, '/');
117 template_len = template_path.len;
118
119 dir = opendir(template_path.buf);
Junio C Hamanod3af6212005-08-06 12:50:14 -0700120 if (!dir) {
Robert P. J. Day44f560f2018-05-29 08:14:35 -0400121 warning(_("templates not found in %s"), template_dir);
Junio C Hamano59362e52014-11-24 11:33:54 -0800122 goto free_return;
Junio C Hamanod3af6212005-08-06 12:50:14 -0700123 }
124
Junio C Hamano4f629532005-11-25 16:03:56 -0800125 /* Make sure that template is from the correct vintage */
Jeff King9c283902015-10-04 23:46:04 -0400126 strbuf_addstr(&template_path, "config");
Jeff King94ce1672016-03-11 17:37:11 -0500127 read_repository_format(&template_format, template_path.buf);
Jeff King9c283902015-10-04 23:46:04 -0400128 strbuf_setlen(&template_path, template_len);
Junio C Hamano4f629532005-11-25 16:03:56 -0800129
Jeff King94ce1672016-03-11 17:37:11 -0500130 /*
131 * No mention of version at all is OK, but anything else should be
132 * verified.
133 */
134 if (template_format.version >= 0 &&
135 verify_repository_format(&template_format, &err) < 0) {
136 warning(_("not copying templates from '%s': %s"),
137 template_dir, err.buf);
138 strbuf_release(&err);
Junio C Hamano59362e52014-11-24 11:33:54 -0800139 goto close_free_return;
Junio C Hamano4f629532005-11-25 16:03:56 -0800140 }
141
Nguyễn Thái Ngọc Duyfe9aa0b2016-09-25 10:14:36 +0700142 strbuf_addstr(&path, get_git_common_dir());
Jeff King9c283902015-10-04 23:46:04 -0400143 strbuf_complete(&path, '/');
144 copy_templates_1(&path, &template_path, dir);
Junio C Hamano59362e52014-11-24 11:33:54 -0800145close_free_return:
Junio C Hamano8d5afef2005-08-02 16:45:21 -0700146 closedir(dir);
Junio C Hamano59362e52014-11-24 11:33:54 -0800147free_return:
148 free(to_free);
Jeff King9c283902015-10-04 23:46:04 -0400149 strbuf_release(&path);
150 strbuf_release(&template_path);
Junio C Hamano8d5afef2005-08-02 16:45:21 -0700151}
152
Steven Drake90b45182010-02-17 12:42:31 +1300153static int git_init_db_config(const char *k, const char *v, void *cb)
154{
Steven Drake90b45182010-02-17 12:42:31 +1300155 if (!strcmp(k, "init.templatedir"))
156 return git_config_pathname(&init_db_template_dir, k, v);
157
158 return 0;
159}
160
Jeff King84ccad82015-04-02 14:37:40 -0400161/*
162 * If the git_dir is not directly inside the working tree, then git will not
163 * find it by default, and we need to set the worktree explicitly.
164 */
165static int needs_work_tree_config(const char *git_dir, const char *work_tree)
166{
167 if (!strcmp(work_tree, "/") && !strcmp(git_dir, "/.git"))
168 return 0;
169 if (skip_prefix(git_dir, work_tree, &git_dir) &&
170 !strcmp(git_dir, "/.git"))
171 return 0;
172 return 1;
173}
174
Nguyễn Thái Ngọc Duy6311cfa2016-09-25 10:14:39 +0700175static int create_default_files(const char *template_path,
176 const char *original_git_dir)
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700177{
Junio C Hamano4f629532005-11-25 16:03:56 -0800178 struct stat st1;
Jeff King9c283902015-10-04 23:46:04 -0400179 struct strbuf buf = STRBUF_INIT;
180 char *path;
Junio C Hamano4f629532005-11-25 16:03:56 -0800181 char repo_version_string[10];
Johannes Schindelin5cc8f372008-03-24 16:14:52 +0100182 char junk[2];
Shawn O. Pearceef0a89a2006-12-15 00:44:58 -0500183 int reinit;
Shawn O. Pearcec8697532006-12-30 23:53:55 -0500184 int filemode;
David Turner6fb5acf2016-09-04 18:08:41 +0200185 struct strbuf err = STRBUF_INIT;
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700186
Steven Drake90b45182010-02-17 12:42:31 +1300187 /* Just look for `init.templatedir` */
188 git_config(git_init_db_config, NULL);
189
Jeff King7c0a8422016-09-12 20:24:19 -0700190 /*
191 * First copy the templates -- we might have the default
Junio C Hamano4f629532005-11-25 16:03:56 -0800192 * config file there, in which case we would want to read
193 * from it after installing.
Jeff King45439262016-09-12 20:24:23 -0700194 *
195 * Before reading that config, we also need to clear out any cached
196 * values (since we've just potentially changed what's available on
197 * disk).
Junio C Hamano4f629532005-11-25 16:03:56 -0800198 */
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400199 copy_templates(template_path);
Jeff King45439262016-09-12 20:24:23 -0700200 git_config_clear();
201 reset_shared_repository();
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100202 git_config(git_default_config, NULL);
Junio C Hamano5a688fe2009-03-25 16:19:36 -0700203
Jeff King7c0a8422016-09-12 20:24:19 -0700204 /*
205 * We must make sure command-line options continue to override any
206 * values we might have just re-read from the config.
207 */
208 is_bare_repository_cfg = init_is_bare_repository;
Deskin Miller0a2c7ee2008-10-07 01:37:48 -0400209 if (init_shared_repository != -1)
Jeff King7875acb2016-03-11 17:36:49 -0500210 set_shared_repository(init_shared_repository);
Junio C Hamano4f629532005-11-25 16:03:56 -0800211
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700212 /*
Junio C Hamano138086a2006-06-09 22:07:23 -0700213 * We would have created the above under user's umask -- under
214 * shared-repository settings, we would need to fix them up.
215 */
Jeff King7875acb2016-03-11 17:36:49 -0500216 if (get_shared_repository()) {
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400217 adjust_shared_perm(get_git_dir());
Junio C Hamano138086a2006-06-09 22:07:23 -0700218 }
219
220 /*
David Turner6fb5acf2016-09-04 18:08:41 +0200221 * We need to create a "refs" dir in any case so that older
222 * versions of git can tell that this is a repository.
223 */
224 safe_create_dir(git_path("refs"), 1);
225 adjust_shared_perm(git_path("refs"));
226
227 if (refs_init_db(&err))
228 die("failed to set up refs db: %s", err.buf);
229
230 /*
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700231 * Create the default symlink from ".git/HEAD" to the "master"
Junio C Hamano8098a172005-09-30 14:26:57 -0700232 * branch, if it does not exist yet.
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700233 */
Jeff King9c283902015-10-04 23:46:04 -0400234 path = git_path_buf(&buf, "HEAD");
Johannes Schindelin5cc8f372008-03-24 16:14:52 +0100235 reinit = (!access(path, R_OK)
236 || readlink(path, junk, sizeof(junk)-1) != -1);
Shawn O. Pearceef0a89a2006-12-15 00:44:58 -0500237 if (!reinit) {
Nicolas Pitre8b5157e2007-01-26 17:26:10 -0500238 if (create_symref("HEAD", "refs/heads/master", NULL) < 0)
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700239 exit(1);
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700240 }
Junio C Hamano4f629532005-11-25 16:03:56 -0800241
242 /* This forces creation of new config file */
Jeff King5096d492015-09-24 17:06:08 -0400243 xsnprintf(repo_version_string, sizeof(repo_version_string),
244 "%d", GIT_REPO_VERSION);
Patrick Steinhardt3d180642016-02-22 12:23:36 +0100245 git_config_set("core.repositoryformatversion", repo_version_string);
Junio C Hamano4f629532005-11-25 16:03:56 -0800246
Junio C Hamano4f629532005-11-25 16:03:56 -0800247 /* Check filemode trustability */
Jeff King9c283902015-10-04 23:46:04 -0400248 path = git_path_buf(&buf, "config");
Shawn O. Pearcec8697532006-12-30 23:53:55 -0500249 filemode = TEST_FILEMODE;
250 if (TEST_FILEMODE && !lstat(path, &st1)) {
Junio C Hamano4f629532005-11-25 16:03:56 -0800251 struct stat st2;
Shawn O. Pearcec8697532006-12-30 23:53:55 -0500252 filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) &&
Junio C Hamano4f629532005-11-25 16:03:56 -0800253 !lstat(path, &st2) &&
Michael Haggerty1f32ecf2014-11-18 14:50:24 +0100254 st1.st_mode != st2.st_mode &&
255 !chmod(path, st1.st_mode));
Torsten Bögershausenc7bf68d2014-11-21 10:34:54 +0100256 if (filemode && !reinit && (st1.st_mode & S_IXUSR))
257 filemode = 0;
Johannes Schindeline24317b2005-10-26 01:43:03 +0200258 }
Patrick Steinhardt3d180642016-02-22 12:23:36 +0100259 git_config_set("core.filemode", filemode ? "true" : "false");
Shawn O. Pearceef0a89a2006-12-15 00:44:58 -0500260
Johannes Schindeline90fdc32007-08-01 01:30:14 +0100261 if (is_bare_repository())
Patrick Steinhardt3d180642016-02-22 12:23:36 +0100262 git_config_set("core.bare", "true");
Junio C Hamano7d1864c2007-01-07 02:00:28 -0800263 else {
Johannes Schindeline90fdc32007-08-01 01:30:14 +0100264 const char *work_tree = get_git_work_tree();
Patrick Steinhardt3d180642016-02-22 12:23:36 +0100265 git_config_set("core.bare", "false");
Alex Riesen196055c2007-01-23 16:51:18 +0100266 /* allow template config file to override the default */
Cornelius Weig341fb282017-01-27 11:09:47 +0100267 if (log_all_ref_updates == LOG_REFS_UNSET)
Patrick Steinhardt3d180642016-02-22 12:23:36 +0100268 git_config_set("core.logallrefupdates", "true");
Nguyễn Thái Ngọc Duy6311cfa2016-09-25 10:14:39 +0700269 if (needs_work_tree_config(original_git_dir, work_tree))
Patrick Steinhardt3d180642016-02-22 12:23:36 +0100270 git_config_set("core.worktree", work_tree);
Junio C Hamano7d1864c2007-01-07 02:00:28 -0800271 }
Junio C Hamano75d24492007-08-31 00:25:04 -0700272
Junio C Hamano75d24492007-08-31 00:25:04 -0700273 if (!reinit) {
Dmitry Potapov24554062008-05-11 18:16:39 +0200274 /* Check if symlink is supported in the work tree */
Jeff King9c283902015-10-04 23:46:04 -0400275 path = git_path_buf(&buf, "tXXXXXX");
Junio C Hamano75d24492007-08-31 00:25:04 -0700276 if (!close(xmkstemp(path)) &&
277 !unlink(path) &&
278 !symlink("testing", path) &&
279 !lstat(path, &st1) &&
280 S_ISLNK(st1.st_mode))
281 unlink(path); /* good */
282 else
Patrick Steinhardt3d180642016-02-22 12:23:36 +0100283 git_config_set("core.symlinks", "false");
Dmitry Potapov24554062008-05-11 18:16:39 +0200284
285 /* Check if the filesystem is case-insensitive */
Jeff King9c283902015-10-04 23:46:04 -0400286 path = git_path_buf(&buf, "CoNfIg");
Dmitry Potapov24554062008-05-11 18:16:39 +0200287 if (!access(path, F_OK))
Patrick Steinhardt3d180642016-02-22 12:23:36 +0100288 git_config_set("core.ignorecase", "true");
Jeff Kingfdf72962015-10-04 23:45:26 -0400289 probe_utf8_pathname_composition();
Junio C Hamano75d24492007-08-31 00:25:04 -0700290 }
291
Jeff King9c283902015-10-04 23:46:04 -0400292 strbuf_release(&buf);
Shawn O. Pearceef0a89a2006-12-15 00:44:58 -0500293 return reinit;
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700294}
295
Jonathan Nieder91739122010-10-03 23:34:27 -0500296static void create_object_directory(void)
297{
Jeff King9c283902015-10-04 23:46:04 -0400298 struct strbuf path = STRBUF_INIT;
299 size_t baselen;
Jonathan Nieder91739122010-10-03 23:34:27 -0500300
Jeff King9c283902015-10-04 23:46:04 -0400301 strbuf_addstr(&path, get_object_directory());
302 baselen = path.len;
Jonathan Nieder91739122010-10-03 23:34:27 -0500303
Jeff King9c283902015-10-04 23:46:04 -0400304 safe_create_dir(path.buf, 1);
Jonathan Nieder91739122010-10-03 23:34:27 -0500305
Jeff King9c283902015-10-04 23:46:04 -0400306 strbuf_setlen(&path, baselen);
307 strbuf_addstr(&path, "/pack");
308 safe_create_dir(path.buf, 1);
309
310 strbuf_setlen(&path, baselen);
311 strbuf_addstr(&path, "/info");
312 safe_create_dir(path.buf, 1);
313
314 strbuf_release(&path);
Jonathan Nieder91739122010-10-03 23:34:27 -0500315}
316
Nguyễn Thái Ngọc Duy822d9402016-09-25 10:14:40 +0700317static void separate_git_dir(const char *git_dir, const char *git_link)
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700318{
319 struct stat st;
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700320
321 if (!stat(git_link, &st)) {
322 const char *src;
323
324 if (S_ISREG(st.st_mode))
Junio C Hamano13d6ec92011-08-22 14:04:56 -0700325 src = read_gitfile(git_link);
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700326 else if (S_ISDIR(st.st_mode))
327 src = git_link;
328 else
Ævar Arnfjörð Bjarmason97f261b2011-12-20 23:27:41 +0000329 die(_("unable to handle file type %d"), (int)st.st_mode);
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700330
331 if (rename(src, git_dir))
Ævar Arnfjörð Bjarmason2c050e02011-04-10 19:34:02 +0000332 die_errno(_("unable to move %s to %s"), src, git_dir);
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700333 }
334
Junio C Hamano1f76a102015-08-24 13:20:39 -0700335 write_file(git_link, "gitdir: %s", git_dir);
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700336}
337
Nguyễn Thái Ngọc Duy33158702016-09-25 10:14:37 +0700338int init_db(const char *git_dir, const char *real_git_dir,
339 const char *template_dir, unsigned int flags)
Junio C Hamano6adcca32007-08-27 00:58:06 -0700340{
Jonathan Nieder91739122010-10-03 23:34:27 -0500341 int reinit;
Nguyễn Thái Ngọc Duy1bd19072016-09-25 10:14:38 +0700342 int exist_ok = flags & INIT_DB_EXIST_OK;
Johannes Schindelince83ead2017-03-08 16:43:40 +0100343 char *original_git_dir = real_pathdup(git_dir, 1);
Linus Torvaldse83c5162005-04-07 15:13:13 -0700344
Nguyễn Thái Ngọc Duy1bd19072016-09-25 10:14:38 +0700345 if (real_git_dir) {
346 struct stat st;
Nguyễn Thái Ngọc Duy33158702016-09-25 10:14:37 +0700347
Nguyễn Thái Ngọc Duy1bd19072016-09-25 10:14:38 +0700348 if (!exist_ok && !stat(git_dir, &st))
349 die(_("%s already exists"), git_dir);
350
351 if (!exist_ok && !stat(real_git_dir, &st))
352 die(_("%s already exists"), real_git_dir);
353
Nguyễn Thái Ngọc Duy1bd19072016-09-25 10:14:38 +0700354 set_git_dir(real_path(real_git_dir));
Nguyễn Thái Ngọc Duy822d9402016-09-25 10:14:40 +0700355 git_dir = get_git_dir();
356 separate_git_dir(git_dir, original_git_dir);
Nguyễn Thái Ngọc Duy1bd19072016-09-25 10:14:38 +0700357 }
358 else {
359 set_git_dir(real_path(git_dir));
Nguyễn Thái Ngọc Duy822d9402016-09-25 10:14:40 +0700360 git_dir = get_git_dir();
Nguyễn Thái Ngọc Duy1bd19072016-09-25 10:14:38 +0700361 }
362 startup_info->have_repository = 1;
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700363
364 safe_create_dir(git_dir, 0);
Junio C Hamano4f629532005-11-25 16:03:56 -0800365
Deskin Miller0a2c7ee2008-10-07 01:37:48 -0400366 init_is_bare_repository = is_bare_repository();
367
Junio C Hamano4f629532005-11-25 16:03:56 -0800368 /* Check to see if the repository version is right.
369 * Note that a newly created repository does not have
370 * config file, so this will not fail. What we are catching
371 * is an attempt to reinitialize new repository with an old tool.
372 */
373 check_repository_format();
374
Nguyễn Thái Ngọc Duy6311cfa2016-09-25 10:14:39 +0700375 reinit = create_default_files(template_dir, original_git_dir);
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700376
Jonathan Nieder91739122010-10-03 23:34:27 -0500377 create_object_directory();
Johannes Schindelinaf6e2772005-12-22 23:19:37 +0100378
Jeff King7875acb2016-03-11 17:36:49 -0500379 if (get_shared_repository()) {
Junio C Hamano94df2502006-06-09 23:09:49 -0700380 char buf[10];
381 /* We do not spell "group" and such, so that
382 * the configuration can be read by older version
Heikki Orsila06cbe852008-04-16 11:34:24 +0300383 * of git. Note, we use octal numbers for new share modes,
384 * and compatibility values for PERM_GROUP and
385 * PERM_EVERYBODY.
Junio C Hamano94df2502006-06-09 23:09:49 -0700386 */
Jeff King7875acb2016-03-11 17:36:49 -0500387 if (get_shared_repository() < 0)
Junio C Hamano5a688fe2009-03-25 16:19:36 -0700388 /* force to the mode value */
Jeff King7875acb2016-03-11 17:36:49 -0500389 xsnprintf(buf, sizeof(buf), "0%o", -get_shared_repository());
390 else if (get_shared_repository() == PERM_GROUP)
Jeff King5096d492015-09-24 17:06:08 -0400391 xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_GROUP);
Jeff King7875acb2016-03-11 17:36:49 -0500392 else if (get_shared_repository() == PERM_EVERYBODY)
Jeff King5096d492015-09-24 17:06:08 -0400393 xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_EVERYBODY);
Heikki Orsila06cbe852008-04-16 11:34:24 +0300394 else
Johannes Schindelin033abf92018-05-02 11:38:39 +0200395 BUG("invalid value for shared_repository");
Patrick Steinhardt3d180642016-02-22 12:23:36 +0100396 git_config_set("core.sharedrepository", buf);
397 git_config_set("receive.denyNonFastforwards", "true");
Junio C Hamano94df2502006-06-09 23:09:49 -0700398 }
Johannes Schindelinaf6e2772005-12-22 23:19:37 +0100399
Nguyễn Thái Ngọc Duyd06f15d2010-02-14 22:44:42 +0700400 if (!(flags & INIT_DB_QUIET)) {
Nguyễn Thái Ngọc Duyd06f15d2010-02-14 22:44:42 +0700401 int len = strlen(git_dir);
Ævar Arnfjörð Bjarmason3e5dd7e2011-02-22 23:41:25 +0000402
Vasco Almeidac30364d2016-06-17 21:54:11 +0000403 if (reinit)
404 printf(get_shared_repository()
405 ? _("Reinitialized existing shared Git repository in %s%s\n")
406 : _("Reinitialized existing Git repository in %s%s\n"),
407 git_dir, len && git_dir[len-1] != '/' ? "/" : "");
408 else
409 printf(get_shared_repository()
410 ? _("Initialized empty shared Git repository in %s%s\n")
411 : _("Initialized empty Git repository in %s%s\n"),
412 git_dir, len && git_dir[len-1] != '/' ? "/" : "");
Nguyễn Thái Ngọc Duyd06f15d2010-02-14 22:44:42 +0700413 }
Shawn O. Pearceef0a89a2006-12-15 00:44:58 -0500414
Nguyễn Thái Ngọc Duy6311cfa2016-09-25 10:14:39 +0700415 free(original_git_dir);
Linus Torvaldse83c5162005-04-07 15:13:13 -0700416 return 0;
417}
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400418
419static int guess_repository_type(const char *git_dir)
420{
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400421 const char *slash;
René Scharfe56b9f6e2014-07-28 20:30:39 +0200422 char *cwd;
423 int cwd_is_git_dir;
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400424
425 /*
426 * "GIT_DIR=. git init" is always bare.
427 * "GIT_DIR=`pwd` git init" too.
428 */
429 if (!strcmp(".", git_dir))
430 return 1;
René Scharfe56b9f6e2014-07-28 20:30:39 +0200431 cwd = xgetcwd();
432 cwd_is_git_dir = !strcmp(git_dir, cwd);
433 free(cwd);
434 if (cwd_is_git_dir)
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400435 return 1;
436 /*
437 * "GIT_DIR=.git or GIT_DIR=something/.git is usually not.
438 */
439 if (!strcmp(git_dir, ".git"))
440 return 0;
441 slash = strrchr(git_dir, '/');
442 if (slash && !strcmp(slash, "/.git"))
443 return 0;
444
445 /*
446 * Otherwise it is often bare. At this point
447 * we are just guessing.
448 */
449 return 1;
450}
451
Michał Kiedrowicz596f91e2009-07-12 12:24:32 +0200452static int shared_callback(const struct option *opt, const char *arg, int unset)
453{
454 *((int *) opt->value) = (arg) ? git_config_perm("arg", arg) : PERM_GROUP;
455 return 0;
456}
457
458static const char *const init_db_usage[] = {
Alex Henrie9c9b4f22015-01-13 00:44:47 -0700459 N_("git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]] [<directory>]"),
Michał Kiedrowicz596f91e2009-07-12 12:24:32 +0200460 NULL
461};
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400462
463/*
464 * If you want to, you can share the DB area with any number of branches.
465 * That has advantages: you can save space by sharing all the SHA1 objects.
466 * On the other hand, it might just make lookup slower and messier. You
467 * be the judge. The default case is to have one DB per managed directory.
468 */
469int cmd_init_db(int argc, const char **argv, const char *prefix)
470{
471 const char *git_dir;
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700472 const char *real_git_dir = NULL;
Nguyễn Thái Ngọc Duy83518362010-11-26 22:32:40 +0700473 const char *work_tree;
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400474 const char *template_dir = NULL;
475 unsigned int flags = 0;
Michał Kiedrowicz596f91e2009-07-12 12:24:32 +0200476 const struct option init_db_options[] = {
Nguyễn Thái Ngọc Duyce4a5e52012-08-20 19:32:18 +0700477 OPT_STRING(0, "template", &template_dir, N_("template-directory"),
478 N_("directory from which templates will be used")),
Michał Kiedrowicz596f91e2009-07-12 12:24:32 +0200479 OPT_SET_INT(0, "bare", &is_bare_repository_cfg,
Nguyễn Thái Ngọc Duyce4a5e52012-08-20 19:32:18 +0700480 N_("create a bare repository"), 1),
Michał Kiedrowicz596f91e2009-07-12 12:24:32 +0200481 { OPTION_CALLBACK, 0, "shared", &init_shared_repository,
Nguyễn Thái Ngọc Duyce4a5e52012-08-20 19:32:18 +0700482 N_("permissions"),
483 N_("specify that the git repository is to be shared amongst several users"),
Michał Kiedrowicz596f91e2009-07-12 12:24:32 +0200484 PARSE_OPT_OPTARG | PARSE_OPT_NONEG, shared_callback, 0},
Nguyễn Thái Ngọc Duyce4a5e52012-08-20 19:32:18 +0700485 OPT_BIT('q', "quiet", &flags, N_("be quiet"), INIT_DB_QUIET),
486 OPT_STRING(0, "separate-git-dir", &real_git_dir, N_("gitdir"),
487 N_("separate git dir from working tree")),
Michał Kiedrowicz596f91e2009-07-12 12:24:32 +0200488 OPT_END()
489 };
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400490
Junio C Hamano0397ff22009-08-05 12:39:33 -0700491 argc = parse_options(argc, argv, prefix, init_db_options, init_db_usage, 0);
Michał Kiedrowicz596f91e2009-07-12 12:24:32 +0200492
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700493 if (real_git_dir && !is_absolute_path(real_git_dir))
Johannes Schindelince83ead2017-03-08 16:43:40 +0100494 real_git_dir = real_pathdup(real_git_dir, 1);
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700495
Junio C Hamano0397ff22009-08-05 12:39:33 -0700496 if (argc == 1) {
Nanako Shiraishi53d48882009-07-25 06:59:28 +0900497 int mkdir_tried = 0;
498 retry:
Junio C Hamano0397ff22009-08-05 12:39:33 -0700499 if (chdir(argv[0]) < 0) {
Nanako Shiraishi53d48882009-07-25 06:59:28 +0900500 if (!mkdir_tried) {
501 int saved;
502 /*
503 * At this point we haven't read any configuration,
504 * and we know shared_repository should always be 0;
505 * but just in case we play safe.
506 */
Jeff King7875acb2016-03-11 17:36:49 -0500507 saved = get_shared_repository();
508 set_shared_repository(0);
Junio C Hamano0397ff22009-08-05 12:39:33 -0700509 switch (safe_create_leading_directories_const(argv[0])) {
Michael Haggertyf3565c02014-01-06 14:45:26 +0100510 case SCLD_OK:
511 case SCLD_PERMS:
512 break;
Michael Haggerty0be05212014-01-06 14:45:25 +0100513 case SCLD_EXISTS:
Nanako Shiraishi53d48882009-07-25 06:59:28 +0900514 errno = EEXIST;
515 /* fallthru */
Nanako Shiraishi53d48882009-07-25 06:59:28 +0900516 default:
Michael Haggertyf3565c02014-01-06 14:45:26 +0100517 die_errno(_("cannot mkdir %s"), argv[0]);
Nanako Shiraishi53d48882009-07-25 06:59:28 +0900518 break;
519 }
Jeff King7875acb2016-03-11 17:36:49 -0500520 set_shared_repository(saved);
Junio C Hamano0397ff22009-08-05 12:39:33 -0700521 if (mkdir(argv[0], 0777) < 0)
Ævar Arnfjörð Bjarmason33e92e42011-02-22 23:41:24 +0000522 die_errno(_("cannot mkdir %s"), argv[0]);
Nanako Shiraishi53d48882009-07-25 06:59:28 +0900523 mkdir_tried = 1;
524 goto retry;
525 }
Ævar Arnfjörð Bjarmason33e92e42011-02-22 23:41:24 +0000526 die_errno(_("cannot chdir to %s"), argv[0]);
Nanako Shiraishi53d48882009-07-25 06:59:28 +0900527 }
Junio C Hamano0397ff22009-08-05 12:39:33 -0700528 } else if (0 < argc) {
529 usage(init_db_usage[0]);
Nanako Shiraishi53d48882009-07-25 06:59:28 +0900530 }
Junio C Hamano0397ff22009-08-05 12:39:33 -0700531 if (is_bare_repository_cfg == 1) {
René Scharfe4d3ab442014-07-28 20:31:57 +0200532 char *cwd = xgetcwd();
533 setenv(GIT_DIR_ENVIRONMENT, cwd, argc > 0);
534 free(cwd);
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400535 }
536
Junio C Hamano5a688fe2009-03-25 16:19:36 -0700537 if (init_shared_repository != -1)
Jeff King7875acb2016-03-11 17:36:49 -0500538 set_shared_repository(init_shared_repository);
Junio C Hamano5a688fe2009-03-25 16:19:36 -0700539
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400540 /*
541 * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
542 * without --bare. Catch the error early.
543 */
544 git_dir = getenv(GIT_DIR_ENVIRONMENT);
Nguyễn Thái Ngọc Duy83518362010-11-26 22:32:40 +0700545 work_tree = getenv(GIT_WORK_TREE_ENVIRONMENT);
546 if ((!git_dir || is_bare_repository_cfg == 1) && work_tree)
Ævar Arnfjörð Bjarmason33e92e42011-02-22 23:41:24 +0000547 die(_("%s (or --work-tree=<directory>) not allowed without "
548 "specifying %s (or --git-dir=<directory>)"),
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400549 GIT_WORK_TREE_ENVIRONMENT,
550 GIT_DIR_ENVIRONMENT);
551
552 /*
553 * Set up the default .git directory contents
554 */
555 if (!git_dir)
556 git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
557
558 if (is_bare_repository_cfg < 0)
559 is_bare_repository_cfg = guess_repository_type(git_dir);
560
561 if (!is_bare_repository_cfg) {
Nguyễn Thái Ngọc Duyb31d2022011-03-03 19:34:51 +0700562 const char *git_dir_parent = strrchr(git_dir, '/');
563 if (git_dir_parent) {
564 char *rel = xstrndup(git_dir, git_dir_parent - git_dir);
Johannes Schindelince83ead2017-03-08 16:43:40 +0100565 git_work_tree_cfg = real_pathdup(rel, 1);
Nguyễn Thái Ngọc Duyb31d2022011-03-03 19:34:51 +0700566 free(rel);
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400567 }
René Scharfe56b9f6e2014-07-28 20:30:39 +0200568 if (!git_work_tree_cfg)
569 git_work_tree_cfg = xgetcwd();
Nguyễn Thái Ngọc Duy83518362010-11-26 22:32:40 +0700570 if (work_tree)
René Scharfe2d186c82014-07-28 20:42:05 +0200571 set_git_work_tree(work_tree);
Nguyễn Thái Ngọc Duy83518362010-11-26 22:32:40 +0700572 else
573 set_git_work_tree(git_work_tree_cfg);
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400574 if (access(get_git_work_tree(), X_OK))
Ævar Arnfjörð Bjarmason33e92e42011-02-22 23:41:24 +0000575 die_errno (_("Cannot access work tree '%s'"),
Thomas Rast0721c312009-06-27 17:58:47 +0200576 get_git_work_tree());
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400577 }
Nguyễn Thái Ngọc Duy83518362010-11-26 22:32:40 +0700578 else {
579 if (work_tree)
René Scharfe2d186c82014-07-28 20:42:05 +0200580 set_git_work_tree(work_tree);
Nguyễn Thái Ngọc Duy83518362010-11-26 22:32:40 +0700581 }
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400582
Jeff King0e5bba52017-09-08 02:38:41 -0400583 UNLEAK(real_git_dir);
584
Nguyễn Thái Ngọc Duy33158702016-09-25 10:14:37 +0700585 flags |= INIT_DB_EXIST_OK;
586 return init_db(git_dir, real_git_dir, template_dir, flags);
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400587}