Show More
@@ -0,0 +1,11 b'' | |||||
|
1 | #!/usr/bin/env python | |||
|
2 | ||||
|
3 | from hgext import color | |||
|
4 | ||||
|
5 | # ensure errors aren't buffered | |||
|
6 | testui = color.colorui() | |||
|
7 | testui.pushbuffer() | |||
|
8 | testui.write('buffered\n') | |||
|
9 | testui.warn('warning\n') | |||
|
10 | testui.write_err('error\n') | |||
|
11 | print repr(testui.popbuffer()) |
@@ -1,286 +1,286 b'' | |||||
1 | # color.py color output for the status and qseries commands |
|
1 | # color.py color output for the status and qseries commands | |
2 | # |
|
2 | # | |
3 | # Copyright (C) 2007 Kevin Christen <kevin.christen@gmail.com> |
|
3 | # Copyright (C) 2007 Kevin Christen <kevin.christen@gmail.com> | |
4 | # |
|
4 | # | |
5 | # This program is free software; you can redistribute it and/or modify it |
|
5 | # This program is free software; you can redistribute it and/or modify it | |
6 | # under the terms of the GNU General Public License as published by the |
|
6 | # under the terms of the GNU General Public License as published by the | |
7 | # Free Software Foundation; either version 2 of the License, or (at your |
|
7 | # Free Software Foundation; either version 2 of the License, or (at your | |
8 | # option) any later version. |
|
8 | # option) any later version. | |
9 | # |
|
9 | # | |
10 | # This program is distributed in the hope that it will be useful, but |
|
10 | # This program is distributed in the hope that it will be useful, but | |
11 | # WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | # WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
|
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General | |
13 | # Public License for more details. |
|
13 | # Public License for more details. | |
14 | # |
|
14 | # | |
15 | # You should have received a copy of the GNU General Public License along |
|
15 | # You should have received a copy of the GNU General Public License along | |
16 | # with this program; if not, write to the Free Software Foundation, Inc., |
|
16 | # with this program; if not, write to the Free Software Foundation, Inc., | |
17 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
|
17 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 |
|
18 | |||
19 | '''colorize output from some commands |
|
19 | '''colorize output from some commands | |
20 |
|
20 | |||
21 | This extension modifies the status and resolve commands to add color to their |
|
21 | This extension modifies the status and resolve commands to add color to their | |
22 | output to reflect file status, the qseries command to add color to reflect |
|
22 | output to reflect file status, the qseries command to add color to reflect | |
23 | patch status (applied, unapplied, missing), and to diff-related |
|
23 | patch status (applied, unapplied, missing), and to diff-related | |
24 | commands to highlight additions, removals, diff headers, and trailing |
|
24 | commands to highlight additions, removals, diff headers, and trailing | |
25 | whitespace. |
|
25 | whitespace. | |
26 |
|
26 | |||
27 | Other effects in addition to color, like bold and underlined text, are |
|
27 | Other effects in addition to color, like bold and underlined text, are | |
28 | also available. Effects are rendered with the ECMA-48 SGR control |
|
28 | also available. Effects are rendered with the ECMA-48 SGR control | |
29 | function (aka ANSI escape codes). This module also provides the |
|
29 | function (aka ANSI escape codes). This module also provides the | |
30 | render_text function, which can be used to add effects to any text. |
|
30 | render_text function, which can be used to add effects to any text. | |
31 |
|
31 | |||
32 | Default effects may be overridden from the .hgrc file:: |
|
32 | Default effects may be overridden from the .hgrc file:: | |
33 |
|
33 | |||
34 | [color] |
|
34 | [color] | |
35 | status.modified = blue bold underline red_background |
|
35 | status.modified = blue bold underline red_background | |
36 | status.added = green bold |
|
36 | status.added = green bold | |
37 | status.removed = red bold blue_background |
|
37 | status.removed = red bold blue_background | |
38 | status.deleted = cyan bold underline |
|
38 | status.deleted = cyan bold underline | |
39 | status.unknown = magenta bold underline |
|
39 | status.unknown = magenta bold underline | |
40 | status.ignored = black bold |
|
40 | status.ignored = black bold | |
41 |
|
41 | |||
42 | # 'none' turns off all effects |
|
42 | # 'none' turns off all effects | |
43 | status.clean = none |
|
43 | status.clean = none | |
44 | status.copied = none |
|
44 | status.copied = none | |
45 |
|
45 | |||
46 | qseries.applied = blue bold underline |
|
46 | qseries.applied = blue bold underline | |
47 | qseries.unapplied = black bold |
|
47 | qseries.unapplied = black bold | |
48 | qseries.missing = red bold |
|
48 | qseries.missing = red bold | |
49 |
|
49 | |||
50 | diff.diffline = bold |
|
50 | diff.diffline = bold | |
51 | diff.extended = cyan bold |
|
51 | diff.extended = cyan bold | |
52 | diff.file_a = red bold |
|
52 | diff.file_a = red bold | |
53 | diff.file_b = green bold |
|
53 | diff.file_b = green bold | |
54 | diff.hunk = magenta |
|
54 | diff.hunk = magenta | |
55 | diff.deleted = red |
|
55 | diff.deleted = red | |
56 | diff.inserted = green |
|
56 | diff.inserted = green | |
57 | diff.changed = white |
|
57 | diff.changed = white | |
58 | diff.trailingwhitespace = bold red_background |
|
58 | diff.trailingwhitespace = bold red_background | |
59 |
|
59 | |||
60 | resolve.unresolved = red bold |
|
60 | resolve.unresolved = red bold | |
61 | resolve.resolved = green bold |
|
61 | resolve.resolved = green bold | |
62 |
|
62 | |||
63 | bookmarks.current = green |
|
63 | bookmarks.current = green | |
64 |
|
64 | |||
65 | The color extension will try to detect whether to use ANSI codes or |
|
65 | The color extension will try to detect whether to use ANSI codes or | |
66 | Win32 console APIs, unless it is made explicit:: |
|
66 | Win32 console APIs, unless it is made explicit:: | |
67 |
|
67 | |||
68 | [color] |
|
68 | [color] | |
69 | mode = ansi |
|
69 | mode = ansi | |
70 |
|
70 | |||
71 | Any value other than 'ansi', 'win32', or 'auto' will disable color. |
|
71 | Any value other than 'ansi', 'win32', or 'auto' will disable color. | |
72 |
|
72 | |||
73 | ''' |
|
73 | ''' | |
74 |
|
74 | |||
75 | import os, sys |
|
75 | import os, sys | |
76 |
|
76 | |||
77 | from mercurial import commands, dispatch, extensions, ui as uimod |
|
77 | from mercurial import commands, dispatch, extensions, ui as uimod | |
78 | from mercurial.i18n import _ |
|
78 | from mercurial.i18n import _ | |
79 |
|
79 | |||
80 | # start and stop parameters for effects |
|
80 | # start and stop parameters for effects | |
81 | _effects = {'none': 0, 'black': 30, 'red': 31, 'green': 32, 'yellow': 33, |
|
81 | _effects = {'none': 0, 'black': 30, 'red': 31, 'green': 32, 'yellow': 33, | |
82 | 'blue': 34, 'magenta': 35, 'cyan': 36, 'white': 37, 'bold': 1, |
|
82 | 'blue': 34, 'magenta': 35, 'cyan': 36, 'white': 37, 'bold': 1, | |
83 | 'italic': 3, 'underline': 4, 'inverse': 7, |
|
83 | 'italic': 3, 'underline': 4, 'inverse': 7, | |
84 | 'black_background': 40, 'red_background': 41, |
|
84 | 'black_background': 40, 'red_background': 41, | |
85 | 'green_background': 42, 'yellow_background': 43, |
|
85 | 'green_background': 42, 'yellow_background': 43, | |
86 | 'blue_background': 44, 'purple_background': 45, |
|
86 | 'blue_background': 44, 'purple_background': 45, | |
87 | 'cyan_background': 46, 'white_background': 47} |
|
87 | 'cyan_background': 46, 'white_background': 47} | |
88 |
|
88 | |||
89 | _styles = {'grep.match': 'red bold', |
|
89 | _styles = {'grep.match': 'red bold', | |
90 | 'diff.changed': 'white', |
|
90 | 'diff.changed': 'white', | |
91 | 'diff.deleted': 'red', |
|
91 | 'diff.deleted': 'red', | |
92 | 'diff.diffline': 'bold', |
|
92 | 'diff.diffline': 'bold', | |
93 | 'diff.extended': 'cyan bold', |
|
93 | 'diff.extended': 'cyan bold', | |
94 | 'diff.file_a': 'red bold', |
|
94 | 'diff.file_a': 'red bold', | |
95 | 'diff.file_b': 'green bold', |
|
95 | 'diff.file_b': 'green bold', | |
96 | 'diff.hunk': 'magenta', |
|
96 | 'diff.hunk': 'magenta', | |
97 | 'diff.inserted': 'green', |
|
97 | 'diff.inserted': 'green', | |
98 | 'diff.trailingwhitespace': 'bold red_background', |
|
98 | 'diff.trailingwhitespace': 'bold red_background', | |
99 | 'diffstat.deleted': 'red', |
|
99 | 'diffstat.deleted': 'red', | |
100 | 'diffstat.inserted': 'green', |
|
100 | 'diffstat.inserted': 'green', | |
101 | 'log.changeset': 'yellow', |
|
101 | 'log.changeset': 'yellow', | |
102 | 'resolve.resolved': 'green bold', |
|
102 | 'resolve.resolved': 'green bold', | |
103 | 'resolve.unresolved': 'red bold', |
|
103 | 'resolve.unresolved': 'red bold', | |
104 | 'status.added': 'green bold', |
|
104 | 'status.added': 'green bold', | |
105 | 'status.clean': 'none', |
|
105 | 'status.clean': 'none', | |
106 | 'status.copied': 'none', |
|
106 | 'status.copied': 'none', | |
107 | 'status.deleted': 'cyan bold underline', |
|
107 | 'status.deleted': 'cyan bold underline', | |
108 | 'status.ignored': 'black bold', |
|
108 | 'status.ignored': 'black bold', | |
109 | 'status.modified': 'blue bold', |
|
109 | 'status.modified': 'blue bold', | |
110 | 'status.removed': 'red bold', |
|
110 | 'status.removed': 'red bold', | |
111 | 'status.unknown': 'magenta bold underline'} |
|
111 | 'status.unknown': 'magenta bold underline'} | |
112 |
|
112 | |||
113 |
|
113 | |||
114 | def render_effects(text, effects): |
|
114 | def render_effects(text, effects): | |
115 | 'Wrap text in commands to turn on each effect.' |
|
115 | 'Wrap text in commands to turn on each effect.' | |
116 | if not text: |
|
116 | if not text: | |
117 | return text |
|
117 | return text | |
118 | start = [str(_effects[e]) for e in ['none'] + effects.split()] |
|
118 | start = [str(_effects[e]) for e in ['none'] + effects.split()] | |
119 | start = '\033[' + ';'.join(start) + 'm' |
|
119 | start = '\033[' + ';'.join(start) + 'm' | |
120 | stop = '\033[' + str(_effects['none']) + 'm' |
|
120 | stop = '\033[' + str(_effects['none']) + 'm' | |
121 | return ''.join([start, text, stop]) |
|
121 | return ''.join([start, text, stop]) | |
122 |
|
122 | |||
123 | def extstyles(): |
|
123 | def extstyles(): | |
124 | for name, ext in extensions.extensions(): |
|
124 | for name, ext in extensions.extensions(): | |
125 | _styles.update(getattr(ext, 'colortable', {})) |
|
125 | _styles.update(getattr(ext, 'colortable', {})) | |
126 |
|
126 | |||
127 | def configstyles(ui): |
|
127 | def configstyles(ui): | |
128 | for status, cfgeffects in ui.configitems('color'): |
|
128 | for status, cfgeffects in ui.configitems('color'): | |
129 | if '.' not in status: |
|
129 | if '.' not in status: | |
130 | continue |
|
130 | continue | |
131 | cfgeffects = ui.configlist('color', status) |
|
131 | cfgeffects = ui.configlist('color', status) | |
132 | if cfgeffects: |
|
132 | if cfgeffects: | |
133 | good = [] |
|
133 | good = [] | |
134 | for e in cfgeffects: |
|
134 | for e in cfgeffects: | |
135 | if e in _effects: |
|
135 | if e in _effects: | |
136 | good.append(e) |
|
136 | good.append(e) | |
137 | else: |
|
137 | else: | |
138 | ui.warn(_("ignoring unknown color/effect %r " |
|
138 | ui.warn(_("ignoring unknown color/effect %r " | |
139 | "(configured in color.%s)\n") |
|
139 | "(configured in color.%s)\n") | |
140 | % (e, status)) |
|
140 | % (e, status)) | |
141 | _styles[status] = ' '.join(good) |
|
141 | _styles[status] = ' '.join(good) | |
142 |
|
142 | |||
143 | class colorui(uimod.ui): |
|
143 | class colorui(uimod.ui): | |
144 | def popbuffer(self, labeled=False): |
|
144 | def popbuffer(self, labeled=False): | |
145 | if labeled: |
|
145 | if labeled: | |
146 | return ''.join(self.label(a, label) for a, label |
|
146 | return ''.join(self.label(a, label) for a, label | |
147 | in self._buffers.pop()) |
|
147 | in self._buffers.pop()) | |
148 | return ''.join(a for a, label in self._buffers.pop()) |
|
148 | return ''.join(a for a, label in self._buffers.pop()) | |
149 |
|
149 | |||
150 | _colormode = 'ansi' |
|
150 | _colormode = 'ansi' | |
151 | def write(self, *args, **opts): |
|
151 | def write(self, *args, **opts): | |
152 | label = opts.get('label', '') |
|
152 | label = opts.get('label', '') | |
153 | if self._buffers: |
|
153 | if self._buffers: | |
154 | self._buffers[-1].extend([(str(a), label) for a in args]) |
|
154 | self._buffers[-1].extend([(str(a), label) for a in args]) | |
155 | elif self._colormode == 'win32': |
|
155 | elif self._colormode == 'win32': | |
156 | for a in args: |
|
156 | for a in args: | |
157 |
win32print(a, |
|
157 | win32print(a, super(colorui, self).write, **opts) | |
158 | else: |
|
158 | else: | |
159 | return super(colorui, self).write( |
|
159 | return super(colorui, self).write( | |
160 | *[self.label(str(a), label) for a in args], **opts) |
|
160 | *[self.label(str(a), label) for a in args], **opts) | |
161 |
|
161 | |||
162 | def write_err(self, *args, **opts): |
|
162 | def write_err(self, *args, **opts): | |
163 | label = opts.get('label', '') |
|
163 | label = opts.get('label', '') | |
164 | if self._colormode == 'win32': |
|
164 | if self._colormode == 'win32': | |
165 | for a in args: |
|
165 | for a in args: | |
166 |
win32print(a, |
|
166 | win32print(a, super(colorui, self).write_err, **opts) | |
167 | else: |
|
167 | else: | |
168 | return super(colorui, self).write( |
|
168 | return super(colorui, self).write_err( | |
169 | *[self.label(str(a), label) for a in args], **opts) |
|
169 | *[self.label(str(a), label) for a in args], **opts) | |
170 |
|
170 | |||
171 | def label(self, msg, label): |
|
171 | def label(self, msg, label): | |
172 | effects = [] |
|
172 | effects = [] | |
173 | for l in label.split(): |
|
173 | for l in label.split(): | |
174 | s = _styles.get(l, '') |
|
174 | s = _styles.get(l, '') | |
175 | if s: |
|
175 | if s: | |
176 | effects.append(s) |
|
176 | effects.append(s) | |
177 | effects = ''.join(effects) |
|
177 | effects = ''.join(effects) | |
178 | if effects: |
|
178 | if effects: | |
179 | return '\n'.join([render_effects(s, effects) |
|
179 | return '\n'.join([render_effects(s, effects) | |
180 | for s in msg.split('\n')]) |
|
180 | for s in msg.split('\n')]) | |
181 | return msg |
|
181 | return msg | |
182 |
|
182 | |||
183 |
|
183 | |||
184 | def uisetup(ui): |
|
184 | def uisetup(ui): | |
185 | if ui.plain(): |
|
185 | if ui.plain(): | |
186 | return |
|
186 | return | |
187 | mode = ui.config('color', 'mode', 'auto') |
|
187 | mode = ui.config('color', 'mode', 'auto') | |
188 | if mode == 'auto': |
|
188 | if mode == 'auto': | |
189 | if os.name == 'nt' and 'TERM' not in os.environ: |
|
189 | if os.name == 'nt' and 'TERM' not in os.environ: | |
190 | # looks line a cmd.exe console, use win32 API or nothing |
|
190 | # looks line a cmd.exe console, use win32 API or nothing | |
191 | mode = w32effects and 'win32' or 'none' |
|
191 | mode = w32effects and 'win32' or 'none' | |
192 | else: |
|
192 | else: | |
193 | mode = 'ansi' |
|
193 | mode = 'ansi' | |
194 | if mode == 'win32': |
|
194 | if mode == 'win32': | |
195 | if w32effects is None: |
|
195 | if w32effects is None: | |
196 | # only warn if color.mode is explicitly set to win32 |
|
196 | # only warn if color.mode is explicitly set to win32 | |
197 | ui.warn(_('win32console not found, please install pywin32\n')) |
|
197 | ui.warn(_('win32console not found, please install pywin32\n')) | |
198 | return |
|
198 | return | |
199 | _effects.update(w32effects) |
|
199 | _effects.update(w32effects) | |
200 | elif mode != 'ansi': |
|
200 | elif mode != 'ansi': | |
201 | return |
|
201 | return | |
202 | def colorcmd(orig, ui_, opts, cmd, cmdfunc): |
|
202 | def colorcmd(orig, ui_, opts, cmd, cmdfunc): | |
203 | if (opts['color'] == 'always' or |
|
203 | if (opts['color'] == 'always' or | |
204 | (opts['color'] == 'auto' and (os.environ.get('TERM') != 'dumb' |
|
204 | (opts['color'] == 'auto' and (os.environ.get('TERM') != 'dumb' | |
205 | and ui_.formatted()))): |
|
205 | and ui_.formatted()))): | |
206 | colorui._colormode = mode |
|
206 | colorui._colormode = mode | |
207 | colorui.__bases__ = (ui_.__class__,) |
|
207 | colorui.__bases__ = (ui_.__class__,) | |
208 | ui_.__class__ = colorui |
|
208 | ui_.__class__ = colorui | |
209 | extstyles() |
|
209 | extstyles() | |
210 | configstyles(ui_) |
|
210 | configstyles(ui_) | |
211 | return orig(ui_, opts, cmd, cmdfunc) |
|
211 | return orig(ui_, opts, cmd, cmdfunc) | |
212 | extensions.wrapfunction(dispatch, '_runcommand', colorcmd) |
|
212 | extensions.wrapfunction(dispatch, '_runcommand', colorcmd) | |
213 |
|
213 | |||
214 | commands.globalopts.append(('', 'color', 'auto', |
|
214 | commands.globalopts.append(('', 'color', 'auto', | |
215 | _("when to colorize (always, auto, or never)"), |
|
215 | _("when to colorize (always, auto, or never)"), | |
216 | _('TYPE'))) |
|
216 | _('TYPE'))) | |
217 |
|
217 | |||
218 | try: |
|
218 | try: | |
219 | import re, pywintypes |
|
219 | import re, pywintypes | |
220 | from win32console import * |
|
220 | from win32console import * | |
221 |
|
221 | |||
222 | # http://msdn.microsoft.com/en-us/library/ms682088%28VS.85%29.aspx |
|
222 | # http://msdn.microsoft.com/en-us/library/ms682088%28VS.85%29.aspx | |
223 | w32effects = { |
|
223 | w32effects = { | |
224 | 'none': 0, |
|
224 | 'none': 0, | |
225 | 'black': 0, |
|
225 | 'black': 0, | |
226 | 'red': FOREGROUND_RED, |
|
226 | 'red': FOREGROUND_RED, | |
227 | 'green': FOREGROUND_GREEN, |
|
227 | 'green': FOREGROUND_GREEN, | |
228 | 'yellow': FOREGROUND_RED | FOREGROUND_GREEN, |
|
228 | 'yellow': FOREGROUND_RED | FOREGROUND_GREEN, | |
229 | 'blue': FOREGROUND_BLUE, |
|
229 | 'blue': FOREGROUND_BLUE, | |
230 | 'magenta': FOREGROUND_BLUE | FOREGROUND_RED, |
|
230 | 'magenta': FOREGROUND_BLUE | FOREGROUND_RED, | |
231 | 'cyan': FOREGROUND_BLUE | FOREGROUND_GREEN, |
|
231 | 'cyan': FOREGROUND_BLUE | FOREGROUND_GREEN, | |
232 | 'white': FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, |
|
232 | 'white': FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, | |
233 | 'bold': FOREGROUND_INTENSITY, |
|
233 | 'bold': FOREGROUND_INTENSITY, | |
234 | 'black_background': 0, |
|
234 | 'black_background': 0, | |
235 | 'red_background': BACKGROUND_RED, |
|
235 | 'red_background': BACKGROUND_RED, | |
236 | 'green_background': BACKGROUND_GREEN, |
|
236 | 'green_background': BACKGROUND_GREEN, | |
237 | 'yellow_background': BACKGROUND_RED | BACKGROUND_GREEN, |
|
237 | 'yellow_background': BACKGROUND_RED | BACKGROUND_GREEN, | |
238 | 'blue_background': BACKGROUND_BLUE, |
|
238 | 'blue_background': BACKGROUND_BLUE, | |
239 | 'purple_background': BACKGROUND_BLUE | BACKGROUND_RED, |
|
239 | 'purple_background': BACKGROUND_BLUE | BACKGROUND_RED, | |
240 | 'cyan_background': BACKGROUND_BLUE | BACKGROUND_GREEN, |
|
240 | 'cyan_background': BACKGROUND_BLUE | BACKGROUND_GREEN, | |
241 | 'white_background': BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE, |
|
241 | 'white_background': BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE, | |
242 | 'bold_background': BACKGROUND_INTENSITY, |
|
242 | 'bold_background': BACKGROUND_INTENSITY, | |
243 | 'underline': COMMON_LVB_UNDERSCORE, # double-byte charsets only |
|
243 | 'underline': COMMON_LVB_UNDERSCORE, # double-byte charsets only | |
244 | 'inverse': COMMON_LVB_REVERSE_VIDEO, # double-byte charsets only |
|
244 | 'inverse': COMMON_LVB_REVERSE_VIDEO, # double-byte charsets only | |
245 | } |
|
245 | } | |
246 |
|
246 | |||
247 | stdout = GetStdHandle(STD_OUTPUT_HANDLE) |
|
247 | stdout = GetStdHandle(STD_OUTPUT_HANDLE) | |
248 | try: |
|
248 | try: | |
249 | origattr = stdout.GetConsoleScreenBufferInfo()['Attributes'] |
|
249 | origattr = stdout.GetConsoleScreenBufferInfo()['Attributes'] | |
250 | except pywintypes.error: |
|
250 | except pywintypes.error: | |
251 | # stdout may be defined but not support |
|
251 | # stdout may be defined but not support | |
252 | # GetConsoleScreenBufferInfo(), when called from subprocess or |
|
252 | # GetConsoleScreenBufferInfo(), when called from subprocess or | |
253 | # redirected. |
|
253 | # redirected. | |
254 | raise ImportError() |
|
254 | raise ImportError() | |
255 | ansire = re.compile('\033\[([^m]*)m([^\033]*)(.*)', re.MULTILINE | re.DOTALL) |
|
255 | ansire = re.compile('\033\[([^m]*)m([^\033]*)(.*)', re.MULTILINE | re.DOTALL) | |
256 |
|
256 | |||
257 | def win32print(text, orig, **opts): |
|
257 | def win32print(text, orig, **opts): | |
258 | label = opts.get('label', '') |
|
258 | label = opts.get('label', '') | |
259 | attr = 0 |
|
259 | attr = 0 | |
260 |
|
260 | |||
261 | # determine console attributes based on labels |
|
261 | # determine console attributes based on labels | |
262 | for l in label.split(): |
|
262 | for l in label.split(): | |
263 | style = _styles.get(l, '') |
|
263 | style = _styles.get(l, '') | |
264 | for effect in style.split(): |
|
264 | for effect in style.split(): | |
265 | attr |= w32effects[effect] |
|
265 | attr |= w32effects[effect] | |
266 |
|
266 | |||
267 | # hack to ensure regexp finds data |
|
267 | # hack to ensure regexp finds data | |
268 | if not text.startswith('\033['): |
|
268 | if not text.startswith('\033['): | |
269 | text = '\033[m' + text |
|
269 | text = '\033[m' + text | |
270 |
|
270 | |||
271 | # Look for ANSI-like codes embedded in text |
|
271 | # Look for ANSI-like codes embedded in text | |
272 | m = re.match(ansire, text) |
|
272 | m = re.match(ansire, text) | |
273 | while m: |
|
273 | while m: | |
274 | for sattr in m.group(1).split(';'): |
|
274 | for sattr in m.group(1).split(';'): | |
275 | if sattr: |
|
275 | if sattr: | |
276 | val = int(sattr) |
|
276 | val = int(sattr) | |
277 | attr = val and attr|val or 0 |
|
277 | attr = val and attr|val or 0 | |
278 | stdout.SetConsoleTextAttribute(attr or origattr) |
|
278 | stdout.SetConsoleTextAttribute(attr or origattr) | |
279 | orig(m.group(2), **opts) |
|
279 | orig(m.group(2), **opts) | |
280 | m = re.match(ansire, m.group(3)) |
|
280 | m = re.match(ansire, m.group(3)) | |
281 |
|
281 | |||
282 | # Explicity reset original attributes |
|
282 | # Explicity reset original attributes | |
283 | stdout.SetConsoleTextAttribute(origattr) |
|
283 | stdout.SetConsoleTextAttribute(origattr) | |
284 |
|
284 | |||
285 | except ImportError: |
|
285 | except ImportError: | |
286 | w32effects = None |
|
286 | w32effects = None |
@@ -1,430 +1,430 b'' | |||||
1 | # convcmd - convert extension commands definition |
|
1 | # convcmd - convert extension commands definition | |
2 | # |
|
2 | # | |
3 | # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> |
|
3 | # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> | |
4 | # |
|
4 | # | |
5 | # This software may be used and distributed according to the terms of the |
|
5 | # This software may be used and distributed according to the terms of the | |
6 | # GNU General Public License version 2 or any later version. |
|
6 | # GNU General Public License version 2 or any later version. | |
7 |
|
7 | |||
8 | from common import NoRepo, MissingTool, SKIPREV, mapfile |
|
8 | from common import NoRepo, MissingTool, SKIPREV, mapfile | |
9 | from cvs import convert_cvs |
|
9 | from cvs import convert_cvs | |
10 | from darcs import darcs_source |
|
10 | from darcs import darcs_source | |
11 | from git import convert_git |
|
11 | from git import convert_git | |
12 | from hg import mercurial_source, mercurial_sink |
|
12 | from hg import mercurial_source, mercurial_sink | |
13 | from subversion import svn_source, svn_sink |
|
13 | from subversion import svn_source, svn_sink | |
14 | from monotone import monotone_source |
|
14 | from monotone import monotone_source | |
15 | from gnuarch import gnuarch_source |
|
15 | from gnuarch import gnuarch_source | |
16 | from bzr import bzr_source |
|
16 | from bzr import bzr_source | |
17 | from p4 import p4_source |
|
17 | from p4 import p4_source | |
18 | import filemap |
|
18 | import filemap | |
19 |
|
19 | |||
20 | import os, shutil |
|
20 | import os, shutil | |
21 | from mercurial import hg, util, encoding |
|
21 | from mercurial import hg, util, encoding | |
22 | from mercurial.i18n import _ |
|
22 | from mercurial.i18n import _ | |
23 |
|
23 | |||
24 | orig_encoding = 'ascii' |
|
24 | orig_encoding = 'ascii' | |
25 |
|
25 | |||
26 | def recode(s): |
|
26 | def recode(s): | |
27 | if isinstance(s, unicode): |
|
27 | if isinstance(s, unicode): | |
28 | return s.encode(orig_encoding, 'replace') |
|
28 | return s.encode(orig_encoding, 'replace') | |
29 | else: |
|
29 | else: | |
30 | return s.decode('utf-8').encode(orig_encoding, 'replace') |
|
30 | return s.decode('utf-8').encode(orig_encoding, 'replace') | |
31 |
|
31 | |||
32 | source_converters = [ |
|
32 | source_converters = [ | |
33 | ('cvs', convert_cvs, 'branchsort'), |
|
33 | ('cvs', convert_cvs, 'branchsort'), | |
34 | ('git', convert_git, 'branchsort'), |
|
34 | ('git', convert_git, 'branchsort'), | |
35 | ('svn', svn_source, 'branchsort'), |
|
35 | ('svn', svn_source, 'branchsort'), | |
36 | ('hg', mercurial_source, 'sourcesort'), |
|
36 | ('hg', mercurial_source, 'sourcesort'), | |
37 | ('darcs', darcs_source, 'branchsort'), |
|
37 | ('darcs', darcs_source, 'branchsort'), | |
38 | ('mtn', monotone_source, 'branchsort'), |
|
38 | ('mtn', monotone_source, 'branchsort'), | |
39 | ('gnuarch', gnuarch_source, 'branchsort'), |
|
39 | ('gnuarch', gnuarch_source, 'branchsort'), | |
40 | ('bzr', bzr_source, 'branchsort'), |
|
40 | ('bzr', bzr_source, 'branchsort'), | |
41 | ('p4', p4_source, 'branchsort'), |
|
41 | ('p4', p4_source, 'branchsort'), | |
42 | ] |
|
42 | ] | |
43 |
|
43 | |||
44 | sink_converters = [ |
|
44 | sink_converters = [ | |
45 | ('hg', mercurial_sink), |
|
45 | ('hg', mercurial_sink), | |
46 | ('svn', svn_sink), |
|
46 | ('svn', svn_sink), | |
47 | ] |
|
47 | ] | |
48 |
|
48 | |||
49 | def convertsource(ui, path, type, rev): |
|
49 | def convertsource(ui, path, type, rev): | |
50 | exceptions = [] |
|
50 | exceptions = [] | |
51 | if type and type not in [s[0] for s in source_converters]: |
|
51 | if type and type not in [s[0] for s in source_converters]: | |
52 | raise util.Abort(_('%s: invalid source repository type') % type) |
|
52 | raise util.Abort(_('%s: invalid source repository type') % type) | |
53 | for name, source, sortmode in source_converters: |
|
53 | for name, source, sortmode in source_converters: | |
54 | try: |
|
54 | try: | |
55 | if not type or name == type: |
|
55 | if not type or name == type: | |
56 | return source(ui, path, rev), sortmode |
|
56 | return source(ui, path, rev), sortmode | |
57 | except (NoRepo, MissingTool), inst: |
|
57 | except (NoRepo, MissingTool), inst: | |
58 | exceptions.append(inst) |
|
58 | exceptions.append(inst) | |
59 | if not ui.quiet: |
|
59 | if not ui.quiet: | |
60 | for inst in exceptions: |
|
60 | for inst in exceptions: | |
61 | ui.write("%s\n" % inst) |
|
61 | ui.write("%s\n" % inst) | |
62 | raise util.Abort(_('%s: missing or unsupported repository') % path) |
|
62 | raise util.Abort(_('%s: missing or unsupported repository') % path) | |
63 |
|
63 | |||
64 | def convertsink(ui, path, type): |
|
64 | def convertsink(ui, path, type): | |
65 | if type and type not in [s[0] for s in sink_converters]: |
|
65 | if type and type not in [s[0] for s in sink_converters]: | |
66 | raise util.Abort(_('%s: invalid destination repository type') % type) |
|
66 | raise util.Abort(_('%s: invalid destination repository type') % type) | |
67 | for name, sink in sink_converters: |
|
67 | for name, sink in sink_converters: | |
68 | try: |
|
68 | try: | |
69 | if not type or name == type: |
|
69 | if not type or name == type: | |
70 | return sink(ui, path) |
|
70 | return sink(ui, path) | |
71 | except NoRepo, inst: |
|
71 | except NoRepo, inst: | |
72 | ui.note(_("convert: %s\n") % inst) |
|
72 | ui.note(_("convert: %s\n") % inst) | |
73 | raise util.Abort(_('%s: unknown repository type') % path) |
|
73 | raise util.Abort(_('%s: unknown repository type') % path) | |
74 |
|
74 | |||
75 | class progresssource(object): |
|
75 | class progresssource(object): | |
76 | def __init__(self, ui, source, filecount): |
|
76 | def __init__(self, ui, source, filecount): | |
77 | self.ui = ui |
|
77 | self.ui = ui | |
78 | self.source = source |
|
78 | self.source = source | |
79 | self.filecount = filecount |
|
79 | self.filecount = filecount | |
80 | self.retrieved = 0 |
|
80 | self.retrieved = 0 | |
81 |
|
81 | |||
82 | def getfile(self, file, rev): |
|
82 | def getfile(self, file, rev): | |
83 | self.retrieved += 1 |
|
83 | self.retrieved += 1 | |
84 |
self.ui.progress(_(' |
|
84 | self.ui.progress(_('getting files'), self.retrieved, | |
85 | item=file, total=self.filecount) |
|
85 | item=file, total=self.filecount) | |
86 | return self.source.getfile(file, rev) |
|
86 | return self.source.getfile(file, rev) | |
87 |
|
87 | |||
88 | def lookuprev(self, rev): |
|
88 | def lookuprev(self, rev): | |
89 | return self.source.lookuprev(rev) |
|
89 | return self.source.lookuprev(rev) | |
90 |
|
90 | |||
91 | def close(self): |
|
91 | def close(self): | |
92 |
self.ui.progress(_(' |
|
92 | self.ui.progress(_('getting files'), None) | |
93 |
|
93 | |||
94 | class converter(object): |
|
94 | class converter(object): | |
95 | def __init__(self, ui, source, dest, revmapfile, opts): |
|
95 | def __init__(self, ui, source, dest, revmapfile, opts): | |
96 |
|
96 | |||
97 | self.source = source |
|
97 | self.source = source | |
98 | self.dest = dest |
|
98 | self.dest = dest | |
99 | self.ui = ui |
|
99 | self.ui = ui | |
100 | self.opts = opts |
|
100 | self.opts = opts | |
101 | self.commitcache = {} |
|
101 | self.commitcache = {} | |
102 | self.authors = {} |
|
102 | self.authors = {} | |
103 | self.authorfile = None |
|
103 | self.authorfile = None | |
104 |
|
104 | |||
105 | # Record converted revisions persistently: maps source revision |
|
105 | # Record converted revisions persistently: maps source revision | |
106 | # ID to target revision ID (both strings). (This is how |
|
106 | # ID to target revision ID (both strings). (This is how | |
107 | # incremental conversions work.) |
|
107 | # incremental conversions work.) | |
108 | self.map = mapfile(ui, revmapfile) |
|
108 | self.map = mapfile(ui, revmapfile) | |
109 |
|
109 | |||
110 | # Read first the dst author map if any |
|
110 | # Read first the dst author map if any | |
111 | authorfile = self.dest.authorfile() |
|
111 | authorfile = self.dest.authorfile() | |
112 | if authorfile and os.path.exists(authorfile): |
|
112 | if authorfile and os.path.exists(authorfile): | |
113 | self.readauthormap(authorfile) |
|
113 | self.readauthormap(authorfile) | |
114 | # Extend/Override with new author map if necessary |
|
114 | # Extend/Override with new author map if necessary | |
115 | if opts.get('authors'): |
|
115 | if opts.get('authors'): | |
116 | self.readauthormap(opts.get('authors')) |
|
116 | self.readauthormap(opts.get('authors')) | |
117 | self.authorfile = self.dest.authorfile() |
|
117 | self.authorfile = self.dest.authorfile() | |
118 |
|
118 | |||
119 | self.splicemap = mapfile(ui, opts.get('splicemap')) |
|
119 | self.splicemap = mapfile(ui, opts.get('splicemap')) | |
120 | self.branchmap = mapfile(ui, opts.get('branchmap')) |
|
120 | self.branchmap = mapfile(ui, opts.get('branchmap')) | |
121 |
|
121 | |||
122 | def walktree(self, heads): |
|
122 | def walktree(self, heads): | |
123 | '''Return a mapping that identifies the uncommitted parents of every |
|
123 | '''Return a mapping that identifies the uncommitted parents of every | |
124 | uncommitted changeset.''' |
|
124 | uncommitted changeset.''' | |
125 | visit = heads |
|
125 | visit = heads | |
126 | known = set() |
|
126 | known = set() | |
127 | parents = {} |
|
127 | parents = {} | |
128 | while visit: |
|
128 | while visit: | |
129 | n = visit.pop(0) |
|
129 | n = visit.pop(0) | |
130 | if n in known or n in self.map: |
|
130 | if n in known or n in self.map: | |
131 | continue |
|
131 | continue | |
132 | known.add(n) |
|
132 | known.add(n) | |
133 | self.ui.progress(_('scanning'), len(known), unit=_('revisions')) |
|
133 | self.ui.progress(_('scanning'), len(known), unit=_('revisions')) | |
134 | commit = self.cachecommit(n) |
|
134 | commit = self.cachecommit(n) | |
135 | parents[n] = [] |
|
135 | parents[n] = [] | |
136 | for p in commit.parents: |
|
136 | for p in commit.parents: | |
137 | parents[n].append(p) |
|
137 | parents[n].append(p) | |
138 | visit.append(p) |
|
138 | visit.append(p) | |
139 | self.ui.progress(_('scanning'), None) |
|
139 | self.ui.progress(_('scanning'), None) | |
140 |
|
140 | |||
141 | return parents |
|
141 | return parents | |
142 |
|
142 | |||
143 | def toposort(self, parents, sortmode): |
|
143 | def toposort(self, parents, sortmode): | |
144 | '''Return an ordering such that every uncommitted changeset is |
|
144 | '''Return an ordering such that every uncommitted changeset is | |
145 | preceeded by all its uncommitted ancestors.''' |
|
145 | preceeded by all its uncommitted ancestors.''' | |
146 |
|
146 | |||
147 | def mapchildren(parents): |
|
147 | def mapchildren(parents): | |
148 | """Return a (children, roots) tuple where 'children' maps parent |
|
148 | """Return a (children, roots) tuple where 'children' maps parent | |
149 | revision identifiers to children ones, and 'roots' is the list of |
|
149 | revision identifiers to children ones, and 'roots' is the list of | |
150 | revisions without parents. 'parents' must be a mapping of revision |
|
150 | revisions without parents. 'parents' must be a mapping of revision | |
151 | identifier to its parents ones. |
|
151 | identifier to its parents ones. | |
152 | """ |
|
152 | """ | |
153 | visit = parents.keys() |
|
153 | visit = parents.keys() | |
154 | seen = set() |
|
154 | seen = set() | |
155 | children = {} |
|
155 | children = {} | |
156 | roots = [] |
|
156 | roots = [] | |
157 |
|
157 | |||
158 | while visit: |
|
158 | while visit: | |
159 | n = visit.pop(0) |
|
159 | n = visit.pop(0) | |
160 | if n in seen: |
|
160 | if n in seen: | |
161 | continue |
|
161 | continue | |
162 | seen.add(n) |
|
162 | seen.add(n) | |
163 | # Ensure that nodes without parents are present in the |
|
163 | # Ensure that nodes without parents are present in the | |
164 | # 'children' mapping. |
|
164 | # 'children' mapping. | |
165 | children.setdefault(n, []) |
|
165 | children.setdefault(n, []) | |
166 | hasparent = False |
|
166 | hasparent = False | |
167 | for p in parents[n]: |
|
167 | for p in parents[n]: | |
168 | if not p in self.map: |
|
168 | if not p in self.map: | |
169 | visit.append(p) |
|
169 | visit.append(p) | |
170 | hasparent = True |
|
170 | hasparent = True | |
171 | children.setdefault(p, []).append(n) |
|
171 | children.setdefault(p, []).append(n) | |
172 | if not hasparent: |
|
172 | if not hasparent: | |
173 | roots.append(n) |
|
173 | roots.append(n) | |
174 |
|
174 | |||
175 | return children, roots |
|
175 | return children, roots | |
176 |
|
176 | |||
177 | # Sort functions are supposed to take a list of revisions which |
|
177 | # Sort functions are supposed to take a list of revisions which | |
178 | # can be converted immediately and pick one |
|
178 | # can be converted immediately and pick one | |
179 |
|
179 | |||
180 | def makebranchsorter(): |
|
180 | def makebranchsorter(): | |
181 | """If the previously converted revision has a child in the |
|
181 | """If the previously converted revision has a child in the | |
182 | eligible revisions list, pick it. Return the list head |
|
182 | eligible revisions list, pick it. Return the list head | |
183 | otherwise. Branch sort attempts to minimize branch |
|
183 | otherwise. Branch sort attempts to minimize branch | |
184 | switching, which is harmful for Mercurial backend |
|
184 | switching, which is harmful for Mercurial backend | |
185 | compression. |
|
185 | compression. | |
186 | """ |
|
186 | """ | |
187 | prev = [None] |
|
187 | prev = [None] | |
188 | def picknext(nodes): |
|
188 | def picknext(nodes): | |
189 | next = nodes[0] |
|
189 | next = nodes[0] | |
190 | for n in nodes: |
|
190 | for n in nodes: | |
191 | if prev[0] in parents[n]: |
|
191 | if prev[0] in parents[n]: | |
192 | next = n |
|
192 | next = n | |
193 | break |
|
193 | break | |
194 | prev[0] = next |
|
194 | prev[0] = next | |
195 | return next |
|
195 | return next | |
196 | return picknext |
|
196 | return picknext | |
197 |
|
197 | |||
198 | def makesourcesorter(): |
|
198 | def makesourcesorter(): | |
199 | """Source specific sort.""" |
|
199 | """Source specific sort.""" | |
200 | keyfn = lambda n: self.commitcache[n].sortkey |
|
200 | keyfn = lambda n: self.commitcache[n].sortkey | |
201 | def picknext(nodes): |
|
201 | def picknext(nodes): | |
202 | return sorted(nodes, key=keyfn)[0] |
|
202 | return sorted(nodes, key=keyfn)[0] | |
203 | return picknext |
|
203 | return picknext | |
204 |
|
204 | |||
205 | def makedatesorter(): |
|
205 | def makedatesorter(): | |
206 | """Sort revisions by date.""" |
|
206 | """Sort revisions by date.""" | |
207 | dates = {} |
|
207 | dates = {} | |
208 | def getdate(n): |
|
208 | def getdate(n): | |
209 | if n not in dates: |
|
209 | if n not in dates: | |
210 | dates[n] = util.parsedate(self.commitcache[n].date) |
|
210 | dates[n] = util.parsedate(self.commitcache[n].date) | |
211 | return dates[n] |
|
211 | return dates[n] | |
212 |
|
212 | |||
213 | def picknext(nodes): |
|
213 | def picknext(nodes): | |
214 | return min([(getdate(n), n) for n in nodes])[1] |
|
214 | return min([(getdate(n), n) for n in nodes])[1] | |
215 |
|
215 | |||
216 | return picknext |
|
216 | return picknext | |
217 |
|
217 | |||
218 | if sortmode == 'branchsort': |
|
218 | if sortmode == 'branchsort': | |
219 | picknext = makebranchsorter() |
|
219 | picknext = makebranchsorter() | |
220 | elif sortmode == 'datesort': |
|
220 | elif sortmode == 'datesort': | |
221 | picknext = makedatesorter() |
|
221 | picknext = makedatesorter() | |
222 | elif sortmode == 'sourcesort': |
|
222 | elif sortmode == 'sourcesort': | |
223 | picknext = makesourcesorter() |
|
223 | picknext = makesourcesorter() | |
224 | else: |
|
224 | else: | |
225 | raise util.Abort(_('unknown sort mode: %s') % sortmode) |
|
225 | raise util.Abort(_('unknown sort mode: %s') % sortmode) | |
226 |
|
226 | |||
227 | children, actives = mapchildren(parents) |
|
227 | children, actives = mapchildren(parents) | |
228 |
|
228 | |||
229 | s = [] |
|
229 | s = [] | |
230 | pendings = {} |
|
230 | pendings = {} | |
231 | while actives: |
|
231 | while actives: | |
232 | n = picknext(actives) |
|
232 | n = picknext(actives) | |
233 | actives.remove(n) |
|
233 | actives.remove(n) | |
234 | s.append(n) |
|
234 | s.append(n) | |
235 |
|
235 | |||
236 | # Update dependents list |
|
236 | # Update dependents list | |
237 | for c in children.get(n, []): |
|
237 | for c in children.get(n, []): | |
238 | if c not in pendings: |
|
238 | if c not in pendings: | |
239 | pendings[c] = [p for p in parents[c] if p not in self.map] |
|
239 | pendings[c] = [p for p in parents[c] if p not in self.map] | |
240 | try: |
|
240 | try: | |
241 | pendings[c].remove(n) |
|
241 | pendings[c].remove(n) | |
242 | except ValueError: |
|
242 | except ValueError: | |
243 | raise util.Abort(_('cycle detected between %s and %s') |
|
243 | raise util.Abort(_('cycle detected between %s and %s') | |
244 | % (recode(c), recode(n))) |
|
244 | % (recode(c), recode(n))) | |
245 | if not pendings[c]: |
|
245 | if not pendings[c]: | |
246 | # Parents are converted, node is eligible |
|
246 | # Parents are converted, node is eligible | |
247 | actives.insert(0, c) |
|
247 | actives.insert(0, c) | |
248 | pendings[c] = None |
|
248 | pendings[c] = None | |
249 |
|
249 | |||
250 | if len(s) != len(parents): |
|
250 | if len(s) != len(parents): | |
251 | raise util.Abort(_("not all revisions were sorted")) |
|
251 | raise util.Abort(_("not all revisions were sorted")) | |
252 |
|
252 | |||
253 | return s |
|
253 | return s | |
254 |
|
254 | |||
255 | def writeauthormap(self): |
|
255 | def writeauthormap(self): | |
256 | authorfile = self.authorfile |
|
256 | authorfile = self.authorfile | |
257 | if authorfile: |
|
257 | if authorfile: | |
258 | self.ui.status(_('Writing author map file %s\n') % authorfile) |
|
258 | self.ui.status(_('Writing author map file %s\n') % authorfile) | |
259 | ofile = open(authorfile, 'w+') |
|
259 | ofile = open(authorfile, 'w+') | |
260 | for author in self.authors: |
|
260 | for author in self.authors: | |
261 | ofile.write("%s=%s\n" % (author, self.authors[author])) |
|
261 | ofile.write("%s=%s\n" % (author, self.authors[author])) | |
262 | ofile.close() |
|
262 | ofile.close() | |
263 |
|
263 | |||
264 | def readauthormap(self, authorfile): |
|
264 | def readauthormap(self, authorfile): | |
265 | afile = open(authorfile, 'r') |
|
265 | afile = open(authorfile, 'r') | |
266 | for line in afile: |
|
266 | for line in afile: | |
267 |
|
267 | |||
268 | line = line.strip() |
|
268 | line = line.strip() | |
269 | if not line or line.startswith('#'): |
|
269 | if not line or line.startswith('#'): | |
270 | continue |
|
270 | continue | |
271 |
|
271 | |||
272 | try: |
|
272 | try: | |
273 | srcauthor, dstauthor = line.split('=', 1) |
|
273 | srcauthor, dstauthor = line.split('=', 1) | |
274 | except ValueError: |
|
274 | except ValueError: | |
275 | msg = _('Ignoring bad line in author map file %s: %s\n') |
|
275 | msg = _('Ignoring bad line in author map file %s: %s\n') | |
276 | self.ui.warn(msg % (authorfile, line.rstrip())) |
|
276 | self.ui.warn(msg % (authorfile, line.rstrip())) | |
277 | continue |
|
277 | continue | |
278 |
|
278 | |||
279 | srcauthor = srcauthor.strip() |
|
279 | srcauthor = srcauthor.strip() | |
280 | dstauthor = dstauthor.strip() |
|
280 | dstauthor = dstauthor.strip() | |
281 | if self.authors.get(srcauthor) in (None, dstauthor): |
|
281 | if self.authors.get(srcauthor) in (None, dstauthor): | |
282 | msg = _('mapping author %s to %s\n') |
|
282 | msg = _('mapping author %s to %s\n') | |
283 | self.ui.debug(msg % (srcauthor, dstauthor)) |
|
283 | self.ui.debug(msg % (srcauthor, dstauthor)) | |
284 | self.authors[srcauthor] = dstauthor |
|
284 | self.authors[srcauthor] = dstauthor | |
285 | continue |
|
285 | continue | |
286 |
|
286 | |||
287 | m = _('overriding mapping for author %s, was %s, will be %s\n') |
|
287 | m = _('overriding mapping for author %s, was %s, will be %s\n') | |
288 | self.ui.status(m % (srcauthor, self.authors[srcauthor], dstauthor)) |
|
288 | self.ui.status(m % (srcauthor, self.authors[srcauthor], dstauthor)) | |
289 |
|
289 | |||
290 | afile.close() |
|
290 | afile.close() | |
291 |
|
291 | |||
292 | def cachecommit(self, rev): |
|
292 | def cachecommit(self, rev): | |
293 | commit = self.source.getcommit(rev) |
|
293 | commit = self.source.getcommit(rev) | |
294 | commit.author = self.authors.get(commit.author, commit.author) |
|
294 | commit.author = self.authors.get(commit.author, commit.author) | |
295 | commit.branch = self.branchmap.get(commit.branch, commit.branch) |
|
295 | commit.branch = self.branchmap.get(commit.branch, commit.branch) | |
296 | self.commitcache[rev] = commit |
|
296 | self.commitcache[rev] = commit | |
297 | return commit |
|
297 | return commit | |
298 |
|
298 | |||
299 | def copy(self, rev): |
|
299 | def copy(self, rev): | |
300 | commit = self.commitcache[rev] |
|
300 | commit = self.commitcache[rev] | |
301 |
|
301 | |||
302 | changes = self.source.getchanges(rev) |
|
302 | changes = self.source.getchanges(rev) | |
303 | if isinstance(changes, basestring): |
|
303 | if isinstance(changes, basestring): | |
304 | if changes == SKIPREV: |
|
304 | if changes == SKIPREV: | |
305 | dest = SKIPREV |
|
305 | dest = SKIPREV | |
306 | else: |
|
306 | else: | |
307 | dest = self.map[changes] |
|
307 | dest = self.map[changes] | |
308 | self.map[rev] = dest |
|
308 | self.map[rev] = dest | |
309 | return |
|
309 | return | |
310 | files, copies = changes |
|
310 | files, copies = changes | |
311 | pbranches = [] |
|
311 | pbranches = [] | |
312 | if commit.parents: |
|
312 | if commit.parents: | |
313 | for prev in commit.parents: |
|
313 | for prev in commit.parents: | |
314 | if prev not in self.commitcache: |
|
314 | if prev not in self.commitcache: | |
315 | self.cachecommit(prev) |
|
315 | self.cachecommit(prev) | |
316 | pbranches.append((self.map[prev], |
|
316 | pbranches.append((self.map[prev], | |
317 | self.commitcache[prev].branch)) |
|
317 | self.commitcache[prev].branch)) | |
318 | self.dest.setbranch(commit.branch, pbranches) |
|
318 | self.dest.setbranch(commit.branch, pbranches) | |
319 | try: |
|
319 | try: | |
320 | parents = self.splicemap[rev].replace(',', ' ').split() |
|
320 | parents = self.splicemap[rev].replace(',', ' ').split() | |
321 | self.ui.status(_('spliced in %s as parents of %s\n') % |
|
321 | self.ui.status(_('spliced in %s as parents of %s\n') % | |
322 | (parents, rev)) |
|
322 | (parents, rev)) | |
323 | parents = [self.map.get(p, p) for p in parents] |
|
323 | parents = [self.map.get(p, p) for p in parents] | |
324 | except KeyError: |
|
324 | except KeyError: | |
325 | parents = [b[0] for b in pbranches] |
|
325 | parents = [b[0] for b in pbranches] | |
326 | source = progresssource(self.ui, self.source, len(files)) |
|
326 | source = progresssource(self.ui, self.source, len(files)) | |
327 | newnode = self.dest.putcommit(files, copies, parents, commit, |
|
327 | newnode = self.dest.putcommit(files, copies, parents, commit, | |
328 | source, self.map) |
|
328 | source, self.map) | |
329 | source.close() |
|
329 | source.close() | |
330 | self.source.converted(rev, newnode) |
|
330 | self.source.converted(rev, newnode) | |
331 | self.map[rev] = newnode |
|
331 | self.map[rev] = newnode | |
332 |
|
332 | |||
333 | def convert(self, sortmode): |
|
333 | def convert(self, sortmode): | |
334 | try: |
|
334 | try: | |
335 | self.source.before() |
|
335 | self.source.before() | |
336 | self.dest.before() |
|
336 | self.dest.before() | |
337 | self.source.setrevmap(self.map) |
|
337 | self.source.setrevmap(self.map) | |
338 | self.ui.status(_("scanning source...\n")) |
|
338 | self.ui.status(_("scanning source...\n")) | |
339 | heads = self.source.getheads() |
|
339 | heads = self.source.getheads() | |
340 | parents = self.walktree(heads) |
|
340 | parents = self.walktree(heads) | |
341 | self.ui.status(_("sorting...\n")) |
|
341 | self.ui.status(_("sorting...\n")) | |
342 | t = self.toposort(parents, sortmode) |
|
342 | t = self.toposort(parents, sortmode) | |
343 | num = len(t) |
|
343 | num = len(t) | |
344 | c = None |
|
344 | c = None | |
345 |
|
345 | |||
346 | self.ui.status(_("converting...\n")) |
|
346 | self.ui.status(_("converting...\n")) | |
347 | for i, c in enumerate(t): |
|
347 | for i, c in enumerate(t): | |
348 | num -= 1 |
|
348 | num -= 1 | |
349 | desc = self.commitcache[c].desc |
|
349 | desc = self.commitcache[c].desc | |
350 | if "\n" in desc: |
|
350 | if "\n" in desc: | |
351 | desc = desc.splitlines()[0] |
|
351 | desc = desc.splitlines()[0] | |
352 | # convert log message to local encoding without using |
|
352 | # convert log message to local encoding without using | |
353 | # tolocal() because encoding.encoding conver() use it as |
|
353 | # tolocal() because encoding.encoding conver() use it as | |
354 | # 'utf-8' |
|
354 | # 'utf-8' | |
355 | self.ui.status("%d %s\n" % (num, recode(desc))) |
|
355 | self.ui.status("%d %s\n" % (num, recode(desc))) | |
356 | self.ui.note(_("source: %s\n") % recode(c)) |
|
356 | self.ui.note(_("source: %s\n") % recode(c)) | |
357 | self.ui.progress(_('converting'), i, unit=_('revisions'), |
|
357 | self.ui.progress(_('converting'), i, unit=_('revisions'), | |
358 | total=len(t)) |
|
358 | total=len(t)) | |
359 | self.copy(c) |
|
359 | self.copy(c) | |
360 | self.ui.progress(_('converting'), None) |
|
360 | self.ui.progress(_('converting'), None) | |
361 |
|
361 | |||
362 | tags = self.source.gettags() |
|
362 | tags = self.source.gettags() | |
363 | ctags = {} |
|
363 | ctags = {} | |
364 | for k in tags: |
|
364 | for k in tags: | |
365 | v = tags[k] |
|
365 | v = tags[k] | |
366 | if self.map.get(v, SKIPREV) != SKIPREV: |
|
366 | if self.map.get(v, SKIPREV) != SKIPREV: | |
367 | ctags[k] = self.map[v] |
|
367 | ctags[k] = self.map[v] | |
368 |
|
368 | |||
369 | if c and ctags: |
|
369 | if c and ctags: | |
370 | nrev, tagsparent = self.dest.puttags(ctags) |
|
370 | nrev, tagsparent = self.dest.puttags(ctags) | |
371 | if nrev and tagsparent: |
|
371 | if nrev and tagsparent: | |
372 | # write another hash correspondence to override the previous |
|
372 | # write another hash correspondence to override the previous | |
373 | # one so we don't end up with extra tag heads |
|
373 | # one so we don't end up with extra tag heads | |
374 | tagsparents = [e for e in self.map.iteritems() |
|
374 | tagsparents = [e for e in self.map.iteritems() | |
375 | if e[1] == tagsparent] |
|
375 | if e[1] == tagsparent] | |
376 | if tagsparents: |
|
376 | if tagsparents: | |
377 | self.map[tagsparents[0][0]] = nrev |
|
377 | self.map[tagsparents[0][0]] = nrev | |
378 |
|
378 | |||
379 | self.writeauthormap() |
|
379 | self.writeauthormap() | |
380 | finally: |
|
380 | finally: | |
381 | self.cleanup() |
|
381 | self.cleanup() | |
382 |
|
382 | |||
383 | def cleanup(self): |
|
383 | def cleanup(self): | |
384 | try: |
|
384 | try: | |
385 | self.dest.after() |
|
385 | self.dest.after() | |
386 | finally: |
|
386 | finally: | |
387 | self.source.after() |
|
387 | self.source.after() | |
388 | self.map.close() |
|
388 | self.map.close() | |
389 |
|
389 | |||
390 | def convert(ui, src, dest=None, revmapfile=None, **opts): |
|
390 | def convert(ui, src, dest=None, revmapfile=None, **opts): | |
391 | global orig_encoding |
|
391 | global orig_encoding | |
392 | orig_encoding = encoding.encoding |
|
392 | orig_encoding = encoding.encoding | |
393 | encoding.encoding = 'UTF-8' |
|
393 | encoding.encoding = 'UTF-8' | |
394 |
|
394 | |||
395 | if not dest: |
|
395 | if not dest: | |
396 | dest = hg.defaultdest(src) + "-hg" |
|
396 | dest = hg.defaultdest(src) + "-hg" | |
397 | ui.status(_("assuming destination %s\n") % dest) |
|
397 | ui.status(_("assuming destination %s\n") % dest) | |
398 |
|
398 | |||
399 | destc = convertsink(ui, dest, opts.get('dest_type')) |
|
399 | destc = convertsink(ui, dest, opts.get('dest_type')) | |
400 |
|
400 | |||
401 | try: |
|
401 | try: | |
402 | srcc, defaultsort = convertsource(ui, src, opts.get('source_type'), |
|
402 | srcc, defaultsort = convertsource(ui, src, opts.get('source_type'), | |
403 | opts.get('rev')) |
|
403 | opts.get('rev')) | |
404 | except Exception: |
|
404 | except Exception: | |
405 | for path in destc.created: |
|
405 | for path in destc.created: | |
406 | shutil.rmtree(path, True) |
|
406 | shutil.rmtree(path, True) | |
407 | raise |
|
407 | raise | |
408 |
|
408 | |||
409 | sortmodes = ('branchsort', 'datesort', 'sourcesort') |
|
409 | sortmodes = ('branchsort', 'datesort', 'sourcesort') | |
410 | sortmode = [m for m in sortmodes if opts.get(m)] |
|
410 | sortmode = [m for m in sortmodes if opts.get(m)] | |
411 | if len(sortmode) > 1: |
|
411 | if len(sortmode) > 1: | |
412 | raise util.Abort(_('more than one sort mode specified')) |
|
412 | raise util.Abort(_('more than one sort mode specified')) | |
413 | sortmode = sortmode and sortmode[0] or defaultsort |
|
413 | sortmode = sortmode and sortmode[0] or defaultsort | |
414 | if sortmode == 'sourcesort' and not srcc.hasnativeorder(): |
|
414 | if sortmode == 'sourcesort' and not srcc.hasnativeorder(): | |
415 | raise util.Abort(_('--sourcesort is not supported by this data source')) |
|
415 | raise util.Abort(_('--sourcesort is not supported by this data source')) | |
416 |
|
416 | |||
417 | fmap = opts.get('filemap') |
|
417 | fmap = opts.get('filemap') | |
418 | if fmap: |
|
418 | if fmap: | |
419 | srcc = filemap.filemap_source(ui, srcc, fmap) |
|
419 | srcc = filemap.filemap_source(ui, srcc, fmap) | |
420 | destc.setfilemapmode(True) |
|
420 | destc.setfilemapmode(True) | |
421 |
|
421 | |||
422 | if not revmapfile: |
|
422 | if not revmapfile: | |
423 | try: |
|
423 | try: | |
424 | revmapfile = destc.revmapfile() |
|
424 | revmapfile = destc.revmapfile() | |
425 | except: |
|
425 | except: | |
426 | revmapfile = os.path.join(destc, "map") |
|
426 | revmapfile = os.path.join(destc, "map") | |
427 |
|
427 | |||
428 | c = converter(ui, srcc, destc, revmapfile, opts) |
|
428 | c = converter(ui, srcc, destc, revmapfile, opts) | |
429 | c.convert(sortmode) |
|
429 | c.convert(sortmode) | |
430 |
|
430 |
@@ -1,123 +1,123 b'' | |||||
1 | % convert trunk and branches |
|
1 | % convert trunk and branches | |
2 | initializing destination A-hg repository |
|
2 | initializing destination A-hg repository | |
3 | scanning source... |
|
3 | scanning source... | |
4 | sorting... |
|
4 | sorting... | |
5 | converting... |
|
5 | converting... | |
6 | 13 createtrunk |
|
6 | 13 createtrunk | |
7 | 12 moved1 |
|
7 | 12 moved1 | |
8 | 11 moved1 |
|
8 | 11 moved1 | |
9 | 10 moved2 |
|
9 | 10 moved2 | |
10 | 9 changeb and rm d2 |
|
10 | 9 changeb and rm d2 | |
11 | 8 changeb and rm d2 |
|
11 | 8 changeb and rm d2 | |
12 | 7 moved1again |
|
12 | 7 moved1again | |
13 | 6 moved1again |
|
13 | 6 moved1again | |
14 | 5 copyfilefrompast |
|
14 | 5 copyfilefrompast | |
15 | 4 copydirfrompast |
|
15 | 4 copydirfrompast | |
16 | 3 add d3 |
|
16 | 3 add d3 | |
17 | 2 copy dir and remove subdir |
|
17 | 2 copy dir and remove subdir | |
18 | 1 add d4old |
|
18 | 1 add d4old | |
19 | 0 rename d4old into d4new |
|
19 | 0 rename d4old into d4new | |
20 | o 13 rename d4old into d4new files: d4new/g d4old/g |
|
20 | o 13 rename d4old into d4new files: d4new/g d4old/g | |
21 | | |
|
21 | | | |
22 | o 12 add d4old files: d4old/g |
|
22 | o 12 add d4old files: d4old/g | |
23 | | |
|
23 | | | |
24 | o 11 copy dir and remove subdir files: d3/d31/e d4/d31/e d4/f |
|
24 | o 11 copy dir and remove subdir files: d3/d31/e d4/d31/e d4/f | |
25 | | |
|
25 | | | |
26 | o 10 add d3 files: d3/d31/e d3/f |
|
26 | o 10 add d3 files: d3/d31/e d3/f | |
27 | | |
|
27 | | | |
28 | o 9 copydirfrompast files: d2/d |
|
28 | o 9 copydirfrompast files: d2/d | |
29 | | |
|
29 | | | |
30 | o 8 copyfilefrompast files: d |
|
30 | o 8 copyfilefrompast files: d | |
31 | | |
|
31 | | | |
32 | o 7 moved1again files: d1/b d1/c |
|
32 | o 7 moved1again files: d1/b d1/c | |
33 | | |
|
33 | | | |
34 | | o 6 moved1again files: |
|
34 | | o 6 moved1again files: | |
35 | | | |
|
35 | | | | |
36 | o | 5 changeb and rm d2 files: d1/b d2/d |
|
36 | o | 5 changeb and rm d2 files: d1/b d2/d | |
37 | | | |
|
37 | | | | |
38 | | o 4 changeb and rm d2 files: b |
|
38 | | o 4 changeb and rm d2 files: b | |
39 | | | |
|
39 | | | | |
40 | o | 3 moved2 files: d2/d |
|
40 | o | 3 moved2 files: d2/d | |
41 | | | |
|
41 | | | | |
42 | o | 2 moved1 files: d1/b d1/c |
|
42 | o | 2 moved1 files: d1/b d1/c | |
43 | | | |
|
43 | | | | |
44 | | o 1 moved1 files: b c |
|
44 | | o 1 moved1 files: b c | |
45 | | |
|
45 | | | |
46 | o 0 createtrunk files: |
|
46 | o 0 createtrunk files: | |
47 |
|
47 | |||
48 | % check move copy records |
|
48 | % check move copy records | |
49 | A d4new/g |
|
49 | A d4new/g | |
50 | d4old/g |
|
50 | d4old/g | |
51 | R d4old/g |
|
51 | R d4old/g | |
52 | % check branches |
|
52 | % check branches | |
53 | default 13: |
|
53 | default 13: | |
54 | d1 6: |
|
54 | d1 6: | |
55 | % convert files being replaced by directories |
|
55 | % convert files being replaced by directories | |
56 | initializing destination hg-repo repository |
|
56 | initializing destination hg-repo repository | |
57 | scanning source... |
|
57 | scanning source... | |
58 | sorting... |
|
58 | sorting... | |
59 | converting... |
|
59 | converting... | |
60 | 3 initial |
|
60 | 3 initial | |
61 | 2 clobber symlink |
|
61 | 2 clobber symlink | |
62 | 1 clobber1 |
|
62 | 1 clobber1 | |
63 | 0 clobber2 |
|
63 | 0 clobber2 | |
64 | % manifest before |
|
64 | % manifest before | |
65 | 644 a |
|
65 | 644 a | |
66 | 644 d/b |
|
66 | 644 d/b | |
67 | 644 @ dlink |
|
67 | 644 @ dlink | |
68 | 644 @ dlink2 |
|
68 | 644 @ dlink2 | |
69 | 644 dlink3 |
|
69 | 644 dlink3 | |
70 | % manifest after clobber1 |
|
70 | % manifest after clobber1 | |
71 | 644 a/b |
|
71 | 644 a/b | |
72 | 644 d/b |
|
72 | 644 d/b | |
73 | 644 dlink/b |
|
73 | 644 dlink/b | |
74 | 644 @ dlink2 |
|
74 | 644 @ dlink2 | |
75 | 644 dlink3 |
|
75 | 644 dlink3 | |
76 | % manifest after clobber2 |
|
76 | % manifest after clobber2 | |
77 | 644 a/b |
|
77 | 644 a/b | |
78 | 644 d/b |
|
78 | 644 d/b | |
79 | 644 dlink/b |
|
79 | 644 dlink/b | |
80 | 644 @ dlink2 |
|
80 | 644 @ dlink2 | |
81 | 644 @ dlink3 |
|
81 | 644 @ dlink3 | |
82 | % try updating |
|
82 | % try updating | |
83 | % test convert progress bar |
|
83 | % test convert progress bar | |
84 |
|
84 | |||
85 | scanning [ <=> ] 1 |
|
85 | scanning [ <=> ] 1 | |
86 | scanning [ <=> ] 2 |
|
86 | scanning [ <=> ] 2 | |
87 | scanning [ <=> ] 3 |
|
87 | scanning [ <=> ] 3 | |
88 | scanning [ <=> ] 4 |
|
88 | scanning [ <=> ] 4 | |
89 |
|
89 | |||
90 | converting [ ] 0/4 |
|
90 | converting [ ] 0/4 | |
91 |
|
|
91 | getting files [==========> ] 1/5 | |
92 |
|
|
92 | getting files [======================> ] 2/5 | |
93 |
|
|
93 | getting files [==================================> ] 3/5 | |
94 |
|
|
94 | getting files [==============================================> ] 4/5 | |
95 |
|
|
95 | getting files [==========================================================>] 5/5 | |
96 |
|
96 | |||
97 | converting [==============> ] 1/4 |
|
97 | converting [==============> ] 1/4 | |
98 | scanning paths [ ] 0/1 |
|
98 | scanning paths [ ] 0/1 | |
99 |
|
99 | |||
100 |
|
|
100 | getting files [==========================================================>] 1/1 | |
101 |
|
101 | |||
102 | converting [==============================> ] 2/4 |
|
102 | converting [==============================> ] 2/4 | |
103 | scanning paths [ ] 0/2 |
|
103 | scanning paths [ ] 0/2 | |
104 | scanning paths [============================> ] 1/2 |
|
104 | scanning paths [============================> ] 1/2 | |
105 |
|
105 | |||
106 |
|
|
106 | getting files [=============> ] 1/4 | |
107 |
|
|
107 | getting files [============================> ] 2/4 | |
108 |
|
|
108 | getting files [===========================================> ] 3/4 | |
109 |
|
|
109 | getting files [==========================================================>] 4/4 | |
110 |
|
110 | |||
111 | converting [=============================================> ] 3/4 |
|
111 | converting [=============================================> ] 3/4 | |
112 | scanning paths [ ] 0/1 |
|
112 | scanning paths [ ] 0/1 | |
113 |
|
113 | |||
114 |
|
|
114 | getting files [==========================================================>] 1/1 | |
115 |
|
115 | |||
116 | initializing destination hg-progress repository |
|
116 | initializing destination hg-progress repository | |
117 | scanning source... |
|
117 | scanning source... | |
118 | sorting... |
|
118 | sorting... | |
119 | converting... |
|
119 | converting... | |
120 | 3 initial |
|
120 | 3 initial | |
121 | 2 clobber symlink |
|
121 | 2 clobber symlink | |
122 | 1 clobber1 |
|
122 | 1 clobber1 | |
123 | 0 clobber2 |
|
123 | 0 clobber2 |
General Comments 0
You need to be logged in to leave comments.
Login now