blob: e1393b8d1e74c03ff2b45ec93e268daa2e286fd8 [file] [log] [blame]
Linus Torvalds8bc9a0c2005-04-07 15:16:10 -07001/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
Linus Torvaldse83c5162005-04-07 15:13:13 -07006#include "cache.h"
Timo Hirvonenc3c88352006-05-19 13:03:57 +03007#include "builtin.h"
Johannes Sixta47d1812007-11-13 21:05:04 +01008#include "exec_cmd.h"
Linus Torvaldse83c5162005-04-07 15:13:13 -07009
Junio C Hamanod3af6212005-08-06 12:50:14 -070010#ifndef DEFAULT_GIT_TEMPLATE_DIR
Johannes Sixtd52fd422007-06-11 11:10:47 +020011#define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates"
Junio C Hamanod3af6212005-08-06 12:50:14 -070012#endif
13
Shawn O. Pearcec8697532006-12-30 23:53:55 -050014#ifdef NO_TRUSTABLE_FILEMODE
15#define TEST_FILEMODE 0
16#else
17#define TEST_FILEMODE 1
18#endif
19
Johannes Schindelinaf6e2772005-12-22 23:19:37 +010020static void safe_create_dir(const char *dir, int share)
Zach Welchcb126d82005-04-19 21:48:15 -070021{
Junio C Hamanof312de02005-07-06 01:21:46 -070022 if (mkdir(dir, 0777) < 0) {
Zach Welchcb126d82005-04-19 21:48:15 -070023 if (errno != EEXIST) {
24 perror(dir);
25 exit(1);
26 }
27 }
Johannes Schindelinaf6e2772005-12-22 23:19:37 +010028 else if (share && adjust_shared_perm(dir))
29 die("Could not make %s writable by group\n", dir);
Zach Welchcb126d82005-04-19 21:48:15 -070030}
31
Junio C Hamano8d5afef2005-08-02 16:45:21 -070032static int copy_file(const char *dst, const char *src, int mode)
33{
Junio C Hamano32276c82005-11-05 11:07:22 -080034 int fdi, fdo, status;
Junio C Hamano8d5afef2005-08-02 16:45:21 -070035
36 mode = (mode & 0111) ? 0777 : 0666;
37 if ((fdi = open(src, O_RDONLY)) < 0)
38 return fdi;
39 if ((fdo = open(dst, O_WRONLY | O_CREAT | O_EXCL, mode)) < 0) {
40 close(fdi);
41 return fdo;
42 }
Junio C Hamano32276c82005-11-05 11:07:22 -080043 status = copy_fd(fdi, fdo);
Jim Meyering91c8d592007-06-24 21:20:41 +020044 if (close(fdo) != 0)
45 return error("%s: write error: %s", dst, strerror(errno));
Johannes Schindelinaf6e2772005-12-22 23:19:37 +010046
47 if (!status && adjust_shared_perm(dst))
48 return -1;
49
Junio C Hamano32276c82005-11-05 11:07:22 -080050 return status;
Junio C Hamano8d5afef2005-08-02 16:45:21 -070051}
52
53static void copy_templates_1(char *path, int baselen,
54 char *template, int template_baselen,
55 DIR *dir)
56{
57 struct dirent *de;
58
59 /* Note: if ".git/hooks" file exists in the repository being
60 * re-initialized, /etc/core-git/templates/hooks/update would
Nicolas Pitre5c94f872007-01-12 16:01:46 -050061 * cause git-init to fail here. I think this is sane but
Junio C Hamano8d5afef2005-08-02 16:45:21 -070062 * it means that the set of templates we ship by default, along
63 * with the way the namespace under .git/ is organized, should
64 * be really carefully chosen.
65 */
Johannes Schindelinaf6e2772005-12-22 23:19:37 +010066 safe_create_dir(path, 1);
Junio C Hamano8d5afef2005-08-02 16:45:21 -070067 while ((de = readdir(dir)) != NULL) {
68 struct stat st_git, st_template;
69 int namelen;
70 int exists = 0;
71
72 if (de->d_name[0] == '.')
73 continue;
74 namelen = strlen(de->d_name);
75 if ((PATH_MAX <= baselen + namelen) ||
76 (PATH_MAX <= template_baselen + namelen))
77 die("insanely long template name %s", de->d_name);
78 memcpy(path + baselen, de->d_name, namelen+1);
79 memcpy(template + template_baselen, de->d_name, namelen+1);
80 if (lstat(path, &st_git)) {
81 if (errno != ENOENT)
82 die("cannot stat %s", path);
83 }
84 else
85 exists = 1;
86
87 if (lstat(template, &st_template))
88 die("cannot stat template %s", template);
89
90 if (S_ISDIR(st_template.st_mode)) {
91 DIR *subdir = opendir(template);
92 int baselen_sub = baselen + namelen;
93 int template_baselen_sub = template_baselen + namelen;
94 if (!subdir)
95 die("cannot opendir %s", template);
96 path[baselen_sub++] =
97 template[template_baselen_sub++] = '/';
98 path[baselen_sub] =
99 template[template_baselen_sub] = 0;
100 copy_templates_1(path, baselen_sub,
101 template, template_baselen_sub,
102 subdir);
103 closedir(subdir);
104 }
105 else if (exists)
106 continue;
107 else if (S_ISLNK(st_template.st_mode)) {
108 char lnk[256];
109 int len;
110 len = readlink(template, lnk, sizeof(lnk));
111 if (len < 0)
112 die("cannot readlink %s", template);
113 if (sizeof(lnk) <= len)
114 die("insanely long symlink %s", template);
115 lnk[len] = 0;
116 if (symlink(lnk, path))
117 die("cannot symlink %s %s", lnk, path);
118 }
119 else if (S_ISREG(st_template.st_mode)) {
120 if (copy_file(path, template, st_template.st_mode))
121 die("cannot copy %s to %s", template, path);
122 }
123 else
124 error("ignoring template %s", template);
125 }
126}
127
Timo Hirvonenc3c88352006-05-19 13:03:57 +0300128static void copy_templates(const char *git_dir, int len, const char *template_dir)
Junio C Hamano8d5afef2005-08-02 16:45:21 -0700129{
130 char path[PATH_MAX];
131 char template_path[PATH_MAX];
Junio C Hamanod3af6212005-08-06 12:50:14 -0700132 int template_len;
Junio C Hamano8d5afef2005-08-02 16:45:21 -0700133 DIR *dir;
134
Johannes Sixta47d1812007-11-13 21:05:04 +0100135 if (!template_dir)
Junio C Hamanod4ebc362006-12-19 01:28:15 -0800136 template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT);
Johannes Sixta47d1812007-11-13 21:05:04 +0100137 if (!template_dir) {
138 /*
139 * if the hard-coded template is relative, it is
140 * interpreted relative to the exec_dir
141 */
142 template_dir = DEFAULT_GIT_TEMPLATE_DIR;
143 if (!is_absolute_path(template_dir)) {
144 const char *exec_path = git_exec_path();
145 template_dir = prefix_path(exec_path, strlen(exec_path),
146 template_dir);
147 }
Johannes Schindelin8683a452006-12-19 09:18:09 +0100148 }
Junio C Hamano8d5afef2005-08-02 16:45:21 -0700149 strcpy(template_path, template_dir);
150 template_len = strlen(template_path);
151 if (template_path[template_len-1] != '/') {
152 template_path[template_len++] = '/';
153 template_path[template_len] = 0;
154 }
Junio C Hamano8d5afef2005-08-02 16:45:21 -0700155 dir = opendir(template_path);
Junio C Hamanod3af6212005-08-06 12:50:14 -0700156 if (!dir) {
157 fprintf(stderr, "warning: templates not found %s\n",
158 template_dir);
Junio C Hamano8d5afef2005-08-02 16:45:21 -0700159 return;
Junio C Hamanod3af6212005-08-06 12:50:14 -0700160 }
161
Junio C Hamano4f629532005-11-25 16:03:56 -0800162 /* Make sure that template is from the correct vintage */
163 strcpy(template_path + template_len, "config");
164 repository_format_version = 0;
165 git_config_from_file(check_repository_format_version,
166 template_path);
167 template_path[template_len] = 0;
168
169 if (repository_format_version &&
170 repository_format_version != GIT_REPO_VERSION) {
171 fprintf(stderr, "warning: not copying templates of "
172 "a wrong format version %d from '%s'\n",
173 repository_format_version,
174 template_dir);
175 closedir(dir);
176 return;
177 }
178
Junio C Hamanod3af6212005-08-06 12:50:14 -0700179 memcpy(path, git_dir, len);
Petr Baudis1f961c12005-09-20 02:19:50 +0200180 path[len] = 0;
Junio C Hamano8d5afef2005-08-02 16:45:21 -0700181 copy_templates_1(path, len,
182 template_path, template_len,
183 dir);
184 closedir(dir);
185}
186
Johannes Schindeline90fdc32007-08-01 01:30:14 +0100187static int create_default_files(const char *git_dir, const char *template_path)
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700188{
189 unsigned len = strlen(git_dir);
190 static char path[PATH_MAX];
Junio C Hamano8098a172005-09-30 14:26:57 -0700191 unsigned char sha1[20];
Junio C Hamano4f629532005-11-25 16:03:56 -0800192 struct stat st1;
193 char repo_version_string[10];
Shawn O. Pearceef0a89a2006-12-15 00:44:58 -0500194 int reinit;
Shawn O. Pearcec8697532006-12-30 23:53:55 -0500195 int filemode;
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700196
197 if (len > sizeof(path)-50)
198 die("insane git directory %s", git_dir);
199 memcpy(path, git_dir, len);
200
201 if (len && path[len-1] != '/')
202 path[len++] = '/';
203
204 /*
205 * Create .git/refs/{heads,tags}
206 */
207 strcpy(path + len, "refs");
Johannes Schindelinaf6e2772005-12-22 23:19:37 +0100208 safe_create_dir(path, 1);
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700209 strcpy(path + len, "refs/heads");
Johannes Schindelinaf6e2772005-12-22 23:19:37 +0100210 safe_create_dir(path, 1);
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700211 strcpy(path + len, "refs/tags");
Johannes Schindelinaf6e2772005-12-22 23:19:37 +0100212 safe_create_dir(path, 1);
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700213
Junio C Hamano4f629532005-11-25 16:03:56 -0800214 /* First copy the templates -- we might have the default
215 * config file there, in which case we would want to read
216 * from it after installing.
217 */
218 path[len] = 0;
219 copy_templates(path, len, template_path);
220
221 git_config(git_default_config);
222
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700223 /*
Junio C Hamano138086a2006-06-09 22:07:23 -0700224 * We would have created the above under user's umask -- under
225 * shared-repository settings, we would need to fix them up.
226 */
227 if (shared_repository) {
228 path[len] = 0;
229 adjust_shared_perm(path);
230 strcpy(path + len, "refs");
231 adjust_shared_perm(path);
232 strcpy(path + len, "refs/heads");
233 adjust_shared_perm(path);
234 strcpy(path + len, "refs/tags");
235 adjust_shared_perm(path);
236 }
237
238 /*
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700239 * Create the default symlink from ".git/HEAD" to the "master"
Junio C Hamano8098a172005-09-30 14:26:57 -0700240 * branch, if it does not exist yet.
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700241 */
242 strcpy(path + len, "HEAD");
Shawn O. Pearceef0a89a2006-12-15 00:44:58 -0500243 reinit = !read_ref("HEAD", sha1);
244 if (!reinit) {
Nicolas Pitre8b5157e2007-01-26 17:26:10 -0500245 if (create_symref("HEAD", "refs/heads/master", NULL) < 0)
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700246 exit(1);
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700247 }
Junio C Hamano4f629532005-11-25 16:03:56 -0800248
249 /* This forces creation of new config file */
250 sprintf(repo_version_string, "%d", GIT_REPO_VERSION);
251 git_config_set("core.repositoryformatversion", repo_version_string);
252
Junio C Hamano8098a172005-09-30 14:26:57 -0700253 path[len] = 0;
Johannes Schindeline24317b2005-10-26 01:43:03 +0200254 strcpy(path + len, "config");
Johannes Schindeline24317b2005-10-26 01:43:03 +0200255
Junio C Hamano4f629532005-11-25 16:03:56 -0800256 /* Check filemode trustability */
Shawn O. Pearcec8697532006-12-30 23:53:55 -0500257 filemode = TEST_FILEMODE;
258 if (TEST_FILEMODE && !lstat(path, &st1)) {
Junio C Hamano4f629532005-11-25 16:03:56 -0800259 struct stat st2;
Shawn O. Pearcec8697532006-12-30 23:53:55 -0500260 filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) &&
Junio C Hamano4f629532005-11-25 16:03:56 -0800261 !lstat(path, &st2) &&
262 st1.st_mode != st2.st_mode);
Johannes Schindeline24317b2005-10-26 01:43:03 +0200263 }
Shawn O. Pearcec8697532006-12-30 23:53:55 -0500264 git_config_set("core.filemode", filemode ? "true" : "false");
Shawn O. Pearceef0a89a2006-12-15 00:44:58 -0500265
Johannes Schindeline90fdc32007-08-01 01:30:14 +0100266 if (is_bare_repository())
Junio C Hamano7d1864c2007-01-07 02:00:28 -0800267 git_config_set("core.bare", "true");
Junio C Hamano7d1864c2007-01-07 02:00:28 -0800268 else {
Johannes Schindeline90fdc32007-08-01 01:30:14 +0100269 const char *work_tree = get_git_work_tree();
Junio C Hamano7d1864c2007-01-07 02:00:28 -0800270 git_config_set("core.bare", "false");
Alex Riesen196055c2007-01-23 16:51:18 +0100271 /* allow template config file to override the default */
272 if (log_all_ref_updates == -1)
273 git_config_set("core.logallrefupdates", "true");
Johannes Schindeline90fdc32007-08-01 01:30:14 +0100274 if (work_tree != git_work_tree_cfg)
275 git_config_set("core.worktree", work_tree);
Junio C Hamano7d1864c2007-01-07 02:00:28 -0800276 }
Junio C Hamano75d24492007-08-31 00:25:04 -0700277
278 /* Check if symlink is supported in the work tree */
279 if (!reinit) {
280 path[len] = 0;
281 strcpy(path + len, "tXXXXXX");
282 if (!close(xmkstemp(path)) &&
283 !unlink(path) &&
284 !symlink("testing", path) &&
285 !lstat(path, &st1) &&
286 S_ISLNK(st1.st_mode))
287 unlink(path); /* good */
288 else
289 git_config_set("core.symlinks", "false");
290 }
291
Shawn O. Pearceef0a89a2006-12-15 00:44:58 -0500292 return reinit;
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700293}
294
Junio C Hamano6adcca32007-08-27 00:58:06 -0700295static void guess_repository_type(const char *git_dir)
296{
297 char cwd[PATH_MAX];
298 const char *slash;
299
300 if (0 <= is_bare_repository_cfg)
301 return;
302 if (!git_dir)
303 return;
304
305 /*
306 * "GIT_DIR=. git init" is always bare.
307 * "GIT_DIR=`pwd` git init" too.
308 */
309 if (!strcmp(".", git_dir))
310 goto force_bare;
311 if (!getcwd(cwd, sizeof(cwd)))
312 die("cannot tell cwd");
313 if (!strcmp(git_dir, cwd))
314 goto force_bare;
315 /*
316 * "GIT_DIR=.git or GIT_DIR=something/.git is usually not.
317 */
318 if (!strcmp(git_dir, ".git"))
319 return;
320 slash = strrchr(git_dir, '/');
321 if (slash && !strcmp(slash, "/.git"))
322 return;
323
324 /*
325 * Otherwise it is often bare. At this point
326 * we are just guessing.
327 */
328 force_bare:
329 is_bare_repository_cfg = 1;
330 return;
331}
332
Junio C Hamanod3af6212005-08-06 12:50:14 -0700333static const char init_db_usage[] =
Jeffrey C. Ollie45765182007-06-07 07:50:29 -0500334"git-init [-q | --quiet] [--template=<template-directory>] [--shared]";
Junio C Hamanod3af6212005-08-06 12:50:14 -0700335
Zach Welch4696cb92005-04-19 21:48:15 -0700336/*
337 * If you want to, you can share the DB area with any number of branches.
338 * That has advantages: you can save space by sharing all the SHA1 objects.
339 * On the other hand, it might just make lookup slower and messier. You
340 * be the judge. The default case is to have one DB per managed directory.
341 */
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700342int cmd_init_db(int argc, const char **argv, const char *prefix)
Linus Torvaldse83c5162005-04-07 15:13:13 -0700343{
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700344 const char *git_dir;
Junio C Hamanod19938a2005-05-09 17:57:56 -0700345 const char *sha1_dir;
Timo Hirvonenc3c88352006-05-19 13:03:57 +0300346 const char *template_dir = NULL;
347 char *path;
Shawn O. Pearceef0a89a2006-12-15 00:44:58 -0500348 int len, i, reinit;
Jeffrey C. Ollie45765182007-06-07 07:50:29 -0500349 int quiet = 0;
Linus Torvaldse83c5162005-04-07 15:13:13 -0700350
Junio C Hamanod3af6212005-08-06 12:50:14 -0700351 for (i = 1; i < argc; i++, argv++) {
Timo Hirvonenc3c88352006-05-19 13:03:57 +0300352 const char *arg = argv[1];
Junio C Hamanocc44c762007-02-20 01:53:29 -0800353 if (!prefixcmp(arg, "--template="))
Junio C Hamanod3af6212005-08-06 12:50:14 -0700354 template_dir = arg+11;
Johannes Schindelinaf6e2772005-12-22 23:19:37 +0100355 else if (!strcmp(arg, "--shared"))
Junio C Hamano94df2502006-06-09 23:09:49 -0700356 shared_repository = PERM_GROUP;
Junio C Hamanocc44c762007-02-20 01:53:29 -0800357 else if (!prefixcmp(arg, "--shared="))
Junio C Hamano94df2502006-06-09 23:09:49 -0700358 shared_repository = git_config_perm("arg", arg+9);
Jeffrey C. Ollie45765182007-06-07 07:50:29 -0500359 else if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet"))
360 quiet = 1;
Junio C Hamanod3af6212005-08-06 12:50:14 -0700361 else
Ramsay Allan Jones8cdf3362006-08-03 16:48:41 +0100362 usage(init_db_usage);
Junio C Hamanod3af6212005-08-06 12:50:14 -0700363 }
364
Junio C Hamano6adcca32007-08-27 00:58:06 -0700365 /*
366 * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
367 * without --bare. Catch the error early.
368 */
369 git_dir = getenv(GIT_DIR_ENVIRONMENT);
370 if ((!git_dir || is_bare_repository_cfg == 1)
371 && getenv(GIT_WORK_TREE_ENVIRONMENT))
372 die("%s (or --work-tree=<directory>) not allowed without "
373 "specifying %s (or --git-dir=<directory>)",
374 GIT_WORK_TREE_ENVIRONMENT,
375 GIT_DIR_ENVIRONMENT);
376
377 guess_repository_type(git_dir);
378
379 if (is_bare_repository_cfg <= 0) {
380 git_work_tree_cfg = xcalloc(PATH_MAX, 1);
381 if (!getcwd(git_work_tree_cfg, PATH_MAX))
382 die ("Cannot access current working directory.");
383 if (access(get_git_work_tree(), X_OK))
384 die ("Cannot access work tree '%s'",
385 get_git_work_tree());
386 }
Matthias Lederhoferef6f0af2007-07-04 00:49:19 +0200387
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700388 /*
389 * Set up the default .git directory contents
390 */
Junio C Hamanoa9ab5862005-09-09 14:48:54 -0700391 git_dir = getenv(GIT_DIR_ENVIRONMENT);
Shawn O. Pearceef0a89a2006-12-15 00:44:58 -0500392 if (!git_dir)
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700393 git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
Johannes Schindelinaf6e2772005-12-22 23:19:37 +0100394 safe_create_dir(git_dir, 0);
Junio C Hamano4f629532005-11-25 16:03:56 -0800395
396 /* Check to see if the repository version is right.
397 * Note that a newly created repository does not have
398 * config file, so this will not fail. What we are catching
399 * is an attempt to reinitialize new repository with an old tool.
400 */
401 check_repository_format();
402
Johannes Schindeline90fdc32007-08-01 01:30:14 +0100403 reinit = create_default_files(git_dir, template_dir);
Linus Torvaldscad88fd2005-05-30 10:20:44 -0700404
405 /*
406 * And set up the object store.
407 */
408 sha1_dir = get_object_directory();
Linus Torvaldse83c5162005-04-07 15:13:13 -0700409 len = strlen(sha1_dir);
Christopher Li812666c2005-04-26 12:00:58 -0700410 path = xmalloc(len + 40);
Linus Torvaldse83c5162005-04-07 15:13:13 -0700411 memcpy(path, sha1_dir, len);
Zach Welchcb126d82005-04-19 21:48:15 -0700412
Johannes Schindelinaf6e2772005-12-22 23:19:37 +0100413 safe_create_dir(sha1_dir, 1);
Linus Torvaldsf49fb352005-06-27 18:26:11 -0700414 strcpy(path+len, "/pack");
Johannes Schindelinaf6e2772005-12-22 23:19:37 +0100415 safe_create_dir(path, 1);
Junio C Hamanod57306c2005-08-20 02:05:31 -0700416 strcpy(path+len, "/info");
Johannes Schindelinaf6e2772005-12-22 23:19:37 +0100417 safe_create_dir(path, 1);
418
Junio C Hamano94df2502006-06-09 23:09:49 -0700419 if (shared_repository) {
420 char buf[10];
421 /* We do not spell "group" and such, so that
422 * the configuration can be read by older version
423 * of git.
424 */
425 sprintf(buf, "%d", shared_repository);
426 git_config_set("core.sharedrepository", buf);
Johannes Schindelin11031d72006-09-21 01:07:54 +0200427 git_config_set("receive.denyNonFastforwards", "true");
Junio C Hamano94df2502006-06-09 23:09:49 -0700428 }
Johannes Schindelinaf6e2772005-12-22 23:19:37 +0100429
Jeffrey C. Ollie45765182007-06-07 07:50:29 -0500430 if (!quiet)
431 printf("%s%s Git repository in %s/\n",
432 reinit ? "Reinitialized existing" : "Initialized empty",
433 shared_repository ? " shared" : "",
434 git_dir);
Shawn O. Pearceef0a89a2006-12-15 00:44:58 -0500435
Linus Torvaldse83c5162005-04-07 15:13:13 -0700436 return 0;
437}