4 static int inside_git_dir
= -1;
5 static int inside_work_tree
= -1;
7 static int sanitary_path_copy(char *dst
, const char *src
)
11 if (has_dos_drive_prefix(src
)) {
17 if (is_dir_sep(*src
)) {
19 while (is_dir_sep(*src
))
27 * A path component that begins with . could be
29 * (1) "." and ends -- ignore and terminate.
30 * (2) "./" -- ignore them, eat slash and continue.
31 * (3) ".." and ends -- strip one and terminate.
32 * (4) "../" -- strip one, eat slash and continue.
38 } else if (is_dir_sep(src
[1])) {
41 while (is_dir_sep(*src
))
44 } else if (src
[1] == '.') {
49 } else if (is_dir_sep(src
[2])) {
52 while (is_dir_sep(*src
))
59 /* copy up to the next '/', and eat all '/' */
60 while ((c
= *src
++) != '\0' && !is_dir_sep(c
))
73 * dst0..dst is prefix portion, and dst[-1] is '/';
76 dst
-= 2; /* go past trailing '/' if any */
83 if (c
== '/') { /* MinGW: cannot be '\\' anymore */
93 const char *prefix_path(const char *prefix
, int len
, const char *path
)
95 const char *orig
= path
;
96 char *sanitized
= xmalloc(len
+ strlen(path
) + 1);
97 if (is_absolute_path(orig
))
98 strcpy(sanitized
, path
);
101 memcpy(sanitized
, prefix
, len
);
102 strcpy(sanitized
+ len
, path
);
104 if (sanitary_path_copy(sanitized
, sanitized
))
106 if (is_absolute_path(orig
)) {
107 const char *work_tree
= get_git_work_tree();
108 size_t len
= strlen(work_tree
);
109 size_t total
= strlen(sanitized
) + 1;
110 if (strncmp(sanitized
, work_tree
, len
) ||
111 (sanitized
[len
] != '\0' && sanitized
[len
] != '/')) {
113 die("'%s' is outside repository", orig
);
115 if (sanitized
[len
] == '/')
117 memmove(sanitized
, sanitized
+ len
, total
- len
);
123 * Unlike prefix_path, this should be used if the named file does
124 * not have to interact with index entry; i.e. name of a random file
127 const char *prefix_filename(const char *pfx
, int pfx_len
, const char *arg
)
129 static char path
[PATH_MAX
];
131 if (!pfx
|| !*pfx
|| is_absolute_path(arg
))
133 memcpy(path
, pfx
, pfx_len
);
134 strcpy(path
+ pfx_len
, arg
);
137 /* don't add prefix to absolute paths, but still replace '\' by '/' */
138 if (is_absolute_path(arg
))
141 memcpy(path
, pfx
, pfx_len
);
142 strcpy(path
+ pfx_len
, arg
);
143 for (p
= path
+ pfx_len
; *p
; p
++)
151 * Verify a filename that we got as an argument for a pathspec
152 * entry. Note that a filename that begins with "-" never verifies
153 * as true, because even if such a filename were to exist, we want
154 * it to be preceded by the "--" marker (or we want the user to
155 * use a format like "./-filename")
157 void verify_filename(const char *prefix
, const char *arg
)
163 die("bad flag '%s' used after filename", arg
);
164 name
= prefix
? prefix_filename(prefix
, strlen(prefix
), arg
) : arg
;
165 if (!lstat(name
, &st
))
168 die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
169 "Use '--' to separate paths from revisions", arg
);
170 die("'%s': %s", arg
, strerror(errno
));
174 * Opposite of the above: the command line did not have -- marker
175 * and we parsed the arg as a refname. It should not be interpretable
178 void verify_non_filename(const char *prefix
, const char *arg
)
183 if (!is_inside_work_tree() || is_inside_git_dir())
187 name
= prefix
? prefix_filename(prefix
, strlen(prefix
), arg
) : arg
;
188 if (!lstat(name
, &st
))
189 die("ambiguous argument '%s': both revision and filename\n"
190 "Use '--' to separate filenames from revisions", arg
);
191 if (errno
!= ENOENT
&& errno
!= ENOTDIR
)
192 die("'%s': %s", arg
, strerror(errno
));
195 const char **get_pathspec(const char *prefix
, const char **pathspec
)
197 const char *entry
= *pathspec
;
198 const char **src
, **dst
;
201 if (!prefix
&& !entry
)
205 static const char *spec
[2];
211 /* Otherwise we have to re-write the entries.. */
214 prefixlen
= prefix
? strlen(prefix
) : 0;
216 const char *p
= prefix_path(prefix
, prefixlen
, *src
);
227 * Test if it looks like we're at a git directory.
230 * - either an objects/ directory _or_ the proper
231 * GIT_OBJECT_DIRECTORY environment variable
232 * - a refs/ directory
233 * - either a HEAD symlink or a HEAD file that is formatted as
234 * a proper "ref:", or a regular file HEAD that has a properly
235 * formatted sha1 object name.
237 static int is_git_directory(const char *suspect
)
240 size_t len
= strlen(suspect
);
242 strcpy(path
, suspect
);
243 if (getenv(DB_ENVIRONMENT
)) {
244 if (access(getenv(DB_ENVIRONMENT
), X_OK
))
248 strcpy(path
+ len
, "/objects");
249 if (access(path
, X_OK
))
253 strcpy(path
+ len
, "/refs");
254 if (access(path
, X_OK
))
257 strcpy(path
+ len
, "/HEAD");
258 if (validate_headref(path
))
264 int is_inside_git_dir(void)
266 if (inside_git_dir
< 0)
267 inside_git_dir
= is_inside_dir(get_git_dir());
268 return inside_git_dir
;
271 int is_inside_work_tree(void)
273 if (inside_work_tree
< 0)
274 inside_work_tree
= is_inside_dir(get_git_work_tree());
275 return inside_work_tree
;
279 * set_work_tree() is only ever called if you set GIT_DIR explicitely.
280 * The old behaviour (which we retain here) is to set the work tree root
281 * to the cwd, unless overridden by the config, the command line, or
284 static const char *set_work_tree(const char *dir
)
286 char buffer
[PATH_MAX
+ 1];
288 if (!getcwd(buffer
, sizeof(buffer
)))
289 die ("Could not get the current working directory");
290 git_work_tree_cfg
= xstrdup(buffer
);
291 inside_work_tree
= 1;
296 void setup_work_tree(void)
298 const char *work_tree
, *git_dir
;
299 static int initialized
= 0;
303 work_tree
= get_git_work_tree();
304 git_dir
= get_git_dir();
305 if (!is_absolute_path(git_dir
))
306 git_dir
= make_absolute_path(git_dir
);
307 if (!work_tree
|| chdir(work_tree
))
308 die("This operation must be run in a work tree");
309 set_git_dir(make_relative_path(git_dir
, work_tree
));
313 static int check_repository_format_gently(int *nongit_ok
)
315 git_config(check_repository_format_version
, NULL
);
316 if (GIT_REPO_VERSION
< repository_format_version
) {
318 die ("Expected git repo version <= %d, found %d",
319 GIT_REPO_VERSION
, repository_format_version
);
320 warning("Expected git repo version <= %d, found %d",
321 GIT_REPO_VERSION
, repository_format_version
);
322 warning("Please upgrade Git");
330 * Try to read the location of the git directory from the .git file,
331 * return path to git directory if found.
333 const char *read_gitfile_gently(const char *path
)
342 if (!S_ISREG(st
.st_mode
))
344 fd
= open(path
, O_RDONLY
);
346 die("Error opening %s: %s", path
, strerror(errno
));
347 buf
= xmalloc(st
.st_size
+ 1);
348 len
= read_in_full(fd
, buf
, st
.st_size
);
350 if (len
!= st
.st_size
)
351 die("Error reading %s", path
);
353 if (prefixcmp(buf
, "gitdir: "))
354 die("Invalid gitfile format: %s", path
);
355 while (buf
[len
- 1] == '\n' || buf
[len
- 1] == '\r')
358 die("No path in gitfile: %s", path
);
360 if (!is_git_directory(buf
+ 8))
361 die("Not a git repository: %s", buf
+ 8);
362 path
= make_absolute_path(buf
+ 8);
368 * We cannot decide in this function whether we are in the work tree or
369 * not, since the config can only be read _after_ this function was called.
371 const char *setup_git_directory_gently(int *nongit_ok
)
373 const char *work_tree_env
= getenv(GIT_WORK_TREE_ENVIRONMENT
);
374 const char *env_ceiling_dirs
= getenv(CEILING_DIRECTORIES_ENVIRONMENT
);
375 static char cwd
[PATH_MAX
+1];
376 const char *gitdirenv
;
377 const char *gitfile_dir
;
378 int len
, offset
, ceil_offset
;
381 * Let's assume that we are in a git repository.
382 * If it turns out later that we are somewhere else, the value will be
383 * updated accordingly.
389 * If GIT_DIR is set explicitly, we're not going
390 * to do any discovery, but we still do repository
393 gitdirenv
= getenv(GIT_DIR_ENVIRONMENT
);
395 if (PATH_MAX
- 40 < strlen(gitdirenv
))
396 die("'$%s' too big", GIT_DIR_ENVIRONMENT
);
397 if (is_git_directory(gitdirenv
)) {
398 static char buffer
[1024 + 1];
401 if (!work_tree_env
) {
402 retval
= set_work_tree(gitdirenv
);
403 /* config may override worktree */
404 if (check_repository_format_gently(nongit_ok
))
408 if (check_repository_format_gently(nongit_ok
))
410 retval
= get_relative_cwd(buffer
, sizeof(buffer
) - 1,
411 get_git_work_tree());
412 if (!retval
|| !*retval
)
414 set_git_dir(make_absolute_path(gitdirenv
));
415 if (chdir(work_tree_env
) < 0)
416 die ("Could not chdir to %s", work_tree_env
);
424 die("Not a git repository: '%s'", gitdirenv
);
427 if (!getcwd(cwd
, sizeof(cwd
)-1))
428 die("Unable to read current working directory");
430 ceil_offset
= longest_ancestor_length(cwd
, env_ceiling_dirs
);
431 if (ceil_offset
< 0 && has_dos_drive_prefix(cwd
))
435 * Test in the following order (relative to the cwd):
436 * - .git (file containing "gitdir: <path>")
445 offset
= len
= strlen(cwd
);
447 gitfile_dir
= read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT
);
449 if (set_git_dir(gitfile_dir
))
450 die("Repository setup failed");
453 if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT
))
455 if (is_git_directory(".")) {
458 inside_work_tree
= 0;
459 setenv(GIT_DIR_ENVIRONMENT
, ".", 1);
460 check_repository_format_gently(nongit_ok
);
463 while (--offset
> ceil_offset
&& cwd
[offset
] != '/');
464 if (offset
<= ceil_offset
) {
467 die("Cannot come back to cwd");
471 die("Not a git repository");
478 inside_work_tree
= 1;
479 git_work_tree_cfg
= xstrndup(cwd
, offset
);
480 if (check_repository_format_gently(nongit_ok
))
485 /* Make "offset" point to past the '/', and add a '/' at the end */
492 int git_config_perm(const char *var
, const char *value
)
500 if (!strcmp(value
, "umask"))
502 if (!strcmp(value
, "group"))
504 if (!strcmp(value
, "all") ||
505 !strcmp(value
, "world") ||
506 !strcmp(value
, "everybody"))
507 return PERM_EVERYBODY
;
509 /* Parse octal numbers */
510 i
= strtol(value
, &endptr
, 8);
512 /* If not an octal number, maybe true/false? */
514 return git_config_bool(var
, value
) ? PERM_GROUP
: PERM_UMASK
;
517 * Treat values 0, 1 and 2 as compatibility cases, otherwise it is
521 case PERM_UMASK
: /* 0 */
523 case OLD_PERM_GROUP
: /* 1 */
525 case OLD_PERM_EVERYBODY
: /* 2 */
526 return PERM_EVERYBODY
;
529 /* A filemode value was given: 0xxx */
531 if ((i
& 0600) != 0600)
532 die("Problem with core.sharedRepository filemode value "
533 "(0%.3o).\nThe owner of files must always have "
534 "read and write permissions.", i
);
537 * Mask filemode value. Others can not get write permission.
538 * x flags for directories are handled separately.
543 int check_repository_format_version(const char *var
, const char *value
, void *cb
)
545 if (strcmp(var
, "core.repositoryformatversion") == 0)
546 repository_format_version
= git_config_int(var
, value
);
547 else if (strcmp(var
, "core.sharedrepository") == 0)
548 shared_repository
= git_config_perm(var
, value
);
549 else if (strcmp(var
, "core.bare") == 0) {
550 is_bare_repository_cfg
= git_config_bool(var
, value
);
551 if (is_bare_repository_cfg
== 1)
552 inside_work_tree
= -1;
553 } else if (strcmp(var
, "core.worktree") == 0) {
555 return config_error_nonbool(var
);
556 free(git_work_tree_cfg
);
557 git_work_tree_cfg
= xstrdup(value
);
558 inside_work_tree
= -1;
563 int check_repository_format(void)
565 return check_repository_format_gently(NULL
);
568 const char *setup_git_directory(void)
570 const char *retval
= setup_git_directory_gently(NULL
);
572 /* If the work tree is not the default one, recompute prefix */
573 if (inside_work_tree
< 0) {
574 static char buffer
[PATH_MAX
+ 1];
576 if (retval
&& chdir(retval
))
577 die ("Could not jump back into original cwd");
578 rel
= get_relative_cwd(buffer
, PATH_MAX
, get_git_work_tree());
579 if (rel
&& *rel
&& chdir(get_git_work_tree()))
580 die ("Could not jump to working directory");
581 return rel
&& *rel
? strcat(rel
, "/") : NULL
;