Add a function to set a non-default work tree
This function may only be used before the work tree is used. Signed-off-by: Daniel Barkalow <barkalow@iabervon.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Daniel Barkalow committed
Apr 27, 2008 at 13:39 UTC
19757d80e522e9f04c125ad5977c3a79db99b192
2 files changed
+21
-4
cache.h
+1
@@ -311,6 +311,7 @@ extern char *get_index_file(void);
311
extern char *get_graft_file(void);
312
extern int set_git_dir(const char *path);
313
extern const char *get_git_work_tree(void);
314
+extern void set_git_work_tree(const char *tree);
315
316
#define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES"
317
environment.c
+20
-4
@@ -41,7 +41,7 @@ enum branch_track git_branch_track = BRANCH_TRACK_REMOTE;
41
42
/* This is set by setup_git_dir_gently() and/or git_default_config() */
43
char *git_work_tree_cfg;
44
-static const char *work_tree;
44
+static char *work_tree;
45
46
static const char *git_dir;
47
static char *git_object_dir, *git_index_file, *git_refs_dir, *git_graft_file;
@@ -81,10 +81,26 @@ const char *get_git_dir(void)
81
return git_dir;
82
}
83
84
+static int git_work_tree_initialized;
85
+
86
+/*
87
+ * Note. This works only before you used a work tree. This was added
88
+ * primarily to support git-clone to work in a new repository it just
89
+ * created, and is not meant to flip between different work trees.
90
+ */
91
+void set_git_work_tree(const char *new_work_tree)
92
+{
93
+ if (is_bare_repository_cfg >= 0)
94
+ die("cannot set work tree after initialization");
95
+ git_work_tree_initialized = 1;
96
+ free(work_tree);
97
+ work_tree = xstrdup(make_absolute_path(new_work_tree));
98
+ is_bare_repository_cfg = 0;
99
+}
100
+
101
const char *get_git_work_tree(void)
102
{
86
- static int initialized = 0;
87
- if (!initialized) {
103
+ if (!git_work_tree_initialized) {
104
work_tree = getenv(GIT_WORK_TREE_ENVIRONMENT);
105
/* core.bare = true overrides implicit and config work tree */
106
if (!work_tree && is_bare_repository_cfg < 1) {
@@ -94,7 +110,7 @@ const char *get_git_work_tree(void)
110
work_tree = xstrdup(make_absolute_path(git_path(work_tree)));
111
} else if (work_tree)
112
work_tree = xstrdup(make_absolute_path(work_tree));
97
- initialized = 1;
113
+ git_work_tree_initialized = 1;
114
if (work_tree)
115
is_bare_repository_cfg = 0;
116
}