blob: 56f85e239ae0d2607c38e3c46013007ba9e71b12 [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"
Timo Hirvonenc3c88352006-05-19 13:03:57 +03007#include "builtin.h"
Johannes Sixta47d1812007-11-13 21:05:04 +01008#include "exec_cmd.h"
Michał Kiedrowicz596f91e2009-07-12 12:24:32 +02009#include "parse-options.h"
Linus Torvaldse83c5162005-04-07 15:13:13 -070010
Junio C Hamanod3af6212005-08-06 12:50:14 -070011#ifndef DEFAULT_GIT_TEMPLATE_DIR
Johannes Sixtd52fd422007-06-11 11:10:47 +020012#define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates"
Junio C Hamanod3af6212005-08-06 12:50:14 -070013#endif
14
Shawn O. Pearcec8697532006-12-30 23:53:55 -050015#ifdef NO_TRUSTABLE_FILEMODE
16#define TEST_FILEMODE 0
17#else
18#define TEST_FILEMODE 1
19#endif
20
Deskin Miller0a2c7ee2008-10-07 01:37:48 -040021static int init_is_bare_repository = 0;
22static int init_shared_repository = -1;
Steven Drake90b45182010-02-17 12:42:31 +130023static const char *init_db_template_dir;
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +070024static const char *git_link;
Deskin Miller0a2c7ee2008-10-07 01:37:48 -040025
Johannes Schindelinaf6e2772005-12-22 23:19:37 +010026static void safe_create_dir(const char *dir, int share)
Zach Welchcb126d82005-04-19 21:48:15 -070027{
Junio C Hamanof312de02005-07-06 01:21:46 -070028 if (mkdir(dir, 0777) < 0) {
Zach Welchcb126d82005-04-19 21:48:15 -070029 if (errno != EEXIST) {
30 perror(dir);
31 exit(1);
32 }
33 }
Johannes Schindelinaf6e2772005-12-22 23:19:37 +010034 else if (share && adjust_shared_perm(dir))
Ævar Arnfjörð Bjarmason33e92e42011-02-22 23:41:24 +000035 die(_("Could not make %s writable by group"), dir);
Zach Welchcb126d82005-04-19 21:48:15 -070036}
37
Junio C Hamano8d5afef2005-08-02 16:45:21 -070038static void copy_templates_1(char *path, int baselen,
39 char *template, int template_baselen,
40 DIR *dir)
41{
42 struct dirent *de;
43
44 /* Note: if ".git/hooks" file exists in the repository being
45 * re-initialized, /etc/core-git/templates/hooks/update would
Heikki Orsilaf18d2442008-09-13 20:18:36 +030046 * cause "git init" to fail here. I think this is sane but
Junio C Hamano8d5afef2005-08-02 16:45:21 -070047 * it means that the set of templates we ship by default, along
48 * with the way the namespace under .git/ is organized, should
49 * be really carefully chosen.
50 */
Johannes Schindelinaf6e2772005-12-22 23:19:37 +010051 safe_create_dir(path, 1);
Junio C Hamano8d5afef2005-08-02 16:45:21 -070052 while ((de = readdir(dir)) != NULL) {
53 struct stat st_git, st_template;
54 int namelen;
55 int exists = 0;
56
57 if (de->d_name[0] == '.')
58 continue;
59 namelen = strlen(de->d_name);
60 if ((PATH_MAX <= baselen + namelen) ||
61 (PATH_MAX <= template_baselen + namelen))
Ævar Arnfjörð Bjarmason33e92e42011-02-22 23:41:24 +000062 die(_("insanely long template name %s"), de->d_name);
Junio C Hamano8d5afef2005-08-02 16:45:21 -070063 memcpy(path + baselen, de->d_name, namelen+1);
64 memcpy(template + template_baselen, de->d_name, namelen+1);
65 if (lstat(path, &st_git)) {
66 if (errno != ENOENT)
Ævar Arnfjörð Bjarmason33e92e42011-02-22 23:41:24 +000067 die_errno(_("cannot stat '%s'"), path);
Junio C Hamano8d5afef2005-08-02 16:45:21 -070068 }
69 else
70 exists = 1;
71
72 if (lstat(template, &st_template))
Ævar Arnfjörð Bjarmason33e92e42011-02-22 23:41:24 +000073 die_errno(_("cannot stat template '%s'"), template);
Junio C Hamano8d5afef2005-08-02 16:45:21 -070074
75 if (S_ISDIR(st_template.st_mode)) {
76 DIR *subdir = opendir(template);
77 int baselen_sub = baselen + namelen;
78 int template_baselen_sub = template_baselen + namelen;
79 if (!subdir)
Ævar Arnfjörð Bjarmason33e92e42011-02-22 23:41:24 +000080 die_errno(_("cannot opendir '%s'"), template);
Junio C Hamano8d5afef2005-08-02 16:45:21 -070081 path[baselen_sub++] =
82 template[template_baselen_sub++] = '/';
83 path[baselen_sub] =
84 template[template_baselen_sub] = 0;
85 copy_templates_1(path, baselen_sub,
86 template, template_baselen_sub,
87 subdir);
88 closedir(subdir);
89 }
90 else if (exists)
91 continue;
92 else if (S_ISLNK(st_template.st_mode)) {
93 char lnk[256];
94 int len;
95 len = readlink(template, lnk, sizeof(lnk));
96 if (len < 0)
Ævar Arnfjörð Bjarmason33e92e42011-02-22 23:41:24 +000097 die_errno(_("cannot readlink '%s'"), template);
Junio C Hamano8d5afef2005-08-02 16:45:21 -070098 if (sizeof(lnk) <= len)
Ævar Arnfjörð Bjarmason33e92e42011-02-22 23:41:24 +000099 die(_("insanely long symlink %s"), template);
Junio C Hamano8d5afef2005-08-02 16:45:21 -0700100 lnk[len] = 0;
101 if (symlink(lnk, path))
Ævar Arnfjörð Bjarmason33e92e42011-02-22 23:41:24 +0000102 die_errno(_("cannot symlink '%s' '%s'"), lnk, path);
Junio C Hamano8d5afef2005-08-02 16:45:21 -0700103 }
104 else if (S_ISREG(st_template.st_mode)) {
105 if (copy_file(path, template, st_template.st_mode))
Ævar Arnfjörð Bjarmason33e92e42011-02-22 23:41:24 +0000106 die_errno(_("cannot copy '%s' to '%s'"), template,
Thomas Rast0721c312009-06-27 17:58:47 +0200107 path);
Junio C Hamano8d5afef2005-08-02 16:45:21 -0700108 }
109 else
Ævar Arnfjörð Bjarmason33e92e42011-02-22 23:41:24 +0000110 error(_("ignoring template %s"), template);
Junio C Hamano8d5afef2005-08-02 16:45:21 -0700111 }
112}
113
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400114static void copy_templates(const char *template_dir)
Junio C Hamano8d5afef2005-08-02 16:45:21 -0700115{
116 char path[PATH_MAX];
117 char template_path[PATH_MAX];
Junio C Hamanod3af6212005-08-06 12:50:14 -0700118 int template_len;
Junio C Hamano8d5afef2005-08-02 16:45:21 -0700119 DIR *dir;
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400120 const char *git_dir = get_git_dir();
121 int len = strlen(git_dir);
Junio C Hamano8d5afef2005-08-02 16:45:21 -0700122
Johannes Sixta47d1812007-11-13 21:05:04 +0100123 if (!template_dir)
Junio C Hamanod4ebc362006-12-19 01:28:15 -0800124 template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT);
Steffen Prohaska2de9de52008-07-13 22:31:18 +0200125 if (!template_dir)
Steven Drake90b45182010-02-17 12:42:31 +1300126 template_dir = init_db_template_dir;
127 if (!template_dir)
Steffen Prohaska2de9de52008-07-13 22:31:18 +0200128 template_dir = system_path(DEFAULT_GIT_TEMPLATE_DIR);
Jeff King172035f2008-07-28 02:02:04 -0400129 if (!template_dir[0])
130 return;
Frank Lichtenheld32d17762009-04-18 16:14:02 +0200131 template_len = strlen(template_dir);
132 if (PATH_MAX <= (template_len+strlen("/config")))
Ævar Arnfjörð Bjarmason33e92e42011-02-22 23:41:24 +0000133 die(_("insanely long template path %s"), template_dir);
Junio C Hamano8d5afef2005-08-02 16:45:21 -0700134 strcpy(template_path, template_dir);
Junio C Hamano8d5afef2005-08-02 16:45:21 -0700135 if (template_path[template_len-1] != '/') {
136 template_path[template_len++] = '/';
137 template_path[template_len] = 0;
138 }
Junio C Hamano8d5afef2005-08-02 16:45:21 -0700139 dir = opendir(template_path);
Junio C Hamanod3af6212005-08-06 12:50:14 -0700140 if (!dir) {
Ævar Arnfjörð Bjarmason33e92e42011-02-22 23:41:24 +0000141 warning(_("templates not found %s"), template_dir);
Junio C Hamano8d5afef2005-08-02 16:45:21 -0700142 return;
Junio C Hamanod3af6212005-08-06 12:50:14 -0700143 }
144
Junio C Hamano4f629532005-11-25 16:03:56 -0800145 /* Make sure that template is from the correct vintage */
146 strcpy(template_path + template_len, "config");
147 repository_format_version = 0;
148 git_config_from_file(check_repository_format_version,
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100149 template_path, NULL);
Junio C Hamano4f629532005-11-25 16:03:56 -0800150 template_path[template_len] = 0;
151
152 if (repository_format_version &&
153 repository_format_version != GIT_REPO_VERSION) {
Ævar Arnfjörð Bjarmason33e92e42011-02-22 23:41:24 +0000154 warning(_("not copying templates of "
155 "a wrong format version %d from '%s'"),
Junio C Hamano4f629532005-11-25 16:03:56 -0800156 repository_format_version,
157 template_dir);
158 closedir(dir);
159 return;
160 }
161
Junio C Hamanod3af6212005-08-06 12:50:14 -0700162 memcpy(path, git_dir, len);
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400163 if (len && path[len - 1] != '/')
164 path[len++] = '/';
Petr Baudis1f961c12005-09-20 02:19:50 +0200165 path[len] = 0;
Junio C Hamano8d5afef2005-08-02 16:45:21 -0700166 copy_templates_1(path, len,
167 template_path, template_len,
168 dir);
169 closedir(dir);
170}
171
Steven Drake90b45182010-02-17 12:42:31 +1300172static int git_init_db_config(const char *k, const char *v, void *cb)
173{
Steven Drake90b45182010-02-17 12:42:31 +1300174 if (!strcmp(k, "init.templatedir"))
175 return git_config_pathname(&init_db_template_dir, k, v);
176
177 return 0;
178}
179
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400180static int create_default_files(const char *template_path)
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700181{
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400182 const char *git_dir = get_git_dir();
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700183 unsigned len = strlen(git_dir);
184 static char path[PATH_MAX];
Junio C Hamano4f629532005-11-25 16:03:56 -0800185 struct stat st1;
186 char repo_version_string[10];
Johannes Schindelin5cc8f372008-03-24 16:14:52 +0100187 char junk[2];
Shawn O. Pearceef0a89a2006-12-15 00:44:58 -0500188 int reinit;
Shawn O. Pearcec8697532006-12-30 23:53:55 -0500189 int filemode;
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700190
191 if (len > sizeof(path)-50)
Ævar Arnfjörð Bjarmason33e92e42011-02-22 23:41:24 +0000192 die(_("insane git directory %s"), git_dir);
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700193 memcpy(path, git_dir, len);
194
195 if (len && path[len-1] != '/')
196 path[len++] = '/';
197
198 /*
199 * Create .git/refs/{heads,tags}
200 */
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400201 safe_create_dir(git_path("refs"), 1);
202 safe_create_dir(git_path("refs/heads"), 1);
203 safe_create_dir(git_path("refs/tags"), 1);
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700204
Steven Drake90b45182010-02-17 12:42:31 +1300205 /* Just look for `init.templatedir` */
206 git_config(git_init_db_config, NULL);
207
Junio C Hamano4f629532005-11-25 16:03:56 -0800208 /* First copy the templates -- we might have the default
209 * config file there, in which case we would want to read
210 * from it after installing.
211 */
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400212 copy_templates(template_path);
Junio C Hamano4f629532005-11-25 16:03:56 -0800213
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100214 git_config(git_default_config, NULL);
Deskin Miller0a2c7ee2008-10-07 01:37:48 -0400215 is_bare_repository_cfg = init_is_bare_repository;
Junio C Hamano5a688fe2009-03-25 16:19:36 -0700216
217 /* reading existing config may have overwrote it */
Deskin Miller0a2c7ee2008-10-07 01:37:48 -0400218 if (init_shared_repository != -1)
219 shared_repository = init_shared_repository;
Junio C Hamano4f629532005-11-25 16:03:56 -0800220
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700221 /*
Junio C Hamano138086a2006-06-09 22:07:23 -0700222 * We would have created the above under user's umask -- under
223 * shared-repository settings, we would need to fix them up.
224 */
225 if (shared_repository) {
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400226 adjust_shared_perm(get_git_dir());
227 adjust_shared_perm(git_path("refs"));
228 adjust_shared_perm(git_path("refs/heads"));
229 adjust_shared_perm(git_path("refs/tags"));
Junio C Hamano138086a2006-06-09 22:07:23 -0700230 }
231
232 /*
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700233 * Create the default symlink from ".git/HEAD" to the "master"
Junio C Hamano8098a172005-09-30 14:26:57 -0700234 * branch, if it does not exist yet.
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700235 */
236 strcpy(path + len, "HEAD");
Johannes Schindelin5cc8f372008-03-24 16:14:52 +0100237 reinit = (!access(path, R_OK)
238 || readlink(path, junk, sizeof(junk)-1) != -1);
Shawn O. Pearceef0a89a2006-12-15 00:44:58 -0500239 if (!reinit) {
Nicolas Pitre8b5157e2007-01-26 17:26:10 -0500240 if (create_symref("HEAD", "refs/heads/master", NULL) < 0)
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700241 exit(1);
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700242 }
Junio C Hamano4f629532005-11-25 16:03:56 -0800243
244 /* This forces creation of new config file */
245 sprintf(repo_version_string, "%d", GIT_REPO_VERSION);
246 git_config_set("core.repositoryformatversion", repo_version_string);
247
Junio C Hamano8098a172005-09-30 14:26:57 -0700248 path[len] = 0;
Johannes Schindeline24317b2005-10-26 01:43:03 +0200249 strcpy(path + len, "config");
Johannes Schindeline24317b2005-10-26 01:43:03 +0200250
Junio C Hamano4f629532005-11-25 16:03:56 -0800251 /* Check filemode trustability */
Shawn O. Pearcec8697532006-12-30 23:53:55 -0500252 filemode = TEST_FILEMODE;
253 if (TEST_FILEMODE && !lstat(path, &st1)) {
Junio C Hamano4f629532005-11-25 16:03:56 -0800254 struct stat st2;
Shawn O. Pearcec8697532006-12-30 23:53:55 -0500255 filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) &&
Junio C Hamano4f629532005-11-25 16:03:56 -0800256 !lstat(path, &st2) &&
257 st1.st_mode != st2.st_mode);
Johannes Schindeline24317b2005-10-26 01:43:03 +0200258 }
Shawn O. Pearcec8697532006-12-30 23:53:55 -0500259 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())
Junio C Hamano7d1864c2007-01-07 02:00:28 -0800262 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();
Junio C Hamano7d1864c2007-01-07 02:00:28 -0800265 git_config_set("core.bare", "false");
Alex Riesen196055c2007-01-23 16:51:18 +0100266 /* allow template config file to override the default */
267 if (log_all_ref_updates == -1)
268 git_config_set("core.logallrefupdates", "true");
Christian Couder59556542013-11-30 21:55:40 +0100269 if (!starts_with(git_dir, work_tree) ||
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400270 strcmp(git_dir + strlen(work_tree), "/.git")) {
Johannes Schindeline90fdc32007-08-01 01:30:14 +0100271 git_config_set("core.worktree", work_tree);
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400272 }
Junio C Hamano7d1864c2007-01-07 02:00:28 -0800273 }
Junio C Hamano75d24492007-08-31 00:25:04 -0700274
Junio C Hamano75d24492007-08-31 00:25:04 -0700275 if (!reinit) {
Dmitry Potapov24554062008-05-11 18:16:39 +0200276 /* Check if symlink is supported in the work tree */
Junio C Hamano75d24492007-08-31 00:25:04 -0700277 path[len] = 0;
278 strcpy(path + len, "tXXXXXX");
279 if (!close(xmkstemp(path)) &&
280 !unlink(path) &&
281 !symlink("testing", path) &&
282 !lstat(path, &st1) &&
283 S_ISLNK(st1.st_mode))
284 unlink(path); /* good */
285 else
286 git_config_set("core.symlinks", "false");
Dmitry Potapov24554062008-05-11 18:16:39 +0200287
288 /* Check if the filesystem is case-insensitive */
289 path[len] = 0;
290 strcpy(path + len, "CoNfIg");
291 if (!access(path, F_OK))
292 git_config_set("core.ignorecase", "true");
Torsten Bögershausen76759c72012-07-08 15:50:25 +0200293 probe_utf8_pathname_composition(path, len);
Junio C Hamano75d24492007-08-31 00:25:04 -0700294 }
295
Shawn O. Pearceef0a89a2006-12-15 00:44:58 -0500296 return reinit;
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700297}
298
Jonathan Nieder91739122010-10-03 23:34:27 -0500299static void create_object_directory(void)
300{
301 const char *object_directory = get_object_directory();
302 int len = strlen(object_directory);
303 char *path = xmalloc(len + 40);
304
305 memcpy(path, object_directory, len);
306
307 safe_create_dir(object_directory, 1);
308 strcpy(path+len, "/pack");
309 safe_create_dir(path, 1);
310 strcpy(path+len, "/info");
311 safe_create_dir(path, 1);
312
313 free(path);
314}
315
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700316int set_git_dir_init(const char *git_dir, const char *real_git_dir,
317 int exist_ok)
318{
319 if (real_git_dir) {
320 struct stat st;
321
322 if (!exist_ok && !stat(git_dir, &st))
Ævar Arnfjörð Bjarmason2c050e02011-04-10 19:34:02 +0000323 die(_("%s already exists"), git_dir);
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700324
325 if (!exist_ok && !stat(real_git_dir, &st))
Ævar Arnfjörð Bjarmason2c050e02011-04-10 19:34:02 +0000326 die(_("%s already exists"), real_git_dir);
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700327
328 /*
329 * make sure symlinks are resolved because we'll be
330 * moving the target repo later on in separate_git_dir()
331 */
332 git_link = xstrdup(real_path(git_dir));
333 }
334 else {
335 real_git_dir = real_path(git_dir);
336 git_link = NULL;
337 }
338 set_git_dir(real_path(real_git_dir));
339 return 0;
340}
341
342static void separate_git_dir(const char *git_dir)
343{
344 struct stat st;
345 FILE *fp;
346
347 if (!stat(git_link, &st)) {
348 const char *src;
349
350 if (S_ISREG(st.st_mode))
Junio C Hamano13d6ec92011-08-22 14:04:56 -0700351 src = read_gitfile(git_link);
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700352 else if (S_ISDIR(st.st_mode))
353 src = git_link;
354 else
Ævar Arnfjörð Bjarmason97f261b2011-12-20 23:27:41 +0000355 die(_("unable to handle file type %d"), (int)st.st_mode);
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700356
357 if (rename(src, git_dir))
Ævar Arnfjörð Bjarmason2c050e02011-04-10 19:34:02 +0000358 die_errno(_("unable to move %s to %s"), src, git_dir);
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700359 }
360
361 fp = fopen(git_link, "w");
362 if (!fp)
Ævar Arnfjörð Bjarmason2c050e02011-04-10 19:34:02 +0000363 die(_("Could not create git link %s"), git_link);
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700364 fprintf(fp, "gitdir: %s\n", git_dir);
365 fclose(fp);
366}
367
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400368int init_db(const char *template_dir, unsigned int flags)
Junio C Hamano6adcca32007-08-27 00:58:06 -0700369{
Jonathan Nieder91739122010-10-03 23:34:27 -0500370 int reinit;
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700371 const char *git_dir = get_git_dir();
Linus Torvaldse83c5162005-04-07 15:13:13 -0700372
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700373 if (git_link)
374 separate_git_dir(git_dir);
375
376 safe_create_dir(git_dir, 0);
Junio C Hamano4f629532005-11-25 16:03:56 -0800377
Deskin Miller0a2c7ee2008-10-07 01:37:48 -0400378 init_is_bare_repository = is_bare_repository();
379
Junio C Hamano4f629532005-11-25 16:03:56 -0800380 /* Check to see if the repository version is right.
381 * Note that a newly created repository does not have
382 * config file, so this will not fail. What we are catching
383 * is an attempt to reinitialize new repository with an old tool.
384 */
385 check_repository_format();
386
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400387 reinit = create_default_files(template_dir);
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700388
Jonathan Nieder91739122010-10-03 23:34:27 -0500389 create_object_directory();
Johannes Schindelinaf6e2772005-12-22 23:19:37 +0100390
Junio C Hamano94df2502006-06-09 23:09:49 -0700391 if (shared_repository) {
392 char buf[10];
393 /* We do not spell "group" and such, so that
394 * the configuration can be read by older version
Heikki Orsila06cbe852008-04-16 11:34:24 +0300395 * of git. Note, we use octal numbers for new share modes,
396 * and compatibility values for PERM_GROUP and
397 * PERM_EVERYBODY.
Junio C Hamano94df2502006-06-09 23:09:49 -0700398 */
Junio C Hamano5a688fe2009-03-25 16:19:36 -0700399 if (shared_repository < 0)
400 /* force to the mode value */
401 sprintf(buf, "0%o", -shared_repository);
402 else if (shared_repository == PERM_GROUP)
Heikki Orsila06cbe852008-04-16 11:34:24 +0300403 sprintf(buf, "%d", OLD_PERM_GROUP);
404 else if (shared_repository == PERM_EVERYBODY)
405 sprintf(buf, "%d", OLD_PERM_EVERYBODY);
406 else
Junio C Hamano5a688fe2009-03-25 16:19:36 -0700407 die("oops");
Junio C Hamano94df2502006-06-09 23:09:49 -0700408 git_config_set("core.sharedrepository", buf);
Johannes Schindelin11031d72006-09-21 01:07:54 +0200409 git_config_set("receive.denyNonFastforwards", "true");
Junio C Hamano94df2502006-06-09 23:09:49 -0700410 }
Johannes Schindelinaf6e2772005-12-22 23:19:37 +0100411
Nguyễn Thái Ngọc Duyd06f15d2010-02-14 22:44:42 +0700412 if (!(flags & INIT_DB_QUIET)) {
Nguyễn Thái Ngọc Duyd06f15d2010-02-14 22:44:42 +0700413 int len = strlen(git_dir);
Ævar Arnfjörð Bjarmason3e5dd7e2011-02-22 23:41:25 +0000414
Jiang Xin47fbfde2014-04-17 13:37:18 +0800415 /* TRANSLATORS: The first '%s' is either "Reinitialized
416 existing" or "Initialized empty", the second " shared" or
417 "", and the last '%s%s' is the verbatim directory name. */
Ævar Arnfjörð Bjarmason3e5dd7e2011-02-22 23:41:25 +0000418 printf(_("%s%s Git repository in %s%s\n"),
419 reinit ? _("Reinitialized existing") : _("Initialized empty"),
420 shared_repository ? _(" shared") : "",
Nguyễn Thái Ngọc Duyd06f15d2010-02-14 22:44:42 +0700421 git_dir, len && git_dir[len-1] != '/' ? "/" : "");
422 }
Shawn O. Pearceef0a89a2006-12-15 00:44:58 -0500423
Linus Torvaldse83c5162005-04-07 15:13:13 -0700424 return 0;
425}
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400426
427static int guess_repository_type(const char *git_dir)
428{
429 char cwd[PATH_MAX];
430 const char *slash;
431
432 /*
433 * "GIT_DIR=. git init" is always bare.
434 * "GIT_DIR=`pwd` git init" too.
435 */
436 if (!strcmp(".", git_dir))
437 return 1;
438 if (!getcwd(cwd, sizeof(cwd)))
Ævar Arnfjörð Bjarmason33e92e42011-02-22 23:41:24 +0000439 die_errno(_("cannot tell cwd"));
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400440 if (!strcmp(git_dir, cwd))
441 return 1;
442 /*
443 * "GIT_DIR=.git or GIT_DIR=something/.git is usually not.
444 */
445 if (!strcmp(git_dir, ".git"))
446 return 0;
447 slash = strrchr(git_dir, '/');
448 if (slash && !strcmp(slash, "/.git"))
449 return 0;
450
451 /*
452 * Otherwise it is often bare. At this point
453 * we are just guessing.
454 */
455 return 1;
456}
457
Michał Kiedrowicz596f91e2009-07-12 12:24:32 +0200458static int shared_callback(const struct option *opt, const char *arg, int unset)
459{
460 *((int *) opt->value) = (arg) ? git_config_perm("arg", arg) : PERM_GROUP;
461 return 0;
462}
463
464static const char *const init_db_usage[] = {
Nguyễn Thái Ngọc Duyce4a5e52012-08-20 19:32:18 +0700465 N_("git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]] [directory]"),
Michał Kiedrowicz596f91e2009-07-12 12:24:32 +0200466 NULL
467};
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400468
469/*
470 * If you want to, you can share the DB area with any number of branches.
471 * That has advantages: you can save space by sharing all the SHA1 objects.
472 * On the other hand, it might just make lookup slower and messier. You
473 * be the judge. The default case is to have one DB per managed directory.
474 */
475int cmd_init_db(int argc, const char **argv, const char *prefix)
476{
477 const char *git_dir;
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700478 const char *real_git_dir = NULL;
Nguyễn Thái Ngọc Duy83518362010-11-26 22:32:40 +0700479 const char *work_tree;
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400480 const char *template_dir = NULL;
481 unsigned int flags = 0;
Michał Kiedrowicz596f91e2009-07-12 12:24:32 +0200482 const struct option init_db_options[] = {
Nguyễn Thái Ngọc Duyce4a5e52012-08-20 19:32:18 +0700483 OPT_STRING(0, "template", &template_dir, N_("template-directory"),
484 N_("directory from which templates will be used")),
Michał Kiedrowicz596f91e2009-07-12 12:24:32 +0200485 OPT_SET_INT(0, "bare", &is_bare_repository_cfg,
Nguyễn Thái Ngọc Duyce4a5e52012-08-20 19:32:18 +0700486 N_("create a bare repository"), 1),
Michał Kiedrowicz596f91e2009-07-12 12:24:32 +0200487 { OPTION_CALLBACK, 0, "shared", &init_shared_repository,
Nguyễn Thái Ngọc Duyce4a5e52012-08-20 19:32:18 +0700488 N_("permissions"),
489 N_("specify that the git repository is to be shared amongst several users"),
Michał Kiedrowicz596f91e2009-07-12 12:24:32 +0200490 PARSE_OPT_OPTARG | PARSE_OPT_NONEG, shared_callback, 0},
Nguyễn Thái Ngọc Duyce4a5e52012-08-20 19:32:18 +0700491 OPT_BIT('q', "quiet", &flags, N_("be quiet"), INIT_DB_QUIET),
492 OPT_STRING(0, "separate-git-dir", &real_git_dir, N_("gitdir"),
493 N_("separate git dir from working tree")),
Michał Kiedrowicz596f91e2009-07-12 12:24:32 +0200494 OPT_END()
495 };
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400496
Junio C Hamano0397ff22009-08-05 12:39:33 -0700497 argc = parse_options(argc, argv, prefix, init_db_options, init_db_usage, 0);
Michał Kiedrowicz596f91e2009-07-12 12:24:32 +0200498
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700499 if (real_git_dir && !is_absolute_path(real_git_dir))
500 real_git_dir = xstrdup(real_path(real_git_dir));
501
Junio C Hamano0397ff22009-08-05 12:39:33 -0700502 if (argc == 1) {
Nanako Shiraishi53d48882009-07-25 06:59:28 +0900503 int mkdir_tried = 0;
504 retry:
Junio C Hamano0397ff22009-08-05 12:39:33 -0700505 if (chdir(argv[0]) < 0) {
Nanako Shiraishi53d48882009-07-25 06:59:28 +0900506 if (!mkdir_tried) {
507 int saved;
508 /*
509 * At this point we haven't read any configuration,
510 * and we know shared_repository should always be 0;
511 * but just in case we play safe.
512 */
513 saved = shared_repository;
514 shared_repository = 0;
Junio C Hamano0397ff22009-08-05 12:39:33 -0700515 switch (safe_create_leading_directories_const(argv[0])) {
Michael Haggertyf3565c02014-01-06 14:45:26 +0100516 case SCLD_OK:
517 case SCLD_PERMS:
518 break;
Michael Haggerty0be05212014-01-06 14:45:25 +0100519 case SCLD_EXISTS:
Nanako Shiraishi53d48882009-07-25 06:59:28 +0900520 errno = EEXIST;
521 /* fallthru */
Nanako Shiraishi53d48882009-07-25 06:59:28 +0900522 default:
Michael Haggertyf3565c02014-01-06 14:45:26 +0100523 die_errno(_("cannot mkdir %s"), argv[0]);
Nanako Shiraishi53d48882009-07-25 06:59:28 +0900524 break;
525 }
526 shared_repository = saved;
Junio C Hamano0397ff22009-08-05 12:39:33 -0700527 if (mkdir(argv[0], 0777) < 0)
Ævar Arnfjörð Bjarmason33e92e42011-02-22 23:41:24 +0000528 die_errno(_("cannot mkdir %s"), argv[0]);
Nanako Shiraishi53d48882009-07-25 06:59:28 +0900529 mkdir_tried = 1;
530 goto retry;
531 }
Ævar Arnfjörð Bjarmason33e92e42011-02-22 23:41:24 +0000532 die_errno(_("cannot chdir to %s"), argv[0]);
Nanako Shiraishi53d48882009-07-25 06:59:28 +0900533 }
Junio C Hamano0397ff22009-08-05 12:39:33 -0700534 } else if (0 < argc) {
535 usage(init_db_usage[0]);
Nanako Shiraishi53d48882009-07-25 06:59:28 +0900536 }
Junio C Hamano0397ff22009-08-05 12:39:33 -0700537 if (is_bare_repository_cfg == 1) {
Michał Kiedrowicz596f91e2009-07-12 12:24:32 +0200538 static char git_dir[PATH_MAX+1];
Nanako Shiraishi53d48882009-07-25 06:59:28 +0900539
540 setenv(GIT_DIR_ENVIRONMENT,
Jeff King87a074d2010-05-10 05:42:06 -0400541 getcwd(git_dir, sizeof(git_dir)), argc > 0);
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400542 }
543
Junio C Hamano5a688fe2009-03-25 16:19:36 -0700544 if (init_shared_repository != -1)
545 shared_repository = init_shared_repository;
546
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400547 /*
548 * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
549 * without --bare. Catch the error early.
550 */
551 git_dir = getenv(GIT_DIR_ENVIRONMENT);
Nguyễn Thái Ngọc Duy83518362010-11-26 22:32:40 +0700552 work_tree = getenv(GIT_WORK_TREE_ENVIRONMENT);
553 if ((!git_dir || is_bare_repository_cfg == 1) && work_tree)
Ævar Arnfjörð Bjarmason33e92e42011-02-22 23:41:24 +0000554 die(_("%s (or --work-tree=<directory>) not allowed without "
555 "specifying %s (or --git-dir=<directory>)"),
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400556 GIT_WORK_TREE_ENVIRONMENT,
557 GIT_DIR_ENVIRONMENT);
558
559 /*
560 * Set up the default .git directory contents
561 */
562 if (!git_dir)
563 git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
564
565 if (is_bare_repository_cfg < 0)
566 is_bare_repository_cfg = guess_repository_type(git_dir);
567
568 if (!is_bare_repository_cfg) {
Nguyễn Thái Ngọc Duyb31d2022011-03-03 19:34:51 +0700569 const char *git_dir_parent = strrchr(git_dir, '/');
570 if (git_dir_parent) {
571 char *rel = xstrndup(git_dir, git_dir_parent - git_dir);
Carlos Martín Nietoe2a57aa2011-03-17 12:26:46 +0100572 git_work_tree_cfg = xstrdup(real_path(rel));
Nguyễn Thái Ngọc Duyb31d2022011-03-03 19:34:51 +0700573 free(rel);
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400574 }
575 if (!git_work_tree_cfg) {
576 git_work_tree_cfg = xcalloc(PATH_MAX, 1);
577 if (!getcwd(git_work_tree_cfg, PATH_MAX))
Ævar Arnfjörð Bjarmason33e92e42011-02-22 23:41:24 +0000578 die_errno (_("Cannot access current working directory"));
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400579 }
Nguyễn Thái Ngọc Duy83518362010-11-26 22:32:40 +0700580 if (work_tree)
Carlos Martín Nietoe2a57aa2011-03-17 12:26:46 +0100581 set_git_work_tree(real_path(work_tree));
Nguyễn Thái Ngọc Duy83518362010-11-26 22:32:40 +0700582 else
583 set_git_work_tree(git_work_tree_cfg);
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400584 if (access(get_git_work_tree(), X_OK))
Ævar Arnfjörð Bjarmason33e92e42011-02-22 23:41:24 +0000585 die_errno (_("Cannot access work tree '%s'"),
Thomas Rast0721c312009-06-27 17:58:47 +0200586 get_git_work_tree());
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400587 }
Nguyễn Thái Ngọc Duy83518362010-11-26 22:32:40 +0700588 else {
589 if (work_tree)
Carlos Martín Nietoe2a57aa2011-03-17 12:26:46 +0100590 set_git_work_tree(real_path(work_tree));
Nguyễn Thái Ngọc Duy83518362010-11-26 22:32:40 +0700591 }
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400592
Nguyễn Thái Ngọc Duyb57fb802011-03-19 22:16:56 +0700593 set_git_dir_init(git_dir, real_git_dir, 1);
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400594
595 return init_db(template_dir, flags);
596}