pre-git icon indicating copy to clipboard operation
pre-git copied to clipboard

prepare-commit-msg hook?

Open inside opened this issue 8 years ago • 5 comments

Hi,

Is it possible to trigger the prepare-commit-msg hook using this configuration in package.json?

  "config": {
    "pre-git": {
      "prepare-commit-msg": [
        "path/to/prepare-commit-msg"
      ]
    }
  }

Best,

inside avatar Jun 03 '16 09:06 inside

Hi, not sure what this git hook does, can you explain?

bahmutov avatar Jun 03 '16 20:06 bahmutov

I use this hook to populate the commit message with some useful infos like the name of the branch you are currently on. Here's the link to the doc.

For what it's worth, you'll find below the script I use to insert text like this: [feature name] (branch name):

#!/bin/sh

# Put this file in .git/hooks/prepare-commit-msg
# (don't forget to check permissions on the file, it must be executable)
# This script prepares the commit message by inserting
# [PROJECT_NAME] (BRANCH) at the top of the message.
# PROJECT_NAME is your own environment variable,
# you can set it like this:
# $ export PROJECT_NAME=MY_PROJECT_NAME
# or don't set it, you'll just have empty brackets.
# Then you just have to write the commit message.

current_branch()
{
    ref=$(git rev-parse --abbrev-ref HEAD 2> /dev/null) \
        || return
    echo $ref
}

# If $2 is empty, prepare the commit message.
# See what are the values of $2 there:
# http://git-scm.com/docs/githooks#_prepare_commit_msg
# Useful for leaving the commit message untouched on a merge.
if [ -z "$2" ]
then
    BRANCH=$(current_branch)
    TIME=$(date +%s)
    TEMPORARY_COMMIT_FILE="/tmp/temporary_commit_file.$TIME"

    echo "[$PROJECT_NAME] ($BRANCH)" | cat - "$1" > $TEMPORARY_COMMIT_FILE
    mv $TEMPORARY_COMMIT_FILE "$1"
fi

inside avatar Jun 04 '16 20:06 inside

that's a cool feature, I should probably do this.

bahmutov avatar Jun 07 '16 01:06 bahmutov

I am thinking out loud... how about to execute commit-wizard on this step and then use the result, instead of using npm run commit this could be just git commit? Could it be possible?

fernandogmar avatar Jul 25 '17 13:07 fernandogmar

I am going to try it . I will hack a bit using this https://github.com/linxiaowu66/prepare-commit-msg-angular/blob/master/index.js

fernandogmar avatar Jul 27 '17 09:07 fernandogmar