Apply the common rules for good commit messages automatically.
What to build
A program that checks a commit message against common style rules.
Requirements
- Rules, checked in this order: the subject (line 1) has at most 50 characters; it starts with a capital letter; it does not end with a period; if there is a line 2, it is blank; every later line has at most 72 characters.
- Print one line per problem:
subject is N characters (at most 50),subject should start with a capital letter,subject should not end with a period,line 2 should be blank,line K is N characters (at most 72). If there are none, printgood message.
Input
Line 1: n. Then the n lines of the message.
Output
The problems, or good message.
Constraints
n ≤ 100.
Example
Input:
1
Add login form
Output:
good message
Why it works
A short, capitalised subject without a period fits one-line history views, and the blank line tells Git and other tools where the subject ends.
Examples
Example 1 · Input
1 Add login form
Expected output
good message
Example 2 · Input
1 fixed the bug.
Expected output
subject should start with a capital letter subject should not end with a period
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Check the rules in the order given, and collect every problem.
Hint 2
Line numbers start at 1; the body is line 3 onwards.
Solution
Show one correct solution (try it yourself first)
lines = [input() for _ in range(int(input()))]
problems = []
subject = lines[0]
if len(subject) > 50:
problems.append(f"subject is {len(subject)} characters (at most 50)")
if not subject[:1].isupper():
problems.append("subject should start with a capital letter")
if subject.endswith("."):
problems.append("subject should not end with a period")
if len(lines) > 1 and lines[1] != "":
problems.append("line 2 should be blank")
for number, line in enumerate(lines[2:], start=3):
if len(line) > 72:
problems.append(f"line {number} is {len(line)} characters (at most 72)")
print("\n".join(problems) if problems else "good message")