Skip to content

Conversation

@kraftbj
Copy link
Contributor

@kraftbj kraftbj commented Jan 6, 2026

Proposed changes:

  • Add jp init-hooks command to install Docker-aware git hooks
  • Add jp git-hook <hook-name> command to run git hooks inside Docker container
  • Automatically call jp init-hooks during jp init for new installations
  • Detect and unset core.hooksPath if configured (e.g., from husky) to ensure jp hooks are used
  • Add TTY detection to monorepo script to avoid "input device is not a TTY" errors when running in non-interactive contexts
  • Pass CI=true environment variable when running git hooks to prevent pnpm from requiring TTY
  • Add JETPACK_MONOREPO_ENV=1 to Docker container to allow hooks to detect when running inside Docker
  • Read hook commands from .husky/ files to stay in sync with husky configuration

Other information:

  • Have you written new tests for your changes, if applicable? (N/A - shell/JS infrastructure change)
  • Have you checked the E2E test CI results, and verified that your changes do not break them?
  • Have you tested your changes on WordPress.com, if applicable? (N/A)

Jetpack product discussion

N/A - Internal developer tooling improvement

Does this pull request change what data or activity we track or use?

No

⚠️ Additional Testing Required Before Merge

The following edge cases need manual testing:

  • Auto-fix flow: When pre-commit hook catches a fixable linting issue, auto-fixes the file, and commits the fix—verify the original commit proceeds correctly
  • Pre-push with required fixes: When pre-push hook fails and requires fixes before pushing—verify the workflow handles this gracefully
  • Container git operations: Verify that running git commands inside the container (where husky's hooks would fire) still works correctly

Testing instructions:

Prerequisites:

  1. Checkout this branch: git checkout update/jetpack-cli-git-hooks
  2. Link the updated jp CLI globally: cd projects/js-packages/jetpack-cli && npm install && npm link
  3. Return to monorepo root: cd ../../..

Test 1: Install jp hooks

  1. Run: jp init-hooks
  2. Verify you see "Setting up jp git hooks..."
  3. If you had husky configured, verify you see "Detected custom git hooks path: .husky/_" and "Resetting to use .git/hooks/"
  4. Verify you see "Created pre-commit hook", "Created pre-push hook", "Created post-checkout hook", "Created post-merge hook"
  5. Verify: ls -la .git/hooks/ shows executable hook files with recent timestamps

Test 2: Test hooks run in Docker

  1. Make a trivial change: echo "# test" >> README.md
  2. Stage it: git add README.md
  3. Commit: git commit -m "Test hooks"
  4. Verify you see:
    • "✓ Using jp hooks (delegating to Docker)"
    • "Running pre-commit hook in Docker..."
    • Hook runs successfully (linter may auto-format files)
  5. If hook modified files, add them and commit again
  6. Clean up: git reset HEAD~1 && git restore README.md

Test 3: Verify normal jp commands remain interactive

  1. Run: jp install
  2. Verify the command works normally (doesn't show CI warnings or TTY errors)
  3. Verify you can see interactive output from pnpm

Test 4: Verify hooks work without host pnpm install

  1. This should already be the case if you're using jp CLI as intended
  2. Make a change and commit
  3. Verify hooks still run successfully in Docker despite no host pnpm install

Copilot AI review requested due to automatic review settings January 6, 2026 18:14
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot wasn't able to review any files in this pull request.

@kraftbj kraftbj self-assigned this Jan 6, 2026
@kraftbj kraftbj added [Status] In Progress [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it labels Jan 6, 2026
@github-actions
Copy link
Contributor

github-actions bot commented Jan 6, 2026

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ Add a "[Status]" label (In Progress, Needs Review, ...).
  • ✅ Add a "[Type]" label (Bug, Enhancement, Janitorial, Task).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


Follow this PR Review Process:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Make sure to test your changes on all platforms that it applies to. You're responsible for the quality of the code you ship.
  3. You can use GitHub's Reviewers functionality to request a review.
  4. When it's reviewed and merged, you will be pinged in Slack to deploy the changes to WordPress.com simple once the build is done.

If you have questions about anything, reach out in #jetpack-developers for guidance!

Copilot AI review requested due to automatic review settings January 6, 2026 18:32
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 4 changed files in this pull request and generated no new comments.

- Reformat chalk.yellow call to single line to match code style
- Update changelog for 1.1.0-beta.0 release
Follow bash best practices by using [[ ]] builtin syntax for the
TTY check conditional, consistent with bash scripting conventions.
Copilot AI review requested due to automatic review settings January 6, 2026 19:18

This comment was marked as resolved.

The post-checkout hook was failing when checking out with no previous
ref (e.g., fresh clone or initial checkout) because it tried to run
git diff-tree with an empty argument.

Added a check to skip the hook if the previous ref is empty or is the
null SHA (all zeros), which git uses to indicate no previous ref exists.
@kraftbj kraftbj requested a review from anomiex January 7, 2026 18:04
@kraftbj
Copy link
Contributor Author

kraftbj commented Jan 7, 2026

What do you think of this approach? My intent is people who are already setup can continue to use husky fine (and pnpm install locally will set them up, etc), but new folks using jp init or existing who switch over via jp init-hooks can use regular git commands that respect our hooks. For people using the jp dev environment, those hooks would run within docker so local versions of anything doesn't matter.

I tested it by deleting pnpm off my local system and able to switch back. I feel like this might be the way to do it without any of the previous ideas I had (wrapping git for the jp or something like that).

Copy link
Contributor

@anomiex anomiex left a comment

Choose a reason for hiding this comment

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

Looks reasonable, except for one thing flagged inline.

What stops any later pnpm install from re-initializing husky (and re-setting core.hookspath) though?

@kraftbj
Copy link
Contributor Author

kraftbj commented Jan 7, 2026

What stops any later pnpm install from re-initializing husky (and re-setting core.hookspath) though?

Nothing at this point. Intentional to not force a change to using jp that couldn't be undone. pnpm install would require the node_modules dir to be recreated so it would just work as it did before. If you use a jp command, it'd realize that it doesn't work and ask you to re-create as well. It's a flow that I want to test more before advertising this.

@anomiex
Copy link
Contributor

anomiex commented Jan 7, 2026

I meant even a pnpm install inside the container, like from jp install.

- Revert package.json version from 1.1.0-beta.0 to 1.0.2
- Remove premature changelog entry from CHANGELOG.md
- Add runtime detection to display alpha version when running from source
- Add proper changelog entry files for changelogger
Address review feedback to avoid repeating hook command data.
Now both initHooks and runGitHook use getHookCommand() to read
the actual commands from .husky/ files, staying in sync with husky.
@kraftbj
Copy link
Contributor Author

kraftbj commented Jan 7, 2026

Good point. jp install does run pnpm install in the container, which triggers husky and sets core.hooksPath=.husky/ in the container's git config. But this doesn't affect the host:

  • The jp hooks live in the host's .git/hooks/ directory
  • Git operations on the host use the host's git config (where we unset core.hooksPath)
  • Host hooks delegate to jp git-hook <name> which runs the actual command inside the container

So the container's git config can point to .husky/—we don't run git inside the container normally. If someone did run git inside the container, husky's hooks would fire there, which is fine since they run the same underlying scripts (thanks for the suggestion to read from .husky/ directly—that ensures they actually stay in sync).

Still need to test some edge cases though—particularly when pre-commit catches a fixable linting issue, auto-fixes and commits it, then signals to continue with the original operation. Adding those test cases now.

@kraftbj
Copy link
Contributor Author

kraftbj commented Jan 7, 2026

But this doesn't affect the host

Seems to in my testing. There aren't separate "host" and "container" .git directories, the whole checkout is mounted into the container .git and all.

I've avoided installing jp globally, but this seems equivalent:

Hmm, I think my test of all this by just deleting pnpm is insufficient... I'll keep working on it. Thanks.

@kraftbj
Copy link
Contributor Author

kraftbj commented Jan 7, 2026

Looking at the docs ( https://typicode.github.io/husky/how-to.html#ci-server-and-docker ), there are a few options I'm going to explore.

Instead of extracting commands from .husky files, run them directly
via `sh .husky/<hookname>`. This is more maintainable and handles
all the shell logic (like TTY handling in pre-push) properly.
Set HUSKY=0 to prevent husky from modifying the shared .git/config
when pnpm install runs in the container. Since .git is bind-mounted,
husky's prepare script would otherwise set core.hooksPath and bypass
the jp hooks installed in .git/hooks/.
Copilot AI review requested due to automatic review settings January 7, 2026 23:18
@kraftbj
Copy link
Contributor Author

kraftbj commented Jan 7, 2026

Good points on both counts:

  1. Running .husky files directly — Done! Simplified to just run sh .husky/<hookname> instead of extracting commands. This is cleaner and properly handles all the shell logic (like TTY handling in pre-push).

  2. Shared .git config issue — You're right that the .git directory is bind-mounted, so husky's prepare script during pnpm install was modifying core.hooksPath on the host. I tested before the fix and confirmed the behavior you described — after jp init-hooks cleared core.hooksPath, running jp install -r caused husky to set it back to .husky/_. Fixed by adding HUSKY=0 to the monorepo script's Docker environment. This is likely only needed for pnpm install commands, but I can't think of a reason why disabling husky globally within Docker would be problematic. Tested and confirmed:

    • jp init-hookscore.hooksPath unset ✓
    • jp install -rcore.hooksPath still unset ✓

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 6 changed files in this pull request and generated no new comments.

- Remove shell: true from spawnSync to preserve argument quoting
  (fixes changelog entries with spaces being split)
- Only set CI=true when no TTY available, allowing interactive
  prompts like changelog creation during git push
Copilot AI review requested due to automatic review settings January 8, 2026 17:10
@kraftbj kraftbj removed the DO NOT MERGE don't merge it! label Jan 8, 2026
@kraftbj kraftbj added [Status] Needs Review This PR is ready for review. and removed [Status] In Progress labels Jan 8, 2026
@kraftbj
Copy link
Contributor Author

kraftbj commented Jan 8, 2026

I tested the various git hooks that auto-commit, prompt the interactive changelog, etc and made a few changes. I believe this is ready for review and merge.

@kraftbj kraftbj requested a review from anomiex January 8, 2026 17:12
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 6 changed files in this pull request and generated 1 comment.

- Read git hooks dynamically from .husky/ directory instead of hardcoding
- Add existence check in generated hooks for graceful degradation
- Minor style improvement for TTY check condition
@kraftbj kraftbj requested a review from anomiex January 9, 2026 16:32
TTY detection is already handled by the monorepo script,
which reconnects to /dev/tty when available.
Copilot AI review requested due to automatic review settings January 9, 2026 20:36
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 6 changed files in this pull request and generated no new comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Docker [JS Package] Jetpack CLI RNA [Status] Needs Review This PR is ready for review. [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants