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 const char *prefix_path(const char *prefix
, int len
, const char *path
)
15 const char *orig
= path
;
37 else if (is_dir_sep(c
))
42 /* Remove last component of the prefix */
45 die("'%s' is outside repository", orig
);
47 } while (len
&& !is_dir_sep(prefix
[len
-1]));
52 /* we want to convert '\' in path to '/' (prefix already has '/') */
55 while (!do_pfx
&& *p
) {
56 do_pfx
= *p
++ == '\\';
61 int speclen
= strlen(path
);
62 char *n
= xmalloc(speclen
+ len
+ 1);
65 memcpy(n
, prefix
, len
);
66 memcpy(n
+ len
, path
, speclen
+1);
68 for (p
= n
+ len
; *p
; p
++)
78 * Unlike prefix_path, this should be used if the named file does
79 * not have to interact with index entry; i.e. name of a random file
82 const char *prefix_filename(const char *pfx
, int pfx_len
, const char *arg
)
84 static char path
[PATH_MAX
];
86 if (!pfx
|| !*pfx
|| is_absolute_path(arg
))
88 memcpy(path
, pfx
, pfx_len
);
89 strcpy(path
+ pfx_len
, arg
);
92 /* don't add prefix to absolute paths, but still replace '\' by '/' */
93 if (is_absolute_path(arg
))
96 memcpy(path
, pfx
, pfx_len
);
97 strcpy(path
+ pfx_len
, arg
);
98 for (p
= path
+ pfx_len
; *p
; p
++)
106 * Verify a filename that we got as an argument for a pathspec
107 * entry. Note that a filename that begins with "-" never verifies
108 * as true, because even if such a filename were to exist, we want
109 * it to be preceded by the "--" marker (or we want the user to
110 * use a format like "./-filename")
112 void verify_filename(const char *prefix
, const char *arg
)
118 die("bad flag '%s' used after filename", arg
);
119 name
= prefix
? prefix_filename(prefix
, strlen(prefix
), arg
) : arg
;
120 if (!lstat(name
, &st
))
123 die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
124 "Use '--' to separate paths from revisions", arg
);
125 die("'%s': %s", arg
, strerror(errno
));
129 * Opposite of the above: the command line did not have -- marker
130 * and we parsed the arg as a refname. It should not be interpretable
133 void verify_non_filename(const char *prefix
, const char *arg
)
138 if (!is_inside_work_tree() || is_inside_git_dir())
142 name
= prefix
? prefix_filename(prefix
, strlen(prefix
), arg
) : arg
;
143 if (!lstat(name
, &st
))
144 die("ambiguous argument '%s': both revision and filename\n"
145 "Use '--' to separate filenames from revisions", arg
);
146 if (errno
!= ENOENT
&& errno
!= ENOTDIR
)
147 die("'%s': %s", arg
, strerror(errno
));
150 const char **get_pathspec(const char *prefix
, const char **pathspec
)
152 const char *entry
= *pathspec
;
156 if (!prefix
&& !entry
)
160 static const char *spec
[2];
166 /* Otherwise we have to re-write the entries.. */
168 prefixlen
= prefix
? strlen(prefix
) : 0;
170 *p
= prefix_path(prefix
, prefixlen
, entry
);
171 } while ((entry
= *++p
) != NULL
);
172 return (const char **) pathspec
;
176 * Test if it looks like we're at a git directory.
179 * - either a objects/ directory _or_ the proper
180 * GIT_OBJECT_DIRECTORY environment variable
181 * - a refs/ directory
182 * - either a HEAD symlink or a HEAD file that is formatted as
183 * a proper "ref:", or a regular file HEAD that has a properly
184 * formatted sha1 object name.
186 static int is_git_directory(const char *suspect
)
189 size_t len
= strlen(suspect
);
191 strcpy(path
, suspect
);
192 if (getenv(DB_ENVIRONMENT
)) {
193 if (access(getenv(DB_ENVIRONMENT
), X_OK
))
197 strcpy(path
+ len
, "/objects");
198 if (access(path
, X_OK
))
202 strcpy(path
+ len
, "/refs");
203 if (access(path
, X_OK
))
206 strcpy(path
+ len
, "/HEAD");
207 if (validate_headref(path
))
213 int is_inside_git_dir(void)
215 if (inside_git_dir
< 0)
216 inside_git_dir
= is_inside_dir(get_git_dir());
217 return inside_git_dir
;
220 int is_inside_work_tree(void)
222 if (inside_work_tree
< 0)
223 inside_work_tree
= is_inside_dir(get_git_work_tree());
224 return inside_work_tree
;
228 * set_work_tree() is only ever called if you set GIT_DIR explicitely.
229 * The old behaviour (which we retain here) is to set the work tree root
230 * to the cwd, unless overridden by the config, the command line, or
233 static const char *set_work_tree(const char *dir
)
235 char buffer
[PATH_MAX
+ 1];
237 if (!getcwd(buffer
, sizeof(buffer
)))
238 die ("Could not get the current working directory");
239 git_work_tree_cfg
= xstrdup(buffer
);
240 inside_work_tree
= 1;
245 void setup_work_tree(void)
247 const char *work_tree
, *git_dir
;
248 static int initialized
= 0;
252 work_tree
= get_git_work_tree();
253 git_dir
= get_git_dir();
254 if (!is_absolute_path(git_dir
))
255 set_git_dir(make_absolute_path(git_dir
));
256 if (!work_tree
|| chdir(work_tree
))
257 die("This operation must be run in a work tree");
261 static int check_repository_format_gently(int *nongit_ok
)
263 git_config(check_repository_format_version
);
264 if (GIT_REPO_VERSION
< repository_format_version
) {
266 die ("Expected git repo version <= %d, found %d",
267 GIT_REPO_VERSION
, repository_format_version
);
268 warning("Expected git repo version <= %d, found %d",
269 GIT_REPO_VERSION
, repository_format_version
);
270 warning("Please upgrade Git");
278 * We cannot decide in this function whether we are in the work tree or
279 * not, since the config can only be read _after_ this function was called.
281 const char *setup_git_directory_gently(int *nongit_ok
)
283 const char *work_tree_env
= getenv(GIT_WORK_TREE_ENVIRONMENT
);
284 static char cwd
[PATH_MAX
+1];
285 const char *gitdirenv
;
290 * If GIT_DIR is set explicitly, we're not going
291 * to do any discovery, but we still do repository
294 gitdirenv
= getenv(GIT_DIR_ENVIRONMENT
);
296 if (PATH_MAX
- 40 < strlen(gitdirenv
))
297 die("'$%s' too big", GIT_DIR_ENVIRONMENT
);
298 if (is_git_directory(gitdirenv
)) {
299 static char buffer
[1024 + 1];
302 if (!work_tree_env
) {
303 retval
= set_work_tree(gitdirenv
);
304 /* config may override worktree */
305 if (check_repository_format_gently(nongit_ok
))
309 if (check_repository_format_gently(nongit_ok
))
311 retval
= get_relative_cwd(buffer
, sizeof(buffer
) - 1,
312 get_git_work_tree());
313 if (!retval
|| !*retval
)
315 set_git_dir(make_absolute_path(gitdirenv
));
316 if (chdir(work_tree_env
) < 0)
317 die ("Could not chdir to %s", work_tree_env
);
325 die("Not a git repository: '%s'", gitdirenv
);
328 if (!getcwd(cwd
, sizeof(cwd
)-1))
329 die("Unable to read current working directory");
336 * Test in the following order (relative to the cwd):
344 offset
= len
= strlen(cwd
);
346 if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT
))
348 if (is_git_directory(".")) {
351 inside_work_tree
= 0;
352 setenv(GIT_DIR_ENVIRONMENT
, ".", 1);
353 check_repository_format_gently(nongit_ok
);
358 if (offset
<= minoffset
) {
361 die("Cannot come back to cwd");
365 die("Not a git repository");
367 } while (offset
> minoffset
&& cwd
[--offset
] != '/');
372 inside_work_tree
= 1;
373 git_work_tree_cfg
= xstrndup(cwd
, offset
);
374 if (check_repository_format_gently(nongit_ok
))
379 /* Make "offset" point to past the '/', and add a '/' at the end */
386 int git_config_perm(const char *var
, const char *value
)
390 if (!strcmp(value
, "umask"))
392 if (!strcmp(value
, "group"))
394 if (!strcmp(value
, "all") ||
395 !strcmp(value
, "world") ||
396 !strcmp(value
, "everybody"))
397 return PERM_EVERYBODY
;
402 return git_config_bool(var
, value
);
405 int check_repository_format_version(const char *var
, const char *value
)
407 if (strcmp(var
, "core.repositoryformatversion") == 0)
408 repository_format_version
= git_config_int(var
, value
);
409 else if (strcmp(var
, "core.sharedrepository") == 0)
410 shared_repository
= git_config_perm(var
, value
);
411 else if (strcmp(var
, "core.bare") == 0) {
412 is_bare_repository_cfg
= git_config_bool(var
, value
);
413 if (is_bare_repository_cfg
== 1)
414 inside_work_tree
= -1;
415 } else if (strcmp(var
, "core.worktree") == 0) {
416 if (git_work_tree_cfg
)
417 free(git_work_tree_cfg
);
418 git_work_tree_cfg
= xstrdup(value
);
419 inside_work_tree
= -1;
424 int check_repository_format(void)
426 return check_repository_format_gently(NULL
);
429 const char *setup_git_directory(void)
431 const char *retval
= setup_git_directory_gently(NULL
);
433 /* If the work tree is not the default one, recompute prefix */
434 if (inside_work_tree
< 0) {
435 static char buffer
[PATH_MAX
+ 1];
437 if (retval
&& chdir(retval
))
438 die ("Could not jump back into original cwd");
439 rel
= get_relative_cwd(buffer
, PATH_MAX
, get_git_work_tree());
440 return rel
&& *rel
? strcat(rel
, "/") : NULL
;