/* * First, one directory to try is determined by the following algorithm. * * (0) If "strict" is given, the path is used as given and no DWIM is * done. Otherwise: * (1) "~/path" to mean path under the running user's home directory; * (2) "~user/path" to mean path under named user's home directory; * (3) "relative/path" to mean cwd relative directory; or * (4) "/absolute/path" to mean absolute directory. * * Unless "strict" is given, we check "%s/.git", "%s", "%s.git/.git", "%s.git" * in this order. We select the first one that is a valid git repository, and * chdir() to it. If none match, or we fail to chdir, we return NULL. * * If all goes well, we return the directory we used to chdir() (but * before ~user is expanded), avoiding getcwd() resolving symbolic * links. User relative paths are also returned as they are given, * except DWIM suffixing. */ const char *enter_repo(const char *path, int strict) { static struct strbuf validated_path = STRBUF_INIT; static struct strbuf used_path = STRBUF_INIT; if (!path) return NULL; if (!strict) { static const char *suffix[] = { "/.git", "", ".git/.git", ".git", NULL, }; const char *gitfile; int len = strlen(path); int i; while ((1 < len) && (path[len-1] == '/')) len--; /* * We can handle arbitrary-sized buffers, but this remains as a * sanity check on untrusted input. */ if (PATH_MAX <= len) return NULL; strbuf_reset(&used_path); strbuf_reset(&validated_path); strbuf_add(&used_path, path, len); strbuf_add(&validated_path, path, len); if (used_path.buf[0] == '~') { char *newpath = expand_user_path(used_path.buf); if (!newpath) return NULL; strbuf_attach(&used_path, newpath, strlen(newpath), strlen(newpath)); } for (i = 0; suffix[i]; i++) { struct stat st; size_t baselen = used_path.len; strbuf_addstr(&used_path, suffix[i]); if (!stat(used_path.buf, &st) && (S_ISREG(st.st_mode) || (S_ISDIR(st.st_mode) && is_git_directory(used_path.buf)))) { strbuf_addstr(&validated_path, suffix[i]); break; } strbuf_setlen(&used_path, baselen); } if (!suffix[i]) return NULL; gitfile = read_gitfile(used_path.buf); if (gitfile) { strbuf_reset(&used_path); strbuf_addstr(&used_path, gitfile); } if (chdir(used_path.buf)) return NULL; path = validated_path.buf; } else { const char *gitfile = read_gitfile(path); if (gitfile) path = gitfile; if (chdir(path)) return NULL; } if (is_git_directory(".")) { set_git_dir("."); check_repository_format(); return path; } return NULL; }
const char *setup_git_directory(void) { const char *retval = setup_git_directory_gently(NULL); check_repository_format(); return retval; }
/* * First, one directory to try is determined by the following algorithm. * * (0) If "strict" is given, the path is used as given and no DWIM is * done. Otherwise: * (1) "~/path" to mean path under the running user's home directory; * (2) "~user/path" to mean path under named user's home directory; * (3) "relative/path" to mean cwd relative directory; or * (4) "/absolute/path" to mean absolute directory. * * Unless "strict" is given, we try access() for existence of "%s.git/.git", * "%s/.git", "%s.git", "%s" in this order. The first one that exists is * what we try. * * Second, we try chdir() to that. Upon failure, we return NULL. * * Then, we try if the current directory is a valid git repository. * Upon failure, we return NULL. * * If all goes well, we return the directory we used to chdir() (but * before ~user is expanded), avoiding getcwd() resolving symbolic * links. User relative paths are also returned as they are given, * except DWIM suffixing. */ const char *enter_repo(const char *path, int strict) { static char used_path[PATH_MAX]; static char validated_path[PATH_MAX]; if (!path) return NULL; if (!strict) { static const char *suffix[] = { "/.git", "", ".git/.git", ".git", NULL, }; const char *gitfile; int len = strlen(path); int i; while ((1 < len) && (path[len-1] == '/')) len--; if (PATH_MAX <= len) return NULL; strncpy(used_path, path, len); used_path[len] = 0 ; strcpy(validated_path, used_path); if (used_path[0] == '~') { char *newpath = expand_user_path(used_path); if (!newpath || (PATH_MAX - 10 < strlen(newpath))) { free(newpath); return NULL; } /* * Copy back into the static buffer. A pity * since newpath was not bounded, but other * branches of the if are limited by PATH_MAX * anyway. */ strcpy(used_path, newpath); free(newpath); } else if (PATH_MAX - 10 < len) return NULL; len = strlen(used_path); for (i = 0; suffix[i]; i++) { struct stat st; strcpy(used_path + len, suffix[i]); if (!stat(used_path, &st) && (S_ISREG(st.st_mode) || (S_ISDIR(st.st_mode) && is_git_directory(used_path)))) { strcat(validated_path, suffix[i]); break; } } if (!suffix[i]) return NULL; gitfile = read_gitfile(used_path) ; if (gitfile) strcpy(used_path, gitfile); if (chdir(used_path)) return NULL; path = validated_path; } else if (chdir(path)) return NULL; if (access("objects", X_OK) == 0 && access("refs", X_OK) == 0 && validate_headref("HEAD") == 0) { set_git_dir("."); check_repository_format(); return path; } return NULL; }
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; }