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
)) {
22 const char *work_tree
= get_git_work_tree();
25 len
= strlen(work_tree
);
26 total
= strlen(sanitized
) + 1;
27 if (strncmp(sanitized
, work_tree
, len
) ||
28 (sanitized
[len
] != '\0' && sanitized
[len
] != '/')) {
30 die("'%s' is outside repository", orig
);
32 if (sanitized
[len
] == '/')
34 memmove(sanitized
, sanitized
+ len
, total
- len
);
40 * Unlike prefix_path, this should be used if the named file does
41 * not have to interact with index entry; i.e. name of a random file
44 const char *prefix_filename(const char *pfx
, int pfx_len
, const char *arg
)
46 static char path
[PATH_MAX
];
48 if (!pfx
|| !*pfx
|| is_absolute_path(arg
))
50 memcpy(path
, pfx
, pfx_len
);
51 strcpy(path
+ pfx_len
, arg
);
54 /* don't add prefix to absolute paths, but still replace '\' by '/' */
55 if (is_absolute_path(arg
))
58 memcpy(path
, pfx
, pfx_len
);
59 strcpy(path
+ pfx_len
, arg
);
60 for (p
= path
+ pfx_len
; *p
; p
++)
68 * Verify a filename that we got as an argument for a pathspec
69 * entry. Note that a filename that begins with "-" never verifies
70 * as true, because even if such a filename were to exist, we want
71 * it to be preceded by the "--" marker (or we want the user to
72 * use a format like "./-filename")
74 void verify_filename(const char *prefix
, const char *arg
)
80 die("bad flag '%s' used after filename", arg
);
81 name
= prefix
? prefix_filename(prefix
, strlen(prefix
), arg
) : arg
;
82 if (!lstat(name
, &st
))
85 die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
86 "Use '--' to separate paths from revisions", arg
);
87 die_errno("failed to stat '%s'", arg
);
91 * Opposite of the above: the command line did not have -- marker
92 * and we parsed the arg as a refname. It should not be interpretable
95 void verify_non_filename(const char *prefix
, const char *arg
)
100 if (!is_inside_work_tree() || is_inside_git_dir())
104 name
= prefix
? prefix_filename(prefix
, strlen(prefix
), arg
) : arg
;
105 if (!lstat(name
, &st
))
106 die("ambiguous argument '%s': both revision and filename\n"
107 "Use '--' to separate filenames from revisions", arg
);
108 if (errno
!= ENOENT
&& errno
!= ENOTDIR
)
109 die_errno("failed to stat '%s'", arg
);
112 const char **get_pathspec(const char *prefix
, const char **pathspec
)
114 const char *entry
= *pathspec
;
115 const char **src
, **dst
;
118 if (!prefix
&& !entry
)
122 static const char *spec
[2];
128 /* Otherwise we have to re-write the entries.. */
131 prefixlen
= prefix
? strlen(prefix
) : 0;
133 const char *p
= prefix_path(prefix
, prefixlen
, *src
);
144 * Test if it looks like we're at a git directory.
147 * - either an objects/ directory _or_ the proper
148 * GIT_OBJECT_DIRECTORY environment variable
149 * - a refs/ directory
150 * - either a HEAD symlink or a HEAD file that is formatted as
151 * a proper "ref:", or a regular file HEAD that has a properly
152 * formatted sha1 object name.
154 static int is_git_directory(const char *suspect
)
157 size_t len
= strlen(suspect
);
159 strcpy(path
, suspect
);
160 if (getenv(DB_ENVIRONMENT
)) {
161 if (access(getenv(DB_ENVIRONMENT
), X_OK
))
165 strcpy(path
+ len
, "/objects");
166 if (access(path
, X_OK
))
170 strcpy(path
+ len
, "/refs");
171 if (access(path
, X_OK
))
174 strcpy(path
+ len
, "/HEAD");
175 if (validate_headref(path
))
181 int is_inside_git_dir(void)
183 if (inside_git_dir
< 0)
184 inside_git_dir
= is_inside_dir(get_git_dir());
185 return inside_git_dir
;
188 int is_inside_work_tree(void)
190 if (inside_work_tree
< 0)
191 inside_work_tree
= is_inside_dir(get_git_work_tree());
192 return inside_work_tree
;
196 * set_work_tree() is only ever called if you set GIT_DIR explicitely.
197 * The old behaviour (which we retain here) is to set the work tree root
198 * to the cwd, unless overridden by the config, the command line, or
201 static const char *set_work_tree(const char *dir
)
203 char buffer
[PATH_MAX
+ 1];
205 if (!getcwd(buffer
, sizeof(buffer
)))
206 die ("Could not get the current working directory");
207 git_work_tree_cfg
= xstrdup(buffer
);
208 inside_work_tree
= 1;
213 void setup_work_tree(void)
215 const char *work_tree
, *git_dir
;
216 static int initialized
= 0;
220 work_tree
= get_git_work_tree();
221 git_dir
= get_git_dir();
222 if (!is_absolute_path(git_dir
))
223 git_dir
= make_absolute_path(git_dir
);
224 if (!work_tree
|| chdir(work_tree
))
225 die("This operation must be run in a work tree");
226 set_git_dir(make_relative_path(git_dir
, work_tree
));
230 static int check_repository_format_gently(int *nongit_ok
)
232 git_config(check_repository_format_version
, NULL
);
233 if (GIT_REPO_VERSION
< repository_format_version
) {
235 die ("Expected git repo version <= %d, found %d",
236 GIT_REPO_VERSION
, repository_format_version
);
237 warning("Expected git repo version <= %d, found %d",
238 GIT_REPO_VERSION
, repository_format_version
);
239 warning("Please upgrade Git");
247 * Try to read the location of the git directory from the .git file,
248 * return path to git directory if found.
250 const char *read_gitfile_gently(const char *path
)
259 if (!S_ISREG(st
.st_mode
))
261 fd
= open(path
, O_RDONLY
);
263 die_errno("Error opening '%s'", path
);
264 buf
= xmalloc(st
.st_size
+ 1);
265 len
= read_in_full(fd
, buf
, st
.st_size
);
267 if (len
!= st
.st_size
)
268 die("Error reading %s", path
);
270 if (prefixcmp(buf
, "gitdir: "))
271 die("Invalid gitfile format: %s", path
);
272 while (buf
[len
- 1] == '\n' || buf
[len
- 1] == '\r')
275 die("No path in gitfile: %s", path
);
277 if (!is_git_directory(buf
+ 8))
278 die("Not a git repository: %s", buf
+ 8);
279 path
= make_absolute_path(buf
+ 8);
285 * We cannot decide in this function whether we are in the work tree or
286 * not, since the config can only be read _after_ this function was called.
288 const char *setup_git_directory_gently(int *nongit_ok
)
290 const char *work_tree_env
= getenv(GIT_WORK_TREE_ENVIRONMENT
);
291 const char *env_ceiling_dirs
= getenv(CEILING_DIRECTORIES_ENVIRONMENT
);
292 static char cwd
[PATH_MAX
+1];
293 const char *gitdirenv
;
294 const char *gitfile_dir
;
295 int len
, offset
, ceil_offset
;
298 * Let's assume that we are in a git repository.
299 * If it turns out later that we are somewhere else, the value will be
300 * updated accordingly.
306 * If GIT_DIR is set explicitly, we're not going
307 * to do any discovery, but we still do repository
310 gitdirenv
= getenv(GIT_DIR_ENVIRONMENT
);
312 if (PATH_MAX
- 40 < strlen(gitdirenv
))
313 die("'$%s' too big", GIT_DIR_ENVIRONMENT
);
314 if (is_git_directory(gitdirenv
)) {
315 static char buffer
[1024 + 1];
318 if (!work_tree_env
) {
319 retval
= set_work_tree(gitdirenv
);
320 /* config may override worktree */
321 if (check_repository_format_gently(nongit_ok
))
325 if (check_repository_format_gently(nongit_ok
))
327 retval
= get_relative_cwd(buffer
, sizeof(buffer
) - 1,
328 get_git_work_tree());
329 if (!retval
|| !*retval
)
331 set_git_dir(make_absolute_path(gitdirenv
));
332 if (chdir(work_tree_env
) < 0)
333 die_errno ("Could not chdir to '%s'", work_tree_env
);
341 die("Not a git repository: '%s'", gitdirenv
);
344 if (!getcwd(cwd
, sizeof(cwd
)-1))
345 die_errno("Unable to read current working directory");
347 ceil_offset
= longest_ancestor_length(cwd
, env_ceiling_dirs
);
348 if (ceil_offset
< 0 && has_dos_drive_prefix(cwd
))
352 * Test in the following order (relative to the cwd):
353 * - .git (file containing "gitdir: <path>")
362 offset
= len
= strlen(cwd
);
364 gitfile_dir
= read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT
);
366 if (set_git_dir(gitfile_dir
))
367 die("Repository setup failed");
370 if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT
))
372 if (is_git_directory(".")) {
375 inside_work_tree
= 0;
378 setenv(GIT_DIR_ENVIRONMENT
, cwd
, 1);
380 setenv(GIT_DIR_ENVIRONMENT
, ".", 1);
381 check_repository_format_gently(nongit_ok
);
384 while (--offset
> ceil_offset
&& cwd
[offset
] != '/');
385 if (offset
<= ceil_offset
) {
388 die_errno("Cannot come back to cwd");
392 die("Not a git repository (or any of the parent directories): %s", DEFAULT_GIT_DIR_ENVIRONMENT
);
395 die_errno("Cannot change to '%s/..'", cwd
);
400 inside_work_tree
= 1;
401 git_work_tree_cfg
= xstrndup(cwd
, offset
);
402 if (check_repository_format_gently(nongit_ok
))
407 /* Make "offset" point to past the '/', and add a '/' at the end */
414 int git_config_perm(const char *var
, const char *value
)
422 if (!strcmp(value
, "umask"))
424 if (!strcmp(value
, "group"))
426 if (!strcmp(value
, "all") ||
427 !strcmp(value
, "world") ||
428 !strcmp(value
, "everybody"))
429 return PERM_EVERYBODY
;
431 /* Parse octal numbers */
432 i
= strtol(value
, &endptr
, 8);
434 /* If not an octal number, maybe true/false? */
436 return git_config_bool(var
, value
) ? PERM_GROUP
: PERM_UMASK
;
439 * Treat values 0, 1 and 2 as compatibility cases, otherwise it is
440 * a chmod value to restrict to.
443 case PERM_UMASK
: /* 0 */
445 case OLD_PERM_GROUP
: /* 1 */
447 case OLD_PERM_EVERYBODY
: /* 2 */
448 return PERM_EVERYBODY
;
451 /* A filemode value was given: 0xxx */
453 if ((i
& 0600) != 0600)
454 die("Problem with core.sharedRepository filemode value "
455 "(0%.3o).\nThe owner of files must always have "
456 "read and write permissions.", i
);
459 * Mask filemode value. Others can not get write permission.
460 * x flags for directories are handled separately.
465 int check_repository_format_version(const char *var
, const char *value
, void *cb
)
467 if (strcmp(var
, "core.repositoryformatversion") == 0)
468 repository_format_version
= git_config_int(var
, value
);
469 else if (strcmp(var
, "core.sharedrepository") == 0)
470 shared_repository
= git_config_perm(var
, value
);
471 else if (strcmp(var
, "core.bare") == 0) {
472 is_bare_repository_cfg
= git_config_bool(var
, value
);
473 if (is_bare_repository_cfg
== 1)
474 inside_work_tree
= -1;
475 } else if (strcmp(var
, "core.worktree") == 0) {
477 return config_error_nonbool(var
);
478 free(git_work_tree_cfg
);
479 git_work_tree_cfg
= xstrdup(value
);
480 inside_work_tree
= -1;
485 int check_repository_format(void)
487 return check_repository_format_gently(NULL
);
490 const char *setup_git_directory(void)
492 const char *retval
= setup_git_directory_gently(NULL
);
494 /* If the work tree is not the default one, recompute prefix */
495 if (inside_work_tree
< 0) {
496 static char buffer
[PATH_MAX
+ 1];
498 if (retval
&& chdir(retval
))
499 die_errno ("Could not jump back into original cwd");
500 rel
= get_relative_cwd(buffer
, PATH_MAX
, get_git_work_tree());
501 if (rel
&& *rel
&& chdir(get_git_work_tree()))
502 die_errno ("Could not jump to working directory");
503 return rel
&& *rel
? strcat(rel
, "/") : NULL
;