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