Преглед на файлове

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>
Shawn O. Pearce преди 17 години
родител
ревизия
ad3193a0e5
променени са 5 файла, в които са добавени 47 реда и са изтрити 17 реда
  1. 3 7
      git_command.py
  2. 3 3
      git_config.py
  3. 5 5
      git_refs.py
  4. 2 2
      main.py
  5. 34 0
      trace.py

+ 3 - 7
git_command.py

@@ -17,18 +17,14 @@ import os
 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 @@ class GitCommand(object):
     else:
       stderr = None
 
-    if TRACE:
+    if IsTrace():
       global LAST_CWD
       global LAST_GITDIR
 
@@ -127,7 +123,7 @@ class GitCommand(object):
         dbg += ' 1>|'
       if stderr == subprocess.PIPE:
         dbg += ' 2>|'
-      print >>sys.stderr, dbg
+      Trace('%s', dbg)
 
     try:
       p = subprocess.Popen(command,

+ 3 - 3
git_config.py

@@ -19,7 +19,8 @@ import re
 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 @@ class GitConfig(object):
     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)

+ 5 - 5
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 @@ class GitRefs(object):
       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 @@ class GitRefs(object):
     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 = {}

+ 2 - 2
main.py

@@ -27,7 +27,7 @@ import os
 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 @@ class _Repo(object):
     gopts, gargs = global_options.parse_args(glob)
 
     if gopts.trace:
-      git_command.TRACE = True
+      SetTrace()
     if gopts.show_version:
       if name == 'help':
         name = 'version'

+ 34 - 0
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