2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
8 #ifndef DEFAULT_GIT_TEMPLATE_DIR
9 #define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates/"
12 static void safe_create_dir(const char *dir)
14 if (mkdir(dir, 0777) < 0) {
15 if (errno != EEXIST) {
22 static int copy_file(const char *dst, const char *src, int mode)
26 mode = (mode & 0111) ? 0777 : 0666;
27 if ((fdi = open(src, O_RDONLY)) < 0)
29 if ((fdo = open(dst, O_WRONLY | O_CREAT | O_EXCL, mode)) < 0) {
35 ssize_t leni, leno, ofs;
36 leni = read(fdi, buf, sizeof(buf));
47 leno = write(fdo, buf+ofs, leni);
59 static void copy_templates_1(char *path, int baselen,
60 char *template, int template_baselen,
65 /* Note: if ".git/hooks" file exists in the repository being
66 * re-initialized, /etc/core-git/templates/hooks/update would
67 * cause git-init-db to fail here. I think this is sane but
68 * it means that the set of templates we ship by default, along
69 * with the way the namespace under .git/ is organized, should
70 * be really carefully chosen.
72 safe_create_dir(path);
73 while ((de = readdir(dir)) != NULL) {
74 struct stat st_git, st_template;
78 if (de->d_name[0] == '.')
80 namelen = strlen(de->d_name);
81 if ((PATH_MAX <= baselen + namelen) ||
82 (PATH_MAX <= template_baselen + namelen))
83 die("insanely long template name %s", de->d_name);
84 memcpy(path + baselen, de->d_name, namelen+1);
85 memcpy(template + template_baselen, de->d_name, namelen+1);
86 if (lstat(path, &st_git)) {
88 die("cannot stat %s", path);
93 if (lstat(template, &st_template))
94 die("cannot stat template %s", template);
96 if (S_ISDIR(st_template.st_mode)) {
97 DIR *subdir = opendir(template);
98 int baselen_sub = baselen + namelen;
99 int template_baselen_sub = template_baselen + namelen;
101 die("cannot opendir %s", template);
102 path[baselen_sub++] =
103 template[template_baselen_sub++] = '/';
105 template[template_baselen_sub] = 0;
106 copy_templates_1(path, baselen_sub,
107 template, template_baselen_sub,
113 else if (S_ISLNK(st_template.st_mode)) {
116 len = readlink(template, lnk, sizeof(lnk));
118 die("cannot readlink %s", template);
119 if (sizeof(lnk) <= len)
120 die("insanely long symlink %s", template);
122 if (symlink(lnk, path))
123 die("cannot symlink %s %s", lnk, path);
125 else if (S_ISREG(st_template.st_mode)) {
126 if (copy_file(path, template, st_template.st_mode))
127 die("cannot copy %s to %s", template, path);
130 error("ignoring template %s", template);
134 static void copy_templates(const char *git_dir, int len, char *template_dir)
137 char template_path[PATH_MAX];
142 template_dir = DEFAULT_GIT_TEMPLATE_DIR;
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;
149 dir = opendir(template_path);
151 fprintf(stderr, "warning: templates not found %s\n",
156 memcpy(path, git_dir, len);
158 copy_templates_1(path, len,
159 template_path, template_len,
164 static void create_default_files(const char *git_dir,
167 unsigned len = strlen(git_dir);
168 static char path[PATH_MAX];
169 unsigned char sha1[20];
171 if (len > sizeof(path)-50)
172 die("insane git directory %s", git_dir);
173 memcpy(path, git_dir, len);
175 if (len && path[len-1] != '/')
179 * Create .git/refs/{heads,tags}
181 strcpy(path + len, "refs");
182 safe_create_dir(path);
183 strcpy(path + len, "refs/heads");
184 safe_create_dir(path);
185 strcpy(path + len, "refs/tags");
186 safe_create_dir(path);
189 * Create the default symlink from ".git/HEAD" to the "master"
190 * branch, if it does not exist yet.
192 strcpy(path + len, "HEAD");
193 if (read_ref(path, sha1) < 0) {
194 if (create_symref(path, "refs/heads/master") < 0)
198 copy_templates(path, len, template_path);
201 * Find out if we can trust the executable bit.
203 safe_create_dir(path);
204 strcpy(path + len, "config");
205 if (access(path, R_OK) < 0) {
206 static const char contents[] =
208 "# This is the config file\n"
213 " ; Don't trust file modes\n"
214 " filemode = false\n"
216 FILE *config = fopen(path, "w");
220 die("Can not write to %s?", path);
222 fwrite(contents, sizeof(contents)-1, 1, config);
226 if (!lstat(path, &st)) {
228 if (!chmod(path, st.st_mode ^ S_IXUSR) &&
229 !lstat(path, &st2) &&
230 st.st_mode != st2.st_mode)
233 fprintf(stderr, "Ignoring file modes\n");
238 static const char init_db_usage[] =
239 "git-init-db [--template=<template-directory>]";
242 * If you want to, you can share the DB area with any number of branches.
243 * That has advantages: you can save space by sharing all the SHA1 objects.
244 * On the other hand, it might just make lookup slower and messier. You
245 * be the judge. The default case is to have one DB per managed directory.
247 int main(int argc, char **argv)
250 const char *sha1_dir;
251 char *path, *template_dir = NULL;
254 for (i = 1; i < argc; i++, argv++) {
258 else if (!strncmp(arg, "--template=", 11))
259 template_dir = arg+11;
265 * Set up the default .git directory contents
267 git_dir = getenv(GIT_DIR_ENVIRONMENT);
269 git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
270 fprintf(stderr, "defaulting to local storage area\n");
272 safe_create_dir(git_dir);
273 create_default_files(git_dir, template_dir);
276 * And set up the object store.
278 sha1_dir = get_object_directory();
279 len = strlen(sha1_dir);
280 path = xmalloc(len + 40);
281 memcpy(path, sha1_dir, len);
283 safe_create_dir(sha1_dir);
284 strcpy(path+len, "/pack");
285 safe_create_dir(path);
286 strcpy(path+len, "/info");
287 safe_create_dir(path);