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 size_t root_len
, len
, total
;
22 const char *work_tree
= get_git_work_tree();
25 len
= strlen(work_tree
);
26 root_len
= offset_1st_component(work_tree
);
27 total
= strlen(sanitized
) + 1;
28 if (strncmp(sanitized
, work_tree
, len
) ||
29 (len
> root_len
&& sanitized
[len
] != '\0' && sanitized
[len
] != '/')) {
31 die("'%s' is outside repository", orig
);
33 if (sanitized
[len
] == '/')
35 memmove(sanitized
, sanitized
+ len
, total
- len
);
41 * Unlike prefix_path, this should be used if the named file does
42 * not have to interact with index entry; i.e. name of a random file
45 const char *prefix_filename(const char *pfx
, int pfx_len
, const char *arg
)
47 static char path
[PATH_MAX
];
49 if (!pfx
|| !*pfx
|| is_absolute_path(arg
))
51 memcpy(path
, pfx
, pfx_len
);
52 strcpy(path
+ pfx_len
, arg
);
55 /* don't add prefix to absolute paths, but still replace '\' by '/' */
56 if (is_absolute_path(arg
))
59 memcpy(path
, pfx
, pfx_len
);
60 strcpy(path
+ pfx_len
, arg
);
61 for (p
= path
+ pfx_len
; *p
; p
++)
68 int check_filename(const char *prefix
, const char *arg
)
73 name
= prefix
? prefix_filename(prefix
, strlen(prefix
), arg
) : arg
;
74 if (!lstat(name
, &st
))
75 return 1; /* file exists */
76 if (errno
== ENOENT
|| errno
== ENOTDIR
)
77 return 0; /* file does not exist */
78 die_errno("failed to stat '%s'", arg
);
81 static void NORETURN
die_verify_filename(const char *prefix
, const char *arg
)
83 unsigned char sha1
[20];
85 /* try a detailed diagnostic ... */
86 get_sha1_with_mode_1(arg
, sha1
, &mode
, 0, prefix
);
87 /* ... or fall back the most general message. */
88 die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
89 "Use '--' to separate paths from revisions", arg
);
94 * Verify a filename that we got as an argument for a pathspec
95 * entry. Note that a filename that begins with "-" never verifies
96 * as true, because even if such a filename were to exist, we want
97 * it to be preceded by the "--" marker (or we want the user to
98 * use a format like "./-filename")
100 void verify_filename(const char *prefix
, const char *arg
)
103 die("bad flag '%s' used after filename", arg
);
104 if (check_filename(prefix
, arg
))
106 die_verify_filename(prefix
, arg
);
110 * Opposite of the above: the command line did not have -- marker
111 * and we parsed the arg as a refname. It should not be interpretable
114 void verify_non_filename(const char *prefix
, const char *arg
)
116 if (!is_inside_work_tree() || is_inside_git_dir())
120 if (!check_filename(prefix
, arg
))
122 die("ambiguous argument '%s': both revision and filename\n"
123 "Use '--' to separate filenames from revisions", arg
);
126 const char **get_pathspec(const char *prefix
, const char **pathspec
)
128 const char *entry
= *pathspec
;
129 const char **src
, **dst
;
132 if (!prefix
&& !entry
)
136 static const char *spec
[2];
142 /* Otherwise we have to re-write the entries.. */
145 prefixlen
= prefix
? strlen(prefix
) : 0;
147 const char *p
= prefix_path(prefix
, prefixlen
, *src
);
158 * Test if it looks like we're at a git directory.
161 * - either an objects/ directory _or_ the proper
162 * GIT_OBJECT_DIRECTORY environment variable
163 * - a refs/ directory
164 * - either a HEAD symlink or a HEAD file that is formatted as
165 * a proper "ref:", or a regular file HEAD that has a properly
166 * formatted sha1 object name.
168 static int is_git_directory(const char *suspect
)
171 size_t len
= strlen(suspect
);
173 if (PATH_MAX
<= len
+ strlen("/objects"))
174 die("Too long path: %.*s", 60, suspect
);
175 strcpy(path
, suspect
);
176 if (getenv(DB_ENVIRONMENT
)) {
177 if (access(getenv(DB_ENVIRONMENT
), X_OK
))
181 strcpy(path
+ len
, "/objects");
182 if (access(path
, X_OK
))
186 strcpy(path
+ len
, "/refs");
187 if (access(path
, X_OK
))
190 strcpy(path
+ len
, "/HEAD");
191 if (validate_headref(path
))
197 int is_inside_git_dir(void)
199 if (inside_git_dir
< 0)
200 inside_git_dir
= is_inside_dir(get_git_dir());
201 return inside_git_dir
;
204 int is_inside_work_tree(void)
206 if (inside_work_tree
< 0)
207 inside_work_tree
= is_inside_dir(get_git_work_tree());
208 return inside_work_tree
;
212 * set_work_tree() is only ever called if you set GIT_DIR explicitly.
213 * The old behaviour (which we retain here) is to set the work tree root
214 * to the cwd, unless overridden by the config, the command line, or
217 static const char *set_work_tree(const char *dir
)
219 char buffer
[PATH_MAX
+ 1];
221 if (!getcwd(buffer
, sizeof(buffer
)))
222 die ("Could not get the current working directory");
223 git_work_tree_cfg
= xstrdup(buffer
);
224 inside_work_tree
= 1;
229 void setup_work_tree(void)
231 const char *work_tree
, *git_dir
;
232 static int initialized
= 0;
236 work_tree
= get_git_work_tree();
237 git_dir
= get_git_dir();
238 if (!is_absolute_path(git_dir
))
239 git_dir
= make_absolute_path(git_dir
);
240 if (!work_tree
|| chdir(work_tree
))
241 die("This operation must be run in a work tree");
242 set_git_dir(make_relative_path(git_dir
, work_tree
));
246 static int check_repository_format_gently(int *nongit_ok
)
248 git_config(check_repository_format_version
, NULL
);
249 if (GIT_REPO_VERSION
< repository_format_version
) {
251 die ("Expected git repo version <= %d, found %d",
252 GIT_REPO_VERSION
, repository_format_version
);
253 warning("Expected git repo version <= %d, found %d",
254 GIT_REPO_VERSION
, repository_format_version
);
255 warning("Please upgrade Git");
263 * Try to read the location of the git directory from the .git file,
264 * return path to git directory if found.
266 const char *read_gitfile_gently(const char *path
)
277 if (!S_ISREG(st
.st_mode
))
279 fd
= open(path
, O_RDONLY
);
281 die_errno("Error opening '%s'", path
);
282 buf
= xmalloc(st
.st_size
+ 1);
283 len
= read_in_full(fd
, buf
, st
.st_size
);
285 if (len
!= st
.st_size
)
286 die("Error reading %s", path
);
288 if (prefixcmp(buf
, "gitdir: "))
289 die("Invalid gitfile format: %s", path
);
290 while (buf
[len
- 1] == '\n' || buf
[len
- 1] == '\r')
293 die("No path in gitfile: %s", path
);
297 if (!is_absolute_path(dir
) && (slash
= strrchr(path
, '/'))) {
298 size_t pathlen
= slash
+1 - path
;
299 size_t dirlen
= pathlen
+ len
- 8;
300 dir
= xmalloc(dirlen
+ 1);
301 strncpy(dir
, path
, pathlen
);
302 strncpy(dir
+ pathlen
, buf
+ 8, len
- 8);
308 if (!is_git_directory(dir
))
309 die("Not a git repository: %s", dir
);
310 path
= make_absolute_path(dir
);
317 * We cannot decide in this function whether we are in the work tree or
318 * not, since the config can only be read _after_ this function was called.
320 const char *setup_git_directory_gently(int *nongit_ok
)
322 const char *work_tree_env
= getenv(GIT_WORK_TREE_ENVIRONMENT
);
323 const char *env_ceiling_dirs
= getenv(CEILING_DIRECTORIES_ENVIRONMENT
);
324 static char cwd
[PATH_MAX
+1];
325 const char *gitdirenv
;
326 const char *gitfile_dir
;
327 int len
, offset
, ceil_offset
, root_len
;
330 * Let's assume that we are in a git repository.
331 * If it turns out later that we are somewhere else, the value will be
332 * updated accordingly.
338 * If GIT_DIR is set explicitly, we're not going
339 * to do any discovery, but we still do repository
342 gitdirenv
= getenv(GIT_DIR_ENVIRONMENT
);
344 if (PATH_MAX
- 40 < strlen(gitdirenv
))
345 die("'$%s' too big", GIT_DIR_ENVIRONMENT
);
346 if (is_git_directory(gitdirenv
)) {
347 static char buffer
[1024 + 1];
350 if (!work_tree_env
) {
351 retval
= set_work_tree(gitdirenv
);
352 /* config may override worktree */
353 if (check_repository_format_gently(nongit_ok
))
357 if (check_repository_format_gently(nongit_ok
))
359 retval
= get_relative_cwd(buffer
, sizeof(buffer
) - 1,
360 get_git_work_tree());
361 if (!retval
|| !*retval
)
363 set_git_dir(make_absolute_path(gitdirenv
));
364 if (chdir(work_tree_env
) < 0)
365 die_errno ("Could not chdir to '%s'", work_tree_env
);
373 die("Not a git repository: '%s'", gitdirenv
);
376 if (!getcwd(cwd
, sizeof(cwd
)-1))
377 die_errno("Unable to read current working directory");
379 ceil_offset
= longest_ancestor_length(cwd
, env_ceiling_dirs
);
380 if (ceil_offset
< 0 && has_dos_drive_prefix(cwd
))
384 * Test in the following order (relative to the cwd):
385 * - .git (file containing "gitdir: <path>")
394 offset
= len
= strlen(cwd
);
396 gitfile_dir
= read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT
);
398 if (set_git_dir(gitfile_dir
))
399 die("Repository setup failed");
402 if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT
))
404 if (is_git_directory(".")) {
407 inside_work_tree
= 0;
409 root_len
= offset_1st_component(cwd
);
410 cwd
[offset
> root_len
? offset
: root_len
] = '\0';
414 check_repository_format_gently(nongit_ok
);
417 while (--offset
> ceil_offset
&& cwd
[offset
] != '/');
418 if (offset
<= ceil_offset
) {
421 die_errno("Cannot come back to cwd");
425 die("Not a git repository (or any of the parent directories): %s", DEFAULT_GIT_DIR_ENVIRONMENT
);
428 die_errno("Cannot change to '%s/..'", cwd
);
433 inside_work_tree
= 1;
434 root_len
= offset_1st_component(cwd
);
435 git_work_tree_cfg
= xstrndup(cwd
, offset
> root_len
? offset
: root_len
);
436 if (check_repository_format_gently(nongit_ok
))
441 /* Make "offset" point to past the '/', and add a '/' at the end */
448 int git_config_perm(const char *var
, const char *value
)
456 if (!strcmp(value
, "umask"))
458 if (!strcmp(value
, "group"))
460 if (!strcmp(value
, "all") ||
461 !strcmp(value
, "world") ||
462 !strcmp(value
, "everybody"))
463 return PERM_EVERYBODY
;
465 /* Parse octal numbers */
466 i
= strtol(value
, &endptr
, 8);
468 /* If not an octal number, maybe true/false? */
470 return git_config_bool(var
, value
) ? PERM_GROUP
: PERM_UMASK
;
473 * Treat values 0, 1 and 2 as compatibility cases, otherwise it is
474 * a chmod value to restrict to.
477 case PERM_UMASK
: /* 0 */
479 case OLD_PERM_GROUP
: /* 1 */
481 case OLD_PERM_EVERYBODY
: /* 2 */
482 return PERM_EVERYBODY
;
485 /* A filemode value was given: 0xxx */
487 if ((i
& 0600) != 0600)
488 die("Problem with core.sharedRepository filemode value "
489 "(0%.3o).\nThe owner of files must always have "
490 "read and write permissions.", i
);
493 * Mask filemode value. Others can not get write permission.
494 * x flags for directories are handled separately.
499 int check_repository_format_version(const char *var
, const char *value
, void *cb
)
501 if (strcmp(var
, "core.repositoryformatversion") == 0)
502 repository_format_version
= git_config_int(var
, value
);
503 else if (strcmp(var
, "core.sharedrepository") == 0)
504 shared_repository
= git_config_perm(var
, value
);
505 else if (strcmp(var
, "core.bare") == 0) {
506 is_bare_repository_cfg
= git_config_bool(var
, value
);
507 if (is_bare_repository_cfg
== 1)
508 inside_work_tree
= -1;
509 } else if (strcmp(var
, "core.worktree") == 0) {
511 return config_error_nonbool(var
);
512 free(git_work_tree_cfg
);
513 git_work_tree_cfg
= xstrdup(value
);
514 inside_work_tree
= -1;
519 int check_repository_format(void)
521 return check_repository_format_gently(NULL
);
525 * Returns the "prefix", a path to the current working directory
526 * relative to the work tree root, or NULL, if the current working
527 * directory is not a strict subdirectory of the work tree root. The
528 * prefix always ends with a '/' character.
530 const char *setup_git_directory(void)
532 const char *retval
= setup_git_directory_gently(NULL
);
534 /* If the work tree is not the default one, recompute prefix */
535 if (inside_work_tree
< 0) {
536 static char buffer
[PATH_MAX
+ 1];
538 if (retval
&& chdir(retval
))
539 die_errno ("Could not jump back into original cwd");
540 rel
= get_relative_cwd(buffer
, PATH_MAX
, get_git_work_tree());
541 if (rel
&& *rel
&& chdir(get_git_work_tree()))
542 die_errno ("Could not jump to working directory");
543 return rel
&& *rel
? strcat(rel
, "/") : NULL
;