]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/init-db.c
setup: rework setup_explicit_git_dir()
[thirdparty/git.git] / builtin / init-db.c
CommitLineData
8bc9a0c7
LT
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
e83c5163 6#include "cache.h"
c3c8835f 7#include "builtin.h"
a47d1813 8#include "exec_cmd.h"
596f91ee 9#include "parse-options.h"
e83c5163 10
d3af621b 11#ifndef DEFAULT_GIT_TEMPLATE_DIR
d52fd42a 12#define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates"
d3af621b
JH
13#endif
14
c869753e
SP
15#ifdef NO_TRUSTABLE_FILEMODE
16#define TEST_FILEMODE 0
17#else
18#define TEST_FILEMODE 1
19#endif
20
0a2c7eea
DM
21static int init_is_bare_repository = 0;
22static int init_shared_repository = -1;
90b45187 23static const char *init_db_template_dir;
0a2c7eea 24
af6e277c 25static void safe_create_dir(const char *dir, int share)
cb126d8d 26{
f312de01 27 if (mkdir(dir, 0777) < 0) {
cb126d8d
ZW
28 if (errno != EEXIST) {
29 perror(dir);
30 exit(1);
31 }
32 }
af6e277c 33 else if (share && adjust_shared_perm(dir))
d7530708 34 die("Could not make %s writable by group", dir);
cb126d8d
ZW
35}
36
8d5afef0
JH
37static void copy_templates_1(char *path, int baselen,
38 char *template, int template_baselen,
39 DIR *dir)
40{
41 struct dirent *de;
42
43 /* Note: if ".git/hooks" file exists in the repository being
44 * re-initialized, /etc/core-git/templates/hooks/update would
f18d244a 45 * cause "git init" to fail here. I think this is sane but
8d5afef0
JH
46 * it means that the set of templates we ship by default, along
47 * with the way the namespace under .git/ is organized, should
48 * be really carefully chosen.
49 */
af6e277c 50 safe_create_dir(path, 1);
8d5afef0
JH
51 while ((de = readdir(dir)) != NULL) {
52 struct stat st_git, st_template;
53 int namelen;
54 int exists = 0;
55
56 if (de->d_name[0] == '.')
57 continue;
58 namelen = strlen(de->d_name);
59 if ((PATH_MAX <= baselen + namelen) ||
60 (PATH_MAX <= template_baselen + namelen))
61 die("insanely long template name %s", de->d_name);
62 memcpy(path + baselen, de->d_name, namelen+1);
63 memcpy(template + template_baselen, de->d_name, namelen+1);
64 if (lstat(path, &st_git)) {
65 if (errno != ENOENT)
0721c314 66 die_errno("cannot stat '%s'", path);
8d5afef0
JH
67 }
68 else
69 exists = 1;
70
71 if (lstat(template, &st_template))
0721c314 72 die_errno("cannot stat template '%s'", template);
8d5afef0
JH
73
74 if (S_ISDIR(st_template.st_mode)) {
75 DIR *subdir = opendir(template);
76 int baselen_sub = baselen + namelen;
77 int template_baselen_sub = template_baselen + namelen;
78 if (!subdir)
0721c314 79 die_errno("cannot opendir '%s'", template);
8d5afef0
JH
80 path[baselen_sub++] =
81 template[template_baselen_sub++] = '/';
82 path[baselen_sub] =
83 template[template_baselen_sub] = 0;
84 copy_templates_1(path, baselen_sub,
85 template, template_baselen_sub,
86 subdir);
87 closedir(subdir);
88 }
89 else if (exists)
90 continue;
91 else if (S_ISLNK(st_template.st_mode)) {
92 char lnk[256];
93 int len;
94 len = readlink(template, lnk, sizeof(lnk));
95 if (len < 0)
0721c314 96 die_errno("cannot readlink '%s'", template);
8d5afef0
JH
97 if (sizeof(lnk) <= len)
98 die("insanely long symlink %s", template);
99 lnk[len] = 0;
100 if (symlink(lnk, path))
0721c314 101 die_errno("cannot symlink '%s' '%s'", lnk, path);
8d5afef0
JH
102 }
103 else if (S_ISREG(st_template.st_mode)) {
104 if (copy_file(path, template, st_template.st_mode))
0721c314
TR
105 die_errno("cannot copy '%s' to '%s'", template,
106 path);
8d5afef0
JH
107 }
108 else
109 error("ignoring template %s", template);
110 }
111}
112
f225aeb2 113static void copy_templates(const char *template_dir)
8d5afef0
JH
114{
115 char path[PATH_MAX];
116 char template_path[PATH_MAX];
d3af621b 117 int template_len;
8d5afef0 118 DIR *dir;
f225aeb2
DB
119 const char *git_dir = get_git_dir();
120 int len = strlen(git_dir);
8d5afef0 121
a47d1813 122 if (!template_dir)
d4ebc36c 123 template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT);
90b45187
SD
124 if (!template_dir)
125 template_dir = init_db_template_dir;
2de9de5e
SP
126 if (!template_dir)
127 template_dir = system_path(DEFAULT_GIT_TEMPLATE_DIR);
172035f0
JK
128 if (!template_dir[0])
129 return;
32d1776b
FL
130 template_len = strlen(template_dir);
131 if (PATH_MAX <= (template_len+strlen("/config")))
132 die("insanely long template path %s", template_dir);
8d5afef0 133 strcpy(template_path, template_dir);
8d5afef0
JH
134 if (template_path[template_len-1] != '/') {
135 template_path[template_len++] = '/';
136 template_path[template_len] = 0;
137 }
8d5afef0 138 dir = opendir(template_path);
d3af621b 139 if (!dir) {
2fd8c0a5 140 warning("templates not found %s", template_dir);
8d5afef0 141 return;
d3af621b
JH
142 }
143
4f629539
JH
144 /* Make sure that template is from the correct vintage */
145 strcpy(template_path + template_len, "config");
146 repository_format_version = 0;
147 git_config_from_file(check_repository_format_version,
ef90d6d4 148 template_path, NULL);
4f629539
JH
149 template_path[template_len] = 0;
150
151 if (repository_format_version &&
152 repository_format_version != GIT_REPO_VERSION) {
2fd8c0a5
MV
153 warning("not copying templates of "
154 "a wrong format version %d from '%s'",
4f629539
JH
155 repository_format_version,
156 template_dir);
157 closedir(dir);
158 return;
159 }
160
d3af621b 161 memcpy(path, git_dir, len);
f225aeb2
DB
162 if (len && path[len - 1] != '/')
163 path[len++] = '/';
1f961c19 164 path[len] = 0;
8d5afef0
JH
165 copy_templates_1(path, len,
166 template_path, template_len,
167 dir);
168 closedir(dir);
169}
170
90b45187
SD
171static int git_init_db_config(const char *k, const char *v, void *cb)
172{
90b45187
SD
173 if (!strcmp(k, "init.templatedir"))
174 return git_config_pathname(&init_db_template_dir, k, v);
175
176 return 0;
177}
178
f225aeb2 179static int create_default_files(const char *template_path)
cad88fdf 180{
f225aeb2 181 const char *git_dir = get_git_dir();
cad88fdf
LT
182 unsigned len = strlen(git_dir);
183 static char path[PATH_MAX];
4f629539
JH
184 struct stat st1;
185 char repo_version_string[10];
5cc8f372 186 char junk[2];
ef0a89a6 187 int reinit;
c869753e 188 int filemode;
cad88fdf
LT
189
190 if (len > sizeof(path)-50)
191 die("insane git directory %s", git_dir);
192 memcpy(path, git_dir, len);
193
194 if (len && path[len-1] != '/')
195 path[len++] = '/';
196
197 /*
198 * Create .git/refs/{heads,tags}
199 */
f225aeb2
DB
200 safe_create_dir(git_path("refs"), 1);
201 safe_create_dir(git_path("refs/heads"), 1);
202 safe_create_dir(git_path("refs/tags"), 1);
cad88fdf 203
90b45187
SD
204 /* Just look for `init.templatedir` */
205 git_config(git_init_db_config, NULL);
206
4f629539
JH
207 /* First copy the templates -- we might have the default
208 * config file there, in which case we would want to read
209 * from it after installing.
210 */
f225aeb2 211 copy_templates(template_path);
4f629539 212
ef90d6d4 213 git_config(git_default_config, NULL);
0a2c7eea 214 is_bare_repository_cfg = init_is_bare_repository;
5a688fe4
JH
215
216 /* reading existing config may have overwrote it */
0a2c7eea
DM
217 if (init_shared_repository != -1)
218 shared_repository = init_shared_repository;
4f629539 219
138086a7
JH
220 /*
221 * We would have created the above under user's umask -- under
222 * shared-repository settings, we would need to fix them up.
223 */
224 if (shared_repository) {
f225aeb2
DB
225 adjust_shared_perm(get_git_dir());
226 adjust_shared_perm(git_path("refs"));
227 adjust_shared_perm(git_path("refs/heads"));
228 adjust_shared_perm(git_path("refs/tags"));
138086a7
JH
229 }
230
cad88fdf
LT
231 /*
232 * Create the default symlink from ".git/HEAD" to the "master"
8098a178 233 * branch, if it does not exist yet.
cad88fdf
LT
234 */
235 strcpy(path + len, "HEAD");
5cc8f372
JS
236 reinit = (!access(path, R_OK)
237 || readlink(path, junk, sizeof(junk)-1) != -1);
ef0a89a6 238 if (!reinit) {
8b5157e4 239 if (create_symref("HEAD", "refs/heads/master", NULL) < 0)
cad88fdf 240 exit(1);
cad88fdf 241 }
e24317b4 242
4f629539
JH
243 /* This forces creation of new config file */
244 sprintf(repo_version_string, "%d", GIT_REPO_VERSION);
245 git_config_set("core.repositoryformatversion", repo_version_string);
246
247 path[len] = 0;
e24317b4 248 strcpy(path + len, "config");
4f629539
JH
249
250 /* Check filemode trustability */
c869753e
SP
251 filemode = TEST_FILEMODE;
252 if (TEST_FILEMODE && !lstat(path, &st1)) {
4f629539 253 struct stat st2;
c869753e 254 filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) &&
4f629539
JH
255 !lstat(path, &st2) &&
256 st1.st_mode != st2.st_mode);
e24317b4 257 }
c869753e 258 git_config_set("core.filemode", filemode ? "true" : "false");
ef0a89a6 259
e90fdc39 260 if (is_bare_repository())
7d1864ce 261 git_config_set("core.bare", "true");
7d1864ce 262 else {
e90fdc39 263 const char *work_tree = get_git_work_tree();
7d1864ce 264 git_config_set("core.bare", "false");
196055c2
AR
265 /* allow template config file to override the default */
266 if (log_all_ref_updates == -1)
267 git_config_set("core.logallrefupdates", "true");
f225aeb2
DB
268 if (prefixcmp(git_dir, work_tree) ||
269 strcmp(git_dir + strlen(work_tree), "/.git")) {
e90fdc39 270 git_config_set("core.worktree", work_tree);
f225aeb2 271 }
7d1864ce 272 }
75d24499 273
75d24499 274 if (!reinit) {
2455406a 275 /* Check if symlink is supported in the work tree */
75d24499
JH
276 path[len] = 0;
277 strcpy(path + len, "tXXXXXX");
278 if (!close(xmkstemp(path)) &&
279 !unlink(path) &&
280 !symlink("testing", path) &&
281 !lstat(path, &st1) &&
282 S_ISLNK(st1.st_mode))
283 unlink(path); /* good */
284 else
285 git_config_set("core.symlinks", "false");
2455406a
DP
286
287 /* Check if the filesystem is case-insensitive */
288 path[len] = 0;
289 strcpy(path + len, "CoNfIg");
290 if (!access(path, F_OK))
291 git_config_set("core.ignorecase", "true");
75d24499
JH
292 }
293
ef0a89a6 294 return reinit;
cad88fdf
LT
295}
296
9173912b
JN
297static void create_object_directory(void)
298{
299 const char *object_directory = get_object_directory();
300 int len = strlen(object_directory);
301 char *path = xmalloc(len + 40);
302
303 memcpy(path, object_directory, len);
304
305 safe_create_dir(object_directory, 1);
306 strcpy(path+len, "/pack");
307 safe_create_dir(path, 1);
308 strcpy(path+len, "/info");
309 safe_create_dir(path, 1);
310
311 free(path);
312}
313
f225aeb2
DB
314int init_db(const char *template_dir, unsigned int flags)
315{
9173912b 316 int reinit;
f225aeb2
DB
317
318 safe_create_dir(get_git_dir(), 0);
319
0a2c7eea
DM
320 init_is_bare_repository = is_bare_repository();
321
f225aeb2
DB
322 /* Check to see if the repository version is right.
323 * Note that a newly created repository does not have
324 * config file, so this will not fail. What we are catching
325 * is an attempt to reinitialize new repository with an old tool.
326 */
327 check_repository_format();
328
329 reinit = create_default_files(template_dir);
330
9173912b 331 create_object_directory();
f225aeb2
DB
332
333 if (shared_repository) {
334 char buf[10];
335 /* We do not spell "group" and such, so that
336 * the configuration can be read by older version
337 * of git. Note, we use octal numbers for new share modes,
338 * and compatibility values for PERM_GROUP and
339 * PERM_EVERYBODY.
340 */
5a688fe4
JH
341 if (shared_repository < 0)
342 /* force to the mode value */
343 sprintf(buf, "0%o", -shared_repository);
344 else if (shared_repository == PERM_GROUP)
f225aeb2
DB
345 sprintf(buf, "%d", OLD_PERM_GROUP);
346 else if (shared_repository == PERM_EVERYBODY)
347 sprintf(buf, "%d", OLD_PERM_EVERYBODY);
348 else
5a688fe4 349 die("oops");
f225aeb2
DB
350 git_config_set("core.sharedrepository", buf);
351 git_config_set("receive.denyNonFastforwards", "true");
352 }
353
d06f15d9
NTND
354 if (!(flags & INIT_DB_QUIET)) {
355 const char *git_dir = get_git_dir();
356 int len = strlen(git_dir);
357 printf("%s%s Git repository in %s%s\n",
f225aeb2
DB
358 reinit ? "Reinitialized existing" : "Initialized empty",
359 shared_repository ? " shared" : "",
d06f15d9
NTND
360 git_dir, len && git_dir[len-1] != '/' ? "/" : "");
361 }
f225aeb2
DB
362
363 return 0;
364}
365
366static int guess_repository_type(const char *git_dir)
6adcca3f
JH
367{
368 char cwd[PATH_MAX];
369 const char *slash;
370
6adcca3f
JH
371 /*
372 * "GIT_DIR=. git init" is always bare.
373 * "GIT_DIR=`pwd` git init" too.
374 */
375 if (!strcmp(".", git_dir))
f225aeb2 376 return 1;
6adcca3f 377 if (!getcwd(cwd, sizeof(cwd)))
0721c314 378 die_errno("cannot tell cwd");
6adcca3f 379 if (!strcmp(git_dir, cwd))
f225aeb2 380 return 1;
6adcca3f
JH
381 /*
382 * "GIT_DIR=.git or GIT_DIR=something/.git is usually not.
383 */
384 if (!strcmp(git_dir, ".git"))
f225aeb2 385 return 0;
6adcca3f
JH
386 slash = strrchr(git_dir, '/');
387 if (slash && !strcmp(slash, "/.git"))
f225aeb2 388 return 0;
6adcca3f
JH
389
390 /*
391 * Otherwise it is often bare. At this point
392 * we are just guessing.
393 */
f225aeb2 394 return 1;
6adcca3f
JH
395}
396
596f91ee
MK
397static int shared_callback(const struct option *opt, const char *arg, int unset)
398{
399 *((int *) opt->value) = (arg) ? git_config_perm("arg", arg) : PERM_GROUP;
400 return 0;
401}
402
403static const char *const init_db_usage[] = {
0397ff24 404 "git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]] [directory]",
596f91ee
MK
405 NULL
406};
d3af621b 407
4696cb93
ZW
408/*
409 * If you want to, you can share the DB area with any number of branches.
410 * That has advantages: you can save space by sharing all the SHA1 objects.
411 * On the other hand, it might just make lookup slower and messier. You
412 * be the judge. The default case is to have one DB per managed directory.
413 */
a633fca0 414int cmd_init_db(int argc, const char **argv, const char *prefix)
e83c5163 415{
cad88fdf 416 const char *git_dir;
c3c8835f 417 const char *template_dir = NULL;
f225aeb2 418 unsigned int flags = 0;
596f91ee
MK
419 const struct option init_db_options[] = {
420 OPT_STRING(0, "template", &template_dir, "template-directory",
421 "provide the directory from which templates will be used"),
422 OPT_SET_INT(0, "bare", &is_bare_repository_cfg,
423 "create a bare repository", 1),
424 { OPTION_CALLBACK, 0, "shared", &init_shared_repository,
425 "permissions",
426 "specify that the git repository is to be shared amongst several users",
427 PARSE_OPT_OPTARG | PARSE_OPT_NONEG, shared_callback, 0},
428 OPT_BIT('q', "quiet", &flags, "be quiet", INIT_DB_QUIET),
429 OPT_END()
430 };
431
0397ff24
JH
432 argc = parse_options(argc, argv, prefix, init_db_options, init_db_usage, 0);
433
434 if (argc == 1) {
53d48885
NS
435 int mkdir_tried = 0;
436 retry:
0397ff24 437 if (chdir(argv[0]) < 0) {
53d48885
NS
438 if (!mkdir_tried) {
439 int saved;
440 /*
441 * At this point we haven't read any configuration,
442 * and we know shared_repository should always be 0;
443 * but just in case we play safe.
444 */
445 saved = shared_repository;
446 shared_repository = 0;
0397ff24 447 switch (safe_create_leading_directories_const(argv[0])) {
53d48885
NS
448 case -3:
449 errno = EEXIST;
450 /* fallthru */
451 case -1:
0397ff24 452 die_errno("cannot mkdir %s", argv[0]);
53d48885
NS
453 break;
454 default:
455 break;
456 }
457 shared_repository = saved;
0397ff24
JH
458 if (mkdir(argv[0], 0777) < 0)
459 die_errno("cannot mkdir %s", argv[0]);
53d48885
NS
460 mkdir_tried = 1;
461 goto retry;
462 }
0397ff24 463 die_errno("cannot chdir to %s", argv[0]);
53d48885 464 }
0397ff24
JH
465 } else if (0 < argc) {
466 usage(init_db_usage[0]);
d3af621b 467 }
0397ff24 468 if (is_bare_repository_cfg == 1) {
596f91ee 469 static char git_dir[PATH_MAX+1];
d3af621b 470
53d48885 471 setenv(GIT_DIR_ENVIRONMENT,
87a074df 472 getcwd(git_dir, sizeof(git_dir)), argc > 0);
d3af621b
JH
473 }
474
5a688fe4
JH
475 if (init_shared_repository != -1)
476 shared_repository = init_shared_repository;
477
6adcca3f
JH
478 /*
479 * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
480 * without --bare. Catch the error early.
481 */
482 git_dir = getenv(GIT_DIR_ENVIRONMENT);
483 if ((!git_dir || is_bare_repository_cfg == 1)
484 && getenv(GIT_WORK_TREE_ENVIRONMENT))
485 die("%s (or --work-tree=<directory>) not allowed without "
486 "specifying %s (or --git-dir=<directory>)",
487 GIT_WORK_TREE_ENVIRONMENT,
488 GIT_DIR_ENVIRONMENT);
489
cad88fdf
LT
490 /*
491 * Set up the default .git directory contents
492 */
ef0a89a6 493 if (!git_dir)
cad88fdf 494 git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
af6e277c 495
f225aeb2
DB
496 if (is_bare_repository_cfg < 0)
497 is_bare_repository_cfg = guess_repository_type(git_dir);
498
e6aea2db 499 startup_info->setup_explicit = 1;
f225aeb2
DB
500 if (!is_bare_repository_cfg) {
501 if (git_dir) {
502 const char *git_dir_parent = strrchr(git_dir, '/');
503 if (git_dir_parent) {
504 char *rel = xstrndup(git_dir, git_dir_parent - git_dir);
505 git_work_tree_cfg = xstrdup(make_absolute_path(rel));
506 free(rel);
507 }
508 }
509 if (!git_work_tree_cfg) {
510 git_work_tree_cfg = xcalloc(PATH_MAX, 1);
511 if (!getcwd(git_work_tree_cfg, PATH_MAX))
0721c314 512 die_errno ("Cannot access current working directory");
f225aeb2
DB
513 }
514 if (access(get_git_work_tree(), X_OK))
0721c314
TR
515 die_errno ("Cannot access work tree '%s'",
516 get_git_work_tree());
94df2506 517 }
af6e277c 518
f225aeb2 519 set_git_dir(make_absolute_path(git_dir));
ef0a89a6 520
f225aeb2 521 return init_db(template_dir, flags);
e83c5163 522}