git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 5/5] git-init: set core.worktree when GIT_WORK_TREE is specified

From
MLMatthias Lederhofer <matled@gmx.net>
Date
Mar 17, 2007, 14:45 UTC
Message-ID
<20070317144531.GE26290@moooo.ath.cx>
In-Reply-To
<20070317143452.GA21140@moooo.ath.cx>

git init will die with an error message before doing anything if the value of GIT_WORK_TREE is no valid directory. GIT_WORK_TREE is also expanded to an absolute path for the config file.

Signed-off-by: Matthias Lederhofer <matled@gmx.net>
---
 builtin-init-db.c   |   31 ++++++++++++++++++++++++++++++-
 t/t1501-worktree.sh |   15 +++++++++++++++
 2 files changed, 45 insertions(+), 1 deletions(-)
diff --git a/builtin-init-db.c b/builtin-init-db.c
index 4df9fd0..4ea8c1f 100644
--- a/builtin-init-db.c
+++ b/builtin-init-db.c
@@ -182,6 +182,7 @@ static int create_default_files(const char *git_dir, const char *template_path)
 	char repo_version_string[10];
 	int reinit;
 	int filemode;
+	const char *git_work_tree = getenv(GIT_WORKING_TREE_ENVIRONMENT);
 
 	if (len > sizeof(path)-50)
 		die("insane git directory %s", git_dir);
@@ -252,7 +253,7 @@ 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()) {
+	if (is_bare_repository() && !git_work_tree) {
 		git_config_set("core.bare", "true");
 	}
 	else {
@@ -260,6 +261,8 @@ static int create_default_files(const char *git_dir, const char *template_path)
 		/* allow template config file to override the default */
 		if (log_all_ref_updates == -1)
 		    git_config_set("core.logallrefupdates", "true");
+		if (git_work_tree)
+			git_config_set("core.worktree", git_work_tree);
 	}
 	return reinit;
 }
@@ -276,6 +279,7 @@ static const char init_db_usage[] =
 int cmd_init_db(int argc, const char **argv, const char *prefix)
 {
 	const char *git_dir;
+	const char *git_work_tree;
 	const char *sha1_dir;
 	const char *template_dir = NULL;
 	char *path;
@@ -294,6 +298,31 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
 	}
 
 	/*
+	 * check value of $GIT_WORK_TREE
+	 * if it is set it has to be a valid directory
+	 */
+	git_work_tree = getenv(GIT_WORKING_TREE_ENVIRONMENT);
+	if (git_work_tree) {
+		char cwd[PATH_MAX];
+
+		if (!getcwd(cwd, sizeof(cwd)) || cwd[0] != '/')
+			die("Unable to read current working directory");
+		if (chdir(git_work_tree))
+			die("chdir to specified working tree '%s' failed",
+				git_work_tree);
+		/* get absolute path */
+		if (git_work_tree[0] != '/') {
+			char worktree[PATH_MAX];
+			if (!getcwd(worktree, sizeof(worktree)) ||
+				worktree[0] != '/')
+				die("Unable to read current working directory");
+			setenv(GIT_WORKING_TREE_ENVIRONMENT, worktree, 1);
+		}
+		if (chdir(cwd))
+			die("Cannot come back to cwd");
+	}
+
+	/*
 	 * Set up the default .git directory contents
 	 */
 	git_dir = getenv(GIT_DIR_ENVIRONMENT);
diff --git a/t/t1501-worktree.sh b/t/t1501-worktree.sh
index bc08994..4db4b6a 100755
--- a/t/t1501-worktree.sh
+++ b/t/t1501-worktree.sh
@@ -88,4 +88,19 @@ test_expect_success 'repository/worktree: --is-bare-repository' \
 test_expect_success 'repository/worktree: --is-inside-git-dir' \
 	'test "false" = "$(git rev-parse --is-inside-git-dir)"'
 
+# git init
+cd "$top" || exit 1
+export GIT_DIR=$(pwd)/repository2
+test_expect_failure 'git --work-tree non-existent init' \
+	'git --work-tree non-existent init'
+test_expect_success 'git --work-tree working/tree init' \
+	'git --work-tree working/tree init'
+test_expect_success 'config knows core.worktree' \
+	'git config core.worktree'
+git config --unset core.worktree || exit 1
+test_expect_success 'git --work-tree `pwd`/working init' \
+	'git --work-tree "$(pwd)"/working init'
+test_expect_success 'config knows core.worktree' \
+	'git config core.worktree'
+
 test_done
-- 
1.5.0.4.414.g32da9
Previous: Matthias LederhoferNext: Junio C Hamano
Message 14 of 18 in “git-init: set core.workdir when GIT_WORK_DIR is specified”
  1. git-init: set core.workdir when GIT_WORK_DIR is specifiedMatthias Lederhofer, Mar 17, 2007
  2. Junio C HamanoMar 17, 2007
  3. Matthias LederhoferMar 17, 2007
  4. Matthias LederhoferMar 17, 2007
  5. 1/5 rev-parse: --is-bare-repository optionMatthias Lederhofer, Mar 17, 2007
  6. 2/5 test git-rev-parseMatthias Lederhofer, Mar 17, 2007
  7. test git-rev-parseMatthias Lederhofer, Mar 27, 2007
  8. 3/5 introduce GIT_WORK_TREE environment variableMatthias Lederhofer, Mar 17, 2007
  9. Matthias LederhoferMar 18, 2007
  10. Matthias LederhoferMar 18, 2007
  11. Matthias LederhoferMar 18, 2007
  12. Junio C HamanoMar 18, 2007
  13. 4/5 test GIT_WORK_TREEMatthias Lederhofer, Mar 17, 2007
  14. 5/5 git-init: set core.worktree when GIT_WORK_TREE is specifiedMatthias Lederhofer, Mar 17, 2007
  15. Junio C HamanoMar 18, 2007
  16. Matthias LederhoferMar 18, 2007
  17. Matthias LederhoferMar 18, 2007
  18. Johannes SchindelinMar 19, 2007

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.