-
-
Notifications
You must be signed in to change notification settings - Fork 268
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #731 from crai0/write-message-to-file
feat(commit): add --write-message-to-file option
- Loading branch information
Showing
8 changed files
with
198 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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