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