Skip to content

Instantly share code, notes, and snippets.

@sofusalbertsen
Created December 29, 2022 14:29
Show Gist options
  • Save sofusalbertsen/8ee07ea77342b28d67d5481fd13ecc7e to your computer and use it in GitHub Desktop.
Save sofusalbertsen/8ee07ea77342b28d67d5481fd13ecc7e to your computer and use it in GitHub Desktop.
import os
import subprocess
def get_git_history(repo_path):
# Get the list of commits, tags and branches from the repository
result = subprocess.run(['git', '-C', repo_path, 'log', '--oneline', '--graph', '--all'], stdout=subprocess.PIPE)
git_history = result.stdout.decode('utf-8')
# Split the history into a list of lines
lines = git_history.split('\n')
# start mermaid syntax
print("gitGraph")
for line in lines:
print(line)
chunks = line.split()
if len(chunks) == 0:
continue
if "|/" in line:
continue
if "|\\" in line:
continue
# if there is a ( it is a branch
if line.find("(")!=-1:
print("branch")
branch_name, tag_name = extract_branch_and_tag(line)
print (branch_name, tag_name)
parse_commit(line)
def parse_commit(line):
out = "commit "
# identify the commit message
commit= line.split("*")[1]
commit = commit.strip()
# Debug
#print(commit)
start_index = commit.index(" ")
end_index = len(commit)
commit_message = commit[start_index+1:end_index]
#print(commit_message)
out = out + "id: " + wrap_in_quotes(commit_message)
print_with_indent(out,2)
def extract_branch_and_tag(line):
# Extract the branch name from the string
start_index = line.index("(")
end_index = line.index(")")
branch_name = line[start_index+1:end_index]
# Extract the tag name from the string
tag_name = line.split()[1]
return branch_name, tag_name
def wrap_in_quotes(text):
# Wrap the text in quotes
return '"' + text + '"'
def print_with_indent(text, indent):
# Print the text with the specified indent
print(' ' * indent + text)
def main():
# Set the path to the current directory
repo_path = os.getcwd()
# Get the list of commits, tags and branches
get_git_history(repo_path)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment