]>
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" |
b2141fc1 | 7 | #include "config.h" |
fb58c8d5 | 8 | #include "refs.h" |
c3c8835f | 9 | #include "builtin.h" |
d807c4a0 | 10 | #include "exec-cmd.h" |
596f91ee | 11 | #include "parse-options.h" |
42264bc8 | 12 | #include "worktree.h" |
e83c5163 | 13 | |
d3af621b | 14 | #ifndef DEFAULT_GIT_TEMPLATE_DIR |
d52fd42a | 15 | #define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates" |
d3af621b JH |
16 | #endif |
17 | ||
c869753e SP |
18 | #ifdef NO_TRUSTABLE_FILEMODE |
19 | #define TEST_FILEMODE 0 | |
20 | #else | |
21 | #define TEST_FILEMODE 1 | |
22 | #endif | |
23 | ||
3c9331a1 | 24 | #define GIT_DEFAULT_HASH_ENVIRONMENT "GIT_DEFAULT_HASH" |
25 | ||
0a2c7eea DM |
26 | static int init_is_bare_repository = 0; |
27 | static int init_shared_repository = -1; | |
28 | ||
fa0fccae | 29 | static void copy_templates_1(struct strbuf *path, struct strbuf *template_path, |
8d5afef0 JH |
30 | DIR *dir) |
31 | { | |
9c28390b | 32 | size_t path_baselen = path->len; |
fa0fccae | 33 | size_t template_baselen = template_path->len; |
8d5afef0 JH |
34 | struct dirent *de; |
35 | ||
36 | /* Note: if ".git/hooks" file exists in the repository being | |
37 | * re-initialized, /etc/core-git/templates/hooks/update would | |
f18d244a | 38 | * cause "git init" to fail here. I think this is sane but |
8d5afef0 JH |
39 | * it means that the set of templates we ship by default, along |
40 | * with the way the namespace under .git/ is organized, should | |
41 | * be really carefully chosen. | |
42 | */ | |
9c28390b | 43 | safe_create_dir(path->buf, 1); |
8d5afef0 JH |
44 | while ((de = readdir(dir)) != NULL) { |
45 | struct stat st_git, st_template; | |
8d5afef0 JH |
46 | int exists = 0; |
47 | ||
9c28390b | 48 | strbuf_setlen(path, path_baselen); |
fa0fccae | 49 | strbuf_setlen(template_path, template_baselen); |
9c28390b | 50 | |
8d5afef0 JH |
51 | if (de->d_name[0] == '.') |
52 | continue; | |
9c28390b | 53 | strbuf_addstr(path, de->d_name); |
fa0fccae | 54 | strbuf_addstr(template_path, de->d_name); |
9c28390b | 55 | if (lstat(path->buf, &st_git)) { |
8d5afef0 | 56 | if (errno != ENOENT) |
9c28390b | 57 | die_errno(_("cannot stat '%s'"), path->buf); |
8d5afef0 JH |
58 | } |
59 | else | |
60 | exists = 1; | |
61 | ||
fa0fccae BW |
62 | if (lstat(template_path->buf, &st_template)) |
63 | die_errno(_("cannot stat template '%s'"), template_path->buf); | |
8d5afef0 JH |
64 | |
65 | if (S_ISDIR(st_template.st_mode)) { | |
fa0fccae | 66 | DIR *subdir = opendir(template_path->buf); |
8d5afef0 | 67 | if (!subdir) |
fa0fccae | 68 | die_errno(_("cannot opendir '%s'"), template_path->buf); |
9c28390b | 69 | strbuf_addch(path, '/'); |
fa0fccae BW |
70 | strbuf_addch(template_path, '/'); |
71 | copy_templates_1(path, template_path, subdir); | |
8d5afef0 JH |
72 | closedir(subdir); |
73 | } | |
74 | else if (exists) | |
75 | continue; | |
76 | else if (S_ISLNK(st_template.st_mode)) { | |
9c28390b | 77 | struct strbuf lnk = STRBUF_INIT; |
765b496d JK |
78 | if (strbuf_readlink(&lnk, template_path->buf, |
79 | st_template.st_size) < 0) | |
fa0fccae | 80 | die_errno(_("cannot readlink '%s'"), template_path->buf); |
9c28390b JK |
81 | if (symlink(lnk.buf, path->buf)) |
82 | die_errno(_("cannot symlink '%s' '%s'"), | |
83 | lnk.buf, path->buf); | |
84 | strbuf_release(&lnk); | |
8d5afef0 JH |
85 | } |
86 | else if (S_ISREG(st_template.st_mode)) { | |
fa0fccae | 87 | if (copy_file(path->buf, template_path->buf, st_template.st_mode)) |
9c28390b | 88 | die_errno(_("cannot copy '%s' to '%s'"), |
fa0fccae | 89 | template_path->buf, path->buf); |
8d5afef0 JH |
90 | } |
91 | else | |
fa0fccae | 92 | error(_("ignoring template %s"), template_path->buf); |
8d5afef0 JH |
93 | } |
94 | } | |
95 | ||
e4de4502 | 96 | static void copy_templates(const char *template_dir, const char *init_template_dir) |
8d5afef0 | 97 | { |
9c28390b JK |
98 | struct strbuf path = STRBUF_INIT; |
99 | struct strbuf template_path = STRBUF_INIT; | |
100 | size_t template_len; | |
e8805af1 | 101 | struct repository_format template_format = REPOSITORY_FORMAT_INIT; |
94ce1672 | 102 | struct strbuf err = STRBUF_INIT; |
8d5afef0 | 103 | DIR *dir; |
59362e56 | 104 | char *to_free = NULL; |
8d5afef0 | 105 | |
a47d1813 | 106 | if (!template_dir) |
d4ebc36c | 107 | template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT); |
90b45187 | 108 | if (!template_dir) |
e4de4502 | 109 | template_dir = init_template_dir; |
2de9de5e | 110 | if (!template_dir) |
59362e56 JH |
111 | template_dir = to_free = system_path(DEFAULT_GIT_TEMPLATE_DIR); |
112 | if (!template_dir[0]) { | |
113 | free(to_free); | |
172035f0 | 114 | return; |
59362e56 | 115 | } |
9c28390b JK |
116 | |
117 | strbuf_addstr(&template_path, template_dir); | |
118 | strbuf_complete(&template_path, '/'); | |
119 | template_len = template_path.len; | |
120 | ||
121 | dir = opendir(template_path.buf); | |
d3af621b | 122 | if (!dir) { |
44f560fc | 123 | warning(_("templates not found in %s"), template_dir); |
59362e56 | 124 | goto free_return; |
d3af621b JH |
125 | } |
126 | ||
4f629539 | 127 | /* Make sure that template is from the correct vintage */ |
9c28390b | 128 | strbuf_addstr(&template_path, "config"); |
94ce1672 | 129 | read_repository_format(&template_format, template_path.buf); |
9c28390b | 130 | strbuf_setlen(&template_path, template_len); |
4f629539 | 131 | |
94ce1672 JK |
132 | /* |
133 | * No mention of version at all is OK, but anything else should be | |
134 | * verified. | |
135 | */ | |
136 | if (template_format.version >= 0 && | |
137 | verify_repository_format(&template_format, &err) < 0) { | |
138 | warning(_("not copying templates from '%s': %s"), | |
139 | template_dir, err.buf); | |
140 | strbuf_release(&err); | |
59362e56 | 141 | goto close_free_return; |
4f629539 JH |
142 | } |
143 | ||
fe9aa0b2 | 144 | strbuf_addstr(&path, get_git_common_dir()); |
9c28390b JK |
145 | strbuf_complete(&path, '/'); |
146 | copy_templates_1(&path, &template_path, dir); | |
59362e56 | 147 | close_free_return: |
8d5afef0 | 148 | closedir(dir); |
59362e56 JH |
149 | free_return: |
150 | free(to_free); | |
9c28390b JK |
151 | strbuf_release(&path); |
152 | strbuf_release(&template_path); | |
e8805af1 | 153 | clear_repository_format(&template_format); |
8d5afef0 JH |
154 | } |
155 | ||
84ccad8d JK |
156 | /* |
157 | * If the git_dir is not directly inside the working tree, then git will not | |
158 | * find it by default, and we need to set the worktree explicitly. | |
159 | */ | |
160 | static int needs_work_tree_config(const char *git_dir, const char *work_tree) | |
161 | { | |
162 | if (!strcmp(work_tree, "/") && !strcmp(git_dir, "/.git")) | |
163 | return 0; | |
164 | if (skip_prefix(git_dir, work_tree, &git_dir) && | |
165 | !strcmp(git_dir, "/.git")) | |
166 | return 0; | |
167 | return 1; | |
168 | } | |
169 | ||
47ac9703 | 170 | void initialize_repository_version(int hash_algo, int reinit) |
efa7ae36 | 171 | { |
172 | char repo_version_string[10]; | |
173 | int repo_version = GIT_REPO_VERSION; | |
174 | ||
efa7ae36 | 175 | if (hash_algo != GIT_HASH_SHA1) |
176 | repo_version = GIT_REPO_VERSION_READ; | |
177 | ||
178 | /* This forces creation of new config file */ | |
179 | xsnprintf(repo_version_string, sizeof(repo_version_string), | |
180 | "%d", repo_version); | |
181 | git_config_set("core.repositoryformatversion", repo_version_string); | |
182 | ||
183 | if (hash_algo != GIT_HASH_SHA1) | |
184 | git_config_set("extensions.objectformat", | |
185 | hash_algos[hash_algo].name); | |
47ac9703 | 186 | else if (reinit) |
187 | git_config_set_gently("extensions.objectformat", NULL); | |
efa7ae36 | 188 | } |
189 | ||
6311cfaf | 190 | static int create_default_files(const char *template_path, |
8b8f7189 | 191 | const char *original_git_dir, |
32ba12da | 192 | const char *initial_branch, |
cc0f13c5 JS |
193 | const struct repository_format *fmt, |
194 | int quiet) | |
cad88fdf | 195 | { |
4f629539 | 196 | struct stat st1; |
9c28390b JK |
197 | struct strbuf buf = STRBUF_INIT; |
198 | char *path; | |
5cc8f372 | 199 | char junk[2]; |
ef0a89a6 | 200 | int reinit; |
c869753e | 201 | int filemode; |
6fb5acfd | 202 | struct strbuf err = STRBUF_INIT; |
e4de4502 | 203 | const char *init_template_dir = NULL; |
75555676 | 204 | const char *work_tree = get_git_work_tree(); |
cad88fdf | 205 | |
7c0a842b JK |
206 | /* |
207 | * First copy the templates -- we might have the default | |
4f629539 JH |
208 | * config file there, in which case we would want to read |
209 | * from it after installing. | |
4543926b JK |
210 | * |
211 | * Before reading that config, we also need to clear out any cached | |
212 | * values (since we've just potentially changed what's available on | |
213 | * disk). | |
4f629539 | 214 | */ |
a185dd58 | 215 | git_config_get_pathname("init.templatedir", &init_template_dir); |
e4de4502 | 216 | copy_templates(template_path, init_template_dir); |
a185dd58 | 217 | free((char *)init_template_dir); |
4543926b JK |
218 | git_config_clear(); |
219 | reset_shared_repository(); | |
ef90d6d4 | 220 | git_config(git_default_config, NULL); |
5a688fe4 | 221 | |
7c0a842b JK |
222 | /* |
223 | * We must make sure command-line options continue to override any | |
224 | * values we might have just re-read from the config. | |
225 | */ | |
75555676 | 226 | is_bare_repository_cfg = init_is_bare_repository || !work_tree; |
0a2c7eea | 227 | if (init_shared_repository != -1) |
7875acb6 | 228 | set_shared_repository(init_shared_repository); |
4f629539 | 229 | |
138086a7 JH |
230 | /* |
231 | * We would have created the above under user's umask -- under | |
232 | * shared-repository settings, we would need to fix them up. | |
233 | */ | |
7875acb6 | 234 | if (get_shared_repository()) { |
f225aeb2 | 235 | adjust_shared_perm(get_git_dir()); |
138086a7 JH |
236 | } |
237 | ||
6fb5acfd DT |
238 | /* |
239 | * We need to create a "refs" dir in any case so that older | |
240 | * versions of git can tell that this is a repository. | |
241 | */ | |
242 | safe_create_dir(git_path("refs"), 1); | |
243 | adjust_shared_perm(git_path("refs")); | |
244 | ||
245 | if (refs_init_db(&err)) | |
246 | die("failed to set up refs db: %s", err.buf); | |
247 | ||
cad88fdf | 248 | /* |
32ba12da JS |
249 | * Point the HEAD symref to the initial branch with if HEAD does |
250 | * not yet exist. | |
cad88fdf | 251 | */ |
9c28390b | 252 | path = git_path_buf(&buf, "HEAD"); |
5cc8f372 JS |
253 | reinit = (!access(path, R_OK) |
254 | || readlink(path, junk, sizeof(junk)-1) != -1); | |
ef0a89a6 | 255 | if (!reinit) { |
32ba12da JS |
256 | char *ref; |
257 | ||
258 | if (!initial_branch) | |
cc0f13c5 | 259 | initial_branch = git_default_branch_name(quiet); |
32ba12da JS |
260 | |
261 | ref = xstrfmt("refs/heads/%s", initial_branch); | |
262 | if (check_refname_format(ref, 0) < 0) | |
263 | die(_("invalid initial branch name: '%s'"), | |
264 | initial_branch); | |
265 | ||
266 | if (create_symref("HEAD", ref, NULL) < 0) | |
cad88fdf | 267 | exit(1); |
32ba12da | 268 | free(ref); |
cad88fdf | 269 | } |
e24317b4 | 270 | |
47ac9703 | 271 | initialize_repository_version(fmt->hash_algo, 0); |
4f629539 | 272 | |
4f629539 | 273 | /* Check filemode trustability */ |
9c28390b | 274 | path = git_path_buf(&buf, "config"); |
c869753e SP |
275 | filemode = TEST_FILEMODE; |
276 | if (TEST_FILEMODE && !lstat(path, &st1)) { | |
4f629539 | 277 | struct stat st2; |
c869753e | 278 | filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) && |
4f629539 | 279 | !lstat(path, &st2) && |
1f32ecff MH |
280 | st1.st_mode != st2.st_mode && |
281 | !chmod(path, st1.st_mode)); | |
c7bf68d6 TB |
282 | if (filemode && !reinit && (st1.st_mode & S_IXUSR)) |
283 | filemode = 0; | |
e24317b4 | 284 | } |
3d180648 | 285 | git_config_set("core.filemode", filemode ? "true" : "false"); |
ef0a89a6 | 286 | |
e90fdc39 | 287 | if (is_bare_repository()) |
3d180648 | 288 | git_config_set("core.bare", "true"); |
7d1864ce | 289 | else { |
3d180648 | 290 | git_config_set("core.bare", "false"); |
196055c2 | 291 | /* allow template config file to override the default */ |
341fb286 | 292 | if (log_all_ref_updates == LOG_REFS_UNSET) |
3d180648 | 293 | git_config_set("core.logallrefupdates", "true"); |
6311cfaf | 294 | if (needs_work_tree_config(original_git_dir, work_tree)) |
3d180648 | 295 | git_config_set("core.worktree", work_tree); |
7d1864ce | 296 | } |
75d24499 | 297 | |
75d24499 | 298 | if (!reinit) { |
2455406a | 299 | /* Check if symlink is supported in the work tree */ |
9c28390b | 300 | path = git_path_buf(&buf, "tXXXXXX"); |
75d24499 JH |
301 | if (!close(xmkstemp(path)) && |
302 | !unlink(path) && | |
303 | !symlink("testing", path) && | |
304 | !lstat(path, &st1) && | |
305 | S_ISLNK(st1.st_mode)) | |
306 | unlink(path); /* good */ | |
307 | else | |
3d180648 | 308 | git_config_set("core.symlinks", "false"); |
2455406a DP |
309 | |
310 | /* Check if the filesystem is case-insensitive */ | |
9c28390b | 311 | path = git_path_buf(&buf, "CoNfIg"); |
2455406a | 312 | if (!access(path, F_OK)) |
3d180648 | 313 | git_config_set("core.ignorecase", "true"); |
fdf72966 | 314 | probe_utf8_pathname_composition(); |
75d24499 JH |
315 | } |
316 | ||
9c28390b | 317 | strbuf_release(&buf); |
ef0a89a6 | 318 | return reinit; |
cad88fdf LT |
319 | } |
320 | ||
9173912b JN |
321 | static void create_object_directory(void) |
322 | { | |
9c28390b JK |
323 | struct strbuf path = STRBUF_INIT; |
324 | size_t baselen; | |
325 | ||
326 | strbuf_addstr(&path, get_object_directory()); | |
327 | baselen = path.len; | |
328 | ||
329 | safe_create_dir(path.buf, 1); | |
9173912b | 330 | |
9c28390b JK |
331 | strbuf_setlen(&path, baselen); |
332 | strbuf_addstr(&path, "/pack"); | |
333 | safe_create_dir(path.buf, 1); | |
9173912b | 334 | |
9c28390b JK |
335 | strbuf_setlen(&path, baselen); |
336 | strbuf_addstr(&path, "/info"); | |
337 | safe_create_dir(path.buf, 1); | |
9173912b | 338 | |
9c28390b | 339 | strbuf_release(&path); |
9173912b JN |
340 | } |
341 | ||
822d9406 | 342 | static void separate_git_dir(const char *git_dir, const char *git_link) |
b57fb80a NTND |
343 | { |
344 | struct stat st; | |
b57fb80a NTND |
345 | |
346 | if (!stat(git_link, &st)) { | |
347 | const char *src; | |
348 | ||
349 | if (S_ISREG(st.st_mode)) | |
13d6ec91 | 350 | src = read_gitfile(git_link); |
b57fb80a NTND |
351 | else if (S_ISDIR(st.st_mode)) |
352 | src = git_link; | |
353 | else | |
97f261b1 | 354 | die(_("unable to handle file type %d"), (int)st.st_mode); |
b57fb80a NTND |
355 | |
356 | if (rename(src, git_dir)) | |
2c050e01 | 357 | die_errno(_("unable to move %s to %s"), src, git_dir); |
42264bc8 | 358 | repair_worktrees(NULL, NULL); |
b57fb80a NTND |
359 | } |
360 | ||
1f76a10b | 361 | write_file(git_link, "gitdir: %s", git_dir); |
b57fb80a NTND |
362 | } |
363 | ||
8b8f7189 | 364 | static void validate_hash_algorithm(struct repository_format *repo_fmt, int hash) |
365 | { | |
3c9331a1 | 366 | const char *env = getenv(GIT_DEFAULT_HASH_ENVIRONMENT); |
8b8f7189 | 367 | /* |
368 | * If we already have an initialized repo, don't allow the user to | |
369 | * specify a different algorithm, as that could cause corruption. | |
370 | * Otherwise, if the user has specified one on the command line, use it. | |
371 | */ | |
372 | if (repo_fmt->version >= 0 && hash != GIT_HASH_UNKNOWN && hash != repo_fmt->hash_algo) | |
373 | die(_("attempt to reinitialize repository with different hash")); | |
374 | else if (hash != GIT_HASH_UNKNOWN) | |
375 | repo_fmt->hash_algo = hash; | |
3c9331a1 | 376 | else if (env) { |
377 | int env_algo = hash_algo_by_name(env); | |
378 | if (env_algo == GIT_HASH_UNKNOWN) | |
379 | die(_("unknown hash algorithm '%s'"), env); | |
380 | repo_fmt->hash_algo = env_algo; | |
381 | } | |
8b8f7189 | 382 | } |
383 | ||
33158701 | 384 | int init_db(const char *git_dir, const char *real_git_dir, |
32ba12da JS |
385 | const char *template_dir, int hash, const char *initial_branch, |
386 | unsigned int flags) | |
f225aeb2 | 387 | { |
9173912b | 388 | int reinit; |
1bd19079 | 389 | int exist_ok = flags & INIT_DB_EXIST_OK; |
ce83eadd | 390 | char *original_git_dir = real_pathdup(git_dir, 1); |
8b8f7189 | 391 | struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT; |
33158701 | 392 | |
1bd19079 NTND |
393 | if (real_git_dir) { |
394 | struct stat st; | |
33158701 | 395 | |
1bd19079 NTND |
396 | if (!exist_ok && !stat(git_dir, &st)) |
397 | die(_("%s already exists"), git_dir); | |
398 | ||
399 | if (!exist_ok && !stat(real_git_dir, &st)) | |
400 | die(_("%s already exists"), real_git_dir); | |
f225aeb2 | 401 | |
0915a5b4 | 402 | set_git_dir(real_git_dir, 1); |
822d9406 NTND |
403 | git_dir = get_git_dir(); |
404 | separate_git_dir(git_dir, original_git_dir); | |
1bd19079 NTND |
405 | } |
406 | else { | |
0915a5b4 | 407 | set_git_dir(git_dir, 1); |
822d9406 | 408 | git_dir = get_git_dir(); |
1bd19079 NTND |
409 | } |
410 | startup_info->have_repository = 1; | |
f225aeb2 | 411 | |
e4de4502 AH |
412 | /* Ensure `core.hidedotfiles` is processed */ |
413 | git_config(platform_core_config, NULL); | |
28785339 | 414 | |
b57fb80a | 415 | safe_create_dir(git_dir, 0); |
f225aeb2 | 416 | |
0a2c7eea DM |
417 | init_is_bare_repository = is_bare_repository(); |
418 | ||
f225aeb2 DB |
419 | /* Check to see if the repository version is right. |
420 | * Note that a newly created repository does not have | |
421 | * config file, so this will not fail. What we are catching | |
422 | * is an attempt to reinitialize new repository with an old tool. | |
423 | */ | |
8b8f7189 | 424 | check_repository_format(&repo_fmt); |
f225aeb2 | 425 | |
8b8f7189 | 426 | validate_hash_algorithm(&repo_fmt, hash); |
427 | ||
32ba12da | 428 | reinit = create_default_files(template_dir, original_git_dir, |
cc0f13c5 JS |
429 | initial_branch, &repo_fmt, |
430 | flags & INIT_DB_QUIET); | |
32ba12da JS |
431 | if (reinit && initial_branch) |
432 | warning(_("re-init: ignored --initial-branch=%s"), | |
433 | initial_branch); | |
f225aeb2 | 434 | |
9173912b | 435 | create_object_directory(); |
f225aeb2 | 436 | |
7875acb6 | 437 | if (get_shared_repository()) { |
f225aeb2 DB |
438 | char buf[10]; |
439 | /* We do not spell "group" and such, so that | |
440 | * the configuration can be read by older version | |
441 | * of git. Note, we use octal numbers for new share modes, | |
442 | * and compatibility values for PERM_GROUP and | |
443 | * PERM_EVERYBODY. | |
444 | */ | |
7875acb6 | 445 | if (get_shared_repository() < 0) |
5a688fe4 | 446 | /* force to the mode value */ |
7875acb6 JK |
447 | xsnprintf(buf, sizeof(buf), "0%o", -get_shared_repository()); |
448 | else if (get_shared_repository() == PERM_GROUP) | |
5096d490 | 449 | xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_GROUP); |
7875acb6 | 450 | else if (get_shared_repository() == PERM_EVERYBODY) |
5096d490 | 451 | xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_EVERYBODY); |
f225aeb2 | 452 | else |
033abf97 | 453 | BUG("invalid value for shared_repository"); |
3d180648 PS |
454 | git_config_set("core.sharedrepository", buf); |
455 | git_config_set("receive.denyNonFastforwards", "true"); | |
f225aeb2 DB |
456 | } |
457 | ||
d06f15d9 | 458 | if (!(flags & INIT_DB_QUIET)) { |
d06f15d9 | 459 | int len = strlen(git_dir); |
3e5dd7e9 | 460 | |
c30364d0 VA |
461 | if (reinit) |
462 | printf(get_shared_repository() | |
463 | ? _("Reinitialized existing shared Git repository in %s%s\n") | |
464 | : _("Reinitialized existing Git repository in %s%s\n"), | |
465 | git_dir, len && git_dir[len-1] != '/' ? "/" : ""); | |
466 | else | |
467 | printf(get_shared_repository() | |
468 | ? _("Initialized empty shared Git repository in %s%s\n") | |
469 | : _("Initialized empty Git repository in %s%s\n"), | |
470 | git_dir, len && git_dir[len-1] != '/' ? "/" : ""); | |
d06f15d9 | 471 | } |
f225aeb2 | 472 | |
6311cfaf | 473 | free(original_git_dir); |
f225aeb2 DB |
474 | return 0; |
475 | } | |
476 | ||
477 | static int guess_repository_type(const char *git_dir) | |
6adcca3f | 478 | { |
6adcca3f | 479 | const char *slash; |
56b9f6e7 RS |
480 | char *cwd; |
481 | int cwd_is_git_dir; | |
6adcca3f | 482 | |
6adcca3f JH |
483 | /* |
484 | * "GIT_DIR=. git init" is always bare. | |
485 | * "GIT_DIR=`pwd` git init" too. | |
486 | */ | |
487 | if (!strcmp(".", git_dir)) | |
f225aeb2 | 488 | return 1; |
56b9f6e7 RS |
489 | cwd = xgetcwd(); |
490 | cwd_is_git_dir = !strcmp(git_dir, cwd); | |
491 | free(cwd); | |
492 | if (cwd_is_git_dir) | |
f225aeb2 | 493 | return 1; |
6adcca3f JH |
494 | /* |
495 | * "GIT_DIR=.git or GIT_DIR=something/.git is usually not. | |
496 | */ | |
497 | if (!strcmp(git_dir, ".git")) | |
f225aeb2 | 498 | return 0; |
6adcca3f JH |
499 | slash = strrchr(git_dir, '/'); |
500 | if (slash && !strcmp(slash, "/.git")) | |
f225aeb2 | 501 | return 0; |
6adcca3f JH |
502 | |
503 | /* | |
504 | * Otherwise it is often bare. At this point | |
505 | * we are just guessing. | |
506 | */ | |
f225aeb2 | 507 | return 1; |
6adcca3f JH |
508 | } |
509 | ||
596f91ee MK |
510 | static int shared_callback(const struct option *opt, const char *arg, int unset) |
511 | { | |
517fe807 | 512 | BUG_ON_OPT_NEG(unset); |
596f91ee MK |
513 | *((int *) opt->value) = (arg) ? git_config_perm("arg", arg) : PERM_GROUP; |
514 | return 0; | |
515 | } | |
516 | ||
517 | static const char *const init_db_usage[] = { | |
9c9b4f2f | 518 | N_("git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]] [<directory>]"), |
596f91ee MK |
519 | NULL |
520 | }; | |
d3af621b | 521 | |
4696cb93 ZW |
522 | /* |
523 | * If you want to, you can share the DB area with any number of branches. | |
524 | * That has advantages: you can save space by sharing all the SHA1 objects. | |
525 | * On the other hand, it might just make lookup slower and messier. You | |
526 | * be the judge. The default case is to have one DB per managed directory. | |
527 | */ | |
a633fca0 | 528 | int cmd_init_db(int argc, const char **argv, const char *prefix) |
e83c5163 | 529 | { |
cad88fdf | 530 | const char *git_dir; |
b57fb80a | 531 | const char *real_git_dir = NULL; |
83518360 | 532 | const char *work_tree; |
c3c8835f | 533 | const char *template_dir = NULL; |
f225aeb2 | 534 | unsigned int flags = 0; |
8b8f7189 | 535 | const char *object_format = NULL; |
32ba12da | 536 | const char *initial_branch = NULL; |
8b8f7189 | 537 | int hash_algo = GIT_HASH_UNKNOWN; |
596f91ee | 538 | const struct option init_db_options[] = { |
ce4a5e53 NTND |
539 | OPT_STRING(0, "template", &template_dir, N_("template-directory"), |
540 | N_("directory from which templates will be used")), | |
596f91ee | 541 | OPT_SET_INT(0, "bare", &is_bare_repository_cfg, |
ce4a5e53 | 542 | N_("create a bare repository"), 1), |
596f91ee | 543 | { OPTION_CALLBACK, 0, "shared", &init_shared_repository, |
ce4a5e53 NTND |
544 | N_("permissions"), |
545 | N_("specify that the git repository is to be shared amongst several users"), | |
596f91ee | 546 | PARSE_OPT_OPTARG | PARSE_OPT_NONEG, shared_callback, 0}, |
ce4a5e53 NTND |
547 | OPT_BIT('q', "quiet", &flags, N_("be quiet"), INIT_DB_QUIET), |
548 | OPT_STRING(0, "separate-git-dir", &real_git_dir, N_("gitdir"), | |
549 | N_("separate git dir from working tree")), | |
32ba12da JS |
550 | OPT_STRING('b', "initial-branch", &initial_branch, N_("name"), |
551 | N_("override the name of the initial branch")), | |
8b8f7189 | 552 | OPT_STRING(0, "object-format", &object_format, N_("hash"), |
553 | N_("specify the hash algorithm to use")), | |
596f91ee MK |
554 | OPT_END() |
555 | }; | |
556 | ||
0397ff24 JH |
557 | argc = parse_options(argc, argv, prefix, init_db_options, init_db_usage, 0); |
558 | ||
ccf236a2 ES |
559 | if (real_git_dir && is_bare_repository_cfg == 1) |
560 | die(_("--separate-git-dir and --bare are mutually exclusive")); | |
561 | ||
b57fb80a | 562 | if (real_git_dir && !is_absolute_path(real_git_dir)) |
ce83eadd | 563 | real_git_dir = real_pathdup(real_git_dir, 1); |
b57fb80a | 564 | |
04fe4d75 | 565 | if (template_dir && *template_dir && !is_absolute_path(template_dir)) { |
e1df7fe4 | 566 | template_dir = absolute_pathdup(template_dir); |
04fe4d75 AH |
567 | UNLEAK(template_dir); |
568 | } | |
e1df7fe4 | 569 | |
0397ff24 | 570 | if (argc == 1) { |
53d48885 NS |
571 | int mkdir_tried = 0; |
572 | retry: | |
0397ff24 | 573 | if (chdir(argv[0]) < 0) { |
53d48885 NS |
574 | if (!mkdir_tried) { |
575 | int saved; | |
576 | /* | |
577 | * At this point we haven't read any configuration, | |
578 | * and we know shared_repository should always be 0; | |
579 | * but just in case we play safe. | |
580 | */ | |
7875acb6 JK |
581 | saved = get_shared_repository(); |
582 | set_shared_repository(0); | |
0397ff24 | 583 | switch (safe_create_leading_directories_const(argv[0])) { |
f3565c0c MH |
584 | case SCLD_OK: |
585 | case SCLD_PERMS: | |
586 | break; | |
0be0521b | 587 | case SCLD_EXISTS: |
53d48885 NS |
588 | errno = EEXIST; |
589 | /* fallthru */ | |
53d48885 | 590 | default: |
f3565c0c | 591 | die_errno(_("cannot mkdir %s"), argv[0]); |
53d48885 NS |
592 | break; |
593 | } | |
7875acb6 | 594 | set_shared_repository(saved); |
0397ff24 | 595 | if (mkdir(argv[0], 0777) < 0) |
33e92e47 | 596 | die_errno(_("cannot mkdir %s"), argv[0]); |
53d48885 NS |
597 | mkdir_tried = 1; |
598 | goto retry; | |
599 | } | |
33e92e47 | 600 | die_errno(_("cannot chdir to %s"), argv[0]); |
53d48885 | 601 | } |
0397ff24 JH |
602 | } else if (0 < argc) { |
603 | usage(init_db_usage[0]); | |
d3af621b | 604 | } |
0397ff24 | 605 | if (is_bare_repository_cfg == 1) { |
4d3ab44d RS |
606 | char *cwd = xgetcwd(); |
607 | setenv(GIT_DIR_ENVIRONMENT, cwd, argc > 0); | |
608 | free(cwd); | |
d3af621b JH |
609 | } |
610 | ||
8b8f7189 | 611 | if (object_format) { |
612 | hash_algo = hash_algo_by_name(object_format); | |
613 | if (hash_algo == GIT_HASH_UNKNOWN) | |
614 | die(_("unknown hash algorithm '%s'"), object_format); | |
615 | } | |
616 | ||
5a688fe4 | 617 | if (init_shared_repository != -1) |
7875acb6 | 618 | set_shared_repository(init_shared_repository); |
5a688fe4 | 619 | |
6adcca3f JH |
620 | /* |
621 | * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR | |
622 | * without --bare. Catch the error early. | |
623 | */ | |
e5b07c53 JK |
624 | git_dir = xstrdup_or_null(getenv(GIT_DIR_ENVIRONMENT)); |
625 | work_tree = xstrdup_or_null(getenv(GIT_WORK_TREE_ENVIRONMENT)); | |
83518360 | 626 | if ((!git_dir || is_bare_repository_cfg == 1) && work_tree) |
33e92e47 ÆAB |
627 | die(_("%s (or --work-tree=<directory>) not allowed without " |
628 | "specifying %s (or --git-dir=<directory>)"), | |
6adcca3f JH |
629 | GIT_WORK_TREE_ENVIRONMENT, |
630 | GIT_DIR_ENVIRONMENT); | |
631 | ||
cad88fdf LT |
632 | /* |
633 | * Set up the default .git directory contents | |
634 | */ | |
ef0a89a6 | 635 | if (!git_dir) |
cad88fdf | 636 | git_dir = DEFAULT_GIT_DIR_ENVIRONMENT; |
af6e277c | 637 | |
59d876cc ES |
638 | /* |
639 | * When --separate-git-dir is used inside a linked worktree, take | |
640 | * care to ensure that the common .git/ directory is relocated, not | |
641 | * the worktree-specific .git/worktrees/<id>/ directory. | |
642 | */ | |
643 | if (real_git_dir) { | |
644 | int err; | |
645 | const char *p; | |
646 | struct strbuf sb = STRBUF_INIT; | |
647 | ||
648 | p = read_gitfile_gently(git_dir, &err); | |
649 | if (p && get_common_dir(&sb, p)) { | |
650 | struct strbuf mainwt = STRBUF_INIT; | |
651 | ||
652 | strbuf_addbuf(&mainwt, &sb); | |
653 | strbuf_strip_suffix(&mainwt, "/.git"); | |
654 | if (chdir(mainwt.buf) < 0) | |
655 | die_errno(_("cannot chdir to %s"), mainwt.buf); | |
656 | strbuf_release(&mainwt); | |
657 | git_dir = strbuf_detach(&sb, NULL); | |
658 | } | |
659 | strbuf_release(&sb); | |
660 | } | |
661 | ||
f225aeb2 DB |
662 | if (is_bare_repository_cfg < 0) |
663 | is_bare_repository_cfg = guess_repository_type(git_dir); | |
664 | ||
665 | if (!is_bare_repository_cfg) { | |
b31d2022 NTND |
666 | const char *git_dir_parent = strrchr(git_dir, '/'); |
667 | if (git_dir_parent) { | |
668 | char *rel = xstrndup(git_dir, git_dir_parent - git_dir); | |
ce83eadd | 669 | git_work_tree_cfg = real_pathdup(rel, 1); |
b31d2022 | 670 | free(rel); |
f225aeb2 | 671 | } |
56b9f6e7 RS |
672 | if (!git_work_tree_cfg) |
673 | git_work_tree_cfg = xgetcwd(); | |
83518360 | 674 | if (work_tree) |
2d186c8b | 675 | set_git_work_tree(work_tree); |
83518360 NTND |
676 | else |
677 | set_git_work_tree(git_work_tree_cfg); | |
f225aeb2 | 678 | if (access(get_git_work_tree(), X_OK)) |
33e92e47 | 679 | die_errno (_("Cannot access work tree '%s'"), |
0721c314 | 680 | get_git_work_tree()); |
94df2506 | 681 | } |
83518360 | 682 | else { |
ccf236a2 ES |
683 | if (real_git_dir) |
684 | die(_("--separate-git-dir incompatible with bare repository")); | |
83518360 | 685 | if (work_tree) |
2d186c8b | 686 | set_git_work_tree(work_tree); |
83518360 | 687 | } |
af6e277c | 688 | |
0e5bba53 | 689 | UNLEAK(real_git_dir); |
e5b07c53 JK |
690 | UNLEAK(git_dir); |
691 | UNLEAK(work_tree); | |
0e5bba53 | 692 | |
33158701 | 693 | flags |= INIT_DB_EXIST_OK; |
32ba12da JS |
694 | return init_db(git_dir, real_git_dir, template_dir, hash_algo, |
695 | initial_branch, flags); | |
e83c5163 | 696 | } |