annotate sat/plugins/plugin_merge_req_mercurial.py @ 3023:1f74cd0f22c3

plugin XEP-0313: log issues in addToHistory instead of raising error
author Goffi <goffi@goffi.org>
date Wed, 24 Jul 2019 07:51:48 +0200
parents 181735d1b062
children ab2696e34d29
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
2771
003b8b4b56a7 date update
Goffi <goffi@goffi.org>
parents: 2765
diff changeset
5 # Copyright (C) 2009-2019 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
2793
181735d1b062 plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
20 import re
181735d1b062 plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
21 from twisted.python.procutils import which
181735d1b062 plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
22 from sat.tools.common import async_process
181735d1b062 plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
23 from sat.tools import utils
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from sat.core.i18n import _, D_
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from sat.core.constants import Const as C
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from sat.core import exceptions
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
2793
181735d1b062 plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
45 class MercurialProtocol(async_process.CommandProtocol):
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 """handle hg commands"""
2793
181735d1b062 plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
47 name = u"Mercurial"
181735d1b062 plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
48 command = None
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
49
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 @classmethod
2544
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
51 def run(cls, path, command, *args, **kwargs):
2620
72f6f37ab648 core: some more line limiting
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
52 """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
53
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 @param path(unicode): path to the repository
2793
181735d1b062 plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
55 @param command(unicode): hg command to run
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 """
2793
181735d1b062 plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
57 assert u"path" not in kwargs
181735d1b062 plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
58 kwargs["path"] = path
181735d1b062 plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
59 # FIXME: we have to use this workaround because Twisted's protocol.ProcessProtocol
181735d1b062 plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
60 # is not using new style classes. This can be removed once moved to
181735d1b062 plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
61 # Python 3 (super can be used normally then).
181735d1b062 plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
62 d = async_process.CommandProtocol.run.__func__(cls, command, *args, **kwargs)
181735d1b062 plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
63 d.addErrback(utils.logError)
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 return d
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
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 class MercurialHandler(object):
2544
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
68 data_types = (u'mercurial_changeset',)
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
69
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 def __init__(self, host):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 log.info(_(u"Mercurial merge request handler initialization"))
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 try:
2793
181735d1b062 plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
73 MercurialProtocol.command = which('hg')[0]
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 except IndexError:
2620
72f6f37ab648 core: some more line limiting
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
75 raise exceptions.NotFound(_(u"Mercurial executable (hg) not found, "
72f6f37ab648 core: some more line limiting
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
76 u"can't use Mercurial handler"))
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 self.host = host
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 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
79 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
80
2793
181735d1b062 plugin mr mercurial, tools(common/utils): moved command protocol to a new module
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
81
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 def check(self, repository):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 d = MercurialProtocol.run(repository, 'identify')
2765
378188abe941 misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents: 2627
diff changeset
84 d.addCallback(lambda __: True)
378188abe941 misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents: 2627
diff changeset
85 d.addErrback(lambda __: False)
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 return d
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
87
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 def export(self, repository):
2620
72f6f37ab648 core: some more line limiting
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
89 return MercurialProtocol.run(repository, 'export', '-g', '-r', 'outgoing()',
72f6f37ab648 core: some more line limiting
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
90 '--encoding=utf-8')
2449
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 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
93 parsed_data = self.parse(data)
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
94 try:
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
95 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
96 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
97 except Exception:
a64887289931 plugin merge-requests, mercurial merge-requests: merge request import implementation
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
98 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
99 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
100 parsed_name=parsed_name)
72f6f37ab648 core: some more line limiting
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
101 return MercurialProtocol.run(repository, 'qimport', '-g', '--name', name,
72f6f37ab648 core: some more line limiting
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
102 '--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
103
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 def parse(self, data, data_type=None):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 lines = data.splitlines()
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 total_lines = len(lines)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 patches = []
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 while lines:
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 patch = {}
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 commit_msg = []
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 diff = []
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 state = 'init'
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
113 if lines[0] != '# HG changeset patch':
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 raise exceptions.DataError(_(u'invalid changeset signature'))
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 # line index of this patch in the whole data
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
116 patch_idx = total_lines - len(lines)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 del lines[0]
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
118
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 for idx, line in enumerate(lines):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 if state == 'init':
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 if line.startswith(u'# '):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
122 if line.startswith(u'# User '):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 elems = line[7:].split()
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 if not elems:
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 continue
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 last = elems[-1]
2620
72f6f37ab648 core: some more line limiting
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
127 if (last.startswith(u'<') and last.endswith(u'>')
72f6f37ab648 core: some more line limiting
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
128 and u'@' in last):
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 patch[self._m.META_EMAIL] = elems.pop()[1:-1]
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
130 patch[self._m.META_AUTHOR] = u' '.join(elems)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
131 elif line.startswith(u'# Date '):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 time_data = line[7:].split()
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 if len(time_data) != 2:
2620
72f6f37ab648 core: some more line limiting
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
134 log.warning(_(u'unexpected time data: {data}')
72f6f37ab648 core: some more line limiting
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
135 .format(data=line[7:]))
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 continue
2620
72f6f37ab648 core: some more line limiting
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
137 patch[self._m.META_TIMESTAMP] = (int(time_data[0])
72f6f37ab648 core: some more line limiting
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
138 + int(time_data[1]))
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 elif line.startswith(u'# Node ID '):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 patch[self._m.META_HASH] = line[10:]
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 elif line.startswith(u'# Parent '):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 patch[self._m.META_PARENT_HASH] = line[10:]
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
143 else:
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 state = 'commit_msg'
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 if state == 'commit_msg':
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
146 if line.startswith(u'diff --git a/'):
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 state = 'diff'
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
148 patch[self._m.META_DIFF_IDX] = patch_idx + idx + 1
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
149 else:
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 commit_msg.append(line)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
151 if state == 'diff':
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 if line.startswith(u'# ') or idx == len(lines)-1:
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
153 # 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
154 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
155 # 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
156 diff.append(line)
2449
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
157 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
158 patch[self._m.META_DIFF] = u'\n'.join(diff)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
159 patches.append(patch)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 if idx == len(lines)-1:
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
161 del lines[:]
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
162 else:
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
163 del lines[:idx]
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
164 break
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 else:
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
166 diff.append(line)
67942ba2ee55 plugin merge requests Mercurial: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
167 return patches