From: Johannes Sixt Date: Thu, 15 May 2008 07:06:19 GMT Subject: Re: [PATCH v2] Add support for GIT_CEILING_DIRS Message-ID: <482BE0EB.6040306@viscovery.net> In-Reply-To: <482B935D.20105@facebook.com> David Reiss schrieb: > const char *setup_git_directory_gently(int *nongit_ok) > { > const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT); > + const char *env_ceiling_dirs = getenv(CEILING_DIRS_ENVIRONMENT); > static char cwd[PATH_MAX+1]; > const char *gitdirenv; > const char *gitfile_dir; > - int len, offset; > + int len, offset, ceil_offset; > > /* > * Let's assume that we are in a git repository. > @@ -415,6 +517,14 @@ const char *setup_git_directory_gently(int *nongit_ok) > die("Unable to read current working directory"); > > /* > + * Compute ceil_offset based on GIT_CEILING_DIRS. It is actually the offset > + * of the first character in cwd after the trailing slash of the ceiling. > + * Putting it so far to the right is necessary in order to bail out of the > + * "--offset" loop early enough. > + */ > + ceil_offset = 1 + longest_ancestor_length(cwd, env_ceiling_dirs); > + > + /* > * Test in the following order (relative to the cwd): > * - .git (file containing "gitdir: ") > * - .git/ > @@ -443,9 +553,8 @@ const char *setup_git_directory_gently(int *nongit_ok) > check_repository_format_gently(nongit_ok); > return NULL; > } > - chdir(".."); > do { > - if (!offset) { > + if (offset <= ceil_offset) { > if (nongit_ok) { > if (chdir(cwd)) > die("Cannot come back to cwd"); > @@ -455,6 +564,7 @@ const char *setup_git_directory_gently(int *nongit_ok) > die("Not a git repository"); > } > } while (cwd[--offset] != '/'); If you make it so that the default value of ceil_offset is 0 (i.e. in the absence of any GIT_CEILING_DIRS), and at this place you did } while (offset > ceil_offset && cwd[--offset] != '/'); you wouldn't have to bend backwards with this off-by-one magic, would you? (But I admit that I haven't tried this code, I'm only comparing it to how we do it mingw.git.) -- Hannes