From 04a44de34eb1e02cb22434438269f10da13f1490 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Schm=C3=B6lder?= Date: Thu, 13 Aug 2026 11:15:51 +0200 Subject: [PATCH] Archive remote output branches without local heads --- cadetrdm/repositories.py | 8 +++----- tests/test_read_only_loading.py | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/cadetrdm/repositories.py b/cadetrdm/repositories.py index fbdc07d..3c33da5 100644 --- a/cadetrdm/repositories.py +++ b/cadetrdm/repositories.py @@ -1403,12 +1403,10 @@ def copy_data_to_cache(self, branch_name=None, target_folder=None): target_folder = self.cache_folder_for_branch(branch_name) target_folder = Path(target_folder) - # Ensure that the branch is available locally. If it's only a remote branch, git.archive will fail. + archive_ref = branch_name local_branches = [head.name for head in self.output_repo._git_repo.heads] if branch_name not in local_branches: - self.output_repo._git_repo.create_head( - branch_name, f"origin/{branch_name}" - ) + archive_ref = f"origin/{branch_name}" # Create the target directory if it doesn't exist if not target_folder.exists(): @@ -1419,7 +1417,7 @@ def copy_data_to_cache(self, branch_name=None, target_folder=None): os.close(handle) # Create an archive of the specified branch self.output_repo._git_repo.git.archive( - branch_name, output=temp_archive_name + archive_ref, output=temp_archive_name ) # Open the temporary file in read mode diff --git a/tests/test_read_only_loading.py b/tests/test_read_only_loading.py index eb291f1..5cb3739 100644 --- a/tests/test_read_only_loading.py +++ b/tests/test_read_only_loading.py @@ -10,6 +10,7 @@ import pytest from cadetrdm import ProjectRepo, initialize_repo +from cadetrdm.io_utils import delete_path def git_state(repo): @@ -97,3 +98,27 @@ def test_output_log_is_empty_when_no_results_are_recorded(tmp_path): repo = ProjectRepo(path_to_repo) assert repo.output_repo.output_log.n_entries == 0 + + +def test_copy_data_to_cache_uses_remote_ref_without_creating_local_branch(repo_with_results): + output_repo = repo_with_results.output_repo + result_branch = str(output_repo.active_branch) + result_commit = output_repo.current_commit_hash + cache_folder = repo_with_results.cache_folder_for_branch(result_branch) + + delete_path(cache_folder) + output_repo.checkout(output_repo.main_branch) + output_repo._git_repo.git.update_ref( + f"refs/remotes/origin/{result_branch}", + result_commit, + ) + output_repo._git_repo.delete_head(result_branch, force=True) + + state_before = git_state(output_repo) + assert result_branch not in state_before["branches"] + + cache_path = repo_with_results.copy_data_to_cache(result_branch) + + assert (cache_path / "result.csv").read_text() == "1,2,3\n" + assert git_state(output_repo) == state_before + assert result_branch not in [head.name for head in output_repo._git_repo.heads]