Revert "check_packed_git_idx(): check integrity of the idx file itself."
[git/mingw/4msysgit/wingit-dll.git] / setup.c
blobd3556edf121ba78b0b83d20fdbd9cb397e79eb2b
1 #include "cache.h"
3 const char *prefix_path(const char *prefix, int len, const char *path)
5 const char *orig = path;
6 for (;;) {
7 char c;
8 if (*path != '.')
9 break;
10 c = path[1];
11 /* "." */
12 if (!c) {
13 path++;
14 break;
16 /* "./" */
17 if (c == '/') {
18 path += 2;
19 continue;
21 if (c != '.')
22 break;
23 c = path[2];
24 if (!c)
25 path += 2;
26 else if (c == '/')
27 path += 3;
28 else
29 break;
30 /* ".." and "../" */
31 /* Remove last component of the prefix */
32 do {
33 if (!len)
34 die("'%s' is outside repository", orig);
35 len--;
36 } while (len && prefix[len-1] != '/');
37 continue;
39 if (len) {
40 int speclen = strlen(path);
41 char *n = xmalloc(speclen + len + 1);
43 memcpy(n, prefix, len);
44 memcpy(n + len, path, speclen+1);
45 path = n;
47 return path;
50 /*
51 * Unlike prefix_path, this should be used if the named file does
52 * not have to interact with index entry; i.e. name of a random file
53 * on the filesystem.
55 const char *prefix_filename(const char *pfx, int pfx_len, const char *arg)
57 static char path[PATH_MAX];
58 if (!pfx || !*pfx || arg[0] == '/')
59 return arg;
60 memcpy(path, pfx, pfx_len);
61 strcpy(path + pfx_len, arg);
62 return path;
65 const char **get_pathspec(const char *prefix, const char **pathspec)
67 const char *entry = *pathspec;
68 const char **p;
69 int prefixlen;
71 if (!prefix && !entry)
72 return NULL;
74 if (!entry) {
75 static const char *spec[2];
76 spec[0] = prefix;
77 spec[1] = NULL;
78 return spec;
81 /* Otherwise we have to re-write the entries.. */
82 p = pathspec;
83 prefixlen = prefix ? strlen(prefix) : 0;
84 do {
85 *p = prefix_path(prefix, prefixlen, entry);
86 } while ((entry = *++p) != NULL);
87 return (const char **) pathspec;
91 * Test if it looks like we're at the top level git directory.
92 * We want to see:
94 * - either a .git/objects/ directory _or_ the proper
95 * GIT_OBJECT_DIRECTORY environment variable
96 * - a refs/ directory under ".git"
97 * - either a HEAD symlink or a HEAD file that is formatted as
98 * a proper "ref:".
100 static int is_toplevel_directory(void)
102 if (access(".git/refs/", X_OK) ||
103 access(getenv(DB_ENVIRONMENT) ?
104 getenv(DB_ENVIRONMENT) : ".git/objects/", X_OK) ||
105 validate_symref(".git/HEAD"))
106 return 0;
107 return 1;
110 const char *setup_git_directory_gently(int *nongit_ok)
112 static char cwd[PATH_MAX+1];
113 int len, offset;
116 * If GIT_DIR is set explicitly, we're not going
117 * to do any discovery, but we still do repository
118 * validation.
120 if (getenv(GIT_DIR_ENVIRONMENT)) {
121 char path[PATH_MAX];
122 int len = strlen(getenv(GIT_DIR_ENVIRONMENT));
123 if (sizeof(path) - 40 < len)
124 die("'$%s' too big", GIT_DIR_ENVIRONMENT);
125 memcpy(path, getenv(GIT_DIR_ENVIRONMENT), len);
127 strcpy(path + len, "/refs");
128 if (access(path, X_OK))
129 goto bad_dir_environ;
130 strcpy(path + len, "/HEAD");
131 if (validate_symref(path))
132 goto bad_dir_environ;
133 if (getenv(DB_ENVIRONMENT)) {
134 if (access(getenv(DB_ENVIRONMENT), X_OK))
135 goto bad_dir_environ;
137 else {
138 strcpy(path + len, "/objects");
139 if (access(path, X_OK))
140 goto bad_dir_environ;
142 return NULL;
143 bad_dir_environ:
144 path[len] = 0;
145 die("Not a git repository: '%s'", path);
148 if (!getcwd(cwd, sizeof(cwd)) || cwd[0] != '/')
149 die("Unable to read current working directory");
151 offset = len = strlen(cwd);
152 for (;;) {
153 if (is_toplevel_directory())
154 break;
155 chdir("..");
156 do {
157 if (!offset) {
158 if (nongit_ok) {
159 if (chdir(cwd))
160 die("Cannot come back to cwd");
161 *nongit_ok = 1;
162 return NULL;
164 die("Not a git repository");
166 } while (cwd[--offset] != '/');
169 if (offset == len)
170 return NULL;
172 /* Make "offset" point to past the '/', and add a '/' at the end */
173 offset++;
174 cwd[len++] = '/';
175 cwd[len] = 0;
176 return cwd + offset;
179 int check_repository_format_version(const char *var, const char *value)
181 if (strcmp(var, "core.repositoryformatversion") == 0)
182 repository_format_version = git_config_int(var, value);
183 return 0;
186 int check_repository_format(void)
188 git_config(check_repository_format_version);
189 if (GIT_REPO_VERSION < repository_format_version)
190 die ("Expected git repo version <= %d, found %d",
191 GIT_REPO_VERSION, repository_format_version);
192 return 0;
195 const char *setup_git_directory(void)
197 const char *retval = setup_git_directory_gently(NULL);
198 check_repository_format();
199 return retval;