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.
39 } else if (is_dir_sep(src
[1])) {
42 while (is_dir_sep(*src
))
45 } else if (src
[1] == '.') {
50 } else if (is_dir_sep(src
[2])) {
53 while (is_dir_sep(*src
))
60 /* copy up to the next '/', and eat all '/' */
61 while ((c
= *src
++) != '\0' && !is_dir_sep(c
))
74 * dst0..dst is prefix portion, and dst[-1] is '/';
77 dst
-= 2; /* go past trailing '/' if any */
84 if (c
== '/') { /* MinGW: cannot be '\\' anymore */
94 const char *prefix_path(const char *prefix
, int len
, const char *path
)
96 const char *orig
= path
;
97 char *sanitized
= xmalloc(len
+ strlen(path
) + 1);
98 if (is_absolute_path(orig
))
99 strcpy(sanitized
, path
);
102 memcpy(sanitized
, prefix
, len
);
103 strcpy(sanitized
+ len
, path
);
105 if (sanitary_path_copy(sanitized
, sanitized
))
107 if (is_absolute_path(orig
)) {
108 const char *work_tree
= get_git_work_tree();
109 size_t len
= strlen(work_tree
);
110 size_t total
= strlen(sanitized
) + 1;
111 if (strncmp(sanitized
, work_tree
, len
) ||
112 (sanitized
[len
] != '\0' && sanitized
[len
] != '/')) {
114 error("'%s' is outside repository", orig
);
118 if (sanitized
[len
] == '/')
120 memmove(sanitized
, sanitized
+ len
, total
- len
);
126 * Unlike prefix_path, this should be used if the named file does
127 * not have to interact with index entry; i.e. name of a random file
130 const char *prefix_filename(const char *pfx
, int pfx_len
, const char *arg
)
132 static char path
[PATH_MAX
];
134 if (!pfx
|| !*pfx
|| is_absolute_path(arg
))
136 memcpy(path
, pfx
, pfx_len
);
137 strcpy(path
+ pfx_len
, arg
);
140 /* don't add prefix to absolute paths, but still replace '\' by '/' */
141 if (is_absolute_path(arg
))
144 memcpy(path
, pfx
, pfx_len
);
145 strcpy(path
+ pfx_len
, arg
);
146 for (p
= path
+ pfx_len
; *p
; p
++)
154 * Verify a filename that we got as an argument for a pathspec
155 * entry. Note that a filename that begins with "-" never verifies
156 * as true, because even if such a filename were to exist, we want
157 * it to be preceded by the "--" marker (or we want the user to
158 * use a format like "./-filename")
160 void verify_filename(const char *prefix
, const char *arg
)
166 die("bad flag '%s' used after filename", arg
);
167 name
= prefix
? prefix_filename(prefix
, strlen(prefix
), arg
) : arg
;
168 if (!lstat(name
, &st
))
171 die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
172 "Use '--' to separate paths from revisions", arg
);
173 die("'%s': %s", arg
, strerror(errno
));
177 * Opposite of the above: the command line did not have -- marker
178 * and we parsed the arg as a refname. It should not be interpretable
181 void verify_non_filename(const char *prefix
, const char *arg
)
186 if (!is_inside_work_tree() || is_inside_git_dir())
190 name
= prefix
? prefix_filename(prefix
, strlen(prefix
), arg
) : arg
;
191 if (!lstat(name
, &st
))
192 die("ambiguous argument '%s': both revision and filename\n"
193 "Use '--' to separate filenames from revisions", arg
);
194 if (errno
!= ENOENT
&& errno
!= ENOTDIR
)
195 die("'%s': %s", arg
, strerror(errno
));
198 const char **get_pathspec(const char *prefix
, const char **pathspec
)
200 const char *entry
= *pathspec
;
201 const char **src
, **dst
;
204 if (!prefix
&& !entry
)
208 static const char *spec
[2];
214 /* Otherwise we have to re-write the entries.. */
217 prefixlen
= prefix
? strlen(prefix
) : 0;
219 const char *p
= prefix_path(prefix
, prefixlen
, *src
);
223 exit(128); /* error message already given */
233 * Test if it looks like we're at a git directory.
236 * - either an objects/ directory _or_ the proper
237 * GIT_OBJECT_DIRECTORY environment variable
238 * - a refs/ directory
239 * - either a HEAD symlink or a HEAD file that is formatted as
240 * a proper "ref:", or a regular file HEAD that has a properly
241 * formatted sha1 object name.
243 static int is_git_directory(const char *suspect
)
246 size_t len
= strlen(suspect
);
248 strcpy(path
, suspect
);
249 if (getenv(DB_ENVIRONMENT
)) {
250 if (access(getenv(DB_ENVIRONMENT
), X_OK
))
254 strcpy(path
+ len
, "/objects");
255 if (access(path
, X_OK
))
259 strcpy(path
+ len
, "/refs");
260 if (access(path
, X_OK
))
263 strcpy(path
+ len
, "/HEAD");
264 if (validate_headref(path
))
270 int is_inside_git_dir(void)
272 if (inside_git_dir
< 0)
273 inside_git_dir
= is_inside_dir(get_git_dir());
274 return inside_git_dir
;
277 int is_inside_work_tree(void)
279 if (inside_work_tree
< 0)
280 inside_work_tree
= is_inside_dir(get_git_work_tree());
281 return inside_work_tree
;
285 * set_work_tree() is only ever called if you set GIT_DIR explicitely.
286 * The old behaviour (which we retain here) is to set the work tree root
287 * to the cwd, unless overridden by the config, the command line, or
290 static const char *set_work_tree(const char *dir
)
292 char buffer
[PATH_MAX
+ 1];
294 if (!getcwd(buffer
, sizeof(buffer
)))
295 die ("Could not get the current working directory");
296 git_work_tree_cfg
= xstrdup(buffer
);
297 inside_work_tree
= 1;
302 void setup_work_tree(void)
304 const char *work_tree
, *git_dir
;
305 static int initialized
= 0;
309 work_tree
= get_git_work_tree();
310 git_dir
= get_git_dir();
311 if (!is_absolute_path(git_dir
))
312 set_git_dir(make_absolute_path(git_dir
));
313 if (!work_tree
|| chdir(work_tree
))
314 die("This operation must be run in a work tree");
318 static int check_repository_format_gently(int *nongit_ok
)
320 git_config(check_repository_format_version
);
321 if (GIT_REPO_VERSION
< repository_format_version
) {
323 die ("Expected git repo version <= %d, found %d",
324 GIT_REPO_VERSION
, repository_format_version
);
325 warning("Expected git repo version <= %d, found %d",
326 GIT_REPO_VERSION
, repository_format_version
);
327 warning("Please upgrade Git");
335 * We cannot decide in this function whether we are in the work tree or
336 * not, since the config can only be read _after_ this function was called.
338 const char *setup_git_directory_gently(int *nongit_ok
)
340 const char *work_tree_env
= getenv(GIT_WORK_TREE_ENVIRONMENT
);
341 static char cwd
[PATH_MAX
+1];
342 const char *gitdirenv
;
347 * Let's assume that we are in a git repository.
348 * If it turns out later that we are somewhere else, the value will be
349 * updated accordingly.
355 * If GIT_DIR is set explicitly, we're not going
356 * to do any discovery, but we still do repository
359 gitdirenv
= getenv(GIT_DIR_ENVIRONMENT
);
361 if (PATH_MAX
- 40 < strlen(gitdirenv
))
362 die("'$%s' too big", GIT_DIR_ENVIRONMENT
);
363 if (is_git_directory(gitdirenv
)) {
364 static char buffer
[1024 + 1];
367 if (!work_tree_env
) {
368 retval
= set_work_tree(gitdirenv
);
369 /* config may override worktree */
370 if (check_repository_format_gently(nongit_ok
))
374 if (check_repository_format_gently(nongit_ok
))
376 retval
= get_relative_cwd(buffer
, sizeof(buffer
) - 1,
377 get_git_work_tree());
378 if (!retval
|| !*retval
)
380 set_git_dir(make_absolute_path(gitdirenv
));
381 if (chdir(work_tree_env
) < 0)
382 die ("Could not chdir to %s", work_tree_env
);
390 die("Not a git repository: '%s'", gitdirenv
);
393 if (!getcwd(cwd
, sizeof(cwd
)-1))
394 die("Unable to read current working directory");
395 if (has_dos_drive_prefix(cwd
))
399 * Test in the following order (relative to the cwd):
407 offset
= len
= strlen(cwd
);
409 if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT
))
411 if (is_git_directory(".")) {
414 inside_work_tree
= 0;
415 setenv(GIT_DIR_ENVIRONMENT
, ".", 1);
416 check_repository_format_gently(nongit_ok
);
421 if (offset
<= minoffset
) {
424 die("Cannot come back to cwd");
428 die("Not a git repository");
430 } while (offset
> minoffset
&& cwd
[--offset
] != '/');
435 inside_work_tree
= 1;
436 git_work_tree_cfg
= xstrndup(cwd
, offset
);
437 if (check_repository_format_gently(nongit_ok
))
442 /* Make "offset" point to past the '/', and add a '/' at the end */
449 int git_config_perm(const char *var
, const char *value
)
457 if (!strcmp(value
, "umask"))
459 if (!strcmp(value
, "group"))
461 if (!strcmp(value
, "all") ||
462 !strcmp(value
, "world") ||
463 !strcmp(value
, "everybody"))
464 return PERM_EVERYBODY
;
466 /* Parse octal numbers */
467 i
= strtol(value
, &endptr
, 8);
469 /* If not an octal number, maybe true/false? */
471 return git_config_bool(var
, value
) ? PERM_GROUP
: PERM_UMASK
;
474 * Treat values 0, 1 and 2 as compatibility cases, otherwise it is
478 case PERM_UMASK
: /* 0 */
480 case OLD_PERM_GROUP
: /* 1 */
482 case OLD_PERM_EVERYBODY
: /* 2 */
483 return PERM_EVERYBODY
;
486 /* A filemode value was given: 0xxx */
488 if ((i
& 0600) != 0600)
489 die("Problem with core.sharedRepository filemode value "
490 "(0%.3o).\nThe owner of files must always have "
491 "read and write permissions.", i
);
494 * Mask filemode value. Others can not get write permission.
495 * x flags for directories are handled separately.
500 int check_repository_format_version(const char *var
, const char *value
)
502 if (strcmp(var
, "core.repositoryformatversion") == 0)
503 repository_format_version
= git_config_int(var
, value
);
504 else if (strcmp(var
, "core.sharedrepository") == 0)
505 shared_repository
= git_config_perm(var
, value
);
506 else if (strcmp(var
, "core.bare") == 0) {
507 is_bare_repository_cfg
= git_config_bool(var
, value
);
508 if (is_bare_repository_cfg
== 1)
509 inside_work_tree
= -1;
510 } else if (strcmp(var
, "core.worktree") == 0) {
512 return config_error_nonbool(var
);
513 free(git_work_tree_cfg
);
514 git_work_tree_cfg
= xstrdup(value
);
515 inside_work_tree
= -1;
520 int check_repository_format(void)
522 return check_repository_format_gently(NULL
);
525 const char *setup_git_directory(void)
527 const char *retval
= setup_git_directory_gently(NULL
);
529 /* If the work tree is not the default one, recompute prefix */
530 if (inside_work_tree
< 0) {
531 static char buffer
[PATH_MAX
+ 1];
533 if (retval
&& chdir(retval
))
534 die ("Could not jump back into original cwd");
535 rel
= get_relative_cwd(buffer
, PATH_MAX
, get_git_work_tree());
536 return rel
&& *rel
? strcat(rel
, "/") : NULL
;