Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 # This file is part of Adblock Plus <https://adblockplus.org/>, | 1 # This file is part of Adblock Plus <https://adblockplus.org/>, |
2 # Copyright (C) 2006-2016 Eyeo GmbH | 2 # Copyright (C) 2006-2016 Eyeo GmbH |
3 # | 3 # |
4 # Adblock Plus is free software: you can redistribute it and/or modify | 4 # Adblock Plus is free software: you can redistribute it and/or modify |
5 # it under the terms of the GNU General Public License version 3 as | 5 # it under the terms of the GNU General Public License version 3 as |
6 # published by the Free Software Foundation. | 6 # published by the Free Software Foundation. |
7 # | 7 # |
8 # Adblock Plus is distributed in the hope that it will be useful, | 8 # Adblock Plus is distributed in the hope that it will be useful, |
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 # GNU General Public License for more details. | 11 # GNU General Public License for more details. |
12 # | 12 # |
13 # You should have received a copy of the GNU General Public License | 13 # You should have received a copy of the GNU General Public License |
14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
15 | 15 |
16 import sys | 16 import sys |
17 | 17 |
18 # States of the conversion state machine. | |
19 NORMAL, INDEX, METAINFO = range(3) | |
20 # Double line that SVN uses to separate file name from patch content. | |
21 SVN_SEPARATOR = '=' * 67 | |
22 # Template for the line from Git diff which is replaced during SVN | |
23 # conversion. | |
24 GIT_PART_HEAD = 'diff --git a/{} b/{}\n' | |
25 | |
18 | 26 |
19 def rietveld_to_git(lines): | 27 def rietveld_to_git(lines): |
20 """Convert patch from Rietveld format to Git format. | 28 """Convert patch from Rietveld format to Git format. |
21 | 29 |
22 Rietveld format looks similar to SVN patch format but it can also | 30 Rietveld format looks similar to SVN patch format but it can also |
23 contain Git extensions if it was produced by `upload.py` run from | 31 contain Git extensions if it was produced by `upload.py` run from |
24 a project managed by Git or Mercurial. The output format is the | 32 a project managed by Git or Mercurial. The output format is the |
25 original Git patch as produced by `git diff` or `hg diff --git`. | 33 original Git patch as produced by `git diff` or `hg diff --git`. |
26 It can be applied by `hg import` or `git apply`. | 34 It can be applied by `hg import` or `git apply`. |
27 | 35 |
28 Arguments: | 36 Arguments: |
29 lines -- lines of the patch. | 37 lines -- lines of the patch. |
30 Returns: | 38 Returns: |
31 Lines of the converted patch. | 39 Lines of the converted patch. |
32 """ | 40 """ |
33 # States of the conversion state machine. | |
34 NORMAL, INDEX, METAINFO = range(3) | |
35 # Double line that SVN uses to separate file name from patch content. | |
36 SVN_SEPARATOR = '=' * 67 | |
37 # Template for the line from Git diff which is replaced during SVN | |
38 # conversion. | |
39 GIT_PART_HEAD = 'diff --git a/{} b/{}\n' | |
40 | |
41 state = NORMAL | 41 state = NORMAL |
42 new_name = None | 42 new_name = None |
43 | 43 |
44 for line in lines: | 44 for line in lines: |
45 if state is NORMAL: | 45 if state is NORMAL: |
46 if line.startswith('Index: '): | 46 if line.startswith('Index: '): |
47 new_name = line[7:].strip('\n') | 47 new_name = line[7:].strip('\n') |
48 state = INDEX | 48 state = INDEX |
49 else: | 49 else: |
50 yield line | 50 yield line |
(...skipping 13 matching lines...) Expand all Loading... | |
64 # File copied. | 64 # File copied. |
65 old_name = line[10:].strip('\n') | 65 old_name = line[10:].strip('\n') |
66 yield GIT_PART_HEAD.format(old_name, new_name) | 66 yield GIT_PART_HEAD.format(old_name, new_name) |
67 else: | 67 else: |
68 # File added or removed or changed. | 68 # File added or removed or changed. |
69 yield GIT_PART_HEAD.format(new_name, new_name) | 69 yield GIT_PART_HEAD.format(new_name, new_name) |
70 yield line | 70 yield line |
71 state = NORMAL | 71 state = NORMAL |
72 | 72 |
73 | 73 |
74 def script(): | 74 def main(): |
Sebastian Noack
2016/05/12 21:44:26
This seems to be an unconventional name for an ent
Vasily Kuznetsov
2016/05/12 23:25:16
Quickly testing is still pretty easy after `setup.
Sebastian Noack
2016/05/12 23:48:51
I'm just curious is this common practice? Most Pyt
Vasily Kuznetsov
2016/05/17 15:57:30
if __name__ == '__main__' allows running the scrip
Sebastian Noack
2016/05/17 16:04:09
I don't have a strong opinion here.
| |
75 for line in rietveld_to_git(sys.stdin): | 75 for line in rietveld_to_git(sys.stdin): |
76 sys.stdout.write(line) | 76 sys.stdout.write(line) |
LEFT | RIGHT |