Fix `repo --trace` to show ref and config loads

The value of the varible TRACE was copied during the import, which
happens before the --trace option can be processed.  So instead we
now use a function to determine if the value is set, as the function
can be safely copied early during import.

Signed-off-by: Shawn O. Pearce <sop@google.com>
diff --git a/git_command.py b/git_command.py
index a3bd919..b6a4a34 100644
--- a/git_command.py
+++ b/git_command.py
@@ -17,18 +17,14 @@
 import sys
 import subprocess
 from error import GitError
+from trace import REPO_TRACE, IsTrace, Trace
 
 GIT = 'git'
 MIN_GIT_VERSION = (1, 5, 4)
 GIT_DIR = 'GIT_DIR'
-REPO_TRACE = 'REPO_TRACE'
 
 LAST_GITDIR = None
 LAST_CWD = None
-try:
-  TRACE = os.environ[REPO_TRACE] == '1'
-except KeyError:
-  TRACE = False
 
 
 class _GitCall(object):
@@ -101,7 +97,7 @@
     else:
       stderr = None
 
-    if TRACE:
+    if IsTrace():
       global LAST_CWD
       global LAST_GITDIR
 
@@ -127,7 +123,7 @@
         dbg += ' 1>|'
       if stderr == subprocess.PIPE:
         dbg += ' 2>|'
-      print >>sys.stderr, dbg
+      Trace('%s', dbg)
 
     try:
       p = subprocess.Popen(command,
diff --git a/git_config.py b/git_config.py
index f65a035..78069c5 100644
--- a/git_config.py
+++ b/git_config.py
@@ -19,7 +19,8 @@
 import sys
 from urllib2 import urlopen, HTTPError
 from error import GitError, UploadError
-from git_command import GitCommand, TRACE
+from trace import Trace
+from git_command import GitCommand
 
 R_HEADS = 'refs/heads/'
 R_TAGS  = 'refs/tags/'
@@ -189,8 +190,7 @@
     except OSError:
       return None
     try:
-      if TRACE:
-        print >>sys.stderr, ': unpickle %s' % self.file
+      Trace(': unpickle %s', self.file)
       return cPickle.load(open(self._pickle, 'r'))
     except IOError:
       os.remove(self._pickle)
diff --git a/git_refs.py b/git_refs.py
index be8d271..2476091 100644
--- a/git_refs.py
+++ b/git_refs.py
@@ -15,7 +15,7 @@
 
 import os
 import sys
-from git_command import TRACE
+from trace import Trace
 
 HEAD    = 'HEAD'
 R_HEADS = 'refs/heads/'
@@ -65,8 +65,8 @@
       self._LoadAll()
 
   def _NeedUpdate(self):
-    if TRACE:
-      print >>sys.stderr, ': scan refs %s' % self._gitdir
+    Trace(': scan refs %s', self._gitdir)
+
     for name, mtime in self._mtime.iteritems():
       try:
         if mtime != os.path.getmtime(os.path.join(self._gitdir, name)):
@@ -76,8 +76,8 @@
     return False
 
   def _LoadAll(self):
-    if TRACE:
-      print >>sys.stderr, ': load refs %s' % self._gitdir
+    Trace(': load refs %s', self._gitdir)
+
     self._phyref = {}
     self._symref = {}
     self._mtime = {}
diff --git a/main.py b/main.py
index df18183..740fb3a 100755
--- a/main.py
+++ b/main.py
@@ -27,7 +27,7 @@
 import re
 import sys
 
-import git_command
+from trace import SetTrace
 from command import InteractiveCommand
 from command import MirrorSafeCommand
 from command import PagedCommand
@@ -79,7 +79,7 @@
     gopts, gargs = global_options.parse_args(glob)
 
     if gopts.trace:
-      git_command.TRACE = True
+      SetTrace()
     if gopts.show_version:
       if name == 'help':
         name = 'version'
diff --git a/trace.py b/trace.py
new file mode 100644
index 0000000..0376d2b
--- /dev/null
+++ b/trace.py
@@ -0,0 +1,34 @@
+#
+# Copyright (C) 2008 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import sys
+import os
+REPO_TRACE = 'REPO_TRACE'
+
+try:
+  _TRACE = os.environ[REPO_TRACE] == '1'
+except KeyError:
+  _TRACE = False
+
+def IsTrace():
+  return _TRACE
+
+def SetTrace():
+  global _TRACE
+  _TRACE = True
+
+def Trace(fmt, *args):
+  if IsTrace():
+    print >>sys.stderr, fmt % args