Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/builtin/init-db.c # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / builtin / init-db.c` 263 lines ยท 7962 bytes Raw Blame History 1 /* 2 * GIT - The information manager from hell 3 * 4 * Copyright (C) Linus Torvalds, 2005 5 */ 6 #define USE_THE_REPOSITORY_VARIABLE 7 #include "builtin.h" 8 #include "abspath.h" 9 #include "environment.h" 10 #include "gettext.h" 11 #include "parse-options.h" 12 #include "path.h" 13 #include "refs.h" 14 #include "setup.h" 15 #include "strbuf.h" 16 17 static int guess_repository_type(const char *git_dir) 18 { 19 const char *slash; 20 char *cwd; 21 int cwd_is_git_dir; 22 23 /* 24 * "GIT_DIR=. git init" is always bare. 25 * "GIT_DIR=`pwd` git init" too. 26 */ 27 if (!strcmp(".", git_dir)) 28 return 1; 29 cwd = xgetcwd(); 30 cwd_is_git_dir = !strcmp(git_dir, cwd); 31 free(cwd); 32 if (cwd_is_git_dir) 33 return 1; 34 /* 35 * "GIT_DIR=.git or GIT_DIR=something/.git is usually not. 36 */ 37 if (!strcmp(git_dir, ".git")) 38 return 0; 39 slash = strrchr(git_dir, '/'); 40 if (slash && !strcmp(slash, "/.git")) 41 return 0; 42 43 /* 44 * Otherwise it is often bare. At this point 45 * we are just guessing. 46 */ 47 return 1; 48 } 49 50 static int shared_callback(const struct option *opt, const char *arg, int unset) 51 { 52 BUG_ON_OPT_NEG(unset); 53 *((int *) opt->value) = (arg) ? git_config_perm("arg", arg) : PERM_GROUP; 54 return 0; 55 } 56 57 static const char *const init_db_usage[] = { 58 N_("git init [-q | --quiet] [--bare] [--template=]\n" 59 " [--separate-git-dir ] [--object-format=]\n" 60 " [--ref-storage-format=] [--object-storage-format=]\n" 61 " [-b | --initial-branch=]\n" 62 " [--shared[=]] []"), 63 NULL 64 }; 65 66 /* 67 * If you want to, you can share the DB area with any number of branches. 68 * That has advantages: you can save space by sharing all the SHA1 objects. 69 * On the other hand, it might just make lookup slower and messier. You 70 * be the judge. The default case is to have one DB per managed directory. 71 */ 72 int cmd_init_db(int argc, 73 const char **argv, 74 const char *prefix, 75 struct repository *repo UNUSED) 76 { 77 char *git_dir; 78 const char *real_git_dir = NULL; 79 char *real_git_dir_to_free = NULL; 80 char *work_tree = NULL; 81 const char *template_dir = NULL; 82 char *template_dir_to_free = NULL; 83 int quiet = 0; 84 int bare = startup_info->force_bare_repository ? 1 : -1; 85 const char *object_format = NULL; 86 const char *ref_storage_format_uri = NULL; 87 const char *object_storage_format_uri = NULL; 88 const char *initial_branch = NULL; 89 int hash_algo = GIT_HASH_UNKNOWN; 90 int init_shared_repository = -1; 91 const struct option init_db_options[] = { 92 OPT_STRING(0, "template", &template_dir, N_("template-directory"), 93 N_("directory from which templates will be used")), 94 OPT_SET_INT(0, "bare", &bare, 95 N_("create a bare repository"), 1), 96 { 97 .type = OPTION_CALLBACK, 98 .long_name = "shared", 99 .value = &init_shared_repository, 100 .argh = N_("permissions"), 101 .help = N_("specify that the git repository is to be shared amongst several users"), 102 .flags = PARSE_OPT_OPTARG | PARSE_OPT_NONEG, 103 .callback = shared_callback 104 }, 105 OPT_BOOL('q', "quiet", &quiet, N_("be quiet")), 106 OPT_STRING(0, "separate-git-dir", &real_git_dir, N_("gitdir"), 107 N_("separate git dir from working tree")), 108 OPT_STRING('b', "initial-branch", &initial_branch, N_("name"), 109 N_("override the name of the initial branch")), 110 OPT_STRING(0, "object-format", &object_format, N_("hash"), 111 N_("specify the hash algorithm to use")), 112 OPT_STRING(0, "ref-storage-format", &ref_storage_format_uri, N_("format"), 113 N_("specify the reference storage format to use")), 114 OPT_ALIAS_F(0, "ref-format", "ref-storage-format", PARSE_OPT_HIDDEN), 115 OPT_STRING(0, "object-storage-format", &object_storage_format_uri, N_("format"), 116 N_("specify the object storage format to use")), 117 OPT_END() 118 }; 119 int reinit; 120 121 argc = parse_options(argc, argv, prefix, init_db_options, init_db_usage, 0); 122 123 if (real_git_dir && bare == 1) 124 die(_("options '%s' and '%s' cannot be used together"), "--separate-git-dir", "--bare"); 125 126 if (real_git_dir && !is_absolute_path(real_git_dir)) 127 real_git_dir = real_git_dir_to_free = real_pathdup(real_git_dir, 1); 128 129 if (template_dir && *template_dir && !is_absolute_path(template_dir)) 130 template_dir = template_dir_to_free = absolute_pathdup(template_dir); 131 132 if (argc == 1) { 133 int mkdir_tried = 0; 134 retry: 135 if (chdir(argv[0]) < 0) { 136 if (!mkdir_tried) { 137 switch (safe_create_leading_directories_no_share_const(argv[0])) { 138 case SCLD_OK: 139 case SCLD_PERMS: 140 break; 141 case SCLD_EXISTS: 142 errno = EEXIST; 143 /* fallthru */ 144 default: 145 die_errno(_("cannot mkdir %s"), argv[0]); 146 break; 147 } 148 149 if (mkdir(argv[0], 0777) < 0) 150 die_errno(_("cannot mkdir %s"), argv[0]); 151 mkdir_tried = 1; 152 goto retry; 153 } 154 die_errno(_("cannot chdir to %s"), argv[0]); 155 } 156 } else if (0 < argc) { 157 usage(init_db_usage[0]); 158 } 159 if (bare == 1) { 160 char *cwd = xgetcwd(); 161 setenv(GIT_DIR_ENVIRONMENT, cwd, argc > 0); 162 free(cwd); 163 } 164 165 if (object_format) { 166 hash_algo = hash_algo_by_name(object_format); 167 if (hash_algo == GIT_HASH_UNKNOWN) 168 die(_("unknown hash algorithm '%s'"), object_format); 169 } 170 171 /* 172 * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR 173 * without --bare. Catch the error early. 174 */ 175 git_dir = xstrdup_or_null(getenv(GIT_DIR_ENVIRONMENT)); 176 work_tree = xstrdup_or_null(getenv(GIT_WORK_TREE_ENVIRONMENT)); 177 if ((!git_dir || bare == 1) && work_tree) 178 die(_("%s (or --work-tree=) not allowed without " 179 "specifying %s (or --git-dir=)"), 180 GIT_WORK_TREE_ENVIRONMENT, 181 GIT_DIR_ENVIRONMENT); 182 183 /* 184 * Set up the default .git directory contents 185 */ 186 if (!git_dir) 187 git_dir = xstrdup(DEFAULT_GIT_DIR_ENVIRONMENT); 188 189 /* 190 * When --separate-git-dir is used inside a linked worktree, take 191 * care to ensure that the common .git/ directory is relocated, not 192 * the worktree-specific .git/worktrees// directory. 193 */ 194 if (real_git_dir) { 195 int err; 196 const char *p; 197 struct strbuf sb = STRBUF_INIT; 198 199 p = read_gitfile_gently(git_dir, &err); 200 if (p && get_common_dir(&sb, p)) { 201 struct strbuf mainwt = STRBUF_INIT; 202 203 strbuf_addbuf(&mainwt, &sb); 204 strbuf_strip_suffix(&mainwt, "/.git"); 205 if (chdir(mainwt.buf) < 0) 206 die_errno(_("cannot chdir to %s"), mainwt.buf); 207 strbuf_release(&mainwt); 208 free(git_dir); 209 git_dir = strbuf_detach(&sb, NULL); 210 } 211 strbuf_release(&sb); 212 } 213 214 if (bare < 0) 215 bare = guess_repository_type(git_dir); 216 217 if (!bare) { 218 const char *git_dir_parent = strrchr(git_dir, '/'); 219 220 if (!work_tree) { 221 if (git_dir_parent) { 222 char *rel = xstrndup(git_dir, git_dir_parent - git_dir); 223 work_tree = real_pathdup(rel, 1); 224 free(rel); 225 } else { 226 work_tree = xgetcwd(); 227 } 228 } 229 230 if (access(work_tree, X_OK)) 231 die_errno (_("Cannot access work tree '%s'"), work_tree); 232 } else if (real_git_dir) { 233 die(_("--separate-git-dir incompatible with bare repository")); 234 } 235 236 create_repository(the_repository, git_dir, real_git_dir, work_tree, 237 template_dir, hash_algo, ref_storage_format_uri, 238 object_storage_format_uri, init_shared_repository, &reinit); 239 create_reference_database(the_repository, initial_branch, quiet); 240 create_object_database(the_repository, NULL); 241 242 if (!quiet) { 243 const char *full_git_dir = repo_get_git_dir(the_repository); 244 size_t len = strlen(full_git_dir); 245 246 if (reinit) 247 printf(repo_settings_get_shared_repository(the_repository) 248 ? _("Reinitialized existing shared Git repository in %s%s\n") 249 : _("Reinitialized existing Git repository in %s%s\n"), 250 full_git_dir, len && full_git_dir[len-1] != '/' ? "/" : ""); 251 else 252 printf(repo_settings_get_shared_repository(the_repository) 253 ? _("Initialized empty shared Git repository in %s%s\n") 254 : _("Initialized empty Git repository in %s%s\n"), 255 full_git_dir, len && full_git_dir[len-1] != '/' ? "/" : ""); 256 } 257 258 free(template_dir_to_free); 259 free(real_git_dir_to_free); 260 free(work_tree); 261 free(git_dir); 262 return 0; 263 }