From fc8c9a07ec914909486fe410b9817d28f4742c95 Mon Sep 17 00:00:00 2001 From: Justus Kuhlmann Date: Tue, 20 Aug 2024 11:25:27 +0000 Subject: [PATCH] sight rewrite of git tools --- backlogger/git_tools.py | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/backlogger/git_tools.py b/backlogger/git_tools.py index c003e29..5ebcaa2 100644 --- a/backlogger/git_tools.py +++ b/backlogger/git_tools.py @@ -1,6 +1,8 @@ import os import datalad.api as dl +GITMODULES_FILE = '.gitmodules' + def move_submodule(repo_path, old_path, new_path): """ @@ -8,24 +10,31 @@ def move_submodule(repo_path, old_path, new_path): Parameters ---------- - + repo_path: str + Path to the repository. old_path: str The old path of the module. new_path: str The new path of the module. """ - os.rename(repo_path + "/" + old_path, repo_path + "/" + new_path) - with open(repo_path + '/.gitmodules', 'r') as fp: - lines = fp.readlines() + os.rename(os.path.join(repo_path, old_path), os.path.join(repo_path, new_path)) + # Öffne die.gitmodules-Datei + gitmodules_file_path = os.path.join(repo_path, GITMODULES_FILE) + with open(gitmodules_file_path, 'r') as file: + lines = [line.strip() for line in file] + # Ersetzen aller Vorkommnisse von old_path durch new_path + updated_lines = [] for line in lines: - if line.startswith('\tpath'): + if old_path in line: line = line.replace(old_path, new_path) - break - if line.startswith('[submodule "projects/tmp"]'): - line = line.replace(old_path, new_path) - break + updated_lines.append(line) + + # Speichen der aktualisierten.gitmodules-Datei + with open(gitmodules_file_path, 'w') as file: + file.write("\n".join(updated_lines)) + with open(repo_path + '/.gitmodules', 'w') as fp: fp.writelines(lines) - dl.save(repo_path, message="Move module from " + old_path + " to " + new_path, dataset=repo_path) + dl.save(repo_path, message=f"Move module from {old_path} to {new_path}", dataset=repo_path)