Skip to content

[Feat] Implement worktree hook execution with real-time output#51

Merged
samzong merged 1 commit into
mainfrom
feat/worktree-hook-implementation
Mar 25, 2026
Merged

[Feat] Implement worktree hook execution with real-time output#51
samzong merged 1 commit into
mainfrom
feat/worktree-hook-implementation

Conversation

@samzong
Copy link
Copy Markdown
Owner

@samzong samzong commented Mar 25, 2026

What's changed?

  • Add Hook struct, SharedConfig.Hooks field, and AddHook/RemoveHook methods in internal/worktree/resource.go
  • Implement runHooks engine that streams stdout/stderr to terminal in real time (replaces CombinedOutput which buffered all output until completion)
  • Hooks execute after shared resource sync on wt add; skipped during wt share sync bulk operations
  • Add tests for hook remove validation and sync-without-hooks behavior

Why

  • Hook commands like pnpm install can take a long time; users had zero visibility into progress, making the tool appear frozen
  • Streaming output gives immediate feedback during hook execution

Add Hook type, AddHook/RemoveHook methods, and runHooks engine
that streams stdout/stderr in real time so users see progress
during long-running hooks like `pnpm install`.

Hooks run after shared resource sync on `wt add` but are skipped
during `wt share sync` bulk operations.
@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the user experience for worktree operations by introducing support for configurable hooks with real-time output. Previously, long-running hook commands could make the tool appear unresponsive, but now users receive immediate feedback as commands execute. This change improves transparency and usability, particularly for tasks like dependency installation, by providing visibility into the progress of background operations.

Highlights

  • Hook Management: Introduced a new Hook struct and added AddHook and RemoveHook methods to manage worktree hooks within the shared configuration.
  • Real-time Hook Execution: Implemented a runHooks engine that executes hook commands and streams their stdout/stderr directly to the terminal in real-time, replacing previous buffered output.
  • Conditional Hook Execution: Configured hooks to run automatically after shared resource synchronization during wt add operations, but to be explicitly skipped during bulk wt share sync operations.
  • Validation and Testing: Added tests for validating hook removal index input and confirming that hooks are not executed during bulk synchronization operations.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new 'hooks' feature for worktrees, allowing users to define and execute commands as part of the shared resource synchronization process. It adds a Hook struct, integrates hooks into the SharedConfig, and provides AddHook, RemoveHook, and runHooks methods. The syncSharedResourcesToPath function is updated to conditionally run these hooks, and SyncAllSharedResources is modified to explicitly not run hooks. New tests cover invalid hook index rejection and confirm that SyncAllSharedResources does not execute hooks. Feedback suggests improving the readability of a complex conditional statement and refactoring duplicated hook label logic into a dedicated method for better maintainability.

Comment on lines +62 to 64
if len(cfg.Resources) == 0 && (!runHooks || len(cfg.Hooks) == 0) {
return report, nil
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This conditional logic is a bit complex and can be hard to parse at a glance. To improve readability and make the intent clearer, consider refactoring it to use intermediate variables that describe the conditions.

Suggested change
if len(cfg.Resources) == 0 && (!runHooks || len(cfg.Hooks) == 0) {
return report, nil
}
shouldSyncResources := len(cfg.Resources) > 0
shouldRunHooks := runHooks && len(cfg.Hooks) > 0
if !shouldSyncResources && !shouldRunHooks {
return report, nil
}

Comment on lines +303 to +306
desc := hook.Desc
if desc == "" {
desc = hook.Cmd
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This logic to determine the hook's descriptive label (falling back from Desc to Cmd) is duplicated in RemoveHook (lines 330-333) and runHooks (lines 600-603). To improve maintainability and reduce code duplication, consider adding a method to the Hook struct to encapsulate this logic.

For example, you could add this method to the Hook struct:

func (h *Hook) Label() string {
	if h.Desc != "" {
		return h.Desc
	}
	return h.Cmd
}

Then you can simplify this part to desc := hook.Label() and do the same in RemoveHook and runHooks.

@samzong samzong merged commit 2df8146 into main Mar 25, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant