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

Fix commit bug and simplify code

parent 1291de1f
No related branches found
No related tags found
No related merge requests found
...@@ -138,8 +138,6 @@ def initialize_git_repo(path_to_repo: str, output_repo_name: (str | bool) = "out ...@@ -138,8 +138,6 @@ def initialize_git_repo(path_to_repo: str, output_repo_name: (str | bool) = "out
# If output_repo_name is False we are in the output_repo and should finish by committing the changes # If output_repo_name is False we are in the output_repo and should finish by committing the changes
repo = ResultsRepo(".") repo = ResultsRepo(".")
# ToDo: Why does this break tests if changed to repo.add and repo.commit?? repo.commit("initial commit")
repo._git.add(".")
repo._git.commit("-m", "initial commit")
os.chdir(starting_directory) os.chdir(starting_directory)
...@@ -117,28 +117,7 @@ class BaseRepo: ...@@ -117,28 +117,7 @@ class BaseRepo:
:return: :return:
List of all staged changes. List of all staged changes.
""" """
untracked_files = "" self._git.add(".")
if len(self.untracked_files) > 0:
untracked_files = "\n".join(["- " + file for file in self.untracked_files])
if automatically_add_new_files:
for f in self.untracked_files:
self._git.add(f)
else:
proceed = input(
f'Found untracked files. Adding the following untracked files to git: \n{untracked_files}\n'
f'Proceed? Y/n \n'
)
if proceed.lower() == "y" or proceed == "":
for f in self.untracked_files:
self._git.add(f)
else:
raise KeyboardInterrupt
changed_files = self.changed_files
for f in changed_files:
self._git.add(f)
return self.untracked_files + changed_files
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'
...@@ -160,8 +139,8 @@ class BaseRepo: ...@@ -160,8 +139,8 @@ class BaseRepo:
return changed_files return changed_files
@property @property
def exist_unstaged_changes(self): def exist_uncomitted_changes(self):
return len(self.untracked_files) > 0 or len(self.changed_files) > 0 return len(self._git.status("--porcelain")) > 0
def update_package_list(self): def update_package_list(self):
""" """
...@@ -177,7 +156,7 @@ class BaseRepo: ...@@ -177,7 +156,7 @@ class BaseRepo:
print("Dumping pip independent requirements.txt.") print("Dumping pip independent requirements.txt.")
os.system(f"pip list --not-required --format freeze > {repo_path}/pip_independent_requirements.txt") os.system(f"pip list --not-required --format freeze > {repo_path}/pip_independent_requirements.txt")
def commit(self, message: str, add_all=True, update_packages=True): def commit(self, message: str, add_all=True, update_packages=False):
""" """
Commit current state of the repository. Commit current state of the repository.
...@@ -188,7 +167,7 @@ class BaseRepo: ...@@ -188,7 +167,7 @@ class BaseRepo:
:param update_packages: :param update_packages:
Option to automatically dump the python environment information into environment.yml files. Option to automatically dump the python environment information into environment.yml files.
""" """
if not self.exist_unstaged_changes: if not self.exist_uncomitted_changes:
print(f"No changes to commit in repo {self.working_dir}") print(f"No changes to commit in repo {self.working_dir}")
return return
...@@ -196,7 +175,7 @@ class BaseRepo: ...@@ -196,7 +175,7 @@ class BaseRepo:
if update_packages: if update_packages:
self.update_package_list() self.update_package_list()
if add_all: if add_all:
self.add_all_files() self._git.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")
...@@ -234,7 +213,7 @@ class BaseRepo: ...@@ -234,7 +213,7 @@ class BaseRepo:
Adds all untracked files to git and then stashes all changes. Adds all untracked files to git and then stashes all changes.
Will raise a RuntimeError if no changes are found. Will raise a RuntimeError if no changes are found.
""" """
if not self.exist_unstaged_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._git.add(".")
self._git.stash() self._git.stash()
...@@ -272,7 +251,7 @@ class BaseRepo: ...@@ -272,7 +251,7 @@ class BaseRepo:
Raise a RuntimeError if uncommitted changes are in the repository. Raise a RuntimeError if uncommitted changes are in the repository.
:return: :return:
""" """
if self.exist_unstaged_changes: if self.exist_uncomitted_changes:
raise RuntimeError(f"Found uncommitted changes in the repository {self.working_dir}.") raise RuntimeError(f"Found uncommitted changes in the repository {self.working_dir}.")
...@@ -413,7 +392,7 @@ class ProjectRepo(BaseRepo): ...@@ -413,7 +392,7 @@ class ProjectRepo(BaseRepo):
:return: :return:
Absolute path to the newly copied file. Absolute path to the newly copied file.
""" """
if self.output_repo.exist_unstaged_changes: if self.output_repo.exist_uncomitted_changes:
self.output_repo.stash_all_changes() self.output_repo.stash_all_changes()
has_stashed_changes = True has_stashed_changes = True
else: else:
...@@ -483,7 +462,7 @@ class ProjectRepo(BaseRepo): ...@@ -483,7 +462,7 @@ class ProjectRepo(BaseRepo):
self._is_in_context_manager = True self._is_in_context_manager = True
output_repo = self.output_repo output_repo = self.output_repo
if output_repo.exist_unstaged_changes: if output_repo.exist_uncomitted_changes:
output_repo.reset_hard_to_head() output_repo.reset_hard_to_head()
output_repo.delete_active_branch_if_branch_is_empty() output_repo.delete_active_branch_if_branch_is_empty()
......
...@@ -80,7 +80,7 @@ def try_commit_code(path_to_repo): ...@@ -80,7 +80,7 @@ def try_commit_code(path_to_repo):
current_commit_number = count_commit_number(repo) current_commit_number = count_commit_number(repo)
modify_code(path_to_repo) modify_code(path_to_repo)
repo.commit("add code to print random number", update_packages=False) repo.commit("add code to print random number", add_all=True, update_packages=False)
updated_commit_number = count_commit_number(repo) updated_commit_number = count_commit_number(repo)
assert current_commit_number + 1 == updated_commit_number assert current_commit_number + 1 == updated_commit_number
...@@ -95,7 +95,7 @@ def try_add_submodule(path_to_repo): ...@@ -95,7 +95,7 @@ def try_add_submodule(path_to_repo):
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)
current_commit_number = count_commit_number(repo) current_commit_number = count_commit_number(repo)
repo.commit("This commit will not be made") repo.commit("This commit will not be made", add_all=True)
updated_commit_number = count_commit_number(repo) updated_commit_number = count_commit_number(repo)
assert current_commit_number == updated_commit_number assert current_commit_number == updated_commit_number
...@@ -116,7 +116,7 @@ def try_commit_results_with_uncommitted_code_changes(path_to_repo): ...@@ -116,7 +116,7 @@ def try_commit_results_with_uncommitted_code_changes(path_to_repo):
with pytest.raises(Exception): with pytest.raises(Exception):
with repo.track_results(results_commit_message="Add array"): with repo.track_results(results_commit_message="Add array"):
example_generate_results_array(path_to_repo, output_folder=repo._output_folder) example_generate_results_array(path_to_repo, output_folder=repo._output_folder)
repo.commit("add code to print random number", update_packages=False) repo.commit("add code to print random number", add_all=True, update_packages=False)
def try_load_previous_result(path_to_repo, branch_name): def try_load_previous_result(path_to_repo, branch_name):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment