46 lines
1.2 KiB
Python
46 lines
1.2 KiB
Python
import os
|
|
from .tracker import save
|
|
import git
|
|
from pathlib import Path
|
|
|
|
GITMODULES_FILE = '.gitmodules'
|
|
|
|
|
|
def move_submodule(repo_path: Path, old_path: Path, new_path: Path) -> None:
|
|
"""
|
|
Move a submodule to a new location.
|
|
|
|
Parameters
|
|
----------
|
|
repo_path: Path
|
|
Path to the repository.
|
|
old_path: Path
|
|
The old path of the module.
|
|
new_path: Path
|
|
The new path of the module.
|
|
"""
|
|
|
|
os.rename(repo_path / old_path, repo_path / new_path)
|
|
|
|
gitmodules_file_path = repo_path / GITMODULES_FILE
|
|
|
|
# update paths in .gitmodules
|
|
with open(gitmodules_file_path, 'r') as file:
|
|
lines = [line.strip() for line in file]
|
|
|
|
updated_lines = []
|
|
for line in lines:
|
|
if str(old_path) in line:
|
|
line = line.replace(str(old_path), str(new_path))
|
|
updated_lines.append(line)
|
|
|
|
with open(gitmodules_file_path, 'w') as file:
|
|
file.write("\n".join(updated_lines))
|
|
|
|
# stage .gitmodules in git
|
|
repo = git.Repo(repo_path)
|
|
repo.git.add('.gitmodules')
|
|
# save new state of the dataset
|
|
save(repo_path, message=f"Move module from {old_path} to {new_path}", files=[Path('.gitmodules'), repo_path])
|
|
|
|
return
|