Commit e1801196 authored by Francesco Witz's avatar Francesco Witz
Browse files

Merge branch 'chlog-script' into 'main'

Finished cli tool

See merge request !3
parents 78038a66 1c5934f2
.envrc
venv
*__pycache__*
\ No newline at end of file
*__pycache__*
chlog.json
This diff is collapsed.
......@@ -2,20 +2,19 @@
"commit-type": [
["feat", "Commit that add a feature to the project"],
["fix","Commit that fix an issue"],
["style", "Commit that change visual style of the project"],
["removed", "Commit that remove a feature from the project"],
["security", "Commit related to security changes"],
["test", "Commit that concern project testing"],
["doc", "Commit that add doc to the project"]
],
"ignored-commit-type": [
["wip", "Work in progress commit"],
["repo", "Commit concerning repo organisation"],
["refactor", "Code refactoring commit"],
["test", "Commit that concern project testing"],
["merge", "Merge commit"]
],
"commit-type-case": "lowercase",
"commit-subject-case": "lowercase",
"commit-subject-ponctuation": false,
"commit-subject-length": 50,
"commit-description-length": 100
"commit-subject-length": 50
}
\ No newline at end of file
import os
import re
import json
from datetime import datetime
from git_scrapper import Commit
class File():
......@@ -31,10 +33,65 @@ class File():
def write_chlog(self, newlines: list[str]):
""" Method that writes the new lines in the chlog file"""
pass
if os.path.exists(self.path):
with open(self.path, 'w') as file:
file.writelines(newlines)
def write_changes(file: File, commits: list[Commit]):
def write_changes(project_path: str, file: File, commits: list[Commit]):
""" Method that write the given commits by adding a new section to the changelog file """
# Getting released version
os.chdir(project_path)
with open("chlog.json") as chlog_file:
chlog = json.load(chlog_file)
version: str = chlog["version"]
# Creating title (version + date)
section: str = ["## [" + version[1:] + "] - " + datetime.today().isoformat().split('T')[0] + "\n"]
# Sorting commits by types
# Creating sections (title + unnumbered list)
\ No newline at end of file
added = []
changed = []
removed = []
security = []
doc = []
for commit in commits:
if commit.get_type() == "feat":
added.append(commit.chlog_format())
elif commit.get_type() == "fix":
changed.append(commit.chlog_format())
elif commit.get_type() == "removed":
removed.append(commit.chlog_format())
elif commit.get_type() == "security":
security.append(commit.chlog_format())
elif commit.get_type() == "doc":
doc.append(commit.chlog_format())
# Creating sections (title + unnumbered list)
if added:
section.append("### Added")
for e in added:
section.append("- " + e)
section.append("\n")
if changed:
section.append("### Changed")
for e in changed:
section.append("- " + e)
section.append("\n")
if removed:
section.append("### Removed")
for e in removed:
section.append("- " + e)
section.append("\n")
if security:
section.append("### Security")
for e in security:
section.append("- " + e)
section.append("\n")
if doc:
section.append("### Doc")
for e in doc:
section.append("- " + e)
section.append("\n")
# Finally writting everything to file
lines = file.read_chlog()
insert_index = file.get_where_to_write()
lines.insert(insert_index, section)
file.write_chlog(lines)
import subprocess
import os
import re
class Commit():
""" Class that represent a git commit """
......@@ -13,32 +14,37 @@ class Commit():
def get_hash(self) -> str:
return self.__hash
def get_type(self) -> str:
return self.__ctype
def chlog_format(self) -> str:
""" Method that return the commit infos as a chlog file's formatted style """
# TODO: hash: message (date)
return self.__hash + ': ' + self.__message + '(' + self.__date + ')'
def __str__(self) -> str:
return self.__date + " " + self.__ctype + ": " + self.__message + "(" + self.__hash + ")"
def get_commits(directory: str, version: str) -> list[Commit]:
""" Method that retrieve all the commits in a git repo """
os.chdir(directory) #Moving to the repo directory
def get_formatted_commits(version: str) -> list[Commit]:
""" Method that retrieve the correctly formatted commit after the given version """
# Lauching the git log command to retrieve all the commits and split it
cmd = subprocess.check_output(
['git', 'log', '--format=%h/%s/%as'], stderr=subprocess.STDOUT
).split(b'\n')
lines = [c.decode('utf-8') for c in cmd] # Decoding each line from byte to string
lines.pop() #Deleting last element, corresponding to the first commit of the repository
lines = lines[:-1] #Deleting last element, corresponding to the first commit of the repository
# Parsing each commits info to create a Commit object
commits: list[Commit] = []
for line in lines:
if not 'Merge' in line:
elem = line.split('/') # Splitting element
temp = elem[1].split(':') # Splitting commit type and commit subject
elem = line.split('/') # Splitting element
format_pattern = re.compile("[a-z]+:(\s|\S)(?:[a-z]+(\s|\S))+")
if format_pattern.match(elem):
temp = elem[1].split(':') # Splitting commit type and commit subject
if len(temp) == 2:
elem[1] = temp[0]
elem.insert(2, temp[1][1:])
......@@ -51,22 +57,48 @@ def get_commits(directory: str, version: str) -> list[Commit]:
if c.get_hash() == last_version_hash:
start = commits.index(c)
return commits[start:]
return commits[:start]
def get_raw_commits(version: str) -> list[list[str]]:
""" Method that retrieves all the raw commits after the given version """
# Lauching the git log command to retrieve all the commits and split it
cmd = subprocess.check_output(
['git', 'log', '--format=%h/%s/%as'], stderr=subprocess.STDOUT
).split(b'\n')
lines = [c.decode('utf-8') for c in cmd] # Decoding each line from byte to string
lines = lines[:-2] #Deleting last element, corresponding to the first commit of the repository
# Getting raw commits
commits: list[str] = []
for line in lines:
if not 'Merge' in line:
commits.append(line.split('/')[:2])
# Getting the commits concerning the given version
last_version_hash = get_tagged_commit(version)
start = 0
for c in commits:
if c[0] == last_version_hash:
start = commits.index(c)
return commits[:start]
def add_git_tag(version, commit_hash):
def add_git_tag(version: str, commit_hash: str):
""" Method that add a git tag on the last commit or to a specific commit """
# Simply executing the git tag commands. If commit_hash is None, we apply the tag to the last commit
if commit_hash == None:
subprocess.check_output(
['git', 'tag', '-a ' + version, '-m Tag for version ' + version], stderr=subprocess.STDOUT
['git', 'tag', '-a', version, '-m', 'Tag for version ' + version[1:]], stderr=subprocess.STDOUT
)
subprocess.check_output(
['git push'], stderr=subprocess.STDOUT
['git', 'push', '--tags'], stderr=subprocess.STDOUT
)
else:
subprocess.check_output(
['git', 'tag', '-a ' + version, '-m Tag for version ' + version, ' ' + commit_hash], stderr=subprocess.STDOUT
['git', 'tag', '-a', version, '-m', 'Tag for version ' + version, ' ' + commit_hash], stderr=subprocess.STDOUT
)
......@@ -74,10 +106,10 @@ def remove_git_tag(version):
""" Method that remove a git tag from the repo """
# Removing the specific tag from the local repo and from the distant one
subprocess.check_output(
['git', 'tag', '-d ' + version], stderr=subprocess.STDOUT
['git', 'tag', '-d', version], stderr=subprocess.STDOUT
)
subprocess.check_output(
['git', 'push', '--delete origin ' + version], stderr=subprocess.STDOUT
['git', 'push', '--delete origin', version], stderr=subprocess.STDOUT
)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment