4 static int inside_git_dir
= -1;
5 static int inside_work_tree
= -1;
8 static inline int is_dir_sep(char c
) { return c
== '/' || c
== '\\'; }
10 static inline int is_dir_sep(char c
) { return c
== '/'; }
13 const char *prefix_path(const char *prefix
, int len
, const char *path
)
15 const char *orig
= path
;
37 else if (is_dir_sep(c
))
42 /* Remove last component of the prefix */
45 die("'%s' is outside repository", orig
);
47 } while (len
&& !is_dir_sep(prefix
[len
-1]));
52 /* we want to convert '\' in path to '/' (prefix already has '/') */
55 while (!do_pfx
&& *p
) {
56 do_pfx
= *p
++ == '\\';
61 int speclen
= strlen(path
);
62 char *n
= xmalloc(speclen
+ len
+ 1);
65 memcpy(n
, prefix
, len
);
66 memcpy(n
+ len
, path
, speclen
+1);
68 for (p
= n
+ len
; *p
; p
++)
78 * Unlike prefix_path, this should be used if the named file does
79 * not have to interact with index entry; i.e. name of a random file
82 const char *prefix_filename(const char *pfx
, int pfx_len
, const char *arg
)
84 static char path
[PATH_MAX
];
87 if (!pfx
|| !*pfx
|| arg
[0] == '/')
90 /* don't add prefix to absolute paths */
91 if (is_absolute_path(arg
))
95 memcpy(path
, pfx
, pfx_len
);
96 strcpy(path
+ pfx_len
, arg
);
97 for (p
= path
+ pfx_len
; *p
; p
++)
104 * Verify a filename that we got as an argument for a pathspec
105 * entry. Note that a filename that begins with "-" never verifies
106 * as true, because even if such a filename were to exist, we want
107 * it to be preceded by the "--" marker (or we want the user to
108 * use a format like "./-filename")
110 void verify_filename(const char *prefix
, const char *arg
)
116 die("bad flag '%s' used after filename", arg
);
117 name
= prefix
? prefix_filename(prefix
, strlen(prefix
), arg
) : arg
;
118 if (!lstat(name
, &st
))
121 die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
122 "Use '--' to separate paths from revisions", arg
);
123 die("'%s': %s", arg
, strerror(errno
));
127 * Opposite of the above: the command line did not have -- marker
128 * and we parsed the arg as a refname. It should not be interpretable
131 void verify_non_filename(const char *prefix
, const char *arg
)
136 if (!is_inside_work_tree() || is_inside_git_dir())
140 name
= prefix
? prefix_filename(prefix
, strlen(prefix
), arg
) : arg
;
141 if (!lstat(name
, &st
))
142 die("ambiguous argument '%s': both revision and filename\n"
143 "Use '--' to separate filenames from revisions", arg
);
144 if (errno
!= ENOENT
&& errno
!= ENOTDIR
)
145 die("'%s': %s", arg
, strerror(errno
));
148 const char **get_pathspec(const char *prefix
, const char **pathspec
)
150 const char *entry
= *pathspec
;
154 if (!prefix
&& !entry
)
158 static const char *spec
[2];
164 /* Otherwise we have to re-write the entries.. */
166 prefixlen
= prefix
? strlen(prefix
) : 0;
168 *p
= prefix_path(prefix
, prefixlen
, entry
);
169 } while ((entry
= *++p
) != NULL
);
170 return (const char **) pathspec
;
174 * Test if it looks like we're at a git directory.
177 * - either a objects/ directory _or_ the proper
178 * GIT_OBJECT_DIRECTORY environment variable
179 * - a refs/ directory
180 * - either a HEAD symlink or a HEAD file that is formatted as
181 * a proper "ref:", or a regular file HEAD that has a properly
182 * formatted sha1 object name.
184 static int is_git_directory(const char *suspect
)
187 size_t len
= strlen(suspect
);
189 strcpy(path
, suspect
);
190 if (getenv(DB_ENVIRONMENT
)) {
191 if (access(getenv(DB_ENVIRONMENT
), X_OK
))
195 strcpy(path
+ len
, "/objects");
196 if (access(path
, X_OK
))
200 strcpy(path
+ len
, "/refs");
201 if (access(path
, X_OK
))
204 strcpy(path
+ len
, "/HEAD");
205 if (validate_headref(path
))
211 int is_inside_git_dir(void)
213 if (inside_git_dir
< 0)
214 inside_git_dir
= is_inside_dir(get_git_dir());
215 return inside_git_dir
;
218 int is_inside_work_tree(void)
220 if (inside_work_tree
< 0)
221 inside_work_tree
= is_inside_dir(get_git_work_tree());
222 return inside_work_tree
;
226 * set_work_tree() is only ever called if you set GIT_DIR explicitely.
227 * The old behaviour (which we retain here) is to set the work tree root
228 * to the cwd, unless overridden by the config, the command line, or
231 static const char *set_work_tree(const char *dir
)
233 char buffer
[PATH_MAX
+ 1];
235 if (!getcwd(buffer
, sizeof(buffer
)))
236 die ("Could not get the current working directory");
237 git_work_tree_cfg
= xstrdup(buffer
);
238 inside_work_tree
= 1;
243 void setup_work_tree(void)
245 const char *work_tree
, *git_dir
;
246 static int initialized
= 0;
250 work_tree
= get_git_work_tree();
251 git_dir
= get_git_dir();
252 if (!is_absolute_path(git_dir
))
253 set_git_dir(make_absolute_path(git_dir
));
254 if (!work_tree
|| chdir(work_tree
))
255 die("This operation must be run in a work tree");
260 * We cannot decide in this function whether we are in the work tree or
261 * not, since the config can only be read _after_ this function was called.
263 const char *setup_git_directory_gently(int *nongit_ok
)
265 const char *work_tree_env
= getenv(GIT_WORK_TREE_ENVIRONMENT
);
266 static char cwd
[PATH_MAX
+1];
267 const char *gitdirenv
;
272 * If GIT_DIR is set explicitly, we're not going
273 * to do any discovery, but we still do repository
276 gitdirenv
= getenv(GIT_DIR_ENVIRONMENT
);
278 if (PATH_MAX
- 40 < strlen(gitdirenv
))
279 die("'$%s' too big", GIT_DIR_ENVIRONMENT
);
280 if (is_git_directory(gitdirenv
)) {
281 static char buffer
[1024 + 1];
285 return set_work_tree(gitdirenv
);
286 retval
= get_relative_cwd(buffer
, sizeof(buffer
) - 1,
287 get_git_work_tree());
288 if (!retval
|| !*retval
)
290 set_git_dir(make_absolute_path(gitdirenv
));
291 if (chdir(work_tree_env
) < 0)
292 die ("Could not chdir to %s", work_tree_env
);
300 die("Not a git repository: '%s'", gitdirenv
);
303 if (!getcwd(cwd
, sizeof(cwd
)-1))
304 die("Unable to read current working directory");
311 * Test in the following order (relative to the cwd):
319 offset
= len
= strlen(cwd
);
321 if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT
))
323 if (is_git_directory(".")) {
326 inside_work_tree
= 0;
327 setenv(GIT_DIR_ENVIRONMENT
, ".", 1);
332 if (offset
<= minoffset
) {
335 die("Cannot come back to cwd");
339 die("Not a git repository");
341 } while (offset
> minoffset
&& cwd
[--offset
] != '/');
346 inside_work_tree
= 1;
347 git_work_tree_cfg
= xstrndup(cwd
, offset
);
351 /* Make "offset" point to past the '/', and add a '/' at the end */
358 int git_config_perm(const char *var
, const char *value
)
362 if (!strcmp(value
, "umask"))
364 if (!strcmp(value
, "group"))
366 if (!strcmp(value
, "all") ||
367 !strcmp(value
, "world") ||
368 !strcmp(value
, "everybody"))
369 return PERM_EVERYBODY
;
374 return git_config_bool(var
, value
);
377 int check_repository_format_version(const char *var
, const char *value
)
379 if (strcmp(var
, "core.repositoryformatversion") == 0)
380 repository_format_version
= git_config_int(var
, value
);
381 else if (strcmp(var
, "core.sharedrepository") == 0)
382 shared_repository
= git_config_perm(var
, value
);
383 else if (strcmp(var
, "core.bare") == 0) {
384 is_bare_repository_cfg
= git_config_bool(var
, value
);
385 if (is_bare_repository_cfg
== 1)
386 inside_work_tree
= -1;
387 } else if (strcmp(var
, "core.worktree") == 0) {
388 if (git_work_tree_cfg
)
389 free(git_work_tree_cfg
);
390 git_work_tree_cfg
= xstrdup(value
);
391 inside_work_tree
= -1;
396 int check_repository_format(void)
398 git_config(check_repository_format_version
);
399 if (GIT_REPO_VERSION
< repository_format_version
)
400 die ("Expected git repo version <= %d, found %d",
401 GIT_REPO_VERSION
, repository_format_version
);
405 const char *setup_git_directory(void)
407 const char *retval
= setup_git_directory_gently(NULL
);
408 check_repository_format();
410 /* If the work tree is not the default one, recompute prefix */
411 if (inside_work_tree
< 0) {
412 static char buffer
[PATH_MAX
+ 1];
414 if (retval
&& chdir(retval
))
415 die ("Could not jump back into original cwd");
416 rel
= get_relative_cwd(buffer
, PATH_MAX
, get_git_work_tree());
417 return rel
&& *rel
? strcat(rel
, "/") : NULL
;