1
#include "cache.h"
2
+#include "dir.h"
3
+
4
+static int inside_git_dir = -1;
5
+static int inside_work_tree = -1;
6
7
const char *prefix_path(const char *prefix, int len, const char *path)
8
{
174
return 1;
175
}
176
173
-static int inside_git_dir = -1;
174
-
177
int is_inside_git_dir(void)
178
{
177
- if (inside_git_dir >= 0)
178
- return inside_git_dir;
179
- die("BUG: is_inside_git_dir called before setup_git_directory");
179
+ if (inside_git_dir < 0)
180
+ inside_git_dir = is_inside_dir(get_git_dir());
181
+ return inside_git_dir;
182
}
183
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");
186
+ if (inside_work_tree < 0)
187
+ inside_work_tree = is_inside_dir(get_git_work_tree());
188
+ return inside_work_tree;
189
}
190
191
-static char *gitworktree_config;
192
-
193
-static int git_setup_config(const char *var, const char *value)
191
+/*
192
+ * If no worktree was given, and we are outside of a default work tree,
193
+ * now is the time to set it.
194
+ *
195
+ * In other words, if the user calls git with something like
196
+ *
197
+ * git --git-dir=/some/where/else/.git bla
198
+ *
199
+ * default to /some/where/else as working directory; if the specified
200
+ * git-dir does not end in "/.git", the cwd is used as working directory.
201
+ */
202
+const char *set_work_tree(const char *dir)
203
{
195
- if (!strcmp(var, "core.worktree")) {
196
- if (gitworktree_config)
197
- strlcpy(gitworktree_config, value, PATH_MAX);
198
- return 0;
204
+ char dir_buffer[PATH_MAX];
205
+ static char buffer[PATH_MAX + 1], *rel = NULL;
206
+ int len, postfix_len = strlen(DEFAULT_GIT_DIR_ENVIRONMENT) + 1;
207
+
208
+ /* strip the variable 'dir' of the postfix "/.git" if it has it */
209
+ len = strlen(dir);
210
+ if (len > postfix_len && !strcmp(dir + len - postfix_len,
211
+ "/" DEFAULT_GIT_DIR_ENVIRONMENT)) {
212
+ strncpy(dir_buffer, dir, len - postfix_len);
213
+
214
+ /* are we inside the default work tree? */
215
+ rel = get_relative_cwd(buffer, sizeof(buffer), dir_buffer);
216
+ }
217
+ /* if rel is set, the cwd is _not_ the current working tree */
218
+ if (rel && *rel) {
219
+ if (!is_absolute_path(dir))
220
+ set_git_dir(make_absolute_path(dir));
221
+ dir = dir_buffer;
222
+ chdir(dir);
223
+ strcat(rel, "/");
224
+ inside_git_dir = 0;
225
+ } else {
226
+ rel = NULL;
227
+ dir = getcwd(buffer, sizeof(buffer));
228
}
200
- return git_default_config(var, value);
229
+ git_work_tree_cfg = xstrdup(dir);
230
+ inside_work_tree = 1;
231
+
232
+ return rel;
233
}
234
235
+/*
236
+ * We cannot decide in this function whether we are in the work tree or
237
+ * not, since the config can only be read _after_ this function was called.
238
+ */
239
const char *setup_git_directory_gently(int *nongit_ok)
240
{
241
+ const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT);
242
static char cwd[PATH_MAX+1];
206
- char worktree[PATH_MAX+1], gitdir[PATH_MAX+1];
207
- const char *gitdirenv, *gitworktree;
208
- int wt_rel_gitdir = 0;
243
+ const char *gitdirenv;
244
+ int len, offset;
245
246
+ /*
247
+ * If GIT_DIR is set explicitly, we're not going
248
+ * to do any discovery, but we still do repository
249
+ * validation.
250
+ */
251
gitdirenv = getenv(GIT_DIR_ENVIRONMENT);
211
- if (!gitdirenv) {
212
- int len, offset;
213
-
214
- if (!getcwd(cwd, sizeof(cwd)-1))
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
- gitdirenv = getenv(GIT_DIR_ENVIRONMENT);
255
- if (!gitdirenv)
256
- die("getenv after setenv failed");
257
- }
258
-
259
- if (PATH_MAX - 40 < strlen(gitdirenv)) {
260
- if (nongit_ok) {
261
- *nongit_ok = 1;
252
+ if (gitdirenv) {
253
+ if (PATH_MAX - 40 < strlen(gitdirenv))
254
+ die("'$%s' too big", GIT_DIR_ENVIRONMENT);
255
+ if (is_git_directory(gitdirenv)) {
256
+ if (!work_tree_env)
257
+ return set_work_tree(gitdirenv);
258
return NULL;
259
}
264
- die("$%s too big", GIT_DIR_ENVIRONMENT);
265
- }
266
- if (!is_git_directory(gitdirenv)) {
260
if (nongit_ok) {
261
*nongit_ok = 1;
262
return NULL;
266
267
if (!getcwd(cwd, sizeof(cwd)-1))
268
die("Unable to read current working directory");
276
- if (chdir(gitdirenv)) {
277
- if (nongit_ok) {
278
- *nongit_ok = 1;
279
- return NULL;
280
- }
281
- die("Cannot change directory to $%s '%s'",
282
- GIT_DIR_ENVIRONMENT, gitdirenv);
283
- }
284
- if (!getcwd(gitdir, sizeof(gitdir)-1))
285
- die("Unable to read current working directory");
286
- if (chdir(cwd))
287
- die("Cannot come back to cwd");
269
270
/*
290
- * In case there is a work tree we may change the directory,
291
- * therefore make GIT_DIR an absolute path.
271
+ * Test in the following order (relative to the cwd):
272
+ * - .git/
273
+ * - ./ (bare)
274
+ * - ../.git/
275
+ * - ../ (bare)
276
+ * - ../../.git/
277
+ * etc.
278
*/
293
- if (gitdirenv[0] != '/') {
294
- setenv(GIT_DIR_ENVIRONMENT, gitdir, 1);
295
- gitdirenv = getenv(GIT_DIR_ENVIRONMENT);
296
- if (!gitdirenv)
297
- die("getenv after setenv failed");
298
- if (PATH_MAX - 40 < strlen(gitdirenv)) {
299
- if (nongit_ok) {
300
- *nongit_ok = 1;
301
- return NULL;
302
- }
303
- die("$%s too big after expansion to absolute path",
304
- GIT_DIR_ENVIRONMENT);
305
- }
306
- }
307
-
308
- strcat(cwd, "/");
309
- strcat(gitdir, "/");
310
- inside_git_dir = !prefixcmp(cwd, gitdir);
311
-
312
- gitworktree = getenv(GIT_WORK_TREE_ENVIRONMENT);
313
- if (!gitworktree) {
314
- gitworktree_config = worktree;
315
- worktree[0] = '\0';
316
- }
317
- git_config(git_setup_config);
318
- if (!gitworktree) {
319
- gitworktree_config = NULL;
320
- if (worktree[0])
321
- gitworktree = worktree;
322
- if (gitworktree && gitworktree[0] != '/')
323
- wt_rel_gitdir = 1;
324
- }
325
-
326
- if (wt_rel_gitdir && chdir(gitdirenv))
327
- die("Cannot change directory to $%s '%s'",
328
- GIT_DIR_ENVIRONMENT, gitdirenv);
329
- if (gitworktree && chdir(gitworktree)) {
330
- if (nongit_ok) {
331
- if (wt_rel_gitdir && chdir(cwd))
332
- die("Cannot come back to cwd");
333
- *nongit_ok = 1;
279
+ offset = len = strlen(cwd);
280
+ for (;;) {
281
+ if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT))
282
+ break;
283
+ if (is_git_directory(".")) {
284
+ inside_git_dir = 1;
285
+ if (!work_tree_env)
286
+ inside_work_tree = 0;
287
+ setenv(GIT_DIR_ENVIRONMENT, ".", 1);
288
return NULL;
289
}
336
- if (wt_rel_gitdir)
337
- die("Cannot change directory to working tree '%s'"
338
- " from $%s", gitworktree, GIT_DIR_ENVIRONMENT);
339
- else
340
- die("Cannot change directory to working tree '%s'",
341
- gitworktree);
342
- }
343
- if (!getcwd(worktree, sizeof(worktree)-1))
344
- die("Unable to read current working directory");
345
- strcat(worktree, "/");
346
- inside_work_tree = !prefixcmp(cwd, worktree);
347
-
348
- if (gitworktree && inside_work_tree && !prefixcmp(worktree, gitdir) &&
349
- strcmp(worktree, gitdir)) {
350
- inside_git_dir = 0;
290
+ chdir("..");
291
+ do {
292
+ if (!offset) {
293
+ if (nongit_ok) {
294
+ if (chdir(cwd))
295
+ die("Cannot come back to cwd");
296
+ *nongit_ok = 1;
297
+ return NULL;
298
+ }
299
+ die("Not a git repository");
300
+ }
301
+ } while (cwd[--offset] != '/');
302
}
303
353
- if (!inside_work_tree) {
354
- if (chdir(cwd))
355
- die("Cannot come back to cwd");
304
+ inside_git_dir = 0;
305
+ if (!work_tree_env)
306
+ inside_work_tree = 1;
307
+ git_work_tree_cfg = xstrndup(cwd, offset);
308
+ if (offset == len)
309
return NULL;
357
- }
310
359
- if (!strcmp(cwd, worktree))
360
- return NULL;
361
- return cwd+strlen(worktree);
311
+ /* Make "offset" point to past the '/', and add a '/' at the end */
312
+ offset++;
313
+ cwd[len++] = '/';
314
+ cwd[len] = 0;
315
+ return cwd + offset;
316
}
317
318
int git_config_perm(const char *var, const char *value)
340
repository_format_version = git_config_int(var, value);
341
else if (strcmp(var, "core.sharedrepository") == 0)
342
shared_repository = git_config_perm(var, value);
343
+ else if (strcmp(var, "core.bare") == 0) {
344
+ is_bare_repository_cfg = git_config_bool(var, value);
345
+ if (is_bare_repository_cfg == 1)
346
+ inside_work_tree = -1;
347
+ } else if (strcmp(var, "core.worktree") == 0) {
348
+ if (git_work_tree_cfg)
349
+ free(git_work_tree_cfg);
350
+ git_work_tree_cfg = xstrdup(value);
351
+ inside_work_tree = -1;
352
+ }
353
return 0;
354
}
355
366
{
367
const char *retval = setup_git_directory_gently(NULL);
368
check_repository_format();
369
+
370
+ /* If the work tree is not the default one, recompute prefix */
371
+ if (inside_work_tree < 0) {
372
+ static char buffer[PATH_MAX + 1];
373
+ char *rel;
374
+ if (retval && chdir(retval))
375
+ die ("Could not jump back into original cwd");
376
+ rel = get_relative_cwd(buffer, PATH_MAX, get_git_work_tree());
377
+ return rel && *rel ? strcat(rel, "/") : NULL;
378
+ }
379
+
380
return retval;
381
}