From c9ebfbf5793e25d4a094f7f6a8c81069c55874ba Mon Sep 17 00:00:00 2001 From: Richard Maw Date: Tue, 28 Apr 2015 18:43:39 +0000 Subject: GitDirectory: Add a store_commit method commit-tree is usually sufficient, but I have a need to be able to create a commit directly. Change-Id: I80ba63eb9601aa1190554bb07522465ffb2cb5d9 --- morphlib/gitdir.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'morphlib/gitdir.py') diff --git a/morphlib/gitdir.py b/morphlib/gitdir.py index 5450db26..70f9db01 100644 --- a/morphlib/gitdir.py +++ b/morphlib/gitdir.py @@ -651,11 +651,24 @@ class GitDirectory(object): with fileno() method. ''' - if isinstance(blob_contents, basestring): - kwargs = {'feed_stdin': blob_contents} + return self._store_object(contents=blob_contents, type='blob') + + def store_commit(self, commit_contents): # pragma: no cover + '''Hash `commit_contents`, store it in git and return the sha1. + + `commit_contents` must either be a string or a value suitable to + pass to subprocess.Popen i.e. a file descriptor or file object + with fileno() method. + + ''' + return self._store_object(contents=commit_contents, type='commit') + + def _store_object(self, contents, type): + if isinstance(contents, basestring): + kwargs = {'feed_stdin': contents} else: - kwargs = {'stdin': blob_contents} - return morphlib.git.gitcmd(self._runcmd, 'hash-object', '-t', 'blob', + kwargs = {'stdin': contents} + return morphlib.git.gitcmd(self._runcmd, 'hash-object', '-t', type, '-w', '--stdin', **kwargs).strip() def commit_tree(self, tree, parent, message, **kwargs): -- cgit v1.2.1