cz-cli icon indicating copy to clipboard operation
cz-cli copied to clipboard

git commit -m to pre-fill commit title

Open glensc opened this issue 4 years ago • 9 comments

With husky setup, the -m is lost.

I'm a CLI guru, so if I typed -m already, it should be used as a base, for example, add the category to it unless missing.

$ git commit -m 'Add prepare commit msg hook'
$ git commit -m 'feat: Add prepare commit msg hook'

currently have to re-type everything with several prompts. kind of slows down the commit process, driving away from the git hook setup.

Husky part:

--- a/package.json
+++ b/package.json
@@ -105,6 +105,7 @@
   },
   "husky": {
     "hooks": {
+      "prepare-commit-msg": "exec < /dev/tty && git cz --hook || true",
       "pre-commit": "lint-staged"
     }
   },

glensc avatar Aug 30 '21 19:08 glensc

I'm currently working around this in the prepare-commit-msg hook by using a regex along the lines of the following to check the format of any existing basic commit message, which I'll be verifying with commitlint anyway:

#!/bin/bash
. "$(dirname "$0")/_/husky.sh"

file="$1"

cat "$file" | grep -qE "^(fix|feat)(\(\w+\))?: .{3,72}$" || (exec < /dev/tty && node_modules/.bin/cz --hook || true)

Or, thinking about it, as I'm using commitlint anyway:

cat "$file" | npx commitlint -q || (exec < /dev/tty && node_modules/.bin/cz --hook || true)

nevstokes avatar Nov 21 '21 21:11 nevstokes

@nevstokes: quote "$file" to avoid word splitting

glensc avatar Nov 22 '21 07:11 glensc

@glensc Thanks, nice catch. I've updated my response.

Although maybe not strictly necessary in this case as $file will be .git/COMMIT_EDITMSG ?

nevstokes avatar Nov 22 '21 10:11 nevstokes

@nevstokes consistency is the key, you quoted "$1". Also, if it's an absolute path, you may run into trouble, like you can't change OneDrive root folder, and it's always like: /Users/glen/OneDrive - Organization Name. and another thing: you never know where your code could end up by someone copying it ;)

glensc avatar Nov 22 '21 10:11 glensc

Hey guys, I'm looking into this now. I definitely don't like that you're having to interrogate the COMMIT_EDITMSG outside of commitizen. The regex is especially fragile, notably around the allowed prefixes and optional components to the message (breaking, etc.) but it's a good start.

dmwelch avatar Nov 30 '21 16:11 dmwelch

Thanks @dmwelch, that's great to hear! My regex is very much a quick and dirty stop-gap solution for me.

nevstokes avatar Nov 30 '21 17:11 nevstokes

@dmwelch any news on this? Did you found a solution? Thanks in advance

oxodesign avatar Dec 21 '21 09:12 oxodesign

Hi,
Just to add to this thread: This shortcoming is this only thing preventing me from recommending cz.

garronej avatar Feb 22 '22 13:02 garronej

Would be great to have this feature. I agree with @garronej, it's a deal breaker for me and my team 😕

alande-amorim avatar Jan 26 '23 21:01 alande-amorim