Code

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