better moving of submodules

This commit is contained in:
Justus Kuhlmann 2025-03-30 18:04:05 +00:00
parent fef6d95f97
commit ecad536c9e

View file

@ -1,5 +1,6 @@
import os
import datalad.api as dl
import git
GITMODULES_FILE = '.gitmodules'
@ -19,7 +20,10 @@ def move_submodule(repo_path, old_path, new_path):
"""
os.rename(os.path.join(repo_path, old_path), os.path.join(repo_path, new_path))
gitmodules_file_path = os.path.join(repo_path, GITMODULES_FILE)
# update paths in .gitmodules
with open(gitmodules_file_path, 'r') as file:
lines = [line.strip() for line in file]
@ -32,6 +36,8 @@ def move_submodule(repo_path, old_path, new_path):
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)
# stage .gitmodules in git
repo = git.Repo(repo_path)
repo.git.add('.gitmodules')
# save new state of the dataset
dl.save(repo_path, message=f"Move module from {old_path} to {new_path}", dataset=repo_path)