4 static int inside_git_dir
= -1;
5 static int inside_work_tree
= -1;
7 static int sanitary_path_copy(char *dst
, const char *src
)
21 * A path component that begins with . could be
23 * (1) "." and ends -- ignore and terminate.
24 * (2) "./" -- ignore them, eat slash and continue.
25 * (3) ".." and ends -- strip one and terminate.
26 * (4) "../" -- strip one, eat slash and continue.
56 /* copy up to the next '/', and eat all '/' */
57 while ((c
= *src
++) != '\0' && c
!= '/')
70 * dst0..dst is prefix portion, and dst[-1] is '/';
73 dst
-= 2; /* go past trailing '/' if any */
90 const char *prefix_path(const char *prefix
, int len
, const char *path
)
92 const char *orig
= path
;
93 char *sanitized
= xmalloc(len
+ strlen(path
) + 1);
94 if (is_absolute_path(orig
))
95 strcpy(sanitized
, path
);
98 memcpy(sanitized
, prefix
, len
);
99 strcpy(sanitized
+ len
, path
);
101 if (sanitary_path_copy(sanitized
, sanitized
))
103 if (is_absolute_path(orig
)) {
104 const char *work_tree
= get_git_work_tree();
105 size_t len
= strlen(work_tree
);
106 size_t total
= strlen(sanitized
) + 1;
107 if (strncmp(sanitized
, work_tree
, len
) ||
108 (sanitized
[len
] != '\0' && sanitized
[len
] != '/')) {
110 error("'%s' is outside repository", orig
);
114 if (sanitized
[len
] == '/')
116 memmove(sanitized
, sanitized
+ len
, total
- len
);
122 * Unlike prefix_path, this should be used if the named file does
123 * not have to interact with index entry; i.e. name of a random file
126 const char *prefix_filename(const char *pfx
, int pfx_len
, const char *arg
)
128 static char path
[PATH_MAX
];
129 if (!pfx
|| !*pfx
|| is_absolute_path(arg
))
131 memcpy(path
, pfx
, pfx_len
);
132 strcpy(path
+ pfx_len
, arg
);
137 * Verify a filename that we got as an argument for a pathspec
138 * entry. Note that a filename that begins with "-" never verifies
139 * as true, because even if such a filename were to exist, we want
140 * it to be preceded by the "--" marker (or we want the user to
141 * use a format like "./-filename")
143 void verify_filename(const char *prefix
, const char *arg
)
149 die("bad flag '%s' used after filename", arg
);
150 name
= prefix
? prefix_filename(prefix
, strlen(prefix
), arg
) : arg
;
151 if (!lstat(name
, &st
))
154 die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
155 "Use '--' to separate paths from revisions", arg
);
156 die("'%s': %s", arg
, strerror(errno
));
160 * Opposite of the above: the command line did not have -- marker
161 * and we parsed the arg as a refname. It should not be interpretable
164 void verify_non_filename(const char *prefix
, const char *arg
)
169 if (!is_inside_work_tree() || is_inside_git_dir())
173 name
= prefix
? prefix_filename(prefix
, strlen(prefix
), arg
) : arg
;
174 if (!lstat(name
, &st
))
175 die("ambiguous argument '%s': both revision and filename\n"
176 "Use '--' to separate filenames from revisions", arg
);
177 if (errno
!= ENOENT
&& errno
!= ENOTDIR
)
178 die("'%s': %s", arg
, strerror(errno
));
181 const char **get_pathspec(const char *prefix
, const char **pathspec
)
183 const char *entry
= *pathspec
;
184 const char **src
, **dst
;
187 if (!prefix
&& !entry
)
191 static const char *spec
[2];
197 /* Otherwise we have to re-write the entries.. */
200 prefixlen
= prefix
? strlen(prefix
) : 0;
202 const char *p
= prefix_path(prefix
, prefixlen
, *src
);
214 * Test if it looks like we're at a git directory.
217 * - either an objects/ directory _or_ the proper
218 * GIT_OBJECT_DIRECTORY environment variable
219 * - a refs/ directory
220 * - either a HEAD symlink or a HEAD file that is formatted as
221 * a proper "ref:", or a regular file HEAD that has a properly
222 * formatted sha1 object name.
224 static int is_git_directory(const char *suspect
)
227 size_t len
= strlen(suspect
);
229 strcpy(path
, suspect
);
230 if (getenv(DB_ENVIRONMENT
)) {
231 if (access(getenv(DB_ENVIRONMENT
), X_OK
))
235 strcpy(path
+ len
, "/objects");
236 if (access(path
, X_OK
))
240 strcpy(path
+ len
, "/refs");
241 if (access(path
, X_OK
))
244 strcpy(path
+ len
, "/HEAD");
245 if (validate_headref(path
))
251 int is_inside_git_dir(void)
253 if (inside_git_dir
< 0)
254 inside_git_dir
= is_inside_dir(get_git_dir());
255 return inside_git_dir
;
258 int is_inside_work_tree(void)
260 if (inside_work_tree
< 0)
261 inside_work_tree
= is_inside_dir(get_git_work_tree());
262 return inside_work_tree
;
266 * set_work_tree() is only ever called if you set GIT_DIR explicitely.
267 * The old behaviour (which we retain here) is to set the work tree root
268 * to the cwd, unless overridden by the config, the command line, or
271 static const char *set_work_tree(const char *dir
)
273 char buffer
[PATH_MAX
+ 1];
275 if (!getcwd(buffer
, sizeof(buffer
)))
276 die ("Could not get the current working directory");
277 git_work_tree_cfg
= xstrdup(buffer
);
278 inside_work_tree
= 1;
283 void setup_work_tree(void)
285 const char *work_tree
, *git_dir
;
286 static int initialized
= 0;
290 work_tree
= get_git_work_tree();
291 git_dir
= get_git_dir();
292 if (!is_absolute_path(git_dir
))
293 set_git_dir(make_absolute_path(git_dir
));
294 if (!work_tree
|| chdir(work_tree
))
295 die("This operation must be run in a work tree");
299 static int check_repository_format_gently(int *nongit_ok
)
301 git_config(check_repository_format_version
);
302 if (GIT_REPO_VERSION
< repository_format_version
) {
304 die ("Expected git repo version <= %d, found %d",
305 GIT_REPO_VERSION
, repository_format_version
);
306 warning("Expected git repo version <= %d, found %d",
307 GIT_REPO_VERSION
, repository_format_version
);
308 warning("Please upgrade Git");
316 * We cannot decide in this function whether we are in the work tree or
317 * not, since the config can only be read _after_ this function was called.
319 const char *setup_git_directory_gently(int *nongit_ok
)
321 const char *work_tree_env
= getenv(GIT_WORK_TREE_ENVIRONMENT
);
322 static char cwd
[PATH_MAX
+1];
323 const char *gitdirenv
;
327 * If GIT_DIR is set explicitly, we're not going
328 * to do any discovery, but we still do repository
331 gitdirenv
= getenv(GIT_DIR_ENVIRONMENT
);
333 if (PATH_MAX
- 40 < strlen(gitdirenv
))
334 die("'$%s' too big", GIT_DIR_ENVIRONMENT
);
335 if (is_git_directory(gitdirenv
)) {
336 static char buffer
[1024 + 1];
339 if (!work_tree_env
) {
340 retval
= set_work_tree(gitdirenv
);
341 /* config may override worktree */
342 if (check_repository_format_gently(nongit_ok
))
346 if (check_repository_format_gently(nongit_ok
))
348 retval
= get_relative_cwd(buffer
, sizeof(buffer
) - 1,
349 get_git_work_tree());
350 if (!retval
|| !*retval
)
352 set_git_dir(make_absolute_path(gitdirenv
));
353 if (chdir(work_tree_env
) < 0)
354 die ("Could not chdir to %s", work_tree_env
);
362 die("Not a git repository: '%s'", gitdirenv
);
365 if (!getcwd(cwd
, sizeof(cwd
)-1))
366 die("Unable to read current working directory");
369 * Test in the following order (relative to the cwd):
377 offset
= len
= strlen(cwd
);
379 if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT
))
381 if (is_git_directory(".")) {
384 inside_work_tree
= 0;
385 setenv(GIT_DIR_ENVIRONMENT
, ".", 1);
386 check_repository_format_gently(nongit_ok
);
394 die("Cannot come back to cwd");
398 die("Not a git repository");
400 } while (cwd
[--offset
] != '/');
405 inside_work_tree
= 1;
406 git_work_tree_cfg
= xstrndup(cwd
, offset
);
407 if (check_repository_format_gently(nongit_ok
))
412 /* Make "offset" point to past the '/', and add a '/' at the end */
419 int git_config_perm(const char *var
, const char *value
)
423 if (!strcmp(value
, "umask"))
425 if (!strcmp(value
, "group"))
427 if (!strcmp(value
, "all") ||
428 !strcmp(value
, "world") ||
429 !strcmp(value
, "everybody"))
430 return PERM_EVERYBODY
;
435 return git_config_bool(var
, value
);
438 int check_repository_format_version(const char *var
, const char *value
)
440 if (strcmp(var
, "core.repositoryformatversion") == 0)
441 repository_format_version
= git_config_int(var
, value
);
442 else if (strcmp(var
, "core.sharedrepository") == 0)
443 shared_repository
= git_config_perm(var
, value
);
444 else if (strcmp(var
, "core.bare") == 0) {
445 is_bare_repository_cfg
= git_config_bool(var
, value
);
446 if (is_bare_repository_cfg
== 1)
447 inside_work_tree
= -1;
448 } else if (strcmp(var
, "core.worktree") == 0) {
450 return config_error_nonbool(var
);
451 free(git_work_tree_cfg
);
452 git_work_tree_cfg
= xstrdup(value
);
453 inside_work_tree
= -1;
458 int check_repository_format(void)
460 return check_repository_format_gently(NULL
);
463 const char *setup_git_directory(void)
465 const char *retval
= setup_git_directory_gently(NULL
);
467 /* If the work tree is not the default one, recompute prefix */
468 if (inside_work_tree
< 0) {
469 static char buffer
[PATH_MAX
+ 1];
471 if (retval
&& chdir(retval
))
472 die ("Could not jump back into original cwd");
473 rel
= get_relative_cwd(buffer
, PATH_MAX
, get_git_work_tree());
474 return rel
&& *rel
? strcat(rel
, "/") : NULL
;