Debian Codegraph @

hgext/hooklib/changeset_obsoleted.py

raw
1 # Copyright 2020 Joerg Sonnenberger <[email protected]>
2 #
3 # This software may be used and distributed according to the terms of the
4 # GNU General Public License version 2 or any later version.
5 """changeset_obsoleted is a hook to send a mail when an
6 existing draft changeset is obsoleted by an obsmarker without successor.
7
8 Correct message threading requires the same messageidseed to be used for both
9 the original notification and the new mail.
10
11 Usage:
12 [notify]
13 messageidseed = myseed
14
15 [hooks]
16 txnclose.changeset_obsoleted = \
17 python:hgext.hooklib.changeset_obsoleted.hook
18 """
19
20 from __future__ import annotations
21
22 import email.errors as emailerrors
23 import email.utils as emailutils
24
25 from mercurial.i18n import _
26 from mercurial import (
27 encoding,
28 error,
29 formatter,
30 logcmdutil,
31 mail,
32 obsutil,
33 pycompat,
34 registrar,
35 )
36 from mercurial.utils import (
37 dateutil,
38 stringutil,
39 )
40 from .. import notify
41
42 configtable = {}
43 configitem = registrar.configitem(configtable)
44
45 configitem(
46 b'notify_obsoleted',
47 b'domain',
48 default=None,
49 )
50 configitem(
51 b'notify_obsoleted',
52 b'messageidseed',
53 default=None,
54 )
55 configitem(
56 b'notify_obsoleted',
57 b'template',
58 default=b'''Subject: changeset abandoned
59
60 This changeset has been abandoned.
61 ''',
62 )
63
64
65 def _report_commit(ui, repo, ctx):
66 domain = ui.config(b'notify_obsoleted', b'domain') or ui.config(
67 b'notify', b'domain'
68 )
69 messageidseed = ui.config(
70 b'notify_obsoleted', b'messageidseed'
71 ) or ui.config(b'notify', b'messageidseed')
72 template = ui.config(b'notify_obsoleted', b'template')
73 spec = formatter.literal_templatespec(template)
74 templater = logcmdutil.changesettemplater(ui, repo, spec)
75 ui.pushbuffer()
76 n = notify.notifier(ui, repo, b'incoming')
77
78 subs = set()
79 for sub, spec in n.subs:
80 if spec is None:
81 subs.add(sub)
82 continue
83 revs = repo.revs(b'%r and %d:', spec, ctx.rev())
84 if len(revs):
85 subs.add(sub)
86 continue
87 if len(subs) == 0:
88 ui.debug(
89 b'notify_obsoleted: no subscribers to selected repo and revset\n'
90 )
91 return
92
93 templater.show(
94 ctx,
95 changes=ctx.changeset(),
96 baseurl=ui.config(b'web', b'baseurl'),
97 root=repo.root,
98 webroot=n.root,
99 )
100 data = ui.popbuffer()
101
102 try:
103 msg = mail.parsebytes(data)
104 except emailerrors.MessageParseError as inst:
105 raise error.Abort(stringutil.forcebytestr(inst))
106
107 msg['In-reply-to'] = notify.messageid(ctx, domain, messageidseed)
108 msg['Message-Id'] = notify.messageid(
109 ctx, domain, messageidseed + b'-obsoleted'
110 )
111 msg['Date'] = encoding.strfromlocal(
112 dateutil.datestr(format=b"%a, %d %b %Y %H:%M:%S %1%2")
113 )
114 if not msg['From']:
115 sender = ui.config(b'email', b'from') or ui.username()
116 if b'@' not in sender or b'@localhost' in sender:
117 sender = n.fixmail(sender)
118 msg['From'] = mail.addressencode(ui, sender, n.charsets, n.test)
119 msg['To'] = ', '.join(sorted(subs))
120
121 msgtext = msg.as_bytes()
122 if ui.configbool(b'notify', b'test'):
123 ui.write(msgtext)
124 if not msgtext.endswith(b'\n'):
125 ui.write(b'\n')
126 else:
127 ui.status(_(b'notify_obsoleted: sending mail for %d\n') % ctx.rev())
128 mail.sendmail(
129 ui, emailutils.parseaddr(msg['From'])[1], subs, msgtext, mbox=n.mbox
130 )
131
132
133 def has_successor(repo, rev):
134 return any(
135 r for r in obsutil.allsuccessors(repo.obsstore, [rev]) if r != rev
136 )
137
138
139 def hook(ui, repo, hooktype, node=None, **kwargs):
140 if hooktype != b"txnclose":
141 raise error.Abort(
142 _(b'Unsupported hook type %r') % pycompat.bytestr(hooktype)
143 )
144 for rev in obsutil.getobsoleted(repo, changes=kwargs['changes']):
145 ctx = repo.unfiltered()[rev]
146 if not has_successor(repo, ctx.node()):
147 _report_commit(ui, repo, ctx)