Skip to content
Snippets Groups Projects
Commit e697a33d authored by Ronald Jäpel's avatar Ronald Jäpel
Browse files

Add functionality to add external repo

parent d32eea7a
No related branches found
No related tags found
No related merge requests found
...@@ -37,6 +37,8 @@ class BaseRepo: ...@@ -37,6 +37,8 @@ class BaseRepo:
self._most_recent_branch = self.active_branch.name self._most_recent_branch = self.active_branch.name
self._earliest_commit = None self._earliest_commit = None
self.add = self._git.add
@property @property
def active_branch(self): def active_branch(self):
return self._git_repo.active_branch return self._git_repo.active_branch
...@@ -68,10 +70,6 @@ class BaseRepo: ...@@ -68,10 +70,6 @@ class BaseRepo:
self._earliest_commit = earliest_commit self._earliest_commit = earliest_commit
return self._earliest_commit return self._earliest_commit
# ToDo: test if functools.wraps can keep docstrings
def add(self, *args, **kwargs):
self._git.add(*args, **kwargs)
def add_remote(self, remote_url, remote_name="origin"): def add_remote(self, remote_url, remote_name="origin"):
""" """
ToDO add documentation ToDO add documentation
...@@ -83,10 +81,14 @@ class BaseRepo: ...@@ -83,10 +81,14 @@ class BaseRepo:
def push(self, remote=None, local_branch=None, remote_branch=None): def push(self, remote=None, local_branch=None, remote_branch=None):
""" """
# ToDo extend documentation Push local branch to remote.
:param remote: :param remote:
Name of the remote to push to.
:param local_branch: :param local_branch:
Name of the local branch to push.
:param remote_branch: :param remote_branch:
Name of the remote branch to push to.
:return: :return:
""" """
if local_branch is None: if local_branch is None:
...@@ -117,7 +119,7 @@ class BaseRepo: ...@@ -117,7 +119,7 @@ class BaseRepo:
:return: :return:
List of all staged changes. List of all staged changes.
""" """
self._git.add(".") self.add(".")
def reset_hard_to_head(self): def reset_hard_to_head(self):
proceed = input(f'The output directory contains the following uncommitted changes:\n' proceed = input(f'The output directory contains the following uncommitted changes:\n'
...@@ -175,7 +177,7 @@ class BaseRepo: ...@@ -175,7 +177,7 @@ class BaseRepo:
if update_packages: if update_packages:
self.update_package_list() self.update_package_list()
if add_all: if add_all:
self._git.add(".") self.add(".")
commit_return = self._git.commit("-m", message) commit_return = self._git.commit("-m", message)
print("\n" + commit_return + "\n") print("\n" + commit_return + "\n")
...@@ -215,7 +217,7 @@ class BaseRepo: ...@@ -215,7 +217,7 @@ class BaseRepo:
""" """
if not self.exist_uncomitted_changes: if not self.exist_uncomitted_changes:
raise RuntimeError("No changes in repo to stash.") raise RuntimeError("No changes in repo to stash.")
self._git.add(".") self.add(".")
self._git.stash() self._git.stash()
def prepare_new_branch(self, branch_name): def prepare_new_branch(self, branch_name):
...@@ -359,28 +361,49 @@ class ProjectRepo(BaseRepo): ...@@ -359,28 +361,49 @@ class ProjectRepo(BaseRepo):
with open(csv_filepath, "a") as f: with open(csv_filepath, "a") as f:
f.write(csv_data + "\n") f.write(csv_data + "\n")
self._output_repo._git.add(".") self._output_repo.add(".")
self._output_repo._git.commit("-m", output_branch_name) self._output_repo._git.commit("-m", output_branch_name)
self._output_repo._git.checkout(output_branch_name) self._output_repo._git.checkout(output_branch_name)
self._most_recent_branch = output_branch_name self._most_recent_branch = output_branch_name
def load_external_data(self, url, name=None, path=None, branch=None, commit=None): def load_external_repository(self, url, branch=None, commit=None, name=None, path=None, ):
"""
Load an external git repository as a git submodule into this repository.
:param url:
URL of the git repository.
:param branch:
Branch of the external repository to check out.
:param commit:
Commit of the external repository to check out.
:param name:
Optional custom name for the repository.
:param path:
Optional custom relative path where the repository should be placed.
:return:
""" """
name: str, if path is None or name is None:
path: PathLike, if "/" in url:
url: Union[str, None] = None, sep = "/"
branch: Union[str, None] = None,""" elif "\\" in url:
if path is None: sep = "\\"
repo_name = url.split("/")[-1] else:
path = os.path.join("external_repos", repo_name) raise RuntimeError("Could not automatically extract name from URL"
if name is None: " because the URL is not of a known format")
name = url.split("/")[-1]
if path is None:
repo_name = url.split(sep)[-1]
path = os.path.join("external_repos", repo_name)
if name is None:
name = url.split(sep)[-1]
self._git_repo.create_submodule(name=name, url=url, branch=branch, path=path) self._git_repo.create_submodule(name=name, url=url, branch=branch, path=path)
if commit is not None: if commit is not None:
submodule = BaseRepo(path) submodule = BaseRepo(path)
submodule._git.checkout(commit) submodule._git.checkout(commit)
full_path = os.path.join(self.working_dir, path)
return full_path
def load_previous_results(self, branch_name, file_path): def load_previous_results(self, branch_name, file_path):
""" """
...@@ -486,7 +509,7 @@ class ProjectRepo(BaseRepo): ...@@ -486,7 +509,7 @@ class ProjectRepo(BaseRepo):
raise RuntimeError("Code has changed since starting the context. Don't do that.") raise RuntimeError("Code has changed since starting the context. Don't do that.")
print("Completed computations, commiting results") print("Completed computations, commiting results")
self.output_repo._git.add(".") self.output_repo.add(".")
commit_return = self.output_repo._git.commit("-m", message) commit_return = self.output_repo._git.commit("-m", message)
print("\n" + commit_return + "\n") print("\n" + commit_return + "\n")
......
...@@ -88,9 +88,8 @@ def try_commit_code(path_to_repo): ...@@ -88,9 +88,8 @@ def try_commit_code(path_to_repo):
def try_add_submodule(path_to_repo): def try_add_submodule(path_to_repo):
repo = ProjectRepo(path_to_repo) repo = ProjectRepo(path_to_repo)
repo.load_external_data("https://jugit.fz-juelich.de/IBG-1/ModSim/cadet/git_lfs_data_1") submodule_path = repo.load_external_repository("https://jugit.fz-juelich.de/IBG-1/ModSim/cadet/git_lfs_data_1")
# ToDo: current WIP assert os.path.exists(submodule_path)
def try_commit_code_without_code_changes(path_to_repo): def try_commit_code_without_code_changes(path_to_repo):
repo = ProjectRepo(path_to_repo) repo = ProjectRepo(path_to_repo)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment