Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 345 lines (304 sloc) 9.022 kB
8bc9a0c Add copyright notices.
Linus Torvalds authored
1 /*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
e83c516 Initial revision of "git", the information manager from hell
Linus Torvalds authored
6 #include "cache.h"
c3c8835 @tihirvon Builtin git-init-db
tihirvon authored
7 #include "builtin.h"
e83c516 Initial revision of "git", the information manager from hell
Linus Torvalds authored
8
d3af621 Redo the templates generation and installation.
Junio C Hamano authored
9 #ifndef DEFAULT_GIT_TEMPLATE_DIR
10 #define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates/"
11 #endif
12
c869753 @spearce Force core.filemode to false on Cygwin.
spearce authored
13 #ifdef NO_TRUSTABLE_FILEMODE
14 #define TEST_FILEMODE 0
15 #else
16 #define TEST_FILEMODE 1
17 #endif
18
af6e277 @dscho git-init-db: initialize shared repositories with --shared
dscho authored
19 static void safe_create_dir(const char *dir, int share)
cb126d8 [PATCH] init-db.c: create and use safe_create_dir helper
Zach Welch authored
20 {
f312de0 [PATCH] Let umask do its work upon filesystem object creation.
Junio C Hamano authored
21 if (mkdir(dir, 0777) < 0) {
cb126d8 [PATCH] init-db.c: create and use safe_create_dir helper
Zach Welch authored
22 if (errno != EEXIST) {
23 perror(dir);
24 exit(1);
25 }
26 }
af6e277 @dscho git-init-db: initialize shared repositories with --shared
dscho authored
27 else if (share && adjust_shared_perm(dir))
28 die("Could not make %s writable by group\n", dir);
cb126d8 [PATCH] init-db.c: create and use safe_create_dir helper
Zach Welch authored
29 }
30
8d5afef Install sample hooks
Junio C Hamano authored
31 static int copy_file(const char *dst, const char *src, int mode)
32 {
32276c8 init-db::copy_file() - use copy_fd()
Junio C Hamano authored
33 int fdi, fdo, status;
8d5afef Install sample hooks
Junio C Hamano authored
34
35 mode = (mode & 0111) ? 0777 : 0666;
36 if ((fdi = open(src, O_RDONLY)) < 0)
37 return fdi;
38 if ((fdo = open(dst, O_WRONLY | O_CREAT | O_EXCL, mode)) < 0) {
39 close(fdi);
40 return fdo;
41 }
32276c8 init-db::copy_file() - use copy_fd()
Junio C Hamano authored
42 status = copy_fd(fdi, fdo);
8d5afef Install sample hooks
Junio C Hamano authored
43 close(fdo);
af6e277 @dscho git-init-db: initialize shared repositories with --shared
dscho authored
44
45 if (!status && adjust_shared_perm(dst))
46 return -1;
47
32276c8 init-db::copy_file() - use copy_fd()
Junio C Hamano authored
48 return status;
8d5afef Install sample hooks
Junio C Hamano authored
49 }
50
51 static void copy_templates_1(char *path, int baselen,
52 char *template, int template_baselen,
53 DIR *dir)
54 {
55 struct dirent *de;
56
57 /* Note: if ".git/hooks" file exists in the repository being
58 * re-initialized, /etc/core-git/templates/hooks/update would
5c94f87 use 'init' instead of 'init-db' for shipped docs and tools
Nicolas Pitre authored
59 * cause git-init to fail here. I think this is sane but
8d5afef Install sample hooks
Junio C Hamano authored
60 * it means that the set of templates we ship by default, along
61 * with the way the namespace under .git/ is organized, should
62 * be really carefully chosen.
63 */
af6e277 @dscho git-init-db: initialize shared repositories with --shared
dscho authored
64 safe_create_dir(path, 1);
8d5afef Install sample hooks
Junio C Hamano authored
65 while ((de = readdir(dir)) != NULL) {
66 struct stat st_git, st_template;
67 int namelen;
68 int exists = 0;
69
70 if (de->d_name[0] == '.')
71 continue;
72 namelen = strlen(de->d_name);
73 if ((PATH_MAX <= baselen + namelen) ||
74 (PATH_MAX <= template_baselen + namelen))
75 die("insanely long template name %s", de->d_name);
76 memcpy(path + baselen, de->d_name, namelen+1);
77 memcpy(template + template_baselen, de->d_name, namelen+1);
78 if (lstat(path, &st_git)) {
79 if (errno != ENOENT)
80 die("cannot stat %s", path);
81 }
82 else
83 exists = 1;
84
85 if (lstat(template, &st_template))
86 die("cannot stat template %s", template);
87
88 if (S_ISDIR(st_template.st_mode)) {
89 DIR *subdir = opendir(template);
90 int baselen_sub = baselen + namelen;
91 int template_baselen_sub = template_baselen + namelen;
92 if (!subdir)
93 die("cannot opendir %s", template);
94 path[baselen_sub++] =
95 template[template_baselen_sub++] = '/';
96 path[baselen_sub] =
97 template[template_baselen_sub] = 0;
98 copy_templates_1(path, baselen_sub,
99 template, template_baselen_sub,
100 subdir);
101 closedir(subdir);
102 }
103 else if (exists)
104 continue;
105 else if (S_ISLNK(st_template.st_mode)) {
106 char lnk[256];
107 int len;
108 len = readlink(template, lnk, sizeof(lnk));
109 if (len < 0)
110 die("cannot readlink %s", template);
111 if (sizeof(lnk) <= len)
112 die("insanely long symlink %s", template);
113 lnk[len] = 0;
114 if (symlink(lnk, path))
115 die("cannot symlink %s %s", lnk, path);
116 }
117 else if (S_ISREG(st_template.st_mode)) {
118 if (copy_file(path, template, st_template.st_mode))
119 die("cannot copy %s to %s", template, path);
120 }
121 else
122 error("ignoring template %s", template);
123 }
124 }
125
c3c8835 @tihirvon Builtin git-init-db
tihirvon authored
126 static void copy_templates(const char *git_dir, int len, const char *template_dir)
8d5afef Install sample hooks
Junio C Hamano authored
127 {
128 char path[PATH_MAX];
129 char template_path[PATH_MAX];
d3af621 Redo the templates generation and installation.
Junio C Hamano authored
130 int template_len;
8d5afef Install sample hooks
Junio C Hamano authored
131 DIR *dir;
132
8683a45 @dscho Introduce GIT_TEMPLATE_DIR
dscho authored
133 if (!template_dir) {
d4ebc36 Use preprocessor constants for environment variable names.
Junio C Hamano authored
134 template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT);
8683a45 @dscho Introduce GIT_TEMPLATE_DIR
dscho authored
135 if (!template_dir)
136 template_dir = DEFAULT_GIT_TEMPLATE_DIR;
137 }
8d5afef Install sample hooks
Junio C Hamano authored
138 strcpy(template_path, template_dir);
139 template_len = strlen(template_path);
140 if (template_path[template_len-1] != '/') {
141 template_path[template_len++] = '/';
142 template_path[template_len] = 0;
143 }
144 dir = opendir(template_path);
d3af621 Redo the templates generation and installation.
Junio C Hamano authored
145 if (!dir) {
146 fprintf(stderr, "warning: templates not found %s\n",
147 template_dir);
8d5afef Install sample hooks
Junio C Hamano authored
148 return;
d3af621 Redo the templates generation and installation.
Junio C Hamano authored
149 }
150
4f62953 init-db: check template and repository format.
Junio C Hamano authored
151 /* Make sure that template is from the correct vintage */
152 strcpy(template_path + template_len, "config");
153 repository_format_version = 0;
154 git_config_from_file(check_repository_format_version,
155 template_path);
156 template_path[template_len] = 0;
157
158 if (repository_format_version &&
159 repository_format_version != GIT_REPO_VERSION) {
160 fprintf(stderr, "warning: not copying templates of "
161 "a wrong format version %d from '%s'\n",
162 repository_format_version,
163 template_dir);
164 closedir(dir);
165 return;
166 }
167
d3af621 Redo the templates generation and installation.
Junio C Hamano authored
168 memcpy(path, git_dir, len);
1f961c1 [PATCH] Fix git-init-db creating crap directories.
Petr Baudis authored
169 path[len] = 0;
8d5afef Install sample hooks
Junio C Hamano authored
170 copy_templates_1(path, len,
171 template_path, template_len,
172 dir);
173 closedir(dir);
174 }
175
ef0a89a @spearce Provide more meaningful output from 'git init-db'.
spearce authored
176 static int create_default_files(const char *git_dir, const char *template_path)
cad88fd git-init-db: set up the full default environment
Linus Torvalds authored
177 {
178 unsigned len = strlen(git_dir);
179 static char path[PATH_MAX];
8098a17 Add git-symbolic-ref
Junio C Hamano authored
180 unsigned char sha1[20];
4f62953 init-db: check template and repository format.
Junio C Hamano authored
181 struct stat st1;
182 char repo_version_string[10];
ef0a89a @spearce Provide more meaningful output from 'git init-db'.
spearce authored
183 int reinit;
c869753 @spearce Force core.filemode to false on Cygwin.
spearce authored
184 int filemode;
cad88fd git-init-db: set up the full default environment
Linus Torvalds authored
185
186 if (len > sizeof(path)-50)
187 die("insane git directory %s", git_dir);
188 memcpy(path, git_dir, len);
189
190 if (len && path[len-1] != '/')
191 path[len++] = '/';
192
193 /*
194 * Create .git/refs/{heads,tags}
195 */
196 strcpy(path + len, "refs");
af6e277 @dscho git-init-db: initialize shared repositories with --shared
dscho authored
197 safe_create_dir(path, 1);
cad88fd git-init-db: set up the full default environment
Linus Torvalds authored
198 strcpy(path + len, "refs/heads");
af6e277 @dscho git-init-db: initialize shared repositories with --shared
dscho authored
199 safe_create_dir(path, 1);
cad88fd git-init-db: set up the full default environment
Linus Torvalds authored
200 strcpy(path + len, "refs/tags");
af6e277 @dscho git-init-db: initialize shared repositories with --shared
dscho authored
201 safe_create_dir(path, 1);
cad88fd git-init-db: set up the full default environment
Linus Torvalds authored
202
4f62953 init-db: check template and repository format.
Junio C Hamano authored
203 /* First copy the templates -- we might have the default
204 * config file there, in which case we would want to read
205 * from it after installing.
206 */
207 path[len] = 0;
208 copy_templates(path, len, template_path);
209
210 git_config(git_default_config);
211
cad88fd git-init-db: set up the full default environment
Linus Torvalds authored
212 /*
138086a shared repository - add a few missing calls to adjust_shared_perm().
Junio C Hamano authored
213 * We would have created the above under user's umask -- under
214 * shared-repository settings, we would need to fix them up.
215 */
216 if (shared_repository) {
217 path[len] = 0;
218 adjust_shared_perm(path);
219 strcpy(path + len, "refs");
220 adjust_shared_perm(path);
221 strcpy(path + len, "refs/heads");
222 adjust_shared_perm(path);
223 strcpy(path + len, "refs/tags");
224 adjust_shared_perm(path);
225 }
226
227 /*
cad88fd git-init-db: set up the full default environment
Linus Torvalds authored
228 * Create the default symlink from ".git/HEAD" to the "master"
8098a17 Add git-symbolic-ref
Junio C Hamano authored
229 * branch, if it does not exist yet.
cad88fd git-init-db: set up the full default environment
Linus Torvalds authored
230 */
231 strcpy(path + len, "HEAD");
ef0a89a @spearce Provide more meaningful output from 'git init-db'.
spearce authored
232 reinit = !read_ref("HEAD", sha1);
233 if (!reinit) {
8b5157e add logref support to git-symbolic-ref
Nicolas Pitre authored
234 if (create_symref("HEAD", "refs/heads/master", NULL) < 0)
cad88fd git-init-db: set up the full default environment
Linus Torvalds authored
235 exit(1);
236 }
e24317b @dscho Test in git-init-db if the filemode can be trusted
dscho authored
237
4f62953 init-db: check template and repository format.
Junio C Hamano authored
238 /* This forces creation of new config file */
239 sprintf(repo_version_string, "%d", GIT_REPO_VERSION);
240 git_config_set("core.repositoryformatversion", repo_version_string);
241
242 path[len] = 0;
e24317b @dscho Test in git-init-db if the filemode can be trusted
dscho authored
243 strcpy(path + len, "config");
4f62953 init-db: check template and repository format.
Junio C Hamano authored
244
245 /* Check filemode trustability */
c869753 @spearce Force core.filemode to false on Cygwin.
spearce authored
246 filemode = TEST_FILEMODE;
247 if (TEST_FILEMODE && !lstat(path, &st1)) {
4f62953 init-db: check template and repository format.
Junio C Hamano authored
248 struct stat st2;
c869753 @spearce Force core.filemode to false on Cygwin.
spearce authored
249 filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) &&
4f62953 init-db: check template and repository format.
Junio C Hamano authored
250 !lstat(path, &st2) &&
251 st1.st_mode != st2.st_mode);
e24317b @dscho Test in git-init-db if the filemode can be trusted
dscho authored
252 }
c869753 @spearce Force core.filemode to false on Cygwin.
spearce authored
253 git_config_set("core.filemode", filemode ? "true" : "false");
ef0a89a @spearce Provide more meaningful output from 'git init-db'.
spearce authored
254
7d1864c Introduce is_bare_repository() and core.bare configuration variable
Junio C Hamano authored
255 if (is_bare_repository()) {
256 git_config_set("core.bare", "true");
257 }
258 else {
259 git_config_set("core.bare", "false");
196055c @raalkml Allow default core.logallrefupdates to be overridden with template's …
raalkml authored
260 /* allow template config file to override the default */
261 if (log_all_ref_updates == -1)
262 git_config_set("core.logallrefupdates", "true");
7d1864c Introduce is_bare_repository() and core.bare configuration variable
Junio C Hamano authored
263 }
ef0a89a @spearce Provide more meaningful output from 'git init-db'.
spearce authored
264 return reinit;
cad88fd git-init-db: set up the full default environment
Linus Torvalds authored
265 }
266
d3af621 Redo the templates generation and installation.
Junio C Hamano authored
267 static const char init_db_usage[] =
515377e "init-db" can really be just "init"
Nicolas Pitre authored
268 "git-init [--template=<template-directory>] [--shared]";
d3af621 Redo the templates generation and installation.
Junio C Hamano authored
269
4696cb9 [PATCH] init-db.c: cleanup comments
Zach Welch authored
270 /*
271 * If you want to, you can share the DB area with any number of branches.
272 * That has advantages: you can save space by sharing all the SHA1 objects.
273 * On the other hand, it might just make lookup slower and messier. You
274 * be the judge. The default case is to have one DB per managed directory.
275 */
a633fca Call setup_git_directory() much earlier
Linus Torvalds authored
276 int cmd_init_db(int argc, const char **argv, const char *prefix)
e83c516 Initial revision of "git", the information manager from hell
Linus Torvalds authored
277 {
cad88fd git-init-db: set up the full default environment
Linus Torvalds authored
278 const char *git_dir;
d19938a Rename environment variables.
Junio C Hamano authored
279 const char *sha1_dir;
c3c8835 @tihirvon Builtin git-init-db
tihirvon authored
280 const char *template_dir = NULL;
281 char *path;
ef0a89a @spearce Provide more meaningful output from 'git init-db'.
spearce authored
282 int len, i, reinit;
e83c516 Initial revision of "git", the information manager from hell
Linus Torvalds authored
283
d3af621 Redo the templates generation and installation.
Junio C Hamano authored
284 for (i = 1; i < argc; i++, argv++) {
c3c8835 @tihirvon Builtin git-init-db
tihirvon authored
285 const char *arg = argv[1];
cc44c76 Mechanical conversion to use prefixcmp()
Junio C Hamano authored
286 if (!prefixcmp(arg, "--template="))
d3af621 Redo the templates generation and installation.
Junio C Hamano authored
287 template_dir = arg+11;
af6e277 @dscho git-init-db: initialize shared repositories with --shared
dscho authored
288 else if (!strcmp(arg, "--shared"))
94df250 shared repository: optionally allow reading to "others".
Junio C Hamano authored
289 shared_repository = PERM_GROUP;
cc44c76 Mechanical conversion to use prefixcmp()
Junio C Hamano authored
290 else if (!prefixcmp(arg, "--shared="))
94df250 shared repository: optionally allow reading to "others".
Junio C Hamano authored
291 shared_repository = git_config_perm("arg", arg+9);
d3af621 Redo the templates generation and installation.
Junio C Hamano authored
292 else
8cdf336 Replace some calls to die(usage_str) with usage(usage_str).
Ramsay Allan Jones authored
293 usage(init_db_usage);
d3af621 Redo the templates generation and installation.
Junio C Hamano authored
294 }
295
cad88fd git-init-db: set up the full default environment
Linus Torvalds authored
296 /*
297 * Set up the default .git directory contents
298 */
a9ab586 Retire support for old environment variables.
Junio C Hamano authored
299 git_dir = getenv(GIT_DIR_ENVIRONMENT);
ef0a89a @spearce Provide more meaningful output from 'git init-db'.
spearce authored
300 if (!git_dir)
cad88fd git-init-db: set up the full default environment
Linus Torvalds authored
301 git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
af6e277 @dscho git-init-db: initialize shared repositories with --shared
dscho authored
302 safe_create_dir(git_dir, 0);
4f62953 init-db: check template and repository format.
Junio C Hamano authored
303
304 /* Check to see if the repository version is right.
305 * Note that a newly created repository does not have
306 * config file, so this will not fail. What we are catching
307 * is an attempt to reinitialize new repository with an old tool.
308 */
309 check_repository_format();
310
ef0a89a @spearce Provide more meaningful output from 'git init-db'.
spearce authored
311 reinit = create_default_files(git_dir, template_dir);
cad88fd git-init-db: set up the full default environment
Linus Torvalds authored
312
313 /*
314 * And set up the object store.
315 */
316 sha1_dir = get_object_directory();
e83c516 Initial revision of "git", the information manager from hell
Linus Torvalds authored
317 len = strlen(sha1_dir);
812666c [PATCH] introduce xmalloc and xrealloc
Christopher Li authored
318 path = xmalloc(len + 40);
e83c516 Initial revision of "git", the information manager from hell
Linus Torvalds authored
319 memcpy(path, sha1_dir, len);
cb126d8 [PATCH] init-db.c: create and use safe_create_dir helper
Zach Welch authored
320
af6e277 @dscho git-init-db: initialize shared repositories with --shared
dscho authored
321 safe_create_dir(sha1_dir, 1);
f49fb35 git-init-db: create "pack" subdirectory under objects
Linus Torvalds authored
322 strcpy(path+len, "/pack");
af6e277 @dscho git-init-db: initialize shared repositories with --shared
dscho authored
323 safe_create_dir(path, 1);
d57306c Create objects/info/ directory in init-db.
Junio C Hamano authored
324 strcpy(path+len, "/info");
af6e277 @dscho git-init-db: initialize shared repositories with --shared
dscho authored
325 safe_create_dir(path, 1);
326
94df250 shared repository: optionally allow reading to "others".
Junio C Hamano authored
327 if (shared_repository) {
328 char buf[10];
329 /* We do not spell "group" and such, so that
330 * the configuration can be read by older version
331 * of git.
332 */
333 sprintf(buf, "%d", shared_repository);
334 git_config_set("core.sharedrepository", buf);
11031d7 @dscho add receive.denyNonFastforwards config variable
dscho authored
335 git_config_set("receive.denyNonFastforwards", "true");
94df250 shared repository: optionally allow reading to "others".
Junio C Hamano authored
336 }
af6e277 @dscho git-init-db: initialize shared repositories with --shared
dscho authored
337
ef0a89a @spearce Provide more meaningful output from 'git init-db'.
spearce authored
338 printf("%s%s Git repository in %s/\n",
339 reinit ? "Reinitialized existing" : "Initialized empty",
340 shared_repository ? " shared" : "",
341 git_dir);
342
e83c516 Initial revision of "git", the information manager from hell
Linus Torvalds authored
343 return 0;
344 }
Something went wrong with that request. Please try again.