[PATCH] Fix git-merge-cache -q
[git.git] / init-db.c
1 /*
2  * GIT - The information manager from hell
3  *
4  * Copyright (C) Linus Torvalds, 2005
5  */
6 #include "cache.h"
7
8 static void safe_create_dir(const char *dir)
9 {
10         if (mkdir(dir, 0777) < 0) {
11                 if (errno != EEXIST) {
12                         perror(dir);
13                         exit(1);
14                 }
15         }
16 }
17
18 static int copy_file(const char *dst, const char *src, int mode)
19 {
20         int fdi, fdo;
21
22         mode = (mode & 0111) ? 0777 : 0666;
23         if ((fdi = open(src, O_RDONLY)) < 0)
24                 return fdi;
25         if ((fdo = open(dst, O_WRONLY | O_CREAT | O_EXCL, mode)) < 0) {
26                 close(fdi);
27                 return fdo;
28         }
29         while (1) {
30                 char buf[BUFSIZ];
31                 ssize_t leni, leno, ofs;
32                 leni = read(fdi, buf, sizeof(buf));
33                 if (leni < 0) {
34                 error_return:
35                         close(fdo);
36                         close(fdi);
37                         return -1;
38                 }
39                 if (!leni)
40                         break;
41                 ofs = 0;
42                 do {
43                         leno = write(fdo, buf+ofs, leni);
44                         if (leno < 0)
45                                 goto error_return;
46                         leni -= leno;
47                         ofs += leno;
48                 } while (0 < leni);
49         }
50         close(fdo);
51         close(fdi);
52         return 0;
53 }
54
55 static void copy_templates_1(char *path, int baselen,
56                              char *template, int template_baselen,
57                              DIR *dir)
58 {
59         struct dirent *de;
60
61         /* Note: if ".git/hooks" file exists in the repository being
62          * re-initialized, /etc/core-git/templates/hooks/update would
63          * cause git-init-db to fail here.  I think this is sane but
64          * it means that the set of templates we ship by default, along
65          * with the way the namespace under .git/ is organized, should
66          * be really carefully chosen.
67          */
68         safe_create_dir(path);
69         while ((de = readdir(dir)) != NULL) {
70                 struct stat st_git, st_template;
71                 int namelen;
72                 int exists = 0;
73
74                 if (de->d_name[0] == '.')
75                         continue;
76                 namelen = strlen(de->d_name);
77                 if ((PATH_MAX <= baselen + namelen) ||
78                     (PATH_MAX <= template_baselen + namelen))
79                         die("insanely long template name %s", de->d_name);
80                 memcpy(path + baselen, de->d_name, namelen+1);
81                 memcpy(template + template_baselen, de->d_name, namelen+1);
82                 if (lstat(path, &st_git)) {
83                         if (errno != ENOENT)
84                                 die("cannot stat %s", path);
85                 }
86                 else
87                         exists = 1;
88
89                 if (lstat(template, &st_template))
90                         die("cannot stat template %s", template);
91
92                 if (S_ISDIR(st_template.st_mode)) {
93                         DIR *subdir = opendir(template);
94                         int baselen_sub = baselen + namelen;
95                         int template_baselen_sub = template_baselen + namelen;
96                         if (!subdir)
97                                 die("cannot opendir %s", template);
98                         path[baselen_sub++] =
99                                 template[template_baselen_sub++] = '/';
100                         path[baselen_sub] =
101                                 template[template_baselen_sub] = 0;
102                         copy_templates_1(path, baselen_sub,
103                                          template, template_baselen_sub,
104                                          subdir);
105                         closedir(subdir);
106                 }
107                 else if (exists)
108                         continue;
109                 else if (S_ISLNK(st_template.st_mode)) {
110                         char lnk[256];
111                         int len;
112                         len = readlink(template, lnk, sizeof(lnk));
113                         if (len < 0)
114                                 die("cannot readlink %s", template);
115                         if (sizeof(lnk) <= len)
116                                 die("insanely long symlink %s", template);
117                         lnk[len] = 0;
118                         if (symlink(lnk, path))
119                                 die("cannot symlink %s %s", lnk, path);
120                 }
121                 else if (S_ISREG(st_template.st_mode)) {
122                         if (copy_file(path, template, st_template.st_mode))
123                                 die("cannot copy %s to %s", template, path);
124                 }
125                 else
126                         error("ignoring template %s", template);
127         }
128 }
129
130 static void copy_templates(const char *git_dir)
131 {
132         char path[PATH_MAX];
133         char template_path[PATH_MAX];
134         char *template_dir;
135         int len, template_len;
136         DIR *dir;
137
138         strcpy(path, git_dir);
139         len = strlen(path);
140         template_dir = gitenv(TEMPLATE_DIR_ENVIRONMENT);
141         if (!template_dir)
142                 template_dir = DEFAULT_GIT_TEMPLATE_ENVIRONMENT;
143         strcpy(template_path, template_dir);
144         template_len = strlen(template_path);
145         if (template_path[template_len-1] != '/') {
146                 template_path[template_len++] = '/';
147                 template_path[template_len] = 0;
148         }
149
150         dir = opendir(template_path);
151         if (!dir)
152                 return;
153         copy_templates_1(path, len,
154                          template_path, template_len,
155                          dir);
156         closedir(dir);
157 }
158
159 static void create_default_files(const char *git_dir)
160 {
161         unsigned len = strlen(git_dir);
162         static char path[PATH_MAX];
163
164         if (len > sizeof(path)-50)
165                 die("insane git directory %s", git_dir);
166         memcpy(path, git_dir, len);
167
168         if (len && path[len-1] != '/')
169                 path[len++] = '/';
170
171         /*
172          * Create .git/refs/{heads,tags}
173          */
174         strcpy(path + len, "refs");
175         safe_create_dir(path);
176         strcpy(path + len, "refs/heads");
177         safe_create_dir(path);
178         strcpy(path + len, "refs/tags");
179         safe_create_dir(path);
180
181         /*
182          * Create the default symlink from ".git/HEAD" to the "master"
183          * branch
184          */
185         strcpy(path + len, "HEAD");
186         if (symlink("refs/heads/master", path) < 0) {
187                 if (errno != EEXIST) {
188                         perror(path);
189                         exit(1);
190                 }
191         }
192         path[len] = 0;
193         copy_templates(path);
194 }
195
196 /*
197  * If you want to, you can share the DB area with any number of branches.
198  * That has advantages: you can save space by sharing all the SHA1 objects.
199  * On the other hand, it might just make lookup slower and messier. You
200  * be the judge.  The default case is to have one DB per managed directory.
201  */
202 int main(int argc, char **argv)
203 {
204         const char *git_dir;
205         const char *sha1_dir;
206         char *path;
207         int len, i;
208
209         /*
210          * Set up the default .git directory contents
211          */
212         git_dir = gitenv(GIT_DIR_ENVIRONMENT);
213         if (!git_dir) {
214                 git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
215                 fprintf(stderr, "defaulting to local storage area\n");
216         }
217         safe_create_dir(git_dir);
218         create_default_files(git_dir);
219
220         /*
221          * And set up the object store.
222          */
223         sha1_dir = get_object_directory();
224         len = strlen(sha1_dir);
225         path = xmalloc(len + 40);
226         memcpy(path, sha1_dir, len);
227
228         safe_create_dir(sha1_dir);
229         for (i = 0; i < 256; i++) {
230                 sprintf(path+len, "/%02x", i);
231                 safe_create_dir(path);
232         }
233         strcpy(path+len, "/pack");
234         safe_create_dir(path);
235         return 0;
236 }