Skip to content

Commit

Permalink
refactor(git): Abstract .cargo-ok handling
Browse files Browse the repository at this point in the history
  • Loading branch information
osiewicz authored and epage committed Nov 11, 2024
1 parent 63c01d8 commit ecb6398
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/cargo/sources/git/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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(())
}

Expand Down Expand Up @@ -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 `./`
Expand Down

0 comments on commit ecb6398

Please sign in to comment.