corrlib/backlogger/git_tools.py
2024-06-19 12:55:42 +00:00

31 lines
885 B
Python

import os
import datalad.api as dl
def move_submodule(repo_path, old_path, new_path):
"""
Move a submodule to a new location.
Parameters
----------
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()
for line in lines:
if line.startswith('\tpath'):
line = line.replace(old_path, new_path)
break
if line.startswith('[submodule "projects/tmp"]'):
line = line.replace(old_path, new_path)
break
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)