Commit Diff


commit - 031f88d75f394ffbdee404c3b24fece9f6911a98
commit + acd44c1860f4ea7e6106e0051c94645fa3a4564a
blob - 85c2a5bf2041f9546f86bc088dff9040204ad870
blob + ae34d80ac591375ba96c32e03844e440ec9beec2
--- NEWS
+++ NEWS
@@ -1,4 +1,8 @@
 0.20.47	UNRELEASED
+
+ * Fix Repo.reset_index.
+   Previously, it instead took the union with the given tree.
+   (Christian Sattler, #1072)
 
  * Add -b argument to ``dulwich clone``.
    (Jelmer Vernooij)
blob - ede53e16ec89bbfc6320db16fe4d83f721f57e8d
blob + 7caed0955a8c38197ae6d2f04eb99bf27ea330fe
--- dulwich/index.py
+++ dulwich/index.py
@@ -316,17 +316,19 @@ def cleanup_mode(mode: int) -> int:
 class Index(object):
     """A Git Index file."""
 
-    def __init__(self, filename: Union[bytes, str]):
-        """Open an index file.
+    def __init__(self, filename: Union[bytes, str], read=True):
+        """Create an index object associated with the given filename.
 
         Args:
           filename: Path to the index file
+          read: Whether to initialize the index from the given file, should it exist.
         """
         self._filename = filename
         # TODO(jelmer): Store the version returned by read_index
         self._version = None
         self.clear()
-        self.read()
+        if read:
+            self.read()
 
     @property
     def path(self):
@@ -706,8 +708,7 @@ def build_index_from_tree(
     Note: existing index is wiped and contents are not merged
         in a working dir. Suitable only for fresh clones.
     """
-
-    index = Index(index_path)
+    index = Index(index_path, read=False)
     if not isinstance(root_path, bytes):
         root_path = os.fsencode(root_path)
 
blob - 40456bf756ba778f421c2127a5e89b0368ea7cf9
blob + ad48c89d6aafa1480a62d1d164f58280d36f4b28
--- dulwich/tests/test_repository.py
+++ dulwich/tests/test_repository.py
@@ -1404,6 +1404,7 @@ class BuildRepoRootTests(TestCase):
         porcelain.add(self._repo, paths=[full_path])
         self._repo.unstage([file])
         status = list(porcelain.status(self._repo))
+
         self.assertEqual([{'add': [], 'delete': [], 'modify': []}, [b'foo'], []], status)
 
     def test_unstage_remove_file(self):
@@ -1422,6 +1423,19 @@ class BuildRepoRootTests(TestCase):
         self._repo.unstage([file])
         status = list(porcelain.status(self._repo))
         self.assertEqual([{'add': [], 'delete': [], 'modify': []}, [b'foo'], []], status)
+
+    def test_reset_index(self):
+        r = self._repo
+        with open(os.path.join(r.path, 'a'), 'wb') as f:
+            f.write(b'changed')
+        with open(os.path.join(r.path, 'b'), 'wb') as f:
+            f.write(b'added')
+        r.stage(['a', 'b'])
+        status = list(porcelain.status(self._repo))
+        self.assertEqual([{'add': [b'b'], 'delete': [], 'modify': [b'a']}, [], []], status)
+        r.reset_index()
+        status = list(porcelain.status(self._repo))
+        self.assertEqual([{'add': [], 'delete': [], 'modify': []}, [], ['b']], status)
 
     @skipIf(
         sys.platform in ("win32", "darwin"),