Linus Torvalds | 8bc9a0c | 2005-04-07 15:16:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * GIT - The information manager from hell |
| 3 | * |
| 4 | * Copyright (C) Linus Torvalds, 2005 |
| 5 | */ |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 6 | #include "cache.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 7 | #include "config.h" |
Michael Haggerty | fb58c8d | 2015-06-22 16:03:05 +0200 | [diff] [blame] | 8 | #include "refs.h" |
Timo Hirvonen | c3c8835 | 2006-05-19 13:03:57 +0300 | [diff] [blame] | 9 | #include "builtin.h" |
Stefan Beller | d807c4a | 2018-04-10 14:26:18 -0700 | [diff] [blame] | 10 | #include "exec-cmd.h" |
Michał Kiedrowicz | 596f91e | 2009-07-12 12:24:32 +0200 | [diff] [blame] | 11 | #include "parse-options.h" |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 12 | |
Junio C Hamano | d3af621 | 2005-08-06 12:50:14 -0700 | [diff] [blame] | 13 | #ifndef DEFAULT_GIT_TEMPLATE_DIR |
Johannes Sixt | d52fd42 | 2007-06-11 11:10:47 +0200 | [diff] [blame] | 14 | #define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates" |
Junio C Hamano | d3af621 | 2005-08-06 12:50:14 -0700 | [diff] [blame] | 15 | #endif |
| 16 | |
Shawn O. Pearce | c869753 | 2006-12-30 23:53:55 -0500 | [diff] [blame] | 17 | #ifdef NO_TRUSTABLE_FILEMODE |
| 18 | #define TEST_FILEMODE 0 |
| 19 | #else |
| 20 | #define TEST_FILEMODE 1 |
| 21 | #endif |
| 22 | |
Deskin Miller | 0a2c7ee | 2008-10-07 01:37:48 -0400 | [diff] [blame] | 23 | static int init_is_bare_repository = 0; |
| 24 | static int init_shared_repository = -1; |
Steven Drake | 90b4518 | 2010-02-17 12:42:31 +1300 | [diff] [blame] | 25 | static const char *init_db_template_dir; |
Deskin Miller | 0a2c7ee | 2008-10-07 01:37:48 -0400 | [diff] [blame] | 26 | |
Brandon Williams | fa0fcca | 2018-02-14 10:59:52 -0800 | [diff] [blame] | 27 | static void copy_templates_1(struct strbuf *path, struct strbuf *template_path, |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 28 | DIR *dir) |
| 29 | { |
Jeff King | 9c28390 | 2015-10-04 23:46:04 -0400 | [diff] [blame] | 30 | size_t path_baselen = path->len; |
Brandon Williams | fa0fcca | 2018-02-14 10:59:52 -0800 | [diff] [blame] | 31 | size_t template_baselen = template_path->len; |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 32 | struct dirent *de; |
| 33 | |
| 34 | /* Note: if ".git/hooks" file exists in the repository being |
| 35 | * re-initialized, /etc/core-git/templates/hooks/update would |
Heikki Orsila | f18d244 | 2008-09-13 20:18:36 +0300 | [diff] [blame] | 36 | * cause "git init" to fail here. I think this is sane but |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 37 | * it means that the set of templates we ship by default, along |
| 38 | * with the way the namespace under .git/ is organized, should |
| 39 | * be really carefully chosen. |
| 40 | */ |
Jeff King | 9c28390 | 2015-10-04 23:46:04 -0400 | [diff] [blame] | 41 | safe_create_dir(path->buf, 1); |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 42 | while ((de = readdir(dir)) != NULL) { |
| 43 | struct stat st_git, st_template; |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 44 | int exists = 0; |
| 45 | |
Jeff King | 9c28390 | 2015-10-04 23:46:04 -0400 | [diff] [blame] | 46 | strbuf_setlen(path, path_baselen); |
Brandon Williams | fa0fcca | 2018-02-14 10:59:52 -0800 | [diff] [blame] | 47 | strbuf_setlen(template_path, template_baselen); |
Jeff King | 9c28390 | 2015-10-04 23:46:04 -0400 | [diff] [blame] | 48 | |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 49 | if (de->d_name[0] == '.') |
| 50 | continue; |
Jeff King | 9c28390 | 2015-10-04 23:46:04 -0400 | [diff] [blame] | 51 | strbuf_addstr(path, de->d_name); |
Brandon Williams | fa0fcca | 2018-02-14 10:59:52 -0800 | [diff] [blame] | 52 | strbuf_addstr(template_path, de->d_name); |
Jeff King | 9c28390 | 2015-10-04 23:46:04 -0400 | [diff] [blame] | 53 | if (lstat(path->buf, &st_git)) { |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 54 | if (errno != ENOENT) |
Jeff King | 9c28390 | 2015-10-04 23:46:04 -0400 | [diff] [blame] | 55 | die_errno(_("cannot stat '%s'"), path->buf); |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 56 | } |
| 57 | else |
| 58 | exists = 1; |
| 59 | |
Brandon Williams | fa0fcca | 2018-02-14 10:59:52 -0800 | [diff] [blame] | 60 | if (lstat(template_path->buf, &st_template)) |
| 61 | die_errno(_("cannot stat template '%s'"), template_path->buf); |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 62 | |
| 63 | if (S_ISDIR(st_template.st_mode)) { |
Brandon Williams | fa0fcca | 2018-02-14 10:59:52 -0800 | [diff] [blame] | 64 | DIR *subdir = opendir(template_path->buf); |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 65 | if (!subdir) |
Brandon Williams | fa0fcca | 2018-02-14 10:59:52 -0800 | [diff] [blame] | 66 | die_errno(_("cannot opendir '%s'"), template_path->buf); |
Jeff King | 9c28390 | 2015-10-04 23:46:04 -0400 | [diff] [blame] | 67 | strbuf_addch(path, '/'); |
Brandon Williams | fa0fcca | 2018-02-14 10:59:52 -0800 | [diff] [blame] | 68 | strbuf_addch(template_path, '/'); |
| 69 | copy_templates_1(path, template_path, subdir); |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 70 | closedir(subdir); |
| 71 | } |
| 72 | else if (exists) |
| 73 | continue; |
| 74 | else if (S_ISLNK(st_template.st_mode)) { |
Jeff King | 9c28390 | 2015-10-04 23:46:04 -0400 | [diff] [blame] | 75 | struct strbuf lnk = STRBUF_INIT; |
Jeff King | 765b496 | 2018-07-24 06:51:39 -0400 | [diff] [blame] | 76 | if (strbuf_readlink(&lnk, template_path->buf, |
| 77 | st_template.st_size) < 0) |
Brandon Williams | fa0fcca | 2018-02-14 10:59:52 -0800 | [diff] [blame] | 78 | die_errno(_("cannot readlink '%s'"), template_path->buf); |
Jeff King | 9c28390 | 2015-10-04 23:46:04 -0400 | [diff] [blame] | 79 | if (symlink(lnk.buf, path->buf)) |
| 80 | die_errno(_("cannot symlink '%s' '%s'"), |
| 81 | lnk.buf, path->buf); |
| 82 | strbuf_release(&lnk); |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 83 | } |
| 84 | else if (S_ISREG(st_template.st_mode)) { |
Brandon Williams | fa0fcca | 2018-02-14 10:59:52 -0800 | [diff] [blame] | 85 | if (copy_file(path->buf, template_path->buf, st_template.st_mode)) |
Jeff King | 9c28390 | 2015-10-04 23:46:04 -0400 | [diff] [blame] | 86 | die_errno(_("cannot copy '%s' to '%s'"), |
Brandon Williams | fa0fcca | 2018-02-14 10:59:52 -0800 | [diff] [blame] | 87 | template_path->buf, path->buf); |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 88 | } |
| 89 | else |
Brandon Williams | fa0fcca | 2018-02-14 10:59:52 -0800 | [diff] [blame] | 90 | error(_("ignoring template %s"), template_path->buf); |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
Daniel Barkalow | f225aeb | 2008-04-27 13:39:27 -0400 | [diff] [blame] | 94 | static void copy_templates(const char *template_dir) |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 95 | { |
Jeff King | 9c28390 | 2015-10-04 23:46:04 -0400 | [diff] [blame] | 96 | struct strbuf path = STRBUF_INIT; |
| 97 | struct strbuf template_path = STRBUF_INIT; |
| 98 | size_t template_len; |
Jeff King | 94ce167 | 2016-03-11 17:37:11 -0500 | [diff] [blame] | 99 | struct repository_format template_format; |
| 100 | struct strbuf err = STRBUF_INIT; |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 101 | DIR *dir; |
Junio C Hamano | 59362e5 | 2014-11-24 11:33:54 -0800 | [diff] [blame] | 102 | char *to_free = NULL; |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 103 | |
Johannes Sixt | a47d181 | 2007-11-13 21:05:04 +0100 | [diff] [blame] | 104 | if (!template_dir) |
Junio C Hamano | d4ebc36 | 2006-12-19 01:28:15 -0800 | [diff] [blame] | 105 | template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT); |
Steffen Prohaska | 2de9de5 | 2008-07-13 22:31:18 +0200 | [diff] [blame] | 106 | if (!template_dir) |
Steven Drake | 90b4518 | 2010-02-17 12:42:31 +1300 | [diff] [blame] | 107 | template_dir = init_db_template_dir; |
| 108 | if (!template_dir) |
Junio C Hamano | 59362e5 | 2014-11-24 11:33:54 -0800 | [diff] [blame] | 109 | template_dir = to_free = system_path(DEFAULT_GIT_TEMPLATE_DIR); |
| 110 | if (!template_dir[0]) { |
| 111 | free(to_free); |
Jeff King | 172035f | 2008-07-28 02:02:04 -0400 | [diff] [blame] | 112 | return; |
Junio C Hamano | 59362e5 | 2014-11-24 11:33:54 -0800 | [diff] [blame] | 113 | } |
Jeff King | 9c28390 | 2015-10-04 23:46:04 -0400 | [diff] [blame] | 114 | |
| 115 | strbuf_addstr(&template_path, template_dir); |
| 116 | strbuf_complete(&template_path, '/'); |
| 117 | template_len = template_path.len; |
| 118 | |
| 119 | dir = opendir(template_path.buf); |
Junio C Hamano | d3af621 | 2005-08-06 12:50:14 -0700 | [diff] [blame] | 120 | if (!dir) { |
Robert P. J. Day | 44f560f | 2018-05-29 08:14:35 -0400 | [diff] [blame] | 121 | warning(_("templates not found in %s"), template_dir); |
Junio C Hamano | 59362e5 | 2014-11-24 11:33:54 -0800 | [diff] [blame] | 122 | goto free_return; |
Junio C Hamano | d3af621 | 2005-08-06 12:50:14 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Junio C Hamano | 4f62953 | 2005-11-25 16:03:56 -0800 | [diff] [blame] | 125 | /* Make sure that template is from the correct vintage */ |
Jeff King | 9c28390 | 2015-10-04 23:46:04 -0400 | [diff] [blame] | 126 | strbuf_addstr(&template_path, "config"); |
Jeff King | 94ce167 | 2016-03-11 17:37:11 -0500 | [diff] [blame] | 127 | read_repository_format(&template_format, template_path.buf); |
Jeff King | 9c28390 | 2015-10-04 23:46:04 -0400 | [diff] [blame] | 128 | strbuf_setlen(&template_path, template_len); |
Junio C Hamano | 4f62953 | 2005-11-25 16:03:56 -0800 | [diff] [blame] | 129 | |
Jeff King | 94ce167 | 2016-03-11 17:37:11 -0500 | [diff] [blame] | 130 | /* |
| 131 | * No mention of version at all is OK, but anything else should be |
| 132 | * verified. |
| 133 | */ |
| 134 | if (template_format.version >= 0 && |
| 135 | verify_repository_format(&template_format, &err) < 0) { |
| 136 | warning(_("not copying templates from '%s': %s"), |
| 137 | template_dir, err.buf); |
| 138 | strbuf_release(&err); |
Junio C Hamano | 59362e5 | 2014-11-24 11:33:54 -0800 | [diff] [blame] | 139 | goto close_free_return; |
Junio C Hamano | 4f62953 | 2005-11-25 16:03:56 -0800 | [diff] [blame] | 140 | } |
| 141 | |
Nguyễn Thái Ngọc Duy | fe9aa0b | 2016-09-25 10:14:36 +0700 | [diff] [blame] | 142 | strbuf_addstr(&path, get_git_common_dir()); |
Jeff King | 9c28390 | 2015-10-04 23:46:04 -0400 | [diff] [blame] | 143 | strbuf_complete(&path, '/'); |
| 144 | copy_templates_1(&path, &template_path, dir); |
Junio C Hamano | 59362e5 | 2014-11-24 11:33:54 -0800 | [diff] [blame] | 145 | close_free_return: |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 146 | closedir(dir); |
Junio C Hamano | 59362e5 | 2014-11-24 11:33:54 -0800 | [diff] [blame] | 147 | free_return: |
| 148 | free(to_free); |
Jeff King | 9c28390 | 2015-10-04 23:46:04 -0400 | [diff] [blame] | 149 | strbuf_release(&path); |
| 150 | strbuf_release(&template_path); |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Steven Drake | 90b4518 | 2010-02-17 12:42:31 +1300 | [diff] [blame] | 153 | static int git_init_db_config(const char *k, const char *v, void *cb) |
| 154 | { |
Steven Drake | 90b4518 | 2010-02-17 12:42:31 +1300 | [diff] [blame] | 155 | if (!strcmp(k, "init.templatedir")) |
| 156 | return git_config_pathname(&init_db_template_dir, k, v); |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
Jeff King | 84ccad8 | 2015-04-02 14:37:40 -0400 | [diff] [blame] | 161 | /* |
| 162 | * If the git_dir is not directly inside the working tree, then git will not |
| 163 | * find it by default, and we need to set the worktree explicitly. |
| 164 | */ |
| 165 | static int needs_work_tree_config(const char *git_dir, const char *work_tree) |
| 166 | { |
| 167 | if (!strcmp(work_tree, "/") && !strcmp(git_dir, "/.git")) |
| 168 | return 0; |
| 169 | if (skip_prefix(git_dir, work_tree, &git_dir) && |
| 170 | !strcmp(git_dir, "/.git")) |
| 171 | return 0; |
| 172 | return 1; |
| 173 | } |
| 174 | |
Nguyễn Thái Ngọc Duy | 6311cfa | 2016-09-25 10:14:39 +0700 | [diff] [blame] | 175 | static int create_default_files(const char *template_path, |
| 176 | const char *original_git_dir) |
Linus Torvalds | cad88fd | 2005-05-30 10:20:44 -0700 | [diff] [blame] | 177 | { |
Junio C Hamano | 4f62953 | 2005-11-25 16:03:56 -0800 | [diff] [blame] | 178 | struct stat st1; |
Jeff King | 9c28390 | 2015-10-04 23:46:04 -0400 | [diff] [blame] | 179 | struct strbuf buf = STRBUF_INIT; |
| 180 | char *path; |
Junio C Hamano | 4f62953 | 2005-11-25 16:03:56 -0800 | [diff] [blame] | 181 | char repo_version_string[10]; |
Johannes Schindelin | 5cc8f37 | 2008-03-24 16:14:52 +0100 | [diff] [blame] | 182 | char junk[2]; |
Shawn O. Pearce | ef0a89a | 2006-12-15 00:44:58 -0500 | [diff] [blame] | 183 | int reinit; |
Shawn O. Pearce | c869753 | 2006-12-30 23:53:55 -0500 | [diff] [blame] | 184 | int filemode; |
David Turner | 6fb5acf | 2016-09-04 18:08:41 +0200 | [diff] [blame] | 185 | struct strbuf err = STRBUF_INIT; |
Linus Torvalds | cad88fd | 2005-05-30 10:20:44 -0700 | [diff] [blame] | 186 | |
Steven Drake | 90b4518 | 2010-02-17 12:42:31 +1300 | [diff] [blame] | 187 | /* Just look for `init.templatedir` */ |
| 188 | git_config(git_init_db_config, NULL); |
| 189 | |
Jeff King | 7c0a842 | 2016-09-12 20:24:19 -0700 | [diff] [blame] | 190 | /* |
| 191 | * First copy the templates -- we might have the default |
Junio C Hamano | 4f62953 | 2005-11-25 16:03:56 -0800 | [diff] [blame] | 192 | * config file there, in which case we would want to read |
| 193 | * from it after installing. |
Jeff King | 4543926 | 2016-09-12 20:24:23 -0700 | [diff] [blame] | 194 | * |
| 195 | * Before reading that config, we also need to clear out any cached |
| 196 | * values (since we've just potentially changed what's available on |
| 197 | * disk). |
Junio C Hamano | 4f62953 | 2005-11-25 16:03:56 -0800 | [diff] [blame] | 198 | */ |
Daniel Barkalow | f225aeb | 2008-04-27 13:39:27 -0400 | [diff] [blame] | 199 | copy_templates(template_path); |
Jeff King | 4543926 | 2016-09-12 20:24:23 -0700 | [diff] [blame] | 200 | git_config_clear(); |
| 201 | reset_shared_repository(); |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 202 | git_config(git_default_config, NULL); |
Junio C Hamano | 5a688fe | 2009-03-25 16:19:36 -0700 | [diff] [blame] | 203 | |
Jeff King | 7c0a842 | 2016-09-12 20:24:19 -0700 | [diff] [blame] | 204 | /* |
| 205 | * We must make sure command-line options continue to override any |
| 206 | * values we might have just re-read from the config. |
| 207 | */ |
| 208 | is_bare_repository_cfg = init_is_bare_repository; |
Deskin Miller | 0a2c7ee | 2008-10-07 01:37:48 -0400 | [diff] [blame] | 209 | if (init_shared_repository != -1) |
Jeff King | 7875acb | 2016-03-11 17:36:49 -0500 | [diff] [blame] | 210 | set_shared_repository(init_shared_repository); |
Junio C Hamano | 4f62953 | 2005-11-25 16:03:56 -0800 | [diff] [blame] | 211 | |
Linus Torvalds | cad88fd | 2005-05-30 10:20:44 -0700 | [diff] [blame] | 212 | /* |
Junio C Hamano | 138086a | 2006-06-09 22:07:23 -0700 | [diff] [blame] | 213 | * We would have created the above under user's umask -- under |
| 214 | * shared-repository settings, we would need to fix them up. |
| 215 | */ |
Jeff King | 7875acb | 2016-03-11 17:36:49 -0500 | [diff] [blame] | 216 | if (get_shared_repository()) { |
Daniel Barkalow | f225aeb | 2008-04-27 13:39:27 -0400 | [diff] [blame] | 217 | adjust_shared_perm(get_git_dir()); |
Junio C Hamano | 138086a | 2006-06-09 22:07:23 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | /* |
David Turner | 6fb5acf | 2016-09-04 18:08:41 +0200 | [diff] [blame] | 221 | * We need to create a "refs" dir in any case so that older |
| 222 | * versions of git can tell that this is a repository. |
| 223 | */ |
| 224 | safe_create_dir(git_path("refs"), 1); |
| 225 | adjust_shared_perm(git_path("refs")); |
| 226 | |
| 227 | if (refs_init_db(&err)) |
| 228 | die("failed to set up refs db: %s", err.buf); |
| 229 | |
| 230 | /* |
Linus Torvalds | cad88fd | 2005-05-30 10:20:44 -0700 | [diff] [blame] | 231 | * Create the default symlink from ".git/HEAD" to the "master" |
Junio C Hamano | 8098a17 | 2005-09-30 14:26:57 -0700 | [diff] [blame] | 232 | * branch, if it does not exist yet. |
Linus Torvalds | cad88fd | 2005-05-30 10:20:44 -0700 | [diff] [blame] | 233 | */ |
Jeff King | 9c28390 | 2015-10-04 23:46:04 -0400 | [diff] [blame] | 234 | path = git_path_buf(&buf, "HEAD"); |
Johannes Schindelin | 5cc8f37 | 2008-03-24 16:14:52 +0100 | [diff] [blame] | 235 | reinit = (!access(path, R_OK) |
| 236 | || readlink(path, junk, sizeof(junk)-1) != -1); |
Shawn O. Pearce | ef0a89a | 2006-12-15 00:44:58 -0500 | [diff] [blame] | 237 | if (!reinit) { |
Nicolas Pitre | 8b5157e | 2007-01-26 17:26:10 -0500 | [diff] [blame] | 238 | if (create_symref("HEAD", "refs/heads/master", NULL) < 0) |
Linus Torvalds | cad88fd | 2005-05-30 10:20:44 -0700 | [diff] [blame] | 239 | exit(1); |
Linus Torvalds | cad88fd | 2005-05-30 10:20:44 -0700 | [diff] [blame] | 240 | } |
Junio C Hamano | 4f62953 | 2005-11-25 16:03:56 -0800 | [diff] [blame] | 241 | |
| 242 | /* This forces creation of new config file */ |
Jeff King | 5096d49 | 2015-09-24 17:06:08 -0400 | [diff] [blame] | 243 | xsnprintf(repo_version_string, sizeof(repo_version_string), |
| 244 | "%d", GIT_REPO_VERSION); |
Patrick Steinhardt | 3d18064 | 2016-02-22 12:23:36 +0100 | [diff] [blame] | 245 | git_config_set("core.repositoryformatversion", repo_version_string); |
Junio C Hamano | 4f62953 | 2005-11-25 16:03:56 -0800 | [diff] [blame] | 246 | |
Junio C Hamano | 4f62953 | 2005-11-25 16:03:56 -0800 | [diff] [blame] | 247 | /* Check filemode trustability */ |
Jeff King | 9c28390 | 2015-10-04 23:46:04 -0400 | [diff] [blame] | 248 | path = git_path_buf(&buf, "config"); |
Shawn O. Pearce | c869753 | 2006-12-30 23:53:55 -0500 | [diff] [blame] | 249 | filemode = TEST_FILEMODE; |
| 250 | if (TEST_FILEMODE && !lstat(path, &st1)) { |
Junio C Hamano | 4f62953 | 2005-11-25 16:03:56 -0800 | [diff] [blame] | 251 | struct stat st2; |
Shawn O. Pearce | c869753 | 2006-12-30 23:53:55 -0500 | [diff] [blame] | 252 | filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) && |
Junio C Hamano | 4f62953 | 2005-11-25 16:03:56 -0800 | [diff] [blame] | 253 | !lstat(path, &st2) && |
Michael Haggerty | 1f32ecf | 2014-11-18 14:50:24 +0100 | [diff] [blame] | 254 | st1.st_mode != st2.st_mode && |
| 255 | !chmod(path, st1.st_mode)); |
Torsten Bögershausen | c7bf68d | 2014-11-21 10:34:54 +0100 | [diff] [blame] | 256 | if (filemode && !reinit && (st1.st_mode & S_IXUSR)) |
| 257 | filemode = 0; |
Johannes Schindelin | e24317b | 2005-10-26 01:43:03 +0200 | [diff] [blame] | 258 | } |
Patrick Steinhardt | 3d18064 | 2016-02-22 12:23:36 +0100 | [diff] [blame] | 259 | git_config_set("core.filemode", filemode ? "true" : "false"); |
Shawn O. Pearce | ef0a89a | 2006-12-15 00:44:58 -0500 | [diff] [blame] | 260 | |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 261 | if (is_bare_repository()) |
Patrick Steinhardt | 3d18064 | 2016-02-22 12:23:36 +0100 | [diff] [blame] | 262 | git_config_set("core.bare", "true"); |
Junio C Hamano | 7d1864c | 2007-01-07 02:00:28 -0800 | [diff] [blame] | 263 | else { |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 264 | const char *work_tree = get_git_work_tree(); |
Patrick Steinhardt | 3d18064 | 2016-02-22 12:23:36 +0100 | [diff] [blame] | 265 | git_config_set("core.bare", "false"); |
Alex Riesen | 196055c | 2007-01-23 16:51:18 +0100 | [diff] [blame] | 266 | /* allow template config file to override the default */ |
Cornelius Weig | 341fb28 | 2017-01-27 11:09:47 +0100 | [diff] [blame] | 267 | if (log_all_ref_updates == LOG_REFS_UNSET) |
Patrick Steinhardt | 3d18064 | 2016-02-22 12:23:36 +0100 | [diff] [blame] | 268 | git_config_set("core.logallrefupdates", "true"); |
Nguyễn Thái Ngọc Duy | 6311cfa | 2016-09-25 10:14:39 +0700 | [diff] [blame] | 269 | if (needs_work_tree_config(original_git_dir, work_tree)) |
Patrick Steinhardt | 3d18064 | 2016-02-22 12:23:36 +0100 | [diff] [blame] | 270 | git_config_set("core.worktree", work_tree); |
Junio C Hamano | 7d1864c | 2007-01-07 02:00:28 -0800 | [diff] [blame] | 271 | } |
Junio C Hamano | 75d2449 | 2007-08-31 00:25:04 -0700 | [diff] [blame] | 272 | |
Junio C Hamano | 75d2449 | 2007-08-31 00:25:04 -0700 | [diff] [blame] | 273 | if (!reinit) { |
Dmitry Potapov | 2455406 | 2008-05-11 18:16:39 +0200 | [diff] [blame] | 274 | /* Check if symlink is supported in the work tree */ |
Jeff King | 9c28390 | 2015-10-04 23:46:04 -0400 | [diff] [blame] | 275 | path = git_path_buf(&buf, "tXXXXXX"); |
Junio C Hamano | 75d2449 | 2007-08-31 00:25:04 -0700 | [diff] [blame] | 276 | if (!close(xmkstemp(path)) && |
| 277 | !unlink(path) && |
| 278 | !symlink("testing", path) && |
| 279 | !lstat(path, &st1) && |
| 280 | S_ISLNK(st1.st_mode)) |
| 281 | unlink(path); /* good */ |
| 282 | else |
Patrick Steinhardt | 3d18064 | 2016-02-22 12:23:36 +0100 | [diff] [blame] | 283 | git_config_set("core.symlinks", "false"); |
Dmitry Potapov | 2455406 | 2008-05-11 18:16:39 +0200 | [diff] [blame] | 284 | |
| 285 | /* Check if the filesystem is case-insensitive */ |
Jeff King | 9c28390 | 2015-10-04 23:46:04 -0400 | [diff] [blame] | 286 | path = git_path_buf(&buf, "CoNfIg"); |
Dmitry Potapov | 2455406 | 2008-05-11 18:16:39 +0200 | [diff] [blame] | 287 | if (!access(path, F_OK)) |
Patrick Steinhardt | 3d18064 | 2016-02-22 12:23:36 +0100 | [diff] [blame] | 288 | git_config_set("core.ignorecase", "true"); |
Jeff King | fdf7296 | 2015-10-04 23:45:26 -0400 | [diff] [blame] | 289 | probe_utf8_pathname_composition(); |
Junio C Hamano | 75d2449 | 2007-08-31 00:25:04 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Jeff King | 9c28390 | 2015-10-04 23:46:04 -0400 | [diff] [blame] | 292 | strbuf_release(&buf); |
Shawn O. Pearce | ef0a89a | 2006-12-15 00:44:58 -0500 | [diff] [blame] | 293 | return reinit; |
Linus Torvalds | cad88fd | 2005-05-30 10:20:44 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Jonathan Nieder | 9173912 | 2010-10-03 23:34:27 -0500 | [diff] [blame] | 296 | static void create_object_directory(void) |
| 297 | { |
Jeff King | 9c28390 | 2015-10-04 23:46:04 -0400 | [diff] [blame] | 298 | struct strbuf path = STRBUF_INIT; |
| 299 | size_t baselen; |
Jonathan Nieder | 9173912 | 2010-10-03 23:34:27 -0500 | [diff] [blame] | 300 | |
Jeff King | 9c28390 | 2015-10-04 23:46:04 -0400 | [diff] [blame] | 301 | strbuf_addstr(&path, get_object_directory()); |
| 302 | baselen = path.len; |
Jonathan Nieder | 9173912 | 2010-10-03 23:34:27 -0500 | [diff] [blame] | 303 | |
Jeff King | 9c28390 | 2015-10-04 23:46:04 -0400 | [diff] [blame] | 304 | safe_create_dir(path.buf, 1); |
Jonathan Nieder | 9173912 | 2010-10-03 23:34:27 -0500 | [diff] [blame] | 305 | |
Jeff King | 9c28390 | 2015-10-04 23:46:04 -0400 | [diff] [blame] | 306 | strbuf_setlen(&path, baselen); |
| 307 | strbuf_addstr(&path, "/pack"); |
| 308 | safe_create_dir(path.buf, 1); |
| 309 | |
| 310 | strbuf_setlen(&path, baselen); |
| 311 | strbuf_addstr(&path, "/info"); |
| 312 | safe_create_dir(path.buf, 1); |
| 313 | |
| 314 | strbuf_release(&path); |
Jonathan Nieder | 9173912 | 2010-10-03 23:34:27 -0500 | [diff] [blame] | 315 | } |
| 316 | |
Nguyễn Thái Ngọc Duy | 822d940 | 2016-09-25 10:14:40 +0700 | [diff] [blame] | 317 | static void separate_git_dir(const char *git_dir, const char *git_link) |
Nguyễn Thái Ngọc Duy | b57fb80 | 2011-03-19 22:16:56 +0700 | [diff] [blame] | 318 | { |
| 319 | struct stat st; |
Nguyễn Thái Ngọc Duy | b57fb80 | 2011-03-19 22:16:56 +0700 | [diff] [blame] | 320 | |
| 321 | if (!stat(git_link, &st)) { |
| 322 | const char *src; |
| 323 | |
| 324 | if (S_ISREG(st.st_mode)) |
Junio C Hamano | 13d6ec9 | 2011-08-22 14:04:56 -0700 | [diff] [blame] | 325 | src = read_gitfile(git_link); |
Nguyễn Thái Ngọc Duy | b57fb80 | 2011-03-19 22:16:56 +0700 | [diff] [blame] | 326 | else if (S_ISDIR(st.st_mode)) |
| 327 | src = git_link; |
| 328 | else |
Ævar Arnfjörð Bjarmason | 97f261b | 2011-12-20 23:27:41 +0000 | [diff] [blame] | 329 | die(_("unable to handle file type %d"), (int)st.st_mode); |
Nguyễn Thái Ngọc Duy | b57fb80 | 2011-03-19 22:16:56 +0700 | [diff] [blame] | 330 | |
| 331 | if (rename(src, git_dir)) |
Ævar Arnfjörð Bjarmason | 2c050e0 | 2011-04-10 19:34:02 +0000 | [diff] [blame] | 332 | die_errno(_("unable to move %s to %s"), src, git_dir); |
Nguyễn Thái Ngọc Duy | b57fb80 | 2011-03-19 22:16:56 +0700 | [diff] [blame] | 333 | } |
| 334 | |
Junio C Hamano | 1f76a10 | 2015-08-24 13:20:39 -0700 | [diff] [blame] | 335 | write_file(git_link, "gitdir: %s", git_dir); |
Nguyễn Thái Ngọc Duy | b57fb80 | 2011-03-19 22:16:56 +0700 | [diff] [blame] | 336 | } |
| 337 | |
Nguyễn Thái Ngọc Duy | 3315870 | 2016-09-25 10:14:37 +0700 | [diff] [blame] | 338 | int init_db(const char *git_dir, const char *real_git_dir, |
| 339 | const char *template_dir, unsigned int flags) |
Junio C Hamano | 6adcca3 | 2007-08-27 00:58:06 -0700 | [diff] [blame] | 340 | { |
Jonathan Nieder | 9173912 | 2010-10-03 23:34:27 -0500 | [diff] [blame] | 341 | int reinit; |
Nguyễn Thái Ngọc Duy | 1bd1907 | 2016-09-25 10:14:38 +0700 | [diff] [blame] | 342 | int exist_ok = flags & INIT_DB_EXIST_OK; |
Johannes Schindelin | ce83ead | 2017-03-08 16:43:40 +0100 | [diff] [blame] | 343 | char *original_git_dir = real_pathdup(git_dir, 1); |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 344 | |
Nguyễn Thái Ngọc Duy | 1bd1907 | 2016-09-25 10:14:38 +0700 | [diff] [blame] | 345 | if (real_git_dir) { |
| 346 | struct stat st; |
Nguyễn Thái Ngọc Duy | 3315870 | 2016-09-25 10:14:37 +0700 | [diff] [blame] | 347 | |
Nguyễn Thái Ngọc Duy | 1bd1907 | 2016-09-25 10:14:38 +0700 | [diff] [blame] | 348 | if (!exist_ok && !stat(git_dir, &st)) |
| 349 | die(_("%s already exists"), git_dir); |
| 350 | |
| 351 | if (!exist_ok && !stat(real_git_dir, &st)) |
| 352 | die(_("%s already exists"), real_git_dir); |
| 353 | |
Nguyễn Thái Ngọc Duy | 1bd1907 | 2016-09-25 10:14:38 +0700 | [diff] [blame] | 354 | set_git_dir(real_path(real_git_dir)); |
Nguyễn Thái Ngọc Duy | 822d940 | 2016-09-25 10:14:40 +0700 | [diff] [blame] | 355 | git_dir = get_git_dir(); |
| 356 | separate_git_dir(git_dir, original_git_dir); |
Nguyễn Thái Ngọc Duy | 1bd1907 | 2016-09-25 10:14:38 +0700 | [diff] [blame] | 357 | } |
| 358 | else { |
| 359 | set_git_dir(real_path(git_dir)); |
Nguyễn Thái Ngọc Duy | 822d940 | 2016-09-25 10:14:40 +0700 | [diff] [blame] | 360 | git_dir = get_git_dir(); |
Nguyễn Thái Ngọc Duy | 1bd1907 | 2016-09-25 10:14:38 +0700 | [diff] [blame] | 361 | } |
| 362 | startup_info->have_repository = 1; |
Nguyễn Thái Ngọc Duy | b57fb80 | 2011-03-19 22:16:56 +0700 | [diff] [blame] | 363 | |
| 364 | safe_create_dir(git_dir, 0); |
Junio C Hamano | 4f62953 | 2005-11-25 16:03:56 -0800 | [diff] [blame] | 365 | |
Deskin Miller | 0a2c7ee | 2008-10-07 01:37:48 -0400 | [diff] [blame] | 366 | init_is_bare_repository = is_bare_repository(); |
| 367 | |
Junio C Hamano | 4f62953 | 2005-11-25 16:03:56 -0800 | [diff] [blame] | 368 | /* Check to see if the repository version is right. |
| 369 | * Note that a newly created repository does not have |
| 370 | * config file, so this will not fail. What we are catching |
| 371 | * is an attempt to reinitialize new repository with an old tool. |
| 372 | */ |
| 373 | check_repository_format(); |
| 374 | |
Nguyễn Thái Ngọc Duy | 6311cfa | 2016-09-25 10:14:39 +0700 | [diff] [blame] | 375 | reinit = create_default_files(template_dir, original_git_dir); |
Linus Torvalds | cad88fd | 2005-05-30 10:20:44 -0700 | [diff] [blame] | 376 | |
Jonathan Nieder | 9173912 | 2010-10-03 23:34:27 -0500 | [diff] [blame] | 377 | create_object_directory(); |
Johannes Schindelin | af6e277 | 2005-12-22 23:19:37 +0100 | [diff] [blame] | 378 | |
Jeff King | 7875acb | 2016-03-11 17:36:49 -0500 | [diff] [blame] | 379 | if (get_shared_repository()) { |
Junio C Hamano | 94df250 | 2006-06-09 23:09:49 -0700 | [diff] [blame] | 380 | char buf[10]; |
| 381 | /* We do not spell "group" and such, so that |
| 382 | * the configuration can be read by older version |
Heikki Orsila | 06cbe85 | 2008-04-16 11:34:24 +0300 | [diff] [blame] | 383 | * of git. Note, we use octal numbers for new share modes, |
| 384 | * and compatibility values for PERM_GROUP and |
| 385 | * PERM_EVERYBODY. |
Junio C Hamano | 94df250 | 2006-06-09 23:09:49 -0700 | [diff] [blame] | 386 | */ |
Jeff King | 7875acb | 2016-03-11 17:36:49 -0500 | [diff] [blame] | 387 | if (get_shared_repository() < 0) |
Junio C Hamano | 5a688fe | 2009-03-25 16:19:36 -0700 | [diff] [blame] | 388 | /* force to the mode value */ |
Jeff King | 7875acb | 2016-03-11 17:36:49 -0500 | [diff] [blame] | 389 | xsnprintf(buf, sizeof(buf), "0%o", -get_shared_repository()); |
| 390 | else if (get_shared_repository() == PERM_GROUP) |
Jeff King | 5096d49 | 2015-09-24 17:06:08 -0400 | [diff] [blame] | 391 | xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_GROUP); |
Jeff King | 7875acb | 2016-03-11 17:36:49 -0500 | [diff] [blame] | 392 | else if (get_shared_repository() == PERM_EVERYBODY) |
Jeff King | 5096d49 | 2015-09-24 17:06:08 -0400 | [diff] [blame] | 393 | xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_EVERYBODY); |
Heikki Orsila | 06cbe85 | 2008-04-16 11:34:24 +0300 | [diff] [blame] | 394 | else |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 395 | BUG("invalid value for shared_repository"); |
Patrick Steinhardt | 3d18064 | 2016-02-22 12:23:36 +0100 | [diff] [blame] | 396 | git_config_set("core.sharedrepository", buf); |
| 397 | git_config_set("receive.denyNonFastforwards", "true"); |
Junio C Hamano | 94df250 | 2006-06-09 23:09:49 -0700 | [diff] [blame] | 398 | } |
Johannes Schindelin | af6e277 | 2005-12-22 23:19:37 +0100 | [diff] [blame] | 399 | |
Nguyễn Thái Ngọc Duy | d06f15d | 2010-02-14 22:44:42 +0700 | [diff] [blame] | 400 | if (!(flags & INIT_DB_QUIET)) { |
Nguyễn Thái Ngọc Duy | d06f15d | 2010-02-14 22:44:42 +0700 | [diff] [blame] | 401 | int len = strlen(git_dir); |
Ævar Arnfjörð Bjarmason | 3e5dd7e | 2011-02-22 23:41:25 +0000 | [diff] [blame] | 402 | |
Vasco Almeida | c30364d | 2016-06-17 21:54:11 +0000 | [diff] [blame] | 403 | if (reinit) |
| 404 | printf(get_shared_repository() |
| 405 | ? _("Reinitialized existing shared Git repository in %s%s\n") |
| 406 | : _("Reinitialized existing Git repository in %s%s\n"), |
| 407 | git_dir, len && git_dir[len-1] != '/' ? "/" : ""); |
| 408 | else |
| 409 | printf(get_shared_repository() |
| 410 | ? _("Initialized empty shared Git repository in %s%s\n") |
| 411 | : _("Initialized empty Git repository in %s%s\n"), |
| 412 | git_dir, len && git_dir[len-1] != '/' ? "/" : ""); |
Nguyễn Thái Ngọc Duy | d06f15d | 2010-02-14 22:44:42 +0700 | [diff] [blame] | 413 | } |
Shawn O. Pearce | ef0a89a | 2006-12-15 00:44:58 -0500 | [diff] [blame] | 414 | |
Nguyễn Thái Ngọc Duy | 6311cfa | 2016-09-25 10:14:39 +0700 | [diff] [blame] | 415 | free(original_git_dir); |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 416 | return 0; |
| 417 | } |
Daniel Barkalow | f225aeb | 2008-04-27 13:39:27 -0400 | [diff] [blame] | 418 | |
| 419 | static int guess_repository_type(const char *git_dir) |
| 420 | { |
Daniel Barkalow | f225aeb | 2008-04-27 13:39:27 -0400 | [diff] [blame] | 421 | const char *slash; |
René Scharfe | 56b9f6e | 2014-07-28 20:30:39 +0200 | [diff] [blame] | 422 | char *cwd; |
| 423 | int cwd_is_git_dir; |
Daniel Barkalow | f225aeb | 2008-04-27 13:39:27 -0400 | [diff] [blame] | 424 | |
| 425 | /* |
| 426 | * "GIT_DIR=. git init" is always bare. |
| 427 | * "GIT_DIR=`pwd` git init" too. |
| 428 | */ |
| 429 | if (!strcmp(".", git_dir)) |
| 430 | return 1; |
René Scharfe | 56b9f6e | 2014-07-28 20:30:39 +0200 | [diff] [blame] | 431 | cwd = xgetcwd(); |
| 432 | cwd_is_git_dir = !strcmp(git_dir, cwd); |
| 433 | free(cwd); |
| 434 | if (cwd_is_git_dir) |
Daniel Barkalow | f225aeb | 2008-04-27 13:39:27 -0400 | [diff] [blame] | 435 | return 1; |
| 436 | /* |
| 437 | * "GIT_DIR=.git or GIT_DIR=something/.git is usually not. |
| 438 | */ |
| 439 | if (!strcmp(git_dir, ".git")) |
| 440 | return 0; |
| 441 | slash = strrchr(git_dir, '/'); |
| 442 | if (slash && !strcmp(slash, "/.git")) |
| 443 | return 0; |
| 444 | |
| 445 | /* |
| 446 | * Otherwise it is often bare. At this point |
| 447 | * we are just guessing. |
| 448 | */ |
| 449 | return 1; |
| 450 | } |
| 451 | |
Michał Kiedrowicz | 596f91e | 2009-07-12 12:24:32 +0200 | [diff] [blame] | 452 | static int shared_callback(const struct option *opt, const char *arg, int unset) |
| 453 | { |
| 454 | *((int *) opt->value) = (arg) ? git_config_perm("arg", arg) : PERM_GROUP; |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | static const char *const init_db_usage[] = { |
Alex Henrie | 9c9b4f2 | 2015-01-13 00:44:47 -0700 | [diff] [blame] | 459 | N_("git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]] [<directory>]"), |
Michał Kiedrowicz | 596f91e | 2009-07-12 12:24:32 +0200 | [diff] [blame] | 460 | NULL |
| 461 | }; |
Daniel Barkalow | f225aeb | 2008-04-27 13:39:27 -0400 | [diff] [blame] | 462 | |
| 463 | /* |
| 464 | * If you want to, you can share the DB area with any number of branches. |
| 465 | * That has advantages: you can save space by sharing all the SHA1 objects. |
| 466 | * On the other hand, it might just make lookup slower and messier. You |
| 467 | * be the judge. The default case is to have one DB per managed directory. |
| 468 | */ |
| 469 | int cmd_init_db(int argc, const char **argv, const char *prefix) |
| 470 | { |
| 471 | const char *git_dir; |
Nguyễn Thái Ngọc Duy | b57fb80 | 2011-03-19 22:16:56 +0700 | [diff] [blame] | 472 | const char *real_git_dir = NULL; |
Nguyễn Thái Ngọc Duy | 8351836 | 2010-11-26 22:32:40 +0700 | [diff] [blame] | 473 | const char *work_tree; |
Daniel Barkalow | f225aeb | 2008-04-27 13:39:27 -0400 | [diff] [blame] | 474 | const char *template_dir = NULL; |
| 475 | unsigned int flags = 0; |
Michał Kiedrowicz | 596f91e | 2009-07-12 12:24:32 +0200 | [diff] [blame] | 476 | const struct option init_db_options[] = { |
Nguyễn Thái Ngọc Duy | ce4a5e5 | 2012-08-20 19:32:18 +0700 | [diff] [blame] | 477 | OPT_STRING(0, "template", &template_dir, N_("template-directory"), |
| 478 | N_("directory from which templates will be used")), |
Michał Kiedrowicz | 596f91e | 2009-07-12 12:24:32 +0200 | [diff] [blame] | 479 | OPT_SET_INT(0, "bare", &is_bare_repository_cfg, |
Nguyễn Thái Ngọc Duy | ce4a5e5 | 2012-08-20 19:32:18 +0700 | [diff] [blame] | 480 | N_("create a bare repository"), 1), |
Michał Kiedrowicz | 596f91e | 2009-07-12 12:24:32 +0200 | [diff] [blame] | 481 | { OPTION_CALLBACK, 0, "shared", &init_shared_repository, |
Nguyễn Thái Ngọc Duy | ce4a5e5 | 2012-08-20 19:32:18 +0700 | [diff] [blame] | 482 | N_("permissions"), |
| 483 | N_("specify that the git repository is to be shared amongst several users"), |
Michał Kiedrowicz | 596f91e | 2009-07-12 12:24:32 +0200 | [diff] [blame] | 484 | PARSE_OPT_OPTARG | PARSE_OPT_NONEG, shared_callback, 0}, |
Nguyễn Thái Ngọc Duy | ce4a5e5 | 2012-08-20 19:32:18 +0700 | [diff] [blame] | 485 | OPT_BIT('q', "quiet", &flags, N_("be quiet"), INIT_DB_QUIET), |
| 486 | OPT_STRING(0, "separate-git-dir", &real_git_dir, N_("gitdir"), |
| 487 | N_("separate git dir from working tree")), |
Michał Kiedrowicz | 596f91e | 2009-07-12 12:24:32 +0200 | [diff] [blame] | 488 | OPT_END() |
| 489 | }; |
Daniel Barkalow | f225aeb | 2008-04-27 13:39:27 -0400 | [diff] [blame] | 490 | |
Junio C Hamano | 0397ff2 | 2009-08-05 12:39:33 -0700 | [diff] [blame] | 491 | argc = parse_options(argc, argv, prefix, init_db_options, init_db_usage, 0); |
Michał Kiedrowicz | 596f91e | 2009-07-12 12:24:32 +0200 | [diff] [blame] | 492 | |
Nguyễn Thái Ngọc Duy | b57fb80 | 2011-03-19 22:16:56 +0700 | [diff] [blame] | 493 | if (real_git_dir && !is_absolute_path(real_git_dir)) |
Johannes Schindelin | ce83ead | 2017-03-08 16:43:40 +0100 | [diff] [blame] | 494 | real_git_dir = real_pathdup(real_git_dir, 1); |
Nguyễn Thái Ngọc Duy | b57fb80 | 2011-03-19 22:16:56 +0700 | [diff] [blame] | 495 | |
Junio C Hamano | 0397ff2 | 2009-08-05 12:39:33 -0700 | [diff] [blame] | 496 | if (argc == 1) { |
Nanako Shiraishi | 53d4888 | 2009-07-25 06:59:28 +0900 | [diff] [blame] | 497 | int mkdir_tried = 0; |
| 498 | retry: |
Junio C Hamano | 0397ff2 | 2009-08-05 12:39:33 -0700 | [diff] [blame] | 499 | if (chdir(argv[0]) < 0) { |
Nanako Shiraishi | 53d4888 | 2009-07-25 06:59:28 +0900 | [diff] [blame] | 500 | if (!mkdir_tried) { |
| 501 | int saved; |
| 502 | /* |
| 503 | * At this point we haven't read any configuration, |
| 504 | * and we know shared_repository should always be 0; |
| 505 | * but just in case we play safe. |
| 506 | */ |
Jeff King | 7875acb | 2016-03-11 17:36:49 -0500 | [diff] [blame] | 507 | saved = get_shared_repository(); |
| 508 | set_shared_repository(0); |
Junio C Hamano | 0397ff2 | 2009-08-05 12:39:33 -0700 | [diff] [blame] | 509 | switch (safe_create_leading_directories_const(argv[0])) { |
Michael Haggerty | f3565c0 | 2014-01-06 14:45:26 +0100 | [diff] [blame] | 510 | case SCLD_OK: |
| 511 | case SCLD_PERMS: |
| 512 | break; |
Michael Haggerty | 0be0521 | 2014-01-06 14:45:25 +0100 | [diff] [blame] | 513 | case SCLD_EXISTS: |
Nanako Shiraishi | 53d4888 | 2009-07-25 06:59:28 +0900 | [diff] [blame] | 514 | errno = EEXIST; |
| 515 | /* fallthru */ |
Nanako Shiraishi | 53d4888 | 2009-07-25 06:59:28 +0900 | [diff] [blame] | 516 | default: |
Michael Haggerty | f3565c0 | 2014-01-06 14:45:26 +0100 | [diff] [blame] | 517 | die_errno(_("cannot mkdir %s"), argv[0]); |
Nanako Shiraishi | 53d4888 | 2009-07-25 06:59:28 +0900 | [diff] [blame] | 518 | break; |
| 519 | } |
Jeff King | 7875acb | 2016-03-11 17:36:49 -0500 | [diff] [blame] | 520 | set_shared_repository(saved); |
Junio C Hamano | 0397ff2 | 2009-08-05 12:39:33 -0700 | [diff] [blame] | 521 | if (mkdir(argv[0], 0777) < 0) |
Ævar Arnfjörð Bjarmason | 33e92e4 | 2011-02-22 23:41:24 +0000 | [diff] [blame] | 522 | die_errno(_("cannot mkdir %s"), argv[0]); |
Nanako Shiraishi | 53d4888 | 2009-07-25 06:59:28 +0900 | [diff] [blame] | 523 | mkdir_tried = 1; |
| 524 | goto retry; |
| 525 | } |
Ævar Arnfjörð Bjarmason | 33e92e4 | 2011-02-22 23:41:24 +0000 | [diff] [blame] | 526 | die_errno(_("cannot chdir to %s"), argv[0]); |
Nanako Shiraishi | 53d4888 | 2009-07-25 06:59:28 +0900 | [diff] [blame] | 527 | } |
Junio C Hamano | 0397ff2 | 2009-08-05 12:39:33 -0700 | [diff] [blame] | 528 | } else if (0 < argc) { |
| 529 | usage(init_db_usage[0]); |
Nanako Shiraishi | 53d4888 | 2009-07-25 06:59:28 +0900 | [diff] [blame] | 530 | } |
Junio C Hamano | 0397ff2 | 2009-08-05 12:39:33 -0700 | [diff] [blame] | 531 | if (is_bare_repository_cfg == 1) { |
René Scharfe | 4d3ab44 | 2014-07-28 20:31:57 +0200 | [diff] [blame] | 532 | char *cwd = xgetcwd(); |
| 533 | setenv(GIT_DIR_ENVIRONMENT, cwd, argc > 0); |
| 534 | free(cwd); |
Daniel Barkalow | f225aeb | 2008-04-27 13:39:27 -0400 | [diff] [blame] | 535 | } |
| 536 | |
Junio C Hamano | 5a688fe | 2009-03-25 16:19:36 -0700 | [diff] [blame] | 537 | if (init_shared_repository != -1) |
Jeff King | 7875acb | 2016-03-11 17:36:49 -0500 | [diff] [blame] | 538 | set_shared_repository(init_shared_repository); |
Junio C Hamano | 5a688fe | 2009-03-25 16:19:36 -0700 | [diff] [blame] | 539 | |
Daniel Barkalow | f225aeb | 2008-04-27 13:39:27 -0400 | [diff] [blame] | 540 | /* |
| 541 | * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR |
| 542 | * without --bare. Catch the error early. |
| 543 | */ |
| 544 | git_dir = getenv(GIT_DIR_ENVIRONMENT); |
Nguyễn Thái Ngọc Duy | 8351836 | 2010-11-26 22:32:40 +0700 | [diff] [blame] | 545 | work_tree = getenv(GIT_WORK_TREE_ENVIRONMENT); |
| 546 | if ((!git_dir || is_bare_repository_cfg == 1) && work_tree) |
Ævar Arnfjörð Bjarmason | 33e92e4 | 2011-02-22 23:41:24 +0000 | [diff] [blame] | 547 | die(_("%s (or --work-tree=<directory>) not allowed without " |
| 548 | "specifying %s (or --git-dir=<directory>)"), |
Daniel Barkalow | f225aeb | 2008-04-27 13:39:27 -0400 | [diff] [blame] | 549 | GIT_WORK_TREE_ENVIRONMENT, |
| 550 | GIT_DIR_ENVIRONMENT); |
| 551 | |
| 552 | /* |
| 553 | * Set up the default .git directory contents |
| 554 | */ |
| 555 | if (!git_dir) |
| 556 | git_dir = DEFAULT_GIT_DIR_ENVIRONMENT; |
| 557 | |
| 558 | if (is_bare_repository_cfg < 0) |
| 559 | is_bare_repository_cfg = guess_repository_type(git_dir); |
| 560 | |
| 561 | if (!is_bare_repository_cfg) { |
Nguyễn Thái Ngọc Duy | b31d202 | 2011-03-03 19:34:51 +0700 | [diff] [blame] | 562 | const char *git_dir_parent = strrchr(git_dir, '/'); |
| 563 | if (git_dir_parent) { |
| 564 | char *rel = xstrndup(git_dir, git_dir_parent - git_dir); |
Johannes Schindelin | ce83ead | 2017-03-08 16:43:40 +0100 | [diff] [blame] | 565 | git_work_tree_cfg = real_pathdup(rel, 1); |
Nguyễn Thái Ngọc Duy | b31d202 | 2011-03-03 19:34:51 +0700 | [diff] [blame] | 566 | free(rel); |
Daniel Barkalow | f225aeb | 2008-04-27 13:39:27 -0400 | [diff] [blame] | 567 | } |
René Scharfe | 56b9f6e | 2014-07-28 20:30:39 +0200 | [diff] [blame] | 568 | if (!git_work_tree_cfg) |
| 569 | git_work_tree_cfg = xgetcwd(); |
Nguyễn Thái Ngọc Duy | 8351836 | 2010-11-26 22:32:40 +0700 | [diff] [blame] | 570 | if (work_tree) |
René Scharfe | 2d186c8 | 2014-07-28 20:42:05 +0200 | [diff] [blame] | 571 | set_git_work_tree(work_tree); |
Nguyễn Thái Ngọc Duy | 8351836 | 2010-11-26 22:32:40 +0700 | [diff] [blame] | 572 | else |
| 573 | set_git_work_tree(git_work_tree_cfg); |
Daniel Barkalow | f225aeb | 2008-04-27 13:39:27 -0400 | [diff] [blame] | 574 | if (access(get_git_work_tree(), X_OK)) |
Ævar Arnfjörð Bjarmason | 33e92e4 | 2011-02-22 23:41:24 +0000 | [diff] [blame] | 575 | die_errno (_("Cannot access work tree '%s'"), |
Thomas Rast | 0721c31 | 2009-06-27 17:58:47 +0200 | [diff] [blame] | 576 | get_git_work_tree()); |
Daniel Barkalow | f225aeb | 2008-04-27 13:39:27 -0400 | [diff] [blame] | 577 | } |
Nguyễn Thái Ngọc Duy | 8351836 | 2010-11-26 22:32:40 +0700 | [diff] [blame] | 578 | else { |
| 579 | if (work_tree) |
René Scharfe | 2d186c8 | 2014-07-28 20:42:05 +0200 | [diff] [blame] | 580 | set_git_work_tree(work_tree); |
Nguyễn Thái Ngọc Duy | 8351836 | 2010-11-26 22:32:40 +0700 | [diff] [blame] | 581 | } |
Daniel Barkalow | f225aeb | 2008-04-27 13:39:27 -0400 | [diff] [blame] | 582 | |
Jeff King | 0e5bba5 | 2017-09-08 02:38:41 -0400 | [diff] [blame] | 583 | UNLEAK(real_git_dir); |
| 584 | |
Nguyễn Thái Ngọc Duy | 3315870 | 2016-09-25 10:14:37 +0700 | [diff] [blame] | 585 | flags |= INIT_DB_EXIST_OK; |
| 586 | return init_db(git_dir, real_git_dir, template_dir, flags); |
Daniel Barkalow | f225aeb | 2008-04-27 13:39:27 -0400 | [diff] [blame] | 587 | } |