|
28 | 28 | from griffe.extensions import Extensions |
29 | 29 |
|
30 | 30 |
|
31 | | -def _assert_git_repo(repo: str) -> None: |
| 31 | +WORKTREE_PREFIX = "griffe-worktree-" |
| 32 | + |
| 33 | + |
| 34 | +def _assert_git_repo(repo: str | Path) -> None: |
32 | 35 | if not shutil.which("git"): |
33 | 36 | raise RuntimeError("Could not find git executable. Please install git.") |
34 | 37 |
|
35 | 38 | try: |
36 | 39 | subprocess.run( |
37 | | - ["git", "-C", repo, "rev-parse", "--is-inside-work-tree"], |
| 40 | + ["git", "-C", str(repo), "rev-parse", "--is-inside-work-tree"], |
38 | 41 | check=True, |
39 | 42 | stdout=subprocess.DEVNULL, |
40 | 43 | stderr=subprocess.DEVNULL, |
41 | 44 | ) |
42 | 45 | except subprocess.CalledProcessError as err: |
43 | | - raise OSError(f"Not a git repository: {repo!r}") from err |
| 46 | + raise OSError(f"Not a git repository: {repo}") from err |
44 | 47 |
|
45 | 48 |
|
46 | 49 | def _get_latest_tag(path: str | Path) -> str: |
@@ -88,24 +91,24 @@ def tmp_worktree(repo: str | Path = ".", ref: str = "HEAD") -> Iterator[Path]: |
88 | 91 | OSError: If `repo` is not a valid `.git` repository |
89 | 92 | RuntimeError: If the `git` executable is unavailable, or if it cannot create a worktree |
90 | 93 | """ |
91 | | - repo = str(repo) |
92 | 94 | _assert_git_repo(repo) |
93 | | - with TemporaryDirectory(prefix="griffe-worktree-") as td: |
94 | | - uid = f"griffe_{ref}" |
95 | | - target = os.path.join(td, uid) |
96 | | - retval = subprocess.run( |
97 | | - ["git", "-C", repo, "worktree", "add", "-b", uid, target, ref], |
| 95 | + repo_name = Path(repo).resolve().name |
| 96 | + with TemporaryDirectory(prefix=f"{WORKTREE_PREFIX}{repo_name}-{ref}-") as tmp_dir: |
| 97 | + branch = f"griffe_{ref}" |
| 98 | + location = os.path.join(tmp_dir, branch) |
| 99 | + process = subprocess.run( |
| 100 | + ["git", "-C", repo, "worktree", "add", "-b", branch, location, ref], |
98 | 101 | capture_output=True, |
99 | 102 | ) |
100 | | - if retval.returncode: |
101 | | - raise RuntimeError(f"Could not create git worktree: {retval.stderr.decode()}") |
| 103 | + if process.returncode: |
| 104 | + raise RuntimeError(f"Could not create git worktree: {process.stderr.decode()}") |
102 | 105 |
|
103 | 106 | try: |
104 | | - yield Path(target) |
| 107 | + yield Path(location) |
105 | 108 | finally: |
106 | | - subprocess.run(["git", "-C", repo, "worktree", "remove", uid], stdout=subprocess.DEVNULL) |
| 109 | + subprocess.run(["git", "-C", repo, "worktree", "remove", branch], stdout=subprocess.DEVNULL) |
107 | 110 | subprocess.run(["git", "-C", repo, "worktree", "prune"], stdout=subprocess.DEVNULL) |
108 | | - subprocess.run(["git", "-C", repo, "branch", "-D", uid], stdout=subprocess.DEVNULL) |
| 111 | + subprocess.run(["git", "-C", repo, "branch", "-D", branch], stdout=subprocess.DEVNULL) |
109 | 112 |
|
110 | 113 |
|
111 | 114 | def load_git( |
|
0 commit comments