Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

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

Open
glensc opened this issue Aug 30, 2021 · 9 comments
Open

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

glensc opened this issue Aug 30, 2021 · 9 comments
Assignees

Comments

@glensc
Copy link

glensc commented Aug 30, 2021

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
Copy link

nevstokes commented Nov 21, 2021

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

This comment has been minimized.

@nevstokes
Copy link

@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
Copy link
Author

glensc commented Nov 22, 2021

@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 dmwelch self-assigned this Nov 30, 2021
@dmwelch
Copy link
Contributor

dmwelch commented Nov 30, 2021

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
Copy link

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

@oxodesign
Copy link

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

@garronej
Copy link

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

@alande-amorim
Copy link

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

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants