🌐 AI搜索 & 代理 主页
blob: ff294960f29c8763e34e28060dee04b799bd55fd [file] [log] [blame]
Linus Torvalds8bc9a0c2005-04-07 22:16:101/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
Linus Torvaldse83c5162005-04-07 22:13:136#include "cache.h"
7
Junio C Hamanod3af6212005-08-06 19:50:148#ifndef DEFAULT_GIT_TEMPLATE_DIR
9#define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates/"
10#endif
11
Johannes Schindelinaf6e2772005-12-22 22:19:3712static void safe_create_dir(const char *dir, int share)
Zach Welchcb126d82005-04-20 04:48:1513{
Junio C Hamanof312de02005-07-06 08:21:4614 if (mkdir(dir, 0777) < 0) {
Zach Welchcb126d82005-04-20 04:48:1515 if (errno != EEXIST) {
16 perror(dir);
17 exit(1);
18 }
19 }
Johannes Schindelinaf6e2772005-12-22 22:19:3720 else if (share && adjust_shared_perm(dir))
21 die("Could not make %s writable by group\n", dir);
Zach Welchcb126d82005-04-20 04:48:1522}
23
Junio C Hamano8d5afef2005-08-02 23:45:2124static int copy_file(const char *dst, const char *src, int mode)
25{
Junio C Hamano32276c82005-11-05 19:07:2226 int fdi, fdo, status;
Junio C Hamano8d5afef2005-08-02 23:45:2127
28 mode = (mode & 0111) ? 0777 : 0666;
29 if ((fdi = open(src, O_RDONLY)) < 0)
30 return fdi;
31 if ((fdo = open(dst, O_WRONLY | O_CREAT | O_EXCL, mode)) < 0) {
32 close(fdi);
33 return fdo;
34 }
Junio C Hamano32276c82005-11-05 19:07:2235 status = copy_fd(fdi, fdo);
Junio C Hamano8d5afef2005-08-02 23:45:2136 close(fdo);
Johannes Schindelinaf6e2772005-12-22 22:19:3737
38 if (!status && adjust_shared_perm(dst))
39 return -1;
40
Junio C Hamano32276c82005-11-05 19:07:2241 return status;
Junio C Hamano8d5afef2005-08-02 23:45:2142}
43
44static void copy_templates_1(char *path, int baselen,
45 char *template, int template_baselen,
46 DIR *dir)
47{
48 struct dirent *de;
49
50 /* Note: if ".git/hooks" file exists in the repository being
51 * re-initialized, /etc/core-git/templates/hooks/update would
52 * cause git-init-db to fail here. I think this is sane but
53 * it means that the set of templates we ship by default, along
54 * with the way the namespace under .git/ is organized, should
55 * be really carefully chosen.
56 */
Johannes Schindelinaf6e2772005-12-22 22:19:3757 safe_create_dir(path, 1);
Junio C Hamano8d5afef2005-08-02 23:45:2158 while ((de = readdir(dir)) != NULL) {
59 struct stat st_git, st_template;
60 int namelen;
61 int exists = 0;
62
63 if (de->d_name[0] == '.')
64 continue;
65 namelen = strlen(de->d_name);
66 if ((PATH_MAX <= baselen + namelen) ||
67 (PATH_MAX <= template_baselen + namelen))
68 die("insanely long template name %s", de->d_name);
69 memcpy(path + baselen, de->d_name, namelen+1);
70 memcpy(template + template_baselen, de->d_name, namelen+1);
71 if (lstat(path, &st_git)) {
72 if (errno != ENOENT)
73 die("cannot stat %s", path);
74 }
75 else
76 exists = 1;
77
78 if (lstat(template, &st_template))
79 die("cannot stat template %s", template);
80
81 if (S_ISDIR(st_template.st_mode)) {
82 DIR *subdir = opendir(template);
83 int baselen_sub = baselen + namelen;
84 int template_baselen_sub = template_baselen + namelen;
85 if (!subdir)
86 die("cannot opendir %s", template);
87 path[baselen_sub++] =
88 template[template_baselen_sub++] = '/';
89 path[baselen_sub] =
90 template[template_baselen_sub] = 0;
91 copy_templates_1(path, baselen_sub,
92 template, template_baselen_sub,
93 subdir);
94 closedir(subdir);
95 }
96 else if (exists)
97 continue;
98 else if (S_ISLNK(st_template.st_mode)) {
99 char lnk[256];
100 int len;
101 len = readlink(template, lnk, sizeof(lnk));
102 if (len < 0)
103 die("cannot readlink %s", template);
104 if (sizeof(lnk) <= len)
105 die("insanely long symlink %s", template);
106 lnk[len] = 0;
107 if (symlink(lnk, path))
108 die("cannot symlink %s %s", lnk, path);
109 }
110 else if (S_ISREG(st_template.st_mode)) {
111 if (copy_file(path, template, st_template.st_mode))
112 die("cannot copy %s to %s", template, path);
113 }
114 else
115 error("ignoring template %s", template);
116 }
117}
118
Junio C Hamanod3af6212005-08-06 19:50:14119static void copy_templates(const char *git_dir, int len, char *template_dir)
Junio C Hamano8d5afef2005-08-02 23:45:21120{
121 char path[PATH_MAX];
122 char template_path[PATH_MAX];
Junio C Hamanod3af6212005-08-06 19:50:14123 int template_len;
Junio C Hamano8d5afef2005-08-02 23:45:21124 DIR *dir;
125
Junio C Hamano8d5afef2005-08-02 23:45:21126 if (!template_dir)
Junio C Hamanod3af6212005-08-06 19:50:14127 template_dir = DEFAULT_GIT_TEMPLATE_DIR;
Junio C Hamano8d5afef2005-08-02 23:45:21128 strcpy(template_path, template_dir);
129 template_len = strlen(template_path);
130 if (template_path[template_len-1] != '/') {
131 template_path[template_len++] = '/';
132 template_path[template_len] = 0;
133 }
Junio C Hamano8d5afef2005-08-02 23:45:21134 dir = opendir(template_path);
Junio C Hamanod3af6212005-08-06 19:50:14135 if (!dir) {
136 fprintf(stderr, "warning: templates not found %s\n",
137 template_dir);
Junio C Hamano8d5afef2005-08-02 23:45:21138 return;
Junio C Hamanod3af6212005-08-06 19:50:14139 }
140
Junio C Hamano4f629532005-11-26 00:03:56141 /* Make sure that template is from the correct vintage */
142 strcpy(template_path + template_len, "config");
143 repository_format_version = 0;
144 git_config_from_file(check_repository_format_version,
145 template_path);
146 template_path[template_len] = 0;
147
148 if (repository_format_version &&
149 repository_format_version != GIT_REPO_VERSION) {
150 fprintf(stderr, "warning: not copying templates of "
151 "a wrong format version %d from '%s'\n",
152 repository_format_version,
153 template_dir);
154 closedir(dir);
155 return;
156 }
157
Junio C Hamanod3af6212005-08-06 19:50:14158 memcpy(path, git_dir, len);
Petr Baudis1f961c12005-09-20 00:19:50159 path[len] = 0;
Junio C Hamano8d5afef2005-08-02 23:45:21160 copy_templates_1(path, len,
161 template_path, template_len,
162 dir);
163 closedir(dir);
164}
165
Junio C Hamano4f629532005-11-26 00:03:56166static void create_default_files(const char *git_dir, char *template_path)
Linus Torvaldscad88fd2005-05-30 17:20:44167{
168 unsigned len = strlen(git_dir);
169 static char path[PATH_MAX];
Junio C Hamano8098a172005-09-30 21:26:57170 unsigned char sha1[20];
Junio C Hamano4f629532005-11-26 00:03:56171 struct stat st1;
172 char repo_version_string[10];
Linus Torvaldscad88fd2005-05-30 17:20:44173
174 if (len > sizeof(path)-50)
175 die("insane git directory %s", git_dir);
176 memcpy(path, git_dir, len);
177
178 if (len && path[len-1] != '/')
179 path[len++] = '/';
180
181 /*
182 * Create .git/refs/{heads,tags}
183 */
184 strcpy(path + len, "refs");
Johannes Schindelinaf6e2772005-12-22 22:19:37185 safe_create_dir(path, 1);
Linus Torvaldscad88fd2005-05-30 17:20:44186 strcpy(path + len, "refs/heads");
Johannes Schindelinaf6e2772005-12-22 22:19:37187 safe_create_dir(path, 1);
Linus Torvaldscad88fd2005-05-30 17:20:44188 strcpy(path + len, "refs/tags");
Johannes Schindelinaf6e2772005-12-22 22:19:37189 safe_create_dir(path, 1);
Linus Torvaldscad88fd2005-05-30 17:20:44190
Junio C Hamano4f629532005-11-26 00:03:56191 /* First copy the templates -- we might have the default
192 * config file there, in which case we would want to read
193 * from it after installing.
194 */
195 path[len] = 0;
196 copy_templates(path, len, template_path);
197
198 git_config(git_default_config);
199
Linus Torvaldscad88fd2005-05-30 17:20:44200 /*
201 * Create the default symlink from ".git/HEAD" to the "master"
Junio C Hamano8098a172005-09-30 21:26:57202 * branch, if it does not exist yet.
Linus Torvaldscad88fd2005-05-30 17:20:44203 */
204 strcpy(path + len, "HEAD");
Junio C Hamano8098a172005-09-30 21:26:57205 if (read_ref(path, sha1) < 0) {
206 if (create_symref(path, "refs/heads/master") < 0)
Linus Torvaldscad88fd2005-05-30 17:20:44207 exit(1);
Linus Torvaldscad88fd2005-05-30 17:20:44208 }
Junio C Hamano4f629532005-11-26 00:03:56209
210 /* This forces creation of new config file */
211 sprintf(repo_version_string, "%d", GIT_REPO_VERSION);
212 git_config_set("core.repositoryformatversion", repo_version_string);
213
Junio C Hamano8098a172005-09-30 21:26:57214 path[len] = 0;
Johannes Schindeline24317b2005-10-25 23:43:03215 strcpy(path + len, "config");
Johannes Schindeline24317b2005-10-25 23:43:03216
Junio C Hamano4f629532005-11-26 00:03:56217 /* Check filemode trustability */
218 if (!lstat(path, &st1)) {
219 struct stat st2;
220 int filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) &&
221 !lstat(path, &st2) &&
222 st1.st_mode != st2.st_mode);
223 git_config_set("core.filemode",
224 filemode ? "true" : "false");
Johannes Schindeline24317b2005-10-25 23:43:03225 }
Linus Torvaldscad88fd2005-05-30 17:20:44226}
227
Junio C Hamanod3af6212005-08-06 19:50:14228static const char init_db_usage[] =
Johannes Schindelinaf6e2772005-12-22 22:19:37229"git-init-db [--template=<template-directory>] [--shared]";
Junio C Hamanod3af6212005-08-06 19:50:14230
Zach Welch4696cb92005-04-20 04:48:15231/*
232 * If you want to, you can share the DB area with any number of branches.
233 * That has advantages: you can save space by sharing all the SHA1 objects.
234 * On the other hand, it might just make lookup slower and messier. You
235 * be the judge. The default case is to have one DB per managed directory.
236 */
Linus Torvaldse83c5162005-04-07 22:13:13237int main(int argc, char **argv)
238{
Linus Torvaldscad88fd2005-05-30 17:20:44239 const char *git_dir;
Junio C Hamanod19938a2005-05-10 00:57:56240 const char *sha1_dir;
Junio C Hamanod3af6212005-08-06 19:50:14241 char *path, *template_dir = NULL;
Linus Torvalds19b28602005-04-08 16:59:28242 int len, i;
Linus Torvaldse83c5162005-04-07 22:13:13243
Junio C Hamanod3af6212005-08-06 19:50:14244 for (i = 1; i < argc; i++, argv++) {
245 char *arg = argv[1];
Junio C Hamano4a62eae2005-12-06 06:29:36246 if (!strncmp(arg, "--template=", 11))
Junio C Hamanod3af6212005-08-06 19:50:14247 template_dir = arg+11;
Johannes Schindelinaf6e2772005-12-22 22:19:37248 else if (!strcmp(arg, "--shared"))
249 shared_repository = 1;
Junio C Hamanod3af6212005-08-06 19:50:14250 else
251 die(init_db_usage);
252 }
253
Linus Torvaldscad88fd2005-05-30 17:20:44254 /*
255 * Set up the default .git directory contents
256 */
Junio C Hamanoa9ab5862005-09-09 21:48:54257 git_dir = getenv(GIT_DIR_ENVIRONMENT);
Linus Torvaldscad88fd2005-05-30 17:20:44258 if (!git_dir) {
259 git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
Zach Welchaddb3152005-04-20 04:48:15260 fprintf(stderr, "defaulting to local storage area\n");
Linus Torvaldse83c5162005-04-07 22:13:13261 }
Johannes Schindelinaf6e2772005-12-22 22:19:37262 safe_create_dir(git_dir, 0);
Junio C Hamano4f629532005-11-26 00:03:56263
264 /* Check to see if the repository version is right.
265 * Note that a newly created repository does not have
266 * config file, so this will not fail. What we are catching
267 * is an attempt to reinitialize new repository with an old tool.
268 */
269 check_repository_format();
270
Junio C Hamanod3af6212005-08-06 19:50:14271 create_default_files(git_dir, template_dir);
Linus Torvaldscad88fd2005-05-30 17:20:44272
273 /*
274 * And set up the object store.
275 */
276 sha1_dir = get_object_directory();
Linus Torvaldse83c5162005-04-07 22:13:13277 len = strlen(sha1_dir);
Christopher Li812666c2005-04-26 19:00:58278 path = xmalloc(len + 40);
Linus Torvaldse83c5162005-04-07 22:13:13279 memcpy(path, sha1_dir, len);
Zach Welchcb126d82005-04-20 04:48:15280
Johannes Schindelinaf6e2772005-12-22 22:19:37281 safe_create_dir(sha1_dir, 1);
Linus Torvaldsf49fb352005-06-28 01:26:11282 strcpy(path+len, "/pack");
Johannes Schindelinaf6e2772005-12-22 22:19:37283 safe_create_dir(path, 1);
Junio C Hamanod57306c2005-08-20 09:05:31284 strcpy(path+len, "/info");
Johannes Schindelinaf6e2772005-12-22 22:19:37285 safe_create_dir(path, 1);
286
287 if (shared_repository)
288 git_config_set("core.sharedRepository", "true");
289
Linus Torvaldse83c5162005-04-07 22:13:13290 return 0;
291}