From ecb63986881a66020fbe599b8775485e58607b7b Mon Sep 17 00:00:00 2001 From: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com> Date: Tue, 1 Oct 2024 11:03:52 +0200 Subject: [PATCH] refactor(git): Abstract `.cargo-ok` handling --- src/cargo/sources/git/utils.rs | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/cargo/sources/git/utils.rs b/src/cargo/sources/git/utils.rs index d28931f8642..39cd5616a5f 100644 --- a/src/cargo/sources/git/utils.rs +++ b/src/cargo/sources/git/utils.rs @@ -359,8 +359,7 @@ impl<'a> GitCheckout<'a> { /// /// [`.cargo-ok`]: CHECKOUT_READY_LOCK fn reset(&self, gctx: &GlobalContext) -> CargoResult<()> { - let ok_file = self.path.join(CHECKOUT_READY_LOCK); - let _ = paths::remove_file(&ok_file); + let guard = CheckoutGuard::guard(&self.path); info!("reset {} to {}", self.repo.path().display(), self.revision); // Ensure libgit2 won't mess with newlines when we vendor. @@ -370,7 +369,8 @@ impl<'a> GitCheckout<'a> { let object = self.repo.find_object(self.revision, None)?; reset(&self.repo, &object, gctx)?; - paths::create(ok_file)?; + + guard.mark_ok()?; Ok(()) } @@ -479,6 +479,25 @@ impl<'a> GitCheckout<'a> { } } +/// See [`GitCheckout::reset`] for rationale on this type. +#[must_use] +struct CheckoutGuard { + ok_file: PathBuf, +} + +impl CheckoutGuard { + fn guard(path: &Path) -> Self { + let ok_file = path.join(CHECKOUT_READY_LOCK); + let _ = paths::remove_file(&ok_file); + Self { ok_file } + } + + fn mark_ok(self) -> CargoResult<()> { + let _ = paths::create(self.ok_file)?; + Ok(()) + } +} + /// Constructs an absolute URL for a child submodule URL with its parent base URL. /// /// Git only assumes a submodule URL is a relative path if it starts with `./`