]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-init-db.c
git-submodule summary: show commit summary
[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
af6e277c 20static void safe_create_dir(const char *dir, int share)
cb126d8d 21{
f312de01 22 if (mkdir(dir, 0777) < 0) {
cb126d8d
ZW
23 if (errno != EEXIST) {
24 perror(dir);
25 exit(1);
26 }
27 }
af6e277c
JS
28 else if (share && adjust_shared_perm(dir))
29 die("Could not make %s writable by group\n", dir);
cb126d8d
ZW
30}
31
8d5afef0
JH
32static void copy_templates_1(char *path, int baselen,
33 char *template, int template_baselen,
34 DIR *dir)
35{
36 struct dirent *de;
37
38 /* Note: if ".git/hooks" file exists in the repository being
39 * re-initialized, /etc/core-git/templates/hooks/update would
5c94f87e 40 * cause git-init to fail here. I think this is sane but
8d5afef0
JH
41 * it means that the set of templates we ship by default, along
42 * with the way the namespace under .git/ is organized, should
43 * be really carefully chosen.
44 */
af6e277c 45 safe_create_dir(path, 1);
8d5afef0
JH
46 while ((de = readdir(dir)) != NULL) {
47 struct stat st_git, st_template;
48 int namelen;
49 int exists = 0;
50
51 if (de->d_name[0] == '.')
52 continue;
53 namelen = strlen(de->d_name);
54 if ((PATH_MAX <= baselen + namelen) ||
55 (PATH_MAX <= template_baselen + namelen))
56 die("insanely long template name %s", de->d_name);
57 memcpy(path + baselen, de->d_name, namelen+1);
58 memcpy(template + template_baselen, de->d_name, namelen+1);
59 if (lstat(path, &st_git)) {
60 if (errno != ENOENT)
61 die("cannot stat %s", path);
62 }
63 else
64 exists = 1;
65
66 if (lstat(template, &st_template))
67 die("cannot stat template %s", template);
68
69 if (S_ISDIR(st_template.st_mode)) {
70 DIR *subdir = opendir(template);
71 int baselen_sub = baselen + namelen;
72 int template_baselen_sub = template_baselen + namelen;
73 if (!subdir)
74 die("cannot opendir %s", template);
75 path[baselen_sub++] =
76 template[template_baselen_sub++] = '/';
77 path[baselen_sub] =
78 template[template_baselen_sub] = 0;
79 copy_templates_1(path, baselen_sub,
80 template, template_baselen_sub,
81 subdir);
82 closedir(subdir);
83 }
84 else if (exists)
85 continue;
86 else if (S_ISLNK(st_template.st_mode)) {
87 char lnk[256];
88 int len;
89 len = readlink(template, lnk, sizeof(lnk));
90 if (len < 0)
91 die("cannot readlink %s", template);
92 if (sizeof(lnk) <= len)
93 die("insanely long symlink %s", template);
94 lnk[len] = 0;
95 if (symlink(lnk, path))
96 die("cannot symlink %s %s", lnk, path);
97 }
98 else if (S_ISREG(st_template.st_mode)) {
99 if (copy_file(path, template, st_template.st_mode))
100 die("cannot copy %s to %s", template, path);
101 }
102 else
103 error("ignoring template %s", template);
104 }
105}
106
c3c8835f 107static void copy_templates(const char *git_dir, int len, const char *template_dir)
8d5afef0
JH
108{
109 char path[PATH_MAX];
110 char template_path[PATH_MAX];
d3af621b 111 int template_len;
8d5afef0
JH
112 DIR *dir;
113
a47d1813 114 if (!template_dir)
d4ebc36c 115 template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT);
a47d1813
JS
116 if (!template_dir) {
117 /*
118 * if the hard-coded template is relative, it is
119 * interpreted relative to the exec_dir
120 */
121 template_dir = DEFAULT_GIT_TEMPLATE_DIR;
122 if (!is_absolute_path(template_dir)) {
ef5b9d6e
JS
123 struct strbuf d = STRBUF_INIT;
124 strbuf_addf(&d, "%s/%s", git_exec_path(), template_dir);
125 template_dir = strbuf_detach(&d, NULL);
a47d1813 126 }
8683a45d 127 }
8d5afef0
JH
128 strcpy(template_path, template_dir);
129 template_len = strlen(template_path);
130 if (template_path[template_len-1] != '/') {
131 template_path[template_len++] = '/';
132 template_path[template_len] = 0;
133 }
8d5afef0 134 dir = opendir(template_path);
d3af621b
JH
135 if (!dir) {
136 fprintf(stderr, "warning: templates not found %s\n",
137 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,
145 template_path);
146 template_path[template_len] = 0;
147
148 if (repository_format_version &&
149 repository_format_version != GIT_REPO_VERSION) {
150 fprintf(stderr, "warning: not copying templates of "
151 "a wrong format version %d from '%s'\n",
152 repository_format_version,
153 template_dir);
154 closedir(dir);
155 return;
156 }
157
d3af621b 158 memcpy(path, git_dir, 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
e90fdc39 166static int create_default_files(const char *git_dir, const char *template_path)
cad88fdf
LT
167{
168 unsigned len = strlen(git_dir);
169 static char path[PATH_MAX];
8098a178 170 unsigned char sha1[20];
4f629539
JH
171 struct stat st1;
172 char repo_version_string[10];
ef0a89a6 173 int reinit;
c869753e 174 int filemode;
cad88fdf
LT
175
176 if (len > sizeof(path)-50)
177 die("insane git directory %s", git_dir);
178 memcpy(path, git_dir, len);
179
180 if (len && path[len-1] != '/')
181 path[len++] = '/';
182
183 /*
184 * Create .git/refs/{heads,tags}
185 */
186 strcpy(path + len, "refs");
af6e277c 187 safe_create_dir(path, 1);
cad88fdf 188 strcpy(path + len, "refs/heads");
af6e277c 189 safe_create_dir(path, 1);
cad88fdf 190 strcpy(path + len, "refs/tags");
af6e277c 191 safe_create_dir(path, 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 */
197 path[len] = 0;
198 copy_templates(path, len, template_path);
199
200 git_config(git_default_config);
201
138086a7
JH
202 /*
203 * We would have created the above under user's umask -- under
204 * shared-repository settings, we would need to fix them up.
205 */
206 if (shared_repository) {
207 path[len] = 0;
208 adjust_shared_perm(path);
209 strcpy(path + len, "refs");
210 adjust_shared_perm(path);
211 strcpy(path + len, "refs/heads");
212 adjust_shared_perm(path);
213 strcpy(path + len, "refs/tags");
214 adjust_shared_perm(path);
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");
ef0a89a6
SP
222 reinit = !read_ref("HEAD", sha1);
223 if (!reinit) {
8b5157e4 224 if (create_symref("HEAD", "refs/heads/master", NULL) < 0)
cad88fdf 225 exit(1);
cad88fdf 226 }
e24317b4 227
4f629539
JH
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
232 path[len] = 0;
e24317b4 233 strcpy(path + len, "config");
4f629539
JH
234
235 /* Check filemode trustability */
c869753e
SP
236 filemode = TEST_FILEMODE;
237 if (TEST_FILEMODE && !lstat(path, &st1)) {
4f629539 238 struct stat st2;
c869753e 239 filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) &&
4f629539
JH
240 !lstat(path, &st2) &&
241 st1.st_mode != st2.st_mode);
e24317b4 242 }
c869753e 243 git_config_set("core.filemode", filemode ? "true" : "false");
ef0a89a6 244
e90fdc39 245 if (is_bare_repository())
7d1864ce 246 git_config_set("core.bare", "true");
7d1864ce 247 else {
e90fdc39 248 const char *work_tree = get_git_work_tree();
7d1864ce 249 git_config_set("core.bare", "false");
196055c2
AR
250 /* allow template config file to override the default */
251 if (log_all_ref_updates == -1)
252 git_config_set("core.logallrefupdates", "true");
e90fdc39
JS
253 if (work_tree != git_work_tree_cfg)
254 git_config_set("core.worktree", work_tree);
7d1864ce 255 }
75d24499
JH
256
257 /* Check if symlink is supported in the work tree */
258 if (!reinit) {
259 path[len] = 0;
260 strcpy(path + len, "tXXXXXX");
261 if (!close(xmkstemp(path)) &&
262 !unlink(path) &&
263 !symlink("testing", path) &&
264 !lstat(path, &st1) &&
265 S_ISLNK(st1.st_mode))
266 unlink(path); /* good */
267 else
268 git_config_set("core.symlinks", "false");
269 }
270
ef0a89a6 271 return reinit;
cad88fdf
LT
272}
273
6adcca3f
JH
274static void guess_repository_type(const char *git_dir)
275{
276 char cwd[PATH_MAX];
277 const char *slash;
278
279 if (0 <= is_bare_repository_cfg)
280 return;
281 if (!git_dir)
282 return;
283
284 /*
285 * "GIT_DIR=. git init" is always bare.
286 * "GIT_DIR=`pwd` git init" too.
287 */
288 if (!strcmp(".", git_dir))
289 goto force_bare;
290 if (!getcwd(cwd, sizeof(cwd)))
291 die("cannot tell cwd");
292 if (!strcmp(git_dir, cwd))
293 goto force_bare;
294 /*
295 * "GIT_DIR=.git or GIT_DIR=something/.git is usually not.
296 */
297 if (!strcmp(git_dir, ".git"))
298 return;
299 slash = strrchr(git_dir, '/');
300 if (slash && !strcmp(slash, "/.git"))
301 return;
302
303 /*
304 * Otherwise it is often bare. At this point
305 * we are just guessing.
306 */
307 force_bare:
308 is_bare_repository_cfg = 1;
309 return;
310}
311
d3af621b 312static const char init_db_usage[] =
4576518d 313"git-init [-q | --quiet] [--template=<template-directory>] [--shared]";
d3af621b 314
4696cb93
ZW
315/*
316 * If you want to, you can share the DB area with any number of branches.
317 * That has advantages: you can save space by sharing all the SHA1 objects.
318 * On the other hand, it might just make lookup slower and messier. You
319 * be the judge. The default case is to have one DB per managed directory.
320 */
a633fca0 321int cmd_init_db(int argc, const char **argv, const char *prefix)
e83c5163 322{
cad88fdf 323 const char *git_dir;
d19938ab 324 const char *sha1_dir;
c3c8835f
TH
325 const char *template_dir = NULL;
326 char *path;
ef0a89a6 327 int len, i, reinit;
4576518d 328 int quiet = 0;
e83c5163 329
d3af621b 330 for (i = 1; i < argc; i++, argv++) {
c3c8835f 331 const char *arg = argv[1];
cc44c765 332 if (!prefixcmp(arg, "--template="))
d3af621b 333 template_dir = arg+11;
af6e277c 334 else if (!strcmp(arg, "--shared"))
94df2506 335 shared_repository = PERM_GROUP;
cc44c765 336 else if (!prefixcmp(arg, "--shared="))
94df2506 337 shared_repository = git_config_perm("arg", arg+9);
4576518d
JO
338 else if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet"))
339 quiet = 1;
d3af621b 340 else
8cdf3364 341 usage(init_db_usage);
d3af621b
JH
342 }
343
6adcca3f
JH
344 /*
345 * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
346 * without --bare. Catch the error early.
347 */
348 git_dir = getenv(GIT_DIR_ENVIRONMENT);
349 if ((!git_dir || is_bare_repository_cfg == 1)
350 && getenv(GIT_WORK_TREE_ENVIRONMENT))
351 die("%s (or --work-tree=<directory>) not allowed without "
352 "specifying %s (or --git-dir=<directory>)",
353 GIT_WORK_TREE_ENVIRONMENT,
354 GIT_DIR_ENVIRONMENT);
355
356 guess_repository_type(git_dir);
357
358 if (is_bare_repository_cfg <= 0) {
359 git_work_tree_cfg = xcalloc(PATH_MAX, 1);
360 if (!getcwd(git_work_tree_cfg, PATH_MAX))
361 die ("Cannot access current working directory.");
362 if (access(get_git_work_tree(), X_OK))
363 die ("Cannot access work tree '%s'",
364 get_git_work_tree());
365 }
ef6f0af2 366
cad88fdf
LT
367 /*
368 * Set up the default .git directory contents
369 */
a9ab586a 370 git_dir = getenv(GIT_DIR_ENVIRONMENT);
ef0a89a6 371 if (!git_dir)
cad88fdf 372 git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
af6e277c 373 safe_create_dir(git_dir, 0);
4f629539
JH
374
375 /* Check to see if the repository version is right.
376 * Note that a newly created repository does not have
377 * config file, so this will not fail. What we are catching
378 * is an attempt to reinitialize new repository with an old tool.
379 */
380 check_repository_format();
381
e90fdc39 382 reinit = create_default_files(git_dir, template_dir);
cad88fdf
LT
383
384 /*
385 * And set up the object store.
386 */
387 sha1_dir = get_object_directory();
e83c5163 388 len = strlen(sha1_dir);
812666c8 389 path = xmalloc(len + 40);
e83c5163 390 memcpy(path, sha1_dir, len);
cb126d8d 391
af6e277c 392 safe_create_dir(sha1_dir, 1);
f49fb35d 393 strcpy(path+len, "/pack");
af6e277c 394 safe_create_dir(path, 1);
d57306c7 395 strcpy(path+len, "/info");
af6e277c
JS
396 safe_create_dir(path, 1);
397
94df2506
JH
398 if (shared_repository) {
399 char buf[10];
400 /* We do not spell "group" and such, so that
401 * the configuration can be read by older version
402 * of git.
403 */
404 sprintf(buf, "%d", shared_repository);
405 git_config_set("core.sharedrepository", buf);
11031d7e 406 git_config_set("receive.denyNonFastforwards", "true");
94df2506 407 }
af6e277c 408
4576518d
JO
409 if (!quiet)
410 printf("%s%s Git repository in %s/\n",
411 reinit ? "Reinitialized existing" : "Initialized empty",
412 shared_repository ? " shared" : "",
413 git_dir);
ef0a89a6 414
e83c5163
LT
415 return 0;
416}