Slide 1

Slide 1 text

@clleew commitizen-tools What can we gain from crafting a git message convention?

Slide 2

Slide 2 text

@clleew 2019/10/24 - Taipei.py

Slide 3

Slide 3 text

@clleew 2019/10/24 - Taipei.py

Slide 4

Slide 4 text

@clleew

Slide 5

Slide 5 text

@clleew Slide

Slide 6

Slide 6 text

@clleew __name__ = 李唯 / Wei Lee __position__ = [ Software Engineer @ Rakuten Slice, Volunteer @ PyCon Taiwan, "Maintainer of commitizen-tools, ] __twitter__ = @clleew __github__ = Lee-W __blog__ = http://lee-w.github.io $ cat speaker.py

Slide 7

Slide 7 text

@clleew File "speaker.py", line 1 __name__ = 李唯 / Wei Lee ^ SyntaxError: invalid syntax $ python speaker.py

Slide 8

Slide 8 text

@clleew Who have ever used git?

Slide 9

Slide 9 text

@clleew Who have ever commit like this? git commit -m "update"

Slide 10

Slide 10 text

@clleew

Slide 11

Slide 11 text

@clleew

Slide 12

Slide 12 text

@clleew Why not use "update" as a commit message? • Hard to find the right commit to rollback • Hard to know the trace of this change during code review • Tend to make a lot of changes in a single commit
 (because you don't know how to describe these changes)

Slide 13

Slide 13 text

@clleew Why should we write good commit message? • Guide teams on how code changes should be made • Save reviewers' time to know the intention of changes • Give newcomer a clear view of the evolvement of projects

Slide 14

Slide 14 text

@clleew How to write good commit messages? 1. Separate subject from body with a blank line 2. Limit the subject line to 50 characters 3. Capitalize the subject line 4. Do not end the subject line with a period 5. Use the imperative mood in the subject line 6. Wrap the body at 72 characters 7. Use the body to explain what and why vs. how

Slide 15

Slide 15 text

@clleew But... It's easy to forget all the guidelines when you enter "git commit"

Slide 16

Slide 16 text

@clleew commitizen-tools

Slide 17

Slide 17 text

@clleew

Slide 18

Slide 18 text

@clleew The command line tool for creating committing rules

Slide 19

Slide 19 text

@clleew GitHub Action that runs commitizen

Slide 20

Slide 20 text

@clleew cookiecutter template for creating customized rule

Slide 21

Slide 21 text

@clleew Commitizen

Slide 22

Slide 22 text

@clleew Install Commitizen

Slide 23

Slide 23 text

@clleew Initialize commitizen configuration

Slide 24

Slide 24 text

@clleew Initialize commitizen configuration

Slide 25

Slide 25 text

@clleew Initialize commitizen configuration

Slide 26

Slide 26 text

@clleew Initialize commitizen configuration

Slide 27

Slide 27 text

@clleew Initialize commitizen configuration

Slide 28

Slide 28 text

@clleew Initialize commitizen configuration

Slide 29

Slide 29 text

@clleew Initialize commitizen configuration We'll discuss more about it later.

Slide 30

Slide 30 text

@clleew [tool.commitizen] name = "cz_conventional_commits" version = "0.0.1" tag_format = "$version" Commtizen configuration pyproject.toml

Slide 31

Slide 31 text

@clleew cz commit type of this change A hint for users to break down multiple changes into separate commits

Slide 32

Slide 32 text

@clleew $ cz commit scope of this change

Slide 33

Slide 33 text

@clleew $ cz commit
 a short description

Slide 34

Slide 34 text

@clleew $ cz commit
 detail description of this change

Slide 35

Slide 35 text

@clleew $ cz commit
 whether it's a breaking change

Slide 36

Slide 36 text

@clleew $ cz commit
 footer reference to the ticket or issue related to this change

Slide 37

Slide 37 text

@clleew How does the commit look like? feat(tainan.py): awesome meetup hope everyone enjoys this talk https://www.meetup.com/Tainan-py-Python-Tainan-User-Group/events/ 272270687/

Slide 38

Slide 38 text

@clleew $ cz commit

Slide 39

Slide 39 text

@clleew $ cz commit

Slide 40

Slide 40 text

@clleew $ cz commit

Slide 41

Slide 41 text

@clleew Standardized git commits

Slide 42

Slide 42 text

@clleew What if I forget to use "cz commit" to do git commit?

Slide 43

Slide 43 text

@clleew Enforce commit check through pre-commit 1.Create .pre-commit-config.yaml
 
 
 
 
 2.Install pre-commit hook repos: - hooks: - id: commitizen stages: - commit-msg repo: https://github.com/commitizen-tools/commitizen rev: v2.1.0

Slide 44

Slide 44 text

@clleew It looks familiar

Slide 45

Slide 45 text

@clleew It looks familiar We already initialize it when running "cz init".

Slide 46

Slide 46 text

@clleew git will block it if your commit does not follow the rule.

Slide 47

Slide 47 text

@clleew git will block it if your commit does not follow the rule.

Slide 48

Slide 48 text

@clleew git will block it if your commit does not follow the rule.

Slide 49

Slide 49 text

@clleew How can we utilize these standardize commit messages? • Auto versioning • Auto generate changelog

Slide 50

Slide 50 text

@clleew Semantic Versioning x.y.z MAJOR Minor Patch

Slide 51

Slide 51 text

@clleew • MAJOR: incompatible API changes • MINOR: add backwards compatible functionality • PATCH: make backwards compatible bug fixes Semantic Versioning

Slide 52

Slide 52 text

@clleew Bump Version • MAJOR: breaking change • MINOR: feat • PATCH: fix, refactor, perf

Slide 53

Slide 53 text

@clleew Bump Version • MAJOR: breaking change • MINOR: feat • PATCH: fix, refactor, perf

Slide 54

Slide 54 text

@clleew Bump Version $ cz bump

Slide 55

Slide 55 text

@clleew The commit with the highest version to bump is feat (The original version of the sample project is 1.2.0.) Bump Version $ cz bump

Slide 56

Slide 56 text

@clleew message tag to create: 1.3.0 increment detected: MINOR The commit with the highest version to bump is feat (The original version of the sample project is 1.2.0.) Bump Version $ cz bump

Slide 57

Slide 57 text

@clleew Bump Version in Source File [tool.commitizen] name = "cz_conventional_commits" version = "0.0.1" tag_format = "$version" version_files = [ "my_project/__init__.py" ]

Slide 58

Slide 58 text

@clleew Keep a changelog

Slide 59

Slide 59 text

@clleew Auto generate changelog

Slide 60

Slide 60 text

@clleew Auto generate changelog # CHANGELOG ## Unreleased ### feat - add cov task to check test coverage - make scripts a package - add pylint task - add mypy, flake8 style check - add reformat task to reformat code through black

Slide 61

Slide 61 text

@clleew Auto generate changelog

Slide 62

Slide 62 text

@clleew Cutsomization 1. Use "cz-customize" in configuration file • No Python Knowledge needed • Suitable for basic rules 2. Creating a customized python package • Suitable for advance usage
 (e.g., customize changelog parser and hook)

Slide 63

Slide 63 text

@clleew Back to our config file [tool.commitizen] name = "cz_conventional_commits" version = "0.0.1" tag_format = "$version"

Slide 64

Slide 64 text

@clleew cz_customize [tool.commitizen] name = "cz_customize"

Slide 65

Slide 65 text

@clleew cz_customize [tool.commitizen] name = "cz_customize" [tool.commitizen.customize] message_template = "{{change_type}}: {{message}}" bump_pattern = "^(break|new|hotfix)" bump_map = {"break" = "MAJOR", "new" = "MINOR", "hotfix" = "PATCH"} [tool.commitizen] name = "cz_customize"

Slide 66

Slide 66 text

@clleew cz_customize [tool.commitizen] name = "cz_customize" [tool.commitizen.customize] message_template = "{{change_type}}: {{message}}" bump_pattern = "^(break|new|hotfix)" bump_map = {"break" = "MAJOR", "new" = "MINOR", "hotfix" = "PATCH"} [[tool.commitizen.customize.questions]] type = "list" name = "change_type" choices = [ {value = "feature", name = "feature: A new feature."}, {value = "hotfix", name = "bug fix: A bug fix."} ] message = "Select the type of change you are committing" [[tool.commitizen.customize.questions]] type = "input" name = "message" message = "Body." [tool.commitizen] name = "cz_customize" [tool.commitizen.customize] message_template = "{{change_type}}: {{message}}" bump_pattern = "^(break|new|hotfix)" bump_map = {"break" = "MAJOR", "new" = "MINOR", "hotfix" = "PATCH"} [tool.commitizen] name = "cz_customize"

Slide 67

Slide 67 text

@clleew Customize through python package from commitizen.cz.base import BaseCommitizen class JiraCz(BaseCommitizen): def questions(self) -> list: """Questions regarding the commit message.""" questions = [ {"type": "input", "name": "title", "message": "Commit title"}, {"type": "input", "name": "issue", "message": "Jira Issue number:"}, ] return questions def message(self, answers: dict) -> str: """Generate the message with the given answers.""" return f"{answers['title']} (#{answers['issue']})" discover_this = JiraCz # used by the plug-in system

Slide 68

Slide 68 text

@clleew commitizen-tools/commitizen_cz_template

Slide 69

Slide 69 text

@clleew Join us and make commitizen-tools better ⭐

Slide 70

Slide 70 text

@clleew Reference • commitizen-tools • Conventional Commits • Semantic Versioning • Keep a changelog • How to Write a Git Commit Message