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

Automatically detect a bare git repository.

Many users find it unfriendly that they can create a bare git repository easily with `git clone --bare` but are then unable to run simple commands like `git log` once they cd into that newly created bare repository. This occurs because we do not check to see if the current working directory is a git repository. Instead of failing out with "fatal: Not a git repository" we should try to automatically detect if the current working directory is a bare repository and use that for GIT_DIR, and fail out only if that doesn't appear to be true. We test the current working directory only after we have tried searching up the directory tree. This is to retain backwards compatibility with our previous behavior on the off chance that a user has a 'refs' and 'objects' subdirectories and a 'HEAD' file that looks like a symref, all stored within a repository's associated working directory. This change also consolidates the validation logic between the case of GIT_DIR being supplied and GIT_DIR not being supplied, cleaning up the code. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Shawn O. Pearce committed Dec 30, 2006 at 23:30 UTC ad1a382fbb3ecb1bb017854a470816c815cc46c9
1 file changed +39 -34
setup.c
+39 -34
@@ -131,28 +131,46 @@ const char **get_pathspec(const char *prefix, const char **pathspec)
131 }
132
133 /*
134 - * Test if it looks like we're at the top level git directory.
134 + * Test if it looks like we're at a git directory.
135 * We want to see:
136 *
137 - * - either a .git/objects/ directory _or_ the proper
137 + * - either a objects/ directory _or_ the proper
138 * GIT_OBJECT_DIRECTORY environment variable
139 - * - a refs/ directory under ".git"
139 + * - a refs/ directory
140 * - either a HEAD symlink or a HEAD file that is formatted as
141 * a proper "ref:".
142 */
143 -static int is_toplevel_directory(void)
143 +static int is_git_directory(const char *suspect)
144 {
145 - if (access(".git/refs/", X_OK) ||
146 - access(getenv(DB_ENVIRONMENT) ?
147 - getenv(DB_ENVIRONMENT) : ".git/objects/", X_OK) ||
148 - validate_symref(".git/HEAD"))
145 + char path[PATH_MAX];
146 + size_t len = strlen(suspect);
147 +
148 + strcpy(path, suspect);
149 + if (getenv(DB_ENVIRONMENT)) {
150 + if (access(getenv(DB_ENVIRONMENT), X_OK))
151 + return 0;
152 + }
153 + else {
154 + strcpy(path + len, "/objects");
155 + if (access(path, X_OK))
156 + return 0;
157 + }
158 +
159 + strcpy(path + len, "/refs");
160 + if (access(path, X_OK))
161 return 0;
162 +
163 + strcpy(path + len, "/HEAD");
164 + if (validate_symref(path))
165 + return 0;
166 +
167 return 1;
168 }
169
170 const char *setup_git_directory_gently(int *nongit_ok)
171 {
172 static char cwd[PATH_MAX+1];
173 + const char *gitdirenv;
174 int len, offset;
175
176 /*
@@ -160,36 +178,17 @@ const char *setup_git_directory_gently(int *nongit_ok)
178 * to do any discovery, but we still do repository
179 * validation.
180 */
163 - if (getenv(GIT_DIR_ENVIRONMENT)) {
164 - char path[PATH_MAX];
165 - int len = strlen(getenv(GIT_DIR_ENVIRONMENT));
166 - if (sizeof(path) - 40 < len)
181 + gitdirenv = getenv(GIT_DIR_ENVIRONMENT);
182 + if (gitdirenv) {
183 + if (PATH_MAX - 40 < strlen(gitdirenv))
184 die("'$%s' too big", GIT_DIR_ENVIRONMENT);
168 - memcpy(path, getenv(GIT_DIR_ENVIRONMENT), len);
169 -
170 - strcpy(path + len, "/refs");
171 - if (access(path, X_OK))
172 - goto bad_dir_environ;
173 - strcpy(path + len, "/HEAD");
174 - if (validate_symref(path))
175 - goto bad_dir_environ;
176 - if (getenv(DB_ENVIRONMENT)) {
177 - if (access(getenv(DB_ENVIRONMENT), X_OK))
178 - goto bad_dir_environ;
179 - }
180 - else {
181 - strcpy(path + len, "/objects");
182 - if (access(path, X_OK))
183 - goto bad_dir_environ;
184 - }
185 - return NULL;
186 - bad_dir_environ:
185 + if (is_git_directory(gitdirenv))
186 + return NULL;
187 if (nongit_ok) {
188 *nongit_ok = 1;
189 return NULL;
190 }
191 - path[len] = 0;
192 - die("Not a git repository: '%s'", path);
191 + die("Not a git repository: '%s'", gitdirenv);
192 }
193
194 if (!getcwd(cwd, sizeof(cwd)) || cwd[0] != '/')
@@ -197,11 +196,17 @@ const char *setup_git_directory_gently(int *nongit_ok)
196
197 offset = len = strlen(cwd);
198 for (;;) {
200 - if (is_toplevel_directory())
199 + if (is_git_directory(".git"))
200 break;
201 chdir("..");
202 do {
203 if (!offset) {
204 + if (is_git_directory(cwd)) {
205 + if (chdir(cwd))
206 + die("Cannot come back to cwd");
207 + setenv(GIT_DIR_ENVIRONMENT, cwd, 1);
208 + return NULL;
209 + }
210 if (nongit_ok) {
211 if (chdir(cwd))
212 die("Cannot come back to cwd");