From ad3193a0e587073dee0edef46bdf24f6c6e09779 Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Sat, 18 Apr 2009 09:54:51 -0700 Subject: [PATCH] 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 --- git_command.py | 10 +++------- git_config.py | 6 +++--- git_refs.py | 10 +++++----- main.py | 4 ++-- trace.py | 34 ++++++++++++++++++++++++++++++++++ 5 files changed, 47 insertions(+), 17 deletions(-) create mode 100644 trace.py diff --git a/git_command.py b/git_command.py index a3bd9192..b6a4a343 100644 --- a/git_command.py +++ b/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, diff --git a/git_config.py b/git_config.py index f65a0353..78069c5d 100644 --- a/git_config.py +++ b/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) diff --git a/git_refs.py b/git_refs.py index be8d271b..24760918 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 @@ 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 = {} diff --git a/main.py b/main.py index df181835..740fb3a6 100755 --- a/main.py +++ b/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' diff --git a/trace.py b/trace.py new file mode 100644 index 00000000..0376d2b4 --- /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