git-p4: use raw string literals for regular expressions

Fixes several Python diagnostics about invalid escape sequences. The diagnostics appear for me in Python 3.12, and may appear in earlier versions. The fix is to use raw string literals so that backslashes are not interpreted as introducing escape sequences. Raw string literals are already in use in this file, so adding more does not impact toolchain compatibility. Signed-off-by: James Touton <bekenn@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

James Touton committed Jan 26, 2024 at 23:41 UTC 90231982800152c4d66d0feb026c2ed8d28949d4
1 file changed +13 -13
git-p4.py
+13 -13
@@ -689,8 +689,8 @@ def setP4ExecBit(file, mode):
689
690 if not isModeExec(mode):
691 p4Type = getP4OpenedType(file)
692 - p4Type = re.sub('^([cku]?)x(.*)', '\\1\\2', p4Type)
693 - p4Type = re.sub('(.*?\+.*?)x(.*?)', '\\1\\2', p4Type)
692 + p4Type = re.sub(r'^([cku]?)x(.*)', r'\1\2', p4Type)
693 + p4Type = re.sub(r'(.*?\+.*?)x(.*?)', r'\1\2', p4Type)
694 if p4Type[-1] == "+":
695 p4Type = p4Type[0:-1]
696
@@ -701,7 +701,7 @@ def getP4OpenedType(file):
701 """Returns the perforce file type for the given file."""
702
703 result = p4_read_pipe(["opened", wildcard_encode(file)])
704 - match = re.match(".*\((.+)\)( \*exclusive\*)?\r?$", result)
704 + match = re.match(r".*\((.+)\)( \*exclusive\*)?\r?$", result)
705 if match:
706 return match.group(1)
707 else:
@@ -757,7 +757,7 @@ def parseDiffTreeEntry(entry):
757
758 global _diff_tree_pattern
759 if not _diff_tree_pattern:
760 - _diff_tree_pattern = re.compile(':(\d+) (\d+) (\w+) (\w+) ([A-Z])(\d+)?\t(.*?)((\t(.*))|$)')
760 + _diff_tree_pattern = re.compile(r':(\d+) (\d+) (\w+) (\w+) ([A-Z])(\d+)?\t(.*?)((\t(.*))|$)')
761
762 match = _diff_tree_pattern.match(entry)
763 if match:
@@ -918,9 +918,9 @@ def p4CmdList(cmd, stdin=None, stdin_mode='w+b', cb=None, skip_info=False,
918 if len(result) > 0:
919 data = result[0].get('data')
920 if data:
921 - m = re.search('Too many rows scanned \(over (\d+)\)', data)
921 + m = re.search(r'Too many rows scanned \(over (\d+)\)', data)
922 if not m:
923 - m = re.search('Request too large \(over (\d+)\)', data)
923 + m = re.search(r'Request too large \(over (\d+)\)', data)
924
925 if m:
926 limit = int(m.group(1))
@@ -1452,7 +1452,7 @@ def wildcard_encode(path):
1452
1453
1454 def wildcard_present(path):
1455 - m = re.search("[*#@%]", path)
1455 + m = re.search(r"[*#@%]", path)
1456 return m is not None
1457
1458
@@ -3048,7 +3048,7 @@ class P4Sync(Command, P4UserMap):
3048 # Preserve everything in relative path name except leading
3049 # //depot/; just look at first prefix as they all should
3050 # be in the same depot.
3051 - depot = re.sub("^(//[^/]+/).*", r'\1', prefixes[0])
3051 + depot = re.sub(r"^(//[^/]+/).*", r'\1', prefixes[0])
3052 if p4PathStartsWith(path, depot):
3053 path = path[len(depot):]
3054
@@ -3603,7 +3603,7 @@ class P4Sync(Command, P4UserMap):
3603 commitFound = True
3604 else:
3605 gitCommit = read_pipe(["git", "rev-list", "--max-count=1",
3606 - "--reverse", ":/\[git-p4:.*change = %d\]" % changelist], ignore_error=True)
3606 + "--reverse", r":/\[git-p4:.*change = %d\]" % changelist], ignore_error=True)
3607 if len(gitCommit) == 0:
3608 print("importing label %s: could not find git commit for changelist %d" % (name, changelist))
3609 else:
@@ -4182,7 +4182,7 @@ class P4Sync(Command, P4UserMap):
4182 if len(self.changesFile) == 0:
4183 revision = "#head"
4184
4185 - p = re.sub("\.\.\.$", "", p)
4185 + p = re.sub(r"\.\.\.$", "", p)
4186 if not p.endswith("/"):
4187 p += "/"
4188
@@ -4291,7 +4291,7 @@ class P4Rebase(Command):
4291 die("Cannot find upstream branchpoint for rebase")
4292
4293 # the branchpoint may be p4/foo~3, so strip off the parent
4294 - upstream = re.sub("~[0-9]+$", "", upstream)
4294 + upstream = re.sub(r"~[0-9]+$", "", upstream)
4295
4296 print("Rebasing the current branch onto %s" % upstream)
4297 oldHead = read_pipe(["git", "rev-parse", "HEAD"]).strip()
@@ -4320,8 +4320,8 @@ class P4Clone(P4Sync):
4320 def defaultDestination(self, args):
4321 # TODO: use common prefix of args?
4322 depotPath = args[0]
4323 - depotDir = re.sub("(@[^@]*)$", "", depotPath)
4324 - depotDir = re.sub("(#[^#]*)$", "", depotDir)
4323 + depotDir = re.sub(r"(@[^@]*)$", "", depotPath)
4324 + depotDir = re.sub(r"(#[^#]*)$", "", depotDir)
4325 depotDir = re.sub(r"\.\.\.$", "", depotDir)
4326 depotDir = re.sub(r"/$", "", depotDir)
4327 return os.path.split(depotDir)[1]