diff options
-rw-r--r-- | morphlib/gitdir.py | 15 | ||||
-rw-r--r-- | morphlib/gitdir_tests.py | 12 |
2 files changed, 22 insertions, 5 deletions
diff --git a/morphlib/gitdir.py b/morphlib/gitdir.py index e6426e03..b08f69d2 100644 --- a/morphlib/gitdir.py +++ b/morphlib/gitdir.py @@ -185,13 +185,18 @@ class GitDirectory(object): else: return self._list_files_in_ref(ref) - def _rev_parse_tree(self, ref): + def _rev_parse(self, ref): try: - return self._runcmd(['git', 'rev-parse', '--verify', - '%s^{tree}' % ref]).strip() + return self._runcmd(['git', 'rev-parse', '--verify', ref]).strip() except cliapp.AppException as e: raise InvalidRefError(self, ref) + def resolve_ref_to_commit(self, ref): + return self._rev_parse('%s^{commit}' % ref) + + def resolve_ref_to_tree(self, ref): + return self._rev_parse('%s^{tree}' % ref) + def _list_files_in_work_tree(self): for dirpath, subdirs, filenames in os.walk(self.dirname): if dirpath == self.dirname and '.git' in subdirs: @@ -200,7 +205,7 @@ class GitDirectory(object): yield os.path.join(dirpath, filename)[len(self.dirname)+1:] def _list_files_in_ref(self, ref): - tree = self._rev_parse_tree(ref) + tree = self.resolve_ref_to_tree(ref) output = self._runcmd(['git', 'ls-tree', '--name-only', '-rz', tree]) # ls-tree appends \0 instead of interspersing, so we need to # strip the trailing \0 before splitting @@ -213,7 +218,7 @@ class GitDirectory(object): if ref is None: with open(os.path.join(self.dirname, filename)) as f: return f.read() - tree = self._rev_parse_tree(ref) + tree = self.resolve_ref_to_tree(ref) return self.get_file_from_ref(tree, filename) @property diff --git a/morphlib/gitdir_tests.py b/morphlib/gitdir_tests.py index 803f1b3e..a2e530f1 100644 --- a/morphlib/gitdir_tests.py +++ b/morphlib/gitdir_tests.py @@ -148,6 +148,18 @@ class GitDirectoryContentsTests(unittest.TestCase): gd.checkout('foo') self.assertEqual(gd.HEAD, 'foo') + def test_resolve_ref(self): + # Just tests that you get an object IDs back and that the + # commit and tree IDs are different, since checking the actual + # value of the commit requires foreknowledge of the result or + # re-implementing the body in the test. + gd = morphlib.gitdir.GitDirectory(self.dirname) + commit = gd.resolve_ref_to_commit(gd.HEAD) + self.assertEqual(len(commit), 40) + tree = gd.resolve_ref_to_tree(gd.HEAD) + self.assertEqual(len(tree), 40) + self.assertNotEqual(commit, tree) + def test_uncommitted_changes(self): gd = morphlib.gitdir.GitDirectory(self.dirname) self.assertEqual(sorted(gd.get_uncommitted_changes()), |