git_path(): be aware of file relocation in $GIT_DIR
commitb8a47af14706699d105489c1408d0556a84f5c2e
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>
Sun, 13 Jul 2014 04:50:43 +0000 (13 11:50 +0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 14 Jul 2014 01:26:53 +0000 (13 18:26 -0700)
treee0c5ae75e76360cf257d5c2dcf191f5cb397c07b
parentaa743c12271b06957e7e7e68bddd8e7b837a232b
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>
Documentation/git-rev-parse.txt
builtin/rev-parse.c
cache.h
environment.c
path.c
t/t0060-path-utils.sh