4 static int inside_git_dir
= -1;
5 static int inside_work_tree
= -1;
7 const char *prefix_path(const char *prefix
, int len
, const char *path
)
9 const char *orig
= path
;
10 char *sanitized
= xmalloc(len
+ strlen(path
) + 1);
11 if (is_absolute_path(orig
))
12 strcpy(sanitized
, path
);
15 memcpy(sanitized
, prefix
, len
);
16 strcpy(sanitized
+ len
, path
);
18 if (normalize_path_copy(sanitized
, sanitized
))
20 if (is_absolute_path(orig
)) {
21 const char *work_tree
= get_git_work_tree();
22 size_t len
= strlen(work_tree
);
23 size_t total
= strlen(sanitized
) + 1;
24 if (strncmp(sanitized
, work_tree
, len
) ||
25 (sanitized
[len
] != '\0' && sanitized
[len
] != '/')) {
27 die("'%s' is outside repository", orig
);
29 if (sanitized
[len
] == '/')
31 memmove(sanitized
, sanitized
+ len
, total
- len
);
37 * Unlike prefix_path, this should be used if the named file does
38 * not have to interact with index entry; i.e. name of a random file
41 const char *prefix_filename(const char *pfx
, int pfx_len
, const char *arg
)
43 static char path
[PATH_MAX
];
45 if (!pfx
|| !*pfx
|| is_absolute_path(arg
))
47 memcpy(path
, pfx
, pfx_len
);
48 strcpy(path
+ pfx_len
, arg
);
51 /* don't add prefix to absolute paths, but still replace '\' by '/' */
52 if (is_absolute_path(arg
))
55 memcpy(path
, pfx
, pfx_len
);
56 strcpy(path
+ pfx_len
, arg
);
57 for (p
= path
+ pfx_len
; *p
; p
++)
64 int check_filename(const char *prefix
, const char *arg
)
69 name
= prefix
? prefix_filename(prefix
, strlen(prefix
), arg
) : arg
;
70 if (!lstat(name
, &st
))
71 return 1; /* file exists */
72 if (errno
== ENOENT
|| errno
== ENOTDIR
)
73 return 0; /* file does not exist */
74 die_errno("failed to stat '%s'", arg
);
78 * Verify a filename that we got as an argument for a pathspec
79 * entry. Note that a filename that begins with "-" never verifies
80 * as true, because even if such a filename were to exist, we want
81 * it to be preceded by the "--" marker (or we want the user to
82 * use a format like "./-filename")
84 void verify_filename(const char *prefix
, const char *arg
)
87 die("bad flag '%s' used after filename", arg
);
88 if (check_filename(prefix
, arg
))
90 die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
91 "Use '--' to separate paths from revisions", arg
);
95 * Opposite of the above: the command line did not have -- marker
96 * and we parsed the arg as a refname. It should not be interpretable
99 void verify_non_filename(const char *prefix
, const char *arg
)
101 if (!is_inside_work_tree() || is_inside_git_dir())
105 if (!check_filename(prefix
, arg
))
107 die("ambiguous argument '%s': both revision and filename\n"
108 "Use '--' to separate filenames from revisions", arg
);
111 const char **get_pathspec(const char *prefix
, const char **pathspec
)
113 const char *entry
= *pathspec
;
114 const char **src
, **dst
;
117 if (!prefix
&& !entry
)
121 static const char *spec
[2];
127 /* Otherwise we have to re-write the entries.. */
130 prefixlen
= prefix
? strlen(prefix
) : 0;
132 const char *p
= prefix_path(prefix
, prefixlen
, *src
);
143 * Test if it looks like we're at a git directory.
146 * - either an objects/ directory _or_ the proper
147 * GIT_OBJECT_DIRECTORY environment variable
148 * - a refs/ directory
149 * - either a HEAD symlink or a HEAD file that is formatted as
150 * a proper "ref:", or a regular file HEAD that has a properly
151 * formatted sha1 object name.
153 static int is_git_directory(const char *suspect
)
156 size_t len
= strlen(suspect
);
158 strcpy(path
, suspect
);
159 if (getenv(DB_ENVIRONMENT
)) {
160 if (access(getenv(DB_ENVIRONMENT
), X_OK
))
164 strcpy(path
+ len
, "/objects");
165 if (access(path
, X_OK
))
169 strcpy(path
+ len
, "/refs");
170 if (access(path
, X_OK
))
173 strcpy(path
+ len
, "/HEAD");
174 if (validate_headref(path
))
180 int is_inside_git_dir(void)
182 if (inside_git_dir
< 0)
183 inside_git_dir
= is_inside_dir(get_git_dir());
184 return inside_git_dir
;
187 int is_inside_work_tree(void)
189 if (inside_work_tree
< 0)
190 inside_work_tree
= is_inside_dir(get_git_work_tree());
191 return inside_work_tree
;
195 * set_work_tree() is only ever called if you set GIT_DIR explicitely.
196 * The old behaviour (which we retain here) is to set the work tree root
197 * to the cwd, unless overridden by the config, the command line, or
200 static const char *set_work_tree(const char *dir
)
202 char buffer
[PATH_MAX
+ 1];
204 if (!getcwd(buffer
, sizeof(buffer
)))
205 die ("Could not get the current working directory");
206 git_work_tree_cfg
= xstrdup(buffer
);
207 inside_work_tree
= 1;
212 void setup_work_tree(void)
214 const char *work_tree
, *git_dir
;
215 static int initialized
= 0;
219 work_tree
= get_git_work_tree();
220 git_dir
= get_git_dir();
221 if (!is_absolute_path(git_dir
))
222 git_dir
= make_absolute_path(git_dir
);
223 if (!work_tree
|| chdir(work_tree
))
224 die("This operation must be run in a work tree");
225 set_git_dir(make_relative_path(git_dir
, work_tree
));
229 static int check_repository_format_gently(int *nongit_ok
)
231 git_config(check_repository_format_version
, NULL
);
232 if (GIT_REPO_VERSION
< repository_format_version
) {
234 die ("Expected git repo version <= %d, found %d",
235 GIT_REPO_VERSION
, repository_format_version
);
236 warning("Expected git repo version <= %d, found %d",
237 GIT_REPO_VERSION
, repository_format_version
);
238 warning("Please upgrade Git");
246 * Try to read the location of the git directory from the .git file,
247 * return path to git directory if found.
249 const char *read_gitfile_gently(const char *path
)
258 if (!S_ISREG(st
.st_mode
))
260 fd
= open(path
, O_RDONLY
);
262 die_errno("Error opening '%s'", path
);
263 buf
= xmalloc(st
.st_size
+ 1);
264 len
= read_in_full(fd
, buf
, st
.st_size
);
266 if (len
!= st
.st_size
)
267 die("Error reading %s", path
);
269 if (prefixcmp(buf
, "gitdir: "))
270 die("Invalid gitfile format: %s", path
);
271 while (buf
[len
- 1] == '\n' || buf
[len
- 1] == '\r')
274 die("No path in gitfile: %s", path
);
276 if (!is_git_directory(buf
+ 8))
277 die("Not a git repository: %s", buf
+ 8);
278 path
= make_absolute_path(buf
+ 8);
284 * We cannot decide in this function whether we are in the work tree or
285 * not, since the config can only be read _after_ this function was called.
287 const char *setup_git_directory_gently(int *nongit_ok
)
289 const char *work_tree_env
= getenv(GIT_WORK_TREE_ENVIRONMENT
);
290 const char *env_ceiling_dirs
= getenv(CEILING_DIRECTORIES_ENVIRONMENT
);
291 static char cwd
[PATH_MAX
+1];
292 const char *gitdirenv
;
293 const char *gitfile_dir
;
294 int len
, offset
, ceil_offset
;
297 * Let's assume that we are in a git repository.
298 * If it turns out later that we are somewhere else, the value will be
299 * updated accordingly.
305 * If GIT_DIR is set explicitly, we're not going
306 * to do any discovery, but we still do repository
309 gitdirenv
= getenv(GIT_DIR_ENVIRONMENT
);
311 if (PATH_MAX
- 40 < strlen(gitdirenv
))
312 die("'$%s' too big", GIT_DIR_ENVIRONMENT
);
313 if (is_git_directory(gitdirenv
)) {
314 static char buffer
[1024 + 1];
317 if (!work_tree_env
) {
318 retval
= set_work_tree(gitdirenv
);
319 /* config may override worktree */
320 if (check_repository_format_gently(nongit_ok
))
324 if (check_repository_format_gently(nongit_ok
))
326 retval
= get_relative_cwd(buffer
, sizeof(buffer
) - 1,
327 get_git_work_tree());
328 if (!retval
|| !*retval
)
330 set_git_dir(make_absolute_path(gitdirenv
));
331 if (chdir(work_tree_env
) < 0)
332 die_errno ("Could not chdir to '%s'", work_tree_env
);
340 die("Not a git repository: '%s'", gitdirenv
);
343 if (!getcwd(cwd
, sizeof(cwd
)-1))
344 die_errno("Unable to read current working directory");
346 ceil_offset
= longest_ancestor_length(cwd
, env_ceiling_dirs
);
347 if (ceil_offset
< 0 && has_dos_drive_prefix(cwd
))
351 * Test in the following order (relative to the cwd):
352 * - .git (file containing "gitdir: <path>")
361 offset
= len
= strlen(cwd
);
363 gitfile_dir
= read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT
);
365 if (set_git_dir(gitfile_dir
))
366 die("Repository setup failed");
369 if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT
))
371 if (is_git_directory(".")) {
374 inside_work_tree
= 0;
377 setenv(GIT_DIR_ENVIRONMENT
, cwd
, 1);
379 setenv(GIT_DIR_ENVIRONMENT
, ".", 1);
380 check_repository_format_gently(nongit_ok
);
383 while (--offset
> ceil_offset
&& cwd
[offset
] != '/');
384 if (offset
<= ceil_offset
) {
387 die_errno("Cannot come back to cwd");
391 die("Not a git repository (or any of the parent directories): %s", DEFAULT_GIT_DIR_ENVIRONMENT
);
394 die_errno("Cannot change to '%s/..'", cwd
);
399 inside_work_tree
= 1;
400 git_work_tree_cfg
= xstrndup(cwd
, offset
);
401 if (check_repository_format_gently(nongit_ok
))
406 /* Make "offset" point to past the '/', and add a '/' at the end */
413 int git_config_perm(const char *var
, const char *value
)
421 if (!strcmp(value
, "umask"))
423 if (!strcmp(value
, "group"))
425 if (!strcmp(value
, "all") ||
426 !strcmp(value
, "world") ||
427 !strcmp(value
, "everybody"))
428 return PERM_EVERYBODY
;
430 /* Parse octal numbers */
431 i
= strtol(value
, &endptr
, 8);
433 /* If not an octal number, maybe true/false? */
435 return git_config_bool(var
, value
) ? PERM_GROUP
: PERM_UMASK
;
438 * Treat values 0, 1 and 2 as compatibility cases, otherwise it is
439 * a chmod value to restrict to.
442 case PERM_UMASK
: /* 0 */
444 case OLD_PERM_GROUP
: /* 1 */
446 case OLD_PERM_EVERYBODY
: /* 2 */
447 return PERM_EVERYBODY
;
450 /* A filemode value was given: 0xxx */
452 if ((i
& 0600) != 0600)
453 die("Problem with core.sharedRepository filemode value "
454 "(0%.3o).\nThe owner of files must always have "
455 "read and write permissions.", i
);
458 * Mask filemode value. Others can not get write permission.
459 * x flags for directories are handled separately.
464 int check_repository_format_version(const char *var
, const char *value
, void *cb
)
466 if (strcmp(var
, "core.repositoryformatversion") == 0)
467 repository_format_version
= git_config_int(var
, value
);
468 else if (strcmp(var
, "core.sharedrepository") == 0)
469 shared_repository
= git_config_perm(var
, value
);
470 else if (strcmp(var
, "core.bare") == 0) {
471 is_bare_repository_cfg
= git_config_bool(var
, value
);
472 if (is_bare_repository_cfg
== 1)
473 inside_work_tree
= -1;
474 } else if (strcmp(var
, "core.worktree") == 0) {
476 return config_error_nonbool(var
);
477 free(git_work_tree_cfg
);
478 git_work_tree_cfg
= xstrdup(value
);
479 inside_work_tree
= -1;
484 int check_repository_format(void)
486 return check_repository_format_gently(NULL
);
489 const char *setup_git_directory(void)
491 const char *retval
= setup_git_directory_gently(NULL
);
493 /* If the work tree is not the default one, recompute prefix */
494 if (inside_work_tree
< 0) {
495 static char buffer
[PATH_MAX
+ 1];
497 if (retval
&& chdir(retval
))
498 die_errno ("Could not jump back into original cwd");
499 rel
= get_relative_cwd(buffer
, PATH_MAX
, get_git_work_tree());
500 if (rel
&& *rel
&& chdir(get_git_work_tree()))
501 die_errno ("Could not jump to working directory");
502 return rel
&& *rel
? strcat(rel
, "/") : NULL
;