hgext/hgcia.py
author Yuya Nishihara <yuya@tcha.org>
Thu, 26 Mar 2015 23:56:18 +0900
changeset 24482 3eb9045396b0
parent 24306 6ddc86eedc3b
child 24987 fd7287f0b43c
permissions -rw-r--r--
children: don't pass filectx to displayer displayer doesn't want a fctx but a ctx. It failed with -Tdefault template. Traceback (most recent call last): ... File "mercurial/templatekw.py", line 212, in showbookmarks bookmarks = args['ctx'].bookmarks() AttributeError: 'filectx' object has no attribute 'bookmarks'
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7438
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
     1
# Copyright (C) 2007-8 Brendan Cully <brendan@kublai.com>
10263
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 9467
diff changeset
     2
#
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 9467
diff changeset
     3
# This software may be used and distributed according to the terms of the
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 9467
diff changeset
     4
# GNU General Public License version 2 or any later version.
7438
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
     5
8935
f4f0e902b750 extensions: change descriptions for hook-providing extensions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8894
diff changeset
     6
"""hooks for integrating with the CIA.vc notification service
7438
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
     7
9260
b2f37dbc5952 hgcia: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9208
diff changeset
     8
This is meant to be run as a changegroup or incoming hook. To
b2f37dbc5952 hgcia: wrap docstrings at 70 characters
Martin Geisler <mg@lazybytes.net>
parents: 9208
diff changeset
     9
configure it, set the following options in your hgrc::
7438
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    10
9208
12d119ae39bc hgcia: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9136
diff changeset
    11
  [cia]
12d119ae39bc hgcia: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9136
diff changeset
    12
  # your registered CIA user name
12d119ae39bc hgcia: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9136
diff changeset
    13
  user = foo
12d119ae39bc hgcia: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9136
diff changeset
    14
  # the name of the project in CIA
12d119ae39bc hgcia: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9136
diff changeset
    15
  project = foo
12d119ae39bc hgcia: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9136
diff changeset
    16
  # the module (subproject) (optional)
12d119ae39bc hgcia: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9136
diff changeset
    17
  #module = foo
12d119ae39bc hgcia: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9136
diff changeset
    18
  # Append a diffstat to the log message (optional)
12d119ae39bc hgcia: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9136
diff changeset
    19
  #diffstat = False
12d119ae39bc hgcia: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9136
diff changeset
    20
  # Template to use for log messages (optional)
14314
c322890b03e6 hgcia: Handle URL like in notify (issue2406)
Cédric Krier <ced@b2ck.com>
parents: 13878
diff changeset
    21
  #template = {desc}\\n{baseurl}{webroot}/rev/{node}-- {diffstat}
9208
12d119ae39bc hgcia: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9136
diff changeset
    22
  # Style to use (optional)
12d119ae39bc hgcia: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9136
diff changeset
    23
  #style = foo
12d119ae39bc hgcia: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9136
diff changeset
    24
  # The URL of the CIA notification service (optional)
17424
e7cfe3587ea4 fix trivial spelling errors
Mads Kiilerich <mads@kiilerich.com>
parents: 16743
diff changeset
    25
  # You can use mailto: URLs to send by email, e.g.
9208
12d119ae39bc hgcia: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9136
diff changeset
    26
  # mailto:cia@cia.vc
12d119ae39bc hgcia: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9136
diff changeset
    27
  # Make sure to set email.from if you do this.
12d119ae39bc hgcia: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9136
diff changeset
    28
  #url = http://cia.vc/
12d119ae39bc hgcia: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9136
diff changeset
    29
  # print message instead of sending it (optional)
12d119ae39bc hgcia: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9136
diff changeset
    30
  #test = False
14314
c322890b03e6 hgcia: Handle URL like in notify (issue2406)
Cédric Krier <ced@b2ck.com>
parents: 13878
diff changeset
    31
  # number of slashes to strip for url paths
c322890b03e6 hgcia: Handle URL like in notify (issue2406)
Cédric Krier <ced@b2ck.com>
parents: 13878
diff changeset
    32
  #strip = 0
7438
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    33
9208
12d119ae39bc hgcia: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9136
diff changeset
    34
  [hooks]
12d119ae39bc hgcia: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9136
diff changeset
    35
  # one of these:
12d119ae39bc hgcia: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9136
diff changeset
    36
  changegroup.cia = python:hgcia.hook
12d119ae39bc hgcia: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9136
diff changeset
    37
  #incoming.cia = python:hgcia.hook
7438
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    38
9208
12d119ae39bc hgcia: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9136
diff changeset
    39
  [web]
12d119ae39bc hgcia: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9136
diff changeset
    40
  # If you want hyperlinks (optional)
12d119ae39bc hgcia: use reST syntax for literal block
Martin Geisler <mg@lazybytes.net>
parents: 9136
diff changeset
    41
  baseurl = http://server/path/to/repo
7438
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    42
"""
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    43
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    44
from mercurial.i18n import _
11750
26e413f55b5e hgcia/color: remove star imports
Brodie Rao <brodie@bitheap.org>
parents: 10611
diff changeset
    45
from mercurial.node import bin, short
7438
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    46
from mercurial import cmdutil, patch, templater, util, mail
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    47
import email.Parser
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    48
16688
cfb6682961b8 cleanup: replace naked excepts with more specific ones
Brodie Rao <brodie@sf.io>
parents: 15776
diff changeset
    49
import socket, xmlrpclib
7438
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    50
from xml.sax import saxutils
16743
38caf405d010 hgext: mark all first-party extensions as such
Augie Fackler <raf@durin42.com>
parents: 16688
diff changeset
    51
testedwith = 'internal'
7438
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    52
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    53
socket_timeout = 30 # seconds
16688
cfb6682961b8 cleanup: replace naked excepts with more specific ones
Brodie Rao <brodie@sf.io>
parents: 15776
diff changeset
    54
if util.safehasattr(socket, 'setdefaulttimeout'):
7438
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    55
    # set a timeout for the socket so you don't have to wait so looooong
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    56
    # when cia.vc is having problems. requires python >= 2.3:
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    57
    socket.setdefaulttimeout(socket_timeout)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    58
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    59
HGCIA_VERSION = '0.1'
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    60
HGCIA_URL = 'http://hg.kublai.com/mercurial/hgcia'
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    61
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    62
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    63
class ciamsg(object):
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    64
    """ A CIA message """
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    65
    def __init__(self, cia, ctx):
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    66
        self.cia = cia
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    67
        self.ctx = ctx
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    68
        self.url = self.cia.url
14314
c322890b03e6 hgcia: Handle URL like in notify (issue2406)
Cédric Krier <ced@b2ck.com>
parents: 13878
diff changeset
    69
        if self.url:
c322890b03e6 hgcia: Handle URL like in notify (issue2406)
Cédric Krier <ced@b2ck.com>
parents: 13878
diff changeset
    70
            self.url += self.cia.root
7438
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    71
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    72
    def fileelem(self, path, uri, action):
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    73
        if uri:
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    74
            uri = ' uri=%s' % saxutils.quoteattr(uri)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    75
        return '<file%s action=%s>%s</file>' % (
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    76
            uri, saxutils.quoteattr(action), saxutils.escape(path))
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    77
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    78
    def fileelems(self):
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    79
        n = self.ctx.node()
13878
a8d13ee0ce68 misc: replace .parents()[0] with p1()
Matt Mackall <mpm@selenic.com>
parents: 13551
diff changeset
    80
        f = self.cia.repo.status(self.ctx.p1().node(), n)
7438
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    81
        url = self.url or ''
14850
a95242af945c hgcia: Set default value of strip to -1 (issue2891)
Cédric Krier <ced@b2ck.com>
parents: 14314
diff changeset
    82
        if url and url[-1] == '/':
a95242af945c hgcia: Set default value of strip to -1 (issue2891)
Cédric Krier <ced@b2ck.com>
parents: 14314
diff changeset
    83
            url = url[:-1]
7438
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    84
        elems = []
22917
1c38b4063586 hgcia: access status fields by name rather than index
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 20667
diff changeset
    85
        for path in f.modified:
7438
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    86
            uri = '%s/diff/%s/%s' % (url, short(n), path)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    87
            elems.append(self.fileelem(path, url and uri, 'modify'))
22917
1c38b4063586 hgcia: access status fields by name rather than index
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 20667
diff changeset
    88
        for path in f.added:
7438
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    89
            # TODO: copy/rename ?
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    90
            uri = '%s/file/%s/%s' % (url, short(n), path)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    91
            elems.append(self.fileelem(path, url and uri, 'add'))
22917
1c38b4063586 hgcia: access status fields by name rather than index
Martin von Zweigbergk <martinvonz@gmail.com>
parents: 20667
diff changeset
    92
        for path in f.removed:
7438
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    93
            elems.append(self.fileelem(path, '', 'remove'))
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    94
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    95
        return '\n'.join(elems)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    96
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    97
    def sourceelem(self, project, module=None, branch=None):
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    98
        msg = ['<source>', '<project>%s</project>' % saxutils.escape(project)]
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    99
        if module:
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   100
            msg.append('<module>%s</module>' % saxutils.escape(module))
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   101
        if branch:
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   102
            msg.append('<branch>%s</branch>' % saxutils.escape(branch))
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   103
        msg.append('</source>')
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   104
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   105
        return '\n'.join(msg)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   106
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   107
    def diffstat(self):
8778
c5f36402daad use new style classes
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 8541
diff changeset
   108
        class patchbuf(object):
7438
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   109
            def __init__(self):
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   110
                self.lines = []
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   111
                # diffstat is stupid
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   112
                self.name = 'cia'
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   113
            def write(self, data):
15776
55a85a55f020 hgcia: fix diffstat support
Matt Mackall <mpm@selenic.com>
parents: 14850
diff changeset
   114
                self.lines += data.splitlines(True)
7438
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   115
            def close(self):
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   116
                pass
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   117
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   118
        n = self.ctx.node()
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   119
        pbuf = patchbuf()
10611
e764f24a45ee patch/diff: move patch.export() to cmdutil.export()
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10529
diff changeset
   120
        cmdutil.export(self.cia.repo, [n], fp=pbuf)
7438
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   121
        return patch.diffstat(pbuf.lines) or ''
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   122
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   123
    def logmsg(self):
24306
6ddc86eedc3b style: kill ersatz if-else ternary operators
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22917
diff changeset
   124
        if self.cia.diffstat:
6ddc86eedc3b style: kill ersatz if-else ternary operators
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22917
diff changeset
   125
            diffstat = self.diffstat()
6ddc86eedc3b style: kill ersatz if-else ternary operators
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22917
diff changeset
   126
        else:
6ddc86eedc3b style: kill ersatz if-else ternary operators
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22917
diff changeset
   127
            diffstat = ''
7438
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   128
        self.cia.ui.pushbuffer()
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   129
        self.cia.templater.show(self.ctx, changes=self.ctx.changeset(),
14314
c322890b03e6 hgcia: Handle URL like in notify (issue2406)
Cédric Krier <ced@b2ck.com>
parents: 13878
diff changeset
   130
                                baseurl=self.cia.ui.config('web', 'baseurl'),
c322890b03e6 hgcia: Handle URL like in notify (issue2406)
Cédric Krier <ced@b2ck.com>
parents: 13878
diff changeset
   131
                                url=self.url, diffstat=diffstat,
c322890b03e6 hgcia: Handle URL like in notify (issue2406)
Cédric Krier <ced@b2ck.com>
parents: 13878
diff changeset
   132
                                webroot=self.cia.root)
7438
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   133
        return self.cia.ui.popbuffer()
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   134
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   135
    def xml(self):
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   136
        n = short(self.ctx.node())
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   137
        src = self.sourceelem(self.cia.project, module=self.cia.module,
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   138
                              branch=self.ctx.branch())
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   139
        # unix timestamp
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   140
        dt = self.ctx.date()
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   141
        timestamp = dt[0]
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   142
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   143
        author = saxutils.escape(self.ctx.user())
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   144
        rev = '%d:%s' % (self.ctx.rev(), n)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   145
        log = saxutils.escape(self.logmsg())
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   146
14850
a95242af945c hgcia: Set default value of strip to -1 (issue2891)
Cédric Krier <ced@b2ck.com>
parents: 14314
diff changeset
   147
        url = self.url
a95242af945c hgcia: Set default value of strip to -1 (issue2891)
Cédric Krier <ced@b2ck.com>
parents: 14314
diff changeset
   148
        if url and url[-1] == '/':
a95242af945c hgcia: Set default value of strip to -1 (issue2891)
Cédric Krier <ced@b2ck.com>
parents: 14314
diff changeset
   149
            url = url[:-1]
a95242af945c hgcia: Set default value of strip to -1 (issue2891)
Cédric Krier <ced@b2ck.com>
parents: 14314
diff changeset
   150
        url = url and '<url>%s/rev/%s</url>' % (saxutils.escape(url), n) or ''
7438
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   151
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   152
        msg = """
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   153
<message>
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   154
  <generator>
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   155
    <name>Mercurial (hgcia)</name>
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   156
    <version>%s</version>
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   157
    <url>%s</url>
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   158
    <user>%s</user>
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   159
  </generator>
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   160
  %s
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   161
  <body>
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   162
    <commit>
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   163
      <author>%s</author>
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   164
      <version>%s</version>
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   165
      <log>%s</log>
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   166
      %s
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   167
      <files>%s</files>
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   168
    </commit>
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   169
  </body>
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   170
  <timestamp>%d</timestamp>
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   171
</message>
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   172
""" % \
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   173
            (HGCIA_VERSION, saxutils.escape(HGCIA_URL),
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   174
            saxutils.escape(self.cia.user), src, author, rev, log, url,
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   175
            self.fileelems(), timestamp)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   176
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   177
        return msg
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   178
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   179
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   180
class hgcia(object):
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   181
    """ CIA notification class """
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   182
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   183
    deftemplate = '{desc}'
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   184
    dstemplate = '{desc}\n-- \n{diffstat}'
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   185
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   186
    def __init__(self, ui, repo):
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   187
        self.ui = ui
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   188
        self.repo = repo
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   189
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   190
        self.ciaurl = self.ui.config('cia', 'url', 'http://cia.vc')
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   191
        self.user = self.ui.config('cia', 'user')
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   192
        self.project = self.ui.config('cia', 'project')
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   193
        self.module = self.ui.config('cia', 'module')
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   194
        self.diffstat = self.ui.configbool('cia', 'diffstat')
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   195
        self.emailfrom = self.ui.config('email', 'from')
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   196
        self.dryrun = self.ui.configbool('cia', 'test')
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   197
        self.url = self.ui.config('web', 'baseurl')
14850
a95242af945c hgcia: Set default value of strip to -1 (issue2891)
Cédric Krier <ced@b2ck.com>
parents: 14314
diff changeset
   198
        # Default to -1 for backward compatibility
a95242af945c hgcia: Set default value of strip to -1 (issue2891)
Cédric Krier <ced@b2ck.com>
parents: 14314
diff changeset
   199
        self.stripcount = int(self.ui.config('cia', 'strip', -1))
14314
c322890b03e6 hgcia: Handle URL like in notify (issue2406)
Cédric Krier <ced@b2ck.com>
parents: 13878
diff changeset
   200
        self.root = self.strip(self.repo.root)
7438
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   201
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   202
        style = self.ui.config('cia', 'style')
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   203
        template = self.ui.config('cia', 'template')
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   204
        if not template:
24306
6ddc86eedc3b style: kill ersatz if-else ternary operators
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22917
diff changeset
   205
            if self.diffstat:
6ddc86eedc3b style: kill ersatz if-else ternary operators
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22917
diff changeset
   206
                template = self.dstemplate
6ddc86eedc3b style: kill ersatz if-else ternary operators
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22917
diff changeset
   207
            else:
6ddc86eedc3b style: kill ersatz if-else ternary operators
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 22917
diff changeset
   208
                template = self.deftemplate
7438
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   209
        template = templater.parsestring(template, quoted=False)
7762
fece056bf240 add --git option to commands supporting --patch (log, incoming, history, tip)
Jim Correia <jim.correia@pobox.com>
parents: 7438
diff changeset
   210
        t = cmdutil.changeset_templater(self.ui, self.repo, False, None,
20667
e96e9f805c19 changeset_templater: remove use_template method
Matt Mackall <mpm@selenic.com>
parents: 17424
diff changeset
   211
                                        template, style, False)
7438
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   212
        self.templater = t
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   213
14314
c322890b03e6 hgcia: Handle URL like in notify (issue2406)
Cédric Krier <ced@b2ck.com>
parents: 13878
diff changeset
   214
    def strip(self, path):
c322890b03e6 hgcia: Handle URL like in notify (issue2406)
Cédric Krier <ced@b2ck.com>
parents: 13878
diff changeset
   215
        '''strip leading slashes from local path, turn into web-safe path.'''
c322890b03e6 hgcia: Handle URL like in notify (issue2406)
Cédric Krier <ced@b2ck.com>
parents: 13878
diff changeset
   216
c322890b03e6 hgcia: Handle URL like in notify (issue2406)
Cédric Krier <ced@b2ck.com>
parents: 13878
diff changeset
   217
        path = util.pconvert(path)
c322890b03e6 hgcia: Handle URL like in notify (issue2406)
Cédric Krier <ced@b2ck.com>
parents: 13878
diff changeset
   218
        count = self.stripcount
14850
a95242af945c hgcia: Set default value of strip to -1 (issue2891)
Cédric Krier <ced@b2ck.com>
parents: 14314
diff changeset
   219
        if count < 0:
a95242af945c hgcia: Set default value of strip to -1 (issue2891)
Cédric Krier <ced@b2ck.com>
parents: 14314
diff changeset
   220
            return ''
14314
c322890b03e6 hgcia: Handle URL like in notify (issue2406)
Cédric Krier <ced@b2ck.com>
parents: 13878
diff changeset
   221
        while count > 0:
c322890b03e6 hgcia: Handle URL like in notify (issue2406)
Cédric Krier <ced@b2ck.com>
parents: 13878
diff changeset
   222
            c = path.find('/')
c322890b03e6 hgcia: Handle URL like in notify (issue2406)
Cédric Krier <ced@b2ck.com>
parents: 13878
diff changeset
   223
            if c == -1:
c322890b03e6 hgcia: Handle URL like in notify (issue2406)
Cédric Krier <ced@b2ck.com>
parents: 13878
diff changeset
   224
                break
c322890b03e6 hgcia: Handle URL like in notify (issue2406)
Cédric Krier <ced@b2ck.com>
parents: 13878
diff changeset
   225
            path = path[c + 1:]
c322890b03e6 hgcia: Handle URL like in notify (issue2406)
Cédric Krier <ced@b2ck.com>
parents: 13878
diff changeset
   226
            count -= 1
c322890b03e6 hgcia: Handle URL like in notify (issue2406)
Cédric Krier <ced@b2ck.com>
parents: 13878
diff changeset
   227
        return path
c322890b03e6 hgcia: Handle URL like in notify (issue2406)
Cédric Krier <ced@b2ck.com>
parents: 13878
diff changeset
   228
7438
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   229
    def sendrpc(self, msg):
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   230
        srv = xmlrpclib.Server(self.ciaurl)
10529
076e8a8000a3 hgcia: report RPC errors
Brendan Cully <brendan@kublai.com>
parents: 10263
diff changeset
   231
        res = srv.hub.deliver(msg)
13551
bbfae32f178e hgcia: accept "queued." xmlrpc return as success
Georg Brandl <georg@python.org>
parents: 11750
diff changeset
   232
        if res is not True and res != 'queued.':
10529
076e8a8000a3 hgcia: report RPC errors
Brendan Cully <brendan@kublai.com>
parents: 10263
diff changeset
   233
            raise util.Abort(_('%s returned an error: %s') %
076e8a8000a3 hgcia: report RPC errors
Brendan Cully <brendan@kublai.com>
parents: 10263
diff changeset
   234
                             (self.ciaurl, res))
7438
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   235
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   236
    def sendemail(self, address, data):
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   237
        p = email.Parser.Parser()
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   238
        msg = p.parsestr(data)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   239
        msg['Date'] = util.datestr(format="%a, %d %b %Y %H:%M:%S %1%2")
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   240
        msg['To'] = address
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   241
        msg['From'] = self.emailfrom
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   242
        msg['Subject'] = 'DeliverXML'
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   243
        msg['Content-type'] = 'text/xml'
9136
31177742f54a for calls expecting bool args, pass bool instead of int
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9062
diff changeset
   244
        msgtext = msg.as_string()
7438
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   245
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   246
        self.ui.status(_('hgcia: sending update to %s\n') % address)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   247
        mail.sendmail(self.ui, util.email(self.emailfrom),
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   248
                      [address], msgtext)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   249
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   250
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   251
def hook(ui, repo, hooktype, node=None, url=None, **kwargs):
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   252
    """ send CIA notification """
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   253
    def sendmsg(cia, ctx):
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   254
        msg = ciamsg(cia, ctx).xml()
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   255
        if cia.dryrun:
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   256
            ui.write(msg)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   257
        elif cia.ciaurl.startswith('mailto:'):
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   258
            if not cia.emailfrom:
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   259
                raise util.Abort(_('email.from must be defined when '
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   260
                                   'sending by email'))
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   261
            cia.sendemail(cia.ciaurl[7:], msg)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   262
        else:
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   263
            cia.sendrpc(msg)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   264
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   265
    n = bin(node)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   266
    cia = hgcia(ui, repo)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   267
    if not cia.user:
9467
4c041f1ee1b4 do not attempt to translate ui.debug output
Martin Geisler <mg@lazybytes.net>
parents: 9260
diff changeset
   268
        ui.debug('cia: no user specified')
7438
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   269
        return
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   270
    if not cia.project:
9467
4c041f1ee1b4 do not attempt to translate ui.debug output
Martin Geisler <mg@lazybytes.net>
parents: 9260
diff changeset
   271
        ui.debug('cia: no project specified')
7438
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   272
        return
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   273
    if hooktype == 'changegroup':
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   274
        start = repo.changelog.rev(n)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   275
        end = len(repo.changelog)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   276
        for rev in xrange(start, end):
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   277
            n = repo.changelog.node(rev)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   278
            ctx = repo.changectx(n)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   279
            sendmsg(cia, ctx)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   280
    else:
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   281
        ctx = repo.changectx(n)
b80b944836ec Add hgcia to hgext
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   282
        sendmsg(cia, ctx)