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
];
87 if (!pfx
|| !*pfx
|| arg
[0] == '/')
90 /* don't add prefix to absolute paths */
91 const int is_absolute
=
93 (arg
[0] && arg
[1] == ':' && is_dir_sep(arg
[2]));
98 memcpy(path
, pfx
, pfx_len
);
99 strcpy(path
+ pfx_len
, arg
);
100 for (p
= path
+ pfx_len
; *p
; p
++)
107 * Verify a filename that we got as an argument for a pathspec
108 * entry. Note that a filename that begins with "-" never verifies
109 * as true, because even if such a filename were to exist, we want
110 * it to be preceded by the "--" marker (or we want the user to
111 * use a format like "./-filename")
113 void verify_filename(const char *prefix
, const char *arg
)
119 die("bad flag '%s' used after filename", arg
);
120 name
= prefix
? prefix_filename(prefix
, strlen(prefix
), arg
) : arg
;
121 if (!lstat(name
, &st
))
124 die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
125 "Use '--' to separate paths from revisions", arg
);
126 die("'%s': %s", arg
, strerror(errno
));
130 * Opposite of the above: the command line did not have -- marker
131 * and we parsed the arg as a refname. It should not be interpretable
134 void verify_non_filename(const char *prefix
, const char *arg
)
139 if (!is_inside_work_tree() || is_inside_git_dir())
143 name
= prefix
? prefix_filename(prefix
, strlen(prefix
), arg
) : arg
;
144 if (!lstat(name
, &st
))
145 die("ambiguous argument '%s': both revision and filename\n"
146 "Use '--' to separate filenames from revisions", arg
);
147 if (errno
!= ENOENT
&& errno
!= ENOTDIR
)
148 die("'%s': %s", arg
, strerror(errno
));
151 const char **get_pathspec(const char *prefix
, const char **pathspec
)
153 const char *entry
= *pathspec
;
157 if (!prefix
&& !entry
)
161 static const char *spec
[2];
167 /* Otherwise we have to re-write the entries.. */
169 prefixlen
= prefix
? strlen(prefix
) : 0;
171 *p
= prefix_path(prefix
, prefixlen
, entry
);
172 } while ((entry
= *++p
) != NULL
);
173 return (const char **) pathspec
;
177 * Test if it looks like we're at a git directory.
180 * - either a objects/ directory _or_ the proper
181 * GIT_OBJECT_DIRECTORY environment variable
182 * - a refs/ directory
183 * - either a HEAD symlink or a HEAD file that is formatted as
184 * a proper "ref:", or a regular file HEAD that has a properly
185 * formatted sha1 object name.
187 static int is_git_directory(const char *suspect
)
190 size_t len
= strlen(suspect
);
192 strcpy(path
, suspect
);
193 if (getenv(DB_ENVIRONMENT
)) {
194 if (access(getenv(DB_ENVIRONMENT
), X_OK
))
198 strcpy(path
+ len
, "/objects");
199 if (access(path
, X_OK
))
203 strcpy(path
+ len
, "/refs");
204 if (access(path
, X_OK
))
207 strcpy(path
+ len
, "/HEAD");
208 if (validate_headref(path
))
214 int is_inside_git_dir(void)
216 if (inside_git_dir
< 0)
217 inside_git_dir
= is_inside_dir(get_git_dir());
218 return inside_git_dir
;
221 int is_inside_work_tree(void)
223 if (inside_work_tree
< 0)
224 inside_work_tree
= is_inside_dir(get_git_work_tree());
225 return inside_work_tree
;
229 * set_work_tree() is only ever called if you set GIT_DIR explicitely.
230 * The old behaviour (which we retain here) is to set the work tree root
231 * to the cwd, unless overridden by the config, the command line, or
234 static const char *set_work_tree(const char *dir
)
236 char buffer
[PATH_MAX
+ 1];
238 if (!getcwd(buffer
, sizeof(buffer
)))
239 die ("Could not get the current working directory");
240 git_work_tree_cfg
= xstrdup(buffer
);
241 inside_work_tree
= 1;
247 * We cannot decide in this function whether we are in the work tree or
248 * not, since the config can only be read _after_ this function was called.
250 const char *setup_git_directory_gently(int *nongit_ok
)
252 const char *work_tree_env
= getenv(GIT_WORK_TREE_ENVIRONMENT
);
253 static char cwd
[PATH_MAX
+1];
254 const char *gitdirenv
;
255 int len
, offset
, minoffset
= 0;
258 * If GIT_DIR is set explicitly, we're not going
259 * to do any discovery, but we still do repository
262 gitdirenv
= getenv(GIT_DIR_ENVIRONMENT
);
264 if (PATH_MAX
- 40 < strlen(gitdirenv
))
265 die("'$%s' too big", GIT_DIR_ENVIRONMENT
);
266 if (is_git_directory(gitdirenv
)) {
268 return set_work_tree(gitdirenv
);
275 die("Not a git repository: '%s'", gitdirenv
);
278 if (!getcwd(cwd
, sizeof(cwd
)-1))
279 die("Unable to read current working directory");
287 * Test in the following order (relative to the cwd):
295 offset
= len
= strlen(cwd
);
297 if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT
))
299 if (is_git_directory(".")) {
302 inside_work_tree
= 0;
308 if (offset
== minoffset
) {
311 die("Cannot come back to cwd");
315 die("Not a git repository");
317 } while (cwd
[--offset
] != '/');
322 inside_work_tree
= 1;
323 git_work_tree_cfg
= xstrndup(cwd
, offset
);
327 /* Make "offset" point to past the '/', and add a '/' at the end */
334 int git_config_perm(const char *var
, const char *value
)
338 if (!strcmp(value
, "umask"))
340 if (!strcmp(value
, "group"))
342 if (!strcmp(value
, "all") ||
343 !strcmp(value
, "world") ||
344 !strcmp(value
, "everybody"))
345 return PERM_EVERYBODY
;
350 return git_config_bool(var
, value
);
353 int check_repository_format_version(const char *var
, const char *value
)
355 if (strcmp(var
, "core.repositoryformatversion") == 0)
356 repository_format_version
= git_config_int(var
, value
);
357 else if (strcmp(var
, "core.sharedrepository") == 0)
358 shared_repository
= git_config_perm(var
, value
);
359 else if (strcmp(var
, "core.bare") == 0) {
360 is_bare_repository_cfg
= git_config_bool(var
, value
);
361 if (is_bare_repository_cfg
== 1)
362 inside_work_tree
= -1;
363 } else if (strcmp(var
, "core.worktree") == 0) {
364 if (git_work_tree_cfg
)
365 free(git_work_tree_cfg
);
366 git_work_tree_cfg
= xstrdup(value
);
367 inside_work_tree
= -1;
372 int check_repository_format(void)
374 git_config(check_repository_format_version
);
375 if (GIT_REPO_VERSION
< repository_format_version
)
376 die ("Expected git repo version <= %d, found %d",
377 GIT_REPO_VERSION
, repository_format_version
);
381 const char *setup_git_directory(void)
383 const char *retval
= setup_git_directory_gently(NULL
);
384 check_repository_format();
386 /* If the work tree is not the default one, recompute prefix */
387 if (inside_work_tree
< 0) {
388 static char buffer
[PATH_MAX
+ 1];
390 if (retval
&& chdir(retval
))
391 die ("Could not jump back into original cwd");
392 rel
= get_relative_cwd(buffer
, PATH_MAX
, get_git_work_tree());
393 return rel
&& *rel
? strcat(rel
, "/") : NULL
;