@matheusbsbs / matheus-bagliano / commits / 892c41b98a
Fork 0

introduce GIT_WORK_TREE to specify the work tree

setup_gdg is used as abbreviation for setup_git_directory_gently. The work tree can be specified using the environment variable GIT_WORK_TREE and the config option core.worktree (the environment variable has precendence over the config option). Additionally there is a command line option --work-tree which sets the environment variable. setup_gdg does the following now: GIT_DIR unspecified repository in .git directory parent directory of the .git directory is used as work tree, GIT_WORK_TREE is ignored GIT_DIR unspecified repository in cwd GIT_DIR is set to cwd see the cases with GIT_DIR specified what happens next and also see the note below GIT_DIR specified GIT_WORK_TREE/core.worktree unspecified cwd is used as work tree GIT_DIR specified GIT_WORK_TREE/core.worktree specified the specified work tree is used Note on the case where GIT_DIR is unspecified and repository is in cwd: GIT_WORK_TREE is used but is_inside_git_dir is always true. I did it this way because setup_gdg might be called multiple times (e.g. when doing alias expansion) and in successive calls setup_gdg should do the same thing every time. Meaning of is_bare/is_inside_work_tree/is_inside_git_dir: (1) is_bare_repository A repository is bare if core.bare is true or core.bare is unspecified and the name suggests it is bare (directory not named .git). The bare option disables a few protective checks which are useful with a working tree. Currently this changes if a repository is bare: updates of HEAD are allowed git gc packs the refs the reflog is disabled by default (2) is_inside_work_tree True if the cwd is inside the associated working tree (if there is one), false otherwise. (3) is_inside_git_dir True if the cwd is inside the git directory, false otherwise. Before this patch is_inside_git_dir was always true for bare repositories. When setup_gdg finds a repository git_config(git_default_config) is always called. This ensure that is_bare_repository makes use of core.bare and does not guess even though core.bare is specified. inside_work_tree and inside_git_dir are set if setup_gdg finds a repository. The is_inside_work_tree and is_inside_git_dir functions will die if they are called before a successful call to setup_gdg. Signed-off-by: Matthias Lederhofer <matled@gmx.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Matthias Lederhofer committed Jun 6, 2007 at 09:10 UTC 892c41b98ae2e6baf3aa13901cb10db9ac67d2f3
9 files changed +204 -57
Documentation/config.txt
+7
@@ -172,6 +172,13 @@ repository that ends in "/.git" is assumed to be not bare (bare =
172 false), while all other repositories are assumed to be bare (bare
173 = true).
174
175 +core.worktree::
176 + Set the path to the working tree. The value will not be
177 + used in combination with repositories found automatically in
178 + a .git directory (i.e. $GIT_DIR is not set).
179 + This can be overriden by the GIT_WORK_TREE environment
180 + variable and the '--work-tree' command line option.
181 +
182 core.logAllRefUpdates::
183 Updates to a ref <ref> is logged to the file
184 "$GIT_DIR/logs/<ref>", by appending the new and old
Documentation/git-rev-parse.txt
+4
@@ -93,6 +93,10 @@ OPTIONS
93 When the current working directory is below the repository
94 directory print "true", otherwise "false".
95
96 +--is-inside-work-tree::
97 + When the current working directory is inside the work tree of the
98 + repository print "true", otherwise "false".
99 +
100 --is-bare-repository::
101 When the repository is bare print "true", otherwise "false".
102
Documentation/git.txt
+17 -1
@@ -10,7 +10,8 @@ SYNOPSIS
10 --------
11 [verse]
12 'git' [--version] [--exec-path[=GIT_EXEC_PATH]] [-p|--paginate]
13 - [--bare] [--git-dir=GIT_DIR] [--help] COMMAND [ARGS]
13 + [--bare] [--git-dir=GIT_DIR] [--work-tree=GIT_WORK_TREE]
14 + [--help] COMMAND [ARGS]
15
16 DESCRIPTION
17 -----------
@@ -101,6 +102,14 @@ OPTIONS
102 Set the path to the repository. This can also be controlled by
103 setting the GIT_DIR environment variable.
104
105 +--work-tree=<path>::
106 + Set the path to the working tree. The value will not be
107 + used in combination with repositories found automatically in
108 + a .git directory (i.e. $GIT_DIR is not set).
109 + This can also be controlled by setting the GIT_WORK_TREE
110 + environment variable and the core.worktree configuration
111 + variable.
112 +
113 --bare::
114 Same as --git-dir=`pwd`.
115
@@ -345,6 +354,13 @@ git so take care if using Cogito etc.
354 specifies a path to use instead of the default `.git`
355 for the base of the repository.
356
357 +'GIT_WORK_TREE'::
358 + Set the path to the working tree. The value will not be
359 + used in combination with repositories found automatically in
360 + a .git directory (i.e. $GIT_DIR is not set).
361 + This can also be controlled by the '--work-tree' command line
362 + option and the core.worktree configuration variable.
363 +
364 git Commits
365 ~~~~~~~~~~~
366 'GIT_AUTHOR_NAME'::
builtin-rev-parse.c
+5
@@ -352,6 +352,11 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
352 : "false");
353 continue;
354 }
355 + if (!strcmp(arg, "--is-inside-work-tree")) {
356 + printf("%s\n", is_inside_work_tree() ? "true"
357 + : "false");
358 + continue;
359 + }
360 if (!strcmp(arg, "--is-bare-repository")) {
361 printf("%s\n", is_bare_repository() ? "true"
362 : "false");
cache.h
+2
@@ -192,6 +192,7 @@ enum object_type {
192 };
193
194 #define GIT_DIR_ENVIRONMENT "GIT_DIR"
195 +#define GIT_WORK_TREE_ENVIRONMENT "GIT_WORK_TREE"
196 #define DEFAULT_GIT_DIR_ENVIRONMENT ".git"
197 #define DB_ENVIRONMENT "GIT_OBJECT_DIRECTORY"
198 #define INDEX_ENVIRONMENT "GIT_INDEX_FILE"
@@ -207,6 +208,7 @@ enum object_type {
208 extern int is_bare_repository_cfg;
209 extern int is_bare_repository(void);
210 extern int is_inside_git_dir(void);
211 +extern int is_inside_work_tree(void);
212 extern const char *get_git_dir(void);
213 extern char *get_object_directory(void);
214 extern char *get_refs_directory(void);
connect.c
+1
@@ -589,6 +589,7 @@ pid_t git_connect(int fd[2], char *url, const char *prog, int flags)
589 unsetenv(ALTERNATE_DB_ENVIRONMENT);
590 unsetenv(DB_ENVIRONMENT);
591 unsetenv(GIT_DIR_ENVIRONMENT);
592 + unsetenv(GIT_WORK_TREE_ENVIRONMENT);
593 unsetenv(GRAFT_ENVIRONMENT);
594 unsetenv(INDEX_ENVIRONMENT);
595 execlp("sh", "sh", "-c", command, NULL);
git.c
+11 -1
@@ -4,7 +4,7 @@
4 #include "quote.h"
5
6 const char git_usage_string[] =
7 - "git [--version] [--exec-path[=GIT_EXEC_PATH]] [-p|--paginate] [--bare] [--git-dir=GIT_DIR] [--help] COMMAND [ARGS]";
7 + "git [--version] [--exec-path[=GIT_EXEC_PATH]] [-p|--paginate] [--bare] [--git-dir=GIT_DIR] [--work-tree=GIT_WORK_TREE] [--help] COMMAND [ARGS]";
8
9 static void prepend_to_path(const char *dir, int len)
10 {
@@ -69,6 +69,16 @@ static int handle_options(const char*** argv, int* argc)
69 handled++;
70 } else if (!prefixcmp(cmd, "--git-dir=")) {
71 setenv(GIT_DIR_ENVIRONMENT, cmd + 10, 1);
72 + } else if (!strcmp(cmd, "--work-tree")) {
73 + if (*argc < 2) {
74 + fprintf(stderr, "No directory given for --work-tree.\n" );
75 + usage(git_usage_string);
76 + }
77 + setenv(GIT_WORK_TREE_ENVIRONMENT, (*argv)[1], 1);
78 + (*argv)++;
79 + (*argc)--;
80 + } else if (!prefixcmp(cmd, "--work-tree=")) {
81 + setenv(GIT_WORK_TREE_ENVIRONMENT, cmd + 12, 1);
82 } else if (!strcmp(cmd, "--bare")) {
83 static char git_dir[PATH_MAX+1];
84 setenv(GIT_DIR_ENVIRONMENT, getcwd(git_dir, sizeof(git_dir)), 1);
setup.c
+156 -55
@@ -174,41 +174,93 @@ static int inside_git_dir = -1;
174
175 int is_inside_git_dir(void)
176 {
177 - if (inside_git_dir < 0) {
178 - char buffer[1024];
179 -
180 - if (is_bare_repository())
181 - return (inside_git_dir = 1);
182 - if (getcwd(buffer, sizeof(buffer))) {
183 - const char *git_dir = get_git_dir(), *cwd = buffer;
184 - while (*git_dir && *git_dir == *cwd) {
185 - git_dir++;
186 - cwd++;
187 - }
188 - inside_git_dir = !*git_dir;
189 - } else
190 - inside_git_dir = 0;
177 + if (inside_git_dir >= 0)
178 + return inside_git_dir;
179 + die("BUG: is_inside_git_dir called before setup_git_directory");
180 +}
181 +
182 +static int inside_work_tree = -1;
183 +
184 +int is_inside_work_tree(void)
185 +{
186 + if (inside_git_dir >= 0)
187 + return inside_work_tree;
188 + die("BUG: is_inside_work_tree called before setup_git_directory");
189 +}
190 +
191 +static char *gitworktree_config;
192 +
193 +static int git_setup_config(const char *var, const char *value)
194 +{
195 + if (!strcmp(var, "core.worktree")) {
196 + if (gitworktree_config)
197 + strlcpy(gitworktree_config, value, PATH_MAX);
198 + return 0;
199 }
192 - return inside_git_dir;
200 + return git_default_config(var, value);
201 }
202
203 const char *setup_git_directory_gently(int *nongit_ok)
204 {
205 static char cwd[PATH_MAX+1];
198 - const char *gitdirenv;
199 - int len, offset;
206 + char worktree[PATH_MAX+1], gitdir[PATH_MAX+1];
207 + const char *gitdirenv, *gitworktree;
208 + int wt_rel_gitdir = 0;
209
201 - /*
202 - * If GIT_DIR is set explicitly, we're not going
203 - * to do any discovery, but we still do repository
204 - * validation.
205 - */
210 gitdirenv = getenv(GIT_DIR_ENVIRONMENT);
207 - if (gitdirenv) {
208 - if (PATH_MAX - 40 < strlen(gitdirenv))
209 - die("'$%s' too big", GIT_DIR_ENVIRONMENT);
210 - if (is_git_directory(gitdirenv))
211 + if (!gitdirenv) {
212 + int len, offset;
213 +
214 + if (!getcwd(cwd, sizeof(cwd)-1) || cwd[0] != '/')
215 + die("Unable to read current working directory");
216 +
217 + offset = len = strlen(cwd);
218 + for (;;) {
219 + if (is_git_directory(".git"))
220 + break;
221 + if (offset == 0) {
222 + offset = -1;
223 + break;
224 + }
225 + chdir("..");
226 + while (cwd[--offset] != '/')
227 + ; /* do nothing */
228 + }
229 +
230 + if (offset >= 0) {
231 + inside_work_tree = 1;
232 + git_config(git_default_config);
233 + if (offset == len) {
234 + inside_git_dir = 0;
235 + return NULL;
236 + }
237 +
238 + cwd[len++] = '/';
239 + cwd[len] = '\0';
240 + inside_git_dir = !prefixcmp(cwd + offset + 1, ".git/");
241 + return cwd + offset + 1;
242 + }
243 +
244 + if (chdir(cwd))
245 + die("Cannot come back to cwd");
246 + if (!is_git_directory(".")) {
247 + if (nongit_ok) {
248 + *nongit_ok = 1;
249 + return NULL;
250 + }
251 + die("Not a git repository");
252 + }
253 + setenv(GIT_DIR_ENVIRONMENT, cwd, 1);
254 + }
255 +
256 + if (PATH_MAX - 40 < strlen(gitdirenv)) {
257 + if (nongit_ok) {
258 + *nongit_ok = 1;
259 return NULL;
260 + }
261 + die("$%s too big", GIT_DIR_ENVIRONMENT);
262 + }
263 + if (!is_git_directory(gitdirenv)) {
264 if (nongit_ok) {
265 *nongit_ok = 1;
266 return NULL;
@@ -218,41 +270,90 @@ const char *setup_git_directory_gently(int *nongit_ok)
270
271 if (!getcwd(cwd, sizeof(cwd)-1) || cwd[0] != '/')
272 die("Unable to read current working directory");
273 + if (chdir(gitdirenv)) {
274 + if (nongit_ok) {
275 + *nongit_ok = 1;
276 + return NULL;
277 + }
278 + die("Cannot change directory to $%s '%s'",
279 + GIT_DIR_ENVIRONMENT, gitdirenv);
280 + }
281 + if (!getcwd(gitdir, sizeof(gitdir)-1) || gitdir[0] != '/')
282 + die("Unable to read current working directory");
283 + if (chdir(cwd))
284 + die("Cannot come back to cwd");
285
222 - offset = len = strlen(cwd);
223 - for (;;) {
224 - if (is_git_directory(".git"))
225 - break;
226 - chdir("..");
227 - do {
228 - if (!offset) {
229 - if (is_git_directory(cwd)) {
230 - if (chdir(cwd))
231 - die("Cannot come back to cwd");
232 - setenv(GIT_DIR_ENVIRONMENT, cwd, 1);
233 - inside_git_dir = 1;
234 - return NULL;
235 - }
236 - if (nongit_ok) {
237 - if (chdir(cwd))
238 - die("Cannot come back to cwd");
239 - *nongit_ok = 1;
240 - return NULL;
241 - }
242 - die("Not a git repository");
286 + /*
287 + * In case there is a work tree we may change the directory,
288 + * therefore make GIT_DIR an absolute path.
289 + */
290 + if (gitdirenv[0] != '/') {
291 + setenv(GIT_DIR_ENVIRONMENT, gitdir, 1);
292 + gitdirenv = getenv(GIT_DIR_ENVIRONMENT);
293 + if (PATH_MAX - 40 < strlen(gitdirenv)) {
294 + if (nongit_ok) {
295 + *nongit_ok = 1;
296 + return NULL;
297 }
244 - } while (cwd[--offset] != '/');
298 + die("$%s too big after expansion to absolute path",
299 + GIT_DIR_ENVIRONMENT);
300 + }
301 + }
302 +
303 + strcat(cwd, "/");
304 + strcat(gitdir, "/");
305 + inside_git_dir = !prefixcmp(cwd, gitdir);
306 +
307 + gitworktree = getenv(GIT_WORK_TREE_ENVIRONMENT);
308 + if (!gitworktree) {
309 + gitworktree_config = worktree;
310 + worktree[0] = '\0';
311 + }
312 + git_config(git_setup_config);
313 + if (!gitworktree) {
314 + gitworktree_config = NULL;
315 + if (worktree[0])
316 + gitworktree = worktree;
317 + if (gitworktree && gitworktree[0] != '/')
318 + wt_rel_gitdir = 1;
319 + }
320 +
321 + if (wt_rel_gitdir && chdir(gitdirenv))
322 + die("Cannot change directory to $%s '%s'",
323 + GIT_DIR_ENVIRONMENT, gitdirenv);
324 + if (gitworktree && chdir(gitworktree)) {
325 + if (nongit_ok) {
326 + if (wt_rel_gitdir && chdir(cwd))
327 + die("Cannot come back to cwd");
328 + *nongit_ok = 1;
329 + return NULL;
330 + }
331 + if (wt_rel_gitdir)
332 + die("Cannot change directory to working tree '%s'"
333 + " from $%s", gitworktree, GIT_DIR_ENVIRONMENT);
334 + else
335 + die("Cannot change directory to working tree '%s'",
336 + gitworktree);
337 }
338 + if (!getcwd(worktree, sizeof(worktree)-1) || worktree[0] != '/')
339 + die("Unable to read current working directory");
340 + strcat(worktree, "/");
341 + inside_work_tree = !prefixcmp(cwd, worktree);
342
247 - if (offset == len)
343 + if (gitworktree && inside_work_tree && !prefixcmp(worktree, gitdir) &&
344 + strcmp(worktree, gitdir)) {
345 + inside_git_dir = 0;
346 + }
347 +
348 + if (!inside_work_tree) {
349 + if (chdir(cwd))
350 + die("Cannot come back to cwd");
351 return NULL;
352 + }
353
250 - /* Make "offset" point to past the '/', and add a '/' at the end */
251 - offset++;
252 - cwd[len++] = '/';
253 - cwd[len] = 0;
254 - inside_git_dir = !prefixcmp(cwd + offset, ".git/");
255 - return cwd + offset;
354 + if (!strcmp(cwd, worktree))
355 + return NULL;
356 + return cwd+strlen(worktree);
357 }
358
359 int git_config_perm(const char *var, const char *value)
t/test-lib.sh
+1
@@ -26,6 +26,7 @@ GIT_COMMITTER_EMAIL=committer@example.com
26 GIT_COMMITTER_NAME='C O Mitter'
27 unset GIT_DIFF_OPTS
28 unset GIT_DIR
29 +unset GIT_WORK_TREE
30 unset GIT_EXTERNAL_DIFF
31 unset GIT_INDEX_FILE
32 unset GIT_OBJECT_DIRECTORY