[PATCH] Make "git-ls-files" work in subdirectories
[git/dscho.git] / init-db.c
blob50b16b34b0e3a3fe4de29252eb9261055913e262
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
8 #ifndef DEFAULT_GIT_TEMPLATE_DIR
9 #define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates/"
10 #endif
12 static void safe_create_dir(const char *dir)
14 if (mkdir(dir, 0777) < 0) {
15 if (errno != EEXIST) {
16 perror(dir);
17 exit(1);
22 static int copy_file(const char *dst, const char *src, int mode)
24 int fdi, fdo;
26 mode = (mode & 0111) ? 0777 : 0666;
27 if ((fdi = open(src, O_RDONLY)) < 0)
28 return fdi;
29 if ((fdo = open(dst, O_WRONLY | O_CREAT | O_EXCL, mode)) < 0) {
30 close(fdi);
31 return fdo;
33 while (1) {
34 char buf[BUFSIZ];
35 ssize_t leni, leno, ofs;
36 leni = read(fdi, buf, sizeof(buf));
37 if (leni < 0) {
38 error_return:
39 close(fdo);
40 close(fdi);
41 return -1;
43 if (!leni)
44 break;
45 ofs = 0;
46 do {
47 leno = write(fdo, buf+ofs, leni);
48 if (leno < 0)
49 goto error_return;
50 leni -= leno;
51 ofs += leno;
52 } while (0 < leni);
54 close(fdo);
55 close(fdi);
56 return 0;
59 static void copy_templates_1(char *path, int baselen,
60 char *template, int template_baselen,
61 DIR *dir)
63 struct dirent *de;
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;
75 int namelen;
76 int exists = 0;
78 if (de->d_name[0] == '.')
79 continue;
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)) {
87 if (errno != ENOENT)
88 die("cannot stat %s", path);
90 else
91 exists = 1;
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;
100 if (!subdir)
101 die("cannot opendir %s", template);
102 path[baselen_sub++] =
103 template[template_baselen_sub++] = '/';
104 path[baselen_sub] =
105 template[template_baselen_sub] = 0;
106 copy_templates_1(path, baselen_sub,
107 template, template_baselen_sub,
108 subdir);
109 closedir(subdir);
111 else if (exists)
112 continue;
113 else if (S_ISLNK(st_template.st_mode)) {
114 char lnk[256];
115 int len;
116 len = readlink(template, lnk, sizeof(lnk));
117 if (len < 0)
118 die("cannot readlink %s", template);
119 if (sizeof(lnk) <= len)
120 die("insanely long symlink %s", template);
121 lnk[len] = 0;
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);
129 else
130 error("ignoring template %s", template);
134 static void copy_templates(const char *git_dir, int len, char *template_dir)
136 char path[PATH_MAX];
137 char template_path[PATH_MAX];
138 int template_len;
139 DIR *dir;
141 if (!template_dir)
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);
150 if (!dir) {
151 fprintf(stderr, "warning: templates not found %s\n",
152 template_dir);
153 return;
156 memcpy(path, git_dir, len);
157 copy_templates_1(path, len,
158 template_path, template_len,
159 dir);
160 closedir(dir);
163 static void create_default_files(const char *git_dir,
164 char *template_path)
166 unsigned len = strlen(git_dir);
167 static char path[PATH_MAX];
169 if (len > sizeof(path)-50)
170 die("insane git directory %s", git_dir);
171 memcpy(path, git_dir, len);
173 if (len && path[len-1] != '/')
174 path[len++] = '/';
177 * Create .git/refs/{heads,tags}
179 strcpy(path + len, "refs");
180 safe_create_dir(path);
181 strcpy(path + len, "refs/heads");
182 safe_create_dir(path);
183 strcpy(path + len, "refs/tags");
184 safe_create_dir(path);
187 * Create the default symlink from ".git/HEAD" to the "master"
188 * branch
190 strcpy(path + len, "HEAD");
191 if (symlink("refs/heads/master", path) < 0) {
192 if (errno != EEXIST) {
193 perror(path);
194 exit(1);
197 copy_templates(path, len, template_path);
200 static const char init_db_usage[] =
201 "git-init-db [--template=<template-directory>]";
204 * If you want to, you can share the DB area with any number of branches.
205 * That has advantages: you can save space by sharing all the SHA1 objects.
206 * On the other hand, it might just make lookup slower and messier. You
207 * be the judge. The default case is to have one DB per managed directory.
209 int main(int argc, char **argv)
211 const char *git_dir;
212 const char *sha1_dir;
213 char *path, *template_dir = NULL;
214 int len, i;
216 for (i = 1; i < argc; i++, argv++) {
217 char *arg = argv[1];
218 if (arg[0] != '-')
219 break;
220 else if (!strncmp(arg, "--template=", 11))
221 template_dir = arg+11;
222 else
223 die(init_db_usage);
227 * Set up the default .git directory contents
229 git_dir = gitenv(GIT_DIR_ENVIRONMENT);
230 if (!git_dir) {
231 git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
232 fprintf(stderr, "defaulting to local storage area\n");
234 safe_create_dir(git_dir);
235 create_default_files(git_dir, template_dir);
238 * And set up the object store.
240 sha1_dir = get_object_directory();
241 len = strlen(sha1_dir);
242 path = xmalloc(len + 40);
243 memcpy(path, sha1_dir, len);
245 safe_create_dir(sha1_dir);
246 for (i = 0; i < 256; i++) {
247 sprintf(path+len, "/%02x", i);
248 safe_create_dir(path);
250 strcpy(path+len, "/pack");
251 safe_create_dir(path);
252 strcpy(path+len, "/info");
253 safe_create_dir(path);
254 return 0;