commitizen icon indicating copy to clipboard operation
commitizen copied to clipboard

feat(commands/commit): apply prepare-commit-msg hook

Open saygox opened this issue 5 years ago • 17 comments

Types of changes

  • [ ] Bug fix
  • [x] New feature
  • [ ] Refactoring
  • [ ] Breaking change (any change that would cause existing functionality to not work as expected)
  • [x] Documentation update
  • [ ] Other (please describe)

Description

invoke cz commit from git prepare-commit-msg hook

Checklist

  • [x] Add test cases to all the changes you introduce
  • [x] Run ./script/format and ./script/test locally to ensure this change passes linter check and test
  • [x] Test the changes on the local machine manually
  • [x] Update the documentation for the changes

Steps to Test This Pull Request

  1. Create a fake repo
  2. Add prepare-commit-msg hook
  3. Start comitting

Expected behavior

  • hook invokes cz commit
  • Editor is invoked after questionary
  • commit message is filled by cz commit

Additional context

  • #249
  • test only in linux

saygox avatar Aug 14 '20 08:08 saygox

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Comparison is base (65645e0) 97.92% compared to head (1053d01) 97.97%. Report is 617 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #250      +/-   ##
==========================================
+ Coverage   97.92%   97.97%   +0.05%     
==========================================
  Files          39       43       +4     
  Lines        1395     1432      +37     
==========================================
+ Hits         1366     1403      +37     
  Misses         29       29              
Flag Coverage Δ
unittests 97.97% <100.00%> (+0.05%) :arrow_up:

Flags with carried forward coverage won't be shown. Click here to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar Aug 14 '20 09:08 codecov[bot]

Hi, although I've not yet had the time to fully review the code, I just test it on my local machine and failed.

This is the .pre-commit-config.yaml I used.

default_stages: [push]
repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v3.1.0
    hooks:
      - id: check-vcs-permalinks
      - id: end-of-file-fixer
      - id: trailing-whitespace
        args: [--markdown-linebreak-ext=md]
      - id: debug-statements
      - id: no-commit-to-branch

  - repo: https://github.com/saygox/commitizen
    rev: 09fa312b7846850f44f7860632685189e37c1bec
    hooks:
      - id: commitizen-prepare-commit-msg
        stages: [prepare-commit-msg

The command I used.

mkdir test
cd test
git init

# add .pre-commit-config.yaml

pre-commit install -t prepare-commit-msg
git commt

The error message

  File "/.../.cache/pre-commit/repof8ns4eua/py_env-python3/bin/cz", line 8, in <module>
    sys.exit(main())
  File "/.../.cache/pre-commit/repof8ns4eua/py_env-python3/lib/python3.7/site-packages/commitizen/cli.py", line 286, in main
    args.func(conf, vars(args))()
  File "/.../.cache/pre-commit/repof8ns4eua/py_env-python3/lib/python3.7/site-packages/commitizen/commands/commit.py", line 94, in __call__
    m = self.prompt_commit_questions()
  File "/.../.cache/pre-commit/repof8ns4eua/py_env-python3/lib/python3.7/site-packages/commitizen/commands/commit.py", line 63, in prompt_commit_questions
    answers = questionary.prompt(questions, style=cz.style)
  File "/.../.cache/pre-commit/repof8ns4eua/py_env-python3/lib/python3.7/site-packages/questionary/prompt.py", line 97, in prompt
    answer = question.unsafe_ask(patch_stdout)
  File "/.../.cache/pre-commit/repof8ns4eua/py_env-python3/lib/python3.7/site-packages/questionary/question.py", line 59, in unsafe_ask
    return self.application.run()
  File "/.../.cache/pre-commit/repof8ns4eua/py_env-python3/lib/python3.7/site-packages/prompt_toolkit/application/application.py", line 816, in run
    self.run_async(pre_run=pre_run, set_exception_handler=set_exception_handler)
  • Python version: 3.7
  • OS: macOS 10.15

Lee-W avatar Aug 20 '20 10:08 Lee-W

Glad to see this is being implemented as it is the biggest thing preventing us from adopting this technology. Is there a timeline for this PR?

Also @Lee-W not sure if this error occurred when you were creating the comment on this PR but you have a typo in the stages section of your .pre-commit-config.yaml for the commitizen-prepare-commit-msg hook. You are missing a closing ]. Perhaps that is what caused your error when running on your local machine.

ShaneKosieradzki avatar Jan 05 '21 23:01 ShaneKosieradzki

@ShaneKosieradzki Thanks for reminding me! But I still encounter an error on my local machine. Did you succeed on your local machine?

Traceback (most recent call last):
  File "/Users/weilee/.cache/pre-commit/repof8ns4eua/py_env-python3/bin/cz", line 8, in <module>
    sys.exit(main())
  File "/Users/weilee/.cache/pre-commit/repof8ns4eua/py_env-python3/lib/python3.8/site-packages/commitizen/cli.py", line 286, in main
    args.func(conf, vars(args))()
  File "/Users/weilee/.cache/pre-commit/repof8ns4eua/py_env-python3/lib/python3.8/site-packages/commitizen/commands/commit.py", line 95, in __call__
    m = self.prompt_commit_questions()
  File "/Users/weilee/.cache/pre-commit/repof8ns4eua/py_env-python3/lib/python3.8/site-packages/commitizen/commands/commit.py", line 63, in prompt_commit_questions
    answers = questionary.prompt(questions, style=cz.style)
  File "/Users/weilee/.cache/pre-commit/repof8ns4eua/py_env-python3/lib/python3.8/site-packages/questionary/prompt.py", line 97, in prompt
    answer = question.unsafe_ask(patch_stdout)
  File "/Users/weilee/.cache/pre-commit/repof8ns4eua/py_env-python3/lib/python3.8/site-packages/questionary/question.py", line 59, in unsafe_ask
    return self.application.run()
  File "/Users/weilee/.cache/pre-commit/repof8ns4eua/py_env-python3/lib/python3.8/site-packages/prompt_toolkit/application/application.py", line 814, in run
    return loop.run_until_complete(
  File "/Users/weilee/.pyenv/versions/3.8.5/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "/Users/weilee/.cache/pre-commit/repof8ns4eua/py_env-python3/lib/python3.8/site-packages/prompt_toolkit/application/application.py", line 781, in run_async
    return await _run_async2()
  File "/Users/weilee/.cache/pre-commit/repof8ns4eua/py_env-python3/lib/python3.8/site-packages/prompt_toolkit/application/application.py", line 763, in _run_async2
    result = await _run_async()
  File "/Users/weilee/.cache/pre-commit/repof8ns4eua/py_env-python3/lib/python3.8/site-packages/prompt_toolkit/application/application.py", line 694, in _run_async
    with self.input.raw_mode(), self.input.attach(
  File "/Users/weilee/.pyenv/versions/3.8.5/lib/python3.8/contextlib.py", line 113, in __enter__
    return next(self.gen)
  File "/Users/weilee/.cache/pre-commit/repof8ns4eua/py_env-python3/lib/python3.8/site-packages/prompt_toolkit/input/vt100.py", line 161, in _attached_input
    loop.add_reader(fd, callback)
  File "/Users/weilee/.pyenv/versions/3.8.5/lib/python3.8/asyncio/selector_events.py", line 332, in add_reader
    return self._add_reader(fd, callback, *args)
  File "/Users/weilee/.pyenv/versions/3.8.5/lib/python3.8/asyncio/selector_events.py", line 261, in _add_reader
    self._selector.register(fd, selectors.EVENT_READ,
  File "/Users/weilee/.pyenv/versions/3.8.5/lib/python3.8/selectors.py", line 522, in register
    self._selector.control([kev], 0, 0)
OSError: [Errno 22] Invalid argument

Lee-W avatar Jan 07 '21 04:01 Lee-W

@Lee-W it worked for me (Ubuntu 18.04.5; python 3.6.9; pre-commit 2.13.0) using pretty much your steps (with the closing ']' fix). Exact steps:

  1. make test
  2. cd test
  3. python3 -m venv venv
  4. source venv/bin/activate
  5. pip install pre-commit
  6. vim .pre-commit-config.yaml
repos:                                                                                                                                                                                                             
  - repo: https://github.com/saygox/commitizen                                                                                                                                                                     
    rev: 09fa312b7846850f44f7860632685189e37c1bec                                                                                                                                                                  
    hooks:                                                                                                                                                                                                         
      - id: commitizen-prepare-commit-msg                                                                                                                                                                          
        stages: [prepare-commit-msg]
  1. git init
  2. git add .pre-commit-config.yaml
  3. pre-commit install --install-hooks -t prepare-commit-msg
  4. git commit

I did encounter one potential issue in playing around a bit, however, after successfully commiting.

  1. git commit --amend
commitizen prepare commit msg............................................No files added to staging!
Failed
- hook id: commitizen-prepare-commit-msg
- exit code: 11

I'd like to see this issue/PR continued -- note that it would also "solve", or at least provide a viable alternative to, #248 since you are actually using git commit and therefore any flags or configs that you want for that, such as -S or --signoff.

lobotmcj avatar Aug 01 '21 19:08 lobotmcj

Hi @lobotmcj , I still encounter the same error on my mac. Still have no idea on how this could be fixed. In addition, we might need to take care of windows cases as well

Lee-W avatar Aug 07 '21 15:08 Lee-W

@Woile Do you have any though on this one?

Lee-W avatar Aug 08 '21 02:08 Lee-W

Hi @saygox thanks for your update. Could you please help us rebase the latest change from the master branch? I've tried to do so but fail to pass the test. Also, I'm wondering is it excepted to call git.commit twice on line 143 in commitizen/commands/commit.py

Lee-W avatar Nov 07 '21 04:11 Lee-W

Hi @Lee-W san. I tried to rebase this branch from HEAD. but my operation is not good so I can't push it. So I push the rebased branch to https://github.com/saygox/commitizen/tree/feature/prepare-commit-msg2 .

sorry to the line 143's git.commit is miss. prepare-commit-msg2 branch is fixed it. Would you mind to tell me how to merge a new rebased one to this request ? (or need to create a new push request?)

saygox avatar Nov 07 '21 05:11 saygox

Hi @saygox , have you try git push origin feature/prepare-commit-msg -f?

Lee-W avatar Nov 07 '21 06:11 Lee-W

@saygox Thanks for your hard work. I'm a bit busy lately, but I'll start digging into it these days.

Lee-W avatar Nov 23 '21 00:11 Lee-W

Thank you for your good comment and sorry to my late reply.

next commit is bellow.

Wrap standard in/out files turn to the package. cz commit will be walk well.

saygox avatar Dec 07 '21 06:12 saygox

It seems the pipeline is due to lack of runner which is our side of the problem. I'll take a look at it these days and see how we can tackle it.

Lee-W avatar Dec 07 '21 14:12 Lee-W

It seems the pipeline is due to lack of runner which is our side of the problem. I'll take a look at it these days and see how we can tackle it.

This PR https://github.com/commitizen-tools/commitizen/pull/456 should address this issue

Lee-W avatar Dec 08 '21 02:12 Lee-W

I just tried the latest version on my Mac but failed

Failed
- hook id: commitizen-prepare-commit-msg
- exit code: 1

also, we'll need to address https://github.com/commitizen-tools/commitizen/pull/250#pullrequestreview-814993357

Lee-W avatar Dec 22 '21 15:12 Lee-W

Hi, @Lee-W san. Sorry to this trouble.

I tell you about sad thing. I am far from Mac, so I don't know about Mac OS specification well. For this PR I bought used Mac mini(2014) and OS is 10.15.7.(new Mac is expensive) This programming woks well my Mac.

I try to upgrade my Mac to latest OS, but hung and reboot. So in this time I have no way to what is happen.

Would you mind tell me more information about error.

saygox avatar Dec 23 '21 15:12 saygox

@saygox Sure! I was trying to dig a bit but didn't find out the cause. Will run a deeper investigation when I have time. Thanks!

Lee-W avatar Dec 24 '21 03:12 Lee-W