hgext/notify.py
author Jun Wu <quark@fb.com>
Mon, 02 Oct 2017 20:23:25 -0700
changeset 34447 5385b76fd1fd
parent 33750 a0e28577f7d4
child 34752 b811f06b144d
permissions -rw-r--r--
zeroconf: do not crash if socket being read is closed by another thread In zeroconf/__init__.py, there is: server = Zeroconf.Zeroconf(ip) l = listener() Zeroconf.ServiceBrowser(server, "_hg._tcp.local.", l) time.sleep(1) server.close() `server.close()` closes the underlying socket while the `ServiceBrowser` may still have a background thread reading the socket. There could be a race condition where the reading thread reads the closed socket, resulting in EBADF crash. This patch catches the exception. This makes test-paths.t pass with chg. Differential Revision: https://phab.mercurial-scm.org/D919
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
     1
# notify.py - email notifications for mercurial
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
     2
#
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
     3
# Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
     4
#
8225
46293a0c7e9f updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents: 8154
diff changeset
     5
# This software may be used and distributed according to the terms of the
10263
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 10112
diff changeset
     6
# GNU General Public License version 2 or any later version.
7127
9df67ee30ef5 help: better documentation intro for a few extensions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7116
diff changeset
     7
14940
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
     8
'''hooks for sending email push notifications
7127
9df67ee30ef5 help: better documentation intro for a few extensions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7116
diff changeset
     9
16950
0fdd8193c8b5 notify: various fixes to docstring
David Champion <dgc@uchicago.edu>
parents: 16500
diff changeset
    10
This extension implements hooks to send email notifications when
0fdd8193c8b5 notify: various fixes to docstring
David Champion <dgc@uchicago.edu>
parents: 16500
diff changeset
    11
changesets are sent from or received by the local repository.
7127
9df67ee30ef5 help: better documentation intro for a few extensions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7116
diff changeset
    12
14940
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
    13
First, enable the extension as explained in :hg:`help extensions`, and
16500
8436a4e21417 doc: fix careless document miss in help of hgext/notify
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 15654
diff changeset
    14
register the hook you want to run. ``incoming`` and ``changegroup`` hooks
16950
0fdd8193c8b5 notify: various fixes to docstring
David Champion <dgc@uchicago.edu>
parents: 16500
diff changeset
    15
are run when changesets are received, while ``outgoing`` hooks are for
0fdd8193c8b5 notify: various fixes to docstring
David Champion <dgc@uchicago.edu>
parents: 16500
diff changeset
    16
changesets sent to another repository::
7127
9df67ee30ef5 help: better documentation intro for a few extensions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7116
diff changeset
    17
9105
6188f2cc4a37 notify: fix indentation in module docstring
Martin Geisler <mg@lazybytes.net>
parents: 9104
diff changeset
    18
  [hooks]
6188f2cc4a37 notify: fix indentation in module docstring
Martin Geisler <mg@lazybytes.net>
parents: 9104
diff changeset
    19
  # one email for each incoming changeset
6188f2cc4a37 notify: fix indentation in module docstring
Martin Geisler <mg@lazybytes.net>
parents: 9104
diff changeset
    20
  incoming.notify = python:hgext.notify.hook
14940
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
    21
  # one email for all incoming changesets
9105
6188f2cc4a37 notify: fix indentation in module docstring
Martin Geisler <mg@lazybytes.net>
parents: 9104
diff changeset
    22
  changegroup.notify = python:hgext.notify.hook
14940
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
    23
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
    24
  # one email for all outgoing changesets
14617
23f4e1e40988 notify: send changesets on 'outgoing' hook, updated doc
Ingo Bressler <dev@ingobressler.net>
parents: 13878
diff changeset
    25
  outgoing.notify = python:hgext.notify.hook
7127
9df67ee30ef5 help: better documentation intro for a few extensions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7116
diff changeset
    26
16950
0fdd8193c8b5 notify: various fixes to docstring
David Champion <dgc@uchicago.edu>
parents: 16500
diff changeset
    27
This registers the hooks. To enable notification, subscribers must
0fdd8193c8b5 notify: various fixes to docstring
David Champion <dgc@uchicago.edu>
parents: 16500
diff changeset
    28
be assigned to repositories. The ``[usersubs]`` section maps multiple
0fdd8193c8b5 notify: various fixes to docstring
David Champion <dgc@uchicago.edu>
parents: 16500
diff changeset
    29
repositories to a given recipient. The ``[reposubs]`` section maps
0fdd8193c8b5 notify: various fixes to docstring
David Champion <dgc@uchicago.edu>
parents: 16500
diff changeset
    30
multiple recipients to a single repository::
9157
9261667e9b82 commands: use minirst parser when displaying help
Martin Geisler <mg@lazybytes.net>
parents: 9136
diff changeset
    31
9105
6188f2cc4a37 notify: fix indentation in module docstring
Martin Geisler <mg@lazybytes.net>
parents: 9104
diff changeset
    32
  [usersubs]
17754
19e9bf7c0927 notify: support revset selection for subscriptions
Michal Sznajder <michalsznajder@gmail.com>
parents: 16951
diff changeset
    33
  # key is subscriber email, value is a comma-separated list of repo patterns
9105
6188f2cc4a37 notify: fix indentation in module docstring
Martin Geisler <mg@lazybytes.net>
parents: 9104
diff changeset
    34
  user@host = pattern
7127
9df67ee30ef5 help: better documentation intro for a few extensions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7116
diff changeset
    35
9105
6188f2cc4a37 notify: fix indentation in module docstring
Martin Geisler <mg@lazybytes.net>
parents: 9104
diff changeset
    36
  [reposubs]
17754
19e9bf7c0927 notify: support revset selection for subscriptions
Michal Sznajder <michalsznajder@gmail.com>
parents: 16951
diff changeset
    37
  # key is repo pattern, value is a comma-separated list of subscriber emails
9105
6188f2cc4a37 notify: fix indentation in module docstring
Martin Geisler <mg@lazybytes.net>
parents: 9104
diff changeset
    38
  pattern = user@host
7127
9df67ee30ef5 help: better documentation intro for a few extensions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7116
diff changeset
    39
17754
19e9bf7c0927 notify: support revset selection for subscriptions
Michal Sznajder <michalsznajder@gmail.com>
parents: 16951
diff changeset
    40
A ``pattern`` is a ``glob`` matching the absolute path to a repository,
19e9bf7c0927 notify: support revset selection for subscriptions
Michal Sznajder <michalsznajder@gmail.com>
parents: 16951
diff changeset
    41
optionally combined with a revset expression. A revset expression, if
19e9bf7c0927 notify: support revset selection for subscriptions
Michal Sznajder <michalsznajder@gmail.com>
parents: 16951
diff changeset
    42
present, is separated from the glob by a hash. Example::
19e9bf7c0927 notify: support revset selection for subscriptions
Michal Sznajder <michalsznajder@gmail.com>
parents: 16951
diff changeset
    43
19e9bf7c0927 notify: support revset selection for subscriptions
Michal Sznajder <michalsznajder@gmail.com>
parents: 16951
diff changeset
    44
  [reposubs]
19e9bf7c0927 notify: support revset selection for subscriptions
Michal Sznajder <michalsznajder@gmail.com>
parents: 16951
diff changeset
    45
  */widgets#branch(release) = qa-team@example.com
19e9bf7c0927 notify: support revset selection for subscriptions
Michal Sznajder <michalsznajder@gmail.com>
parents: 16951
diff changeset
    46
19e9bf7c0927 notify: support revset selection for subscriptions
Michal Sznajder <michalsznajder@gmail.com>
parents: 16951
diff changeset
    47
This sends to ``qa-team@example.com`` whenever a changeset on the ``release``
19e9bf7c0927 notify: support revset selection for subscriptions
Michal Sznajder <michalsznajder@gmail.com>
parents: 16951
diff changeset
    48
branch triggers a notification in any repository ending in ``widgets``.
16950
0fdd8193c8b5 notify: various fixes to docstring
David Champion <dgc@uchicago.edu>
parents: 16500
diff changeset
    49
0fdd8193c8b5 notify: various fixes to docstring
David Champion <dgc@uchicago.edu>
parents: 16500
diff changeset
    50
In order to place them under direct user management, ``[usersubs]`` and
0fdd8193c8b5 notify: various fixes to docstring
David Champion <dgc@uchicago.edu>
parents: 16500
diff changeset
    51
``[reposubs]`` sections may be placed in a separate ``hgrc`` file and
0fdd8193c8b5 notify: various fixes to docstring
David Champion <dgc@uchicago.edu>
parents: 16500
diff changeset
    52
incorporated by reference::
14940
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
    53
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
    54
  [notify]
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
    55
  config = /path/to/subscriptionsfile
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
    56
16950
0fdd8193c8b5 notify: various fixes to docstring
David Champion <dgc@uchicago.edu>
parents: 16500
diff changeset
    57
Notifications will not be sent until the ``notify.test`` value is set
0fdd8193c8b5 notify: various fixes to docstring
David Champion <dgc@uchicago.edu>
parents: 16500
diff changeset
    58
to ``False``; see below.
14940
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
    59
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
    60
Notifications content can be tweaked with the following configuration entries:
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
    61
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
    62
notify.test
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
    63
  If ``True``, print messages to stdout instead of sending them. Default: True.
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
    64
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
    65
notify.sources
16950
0fdd8193c8b5 notify: various fixes to docstring
David Champion <dgc@uchicago.edu>
parents: 16500
diff changeset
    66
  Space-separated list of change sources. Notifications are activated only
0fdd8193c8b5 notify: various fixes to docstring
David Champion <dgc@uchicago.edu>
parents: 16500
diff changeset
    67
  when a changeset's source is in this list. Sources may be:
0fdd8193c8b5 notify: various fixes to docstring
David Champion <dgc@uchicago.edu>
parents: 16500
diff changeset
    68
0fdd8193c8b5 notify: various fixes to docstring
David Champion <dgc@uchicago.edu>
parents: 16500
diff changeset
    69
  :``serve``: changesets received via http or ssh
0fdd8193c8b5 notify: various fixes to docstring
David Champion <dgc@uchicago.edu>
parents: 16500
diff changeset
    70
  :``pull``: changesets received via ``hg pull``
0fdd8193c8b5 notify: various fixes to docstring
David Champion <dgc@uchicago.edu>
parents: 16500
diff changeset
    71
  :``unbundle``: changesets received via ``hg unbundle``
0fdd8193c8b5 notify: various fixes to docstring
David Champion <dgc@uchicago.edu>
parents: 16500
diff changeset
    72
  :``push``: changesets sent or received via ``hg push``
0fdd8193c8b5 notify: various fixes to docstring
David Champion <dgc@uchicago.edu>
parents: 16500
diff changeset
    73
  :``bundle``: changesets sent via ``hg unbundle``
0fdd8193c8b5 notify: various fixes to docstring
David Champion <dgc@uchicago.edu>
parents: 16500
diff changeset
    74
0fdd8193c8b5 notify: various fixes to docstring
David Champion <dgc@uchicago.edu>
parents: 16500
diff changeset
    75
  Default: serve.
14940
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
    76
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
    77
notify.strip
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
    78
  Number of leading slashes to strip from url paths. By default, notifications
16950
0fdd8193c8b5 notify: various fixes to docstring
David Champion <dgc@uchicago.edu>
parents: 16500
diff changeset
    79
  reference repositories with their absolute path. ``notify.strip`` lets you
14940
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
    80
  turn them into relative paths. For example, ``notify.strip=3`` will change
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
    81
  ``/long/path/repository`` into ``repository``. Default: 0.
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
    82
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
    83
notify.domain
16950
0fdd8193c8b5 notify: various fixes to docstring
David Champion <dgc@uchicago.edu>
parents: 16500
diff changeset
    84
  Default email domain for sender or recipients with no explicit domain.
7127
9df67ee30ef5 help: better documentation intro for a few extensions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7116
diff changeset
    85
14940
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
    86
notify.style
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
    87
  Style file to use when formatting emails.
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
    88
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
    89
notify.template
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
    90
  Template to use when formatting emails.
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
    91
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
    92
notify.incoming
16950
0fdd8193c8b5 notify: various fixes to docstring
David Champion <dgc@uchicago.edu>
parents: 16500
diff changeset
    93
  Template to use when run as an incoming hook, overriding ``notify.template``.
14940
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
    94
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
    95
notify.outgoing
16950
0fdd8193c8b5 notify: various fixes to docstring
David Champion <dgc@uchicago.edu>
parents: 16500
diff changeset
    96
  Template to use when run as an outgoing hook, overriding ``notify.template``.
14940
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
    97
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
    98
notify.changegroup
16950
0fdd8193c8b5 notify: various fixes to docstring
David Champion <dgc@uchicago.edu>
parents: 16500
diff changeset
    99
  Template to use when running as a changegroup hook, overriding
14940
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
   100
  ``notify.template``.
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
   101
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
   102
notify.maxdiff
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
   103
  Maximum number of diff lines to include in notification email. Set to 0
16950
0fdd8193c8b5 notify: various fixes to docstring
David Champion <dgc@uchicago.edu>
parents: 16500
diff changeset
   104
  to disable the diff, or -1 to include all of it. Default: 300.
14940
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
   105
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
   106
notify.maxsubject
16950
0fdd8193c8b5 notify: various fixes to docstring
David Champion <dgc@uchicago.edu>
parents: 16500
diff changeset
   107
  Maximum number of characters in email's subject line. Default: 67.
14940
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
   108
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
   109
notify.diffstat
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
   110
  Set to True to include a diffstat before diff content. Default: True.
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
   111
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
   112
notify.merge
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
   113
  If True, send notifications for merge changesets. Default: True.
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
   114
15561
ca572e94d8e7 notify: add option for writing to mbox
Mads Kiilerich <mads@kiilerich.com>
parents: 14940
diff changeset
   115
notify.mbox
ca572e94d8e7 notify: add option for writing to mbox
Mads Kiilerich <mads@kiilerich.com>
parents: 14940
diff changeset
   116
  If set, append mails to this mbox file instead of sending. Default: None.
ca572e94d8e7 notify: add option for writing to mbox
Mads Kiilerich <mads@kiilerich.com>
parents: 14940
diff changeset
   117
15654
2a7fa7c641d8 notify: change behavior of "changegroup" hook
Nikolaus Schueler <nikolaus.schueler@lantiq.com>
parents: 15561
diff changeset
   118
notify.fromauthor
16950
0fdd8193c8b5 notify: various fixes to docstring
David Champion <dgc@uchicago.edu>
parents: 16500
diff changeset
   119
  If set, use the committer of the first changeset in a changegroup for
0fdd8193c8b5 notify: various fixes to docstring
David Champion <dgc@uchicago.edu>
parents: 16500
diff changeset
   120
  the "From" field of the notification mail. If not set, take the user
0fdd8193c8b5 notify: various fixes to docstring
David Champion <dgc@uchicago.edu>
parents: 16500
diff changeset
   121
  from the pushing repo.  Default: False.
15654
2a7fa7c641d8 notify: change behavior of "changegroup" hook
Nikolaus Schueler <nikolaus.schueler@lantiq.com>
parents: 15561
diff changeset
   122
16950
0fdd8193c8b5 notify: various fixes to docstring
David Champion <dgc@uchicago.edu>
parents: 16500
diff changeset
   123
If set, the following entries will also be used to customize the
0fdd8193c8b5 notify: various fixes to docstring
David Champion <dgc@uchicago.edu>
parents: 16500
diff changeset
   124
notifications:
14940
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
   125
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
   126
email.from
16950
0fdd8193c8b5 notify: various fixes to docstring
David Champion <dgc@uchicago.edu>
parents: 16500
diff changeset
   127
  Email ``From`` address to use if none can be found in the generated
0fdd8193c8b5 notify: various fixes to docstring
David Champion <dgc@uchicago.edu>
parents: 16500
diff changeset
   128
  email content.
14940
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
   129
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
   130
web.baseurl
16950
0fdd8193c8b5 notify: various fixes to docstring
David Champion <dgc@uchicago.edu>
parents: 16500
diff changeset
   131
  Root repository URL to combine with repository paths when making
14940
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
   132
  references. See also ``notify.strip``.
d78b92353f26 notify: rewrite user documentation
Patrick Mezard <pmezard@gmail.com>
parents: 14869
diff changeset
   133
9068
27a41250a9ce notify: wrapped docstrings at 78 characters
Martin Geisler <mg@lazybytes.net>
parents: 8935
diff changeset
   134
'''
28416
d3c8183f429e notify: use absolute_import
timeless <timeless@mozdev.org>
parents: 26587
diff changeset
   135
from __future__ import absolute_import
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   136
28416
d3c8183f429e notify: use absolute_import
timeless <timeless@mozdev.org>
parents: 26587
diff changeset
   137
import email
d3c8183f429e notify: use absolute_import
timeless <timeless@mozdev.org>
parents: 26587
diff changeset
   138
import fnmatch
d3c8183f429e notify: use absolute_import
timeless <timeless@mozdev.org>
parents: 26587
diff changeset
   139
import socket
d3c8183f429e notify: use absolute_import
timeless <timeless@mozdev.org>
parents: 26587
diff changeset
   140
import time
d3c8183f429e notify: use absolute_import
timeless <timeless@mozdev.org>
parents: 26587
diff changeset
   141
29205
a0939666b836 py3: move up symbol imports to enforce import-checker rules
Yuya Nishihara <yuya@tcha.org>
parents: 28951
diff changeset
   142
from mercurial.i18n import _
28416
d3c8183f429e notify: use absolute_import
timeless <timeless@mozdev.org>
parents: 26587
diff changeset
   143
from mercurial import (
d3c8183f429e notify: use absolute_import
timeless <timeless@mozdev.org>
parents: 26587
diff changeset
   144
    cmdutil,
d3c8183f429e notify: use absolute_import
timeless <timeless@mozdev.org>
parents: 26587
diff changeset
   145
    error,
d3c8183f429e notify: use absolute_import
timeless <timeless@mozdev.org>
parents: 26587
diff changeset
   146
    mail,
d3c8183f429e notify: use absolute_import
timeless <timeless@mozdev.org>
parents: 26587
diff changeset
   147
    patch,
33738
2069a8c336b9 configitems: register the 'notify.config' config
Boris Feld <boris.feld@octobus.net>
parents: 32839
diff changeset
   148
    registrar,
28416
d3c8183f429e notify: use absolute_import
timeless <timeless@mozdev.org>
parents: 26587
diff changeset
   149
    util,
d3c8183f429e notify: use absolute_import
timeless <timeless@mozdev.org>
parents: 26587
diff changeset
   150
)
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   151
29841
d5883fd055c6 extensions: change magic "shipped with hg" string
Augie Fackler <augie@google.com>
parents: 29240
diff changeset
   152
# Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
25186
80c5b2666a96 extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents: 24987
diff changeset
   153
# extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
80c5b2666a96 extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents: 24987
diff changeset
   154
# be specifying the version(s) of Mercurial they are tested with, or
80c5b2666a96 extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents: 24987
diff changeset
   155
# leave the attribute unspecified.
29841
d5883fd055c6 extensions: change magic "shipped with hg" string
Augie Fackler <augie@google.com>
parents: 29240
diff changeset
   156
testedwith = 'ships-with-hg-core'
16743
38caf405d010 hgext: mark all first-party extensions as such
Augie Fackler <raf@durin42.com>
parents: 16683
diff changeset
   157
33738
2069a8c336b9 configitems: register the 'notify.config' config
Boris Feld <boris.feld@octobus.net>
parents: 32839
diff changeset
   158
configtable = {}
2069a8c336b9 configitems: register the 'notify.config' config
Boris Feld <boris.feld@octobus.net>
parents: 32839
diff changeset
   159
configitem = registrar.configitem(configtable)
2069a8c336b9 configitems: register the 'notify.config' config
Boris Feld <boris.feld@octobus.net>
parents: 32839
diff changeset
   160
2069a8c336b9 configitems: register the 'notify.config' config
Boris Feld <boris.feld@octobus.net>
parents: 32839
diff changeset
   161
configitem('notify', 'config',
2069a8c336b9 configitems: register the 'notify.config' config
Boris Feld <boris.feld@octobus.net>
parents: 32839
diff changeset
   162
    default=None,
2069a8c336b9 configitems: register the 'notify.config' config
Boris Feld <boris.feld@octobus.net>
parents: 32839
diff changeset
   163
)
33739
b30da11d9233 configitems: register the 'notify.diffstat' config
Boris Feld <boris.feld@octobus.net>
parents: 33738
diff changeset
   164
configitem('notify', 'diffstat',
b30da11d9233 configitems: register the 'notify.diffstat' config
Boris Feld <boris.feld@octobus.net>
parents: 33738
diff changeset
   165
    default=True,
b30da11d9233 configitems: register the 'notify.diffstat' config
Boris Feld <boris.feld@octobus.net>
parents: 33738
diff changeset
   166
)
33740
a853325102a0 configitems: register the 'notify.domain' config
Boris Feld <boris.feld@octobus.net>
parents: 33739
diff changeset
   167
configitem('notify', 'domain',
a853325102a0 configitems: register the 'notify.domain' config
Boris Feld <boris.feld@octobus.net>
parents: 33739
diff changeset
   168
    default=None,
a853325102a0 configitems: register the 'notify.domain' config
Boris Feld <boris.feld@octobus.net>
parents: 33739
diff changeset
   169
)
33741
b14be4deac06 configitems: register the 'notify.fromauthor' config
Boris Feld <boris.feld@octobus.net>
parents: 33740
diff changeset
   170
configitem('notify', 'fromauthor',
b14be4deac06 configitems: register the 'notify.fromauthor' config
Boris Feld <boris.feld@octobus.net>
parents: 33740
diff changeset
   171
    default=None,
b14be4deac06 configitems: register the 'notify.fromauthor' config
Boris Feld <boris.feld@octobus.net>
parents: 33740
diff changeset
   172
)
33742
66860180b009 configitems: register the 'notify.maxdiff' config
Boris Feld <boris.feld@octobus.net>
parents: 33741
diff changeset
   173
configitem('notify', 'maxdiff',
66860180b009 configitems: register the 'notify.maxdiff' config
Boris Feld <boris.feld@octobus.net>
parents: 33741
diff changeset
   174
    default=300,
66860180b009 configitems: register the 'notify.maxdiff' config
Boris Feld <boris.feld@octobus.net>
parents: 33741
diff changeset
   175
)
33743
3d08f4810690 configitems: register the 'notify.maxsubject' config
Boris Feld <boris.feld@octobus.net>
parents: 33742
diff changeset
   176
configitem('notify', 'maxsubject',
3d08f4810690 configitems: register the 'notify.maxsubject' config
Boris Feld <boris.feld@octobus.net>
parents: 33742
diff changeset
   177
    default=67,
3d08f4810690 configitems: register the 'notify.maxsubject' config
Boris Feld <boris.feld@octobus.net>
parents: 33742
diff changeset
   178
)
33744
b76dc3e6bf99 configitems: register the 'notify.mbox' config
Boris Feld <boris.feld@octobus.net>
parents: 33743
diff changeset
   179
configitem('notify', 'mbox',
b76dc3e6bf99 configitems: register the 'notify.mbox' config
Boris Feld <boris.feld@octobus.net>
parents: 33743
diff changeset
   180
    default=None,
b76dc3e6bf99 configitems: register the 'notify.mbox' config
Boris Feld <boris.feld@octobus.net>
parents: 33743
diff changeset
   181
)
33745
8bf8ed0deab8 configitems: register the 'notify.merge' config
Boris Feld <boris.feld@octobus.net>
parents: 33744
diff changeset
   182
configitem('notify', 'merge',
8bf8ed0deab8 configitems: register the 'notify.merge' config
Boris Feld <boris.feld@octobus.net>
parents: 33744
diff changeset
   183
    default=True,
8bf8ed0deab8 configitems: register the 'notify.merge' config
Boris Feld <boris.feld@octobus.net>
parents: 33744
diff changeset
   184
)
33746
20189ba5fddf configitems: register the 'notify.sources' config
Boris Feld <boris.feld@octobus.net>
parents: 33745
diff changeset
   185
configitem('notify', 'sources',
20189ba5fddf configitems: register the 'notify.sources' config
Boris Feld <boris.feld@octobus.net>
parents: 33745
diff changeset
   186
    default='serve',
20189ba5fddf configitems: register the 'notify.sources' config
Boris Feld <boris.feld@octobus.net>
parents: 33745
diff changeset
   187
)
33747
71665bbe82c1 configitems: register the 'notify.strip' config
Boris Feld <boris.feld@octobus.net>
parents: 33746
diff changeset
   188
configitem('notify', 'strip',
71665bbe82c1 configitems: register the 'notify.strip' config
Boris Feld <boris.feld@octobus.net>
parents: 33746
diff changeset
   189
    default=0,
71665bbe82c1 configitems: register the 'notify.strip' config
Boris Feld <boris.feld@octobus.net>
parents: 33746
diff changeset
   190
)
33748
c27fac1ca8ed configitems: register the 'notify.style' config
Boris Feld <boris.feld@octobus.net>
parents: 33747
diff changeset
   191
configitem('notify', 'style',
c27fac1ca8ed configitems: register the 'notify.style' config
Boris Feld <boris.feld@octobus.net>
parents: 33747
diff changeset
   192
    default=None,
c27fac1ca8ed configitems: register the 'notify.style' config
Boris Feld <boris.feld@octobus.net>
parents: 33747
diff changeset
   193
)
33749
ff4a11ff7aa3 configitems: register the 'notify.template' config
Boris Feld <boris.feld@octobus.net>
parents: 33748
diff changeset
   194
configitem('notify', 'template',
ff4a11ff7aa3 configitems: register the 'notify.template' config
Boris Feld <boris.feld@octobus.net>
parents: 33748
diff changeset
   195
    default=None,
ff4a11ff7aa3 configitems: register the 'notify.template' config
Boris Feld <boris.feld@octobus.net>
parents: 33748
diff changeset
   196
)
33750
a0e28577f7d4 configitems: register the 'notify.test' config
Boris Feld <boris.feld@octobus.net>
parents: 33749
diff changeset
   197
configitem('notify', 'test',
a0e28577f7d4 configitems: register the 'notify.test' config
Boris Feld <boris.feld@octobus.net>
parents: 33749
diff changeset
   198
    default=True,
a0e28577f7d4 configitems: register the 'notify.test' config
Boris Feld <boris.feld@octobus.net>
parents: 33749
diff changeset
   199
)
33738
2069a8c336b9 configitems: register the 'notify.config' config
Boris Feld <boris.feld@octobus.net>
parents: 32839
diff changeset
   200
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   201
# template for single changeset can include email headers.
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   202
single_template = '''
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   203
Subject: changeset in {webroot}: {desc|firstline|strip}
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   204
From: {author}
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   205
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   206
changeset {node|short} in {root}
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   207
details: {baseurl}{webroot}?cmd=changeset;node={node|short}
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   208
description:
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   209
\t{desc|tabindent|strip}
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   210
'''.lstrip()
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   211
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   212
# template for multiple changesets should not contain email headers,
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   213
# because only first set of headers will be used and result will look
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   214
# strange.
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   215
multiple_template = '''
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   216
changeset {node|short} in {root}
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   217
details: {baseurl}{webroot}?cmd=changeset;node={node|short}
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   218
summary: {desc|firstline}
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   219
'''
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   220
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   221
deftemplates = {
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   222
    'changegroup': multiple_template,
4498
4dfb9f232a63 Fixed indentation in hgext/notify.py
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4479
diff changeset
   223
}
2201
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   224
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   225
class notifier(object):
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   226
    '''email notification class.'''
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   227
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   228
    def __init__(self, ui, repo, hooktype):
2201
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   229
        self.ui = ui
2329
90368f89340a notify: add debug output. do not fail if no config file.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2326
diff changeset
   230
        cfg = self.ui.config('notify', 'config')
90368f89340a notify: add debug output. do not fail if no config file.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2326
diff changeset
   231
        if cfg:
8142
912bfef12ba6 ui: fold readsections into readconfig
Matt Mackall <mpm@selenic.com>
parents: 8029
diff changeset
   232
            self.ui.readconfig(cfg, sections=['usersubs', 'reposubs'])
2201
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   233
        self.repo = repo
33747
71665bbe82c1 configitems: register the 'notify.strip' config
Boris Feld <boris.feld@octobus.net>
parents: 33746
diff changeset
   234
        self.stripcount = int(self.ui.config('notify', 'strip'))
2201
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   235
        self.root = self.strip(self.repo.root)
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   236
        self.domain = self.ui.config('notify', 'domain')
15561
ca572e94d8e7 notify: add option for writing to mbox
Mads Kiilerich <mads@kiilerich.com>
parents: 14940
diff changeset
   237
        self.mbox = self.ui.config('notify', 'mbox')
33750
a0e28577f7d4 configitems: register the 'notify.test' config
Boris Feld <boris.feld@octobus.net>
parents: 33749
diff changeset
   238
        self.test = self.ui.configbool('notify', 'test')
7116
e981725da3fe notify: mime-encode messages
Christian Ebert <blacktrash@gmx.net>
parents: 6979
diff changeset
   239
        self.charsets = mail._charsets(self.ui)
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   240
        self.subs = self.subscribers()
33745
8bf8ed0deab8 configitems: register the 'notify.merge' config
Boris Feld <boris.feld@octobus.net>
parents: 33744
diff changeset
   241
        self.merge = self.ui.configbool('notify', 'merge')
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   242
28951
1bba1b43525a notify: do not load style file if template is specified (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 28416
diff changeset
   243
        mapfile = None
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   244
        template = (self.ui.config('notify', hooktype) or
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   245
                    self.ui.config('notify', 'template'))
28951
1bba1b43525a notify: do not load style file if template is specified (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 28416
diff changeset
   246
        if not template:
1bba1b43525a notify: do not load style file if template is specified (BC)
Yuya Nishihara <yuya@tcha.org>
parents: 28416
diff changeset
   247
            mapfile = self.ui.config('notify', 'style')
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   248
        if not mapfile and not template:
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   249
            template = deftemplates.get(hooktype) or single_template
32839
b425ec7fb7f6 cmdutil: pass templatespec tuple directly to changeset_templater (API)
Yuya Nishihara <yuya@tcha.org>
parents: 29841
diff changeset
   250
        spec = cmdutil.logtemplatespec(template, mapfile)
b425ec7fb7f6 cmdutil: pass templatespec tuple directly to changeset_templater (API)
Yuya Nishihara <yuya@tcha.org>
parents: 29841
diff changeset
   251
        self.t = cmdutil.changeset_templater(self.ui, self.repo, spec,
b425ec7fb7f6 cmdutil: pass templatespec tuple directly to changeset_templater (API)
Yuya Nishihara <yuya@tcha.org>
parents: 29841
diff changeset
   252
                                             False, None, False)
2201
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   253
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   254
    def strip(self, path):
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   255
        '''strip leading slashes from local path, turn into web-safe path.'''
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   256
2201
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   257
        path = util.pconvert(path)
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   258
        count = self.stripcount
2326
d0ba2f6b9d97 notify: fix off by one error.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2296
diff changeset
   259
        while count > 0:
2201
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   260
            c = path.find('/')
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   261
            if c == -1:
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   262
                break
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
   263
            path = path[c + 1:]
2201
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   264
            count -= 1
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   265
        return path
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   266
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   267
    def fixmail(self, addr):
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   268
        '''try to clean up email addresses.'''
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   269
5975
75d9fe70c654 templater: move email function to util
Matt Mackall <mpm@selenic.com>
parents: 4500
diff changeset
   270
        addr = util.email(addr.strip())
4094
fbf0e9acfd83 notify: don't try to fix addresses if notify.domain is not set
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4077
diff changeset
   271
        if self.domain:
fbf0e9acfd83 notify: don't try to fix addresses if notify.domain is not set
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4077
diff changeset
   272
            a = addr.find('@localhost')
fbf0e9acfd83 notify: don't try to fix addresses if notify.domain is not set
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4077
diff changeset
   273
            if a != -1:
fbf0e9acfd83 notify: don't try to fix addresses if notify.domain is not set
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4077
diff changeset
   274
                addr = addr[:a]
fbf0e9acfd83 notify: don't try to fix addresses if notify.domain is not set
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4077
diff changeset
   275
            if '@' not in addr:
fbf0e9acfd83 notify: don't try to fix addresses if notify.domain is not set
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 4077
diff changeset
   276
                return addr + '@' + self.domain
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   277
        return addr
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   278
2201
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   279
    def subscribers(self):
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   280
        '''return list of email addresses of subscribers to this repo.'''
8154
06f1e4e309ed notify: turned a set-like dict into a real set
Martin Geisler <mg@lazybytes.net>
parents: 8142
diff changeset
   281
        subs = set()
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   282
        for user, pats in self.ui.configitems('usersubs'):
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   283
            for pat in pats.split(','):
17754
19e9bf7c0927 notify: support revset selection for subscriptions
Michal Sznajder <michalsznajder@gmail.com>
parents: 16951
diff changeset
   284
                if '#' in pat:
19e9bf7c0927 notify: support revset selection for subscriptions
Michal Sznajder <michalsznajder@gmail.com>
parents: 16951
diff changeset
   285
                    pat, revs = pat.split('#', 1)
19e9bf7c0927 notify: support revset selection for subscriptions
Michal Sznajder <michalsznajder@gmail.com>
parents: 16951
diff changeset
   286
                else:
19e9bf7c0927 notify: support revset selection for subscriptions
Michal Sznajder <michalsznajder@gmail.com>
parents: 16951
diff changeset
   287
                    revs = None
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   288
                if fnmatch.fnmatch(self.repo.root, pat.strip()):
17754
19e9bf7c0927 notify: support revset selection for subscriptions
Michal Sznajder <michalsznajder@gmail.com>
parents: 16951
diff changeset
   289
                    subs.add((self.fixmail(user), revs))
2201
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   290
        for pat, users in self.ui.configitems('reposubs'):
17754
19e9bf7c0927 notify: support revset selection for subscriptions
Michal Sznajder <michalsznajder@gmail.com>
parents: 16951
diff changeset
   291
            if '#' in pat:
19e9bf7c0927 notify: support revset selection for subscriptions
Michal Sznajder <michalsznajder@gmail.com>
parents: 16951
diff changeset
   292
                pat, revs = pat.split('#', 1)
19e9bf7c0927 notify: support revset selection for subscriptions
Michal Sznajder <michalsznajder@gmail.com>
parents: 16951
diff changeset
   293
            else:
19e9bf7c0927 notify: support revset selection for subscriptions
Michal Sznajder <michalsznajder@gmail.com>
parents: 16951
diff changeset
   294
                revs = None
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   295
            if fnmatch.fnmatch(self.repo.root, pat):
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   296
                for user in users.split(','):
17754
19e9bf7c0927 notify: support revset selection for subscriptions
Michal Sznajder <michalsznajder@gmail.com>
parents: 16951
diff changeset
   297
                    subs.add((self.fixmail(user), revs))
19e9bf7c0927 notify: support revset selection for subscriptions
Michal Sznajder <michalsznajder@gmail.com>
parents: 16951
diff changeset
   298
        return [(mail.addressencode(self.ui, s, self.charsets, self.test), r)
19e9bf7c0927 notify: support revset selection for subscriptions
Michal Sznajder <michalsznajder@gmail.com>
parents: 16951
diff changeset
   299
                for s, r in sorted(subs)]
2201
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   300
9486
dd8d10c36c9c notify: make it possible to pass extra info into templates
Bryan O'Sullivan <bos@serpentine.com>
parents: 9327
diff changeset
   301
    def node(self, ctx, **props):
9516
f8048c334066 notify: permit suppression of merge changeset notification
David Champion <dgc@uchicago.edu>
parents: 9487
diff changeset
   302
        '''format one changeset, unless it is a suppressed merge.'''
f8048c334066 notify: permit suppression of merge changeset notification
David Champion <dgc@uchicago.edu>
parents: 9487
diff changeset
   303
        if not self.merge and len(ctx.parents()) > 1:
f8048c334066 notify: permit suppression of merge changeset notification
David Champion <dgc@uchicago.edu>
parents: 9487
diff changeset
   304
            return False
7726
2486980fe211 notify: use contexts more pervasively
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7705
diff changeset
   305
        self.t.show(ctx, changes=ctx.changeset(),
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   306
                    baseurl=self.ui.config('web', 'baseurl'),
9486
dd8d10c36c9c notify: make it possible to pass extra info into templates
Bryan O'Sullivan <bos@serpentine.com>
parents: 9327
diff changeset
   307
                    root=self.repo.root, webroot=self.root, **props)
9516
f8048c334066 notify: permit suppression of merge changeset notification
David Champion <dgc@uchicago.edu>
parents: 9487
diff changeset
   308
        return True
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   309
2230
332950340788 localrepository.addchangegroup: add more source infos to hooks
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2225
diff changeset
   310
    def skipsource(self, source):
332950340788 localrepository.addchangegroup: add more source infos to hooks
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2225
diff changeset
   311
        '''true if incoming changes from this source should be skipped.'''
33746
20189ba5fddf configitems: register the 'notify.sources' config
Boris Feld <boris.feld@octobus.net>
parents: 33745
diff changeset
   312
        ok_sources = self.ui.config('notify', 'sources').split()
2230
332950340788 localrepository.addchangegroup: add more source infos to hooks
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2225
diff changeset
   313
        return source not in ok_sources
332950340788 localrepository.addchangegroup: add more source infos to hooks
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2225
diff changeset
   314
7726
2486980fe211 notify: use contexts more pervasively
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7705
diff changeset
   315
    def send(self, ctx, count, data):
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   316
        '''send message.'''
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   317
17754
19e9bf7c0927 notify: support revset selection for subscriptions
Michal Sznajder <michalsznajder@gmail.com>
parents: 16951
diff changeset
   318
        # Select subscribers by revset
19e9bf7c0927 notify: support revset selection for subscriptions
Michal Sznajder <michalsznajder@gmail.com>
parents: 16951
diff changeset
   319
        subs = set()
19e9bf7c0927 notify: support revset selection for subscriptions
Michal Sznajder <michalsznajder@gmail.com>
parents: 16951
diff changeset
   320
        for sub, spec in self.subs:
19e9bf7c0927 notify: support revset selection for subscriptions
Michal Sznajder <michalsznajder@gmail.com>
parents: 16951
diff changeset
   321
            if spec is None:
19e9bf7c0927 notify: support revset selection for subscriptions
Michal Sznajder <michalsznajder@gmail.com>
parents: 16951
diff changeset
   322
                subs.add(sub)
19e9bf7c0927 notify: support revset selection for subscriptions
Michal Sznajder <michalsznajder@gmail.com>
parents: 16951
diff changeset
   323
                continue
19e9bf7c0927 notify: support revset selection for subscriptions
Michal Sznajder <michalsznajder@gmail.com>
parents: 16951
diff changeset
   324
            revs = self.repo.revs('%r and %d:', spec, ctx.rev())
19e9bf7c0927 notify: support revset selection for subscriptions
Michal Sznajder <michalsznajder@gmail.com>
parents: 16951
diff changeset
   325
            if len(revs):
19e9bf7c0927 notify: support revset selection for subscriptions
Michal Sznajder <michalsznajder@gmail.com>
parents: 16951
diff changeset
   326
                subs.add(sub)
19e9bf7c0927 notify: support revset selection for subscriptions
Michal Sznajder <michalsznajder@gmail.com>
parents: 16951
diff changeset
   327
                continue
19e9bf7c0927 notify: support revset selection for subscriptions
Michal Sznajder <michalsznajder@gmail.com>
parents: 16951
diff changeset
   328
        if len(subs) == 0:
19e9bf7c0927 notify: support revset selection for subscriptions
Michal Sznajder <michalsznajder@gmail.com>
parents: 16951
diff changeset
   329
            self.ui.debug('notify: no subscribers to selected repo '
19e9bf7c0927 notify: support revset selection for subscriptions
Michal Sznajder <michalsznajder@gmail.com>
parents: 16951
diff changeset
   330
                          'and revset\n')
19e9bf7c0927 notify: support revset selection for subscriptions
Michal Sznajder <michalsznajder@gmail.com>
parents: 16951
diff changeset
   331
            return
19e9bf7c0927 notify: support revset selection for subscriptions
Michal Sznajder <michalsznajder@gmail.com>
parents: 16951
diff changeset
   332
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   333
        p = email.Parser.Parser()
9313
8736b1c853ff notify: do not mime encode multipart templates
Christian Ebert <blacktrash@gmx.net>
parents: 8935
diff changeset
   334
        try:
8736b1c853ff notify: do not mime encode multipart templates
Christian Ebert <blacktrash@gmx.net>
parents: 8935
diff changeset
   335
            msg = p.parsestr(data)
25660
328739ea70c3 global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25203
diff changeset
   336
        except email.Errors.MessageParseError as inst:
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26503
diff changeset
   337
            raise error.Abort(inst)
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   338
7116
e981725da3fe notify: mime-encode messages
Christian Ebert <blacktrash@gmx.net>
parents: 6979
diff changeset
   339
        # store sender and subject
e981725da3fe notify: mime-encode messages
Christian Ebert <blacktrash@gmx.net>
parents: 6979
diff changeset
   340
        sender, subject = msg['From'], msg['Subject']
7658
44b3f7bbe2f3 notify: fix neglect of custom headers set via template
Christian Ebert <blacktrash@gmx.net>
parents: 7498
diff changeset
   341
        del msg['From'], msg['Subject']
9313
8736b1c853ff notify: do not mime encode multipart templates
Christian Ebert <blacktrash@gmx.net>
parents: 8935
diff changeset
   342
8736b1c853ff notify: do not mime encode multipart templates
Christian Ebert <blacktrash@gmx.net>
parents: 8935
diff changeset
   343
        if not msg.is_multipart():
8736b1c853ff notify: do not mime encode multipart templates
Christian Ebert <blacktrash@gmx.net>
parents: 8935
diff changeset
   344
            # create fresh mime message from scratch
8736b1c853ff notify: do not mime encode multipart templates
Christian Ebert <blacktrash@gmx.net>
parents: 8935
diff changeset
   345
            # (multipart templates must take care of this themselves)
8736b1c853ff notify: do not mime encode multipart templates
Christian Ebert <blacktrash@gmx.net>
parents: 8935
diff changeset
   346
            headers = msg.items()
8736b1c853ff notify: do not mime encode multipart templates
Christian Ebert <blacktrash@gmx.net>
parents: 8935
diff changeset
   347
            payload = msg.get_payload()
8736b1c853ff notify: do not mime encode multipart templates
Christian Ebert <blacktrash@gmx.net>
parents: 8935
diff changeset
   348
            # for notification prefer readability over data precision
8736b1c853ff notify: do not mime encode multipart templates
Christian Ebert <blacktrash@gmx.net>
parents: 8935
diff changeset
   349
            msg = mail.mimeencode(self.ui, payload, self.charsets, self.test)
8736b1c853ff notify: do not mime encode multipart templates
Christian Ebert <blacktrash@gmx.net>
parents: 8935
diff changeset
   350
            # reinstate custom headers
8736b1c853ff notify: do not mime encode multipart templates
Christian Ebert <blacktrash@gmx.net>
parents: 8935
diff changeset
   351
            for k, v in headers:
8736b1c853ff notify: do not mime encode multipart templates
Christian Ebert <blacktrash@gmx.net>
parents: 8935
diff changeset
   352
                msg[k] = v
7116
e981725da3fe notify: mime-encode messages
Christian Ebert <blacktrash@gmx.net>
parents: 6979
diff changeset
   353
7705
c55e68e8f256 notify: remove subfunctions that are called only once
Christian Ebert <blacktrash@gmx.net>
parents: 7658
diff changeset
   354
        msg['Date'] = util.datestr(format="%a, %d %b %Y %H:%M:%S %1%2")
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   355
7705
c55e68e8f256 notify: remove subfunctions that are called only once
Christian Ebert <blacktrash@gmx.net>
parents: 7658
diff changeset
   356
        # try to make subject line exist and be useful
c55e68e8f256 notify: remove subfunctions that are called only once
Christian Ebert <blacktrash@gmx.net>
parents: 7658
diff changeset
   357
        if not subject:
c55e68e8f256 notify: remove subfunctions that are called only once
Christian Ebert <blacktrash@gmx.net>
parents: 7658
diff changeset
   358
            if count > 1:
c55e68e8f256 notify: remove subfunctions that are called only once
Christian Ebert <blacktrash@gmx.net>
parents: 7658
diff changeset
   359
                subject = _('%s: %d new changesets') % (self.root, count)
c55e68e8f256 notify: remove subfunctions that are called only once
Christian Ebert <blacktrash@gmx.net>
parents: 7658
diff changeset
   360
            else:
7726
2486980fe211 notify: use contexts more pervasively
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7705
diff changeset
   361
                s = ctx.description().lstrip().split('\n', 1)[0].rstrip()
7705
c55e68e8f256 notify: remove subfunctions that are called only once
Christian Ebert <blacktrash@gmx.net>
parents: 7658
diff changeset
   362
                subject = '%s: %s' % (self.root, s)
33743
3d08f4810690 configitems: register the 'notify.maxsubject' config
Boris Feld <boris.feld@octobus.net>
parents: 33742
diff changeset
   363
        maxsubject = int(self.ui.config('notify', 'maxsubject'))
13202
d83566f4453b notify: use util.ellipsis() to truncate long subject
Yuya Nishihara <yuya@tcha.org>
parents: 10282
diff changeset
   364
        if maxsubject:
d83566f4453b notify: use util.ellipsis() to truncate long subject
Yuya Nishihara <yuya@tcha.org>
parents: 10282
diff changeset
   365
            subject = util.ellipsis(subject, maxsubject)
7705
c55e68e8f256 notify: remove subfunctions that are called only once
Christian Ebert <blacktrash@gmx.net>
parents: 7658
diff changeset
   366
        msg['Subject'] = mail.headencode(self.ui, subject,
c55e68e8f256 notify: remove subfunctions that are called only once
Christian Ebert <blacktrash@gmx.net>
parents: 7658
diff changeset
   367
                                         self.charsets, self.test)
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   368
7705
c55e68e8f256 notify: remove subfunctions that are called only once
Christian Ebert <blacktrash@gmx.net>
parents: 7658
diff changeset
   369
        # try to make message have proper sender
c55e68e8f256 notify: remove subfunctions that are called only once
Christian Ebert <blacktrash@gmx.net>
parents: 7658
diff changeset
   370
        if not sender:
c55e68e8f256 notify: remove subfunctions that are called only once
Christian Ebert <blacktrash@gmx.net>
parents: 7658
diff changeset
   371
            sender = self.ui.config('email', 'from') or self.ui.username()
c55e68e8f256 notify: remove subfunctions that are called only once
Christian Ebert <blacktrash@gmx.net>
parents: 7658
diff changeset
   372
        if '@' not in sender or '@localhost' in sender:
c55e68e8f256 notify: remove subfunctions that are called only once
Christian Ebert <blacktrash@gmx.net>
parents: 7658
diff changeset
   373
            sender = self.fixmail(sender)
c55e68e8f256 notify: remove subfunctions that are called only once
Christian Ebert <blacktrash@gmx.net>
parents: 7658
diff changeset
   374
        msg['From'] = mail.addressencode(self.ui, sender,
c55e68e8f256 notify: remove subfunctions that are called only once
Christian Ebert <blacktrash@gmx.net>
parents: 7658
diff changeset
   375
                                         self.charsets, self.test)
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   376
7726
2486980fe211 notify: use contexts more pervasively
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7705
diff changeset
   377
        msg['X-Hg-Notification'] = 'changeset %s' % ctx
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   378
        if not msg['Message-Id']:
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   379
            msg['Message-Id'] = ('<hg.%s.%s.%s@%s>' %
7726
2486980fe211 notify: use contexts more pervasively
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7705
diff changeset
   380
                                 (ctx, int(time.time()),
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   381
                                  hash(self.repo.root), socket.getfqdn()))
17754
19e9bf7c0927 notify: support revset selection for subscriptions
Michal Sznajder <michalsznajder@gmail.com>
parents: 16951
diff changeset
   382
        msg['To'] = ', '.join(sorted(subs))
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   383
9136
31177742f54a for calls expecting bool args, pass bool instead of int
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9105
diff changeset
   384
        msgtext = msg.as_string()
7498
64840fcb79e1 notify: no charset conversion when testing
Christian Ebert <blacktrash@gmx.net>
parents: 7369
diff changeset
   385
        if self.test:
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   386
            self.ui.write(msgtext)
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   387
            if not msgtext.endswith('\n'):
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   388
                self.ui.write('\n')
2201
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   389
        else:
2329
90368f89340a notify: add debug output. do not fail if no config file.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2326
diff changeset
   390
            self.ui.status(_('notify: sending %d subscribers %d changes\n') %
17754
19e9bf7c0927 notify: support revset selection for subscriptions
Michal Sznajder <michalsznajder@gmail.com>
parents: 16951
diff changeset
   391
                           (len(subs), count))
5975
75d9fe70c654 templater: move email function to util
Matt Mackall <mpm@selenic.com>
parents: 4500
diff changeset
   392
            mail.sendmail(self.ui, util.email(msg['From']),
17754
19e9bf7c0927 notify: support revset selection for subscriptions
Michal Sznajder <michalsznajder@gmail.com>
parents: 16951
diff changeset
   393
                          subs, msgtext, mbox=self.mbox)
2201
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   394
7726
2486980fe211 notify: use contexts more pervasively
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7705
diff changeset
   395
    def diff(self, ctx, ref=None):
2486980fe211 notify: use contexts more pervasively
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7705
diff changeset
   396
33742
66860180b009 configitems: register the 'notify.maxdiff' config
Boris Feld <boris.feld@octobus.net>
parents: 33741
diff changeset
   397
        maxdiff = int(self.ui.config('notify', 'maxdiff'))
13878
a8d13ee0ce68 misc: replace .parents()[0] with p1()
Matt Mackall <mpm@selenic.com>
parents: 13202
diff changeset
   398
        prev = ctx.p1().node()
24306
6ddc86eedc3b style: kill ersatz if-else ternary operators
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 23454
diff changeset
   399
        if ref:
6ddc86eedc3b style: kill ersatz if-else ternary operators
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 23454
diff changeset
   400
            ref = ref.node()
6ddc86eedc3b style: kill ersatz if-else ternary operators
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 23454
diff changeset
   401
        else:
6ddc86eedc3b style: kill ersatz if-else ternary operators
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents: 23454
diff changeset
   402
            ref = ctx.node()
23454
317ccfbd1a84 notify: explicitly honor all diffopts
Siddharth Agarwal <sid0@fb.com>
parents: 20667
diff changeset
   403
        chunks = patch.diff(self.repo, prev, ref,
317ccfbd1a84 notify: explicitly honor all diffopts
Siddharth Agarwal <sid0@fb.com>
parents: 20667
diff changeset
   404
                            opts=patch.diffallopts(self.ui))
7308
b6f5490effbf patch: turn patch.diff() into a generator
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7127
diff changeset
   405
        difflines = ''.join(chunks).splitlines()
6979
a3fd4aa154af notify: fix diffstat printing
divy@chelsio.com
parents: 6974
diff changeset
   406
33739
b30da11d9233 configitems: register the 'notify.diffstat' config
Boris Feld <boris.feld@octobus.net>
parents: 33738
diff changeset
   407
        if self.ui.configbool('notify', 'diffstat'):
3096
f422c8265ae5 Add support for diffstat in commit emails, and move diffstat from
Matt Doar <matt@xensource.com>
parents: 3017
diff changeset
   408
            s = patch.diffstat(difflines)
4077
1305ba7dee88 Prevent type exception on concatenation if diffstat returns None.
Sean Dague <sean@dague.net>
parents: 3739
diff changeset
   409
            # s may be nil, don't include the header if it is
1305ba7dee88 Prevent type exception on concatenation if diffstat returns None.
Sean Dague <sean@dague.net>
parents: 3739
diff changeset
   410
            if s:
29240
48afcaadf70e notify: make a message translatable
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 29205
diff changeset
   411
                self.ui.write(_('\ndiffstat:\n\n%s') % s)
7726
2486980fe211 notify: use contexts more pervasively
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7705
diff changeset
   412
6305
e8d447d91cdb notify: print diffstat even if maxline == 0
Benoît Allard <benoit@aeteurope.nl>
parents: 6229
diff changeset
   413
        if maxdiff == 0:
e8d447d91cdb notify: print diffstat even if maxline == 0
Benoît Allard <benoit@aeteurope.nl>
parents: 6229
diff changeset
   414
            return
7726
2486980fe211 notify: use contexts more pervasively
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7705
diff changeset
   415
        elif maxdiff > 0 and len(difflines) > maxdiff:
2486980fe211 notify: use contexts more pervasively
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7705
diff changeset
   416
            msg = _('\ndiffs (truncated from %d to %d lines):\n\n')
2486980fe211 notify: use contexts more pervasively
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7705
diff changeset
   417
            self.ui.write(msg % (len(difflines), maxdiff))
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   418
            difflines = difflines[:maxdiff]
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   419
        elif difflines:
3739
16f8e7d1dd54 fix notify with new ui buffering
Matt Mackall <mpm@selenic.com>
parents: 3679
diff changeset
   420
            self.ui.write(_('\ndiffs (%d lines):\n\n') % len(difflines))
7726
2486980fe211 notify: use contexts more pervasively
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7705
diff changeset
   421
6979
a3fd4aa154af notify: fix diffstat printing
divy@chelsio.com
parents: 6974
diff changeset
   422
        self.ui.write("\n".join(difflines))
2201
f15056b29472 patch queue: notify.patch
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   423
2230
332950340788 localrepository.addchangegroup: add more source infos to hooks
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2225
diff changeset
   424
def hook(ui, repo, hooktype, node=None, source=None, **kwargs):
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   425
    '''send email notifications to interested subscribers.
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   426
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   427
    if used as changegroup hook, send one email for all changesets in
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   428
    changegroup. else send one email per changeset.'''
7726
2486980fe211 notify: use contexts more pervasively
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7705
diff changeset
   429
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   430
    n = notifier(ui, repo, hooktype)
7726
2486980fe211 notify: use contexts more pervasively
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7705
diff changeset
   431
    ctx = repo[node]
2486980fe211 notify: use contexts more pervasively
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7705
diff changeset
   432
2329
90368f89340a notify: add debug output. do not fail if no config file.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2326
diff changeset
   433
    if not n.subs:
9467
4c041f1ee1b4 do not attempt to translate ui.debug output
Martin Geisler <mg@lazybytes.net>
parents: 9327
diff changeset
   434
        ui.debug('notify: no subscribers to repository %s\n' % n.root)
2329
90368f89340a notify: add debug output. do not fail if no config file.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2326
diff changeset
   435
        return
90368f89340a notify: add debug output. do not fail if no config file.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2326
diff changeset
   436
    if n.skipsource(source):
9467
4c041f1ee1b4 do not attempt to translate ui.debug output
Martin Geisler <mg@lazybytes.net>
parents: 9327
diff changeset
   437
        ui.debug('notify: changes have source "%s" - skipping\n' % source)
2230
332950340788 localrepository.addchangegroup: add more source infos to hooks
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2225
diff changeset
   438
        return
7726
2486980fe211 notify: use contexts more pervasively
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7705
diff changeset
   439
3739
16f8e7d1dd54 fix notify with new ui buffering
Matt Mackall <mpm@selenic.com>
parents: 3679
diff changeset
   440
    ui.pushbuffer()
9516
f8048c334066 notify: permit suppression of merge changeset notification
David Champion <dgc@uchicago.edu>
parents: 9487
diff changeset
   441
    data = ''
f8048c334066 notify: permit suppression of merge changeset notification
David Champion <dgc@uchicago.edu>
parents: 9487
diff changeset
   442
    count = 0
15654
2a7fa7c641d8 notify: change behavior of "changegroup" hook
Nikolaus Schueler <nikolaus.schueler@lantiq.com>
parents: 15561
diff changeset
   443
    author = ''
14617
23f4e1e40988 notify: send changesets on 'outgoing' hook, updated doc
Ingo Bressler <dev@ingobressler.net>
parents: 13878
diff changeset
   444
    if hooktype == 'changegroup' or hooktype == 'outgoing':
7726
2486980fe211 notify: use contexts more pervasively
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7705
diff changeset
   445
        start, end = ctx.rev(), len(repo)
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   446
        for rev in xrange(start, end):
9516
f8048c334066 notify: permit suppression of merge changeset notification
David Champion <dgc@uchicago.edu>
parents: 9487
diff changeset
   447
            if n.node(repo[rev]):
f8048c334066 notify: permit suppression of merge changeset notification
David Champion <dgc@uchicago.edu>
parents: 9487
diff changeset
   448
                count += 1
15654
2a7fa7c641d8 notify: change behavior of "changegroup" hook
Nikolaus Schueler <nikolaus.schueler@lantiq.com>
parents: 15561
diff changeset
   449
                if not author:
2a7fa7c641d8 notify: change behavior of "changegroup" hook
Nikolaus Schueler <nikolaus.schueler@lantiq.com>
parents: 15561
diff changeset
   450
                    author = repo[rev].user()
9516
f8048c334066 notify: permit suppression of merge changeset notification
David Champion <dgc@uchicago.edu>
parents: 9487
diff changeset
   451
            else:
f8048c334066 notify: permit suppression of merge changeset notification
David Champion <dgc@uchicago.edu>
parents: 9487
diff changeset
   452
                data += ui.popbuffer()
16683
525fdb738975 cleanup: eradicate long lines
Brodie Rao <brodie@sf.io>
parents: 16500
diff changeset
   453
                ui.note(_('notify: suppressing notification for merge %d:%s\n')
525fdb738975 cleanup: eradicate long lines
Brodie Rao <brodie@sf.io>
parents: 16500
diff changeset
   454
                        % (rev, repo[rev].hex()[:12]))
9516
f8048c334066 notify: permit suppression of merge changeset notification
David Champion <dgc@uchicago.edu>
parents: 9487
diff changeset
   455
                ui.pushbuffer()
f8048c334066 notify: permit suppression of merge changeset notification
David Champion <dgc@uchicago.edu>
parents: 9487
diff changeset
   456
        if count:
f8048c334066 notify: permit suppression of merge changeset notification
David Champion <dgc@uchicago.edu>
parents: 9487
diff changeset
   457
            n.diff(ctx, repo['tip'])
2203
9569eea1707c add email notification hook. hook written in python.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2201
diff changeset
   458
    else:
9516
f8048c334066 notify: permit suppression of merge changeset notification
David Champion <dgc@uchicago.edu>
parents: 9487
diff changeset
   459
        if not n.node(ctx):
f8048c334066 notify: permit suppression of merge changeset notification
David Champion <dgc@uchicago.edu>
parents: 9487
diff changeset
   460
            ui.popbuffer()
f8048c334066 notify: permit suppression of merge changeset notification
David Champion <dgc@uchicago.edu>
parents: 9487
diff changeset
   461
            ui.note(_('notify: suppressing notification for merge %d:%s\n') %
f8048c334066 notify: permit suppression of merge changeset notification
David Champion <dgc@uchicago.edu>
parents: 9487
diff changeset
   462
                    (ctx.rev(), ctx.hex()[:12]))
f8048c334066 notify: permit suppression of merge changeset notification
David Champion <dgc@uchicago.edu>
parents: 9487
diff changeset
   463
            return
f8048c334066 notify: permit suppression of merge changeset notification
David Champion <dgc@uchicago.edu>
parents: 9487
diff changeset
   464
        count += 1
7726
2486980fe211 notify: use contexts more pervasively
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7705
diff changeset
   465
        n.diff(ctx)
26503
5a84a453b503 notify: fix fromauthor setting for 'incoming' hook type (issue4194)
Bruce Cran <bruce.cran@gmail.com>
parents: 25660
diff changeset
   466
        if not author:
5a84a453b503 notify: fix fromauthor setting for 'incoming' hook type (issue4194)
Bruce Cran <bruce.cran@gmail.com>
parents: 25660
diff changeset
   467
            author = ctx.user()
7726
2486980fe211 notify: use contexts more pervasively
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7705
diff changeset
   468
9516
f8048c334066 notify: permit suppression of merge changeset notification
David Champion <dgc@uchicago.edu>
parents: 9487
diff changeset
   469
    data += ui.popbuffer()
15654
2a7fa7c641d8 notify: change behavior of "changegroup" hook
Nikolaus Schueler <nikolaus.schueler@lantiq.com>
parents: 15561
diff changeset
   470
    fromauthor = ui.config('notify', 'fromauthor')
2a7fa7c641d8 notify: change behavior of "changegroup" hook
Nikolaus Schueler <nikolaus.schueler@lantiq.com>
parents: 15561
diff changeset
   471
    if author and fromauthor:
2a7fa7c641d8 notify: change behavior of "changegroup" hook
Nikolaus Schueler <nikolaus.schueler@lantiq.com>
parents: 15561
diff changeset
   472
        data = '\n'.join(['From: %s' % author, data])
2a7fa7c641d8 notify: change behavior of "changegroup" hook
Nikolaus Schueler <nikolaus.schueler@lantiq.com>
parents: 15561
diff changeset
   473
9516
f8048c334066 notify: permit suppression of merge changeset notification
David Champion <dgc@uchicago.edu>
parents: 9487
diff changeset
   474
    if count:
f8048c334066 notify: permit suppression of merge changeset notification
David Champion <dgc@uchicago.edu>
parents: 9487
diff changeset
   475
        n.send(ctx, count, data)