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 if (is_absolute_path(arg
))
95 memcpy(path
, pfx
, pfx_len
);
96 strcpy(path
+ pfx_len
, arg
);
97 for (p
= path
+ pfx_len
; *p
; p
++)
104 * Verify a filename that we got as an argument for a pathspec
105 * entry. Note that a filename that begins with "-" never verifies
106 * as true, because even if such a filename were to exist, we want
107 * it to be preceded by the "--" marker (or we want the user to
108 * use a format like "./-filename")
110 void verify_filename(const char *prefix
, const char *arg
)
116 die("bad flag '%s' used after filename", arg
);
117 name
= prefix
? prefix_filename(prefix
, strlen(prefix
), arg
) : arg
;
118 if (!lstat(name
, &st
))
121 die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
122 "Use '--' to separate paths from revisions", arg
);
123 die("'%s': %s", arg
, strerror(errno
));
127 * Opposite of the above: the command line did not have -- marker
128 * and we parsed the arg as a refname. It should not be interpretable
131 void verify_non_filename(const char *prefix
, const char *arg
)
136 if (!is_inside_work_tree() || is_inside_git_dir())
140 name
= prefix
? prefix_filename(prefix
, strlen(prefix
), arg
) : arg
;
141 if (!lstat(name
, &st
))
142 die("ambiguous argument '%s': both revision and filename\n"
143 "Use '--' to separate filenames from revisions", arg
);
144 if (errno
!= ENOENT
&& errno
!= ENOTDIR
)
145 die("'%s': %s", arg
, strerror(errno
));
148 const char **get_pathspec(const char *prefix
, const char **pathspec
)
150 const char *entry
= *pathspec
;
154 if (!prefix
&& !entry
)
158 static const char *spec
[2];
164 /* Otherwise we have to re-write the entries.. */
166 prefixlen
= prefix
? strlen(prefix
) : 0;
168 *p
= prefix_path(prefix
, prefixlen
, entry
);
169 } while ((entry
= *++p
) != NULL
);
170 return (const char **) pathspec
;
174 * Test if it looks like we're at a git directory.
177 * - either a objects/ directory _or_ the proper
178 * GIT_OBJECT_DIRECTORY environment variable
179 * - a refs/ directory
180 * - either a HEAD symlink or a HEAD file that is formatted as
181 * a proper "ref:", or a regular file HEAD that has a properly
182 * formatted sha1 object name.
184 static int is_git_directory(const char *suspect
)
187 size_t len
= strlen(suspect
);
189 strcpy(path
, suspect
);
190 if (getenv(DB_ENVIRONMENT
)) {
191 if (access(getenv(DB_ENVIRONMENT
), X_OK
))
195 strcpy(path
+ len
, "/objects");
196 if (access(path
, X_OK
))
200 strcpy(path
+ len
, "/refs");
201 if (access(path
, X_OK
))
204 strcpy(path
+ len
, "/HEAD");
205 if (validate_headref(path
))
211 int is_inside_git_dir(void)
213 if (inside_git_dir
< 0)
214 inside_git_dir
= is_inside_dir(get_git_dir());
215 return inside_git_dir
;
218 int is_inside_work_tree(void)
220 if (inside_work_tree
< 0)
221 inside_work_tree
= is_inside_dir(get_git_work_tree());
222 return inside_work_tree
;
226 * set_work_tree() is only ever called if you set GIT_DIR explicitely.
227 * The old behaviour (which we retain here) is to set the work tree root
228 * to the cwd, unless overridden by the config, the command line, or
231 static const char *set_work_tree(const char *dir
)
233 char buffer
[PATH_MAX
+ 1];
235 if (!getcwd(buffer
, sizeof(buffer
)))
236 die ("Could not get the current working directory");
237 git_work_tree_cfg
= xstrdup(buffer
);
238 inside_work_tree
= 1;
244 * We cannot decide in this function whether we are in the work tree or
245 * not, since the config can only be read _after_ this function was called.
247 const char *setup_git_directory_gently(int *nongit_ok
)
249 const char *work_tree_env
= getenv(GIT_WORK_TREE_ENVIRONMENT
);
250 static char cwd
[PATH_MAX
+1];
251 const char *gitdirenv
;
256 * If GIT_DIR is set explicitly, we're not going
257 * to do any discovery, but we still do repository
260 gitdirenv
= getenv(GIT_DIR_ENVIRONMENT
);
262 if (PATH_MAX
- 40 < strlen(gitdirenv
))
263 die("'$%s' too big", GIT_DIR_ENVIRONMENT
);
264 if (is_git_directory(gitdirenv
)) {
265 static char buffer
[1024 + 1];
269 return set_work_tree(gitdirenv
);
270 retval
= get_relative_cwd(buffer
, sizeof(buffer
) - 1,
271 get_git_work_tree());
272 if (!retval
|| !*retval
)
274 set_git_dir(make_absolute_path(gitdirenv
));
275 if (chdir(work_tree_env
) < 0)
276 die ("Could not chdir to %s", work_tree_env
);
284 die("Not a git repository: '%s'", gitdirenv
);
287 if (!getcwd(cwd
, sizeof(cwd
)-1))
288 die("Unable to read current working directory");
295 * Test in the following order (relative to the cwd):
303 offset
= len
= strlen(cwd
);
305 if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT
))
307 if (is_git_directory(".")) {
310 inside_work_tree
= 0;
311 setenv(GIT_DIR_ENVIRONMENT
, ".", 1);
316 if (offset
<= minoffset
) {
319 die("Cannot come back to cwd");
323 die("Not a git repository");
325 } while (offset
> minoffset
&& cwd
[--offset
] != '/');
330 inside_work_tree
= 1;
331 git_work_tree_cfg
= xstrndup(cwd
, offset
);
335 /* Make "offset" point to past the '/', and add a '/' at the end */
342 int git_config_perm(const char *var
, const char *value
)
346 if (!strcmp(value
, "umask"))
348 if (!strcmp(value
, "group"))
350 if (!strcmp(value
, "all") ||
351 !strcmp(value
, "world") ||
352 !strcmp(value
, "everybody"))
353 return PERM_EVERYBODY
;
358 return git_config_bool(var
, value
);
361 int check_repository_format_version(const char *var
, const char *value
)
363 if (strcmp(var
, "core.repositoryformatversion") == 0)
364 repository_format_version
= git_config_int(var
, value
);
365 else if (strcmp(var
, "core.sharedrepository") == 0)
366 shared_repository
= git_config_perm(var
, value
);
367 else if (strcmp(var
, "core.bare") == 0) {
368 is_bare_repository_cfg
= git_config_bool(var
, value
);
369 if (is_bare_repository_cfg
== 1)
370 inside_work_tree
= -1;
371 } else if (strcmp(var
, "core.worktree") == 0) {
372 if (git_work_tree_cfg
)
373 free(git_work_tree_cfg
);
374 git_work_tree_cfg
= xstrdup(value
);
375 inside_work_tree
= -1;
380 int check_repository_format(void)
382 git_config(check_repository_format_version
);
383 if (GIT_REPO_VERSION
< repository_format_version
)
384 die ("Expected git repo version <= %d, found %d",
385 GIT_REPO_VERSION
, repository_format_version
);
389 const char *setup_git_directory(void)
391 const char *retval
= setup_git_directory_gently(NULL
);
392 check_repository_format();
394 /* If the work tree is not the default one, recompute prefix */
395 if (inside_work_tree
< 0) {
396 static char buffer
[PATH_MAX
+ 1];
398 if (retval
&& chdir(retval
))
399 die ("Could not jump back into original cwd");
400 rel
= get_relative_cwd(buffer
, PATH_MAX
, get_git_work_tree());
401 return rel
&& *rel
? strcat(rel
, "/") : NULL
;