shibapm / Komondor

Git Hooks for Swift projects 🐩
MIT License
552 stars 32 forks source link

Commit msg not passed to commit-msg hook #21

Closed prasadp-qp closed 5 years ago

prasadp-qp commented 5 years ago

I have installed a commit-msg hook similar to the one here: https://gist.github.com/pgilad/5d7e4db725a906bd7aa7

But the hook fails as the .git/COMMIT_EDITMSG is not available to the script while execution.

orta commented 5 years ago

I think you might have to research this yourself, I'm not really sure about how that works

prasadp-qp commented 5 years ago

Git calls the commit-msg hook script with one argument, the name of the file that has the commit message. Komondor while executing commit-msg hook does not pass this argument.

The render script method is not passing the params gitParams to the hook script.

The Package.swift:

...
#if canImport(PackageConfig)
    import PackageConfig

    let config = PackageConfig([
        "komondor": [
            "commit-msg": "sh commitmsgcheck.sh",
        ],
    ]).write()
#endif

commitmsgcheck.sh:

#!/usr/bin/env bash
# regex to validate in commit msg
commit_regex='(wap-[0-9]+|merge)'
error_msg="Aborting commit. Your commit message is missing either a JIRA Issue ('WAP-1111') or 'Merge'"
if ! grep -iqE "$commit_regex" "$1"; then
echo "$error_msg" >&2
exit 1
fi
orta commented 5 years ago

Cool - yep makes sense, you're welcome to add it, looks an an omission 👍

prasadp-qp commented 5 years ago

@orta here is the PR:https://github.com/shibapm/Komondor/pull/23

orta commented 5 years ago

Fixed in #23

agarrharr commented 2 years ago

This should be added to the documentation. I finally realized I could access the commit message with $GIT_PARAMS.

For example, I'm getting the Jira ticket number from the branch name and appending it to the third line of the commit message like this.

In scripts/add-jira-ticket.sh

#!/bin/sh

 if [ x = x${2} ]; then
   BRANCH_NAME=$(git symbolic-ref --short HEAD)
   TICKET_NUMBER=$(echo $BRANCH_NAME | sed -n 's/.*\([A-Z]*-[0-9]*\)_.*/\1/p')
   if [ x != x${TICKET_NUMBER} ]; then
     sed -i.bak "1s/$/\n\n$TICKET_NUMBER/" ../$GIT_PARAMS
   fi
 fi

And in the Package.swift:

#if canImport(PackageConfig)
     import PackageConfig

     let config = PackageConfiguration([
         "komondor": [
             "commit-msg": [
                 "sh ../scripts/add-jira-ticket.sh"
             ],
         ],
     ]).write()
 #endif