浏览代码

Avoid re module.

Jelmer Vernooij 5 年之前
父节点
当前提交
3f87027ed1
共有 2 个文件被更改,包括 18 次插入7 次删除
  1. 2 7
      dulwich/repo.py
  2. 16 0
      dulwich/tests/test_repository.py

+ 2 - 7
dulwich/repo.py

@@ -34,7 +34,6 @@ import os
 import sys
 import stat
 import time
-import re
 
 from dulwich.errors import (
     NoIndexPresent,
@@ -113,9 +112,6 @@ BASE_DIRECTORIES = [
 
 DEFAULT_REF = b'refs/heads/master'
 
-quoted_email_re = re.compile(br"^\s*<\s*(.*)\s*>\s*$")
-
-
 class InvalidUserIdentity(Exception):
     """User identity is not of the format 'user <email>'"""
 
@@ -178,9 +174,8 @@ def get_user_identity(config, kind=None):
         email = default_email
         if not isinstance(email, bytes):
             email = email.encode('utf-8')
-    m = quoted_email_re.match(email)
-    if m:
-        email = m.group(1)
+    if email.startswith(b'<') and email.endswith(b'>'):
+        email = email[1:-1]
     return (user + b" <" + email + b">")
 
 

+ 16 - 0
dulwich/tests/test_repository.py

@@ -882,6 +882,22 @@ class BuildRepoRootTests(TestCase):
         r = self._repo
         c = r.get_config()
         c.set((b"user", ), b"name", b"Jelmer")
+        c.set((b"user", ), b"email", b"jelmer@apache.org")
+        c.write_to_path()
+        commit_sha = r.do_commit(b'message')
+        self.assertEqual(
+            b"Jelmer <jelmer@apache.org>",
+            r[commit_sha].author)
+        self.assertEqual(
+            b"Jelmer <jelmer@apache.org>",
+            r[commit_sha].committer)
+
+    def test_commit_config_identity_strips_than(self):
+        # commit falls back to the users' identity if it wasn't specified,
+        # and strips superfluous <>
+        r = self._repo
+        c = r.get_config()
+        c.set((b"user", ), b"name", b"Jelmer")
         c.set((b"user", ), b"email", b"<jelmer@apache.org>")
         c.write_to_path()
         commit_sha = r.do_commit(b'message')