mercurial – Tony's Blog /anthony Just another Stolen Notebook weblog Sat, 18 Aug 2012 14:09:42 +0000 en-US hourly 1 https://wordpress.org/?v=4.7.1 simple twitter mercurial hook /anthony/2009/03/30/simple-twitter-mercurial-hook/ /anthony/2009/03/30/simple-twitter-mercurial-hook/#comments Mon, 30 Mar 2009 06:56:17 +0000 /anthony/?p=33

UPDATE: This script uses basic auth and no longer works as of Aug 31, 2010. Please use the updated version with OAuth support

This is a really simple mercurial 1.2 hook for posting incoming changesets to twitter. It requires python-twitter.
File: hgtwitter.py

'''
[extensions]
hgext.hgtwitter=

[hooks]
incoming.notify = python:hgext.hgtwitter.hook

[twitter]
username = twitter_username
password = twitter_password
'''
from mercurial import cmdutil, templater
import twitter

tweet_template = '''
|{root|basename}:{rev}|
{desc|strip}
'''.strip()

def hook(ui, repo, hooktype, node=None, source=None, **kwargs):
    ctx = repo[node]
    tuser = ui.config('twitter', 'username')
    tpass = ui.config('twitter', 'password')

    t = cmdutil.changeset_templater(ui=ui, repo=repo,
                                   patch=False, diffopts=None,
                                   mapfile=None, buffered=False)
    t.use_template(templater.parsestring(tweet_template,
                                        quoted=False))

    ui.pushbuffer()
    t.show(ctx, changes=ctx.changeset(), root=repo.root)
    tweet = ui.popbuffer()

    if len(tweet) > 140:
        tweet = tweet[:139] + u"\u2026"

    api = twitter.Api(username=tuser, password=tpass)
    status = api.PostUpdate(tweet)

If you have problems with mercurial 1.1 and lower, try removing the diffopts argument from changeset_templater() so it looks like:
t = cmdutil.changeset_templater(ui=ui, repo=repo,
                                   patch=False, mapfile=None,
                                   buffered=False)

]]>
/anthony/2009/03/30/simple-twitter-mercurial-hook/feed/ 9