2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
8 static void safe_create_dir(const char *dir)
10 if (mkdir(dir, 0777) < 0) {
11 if (errno != EEXIST) {
18 static int copy_file(const char *dst, const char *src, int mode)
22 mode = (mode & 0111) ? 0777 : 0666;
23 if ((fdi = open(src, O_RDONLY)) < 0)
25 if ((fdo = open(dst, O_WRONLY | O_CREAT | O_EXCL, mode)) < 0) {
31 ssize_t leni, leno, ofs;
32 leni = read(fdi, buf, sizeof(buf));
43 leno = write(fdo, buf+ofs, leni);
55 static void copy_templates_1(char *path, int baselen,
56 char *template, int template_baselen,
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.
68 safe_create_dir(path);
69 while ((de = readdir(dir)) != NULL) {
70 struct stat st_git, st_template;
74 if (de->d_name[0] == '.')
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)) {
84 die("cannot stat %s", path);
89 if (lstat(template, &st_template))
90 die("cannot stat template %s", template);
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;
97 die("cannot opendir %s", template);
99 template[template_baselen_sub++] = '/';
101 template[template_baselen_sub] = 0;
102 copy_templates_1(path, baselen_sub,
103 template, template_baselen_sub,
109 else if (S_ISLNK(st_template.st_mode)) {
112 len = readlink(template, lnk, sizeof(lnk));
114 die("cannot readlink %s", template);
115 if (sizeof(lnk) <= len)
116 die("insanely long symlink %s", template);
118 if (symlink(lnk, path))
119 die("cannot symlink %s %s", lnk, path);
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);
126 error("ignoring template %s", template);
130 static void copy_templates(const char *git_dir)
133 char template_path[PATH_MAX];
135 int len, template_len;
138 strcpy(path, git_dir);
140 template_dir = gitenv(TEMPLATE_DIR_ENVIRONMENT);
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;
150 dir = opendir(template_path);
153 copy_templates_1(path, len,
154 template_path, template_len,
159 static void create_default_files(const char *git_dir)
161 unsigned len = strlen(git_dir);
162 static char path[PATH_MAX];
164 if (len > sizeof(path)-50)
165 die("insane git directory %s", git_dir);
166 memcpy(path, git_dir, len);
168 if (len && path[len-1] != '/')
172 * Create .git/refs/{heads,tags}
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);
182 * Create the default symlink from ".git/HEAD" to the "master"
185 strcpy(path + len, "HEAD");
186 if (symlink("refs/heads/master", path) < 0) {
187 if (errno != EEXIST) {
193 copy_templates(path);
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.
202 int main(int argc, char **argv)
205 const char *sha1_dir;
210 * Set up the default .git directory contents
212 git_dir = gitenv(GIT_DIR_ENVIRONMENT);
214 git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
215 fprintf(stderr, "defaulting to local storage area\n");
217 safe_create_dir(git_dir);
218 create_default_files(git_dir);
221 * And set up the object store.
223 sha1_dir = get_object_directory();
224 len = strlen(sha1_dir);
225 path = xmalloc(len + 40);
226 memcpy(path, sha1_dir, len);
228 safe_create_dir(sha1_dir);
229 for (i = 0; i < 256; i++) {
230 sprintf(path+len, "/%02x", i);
231 safe_create_dir(path);
233 strcpy(path+len, "/pack");
234 safe_create_dir(path);