corrlib/corrlib/git_tools.py
Justus Kuhlmann 8162758cec
Some checks failed
Ruff / ruff (push) Waiting to run
Pytest / pytest (3.12) (push) Successful in 1m15s
Pytest / pytest (3.13) (push) Has been cancelled
Mypy / mypy (push) Successful in 1m13s
Pytest / pytest (3.14) (push) Has been cancelled
use pathlib.Path for directories and files
2026-03-23 16:15:55 +01:00

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