summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <lars.wirzenius@codethink.co.uk>2013-08-15 16:14:33 +0000
committerLars Wirzenius <lars.wirzenius@codethink.co.uk>2013-08-15 17:02:12 +0000
commit5ff98b9d6e7073c47d051a8cadb0979cc959493f (patch)
tree878c7b20187a95570e6b462b9e4188a30410c2cb
parent8b88eeed5acf03e0b95df6ae19b20777b83ad4fd (diff)
downloaddefinitions-5ff98b9d6e7073c47d051a8cadb0979cc959493f.tar.gz
Add GitDir.is_currently_checked_out
This is needed when we load morphologies in "morph edit": it needs to load things differently depending on whether we're loading from the currently checked out ref or not. The new method does not have a unit test, because I'm betting that the test would not find a real bug. The test would just set up a git repo and check whether the right ref is checked out in a couple of ways (it is, and it isn't), and the code is simple enough that it's unlikely to break the test. Creating the test, however, is a fair bit of work. What the test would not catch is when we need the method to do something quite different, but then we'll want a new method anyway.
-rw-r--r--morphlib/gitdir.py17
1 files changed, 17 insertions, 0 deletions
diff --git a/morphlib/gitdir.py b/morphlib/gitdir.py
index 2bf74437..f40190ff 100644
--- a/morphlib/gitdir.py
+++ b/morphlib/gitdir.py
@@ -65,6 +65,23 @@ class GitDirectory(object):
argv.append(base_ref)
self._runcmd(argv)
+ def is_currently_checked_out(self, ref): # pragma: no cover
+ '''Is ref currently checked out?'''
+
+ # Try the ref name directly first. If that fails, prepend origin/
+ # to it. (FIXME: That's a kludge, and should be fixed.)
+ try:
+ parsed_ref = self._runcmd(['git', 'rev-parse', ref]).strip()
+ except cliapp.AppException:
+ parsed_ref = self._runcmd(
+ ['git', 'rev-parse', 'origin/%s' % ref]).strip()
+ parsed_head = self._runcmd(['git', 'rev-parse', 'HEAD']).strip()
+ return parsed_ref == parsed_head
+
+ def cat_file(self, obj_type, ref, filename): # pragma: no cover
+ return self._runcmd(
+ ['git', 'cat-file', obj_type, '%s:%s' % (ref, filename)])
+
def update_remotes(self): # pragma: no cover
'''Update remotes.'''
self._runcmd(['git', 'remote', 'update', '--prune'])