]>
Commit | Line | Data |
---|---|---|
8bc9a0c7 LT |
1 | /* |
2 | * GIT - The information manager from hell | |
3 | * | |
4 | * Copyright (C) Linus Torvalds, 2005 | |
5 | */ | |
e83c5163 | 6 | #include "cache.h" |
c3c8835f | 7 | #include "builtin.h" |
a47d1813 | 8 | #include "exec_cmd.h" |
596f91ee | 9 | #include "parse-options.h" |
e83c5163 | 10 | |
d3af621b | 11 | #ifndef DEFAULT_GIT_TEMPLATE_DIR |
d52fd42a | 12 | #define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates" |
d3af621b JH |
13 | #endif |
14 | ||
c869753e SP |
15 | #ifdef NO_TRUSTABLE_FILEMODE |
16 | #define TEST_FILEMODE 0 | |
17 | #else | |
18 | #define TEST_FILEMODE 1 | |
19 | #endif | |
20 | ||
0a2c7eea DM |
21 | static int init_is_bare_repository = 0; |
22 | static int init_shared_repository = -1; | |
90b45187 | 23 | static const char *init_db_template_dir; |
b57fb80a | 24 | static const char *git_link; |
0a2c7eea | 25 | |
af6e277c | 26 | static void safe_create_dir(const char *dir, int share) |
cb126d8d | 27 | { |
f312de01 | 28 | if (mkdir(dir, 0777) < 0) { |
cb126d8d ZW |
29 | if (errno != EEXIST) { |
30 | perror(dir); | |
31 | exit(1); | |
32 | } | |
33 | } | |
af6e277c | 34 | else if (share && adjust_shared_perm(dir)) |
33e92e47 | 35 | die(_("Could not make %s writable by group"), dir); |
cb126d8d ZW |
36 | } |
37 | ||
8d5afef0 JH |
38 | static void copy_templates_1(char *path, int baselen, |
39 | char *template, int template_baselen, | |
40 | DIR *dir) | |
41 | { | |
42 | struct dirent *de; | |
43 | ||
44 | /* Note: if ".git/hooks" file exists in the repository being | |
45 | * re-initialized, /etc/core-git/templates/hooks/update would | |
f18d244a | 46 | * cause "git init" to fail here. I think this is sane but |
8d5afef0 JH |
47 | * it means that the set of templates we ship by default, along |
48 | * with the way the namespace under .git/ is organized, should | |
49 | * be really carefully chosen. | |
50 | */ | |
af6e277c | 51 | safe_create_dir(path, 1); |
8d5afef0 JH |
52 | while ((de = readdir(dir)) != NULL) { |
53 | struct stat st_git, st_template; | |
54 | int namelen; | |
55 | int exists = 0; | |
56 | ||
57 | if (de->d_name[0] == '.') | |
58 | continue; | |
59 | namelen = strlen(de->d_name); | |
60 | if ((PATH_MAX <= baselen + namelen) || | |
61 | (PATH_MAX <= template_baselen + namelen)) | |
33e92e47 | 62 | die(_("insanely long template name %s"), de->d_name); |
8d5afef0 JH |
63 | memcpy(path + baselen, de->d_name, namelen+1); |
64 | memcpy(template + template_baselen, de->d_name, namelen+1); | |
65 | if (lstat(path, &st_git)) { | |
66 | if (errno != ENOENT) | |
33e92e47 | 67 | die_errno(_("cannot stat '%s'"), path); |
8d5afef0 JH |
68 | } |
69 | else | |
70 | exists = 1; | |
71 | ||
72 | if (lstat(template, &st_template)) | |
33e92e47 | 73 | die_errno(_("cannot stat template '%s'"), template); |
8d5afef0 JH |
74 | |
75 | if (S_ISDIR(st_template.st_mode)) { | |
76 | DIR *subdir = opendir(template); | |
77 | int baselen_sub = baselen + namelen; | |
78 | int template_baselen_sub = template_baselen + namelen; | |
79 | if (!subdir) | |
33e92e47 | 80 | die_errno(_("cannot opendir '%s'"), template); |
8d5afef0 JH |
81 | path[baselen_sub++] = |
82 | template[template_baselen_sub++] = '/'; | |
83 | path[baselen_sub] = | |
84 | template[template_baselen_sub] = 0; | |
85 | copy_templates_1(path, baselen_sub, | |
86 | template, template_baselen_sub, | |
87 | subdir); | |
88 | closedir(subdir); | |
89 | } | |
90 | else if (exists) | |
91 | continue; | |
92 | else if (S_ISLNK(st_template.st_mode)) { | |
93 | char lnk[256]; | |
94 | int len; | |
95 | len = readlink(template, lnk, sizeof(lnk)); | |
96 | if (len < 0) | |
33e92e47 | 97 | die_errno(_("cannot readlink '%s'"), template); |
8d5afef0 | 98 | if (sizeof(lnk) <= len) |
33e92e47 | 99 | die(_("insanely long symlink %s"), template); |
8d5afef0 JH |
100 | lnk[len] = 0; |
101 | if (symlink(lnk, path)) | |
33e92e47 | 102 | die_errno(_("cannot symlink '%s' '%s'"), lnk, path); |
8d5afef0 JH |
103 | } |
104 | else if (S_ISREG(st_template.st_mode)) { | |
105 | if (copy_file(path, template, st_template.st_mode)) | |
33e92e47 | 106 | die_errno(_("cannot copy '%s' to '%s'"), template, |
0721c314 | 107 | path); |
8d5afef0 JH |
108 | } |
109 | else | |
33e92e47 | 110 | error(_("ignoring template %s"), template); |
8d5afef0 JH |
111 | } |
112 | } | |
113 | ||
f225aeb2 | 114 | static void copy_templates(const char *template_dir) |
8d5afef0 JH |
115 | { |
116 | char path[PATH_MAX]; | |
117 | char template_path[PATH_MAX]; | |
d3af621b | 118 | int template_len; |
8d5afef0 | 119 | DIR *dir; |
f225aeb2 DB |
120 | const char *git_dir = get_git_dir(); |
121 | int len = strlen(git_dir); | |
59362e56 | 122 | char *to_free = NULL; |
8d5afef0 | 123 | |
a47d1813 | 124 | if (!template_dir) |
d4ebc36c | 125 | template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT); |
90b45187 SD |
126 | if (!template_dir) |
127 | template_dir = init_db_template_dir; | |
2de9de5e | 128 | if (!template_dir) |
59362e56 JH |
129 | template_dir = to_free = system_path(DEFAULT_GIT_TEMPLATE_DIR); |
130 | if (!template_dir[0]) { | |
131 | free(to_free); | |
172035f0 | 132 | return; |
59362e56 | 133 | } |
32d1776b FL |
134 | template_len = strlen(template_dir); |
135 | if (PATH_MAX <= (template_len+strlen("/config"))) | |
33e92e47 | 136 | die(_("insanely long template path %s"), template_dir); |
8d5afef0 | 137 | strcpy(template_path, template_dir); |
8d5afef0 JH |
138 | if (template_path[template_len-1] != '/') { |
139 | template_path[template_len++] = '/'; | |
140 | template_path[template_len] = 0; | |
141 | } | |
8d5afef0 | 142 | dir = opendir(template_path); |
d3af621b | 143 | if (!dir) { |
33e92e47 | 144 | warning(_("templates not found %s"), template_dir); |
59362e56 | 145 | goto free_return; |
d3af621b JH |
146 | } |
147 | ||
4f629539 JH |
148 | /* Make sure that template is from the correct vintage */ |
149 | strcpy(template_path + template_len, "config"); | |
150 | repository_format_version = 0; | |
151 | git_config_from_file(check_repository_format_version, | |
ef90d6d4 | 152 | template_path, NULL); |
4f629539 JH |
153 | template_path[template_len] = 0; |
154 | ||
155 | if (repository_format_version && | |
156 | repository_format_version != GIT_REPO_VERSION) { | |
33e92e47 ÆAB |
157 | warning(_("not copying templates of " |
158 | "a wrong format version %d from '%s'"), | |
4f629539 JH |
159 | repository_format_version, |
160 | template_dir); | |
59362e56 | 161 | goto close_free_return; |
4f629539 JH |
162 | } |
163 | ||
d3af621b | 164 | memcpy(path, git_dir, len); |
f225aeb2 DB |
165 | if (len && path[len - 1] != '/') |
166 | path[len++] = '/'; | |
1f961c19 | 167 | path[len] = 0; |
8d5afef0 JH |
168 | copy_templates_1(path, len, |
169 | template_path, template_len, | |
170 | dir); | |
59362e56 | 171 | close_free_return: |
8d5afef0 | 172 | closedir(dir); |
59362e56 JH |
173 | free_return: |
174 | free(to_free); | |
8d5afef0 JH |
175 | } |
176 | ||
90b45187 SD |
177 | static int git_init_db_config(const char *k, const char *v, void *cb) |
178 | { | |
90b45187 SD |
179 | if (!strcmp(k, "init.templatedir")) |
180 | return git_config_pathname(&init_db_template_dir, k, v); | |
181 | ||
182 | return 0; | |
183 | } | |
184 | ||
f225aeb2 | 185 | static int create_default_files(const char *template_path) |
cad88fdf | 186 | { |
f225aeb2 | 187 | const char *git_dir = get_git_dir(); |
cad88fdf LT |
188 | unsigned len = strlen(git_dir); |
189 | static char path[PATH_MAX]; | |
4f629539 JH |
190 | struct stat st1; |
191 | char repo_version_string[10]; | |
5cc8f372 | 192 | char junk[2]; |
ef0a89a6 | 193 | int reinit; |
c869753e | 194 | int filemode; |
cad88fdf LT |
195 | |
196 | if (len > sizeof(path)-50) | |
33e92e47 | 197 | die(_("insane git directory %s"), git_dir); |
cad88fdf LT |
198 | memcpy(path, git_dir, len); |
199 | ||
200 | if (len && path[len-1] != '/') | |
201 | path[len++] = '/'; | |
202 | ||
203 | /* | |
204 | * Create .git/refs/{heads,tags} | |
205 | */ | |
f225aeb2 DB |
206 | safe_create_dir(git_path("refs"), 1); |
207 | safe_create_dir(git_path("refs/heads"), 1); | |
208 | safe_create_dir(git_path("refs/tags"), 1); | |
cad88fdf | 209 | |
90b45187 SD |
210 | /* Just look for `init.templatedir` */ |
211 | git_config(git_init_db_config, NULL); | |
212 | ||
4f629539 JH |
213 | /* First copy the templates -- we might have the default |
214 | * config file there, in which case we would want to read | |
215 | * from it after installing. | |
216 | */ | |
f225aeb2 | 217 | copy_templates(template_path); |
4f629539 | 218 | |
ef90d6d4 | 219 | git_config(git_default_config, NULL); |
0a2c7eea | 220 | is_bare_repository_cfg = init_is_bare_repository; |
5a688fe4 JH |
221 | |
222 | /* reading existing config may have overwrote it */ | |
0a2c7eea DM |
223 | if (init_shared_repository != -1) |
224 | shared_repository = init_shared_repository; | |
4f629539 | 225 | |
138086a7 JH |
226 | /* |
227 | * We would have created the above under user's umask -- under | |
228 | * shared-repository settings, we would need to fix them up. | |
229 | */ | |
230 | if (shared_repository) { | |
f225aeb2 DB |
231 | adjust_shared_perm(get_git_dir()); |
232 | adjust_shared_perm(git_path("refs")); | |
233 | adjust_shared_perm(git_path("refs/heads")); | |
234 | adjust_shared_perm(git_path("refs/tags")); | |
138086a7 JH |
235 | } |
236 | ||
cad88fdf LT |
237 | /* |
238 | * Create the default symlink from ".git/HEAD" to the "master" | |
8098a178 | 239 | * branch, if it does not exist yet. |
cad88fdf LT |
240 | */ |
241 | strcpy(path + len, "HEAD"); | |
5cc8f372 JS |
242 | reinit = (!access(path, R_OK) |
243 | || readlink(path, junk, sizeof(junk)-1) != -1); | |
ef0a89a6 | 244 | if (!reinit) { |
8b5157e4 | 245 | if (create_symref("HEAD", "refs/heads/master", NULL) < 0) |
cad88fdf | 246 | exit(1); |
cad88fdf | 247 | } |
e24317b4 | 248 | |
4f629539 JH |
249 | /* This forces creation of new config file */ |
250 | sprintf(repo_version_string, "%d", GIT_REPO_VERSION); | |
251 | git_config_set("core.repositoryformatversion", repo_version_string); | |
252 | ||
253 | path[len] = 0; | |
e24317b4 | 254 | strcpy(path + len, "config"); |
4f629539 JH |
255 | |
256 | /* Check filemode trustability */ | |
c869753e SP |
257 | filemode = TEST_FILEMODE; |
258 | if (TEST_FILEMODE && !lstat(path, &st1)) { | |
4f629539 | 259 | struct stat st2; |
c869753e | 260 | filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) && |
4f629539 | 261 | !lstat(path, &st2) && |
1f32ecff MH |
262 | st1.st_mode != st2.st_mode && |
263 | !chmod(path, st1.st_mode)); | |
c7bf68d6 TB |
264 | if (filemode && !reinit && (st1.st_mode & S_IXUSR)) |
265 | filemode = 0; | |
e24317b4 | 266 | } |
c869753e | 267 | git_config_set("core.filemode", filemode ? "true" : "false"); |
ef0a89a6 | 268 | |
e90fdc39 | 269 | if (is_bare_repository()) |
7d1864ce | 270 | git_config_set("core.bare", "true"); |
7d1864ce | 271 | else { |
e90fdc39 | 272 | const char *work_tree = get_git_work_tree(); |
7d1864ce | 273 | git_config_set("core.bare", "false"); |
196055c2 AR |
274 | /* allow template config file to override the default */ |
275 | if (log_all_ref_updates == -1) | |
276 | git_config_set("core.logallrefupdates", "true"); | |
59556548 | 277 | if (!starts_with(git_dir, work_tree) || |
f225aeb2 | 278 | strcmp(git_dir + strlen(work_tree), "/.git")) { |
e90fdc39 | 279 | git_config_set("core.worktree", work_tree); |
f225aeb2 | 280 | } |
7d1864ce | 281 | } |
75d24499 | 282 | |
75d24499 | 283 | if (!reinit) { |
2455406a | 284 | /* Check if symlink is supported in the work tree */ |
75d24499 JH |
285 | path[len] = 0; |
286 | strcpy(path + len, "tXXXXXX"); | |
287 | if (!close(xmkstemp(path)) && | |
288 | !unlink(path) && | |
289 | !symlink("testing", path) && | |
290 | !lstat(path, &st1) && | |
291 | S_ISLNK(st1.st_mode)) | |
292 | unlink(path); /* good */ | |
293 | else | |
294 | git_config_set("core.symlinks", "false"); | |
2455406a DP |
295 | |
296 | /* Check if the filesystem is case-insensitive */ | |
297 | path[len] = 0; | |
298 | strcpy(path + len, "CoNfIg"); | |
299 | if (!access(path, F_OK)) | |
300 | git_config_set("core.ignorecase", "true"); | |
76759c7d | 301 | probe_utf8_pathname_composition(path, len); |
75d24499 JH |
302 | } |
303 | ||
ef0a89a6 | 304 | return reinit; |
cad88fdf LT |
305 | } |
306 | ||
9173912b JN |
307 | static void create_object_directory(void) |
308 | { | |
309 | const char *object_directory = get_object_directory(); | |
310 | int len = strlen(object_directory); | |
311 | char *path = xmalloc(len + 40); | |
312 | ||
313 | memcpy(path, object_directory, len); | |
314 | ||
315 | safe_create_dir(object_directory, 1); | |
316 | strcpy(path+len, "/pack"); | |
317 | safe_create_dir(path, 1); | |
318 | strcpy(path+len, "/info"); | |
319 | safe_create_dir(path, 1); | |
320 | ||
321 | free(path); | |
322 | } | |
323 | ||
b57fb80a NTND |
324 | int set_git_dir_init(const char *git_dir, const char *real_git_dir, |
325 | int exist_ok) | |
326 | { | |
327 | if (real_git_dir) { | |
328 | struct stat st; | |
329 | ||
330 | if (!exist_ok && !stat(git_dir, &st)) | |
2c050e01 | 331 | die(_("%s already exists"), git_dir); |
b57fb80a NTND |
332 | |
333 | if (!exist_ok && !stat(real_git_dir, &st)) | |
2c050e01 | 334 | die(_("%s already exists"), real_git_dir); |
b57fb80a NTND |
335 | |
336 | /* | |
337 | * make sure symlinks are resolved because we'll be | |
338 | * moving the target repo later on in separate_git_dir() | |
339 | */ | |
340 | git_link = xstrdup(real_path(git_dir)); | |
2d186c8b | 341 | set_git_dir(real_path(real_git_dir)); |
b57fb80a NTND |
342 | } |
343 | else { | |
2d186c8b | 344 | set_git_dir(real_path(git_dir)); |
b57fb80a NTND |
345 | git_link = NULL; |
346 | } | |
b57fb80a NTND |
347 | return 0; |
348 | } | |
349 | ||
350 | static void separate_git_dir(const char *git_dir) | |
351 | { | |
352 | struct stat st; | |
353 | FILE *fp; | |
354 | ||
355 | if (!stat(git_link, &st)) { | |
356 | const char *src; | |
357 | ||
358 | if (S_ISREG(st.st_mode)) | |
13d6ec91 | 359 | src = read_gitfile(git_link); |
b57fb80a NTND |
360 | else if (S_ISDIR(st.st_mode)) |
361 | src = git_link; | |
362 | else | |
97f261b1 | 363 | die(_("unable to handle file type %d"), (int)st.st_mode); |
b57fb80a NTND |
364 | |
365 | if (rename(src, git_dir)) | |
2c050e01 | 366 | die_errno(_("unable to move %s to %s"), src, git_dir); |
b57fb80a NTND |
367 | } |
368 | ||
369 | fp = fopen(git_link, "w"); | |
370 | if (!fp) | |
2c050e01 | 371 | die(_("Could not create git link %s"), git_link); |
b57fb80a NTND |
372 | fprintf(fp, "gitdir: %s\n", git_dir); |
373 | fclose(fp); | |
374 | } | |
375 | ||
f225aeb2 DB |
376 | int init_db(const char *template_dir, unsigned int flags) |
377 | { | |
9173912b | 378 | int reinit; |
b57fb80a | 379 | const char *git_dir = get_git_dir(); |
f225aeb2 | 380 | |
b57fb80a NTND |
381 | if (git_link) |
382 | separate_git_dir(git_dir); | |
f225aeb2 | 383 | |
b57fb80a | 384 | safe_create_dir(git_dir, 0); |
f225aeb2 | 385 | |
0a2c7eea DM |
386 | init_is_bare_repository = is_bare_repository(); |
387 | ||
f225aeb2 DB |
388 | /* Check to see if the repository version is right. |
389 | * Note that a newly created repository does not have | |
390 | * config file, so this will not fail. What we are catching | |
391 | * is an attempt to reinitialize new repository with an old tool. | |
392 | */ | |
393 | check_repository_format(); | |
394 | ||
395 | reinit = create_default_files(template_dir); | |
396 | ||
9173912b | 397 | create_object_directory(); |
f225aeb2 DB |
398 | |
399 | if (shared_repository) { | |
400 | char buf[10]; | |
401 | /* We do not spell "group" and such, so that | |
402 | * the configuration can be read by older version | |
403 | * of git. Note, we use octal numbers for new share modes, | |
404 | * and compatibility values for PERM_GROUP and | |
405 | * PERM_EVERYBODY. | |
406 | */ | |
5a688fe4 JH |
407 | if (shared_repository < 0) |
408 | /* force to the mode value */ | |
409 | sprintf(buf, "0%o", -shared_repository); | |
410 | else if (shared_repository == PERM_GROUP) | |
f225aeb2 DB |
411 | sprintf(buf, "%d", OLD_PERM_GROUP); |
412 | else if (shared_repository == PERM_EVERYBODY) | |
413 | sprintf(buf, "%d", OLD_PERM_EVERYBODY); | |
414 | else | |
5a688fe4 | 415 | die("oops"); |
f225aeb2 DB |
416 | git_config_set("core.sharedrepository", buf); |
417 | git_config_set("receive.denyNonFastforwards", "true"); | |
418 | } | |
419 | ||
d06f15d9 | 420 | if (!(flags & INIT_DB_QUIET)) { |
d06f15d9 | 421 | int len = strlen(git_dir); |
3e5dd7e9 | 422 | |
47fbfded JX |
423 | /* TRANSLATORS: The first '%s' is either "Reinitialized |
424 | existing" or "Initialized empty", the second " shared" or | |
425 | "", and the last '%s%s' is the verbatim directory name. */ | |
3e5dd7e9 ÆAB |
426 | printf(_("%s%s Git repository in %s%s\n"), |
427 | reinit ? _("Reinitialized existing") : _("Initialized empty"), | |
428 | shared_repository ? _(" shared") : "", | |
d06f15d9 NTND |
429 | git_dir, len && git_dir[len-1] != '/' ? "/" : ""); |
430 | } | |
f225aeb2 DB |
431 | |
432 | return 0; | |
433 | } | |
434 | ||
435 | static int guess_repository_type(const char *git_dir) | |
6adcca3f | 436 | { |
6adcca3f | 437 | const char *slash; |
56b9f6e7 RS |
438 | char *cwd; |
439 | int cwd_is_git_dir; | |
6adcca3f | 440 | |
6adcca3f JH |
441 | /* |
442 | * "GIT_DIR=. git init" is always bare. | |
443 | * "GIT_DIR=`pwd` git init" too. | |
444 | */ | |
445 | if (!strcmp(".", git_dir)) | |
f225aeb2 | 446 | return 1; |
56b9f6e7 RS |
447 | cwd = xgetcwd(); |
448 | cwd_is_git_dir = !strcmp(git_dir, cwd); | |
449 | free(cwd); | |
450 | if (cwd_is_git_dir) | |
f225aeb2 | 451 | return 1; |
6adcca3f JH |
452 | /* |
453 | * "GIT_DIR=.git or GIT_DIR=something/.git is usually not. | |
454 | */ | |
455 | if (!strcmp(git_dir, ".git")) | |
f225aeb2 | 456 | return 0; |
6adcca3f JH |
457 | slash = strrchr(git_dir, '/'); |
458 | if (slash && !strcmp(slash, "/.git")) | |
f225aeb2 | 459 | return 0; |
6adcca3f JH |
460 | |
461 | /* | |
462 | * Otherwise it is often bare. At this point | |
463 | * we are just guessing. | |
464 | */ | |
f225aeb2 | 465 | return 1; |
6adcca3f JH |
466 | } |
467 | ||
596f91ee MK |
468 | static int shared_callback(const struct option *opt, const char *arg, int unset) |
469 | { | |
470 | *((int *) opt->value) = (arg) ? git_config_perm("arg", arg) : PERM_GROUP; | |
471 | return 0; | |
472 | } | |
473 | ||
474 | static const char *const init_db_usage[] = { | |
ce4a5e53 | 475 | N_("git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]] [directory]"), |
596f91ee MK |
476 | NULL |
477 | }; | |
d3af621b | 478 | |
4696cb93 ZW |
479 | /* |
480 | * If you want to, you can share the DB area with any number of branches. | |
481 | * That has advantages: you can save space by sharing all the SHA1 objects. | |
482 | * On the other hand, it might just make lookup slower and messier. You | |
483 | * be the judge. The default case is to have one DB per managed directory. | |
484 | */ | |
a633fca0 | 485 | int cmd_init_db(int argc, const char **argv, const char *prefix) |
e83c5163 | 486 | { |
cad88fdf | 487 | const char *git_dir; |
b57fb80a | 488 | const char *real_git_dir = NULL; |
83518360 | 489 | const char *work_tree; |
c3c8835f | 490 | const char *template_dir = NULL; |
f225aeb2 | 491 | unsigned int flags = 0; |
596f91ee | 492 | const struct option init_db_options[] = { |
ce4a5e53 NTND |
493 | OPT_STRING(0, "template", &template_dir, N_("template-directory"), |
494 | N_("directory from which templates will be used")), | |
596f91ee | 495 | OPT_SET_INT(0, "bare", &is_bare_repository_cfg, |
ce4a5e53 | 496 | N_("create a bare repository"), 1), |
596f91ee | 497 | { OPTION_CALLBACK, 0, "shared", &init_shared_repository, |
ce4a5e53 NTND |
498 | N_("permissions"), |
499 | N_("specify that the git repository is to be shared amongst several users"), | |
596f91ee | 500 | PARSE_OPT_OPTARG | PARSE_OPT_NONEG, shared_callback, 0}, |
ce4a5e53 NTND |
501 | OPT_BIT('q', "quiet", &flags, N_("be quiet"), INIT_DB_QUIET), |
502 | OPT_STRING(0, "separate-git-dir", &real_git_dir, N_("gitdir"), | |
503 | N_("separate git dir from working tree")), | |
596f91ee MK |
504 | OPT_END() |
505 | }; | |
506 | ||
0397ff24 JH |
507 | argc = parse_options(argc, argv, prefix, init_db_options, init_db_usage, 0); |
508 | ||
b57fb80a NTND |
509 | if (real_git_dir && !is_absolute_path(real_git_dir)) |
510 | real_git_dir = xstrdup(real_path(real_git_dir)); | |
511 | ||
0397ff24 | 512 | if (argc == 1) { |
53d48885 NS |
513 | int mkdir_tried = 0; |
514 | retry: | |
0397ff24 | 515 | if (chdir(argv[0]) < 0) { |
53d48885 NS |
516 | if (!mkdir_tried) { |
517 | int saved; | |
518 | /* | |
519 | * At this point we haven't read any configuration, | |
520 | * and we know shared_repository should always be 0; | |
521 | * but just in case we play safe. | |
522 | */ | |
523 | saved = shared_repository; | |
524 | shared_repository = 0; | |
0397ff24 | 525 | switch (safe_create_leading_directories_const(argv[0])) { |
f3565c0c MH |
526 | case SCLD_OK: |
527 | case SCLD_PERMS: | |
528 | break; | |
0be0521b | 529 | case SCLD_EXISTS: |
53d48885 NS |
530 | errno = EEXIST; |
531 | /* fallthru */ | |
53d48885 | 532 | default: |
f3565c0c | 533 | die_errno(_("cannot mkdir %s"), argv[0]); |
53d48885 NS |
534 | break; |
535 | } | |
536 | shared_repository = saved; | |
0397ff24 | 537 | if (mkdir(argv[0], 0777) < 0) |
33e92e47 | 538 | die_errno(_("cannot mkdir %s"), argv[0]); |
53d48885 NS |
539 | mkdir_tried = 1; |
540 | goto retry; | |
541 | } | |
33e92e47 | 542 | die_errno(_("cannot chdir to %s"), argv[0]); |
53d48885 | 543 | } |
0397ff24 JH |
544 | } else if (0 < argc) { |
545 | usage(init_db_usage[0]); | |
d3af621b | 546 | } |
0397ff24 | 547 | if (is_bare_repository_cfg == 1) { |
4d3ab44d RS |
548 | char *cwd = xgetcwd(); |
549 | setenv(GIT_DIR_ENVIRONMENT, cwd, argc > 0); | |
550 | free(cwd); | |
d3af621b JH |
551 | } |
552 | ||
5a688fe4 JH |
553 | if (init_shared_repository != -1) |
554 | shared_repository = init_shared_repository; | |
555 | ||
6adcca3f JH |
556 | /* |
557 | * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR | |
558 | * without --bare. Catch the error early. | |
559 | */ | |
560 | git_dir = getenv(GIT_DIR_ENVIRONMENT); | |
83518360 NTND |
561 | work_tree = getenv(GIT_WORK_TREE_ENVIRONMENT); |
562 | if ((!git_dir || is_bare_repository_cfg == 1) && work_tree) | |
33e92e47 ÆAB |
563 | die(_("%s (or --work-tree=<directory>) not allowed without " |
564 | "specifying %s (or --git-dir=<directory>)"), | |
6adcca3f JH |
565 | GIT_WORK_TREE_ENVIRONMENT, |
566 | GIT_DIR_ENVIRONMENT); | |
567 | ||
cad88fdf LT |
568 | /* |
569 | * Set up the default .git directory contents | |
570 | */ | |
ef0a89a6 | 571 | if (!git_dir) |
cad88fdf | 572 | git_dir = DEFAULT_GIT_DIR_ENVIRONMENT; |
af6e277c | 573 | |
f225aeb2 DB |
574 | if (is_bare_repository_cfg < 0) |
575 | is_bare_repository_cfg = guess_repository_type(git_dir); | |
576 | ||
577 | if (!is_bare_repository_cfg) { | |
b31d2022 NTND |
578 | const char *git_dir_parent = strrchr(git_dir, '/'); |
579 | if (git_dir_parent) { | |
580 | char *rel = xstrndup(git_dir, git_dir_parent - git_dir); | |
e2a57aac | 581 | git_work_tree_cfg = xstrdup(real_path(rel)); |
b31d2022 | 582 | free(rel); |
f225aeb2 | 583 | } |
56b9f6e7 RS |
584 | if (!git_work_tree_cfg) |
585 | git_work_tree_cfg = xgetcwd(); | |
83518360 | 586 | if (work_tree) |
2d186c8b | 587 | set_git_work_tree(work_tree); |
83518360 NTND |
588 | else |
589 | set_git_work_tree(git_work_tree_cfg); | |
f225aeb2 | 590 | if (access(get_git_work_tree(), X_OK)) |
33e92e47 | 591 | die_errno (_("Cannot access work tree '%s'"), |
0721c314 | 592 | get_git_work_tree()); |
94df2506 | 593 | } |
83518360 NTND |
594 | else { |
595 | if (work_tree) | |
2d186c8b | 596 | set_git_work_tree(work_tree); |
83518360 | 597 | } |
af6e277c | 598 | |
b57fb80a | 599 | set_git_dir_init(git_dir, real_git_dir, 1); |
ef0a89a6 | 600 | |
f225aeb2 | 601 | return init_db(template_dir, flags); |
e83c5163 | 602 | } |