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