conventional-changelog-archived-repos / validate-commit-msg

DEPRECATED. Use https://github.com/marionebl/commitlint instead. githook to validate commit messages are up to standard
http://conventionalcommits.org/
MIT License
556 stars 101 forks source link

Feature request: validate rebases through post-rewrite #38

Closed joaocunha closed 6 years ago

joaocunha commented 7 years ago

I'm not entirely sure if it's possible... but a great enhancement would be to also validate on git rebase -i.

We squash our commits prior to opening a PR, so running git rebase -i master will bypass the commit-msg validation.

From the docs:

post-rewrite
This hook is invoked by commands that rewrite commits (git commit --amend, git-rebase; currently git-filter-branch does not call it!). Its first argument denotes the command it was invoked by: currently one of amend or rebase. Further command-dependent arguments may be passed in the future.

The hook receives a list of the rewritten commits on stdin, in the format

<old-sha1> SP <new-sha1> [ SP <extra-info> ] LF
The extra-info is again command-dependent. If it is empty, the preceding SP is also omitted. Currently, no commands pass any extra-info.

The hook always runs after the automatic note copying (see "notes.rewrite.<command>" in git-config[1]) has happened, and thus has access to these notes.

The following command-specific comments apply:

rebase
For the squash and fixup operation, all commits that were squashed are listed as being rewritten to the squashed commit. This means that there will be several lines sharing the same new-sha1.

The commits are guaranteed to be listed in the order that they were processed by rebase.
kentcdodds commented 7 years ago

I'm fairly certain that you could actually use this with the post-rewrite hook. This is where it gets the commit message file. What changes do you think would be needed to support post-rewrite?

joaocunha commented 7 years ago

Squashing commits through rebase will bypass the validation. On the example below, I'll commit twice and rebase to squash them into one single commit:

# some changes...

❯ git add . & git commit -m 'WIP 1'
Commit message validation ignored.
[reb-test 0393464] WIP 1
 1 file changed, 2 insertions(+), 1 deletion(-)

# more changes...

❯ git add . & git commit -m 'WIP 2'
Commit message validation ignored.
[reb-test 0393464] WIP 2
 1 file changed, 2 insertions(+), 1 deletion(-)

All good up to this point. We have two fine WIP commits, now I'll squash them into one and change the commit message to an invalid format:

❯ git rebase -i master

The editor opens up:

# pick 544b0d3 WIP 1
# squash 7a9db3e WIP 2
# save and close

The editor opens up once again for the new commit message:

# write "invalid commit message"
# save and close

Then I'm presented with:

INVALID COMMIT MSG: does not match "<type>(<scope>): <subject>" !
invalid commit message

[detached HEAD 7df25ab] invalid commit message
 Date: Tue Jul 26 20:55:32 2016 -0300
 1 file changed, 3 insertions(+), 2 deletions(-)

INVALID COMMIT MSG: does not match "<type>(<scope>): <subject>" !
invalid commit message

Successfully rebased and updated refs/heads/reb-test.

As you can see, the validation routine ran twice, but the rebase was actually successful.

Also, it doesn't validate rebases without a post-rewrite hook (expected behavior).

joaocunha commented 7 years ago

Adding to that, if I write a valid commit message, it runs only once and works as expected.

Using WIP whatever as a commit message after squashing to bypass the validation:

❯ git rebase -i master
Commit message validation ignored.
[detached HEAD 3f690b0] WIP whatever
 Date: Tue Jul 26 21:10:22 2016 -0300
 1 file changed, 3 insertions(+), 5 deletions(-)
Commit message validation ignored.
Successfully rebased and updated refs/heads/another-test.

Using feat(valid): this is valid as a commit message after squashing to conform with the validation format:

❯ git rebase -i master
[detached HEAD dd1fb04] feat(valid): this is valid
 Date: Tue Jul 26 21:10:22 2016 -0300
 1 file changed, 3 insertions(+), 5 deletions(-)
Successfully rebased and updated refs/heads/another-test.

Hope this helps, @kentcdodds!

kentcdodds commented 7 years ago

Not certain I understand exactly 100%, but if you know what needs to happen, feel free to makeapullrequest.com :)

mloayzagahona commented 7 years ago

@kentcdodds

I have a similar issue. I was trying to rebase like this:

$ git rebase -i HEAD~2
INVALID COMMIT MSG: does not match "<type>(<scope>): <subject>" !
hook demo
[detached HEAD b4785fe] hook demo
 Date: Fri Jul 29 10:34:17 2016 -0400
 6 files changed, 97 insertions(+), 15 deletions(-)
 create mode 100644 myfolder/easecoverage.js
INVALID COMMIT MSG: does not match "<type>(<scope>): <subject>" !
hook demo
Successfully rebased and updated refs/heads/hook.

The commit was created without the commit message format. It shown the error, but it is not canceling or rejecting the commit action for rebase (squashing commits)

Thanks

joaocunha commented 7 years ago

@kentcdodds I have edited my comment so you can better understand what's up: https://github.com/kentcdodds/validate-commit-msg/issues/38#issuecomment-235443884

kentcdodds commented 7 years ago

Yeah, I'm not sure how to solve this issue, but if you come up with something a pull request would be welcome :)

hutson commented 6 years ago

This package has been deprecated. Please use https://github.com/marionebl/commitlint instead.