annotate src/plugins/plugin_merge_req_mercurial.py @ 2548:54b3853b55c0

plugin uri finder: plugin to find URIs in well-known locations
author Goffi <goffi@goffi.org>
date Sat, 31 Mar 2018 17:20:14 +0200
parents a64887289931
children 1362cabad225
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python2
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # SàT plugin for import external blogs
2483
0046283a285d dates update
Goffi <goffi@goffi.org>
parents: 2449
diff changeset
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from sat.core.i18n import _, D_
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from sat.core.constants import Const as C
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from sat.core import exceptions
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from twisted.internet import reactor, defer, protocol
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from twisted.python.failure import Failure
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from twisted.python.procutils import which
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from sat.core.log import getLogger
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 log = getLogger(__name__)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
28
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
29
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 PLUGIN_INFO = {
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 C.PI_NAME: "Mercurial Merge Request handler",
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 C.PI_IMPORT_NAME: "MERGE_REQUEST_MERCURIAL",
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 C.PI_TYPE: C.PLUG_TYPE_MISC,
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 C.PI_DEPENDENCIES: ["MERGE_REQUESTS"],
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 C.PI_MAIN: "MercurialHandler",
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 C.PI_HANDLER: "no",
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 C.PI_DESCRIPTION: _(u"""Merge request handler for Mercurial""")
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 }
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
39
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 SHORT_DESC = D_(u"handle Mercurial repository")
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
41
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
42
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 class MercurialProtocol(protocol.ProcessProtocol):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 """handle hg commands"""
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 hg = None
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
46
2544
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
47 def __init__(self, deferred, stdin=None):
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
48 """
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
49 @param deferred(defer.Deferred): will be called when command is completed
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
50 @param stdin(str, None): if not None, will be push to standard input
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
51 """
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
52 self._stdin = stdin
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 self._deferred = deferred
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 self.data = []
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
55
2544
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
56 def connectionMade(self):
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
57 if self._stdin is not None:
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
58 self.transport.write(self._stdin)
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
59 self.transport.closeStdin()
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
60
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 def outReceived(self, data):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 self.data.append(data)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
63
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 def errReceived(self, data):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 self.data.append(data)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
66
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 def processEnded(self, reason):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 data = u''.join([d.decode('utf-8') for d in self.data])
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 if (reason.value.exitCode == 0):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 log.debug(_('Mercurial command succeed'))
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 self._deferred.callback(data)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 else:
2544
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
73 msg = _(u"Can't complete Mercurial command (error code: {code}): {message}").format(
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 code = reason.value.exitCode,
2544
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
75 message = data)
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
76 log.warning(msg)
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
77 self._deferred.errback(Failure(RuntimeError(msg)))
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
78
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 @classmethod
2544
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
80 def run(cls, path, command, *args, **kwargs):
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 """Create a new MercurialRegisterProtocol and execute the given mercurialctl command.
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
82
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 @param path(unicode): path to the repository
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 @param command(unicode): command to run
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 @param *args(unicode): command arguments
2544
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
86 @param **kwargs: used because Python2 doesn't handle normal kw args after *args
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
87 can only be:
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
88 - stdin(unicode, None): data to push to standard input
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 @return ((D)):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 """
2544
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
91 stdin = kwargs.pop('stdin', None)
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
92 if kwargs:
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
93 raise exceptions.InternalError(u'only stdin is allowed as keyword argument')
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
94 if stdin is not None:
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
95 stdin = stdin.encode('utf-8')
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 d = defer.Deferred()
2544
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
97 mercurial_prot = MercurialProtocol(d, stdin=stdin)
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 cmd_args = [cls.hg, command.encode('utf-8')]
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 cmd_args.extend([a.encode('utf-8') for a in args])
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 reactor.spawnProcess(mercurial_prot,
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 cls.hg,
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 cmd_args,
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 path=path.encode('utf-8'))
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 return d
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
105
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
106
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 class MercurialHandler(object):
2544
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
108 data_types = (u'mercurial_changeset',)
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
109
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 def __init__(self, host):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 log.info(_(u"Mercurial merge request handler initialization"))
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 try:
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
113 MercurialProtocol.hg = which('hg')[0]
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 except IndexError:
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 raise exceptions.NotFound(_(u"Mercurial executable (hg) not found, can't use Mercurial handler"))
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
116 self.host = host
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 self._m = host.plugins['MERGE_REQUESTS']
2544
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
118 self._m.register('mercurial', self, self.data_types, SHORT_DESC)
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
119
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 def check(self, repository):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 d = MercurialProtocol.run(repository, 'identify')
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
122 d.addCallback(lambda dummy: True)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 d.addErrback(lambda dummy: False)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 return d
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
125
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 def export(self, repository):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
127 return MercurialProtocol.run(repository, 'export', '-g', '-r', 'outgoing()', '--encoding=utf-8')
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
128
2544
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
129 def import_(self, repository, data, data_type, item_id, service, node, extra):
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
130 parsed_data = self.parse(data)
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
131 try:
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
132 parsed_name = parsed_data[0][u'commit_msg'].split(u'\n')[0]
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
133 parsed_name = parsed_name.replace(u' ', u'_')[:20]
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
134 except Exception:
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
135 parsed_name = u''
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
136 name = u'mr_{item_id}_{parsed_name}'.format(item_id=item_id, parsed_name=parsed_name)
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
137 return MercurialProtocol.run(repository, 'qimport', '-g', '--name', name, '--encoding=utf-8', '-', stdin=data)
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
138
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 def parse(self, data, data_type=None):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 lines = data.splitlines()
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 total_lines = len(lines)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 patches = []
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
143 while lines:
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 patch = {}
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 commit_msg = []
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
146 diff = []
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 state = 'init'
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
148 if lines[0] != '# HG changeset patch':
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
149 raise exceptions.DataError(_(u'invalid changeset signature'))
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 # line index of this patch in the whole data
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
151 patch_idx = total_lines - len(lines)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 del lines[0]
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
153
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 for idx, line in enumerate(lines):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 if state == 'init':
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 if line.startswith(u'# '):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
157 if line.startswith(u'# User '):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 elems = line[7:].split()
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
159 if not elems:
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 continue
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
161 last = elems[-1]
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
162 if last.startswith(u'<') and last.endswith(u'>') and u'@' in last:
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
163 patch[self._m.META_EMAIL] = elems.pop()[1:-1]
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
164 patch[self._m.META_AUTHOR] = u' '.join(elems)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 elif line.startswith(u'# Date '):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
166 time_data = line[7:].split()
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
167 if len(time_data) != 2:
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
168 log.warning(_(u'unexpected time data: {data}').format(data=line[7:]))
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
169 continue
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
170 patch[self._m.META_TIMESTAMP] = int(time_data[0]) + int(time_data[1])
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
171 elif line.startswith(u'# Node ID '):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
172 patch[self._m.META_HASH] = line[10:]
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
173 elif line.startswith(u'# Parent '):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
174 patch[self._m.META_PARENT_HASH] = line[10:]
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
175 else:
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
176 state = 'commit_msg'
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
177 if state == 'commit_msg':
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
178 if line.startswith(u'diff --git a/'):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
179 state = 'diff'
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
180 patch[self._m.META_DIFF_IDX] = patch_idx + idx + 1
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
181 else:
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
182 commit_msg.append(line)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
183 if state == 'diff':
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
184 if line.startswith(u'# ') or idx == len(lines)-1:
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
185 # a new patch is starting or we have reached end of patches
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
186 patch[self._m.META_COMMIT_MSG] = u'\n'.join(commit_msg)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
187 patch[self._m.META_DIFF] = u'\n'.join(diff)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
188 patches.append(patch)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
189 if idx == len(lines)-1:
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
190 del lines[:]
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
191 else:
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
192 del lines[:idx]
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
193 break
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
194 else:
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
195 diff.append(line)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
196 return patches