From dfd92073f45b257018f895696b614b57523d1052 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Fri, 21 Jul 2023 15:41:57 -0300 Subject: [PATCH] Handle files in the git repo which have been deleted but not committed, with test --- aider/coders/base_coder.py | 4 ++-- tests/test_coder.py | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/aider/coders/base_coder.py b/aider/coders/base_coder.py index 14e076764..35cd6e25c 100755 --- a/aider/coders/base_coder.py +++ b/aider/coders/base_coder.py @@ -957,10 +957,10 @@ class Coder: return files def get_last_modified(self): - files = self.get_all_abs_files() + files = [Path(fn) for fn in self.get_all_abs_files() if Path(fn).exists()] if not files: return 0 - return max(Path(path).stat().st_mtime for path in files) + return max(path.stat().st_mtime for path in files) def get_addable_relative_files(self): return set(self.get_all_relative_files()) - set(self.get_inchat_relative_files()) diff --git a/tests/test_coder.py b/tests/test_coder.py index 41ae7e644..818f6fef9 100644 --- a/tests/test_coder.py +++ b/tests/test_coder.py @@ -24,6 +24,29 @@ class TestCoder(unittest.TestCase): def tearDown(self): self.patcher.stop() + def test_get_last_modified(self): + # Mock the IO object + mock_io = MagicMock() + + with GitTemporaryDirectory(): + repo = git.Repo(Path.cwd()) + fname = Path("new.txt") + fname.touch() + repo.git.add(str(fname)) + repo.git.commit("-m", "new") + + # Initialize the Coder object with the mocked IO and mocked repo + coder = Coder.create(models.GPT4, None, mock_io) + + mod = coder.get_last_modified() + + fname.write_text("hi") + mod_newer = coder.get_last_modified() + self.assertLess(mod, mod_newer) + + fname.unlink() + self.assertEqual(coder.get_last_modified(), 0) + def test_should_dirty_commit(self): # Mock the IO object mock_io = MagicMock()