From 99e954c37b622a1d23afba7b8e202b321cd6fdb4 Mon Sep 17 00:00:00 2001 From: Dominik Jain Date: Mon, 1 Sep 2025 22:09:40 +0200 Subject: [PATCH] Add .gitignore file to project .serena folder by default (ignoring cache) #283 --- src/serena/project.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/serena/project.py b/src/serena/project.py index a58e037c..d2da0d0b 100644 --- a/src/serena/project.py +++ b/src/serena/project.py @@ -5,7 +5,7 @@ from pathlib import Path import pathspec from serena.config.serena_config import DEFAULT_TOOL_TIMEOUT, ProjectConfig -from serena.constants import SERENA_MANAGED_DIR_IN_HOME +from serena.constants import SERENA_MANAGED_DIR_IN_HOME, SERENA_MANAGED_DIR_NAME from serena.text_utils import MatchedConsecutiveLines, search_files from serena.util.file_system import GitignoreParser, match_path from solidlsp import SolidLanguageServer @@ -22,6 +22,14 @@ class Project: self.project_config = project_config self.is_newly_created = is_newly_created + # create .gitignore file in the project's Serena data folder if not yet present + serena_data_gitignore_path = os.path.join(self.path_to_serena_data_folder(), ".gitignore") + if not os.path.exists(serena_data_gitignore_path): + os.makedirs(os.path.dirname(serena_data_gitignore_path), exist_ok=True) + log.info(f"Creating .gitignore file in {serena_data_gitignore_path}") + with open(serena_data_gitignore_path, "w", encoding="utf-8") as f: + f.write(f"/{SolidLanguageServer.CACHE_FOLDER_NAME}\n") + # gather ignored paths from the project configuration and gitignore files ignored_patterns = project_config.ignored_paths if len(ignored_patterns) > 0: @@ -60,6 +68,9 @@ class Project: project_config = ProjectConfig.load(project_root, autogenerate=autogenerate) return Project(project_root=str(project_root), project_config=project_config) + def path_to_serena_data_folder(self) -> str: + return os.path.join(self.project_root, SERENA_MANAGED_DIR_NAME) + def path_to_project_yml(self) -> str: return os.path.join(self.project_root, self.project_config.rel_path_to_project_yml()) @@ -291,5 +302,5 @@ class Project: ls_logger, self.project_root, timeout=ls_timeout, - solidlsp_settings=SolidLSPSettings(solidlsp_dir=SERENA_MANAGED_DIR_IN_HOME), + solidlsp_settings=SolidLSPSettings(solidlsp_dir=SERENA_MANAGED_DIR_IN_HOME, project_data_relative_path=SERENA_MANAGED_DIR_NAME), )