Browse Source

Special case boolean config values

David Hotham 2 years ago
parent
commit
e9071c627f
3 changed files with 21 additions and 20 deletions
  1. 14 6
      dulwich/config.py
  2. 1 4
      dulwich/porcelain.py
  3. 6 10
      dulwich/repo.py

+ 14 - 6
dulwich/config.py

@@ -142,8 +142,8 @@ class CaseInsensitiveOrderedMultiDict(MutableMapping):
 BytesLike = Union[bytes, str]
 Key = Tuple[bytes, ...]
 KeyLike = Union[bytes, str, Tuple[BytesLike, ...]]
-Value = Union[bytes, bool]
-ValueLike = Union[bytes, str, bool]
+Value = bytes
+ValueLike = Union[bytes, str]
 
 
 class Config(object):
@@ -213,6 +213,18 @@ class Config(object):
         """
         raise NotImplementedError(self.set)
 
+    def set_boolean(self, section: KeyLike, name: BytesLike, value: bool) -> None:
+        """Set a boolean configuration value.
+
+        Args:
+          section: Tuple with section name and optional subsection namee
+          name: Name of the configuration value, including section
+            and optional subsection
+          value: value of the setting
+        """
+        text = b"true" if value else b"false"
+        self.set(section, name, text)
+
     def items(self, section: KeyLike) -> Iterator[Tuple[bytes, Value]]:
         """Iterate over the configuration pairs for a specific section.
 
@@ -759,9 +771,5 @@ def parse_submodules(config: ConfigFile) -> Iterator[Tuple[bytes, bytes, bytes]]
         section_kind, section_name = section
         if section_kind == b"submodule":
             sm_path = config.get(section, b"path")
-            assert isinstance(sm_path, bytes)
-            assert sm_path is not None
             sm_url = config.get(section, b"url")
-            assert sm_url is not None
-            assert isinstance(sm_url, bytes)
             yield (sm_path, sm_url, section_name)

+ 1 - 4
dulwich/porcelain.py

@@ -1001,10 +1001,7 @@ def get_remote_repo(
 
     if config.has_section(section):
         remote_name = encoded_location.decode()
-        url = config.get(section, "url")
-        assert url is not None
-        assert isinstance(url, bytes)
-        encoded_location = url
+        encoded_location = config.get(section, "url")
     else:
         remote_name = None
 

+ 6 - 10
dulwich/repo.py

@@ -195,16 +195,12 @@ def get_user_identity(config: "StackedConfig", kind: Optional[str] = None) -> by
             email = email_uc.encode("utf-8")
     if user is None:
         try:
-            config_value = config.get(("user",), "name")
-            assert isinstance(config_value, bytes)
-            user = config_value
+            user = config.get(("user",), "name")
         except KeyError:
             user = None
     if email is None:
         try:
-            config_value = config.get(("user",), "email")
-            assert isinstance(config_value, bytes)
-            email = config_value
+            email = config.get(("user",), "email")
         except KeyError:
             email = None
     default_user, default_email = _get_default_identity()
@@ -370,12 +366,12 @@ class BaseRepo(object):
         cf = ConfigFile()
         cf.set("core", "repositoryformatversion", "0")
         if self._determine_file_mode():
-            cf.set("core", "filemode", True)
+            cf.set_boolean("core", "filemode", True)
         else:
-            cf.set("core", "filemode", False)
+            cf.set_boolean("core", "filemode", False)
 
-        cf.set("core", "bare", bare)
-        cf.set("core", "logallrefupdates", True)
+        cf.set_boolean("core", "bare", bare)
+        cf.set_boolean("core", "logallrefupdates", True)
         cf.write_to_file(f)
         self._put_named_file("config", f.getvalue())
         self._put_named_file(os.path.join("info", "exclude"), b"")