git: Add GitModifier
authorGuido Günther <agx@sigxcpu.org>
Sat, 22 Oct 2011 11:53:48 +0000 (13:53 +0200)
committerGuido Günther <agx@sigxcpu.org>
Sun, 23 Oct 2011 14:18:53 +0000 (16:18 +0200)
to make passing author/comitter information easier.

Git-Dch: Ignore

gbp/git.py

index 4b13911..610a419 100644 (file)
@@ -30,6 +30,53 @@ class GitRepositoryError(Exception):
     pass
 
 
+class GitModifier(object):
+    """Stores authorship/comitter information"""
+    def __init__(self, name=None, email=None, date=None):
+        self.__dict__.update(locals())
+
+    def _get_env(self, who):
+        """Get author or comitter information as env var dictionary"""
+        who = who.upper()
+        if who not in ['AUTHOR', 'COMMITTER']:
+            raise GitRepository("Neither comitter nor author")
+
+        extra_env = {}
+        if self.name:
+            extra_env['GIT_%s_NAME' % who] = self.name
+        if self.email:
+            extra_env['GIT_%s_EMAIL' % who] = self.email
+        if self.date:
+            extra_env['GIT_%s_DATE' % who] = self.date
+        return extra_env
+
+    def get_author_env(self):
+        """
+        Get env vars for authorship information
+
+        >>> g = GitModifier("foo", "bar")
+        >>> g.get_author_env()
+        {'GIT_AUTHOR_EMAIL': 'bar', 'GIT_AUTHOR_NAME': 'foo'}
+
+        @return: Author information suitable to use as environment variables
+        @rtype: dict
+        """
+        return self._get_env('author')
+
+    def get_committer_env(self):
+        """
+        Get env vars for comitter information
+
+        >>> g = GitModifier("foo", "bar")
+        >>> g.get_committer_env()
+        {'GIT_COMMITTER_NAME': 'foo', 'GIT_COMMITTER_EMAIL': 'bar'}
+
+        @return: Commiter information suitable to use as environment variables
+        @rtype: dict
+        """
+        return self._get_env('committer')
+
+
 class GitRepository(object):
     """
     Represents a git repository at I{path}. It's currently assumed that the git