rename() fails on Windows if the destination exists.
[4msysgit-hv.git] / setup.c
blob707f8f9a55c3cb3e748871bd1f1a470b2df67382
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;
66 * Verify a filename that we got as an argument for a pathspec
67 * entry. Note that a filename that begins with "-" never verifies
68 * as true, because even if such a filename were to exist, we want
69 * it to be preceded by the "--" marker (or we want the user to
70 * use a format like "./-filename")
72 void verify_filename(const char *prefix, const char *arg)
74 const char *name;
75 struct stat st;
77 if (*arg == '-')
78 die("bad flag '%s' used after filename", arg);
79 name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg;
80 if (!lstat(name, &st))
81 return;
82 if (errno == ENOENT)
83 die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
84 "Use '--' to separate paths from revisions", arg);
85 die("'%s': %s", arg, strerror(errno));
89 * Opposite of the above: the command line did not have -- marker
90 * and we parsed the arg as a refname. It should not be interpretable
91 * as a filename.
93 void verify_non_filename(const char *prefix, const char *arg)
95 const char *name;
96 struct stat st;
98 if (*arg == '-')
99 return; /* flag */
100 name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg;
101 if (!lstat(name, &st))
102 die("ambiguous argument '%s': both revision and filename\n"
103 "Use '--' to separate filenames from revisions", arg);
104 if (errno != ENOENT)
105 die("'%s': %s", arg, strerror(errno));
108 const char **get_pathspec(const char *prefix, const char **pathspec)
110 const char *entry = *pathspec;
111 const char **p;
112 int prefixlen;
114 if (!prefix && !entry)
115 return NULL;
117 if (!entry) {
118 static const char *spec[2];
119 spec[0] = prefix;
120 spec[1] = NULL;
121 return spec;
124 /* Otherwise we have to re-write the entries.. */
125 p = pathspec;
126 prefixlen = prefix ? strlen(prefix) : 0;
127 do {
128 *p = prefix_path(prefix, prefixlen, entry);
129 } while ((entry = *++p) != NULL);
130 return (const char **) pathspec;
134 * Test if it looks like we're at a git directory.
135 * We want to see:
137 * - either a objects/ directory _or_ the proper
138 * GIT_OBJECT_DIRECTORY environment variable
139 * - a refs/ directory
140 * - either a HEAD symlink or a HEAD file that is formatted as
141 * a proper "ref:", or a regular file HEAD that has a properly
142 * formatted sha1 object name.
144 static int is_git_directory(const char *suspect)
146 char path[PATH_MAX];
147 size_t len = strlen(suspect);
149 strcpy(path, suspect);
150 if (getenv(DB_ENVIRONMENT)) {
151 if (access(getenv(DB_ENVIRONMENT), X_OK))
152 return 0;
154 else {
155 strcpy(path + len, "/objects");
156 if (access(path, X_OK))
157 return 0;
160 strcpy(path + len, "/refs");
161 if (access(path, X_OK))
162 return 0;
164 strcpy(path + len, "/HEAD");
165 if (validate_headref(path))
166 return 0;
168 return 1;
171 const char *setup_git_directory_gently(int *nongit_ok)
173 static char cwd[PATH_MAX+1];
174 const char *gitdirenv;
175 int len, offset;
176 int minoffset = 0;
179 * If GIT_DIR is set explicitly, we're not going
180 * to do any discovery, but we still do repository
181 * validation.
183 gitdirenv = getenv(GIT_DIR_ENVIRONMENT);
184 if (gitdirenv) {
185 if (PATH_MAX - 40 < strlen(gitdirenv))
186 die("'$%s' too big", GIT_DIR_ENVIRONMENT);
187 if (is_git_directory(gitdirenv))
188 return NULL;
189 if (nongit_ok) {
190 *nongit_ok = 1;
191 return NULL;
193 die("Not a git repository: '%s'", gitdirenv);
196 #ifdef __MINGW32__
197 if (!getcwd(cwd, sizeof(cwd)) || !(cwd[0] == '/' || cwd[1] == ':'))
198 die("Unable to read current working directory");
199 if (cwd[1] == ':')
200 minoffset = 2;
201 #else
202 if (!getcwd(cwd, sizeof(cwd)) || cwd[0] != '/')
203 die("Unable to read current working directory");
204 #endif
206 offset = len = strlen(cwd);
207 for (;;) {
208 if (is_git_directory(".git"))
209 break;
210 chdir("..");
211 do {
212 if (offset <= minoffset) {
213 if (is_git_directory(cwd)) {
214 if (chdir(cwd))
215 die("Cannot come back to cwd");
216 setenv(GIT_DIR_ENVIRONMENT, cwd, 1);
217 return NULL;
219 if (nongit_ok) {
220 if (chdir(cwd))
221 die("Cannot come back to cwd");
222 *nongit_ok = 1;
223 return NULL;
225 die("Not a git repository");
227 } while (offset > minoffset && cwd[--offset] != '/');
230 if (offset == len)
231 return NULL;
233 /* Make "offset" point to past the '/', and add a '/' at the end */
234 offset++;
235 cwd[len++] = '/';
236 cwd[len] = 0;
237 return cwd + offset;
240 int git_config_perm(const char *var, const char *value)
242 if (value) {
243 if (!strcmp(value, "umask"))
244 return PERM_UMASK;
245 if (!strcmp(value, "group"))
246 return PERM_GROUP;
247 if (!strcmp(value, "all") ||
248 !strcmp(value, "world") ||
249 !strcmp(value, "everybody"))
250 return PERM_EVERYBODY;
252 return git_config_bool(var, value);
255 int check_repository_format_version(const char *var, const char *value)
257 if (strcmp(var, "core.repositoryformatversion") == 0)
258 repository_format_version = git_config_int(var, value);
259 else if (strcmp(var, "core.sharedrepository") == 0)
260 shared_repository = git_config_perm(var, value);
261 return 0;
264 int check_repository_format(void)
266 git_config(check_repository_format_version);
267 if (GIT_REPO_VERSION < repository_format_version)
268 die ("Expected git repo version <= %d, found %d",
269 GIT_REPO_VERSION, repository_format_version);
270 return 0;
273 const char *setup_git_directory(void)
275 const char *retval = setup_git_directory_gently(NULL);
276 check_repository_format();
277 return retval;