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.
This commit is contained in:
Paul Gauthier 2023-05-10 09:12:14 -07:00
parent 9ab1a0d513
commit 70d01f5434

View file

@ -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)