|
1 | 1 | import re |
2 | 2 | import subprocess |
3 | 3 | from collections.abc import Iterator |
| 4 | +from dataclasses import dataclass |
4 | 5 | from pathlib import Path |
5 | 6 | from urllib import parse |
6 | 7 |
|
|
11 | 12 | GIT_EXT_RE = re.compile(r"\.git$", re.IGNORECASE) |
12 | 13 | # Git records checked-out local branches in worktree listings as this ref prefix. |
13 | 14 | LOCAL_BRANCH_REF_PREFIX = "refs/heads/" |
| 15 | +# Tree modes that hold regular file content: 100644 is plain and 100755 is executable. |
| 16 | +# Symlinks are recorded as 120000 and subtrees as 040000, so neither carries file content. |
| 17 | +TREE_FILE_MODES = ("100644", "100755") |
14 | 18 |
|
15 | 19 |
|
16 | 20 | class GitUri: |
@@ -99,6 +103,46 @@ def matches(self, owner: str, name: str) -> bool: |
99 | 103 | return self.git_uri.matches(owner, name) |
100 | 104 |
|
101 | 105 |
|
| 106 | +@dataclass(frozen=True) |
| 107 | +class GitTreeEntry: |
| 108 | + """A single ``git ls-tree`` record describing one path in a Git tree.""" |
| 109 | + |
| 110 | + mode: str |
| 111 | + """The tree mode, such as ``100644``, ``100755``, or ``120000`` for a symlink.""" |
| 112 | + |
| 113 | + obj_type: str |
| 114 | + """The object type, which is ``blob`` for files and symlinks.""" |
| 115 | + |
| 116 | + sha: str |
| 117 | + """The object name of the blob holding the file content.""" |
| 118 | + |
| 119 | + path: str |
| 120 | + """The repository-relative POSIX path of the entry.""" |
| 121 | + |
| 122 | + @property |
| 123 | + def is_executable(self) -> bool: |
| 124 | + return self.mode == "100755" |
| 125 | + |
| 126 | + |
| 127 | +def _parse_tree_entries(output: str) -> list[GitTreeEntry]: |
| 128 | + """Parse NUL-delimited ``git ls-tree`` records into tree entries. |
| 129 | +
|
| 130 | + :param output: The raw ``git ls-tree -r -z`` output |
| 131 | + :return: The parsed entries, in the order Git emitted them |
| 132 | + """ |
| 133 | + entries = [] |
| 134 | + for record in output.split("\x00"): |
| 135 | + if not record: |
| 136 | + continue |
| 137 | + metadata, separator, path = record.partition("\t") |
| 138 | + fields = metadata.split(" ") |
| 139 | + if not separator or len(fields) != 3: |
| 140 | + raise CommandError(f"Unexpected git ls-tree record: {record!r}") |
| 141 | + mode, obj_type, sha = fields |
| 142 | + entries.append(GitTreeEntry(mode, obj_type, sha, path)) |
| 143 | + return entries |
| 144 | + |
| 145 | + |
102 | 146 | class GitCLI(SubprocessOperator): |
103 | 147 | command_name = "git" |
104 | 148 |
|
@@ -126,6 +170,39 @@ def ls_tree(self, branch_name: str, file_path: str) -> Iterator[str]: |
126 | 170 | def cat_file(self, branch_name: str, file_path: str) -> Iterator[str]: |
127 | 171 | yield from self.iter_output(["cat-file", "-p", f"{branch_name}:{file_path}"]) |
128 | 172 |
|
| 173 | + def ls_tree_entries(self, ref: str, path_prefix: str) -> list[GitTreeEntry]: |
| 174 | + """List the tree entries under a path in a Git ref. |
| 175 | +
|
| 176 | + :param ref: The ref whose tree to read, such as ``origin/main`` |
| 177 | + :param path_prefix: The repository-relative path to list |
| 178 | + :return: The entries recorded in the tree, in Git's order |
| 179 | + """ |
| 180 | + if ref.startswith("-"): |
| 181 | + raise CommandError(f"Invalid ref: {ref}") |
| 182 | + |
| 183 | + command = ["ls-tree", "-r", "-z", ref, "--", path_prefix] |
| 184 | + try: |
| 185 | + with self.run(command) as process: |
| 186 | + return _parse_tree_entries(process.stdout) |
| 187 | + except subprocess.CalledProcessError as e: |
| 188 | + raise CommandError(self._format_command_error(e.returncode, command)) from e |
| 189 | + |
| 190 | + def read_blob(self, sha: str) -> bytes: |
| 191 | + """Read the raw content of a blob object. |
| 192 | +
|
| 193 | + :param sha: The object name of the blob to read |
| 194 | + :return: The undecoded blob content, so binary files survive intact |
| 195 | + """ |
| 196 | + if sha.startswith("-"): |
| 197 | + raise CommandError(f"Invalid object name: {sha}") |
| 198 | + |
| 199 | + command = ["cat-file", "blob", sha] |
| 200 | + try: |
| 201 | + with self.run(command, text=False) as process: |
| 202 | + return process.stdout |
| 203 | + except subprocess.CalledProcessError as e: |
| 204 | + raise CommandError(self._format_command_error(e.returncode, command)) from e |
| 205 | + |
129 | 206 | def fetch(self, remote: str = "origin", refspec: str | None = None): |
130 | 207 | args = ["fetch", remote] |
131 | 208 | if refspec is not None: |
|
0 commit comments