From 70d01f54347f7d241779ef2b0915e2e5d0ac37a9 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Wed, 10 May 2023 09:12:14 -0700 Subject: [PATCH] aider: Implemented `find_common_root` method to set `self.root` to the most specific directory that all the file paths in `self.fnames` have in common. # Context: USER: thanks, i used that. ASSISTANT: You're welcome! If you have any more questions or need further assistance, feel free to ask. USER: implement the find_common_root method. it should set self.root to be the most specific directory which all the self.fnames have in common ASSISTANT: To implement the `find_common_root` method, you can use the `os.path.commonprefix` function to find the common directory for all the file paths in `self.fnames`. Here's the implementation: ```python aider/coder.py <<<<<<< ORIGINAL def find_common_root(self): pass ======= def find_common_root(self): import os common_prefix = os.path.commonprefix(list(self.fnames)) self.root = os.path.dirname(common_prefix) self.console.print(f"[red]Common root directory: {self.root}") >>>>>>> UPDATED ``` This will set `self.root` to the most specific directory that all the file paths in `self.fnames` have in common. --- aider/coder.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/aider/coder.py b/aider/coder.py index 9528f2d1c..e782fde61 100755 --- a/aider/coder.py +++ b/aider/coder.py @@ -75,14 +75,17 @@ class Coder: if num_repos == 0: self.console.print("[red bold]Files are not in a git repo.") - return if num_repos > 1: self.console.print("[red bold]Files are in different git repos.") + if num_repos != 1: + self.find_common_root() return # https://github.com/gitpython-developers/GitPython/issues/427 repo = git.Repo(repo_paths.pop(), odbt=git.GitDB) + self.root = repo.working_tree_dir + new_files = [] for fname in self.fnames: relative_fname = os.path.relpath(fname, repo.working_tree_dir)