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" |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 8 | |
Junio C Hamano | d3af621 | 2005-08-06 12:50:14 -0700 | [diff] [blame] | 9 | #ifndef DEFAULT_GIT_TEMPLATE_DIR |
Johannes Sixt | d52fd42 | 2007-06-11 11:10:47 +0200 | [diff] [blame] | 10 | #define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates" |
Junio C Hamano | d3af621 | 2005-08-06 12:50:14 -0700 | [diff] [blame] | 11 | #endif |
| 12 | |
Shawn O. Pearce | c869753 | 2006-12-30 23:53:55 -0500 | [diff] [blame] | 13 | #ifdef NO_TRUSTABLE_FILEMODE |
| 14 | #define TEST_FILEMODE 0 |
| 15 | #else |
| 16 | #define TEST_FILEMODE 1 |
| 17 | #endif |
| 18 | |
Johannes Schindelin | af6e277 | 2005-12-22 23:19:37 +0100 | [diff] [blame] | 19 | static void safe_create_dir(const char *dir, int share) |
Zach Welch | cb126d8 | 2005-04-19 21:48:15 -0700 | [diff] [blame] | 20 | { |
Junio C Hamano | f312de0 | 2005-07-06 01:21:46 -0700 | [diff] [blame] | 21 | if (mkdir(dir, 0777) < 0) { |
Zach Welch | cb126d8 | 2005-04-19 21:48:15 -0700 | [diff] [blame] | 22 | if (errno != EEXIST) { |
| 23 | perror(dir); |
| 24 | exit(1); |
| 25 | } |
| 26 | } |
Johannes Schindelin | af6e277 | 2005-12-22 23:19:37 +0100 | [diff] [blame] | 27 | else if (share && adjust_shared_perm(dir)) |
| 28 | die("Could not make %s writable by group\n", dir); |
Zach Welch | cb126d8 | 2005-04-19 21:48:15 -0700 | [diff] [blame] | 29 | } |
| 30 | |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 31 | static int copy_file(const char *dst, const char *src, int mode) |
| 32 | { |
Junio C Hamano | 32276c8 | 2005-11-05 11:07:22 -0800 | [diff] [blame] | 33 | int fdi, fdo, status; |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 34 | |
| 35 | mode = (mode & 0111) ? 0777 : 0666; |
| 36 | if ((fdi = open(src, O_RDONLY)) < 0) |
| 37 | return fdi; |
| 38 | if ((fdo = open(dst, O_WRONLY | O_CREAT | O_EXCL, mode)) < 0) { |
| 39 | close(fdi); |
| 40 | return fdo; |
| 41 | } |
Junio C Hamano | 32276c8 | 2005-11-05 11:07:22 -0800 | [diff] [blame] | 42 | status = copy_fd(fdi, fdo); |
Jim Meyering | 91c8d59 | 2007-06-24 21:20:41 +0200 | [diff] [blame] | 43 | if (close(fdo) != 0) |
| 44 | return error("%s: write error: %s", dst, strerror(errno)); |
Johannes Schindelin | af6e277 | 2005-12-22 23:19:37 +0100 | [diff] [blame] | 45 | |
| 46 | if (!status && adjust_shared_perm(dst)) |
| 47 | return -1; |
| 48 | |
Junio C Hamano | 32276c8 | 2005-11-05 11:07:22 -0800 | [diff] [blame] | 49 | return status; |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | static void copy_templates_1(char *path, int baselen, |
| 53 | char *template, int template_baselen, |
| 54 | DIR *dir) |
| 55 | { |
| 56 | struct dirent *de; |
| 57 | |
| 58 | /* Note: if ".git/hooks" file exists in the repository being |
| 59 | * re-initialized, /etc/core-git/templates/hooks/update would |
Nicolas Pitre | 5c94f87 | 2007-01-12 16:01:46 -0500 | [diff] [blame] | 60 | * 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] | 61 | * it means that the set of templates we ship by default, along |
| 62 | * with the way the namespace under .git/ is organized, should |
| 63 | * be really carefully chosen. |
| 64 | */ |
Johannes Schindelin | af6e277 | 2005-12-22 23:19:37 +0100 | [diff] [blame] | 65 | safe_create_dir(path, 1); |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 66 | while ((de = readdir(dir)) != NULL) { |
| 67 | struct stat st_git, st_template; |
| 68 | int namelen; |
| 69 | int exists = 0; |
| 70 | |
| 71 | if (de->d_name[0] == '.') |
| 72 | continue; |
| 73 | namelen = strlen(de->d_name); |
| 74 | if ((PATH_MAX <= baselen + namelen) || |
| 75 | (PATH_MAX <= template_baselen + namelen)) |
| 76 | die("insanely long template name %s", de->d_name); |
| 77 | memcpy(path + baselen, de->d_name, namelen+1); |
| 78 | memcpy(template + template_baselen, de->d_name, namelen+1); |
| 79 | if (lstat(path, &st_git)) { |
| 80 | if (errno != ENOENT) |
| 81 | die("cannot stat %s", path); |
| 82 | } |
| 83 | else |
| 84 | exists = 1; |
| 85 | |
| 86 | if (lstat(template, &st_template)) |
| 87 | die("cannot stat template %s", template); |
| 88 | |
| 89 | if (S_ISDIR(st_template.st_mode)) { |
| 90 | DIR *subdir = opendir(template); |
| 91 | int baselen_sub = baselen + namelen; |
| 92 | int template_baselen_sub = template_baselen + namelen; |
| 93 | if (!subdir) |
| 94 | die("cannot opendir %s", template); |
| 95 | path[baselen_sub++] = |
| 96 | template[template_baselen_sub++] = '/'; |
| 97 | path[baselen_sub] = |
| 98 | template[template_baselen_sub] = 0; |
| 99 | copy_templates_1(path, baselen_sub, |
| 100 | template, template_baselen_sub, |
| 101 | subdir); |
| 102 | closedir(subdir); |
| 103 | } |
| 104 | else if (exists) |
| 105 | continue; |
| 106 | else if (S_ISLNK(st_template.st_mode)) { |
| 107 | char lnk[256]; |
| 108 | int len; |
| 109 | len = readlink(template, lnk, sizeof(lnk)); |
| 110 | if (len < 0) |
| 111 | die("cannot readlink %s", template); |
| 112 | if (sizeof(lnk) <= len) |
| 113 | die("insanely long symlink %s", template); |
| 114 | lnk[len] = 0; |
| 115 | if (symlink(lnk, path)) |
| 116 | die("cannot symlink %s %s", lnk, path); |
| 117 | } |
| 118 | else if (S_ISREG(st_template.st_mode)) { |
| 119 | if (copy_file(path, template, st_template.st_mode)) |
| 120 | die("cannot copy %s to %s", template, path); |
| 121 | } |
| 122 | else |
| 123 | error("ignoring template %s", template); |
| 124 | } |
| 125 | } |
| 126 | |
Timo Hirvonen | c3c8835 | 2006-05-19 13:03:57 +0300 | [diff] [blame] | 127 | static void copy_templates(const char *git_dir, int len, const char *template_dir) |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 128 | { |
| 129 | char path[PATH_MAX]; |
| 130 | char template_path[PATH_MAX]; |
Junio C Hamano | d3af621 | 2005-08-06 12:50:14 -0700 | [diff] [blame] | 131 | int template_len; |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 132 | DIR *dir; |
| 133 | |
Johannes Schindelin | 8683a45 | 2006-12-19 09:18:09 +0100 | [diff] [blame] | 134 | if (!template_dir) { |
Junio C Hamano | d4ebc36 | 2006-12-19 01:28:15 -0800 | [diff] [blame] | 135 | template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT); |
Johannes Schindelin | 8683a45 | 2006-12-19 09:18:09 +0100 | [diff] [blame] | 136 | if (!template_dir) |
| 137 | template_dir = DEFAULT_GIT_TEMPLATE_DIR; |
| 138 | } |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 139 | strcpy(template_path, template_dir); |
| 140 | template_len = strlen(template_path); |
| 141 | if (template_path[template_len-1] != '/') { |
| 142 | template_path[template_len++] = '/'; |
| 143 | template_path[template_len] = 0; |
| 144 | } |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 145 | dir = opendir(template_path); |
Junio C Hamano | d3af621 | 2005-08-06 12:50:14 -0700 | [diff] [blame] | 146 | if (!dir) { |
| 147 | fprintf(stderr, "warning: templates not found %s\n", |
| 148 | template_dir); |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 149 | return; |
Junio C Hamano | d3af621 | 2005-08-06 12:50:14 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Junio C Hamano | 4f62953 | 2005-11-25 16:03:56 -0800 | [diff] [blame] | 152 | /* Make sure that template is from the correct vintage */ |
| 153 | strcpy(template_path + template_len, "config"); |
| 154 | repository_format_version = 0; |
| 155 | git_config_from_file(check_repository_format_version, |
| 156 | template_path); |
| 157 | template_path[template_len] = 0; |
| 158 | |
| 159 | if (repository_format_version && |
| 160 | repository_format_version != GIT_REPO_VERSION) { |
| 161 | fprintf(stderr, "warning: not copying templates of " |
| 162 | "a wrong format version %d from '%s'\n", |
| 163 | repository_format_version, |
| 164 | template_dir); |
| 165 | closedir(dir); |
| 166 | return; |
| 167 | } |
| 168 | |
Junio C Hamano | d3af621 | 2005-08-06 12:50:14 -0700 | [diff] [blame] | 169 | memcpy(path, git_dir, len); |
Petr Baudis | 1f961c1 | 2005-09-20 02:19:50 +0200 | [diff] [blame] | 170 | path[len] = 0; |
Junio C Hamano | 8d5afef | 2005-08-02 16:45:21 -0700 | [diff] [blame] | 171 | copy_templates_1(path, len, |
| 172 | template_path, template_len, |
| 173 | dir); |
| 174 | closedir(dir); |
| 175 | } |
| 176 | |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 177 | static int create_default_files(const char *git_dir, const char *template_path) |
Linus Torvalds | cad88fd | 2005-05-30 10:20:44 -0700 | [diff] [blame] | 178 | { |
| 179 | unsigned len = strlen(git_dir); |
| 180 | static char path[PATH_MAX]; |
Junio C Hamano | 8098a17 | 2005-09-30 14:26:57 -0700 | [diff] [blame] | 181 | unsigned char sha1[20]; |
Junio C Hamano | 4f62953 | 2005-11-25 16:03:56 -0800 | [diff] [blame] | 182 | struct stat st1; |
| 183 | char repo_version_string[10]; |
Shawn O. Pearce | ef0a89a | 2006-12-15 00:44:58 -0500 | [diff] [blame] | 184 | int reinit; |
Shawn O. Pearce | c869753 | 2006-12-30 23:53:55 -0500 | [diff] [blame] | 185 | int filemode; |
Linus Torvalds | cad88fd | 2005-05-30 10:20:44 -0700 | [diff] [blame] | 186 | |
| 187 | if (len > sizeof(path)-50) |
| 188 | die("insane git directory %s", git_dir); |
| 189 | memcpy(path, git_dir, len); |
| 190 | |
| 191 | if (len && path[len-1] != '/') |
| 192 | path[len++] = '/'; |
| 193 | |
| 194 | /* |
| 195 | * Create .git/refs/{heads,tags} |
| 196 | */ |
| 197 | strcpy(path + len, "refs"); |
Johannes Schindelin | af6e277 | 2005-12-22 23:19:37 +0100 | [diff] [blame] | 198 | safe_create_dir(path, 1); |
Linus Torvalds | cad88fd | 2005-05-30 10:20:44 -0700 | [diff] [blame] | 199 | strcpy(path + len, "refs/heads"); |
Johannes Schindelin | af6e277 | 2005-12-22 23:19:37 +0100 | [diff] [blame] | 200 | safe_create_dir(path, 1); |
Linus Torvalds | cad88fd | 2005-05-30 10:20:44 -0700 | [diff] [blame] | 201 | strcpy(path + len, "refs/tags"); |
Johannes Schindelin | af6e277 | 2005-12-22 23:19:37 +0100 | [diff] [blame] | 202 | safe_create_dir(path, 1); |
Linus Torvalds | cad88fd | 2005-05-30 10:20:44 -0700 | [diff] [blame] | 203 | |
Junio C Hamano | 4f62953 | 2005-11-25 16:03:56 -0800 | [diff] [blame] | 204 | /* First copy the templates -- we might have the default |
| 205 | * config file there, in which case we would want to read |
| 206 | * from it after installing. |
| 207 | */ |
| 208 | path[len] = 0; |
| 209 | copy_templates(path, len, template_path); |
| 210 | |
| 211 | git_config(git_default_config); |
| 212 | |
Linus Torvalds | cad88fd | 2005-05-30 10:20:44 -0700 | [diff] [blame] | 213 | /* |
Junio C Hamano | 138086a | 2006-06-09 22:07:23 -0700 | [diff] [blame] | 214 | * We would have created the above under user's umask -- under |
| 215 | * shared-repository settings, we would need to fix them up. |
| 216 | */ |
| 217 | if (shared_repository) { |
| 218 | path[len] = 0; |
| 219 | adjust_shared_perm(path); |
| 220 | strcpy(path + len, "refs"); |
| 221 | adjust_shared_perm(path); |
| 222 | strcpy(path + len, "refs/heads"); |
| 223 | adjust_shared_perm(path); |
| 224 | strcpy(path + len, "refs/tags"); |
| 225 | adjust_shared_perm(path); |
| 226 | } |
| 227 | |
| 228 | /* |
Linus Torvalds | cad88fd | 2005-05-30 10:20:44 -0700 | [diff] [blame] | 229 | * Create the default symlink from ".git/HEAD" to the "master" |
Junio C Hamano | 8098a17 | 2005-09-30 14:26:57 -0700 | [diff] [blame] | 230 | * branch, if it does not exist yet. |
Linus Torvalds | cad88fd | 2005-05-30 10:20:44 -0700 | [diff] [blame] | 231 | */ |
| 232 | strcpy(path + len, "HEAD"); |
Shawn O. Pearce | ef0a89a | 2006-12-15 00:44:58 -0500 | [diff] [blame] | 233 | reinit = !read_ref("HEAD", sha1); |
| 234 | if (!reinit) { |
Nicolas Pitre | 8b5157e | 2007-01-26 17:26:10 -0500 | [diff] [blame] | 235 | if (create_symref("HEAD", "refs/heads/master", NULL) < 0) |
Linus Torvalds | cad88fd | 2005-05-30 10:20:44 -0700 | [diff] [blame] | 236 | exit(1); |
Linus Torvalds | cad88fd | 2005-05-30 10:20:44 -0700 | [diff] [blame] | 237 | } |
Junio C Hamano | 4f62953 | 2005-11-25 16:03:56 -0800 | [diff] [blame] | 238 | |
| 239 | /* This forces creation of new config file */ |
| 240 | sprintf(repo_version_string, "%d", GIT_REPO_VERSION); |
| 241 | git_config_set("core.repositoryformatversion", repo_version_string); |
| 242 | |
Junio C Hamano | 8098a17 | 2005-09-30 14:26:57 -0700 | [diff] [blame] | 243 | path[len] = 0; |
Johannes Schindelin | e24317b | 2005-10-26 01:43:03 +0200 | [diff] [blame] | 244 | strcpy(path + len, "config"); |
Johannes Schindelin | e24317b | 2005-10-26 01:43:03 +0200 | [diff] [blame] | 245 | |
Junio C Hamano | 4f62953 | 2005-11-25 16:03:56 -0800 | [diff] [blame] | 246 | /* Check filemode trustability */ |
Shawn O. Pearce | c869753 | 2006-12-30 23:53:55 -0500 | [diff] [blame] | 247 | filemode = TEST_FILEMODE; |
| 248 | if (TEST_FILEMODE && !lstat(path, &st1)) { |
Junio C Hamano | 4f62953 | 2005-11-25 16:03:56 -0800 | [diff] [blame] | 249 | struct stat st2; |
Shawn O. Pearce | c869753 | 2006-12-30 23:53:55 -0500 | [diff] [blame] | 250 | filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) && |
Junio C Hamano | 4f62953 | 2005-11-25 16:03:56 -0800 | [diff] [blame] | 251 | !lstat(path, &st2) && |
| 252 | st1.st_mode != st2.st_mode); |
Johannes Schindelin | e24317b | 2005-10-26 01:43:03 +0200 | [diff] [blame] | 253 | } |
Shawn O. Pearce | c869753 | 2006-12-30 23:53:55 -0500 | [diff] [blame] | 254 | git_config_set("core.filemode", filemode ? "true" : "false"); |
Shawn O. Pearce | ef0a89a | 2006-12-15 00:44:58 -0500 | [diff] [blame] | 255 | |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 256 | if (is_bare_repository()) |
Junio C Hamano | 7d1864c | 2007-01-07 02:00:28 -0800 | [diff] [blame] | 257 | git_config_set("core.bare", "true"); |
Junio C Hamano | 7d1864c | 2007-01-07 02:00:28 -0800 | [diff] [blame] | 258 | else { |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 259 | const char *work_tree = get_git_work_tree(); |
Junio C Hamano | 7d1864c | 2007-01-07 02:00:28 -0800 | [diff] [blame] | 260 | git_config_set("core.bare", "false"); |
Alex Riesen | 196055c | 2007-01-23 16:51:18 +0100 | [diff] [blame] | 261 | /* allow template config file to override the default */ |
| 262 | if (log_all_ref_updates == -1) |
| 263 | git_config_set("core.logallrefupdates", "true"); |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 264 | if (work_tree != git_work_tree_cfg) |
| 265 | git_config_set("core.worktree", work_tree); |
Junio C Hamano | 7d1864c | 2007-01-07 02:00:28 -0800 | [diff] [blame] | 266 | } |
Junio C Hamano | 75d2449 | 2007-08-31 00:25:04 -0700 | [diff] [blame] | 267 | |
| 268 | /* Check if symlink is supported in the work tree */ |
| 269 | if (!reinit) { |
| 270 | path[len] = 0; |
| 271 | strcpy(path + len, "tXXXXXX"); |
| 272 | if (!close(xmkstemp(path)) && |
| 273 | !unlink(path) && |
| 274 | !symlink("testing", path) && |
| 275 | !lstat(path, &st1) && |
| 276 | S_ISLNK(st1.st_mode)) |
| 277 | unlink(path); /* good */ |
| 278 | else |
| 279 | git_config_set("core.symlinks", "false"); |
| 280 | } |
| 281 | |
Shawn O. Pearce | ef0a89a | 2006-12-15 00:44:58 -0500 | [diff] [blame] | 282 | return reinit; |
Linus Torvalds | cad88fd | 2005-05-30 10:20:44 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Junio C Hamano | 6adcca3 | 2007-08-27 00:58:06 -0700 | [diff] [blame] | 285 | static void guess_repository_type(const char *git_dir) |
| 286 | { |
| 287 | char cwd[PATH_MAX]; |
| 288 | const char *slash; |
| 289 | |
| 290 | if (0 <= is_bare_repository_cfg) |
| 291 | return; |
| 292 | if (!git_dir) |
| 293 | return; |
| 294 | |
| 295 | /* |
| 296 | * "GIT_DIR=. git init" is always bare. |
| 297 | * "GIT_DIR=`pwd` git init" too. |
| 298 | */ |
| 299 | if (!strcmp(".", git_dir)) |
| 300 | goto force_bare; |
| 301 | if (!getcwd(cwd, sizeof(cwd))) |
| 302 | die("cannot tell cwd"); |
| 303 | if (!strcmp(git_dir, cwd)) |
| 304 | goto force_bare; |
| 305 | /* |
| 306 | * "GIT_DIR=.git or GIT_DIR=something/.git is usually not. |
| 307 | */ |
| 308 | if (!strcmp(git_dir, ".git")) |
| 309 | return; |
| 310 | slash = strrchr(git_dir, '/'); |
| 311 | if (slash && !strcmp(slash, "/.git")) |
| 312 | return; |
| 313 | |
| 314 | /* |
| 315 | * Otherwise it is often bare. At this point |
| 316 | * we are just guessing. |
| 317 | */ |
| 318 | force_bare: |
| 319 | is_bare_repository_cfg = 1; |
| 320 | return; |
| 321 | } |
| 322 | |
Junio C Hamano | d3af621 | 2005-08-06 12:50:14 -0700 | [diff] [blame] | 323 | static const char init_db_usage[] = |
Jeffrey C. Ollie | 4576518 | 2007-06-07 07:50:29 -0500 | [diff] [blame] | 324 | "git-init [-q | --quiet] [--template=<template-directory>] [--shared]"; |
Junio C Hamano | d3af621 | 2005-08-06 12:50:14 -0700 | [diff] [blame] | 325 | |
Zach Welch | 4696cb9 | 2005-04-19 21:48:15 -0700 | [diff] [blame] | 326 | /* |
| 327 | * If you want to, you can share the DB area with any number of branches. |
| 328 | * That has advantages: you can save space by sharing all the SHA1 objects. |
| 329 | * On the other hand, it might just make lookup slower and messier. You |
| 330 | * be the judge. The default case is to have one DB per managed directory. |
| 331 | */ |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 332 | int cmd_init_db(int argc, const char **argv, const char *prefix) |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 333 | { |
Linus Torvalds | cad88fd | 2005-05-30 10:20:44 -0700 | [diff] [blame] | 334 | const char *git_dir; |
Junio C Hamano | d19938a | 2005-05-09 17:57:56 -0700 | [diff] [blame] | 335 | const char *sha1_dir; |
Timo Hirvonen | c3c8835 | 2006-05-19 13:03:57 +0300 | [diff] [blame] | 336 | const char *template_dir = NULL; |
| 337 | char *path; |
Shawn O. Pearce | ef0a89a | 2006-12-15 00:44:58 -0500 | [diff] [blame] | 338 | int len, i, reinit; |
Jeffrey C. Ollie | 4576518 | 2007-06-07 07:50:29 -0500 | [diff] [blame] | 339 | int quiet = 0; |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 340 | |
Junio C Hamano | d3af621 | 2005-08-06 12:50:14 -0700 | [diff] [blame] | 341 | for (i = 1; i < argc; i++, argv++) { |
Timo Hirvonen | c3c8835 | 2006-05-19 13:03:57 +0300 | [diff] [blame] | 342 | const char *arg = argv[1]; |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 343 | if (!prefixcmp(arg, "--template=")) |
Junio C Hamano | d3af621 | 2005-08-06 12:50:14 -0700 | [diff] [blame] | 344 | template_dir = arg+11; |
Johannes Schindelin | af6e277 | 2005-12-22 23:19:37 +0100 | [diff] [blame] | 345 | else if (!strcmp(arg, "--shared")) |
Junio C Hamano | 94df250 | 2006-06-09 23:09:49 -0700 | [diff] [blame] | 346 | shared_repository = PERM_GROUP; |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 347 | else if (!prefixcmp(arg, "--shared=")) |
Junio C Hamano | 94df250 | 2006-06-09 23:09:49 -0700 | [diff] [blame] | 348 | shared_repository = git_config_perm("arg", arg+9); |
Jeffrey C. Ollie | 4576518 | 2007-06-07 07:50:29 -0500 | [diff] [blame] | 349 | else if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet")) |
| 350 | quiet = 1; |
Junio C Hamano | d3af621 | 2005-08-06 12:50:14 -0700 | [diff] [blame] | 351 | else |
Ramsay Allan Jones | 8cdf336 | 2006-08-03 16:48:41 +0100 | [diff] [blame] | 352 | usage(init_db_usage); |
Junio C Hamano | d3af621 | 2005-08-06 12:50:14 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Junio C Hamano | 6adcca3 | 2007-08-27 00:58:06 -0700 | [diff] [blame] | 355 | /* |
| 356 | * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR |
| 357 | * without --bare. Catch the error early. |
| 358 | */ |
| 359 | git_dir = getenv(GIT_DIR_ENVIRONMENT); |
| 360 | if ((!git_dir || is_bare_repository_cfg == 1) |
| 361 | && getenv(GIT_WORK_TREE_ENVIRONMENT)) |
| 362 | die("%s (or --work-tree=<directory>) not allowed without " |
| 363 | "specifying %s (or --git-dir=<directory>)", |
| 364 | GIT_WORK_TREE_ENVIRONMENT, |
| 365 | GIT_DIR_ENVIRONMENT); |
| 366 | |
| 367 | guess_repository_type(git_dir); |
| 368 | |
| 369 | if (is_bare_repository_cfg <= 0) { |
| 370 | git_work_tree_cfg = xcalloc(PATH_MAX, 1); |
| 371 | if (!getcwd(git_work_tree_cfg, PATH_MAX)) |
| 372 | die ("Cannot access current working directory."); |
| 373 | if (access(get_git_work_tree(), X_OK)) |
| 374 | die ("Cannot access work tree '%s'", |
| 375 | get_git_work_tree()); |
| 376 | } |
Matthias Lederhofer | ef6f0af | 2007-07-04 00:49:19 +0200 | [diff] [blame] | 377 | |
Linus Torvalds | cad88fd | 2005-05-30 10:20:44 -0700 | [diff] [blame] | 378 | /* |
| 379 | * Set up the default .git directory contents |
| 380 | */ |
Junio C Hamano | a9ab586 | 2005-09-09 14:48:54 -0700 | [diff] [blame] | 381 | git_dir = getenv(GIT_DIR_ENVIRONMENT); |
Shawn O. Pearce | ef0a89a | 2006-12-15 00:44:58 -0500 | [diff] [blame] | 382 | if (!git_dir) |
Linus Torvalds | cad88fd | 2005-05-30 10:20:44 -0700 | [diff] [blame] | 383 | git_dir = DEFAULT_GIT_DIR_ENVIRONMENT; |
Johannes Schindelin | af6e277 | 2005-12-22 23:19:37 +0100 | [diff] [blame] | 384 | safe_create_dir(git_dir, 0); |
Junio C Hamano | 4f62953 | 2005-11-25 16:03:56 -0800 | [diff] [blame] | 385 | |
| 386 | /* Check to see if the repository version is right. |
| 387 | * Note that a newly created repository does not have |
| 388 | * config file, so this will not fail. What we are catching |
| 389 | * is an attempt to reinitialize new repository with an old tool. |
| 390 | */ |
| 391 | check_repository_format(); |
| 392 | |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 393 | reinit = create_default_files(git_dir, template_dir); |
Linus Torvalds | cad88fd | 2005-05-30 10:20:44 -0700 | [diff] [blame] | 394 | |
| 395 | /* |
| 396 | * And set up the object store. |
| 397 | */ |
| 398 | sha1_dir = get_object_directory(); |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 399 | len = strlen(sha1_dir); |
Christopher Li | 812666c | 2005-04-26 12:00:58 -0700 | [diff] [blame] | 400 | path = xmalloc(len + 40); |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 401 | memcpy(path, sha1_dir, len); |
Zach Welch | cb126d8 | 2005-04-19 21:48:15 -0700 | [diff] [blame] | 402 | |
Johannes Schindelin | af6e277 | 2005-12-22 23:19:37 +0100 | [diff] [blame] | 403 | safe_create_dir(sha1_dir, 1); |
Linus Torvalds | f49fb35 | 2005-06-27 18:26:11 -0700 | [diff] [blame] | 404 | strcpy(path+len, "/pack"); |
Johannes Schindelin | af6e277 | 2005-12-22 23:19:37 +0100 | [diff] [blame] | 405 | safe_create_dir(path, 1); |
Junio C Hamano | d57306c | 2005-08-20 02:05:31 -0700 | [diff] [blame] | 406 | strcpy(path+len, "/info"); |
Johannes Schindelin | af6e277 | 2005-12-22 23:19:37 +0100 | [diff] [blame] | 407 | safe_create_dir(path, 1); |
| 408 | |
Junio C Hamano | 94df250 | 2006-06-09 23:09:49 -0700 | [diff] [blame] | 409 | if (shared_repository) { |
| 410 | char buf[10]; |
| 411 | /* We do not spell "group" and such, so that |
| 412 | * the configuration can be read by older version |
| 413 | * of git. |
| 414 | */ |
| 415 | sprintf(buf, "%d", shared_repository); |
| 416 | git_config_set("core.sharedrepository", buf); |
Johannes Schindelin | 11031d7 | 2006-09-21 01:07:54 +0200 | [diff] [blame] | 417 | git_config_set("receive.denyNonFastforwards", "true"); |
Junio C Hamano | 94df250 | 2006-06-09 23:09:49 -0700 | [diff] [blame] | 418 | } |
Johannes Schindelin | af6e277 | 2005-12-22 23:19:37 +0100 | [diff] [blame] | 419 | |
Jeffrey C. Ollie | 4576518 | 2007-06-07 07:50:29 -0500 | [diff] [blame] | 420 | if (!quiet) |
| 421 | printf("%s%s Git repository in %s/\n", |
| 422 | reinit ? "Reinitialized existing" : "Initialized empty", |
| 423 | shared_repository ? " shared" : "", |
| 424 | git_dir); |
Shawn O. Pearce | ef0a89a | 2006-12-15 00:44:58 -0500 | [diff] [blame] | 425 | |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 426 | return 0; |
| 427 | } |