4 static int inside_git_dir
= -1;
5 static int inside_work_tree
= -1;
8 static inline int is_dir_sep(char c
) { return c
== '/' || c
== '\\'; }
10 static inline int is_dir_sep(char c
) { return c
== '/'; }
13 static int sanitary_path_copy(char *dst
, const char *src
)
18 if (isalpha(*src
) && src
[1] == ':') {
24 if (is_dir_sep(*src
)) {
26 while (is_dir_sep(*src
))
34 * A path component that begins with . could be
36 * (1) "." and ends -- ignore and terminate.
37 * (2) "./" -- ignore them, eat slash and continue.
38 * (3) ".." and ends -- strip one and terminate.
39 * (4) "../" -- strip one, eat slash and continue.
53 while (is_dir_sep(*src
))
68 while (is_dir_sep(*src
))
75 /* copy up to the next '/', and eat all '/' */
76 while ((c
= *src
++) != '\0' && !is_dir_sep(c
))
89 * dst0..dst is prefix portion, and dst[-1] is '/';
92 dst
-= 2; /* go past trailing '/' if any */
99 if (c
== '/') { /* MinGW: cannot be '\\' anymore */
109 const char *prefix_path(const char *prefix
, int len
, const char *path
)
111 const char *orig
= path
;
112 char *sanitized
= xmalloc(len
+ strlen(path
) + 1);
113 if (is_absolute_path(orig
))
114 strcpy(sanitized
, path
);
117 memcpy(sanitized
, prefix
, len
);
118 strcpy(sanitized
+ len
, path
);
120 if (sanitary_path_copy(sanitized
, sanitized
))
122 if (is_absolute_path(orig
)) {
123 const char *work_tree
= get_git_work_tree();
124 size_t len
= strlen(work_tree
);
125 size_t total
= strlen(sanitized
) + 1;
126 if (strncmp(sanitized
, work_tree
, len
) ||
127 (sanitized
[len
] != '\0' && sanitized
[len
] != '/')) {
129 error("'%s' is outside repository", orig
);
133 if (sanitized
[len
] == '/')
135 memmove(sanitized
, sanitized
+ len
, total
- len
);
141 * Unlike prefix_path, this should be used if the named file does
142 * not have to interact with index entry; i.e. name of a random file
145 const char *prefix_filename(const char *pfx
, int pfx_len
, const char *arg
)
147 static char path
[PATH_MAX
];
149 if (!pfx
|| !*pfx
|| is_absolute_path(arg
))
151 memcpy(path
, pfx
, pfx_len
);
152 strcpy(path
+ pfx_len
, arg
);
155 /* don't add prefix to absolute paths, but still replace '\' by '/' */
156 if (is_absolute_path(arg
))
159 memcpy(path
, pfx
, pfx_len
);
160 strcpy(path
+ pfx_len
, arg
);
161 for (p
= path
+ pfx_len
; *p
; p
++)
169 * Verify a filename that we got as an argument for a pathspec
170 * entry. Note that a filename that begins with "-" never verifies
171 * as true, because even if such a filename were to exist, we want
172 * it to be preceded by the "--" marker (or we want the user to
173 * use a format like "./-filename")
175 void verify_filename(const char *prefix
, const char *arg
)
181 die("bad flag '%s' used after filename", arg
);
182 name
= prefix
? prefix_filename(prefix
, strlen(prefix
), arg
) : arg
;
183 if (!lstat(name
, &st
))
186 die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
187 "Use '--' to separate paths from revisions", arg
);
188 die("'%s': %s", arg
, strerror(errno
));
192 * Opposite of the above: the command line did not have -- marker
193 * and we parsed the arg as a refname. It should not be interpretable
196 void verify_non_filename(const char *prefix
, const char *arg
)
201 if (!is_inside_work_tree() || is_inside_git_dir())
205 name
= prefix
? prefix_filename(prefix
, strlen(prefix
), arg
) : arg
;
206 if (!lstat(name
, &st
))
207 die("ambiguous argument '%s': both revision and filename\n"
208 "Use '--' to separate filenames from revisions", arg
);
209 if (errno
!= ENOENT
&& errno
!= ENOTDIR
)
210 die("'%s': %s", arg
, strerror(errno
));
213 const char **get_pathspec(const char *prefix
, const char **pathspec
)
215 const char *entry
= *pathspec
;
216 const char **src
, **dst
;
219 if (!prefix
&& !entry
)
223 static const char *spec
[2];
229 /* Otherwise we have to re-write the entries.. */
232 prefixlen
= prefix
? strlen(prefix
) : 0;
234 const char *p
= prefix_path(prefix
, prefixlen
, *src
);
246 * Test if it looks like we're at a git directory.
249 * - either an objects/ directory _or_ the proper
250 * GIT_OBJECT_DIRECTORY environment variable
251 * - a refs/ directory
252 * - either a HEAD symlink or a HEAD file that is formatted as
253 * a proper "ref:", or a regular file HEAD that has a properly
254 * formatted sha1 object name.
256 static int is_git_directory(const char *suspect
)
259 size_t len
= strlen(suspect
);
261 strcpy(path
, suspect
);
262 if (getenv(DB_ENVIRONMENT
)) {
263 if (access(getenv(DB_ENVIRONMENT
), X_OK
))
267 strcpy(path
+ len
, "/objects");
268 if (access(path
, X_OK
))
272 strcpy(path
+ len
, "/refs");
273 if (access(path
, X_OK
))
276 strcpy(path
+ len
, "/HEAD");
277 if (validate_headref(path
))
283 int is_inside_git_dir(void)
285 if (inside_git_dir
< 0)
286 inside_git_dir
= is_inside_dir(get_git_dir());
287 return inside_git_dir
;
290 int is_inside_work_tree(void)
292 if (inside_work_tree
< 0)
293 inside_work_tree
= is_inside_dir(get_git_work_tree());
294 return inside_work_tree
;
298 * set_work_tree() is only ever called if you set GIT_DIR explicitely.
299 * The old behaviour (which we retain here) is to set the work tree root
300 * to the cwd, unless overridden by the config, the command line, or
303 static const char *set_work_tree(const char *dir
)
305 char buffer
[PATH_MAX
+ 1];
307 if (!getcwd(buffer
, sizeof(buffer
)))
308 die ("Could not get the current working directory");
309 git_work_tree_cfg
= xstrdup(buffer
);
310 inside_work_tree
= 1;
315 void setup_work_tree(void)
317 const char *work_tree
, *git_dir
;
318 static int initialized
= 0;
322 work_tree
= get_git_work_tree();
323 git_dir
= get_git_dir();
324 if (!is_absolute_path(git_dir
))
325 set_git_dir(make_absolute_path(git_dir
));
326 if (!work_tree
|| chdir(work_tree
))
327 die("This operation must be run in a work tree");
331 static int check_repository_format_gently(int *nongit_ok
)
333 git_config(check_repository_format_version
);
334 if (GIT_REPO_VERSION
< repository_format_version
) {
336 die ("Expected git repo version <= %d, found %d",
337 GIT_REPO_VERSION
, repository_format_version
);
338 warning("Expected git repo version <= %d, found %d",
339 GIT_REPO_VERSION
, repository_format_version
);
340 warning("Please upgrade Git");
348 * We cannot decide in this function whether we are in the work tree or
349 * not, since the config can only be read _after_ this function was called.
351 const char *setup_git_directory_gently(int *nongit_ok
)
353 const char *work_tree_env
= getenv(GIT_WORK_TREE_ENVIRONMENT
);
354 static char cwd
[PATH_MAX
+1];
355 const char *gitdirenv
;
360 * If GIT_DIR is set explicitly, we're not going
361 * to do any discovery, but we still do repository
364 gitdirenv
= getenv(GIT_DIR_ENVIRONMENT
);
366 if (PATH_MAX
- 40 < strlen(gitdirenv
))
367 die("'$%s' too big", GIT_DIR_ENVIRONMENT
);
368 if (is_git_directory(gitdirenv
)) {
369 static char buffer
[1024 + 1];
372 if (!work_tree_env
) {
373 retval
= set_work_tree(gitdirenv
);
374 /* config may override worktree */
375 if (check_repository_format_gently(nongit_ok
))
379 if (check_repository_format_gently(nongit_ok
))
381 retval
= get_relative_cwd(buffer
, sizeof(buffer
) - 1,
382 get_git_work_tree());
383 if (!retval
|| !*retval
)
385 set_git_dir(make_absolute_path(gitdirenv
));
386 if (chdir(work_tree_env
) < 0)
387 die ("Could not chdir to %s", work_tree_env
);
395 die("Not a git repository: '%s'", gitdirenv
);
398 if (!getcwd(cwd
, sizeof(cwd
)-1))
399 die("Unable to read current working directory");
400 if (has_dos_drive_prefix(cwd
))
404 * Test in the following order (relative to the cwd):
412 offset
= len
= strlen(cwd
);
414 if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT
))
416 if (is_git_directory(".")) {
419 inside_work_tree
= 0;
420 setenv(GIT_DIR_ENVIRONMENT
, ".", 1);
421 check_repository_format_gently(nongit_ok
);
426 if (offset
<= minoffset
) {
429 die("Cannot come back to cwd");
433 die("Not a git repository");
435 } while (offset
> minoffset
&& cwd
[--offset
] != '/');
440 inside_work_tree
= 1;
441 git_work_tree_cfg
= xstrndup(cwd
, offset
);
442 if (check_repository_format_gently(nongit_ok
))
447 /* Make "offset" point to past the '/', and add a '/' at the end */
454 int git_config_perm(const char *var
, const char *value
)
458 if (!strcmp(value
, "umask"))
460 if (!strcmp(value
, "group"))
462 if (!strcmp(value
, "all") ||
463 !strcmp(value
, "world") ||
464 !strcmp(value
, "everybody"))
465 return PERM_EVERYBODY
;
470 return git_config_bool(var
, value
);
473 int check_repository_format_version(const char *var
, const char *value
)
475 if (strcmp(var
, "core.repositoryformatversion") == 0)
476 repository_format_version
= git_config_int(var
, value
);
477 else if (strcmp(var
, "core.sharedrepository") == 0)
478 shared_repository
= git_config_perm(var
, value
);
479 else if (strcmp(var
, "core.bare") == 0) {
480 is_bare_repository_cfg
= git_config_bool(var
, value
);
481 if (is_bare_repository_cfg
== 1)
482 inside_work_tree
= -1;
483 } else if (strcmp(var
, "core.worktree") == 0) {
485 return config_error_nonbool(var
);
486 free(git_work_tree_cfg
);
487 git_work_tree_cfg
= xstrdup(value
);
488 inside_work_tree
= -1;
493 int check_repository_format(void)
495 return check_repository_format_gently(NULL
);
498 const char *setup_git_directory(void)
500 const char *retval
= setup_git_directory_gently(NULL
);
502 /* If the work tree is not the default one, recompute prefix */
503 if (inside_work_tree
< 0) {
504 static char buffer
[PATH_MAX
+ 1];
506 if (retval
&& chdir(retval
))
507 die ("Could not jump back into original cwd");
508 rel
= get_relative_cwd(buffer
, PATH_MAX
, get_git_work_tree());
509 return rel
&& *rel
? strcat(rel
, "/") : NULL
;