Skip to content

Commit e665e98

Browse files
jholgitster
authored andcommitted
git-p4: pre-compile RCS keyword regexes
Previously git-p4.py would compile one of two regular expressions for ever RCS keyword-enabled file. This patch improves simplifies the code by pre-compiling the two regular expressions when the script first loads. Signed-off-by: Joel Holdsworth <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8618d32 commit e665e98

File tree

1 file changed

+18
-30
lines changed

1 file changed

+18
-30
lines changed

git-p4.py

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@
5656

5757
p4_access_checked = False
5858

59+
re_ko_keywords = re.compile(r'\$(Id|Header)(:[^$\n]+)?\$')
60+
re_k_keywords = re.compile(r'\$(Id|Header|Author|Date|DateTime|Change|File|Revision)(:[^$\n]+)?\$')
61+
5962
def p4_build_cmd(cmd):
6063
"""Build a suitable p4 command line.
6164
@@ -577,20 +580,12 @@ def p4_type(f):
577580
#
578581
def p4_keywords_regexp_for_type(base, type_mods):
579582
if base in ("text", "unicode", "binary"):
580-
kwords = None
581583
if "ko" in type_mods:
582-
kwords = 'Id|Header'
584+
return re_ko_keywords
583585
elif "k" in type_mods:
584-
kwords = 'Id|Header|Author|Date|DateTime|Change|File|Revision'
586+
return re_k_keywords
585587
else:
586588
return None
587-
pattern = r"""
588-
\$ # Starts with a dollar, followed by...
589-
(%s) # one of the keywords, followed by...
590-
(:[^$\n]+)? # possibly an old expansion, followed by...
591-
\$ # another dollar
592-
""" % kwords
593-
return pattern
594589
else:
595590
return None
596591

@@ -1753,15 +1748,13 @@ def prepareLogMessage(self, template, message, jobs):
17531748

17541749
return result
17551750

1756-
def patchRCSKeywords(self, file, pattern):
1757-
# Attempt to zap the RCS keywords in a p4 controlled file matching the given pattern
1751+
def patchRCSKeywords(self, file, regexp):
1752+
# Attempt to zap the RCS keywords in a p4 controlled file matching the given regex
17581753
(handle, outFileName) = tempfile.mkstemp(dir='.')
17591754
try:
17601755
with os.fdopen(handle, "w+") as outFile, open(file, "r") as inFile:
1761-
regexp = re.compile(pattern, re.VERBOSE)
17621756
for line in inFile.readlines():
1763-
line = regexp.sub(r'$\1$', line)
1764-
outFile.write(line)
1757+
outFile.write(regexp.sub(r'$\1$', line))
17651758
# Forcibly overwrite the original file
17661759
os.unlink(file)
17671760
shutil.move(outFileName, file)
@@ -2088,25 +2081,22 @@ def applyCommit(self, id):
20882081
# the patch to see if that's possible.
20892082
if gitConfigBool("git-p4.attemptRCSCleanup"):
20902083
file = None
2091-
pattern = None
20922084
kwfiles = {}
20932085
for file in editedFiles | filesToDelete:
20942086
# did this file's delta contain RCS keywords?
2095-
pattern = p4_keywords_regexp_for_file(file)
2096-
2097-
if pattern:
2087+
regexp = p4_keywords_regexp_for_file(file)
2088+
if regexp:
20982089
# this file is a possibility...look for RCS keywords.
2099-
regexp = re.compile(pattern, re.VERBOSE)
21002090
for line in read_pipe_lines(["git", "diff", "%s^..%s" % (id, id), file]):
21012091
if regexp.search(line):
21022092
if verbose:
2103-
print("got keyword match on %s in %s in %s" % (pattern, line, file))
2104-
kwfiles[file] = pattern
2093+
print("got keyword match on %s in %s in %s" % (regex.pattern, line, file))
2094+
kwfiles[file] = regexp
21052095
break
21062096

2107-
for file in kwfiles:
2097+
for file, regexp in kwfiles.items():
21082098
if verbose:
2109-
print("zapping %s with %s" % (line,pattern))
2099+
print("zapping %s with %s" % (line, regexp.pattern))
21102100
# File is being deleted, so not open in p4. Must
21112101
# disable the read-only bit on windows.
21122102
if self.isWindows and file not in editedFiles:
@@ -3026,12 +3016,10 @@ def streamOneP4File(self, file, contents):
30263016

30273017
# Note that we do not try to de-mangle keywords on utf16 files,
30283018
# even though in theory somebody may want that.
3029-
pattern = p4_keywords_regexp_for_type(type_base, type_mods)
3030-
if pattern:
3031-
regexp = re.compile(pattern, re.VERBOSE)
3032-
text = ''.join(decode_text_stream(c) for c in contents)
3033-
text = regexp.sub(r'$\1$', text)
3034-
contents = [ encode_text_stream(text) ]
3019+
regexp = p4_keywords_regexp_for_type(type_base, type_mods)
3020+
if regexp:
3021+
contents = [encode_text_stream(regexp.sub(
3022+
r'$\1$', ''.join(decode_text_stream(c) for c in contents)))]
30353023

30363024
if self.largeFileSystem:
30373025
(git_mode, contents) = self.largeFileSystem.processContent(git_mode, relPath, contents)

0 commit comments

Comments
 (0)