Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: patchconv/patchconv.py

Issue 29341347: Noissue - Add a script for converting Rietveld patches to Git format (Closed)
Patch Set: Created May 12, 2016, 5:50 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # This file is part of Adblock Plus <https://adblockplus.org/>,
2 # Copyright (C) 2006-2016 Eyeo GmbH
3 #
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
6 # published by the Free Software Foundation.
7 #
8 # Adblock Plus is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
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/>.
15
16 import sys
17
18
19 def rietveld_to_git(lines):
20 """Convert patch from Rietveld format to Git format.
21
22 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
24 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`.
26 It can be applied by `hg import` or `git apply`.
27
28 Arguments:
29 lines -- lines of the patch.
30 Returns:
31 Lines of the converted patch.
32 """
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
42 new_name = None
43
44 for line in lines:
45 if state is NORMAL:
46 if line.startswith('Index: '):
47 new_name = line[7:].strip('\n')
48 state = INDEX
49 else:
50 yield line
51 elif state is INDEX:
52 if line.startswith(SVN_SEPARATOR):
53 state = METAINFO
54 else:
55 yield 'Index: {}\n'.format(new_name)
56 yield line
57 state = NORMAL
58 elif state is METAINFO:
59 if line.startswith('rename from '):
60 # File renamed.
61 old_name = line[12:].strip('\n')
62 yield GIT_PART_HEAD.format(old_name, new_name)
63 elif line.startswith('copy from '):
64 # File copied.
65 old_name = line[10:].strip('\n')
66 yield GIT_PART_HEAD.format(old_name, new_name)
67 else:
68 # File added or removed or changed.
69 yield GIT_PART_HEAD.format(new_name, new_name)
70 yield line
71 state = NORMAL
72
73
74 def script():
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):
76 sys.stdout.write(line)
OLDNEW
« no previous file with comments | « patchconv/README.md ('k') | patchconv/setup.py » ('j') | patchconv/tox.ini » ('J')

Powered by Google App Engine
This is Rietveld