]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-init-db.c
t6030: test skipping away from an already skipped commit
[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"
e83c5163 9
d3af621b 10#ifndef DEFAULT_GIT_TEMPLATE_DIR
d52fd42a 11#define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates"
d3af621b
JH
12#endif
13
c869753e
SP
14#ifdef NO_TRUSTABLE_FILEMODE
15#define TEST_FILEMODE 0
16#else
17#define TEST_FILEMODE 1
18#endif
19
0a2c7eea
DM
20static int init_is_bare_repository = 0;
21static int init_shared_repository = -1;
22
af6e277c 23static void safe_create_dir(const char *dir, int share)
cb126d8d 24{
f312de01 25 if (mkdir(dir, 0777) < 0) {
cb126d8d
ZW
26 if (errno != EEXIST) {
27 perror(dir);
28 exit(1);
29 }
30 }
af6e277c 31 else if (share && adjust_shared_perm(dir))
d7530708 32 die("Could not make %s writable by group", dir);
cb126d8d
ZW
33}
34
8d5afef0
JH
35static 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
f18d244a 43 * cause "git init" to fail here. I think this is sane but
8d5afef0
JH
44 * 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 */
af6e277c 48 safe_create_dir(path, 1);
8d5afef0
JH
49 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)
64 die("cannot stat %s", path);
65 }
66 else
67 exists = 1;
68
69 if (lstat(template, &st_template))
70 die("cannot stat template %s", template);
71
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)
77 die("cannot opendir %s", template);
78 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)
94 die("cannot readlink %s", template);
95 if (sizeof(lnk) <= len)
96 die("insanely long symlink %s", template);
97 lnk[len] = 0;
98 if (symlink(lnk, path))
99 die("cannot symlink %s %s", lnk, path);
100 }
101 else if (S_ISREG(st_template.st_mode)) {
102 if (copy_file(path, template, st_template.st_mode))
103 die("cannot copy %s to %s", template, path);
104 }
105 else
106 error("ignoring template %s", template);
107 }
108}
109
f225aeb2 110static void copy_templates(const char *template_dir)
8d5afef0
JH
111{
112 char path[PATH_MAX];
113 char template_path[PATH_MAX];
d3af621b 114 int template_len;
8d5afef0 115 DIR *dir;
f225aeb2
DB
116 const char *git_dir = get_git_dir();
117 int len = strlen(git_dir);
8d5afef0 118
a47d1813 119 if (!template_dir)
d4ebc36c 120 template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT);
2de9de5e
SP
121 if (!template_dir)
122 template_dir = system_path(DEFAULT_GIT_TEMPLATE_DIR);
172035f0
JK
123 if (!template_dir[0])
124 return;
32d1776b
FL
125 template_len = strlen(template_dir);
126 if (PATH_MAX <= (template_len+strlen("/config")))
127 die("insanely long template path %s", template_dir);
8d5afef0 128 strcpy(template_path, template_dir);
8d5afef0
JH
129 if (template_path[template_len-1] != '/') {
130 template_path[template_len++] = '/';
131 template_path[template_len] = 0;
132 }
8d5afef0 133 dir = opendir(template_path);
d3af621b 134 if (!dir) {
2fd8c0a5 135 warning("templates not found %s", template_dir);
8d5afef0 136 return;
d3af621b
JH
137 }
138
4f629539
JH
139 /* Make sure that template is from the correct vintage */
140 strcpy(template_path + template_len, "config");
141 repository_format_version = 0;
142 git_config_from_file(check_repository_format_version,
ef90d6d4 143 template_path, NULL);
4f629539
JH
144 template_path[template_len] = 0;
145
146 if (repository_format_version &&
147 repository_format_version != GIT_REPO_VERSION) {
2fd8c0a5
MV
148 warning("not copying templates of "
149 "a wrong format version %d from '%s'",
4f629539
JH
150 repository_format_version,
151 template_dir);
152 closedir(dir);
153 return;
154 }
155
d3af621b 156 memcpy(path, git_dir, len);
f225aeb2
DB
157 if (len && path[len - 1] != '/')
158 path[len++] = '/';
1f961c19 159 path[len] = 0;
8d5afef0
JH
160 copy_templates_1(path, len,
161 template_path, template_len,
162 dir);
163 closedir(dir);
164}
165
f225aeb2 166static int create_default_files(const char *template_path)
cad88fdf 167{
f225aeb2 168 const char *git_dir = get_git_dir();
cad88fdf
LT
169 unsigned len = strlen(git_dir);
170 static char path[PATH_MAX];
4f629539
JH
171 struct stat st1;
172 char repo_version_string[10];
5cc8f372 173 char junk[2];
ef0a89a6 174 int reinit;
c869753e 175 int filemode;
cad88fdf
LT
176
177 if (len > sizeof(path)-50)
178 die("insane git directory %s", git_dir);
179 memcpy(path, git_dir, len);
180
181 if (len && path[len-1] != '/')
182 path[len++] = '/';
183
184 /*
185 * Create .git/refs/{heads,tags}
186 */
f225aeb2
DB
187 safe_create_dir(git_path("refs"), 1);
188 safe_create_dir(git_path("refs/heads"), 1);
189 safe_create_dir(git_path("refs/tags"), 1);
cad88fdf 190
4f629539
JH
191 /* First copy the templates -- we might have the default
192 * config file there, in which case we would want to read
193 * from it after installing.
194 */
f225aeb2 195 copy_templates(template_path);
4f629539 196
ef90d6d4 197 git_config(git_default_config, NULL);
0a2c7eea 198 is_bare_repository_cfg = init_is_bare_repository;
5a688fe4
JH
199
200 /* reading existing config may have overwrote it */
0a2c7eea
DM
201 if (init_shared_repository != -1)
202 shared_repository = init_shared_repository;
4f629539 203
138086a7
JH
204 /*
205 * We would have created the above under user's umask -- under
206 * shared-repository settings, we would need to fix them up.
207 */
208 if (shared_repository) {
f225aeb2
DB
209 adjust_shared_perm(get_git_dir());
210 adjust_shared_perm(git_path("refs"));
211 adjust_shared_perm(git_path("refs/heads"));
212 adjust_shared_perm(git_path("refs/tags"));
138086a7
JH
213 }
214
cad88fdf
LT
215 /*
216 * Create the default symlink from ".git/HEAD" to the "master"
8098a178 217 * branch, if it does not exist yet.
cad88fdf
LT
218 */
219 strcpy(path + len, "HEAD");
5cc8f372
JS
220 reinit = (!access(path, R_OK)
221 || readlink(path, junk, sizeof(junk)-1) != -1);
ef0a89a6 222 if (!reinit) {
8b5157e4 223 if (create_symref("HEAD", "refs/heads/master", NULL) < 0)
cad88fdf 224 exit(1);
cad88fdf 225 }
e24317b4 226
4f629539
JH
227 /* This forces creation of new config file */
228 sprintf(repo_version_string, "%d", GIT_REPO_VERSION);
229 git_config_set("core.repositoryformatversion", repo_version_string);
230
231 path[len] = 0;
e24317b4 232 strcpy(path + len, "config");
4f629539
JH
233
234 /* Check filemode trustability */
c869753e
SP
235 filemode = TEST_FILEMODE;
236 if (TEST_FILEMODE && !lstat(path, &st1)) {
4f629539 237 struct stat st2;
c869753e 238 filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) &&
4f629539
JH
239 !lstat(path, &st2) &&
240 st1.st_mode != st2.st_mode);
e24317b4 241 }
c869753e 242 git_config_set("core.filemode", filemode ? "true" : "false");
ef0a89a6 243
e90fdc39 244 if (is_bare_repository())
7d1864ce 245 git_config_set("core.bare", "true");
7d1864ce 246 else {
e90fdc39 247 const char *work_tree = get_git_work_tree();
7d1864ce 248 git_config_set("core.bare", "false");
196055c2
AR
249 /* allow template config file to override the default */
250 if (log_all_ref_updates == -1)
251 git_config_set("core.logallrefupdates", "true");
f225aeb2
DB
252 if (prefixcmp(git_dir, work_tree) ||
253 strcmp(git_dir + strlen(work_tree), "/.git")) {
e90fdc39 254 git_config_set("core.worktree", work_tree);
f225aeb2 255 }
7d1864ce 256 }
75d24499 257
75d24499 258 if (!reinit) {
2455406a 259 /* Check if symlink is supported in the work tree */
75d24499
JH
260 path[len] = 0;
261 strcpy(path + len, "tXXXXXX");
262 if (!close(xmkstemp(path)) &&
263 !unlink(path) &&
264 !symlink("testing", path) &&
265 !lstat(path, &st1) &&
266 S_ISLNK(st1.st_mode))
267 unlink(path); /* good */
268 else
269 git_config_set("core.symlinks", "false");
2455406a
DP
270
271 /* Check if the filesystem is case-insensitive */
272 path[len] = 0;
273 strcpy(path + len, "CoNfIg");
274 if (!access(path, F_OK))
275 git_config_set("core.ignorecase", "true");
75d24499
JH
276 }
277
ef0a89a6 278 return reinit;
cad88fdf
LT
279}
280
f225aeb2
DB
281int init_db(const char *template_dir, unsigned int flags)
282{
283 const char *sha1_dir;
284 char *path;
285 int len, reinit;
286
287 safe_create_dir(get_git_dir(), 0);
288
0a2c7eea
DM
289 init_is_bare_repository = is_bare_repository();
290
f225aeb2
DB
291 /* Check to see if the repository version is right.
292 * Note that a newly created repository does not have
293 * config file, so this will not fail. What we are catching
294 * is an attempt to reinitialize new repository with an old tool.
295 */
296 check_repository_format();
297
298 reinit = create_default_files(template_dir);
299
300 sha1_dir = get_object_directory();
301 len = strlen(sha1_dir);
302 path = xmalloc(len + 40);
303 memcpy(path, sha1_dir, len);
304
305 safe_create_dir(sha1_dir, 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 if (shared_repository) {
312 char buf[10];
313 /* We do not spell "group" and such, so that
314 * the configuration can be read by older version
315 * of git. Note, we use octal numbers for new share modes,
316 * and compatibility values for PERM_GROUP and
317 * PERM_EVERYBODY.
318 */
5a688fe4
JH
319 if (shared_repository < 0)
320 /* force to the mode value */
321 sprintf(buf, "0%o", -shared_repository);
322 else if (shared_repository == PERM_GROUP)
f225aeb2
DB
323 sprintf(buf, "%d", OLD_PERM_GROUP);
324 else if (shared_repository == PERM_EVERYBODY)
325 sprintf(buf, "%d", OLD_PERM_EVERYBODY);
326 else
5a688fe4 327 die("oops");
f225aeb2
DB
328 git_config_set("core.sharedrepository", buf);
329 git_config_set("receive.denyNonFastforwards", "true");
330 }
331
332 if (!(flags & INIT_DB_QUIET))
333 printf("%s%s Git repository in %s/\n",
334 reinit ? "Reinitialized existing" : "Initialized empty",
335 shared_repository ? " shared" : "",
336 get_git_dir());
337
338 return 0;
339}
340
341static int guess_repository_type(const char *git_dir)
6adcca3f
JH
342{
343 char cwd[PATH_MAX];
344 const char *slash;
345
6adcca3f
JH
346 /*
347 * "GIT_DIR=. git init" is always bare.
348 * "GIT_DIR=`pwd` git init" too.
349 */
350 if (!strcmp(".", git_dir))
f225aeb2 351 return 1;
6adcca3f
JH
352 if (!getcwd(cwd, sizeof(cwd)))
353 die("cannot tell cwd");
354 if (!strcmp(git_dir, cwd))
f225aeb2 355 return 1;
6adcca3f
JH
356 /*
357 * "GIT_DIR=.git or GIT_DIR=something/.git is usually not.
358 */
359 if (!strcmp(git_dir, ".git"))
f225aeb2 360 return 0;
6adcca3f
JH
361 slash = strrchr(git_dir, '/');
362 if (slash && !strcmp(slash, "/.git"))
f225aeb2 363 return 0;
6adcca3f
JH
364
365 /*
366 * Otherwise it is often bare. At this point
367 * we are just guessing.
368 */
f225aeb2 369 return 1;
6adcca3f
JH
370}
371
d3af621b 372static const char init_db_usage[] =
1b1dd23f 373"git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]]";
d3af621b 374
4696cb93
ZW
375/*
376 * If you want to, you can share the DB area with any number of branches.
377 * That has advantages: you can save space by sharing all the SHA1 objects.
378 * On the other hand, it might just make lookup slower and messier. You
379 * be the judge. The default case is to have one DB per managed directory.
380 */
a633fca0 381int cmd_init_db(int argc, const char **argv, const char *prefix)
e83c5163 382{
cad88fdf 383 const char *git_dir;
c3c8835f 384 const char *template_dir = NULL;
f225aeb2
DB
385 unsigned int flags = 0;
386 int i;
e83c5163 387
d3af621b 388 for (i = 1; i < argc; i++, argv++) {
c3c8835f 389 const char *arg = argv[1];
cc44c765 390 if (!prefixcmp(arg, "--template="))
d3af621b 391 template_dir = arg+11;
74d3b23f
LR
392 else if (!strcmp(arg, "--bare")) {
393 static char git_dir[PATH_MAX+1];
394 is_bare_repository_cfg = 1;
395 setenv(GIT_DIR_ENVIRONMENT, getcwd(git_dir,
396 sizeof(git_dir)), 0);
397 } else if (!strcmp(arg, "--shared"))
0a2c7eea 398 init_shared_repository = PERM_GROUP;
cc44c765 399 else if (!prefixcmp(arg, "--shared="))
0a2c7eea 400 init_shared_repository = git_config_perm("arg", arg+9);
4576518d 401 else if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet"))
f225aeb2 402 flags |= INIT_DB_QUIET;
d3af621b 403 else
8cdf3364 404 usage(init_db_usage);
d3af621b
JH
405 }
406
5a688fe4
JH
407 if (init_shared_repository != -1)
408 shared_repository = init_shared_repository;
409
6adcca3f
JH
410 /*
411 * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
412 * without --bare. Catch the error early.
413 */
414 git_dir = getenv(GIT_DIR_ENVIRONMENT);
415 if ((!git_dir || is_bare_repository_cfg == 1)
416 && getenv(GIT_WORK_TREE_ENVIRONMENT))
417 die("%s (or --work-tree=<directory>) not allowed without "
418 "specifying %s (or --git-dir=<directory>)",
419 GIT_WORK_TREE_ENVIRONMENT,
420 GIT_DIR_ENVIRONMENT);
421
cad88fdf
LT
422 /*
423 * Set up the default .git directory contents
424 */
ef0a89a6 425 if (!git_dir)
cad88fdf 426 git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
af6e277c 427
f225aeb2
DB
428 if (is_bare_repository_cfg < 0)
429 is_bare_repository_cfg = guess_repository_type(git_dir);
430
431 if (!is_bare_repository_cfg) {
432 if (git_dir) {
433 const char *git_dir_parent = strrchr(git_dir, '/');
434 if (git_dir_parent) {
435 char *rel = xstrndup(git_dir, git_dir_parent - git_dir);
436 git_work_tree_cfg = xstrdup(make_absolute_path(rel));
437 free(rel);
438 }
439 }
440 if (!git_work_tree_cfg) {
441 git_work_tree_cfg = xcalloc(PATH_MAX, 1);
442 if (!getcwd(git_work_tree_cfg, PATH_MAX))
443 die ("Cannot access current working directory.");
444 }
445 if (access(get_git_work_tree(), X_OK))
446 die ("Cannot access work tree '%s'",
447 get_git_work_tree());
94df2506 448 }
af6e277c 449
f225aeb2 450 set_git_dir(make_absolute_path(git_dir));
ef0a89a6 451
f225aeb2 452 return init_db(template_dir, flags);
e83c5163 453}