Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2673)

Unified Diff: Tools/Scripts/webkitpy/common/checkout/diff_parser.py

Issue 637503004: [webkitpy] Refactor diff_parser.py (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: updated Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Tools/Scripts/webkitpy/common/checkout/diff_parser.py
diff --git a/Tools/Scripts/webkitpy/common/checkout/diff_parser.py b/Tools/Scripts/webkitpy/common/checkout/diff_parser.py
index 3a9ea9224f7cda0d2f81fd3db2f25c304d5f3821..0f4c71325e4dce5e119a97d0af29b44ceb777fdf 100644
--- a/Tools/Scripts/webkitpy/common/checkout/diff_parser.py
+++ b/Tools/Scripts/webkitpy/common/checkout/diff_parser.py
@@ -33,37 +33,27 @@ import re
_log = logging.getLogger(__name__)
+conversion_patterns = (
+ (re.compile("^diff --git \w/(.+) \w/(?P<FilePath>.+)"), lambda matched: "Index: " + matched.group('FilePath') + "\n"),
+ (re.compile("^new file.*"), lambda matched: "\n"),
+ (re.compile("^index (([0-9a-f]{7}\.\.[0-9a-f]{7})|([0-9a-f]{40}\.\.[0-9a-f]{40})) [0-9]{6}"), lambda matched: ("=" * 67) + "\n"),
+ (re.compile("^--- \w/(?P<FilePath>.+)"), lambda matched: "--- " + matched.group('FilePath') + "\n"),
+ (re.compile("^\+\+\+ \w/(?P<FilePath>.+)"), lambda matched: "+++ " + matched.group('FilePath') + "\n"),
+)
-# FIXME: This is broken. We should compile our regexps up-front
-# instead of using a custom cache.
-_regexp_compile_cache = {}
+index_pattern = re.compile(r"^Index: (?P<FilePath>.+)")
+lines_changed_pattern = re.compile(r"^@@ -(?P<OldStartLine>\d+)(,\d+)? \+(?P<NewStartLine>\d+)(,\d+)? @@")
+diff_git_pattern = re.compile(r"^diff --git \w/")
-# FIXME: This function should be removed.
-def match(pattern, string):
- """Matches the string with the pattern, caching the compiled regexp."""
- if not pattern in _regexp_compile_cache:
- _regexp_compile_cache[pattern] = re.compile(pattern)
- return _regexp_compile_cache[pattern].match(string)
-
-
-# FIXME: This belongs on DiffParser (e.g. as to_svn_diff()).
def git_diff_to_svn_diff(line):
"""Converts a git formatted diff line to a svn formatted line.
Args:
line: A string representing a line of the diff.
"""
- # FIXME: This list should be a class member on DiffParser.
- # These regexp patterns should be compiled once instead of every time.
- conversion_patterns = (("^diff --git \w/(.+) \w/(?P<FilePath>.+)", lambda matched: "Index: " + matched.group('FilePath') + "\n"),
- ("^new file.*", lambda matched: "\n"),
- ("^index (([0-9a-f]{7}\.\.[0-9a-f]{7})|([0-9a-f]{40}\.\.[0-9a-f]{40})) [0-9]{6}", lambda matched: "===================================================================\n"),
- ("^--- \w/(?P<FilePath>.+)", lambda matched: "--- " + matched.group('FilePath') + "\n"),
- ("^\+\+\+ \w/(?P<FilePath>.+)", lambda matched: "+++ " + matched.group('FilePath') + "\n"))
-
for pattern, conversion in conversion_patterns:
- matched = match(pattern, line)
+ matched = pattern.match(line)
if matched:
return conversion(matched)
return line
@@ -74,7 +64,6 @@ def svn_diff_to_svn_diff(line):
return line
-# FIXME: This method belongs on DiffParser
def get_diff_converter(lines):
"""Gets a converter function of diff lines.
@@ -87,7 +76,7 @@ def get_diff_converter(lines):
# Stop when we find the first patch
if line[:3] == "+++" and lines[i + 1] == "---":
break
- if match(r"^diff --git \w/", line):
+ if diff_git_pattern.match(line):
return git_diff_to_svn_diff
return svn_diff_to_svn_diff
@@ -155,7 +144,7 @@ class DiffParser(object):
line = line.rstrip("\n")
line = transform_line(line)
- file_declaration = match(r"^Index: (?P<FilePath>.+)", line)
+ file_declaration = index_pattern.match(line)
if file_declaration:
filename = file_declaration.group('FilePath')
current_file = DiffFile(filename)
@@ -163,7 +152,7 @@ class DiffParser(object):
state = _DECLARED_FILE_PATH
continue
- lines_changed = match(r"^@@ -(?P<OldStartLine>\d+)(,\d+)? \+(?P<NewStartLine>\d+)(,\d+)? @@", line)
+ lines_changed = lines_changed_pattern.match(line)
if lines_changed:
if state != _DECLARED_FILE_PATH and state != _PROCESSING_CHUNK:
_log.error('Unexpected line change without file path '
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698