Code

Do check_repository_format() early (re-fix)
[git.git] / setup.c
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)
8 {
9         const char *orig = path;
10         for (;;) {
11                 char c;
12                 if (*path != '.')
13                         break;
14                 c = path[1];
15                 /* "." */
16                 if (!c) {
17                         path++;
18                         break;
19                 }
20                 /* "./" */
21                 if (c == '/') {
22                         path += 2;
23                         continue;
24                 }
25                 if (c != '.')
26                         break;
27                 c = path[2];
28                 if (!c)
29                         path += 2;
30                 else if (c == '/')
31                         path += 3;
32                 else
33                         break;
34                 /* ".." and "../" */
35                 /* Remove last component of the prefix */
36                 do {
37                         if (!len)
38                                 die("'%s' is outside repository", orig);
39                         len--;
40                 } while (len && prefix[len-1] != '/');
41                 continue;
42         }
43         if (len) {
44                 int speclen = strlen(path);
45                 char *n = xmalloc(speclen + len + 1);
47                 memcpy(n, prefix, len);
48                 memcpy(n + len, path, speclen+1);
49                 path = n;
50         }
51         return path;
52 }
54 /*
55  * Unlike prefix_path, this should be used if the named file does
56  * not have to interact with index entry; i.e. name of a random file
57  * on the filesystem.
58  */
59 const char *prefix_filename(const char *pfx, int pfx_len, const char *arg)
60 {
61         static char path[PATH_MAX];
62         if (!pfx || !*pfx || arg[0] == '/')
63                 return arg;
64         memcpy(path, pfx, pfx_len);
65         strcpy(path + pfx_len, arg);
66         return path;
67 }
69 /*
70  * Verify a filename that we got as an argument for a pathspec
71  * entry. Note that a filename that begins with "-" never verifies
72  * as true, because even if such a filename were to exist, we want
73  * it to be preceded by the "--" marker (or we want the user to
74  * use a format like "./-filename")
75  */
76 void verify_filename(const char *prefix, const char *arg)
77 {
78         const char *name;
79         struct stat st;
81         if (*arg == '-')
82                 die("bad flag '%s' used after filename", arg);
83         name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg;
84         if (!lstat(name, &st))
85                 return;
86         if (errno == ENOENT)
87                 die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
88                     "Use '--' to separate paths from revisions", arg);
89         die("'%s': %s", arg, strerror(errno));
90 }
92 /*
93  * Opposite of the above: the command line did not have -- marker
94  * and we parsed the arg as a refname.  It should not be interpretable
95  * as a filename.
96  */
97 void verify_non_filename(const char *prefix, const char *arg)
98 {
99         const char *name;
100         struct stat st;
102         if (!is_inside_work_tree() || is_inside_git_dir())
103                 return;
104         if (*arg == '-')
105                 return; /* flag */
106         name = prefix ? prefix_filename(prefix, strlen(prefix), arg) : arg;
107         if (!lstat(name, &st))
108                 die("ambiguous argument '%s': both revision and filename\n"
109                     "Use '--' to separate filenames from revisions", arg);
110         if (errno != ENOENT && errno != ENOTDIR)
111                 die("'%s': %s", arg, strerror(errno));
114 const char **get_pathspec(const char *prefix, const char **pathspec)
116         const char *entry = *pathspec;
117         const char **p;
118         int prefixlen;
120         if (!prefix && !entry)
121                 return NULL;
123         if (!entry) {
124                 static const char *spec[2];
125                 spec[0] = prefix;
126                 spec[1] = NULL;
127                 return spec;
128         }
130         /* Otherwise we have to re-write the entries.. */
131         p = pathspec;
132         prefixlen = prefix ? strlen(prefix) : 0;
133         do {
134                 *p = prefix_path(prefix, prefixlen, entry);
135         } while ((entry = *++p) != NULL);
136         return (const char **) pathspec;
139 /*
140  * Test if it looks like we're at a git directory.
141  * We want to see:
142  *
143  *  - either a objects/ directory _or_ the proper
144  *    GIT_OBJECT_DIRECTORY environment variable
145  *  - a refs/ directory
146  *  - either a HEAD symlink or a HEAD file that is formatted as
147  *    a proper "ref:", or a regular file HEAD that has a properly
148  *    formatted sha1 object name.
149  */
150 static int is_git_directory(const char *suspect)
152         char path[PATH_MAX];
153         size_t len = strlen(suspect);
155         strcpy(path, suspect);
156         if (getenv(DB_ENVIRONMENT)) {
157                 if (access(getenv(DB_ENVIRONMENT), X_OK))
158                         return 0;
159         }
160         else {
161                 strcpy(path + len, "/objects");
162                 if (access(path, X_OK))
163                         return 0;
164         }
166         strcpy(path + len, "/refs");
167         if (access(path, X_OK))
168                 return 0;
170         strcpy(path + len, "/HEAD");
171         if (validate_headref(path))
172                 return 0;
174         return 1;
177 int is_inside_git_dir(void)
179         if (inside_git_dir < 0)
180                 inside_git_dir = is_inside_dir(get_git_dir());
181         return inside_git_dir;
184 int is_inside_work_tree(void)
186         if (inside_work_tree < 0)
187                 inside_work_tree = is_inside_dir(get_git_work_tree());
188         return inside_work_tree;
191 /*
192  * set_work_tree() is only ever called if you set GIT_DIR explicitely.
193  * The old behaviour (which we retain here) is to set the work tree root
194  * to the cwd, unless overridden by the config, the command line, or
195  * GIT_WORK_TREE.
196  */
197 static const char *set_work_tree(const char *dir)
199         char buffer[PATH_MAX + 1];
201         if (!getcwd(buffer, sizeof(buffer)))
202                 die ("Could not get the current working directory");
203         git_work_tree_cfg = xstrdup(buffer);
204         inside_work_tree = 1;
206         return NULL;
209 static int check_repository_format_gently(int *nongit_ok)
211         git_config(check_repository_format_version);
212         if (GIT_REPO_VERSION < repository_format_version) {
213                 if (!nongit_ok)
214                         die ("Expected git repo version <= %d, found %d",
215                              GIT_REPO_VERSION, repository_format_version);
216                 warning("Expected git repo version <= %d, found %d",
217                         GIT_REPO_VERSION, repository_format_version);
218                 warning("Please upgrade Git");
219                 *nongit_ok = -1;
220                 return -1;
221         }
222         return 0;
225 /*
226  * We cannot decide in this function whether we are in the work tree or
227  * not, since the config can only be read _after_ this function was called.
228  */
229 const char *setup_git_directory_gently(int *nongit_ok)
231         const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT);
232         static char cwd[PATH_MAX+1];
233         const char *gitdirenv;
234         int len, offset;
236         /*
237          * If GIT_DIR is set explicitly, we're not going
238          * to do any discovery, but we still do repository
239          * validation.
240          */
241         gitdirenv = getenv(GIT_DIR_ENVIRONMENT);
242         if (gitdirenv) {
243                 if (PATH_MAX - 40 < strlen(gitdirenv))
244                         die("'$%s' too big", GIT_DIR_ENVIRONMENT);
245                 if (is_git_directory(gitdirenv)) {
246                         static char buffer[1024 + 1];
247                         const char *retval;
249                         if (!work_tree_env) {
250                                 retval = set_work_tree(gitdirenv);
251                                 /* config may override worktree */
252                                 if (check_repository_format_gently(nongit_ok))
253                                         return NULL;
254                                 return retval;
255                         }
256                         if (check_repository_format_gently(nongit_ok))
257                                 return NULL;
258                         retval = get_relative_cwd(buffer, sizeof(buffer) - 1,
259                                         get_git_work_tree());
260                         if (!retval || !*retval)
261                                 return NULL;
262                         set_git_dir(make_absolute_path(gitdirenv));
263                         if (chdir(work_tree_env) < 0)
264                                 die ("Could not chdir to %s", work_tree_env);
265                         strcat(buffer, "/");
266                         return retval;
267                 }
268                 if (nongit_ok) {
269                         *nongit_ok = 1;
270                         return NULL;
271                 }
272                 die("Not a git repository: '%s'", gitdirenv);
273         }
275         if (!getcwd(cwd, sizeof(cwd)-1))
276                 die("Unable to read current working directory");
278         /*
279          * Test in the following order (relative to the cwd):
280          * - .git/
281          * - ./ (bare)
282          * - ../.git/
283          * - ../ (bare)
284          * - ../../.git/
285          *   etc.
286          */
287         offset = len = strlen(cwd);
288         for (;;) {
289                 if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT))
290                         break;
291                 if (is_git_directory(".")) {
292                         inside_git_dir = 1;
293                         if (!work_tree_env)
294                                 inside_work_tree = 0;
295                         setenv(GIT_DIR_ENVIRONMENT, ".", 1);
296                         check_repository_format_gently(nongit_ok);
297                         return NULL;
298                 }
299                 chdir("..");
300                 do {
301                         if (!offset) {
302                                 if (nongit_ok) {
303                                         if (chdir(cwd))
304                                                 die("Cannot come back to cwd");
305                                         *nongit_ok = 1;
306                                         return NULL;
307                                 }
308                                 die("Not a git repository");
309                         }
310                 } while (cwd[--offset] != '/');
311         }
313         inside_git_dir = 0;
314         if (!work_tree_env)
315                 inside_work_tree = 1;
316         git_work_tree_cfg = xstrndup(cwd, offset);
317         if (check_repository_format_gently(nongit_ok))
318                 return NULL;
319         if (offset == len)
320                 return NULL;
322         /* Make "offset" point to past the '/', and add a '/' at the end */
323         offset++;
324         cwd[len++] = '/';
325         cwd[len] = 0;
326         return cwd + offset;
329 int git_config_perm(const char *var, const char *value)
331         if (value) {
332                 int i;
333                 if (!strcmp(value, "umask"))
334                         return PERM_UMASK;
335                 if (!strcmp(value, "group"))
336                         return PERM_GROUP;
337                 if (!strcmp(value, "all") ||
338                     !strcmp(value, "world") ||
339                     !strcmp(value, "everybody"))
340                         return PERM_EVERYBODY;
341                 i = atoi(value);
342                 if (i > 1)
343                         return i;
344         }
345         return git_config_bool(var, value);
348 int check_repository_format_version(const char *var, const char *value)
350         if (strcmp(var, "core.repositoryformatversion") == 0)
351                 repository_format_version = git_config_int(var, value);
352         else if (strcmp(var, "core.sharedrepository") == 0)
353                 shared_repository = git_config_perm(var, value);
354         else if (strcmp(var, "core.bare") == 0) {
355                 is_bare_repository_cfg = git_config_bool(var, value);
356                 if (is_bare_repository_cfg == 1)
357                         inside_work_tree = -1;
358         } else if (strcmp(var, "core.worktree") == 0) {
359                 if (git_work_tree_cfg)
360                         free(git_work_tree_cfg);
361                 git_work_tree_cfg = xstrdup(value);
362                 inside_work_tree = -1;
363         }
364         return 0;
367 int check_repository_format(void)
369         return check_repository_format_gently(NULL);
372 const char *setup_git_directory(void)
374         const char *retval = setup_git_directory_gently(NULL);
375         check_repository_format();
377         /* If the work tree is not the default one, recompute prefix */
378         if (inside_work_tree < 0) {
379                 static char buffer[PATH_MAX + 1];
380                 char *rel;
381                 if (retval && chdir(retval))
382                         die ("Could not jump back into original cwd");
383                 rel = get_relative_cwd(buffer, PATH_MAX, get_git_work_tree());
384                 return rel && *rel ? strcat(rel, "/") : NULL;
385         }
387         return retval;