sight rewrite of git tools

This commit is contained in:
Justus Kuhlmann 2024-08-20 11:25:27 +00:00
parent 067667566b
commit fc8c9a07ec

View file

@ -1,6 +1,8 @@
import os import os
import datalad.api as dl import datalad.api as dl
GITMODULES_FILE = '.gitmodules'
def move_submodule(repo_path, old_path, new_path): def move_submodule(repo_path, old_path, new_path):
""" """
@ -8,24 +10,31 @@ def move_submodule(repo_path, old_path, new_path):
Parameters Parameters
---------- ----------
repo_path: str
Path to the repository.
old_path: str old_path: str
The old path of the module. The old path of the module.
new_path: str new_path: str
The new path of the module. The new path of the module.
""" """
os.rename(repo_path + "/" + old_path, repo_path + "/" + new_path) os.rename(os.path.join(repo_path, old_path), os.path.join(repo_path, new_path))
with open(repo_path + '/.gitmodules', 'r') as fp: # Öffne die.gitmodules-Datei
lines = fp.readlines() 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: for line in lines:
if line.startswith('\tpath'): if old_path in line:
line = line.replace(old_path, new_path) line = line.replace(old_path, new_path)
break updated_lines.append(line)
if line.startswith('[submodule "projects/tmp"]'):
line = line.replace(old_path, new_path) # Speichen der aktualisierten.gitmodules-Datei
break with open(gitmodules_file_path, 'w') as file:
file.write("\n".join(updated_lines))
with open(repo_path + '/.gitmodules', 'w') as fp: with open(repo_path + '/.gitmodules', 'w') as fp:
fp.writelines(lines) 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)