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

Unified Diff: infra/libs/git2/ref.py

Issue 413983003: Refactor infra git libs and testing. (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Address comments 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 side-by-side diff with in-line comments
Download patch
Index: infra/libs/git2/ref.py
diff --git a/infra/libs/git2/ref.py b/infra/libs/git2/ref.py
new file mode 100644
index 0000000000000000000000000000000000000000..2c83ab96913a5f3e3e6099bc840a852d9f921d2e
--- /dev/null
+++ b/infra/libs/git2/ref.py
@@ -0,0 +1,64 @@
+# Copyright 2014 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+from infra.libs.git2.util import CalledProcessError
+from infra.libs.git2.util import INVALID
+
+class Ref(object):
+ """Represents a single simple ref in a git Repo."""
+ def __init__(self, repo, ref_str):
+ """
+ @type repo: Repo
+ @type ref_str: str
+ """
+ self._repo = repo
+ self._ref = ref_str
+
+ # Comparison & Representation
+ def __eq__(self, other):
+ return (self is other) or (
+ isinstance(other, Ref) and (
+ self.ref == other.ref and
+ self.repo is other.repo
+ )
+ )
+
+ def __ne__(self, other):
+ return not (self == other)
+
+ def __repr__(self):
+ return 'Ref({_repo!r}, {_ref!r})'.format(**self.__dict__)
+
+ # Accessors
+ # pylint: disable=W0212
+ repo = property(lambda self: self._repo)
+ ref = property(lambda self: self._ref)
+
+ # Properties
+ @property
+ def commit(self):
+ """Get the Commit at the tip of this Ref."""
+ try:
+ val = self._repo.run('show-ref', '--verify', self._ref)
+ except CalledProcessError:
+ return INVALID
+ return self._repo.get_commit(val.split()[0])
+
+ # Methods
+ def to(self, other):
+ """Generate Commit()'s which occur from `self..other`."""
+ assert self.commit is not INVALID
+ arg = '%s..%s' % (self.ref, other.ref)
+ for hsh in self.repo.run('rev-list', '--reverse', arg).splitlines():
+ yield self.repo.get_commit(hsh)
+
+ def fast_forward_push(self, commit):
+ """Push |commit| to this ref on the remote, and update the local copy of the
+ ref to |commit|."""
+ self.repo.run('push', 'origin', '%s:%s' % (commit.hsh, self.ref))
+ self.update_to(commit)
+
+ def update_to(self, commit):
+ """Update the local copy of the ref to |commit|."""
+ self.repo.run('update-ref', self.ref, commit.hsh)

Powered by Google App Engine
This is Rietveld 408576698