Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(103)

Side by Side Diff: infra/libs/git2/git2.py

Issue 355153002: Refactor infra git libs and testing. (Closed) Base URL: https://chromium.googlesource.com/infra/infra@fake_testing_support
Patch Set: Change config ref to have a sandard naming scheme Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 # Copyright 2014 The Chromium Authors. All rights reserved. 1 # Copyright 2014 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 import collections 4 import collections
5 import fnmatch 5 import fnmatch
6 import logging 6 import logging
7 import os 7 import os
8 import subprocess 8 import subprocess
9 import sys 9 import sys
10 import tempfile 10 import tempfile
11 import urlparse 11 import urlparse
12 12
13 from infra.services.gnumbd.support.util import ( 13 from infra.libs.decorators import cached_property
14 cached_property, CalledProcessError)
15 14
16 from infra.services.gnumbd.support.data import CommitData 15 from infra.libs.git2.util import CalledProcessError
16 from infra.libs.git2.util import INVALID
17 from infra.libs.git2.data import CommitData
18
17 19
18 LOGGER = logging.getLogger(__name__) 20 LOGGER = logging.getLogger(__name__)
19 21
20 22
21 class _Invalid(object):
22 def __call__(self, *_args, **_kwargs):
23 return self
24
25 def __getattr__(self, _key):
26 return self
27
28 def __eq__(self, _other):
29 return False
30
31 def __ne__(self, _other): # pylint: disable=R0201
32 return True
33
34 INVALID = _Invalid()
35
36
37 class Repo(object): 23 class Repo(object):
38 """Represents a remote git repo. 24 """Represents a remote git repo.
39 25
40 Manages the (bare) on-disk mirror of the remote repo. 26 Manages the (bare) on-disk mirror of the remote repo.
41 """ 27 """
42 MAX_CACHE_SIZE = 1024 28 MAX_CACHE_SIZE = 1024
43 29
44 def __init__(self, url): 30 def __init__(self, url):
45 self.dry_run = False 31 self.dry_run = False
46 self.repos_dir = None 32 self.repos_dir = None
47 33
48 self._url = url 34 self._url = url
49 self._repo_path = None 35 self._repo_path = None
50 self._commit_cache = collections.OrderedDict() 36 self._commit_cache = collections.OrderedDict()
51 self._log = LOGGER.getChild('Repo') 37 self._log = LOGGER.getChild('Repo')
52 38
39 def __getitem__(self, ref):
40 """Get a Ref attached to this Repo."""
41 return Ref(self, ref)
42
53 def reify(self): 43 def reify(self):
54 """Ensures the local mirror of this Repo exists.""" 44 """Ensures the local mirror of this Repo exists."""
55 assert self.repos_dir is not None 45 assert self.repos_dir is not None
56 parsed = urlparse.urlparse(self._url) 46 parsed = urlparse.urlparse(self._url)
57 norm_url = parsed.netloc + parsed.path 47 norm_url = parsed.netloc + parsed.path
58 if norm_url.endswith('.git'): 48 if norm_url.endswith('.git'):
59 norm_url = norm_url[:-len('.git')] 49 norm_url = norm_url[:-len('.git')]
60 folder = norm_url.replace('-', '--').replace('/', '-').lower() 50 folder = norm_url.replace('-', '--').replace('/', '-').lower()
61 51
62 rpath = os.path.abspath(os.path.join(self.repos_dir, folder)) 52 rpath = os.path.abspath(os.path.join(self.repos_dir, folder))
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
261 251
262 def fast_forward_push(self, commit): 252 def fast_forward_push(self, commit):
263 """Push |commit| to this ref on the remote, and update the local copy of the 253 """Push |commit| to this ref on the remote, and update the local copy of the
264 ref to |commit|.""" 254 ref to |commit|."""
265 self.repo.run('push', 'origin', '%s:%s' % (commit.hsh, self.ref)) 255 self.repo.run('push', 'origin', '%s:%s' % (commit.hsh, self.ref))
266 self.update_to(commit) 256 self.update_to(commit)
267 257
268 def update_to(self, commit): 258 def update_to(self, commit):
269 """Update the local copy of the ref to |commit|.""" 259 """Update the local copy of the ref to |commit|."""
270 self.repo.run('update-ref', self.ref, commit.hsh) 260 self.repo.run('update-ref', self.ref, commit.hsh)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698