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;
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)