path.c: make get_pathname() return strbuf instead of static buffer

We've been avoiding PATH_MAX whenever possible. This patch makes
get_pathname() return a strbuf and updates the callers to take
advantage of this. The code is simplified as we no longer need to
worry about buffer overflow.
Convert git_snpath() to strbuf_git_path()

In the previous patch, git_snpath() is modified to take a strbuf
buffer from get_pathname() because vsnpath() needs that. But that
makes it awkward because git_snpath() receives a pre-allocated buffer
from outside and has to copy data back.

Rename it to strbuf_git_path() and make it receive strbuf directly.
path.c: rename vsnpath() to do_git_path()

The name vsnpath() gives an impression that this is general path
handling function. It's not. This is the underlying implementation of
git_path(), git_pathdup() and strbuf_git_path() which will prefix
$GIT_DIR in the result string.
path.c: group git_path(), git_pathdup() and strbuf_git_path() together
Make git_path() 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. All 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
(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 /tmp/bar/abc. The same is done
for the two remaining env variables.

"git rev-parse --git-path" is the wrapper of this function for scripts
to use. The patch alsos fixes shell scripts not respecting
$GIT_INDEX_FILE
reflog: avoid constructing .lock path with git_path

git_path() understands the path given to it. Unrecognized paths by
default go to $GIT_SUPER_DIR/repos/<id>/... in split-repo mode. We
want the %s.lock file to be at the same place %s file is. Avoid
git_path() in this case and use log_file as %s, which should contain
the final path of %s file.
fast-import: use git_path() for accessing .git dir instead of get_git_dir()

This allows git_path() to redirect info/fast-import to another place
if needed
commit: use SEQ_DIR instead of hardcoding "sequencer"
Add new environment variable $GIT_SUPER_DIR

If $GIT_SUPER_DIR is defined, the repository is split into two places:

 - HEAD, logs/HEAD, index, other top-level refs and unrecognized files
   are from $GIT_DIR

 - the rest like objects, refs, info, hooks, packed-refs, shallow...
   are from $GIT_SUPER_DIR

 - $GIT_DIR/shared is the new thing, intended for third party commands
   because if they put things any unknown places, they will be
   considered worktree-specific and are only visible by one repo

Scripts are supposed to retrieve paths in $GIT_DIR with
"git rev-parse --git-path" if they may belong to $GIT_SUPER_DIR.

Note that logs/refs/.tmp-renamed-log is used to prepare new reflog
entry and it's supposed to be on the same filesystem as the target
reflog file. This is not guaranteed true for logs/HEAD when it's
mapped to repos/xx/logs/HEAD because the user can put "repos"
directory on different filesystem. Don't mess with .git unless you
know what you're doing.

The redirection is done by git_path(), git_pathdup() and
strbuf_git_path(). The selected list of paths goes to $GIT_SUPER_DIR,
not the other way around in case a developer adds a new
worktree-specific file and it's accidentally promoted to be shared
across repositories (this includes unknown files added by third party
commands)

The list of known files that belong to $GIT_DIR are:

ADD_EDIT.patch BISECT_ANCESTORS_OK BISECT_EXPECTED_REV BISECT_LOG
BISECT_NAMES CHERRY_PICK_HEAD COMMIT_MSG FETCH_HEAD HEAD MERGE_HEAD
MERGE_MODE MERGE_RR NOTES_EDITMSG NOTES_MERGE_WORKTREE ORIG_HEAD
REVERT_HEAD SQUASH_MSG TAG_EDITMSG fast_import_crash_* logs/HEAD
next-index-* rebase-apply rebase-merge rsync-refs-* sequencer/*
shallow_*

The list of those belonging to $GIT_SUPER_DIR is a bit shorter and
hard coded in path.c:update_super_dir().

Path mapping is NOT done for git_path_submodule(). Split-repos are not
supported as submodules.
git-sh-setup.sh: use rev-parse --git-path to get $GIT_DIR/objects

If $GIT_SUPER_DIR is set, $GIT_OBJECT_DIRECTORY should be
$GIT_SUPER_DIR/objects. Just let rev-parse --git-path handle it.
*.sh: avoid hardcoding $GIT_DIR/hooks/...

If $GIT_SUPER_DIR is defined, it should become
$GIT_SUPER_DIR/hooks/... Just let rev-parse --git-path handle it.
git-stash: avoid hardcoding $GIT_DIR/logs/....
setup.c: refactor path manipulation out of read_gitfile()
setup.c: add split-repo support to is_git_directory()

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
setup.c: add split-repo support to .git files

If a .git file contains

gitsuper: <path>
gitdir: <id>

then we set GIT_SUPER_DIR to <path> and GIT_DIR to
$GIT_SUPER_DIR/repos/<id>.
setup.c: reduce cleanup sites in setup_explicit_git_dir()

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
environment.c: support super .git file specified by $GIT_DIR
setup: support $GIT_SUPER_DIR as well as super .git files

The same rules for git repo setup apply except:

- detect super .git files, set $GIT_SUPER_DIR accordingly

- if $GIT_SUPER_DIR is set (or specified by super .git files), look
  for non-worktree stuff in this directory instead of $GIT_DIR. This
  mostly affects is_git_directory() and check_repository_format()

- the worktree setting precedence goes from lower to higher:
  core.worktree, GIT_SUPER_DIR then GIT_WORK_TREE
git_path: visibility of worktree information from other worktrees

This makes "repos" a shared repository, which means all split-repos
can see HEAD of others. HEAD of the original worktree is not in
"repos" and can't be seen this way. Original worktree is mapped to
"super" instead.
wrapper.c: wrapper to open a file, fprintf then close