corrlib/backlogger/git_tools.py
2024-08-20 11:25:27 +00:00

40 lines
1.2 KiB
Python

import os
import datalad.api as dl
GITMODULES_FILE = '.gitmodules'
def move_submodule(repo_path, old_path, new_path):
"""
Move a submodule to a new location.
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(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 old_path in line:
line = line.replace(old_path, new_path)
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=f"Move module from {old_path} to {new_path}", dataset=repo_path)