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_work_tree() || 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)
215 return inside_git_dir
;
216 die("BUG: is_inside_git_dir called before setup_git_directory");
219 static int inside_work_tree
= -1;
221 int is_inside_work_tree(void)
223 if (inside_git_dir
>= 0)
224 return inside_work_tree
;
225 die("BUG: is_inside_work_tree called before setup_git_directory");
228 static char *gitworktree_config
;
230 static int git_setup_config(const char *var
, const char *value
)
232 if (!strcmp(var
, "core.worktree")) {
233 if (gitworktree_config
)
234 strlcpy(gitworktree_config
, value
, PATH_MAX
);
237 return git_default_config(var
, value
);
240 const char *setup_git_directory_gently(int *nongit_ok
)
242 static char cwd
[PATH_MAX
+1];
243 char worktree
[PATH_MAX
+1], gitdir
[PATH_MAX
+1];
244 const char *gitdirenv
, *gitworktree
;
245 int wt_rel_gitdir
= 0;
247 gitdirenv
= getenv(GIT_DIR_ENVIRONMENT
);
252 if (!getcwd(cwd
, sizeof(cwd
)-1))
253 die("Unable to read current working directory");
259 offset
= len
= strlen(cwd
);
261 if (is_git_directory(".git"))
268 while (offset
> minoffset
&& cwd
[--offset
] != '/')
273 inside_work_tree
= 1;
274 git_config(git_default_config
);
282 inside_git_dir
= !prefixcmp(cwd
+ offset
+ 1, ".git/");
283 return cwd
+ offset
+ 1;
287 die("Cannot come back to cwd");
288 if (!is_git_directory(".")) {
293 die("Not a git repository");
295 setenv(GIT_DIR_ENVIRONMENT
, cwd
, 1);
296 gitdirenv
= getenv(GIT_DIR_ENVIRONMENT
);
298 die("getenv after setenv failed");
301 if (PATH_MAX
- 40 < strlen(gitdirenv
)) {
306 die("$%s too big", GIT_DIR_ENVIRONMENT
);
308 if (!is_git_directory(gitdirenv
)) {
313 die("Not a git repository: '%s'", gitdirenv
);
316 if (!getcwd(cwd
, sizeof(cwd
)-1))
317 die("Unable to read current working directory");
318 if (chdir(gitdirenv
)) {
323 die("Cannot change directory to $%s '%s'",
324 GIT_DIR_ENVIRONMENT
, gitdirenv
);
326 if (!getcwd(gitdir
, sizeof(gitdir
)-1))
327 die("Unable to read current working directory");
329 die("Cannot come back to cwd");
332 * In case there is a work tree we may change the directory,
333 * therefore make GIT_DIR an absolute path.
335 if (gitdirenv
[0] != '/') {
336 setenv(GIT_DIR_ENVIRONMENT
, gitdir
, 1);
337 gitdirenv
= getenv(GIT_DIR_ENVIRONMENT
);
339 die("getenv after setenv failed");
340 if (PATH_MAX
- 40 < strlen(gitdirenv
)) {
345 die("$%s too big after expansion to absolute path",
346 GIT_DIR_ENVIRONMENT
);
352 inside_git_dir
= !prefixcmp(cwd
, gitdir
);
354 gitworktree
= getenv(GIT_WORK_TREE_ENVIRONMENT
);
356 gitworktree_config
= worktree
;
359 git_config(git_setup_config
);
361 gitworktree_config
= NULL
;
363 gitworktree
= worktree
;
364 if (gitworktree
&& gitworktree
[0] != '/')
368 if (wt_rel_gitdir
&& chdir(gitdirenv
))
369 die("Cannot change directory to $%s '%s'",
370 GIT_DIR_ENVIRONMENT
, gitdirenv
);
371 if (gitworktree
&& chdir(gitworktree
)) {
373 if (wt_rel_gitdir
&& chdir(cwd
))
374 die("Cannot come back to cwd");
379 die("Cannot change directory to working tree '%s'"
380 " from $%s", gitworktree
, GIT_DIR_ENVIRONMENT
);
382 die("Cannot change directory to working tree '%s'",
385 if (!getcwd(worktree
, sizeof(worktree
)-1))
386 die("Unable to read current working directory");
387 strcat(worktree
, "/");
388 inside_work_tree
= !prefixcmp(cwd
, worktree
);
390 if (gitworktree
&& inside_work_tree
&& !prefixcmp(worktree
, gitdir
) &&
391 strcmp(worktree
, gitdir
)) {
395 if (!inside_work_tree
) {
397 die("Cannot come back to cwd");
401 if (!strcmp(cwd
, worktree
))
403 return cwd
+strlen(worktree
);
406 int git_config_perm(const char *var
, const char *value
)
410 if (!strcmp(value
, "umask"))
412 if (!strcmp(value
, "group"))
414 if (!strcmp(value
, "all") ||
415 !strcmp(value
, "world") ||
416 !strcmp(value
, "everybody"))
417 return PERM_EVERYBODY
;
422 return git_config_bool(var
, value
);
425 int check_repository_format_version(const char *var
, const char *value
)
427 if (strcmp(var
, "core.repositoryformatversion") == 0)
428 repository_format_version
= git_config_int(var
, value
);
429 else if (strcmp(var
, "core.sharedrepository") == 0)
430 shared_repository
= git_config_perm(var
, value
);
434 int check_repository_format(void)
436 git_config(check_repository_format_version
);
437 if (GIT_REPO_VERSION
< repository_format_version
)
438 die ("Expected git repo version <= %d, found %d",
439 GIT_REPO_VERSION
, repository_format_version
);
443 const char *setup_git_directory(void)
445 const char *retval
= setup_git_directory_gently(NULL
);
446 check_repository_format();