Skip to content

Commit a6b89ce

Browse files
authored
Merge pull request #141 from Lee-W/batch-check
Batch check
2 parents e25430a + 4de0008 commit a6b89ce

23 files changed

+162
-51
lines changed

commitizen/bump.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
from packaging.version import Version
88

9-
from commitizen.git import GitCommit
109
from commitizen.defaults import (
1110
MAJOR,
1211
MINOR,
@@ -15,6 +14,7 @@
1514
bump_message,
1615
bump_pattern,
1716
)
17+
from commitizen.git import GitCommit
1818

1919

2020
def find_increment(

commitizen/cli.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,18 @@
151151
"arguments": [
152152
{
153153
"name": "--commit-msg-file",
154-
"required": True,
155154
"help": (
156155
"ask for the name of the temporal file that contains "
157156
"the commit message. "
158157
"Using it in a git hook script: MSG_FILE=$1"
159158
),
160-
}
159+
"exclusive_group": "group1",
160+
},
161+
{
162+
"name": "--rev-range",
163+
"help": ("a reange of git rev to check. e.g, master..HEAD"),
164+
"exclusive_group": "group1",
165+
},
161166
],
162167
},
163168
{

commitizen/commands/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
from .commit import Commit
44
from .example import Example
55
from .info import Info
6+
from .init import Init
67
from .list_cz import ListCz
78
from .schema import Schema
89
from .version import Version
9-
from .init import Init
10-
1110

1211
__all__ = (
1312
"Bump",

commitizen/commands/bump.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
from typing import Optional, List
1+
from typing import List, Optional
22

33
import questionary
44
from packaging.version import Version
55

66
from commitizen import bump, factory, git, out
77
from commitizen.config import BaseConfig
88
from commitizen.error_codes import (
9+
COMMIT_FAILED,
910
NO_COMMITS_FOUND,
10-
NO_VERSION_SPECIFIED,
1111
NO_PATTERN_MAP,
12-
COMMIT_FAILED,
12+
NO_VERSION_SPECIFIED,
1313
TAG_FAILED,
1414
)
1515

commitizen/commands/check.py

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import os
22
import re
3+
from typing import Dict
34

4-
from commitizen import factory, out
5+
from commitizen import factory, git, out
56
from commitizen.config import BaseConfig
67
from commitizen.error_codes import INVALID_COMMIT_MSG
78

89

910
class Check:
1011
"""Check if the current commit msg matches the commitizen format."""
1112

12-
def __init__(self, config: BaseConfig, arguments: dict, cwd=os.getcwd()):
13+
def __init__(self, config: BaseConfig, arguments: Dict[str, str], cwd=os.getcwd()):
1314
"""Init method.
1415
1516
Parameters
@@ -21,31 +22,53 @@ def __init__(self, config: BaseConfig, arguments: dict, cwd=os.getcwd()):
2122
the flags provided by the user
2223
2324
"""
25+
self.commit_msg_file: str = arguments.get("commit_msg_file")
26+
self.rev_range: str = arguments.get("rev_range")
27+
28+
self._valid_command_argument()
29+
2430
self.config: BaseConfig = config
2531
self.cz = factory.commiter_factory(self.config)
26-
self.arguments: dict = arguments
32+
33+
def _valid_command_argument(self):
34+
if bool(self.commit_msg_file) is bool(self.rev_range):
35+
out.error("One and only one argument is required for check command!")
36+
raise SystemExit()
2737

2838
def __call__(self):
29-
"""Validate if a commit message follows the conventional pattern.
39+
"""Validate if commit messages follows the conventional pattern.
3040
3141
Raises
3242
------
3343
SystemExit
3444
if the commit provided not follows the conventional pattern
3545
3646
"""
37-
commit_msg_content = self._get_commit_msg()
47+
commit_msgs = self._get_commit_messages()
3848
pattern = self.cz.schema_pattern()
39-
if self._has_proper_format(pattern, commit_msg_content) is not None:
40-
out.success("Commit validation: successful!")
41-
else:
42-
out.error("commit validation: failed!")
43-
out.error("please enter a commit message in the commitizen format.")
44-
raise SystemExit(INVALID_COMMIT_MSG)
45-
46-
def _get_commit_msg(self):
47-
temp_filename: str = self.arguments.get("commit_msg_file")
48-
return open(temp_filename, "r").read()
49-
50-
def _has_proper_format(self, pattern, commit_msg):
49+
for commit_msg in commit_msgs:
50+
if not Check.validate_commit_message(commit_msg, pattern):
51+
out.error(
52+
"commit validation: failed!\n"
53+
"please enter a commit message in the commitizen format.\n"
54+
f"commit: {commit_msg}\n"
55+
f"pattern: {pattern}"
56+
)
57+
raise SystemExit(INVALID_COMMIT_MSG)
58+
out.success("Commit validation: successful!")
59+
60+
def _get_commit_messages(self):
61+
# Get commit message from file (--commit-msg-file)
62+
if self.commit_msg_file:
63+
with open(self.commit_msg_file, "r") as commit_file:
64+
commit_msg = commit_file.read()
65+
return [commit_msg]
66+
67+
# Get commit messages from git log (--rev-range)
68+
return [commit.message for commit in git.get_commits(end=self.rev_range)]
69+
70+
@staticmethod
71+
def validate_commit_message(commit_msg: str, pattern: str) -> bool:
72+
if commit_msg.startswith("Merge") or commit_msg.startswith("Revert"):
73+
return True
5174
return re.match(pattern, commit_msg)

commitizen/commands/commit.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
import questionary
66

77
from commitizen import factory, git, out
8-
from commitizen.cz.exceptions import CzException
98
from commitizen.config import BaseConfig
9+
from commitizen.cz.exceptions import CzException
1010
from commitizen.error_codes import (
11-
NO_ANSWERS,
1211
COMMIT_ERROR,
12+
CUSTOM_ERROR,
13+
NO_ANSWERS,
1314
NO_COMMIT_BACKUP,
1415
NOTHING_TO_COMMIT,
15-
CUSTOM_ERROR,
1616
)
1717

1818

commitizen/commands/init.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
from packaging.version import Version
2-
31
import questionary
2+
from packaging.version import Version
43

54
from commitizen import factory, out
5+
from commitizen.config import BaseConfig, IniConfig, TomlConfig
66
from commitizen.cz import registry
7-
from commitizen.config import BaseConfig, TomlConfig, IniConfig
8-
from commitizen.git import get_latest_tag_name, get_tag_names
97
from commitizen.defaults import long_term_support_config_files
8+
from commitizen.git import get_latest_tag_name, get_tag_names
109

1110

1211
class Init:

commitizen/commands/list_cz.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from commitizen import out
2-
from commitizen.cz import registry
32
from commitizen.config import BaseConfig
3+
from commitizen.cz import registry
44

55

66
class ListCz:

commitizen/commands/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from commitizen import out
2-
from commitizen.config import BaseConfig
32
from commitizen.__version__ import __version__
3+
from commitizen.config import BaseConfig
44

55

66
class Version:

commitizen/config/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
from commitizen import defaults, git, out
66
from commitizen.error_codes import NOT_A_GIT_PROJECT
7+
78
from .base_config import BaseConfig
8-
from .toml_config import TomlConfig
99
from .ini_config import IniConfig
10+
from .toml_config import TomlConfig
1011

1112

1213
def load_global_conf() -> Optional[IniConfig]:

0 commit comments

Comments
 (0)