diff --git a/providers/git/src/airflow/providers/git/bundles/git.py b/providers/git/src/airflow/providers/git/bundles/git.py index 89c7f7af08444..2bf1f9980ae63 100644 --- a/providers/git/src/airflow/providers/git/bundles/git.py +++ b/providers/git/src/airflow/providers/git/bundles/git.py @@ -175,9 +175,12 @@ def _clone_bare_repo_if_required(self) -> None: env=self.hook.env if self.hook else None, ) self.bare_repo = Repo(self.bare_repo_path) + + # Fetch to ensure we have latest refs and validate repo integrity + self._fetch_bare_repo() except (InvalidGitRepositoryError, GitCommandError) as e: self._log.warning( - "Bare repository clone/open failed, cleaning up and retrying", + "Bare repository clone/open/fetch failed, cleaning up and retrying", bare_repo_path=self.bare_repo_path, exc=e, ) diff --git a/providers/git/tests/unit/git/bundles/test_git.py b/providers/git/tests/unit/git/bundles/test_git.py index cf85e71758b7f..b6c110e35477b 100644 --- a/providers/git/tests/unit/git/bundles/test_git.py +++ b/providers/git/tests/unit/git/bundles/test_git.py @@ -377,7 +377,8 @@ def test_refresh_with_git_connection(self, mock_gitRepo): tracking_ref=GIT_DEFAULT_BRANCH, ) bundle.initialize() - assert mock_gitRepo.return_value.remotes.origin.fetch.call_count == 2 # 1 in bare, 1 in main repo + # 1 in _clone_bare_repo_if_required, 1 in refresh() for bare repo, 1 in refresh() for working repo + assert mock_gitRepo.return_value.remotes.origin.fetch.call_count == 3 mock_gitRepo.return_value.remotes.origin.fetch.reset_mock() bundle.refresh() assert mock_gitRepo.return_value.remotes.origin.fetch.call_count == 2 @@ -730,7 +731,9 @@ def _fake_clone_from(*_, **kwargs): EXPECTED_ENV = {"GIT_SSH_COMMAND": "ssh -i /id_rsa -o StrictHostKeyChecking=no"} mock_gitRepo.clone_from.side_effect = _fake_clone_from - mock_gitRepo.return_value = types.SimpleNamespace() + # Mock needs to support the fetch operation called in _clone_bare_repo_if_required + mock_repo_instance = mock.MagicMock() + mock_gitRepo.return_value = mock_repo_instance with mock.patch("airflow.providers.git.bundles.git.GitHook") as mock_githook: mock_githook.return_value.repo_url = "git@github.com:apache/airflow.git"