|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import dataclasses |
| 4 | +import git |
| 5 | +import random |
| 6 | +from pathlib import PurePosixPath |
| 7 | +import re |
| 8 | +import string |
| 9 | +import tempfile |
| 10 | +from typing import Match, Sequence |
| 11 | + |
| 12 | +from .backend import NewFileBackend |
| 13 | + |
| 14 | + |
| 15 | +def _enclosing_repo() -> git.Repo: |
| 16 | + return git.Repo(search_parent_directories=True) |
| 17 | + |
| 18 | + |
| 19 | +_random = random.Random() |
| 20 | + |
| 21 | +_SUFFIX_LENGTH = 8 |
| 22 | + |
| 23 | +_branch_name_pattern = re.compile(r"drafts/(.+)/(\w+)") |
| 24 | + |
| 25 | + |
| 26 | +@dataclasses.dataclass(frozen=True) |
| 27 | +class _DraftBranch: |
| 28 | + parent: str |
| 29 | + suffix: str |
| 30 | + repo: git.Repo |
| 31 | + |
| 32 | + def __str__(self) -> str: |
| 33 | + return f"drafts/{self.parent}/{self.suffix}" |
| 34 | + |
| 35 | + @classmethod |
| 36 | + def create(cls, repo: git.Repo) -> _DraftBranch: |
| 37 | + if not repo.active_branch: |
| 38 | + raise RuntimeError("No currently active branch") |
| 39 | + suffix = "".join( |
| 40 | + _random.choice(string.ascii_lowercase + string.digits) |
| 41 | + for _ in range(_SUFFIX_LENGTH) |
| 42 | + ) |
| 43 | + return cls(repo.active_branch.name, suffix, repo) |
| 44 | + |
| 45 | + @classmethod |
| 46 | + def active(cls, repo: git.Repo) -> _DraftBranch: |
| 47 | + match: Match | None = None |
| 48 | + if repo.active_branch: |
| 49 | + match = _branch_name_pattern.fullmatch(repo.active_branch.name) |
| 50 | + if not match: |
| 51 | + raise RuntimeError("Not currently on a draft branch") |
| 52 | + return _DraftBranch(match[1], match[2], repo) |
| 53 | + |
| 54 | + |
| 55 | +@dataclasses.dataclass(frozen=True) |
| 56 | +class _CommitNotes: |
| 57 | + # https://stackoverflow.com/a/40496777 |
| 58 | + pass |
| 59 | + |
| 60 | + |
| 61 | +def create_draft() -> None: |
| 62 | + repo = _enclosing_repo() |
| 63 | + draft_branch = _DraftBranch.create(repo) |
| 64 | + ref = repo.create_head(str(draft_branch)) |
| 65 | + repo.git.checkout(ref) |
| 66 | + |
| 67 | + |
| 68 | +class _Toolbox: |
| 69 | + def __init__(self, repo: git.Repo) -> None: |
| 70 | + self._repo = repo |
| 71 | + |
| 72 | + def list_files(self) -> Sequence[PurePosixPath]: |
| 73 | + # Show staged files. |
| 74 | + return self._repo.git.ls_files() |
| 75 | + |
| 76 | + def read_file(self, path: PurePosixPath) -> str: |
| 77 | + # Read the file from the index. |
| 78 | + return self._repo.git.show(f":{path}") |
| 79 | + |
| 80 | + def write_file(self, path: PurePosixPath, data: str) -> None: |
| 81 | + # Update the index without touching the worktree. |
| 82 | + # https://stackoverflow.com/a/25352119 |
| 83 | + with tempfile.NamedTemporaryFile(delete_on_close=False) as temp: |
| 84 | + temp.write(data.encode("utf8")) |
| 85 | + temp.close() |
| 86 | + sha = self._repo.git.hash_object("-w", "--path", path, temp.name) |
| 87 | + mode = 644 # TODO: Read from original file if it exists. |
| 88 | + self._repo.git.update_index( |
| 89 | + "--add", "--cacheinfo", f"{mode},{sha},{path}" |
| 90 | + ) |
| 91 | + |
| 92 | + |
| 93 | +def extend_draft(prompt: str) -> None: |
| 94 | + repo = _enclosing_repo() |
| 95 | + _ = _DraftBranch.active(repo) |
| 96 | + |
| 97 | + if repo.is_dirty(): |
| 98 | + repo.git.add(all=True) |
| 99 | + repo.index.commit("draft! sync") |
| 100 | + |
| 101 | + NewFileBackend().run(_Toolbox(repo)) |
| 102 | + |
| 103 | + |
| 104 | +def apply_draft(delete=False) -> None: |
| 105 | + repo = _enclosing_repo() |
| 106 | + branch = _DraftBranch.active(repo) |
| 107 | + |
| 108 | + # TODO: Check that parent has not moved. We could do this for example by |
| 109 | + # adding a note to the draft branch with the original branch's commit ref. |
| 110 | + |
| 111 | + # https://stackoverflow.com/a/15993574 |
| 112 | + repo.git.checkout("--detach") |
| 113 | + repo.git.reset("--soft", branch.parent) |
| 114 | + repo.git.checkout(branch.parent) |
| 115 | + |
| 116 | + if delete: |
| 117 | + repo.git.branch("-D", str(branch)) |
0 commit comments