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

Unified 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.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « patchconv/README.md ('k') | patchconv/setup.py » ('j') | patchconv/tox.ini » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: patchconv/patchconv.py
===================================================================
new file mode 100644
--- /dev/null
+++ b/patchconv/patchconv.py
@@ -0,0 +1,76 @@
+# This file is part of Adblock Plus <https://adblockplus.org/>,
+# Copyright (C) 2006-2016 Eyeo GmbH
+#
+# Adblock Plus is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 3 as
+# published by the Free Software Foundation.
+#
+# Adblock Plus is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
+
+import sys
+
+
+def rietveld_to_git(lines):
+ """Convert patch from Rietveld format to Git format.
+
+ Rietveld format looks similar to SVN patch format but it can also
+ contain Git extensions if it was produced by `upload.py` run from
+ a project managed by Git or Mercurial. The output format is the
+ original Git patch as produced by `git diff` or `hg diff --git`.
+ It can be applied by `hg import` or `git apply`.
+
+ Arguments:
+ lines -- lines of the patch.
+ Returns:
+ Lines of the converted patch.
+ """
+ # States of the conversion state machine.
+ NORMAL, INDEX, METAINFO = range(3)
+ # Double line that SVN uses to separate file name from patch content.
+ SVN_SEPARATOR = '=' * 67
+ # Template for the line from Git diff which is replaced during SVN
+ # conversion.
+ GIT_PART_HEAD = 'diff --git a/{} b/{}\n'
+
+ state = NORMAL
+ new_name = None
+
+ for line in lines:
+ if state is NORMAL:
+ if line.startswith('Index: '):
+ new_name = line[7:].strip('\n')
+ state = INDEX
+ else:
+ yield line
+ elif state is INDEX:
+ if line.startswith(SVN_SEPARATOR):
+ state = METAINFO
+ else:
+ yield 'Index: {}\n'.format(new_name)
+ yield line
+ state = NORMAL
+ elif state is METAINFO:
+ if line.startswith('rename from '):
+ # File renamed.
+ old_name = line[12:].strip('\n')
+ yield GIT_PART_HEAD.format(old_name, new_name)
+ elif line.startswith('copy from '):
+ # File copied.
+ old_name = line[10:].strip('\n')
+ yield GIT_PART_HEAD.format(old_name, new_name)
+ else:
+ # File added or removed or changed.
+ yield GIT_PART_HEAD.format(new_name, new_name)
+ yield line
+ state = NORMAL
+
+
+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.
+ for line in rietveld_to_git(sys.stdin):
+ sys.stdout.write(line)
« 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