[PATCH] git-init: set up GIT_DIR/workdir if GIT_WORK_DIR is set
From: Matthias Lederhofer <hidden>
Date: 2016-06-15 22:42:59
Subsystem:
the rest · Maintainer:
Linus Torvalds
git-init will always put an absolute path in GIT_DIR/workdir, relative paths are resolved from the directory git-init was called from. Signed-off-by: Matthias Lederhofer <redacted> --- I found this static char path[PATH_MAX] even though path is neither reused nor returned. If there is any reason to have this as a static buffer please change this back to 'static char' --- builtin-init-db.c | 40 ++++++++++++++++++++++++++++++++++++++-- 1 files changed, 38 insertions(+), 2 deletions(-)
diff --git a/builtin-init-db.c b/builtin-init-db.c
index 4df9fd0..f0b4444 100644
--- a/builtin-init-db.c
+++ b/builtin-init-db.c@@ -176,12 +176,13 @@ static void copy_templates(const char *git_dir, int len, const char *template_di static int create_default_files(const char *git_dir, const char *template_path) { unsigned len = strlen(git_dir); - static char path[PATH_MAX]; + char path[PATH_MAX], workdir[PATH_MAX]; unsigned char sha1[20]; struct stat st1; char repo_version_string[10]; int reinit; int filemode; + const char *gitwd = getenv(GIT_WORKING_DIR_ENVIRONMENT); if (len > sizeof(path)-50) die("insane git directory %s", git_dir);
@@ -252,7 +253,42 @@ static int create_default_files(const char *git_dir, const char *template_path) } git_config_set("core.filemode", filemode ? "true" : "false"); - if (is_bare_repository()) { + /* make gitwd an absolute path */ + if (gitwd && gitwd[0] != '/') { + char cwd[PATH_MAX]; + + if (!getcwd(cwd, sizeof(cwd)) || cwd[0] != '/') + die("Unable to read current working directory"); + if (chdir(gitwd)) { + fprintf(stderr, "warning: chdir to specified relative " + "working directory failed, ignoring\n"); + gitwd = NULL; + } + else if (!getcwd(workdir, sizeof(workdir)) || workdir[0] != '/') + die("Unable to read current working directory"); + else { + gitwd = workdir; + if (chdir(cwd)) + die("Cannot come back to cwd"); + } + } + + if (gitwd) { + FILE *fp = NULL; + + path[len] = 0; + strcpy(path + len, "workdir"); + if (!(fp = fopen(path, "w"))) + die("Cannot open GIT_DIR/workdir"); + fprintf(fp, "%s\n", gitwd); + fclose(fp); + + git_config_set("core.bare", "false"); + /* allow template config file to override the default */ + if (log_all_ref_updates == -1) + git_config_set("core.logallrefupdates", "true"); + } + else if (is_bare_repository()) { git_config_set("core.bare", "true"); } else {
--
1.5.0.3.1006.gd633