mirror of
https://github.com/FoundationAgents/MetaGPT.git
synced 2026-04-25 16:56:26 +02:00
feat: +SummarizeCode, refactor project_name
This commit is contained in:
parent
838b3cfcc8
commit
9d84c8f047
31 changed files with 671 additions and 245 deletions
|
|
@ -14,6 +14,7 @@ from typing import Set
|
|||
|
||||
import aiofiles
|
||||
|
||||
from metagpt.config import CONFIG
|
||||
from metagpt.logs import logger
|
||||
|
||||
|
||||
|
|
@ -81,7 +82,7 @@ class DependencyFile:
|
|||
if persist:
|
||||
await self.save()
|
||||
|
||||
async def get(self, filename: Path | str, persist=False):
|
||||
async def get(self, filename: Path | str, persist=True):
|
||||
"""Get dependencies for a file asynchronously.
|
||||
|
||||
:param filename: The filename or path.
|
||||
|
|
@ -91,7 +92,7 @@ class DependencyFile:
|
|||
if persist:
|
||||
await self.load()
|
||||
|
||||
root = self._filename.parent
|
||||
root = CONFIG.git_repo.workdir
|
||||
try:
|
||||
key = Path(filename).relative_to(root)
|
||||
except ValueError:
|
||||
|
|
|
|||
|
|
@ -151,6 +151,17 @@ class FileRepository:
|
|||
relative_files[str(rf)] = ct
|
||||
return relative_files
|
||||
|
||||
@property
|
||||
def all_files(self) -> List:
|
||||
"""Get a dictionary of all files in the repository.
|
||||
|
||||
The dictionary includes file paths relative to the current FileRepository.
|
||||
|
||||
:return: A dictionary where keys are file paths and values are file information.
|
||||
:rtype: List
|
||||
"""
|
||||
return self._git_repo.get_files(relative_path=self._relative_path)
|
||||
|
||||
def get_change_dir_files(self, dir: Path | str) -> List:
|
||||
"""Get the files in a directory that have changed.
|
||||
|
||||
|
|
@ -259,3 +270,25 @@ class FileRepository:
|
|||
"""
|
||||
file_repo = CONFIG.git_repo.new_file_repository(relative_path=relative_path)
|
||||
return await file_repo.save_doc(doc=doc, with_suffix=with_suffix, dependencies=dependencies)
|
||||
|
||||
async def delete(self, filename: Path | str):
|
||||
"""Delete a file from the file repository.
|
||||
|
||||
This method deletes a file from the file repository based on the provided filename.
|
||||
|
||||
:param filename: The name or path of the file to be deleted.
|
||||
:type filename: Path or str
|
||||
"""
|
||||
pathname = self.workdir / filename
|
||||
if not pathname.exists():
|
||||
return
|
||||
pathname.unlink(missing_ok=True)
|
||||
|
||||
dependency_file = await self._git_repo.get_dependency()
|
||||
await dependency_file.update(filename=pathname, dependencies=None)
|
||||
logger.info(f"remove dependency key: {str(pathname)}")
|
||||
|
||||
@staticmethod
|
||||
async def delete_file(filename: Path | str, relative_path: Path | str = "."):
|
||||
file_repo = CONFIG.git_repo.new_file_repository(relative_path=relative_path)
|
||||
await file_repo.delete(filename=filename)
|
||||
|
|
|
|||
|
|
@ -8,10 +8,11 @@
|
|||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import shutil
|
||||
from enum import Enum
|
||||
from pathlib import Path
|
||||
from typing import Dict
|
||||
from typing import Dict, List
|
||||
|
||||
from git.repo import Repo
|
||||
from git.repo.fun import is_git_dir
|
||||
|
|
@ -196,10 +197,46 @@ class GitRepository:
|
|||
if new_path.exists():
|
||||
logger.info(f"Delete directory {str(new_path)}")
|
||||
shutil.rmtree(new_path)
|
||||
self.workdir.rename(new_path)
|
||||
try:
|
||||
shutil.move(src=str(self.workdir), dst=str(new_path))
|
||||
except Exception as e:
|
||||
logger.warning(f"Move {str(self.workdir)} to {str(new_path)} error: {e}")
|
||||
logger.info(f"Rename directory {str(self.workdir)} to {str(new_path)}")
|
||||
self._repository = Repo(new_path)
|
||||
|
||||
def get_files(self, relative_path: Path | str, root_relative_path: Path | str = None) -> List:
|
||||
"""Retrieve a list of files in the specified relative path.
|
||||
|
||||
The method returns a list of file paths relative to the current FileRepository.
|
||||
|
||||
:param relative_path: The relative path within the repository.
|
||||
:type relative_path: Path or str
|
||||
:param root_relative_path: The root relative path within the repository.
|
||||
:type root_relative_path: Path or str
|
||||
:return: A list of file paths in the specified directory.
|
||||
:rtype: List[str]
|
||||
"""
|
||||
try:
|
||||
relative_path = Path(relative_path).relative_to(self.workdir)
|
||||
except ValueError:
|
||||
relative_path = Path(relative_path)
|
||||
|
||||
if not root_relative_path:
|
||||
root_relative_path = Path(self.workdir) / relative_path
|
||||
files = []
|
||||
try:
|
||||
directory_path = Path(self.workdir) / relative_path
|
||||
for file_path in directory_path.iterdir():
|
||||
if file_path.is_file():
|
||||
rpath = file_path.relative_to(root_relative_path)
|
||||
files.append(str(rpath))
|
||||
else:
|
||||
subfolder_files = self.get_files(relative_path=file_path, root_relative_path=root_relative_path)
|
||||
files.extend(subfolder_files)
|
||||
except Exception as e:
|
||||
logger.error(f"Error: {e}")
|
||||
return files
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
path = DEFAULT_WORKSPACE_ROOT / "git"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue