Skip to main content

commit_msg_script

Function commit_msg_script 

Source
pub fn commit_msg_script() -> String
Expand description

Return the body of the generated commit-msg hook. Exposed as a pure function so tests can assert on the rendered script without touching the filesystem, and so future hook variants (commit-msg, pre-push, …) can share helpers from this module.

The script:

  1. Reads the in-progress commit message (path passed by git as $1).
  2. Locates the first non-empty non-comment line — git’s own commit template puts # Please enter the commit message… above the user’s first real line, and git commit -v appends a diff dump prefixed with #. Both are stripped before the “is the message already prefixed?” check.
  3. Bails out (exit 0) if that line already starts with an emoji-ish prefix (a case match against the :shortcode: form + a grep -E for unicode emoji codepoints).
  4. Shells out to gwm commit-prefix --unicode. If gwm is missing from $PATH, or the shell-out fails for any reason, the hook exits 0 cleanly — the goal is “never block a commit because the hook itself broke”.
  5. Prepends the resolved prefix + a space to the message and writes the result back to the same file. Every filesystem step here is guarded by || exit 0 so a transient failure (full /tmp, noexec mount, read-only fs, …) lets the original commit through unmodified rather than aborting it.