4 static inline int is_dir_sep(char c
) { return c
== '/' || c
== '\\'; }
6 static inline int is_dir_sep(char c
) { return c
== '/'; }
9 const char *prefix_path(const char *prefix
, int len
, const char *path
)
11 const char *orig
= path
;
33 else if (is_dir_sep(c
))
38 /* Remove last component of the prefix */
41 die("'%s' is outside repository", orig
);
43 } while (len
&& !is_dir_sep(prefix
[len
-1]));
48 /* we want to convert '\' in path to '/' (prefix already has '/') */
51 while (!do_pfx
&& *p
) {
52 do_pfx
= *p
++ == '\\';
57 int speclen
= strlen(path
);
58 char *n
= xmalloc(speclen
+ len
+ 1);
61 memcpy(n
, prefix
, len
);
62 memcpy(n
+ len
, path
, speclen
+1);
64 for (p
= n
+ len
; *p
; p
++)
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
78 const char *prefix_filename(const char *pfx
, int pfx_len
, const char *arg
)
80 static char path
[PATH_MAX
];
83 if (!pfx
|| !*pfx
|| arg
[0] == '/')
86 /* don't add prefix to absolute paths */
87 const int is_absolute
=
89 (arg
[0] && arg
[1] == ':' && is_dir_sep(arg
[2]));
94 memcpy(path
, pfx
, pfx_len
);
95 strcpy(path
+ pfx_len
, arg
);
96 for (p
= path
+ pfx_len
; *p
; p
++)
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
)
115 die("bad flag '%s' used after filename", arg
);
116 name
= prefix
? prefix_filename(prefix
, strlen(prefix
), arg
) : arg
;
117 if (!lstat(name
, &st
))
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
130 void verify_non_filename(const char *prefix
, const char *arg
)
135 if (is_inside_git_dir())
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
);
144 die("'%s': %s", arg
, strerror(errno
));
147 const char **get_pathspec(const char *prefix
, const char **pathspec
)
149 const char *entry
= *pathspec
;
153 if (!prefix
&& !entry
)
157 static const char *spec
[2];
163 /* Otherwise we have to re-write the entries.. */
165 prefixlen
= prefix
? strlen(prefix
) : 0;
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.
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
)
186 size_t len
= strlen(suspect
);
188 strcpy(path
, suspect
);
189 if (getenv(DB_ENVIRONMENT
)) {
190 if (access(getenv(DB_ENVIRONMENT
), X_OK
))
194 strcpy(path
+ len
, "/objects");
195 if (access(path
, X_OK
))
199 strcpy(path
+ len
, "/refs");
200 if (access(path
, X_OK
))
203 strcpy(path
+ len
, "/HEAD");
204 if (validate_headref(path
))
210 static int inside_git_dir
= -1;
212 int is_inside_git_dir(void)
214 if (inside_git_dir
< 0) {
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
) {
225 inside_git_dir
= !*git_dir
;
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
;
240 * If GIT_DIR is set explicitly, we're not going
241 * to do any discovery, but we still do repository
244 gitdirenv
= getenv(GIT_DIR_ENVIRONMENT
);
246 if (PATH_MAX
- 40 < strlen(gitdirenv
))
247 die("'$%s' too big", GIT_DIR_ENVIRONMENT
);
248 if (is_git_directory(gitdirenv
))
254 die("Not a git repository: '%s'", gitdirenv
);
258 if (!getcwd(cwd
, sizeof(cwd
)-1) || !(cwd
[0] == '/' || cwd
[1] == ':'))
259 die("Unable to read current working directory");
263 if (!getcwd(cwd
, sizeof(cwd
)-1) || cwd
[0] != '/')
264 die("Unable to read current working directory");
267 offset
= len
= strlen(cwd
);
269 if (is_git_directory(".git"))
273 if (offset
<= minoffset
) {
274 if (is_git_directory(cwd
)) {
276 die("Cannot come back to cwd");
277 setenv(GIT_DIR_ENVIRONMENT
, cwd
, 1);
283 die("Cannot come back to cwd");
287 die("Not a git repository");
289 } while (offset
> minoffset
&& cwd
[--offset
] != '/');
295 /* Make "offset" point to past the '/', and add a '/' at the end */
299 inside_git_dir
= !prefixcmp(cwd
+ offset
, ".git/");
303 int git_config_perm(const char *var
, const char *value
)
306 if (!strcmp(value
, "umask"))
308 if (!strcmp(value
, "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
);
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
);
336 const char *setup_git_directory(void)
338 const char *retval
= setup_git_directory_gently(NULL
);
339 check_repository_format();