git_path(): be aware of file relocation in $GIT_DIR
We allow the user to relocate certain paths out of $GIT_DIR via
environment variables, e.g. GIT_OBJECT_DIRECTORY, GIT_INDEX_FILE and
GIT_GRAFT_FILE. Callers are not supposed to use git_path() or
git_pathdup() to get those paths. Instead they must use
get_object_directory(), get_index_file() and get_graft_file()
respectively. This is inconvenient and could be missed in review (for
example, there's git_path("objects/info/alternates") somewhere in
sha1_file.c).
This patch makes git_path() and git_pathdup() understand those
environment variables. So if you set GIT_OBJECT_DIRECTORY to /foo/bar,
git_path("objects/abc") should return /foo/bar/abc. The same is done
for the two remaining env variables.
"git rev-parse --git-path" is the wrapper for script use.
This patch kinda reverts a0279e1
(setup_git_env: use git_pathdup
instead of xmalloc + sprintf - 2014-06-19) because using git_pathdup
here would result in infinite recursion:
setup_git_env() -> git_pathdup("objects") -> .. -> adjust_git_path()
-> get_object_directory() -> oops, git_object_directory is NOT set
yet -> setup_git_env()
I wanted to make git_pathdup_literal() that skips adjust_git_path().
But that won't work because later on when $GIT_COMMON_DIR is
introduced, git_pathdup_literal("objects") needs adjust_git_path() to
replace $GIT_DIR with $GIT_COMMON_DIR.
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:

committed by
Junio C Hamano

parent
57a23b770a
commit
557bd833bb
@ -83,6 +83,7 @@ static size_t namespace_len;
|
||||
|
||||
static const char *git_dir;
|
||||
static char *git_object_dir, *git_index_file, *git_graft_file;
|
||||
int git_db_env, git_index_env, git_graft_env;
|
||||
|
||||
/*
|
||||
* Repository-local GIT_* environment variables; see cache.h for details.
|
||||
@ -124,10 +125,18 @@ static char *expand_namespace(const char *raw_namespace)
|
||||
return strbuf_detach(&buf, NULL);
|
||||
}
|
||||
|
||||
static char *git_path_from_env(const char *envvar, const char *path)
|
||||
static char *git_path_from_env(const char *envvar, const char *path,
|
||||
int *fromenv)
|
||||
{
|
||||
const char *value = getenv(envvar);
|
||||
return value ? xstrdup(value) : git_pathdup("%s", path);
|
||||
if (!value) {
|
||||
char *buf = xmalloc(strlen(git_dir) + strlen(path) + 2);
|
||||
sprintf(buf, "%s/%s", git_dir, path);
|
||||
return buf;
|
||||
}
|
||||
if (fromenv)
|
||||
*fromenv = 1;
|
||||
return xstrdup(value);
|
||||
}
|
||||
|
||||
static void setup_git_env(void)
|
||||
@ -140,9 +149,9 @@ static void setup_git_env(void)
|
||||
git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
|
||||
gitfile = read_gitfile(git_dir);
|
||||
git_dir = xstrdup(gitfile ? gitfile : git_dir);
|
||||
git_object_dir = git_path_from_env(DB_ENVIRONMENT, "objects");
|
||||
git_index_file = git_path_from_env(INDEX_ENVIRONMENT, "index");
|
||||
git_graft_file = git_path_from_env(GRAFT_ENVIRONMENT, "info/grafts");
|
||||
git_object_dir = git_path_from_env(DB_ENVIRONMENT, "objects", &git_db_env);
|
||||
git_index_file = git_path_from_env(INDEX_ENVIRONMENT, "index", &git_index_env);
|
||||
git_graft_file = git_path_from_env(GRAFT_ENVIRONMENT, "info/grafts", &git_graft_env);
|
||||
if (getenv(NO_REPLACE_OBJECTS_ENVIRONMENT))
|
||||
check_replace_refs = 0;
|
||||
namespace = expand_namespace(getenv(GIT_NAMESPACE_ENVIRONMENT));
|
||||
|
Reference in New Issue
Block a user