git-web--browse: do not start the browser with nohup
[git] / builtin-init-db.c
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
20 static 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
32 static int copy_file(const char *dst, const char *src, int mode)
33 {
34         int fdi, fdo, status;
35
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         }
43         status = copy_fd(fdi, fdo);
44         if (close(fdo) != 0)
45                 return error("%s: write error: %s", dst, strerror(errno));
46
47         if (!status && adjust_shared_perm(dst))
48                 return -1;
49
50         return status;
51 }
52
53 static 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
61          * cause git-init to fail here.  I think this is sane but
62          * 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          */
66         safe_create_dir(path, 1);
67         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
128 static void copy_templates(const char *git_dir, int len, const char *template_dir)
129 {
130         char path[PATH_MAX];
131         char template_path[PATH_MAX];
132         int template_len;
133         DIR *dir;
134
135         if (!template_dir)
136                 template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT);
137         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_filename(exec_path, strlen(exec_path), template_dir);
146                 }
147         }
148         strcpy(template_path, template_dir);
149         template_len = strlen(template_path);
150         if (template_path[template_len-1] != '/') {
151                 template_path[template_len++] = '/';
152                 template_path[template_len] = 0;
153         }
154         dir = opendir(template_path);
155         if (!dir) {
156                 fprintf(stderr, "warning: templates not found %s\n",
157                         template_dir);
158                 return;
159         }
160
161         /* Make sure that template is from the correct vintage */
162         strcpy(template_path + template_len, "config");
163         repository_format_version = 0;
164         git_config_from_file(check_repository_format_version,
165                              template_path);
166         template_path[template_len] = 0;
167
168         if (repository_format_version &&
169             repository_format_version != GIT_REPO_VERSION) {
170                 fprintf(stderr, "warning: not copying templates of "
171                         "a wrong format version %d from '%s'\n",
172                         repository_format_version,
173                         template_dir);
174                 closedir(dir);
175                 return;
176         }
177
178         memcpy(path, git_dir, len);
179         path[len] = 0;
180         copy_templates_1(path, len,
181                          template_path, template_len,
182                          dir);
183         closedir(dir);
184 }
185
186 static int create_default_files(const char *git_dir, const char *template_path)
187 {
188         unsigned len = strlen(git_dir);
189         static char path[PATH_MAX];
190         unsigned char sha1[20];
191         struct stat st1;
192         char repo_version_string[10];
193         int reinit;
194         int filemode;
195
196         if (len > sizeof(path)-50)
197                 die("insane git directory %s", git_dir);
198         memcpy(path, git_dir, len);
199
200         if (len && path[len-1] != '/')
201                 path[len++] = '/';
202
203         /*
204          * Create .git/refs/{heads,tags}
205          */
206         strcpy(path + len, "refs");
207         safe_create_dir(path, 1);
208         strcpy(path + len, "refs/heads");
209         safe_create_dir(path, 1);
210         strcpy(path + len, "refs/tags");
211         safe_create_dir(path, 1);
212
213         /* First copy the templates -- we might have the default
214          * config file there, in which case we would want to read
215          * from it after installing.
216          */
217         path[len] = 0;
218         copy_templates(path, len, template_path);
219
220         git_config(git_default_config);
221
222         /*
223          * We would have created the above under user's umask -- under
224          * shared-repository settings, we would need to fix them up.
225          */
226         if (shared_repository) {
227                 path[len] = 0;
228                 adjust_shared_perm(path);
229                 strcpy(path + len, "refs");
230                 adjust_shared_perm(path);
231                 strcpy(path + len, "refs/heads");
232                 adjust_shared_perm(path);
233                 strcpy(path + len, "refs/tags");
234                 adjust_shared_perm(path);
235         }
236
237         /*
238          * Create the default symlink from ".git/HEAD" to the "master"
239          * branch, if it does not exist yet.
240          */
241         strcpy(path + len, "HEAD");
242         reinit = !read_ref("HEAD", sha1);
243         if (!reinit) {
244                 if (create_symref("HEAD", "refs/heads/master", NULL) < 0)
245                         exit(1);
246         }
247
248         /* This forces creation of new config file */
249         sprintf(repo_version_string, "%d", GIT_REPO_VERSION);
250         git_config_set("core.repositoryformatversion", repo_version_string);
251
252         path[len] = 0;
253         strcpy(path + len, "config");
254
255         /* Check filemode trustability */
256         filemode = TEST_FILEMODE;
257         if (TEST_FILEMODE && !lstat(path, &st1)) {
258                 struct stat st2;
259                 filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) &&
260                                 !lstat(path, &st2) &&
261                                 st1.st_mode != st2.st_mode);
262         }
263         git_config_set("core.filemode", filemode ? "true" : "false");
264
265         if (is_bare_repository())
266                 git_config_set("core.bare", "true");
267         else {
268                 const char *work_tree = get_git_work_tree();
269                 git_config_set("core.bare", "false");
270                 /* allow template config file to override the default */
271                 if (log_all_ref_updates == -1)
272                     git_config_set("core.logallrefupdates", "true");
273                 if (work_tree != git_work_tree_cfg)
274                         git_config_set("core.worktree", work_tree);
275         }
276
277         /* Check if symlink is supported in the work tree */
278         if (!reinit) {
279                 path[len] = 0;
280                 strcpy(path + len, "tXXXXXX");
281                 if (!close(xmkstemp(path)) &&
282                     !unlink(path) &&
283                     !symlink("testing", path) &&
284                     !lstat(path, &st1) &&
285                     S_ISLNK(st1.st_mode))
286                         unlink(path); /* good */
287                 else
288                         git_config_set("core.symlinks", "false");
289         }
290
291         return reinit;
292 }
293
294 static void guess_repository_type(const char *git_dir)
295 {
296         char cwd[PATH_MAX];
297         const char *slash;
298
299         if (0 <= is_bare_repository_cfg)
300                 return;
301         if (!git_dir)
302                 return;
303
304         /*
305          * "GIT_DIR=. git init" is always bare.
306          * "GIT_DIR=`pwd` git init" too.
307          */
308         if (!strcmp(".", git_dir))
309                 goto force_bare;
310         if (!getcwd(cwd, sizeof(cwd)))
311                 die("cannot tell cwd");
312         if (!strcmp(git_dir, cwd))
313                 goto force_bare;
314         /*
315          * "GIT_DIR=.git or GIT_DIR=something/.git is usually not.
316          */
317         if (!strcmp(git_dir, ".git"))
318                 return;
319         slash = strrchr(git_dir, '/');
320         if (slash && !strcmp(slash, "/.git"))
321                 return;
322
323         /*
324          * Otherwise it is often bare.  At this point
325          * we are just guessing.
326          */
327  force_bare:
328         is_bare_repository_cfg = 1;
329         return;
330 }
331
332 static const char init_db_usage[] =
333 "git-init [-q | --quiet] [--template=<template-directory>] [--shared]";
334
335 /*
336  * If you want to, you can share the DB area with any number of branches.
337  * That has advantages: you can save space by sharing all the SHA1 objects.
338  * On the other hand, it might just make lookup slower and messier. You
339  * be the judge.  The default case is to have one DB per managed directory.
340  */
341 int cmd_init_db(int argc, const char **argv, const char *prefix)
342 {
343         const char *git_dir;
344         const char *sha1_dir;
345         const char *template_dir = NULL;
346         char *path;
347         int len, i, reinit;
348         int quiet = 0;
349
350         for (i = 1; i < argc; i++, argv++) {
351                 const char *arg = argv[1];
352                 if (!prefixcmp(arg, "--template="))
353                         template_dir = arg+11;
354                 else if (!strcmp(arg, "--shared"))
355                         shared_repository = PERM_GROUP;
356                 else if (!prefixcmp(arg, "--shared="))
357                         shared_repository = git_config_perm("arg", arg+9);
358                 else if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet"))
359                         quiet = 1;
360                 else
361                         usage(init_db_usage);
362         }
363
364         /*
365          * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
366          * without --bare.  Catch the error early.
367          */
368         git_dir = getenv(GIT_DIR_ENVIRONMENT);
369         if ((!git_dir || is_bare_repository_cfg == 1)
370             && getenv(GIT_WORK_TREE_ENVIRONMENT))
371                 die("%s (or --work-tree=<directory>) not allowed without "
372                     "specifying %s (or --git-dir=<directory>)",
373                     GIT_WORK_TREE_ENVIRONMENT,
374                     GIT_DIR_ENVIRONMENT);
375
376         guess_repository_type(git_dir);
377
378         if (is_bare_repository_cfg <= 0) {
379                 git_work_tree_cfg = xcalloc(PATH_MAX, 1);
380                 if (!getcwd(git_work_tree_cfg, PATH_MAX))
381                         die ("Cannot access current working directory.");
382                 if (access(get_git_work_tree(), X_OK))
383                         die ("Cannot access work tree '%s'",
384                              get_git_work_tree());
385         }
386
387         /*
388          * Set up the default .git directory contents
389          */
390         git_dir = getenv(GIT_DIR_ENVIRONMENT);
391         if (!git_dir)
392                 git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
393         safe_create_dir(git_dir, 0);
394
395         /* Check to see if the repository version is right.
396          * Note that a newly created repository does not have
397          * config file, so this will not fail.  What we are catching
398          * is an attempt to reinitialize new repository with an old tool.
399          */
400         check_repository_format();
401
402         reinit = create_default_files(git_dir, template_dir);
403
404         /*
405          * And set up the object store.
406          */
407         sha1_dir = get_object_directory();
408         len = strlen(sha1_dir);
409         path = xmalloc(len + 40);
410         memcpy(path, sha1_dir, len);
411
412         safe_create_dir(sha1_dir, 1);
413         strcpy(path+len, "/pack");
414         safe_create_dir(path, 1);
415         strcpy(path+len, "/info");
416         safe_create_dir(path, 1);
417
418         if (shared_repository) {
419                 char buf[10];
420                 /* We do not spell "group" and such, so that
421                  * the configuration can be read by older version
422                  * of git.
423                  */
424                 sprintf(buf, "%d", shared_repository);
425                 git_config_set("core.sharedrepository", buf);
426                 git_config_set("receive.denyNonFastforwards", "true");
427         }
428
429         if (!quiet)
430                 printf("%s%s Git repository in %s/\n",
431                        reinit ? "Reinitialized existing" : "Initialized empty",
432                        shared_repository ? " shared" : "",
433                        git_dir);
434
435         return 0;
436 }