mirror of
https://gitlab.com/80486DX2-66/gists
synced 2025-05-31 08:31:41 +05:30
add python-programming/git_commit_simulator.py
Extend .gitignore for Python
This commit is contained in:
103
python-programming/git_commit_simulator.py
Normal file
103
python-programming/git_commit_simulator.py
Normal file
@ -0,0 +1,103 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
from datetime import datetime
|
||||
from difflib import unified_diff
|
||||
from typing import List, Union
|
||||
import random
|
||||
import string
|
||||
|
||||
FAKE_HASH_ALPHABET = string.digits + string.ascii_lowercase[:6]
|
||||
|
||||
def diff_strings(old: str, new: str) -> str:
|
||||
old += "\n" if old else ""
|
||||
new += "\n" if new else ""
|
||||
return "".join(list(unified_diff(old.splitlines(keepends=True),
|
||||
new.splitlines(keepends=True)))[2:])[:-1]
|
||||
|
||||
def fake_hash(length: int) -> str:
|
||||
return "".join([random.choice(FAKE_HASH_ALPHABET) for i in range(length)])
|
||||
|
||||
def styled_date(dt: datetime = None) -> str:
|
||||
if not dt:
|
||||
dt = datetime.now()
|
||||
|
||||
# res = datetime.strftime(dt, "%a %b %d %H:%M:%S %Y")
|
||||
res = dt.ctime()
|
||||
|
||||
offset = dt.astimezone().utcoffset().seconds
|
||||
positive = offset >= 0
|
||||
hours, minutes = offset // 3600, offset % 3600
|
||||
hours, minutes = str(hours).zfill(2), str(minutes).zfill(2)
|
||||
sign = "+" if positive else "-"
|
||||
res += " " + sign + hours + minutes
|
||||
return res
|
||||
|
||||
def commit(*, commit_message: Union[List[str], str], branch_name: str = "main",
|
||||
old_content: str = None, new_content: str, file: str, name: str,
|
||||
email: str, mode: str = "100644", dt: datetime = None) -> str:
|
||||
if isinstance(commit_message, str):
|
||||
commit_message = [commit_message + "\n"]
|
||||
commit_message = [" " * 4 + line if line else "" for line in commit_message]
|
||||
if len(commit_message) > 1:
|
||||
commit_message.insert(1, "")
|
||||
commit_message = "\n".join(commit_message)
|
||||
|
||||
new_file = False
|
||||
if old_content is None:
|
||||
new_file = True
|
||||
old_content = ""
|
||||
|
||||
first_file = "/dev/null" if new_file else file
|
||||
second_file = "b/" + file
|
||||
|
||||
first_hash = "0" * 7 if new_file else fake_hash(7)
|
||||
|
||||
index_mode_lines = f"index {first_hash}..{fake_hash(7)}"
|
||||
if new_file:
|
||||
index_mode_lines = f"""\
|
||||
new file mode {mode}
|
||||
{index_mode_lines}"""
|
||||
else:
|
||||
index_mode_lines += f" {mode}"
|
||||
|
||||
diff = diff_strings(old_content, new_content)
|
||||
|
||||
return f"""\
|
||||
commit {fake_hash(40)} (HEAD -> {branch_name})
|
||||
Author: {name} <{email}>
|
||||
Date: {styled_date(dt)}
|
||||
|
||||
{commit_message}
|
||||
|
||||
diff --git {"a/" + file} {second_file}
|
||||
{index_mode_lines}
|
||||
--- {first_file}
|
||||
+++ {second_file}
|
||||
{diff}"""
|
||||
|
||||
example = lambda: commit( \
|
||||
commit_message=["Hello, world!", "Greetings :-)", "Lorem ipsum"], \
|
||||
# old_content="henlo", \
|
||||
new_content="hello", \
|
||||
file="message.txt", \
|
||||
name="Intel A80486DX2-66", \
|
||||
email="larry.holst@disroot.org")
|
||||
|
||||
if __name__ == "__main__":
|
||||
from sys import argv
|
||||
|
||||
if "--example" in argv:
|
||||
print("Example output:")
|
||||
print(example())
|
||||
raise SystemExit
|
||||
|
||||
print("This script is not interactive. Please run Python 3 interpreter,\n"
|
||||
"import this file, and use function git_commit_simulator.commit()")
|
||||
print()
|
||||
print("""Function prototype:
|
||||
def commit(*, commit_message: Union[List[str], str], branch_name: str = "main",
|
||||
old_content: str = None, new_content: str, file: str, name: str,
|
||||
email: str, mode: str = "100644", dt: datetime = None) -> str:""")
|
||||
print()
|
||||
print("Run with --example to get an example output")
|
||||
|
Reference in New Issue
Block a user