All docs

Write and test Git hooks

Enable Git hooks in MoonGit, inspect the scripts a repository runs and test a commit-message rule with sample input.

Use MoonGit 1.0.13 and Git 2.36 or later for test runs. Try the example in a disposable repository.

Open the workbench

  1. Open Settings → Advanced features and turn on Git hooks.
The Git hooks setting and its switch
Turn on Git hooks. Open full-size image
  1. Open the repository and select Git Hooks in the branch bar or More tools.

If the list is hidden, select Browse hooks. Hooks from a core.hooksPath folder are read-only in MoonGit.

Create a commit-message check

  1. Under Commit, select commit-msg.
  2. Select Add hook.
The commit-msg preview with the Add hook button
Select Add hook to open a draft. Open full-size image
  1. Replace the starter text with the script below.
#!/bin/sh

if head -n 1 "$1" | grep -Eq '^APP-[0-9]+: '; then
    printf '%s\n' 'Ticket prefix looks good.'
    exit 0
fi

printf '%s\n' 'Start the subject with a ticket, for example APP-42: Fix login.' >&2
exit 1
  1. Select Create hook.
The new commit-msg draft with Create hook in the toolbar
Select Create hook to save the script. Open full-size image

Run a failing and a passing test

Review the script before running it. Run hook executes the saved script in the repository.

  1. Select Test hook….
The saved commit-msg hook with the Test hook action
Select Test hook to open the run pane. Open full-size image
  1. Enter Fix login in Sample message. Select Run hook.
The hook run pane with Run hook, the sample message, and exit code 1
Run the hook. Exit code 1 means the check failed. Open full-size image
  1. Replace the sample with APP-42: Fix login and run it again.
The hook test passes with exit code 0
Look for exit code 0 and Ticket prefix looks good. Open full-size image

Open a hook from a stopped commit

If Git stops a commit, read the hook name and output in the composer. Select Open Git Hooks to inspect that run.

The commit composer’s rejection with Open Git Hooks
Select Open Git Hooks to open the failed run. Open full-size image

Correct the actual commit message in the composer and retry. Git’s hook reference

More docs