소스 검색

Fix GitAttributes.set_attribute to properly update existing patterns (#1669)

The previous implementation had issues with updating attributes for
existing patterns. This refactors the method to correctly track and
update the pattern index, ensuring attribute modifications are properly
persisted in the patterns list.
Jelmer Vernooij 1 개월 전
부모
커밋
8afea9c0cf
1개의 변경된 파일13개의 추가작업 그리고 15개의 파일을 삭제
  1. 13 15
      dulwich/attrs.py

+ 13 - 15
dulwich/attrs.py

@@ -345,30 +345,28 @@ class GitAttributes:
         """
         # Find existing pattern
         pattern_obj = None
-        attrs_dict: Optional[dict[bytes, AttributeValue]] = None
+        pattern_index = None
         for i, (p, attrs) in enumerate(self._patterns):
             if p.pattern == pattern:
                 pattern_obj = p
-                # Convert to mutable dict
-                attrs_dict = dict(attrs)
+                pattern_index = i
                 break
 
         if pattern_obj is None:
             # Create new pattern
             pattern_obj = Pattern(pattern)
-            attrs_dict = {}
+            attrs_dict: dict[bytes, AttributeValue] = {name: value}
             self._patterns.append((pattern_obj, attrs_dict))
-
-        # Update the attribute
-        if attrs_dict is None:
-            raise AssertionError("attrs_dict should not be None at this point")
-        attrs_dict[name] = value
-
-        # Update the pattern in the list
-        for i, (p, _) in enumerate(self._patterns):
-            if p.pattern == pattern:
-                self._patterns[i] = (p, attrs_dict)
-                break
+        else:
+            # Update existing pattern
+            # Create a new dict with updated attributes
+            assert (
+                pattern_index is not None
+            )  # pattern_index is set when pattern_obj is found
+            old_attrs = self._patterns[pattern_index][1]
+            new_attrs = dict(old_attrs)
+            new_attrs[name] = value
+            self._patterns[pattern_index] = (pattern_obj, new_attrs)
 
     def remove_pattern(self, pattern: bytes) -> None:
         """Remove all attributes for a pattern.