Add new environment variable $GIT_COMMON_DIR

This variable is intended to support multiple working directories
attached to a repository. Such a repository may have a main working
directory, created by either "git init" or "git clone" and one or more
linked working directories. These working directories and the main
repository share the same repository directory.

In linked working directories, $GIT_COMMON_DIR must be defined to point
to the real repository directory and $GIT_DIR points to an unused
subdirectory inside $GIT_COMMON_DIR. File locations inside the
repository are reorganized from the linked worktree view point:

 - worktree-specific such as 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_COMMON_DIR

Scripts are supposed to retrieve paths in $GIT_DIR with "git rev-parse
--git-path", which will take care of "$GIT_DIR vs $GIT_COMMON_DIR"
business.

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_COMMON_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_*

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

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

If $GIT_COMMON_DIR is set, it should be $GIT_COMMON_DIR/hooks/, not
$GIT_DIR/hooks/. Just let rev-parse --git-path handle it.
git-stash: avoid hardcoding $GIT_DIR/logs/....
setup.c: convert is_git_directory() to use strbuf
setup.c: detect $GIT_COMMON_DIR in is_git_directory()

If the file "$GIT_DIR/commondir" exists, it contains the value of
$GIT_COMMON_DIR.
setup.c: convert check_repository_format_gently to use strbuf
setup.c: detect $GIT_COMMON_DIR check_repository_format_gently()
setup.c: support multi-checkout repo setup

The repo setup procedure is updated to detect $GIT_DIR/commondir and
set $GIT_COMMON_DIR properly.

The core.worktree is ignored when $GIT_DIR/commondir presents. This is
because "commondir" repos are intended for separate/linked checkouts
and pointing them back to a fixed core.worktree just does not make
sense.
wrapper.c: wrapper to open a file, fprintf then close
use new wrapper write_file() for simple file writing

This fixes common problems in these code about error handling,
forgetting to close the file handle after fprintf() fails, or not
printing out the error string..
checkout: support checking out into a new working directory

"git checkout --to" sets up a new working directory with a .git file
pointing to $GIT_DIR/repos/<id>. It then executes "git checkout" again
on the new worktree with the same arguments except "--to" is taken
out. The second checkout execution, which is not tamed with any info
from the current repository, will actually check out and everything
that normal "git checkout" does.

The end result is similar to "git stash" except that you don't have to
stash anything. Instead of switching to another branch temporarily
then back (and loses all timestamps), you can just create a separate
checkout, do your thing there then delete it.
checkout: clean up half-prepared directories in --to mode
git_path: visibility of worktree information from other worktrees

This makes "repos" a shared repository, which means all linked
checkouts can see HEAD of others. HEAD of the main checkout is not in
"repos" and can't be seen this way. main checkout is mapped to "1ST_"
prefix instead (e.g. HEAD -> 1ST_HEAD, logs/HEAD -> 1ST_logs/HEAD).
path.c: rewrite reflog path for refs repos/<id>/HEAD and 1ST_HEAD

The normal rule is given ref "abc", its reflog path is
"logs/abc". It's not true for "repos/<id>/HEAD" because the correct
reflog path is "repos/<id>/logs/HEAD", not "logs/repos/<id>/HEAD".
The same rule applies to "1ST_HEAD".
refs.c: create repos/xxx/logs before writing repos/xxx/logs/HEAD
refs.c: detach HEAD when a ref is updated/deleted from another worktree

Between error+reject update and detach, the latter seems less
intrusive on the current repo's operations. People may be doing an
interactive rebase and only realize at the last step that the rebased
ref can't be updated because it's checked out elsewhere.

This is effective when a ref is updated anywhere, including from the
main checkout as well as all linked ones, because "repos" directory is
shared between all repos. The user will be reported about the
detaching. reflog is also updated to keep track of the event.
refs.c: refactor do_head_ref(... to do_one_head_ref("HEAD", ...
rev-list: include repos/xx/HEAD (and maybe 1ST_HEAD) in --all

This makes sure "repack -ad" won't turn detached HEADs from
$GIT_DIR/repos/ into broken refs.
refs.c: include refs 1ST_HEAD and repos/xxx/HEAD in for_each_reflog