tg.sh: fallback to tmp dir outside .git
If the .git directory is not writable by the user running "tg"
then fall back to using the normal temporary location and if that
fails fall back to plain "/tmp" before giving up.
Do this for both the per-run-of-tg temporary directory and the
supposed-to-be-persistent global temporary directcory. Create
the per-run temporary directory first and if the global tg-cache
directory can't be created just move it into the per-run temporary
directory.
As part of this change, limit writing in the repository-global
"tg-cache" directory to a subdirectory based on the effective
user id to avoid bizarre failures where a different user needs
to update a cache entry originally written by someone else.
With this change it's now always possible (presuming there is
a writable temporary directory available somewhere on the system)
to perform the "tg" read-only operations on any git repository,
even ones for which the user running "tg" only has read-only
access. Although if neither "$TMPDIR" (if non-empty) nor /tmp is
writable then TMPDIR will have to be explicitly set to a writable
temporary directory before running "tg" for it to work.
Some operations may be a bit slower if the persistent cache is
not available as it will have to be built as-needed on each
run, but all the read-only operations will still work fine thanks
to the previous change to write tree turds in an alternate
temporary directory.
Signed-off-by: Kyle J. McKay <mackyle@gmail.com>