tg.sh: fallback to tmp dir outside .git
commit1d5d4f1203485827df0e6620776822acb54df4ec
authorKyle J. McKay <mackyle@gmail.com>
Mon, 20 Mar 2017 12:12:20 +0000 (20 05:12 -0700)
committerKyle J. McKay <mackyle@gmail.com>
Mon, 20 Mar 2017 12:12:20 +0000 (20 05:12 -0700)
tree0e58645f6bd47736f2b1929188e2b024bb785965
parentd6c26a541af59ac0a68c5160a99d84d1efab2976
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>
tg.sh