]> git.ipfire.org Git - thirdparty/git.git/blame - setup.c
git-branch: show detached HEAD
[thirdparty/git.git] / setup.c
CommitLineData
d288a700
LT
1#include "cache.h"
2
6b5ee137 3const char *prefix_path(const char *prefix, int len, const char *path)
f332726e 4{
6b5ee137 5 const char *orig = path;
f332726e
LT
6 for (;;) {
7 char c;
8 if (*path != '.')
9 break;
10 c = path[1];
11 /* "." */
12 if (!c) {
13 path++;
14 break;
15 }
16 /* "./" */
17 if (c == '/') {
18 path += 2;
19 continue;
20 }
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;
38 }
39 if (len) {
40 int speclen = strlen(path);
41 char *n = xmalloc(speclen + len + 1);
42
43 memcpy(n, prefix, len);
44 memcpy(n + len, path, speclen+1);
45 path = n;
46 }
47 return path;
48}
49
4ca06608
JH
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.
54 */
55const char *prefix_filename(const char *pfx, int pfx_len, const char *arg)
56{
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;
63}
64
e23d0b4a
LT
65/*
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")
71 */
72void verify_filename(const char *prefix, const char *arg)
73{
74 const char *name;
75 struct stat st;
76
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)
ea92f41f
JH
83 die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
84 "Use '--' to separate paths from revisions", arg);
e23d0b4a
LT
85 die("'%s': %s", arg, strerror(errno));
86}
87
ea92f41f
JH
88/*
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.
92 */
93void verify_non_filename(const char *prefix, const char *arg)
94{
95 const char *name;
96 struct stat st;
97
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));
106}
107
6b5ee137 108const char **get_pathspec(const char *prefix, const char **pathspec)
d288a700 109{
6b5ee137
JH
110 const char *entry = *pathspec;
111 const char **p;
d288a700
LT
112 int prefixlen;
113
f332726e
LT
114 if (!prefix && !entry)
115 return NULL;
d288a700
LT
116
117 if (!entry) {
118 static const char *spec[2];
119 spec[0] = prefix;
120 spec[1] = NULL;
121 return spec;
122 }
123
124 /* Otherwise we have to re-write the entries.. */
d288a700 125 p = pathspec;
f332726e 126 prefixlen = prefix ? strlen(prefix) : 0;
d288a700 127 do {
f332726e 128 *p = prefix_path(prefix, prefixlen, entry);
d288a700
LT
129 } while ((entry = *++p) != NULL);
130 return (const char **) pathspec;
131}
132
5f5608bc 133/*
ad1a382f 134 * Test if it looks like we're at a git directory.
5e7bfe25 135 * We want to see:
5f5608bc 136 *
ad1a382f 137 * - either a objects/ directory _or_ the proper
5f5608bc 138 * GIT_OBJECT_DIRECTORY environment variable
ad1a382f 139 * - a refs/ directory
8098a178
JH
140 * - either a HEAD symlink or a HEAD file that is formatted as
141 * a proper "ref:".
5f5608bc 142 */
ad1a382f 143static int is_git_directory(const char *suspect)
5f5608bc 144{
ad1a382f
SP
145 char path[PATH_MAX];
146 size_t len = strlen(suspect);
147
148 strcpy(path, suspect);
149 if (getenv(DB_ENVIRONMENT)) {
150 if (access(getenv(DB_ENVIRONMENT), X_OK))
151 return 0;
152 }
153 else {
154 strcpy(path + len, "/objects");
155 if (access(path, X_OK))
156 return 0;
157 }
158
159 strcpy(path + len, "/refs");
160 if (access(path, X_OK))
8098a178 161 return 0;
ad1a382f
SP
162
163 strcpy(path + len, "/HEAD");
164 if (validate_symref(path))
165 return 0;
166
8098a178 167 return 1;
5f5608bc
LT
168}
169
4ca06608 170const char *setup_git_directory_gently(int *nongit_ok)
d288a700
LT
171{
172 static char cwd[PATH_MAX+1];
ad1a382f 173 const char *gitdirenv;
d288a700
LT
174 int len, offset;
175
176 /*
177 * If GIT_DIR is set explicitly, we're not going
5e7bfe25
JH
178 * to do any discovery, but we still do repository
179 * validation.
d288a700 180 */
ad1a382f
SP
181 gitdirenv = getenv(GIT_DIR_ENVIRONMENT);
182 if (gitdirenv) {
183 if (PATH_MAX - 40 < strlen(gitdirenv))
5e7bfe25 184 die("'$%s' too big", GIT_DIR_ENVIRONMENT);
ad1a382f
SP
185 if (is_git_directory(gitdirenv))
186 return NULL;
3a3c3fc4 187 if (nongit_ok) {
41e95f69
ML
188 *nongit_ok = 1;
189 return NULL;
190 }
ad1a382f 191 die("Not a git repository: '%s'", gitdirenv);
5e7bfe25 192 }
d288a700
LT
193
194 if (!getcwd(cwd, sizeof(cwd)) || cwd[0] != '/')
195 die("Unable to read current working directory");
196
197 offset = len = strlen(cwd);
198 for (;;) {
ad1a382f 199 if (is_git_directory(".git"))
5f5608bc 200 break;
d288a700
LT
201 chdir("..");
202 do {
4ca06608 203 if (!offset) {
ad1a382f
SP
204 if (is_git_directory(cwd)) {
205 if (chdir(cwd))
206 die("Cannot come back to cwd");
207 setenv(GIT_DIR_ENVIRONMENT, cwd, 1);
208 return NULL;
209 }
4ca06608
JH
210 if (nongit_ok) {
211 if (chdir(cwd))
212 die("Cannot come back to cwd");
213 *nongit_ok = 1;
214 return NULL;
215 }
d288a700 216 die("Not a git repository");
4ca06608 217 }
d288a700
LT
218 } while (cwd[--offset] != '/');
219 }
220
221 if (offset == len)
222 return NULL;
223
224 /* Make "offset" point to past the '/', and add a '/' at the end */
225 offset++;
226 cwd[len++] = '/';
227 cwd[len] = 0;
228 return cwd + offset;
229}
5e7bfe25 230
94df2506
JH
231int git_config_perm(const char *var, const char *value)
232{
233 if (value) {
234 if (!strcmp(value, "umask"))
235 return PERM_UMASK;
236 if (!strcmp(value, "group"))
237 return PERM_GROUP;
238 if (!strcmp(value, "all") ||
239 !strcmp(value, "world") ||
240 !strcmp(value, "everybody"))
241 return PERM_EVERYBODY;
242 }
243 return git_config_bool(var, value);
244}
245
ab9cb76f
JH
246int check_repository_format_version(const char *var, const char *value)
247{
248 if (strcmp(var, "core.repositoryformatversion") == 0)
249 repository_format_version = git_config_int(var, value);
457f06d6 250 else if (strcmp(var, "core.sharedrepository") == 0)
94df2506 251 shared_repository = git_config_perm(var, value);
ab9cb76f
JH
252 return 0;
253}
254
255int check_repository_format(void)
256{
257 git_config(check_repository_format_version);
258 if (GIT_REPO_VERSION < repository_format_version)
259 die ("Expected git repo version <= %d, found %d",
260 GIT_REPO_VERSION, repository_format_version);
261 return 0;
262}
263
5e7bfe25
JH
264const char *setup_git_directory(void)
265{
4ca06608 266 const char *retval = setup_git_directory_gently(NULL);
22752e4c 267 check_repository_format();
5e7bfe25
JH
268 return retval;
269}