blob: 4a5600631c5800d7f9d844118f37c0672b89b581 [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"
Linus Torvaldse83c5162005-04-07 15:13:13 -07009
Junio C Hamanod3af6212005-08-06 12:50:14 -070010#ifndef DEFAULT_GIT_TEMPLATE_DIR
Johannes Sixtd52fd422007-06-11 11:10:47 +020011#define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates"
Junio C Hamanod3af6212005-08-06 12:50:14 -070012#endif
13
Shawn O. Pearcec8697532006-12-30 23:53:55 -050014#ifdef NO_TRUSTABLE_FILEMODE
15#define TEST_FILEMODE 0
16#else
17#define TEST_FILEMODE 1
18#endif
19
Deskin Miller0a2c7ee2008-10-07 01:37:48 -040020static int init_is_bare_repository = 0;
21static int init_shared_repository = -1;
22
Johannes Schindelinaf6e2772005-12-22 23:19:37 +010023static void safe_create_dir(const char *dir, int share)
Zach Welchcb126d82005-04-19 21:48:15 -070024{
Junio C Hamanof312de02005-07-06 01:21:46 -070025 if (mkdir(dir, 0777) < 0) {
Zach Welchcb126d82005-04-19 21:48:15 -070026 if (errno != EEXIST) {
27 perror(dir);
28 exit(1);
29 }
30 }
Johannes Schindelinaf6e2772005-12-22 23:19:37 +010031 else if (share && adjust_shared_perm(dir))
Alexander Potashevd7530702009-01-04 21:38:41 +030032 die("Could not make %s writable by group", dir);
Zach Welchcb126d82005-04-19 21:48:15 -070033}
34
Junio C Hamano8d5afef2005-08-02 16:45:21 -070035static void copy_templates_1(char *path, int baselen,
36 char *template, int template_baselen,
37 DIR *dir)
38{
39 struct dirent *de;
40
41 /* Note: if ".git/hooks" file exists in the repository being
42 * re-initialized, /etc/core-git/templates/hooks/update would
Heikki Orsilaf18d2442008-09-13 20:18:36 +030043 * cause "git init" to fail here. I think this is sane but
Junio C Hamano8d5afef2005-08-02 16:45:21 -070044 * it means that the set of templates we ship by default, along
45 * with the way the namespace under .git/ is organized, should
46 * be really carefully chosen.
47 */
Johannes Schindelinaf6e2772005-12-22 23:19:37 +010048 safe_create_dir(path, 1);
Junio C Hamano8d5afef2005-08-02 16:45:21 -070049 while ((de = readdir(dir)) != NULL) {
50 struct stat st_git, st_template;
51 int namelen;
52 int exists = 0;
53
54 if (de->d_name[0] == '.')
55 continue;
56 namelen = strlen(de->d_name);
57 if ((PATH_MAX <= baselen + namelen) ||
58 (PATH_MAX <= template_baselen + namelen))
59 die("insanely long template name %s", de->d_name);
60 memcpy(path + baselen, de->d_name, namelen+1);
61 memcpy(template + template_baselen, de->d_name, namelen+1);
62 if (lstat(path, &st_git)) {
63 if (errno != ENOENT)
Thomas Rast0721c312009-06-27 17:58:47 +020064 die_errno("cannot stat '%s'", path);
Junio C Hamano8d5afef2005-08-02 16:45:21 -070065 }
66 else
67 exists = 1;
68
69 if (lstat(template, &st_template))
Thomas Rast0721c312009-06-27 17:58:47 +020070 die_errno("cannot stat template '%s'", template);
Junio C Hamano8d5afef2005-08-02 16:45:21 -070071
72 if (S_ISDIR(st_template.st_mode)) {
73 DIR *subdir = opendir(template);
74 int baselen_sub = baselen + namelen;
75 int template_baselen_sub = template_baselen + namelen;
76 if (!subdir)
Thomas Rast0721c312009-06-27 17:58:47 +020077 die_errno("cannot opendir '%s'", template);
Junio C Hamano8d5afef2005-08-02 16:45:21 -070078 path[baselen_sub++] =
79 template[template_baselen_sub++] = '/';
80 path[baselen_sub] =
81 template[template_baselen_sub] = 0;
82 copy_templates_1(path, baselen_sub,
83 template, template_baselen_sub,
84 subdir);
85 closedir(subdir);
86 }
87 else if (exists)
88 continue;
89 else if (S_ISLNK(st_template.st_mode)) {
90 char lnk[256];
91 int len;
92 len = readlink(template, lnk, sizeof(lnk));
93 if (len < 0)
Thomas Rast0721c312009-06-27 17:58:47 +020094 die_errno("cannot readlink '%s'", template);
Junio C Hamano8d5afef2005-08-02 16:45:21 -070095 if (sizeof(lnk) <= len)
96 die("insanely long symlink %s", template);
97 lnk[len] = 0;
98 if (symlink(lnk, path))
Thomas Rast0721c312009-06-27 17:58:47 +020099 die_errno("cannot symlink '%s' '%s'", lnk, path);
Junio C Hamano8d5afef2005-08-02 16:45:21 -0700100 }
101 else if (S_ISREG(st_template.st_mode)) {
102 if (copy_file(path, template, st_template.st_mode))
Thomas Rast0721c312009-06-27 17:58:47 +0200103 die_errno("cannot copy '%s' to '%s'", template,
104 path);
Junio C Hamano8d5afef2005-08-02 16:45:21 -0700105 }
106 else
107 error("ignoring template %s", template);
108 }
109}
110
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400111static void copy_templates(const char *template_dir)
Junio C Hamano8d5afef2005-08-02 16:45:21 -0700112{
113 char path[PATH_MAX];
114 char template_path[PATH_MAX];
Junio C Hamanod3af6212005-08-06 12:50:14 -0700115 int template_len;
Junio C Hamano8d5afef2005-08-02 16:45:21 -0700116 DIR *dir;
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400117 const char *git_dir = get_git_dir();
118 int len = strlen(git_dir);
Junio C Hamano8d5afef2005-08-02 16:45:21 -0700119
Johannes Sixta47d1812007-11-13 21:05:04 +0100120 if (!template_dir)
Junio C Hamanod4ebc362006-12-19 01:28:15 -0800121 template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT);
Steffen Prohaska2de9de52008-07-13 22:31:18 +0200122 if (!template_dir)
123 template_dir = system_path(DEFAULT_GIT_TEMPLATE_DIR);
Jeff King172035f2008-07-28 02:02:04 -0400124 if (!template_dir[0])
125 return;
Frank Lichtenheld32d17762009-04-18 16:14:02 +0200126 template_len = strlen(template_dir);
127 if (PATH_MAX <= (template_len+strlen("/config")))
128 die("insanely long template path %s", template_dir);
Junio C Hamano8d5afef2005-08-02 16:45:21 -0700129 strcpy(template_path, template_dir);
Junio C Hamano8d5afef2005-08-02 16:45:21 -0700130 if (template_path[template_len-1] != '/') {
131 template_path[template_len++] = '/';
132 template_path[template_len] = 0;
133 }
Junio C Hamano8d5afef2005-08-02 16:45:21 -0700134 dir = opendir(template_path);
Junio C Hamanod3af6212005-08-06 12:50:14 -0700135 if (!dir) {
Miklos Vajna2fd8c0a2009-03-24 02:09:13 +0100136 warning("templates not found %s", template_dir);
Junio C Hamano8d5afef2005-08-02 16:45:21 -0700137 return;
Junio C Hamanod3af6212005-08-06 12:50:14 -0700138 }
139
Junio C Hamano4f629532005-11-25 16:03:56 -0800140 /* Make sure that template is from the correct vintage */
141 strcpy(template_path + template_len, "config");
142 repository_format_version = 0;
143 git_config_from_file(check_repository_format_version,
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100144 template_path, NULL);
Junio C Hamano4f629532005-11-25 16:03:56 -0800145 template_path[template_len] = 0;
146
147 if (repository_format_version &&
148 repository_format_version != GIT_REPO_VERSION) {
Miklos Vajna2fd8c0a2009-03-24 02:09:13 +0100149 warning("not copying templates of "
150 "a wrong format version %d from '%s'",
Junio C Hamano4f629532005-11-25 16:03:56 -0800151 repository_format_version,
152 template_dir);
153 closedir(dir);
154 return;
155 }
156
Junio C Hamanod3af6212005-08-06 12:50:14 -0700157 memcpy(path, git_dir, len);
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400158 if (len && path[len - 1] != '/')
159 path[len++] = '/';
Petr Baudis1f961c12005-09-20 02:19:50 +0200160 path[len] = 0;
Junio C Hamano8d5afef2005-08-02 16:45:21 -0700161 copy_templates_1(path, len,
162 template_path, template_len,
163 dir);
164 closedir(dir);
165}
166
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400167static int create_default_files(const char *template_path)
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700168{
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400169 const char *git_dir = get_git_dir();
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700170 unsigned len = strlen(git_dir);
171 static char path[PATH_MAX];
Junio C Hamano4f629532005-11-25 16:03:56 -0800172 struct stat st1;
173 char repo_version_string[10];
Johannes Schindelin5cc8f372008-03-24 16:14:52 +0100174 char junk[2];
Shawn O. Pearceef0a89a2006-12-15 00:44:58 -0500175 int reinit;
Shawn O. Pearcec8697532006-12-30 23:53:55 -0500176 int filemode;
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700177
178 if (len > sizeof(path)-50)
179 die("insane git directory %s", git_dir);
180 memcpy(path, git_dir, len);
181
182 if (len && path[len-1] != '/')
183 path[len++] = '/';
184
185 /*
186 * Create .git/refs/{heads,tags}
187 */
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400188 safe_create_dir(git_path("refs"), 1);
189 safe_create_dir(git_path("refs/heads"), 1);
190 safe_create_dir(git_path("refs/tags"), 1);
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700191
Junio C Hamano4f629532005-11-25 16:03:56 -0800192 /* First copy the templates -- we might have the default
193 * config file there, in which case we would want to read
194 * from it after installing.
195 */
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400196 copy_templates(template_path);
Junio C Hamano4f629532005-11-25 16:03:56 -0800197
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100198 git_config(git_default_config, NULL);
Deskin Miller0a2c7ee2008-10-07 01:37:48 -0400199 is_bare_repository_cfg = init_is_bare_repository;
Junio C Hamano5a688fe2009-03-25 16:19:36 -0700200
201 /* reading existing config may have overwrote it */
Deskin Miller0a2c7ee2008-10-07 01:37:48 -0400202 if (init_shared_repository != -1)
203 shared_repository = init_shared_repository;
Junio C Hamano4f629532005-11-25 16:03:56 -0800204
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700205 /*
Junio C Hamano138086a2006-06-09 22:07:23 -0700206 * We would have created the above under user's umask -- under
207 * shared-repository settings, we would need to fix them up.
208 */
209 if (shared_repository) {
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400210 adjust_shared_perm(get_git_dir());
211 adjust_shared_perm(git_path("refs"));
212 adjust_shared_perm(git_path("refs/heads"));
213 adjust_shared_perm(git_path("refs/tags"));
Junio C Hamano138086a2006-06-09 22:07:23 -0700214 }
215
216 /*
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700217 * Create the default symlink from ".git/HEAD" to the "master"
Junio C Hamano8098a172005-09-30 14:26:57 -0700218 * branch, if it does not exist yet.
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700219 */
220 strcpy(path + len, "HEAD");
Johannes Schindelin5cc8f372008-03-24 16:14:52 +0100221 reinit = (!access(path, R_OK)
222 || readlink(path, junk, sizeof(junk)-1) != -1);
Shawn O. Pearceef0a89a2006-12-15 00:44:58 -0500223 if (!reinit) {
Nicolas Pitre8b5157e2007-01-26 17:26:10 -0500224 if (create_symref("HEAD", "refs/heads/master", NULL) < 0)
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700225 exit(1);
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700226 }
Junio C Hamano4f629532005-11-25 16:03:56 -0800227
228 /* This forces creation of new config file */
229 sprintf(repo_version_string, "%d", GIT_REPO_VERSION);
230 git_config_set("core.repositoryformatversion", repo_version_string);
231
Junio C Hamano8098a172005-09-30 14:26:57 -0700232 path[len] = 0;
Johannes Schindeline24317b2005-10-26 01:43:03 +0200233 strcpy(path + len, "config");
Johannes Schindeline24317b2005-10-26 01:43:03 +0200234
Junio C Hamano4f629532005-11-25 16:03:56 -0800235 /* Check filemode trustability */
Shawn O. Pearcec8697532006-12-30 23:53:55 -0500236 filemode = TEST_FILEMODE;
237 if (TEST_FILEMODE && !lstat(path, &st1)) {
Junio C Hamano4f629532005-11-25 16:03:56 -0800238 struct stat st2;
Shawn O. Pearcec8697532006-12-30 23:53:55 -0500239 filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) &&
Junio C Hamano4f629532005-11-25 16:03:56 -0800240 !lstat(path, &st2) &&
241 st1.st_mode != st2.st_mode);
Johannes Schindeline24317b2005-10-26 01:43:03 +0200242 }
Shawn O. Pearcec8697532006-12-30 23:53:55 -0500243 git_config_set("core.filemode", filemode ? "true" : "false");
Shawn O. Pearceef0a89a2006-12-15 00:44:58 -0500244
Johannes Schindeline90fdc32007-08-01 01:30:14 +0100245 if (is_bare_repository())
Junio C Hamano7d1864c2007-01-07 02:00:28 -0800246 git_config_set("core.bare", "true");
Junio C Hamano7d1864c2007-01-07 02:00:28 -0800247 else {
Johannes Schindeline90fdc32007-08-01 01:30:14 +0100248 const char *work_tree = get_git_work_tree();
Junio C Hamano7d1864c2007-01-07 02:00:28 -0800249 git_config_set("core.bare", "false");
Alex Riesen196055c2007-01-23 16:51:18 +0100250 /* allow template config file to override the default */
251 if (log_all_ref_updates == -1)
252 git_config_set("core.logallrefupdates", "true");
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400253 if (prefixcmp(git_dir, work_tree) ||
254 strcmp(git_dir + strlen(work_tree), "/.git")) {
Johannes Schindeline90fdc32007-08-01 01:30:14 +0100255 git_config_set("core.worktree", work_tree);
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400256 }
Junio C Hamano7d1864c2007-01-07 02:00:28 -0800257 }
Junio C Hamano75d24492007-08-31 00:25:04 -0700258
Junio C Hamano75d24492007-08-31 00:25:04 -0700259 if (!reinit) {
Dmitry Potapov24554062008-05-11 18:16:39 +0200260 /* Check if symlink is supported in the work tree */
Junio C Hamano75d24492007-08-31 00:25:04 -0700261 path[len] = 0;
262 strcpy(path + len, "tXXXXXX");
263 if (!close(xmkstemp(path)) &&
264 !unlink(path) &&
265 !symlink("testing", path) &&
266 !lstat(path, &st1) &&
267 S_ISLNK(st1.st_mode))
268 unlink(path); /* good */
269 else
270 git_config_set("core.symlinks", "false");
Dmitry Potapov24554062008-05-11 18:16:39 +0200271
272 /* Check if the filesystem is case-insensitive */
273 path[len] = 0;
274 strcpy(path + len, "CoNfIg");
275 if (!access(path, F_OK))
276 git_config_set("core.ignorecase", "true");
Junio C Hamano75d24492007-08-31 00:25:04 -0700277 }
278
Shawn O. Pearceef0a89a2006-12-15 00:44:58 -0500279 return reinit;
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700280}
281
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400282int init_db(const char *template_dir, unsigned int flags)
Junio C Hamano6adcca32007-08-27 00:58:06 -0700283{
Junio C Hamanod19938a2005-05-09 17:57:56 -0700284 const char *sha1_dir;
Timo Hirvonenc3c88352006-05-19 13:03:57 +0300285 char *path;
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400286 int len, reinit;
Linus Torvaldse83c5162005-04-07 15:13:13 -0700287
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400288 safe_create_dir(get_git_dir(), 0);
Junio C Hamano4f629532005-11-25 16:03:56 -0800289
Deskin Miller0a2c7ee2008-10-07 01:37:48 -0400290 init_is_bare_repository = is_bare_repository();
291
Junio C Hamano4f629532005-11-25 16:03:56 -0800292 /* Check to see if the repository version is right.
293 * Note that a newly created repository does not have
294 * config file, so this will not fail. What we are catching
295 * is an attempt to reinitialize new repository with an old tool.
296 */
297 check_repository_format();
298
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400299 reinit = create_default_files(template_dir);
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700300
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700301 sha1_dir = get_object_directory();
Linus Torvaldse83c5162005-04-07 15:13:13 -0700302 len = strlen(sha1_dir);
Christopher Li812666c2005-04-26 12:00:58 -0700303 path = xmalloc(len + 40);
Linus Torvaldse83c5162005-04-07 15:13:13 -0700304 memcpy(path, sha1_dir, len);
Zach Welchcb126d82005-04-19 21:48:15 -0700305
Johannes Schindelinaf6e2772005-12-22 23:19:37 +0100306 safe_create_dir(sha1_dir, 1);
Linus Torvaldsf49fb352005-06-27 18:26:11 -0700307 strcpy(path+len, "/pack");
Johannes Schindelinaf6e2772005-12-22 23:19:37 +0100308 safe_create_dir(path, 1);
Junio C Hamanod57306c2005-08-20 02:05:31 -0700309 strcpy(path+len, "/info");
Johannes Schindelinaf6e2772005-12-22 23:19:37 +0100310 safe_create_dir(path, 1);
311
Junio C Hamano94df2502006-06-09 23:09:49 -0700312 if (shared_repository) {
313 char buf[10];
314 /* We do not spell "group" and such, so that
315 * the configuration can be read by older version
Heikki Orsila06cbe852008-04-16 11:34:24 +0300316 * of git. Note, we use octal numbers for new share modes,
317 * and compatibility values for PERM_GROUP and
318 * PERM_EVERYBODY.
Junio C Hamano94df2502006-06-09 23:09:49 -0700319 */
Junio C Hamano5a688fe2009-03-25 16:19:36 -0700320 if (shared_repository < 0)
321 /* force to the mode value */
322 sprintf(buf, "0%o", -shared_repository);
323 else if (shared_repository == PERM_GROUP)
Heikki Orsila06cbe852008-04-16 11:34:24 +0300324 sprintf(buf, "%d", OLD_PERM_GROUP);
325 else if (shared_repository == PERM_EVERYBODY)
326 sprintf(buf, "%d", OLD_PERM_EVERYBODY);
327 else
Junio C Hamano5a688fe2009-03-25 16:19:36 -0700328 die("oops");
Junio C Hamano94df2502006-06-09 23:09:49 -0700329 git_config_set("core.sharedrepository", buf);
Johannes Schindelin11031d72006-09-21 01:07:54 +0200330 git_config_set("receive.denyNonFastforwards", "true");
Junio C Hamano94df2502006-06-09 23:09:49 -0700331 }
Johannes Schindelinaf6e2772005-12-22 23:19:37 +0100332
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400333 if (!(flags & INIT_DB_QUIET))
Jeffrey C. Ollie45765182007-06-07 07:50:29 -0500334 printf("%s%s Git repository in %s/\n",
335 reinit ? "Reinitialized existing" : "Initialized empty",
336 shared_repository ? " shared" : "",
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400337 get_git_dir());
Shawn O. Pearceef0a89a2006-12-15 00:44:58 -0500338
Linus Torvaldse83c5162005-04-07 15:13:13 -0700339 return 0;
340}
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400341
342static int guess_repository_type(const char *git_dir)
343{
344 char cwd[PATH_MAX];
345 const char *slash;
346
347 /*
348 * "GIT_DIR=. git init" is always bare.
349 * "GIT_DIR=`pwd` git init" too.
350 */
351 if (!strcmp(".", git_dir))
352 return 1;
353 if (!getcwd(cwd, sizeof(cwd)))
Thomas Rast0721c312009-06-27 17:58:47 +0200354 die_errno("cannot tell cwd");
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400355 if (!strcmp(git_dir, cwd))
356 return 1;
357 /*
358 * "GIT_DIR=.git or GIT_DIR=something/.git is usually not.
359 */
360 if (!strcmp(git_dir, ".git"))
361 return 0;
362 slash = strrchr(git_dir, '/');
363 if (slash && !strcmp(slash, "/.git"))
364 return 0;
365
366 /*
367 * Otherwise it is often bare. At this point
368 * we are just guessing.
369 */
370 return 1;
371}
372
373static const char init_db_usage[] =
Stephan Beyer1b1dd232008-07-13 15:36:15 +0200374"git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]]";
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400375
376/*
377 * If you want to, you can share the DB area with any number of branches.
378 * That has advantages: you can save space by sharing all the SHA1 objects.
379 * On the other hand, it might just make lookup slower and messier. You
380 * be the judge. The default case is to have one DB per managed directory.
381 */
382int cmd_init_db(int argc, const char **argv, const char *prefix)
383{
384 const char *git_dir;
385 const char *template_dir = NULL;
386 unsigned int flags = 0;
387 int i;
388
389 for (i = 1; i < argc; i++, argv++) {
390 const char *arg = argv[1];
391 if (!prefixcmp(arg, "--template="))
392 template_dir = arg+11;
Luciano Rocha74d3b232008-05-28 19:53:57 +0100393 else if (!strcmp(arg, "--bare")) {
394 static char git_dir[PATH_MAX+1];
395 is_bare_repository_cfg = 1;
396 setenv(GIT_DIR_ENVIRONMENT, getcwd(git_dir,
397 sizeof(git_dir)), 0);
398 } else if (!strcmp(arg, "--shared"))
Deskin Miller0a2c7ee2008-10-07 01:37:48 -0400399 init_shared_repository = PERM_GROUP;
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400400 else if (!prefixcmp(arg, "--shared="))
Deskin Miller0a2c7ee2008-10-07 01:37:48 -0400401 init_shared_repository = git_config_perm("arg", arg+9);
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400402 else if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet"))
403 flags |= INIT_DB_QUIET;
404 else
405 usage(init_db_usage);
406 }
407
Junio C Hamano5a688fe2009-03-25 16:19:36 -0700408 if (init_shared_repository != -1)
409 shared_repository = init_shared_repository;
410
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400411 /*
412 * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
413 * without --bare. Catch the error early.
414 */
415 git_dir = getenv(GIT_DIR_ENVIRONMENT);
416 if ((!git_dir || is_bare_repository_cfg == 1)
417 && getenv(GIT_WORK_TREE_ENVIRONMENT))
418 die("%s (or --work-tree=<directory>) not allowed without "
419 "specifying %s (or --git-dir=<directory>)",
420 GIT_WORK_TREE_ENVIRONMENT,
421 GIT_DIR_ENVIRONMENT);
422
423 /*
424 * Set up the default .git directory contents
425 */
426 if (!git_dir)
427 git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
428
429 if (is_bare_repository_cfg < 0)
430 is_bare_repository_cfg = guess_repository_type(git_dir);
431
432 if (!is_bare_repository_cfg) {
433 if (git_dir) {
434 const char *git_dir_parent = strrchr(git_dir, '/');
435 if (git_dir_parent) {
436 char *rel = xstrndup(git_dir, git_dir_parent - git_dir);
437 git_work_tree_cfg = xstrdup(make_absolute_path(rel));
438 free(rel);
439 }
440 }
441 if (!git_work_tree_cfg) {
442 git_work_tree_cfg = xcalloc(PATH_MAX, 1);
443 if (!getcwd(git_work_tree_cfg, PATH_MAX))
Thomas Rast0721c312009-06-27 17:58:47 +0200444 die_errno ("Cannot access current working directory");
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400445 }
446 if (access(get_git_work_tree(), X_OK))
Thomas Rast0721c312009-06-27 17:58:47 +0200447 die_errno ("Cannot access work tree '%s'",
448 get_git_work_tree());
Daniel Barkalowf225aeb2008-04-27 13:39:27 -0400449 }
450
451 set_git_dir(make_absolute_path(git_dir));
452
453 return init_db(template_dir, flags);
454}