hkc-8010 commented on code in PR #72923:
URL: https://github.com/apache/airflow/pull/72923#discussion_r4749315989
##########
providers/git/src/airflow/providers/git/bundles/git.py:
##########
@@ -314,6 +304,50 @@ def _clone_repo_if_required(self) -> None:
def _uses_github_app_auth(self) -> bool:
return self.hook is not None and self.hook.uses_github_app_auth
+ @property
+ def _staging_repo_path(self) -> Path:
+ """Where a working clone is assembled before it is moved onto
``repo_path``."""
+ return self.base_dir / f".staging-{self.repo_path.name}"
+
+ def _clone_working_repo(self) -> None:
+ """
+ Assemble the working clone in a staging directory and move it onto
``repo_path``.
+
+ ``Repo.clone_from`` writes into its target as it goes, so a process
killed part way
+ through a clone leaves behind a directory that git can open but that
cannot check out
+ the tracking ref, and nothing removes it. Cloning into a sibling
directory and renaming
+ it means an interrupted clone never leaves a half-written
``repo_path``, so the next
+ initialization clones again rather than reusing a repository that can
never recover.
+ """
+ staging_path = self._staging_repo_path
+ # Left behind by an attempt that did not survive long enough to clean
up after itself.
+ if os.path.exists(staging_path):
+ self._log.info("Discarding staging directory from an earlier
clone", path=staging_path)
+ shutil.rmtree(staging_path)
+ try:
+ Repo.clone_from(
+ url=self.bare_repo_path,
+ to_path=staging_path,
+ multi_options=["--sparse", "--no-checkout"] if
self.sparse_dirs else None,
Review Comment:
Good catch, and it reproduces. Killing an in-place `git checkout` of a
second branch part way through gives exactly the #72922 error on retry, and a
SIGKILL leaves `index.lock` behind instead.
Changed so the working clone is always `--no-checkout` and `tracking_ref` is
checked out in the staging directory before the rename, for sparse and
non-sparse alike. The checkout in `_initialize` is now a no-op on a fresh
clone. Added a test that interrupts the checkout with a non-default branch as
`tracking_ref` and asserts nothing is left at `repo_path`; it fails on the
previous commit.
One side effect: a `tracking_ref` that doesn't exist now fails inside the
clone retry and comes out as `RuntimeError("Error cloning repository")`, the
same as the sparse path already did.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]