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
)
9 const char *orig
= path
;
35 /* Remove last component of the prefix */
38 die("'%s' is outside repository", orig
);
40 } while (len
&& prefix
[len
-1] != '/');
44 int speclen
= strlen(path
);
45 char *n
= xmalloc(speclen
+ len
+ 1);
47 memcpy(n
, prefix
, len
);
48 memcpy(n
+ len
, path
, speclen
+1);
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
59 const char *prefix_filename(const char *pfx
, int pfx_len
, const char *arg
)
61 static char path
[PATH_MAX
];
62 if (!pfx
|| !*pfx
|| arg
[0] == '/')
64 memcpy(path
, pfx
, pfx_len
);
65 strcpy(path
+ pfx_len
, arg
);
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")
76 void verify_filename(const char *prefix
, const char *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
))
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
));
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
97 void verify_non_filename(const char *prefix
, const char *arg
)
102 if (!is_inside_work_tree() || is_inside_git_dir())
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
);
111 die("'%s': %s", arg
, strerror(errno
));
114 const char **get_pathspec(const char *prefix
, const char **pathspec
)
116 const char *entry
= *pathspec
;
120 if (!prefix
&& !entry
)
124 static const char *spec
[2];
130 /* Otherwise we have to re-write the entries.. */
132 prefixlen
= prefix
? strlen(prefix
) : 0;
134 *p
= prefix_path(prefix
, prefixlen
, entry
);
135 } while ((entry
= *++p
) != NULL
);
136 return (const char **) pathspec
;
140 * Test if it looks like we're at a git directory.
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.
150 static int is_git_directory(const char *suspect
)
153 size_t len
= strlen(suspect
);
155 strcpy(path
, suspect
);
156 if (getenv(DB_ENVIRONMENT
)) {
157 if (access(getenv(DB_ENVIRONMENT
), X_OK
))
161 strcpy(path
+ len
, "/objects");
162 if (access(path
, X_OK
))
166 strcpy(path
+ len
, "/refs");
167 if (access(path
, X_OK
))
170 strcpy(path
+ len
, "/HEAD");
171 if (validate_headref(path
))
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
;
192 * If no worktree was given, and we are outside of a default work tree,
193 * now is the time to set it.
195 * In other words, if the user calls git with something like
197 * git --git-dir=/some/where/else/.git bla
199 * default to /some/where/else as working directory; if the specified
200 * git-dir does not end in "/.git", the cwd is used as working directory.
202 const char *set_work_tree(const char *dir
)
204 char dir_buffer
[PATH_MAX
], *rel
= NULL
;
205 static char buffer
[PATH_MAX
+ 1];
206 int len
, suffix_len
= strlen(DEFAULT_GIT_DIR_ENVIRONMENT
) + 1;
208 /* strip the variable 'dir' of the postfix "/.git" if it has it */
210 if (len
> suffix_len
&&
211 !strcmp(dir
+ len
- suffix_len
, "/" DEFAULT_GIT_DIR_ENVIRONMENT
)) {
212 if ((len
- suffix_len
) >= sizeof(dir_buffer
))
213 die("directory name too long");
214 memcpy(dir_buffer
, dir
, len
- suffix_len
);
215 dir_buffer
[len
- suffix_len
] = '\0';
217 /* are we inside the default work tree? */
218 rel
= get_relative_cwd(buffer
, sizeof(buffer
), dir_buffer
);
221 /* if rel is set, the cwd is _not_ the current working tree */
223 if (!is_absolute_path(dir
))
224 set_git_dir(make_absolute_path(dir
));
227 die("cannot chdir to %s: %s", dir
, strerror(errno
));
233 dir
= getcwd(buffer
, sizeof(buffer
));
235 git_work_tree_cfg
= xstrdup(dir
);
236 inside_work_tree
= 1;
242 * We cannot decide in this function whether we are in the work tree or
243 * not, since the config can only be read _after_ this function was called.
245 const char *setup_git_directory_gently(int *nongit_ok
)
247 const char *work_tree_env
= getenv(GIT_WORK_TREE_ENVIRONMENT
);
248 static char cwd
[PATH_MAX
+1];
249 const char *gitdirenv
;
253 * If GIT_DIR is set explicitly, we're not going
254 * to do any discovery, but we still do repository
257 gitdirenv
= getenv(GIT_DIR_ENVIRONMENT
);
259 if (PATH_MAX
- 40 < strlen(gitdirenv
))
260 die("'$%s' too big", GIT_DIR_ENVIRONMENT
);
261 if (is_git_directory(gitdirenv
)) {
263 return set_work_tree(gitdirenv
);
270 die("Not a git repository: '%s'", gitdirenv
);
273 if (!getcwd(cwd
, sizeof(cwd
)-1))
274 die("Unable to read current working directory");
277 * Test in the following order (relative to the cwd):
285 offset
= len
= strlen(cwd
);
287 if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT
))
289 if (is_git_directory(".")) {
292 inside_work_tree
= 0;
293 setenv(GIT_DIR_ENVIRONMENT
, ".", 1);
301 die("Cannot come back to cwd");
305 die("Not a git repository");
307 } while (cwd
[--offset
] != '/');
312 inside_work_tree
= 1;
313 git_work_tree_cfg
= xstrndup(cwd
, offset
);
317 /* Make "offset" point to past the '/', and add a '/' at the end */
324 int git_config_perm(const char *var
, const char *value
)
328 if (!strcmp(value
, "umask"))
330 if (!strcmp(value
, "group"))
332 if (!strcmp(value
, "all") ||
333 !strcmp(value
, "world") ||
334 !strcmp(value
, "everybody"))
335 return PERM_EVERYBODY
;
340 return git_config_bool(var
, value
);
343 int check_repository_format_version(const char *var
, const char *value
)
345 if (strcmp(var
, "core.repositoryformatversion") == 0)
346 repository_format_version
= git_config_int(var
, value
);
347 else if (strcmp(var
, "core.sharedrepository") == 0)
348 shared_repository
= git_config_perm(var
, value
);
349 else if (strcmp(var
, "core.bare") == 0) {
350 is_bare_repository_cfg
= git_config_bool(var
, value
);
351 if (is_bare_repository_cfg
== 1)
352 inside_work_tree
= -1;
353 } else if (strcmp(var
, "core.worktree") == 0) {
354 if (git_work_tree_cfg
)
355 free(git_work_tree_cfg
);
356 git_work_tree_cfg
= xstrdup(value
);
357 inside_work_tree
= -1;
362 int check_repository_format(void)
364 git_config(check_repository_format_version
);
365 if (GIT_REPO_VERSION
< repository_format_version
)
366 die ("Expected git repo version <= %d, found %d",
367 GIT_REPO_VERSION
, repository_format_version
);
371 const char *setup_git_directory(void)
373 const char *retval
= setup_git_directory_gently(NULL
);
374 check_repository_format();
376 /* If the work tree is not the default one, recompute prefix */
377 if (inside_work_tree
< 0) {
378 static char buffer
[PATH_MAX
+ 1];
380 if (retval
&& chdir(retval
))
381 die ("Could not jump back into original cwd");
382 rel
= get_relative_cwd(buffer
, PATH_MAX
, get_git_work_tree());
383 return rel
&& *rel
? strcat(rel
, "/") : NULL
;