Skip to content

Commit

Permalink
git-p4: simplify regex pattern generation for parsing diff-tree
Browse files Browse the repository at this point in the history
It is not clear why a generator was used to create the regex used to
parse git-diff-tree output; I assume an early implementation required
it, but is not part of the mainline change.

Simply use a lazily initialized global instead.

Signed-off-by: Yang Zhao <yang.zhao@skyboxlabs.com>
Reviewed-by: Ben Keene <seraphire@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
yangskyboxlabs authored and gitster committed Jan 15, 2020
1 parent 2e2aa8d commit ce425eb
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions git-p4.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,12 +544,7 @@ def getGitTags():
gitTags.add(tag)
return gitTags

def diffTreePattern():
# This is a simple generator for the diff tree regex pattern. This could be
# a class variable if this and parseDiffTreeEntry were a part of a class.
pattern = re.compile(':(\d+) (\d+) (\w+) (\w+) ([A-Z])(\d+)?\t(.*?)((\t(.*))|$)')
while True:
yield pattern
_diff_tree_pattern = None

def parseDiffTreeEntry(entry):
"""Parses a single diff tree entry into its component elements.
Expand All @@ -570,7 +565,11 @@ def parseDiffTreeEntry(entry):
If the pattern is not matched, None is returned."""

match = diffTreePattern().next().match(entry)
global _diff_tree_pattern
if not _diff_tree_pattern:
_diff_tree_pattern = re.compile(':(\d+) (\d+) (\w+) (\w+) ([A-Z])(\d+)?\t(.*?)((\t(.*))|$)')

match = _diff_tree_pattern.match(entry)
if match:
return {
'src_mode': match.group(1),
Expand Down

0 comments on commit ce425eb

Please sign in to comment.