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" |
Timo Hirvonen | c3c8835 | 2006-05-19 13:03:57 +0300 | [diff] [blame] | 7 | #include "builtin.h" |
Johannes Sixt | a47d181 | 2007-11-13 21:05:04 +0100 | [diff] [blame] | 8 | #include "exec_cmd.h" |
Michał Kiedrowicz | 596f91e | 2009-07-12 12:24:32 +0200 | [diff] [blame] | 9 | #include "parse-options.h" |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 10 | |
Junio C Hamano | d3af621 | 2005-08-06 12:50:14 -0700 | [diff] [blame] | 11 | #ifndef DEFAULT_GIT_TEMPLATE_DIR |
Johannes Sixt | d52fd42 | 2007-06-11 11:10:47 +0200 | [diff] [blame] | 12 | #define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates" |
Junio C Hamano | d3af621 | 2005-08-06 12:50:14 -0700 | [diff] [blame] | 13 | #endif |
| 14 | |
Shawn O. Pearce | c869753 | 2006-12-30 23:53:55 -0500 | [diff] [blame] | 15 | #ifdef NO_TRUSTABLE_FILEMODE |
| 16 | #define TEST_FILEMODE 0 |
| 17 | #else |
| 18 | #define TEST_FILEMODE 1 |
| 19 | #endif |
| 20 | |
Deskin Miller | 0a2c7ee | 2008-10-07 01:37:48 -0400 | [diff] [blame] | 21 | static int init_is_bare_repository = 0; |
| 22 | static int init_shared_repository = -1; |
| 23 | |
Johannes Schindelin | af6e277 | 2005-12-22 23:19:37 +0100 | [diff] [blame] | 24 | static void safe_create_dir(const char *dir, int share) |
Zach Welch | cb126d8 | 2005-04-19 21:48:15 -0700 | [diff] [blame] | 25 | { |
Junio C Hamano | f312de0 | 2005-07-06 01:21:46 -0700 | [diff] [blame] | 26 | if (mkdir(dir, 0777) < 0) { |
Zach Welch | cb126d8 | 2005-04-19 21:48:15 -0700 | [diff] [blame] | 27 | if (errno != EEXIST) { |
| 28 | perror(dir); |
| 29 | exit(1); |
| 30 | } |
| 31 | } |
Johannes Schindelin | af6e277 | 2005-12-22 23:19:37 +0100 | [diff] [blame] | 32 | else if (share && adjust_shared_perm(dir)) |
Alexander Potashev | d753070 | 2009-01-04 21:38:41 +0300 | [diff] [blame] | 33 | die("Could not make %s writable by group", dir); |
Zach Welch | cb126d8 | 2005-04-19 21:48:15 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 36 | static void copy_templates_1(char *path, int baselen, |
| 37 | char *template, int template_baselen, |
| 38 | DIR *dir) |
| 39 | { |
| 40 | struct dirent *de; |
| 41 | |
| 42 | /* Note: if ".git/hooks" file exists in the repository being |
| 43 | * re-initialized, /etc/core-git/templates/hooks/update would |
Heikki Orsila | f18d244 | 2008-09-13 20:18:36 +0300 | [diff] [blame] | 44 | * 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] | 45 | * it means that the set of templates we ship by default, along |
| 46 | * with the way the namespace under .git/ is organized, should |
| 47 | * be really carefully chosen. |
| 48 | */ |
Johannes Schindelin | af6e277 | 2005-12-22 23:19:37 +0100 | [diff] [blame] | 49 | safe_create_dir(path, 1); |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 50 | while ((de = readdir(dir)) != NULL) { |
| 51 | struct stat st_git, st_template; |
| 52 | int namelen; |
| 53 | int exists = 0; |
| 54 | |
| 55 | if (de->d_name[0] == '.') |
| 56 | continue; |
| 57 | namelen = strlen(de->d_name); |
| 58 | if ((PATH_MAX <= baselen + namelen) || |
| 59 | (PATH_MAX <= template_baselen + namelen)) |
| 60 | die("insanely long template name %s", de->d_name); |
| 61 | memcpy(path + baselen, de->d_name, namelen+1); |
| 62 | memcpy(template + template_baselen, de->d_name, namelen+1); |
| 63 | if (lstat(path, &st_git)) { |
| 64 | if (errno != ENOENT) |
Thomas Rast | 0721c31 | 2009-06-27 17:58:47 +0200 | [diff] [blame] | 65 | die_errno("cannot stat '%s'", path); |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 66 | } |
| 67 | else |
| 68 | exists = 1; |
| 69 | |
| 70 | if (lstat(template, &st_template)) |
Thomas Rast | 0721c31 | 2009-06-27 17:58:47 +0200 | [diff] [blame] | 71 | die_errno("cannot stat template '%s'", template); |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 72 | |
| 73 | if (S_ISDIR(st_template.st_mode)) { |
| 74 | DIR *subdir = opendir(template); |
| 75 | int baselen_sub = baselen + namelen; |
| 76 | int template_baselen_sub = template_baselen + namelen; |
| 77 | if (!subdir) |
Thomas Rast | 0721c31 | 2009-06-27 17:58:47 +0200 | [diff] [blame] | 78 | die_errno("cannot opendir '%s'", template); |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 79 | path[baselen_sub++] = |
| 80 | template[template_baselen_sub++] = '/'; |
| 81 | path[baselen_sub] = |
| 82 | template[template_baselen_sub] = 0; |
| 83 | copy_templates_1(path, baselen_sub, |
| 84 | template, template_baselen_sub, |
| 85 | subdir); |
| 86 | closedir(subdir); |
| 87 | } |
| 88 | else if (exists) |
| 89 | continue; |
| 90 | else if (S_ISLNK(st_template.st_mode)) { |
| 91 | char lnk[256]; |
| 92 | int len; |
| 93 | len = readlink(template, lnk, sizeof(lnk)); |
| 94 | if (len < 0) |
Thomas Rast | 0721c31 | 2009-06-27 17:58:47 +0200 | [diff] [blame] | 95 | die_errno("cannot readlink '%s'", template); |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 96 | if (sizeof(lnk) <= len) |
| 97 | die("insanely long symlink %s", template); |
| 98 | lnk[len] = 0; |
| 99 | if (symlink(lnk, path)) |
Thomas Rast | 0721c31 | 2009-06-27 17:58:47 +0200 | [diff] [blame] | 100 | die_errno("cannot symlink '%s' '%s'", lnk, path); |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 101 | } |
| 102 | else if (S_ISREG(st_template.st_mode)) { |
| 103 | if (copy_file(path, template, st_template.st_mode)) |
Thomas Rast | 0721c31 | 2009-06-27 17:58:47 +0200 | [diff] [blame] | 104 | die_errno("cannot copy '%s' to '%s'", template, |
| 105 | path); |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 106 | } |
| 107 | else |
| 108 | error("ignoring template %s", template); |
| 109 | } |
| 110 | } |
| 111 | |
Daniel Barkalow | f225aeb | 2008-04-27 13:39:27 -0400 | [diff] [blame] | 112 | static void copy_templates(const char *template_dir) |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 113 | { |
| 114 | char path[PATH_MAX]; |
| 115 | char template_path[PATH_MAX]; |
Junio C Hamano | d3af621 | 2005-08-06 12:50:14 -0700 | [diff] [blame] | 116 | int template_len; |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 117 | DIR *dir; |
Daniel Barkalow | f225aeb | 2008-04-27 13:39:27 -0400 | [diff] [blame] | 118 | const char *git_dir = get_git_dir(); |
| 119 | int len = strlen(git_dir); |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 120 | |
Johannes Sixt | a47d181 | 2007-11-13 21:05:04 +0100 | [diff] [blame] | 121 | if (!template_dir) |
Junio C Hamano | d4ebc36 | 2006-12-19 01:28:15 -0800 | [diff] [blame] | 122 | template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT); |
Steffen Prohaska | 2de9de5 | 2008-07-13 22:31:18 +0200 | [diff] [blame] | 123 | if (!template_dir) |
| 124 | template_dir = system_path(DEFAULT_GIT_TEMPLATE_DIR); |
Jeff King | 172035f | 2008-07-28 02:02:04 -0400 | [diff] [blame] | 125 | if (!template_dir[0]) |
| 126 | return; |
Frank Lichtenheld | 32d1776 | 2009-04-18 16:14:02 +0200 | [diff] [blame] | 127 | template_len = strlen(template_dir); |
| 128 | if (PATH_MAX <= (template_len+strlen("/config"))) |
| 129 | die("insanely long template path %s", template_dir); |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 130 | strcpy(template_path, template_dir); |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 131 | if (template_path[template_len-1] != '/') { |
| 132 | template_path[template_len++] = '/'; |
| 133 | template_path[template_len] = 0; |
| 134 | } |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 135 | dir = opendir(template_path); |
Junio C Hamano | d3af621 | 2005-08-06 12:50:14 -0700 | [diff] [blame] | 136 | if (!dir) { |
Miklos Vajna | 2fd8c0a | 2009-03-24 02:09:13 +0100 | [diff] [blame] | 137 | warning("templates not found %s", template_dir); |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 138 | return; |
Junio C Hamano | d3af621 | 2005-08-06 12:50:14 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Junio C Hamano | 4f62953 | 2005-11-25 16:03:56 -0800 | [diff] [blame] | 141 | /* Make sure that template is from the correct vintage */ |
| 142 | strcpy(template_path + template_len, "config"); |
| 143 | repository_format_version = 0; |
| 144 | git_config_from_file(check_repository_format_version, |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 145 | template_path, NULL); |
Junio C Hamano | 4f62953 | 2005-11-25 16:03:56 -0800 | [diff] [blame] | 146 | template_path[template_len] = 0; |
| 147 | |
| 148 | if (repository_format_version && |
| 149 | repository_format_version != GIT_REPO_VERSION) { |
Miklos Vajna | 2fd8c0a | 2009-03-24 02:09:13 +0100 | [diff] [blame] | 150 | warning("not copying templates of " |
| 151 | "a wrong format version %d from '%s'", |
Junio C Hamano | 4f62953 | 2005-11-25 16:03:56 -0800 | [diff] [blame] | 152 | repository_format_version, |
| 153 | template_dir); |
| 154 | closedir(dir); |
| 155 | return; |
| 156 | } |
| 157 | |
Junio C Hamano | d3af621 | 2005-08-06 12:50:14 -0700 | [diff] [blame] | 158 | memcpy(path, git_dir, len); |
Daniel Barkalow | f225aeb | 2008-04-27 13:39:27 -0400 | [diff] [blame] | 159 | if (len && path[len - 1] != '/') |
| 160 | path[len++] = '/'; |
Petr Baudis | 1f961c1 | 2005-09-20 02:19:50 +0200 | [diff] [blame] | 161 | path[len] = 0; |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 162 | copy_templates_1(path, len, |
| 163 | template_path, template_len, |
| 164 | dir); |
| 165 | closedir(dir); |
| 166 | } |
| 167 | |
Daniel Barkalow | f225aeb | 2008-04-27 13:39:27 -0400 | [diff] [blame] | 168 | static int create_default_files(const char *template_path) |
Linus Torvalds | cad88fd | 2005-05-30 10:20:44 -0700 | [diff] [blame] | 169 | { |
Daniel Barkalow | f225aeb | 2008-04-27 13:39:27 -0400 | [diff] [blame] | 170 | const char *git_dir = get_git_dir(); |
Linus Torvalds | cad88fd | 2005-05-30 10:20:44 -0700 | [diff] [blame] | 171 | unsigned len = strlen(git_dir); |
| 172 | static char path[PATH_MAX]; |
Junio C Hamano | 4f62953 | 2005-11-25 16:03:56 -0800 | [diff] [blame] | 173 | struct stat st1; |
| 174 | char repo_version_string[10]; |
Johannes Schindelin | 5cc8f37 | 2008-03-24 16:14:52 +0100 | [diff] [blame] | 175 | char junk[2]; |
Shawn O. Pearce | ef0a89a | 2006-12-15 00:44:58 -0500 | [diff] [blame] | 176 | int reinit; |
Shawn O. Pearce | c869753 | 2006-12-30 23:53:55 -0500 | [diff] [blame] | 177 | int filemode; |
Linus Torvalds | cad88fd | 2005-05-30 10:20:44 -0700 | [diff] [blame] | 178 | |
| 179 | if (len > sizeof(path)-50) |
| 180 | die("insane git directory %s", git_dir); |
| 181 | memcpy(path, git_dir, len); |
| 182 | |
| 183 | if (len && path[len-1] != '/') |
| 184 | path[len++] = '/'; |
| 185 | |
| 186 | /* |
| 187 | * Create .git/refs/{heads,tags} |
| 188 | */ |
Daniel Barkalow | f225aeb | 2008-04-27 13:39:27 -0400 | [diff] [blame] | 189 | safe_create_dir(git_path("refs"), 1); |
| 190 | safe_create_dir(git_path("refs/heads"), 1); |
| 191 | safe_create_dir(git_path("refs/tags"), 1); |
Linus Torvalds | cad88fd | 2005-05-30 10:20:44 -0700 | [diff] [blame] | 192 | |
Junio C Hamano | 4f62953 | 2005-11-25 16:03:56 -0800 | [diff] [blame] | 193 | /* First copy the templates -- we might have the default |
| 194 | * config file there, in which case we would want to read |
| 195 | * from it after installing. |
| 196 | */ |
Daniel Barkalow | f225aeb | 2008-04-27 13:39:27 -0400 | [diff] [blame] | 197 | copy_templates(template_path); |
Junio C Hamano | 4f62953 | 2005-11-25 16:03:56 -0800 | [diff] [blame] | 198 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 199 | git_config(git_default_config, NULL); |
Deskin Miller | 0a2c7ee | 2008-10-07 01:37:48 -0400 | [diff] [blame] | 200 | is_bare_repository_cfg = init_is_bare_repository; |
Junio C Hamano | 5a688fe | 2009-03-25 16:19:36 -0700 | [diff] [blame] | 201 | |
| 202 | /* reading existing config may have overwrote it */ |
Deskin Miller | 0a2c7ee | 2008-10-07 01:37:48 -0400 | [diff] [blame] | 203 | if (init_shared_repository != -1) |
| 204 | shared_repository = init_shared_repository; |
Junio C Hamano | 4f62953 | 2005-11-25 16:03:56 -0800 | [diff] [blame] | 205 | |
Linus Torvalds | cad88fd | 2005-05-30 10:20:44 -0700 | [diff] [blame] | 206 | /* |
Junio C Hamano | 138086a | 2006-06-09 22:07:23 -0700 | [diff] [blame] | 207 | * We would have created the above under user's umask -- under |
| 208 | * shared-repository settings, we would need to fix them up. |
| 209 | */ |
| 210 | if (shared_repository) { |
Daniel Barkalow | f225aeb | 2008-04-27 13:39:27 -0400 | [diff] [blame] | 211 | adjust_shared_perm(get_git_dir()); |
| 212 | adjust_shared_perm(git_path("refs")); |
| 213 | adjust_shared_perm(git_path("refs/heads")); |
| 214 | adjust_shared_perm(git_path("refs/tags")); |
Junio C Hamano | 138086a | 2006-06-09 22:07:23 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | /* |
Linus Torvalds | cad88fd | 2005-05-30 10:20:44 -0700 | [diff] [blame] | 218 | * Create the default symlink from ".git/HEAD" to the "master" |
Junio C Hamano | 8098a17 | 2005-09-30 14:26:57 -0700 | [diff] [blame] | 219 | * branch, if it does not exist yet. |
Linus Torvalds | cad88fd | 2005-05-30 10:20:44 -0700 | [diff] [blame] | 220 | */ |
| 221 | strcpy(path + len, "HEAD"); |
Johannes Schindelin | 5cc8f37 | 2008-03-24 16:14:52 +0100 | [diff] [blame] | 222 | reinit = (!access(path, R_OK) |
| 223 | || readlink(path, junk, sizeof(junk)-1) != -1); |
Shawn O. Pearce | ef0a89a | 2006-12-15 00:44:58 -0500 | [diff] [blame] | 224 | if (!reinit) { |
Nicolas Pitre | 8b5157e | 2007-01-26 17:26:10 -0500 | [diff] [blame] | 225 | if (create_symref("HEAD", "refs/heads/master", NULL) < 0) |
Linus Torvalds | cad88fd | 2005-05-30 10:20:44 -0700 | [diff] [blame] | 226 | exit(1); |
Linus Torvalds | cad88fd | 2005-05-30 10:20:44 -0700 | [diff] [blame] | 227 | } |
Junio C Hamano | 4f62953 | 2005-11-25 16:03:56 -0800 | [diff] [blame] | 228 | |
| 229 | /* This forces creation of new config file */ |
| 230 | sprintf(repo_version_string, "%d", GIT_REPO_VERSION); |
| 231 | git_config_set("core.repositoryformatversion", repo_version_string); |
| 232 | |
Junio C Hamano | 8098a17 | 2005-09-30 14:26:57 -0700 | [diff] [blame] | 233 | path[len] = 0; |
Johannes Schindelin | e24317b | 2005-10-26 01:43:03 +0200 | [diff] [blame] | 234 | strcpy(path + len, "config"); |
Johannes Schindelin | e24317b | 2005-10-26 01:43:03 +0200 | [diff] [blame] | 235 | |
Junio C Hamano | 4f62953 | 2005-11-25 16:03:56 -0800 | [diff] [blame] | 236 | /* Check filemode trustability */ |
Shawn O. Pearce | c869753 | 2006-12-30 23:53:55 -0500 | [diff] [blame] | 237 | filemode = TEST_FILEMODE; |
| 238 | if (TEST_FILEMODE && !lstat(path, &st1)) { |
Junio C Hamano | 4f62953 | 2005-11-25 16:03:56 -0800 | [diff] [blame] | 239 | struct stat st2; |
Shawn O. Pearce | c869753 | 2006-12-30 23:53:55 -0500 | [diff] [blame] | 240 | filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) && |
Junio C Hamano | 4f62953 | 2005-11-25 16:03:56 -0800 | [diff] [blame] | 241 | !lstat(path, &st2) && |
| 242 | st1.st_mode != st2.st_mode); |
Johannes Schindelin | e24317b | 2005-10-26 01:43:03 +0200 | [diff] [blame] | 243 | } |
Shawn O. Pearce | c869753 | 2006-12-30 23:53:55 -0500 | [diff] [blame] | 244 | git_config_set("core.filemode", filemode ? "true" : "false"); |
Shawn O. Pearce | ef0a89a | 2006-12-15 00:44:58 -0500 | [diff] [blame] | 245 | |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 246 | if (is_bare_repository()) |
Junio C Hamano | 7d1864c | 2007-01-07 02:00:28 -0800 | [diff] [blame] | 247 | git_config_set("core.bare", "true"); |
Junio C Hamano | 7d1864c | 2007-01-07 02:00:28 -0800 | [diff] [blame] | 248 | else { |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 249 | const char *work_tree = get_git_work_tree(); |
Junio C Hamano | 7d1864c | 2007-01-07 02:00:28 -0800 | [diff] [blame] | 250 | git_config_set("core.bare", "false"); |
Alex Riesen | 196055c | 2007-01-23 16:51:18 +0100 | [diff] [blame] | 251 | /* allow template config file to override the default */ |
| 252 | if (log_all_ref_updates == -1) |
| 253 | git_config_set("core.logallrefupdates", "true"); |
Daniel Barkalow | f225aeb | 2008-04-27 13:39:27 -0400 | [diff] [blame] | 254 | if (prefixcmp(git_dir, work_tree) || |
| 255 | strcmp(git_dir + strlen(work_tree), "/.git")) { |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 256 | git_config_set("core.worktree", work_tree); |
Daniel Barkalow | f225aeb | 2008-04-27 13:39:27 -0400 | [diff] [blame] | 257 | } |
Junio C Hamano | 7d1864c | 2007-01-07 02:00:28 -0800 | [diff] [blame] | 258 | } |
Junio C Hamano | 75d2449 | 2007-08-31 00:25:04 -0700 | [diff] [blame] | 259 | |
Junio C Hamano | 75d2449 | 2007-08-31 00:25:04 -0700 | [diff] [blame] | 260 | if (!reinit) { |
Dmitry Potapov | 2455406 | 2008-05-11 18:16:39 +0200 | [diff] [blame] | 261 | /* Check if symlink is supported in the work tree */ |
Junio C Hamano | 75d2449 | 2007-08-31 00:25:04 -0700 | [diff] [blame] | 262 | path[len] = 0; |
| 263 | strcpy(path + len, "tXXXXXX"); |
| 264 | if (!close(xmkstemp(path)) && |
| 265 | !unlink(path) && |
| 266 | !symlink("testing", path) && |
| 267 | !lstat(path, &st1) && |
| 268 | S_ISLNK(st1.st_mode)) |
| 269 | unlink(path); /* good */ |
| 270 | else |
| 271 | git_config_set("core.symlinks", "false"); |
Dmitry Potapov | 2455406 | 2008-05-11 18:16:39 +0200 | [diff] [blame] | 272 | |
| 273 | /* Check if the filesystem is case-insensitive */ |
| 274 | path[len] = 0; |
| 275 | strcpy(path + len, "CoNfIg"); |
| 276 | if (!access(path, F_OK)) |
| 277 | git_config_set("core.ignorecase", "true"); |
Junio C Hamano | 75d2449 | 2007-08-31 00:25:04 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Shawn O. Pearce | ef0a89a | 2006-12-15 00:44:58 -0500 | [diff] [blame] | 280 | return reinit; |
Linus Torvalds | cad88fd | 2005-05-30 10:20:44 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Daniel Barkalow | f225aeb | 2008-04-27 13:39:27 -0400 | [diff] [blame] | 283 | int init_db(const char *template_dir, unsigned int flags) |
Junio C Hamano | 6adcca3 | 2007-08-27 00:58:06 -0700 | [diff] [blame] | 284 | { |
Junio C Hamano | d19938a | 2005-05-09 17:57:56 -0700 | [diff] [blame] | 285 | const char *sha1_dir; |
Timo Hirvonen | c3c8835 | 2006-05-19 13:03:57 +0300 | [diff] [blame] | 286 | char *path; |
Daniel Barkalow | f225aeb | 2008-04-27 13:39:27 -0400 | [diff] [blame] | 287 | int len, reinit; |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 288 | |
Daniel Barkalow | f225aeb | 2008-04-27 13:39:27 -0400 | [diff] [blame] | 289 | safe_create_dir(get_git_dir(), 0); |
Junio C Hamano | 4f62953 | 2005-11-25 16:03:56 -0800 | [diff] [blame] | 290 | |
Deskin Miller | 0a2c7ee | 2008-10-07 01:37:48 -0400 | [diff] [blame] | 291 | init_is_bare_repository = is_bare_repository(); |
| 292 | |
Junio C Hamano | 4f62953 | 2005-11-25 16:03:56 -0800 | [diff] [blame] | 293 | /* Check to see if the repository version is right. |
| 294 | * Note that a newly created repository does not have |
| 295 | * config file, so this will not fail. What we are catching |
| 296 | * is an attempt to reinitialize new repository with an old tool. |
| 297 | */ |
| 298 | check_repository_format(); |
| 299 | |
Daniel Barkalow | f225aeb | 2008-04-27 13:39:27 -0400 | [diff] [blame] | 300 | reinit = create_default_files(template_dir); |
Linus Torvalds | cad88fd | 2005-05-30 10:20:44 -0700 | [diff] [blame] | 301 | |
Linus Torvalds | cad88fd | 2005-05-30 10:20:44 -0700 | [diff] [blame] | 302 | sha1_dir = get_object_directory(); |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 303 | len = strlen(sha1_dir); |
Christopher Li | 812666c | 2005-04-26 12:00:58 -0700 | [diff] [blame] | 304 | path = xmalloc(len + 40); |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 305 | memcpy(path, sha1_dir, len); |
Zach Welch | cb126d8 | 2005-04-19 21:48:15 -0700 | [diff] [blame] | 306 | |
Johannes Schindelin | af6e277 | 2005-12-22 23:19:37 +0100 | [diff] [blame] | 307 | safe_create_dir(sha1_dir, 1); |
Linus Torvalds | f49fb35 | 2005-06-27 18:26:11 -0700 | [diff] [blame] | 308 | strcpy(path+len, "/pack"); |
Johannes Schindelin | af6e277 | 2005-12-22 23:19:37 +0100 | [diff] [blame] | 309 | safe_create_dir(path, 1); |
Junio C Hamano | d57306c | 2005-08-20 02:05:31 -0700 | [diff] [blame] | 310 | strcpy(path+len, "/info"); |
Johannes Schindelin | af6e277 | 2005-12-22 23:19:37 +0100 | [diff] [blame] | 311 | safe_create_dir(path, 1); |
| 312 | |
Junio C Hamano | 94df250 | 2006-06-09 23:09:49 -0700 | [diff] [blame] | 313 | if (shared_repository) { |
| 314 | char buf[10]; |
| 315 | /* We do not spell "group" and such, so that |
| 316 | * the configuration can be read by older version |
Heikki Orsila | 06cbe85 | 2008-04-16 11:34:24 +0300 | [diff] [blame] | 317 | * of git. Note, we use octal numbers for new share modes, |
| 318 | * and compatibility values for PERM_GROUP and |
| 319 | * PERM_EVERYBODY. |
Junio C Hamano | 94df250 | 2006-06-09 23:09:49 -0700 | [diff] [blame] | 320 | */ |
Junio C Hamano | 5a688fe | 2009-03-25 16:19:36 -0700 | [diff] [blame] | 321 | if (shared_repository < 0) |
| 322 | /* force to the mode value */ |
| 323 | sprintf(buf, "0%o", -shared_repository); |
| 324 | else if (shared_repository == PERM_GROUP) |
Heikki Orsila | 06cbe85 | 2008-04-16 11:34:24 +0300 | [diff] [blame] | 325 | sprintf(buf, "%d", OLD_PERM_GROUP); |
| 326 | else if (shared_repository == PERM_EVERYBODY) |
| 327 | sprintf(buf, "%d", OLD_PERM_EVERYBODY); |
| 328 | else |
Junio C Hamano | 5a688fe | 2009-03-25 16:19:36 -0700 | [diff] [blame] | 329 | die("oops"); |
Junio C Hamano | 94df250 | 2006-06-09 23:09:49 -0700 | [diff] [blame] | 330 | git_config_set("core.sharedrepository", buf); |
Johannes Schindelin | 11031d7 | 2006-09-21 01:07:54 +0200 | [diff] [blame] | 331 | git_config_set("receive.denyNonFastforwards", "true"); |
Junio C Hamano | 94df250 | 2006-06-09 23:09:49 -0700 | [diff] [blame] | 332 | } |
Johannes Schindelin | af6e277 | 2005-12-22 23:19:37 +0100 | [diff] [blame] | 333 | |
Daniel Barkalow | f225aeb | 2008-04-27 13:39:27 -0400 | [diff] [blame] | 334 | if (!(flags & INIT_DB_QUIET)) |
Jeffrey C. Ollie | 4576518 | 2007-06-07 07:50:29 -0500 | [diff] [blame] | 335 | printf("%s%s Git repository in %s/\n", |
| 336 | reinit ? "Reinitialized existing" : "Initialized empty", |
| 337 | shared_repository ? " shared" : "", |
Daniel Barkalow | f225aeb | 2008-04-27 13:39:27 -0400 | [diff] [blame] | 338 | get_git_dir()); |
Shawn O. Pearce | ef0a89a | 2006-12-15 00:44:58 -0500 | [diff] [blame] | 339 | |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 340 | return 0; |
| 341 | } |
Daniel Barkalow | f225aeb | 2008-04-27 13:39:27 -0400 | [diff] [blame] | 342 | |
| 343 | static int guess_repository_type(const char *git_dir) |
| 344 | { |
| 345 | char cwd[PATH_MAX]; |
| 346 | const char *slash; |
| 347 | |
| 348 | /* |
| 349 | * "GIT_DIR=. git init" is always bare. |
| 350 | * "GIT_DIR=`pwd` git init" too. |
| 351 | */ |
| 352 | if (!strcmp(".", git_dir)) |
| 353 | return 1; |
| 354 | if (!getcwd(cwd, sizeof(cwd))) |
Thomas Rast | 0721c31 | 2009-06-27 17:58:47 +0200 | [diff] [blame] | 355 | die_errno("cannot tell cwd"); |
Daniel Barkalow | f225aeb | 2008-04-27 13:39:27 -0400 | [diff] [blame] | 356 | if (!strcmp(git_dir, cwd)) |
| 357 | return 1; |
| 358 | /* |
| 359 | * "GIT_DIR=.git or GIT_DIR=something/.git is usually not. |
| 360 | */ |
| 361 | if (!strcmp(git_dir, ".git")) |
| 362 | return 0; |
| 363 | slash = strrchr(git_dir, '/'); |
| 364 | if (slash && !strcmp(slash, "/.git")) |
| 365 | return 0; |
| 366 | |
| 367 | /* |
| 368 | * Otherwise it is often bare. At this point |
| 369 | * we are just guessing. |
| 370 | */ |
| 371 | return 1; |
| 372 | } |
| 373 | |
Michał Kiedrowicz | 596f91e | 2009-07-12 12:24:32 +0200 | [diff] [blame] | 374 | static int shared_callback(const struct option *opt, const char *arg, int unset) |
| 375 | { |
| 376 | *((int *) opt->value) = (arg) ? git_config_perm("arg", arg) : PERM_GROUP; |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | static const char *const init_db_usage[] = { |
Junio C Hamano | 0397ff2 | 2009-08-05 12:39:33 -0700 | [diff] [blame] | 381 | "git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]] [directory]", |
Michał Kiedrowicz | 596f91e | 2009-07-12 12:24:32 +0200 | [diff] [blame] | 382 | NULL |
| 383 | }; |
Daniel Barkalow | f225aeb | 2008-04-27 13:39:27 -0400 | [diff] [blame] | 384 | |
| 385 | /* |
| 386 | * If you want to, you can share the DB area with any number of branches. |
| 387 | * That has advantages: you can save space by sharing all the SHA1 objects. |
| 388 | * On the other hand, it might just make lookup slower and messier. You |
| 389 | * be the judge. The default case is to have one DB per managed directory. |
| 390 | */ |
| 391 | int cmd_init_db(int argc, const char **argv, const char *prefix) |
| 392 | { |
| 393 | const char *git_dir; |
| 394 | const char *template_dir = NULL; |
| 395 | unsigned int flags = 0; |
Michał Kiedrowicz | 596f91e | 2009-07-12 12:24:32 +0200 | [diff] [blame] | 396 | const struct option init_db_options[] = { |
| 397 | OPT_STRING(0, "template", &template_dir, "template-directory", |
| 398 | "provide the directory from which templates will be used"), |
| 399 | OPT_SET_INT(0, "bare", &is_bare_repository_cfg, |
| 400 | "create a bare repository", 1), |
| 401 | { OPTION_CALLBACK, 0, "shared", &init_shared_repository, |
| 402 | "permissions", |
| 403 | "specify that the git repository is to be shared amongst several users", |
| 404 | PARSE_OPT_OPTARG | PARSE_OPT_NONEG, shared_callback, 0}, |
| 405 | OPT_BIT('q', "quiet", &flags, "be quiet", INIT_DB_QUIET), |
| 406 | OPT_END() |
| 407 | }; |
Daniel Barkalow | f225aeb | 2008-04-27 13:39:27 -0400 | [diff] [blame] | 408 | |
Junio C Hamano | 0397ff2 | 2009-08-05 12:39:33 -0700 | [diff] [blame] | 409 | 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] | 410 | |
Junio C Hamano | 0397ff2 | 2009-08-05 12:39:33 -0700 | [diff] [blame] | 411 | if (argc == 1) { |
Nanako Shiraishi | 53d4888 | 2009-07-25 06:59:28 +0900 | [diff] [blame] | 412 | int mkdir_tried = 0; |
| 413 | retry: |
Junio C Hamano | 0397ff2 | 2009-08-05 12:39:33 -0700 | [diff] [blame] | 414 | if (chdir(argv[0]) < 0) { |
Nanako Shiraishi | 53d4888 | 2009-07-25 06:59:28 +0900 | [diff] [blame] | 415 | if (!mkdir_tried) { |
| 416 | int saved; |
| 417 | /* |
| 418 | * At this point we haven't read any configuration, |
| 419 | * and we know shared_repository should always be 0; |
| 420 | * but just in case we play safe. |
| 421 | */ |
| 422 | saved = shared_repository; |
| 423 | shared_repository = 0; |
Junio C Hamano | 0397ff2 | 2009-08-05 12:39:33 -0700 | [diff] [blame] | 424 | switch (safe_create_leading_directories_const(argv[0])) { |
Nanako Shiraishi | 53d4888 | 2009-07-25 06:59:28 +0900 | [diff] [blame] | 425 | case -3: |
| 426 | errno = EEXIST; |
| 427 | /* fallthru */ |
| 428 | case -1: |
Junio C Hamano | 0397ff2 | 2009-08-05 12:39:33 -0700 | [diff] [blame] | 429 | die_errno("cannot mkdir %s", argv[0]); |
Nanako Shiraishi | 53d4888 | 2009-07-25 06:59:28 +0900 | [diff] [blame] | 430 | break; |
| 431 | default: |
| 432 | break; |
| 433 | } |
| 434 | shared_repository = saved; |
Junio C Hamano | 0397ff2 | 2009-08-05 12:39:33 -0700 | [diff] [blame] | 435 | if (mkdir(argv[0], 0777) < 0) |
| 436 | die_errno("cannot mkdir %s", argv[0]); |
Nanako Shiraishi | 53d4888 | 2009-07-25 06:59:28 +0900 | [diff] [blame] | 437 | mkdir_tried = 1; |
| 438 | goto retry; |
| 439 | } |
Junio C Hamano | 0397ff2 | 2009-08-05 12:39:33 -0700 | [diff] [blame] | 440 | die_errno("cannot chdir to %s", argv[0]); |
Nanako Shiraishi | 53d4888 | 2009-07-25 06:59:28 +0900 | [diff] [blame] | 441 | } |
Junio C Hamano | 0397ff2 | 2009-08-05 12:39:33 -0700 | [diff] [blame] | 442 | } else if (0 < argc) { |
| 443 | usage(init_db_usage[0]); |
Nanako Shiraishi | 53d4888 | 2009-07-25 06:59:28 +0900 | [diff] [blame] | 444 | } |
Junio C Hamano | 0397ff2 | 2009-08-05 12:39:33 -0700 | [diff] [blame] | 445 | if (is_bare_repository_cfg == 1) { |
Michał Kiedrowicz | 596f91e | 2009-07-12 12:24:32 +0200 | [diff] [blame] | 446 | static char git_dir[PATH_MAX+1]; |
Nanako Shiraishi | 53d4888 | 2009-07-25 06:59:28 +0900 | [diff] [blame] | 447 | |
| 448 | setenv(GIT_DIR_ENVIRONMENT, |
| 449 | getcwd(git_dir, sizeof(git_dir)), 0); |
Daniel Barkalow | f225aeb | 2008-04-27 13:39:27 -0400 | [diff] [blame] | 450 | } |
| 451 | |
Junio C Hamano | 5a688fe | 2009-03-25 16:19:36 -0700 | [diff] [blame] | 452 | if (init_shared_repository != -1) |
| 453 | shared_repository = init_shared_repository; |
| 454 | |
Daniel Barkalow | f225aeb | 2008-04-27 13:39:27 -0400 | [diff] [blame] | 455 | /* |
| 456 | * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR |
| 457 | * without --bare. Catch the error early. |
| 458 | */ |
| 459 | git_dir = getenv(GIT_DIR_ENVIRONMENT); |
| 460 | if ((!git_dir || is_bare_repository_cfg == 1) |
| 461 | && getenv(GIT_WORK_TREE_ENVIRONMENT)) |
| 462 | die("%s (or --work-tree=<directory>) not allowed without " |
| 463 | "specifying %s (or --git-dir=<directory>)", |
| 464 | GIT_WORK_TREE_ENVIRONMENT, |
| 465 | GIT_DIR_ENVIRONMENT); |
| 466 | |
| 467 | /* |
| 468 | * Set up the default .git directory contents |
| 469 | */ |
| 470 | if (!git_dir) |
| 471 | git_dir = DEFAULT_GIT_DIR_ENVIRONMENT; |
| 472 | |
| 473 | if (is_bare_repository_cfg < 0) |
| 474 | is_bare_repository_cfg = guess_repository_type(git_dir); |
| 475 | |
| 476 | if (!is_bare_repository_cfg) { |
| 477 | if (git_dir) { |
| 478 | const char *git_dir_parent = strrchr(git_dir, '/'); |
| 479 | if (git_dir_parent) { |
| 480 | char *rel = xstrndup(git_dir, git_dir_parent - git_dir); |
| 481 | git_work_tree_cfg = xstrdup(make_absolute_path(rel)); |
| 482 | free(rel); |
| 483 | } |
| 484 | } |
| 485 | if (!git_work_tree_cfg) { |
| 486 | git_work_tree_cfg = xcalloc(PATH_MAX, 1); |
| 487 | if (!getcwd(git_work_tree_cfg, PATH_MAX)) |
Thomas Rast | 0721c31 | 2009-06-27 17:58:47 +0200 | [diff] [blame] | 488 | die_errno ("Cannot access current working directory"); |
Daniel Barkalow | f225aeb | 2008-04-27 13:39:27 -0400 | [diff] [blame] | 489 | } |
| 490 | if (access(get_git_work_tree(), X_OK)) |
Thomas Rast | 0721c31 | 2009-06-27 17:58:47 +0200 | [diff] [blame] | 491 | die_errno ("Cannot access work tree '%s'", |
| 492 | get_git_work_tree()); |
Daniel Barkalow | f225aeb | 2008-04-27 13:39:27 -0400 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | set_git_dir(make_absolute_path(git_dir)); |
| 496 | |
| 497 | return init_db(template_dir, flags); |
| 498 | } |