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
/*
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] != '/')
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");