MSVC: Fix an "incompatible pointer types" compiler warning
[git/ydirson.git] / setup.c
blob3a07aa4df710f5d9584d4ddf412e3debc0e64db4
1 #include "cache.h"
2 #include "dir.h"
4 static int inside_git_dir = -1;
5 static int inside_work_tree = -1;
7 const char *prefix_path(const char *prefix, int len, const char *path)
9 const char *orig = path;
10 char *sanitized = xmalloc(len + strlen(path) + 1);
11 if (is_absolute_path(orig))
12 strcpy(sanitized, path);
13 else {
14 if (len)
15 memcpy(sanitized, prefix, len);
16 strcpy(sanitized + len, path);
18 if (normalize_path_copy(sanitized, sanitized))
19 goto error_out;
20 if (is_absolute_path(orig)) {
21 size_t len, total;
22 const char *work_tree = get_git_work_tree();
23 if (!work_tree)
24 goto error_out;
25 len = strlen(work_tree);
26 total = strlen(sanitized) + 1;
27 if (strncmp(sanitized, work_tree, len) ||
28 (sanitized[len] != '\0' && sanitized[len] != '/')) {
29 error_out:
30 die("'%s' is outside repository", orig);
32 if (sanitized[len] == '/')
33 len++;
34 memmove(sanitized, sanitized + len, total - len);
36 return sanitized;
40 * Unlike prefix_path, this should be used if the named file does
41 * not have to interact with index entry; i.e. name of a random file
42 * on the filesystem.
44 const char *prefix_filename(const char *pfx, int pfx_len, const char *arg)
46 static char path[PATH_MAX];
47 #ifndef WIN32
48 if (!pfx || !*pfx || is_absolute_path(arg))
49 return arg;
50 memcpy(path, pfx, pfx_len);
51 strcpy(path + pfx_len, arg);
52 #else
53 char *p;
54 /* don't add prefix to absolute paths, but still replace '\' by '/' */
55 if (is_absolute_path(arg))
56 pfx_len = 0;
57 else
58 memcpy(path, pfx, pfx_len);
59 strcpy(path + pfx_len, arg);
60 for (p = path + pfx_len; *p; p++)
61 if (*p == '\\')
62 *p = '/';
63 #endif
64 return path;
67 int check_filename(const char *prefix, const char *arg)
69 const char *name;
70 struct stat st;
72 name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg;
73 if (!lstat(name, &st))
74 return 1; /* file exists */
75 if (errno == ENOENT || errno == ENOTDIR)
76 return 0; /* file does not exist */
77 die_errno("failed to stat '%s'", arg);
80 static void NORETURN die_verify_filename(const char *prefix, const char *arg)
82 unsigned char sha1[20];
83 unsigned mode;
84 /* try a detailed diagnostic ... */
85 get_sha1_with_mode_1(arg, sha1, &mode, 0, prefix);
86 /* ... or fall back the most general message. */
87 die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
88 "Use '--' to separate paths from revisions", arg);
93 * Verify a filename that we got as an argument for a pathspec
94 * entry. Note that a filename that begins with "-" never verifies
95 * as true, because even if such a filename were to exist, we want
96 * it to be preceded by the "--" marker (or we want the user to
97 * use a format like "./-filename")
99 void verify_filename(const char *prefix, const char *arg)
101 if (*arg == '-')
102 die("bad flag '%s' used after filename", arg);
103 if (check_filename(prefix, arg))
104 return;
105 die_verify_filename(prefix, arg);
109 * Opposite of the above: the command line did not have -- marker
110 * and we parsed the arg as a refname. It should not be interpretable
111 * as a filename.
113 void verify_non_filename(const char *prefix, const char *arg)
115 if (!is_inside_work_tree() || is_inside_git_dir())
116 return;
117 if (*arg == '-')
118 return; /* flag */
119 if (!check_filename(prefix, arg))
120 return;
121 die("ambiguous argument '%s': both revision and filename\n"
122 "Use '--' to separate filenames from revisions", arg);
125 const char **get_pathspec(const char *prefix, const char **pathspec)
127 const char *entry = *pathspec;
128 const char **src, **dst;
129 int prefixlen;
131 if (!prefix && !entry)
132 return NULL;
134 if (!entry) {
135 static const char *spec[2];
136 spec[0] = prefix;
137 spec[1] = NULL;
138 return spec;
141 /* Otherwise we have to re-write the entries.. */
142 src = pathspec;
143 dst = pathspec;
144 prefixlen = prefix ? strlen(prefix) : 0;
145 while (*src) {
146 const char *p = prefix_path(prefix, prefixlen, *src);
147 *(dst++) = p;
148 src++;
150 *dst = NULL;
151 if (!*pathspec)
152 return NULL;
153 return pathspec;
157 * Test if it looks like we're at a git directory.
158 * We want to see:
160 * - either an objects/ directory _or_ the proper
161 * GIT_OBJECT_DIRECTORY environment variable
162 * - a refs/ directory
163 * - either a HEAD symlink or a HEAD file that is formatted as
164 * a proper "ref:", or a regular file HEAD that has a properly
165 * formatted sha1 object name.
167 static int is_git_directory(const char *suspect)
169 char path[PATH_MAX];
170 size_t len = strlen(suspect);
172 strcpy(path, suspect);
173 if (getenv(DB_ENVIRONMENT)) {
174 if (access(getenv(DB_ENVIRONMENT), X_OK))
175 return 0;
177 else {
178 strcpy(path + len, "/objects");
179 if (access(path, X_OK))
180 return 0;
183 strcpy(path + len, "/refs");
184 if (access(path, X_OK))
185 return 0;
187 strcpy(path + len, "/HEAD");
188 if (validate_headref(path))
189 return 0;
191 return 1;
194 int is_inside_git_dir(void)
196 if (inside_git_dir < 0)
197 inside_git_dir = is_inside_dir(get_git_dir());
198 return inside_git_dir;
201 int is_inside_work_tree(void)
203 if (inside_work_tree < 0)
204 inside_work_tree = is_inside_dir(get_git_work_tree());
205 return inside_work_tree;
209 * set_work_tree() is only ever called if you set GIT_DIR explicitely.
210 * The old behaviour (which we retain here) is to set the work tree root
211 * to the cwd, unless overridden by the config, the command line, or
212 * GIT_WORK_TREE.
214 static const char *set_work_tree(const char *dir)
216 char buffer[PATH_MAX + 1];
218 if (!getcwd(buffer, sizeof(buffer)))
219 die ("Could not get the current working directory");
220 git_work_tree_cfg = xstrdup(buffer);
221 inside_work_tree = 1;
223 return NULL;
226 void setup_work_tree(void)
228 const char *work_tree, *git_dir;
229 static int initialized = 0;
231 if (initialized)
232 return;
233 work_tree = get_git_work_tree();
234 git_dir = get_git_dir();
235 if (!is_absolute_path(git_dir))
236 git_dir = make_absolute_path(git_dir);
237 if (!work_tree || chdir(work_tree))
238 die("This operation must be run in a work tree");
239 set_git_dir(make_relative_path(git_dir, work_tree));
240 initialized = 1;
243 static int check_repository_format_gently(int *nongit_ok)
245 git_config(check_repository_format_version, NULL);
246 if (GIT_REPO_VERSION < repository_format_version) {
247 if (!nongit_ok)
248 die ("Expected git repo version <= %d, found %d",
249 GIT_REPO_VERSION, repository_format_version);
250 warning("Expected git repo version <= %d, found %d",
251 GIT_REPO_VERSION, repository_format_version);
252 warning("Please upgrade Git");
253 *nongit_ok = -1;
254 return -1;
256 return 0;
260 * Try to read the location of the git directory from the .git file,
261 * return path to git directory if found.
263 const char *read_gitfile_gently(const char *path)
265 char *buf;
266 struct stat st;
267 int fd;
268 size_t len;
270 if (stat(path, &st))
271 return NULL;
272 if (!S_ISREG(st.st_mode))
273 return NULL;
274 fd = open(path, O_RDONLY);
275 if (fd < 0)
276 die_errno("Error opening '%s'", path);
277 buf = xmalloc(st.st_size + 1);
278 len = read_in_full(fd, buf, st.st_size);
279 close(fd);
280 if (len != st.st_size)
281 die("Error reading %s", path);
282 buf[len] = '\0';
283 if (prefixcmp(buf, "gitdir: "))
284 die("Invalid gitfile format: %s", path);
285 while (buf[len - 1] == '\n' || buf[len - 1] == '\r')
286 len--;
287 if (len < 9)
288 die("No path in gitfile: %s", path);
289 buf[len] = '\0';
290 if (!is_git_directory(buf + 8))
291 die("Not a git repository: %s", buf + 8);
292 path = make_absolute_path(buf + 8);
293 free(buf);
294 return path;
298 * We cannot decide in this function whether we are in the work tree or
299 * not, since the config can only be read _after_ this function was called.
301 const char *setup_git_directory_gently(int *nongit_ok)
303 const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT);
304 const char *env_ceiling_dirs = getenv(CEILING_DIRECTORIES_ENVIRONMENT);
305 static char cwd[PATH_MAX+1];
306 const char *gitdirenv;
307 const char *gitfile_dir;
308 int len, offset, ceil_offset;
311 * Let's assume that we are in a git repository.
312 * If it turns out later that we are somewhere else, the value will be
313 * updated accordingly.
315 if (nongit_ok)
316 *nongit_ok = 0;
319 * If GIT_DIR is set explicitly, we're not going
320 * to do any discovery, but we still do repository
321 * validation.
323 gitdirenv = getenv(GIT_DIR_ENVIRONMENT);
324 if (gitdirenv) {
325 if (PATH_MAX - 40 < strlen(gitdirenv))
326 die("'$%s' too big", GIT_DIR_ENVIRONMENT);
327 if (is_git_directory(gitdirenv)) {
328 static char buffer[1024 + 1];
329 const char *retval;
331 if (!work_tree_env) {
332 retval = set_work_tree(gitdirenv);
333 /* config may override worktree */
334 if (check_repository_format_gently(nongit_ok))
335 return NULL;
336 return retval;
338 if (check_repository_format_gently(nongit_ok))
339 return NULL;
340 retval = get_relative_cwd(buffer, sizeof(buffer) - 1,
341 get_git_work_tree());
342 if (!retval || !*retval)
343 return NULL;
344 set_git_dir(make_absolute_path(gitdirenv));
345 if (chdir(work_tree_env) < 0)
346 die_errno ("Could not chdir to '%s'", work_tree_env);
347 strcat(buffer, "/");
348 return retval;
350 if (nongit_ok) {
351 *nongit_ok = 1;
352 return NULL;
354 die("Not a git repository: '%s'", gitdirenv);
357 if (!getcwd(cwd, sizeof(cwd)-1))
358 die_errno("Unable to read current working directory");
360 ceil_offset = longest_ancestor_length(cwd, env_ceiling_dirs);
361 if (ceil_offset < 0 && has_dos_drive_prefix(cwd))
362 ceil_offset = 1;
365 * Test in the following order (relative to the cwd):
366 * - .git (file containing "gitdir: <path>")
367 * - .git/
368 * - ./ (bare)
369 * - ../.git
370 * - ../.git/
371 * - ../ (bare)
372 * - ../../.git/
373 * etc.
375 offset = len = strlen(cwd);
376 for (;;) {
377 gitfile_dir = read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT);
378 if (gitfile_dir) {
379 if (set_git_dir(gitfile_dir))
380 die("Repository setup failed");
381 break;
383 if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT))
384 break;
385 if (is_git_directory(".")) {
386 inside_git_dir = 1;
387 if (!work_tree_env)
388 inside_work_tree = 0;
389 if (offset != len) {
390 cwd[offset] = '\0';
391 setenv(GIT_DIR_ENVIRONMENT, cwd, 1);
392 } else
393 setenv(GIT_DIR_ENVIRONMENT, ".", 1);
394 check_repository_format_gently(nongit_ok);
395 return NULL;
397 while (--offset > ceil_offset && cwd[offset] != '/');
398 if (offset <= ceil_offset) {
399 if (nongit_ok) {
400 if (chdir(cwd))
401 die_errno("Cannot come back to cwd");
402 *nongit_ok = 1;
403 return NULL;
405 die("Not a git repository (or any of the parent directories): %s", DEFAULT_GIT_DIR_ENVIRONMENT);
407 if (chdir(".."))
408 die_errno("Cannot change to '%s/..'", cwd);
411 inside_git_dir = 0;
412 if (!work_tree_env)
413 inside_work_tree = 1;
414 git_work_tree_cfg = xstrndup(cwd, offset);
415 if (check_repository_format_gently(nongit_ok))
416 return NULL;
417 if (offset == len)
418 return NULL;
420 /* Make "offset" point to past the '/', and add a '/' at the end */
421 offset++;
422 cwd[len++] = '/';
423 cwd[len] = 0;
424 return cwd + offset;
427 int git_config_perm(const char *var, const char *value)
429 int i;
430 char *endptr;
432 if (value == NULL)
433 return PERM_GROUP;
435 if (!strcmp(value, "umask"))
436 return PERM_UMASK;
437 if (!strcmp(value, "group"))
438 return PERM_GROUP;
439 if (!strcmp(value, "all") ||
440 !strcmp(value, "world") ||
441 !strcmp(value, "everybody"))
442 return PERM_EVERYBODY;
444 /* Parse octal numbers */
445 i = strtol(value, &endptr, 8);
447 /* If not an octal number, maybe true/false? */
448 if (*endptr != 0)
449 return git_config_bool(var, value) ? PERM_GROUP : PERM_UMASK;
452 * Treat values 0, 1 and 2 as compatibility cases, otherwise it is
453 * a chmod value to restrict to.
455 switch (i) {
456 case PERM_UMASK: /* 0 */
457 return PERM_UMASK;
458 case OLD_PERM_GROUP: /* 1 */
459 return PERM_GROUP;
460 case OLD_PERM_EVERYBODY: /* 2 */
461 return PERM_EVERYBODY;
464 /* A filemode value was given: 0xxx */
466 if ((i & 0600) != 0600)
467 die("Problem with core.sharedRepository filemode value "
468 "(0%.3o).\nThe owner of files must always have "
469 "read and write permissions.", i);
472 * Mask filemode value. Others can not get write permission.
473 * x flags for directories are handled separately.
475 return -(i & 0666);
478 int check_repository_format_version(const char *var, const char *value, void *cb)
480 if (strcmp(var, "core.repositoryformatversion") == 0)
481 repository_format_version = git_config_int(var, value);
482 else if (strcmp(var, "core.sharedrepository") == 0)
483 shared_repository = git_config_perm(var, value);
484 else if (strcmp(var, "core.bare") == 0) {
485 is_bare_repository_cfg = git_config_bool(var, value);
486 if (is_bare_repository_cfg == 1)
487 inside_work_tree = -1;
488 } else if (strcmp(var, "core.worktree") == 0) {
489 if (!value)
490 return config_error_nonbool(var);
491 free(git_work_tree_cfg);
492 git_work_tree_cfg = xstrdup(value);
493 inside_work_tree = -1;
495 return 0;
498 int check_repository_format(void)
500 return check_repository_format_gently(NULL);
503 const char *setup_git_directory(void)
505 const char *retval = setup_git_directory_gently(NULL);
507 /* If the work tree is not the default one, recompute prefix */
508 if (inside_work_tree < 0) {
509 static char buffer[PATH_MAX + 1];
510 char *rel;
511 if (retval && chdir(retval))
512 die_errno ("Could not jump back into original cwd");
513 rel = get_relative_cwd(buffer, PATH_MAX, get_git_work_tree());
514 if (rel && *rel && chdir(get_git_work_tree()))
515 die_errno ("Could not jump to working directory");
516 return rel && *rel ? strcat(rel, "/") : NULL;
519 return retval;