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
);
82 * Verify a filename that we got as an argument for a pathspec
83 * entry. Note that a filename that begins with "-" never verifies
84 * as true, because even if such a filename were to exist, we want
85 * it to be preceded by the "--" marker (or we want the user to
86 * use a format like "./-filename")
88 void verify_filename(const char *prefix
, const char *arg
)
91 die("bad flag '%s' used after filename", arg
);
92 if (check_filename(prefix
, arg
))
94 die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
95 "Use '--' to separate paths from revisions", arg
);
99 * Opposite of the above: the command line did not have -- marker
100 * and we parsed the arg as a refname. It should not be interpretable
103 void verify_non_filename(const char *prefix
, const char *arg
)
105 if (!is_inside_work_tree() || is_inside_git_dir())
109 if (!check_filename(prefix
, arg
))
111 die("ambiguous argument '%s': both revision and filename\n"
112 "Use '--' to separate filenames from revisions", arg
);
115 const char **get_pathspec(const char *prefix
, const char **pathspec
)
117 const char *entry
= *pathspec
;
118 const char **src
, **dst
;
121 if (!prefix
&& !entry
)
125 static const char *spec
[2];
131 /* Otherwise we have to re-write the entries.. */
134 prefixlen
= prefix
? strlen(prefix
) : 0;
136 const char *p
= prefix_path(prefix
, prefixlen
, *src
);
147 * Test if it looks like we're at a git directory.
150 * - either an objects/ directory _or_ the proper
151 * GIT_OBJECT_DIRECTORY environment variable
152 * - a refs/ directory
153 * - either a HEAD symlink or a HEAD file that is formatted as
154 * a proper "ref:", or a regular file HEAD that has a properly
155 * formatted sha1 object name.
157 static int is_git_directory(const char *suspect
)
160 size_t len
= strlen(suspect
);
162 strcpy(path
, suspect
);
163 if (getenv(DB_ENVIRONMENT
)) {
164 if (access(getenv(DB_ENVIRONMENT
), X_OK
))
168 strcpy(path
+ len
, "/objects");
169 if (access(path
, X_OK
))
173 strcpy(path
+ len
, "/refs");
174 if (access(path
, X_OK
))
177 strcpy(path
+ len
, "/HEAD");
178 if (validate_headref(path
))
184 int is_inside_git_dir(void)
186 if (inside_git_dir
< 0)
187 inside_git_dir
= is_inside_dir(get_git_dir());
188 return inside_git_dir
;
191 int is_inside_work_tree(void)
193 if (inside_work_tree
< 0)
194 inside_work_tree
= is_inside_dir(get_git_work_tree());
195 return inside_work_tree
;
199 * set_work_tree() is only ever called if you set GIT_DIR explicitely.
200 * The old behaviour (which we retain here) is to set the work tree root
201 * to the cwd, unless overridden by the config, the command line, or
204 static const char *set_work_tree(const char *dir
)
206 char buffer
[PATH_MAX
+ 1];
208 if (!getcwd(buffer
, sizeof(buffer
)))
209 die ("Could not get the current working directory");
210 git_work_tree_cfg
= xstrdup(buffer
);
211 inside_work_tree
= 1;
216 void setup_work_tree(void)
218 const char *work_tree
, *git_dir
;
219 static int initialized
= 0;
223 work_tree
= get_git_work_tree();
224 git_dir
= get_git_dir();
225 if (!is_absolute_path(git_dir
))
226 git_dir
= make_absolute_path(git_dir
);
227 if (!work_tree
|| chdir(work_tree
))
228 die("This operation must be run in a work tree");
229 set_git_dir(make_relative_path(git_dir
, work_tree
));
233 static int check_repository_format_gently(int *nongit_ok
)
235 git_config(check_repository_format_version
, NULL
);
236 if (GIT_REPO_VERSION
< repository_format_version
) {
238 die ("Expected git repo version <= %d, found %d",
239 GIT_REPO_VERSION
, repository_format_version
);
240 warning("Expected git repo version <= %d, found %d",
241 GIT_REPO_VERSION
, repository_format_version
);
242 warning("Please upgrade Git");
250 * Try to read the location of the git directory from the .git file,
251 * return path to git directory if found.
253 const char *read_gitfile_gently(const char *path
)
262 if (!S_ISREG(st
.st_mode
))
264 fd
= open(path
, O_RDONLY
);
266 die_errno("Error opening '%s'", path
);
267 buf
= xmalloc(st
.st_size
+ 1);
268 len
= read_in_full(fd
, buf
, st
.st_size
);
270 if (len
!= st
.st_size
)
271 die("Error reading %s", path
);
273 if (prefixcmp(buf
, "gitdir: "))
274 die("Invalid gitfile format: %s", path
);
275 while (buf
[len
- 1] == '\n' || buf
[len
- 1] == '\r')
278 die("No path in gitfile: %s", path
);
280 if (!is_git_directory(buf
+ 8))
281 die("Not a git repository: %s", buf
+ 8);
282 path
= make_absolute_path(buf
+ 8);
288 * We cannot decide in this function whether we are in the work tree or
289 * not, since the config can only be read _after_ this function was called.
291 const char *setup_git_directory_gently(int *nongit_ok
)
293 const char *work_tree_env
= getenv(GIT_WORK_TREE_ENVIRONMENT
);
294 const char *env_ceiling_dirs
= getenv(CEILING_DIRECTORIES_ENVIRONMENT
);
295 static char cwd
[PATH_MAX
+1];
296 const char *gitdirenv
;
297 const char *gitfile_dir
;
298 int len
, offset
, ceil_offset
, root_len
;
301 * Let's assume that we are in a git repository.
302 * If it turns out later that we are somewhere else, the value will be
303 * updated accordingly.
309 * If GIT_DIR is set explicitly, we're not going
310 * to do any discovery, but we still do repository
313 gitdirenv
= getenv(GIT_DIR_ENVIRONMENT
);
315 if (PATH_MAX
- 40 < strlen(gitdirenv
))
316 die("'$%s' too big", GIT_DIR_ENVIRONMENT
);
317 if (is_git_directory(gitdirenv
)) {
318 static char buffer
[1024 + 1];
321 if (!work_tree_env
) {
322 retval
= set_work_tree(gitdirenv
);
323 /* config may override worktree */
324 if (check_repository_format_gently(nongit_ok
))
328 if (check_repository_format_gently(nongit_ok
))
330 retval
= get_relative_cwd(buffer
, sizeof(buffer
) - 1,
331 get_git_work_tree());
332 if (!retval
|| !*retval
)
334 set_git_dir(make_absolute_path(gitdirenv
));
335 if (chdir(work_tree_env
) < 0)
336 die_errno ("Could not chdir to '%s'", work_tree_env
);
344 die("Not a git repository: '%s'", gitdirenv
);
347 if (!getcwd(cwd
, sizeof(cwd
)-1))
348 die_errno("Unable to read current working directory");
350 ceil_offset
= longest_ancestor_length(cwd
, env_ceiling_dirs
);
351 if (ceil_offset
< 0 && has_dos_drive_prefix(cwd
))
355 * Test in the following order (relative to the cwd):
356 * - .git (file containing "gitdir: <path>")
365 offset
= len
= strlen(cwd
);
367 gitfile_dir
= read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT
);
369 if (set_git_dir(gitfile_dir
))
370 die("Repository setup failed");
373 if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT
))
375 if (is_git_directory(".")) {
378 inside_work_tree
= 0;
380 root_len
= offset_1st_component(cwd
);
381 cwd
[offset
> root_len
? offset
: root_len
] = '\0';
385 check_repository_format_gently(nongit_ok
);
388 while (--offset
> ceil_offset
&& cwd
[offset
] != '/');
389 if (offset
<= ceil_offset
) {
392 die_errno("Cannot come back to cwd");
396 die("Not a git repository (or any of the parent directories): %s", DEFAULT_GIT_DIR_ENVIRONMENT
);
399 die_errno("Cannot change to '%s/..'", cwd
);
404 inside_work_tree
= 1;
405 root_len
= offset_1st_component(cwd
);
406 git_work_tree_cfg
= xstrndup(cwd
, offset
> root_len
? offset
: root_len
);
407 if (check_repository_format_gently(nongit_ok
))
412 /* Make "offset" point to past the '/', and add a '/' at the end */
419 int git_config_perm(const char *var
, const char *value
)
427 if (!strcmp(value
, "umask"))
429 if (!strcmp(value
, "group"))
431 if (!strcmp(value
, "all") ||
432 !strcmp(value
, "world") ||
433 !strcmp(value
, "everybody"))
434 return PERM_EVERYBODY
;
436 /* Parse octal numbers */
437 i
= strtol(value
, &endptr
, 8);
439 /* If not an octal number, maybe true/false? */
441 return git_config_bool(var
, value
) ? PERM_GROUP
: PERM_UMASK
;
444 * Treat values 0, 1 and 2 as compatibility cases, otherwise it is
445 * a chmod value to restrict to.
448 case PERM_UMASK
: /* 0 */
450 case OLD_PERM_GROUP
: /* 1 */
452 case OLD_PERM_EVERYBODY
: /* 2 */
453 return PERM_EVERYBODY
;
456 /* A filemode value was given: 0xxx */
458 if ((i
& 0600) != 0600)
459 die("Problem with core.sharedRepository filemode value "
460 "(0%.3o).\nThe owner of files must always have "
461 "read and write permissions.", i
);
464 * Mask filemode value. Others can not get write permission.
465 * x flags for directories are handled separately.
470 int check_repository_format_version(const char *var
, const char *value
, void *cb
)
472 if (strcmp(var
, "core.repositoryformatversion") == 0)
473 repository_format_version
= git_config_int(var
, value
);
474 else if (strcmp(var
, "core.sharedrepository") == 0)
475 shared_repository
= git_config_perm(var
, value
);
476 else if (strcmp(var
, "core.bare") == 0) {
477 is_bare_repository_cfg
= git_config_bool(var
, value
);
478 if (is_bare_repository_cfg
== 1)
479 inside_work_tree
= -1;
480 } else if (strcmp(var
, "core.worktree") == 0) {
482 return config_error_nonbool(var
);
483 free(git_work_tree_cfg
);
484 git_work_tree_cfg
= xstrdup(value
);
485 inside_work_tree
= -1;
490 int check_repository_format(void)
492 return check_repository_format_gently(NULL
);
495 const char *setup_git_directory(void)
497 const char *retval
= setup_git_directory_gently(NULL
);
499 /* If the work tree is not the default one, recompute prefix */
500 if (inside_work_tree
< 0) {
501 static char buffer
[PATH_MAX
+ 1];
503 if (retval
&& chdir(retval
))
504 die_errno ("Could not jump back into original cwd");
505 rel
= get_relative_cwd(buffer
, PATH_MAX
, get_git_work_tree());
506 if (rel
&& *rel
&& chdir(get_git_work_tree()))
507 die_errno ("Could not jump to working directory");
508 return rel
&& *rel
? strcat(rel
, "/") : NULL
;