Only convert to unicode when needed
[irker.git] / irkerhook.py
index 4b243a20f63188e23d4c8d9850bd0b4bd704ec48..c7c7b0d585ab9a9da8747ed9eba750323c2972bb 100755 (executable)
@@ -37,7 +37,7 @@ default_channels = "irc://chat.freenode.net/#commits"
 # No user-serviceable parts below this line:
 #
 
-version = "1.20"
+version = "2.2"
 
 import os, sys, commands, socket, urllib, subprocess, locale, datetime
 from pipes import quote as shellquote
@@ -88,7 +88,8 @@ class Commit:
                     self.url = webview
             except IOError:
                 self.url = ""
-        return self.template % self.__dict__
+        res = self.template % self.__dict__
+        return unicode(res, 'UTF-8') if not isinstance(res, unicode) else res
 
 class GenericExtractor:
     "Generic class for encapsulating data from a VCS."
@@ -271,12 +272,11 @@ class GitExtractor(GenericExtractor):
         # Extract the meta-information for the commit
         commit.files = do("git diff-tree -r --name-only " + shellquote(commit.commit))
         commit.files = " ".join(commit.files.strip().split("\n")[1:])
-        # Design choice: for git we ship only the first line, which is
+        # Design choice: for git we ship only the first message line, which is
         # conventionally supposed to be a summary of the commit.  Under
         # other VCSes a different choice may be appropriate.
-        commit.author_name = do("git log -1 '--pretty=format:%an' " + shellquote(commit.commit))
-        commit.mail = do("git log -1 '--pretty=format:%ae' " + shellquote(commit.commit))
-        commit.logmsg = do("git log -1 '--pretty=format:%s' " + shellquote(commit.commit))
+        commit.author_name, commit.mail, commit.logmsg = \
+            do("git log -1 '--pretty=format:%an%n%ae%n%s' " + shellquote(commit.commit)).split("\n")
         # This discards the part of the author's address after @.
         # Might be be nice to ship the full email address, if not
         # for spammers' address harvesters - getting this wrong
@@ -393,12 +393,20 @@ class HgExtractor(GenericExtractor):
         return commit
 
 def hg_hook(ui, repo, **kwds):
-    # To be called from a Mercurial "commit" or "incoming" hook.  Example
-    # configuration:
+    # To be called from a Mercurial "commit", "incoming" or "changegroup" hook.
+    # Example configuration:
     # [hooks]
     # incoming.irker = python:/path/to/irkerhook.py:hg_hook
     extractor = HgExtractor([(ui, repo)])
-    ship(extractor, kwds['node'], False)
+    start = repo[kwds['node']].rev()
+    end = len(repo)
+    if start != end:
+        # changegroup with multiple commits, so we generate a notification
+        # for each one
+        for rev in range(start, end):
+            ship(extractor, rev, False)
+    else:
+        ship(extractor, kwds['node'], False)
 
 # The files we use to identify a Subversion repo might occur as content
 # in a git or hg repo, but the special subdirectories for those are more