Add zlib
[git/pclouds.git] / setup.c
blobf241fc6a6021333e6bb0ed8ceb1dd6c222ed334a
1 #include "cache.h"
3 #ifdef __MINGW32__
4 static inline int is_dir_sep(char c) { return c == '/' || c == '\\'; }
5 #else
6 static inline int is_dir_sep(char c) { return c == '/'; }
7 #endif
9 const char *prefix_path(const char *prefix, int len, const char *path)
11 const char *orig = path;
12 int do_pfx;
13 for (;;) {
14 char c;
15 if (*path != '.')
16 break;
17 c = path[1];
18 /* "." */
19 if (!c) {
20 path++;
21 break;
23 /* "./" */
24 if (is_dir_sep(c)) {
25 path += 2;
26 continue;
28 if (c != '.')
29 break;
30 c = path[2];
31 if (!c)
32 path += 2;
33 else if (is_dir_sep(c))
34 path += 3;
35 else
36 break;
37 /* ".." and "../" */
38 /* Remove last component of the prefix */
39 do {
40 if (!len)
41 die("'%s' is outside repository", orig);
42 len--;
43 } while (len && !is_dir_sep(prefix[len-1]));
44 continue;
46 do_pfx = len;
47 #ifdef __MINGW32__
48 /* we want to convert '\' in path to '/' (prefix already has '/') */
50 const char *p = path;
51 while (!do_pfx && *p) {
52 do_pfx = *p++ == '\\';
55 #endif
56 if (do_pfx) {
57 int speclen = strlen(path);
58 char *n = xmalloc(speclen + len + 1);
59 char *p;
61 memcpy(n, prefix, len);
62 memcpy(n + len, path, speclen+1);
63 #ifdef __MINGW32__
64 for (p = n + len; *p; p++)
65 if (*p == '\\')
66 *p = '/';
67 #endif
68 path = n;
70 return path;
74 * Unlike prefix_path, this should be used if the named file does
75 * not have to interact with index entry; i.e. name of a random file
76 * on the filesystem.
78 const char *prefix_filename(const char *pfx, int pfx_len, const char *arg)
80 static char path[PATH_MAX];
81 char *p;
82 #ifndef __MINGW32__
83 if (!pfx || !*pfx || arg[0] == '/')
84 return arg;
85 #else
86 /* don't add prefix to absolute paths */
87 const int is_absolute =
88 is_dir_sep(arg[0]) ||
89 (arg[0] && arg[1] == ':' && is_dir_sep(arg[2]));
90 if (is_absolute)
91 pfx_len = 0;
92 else
93 #endif
94 memcpy(path, pfx, pfx_len);
95 strcpy(path + pfx_len, arg);
96 for (p = path + pfx_len; *p; p++)
97 if (*p == '\\')
98 *p = '/';
99 return path;
103 * Verify a filename that we got as an argument for a pathspec
104 * entry. Note that a filename that begins with "-" never verifies
105 * as true, because even if such a filename were to exist, we want
106 * it to be preceded by the "--" marker (or we want the user to
107 * use a format like "./-filename")
109 void verify_filename(const char *prefix, const char *arg)
111 const char *name;
112 struct stat st;
114 if (*arg == '-')
115 die("bad flag '%s' used after filename", arg);
116 name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg;
117 if (!lstat(name, &st))
118 return;
119 if (errno == ENOENT)
120 die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
121 "Use '--' to separate paths from revisions", arg);
122 die("'%s': %s", arg, strerror(errno));
126 * Opposite of the above: the command line did not have -- marker
127 * and we parsed the arg as a refname. It should not be interpretable
128 * as a filename.
130 void verify_non_filename(const char *prefix, const char *arg)
132 const char *name;
133 struct stat st;
135 if (!is_inside_work_tree() || is_inside_git_dir())
136 return;
137 if (*arg == '-')
138 return; /* flag */
139 name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg;
140 if (!lstat(name, &st))
141 die("ambiguous argument '%s': both revision and filename\n"
142 "Use '--' to separate filenames from revisions", arg);
143 if (errno != ENOENT)
144 die("'%s': %s", arg, strerror(errno));
147 const char **get_pathspec(const char *prefix, const char **pathspec)
149 const char *entry = *pathspec;
150 const char **p;
151 int prefixlen;
153 if (!prefix && !entry)
154 return NULL;
156 if (!entry) {
157 static const char *spec[2];
158 spec[0] = prefix;
159 spec[1] = NULL;
160 return spec;
163 /* Otherwise we have to re-write the entries.. */
164 p = pathspec;
165 prefixlen = prefix ? strlen(prefix) : 0;
166 do {
167 *p = prefix_path(prefix, prefixlen, entry);
168 } while ((entry = *++p) != NULL);
169 return (const char **) pathspec;
173 * Test if it looks like we're at a git directory.
174 * We want to see:
176 * - either a objects/ directory _or_ the proper
177 * GIT_OBJECT_DIRECTORY environment variable
178 * - a refs/ directory
179 * - either a HEAD symlink or a HEAD file that is formatted as
180 * a proper "ref:", or a regular file HEAD that has a properly
181 * formatted sha1 object name.
183 static int is_git_directory(const char *suspect)
185 char path[PATH_MAX];
186 size_t len = strlen(suspect);
188 strcpy(path, suspect);
189 if (getenv(DB_ENVIRONMENT)) {
190 if (access(getenv(DB_ENVIRONMENT), X_OK))
191 return 0;
193 else {
194 strcpy(path + len, "/objects");
195 if (access(path, X_OK))
196 return 0;
199 strcpy(path + len, "/refs");
200 if (access(path, X_OK))
201 return 0;
203 strcpy(path + len, "/HEAD");
204 if (validate_headref(path))
205 return 0;
207 return 1;
210 static int inside_git_dir = -1;
212 int is_inside_git_dir(void)
214 if (inside_git_dir >= 0)
215 return inside_git_dir;
216 die("BUG: is_inside_git_dir called before setup_git_directory");
219 static int inside_work_tree = -1;
221 int is_inside_work_tree(void)
223 if (inside_git_dir >= 0)
224 return inside_work_tree;
225 die("BUG: is_inside_work_tree called before setup_git_directory");
228 static char *gitworktree_config;
230 static int git_setup_config(const char *var, const char *value)
232 if (!strcmp(var, "core.worktree")) {
233 if (gitworktree_config)
234 strlcpy(gitworktree_config, value, PATH_MAX);
235 return 0;
237 return git_default_config(var, value);
240 const char *setup_git_directory_gently(int *nongit_ok)
242 static char cwd[PATH_MAX+1];
243 char worktree[PATH_MAX+1], gitdir[PATH_MAX+1];
244 const char *gitdirenv, *gitworktree;
245 int wt_rel_gitdir = 0;
247 gitdirenv = getenv(GIT_DIR_ENVIRONMENT);
248 if (!gitdirenv) {
249 int len, offset;
250 int minoffset = 0;
252 if (!getcwd(cwd, sizeof(cwd)-1))
253 die("Unable to read current working directory");
254 #ifdef __MINGW32__
255 if (cwd[1] == ':')
256 minoffset = 2;
257 #endif
259 offset = len = strlen(cwd);
260 for (;;) {
261 if (is_git_directory(".git"))
262 break;
263 if (offset == 0) {
264 offset = -1;
265 break;
267 chdir("..");
268 while (offset > minoffset && cwd[--offset] != '/')
269 ; /* do nothing */
272 if (offset >= 0) {
273 inside_work_tree = 1;
274 git_config(git_default_config);
275 if (offset == len) {
276 inside_git_dir = 0;
277 return NULL;
280 cwd[len++] = '/';
281 cwd[len] = '\0';
282 inside_git_dir = !prefixcmp(cwd + offset + 1, ".git/");
283 return cwd + offset + 1;
286 if (chdir(cwd))
287 die("Cannot come back to cwd");
288 if (!is_git_directory(".")) {
289 if (nongit_ok) {
290 *nongit_ok = 1;
291 return NULL;
293 die("Not a git repository");
295 set_git_dir(cwd);
296 gitdirenv = getenv(GIT_DIR_ENVIRONMENT);
297 if (!gitdirenv)
298 die("getenv after setenv failed");
301 if (PATH_MAX - 40 < strlen(gitdirenv)) {
302 if (nongit_ok) {
303 *nongit_ok = 1;
304 return NULL;
306 die("$%s too big", GIT_DIR_ENVIRONMENT);
308 if (!is_git_directory(gitdirenv)) {
309 if (nongit_ok) {
310 *nongit_ok = 1;
311 return NULL;
313 die("Not a git repository: '%s'", gitdirenv);
316 if (!getcwd(cwd, sizeof(cwd)-1))
317 die("Unable to read current working directory");
318 if (chdir(gitdirenv)) {
319 if (nongit_ok) {
320 *nongit_ok = 1;
321 return NULL;
323 die("Cannot change directory to $%s '%s'",
324 GIT_DIR_ENVIRONMENT, gitdirenv);
326 if (!getcwd(gitdir, sizeof(gitdir)-1))
327 die("Unable to read current working directory");
328 if (chdir(cwd))
329 die("Cannot come back to cwd");
332 * In case there is a work tree we may change the directory,
333 * therefore make GIT_DIR an absolute path.
335 if (gitdirenv[0] != '/') {
336 set_git_dir(gitdir);
337 gitdirenv = getenv(GIT_DIR_ENVIRONMENT);
338 if (!gitdirenv)
339 die("getenv after setenv failed");
340 if (PATH_MAX - 40 < strlen(gitdirenv)) {
341 if (nongit_ok) {
342 *nongit_ok = 1;
343 return NULL;
345 die("$%s too big after expansion to absolute path",
346 GIT_DIR_ENVIRONMENT);
350 strcat(cwd, "/");
351 strcat(gitdir, "/");
352 inside_git_dir = !prefixcmp(cwd, gitdir);
354 gitworktree = getenv(GIT_WORK_TREE_ENVIRONMENT);
355 if (!gitworktree) {
356 gitworktree_config = worktree;
357 worktree[0] = '\0';
359 git_config(git_setup_config);
360 if (!gitworktree) {
361 gitworktree_config = NULL;
362 if (worktree[0])
363 gitworktree = worktree;
364 if (gitworktree && gitworktree[0] != '/')
365 wt_rel_gitdir = 1;
368 if (wt_rel_gitdir && chdir(gitdirenv))
369 die("Cannot change directory to $%s '%s'",
370 GIT_DIR_ENVIRONMENT, gitdirenv);
371 if (gitworktree && chdir(gitworktree)) {
372 if (nongit_ok) {
373 if (wt_rel_gitdir && chdir(cwd))
374 die("Cannot come back to cwd");
375 *nongit_ok = 1;
376 return NULL;
378 if (wt_rel_gitdir)
379 die("Cannot change directory to working tree '%s'"
380 " from $%s", gitworktree, GIT_DIR_ENVIRONMENT);
381 else
382 die("Cannot change directory to working tree '%s'",
383 gitworktree);
385 if (!getcwd(worktree, sizeof(worktree)-1))
386 die("Unable to read current working directory");
387 strcat(worktree, "/");
388 inside_work_tree = !prefixcmp(cwd, worktree);
390 if (gitworktree && inside_work_tree && !prefixcmp(worktree, gitdir) &&
391 strcmp(worktree, gitdir)) {
392 inside_git_dir = 0;
395 if (!inside_work_tree) {
396 if (chdir(cwd))
397 die("Cannot come back to cwd");
398 return NULL;
401 if (!strcmp(cwd, worktree))
402 return NULL;
403 return cwd+strlen(worktree);
406 int git_config_perm(const char *var, const char *value)
408 if (value) {
409 int i;
410 if (!strcmp(value, "umask"))
411 return PERM_UMASK;
412 if (!strcmp(value, "group"))
413 return PERM_GROUP;
414 if (!strcmp(value, "all") ||
415 !strcmp(value, "world") ||
416 !strcmp(value, "everybody"))
417 return PERM_EVERYBODY;
418 i = atoi(value);
419 if (i > 1)
420 return i;
422 return git_config_bool(var, value);
425 int check_repository_format_version(const char *var, const char *value)
427 if (strcmp(var, "core.repositoryformatversion") == 0)
428 repository_format_version = git_config_int(var, value);
429 else if (strcmp(var, "core.sharedrepository") == 0)
430 shared_repository = git_config_perm(var, value);
431 return 0;
434 int check_repository_format(void)
436 git_config(check_repository_format_version);
437 if (GIT_REPO_VERSION < repository_format_version)
438 die ("Expected git repo version <= %d, found %d",
439 GIT_REPO_VERSION, repository_format_version);
440 return 0;
443 const char *setup_git_directory(void)
445 const char *retval = setup_git_directory_gently(NULL);
446 check_repository_format();
447 return retval;