Release tools read conventional commit messages and choose the next version. Write that logic.
What to build
A program that finds the next version from the commit messages since the last release.
Requirements
- The first line is the current version
MAJOR.MINOR.PATCH. Then come the messages (spaces at the ends are removed). - A message is breaking if the head (the text before the first
:) ends with!, or if the message containsBREAKING CHANGE. Otherwise its type is the head up to the first(. - If any message is breaking, the next version is
MAJOR+1.0.0. Otherwise, if any message has typefeat, it isMAJOR.MINOR+1.0. Otherwise, if any has typefix, it isMAJOR.MINOR.PATCH+1. - Print
next: VERSION. If no message is breaking,featorfix, printno release.
Input
The first line is the current version. The second line has n (1 to 50). Each of the next n lines is a commit message.
Output
One line.
Constraints
1 ≤ n ≤ 50. The messages contain a colon.
Example
Input:
1.4.2
3
fix(cli): exit with code 1
feat(parser): support nested lists
docs: explain the install steps
Output:
next: 1.5.0
Why it works
Each message votes for a kind of change, and the biggest kind decides the bump, because a release containing a breaking change must be a major release whatever else it holds.
Examples
1.4.2 3 fix(cli): exit with code 1 feat(parser): support nested lists docs: explain the install steps
next: 1.5.0
2.0.1 2 docs: update the README chore: bump dependencies
no release
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
The head of a message is the text before the first colon; the type is the head without "(scope)" and without "!".
Hint 2
A head ending in "!" or the text BREAKING CHANGE in the message means a major bump.
Hint 3
The highest kind wins: major, then minor (feat), then patch (fix).
Solution
Show one correct solution (try it yourself first)
current = input().strip()
n = int(input())
major, minor, patch = [int(x) for x in current.split(".")]
kinds = set()
for _ in range(n):
message = input().strip()
head = message.split(":")[0]
if head.endswith("!") or "BREAKING CHANGE" in message:
kinds.add("major")
else:
kind = head.split("(")[0]
if kind == "feat":
kinds.add("minor")
elif kind == "fix":
kinds.add("patch")
if "major" in kinds:
print(f"next: {major + 1}.0.0")
elif "minor" in kinds:
print(f"next: {major}.{minor + 1}.0")
elif "patch" in kinds:
print(f"next: {major}.{minor}.{patch + 1}")
else:
print("no release")
▶ Open this solution in the compiler, then press Visualize to watch it run step by step