annotate sat/plugins/plugin_merge_req_mercurial.py @ 2712:c3f59c1dcb0a

core (memory/disco): normalise booleans in extensions
author Goffi <goffi@goffi.org>
date Sat, 01 Dec 2018 14:40:32 +0100
parents 163aab916bcf
children 378188abe941
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
2559
1362cabad225 plugin merge-request mercurial: better name sanitization for import
Goffi <goffi@goffi.org>
parents: 2544
diff changeset
26 import re
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from sat.core.log import getLogger
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 log = getLogger(__name__)
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
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 PLUGIN_INFO = {
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 C.PI_NAME: "Mercurial Merge Request handler",
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 C.PI_IMPORT_NAME: "MERGE_REQUEST_MERCURIAL",
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 C.PI_TYPE: C.PLUG_TYPE_MISC,
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 C.PI_DEPENDENCIES: ["MERGE_REQUESTS"],
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 C.PI_MAIN: "MercurialHandler",
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 C.PI_HANDLER: "no",
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 C.PI_DESCRIPTION: _(u"""Merge request handler for Mercurial""")
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
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 SHORT_DESC = D_(u"handle Mercurial repository")
2623
49533de4540b plugin merge request (mercurial): clean item_id for import (some chars are not accepted in name)
Goffi <goffi@goffi.org>
parents: 2621
diff changeset
42 CLEAN_RE = re.compile(ur'[^\w -._]', flags=re.UNICODE)
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
43
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
44
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 class MercurialProtocol(protocol.ProcessProtocol):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 """handle hg commands"""
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 hg = None
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
48
2544
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
49 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
50 """
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
51 @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
52 @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
53 """
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
54 self._stdin = stdin
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 self._deferred = deferred
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 self.data = []
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
57
2544
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
58 def connectionMade(self):
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
59 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
60 self.transport.write(self._stdin)
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
61 self.transport.closeStdin()
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
62
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 def outReceived(self, data):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 self.data.append(data)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
65
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 def errReceived(self, data):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 self.data.append(data)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
68
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 def processEnded(self, reason):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 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
71 if (reason.value.exitCode == 0):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 log.debug(_('Mercurial command succeed'))
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 self._deferred.callback(data)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 else:
2620
72f6f37ab648 core: some more line limiting
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
75 msg = (_(u"Can't complete Mercurial command (error code: {code}): {message}")
72f6f37ab648 core: some more line limiting
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
76 .format(code = reason.value.exitCode, message = data))
2544
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
77 log.warning(msg)
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
78 self._deferred.errback(Failure(RuntimeError(msg)))
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
79
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 @classmethod
2544
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
81 def run(cls, path, command, *args, **kwargs):
2620
72f6f37ab648 core: some more line limiting
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
82 """Create a new MercurialRegisterProtocol and execute the given mercurial command.
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
83
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 @param path(unicode): path to the repository
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 @param command(unicode): command to run
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 @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
87 @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
88 can only be:
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
89 - stdin(unicode, None): data to push to standard input
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 @return ((D)):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 """
2544
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
92 stdin = kwargs.pop('stdin', None)
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
93 if kwargs:
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
94 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
95 if stdin is not None:
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
96 stdin = stdin.encode('utf-8')
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 d = defer.Deferred()
2544
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
98 mercurial_prot = MercurialProtocol(d, stdin=stdin)
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 cmd_args = [cls.hg, command.encode('utf-8')]
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 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
101 reactor.spawnProcess(mercurial_prot,
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 cls.hg,
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 cmd_args,
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 path=path.encode('utf-8'))
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 return d
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
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 class MercurialHandler(object):
2544
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
109 data_types = (u'mercurial_changeset',)
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
110
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 def __init__(self, host):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 log.info(_(u"Mercurial merge request handler initialization"))
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
113 try:
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 MercurialProtocol.hg = which('hg')[0]
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 except IndexError:
2620
72f6f37ab648 core: some more line limiting
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
116 raise exceptions.NotFound(_(u"Mercurial executable (hg) not found, "
72f6f37ab648 core: some more line limiting
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
117 u"can't use Mercurial handler"))
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 self.host = host
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 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
120 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
121
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
122 def check(self, repository):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 d = MercurialProtocol.run(repository, 'identify')
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 d.addCallback(lambda dummy: True)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 d.addErrback(lambda dummy: False)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 return d
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
127
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 def export(self, repository):
2620
72f6f37ab648 core: some more line limiting
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
129 return MercurialProtocol.run(repository, 'export', '-g', '-r', 'outgoing()',
72f6f37ab648 core: some more line limiting
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
130 '--encoding=utf-8')
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
131
2544
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
132 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
133 parsed_data = self.parse(data)
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
134 try:
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
135 parsed_name = parsed_data[0][u'commit_msg'].split(u'\n')[0]
2623
49533de4540b plugin merge request (mercurial): clean item_id for import (some chars are not accepted in name)
Goffi <goffi@goffi.org>
parents: 2621
diff changeset
136 parsed_name = CLEAN_RE.sub(u'', parsed_name)[:40]
2544
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
137 except Exception:
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
138 parsed_name = u''
2623
49533de4540b plugin merge request (mercurial): clean item_id for import (some chars are not accepted in name)
Goffi <goffi@goffi.org>
parents: 2621
diff changeset
139 name = u'mr_{item_id}_{parsed_name}'.format(item_id=CLEAN_RE.sub(u'', item_id),
2620
72f6f37ab648 core: some more line limiting
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
140 parsed_name=parsed_name)
72f6f37ab648 core: some more line limiting
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
141 return MercurialProtocol.run(repository, 'qimport', '-g', '--name', name,
72f6f37ab648 core: some more line limiting
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
142 '--encoding=utf-8', '-', stdin=data)
2544
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
143
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 def parse(self, data, data_type=None):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 lines = data.splitlines()
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
146 total_lines = len(lines)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 patches = []
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
148 while lines:
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
149 patch = {}
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 commit_msg = []
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
151 diff = []
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 state = 'init'
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
153 if lines[0] != '# HG changeset patch':
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 raise exceptions.DataError(_(u'invalid changeset signature'))
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 # line index of this patch in the whole data
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 patch_idx = total_lines - len(lines)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
157 del lines[0]
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
158
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
159 for idx, line in enumerate(lines):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 if state == 'init':
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
161 if line.startswith(u'# '):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
162 if line.startswith(u'# User '):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
163 elems = line[7:].split()
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
164 if not elems:
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 continue
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
166 last = elems[-1]
2620
72f6f37ab648 core: some more line limiting
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
167 if (last.startswith(u'<') and last.endswith(u'>')
72f6f37ab648 core: some more line limiting
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
168 and u'@' in last):
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
169 patch[self._m.META_EMAIL] = elems.pop()[1:-1]
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
170 patch[self._m.META_AUTHOR] = u' '.join(elems)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
171 elif line.startswith(u'# Date '):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
172 time_data = line[7:].split()
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
173 if len(time_data) != 2:
2620
72f6f37ab648 core: some more line limiting
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
174 log.warning(_(u'unexpected time data: {data}')
72f6f37ab648 core: some more line limiting
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
175 .format(data=line[7:]))
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
176 continue
2620
72f6f37ab648 core: some more line limiting
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
177 patch[self._m.META_TIMESTAMP] = (int(time_data[0])
72f6f37ab648 core: some more line limiting
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
178 + int(time_data[1]))
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
179 elif line.startswith(u'# Node ID '):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
180 patch[self._m.META_HASH] = line[10:]
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
181 elif line.startswith(u'# Parent '):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
182 patch[self._m.META_PARENT_HASH] = line[10:]
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
183 else:
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
184 state = 'commit_msg'
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
185 if state == 'commit_msg':
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
186 if line.startswith(u'diff --git a/'):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
187 state = 'diff'
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
188 patch[self._m.META_DIFF_IDX] = patch_idx + idx + 1
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
189 else:
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
190 commit_msg.append(line)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
191 if state == 'diff':
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
192 if line.startswith(u'# ') or idx == len(lines)-1:
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
193 # a new patch is starting or we have reached end of patches
2627
163aab916bcf plugin merge-request/mercurial: fixed missing last line in diff while parsing patch
Goffi <goffi@goffi.org>
parents: 2623
diff changeset
194 if idx == len(lines)-1:
163aab916bcf plugin merge-request/mercurial: fixed missing last line in diff while parsing patch
Goffi <goffi@goffi.org>
parents: 2623
diff changeset
195 # end of patches, we need to keep the line
163aab916bcf plugin merge-request/mercurial: fixed missing last line in diff while parsing patch
Goffi <goffi@goffi.org>
parents: 2623
diff changeset
196 diff.append(line)
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
197 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
198 patch[self._m.META_DIFF] = u'\n'.join(diff)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
199 patches.append(patch)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
200 if idx == len(lines)-1:
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
201 del lines[:]
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
202 else:
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
203 del lines[:idx]
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
204 break
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
205 else:
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
206 diff.append(line)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
207 return patches