For use when using stgit with gerrit, we will need the ability to
generate unique identifiers that can be used as suitable replacement for
the git-commit-hook which generates change ids for the server.

Signed-off-by: Jacob Keller <[email protected]>
---
 stgit/lib/git.py | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/stgit/lib/git.py b/stgit/lib/git.py
index 8addc469420f..759fe5d75363 100644
--- a/stgit/lib/git.py
+++ b/stgit/lib/git.py
@@ -395,6 +395,25 @@ def __str__(self):
         return ('CommitData<tree: %s, parents: %s, author: %s,'
                 ' committer: %s, message: "%s">'
                 ) % (tree, parents, self.author, self.committer, self.message)
+    def changeid(self):
+        """Generate unique identifier for this commit data
+        @return: gerrit suitable identifier
+        """
+
+        # Some data we want to hash over
+        data  = ['tree %s' % self.tree]
+        data += ['parent %s' % p.sha1 for p in self.parents]
+        data += ['author %s' % self.author,
+                 'committer %s' % self.committer,
+                 self.message]
+
+        # Note that we don't use '-t commit' because some versions of git don't
+        # like our input in all cases. Since it doesn't really matter as long 
as
+        # the hash is actually unique, we'll just default to using blob format
+        changeid = run.Run('git', 'hash-object', '--stdin'
+                          ).raw_input('\n'.join(data)).output_one_line()
+        return 'I%s' % changeid
+
     def commit(self, repository):
         """Commit the commit.
         @return: The committed commit
-- 
2.6.1.264.gbab76a9


_______________________________________________
stgit-users mailing list
[email protected]
https://mail.gna.org/listinfo/stgit-users

Reply via email to