| 42 | } |
| 43 | |
| 44 | export async function fromDirectory(directory: string) { |
| 45 | log.info("fromDirectory", { directory }) |
| 46 | |
| 47 | const { id, worktree, vcs } = await iife(async () => { |
| 48 | const matches = Filesystem.up({ targets: [".git"], start: directory }) |
| 49 | const git = await matches.next().then((x) => x.value) |
| 50 | await matches.return() |
| 51 | if (git) { |
| 52 | let worktree = path.dirname(git) |
| 53 | let id = await Bun.file(path.join(git, "arctic")) |
| 54 | .text() |
| 55 | .then((x) => x.trim()) |
| 56 | .catch(() => {}) |
| 57 | if (!id) { |
| 58 | const roots = await $`git rev-list --max-parents=0 --all` |
| 59 | .quiet() |
| 60 | .nothrow() |
| 61 | .cwd(worktree) |
| 62 | .text() |
| 63 | .then((x) => |
| 64 | x |
| 65 | .split("\n") |
| 66 | .filter(Boolean) |
| 67 | .map((x) => x.trim()) |
| 68 | .toSorted(), |
| 69 | ) |
| 70 | id = roots[0] |
| 71 | if (id) Bun.file(path.join(git, "arctic")).write(id) |
| 72 | } |
| 73 | if (!id) |
| 74 | return { |
| 75 | id: "global", |
| 76 | worktree, |
| 77 | vcs: "git", |
| 78 | } |
| 79 | worktree = await $`git rev-parse --show-toplevel` |
| 80 | .quiet() |
| 81 | .nothrow() |
| 82 | .cwd(worktree) |
| 83 | .text() |
| 84 | .then((x) => path.resolve(worktree, x.trim())) |
| 85 | return { id, worktree, vcs: "git" } |
| 86 | } |
| 87 | |
| 88 | return { |
| 89 | id: "global", |
| 90 | worktree: "/", |
| 91 | vcs: Info.shape.vcs.parse(Flag.ARCTIC_FAKE_VCS), |
| 92 | } |
| 93 | }) |
| 94 | |
| 95 | let existing = await Storage.read<Info>(["project", id]).catch(() => undefined) |
| 96 | if (!existing) { |
| 97 | existing = { |
| 98 | id, |
| 99 | worktree, |
| 100 | vcs: vcs as Info["vcs"], |
| 101 | time: { |