-
-
Notifications
You must be signed in to change notification settings - Fork 286
feat(commit): add --write-message-to-file option #731
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ff56b4e
feat(commit): add --write-message-to-file option
crai0 af8e276
test(commit): add test for --write-message-to-file option
crai0 a2d0cb8
docs(commit): document --write-message-to-file option
crai0 dc0c86f
docs(tutorials): add tutorial for automatically preparing commit message
crai0 cd93206
test(commit): add negative test cases for --write-message-to-file option
crai0 14fac5b
docs(commit): link to prepare-commit-msg hook tutorial
crai0 4e28075
docs(prepare-commit-msg): add additional information to tutorial
crai0 ea3ecce
refactor(commit): change type of write_message_to_file to path
crai0 ab42fd4
test(commit): pass path for write_message_to_file
crai0 5b4f944
feat(hooks): add prepare-commit-msg and post-commit hooks
crai0 f04a719
docs(tutorials): add installation guide and feature overview for hooks
crai0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Automatically prepare message before commit | ||
|
||
## About | ||
|
||
It can be desirable to use commitizen for all types of commits (i.e. regular, merge, | ||
squash) so that the complete git history adheres to the commit message convention | ||
without ever having to call `cz commit`. | ||
|
||
To automatically prepare a commit message prior to committing, you can | ||
use a [prepare-commit-msg Git hook](prepare-commit-msg-docs): | ||
|
||
> This hook is invoked by git-commit right after preparing the | ||
> default log message, and before the editor is started. | ||
|
||
To automatically perform arbitrary cleanup steps after a succesful commit you can use a | ||
[post-commit Git hook][post-commit-docs]: | ||
|
||
> This hook is invoked by git-commit. It takes no parameters, and is invoked after a | ||
> commit is made. | ||
|
||
A combination of these two hooks allows for enforcing the usage of commitizen so that | ||
whenever a commit is about to be created, commitizen is used for creating the commit | ||
message. Running `git commit` or `git commit -m "..."` for example, would trigger | ||
commitizen and use the generated commit message for the commit. | ||
|
||
## Installation | ||
|
||
Copy the hooks from [here](https://github.com/commitizen-tools/hooks) into the `.git/hooks` folder and make them | ||
executable by running the following commands from the root of your Git repository: | ||
|
||
```bash | ||
wget -o .git/hooks/prepare-commit-msg https://github.com/commitizen-tools/hooks/prepare-commit-msg.py | ||
chmod +x .git/hooks/prepare-commit-msg | ||
wget -o .git/hooks/post-commit https://github.com/commitizen-tools/hooks/post-commit.py | ||
chmod +x .git/hooks/post-commit | ||
``` | ||
|
||
## Features | ||
|
||
- Commits can be created using both `cz commit` and the regular `git commit` | ||
- The hooks automatically create a backup of the commit message that can be reused if | ||
the commit failed | ||
- The commit message backup can also be used via `cz commit --retry` | ||
|
||
[post-commit-docs]: https://git-scm.com/docs/githooks#_post_commit | ||
[prepare-commit-msg-docs]: https://git-scm.com/docs/githooks#_prepare_commit_msg |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#!/usr/bin/env python | ||
import os | ||
import tempfile | ||
from pathlib import Path | ||
|
||
|
||
def post_commit(): | ||
backup_file = Path( | ||
tempfile.gettempdir(), f"cz.commit{os.environ.get('USER', '')}.backup" | ||
) | ||
|
||
# remove backup file if it exists | ||
if backup_file.is_file(): | ||
backup_file.unlink() | ||
|
||
|
||
if __name__ == "__main__": | ||
exit(post_commit()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#!/usr/bin/env python | ||
import os | ||
import shutil | ||
import subprocess | ||
import sys | ||
import tempfile | ||
from pathlib import Path | ||
from subprocess import CalledProcessError | ||
|
||
|
||
def prepare_commit_msg(commit_msg_file: Path) -> int: | ||
# check that commitizen is installed | ||
if shutil.which("cz") is None: | ||
print("commitizen is not installed!") | ||
return 0 | ||
|
||
# check if the commit message needs to be generated using commitizen | ||
if ( | ||
subprocess.run( | ||
[ | ||
"cz", | ||
"check", | ||
"--commit-msg-file", | ||
commit_msg_file, | ||
], | ||
capture_output=True, | ||
).returncode | ||
!= 0 | ||
): | ||
backup_file = Path( | ||
tempfile.gettempdir(), f"cz.commit{os.environ.get('USER', '')}.backup" | ||
) | ||
|
||
if backup_file.is_file(): | ||
# confirm if commit message from backup file should be reused | ||
answer = input("retry with previous message? [y/N]: ") | ||
if answer.lower() == "y": | ||
shutil.copyfile(backup_file, commit_msg_file) | ||
return 0 | ||
|
||
# use commitizen to generate the commit message | ||
try: | ||
subprocess.run( | ||
[ | ||
"cz", | ||
"commit", | ||
"--dry-run", | ||
"--write-message-to-file", | ||
commit_msg_file, | ||
], | ||
stdin=sys.stdin, | ||
stdout=sys.stdout, | ||
).check_returncode() | ||
except CalledProcessError as error: | ||
return error.returncode | ||
|
||
# write message to backup file | ||
shutil.copyfile(commit_msg_file, backup_file) | ||
|
||
|
||
if __name__ == "__main__": | ||
# make hook interactive by attaching /dev/tty to stdin | ||
with open("/dev/tty") as tty: | ||
sys.stdin = tty | ||
exit(prepare_commit_msg(sys.argv[1])) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.