commitizen / cz-cli

The commitizen command line utility. #BlackLivesMatter
http://commitizen.github.io/cz-cli/
MIT License
16.6k stars 552 forks source link

git commit -m to pre-fill commit title #847

Open glensc opened 2 years ago

glensc commented 2 years ago

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"
     }
   },
nevstokes commented 2 years ago

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)
glensc commented 2 years ago

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

nevstokes commented 2 years ago

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

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

glensc commented 2 years ago

@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 ;)

dmwelch commented 2 years ago

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.

nevstokes commented 2 years ago

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

oxodesign commented 2 years ago

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

garronej commented 2 years ago

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

alande-amorim commented 1 year ago

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