Repository navigation
Expand file tree
/
Copy path_git.py
More file actions
751 lines (603 loc) · 23.2 KB
/
Copy path_git.py
File metadata and controls
751 lines (603 loc) · 23.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
"""Lightweight git metadata helpers for state detection."""
from __future__ import annotations
import logging
import os
import re
from pathlib import Path
from typing import NamedTuple
from urllib.parse import urlparse
logger = logging.getLogger(__name__)
_GIT_DIR_POINTER_PREFIX = "gitdir: "
"""Prefix used by worktree-style `.git` files to point at the real git dir."""
_GIT_HEAD_REF_PREFIX = "ref: "
"""Prefix used by `HEAD` when it points at a named ref instead of a commit."""
_GIT_REF_PREFIXES = ("refs/heads/", "refs/remotes/", "refs/tags/", "refs/")
"""Known git ref prefixes stripped when formatting a branch-like display name."""
_git_dir_cache: dict[str, Path] = {}
"""Positive-only cache of resolved git metadata directories keyed by lookup path."""
def _abbreviate_git_ref(ref: str) -> str:
"""Convert a full git ref into a short display name.
Args:
ref: Full git ref name from repository metadata.
Returns:
The abbreviated ref name suitable for display.
"""
for prefix in _GIT_REF_PREFIXES:
if ref.startswith(prefix):
return ref.removeprefix(prefix)
return ref
def _parse_git_dir_pointer(git_entry: Path) -> Path | None:
"""Resolve a `.git` file containing a `gitdir:` pointer.
Args:
git_entry: `.git` file to parse.
Returns:
The resolved git directory path, or `None` if the file is not a valid
gitdir pointer.
"""
try:
raw = git_entry.read_text(encoding="utf-8").strip()
except OSError:
logger.debug("Failed to read gitdir pointer from %s", git_entry, exc_info=True)
return None
if not raw.startswith(_GIT_DIR_POINTER_PREFIX):
return None
pointer = raw.removeprefix(_GIT_DIR_POINTER_PREFIX).strip()
if not pointer:
return None
git_dir = Path(pointer)
if not git_dir.is_absolute():
git_dir = git_entry.parent / git_dir
return git_dir.resolve(strict=False)
def _normalize_lookup_path(path: str | Path) -> Path:
"""Normalize a lookup path for git metadata discovery.
Args:
path: Directory or file path inside a repository.
Returns:
A normalized absolute path when possible, or the expanded path if full
resolution fails.
"""
try:
return Path(path).expanduser().resolve(strict=False)
except OSError:
return Path(path).expanduser()
def _find_git_dir_uncached(path: Path) -> Path | None:
"""Locate the effective git metadata directory without using caches.
Args:
path: Normalized directory or file path inside a repository.
Returns:
The git metadata directory for the repository containing `path`, or
`None` when no repository can be identified.
"""
current = path
if not current.is_dir():
current = current.parent
for directory in (current, *current.parents):
git_entry = directory / ".git"
if git_entry.is_dir():
return git_entry
if git_entry.is_file():
git_dir = _parse_git_dir_pointer(git_entry)
if git_dir is not None and git_dir.is_dir():
return git_dir
return None
return None
def find_git_dir(path: str | Path) -> Path | None:
"""Locate the effective git metadata directory for a path.
Args:
path: Directory or file path inside a repository.
Returns:
The git metadata directory for the repository containing `path`, or
`None` when no repository can be identified.
"""
current = _normalize_lookup_path(path)
key = str(current)
cached = _git_dir_cache.get(key)
if cached is not None:
return cached
git_dir = _find_git_dir_uncached(current)
if git_dir is not None:
_git_dir_cache[key] = git_dir
return git_dir
def find_git_root(path: str | Path) -> Path | None:
"""Locate the repository root for a path.
Args:
path: Directory or file path inside a repository.
Returns:
The repository root containing `path`, or `None` when no repository can
be identified.
"""
current = _normalize_lookup_path(path)
if not current.is_dir():
current = current.parent
for directory in (current, *current.parents):
git_entry = directory / ".git"
if git_entry.is_dir():
return directory
if git_entry.is_file():
git_dir = _parse_git_dir_pointer(git_entry)
if git_dir is not None and git_dir.is_dir():
return directory
return None
return None
def _read_single_git_metadata_line(path: Path) -> str | None:
"""Read one non-empty line from a regular Git metadata file.
Args:
path: Metadata file to read.
Returns:
The stripped line, or `None` when the file is unsafe or malformed.
"""
if path.is_symlink() or not path.is_file():
return None
try:
raw = path.read_text(encoding="utf-8")
except (OSError, UnicodeError):
logger.debug("Failed to read Git metadata from %s", path, exc_info=True)
return None
lines = raw.splitlines()
if len(lines) != 1:
return None
value = lines[0].strip()
if not value or "\x00" in value:
return None
return value
def _resolve_git_metadata_path(path: Path, *, relative_to: Path) -> Path | None:
"""Resolve a path stored in a single-line Git metadata file.
Args:
path: Metadata file containing the path.
relative_to: Base directory for a relative stored path.
Returns:
The strictly resolved path, or `None` when it cannot be trusted.
"""
value = _read_single_git_metadata_line(path)
if value is None:
return None
candidate = Path(value)
if not candidate.is_absolute():
candidate = relative_to / candidate
try:
return candidate.resolve(strict=True)
except (OSError, RuntimeError, ValueError):
logger.debug("Failed to resolve Git metadata path from %s", path, exc_info=True)
return None
def _normalize_git_metadata_path(path: Path, *, relative_to: Path) -> Path | None:
"""Normalize a stored path without following its target.
Args:
path: Metadata file containing the path.
relative_to: Base directory for a relative stored path.
Returns:
The absolute lexical path, or `None` when the metadata is malformed.
"""
value = _read_single_git_metadata_line(path)
if value is None:
return None
candidate = Path(value)
if not candidate.is_absolute():
candidate = relative_to / candidate
try:
return Path(os.path.abspath(candidate)) # noqa: PTH100 # do not follow links
except (OSError, RuntimeError, ValueError):
logger.debug(
"Failed to normalize Git metadata path from %s", path, exc_info=True
)
return None
def _parse_trusted_git_dir_pointer(git_entry: Path) -> Path | None:
"""Resolve a strict worktree `.git` pointer for a trust decision.
Args:
git_entry: Regular `.git` file to parse.
Returns:
The canonical administration directory, or `None` for an invalid pointer.
"""
value = _read_single_git_metadata_line(git_entry)
if value is None or not value.startswith(_GIT_DIR_POINTER_PREFIX):
return None
pointer = value.removeprefix(_GIT_DIR_POINTER_PREFIX).strip()
if not pointer:
return None
candidate = Path(pointer)
if not candidate.is_absolute():
candidate = git_entry.parent / candidate
try:
git_dir = candidate.resolve(strict=True)
except (OSError, RuntimeError, ValueError):
logger.debug(
"Failed to resolve trusted gitdir pointer %s", git_entry, exc_info=True
)
return None
return git_dir if git_dir.is_dir() else None
def _is_valid_git_common_dir(path: Path) -> bool:
"""Return whether `path` has the required shared-repository metadata."""
return (
path.is_dir()
and (path / "HEAD").is_file()
and (path / "config").is_file()
and (path / "objects").is_dir()
and (path / "refs").is_dir()
)
def find_git_common_dir(path: str | Path) -> Path | None:
"""Resolve a validated identity shared by one repository's Git worktrees.
A normal checkout uses its in-tree `.git` directory. A linked worktree is
accepted only when its administration directory lives directly under the
common repository's `worktrees` directory and its `gitdir` file points back
to the current worktree's `.git` file, and both the shared common directory
and the worktree's own administration directory carry valid repository
metadata (`_is_valid_git_common_dir` plus the worktree's own `HEAD`). These
checks prevent a forged pointer or directory link from borrowing another
repository's identity.
Args:
path: Exact working-tree root or persisted common-directory path.
Returns:
The canonical Git common directory, or `None` when the metadata cannot
be validated confidently.
"""
try:
root = _normalize_lookup_path(path)
if not root.is_dir():
return None
if _is_valid_git_common_dir(root):
return root
root = root.resolve(strict=True)
git_entry = root / ".git"
if git_entry.is_symlink():
return None
if git_entry.is_dir():
common_dir = git_entry.resolve(strict=True)
if common_dir != git_entry or not _is_valid_git_common_dir(common_dir):
return None
return common_dir
if not git_entry.is_file():
return None
git_dir = _parse_trusted_git_dir_pointer(git_entry)
if git_dir is None:
return None
common_dir = _resolve_git_metadata_path(
git_dir / "commondir", relative_to=git_dir
)
if common_dir is None or git_dir.parent != common_dir / "worktrees":
return None
if not _is_valid_git_common_dir(common_dir):
return None
if not (git_dir / "HEAD").is_file():
return None
backlink = _normalize_git_metadata_path(git_dir / "gitdir", relative_to=git_dir)
if backlink is None or backlink != git_entry:
return None
except (OSError, RuntimeError, ValueError):
logger.debug(
"Failed to validate Git common directory for %s", path, exc_info=True
)
return None
return common_dir
def _read_git_config_value(raw: str, section: str, key: str) -> str | None:
"""Read one value from a Git config section.
Returns:
The configured value, or `None` when it is absent.
"""
in_section = False
expected = section.replace(" ", "").lower()
for line in raw.splitlines():
stripped = line.strip()
if stripped.startswith("[") and stripped.endswith("]"):
in_section = stripped.replace(" ", "").lower() == expected
continue
name, separator, value = stripped.partition("=")
if in_section and separator and name.strip().lower() == key:
return value.strip() or None
return None
def _submodule_git_dir(root: Path, parent_config_dir: Path) -> Path | None:
"""Resolve a validated submodule administration directory.
Returns:
The administration directory, or `None` when validation fails.
"""
git_entry = root / ".git"
if git_entry.is_symlink() or not git_entry.is_file():
return None
git_dir = _parse_trusted_git_dir_pointer(git_entry)
if git_dir is None or not _is_valid_git_common_dir(git_dir):
return None
try:
git_dir.relative_to(parent_config_dir / "modules")
raw = (git_dir / "config").read_text(encoding="utf-8")
worktree = _read_git_config_value(raw, "[core]", "worktree")
if worktree is None:
return None
candidate = Path(worktree)
if not candidate.is_absolute():
candidate = git_dir / candidate
return git_dir if candidate.resolve(strict=True) == root else None
except (OSError, RuntimeError, ValueError):
logger.debug("Failed to validate submodule Git metadata")
return None
def _find_git_config_dir(path: str | Path) -> Path | None:
"""Locate a validated repository config directory.
Returns:
The config directory, or `None` when validation fails.
"""
root = find_git_root(path)
if root is None:
return None
common_dir = find_git_common_dir(root)
if common_dir is not None:
return common_dir
parent_config_dir = _find_git_config_dir(root.parent)
return (
_submodule_git_dir(root, parent_config_dir)
if parent_config_dir is not None
else None
)
def read_git_branch_from_filesystem(path: str | Path) -> str | None:
"""Read the current git branch from repository metadata.
Args:
path: Directory or file path inside a repository.
Returns:
The abbreviated branch name, `HEAD` for detached HEAD, an empty string
when `path` is not inside a git repository, or `None` when metadata
exists but cannot be parsed confidently.
"""
git_dir = find_git_dir(path)
if git_dir is None:
return ""
head_path = git_dir / "HEAD"
try:
head = head_path.read_text(encoding="utf-8").strip()
except FileNotFoundError:
logger.debug("Git HEAD file not found in %s", git_dir)
return None
except OSError:
logger.debug("Failed to read git HEAD from %s", git_dir, exc_info=True)
return None
if not head:
return ""
if head.startswith(_GIT_HEAD_REF_PREFIX):
ref = head.removeprefix(_GIT_HEAD_REF_PREFIX).strip()
return _abbreviate_git_ref(ref) if ref else None
return "HEAD"
def read_git_branch_via_subprocess(path: str | Path) -> str:
"""Fall back to `git rev-parse` for unusual repository layouts.
Args:
path: Directory or file path inside a repository.
Returns:
The branch name reported by git, or an empty string on failure.
"""
import subprocess # noqa: S404 # stdlib subprocess fallback
try:
result = subprocess.run(
["git", "rev-parse", "--abbrev-ref", "HEAD"], # noqa: S607
capture_output=True,
text=True,
timeout=2,
check=False,
cwd=path,
)
if result.returncode == 0:
return result.stdout.strip()
except FileNotFoundError:
pass # git not installed
except subprocess.TimeoutExpired:
logger.debug("Git branch detection timed out")
except OSError:
logger.debug("Git branch detection failed", exc_info=True)
return ""
def resolve_git_branch(path: str | Path) -> str:
"""Resolve the current git branch with a filesystem-first strategy.
Args:
path: Directory or file path inside a repository.
Returns:
The current branch name, `HEAD` for detached HEAD, or an empty string
when no branch can be determined.
"""
branch = read_git_branch_from_filesystem(path)
if branch is not None:
return branch
return read_git_branch_via_subprocess(path)
_GIT_SHA_RE = re.compile(r"\A[0-9a-f]{40}(?:[0-9a-f]{24})?\Z")
"""Matches a full 40-char SHA-1 (or 64-char SHA-256) git object id."""
def read_git_commit_sha_from_filesystem(path: str | Path) -> str | None:
"""Read the current `HEAD` commit SHA from repository metadata.
Resolves a symbolic `HEAD` (`ref: refs/heads/<branch>`) by reading the
loose ref file, then `packed-refs`. A detached `HEAD` already contains the
raw SHA.
Args:
path: Directory or file path inside a repository.
Returns:
The full commit SHA (40-char SHA-1 or 64-char SHA-256), an empty string
when `path` is not inside a git repository, or `None` when metadata
exists but cannot be resolved.
"""
git_dir = find_git_dir(path)
if git_dir is None:
return ""
head_path = git_dir / "HEAD"
try:
head = head_path.read_text(encoding="utf-8").strip()
except FileNotFoundError:
logger.debug("Git HEAD file not found in %s", git_dir)
return None
except OSError:
logger.debug("Failed to read git HEAD from %s", git_dir, exc_info=True)
return None
if not head:
return None
if not head.startswith(_GIT_HEAD_REF_PREFIX):
# Detached HEAD: the file holds the commit SHA directly.
return head if _GIT_SHA_RE.match(head) else None
ref = head.removeprefix(_GIT_HEAD_REF_PREFIX).strip()
if not ref:
return None
loose_ref = git_dir / Path(ref)
try:
sha = loose_ref.read_text(encoding="utf-8").strip()
if _GIT_SHA_RE.match(sha):
return sha
except FileNotFoundError:
pass # ref may be packed
except OSError:
logger.debug("Failed to read loose ref %s", loose_ref, exc_info=True)
return None
return _read_packed_ref(git_dir, ref)
def _read_packed_ref(git_dir: Path, ref: str) -> str | None:
"""Resolve a ref to its SHA from `packed-refs`.
Args:
git_dir: Resolved git metadata directory.
ref: Full ref name (e.g. `refs/heads/main`).
Returns:
The full commit SHA, or `None` when the ref is absent or unreadable.
"""
try:
packed = (git_dir / "packed-refs").read_text(encoding="utf-8")
except FileNotFoundError:
return None
except OSError:
logger.debug("Failed to read packed-refs in %s", git_dir, exc_info=True)
return None
for line in packed.splitlines():
if not line or line.startswith(("#", "^")):
continue
sha, _, name = line.partition(" ")
if name.strip() == ref and _GIT_SHA_RE.match(sha):
return sha
return None
def read_git_commit_sha_via_subprocess(path: str | Path) -> str:
"""Fall back to `git rev-parse HEAD` for unusual repository layouts.
Args:
path: Directory or file path inside a repository.
Returns:
The full commit SHA reported by git, or an empty string on failure.
"""
import subprocess # noqa: S404 # stdlib subprocess fallback
try:
result = subprocess.run(
["git", "rev-parse", "HEAD"], # noqa: S607
capture_output=True,
text=True,
timeout=2,
check=False,
cwd=path,
)
if result.returncode == 0:
return result.stdout.strip()
except FileNotFoundError:
pass # git not installed
except subprocess.TimeoutExpired:
logger.debug("Git commit detection timed out")
except OSError:
logger.debug("Git commit detection failed", exc_info=True)
return ""
def resolve_git_commit_sha(path: str | Path) -> str:
"""Resolve the current `HEAD` commit SHA, filesystem-first.
Args:
path: Directory or file path inside a repository.
Returns:
The full commit SHA, or an empty string when none can be determined.
"""
sha = read_git_commit_sha_from_filesystem(path)
if sha is not None:
return sha
return read_git_commit_sha_via_subprocess(path)
def read_git_remote_url_from_filesystem(path: str | Path) -> str | None:
"""Read the `origin` remote URL from the repository `config` file.
Args:
path: Directory or file path inside a repository.
Returns:
The `origin` remote URL, an empty string when `path` is not inside a
git repository, or `None` when no `origin` URL is configured.
"""
config_dir = _find_git_config_dir(path)
if config_dir is None:
return ""
try:
raw = (config_dir / "config").read_text(encoding="utf-8")
except FileNotFoundError:
return None
except OSError:
logger.debug("Failed to read Git config")
return None
return _read_git_config_value(raw, '[remote "origin"]', "url")
def read_git_remote_url_via_subprocess(path: str | Path) -> str:
"""Fall back to `git config --get remote.origin.url`.
Args:
path: Directory or file path inside a repository.
Returns:
The `origin` remote URL reported by git, or an empty string on failure.
"""
import subprocess # noqa: S404 # stdlib subprocess fallback
try:
result = subprocess.run(
["git", "config", "--get", "remote.origin.url"], # noqa: S607
capture_output=True,
text=True,
timeout=2,
check=False,
cwd=path,
)
if result.returncode == 0:
return result.stdout.strip()
except FileNotFoundError:
pass # git not installed
except subprocess.TimeoutExpired:
logger.debug("Git remote detection timed out")
except OSError:
logger.debug("Git remote detection failed", exc_info=True)
return ""
def resolve_git_remote_url(path: str | Path) -> str:
"""Resolve the `origin` remote URL, filesystem-first.
Args:
path: Directory or file path inside a repository.
Returns:
The `origin` remote URL, or an empty string when none can be determined.
"""
url = read_git_remote_url_from_filesystem(path)
if url is not None:
return url
return read_git_remote_url_via_subprocess(path)
_REPO_PROVIDERS: dict[str, str] = {
"github.com": "github",
"gitlab.com": "gitlab",
"bitbucket.org": "bitbucket",
}
"""Maps a known git host to its `repository_provider` slug."""
class RepositoryMetadata(NamedTuple):
"""Parsed `origin` remote attribution for coding-agent-v1 traces.
A `NamedTuple` so callers can still unpack positionally or index, while the
field names keep the slot order from being load-bearing at every call site.
"""
url: str
"""Normalized `https://<host>/<org>/<repo>` URL (credentials stripped)."""
provider: str
"""Provider slug: `github`, `gitlab`, `bitbucket`, or `other`."""
name: str
"""`org/repo` name (may be nested, e.g. `group/subgroup/project`)."""
def parse_repository_metadata(remote_url: str) -> RepositoryMetadata | None:
"""Derive repository attribution from an `origin` remote URL.
Handles both HTTPS (`https://github.com/org/repo.git`) and scp-style SSH
(`git@github.com:org/repo.git`) remotes, normalizing the URL to its
canonical `https://<host>/<org>/<repo>` form and stripping any embedded
credentials.
Args:
remote_url: The raw `origin` remote URL.
Returns:
A `RepositoryMetadata` of the normalized URL, provider slug
(`github`/`gitlab`/`bitbucket`/`other`), and `org/repo` name,
or `None` when the URL cannot be parsed.
"""
url = (remote_url or "").strip()
if not url:
return None
host: str
repo_path: str
if "://" in url:
parsed = urlparse(url)
host = (parsed.hostname or "").lower()
repo_path = parsed.path.lstrip("/")
else:
# scp-style: [user@]host:org/repo(.git)
userhost, sep, repo_path = url.partition(":")
if not sep:
return None
host = userhost.rsplit("@", 1)[-1].lower()
repo_path = repo_path.lstrip("/")
repo_path = repo_path.strip("/").removesuffix(".git").strip("/")
if not host or not repo_path:
return None
provider = _REPO_PROVIDERS.get(host, "other")
normalized_url = f"https://{host}/{repo_path}"
return RepositoryMetadata(normalized_url, provider, repo_path)