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: Address review comments Created May 12, 2016, 11:21 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 # 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
26
27 def rietveld_to_git(lines):
28 """Convert patch from Rietveld format to Git format.
29
30 Rietveld format looks similar to SVN patch format but it can also
31 contain Git extensions if it was produced by `upload.py` run from
32 a project managed by Git or Mercurial. The output format is the
33 original Git patch as produced by `git diff` or `hg diff --git`.
34 It can be applied by `hg import` or `git apply`.
35
36 Arguments:
37 lines -- lines of the patch.
38 Returns:
39 Lines of the converted patch.
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 main():
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