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 * If no worktree was given, and we are outside of a default work tree,
230 * now is the time to set it.
232 * In other words, if the user calls git with something like
234 * git --git-dir=/some/where/else/.git bla
236 * default to /some/where/else as working directory; if the specified
237 * git-dir does not end in "/.git", the cwd is used as working directory.
239 const char *set_work_tree(const char *dir
)
241 char dir_buffer
[PATH_MAX
], *rel
= NULL
;
242 static char buffer
[PATH_MAX
+ 1];
243 int len
, suffix_len
= strlen(DEFAULT_GIT_DIR_ENVIRONMENT
) + 1;
245 /* strip the variable 'dir' of the postfix "/.git" if it has it */
247 if (len
> suffix_len
&&
248 !strcmp(dir
+ len
- suffix_len
, "/" DEFAULT_GIT_DIR_ENVIRONMENT
)) {
249 if ((len
- suffix_len
) >= sizeof(dir_buffer
))
250 die("directory name too long");
251 memcpy(dir_buffer
, dir
, len
- suffix_len
);
252 dir_buffer
[len
- suffix_len
] = '\0';
254 /* are we inside the default work tree? */
255 rel
= get_relative_cwd(buffer
, sizeof(buffer
), dir_buffer
);
258 /* if rel is set, the cwd is _not_ the current working tree */
260 if (!is_absolute_path(dir
))
261 set_git_dir(make_absolute_path(dir
));
264 die("cannot chdir to %s: %s", dir
, strerror(errno
));
270 dir
= getcwd(buffer
, sizeof(buffer
));
272 git_work_tree_cfg
= xstrdup(dir
);
273 inside_work_tree
= 1;
279 * We cannot decide in this function whether we are in the work tree or
280 * not, since the config can only be read _after_ this function was called.
282 const char *setup_git_directory_gently(int *nongit_ok
)
284 const char *work_tree_env
= getenv(GIT_WORK_TREE_ENVIRONMENT
);
285 static char cwd
[PATH_MAX
+1];
286 const char *gitdirenv
;
287 int len
, offset
, minoffset
= 0;
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
)) {
300 return set_work_tree(gitdirenv
);
307 die("Not a git repository: '%s'", gitdirenv
);
310 if (!getcwd(cwd
, sizeof(cwd
)-1))
311 die("Unable to read current working directory");
319 * Test in the following order (relative to the cwd):
327 offset
= len
= strlen(cwd
);
329 if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT
))
331 if (is_git_directory(".")) {
334 inside_work_tree
= 0;
340 if (offset
== minoffset
) {
343 die("Cannot come back to cwd");
347 die("Not a git repository");
349 } while (cwd
[--offset
] != '/');
354 inside_work_tree
= 1;
355 git_work_tree_cfg
= xstrndup(cwd
, offset
);
359 /* Make "offset" point to past the '/', and add a '/' at the end */
366 int git_config_perm(const char *var
, const char *value
)
370 if (!strcmp(value
, "umask"))
372 if (!strcmp(value
, "group"))
374 if (!strcmp(value
, "all") ||
375 !strcmp(value
, "world") ||
376 !strcmp(value
, "everybody"))
377 return PERM_EVERYBODY
;
382 return git_config_bool(var
, value
);
385 int check_repository_format_version(const char *var
, const char *value
)
387 if (strcmp(var
, "core.repositoryformatversion") == 0)
388 repository_format_version
= git_config_int(var
, value
);
389 else if (strcmp(var
, "core.sharedrepository") == 0)
390 shared_repository
= git_config_perm(var
, value
);
391 else if (strcmp(var
, "core.bare") == 0) {
392 is_bare_repository_cfg
= git_config_bool(var
, value
);
393 if (is_bare_repository_cfg
== 1)
394 inside_work_tree
= -1;
395 } else if (strcmp(var
, "core.worktree") == 0) {
396 if (git_work_tree_cfg
)
397 free(git_work_tree_cfg
);
398 git_work_tree_cfg
= xstrdup(value
);
399 inside_work_tree
= -1;
404 int check_repository_format(void)
406 git_config(check_repository_format_version
);
407 if (GIT_REPO_VERSION
< repository_format_version
)
408 die ("Expected git repo version <= %d, found %d",
409 GIT_REPO_VERSION
, repository_format_version
);
413 const char *setup_git_directory(void)
415 const char *retval
= setup_git_directory_gently(NULL
);
416 check_repository_format();
418 /* If the work tree is not the default one, recompute prefix */
419 if (inside_work_tree
< 0) {
420 static char buffer
[PATH_MAX
+ 1];
422 if (retval
&& chdir(retval
))
423 die ("Could not jump back into original cwd");
424 rel
= get_relative_cwd(buffer
, PATH_MAX
, get_git_work_tree());
425 return rel
&& *rel
? strcat(rel
, "/") : NULL
;