gabriel / musehub public
refs.py python
56 lines 2.1 KB
Raw
sha256:bba4b69de173366aeb7d490d687ccffb6db9726374033addece89cf2b87642d8 docs: add production launch discovery report and infra laun… Sonnet 5 7 days ago
1 """On-disk ref I/O for server-side bare repos.
2
3 Every branch pointer is stored as a plain text file at:
4
5 <repo_root>/refs/heads/<branch>
6
7 The file contains the commit ID followed by a single newline. Writes are
8 atomic on POSIX: a temporary file is written beside the target and then
9 renamed into place. Readers either get the old value or the new value —
10 never a torn write.
11
12 Use ``write_ref`` and ``read_ref`` everywhere. Never construct ref paths
13 inline — use ``muse.core.paths.server_ref_path`` as the single source of truth.
14 """
15
16 import pathlib
17
18 from muse.core.paths import server_ref_path, server_heads_dir
19
20 def write_ref(repo_root: pathlib.Path, branch: str, commit_id: str) -> None:
21 """Write *commit_id* to the on-disk ref for *branch*, atomically.
22
23 Creates ``<repo_root>/refs/heads/`` (and any intermediate directories for
24 namespaced branches like ``feat/new-melody``) if they do not yet exist.
25
26 The write is atomic: a ``.tmp`` sibling is written first, then
27 ``os.rename``'d into place. On POSIX this is a single syscall and is
28 guaranteed to be atomic with respect to concurrent readers.
29
30 Args:
31 repo_root: Root of the server-side bare repo.
32 branch: Branch name, e.g. ``"main"`` or ``"feat/new-melody"``.
33 commit_id: Prefixed SHA-256 commit ID, e.g. ``"sha256:<64-hex>"``.
34 """
35 ref_file = server_ref_path(repo_root, branch)
36 ref_file.parent.mkdir(parents=True, exist_ok=True)
37
38 tmp_file = ref_file.with_suffix(".tmp")
39 tmp_file.write_text(f"{commit_id}\n", encoding="utf-8")
40 tmp_file.rename(ref_file)
41
42 def read_ref(repo_root: pathlib.Path, branch: str) -> str | None:
43 """Return the commit ID stored in the on-disk ref for *branch*, or ``None``.
44
45 Args:
46 repo_root: Root of the server-side bare repo.
47 branch: Branch name, e.g. ``"main"``.
48
49 Returns:
50 The commit ID string (stripped of the trailing newline), or ``None``
51 if no ref file exists for *branch*.
52 """
53 ref_file = server_ref_path(repo_root, branch)
54 if not ref_file.exists():
55 return None
56 return ref_file.read_text(encoding="utf-8").strip()
File History 2 commits
sha256:bba4b69de173366aeb7d490d687ccffb6db9726374033addece89cf2b87642d8 docs: add production launch discovery report and infra laun… Sonnet 5 7 days ago
sha256:eb0928124669c933c0ef852bea1ad0c56649cf21bd7e9663c3b51004771b0591 docs: add MuseHub cloud identity/AWS operating model and Go… Sonnet 5 patch 8 days ago