Class WorktreeGuard
- All Implemented Interfaces:
AutoCloseable
Acquired via detach(ReleaseContext, String, Runnable) —
the factory runs git checkout <releaseTag>, leaving the
worktree pointed at the release tag. close() stashes any
foreign worktree changes (delegated to the caller-supplied
Runnable) and then runs git checkout main to
restore the worktree.
Use as a try-with-resources around the externally-visible deploy portion of the release pipeline:
try (WorktreeGuard guard = WorktreeGuard.detach(ctx, "v" + version,
() -> stashForeignWorktreeChanges(ctx, version))) {
// site generation, gh-pages publish, Nexus/Central deploys
}
// worktree is restored to main, regardless of how the block exited
If the detach itself fails, the factory throws and no guard instance is returned — the caller's try-with-resources block never enters and no cleanup runs, matching the prior behavior where a failed detach left no work to undo.
Carved out of ReleaseDraftMojo during the Phase 4 P3
prep commit (IKE-Network/ike-issues#489). The stash callback
preserves the existing stashForeignWorktreeChanges as-is
on the mojo so other code paths that invoke it are unchanged;
a future commit can migrate the stash logic into a sibling
helper once LocalPhase is extracted.
-
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Restores the worktree tomain.static WorktreeGuarddetach(ReleaseContext ctx, String releaseTag, Runnable foreignStash) Detaches the worktree to the given release tag.
-
Method Details
-
detach
Detaches the worktree to the given release tag.Runs
git checkout <releaseTag>fromctx.gitRoot(). If the checkout fails, this method throws and no guard instance is returned.- Parameters:
ctx- the release context carrying the git root and loggerreleaseTag- the tag to detach to (typically"v" + version)foreignStash- callback invoked fromclose()before thegit checkout mainstep; intended to stash foreign mid-flight worktree changes that would otherwise block the checkout- Returns:
- a new
WorktreeGuardready to be closed
-
close
public void close()Restores the worktree tomain.Runs the
foreignStashcallback to clear any mid-flight worktree changes, then runsgit checkout main. Called automatically when the try-with-resources block exits, regardless of whether the block exited normally or by exception.- Specified by:
closein interfaceAutoCloseable
-