Clean up cache_dir handling in gclient/git_cache/bot_update.

This CL makes a couple changes:
  * The goal here is to be able to specify git cache entirely from the
    environment variable $GIT_CACHE_PATH and not require special treatment from
    bot_update. Eventually this will be specified at the swarming task level
    instead of in the recipe (i.e. "cached git" will eventually be an
    implementation detail of git on the bots and completely transparent to
    all other software).
  * Removal of the general --cache-dir option from gclient. This option was
    error-prone; it doesn't actually make sense to configure this on
    a per-invocation basis. The sole exception was `gclient config`, which
    now allows this option to be set directly.
  * Consolidation of GitWrapper.cache_dir and GitWrapper._GetMirror; previously
    these two things could disagree, leading to weird intermediate states. Now
    they're the same value.

R=agable@chromium.org, ehmaldonado@chromium.org, tandrii@chromium.org

Bug: 823434
Change-Id: I14adc7619b5fc10768ce32be2651c6215ba94aff
Reviewed-on: https://chromium-review.googlesource.com/1105473
Reviewed-by: Aaron Gable <agable@chromium.org>
Reviewed-by: Edward Lesmes <ehmaldonado@chromium.org>
Reviewed-by: Andrii Shyshkalov <tandrii@chromium.org>
Commit-Queue: Robbie Iannucci <iannucci@chromium.org>
diff --git a/git_cache.py b/git_cache.py
index f3d5626..02f8209 100755
--- a/git_cache.py
+++ b/git_cache.py
@@ -195,6 +195,11 @@
     os.path.dirname(os.path.abspath(__file__)), 'gsutil.py')
   cachepath_lock = threading.Lock()
 
+  UNSET_CACHEPATH = object()
+
+  # Used for tests
+  _GIT_CONFIG_LOCATION = []
+
   @staticmethod
   def parse_fetch_spec(spec):
     """Parses and canonicalizes a fetch spec.
@@ -272,14 +277,18 @@
       if not hasattr(cls, 'cachepath'):
         try:
           cachepath = subprocess.check_output(
-              [cls.git_exe, 'config', '--global', 'cache.cachepath']).strip()
+              [cls.git_exe, 'config'] +
+              cls._GIT_CONFIG_LOCATION +
+              ['cache.cachepath']).strip()
         except subprocess.CalledProcessError:
-          cachepath = None
-        if not cachepath:
-          raise RuntimeError(
-              'No global cache.cachepath git configuration found.')
+          cachepath = os.environ.get('GIT_CACHE_PATH', cls.UNSET_CACHEPATH)
         setattr(cls, 'cachepath', cachepath)
-      return getattr(cls, 'cachepath')
+
+      ret = getattr(cls, 'cachepath')
+      if ret is cls.UNSET_CACHEPATH:
+        raise RuntimeError('No cache.cachepath git configuration or '
+                           '$GIT_CACHE_PATH is set.')
+      return ret
 
   def Rename(self, src, dst):
     # This is somehow racy on Windows.
@@ -795,7 +804,10 @@
   def __init__(self, *args, **kwargs):
     optparse.OptionParser.__init__(self, *args, prog='git cache', **kwargs)
     self.add_option('-c', '--cache-dir',
-                    help='Path to the directory containing the cache')
+                    help=(
+                      'Path to the directory containing the caches. Normally '
+                      'deduced from git config cache.cachepath or '
+                      '$GIT_CACHE_PATH.'))
     self.add_option('-v', '--verbose', action='count', default=1,
                     help='Increase verbosity (can be passed multiple times)')
     self.add_option('-q', '--quiet', action='store_true',