Class WorktreeGuard

java.lang.Object
network.ike.plugin.release.WorktreeGuard
All Implemented Interfaces:
AutoCloseable

public final class WorktreeGuard extends Object implements AutoCloseable
AutoCloseable wrapper around the release pipeline's worktree detach/restore boundary.

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 Details

    • detach

      public static WorktreeGuard detach(ReleaseContext ctx, String releaseTag, Runnable foreignStash)
      Detaches the worktree to the given release tag.

      Runs git checkout <releaseTag> from ctx.gitRoot(). If the checkout fails, this method throws and no guard instance is returned.

      Parameters:
      ctx - the release context carrying the git root and logger
      releaseTag - the tag to detach to (typically "v" + version)
      foreignStash - callback invoked from close() before the git checkout main step; intended to stash foreign mid-flight worktree changes that would otherwise block the checkout
      Returns:
      a new WorktreeGuard ready to be closed
    • close

      public void close()
      Restores the worktree to main.

      Runs the foreignStash callback to clear any mid-flight worktree changes, then runs git checkout main. Called automatically when the try-with-resources block exits, regardless of whether the block exited normally or by exception.

      Specified by:
      close in interface AutoCloseable