1
0
mirror of https://gitlab.com/80486DX2-66/gists synced 2024-11-10 15:52:04 +05:30

add python-programming/git_commit_simulator.py

Extend .gitignore for Python
This commit is contained in:
Intel A80486DX2-66 2023-12-31 14:25:20 +03:00
parent 6bb7482c3a
commit 93ed0c917a
Signed by: 80486DX2-66
GPG Key ID: 83631EF27054609B
2 changed files with 265 additions and 0 deletions

162
.gitignore vendored
View File

@ -51,3 +51,165 @@ modules.order
Module.symvers
Mkfile.old
dkms.conf
# ---> Python
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
.pybuilder/
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock
# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# Cython debug symbols
cython_debug/
# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

View 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")