From 745ffa64a0ff092610f477b1c43e0175132d08f1 Mon Sep 17 00:00:00 2001 From: "calamity@chromium.org" Date: Mon, 8 Sep 2014 01:03:19 +0000 Subject: [PATCH] Skip tracking status in map-branches when -v flag is not supplied. This CL prevents map-branches from loading the tracking status when the -v flag is not supplied. This prevents the vanilla map-branches from taking a potentially long time to retrieve the tracking status. BUG=410137 Review URL: https://codereview.chromium.org/536793002 git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@291846 0039d316-1c4b-4281-b951-d872f2087c98 --- git_common.py | 11 ++++++----- git_map_branches.py | 36 +++++++++++++++++++++--------------- tests/git_common_test.py | 4 ++-- 3 files changed, 29 insertions(+), 22 deletions(-) diff --git a/git_common.py b/git_common.py index 31a62b890..2e268da0f 100644 --- a/git_common.py +++ b/git_common.py @@ -733,18 +733,19 @@ def get_git_version(): return tuple(int(x) for x in version.split('.')) -def get_all_tracking_info(): +def get_branches_info(include_tracking_status): format_string = ( '--format=%(refname:short):%(objectname:short):%(upstream:short):') # This is not covered by the depot_tools CQ which only has git version 1.8. - if get_git_version() >= MIN_UPSTREAM_TRACK_GIT_VERSION: # pragma: no cover + if (include_tracking_status and + get_git_version() >= MIN_UPSTREAM_TRACK_GIT_VERSION): # pragma: no cover format_string += '%(upstream:track)' info_map = {} data = run('for-each-ref', format_string, 'refs/heads') - TrackingInfo = collections.namedtuple( - 'TrackingInfo', 'hash upstream ahead behind') + BranchesInfo = collections.namedtuple( + 'BranchesInfo', 'hash upstream ahead behind') for line in data.splitlines(): (branch, branch_hash, upstream_branch, tracking_status) = line.split(':') @@ -754,7 +755,7 @@ def get_all_tracking_info(): behind_match = re.search(r'behind (\d+)', tracking_status) behind = int(behind_match.group(1)) if behind_match else None - info_map[branch] = TrackingInfo( + info_map[branch] = BranchesInfo( hash=branch_hash, upstream=upstream_branch, ahead=ahead, behind=behind) # Set None for upstreams which are not branches (e.g empty upstream, remotes diff --git a/git_map_branches.py b/git_map_branches.py index e18202b56..038142e00 100755 --- a/git_map_branches.py +++ b/git_map_branches.py @@ -31,7 +31,7 @@ import sys from third_party import colorama from third_party.colorama import Fore, Style -from git_common import current_branch, upstream, tags, get_all_tracking_info +from git_common import current_branch, upstream, tags, get_branches_info from git_common import get_git_version, MIN_UPSTREAM_TRACK_GIT_VERSION import git_cl @@ -103,42 +103,48 @@ class BranchMapper(object): """A class which constructs output representing the tree's branch structure. Attributes: - __tracking_info: a map of branches to their TrackingInfo objects which + __branches_info: a map of branches to their BranchesInfo objects which consist of the branch hash, upstream and ahead/behind status. __gone_branches: a set of upstreams which are not fetchable by git""" def __init__(self): self.verbosity = 0 self.output = OutputManager() - self.__tracking_info = get_all_tracking_info() self.__gone_branches = set() - self.__roots = set() + self.__branches_info = None + self.__parent_map = collections.defaultdict(list) + self.__current_branch = None + self.__current_hash = None + self.__tag_set = None + + def start(self): + self.__branches_info = get_branches_info( + include_tracking_status=self.verbosity >= 1) + roots = set() # A map of parents to a list of their children. - self.parent_map = collections.defaultdict(list) - for branch, branch_info in self.__tracking_info.iteritems(): + for branch, branch_info in self.__branches_info.iteritems(): if not branch_info: continue parent = branch_info.upstream - if parent and not self.__tracking_info[parent]: + if parent and not self.__branches_info[parent]: branch_upstream = upstream(branch) # If git can't find the upstream, mark the upstream as gone. if branch_upstream: parent = branch_upstream else: self.__gone_branches.add(parent) - # A parent that isn't in the tracking info is a root. - self.__roots.add(parent) + # A parent that isn't in the branches info is a root. + roots.add(parent) - self.parent_map[parent].append(branch) + self.__parent_map[parent].append(branch) self.__current_branch = current_branch() - self.__current_hash = self.__tracking_info[self.__current_branch].hash + self.__current_hash = self.__branches_info[self.__current_branch].hash self.__tag_set = tags() - def start(self): - for root in sorted(self.__roots): + for root in sorted(roots): self.__append_branch(root) def __is_invalid_parent(self, parent): @@ -164,7 +170,7 @@ class BranchMapper(object): def __append_branch(self, branch, depth=0): """Recurses through the tree structure and appends an OutputLine to the OutputManager for each branch.""" - branch_info = self.__tracking_info[branch] + branch_info = self.__branches_info[branch] branch_hash = branch_info.hash if branch_info else None line = OutputLine() @@ -225,7 +231,7 @@ class BranchMapper(object): self.output.append(line) - for child in sorted(self.parent_map.pop(branch, ())): + for child in sorted(self.__parent_map.pop(branch, ())): self.__append_branch(child, depth=depth + 1) diff --git a/tests/git_common_test.py b/tests/git_common_test.py index 28ecb1408..29b9a6813 100755 --- a/tests/git_common_test.py +++ b/tests/git_common_test.py @@ -372,7 +372,7 @@ class GitMutableFunctionsTest(git_test_utils.GitRepoReadWriteTestBase, self.assertTrue(all( isinstance(x, int) for x in self.repo.run(self.gc.get_git_version))) - def testGetAllTrackingInfo(self): + def testGetBranchesInfo(self): self.repo.git('commit', '--allow-empty', '-am', 'foooooo') self.repo.git('checkout', '-tb', 'happybranch', 'master') self.repo.git('commit', '--allow-empty', '-am', 'foooooo') @@ -382,10 +382,10 @@ class GitMutableFunctionsTest(git_test_utils.GitRepoReadWriteTestBase, self.repo.git('checkout', '-tb', 'parent_gone', 'to_delete') self.repo.git('branch', '-D', 'to_delete') - actual = self.repo.run(self.gc.get_all_tracking_info) supports_track = ( self.repo.run(self.gc.get_git_version) >= self.gc.MIN_UPSTREAM_TRACK_GIT_VERSION) + actual = self.repo.run(self.gc.get_branches_info, supports_track) expected = { 'happybranch': (