Skip to content

Commit a99b3e0

Browse files
authored
Merge pull request #15 from ashbob999/improve-release-notes
Add commit descriptions & hashes to the release notes Closes #14
2 parents fb06787 + 4847361 commit a99b3e0

File tree

1 file changed

+42
-18
lines changed

1 file changed

+42
-18
lines changed

tinysemver/tinysemver.py

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def get_last_tag(repository_path: PathLike) -> str:
7575
return result.stdout.strip().decode("utf-8")
7676

7777

78-
def get_commits_since_tag(repository_path: PathLike, tag: str) -> Tuple[List[str], List[str]]:
78+
def get_commits_since_tag(repository_path: PathLike, tag: str) -> List[Tuple[str, str]]:
7979
"""Get commit hashes and messages since the specified Git tag."""
8080
result = subprocess.run(
8181
["git", "log", f"{tag}..HEAD", "--no-merges", "--pretty=format:%h:%s"],
@@ -89,7 +89,7 @@ def get_commits_since_tag(repository_path: PathLike, tag: str) -> Tuple[List[str
8989
lines = result.stdout.strip().decode("utf-8").split("\n")
9090
hashes = [line.partition(":")[0] for line in lines if line.strip()]
9191
commits = [line.partition(":")[2] for line in lines if line.strip()]
92-
return hashes, commits
92+
return list(zip(hashes, commits))
9393

9494

9595
def parse_version(tag: str) -> SemVer:
@@ -119,27 +119,45 @@ def normalize_verbs(verbs: Union[str, List[str]], defaults: List[str]) -> List[s
119119

120120

121121
def group_commits(
122-
commits: List[str],
122+
commits: List[Tuple[str, str]],
123123
major_verbs: List[str],
124124
minor_verbs: List[str],
125125
patch_verbs: List[str],
126-
) -> Tuple[List[str], List[str], List[str]]:
126+
) -> Tuple[List[Tuple[str, str]], List[Tuple[str, str]], List[Tuple[str, str]]]:
127127
"""Group commits into major, minor, and patch categories based on keywords to simplify future `BumpType` resolution."""
128128
major_commits = []
129129
minor_commits = []
130130
patch_commits = []
131131

132132
for commit in commits:
133-
if any(commit_starts_with_verb(commit, verb) for verb in major_verbs):
133+
if any(commit_starts_with_verb(commit[1], verb) for verb in major_verbs):
134134
major_commits.append(commit)
135-
if any(commit_starts_with_verb(commit, verb) for verb in minor_verbs):
135+
if any(commit_starts_with_verb(commit[1], verb) for verb in minor_verbs):
136136
minor_commits.append(commit)
137-
if any(commit_starts_with_verb(commit, verb) for verb in patch_verbs):
137+
if any(commit_starts_with_verb(commit[1], verb) for verb in patch_verbs):
138138
patch_commits.append(commit)
139139

140140
return major_commits, minor_commits, patch_commits
141141

142142

143+
def convert_commits_to_message(
144+
major_commits: List[Tuple[Str, Str]],
145+
minor_commits: List[Tuple[Str, Str]],
146+
patch_commits: List[Tuple[Str, Str]],
147+
) -> Str:
148+
"""Turns the different commits (major, minor, patch) into a single message."""
149+
message = ""
150+
151+
if len(major_commits):
152+
message += f"\n### Major\n\n" + "\n".join(f"- {c[1]} ({c[0]})" for c in major_commits) + "\n"
153+
if len(minor_commits):
154+
message += f"\n### Minor\n\n" + "\n".join(f"- {c[1]} ({c[0]})" for c in minor_commits) + "\n"
155+
if len(patch_commits):
156+
message += f"\n### Patch\n\n" + "\n".join(f"- {c[1]} ({c[0]})" for c in patch_commits) + "\n"
157+
158+
return message
159+
160+
143161
def bump_version(version: SemVer, bump_type: BumpType) -> SemVer:
144162
"""Bump the version based on the specified bump type (major, minor, patch)."""
145163
major, minor, patch = version
@@ -162,6 +180,9 @@ def create_tag(
162180
github_repository: Optional[str] = None,
163181
push: bool = False,
164182
create_release: bool = False,
183+
major_commits: List[Tuple[Str, Str]] = None,
184+
minor_commits: List[Tuple[Str, Str]] = None,
185+
patch_commits: List[Tuple[Str, Str]] = None,
165186
) -> None:
166187
"""Create a new Git tag and optionally push it to a remote GitHub repository."""
167188

@@ -172,7 +193,10 @@ def create_tag(
172193
env["GIT_AUTHOR_NAME"] = user_name
173194
env["GIT_AUTHOR_EMAIL"] = user_email
174195
env["GITHUB_TOKEN"] = github_token
196+
175197
message = f"Release: {tag} [skip ci]"
198+
message += convert_commits_to_message(major_commits or [], minor_commits or [], patch_commits or [])
199+
176200
subprocess.run(["git", "add", "-A"], cwd=repository_path)
177201
subprocess.run(["git", "commit", "-m", message], cwd=repository_path, env=env)
178202

@@ -408,19 +432,19 @@ def normalize_path(file_path: str) -> str:
408432
if verbose:
409433
print(f"Current version: {current_version[0]}.{current_version[1]}.{current_version[2]}")
410434

411-
commits_hashes, commits_messages = get_commits_since_tag(repository_path, last_tag)
435+
commits = get_commits_since_tag(repository_path, last_tag)
412436
if not len(commits_hashes):
413437
raise NoNewCommitsError(f"No new commits since the last {last_tag} tag")
414438

415439
if verbose:
416-
print(f"? Commits since last tag: {len(commits_hashes)}")
417-
for hash, commit in zip(commits_hashes, commits_messages):
440+
print(f"? Commits since last tag: {len(commits)}")
441+
for hash, commit in commits:
418442
print(f"# {hash}: {commit}")
419443

420-
major_commits, minor_commits, patch_commits = group_commits(commits_messages, major_verbs, minor_verbs, patch_verbs)
444+
major_commits, minor_commits, patch_commits = group_commits(commits, major_verbs, minor_verbs, patch_verbs)
421445
assert (
422446
len(major_commits) + len(minor_commits) + len(patch_commits)
423-
), "No commit categories found to bump the version: " + ", ".join(commits_messages)
447+
), "No commit categories found to bump the version: " + ", ".join(map(lambda c: c[1], commits))
424448

425449
if len(major_commits):
426450
bump_type = "major"
@@ -439,17 +463,14 @@ def normalize_path(file_path: str) -> str:
439463
if changelog_file:
440464
now = datetime.now()
441465
changes = f"\n## {now:%B %d, %Y}: v{new_version_str}\n"
442-
if len(major_commits):
443-
changes += f"\n### Major\n\n" + "\n".join(f"- {c}" for c in major_commits) + "\n"
444-
if len(minor_commits):
445-
changes += f"\n### Minor\n\n" + "\n".join(f"- {c}" for c in minor_commits) + "\n"
446-
if len(patch_commits):
447-
changes += f"\n### Patch\n\n" + "\n".join(f"- {c}" for c in patch_commits) + "\n"
466+
changes += convert_commits_to_message(major_commits, minor_commits, patch_commits)
448467

449468
print(f"Will update file: {changelog_file}")
450469
if verbose:
451470
changes_lines = changes.count("\n") + 1
452471
print(f"? Appending {changes_lines} lines")
472+
for line in changes.split("\n"):
473+
print(f"+ {line}")
453474

454475
if not dry_run:
455476
with open(changelog_file, "a") as file:
@@ -479,6 +500,9 @@ def normalize_path(file_path: str) -> str:
479500
github_repository=github_repository,
480501
push=push,
481502
create_release=create_release,
503+
major_commits=major_commits,
504+
minor_commits=minor_commits,
505+
patch_commits=patch_commits,
482506
)
483507

484508

0 commit comments

Comments
 (0)