@matheusbsbs / matheus-bagliano / commits / ef6f0af2b6
Fork 0

git-init: set core.worktree if GIT_WORK_TREE is specified

Now you can do the following to create a repository which has a separate working tree: /tmp/foo$ export GIT_DIR=/tmp/bar /tmp/foo$ git --work-tree . init Initialized empty Git repository in /tmp/bar/ /tmp/foo$ git config core.worktree /tmp/foo Signed-off-by: Matthias Lederhofer <matled@gmx.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Matthias Lederhofer committed Jul 4, 2007 at 00:49 UTC ef6f0af2b6975e6568542123d015aa7e39bea0c3
1 file changed +37 -3
builtin-init-db.c
+37 -3
@@ -174,7 +174,36 @@ static void copy_templates(const char *git_dir, int len, const char *template_di
174 closedir(dir);
175 }
176
177 -static int create_default_files(const char *git_dir, const char *template_path)
177 +/*
178 + * Get the full path to the working tree specified in $GIT_WORK_TREE
179 + * or NULL if no working tree is specified.
180 + */
181 +static const char *get_work_tree(void)
182 +{
183 + const char *git_work_tree;
184 + char cwd[PATH_MAX];
185 + static char worktree[PATH_MAX];
186 +
187 + git_work_tree = getenv(GIT_WORK_TREE_ENVIRONMENT);
188 + if (!git_work_tree)
189 + return NULL;
190 + if (!getcwd(cwd, sizeof(cwd)))
191 + die("Unable to read current working directory");
192 + if (chdir(git_work_tree))
193 + die("Cannot change directory to specified working tree '%s'",
194 + git_work_tree);
195 + if (git_work_tree[0] != '/') {
196 + if (!getcwd(worktree, sizeof(worktree)))
197 + die("Unable to read current working directory");
198 + git_work_tree = worktree;
199 + }
200 + if (chdir(cwd))
201 + die("Cannot come back to cwd");
202 + return git_work_tree;
203 +}
204 +
205 +static int create_default_files(const char *git_dir, const char *git_work_tree,
206 + const char *template_path)
207 {
208 unsigned len = strlen(git_dir);
209 static char path[PATH_MAX];
@@ -253,7 +282,7 @@ static int create_default_files(const char *git_dir, const char *template_path)
282 }
283 git_config_set("core.filemode", filemode ? "true" : "false");
284
256 - if (is_bare_repository()) {
285 + if (is_bare_repository() && !git_work_tree) {
286 git_config_set("core.bare", "true");
287 }
288 else {
@@ -261,6 +290,8 @@ static int create_default_files(const char *git_dir, const char *template_path)
290 /* allow template config file to override the default */
291 if (log_all_ref_updates == -1)
292 git_config_set("core.logallrefupdates", "true");
293 + if (git_work_tree)
294 + git_config_set("core.worktree", git_work_tree);
295 }
296 return reinit;
297 }
@@ -277,6 +308,7 @@ static const char init_db_usage[] =
308 int cmd_init_db(int argc, const char **argv, const char *prefix)
309 {
310 const char *git_dir;
311 + const char *git_work_tree;
312 const char *sha1_dir;
313 const char *template_dir = NULL;
314 char *path;
@@ -297,6 +329,8 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
329 usage(init_db_usage);
330 }
331
332 + git_work_tree = get_work_tree();
333 +
334 /*
335 * Set up the default .git directory contents
336 */
@@ -312,7 +346,7 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
346 */
347 check_repository_format();
348
315 - reinit = create_default_files(git_dir, template_dir);
349 + reinit = create_default_files(git_dir, git_work_tree, template_dir);
350
351 /*
352 * And set up the object store.