3 const char *prefix_path(const char *prefix
, int len
, const char *path
)
5 const char *orig
= path
;
31 /* Remove last component of the prefix */
34 die("'%s' is outside repository", orig
);
36 } while (len
&& prefix
[len
-1] != '/');
40 int speclen
= strlen(path
);
41 char *n
= xmalloc(speclen
+ len
+ 1);
43 memcpy(n
, prefix
, len
);
44 memcpy(n
+ len
, path
, speclen
+1);
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
55 const char *prefix_filename(const char *pfx
, int pfx_len
, const char *arg
)
57 static char path
[PATH_MAX
];
58 if (!pfx
|| !*pfx
|| arg
[0] == '/')
60 memcpy(path
, pfx
, pfx_len
);
61 strcpy(path
+ pfx_len
, arg
);
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")
72 void verify_filename(const char *prefix
, const char *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
))
83 die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
84 "Use '--' to separate paths from revisions", arg
);
85 die("'%s': %s", arg
, strerror(errno
));
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
93 void verify_non_filename(const char *prefix
, const char *arg
)
98 if (is_inside_git_dir())
102 name
= prefix
? prefix_filename(prefix
, strlen(prefix
), arg
) : arg
;
103 if (!lstat(name
, &st
))
104 die("ambiguous argument '%s': both revision and filename\n"
105 "Use '--' to separate filenames from revisions", arg
);
107 die("'%s': %s", arg
, strerror(errno
));
110 const char **get_pathspec(const char *prefix
, const char **pathspec
)
112 const char *entry
= *pathspec
;
116 if (!prefix
&& !entry
)
120 static const char *spec
[2];
126 /* Otherwise we have to re-write the entries.. */
128 prefixlen
= prefix
? strlen(prefix
) : 0;
130 *p
= prefix_path(prefix
, prefixlen
, entry
);
131 } while ((entry
= *++p
) != NULL
);
132 return (const char **) pathspec
;
136 * Test if it looks like we're at a git directory.
139 * - either a objects/ directory _or_ the proper
140 * GIT_OBJECT_DIRECTORY environment variable
141 * - a refs/ directory
142 * - either a HEAD symlink or a HEAD file that is formatted as
143 * a proper "ref:", or a regular file HEAD that has a properly
144 * formatted sha1 object name.
146 static int is_git_directory(const char *suspect
)
149 size_t len
= strlen(suspect
);
151 strcpy(path
, suspect
);
152 if (getenv(DB_ENVIRONMENT
)) {
153 if (access(getenv(DB_ENVIRONMENT
), X_OK
))
157 strcpy(path
+ len
, "/objects");
158 if (access(path
, X_OK
))
162 strcpy(path
+ len
, "/refs");
163 if (access(path
, X_OK
))
166 strcpy(path
+ len
, "/HEAD");
167 if (validate_headref(path
))
173 static int inside_git_dir
= -1;
175 int is_inside_git_dir(void)
177 if (inside_git_dir
>= 0)
178 return inside_git_dir
;
179 die("BUG: is_inside_git_dir called before setup_git_directory");
182 static int inside_work_tree
= -1;
184 int is_inside_work_tree(void)
186 if (inside_git_dir
>= 0)
187 return inside_work_tree
;
188 die("BUG: is_inside_work_tree called before setup_git_directory");
191 static char *gitworktree_config
;
193 static int git_setup_config(const char *var
, const char *value
)
195 if (!strcmp(var
, "core.worktree")) {
196 if (gitworktree_config
)
197 strlcpy(gitworktree_config
, value
, PATH_MAX
);
200 return git_default_config(var
, value
);
203 const char *setup_git_directory_gently(int *nongit_ok
)
205 static char cwd
[PATH_MAX
+1];
206 char worktree
[PATH_MAX
+1], gitdir
[PATH_MAX
+1];
207 const char *gitdirenv
, *gitworktree
;
208 int wt_rel_gitdir
= 0;
210 gitdirenv
= getenv(GIT_DIR_ENVIRONMENT
);
214 if (!getcwd(cwd
, sizeof(cwd
)-1) || cwd
[0] != '/')
215 die("Unable to read current working directory");
217 offset
= len
= strlen(cwd
);
219 if (is_git_directory(".git"))
226 while (cwd
[--offset
] != '/')
231 inside_work_tree
= 1;
232 git_config(git_default_config
);
240 inside_git_dir
= !prefixcmp(cwd
+ offset
+ 1, ".git/");
241 return cwd
+ offset
+ 1;
245 die("Cannot come back to cwd");
246 if (!is_git_directory(".")) {
251 die("Not a git repository");
253 setenv(GIT_DIR_ENVIRONMENT
, cwd
, 1);
256 if (PATH_MAX
- 40 < strlen(gitdirenv
)) {
261 die("$%s too big", GIT_DIR_ENVIRONMENT
);
263 if (!is_git_directory(gitdirenv
)) {
268 die("Not a git repository: '%s'", gitdirenv
);
271 if (!getcwd(cwd
, sizeof(cwd
)-1) || cwd
[0] != '/')
272 die("Unable to read current working directory");
273 if (chdir(gitdirenv
)) {
278 die("Cannot change directory to $%s '%s'",
279 GIT_DIR_ENVIRONMENT
, gitdirenv
);
281 if (!getcwd(gitdir
, sizeof(gitdir
)-1) || gitdir
[0] != '/')
282 die("Unable to read current working directory");
284 die("Cannot come back to cwd");
287 * In case there is a work tree we may change the directory,
288 * therefore make GIT_DIR an absolute path.
290 if (gitdirenv
[0] != '/') {
291 setenv(GIT_DIR_ENVIRONMENT
, gitdir
, 1);
292 gitdirenv
= getenv(GIT_DIR_ENVIRONMENT
);
293 if (PATH_MAX
- 40 < strlen(gitdirenv
)) {
298 die("$%s too big after expansion to absolute path",
299 GIT_DIR_ENVIRONMENT
);
305 inside_git_dir
= !prefixcmp(cwd
, gitdir
);
307 gitworktree
= getenv(GIT_WORK_TREE_ENVIRONMENT
);
309 gitworktree_config
= worktree
;
312 git_config(git_setup_config
);
314 gitworktree_config
= NULL
;
316 gitworktree
= worktree
;
317 if (gitworktree
&& gitworktree
[0] != '/')
321 if (wt_rel_gitdir
&& chdir(gitdirenv
))
322 die("Cannot change directory to $%s '%s'",
323 GIT_DIR_ENVIRONMENT
, gitdirenv
);
324 if (gitworktree
&& chdir(gitworktree
)) {
326 if (wt_rel_gitdir
&& chdir(cwd
))
327 die("Cannot come back to cwd");
332 die("Cannot change directory to working tree '%s'"
333 " from $%s", gitworktree
, GIT_DIR_ENVIRONMENT
);
335 die("Cannot change directory to working tree '%s'",
338 if (!getcwd(worktree
, sizeof(worktree
)-1) || worktree
[0] != '/')
339 die("Unable to read current working directory");
340 strcat(worktree
, "/");
341 inside_work_tree
= !prefixcmp(cwd
, worktree
);
343 if (gitworktree
&& inside_work_tree
&& !prefixcmp(worktree
, gitdir
) &&
344 strcmp(worktree
, gitdir
)) {
348 if (!inside_work_tree
) {
350 die("Cannot come back to cwd");
354 if (!strcmp(cwd
, worktree
))
356 return cwd
+strlen(worktree
);
359 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
;
371 return git_config_bool(var
, value
);
374 int check_repository_format_version(const char *var
, const char *value
)
376 if (strcmp(var
, "core.repositoryformatversion") == 0)
377 repository_format_version
= git_config_int(var
, value
);
378 else if (strcmp(var
, "core.sharedrepository") == 0)
379 shared_repository
= git_config_perm(var
, value
);
383 int check_repository_format(void)
385 git_config(check_repository_format_version
);
386 if (GIT_REPO_VERSION
< repository_format_version
)
387 die ("Expected git repo version <= %d, found %d",
388 GIT_REPO_VERSION
, repository_format_version
);
392 const char *setup_git_directory(void)
394 const char *retval
= setup_git_directory_gently(NULL
);
395 check_repository_format();