Merge branch 'maint' of git://repo.or.cz/alt-git
[git/mingw.git] / setup.c
bloba2903b95730ba728fc23bc4575a22cccfb225cd3
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;
73 /*
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_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 char buffer[1024];
217 if (is_bare_repository())
218 return (inside_git_dir = 1);
219 if (getcwd(buffer, sizeof(buffer))) {
220 const char *git_dir = get_git_dir(), *cwd = buffer;
221 while (*git_dir && *git_dir == *cwd) {
222 git_dir++;
223 cwd++;
225 inside_git_dir = !*git_dir;
226 } else
227 inside_git_dir = 0;
229 return inside_git_dir;
232 const char *setup_git_directory_gently(int *nongit_ok)
234 static char cwd[PATH_MAX+1];
235 const char *gitdirenv;
236 int len, offset;
237 int minoffset = 0;
240 * If GIT_DIR is set explicitly, we're not going
241 * to do any discovery, but we still do repository
242 * validation.
244 gitdirenv = getenv(GIT_DIR_ENVIRONMENT);
245 if (gitdirenv) {
246 if (PATH_MAX - 40 < strlen(gitdirenv))
247 die("'$%s' too big", GIT_DIR_ENVIRONMENT);
248 if (is_git_directory(gitdirenv))
249 return NULL;
250 if (nongit_ok) {
251 *nongit_ok = 1;
252 return NULL;
254 die("Not a git repository: '%s'", gitdirenv);
257 #ifdef __MINGW32__
258 if (!getcwd(cwd, sizeof(cwd)-1) || !(cwd[0] == '/' || cwd[1] == ':'))
259 die("Unable to read current working directory");
260 if (cwd[1] == ':')
261 minoffset = 2;
262 #else
263 if (!getcwd(cwd, sizeof(cwd)-1) || cwd[0] != '/')
264 die("Unable to read current working directory");
265 #endif
267 offset = len = strlen(cwd);
268 for (;;) {
269 if (is_git_directory(".git"))
270 break;
271 chdir("..");
272 do {
273 if (offset <= minoffset) {
274 if (is_git_directory(cwd)) {
275 if (chdir(cwd))
276 die("Cannot come back to cwd");
277 setenv(GIT_DIR_ENVIRONMENT, cwd, 1);
278 inside_git_dir = 1;
279 return NULL;
281 if (nongit_ok) {
282 if (chdir(cwd))
283 die("Cannot come back to cwd");
284 *nongit_ok = 1;
285 return NULL;
287 die("Not a git repository");
289 } while (offset > minoffset && cwd[--offset] != '/');
292 if (offset == len)
293 return NULL;
295 /* Make "offset" point to past the '/', and add a '/' at the end */
296 offset++;
297 cwd[len++] = '/';
298 cwd[len] = 0;
299 inside_git_dir = !prefixcmp(cwd + offset, ".git/");
300 return cwd + offset;
303 int git_config_perm(const char *var, const char *value)
305 if (value) {
306 if (!strcmp(value, "umask"))
307 return PERM_UMASK;
308 if (!strcmp(value, "group"))
309 return PERM_GROUP;
310 if (!strcmp(value, "all") ||
311 !strcmp(value, "world") ||
312 !strcmp(value, "everybody"))
313 return PERM_EVERYBODY;
315 return git_config_bool(var, value);
318 int check_repository_format_version(const char *var, const char *value)
320 if (strcmp(var, "core.repositoryformatversion") == 0)
321 repository_format_version = git_config_int(var, value);
322 else if (strcmp(var, "core.sharedrepository") == 0)
323 shared_repository = git_config_perm(var, value);
324 return 0;
327 int check_repository_format(void)
329 git_config(check_repository_format_version);
330 if (GIT_REPO_VERSION < repository_format_version)
331 die ("Expected git repo version <= %d, found %d",
332 GIT_REPO_VERSION, repository_format_version);
333 return 0;
336 const char *setup_git_directory(void)
338 const char *retval = setup_git_directory_gently(NULL);
339 check_repository_format();
340 return retval;