/* * Attempt to resolve and set the provided 'gitdir' for repository 'repo'. * Return 0 upon success and a non-zero value upon failure. */ static int repo_init_gitdir(struct repository *repo, const char *gitdir) { int ret = 0; int error = 0; char *abspath = NULL; const char *resolved_gitdir; struct set_gitdir_args args = { NULL }; abspath = real_pathdup(gitdir, 0); if (!abspath) { ret = -1; goto out; } /* 'gitdir' must reference the gitdir directly */ resolved_gitdir = resolve_gitdir_gently(abspath, &error); if (!resolved_gitdir) { ret = -1; goto out; } repo_set_gitdir(repo, resolved_gitdir, &args); out: free(abspath); return ret; }
struct worktree *find_worktree(struct worktree **list, const char *prefix, const char *arg) { struct worktree *wt; char *path; char *to_free = NULL; if ((wt = find_worktree_by_suffix(list, arg))) return wt; if (prefix) arg = to_free = prefix_filename(prefix, arg); path = real_pathdup(arg, 0); if (!path) { free(to_free); return NULL; } for (; *list; list++) if (!fspathcmp(path, real_path((*list)->path))) break; free(path); free(to_free); return *list; }
struct worktree *find_worktree(struct worktree **list, const char *prefix, const char *arg) { struct worktree *wt; char *path; if ((wt = find_worktree_by_suffix(list, arg))) return wt; arg = prefix_filename(prefix, strlen(prefix), arg); path = real_pathdup(arg); for (; *list; list++) if (!fspathcmp(path, real_path((*list)->path))) break; free(path); return *list; }
/* * If you want to, you can share the DB area with any number of branches. * That has advantages: you can save space by sharing all the SHA1 objects. * On the other hand, it might just make lookup slower and messier. You * be the judge. The default case is to have one DB per managed directory. */ int cmd_init_db(int argc, const char **argv, const char *prefix) { const char *git_dir; const char *real_git_dir = NULL; const char *work_tree; const char *template_dir = NULL; unsigned int flags = 0; const struct option init_db_options[] = { OPT_STRING(0, "template", &template_dir, N_("template-directory"), N_("directory from which templates will be used")), OPT_SET_INT(0, "bare", &is_bare_repository_cfg, N_("create a bare repository"), 1), { OPTION_CALLBACK, 0, "shared", &init_shared_repository, N_("permissions"), N_("specify that the git repository is to be shared amongst several users"), PARSE_OPT_OPTARG | PARSE_OPT_NONEG, shared_callback, 0}, OPT_BIT('q', "quiet", &flags, N_("be quiet"), INIT_DB_QUIET), OPT_STRING(0, "separate-git-dir", &real_git_dir, N_("gitdir"), N_("separate git dir from working tree")), OPT_END() }; argc = parse_options(argc, argv, prefix, init_db_options, init_db_usage, 0); if (real_git_dir && !is_absolute_path(real_git_dir)) real_git_dir = real_pathdup(real_git_dir, 1); if (argc == 1) { int mkdir_tried = 0; retry: if (chdir(argv[0]) < 0) { if (!mkdir_tried) { int saved; /* * At this point we haven't read any configuration, * and we know shared_repository should always be 0; * but just in case we play safe. */ saved = get_shared_repository(); set_shared_repository(0); switch (safe_create_leading_directories_const(argv[0])) { case SCLD_OK: case SCLD_PERMS: break; case SCLD_EXISTS: errno = EEXIST; /* fallthru */ default: die_errno(_("cannot mkdir %s"), argv[0]); break; } set_shared_repository(saved); if (mkdir(argv[0], 0777) < 0) die_errno(_("cannot mkdir %s"), argv[0]); mkdir_tried = 1; goto retry; } die_errno(_("cannot chdir to %s"), argv[0]); } } else if (0 < argc) { usage(init_db_usage[0]); } if (is_bare_repository_cfg == 1) { char *cwd = xgetcwd(); setenv(GIT_DIR_ENVIRONMENT, cwd, argc > 0); free(cwd); } if (init_shared_repository != -1) set_shared_repository(init_shared_repository); /* * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR * without --bare. Catch the error early. */ git_dir = xstrdup_or_null(getenv(GIT_DIR_ENVIRONMENT)); work_tree = xstrdup_or_null(getenv(GIT_WORK_TREE_ENVIRONMENT)); if ((!git_dir || is_bare_repository_cfg == 1) && work_tree) die(_("%s (or --work-tree=<directory>) not allowed without " "specifying %s (or --git-dir=<directory>)"), GIT_WORK_TREE_ENVIRONMENT, GIT_DIR_ENVIRONMENT); /* * Set up the default .git directory contents */ if (!git_dir) git_dir = DEFAULT_GIT_DIR_ENVIRONMENT; if (is_bare_repository_cfg < 0) is_bare_repository_cfg = guess_repository_type(git_dir); if (!is_bare_repository_cfg) { const char *git_dir_parent = strrchr(git_dir, '/'); if (git_dir_parent) { char *rel = xstrndup(git_dir, git_dir_parent - git_dir); git_work_tree_cfg = real_pathdup(rel, 1); free(rel); } if (!git_work_tree_cfg) git_work_tree_cfg = xgetcwd(); if (work_tree) set_git_work_tree(work_tree); else set_git_work_tree(git_work_tree_cfg); if (access(get_git_work_tree(), X_OK)) die_errno (_("Cannot access work tree '%s'"), get_git_work_tree()); } else { if (work_tree) set_git_work_tree(work_tree); } UNLEAK(real_git_dir); UNLEAK(git_dir); UNLEAK(work_tree); flags |= INIT_DB_EXIST_OK; return init_db(git_dir, real_git_dir, template_dir, flags); }
int init_db(const char *git_dir, const char *real_git_dir, const char *template_dir, unsigned int flags) { int reinit; int exist_ok = flags & INIT_DB_EXIST_OK; char *original_git_dir = real_pathdup(git_dir, 1); if (real_git_dir) { struct stat st; if (!exist_ok && !stat(git_dir, &st)) die(_("%s already exists"), git_dir); if (!exist_ok && !stat(real_git_dir, &st)) die(_("%s already exists"), real_git_dir); set_git_dir(real_path(real_git_dir)); git_dir = get_git_dir(); separate_git_dir(git_dir, original_git_dir); } else { set_git_dir(real_path(git_dir)); git_dir = get_git_dir(); } startup_info->have_repository = 1; /* Just look for `init.templatedir` and `core.hidedotfiles` */ git_config(git_init_db_config, NULL); safe_create_dir(git_dir, 0); init_is_bare_repository = is_bare_repository(); /* Check to see if the repository version is right. * Note that a newly created repository does not have * config file, so this will not fail. What we are catching * is an attempt to reinitialize new repository with an old tool. */ check_repository_format(); reinit = create_default_files(template_dir, original_git_dir); create_object_directory(); if (get_shared_repository()) { char buf[10]; /* We do not spell "group" and such, so that * the configuration can be read by older version * of git. Note, we use octal numbers for new share modes, * and compatibility values for PERM_GROUP and * PERM_EVERYBODY. */ if (get_shared_repository() < 0) /* force to the mode value */ xsnprintf(buf, sizeof(buf), "0%o", -get_shared_repository()); else if (get_shared_repository() == PERM_GROUP) xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_GROUP); else if (get_shared_repository() == PERM_EVERYBODY) xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_EVERYBODY); else BUG("invalid value for shared_repository"); git_config_set("core.sharedrepository", buf); git_config_set("receive.denyNonFastforwards", "true"); } if (!(flags & INIT_DB_QUIET)) { int len = strlen(git_dir); if (reinit) printf(get_shared_repository() ? _("Reinitialized existing shared Git repository in %s%s\n") : _("Reinitialized existing Git repository in %s%s\n"), git_dir, len && git_dir[len-1] != '/' ? "/" : ""); else printf(get_shared_repository() ? _("Initialized empty shared Git repository in %s%s\n") : _("Initialized empty Git repository in %s%s\n"), git_dir, len && git_dir[len-1] != '/' ? "/" : ""); } free(original_git_dir); return 0; }
void repo_set_worktree(struct repository *repo, const char *path) { repo->worktree = real_pathdup(path, 1); }