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
) {
192 copy_templates(path
);
196 * If you want to, you can share the DB area with any number of branches.
197 * That has advantages: you can save space by sharing all the SHA1 objects.
198 * On the other hand, it might just make lookup slower and messier. You
199 * be the judge. The default case is to have one DB per managed directory.
201 int main(int argc
, char **argv
)
204 const char *sha1_dir
;
209 * Set up the default .git directory contents
211 git_dir
= gitenv(GIT_DIR_ENVIRONMENT
);
213 git_dir
= DEFAULT_GIT_DIR_ENVIRONMENT
;
214 fprintf(stderr
, "defaulting to local storage area\n");
216 safe_create_dir(git_dir
);
217 create_default_files(git_dir
);
220 * And set up the object store.
222 sha1_dir
= get_object_directory();
223 len
= strlen(sha1_dir
);
224 path
= xmalloc(len
+ 40);
225 memcpy(path
, sha1_dir
, len
);
227 safe_create_dir(sha1_dir
);
228 for (i
= 0; i
< 256; i
++) {
229 sprintf(path
+len
, "/%02x", i
);
230 safe_create_dir(path
);
232 strcpy(path
+len
, "/pack");
233 safe_create_dir(path
);