type annotation improvements

This commit is contained in:
Jan Petykiewicz 2024-07-29 21:59:02 -07:00
parent 7a871e4e91
commit 9b329f3b8b

View File

@ -3,7 +3,7 @@
Git snapshotting tool Git snapshotting tool
""" """
from typing import Sequence, Union, Optional from typing import Sequence
import subprocess import subprocess
import tempfile import tempfile
import datetime import datetime
@ -11,7 +11,7 @@ import argparse
from itertools import chain from itertools import chain
def _run(command: Union[str, Sequence[str]], **kwargs) -> str: def _run(command: str | Sequence[str], **kwargs) -> str:
""" """
Wrapper for `subprocess.run()`: Wrapper for `subprocess.run()`:
@ -34,7 +34,7 @@ def _run(command: Union[str, Sequence[str]], **kwargs) -> str:
return result.stdout.decode().strip() return result.stdout.decode().strip()
def get_latest_commit(short: bool = True, cwd: Optional[str] = None) -> str: def get_latest_commit(short: bool = True, cwd: str | None = None) -> str:
""" """
Get the most recent commit's hash. Get the most recent commit's hash.
This includes non-lethe commits. This includes non-lethe commits.
@ -43,14 +43,14 @@ def get_latest_commit(short: bool = True, cwd: Optional[str] = None) -> str:
return _run(f'git log --all -1 --format=%{fmt}', cwd=cwd) return _run(f'git log --all -1 --format=%{fmt}', cwd=cwd)
def shorten_hash(sha: str, cwd: Optional[str] = None) -> str: def shorten_hash(sha: str, cwd: str | None = None) -> str:
""" """
Get the short version of a hash Get the short version of a hash
""" """
return _run(f'git rev-parse --short {sha}', cwd=cwd) return _run(f'git rev-parse --short {sha}', cwd=cwd)
def get_root(cwd: Optional[str] = None) -> str: def get_root(cwd: str | None = None) -> str:
""" """
Get the root directory of a git repository Get the root directory of a git repository
""" """
@ -60,7 +60,7 @@ def get_root(cwd: Optional[str] = None) -> str:
return root return root
def get_obj(ref: str, cwd: Optional[str] = None) -> str: def get_obj(ref: str, cwd: str | None = None) -> str:
""" """
Transform a ref into its corresponding hash using git-rev-parse Transform a ref into its corresponding hash using git-rev-parse
""" """
@ -68,24 +68,25 @@ def get_obj(ref: str, cwd: Optional[str] = None) -> str:
return sha return sha
def get_commit(ref: str, cwd: Optional[str] = None) -> str: def get_commit(ref: str, cwd: str | None = None) -> str:
""" """
Transform a ref to a commit into its corresponding hash using git-rev-parse Transform a ref to a commit into its corresponding hash using git-rev-parse
""" """
return get_obj(ref, cwd=cwd) return get_obj(ref, cwd=cwd)
def get_tree(ref: str, cwd: Optional[str] = None) -> str: def get_tree(ref: str, cwd: str | None = None) -> str:
""" """
Take a ref to a commit, and return the hash of the tree it points to Take a ref to a commit, and return the hash of the tree it points to
""" """
return get_obj(ref + ':', cwd=cwd) return get_obj(ref + ':', cwd=cwd)
def commit_tree(tree: str, def commit_tree(
tree: str,
parents: Sequence[str], parents: Sequence[str],
message: Optional[str] = None, message: str | None = None,
cwd: Optional[str] = None, cwd: str | None = None,
) -> str: ) -> str:
""" """
Create a commit pointing to the given tree, with the specified parent commits and message. Create a commit pointing to the given tree, with the specified parent commits and message.
@ -99,12 +100,13 @@ def commit_tree(tree: str,
return commit return commit
def update_ref(target_ref: str, def update_ref(
target_ref: str,
target_commit: str, target_commit: str,
old_commit: Optional[str] = None, old_commit: str | None = None,
*, *,
message: str = 'new snapshot', message: str = 'new snapshot',
cwd: Optional[str] = None, cwd: str | None = None,
) -> str: ) -> str:
""" """
Update `target_ref` to point to `target_commit`, optionally verifying that Update `target_ref` to point to `target_commit`, optionally verifying that
@ -118,11 +120,12 @@ def update_ref(target_ref: str,
return result_ref return result_ref
def push_ref(remote: str = 'origin', def push_ref(
remote: str = 'origin',
target_ref: str = 'refs/lethe/LATEST', target_ref: str = 'refs/lethe/LATEST',
remote_ref: Optional[str] = None, remote_ref: str | None = None,
*, *,
cwd: Optional[str] = None, cwd: str | None = None,
) -> str: ) -> str:
""" """
Push `target_ref` to `remote` as `remote_ref`. Push `target_ref` to `remote` as `remote_ref`.
@ -142,11 +145,12 @@ def push_ref(remote: str = 'origin',
return _run(['git', 'push', '--force', remote, target_ref + ':' + remote_ref], cwd=cwd) return _run(['git', 'push', '--force', remote, target_ref + ':' + remote_ref], cwd=cwd)
def fetch_ref(remote: str = 'origin', def fetch_ref(
remote: str = 'origin',
remote_ref: str = 'refs/lethe/LATEST', remote_ref: str = 'refs/lethe/LATEST',
target_ref: Optional[str] = None, target_ref: str | None = None,
*, *,
cwd: Optional[str] = None, cwd: str | None = None,
) -> str: ) -> str:
""" """
Fetch `remote_ref` from `remote` as `target_ref`. Fetch `remote_ref` from `remote` as `target_ref`.
@ -166,9 +170,10 @@ def fetch_ref(remote: str = 'origin',
return _run(['git', 'fetch', '--force', remote, remote_ref + ':' + target_ref], cwd=cwd) return _run(['git', 'fetch', '--force', remote, remote_ref + ':' + target_ref], cwd=cwd)
def deref_symref(ref: str, def deref_symref(
ref: str,
*, *,
cwd: Optional[str] = None, cwd: str | None = None,
) -> str: ) -> str:
""" """
Dereference a symbolic ref Dereference a symbolic ref
@ -176,9 +181,10 @@ def deref_symref(ref: str,
return _run(['git', 'symbolic-ref', '--quiet', ref], cwd=cwd) return _run(['git', 'symbolic-ref', '--quiet', ref], cwd=cwd)
def find_merge_base(commits: Sequence[str], def find_merge_base(
commits: Sequence[str],
*, *,
cwd: Optional[str] = None, cwd: str | None = None,
) -> str: ) -> str:
""" """
Find the "best common ancestor" commit. Find the "best common ancestor" commit.
@ -200,7 +206,7 @@ def find_merge_base(commits: Sequence[str],
return base return base
def snap_tree(*, cwd: Optional[str] = None) -> str: def snap_tree(*, cwd: str | None = None) -> str:
""" """
Create a new tree, consisting of all non-ignored files in the repository. Create a new tree, consisting of all non-ignored files in the repository.
Return the hash of the tree. Return the hash of the tree.
@ -214,11 +220,12 @@ def snap_tree(*, cwd: Optional[str] = None) -> str:
return tree return tree
def snap_ref(parent_refs: Sequence[str], def snap_ref(
parent_refs: Sequence[str],
target_refs: Sequence[str], target_refs: Sequence[str],
message: Optional[str] = None, message: str | None = None,
*, *,
cwd: Optional[str] = None, cwd: str | None = None,
) -> str: ) -> str:
""" """
`message` is used as the commit message. `message` is used as the commit message.
@ -227,7 +234,7 @@ def snap_ref(parent_refs: Sequence[str],
parent_commits = [c for c in [get_commit(p, cwd=cwd) for p in parent_refs] if c] parent_commits = [c for c in [get_commit(p, cwd=cwd) for p in parent_refs] if c]
old_commits = [get_commit(t, cwd=cwd) for t in target_refs] old_commits = [get_commit(t, cwd=cwd) for t in target_refs]
commit = commit_tree(new_tree, set(parent_commits), message, cwd=cwd) commit = commit_tree(new_tree, list(set(parent_commits)), message, cwd=cwd)
for target_ref, old_commit in zip(target_refs, old_commits): for target_ref, old_commit in zip(target_refs, old_commits):
# update ref to point to commit, or create new ref # update ref to point to commit, or create new ref
@ -237,11 +244,12 @@ def snap_ref(parent_refs: Sequence[str],
return commit return commit
def snap(parent_refs: Optional[Sequence[str]] = None, def snap(
target_refs: Optional[Sequence[str]] = None, parent_refs: Sequence[str] | None = None,
message: Optional[str] = None, target_refs: Sequence[str] | None = None,
message: str | None = None,
*, *,
cwd: Optional[str] = None, cwd: str | None = None,
) -> str: ) -> str:
""" """
Create a new commit, containing all non-ignored files. Create a new commit, containing all non-ignored files.