@n-seiji/hop 0.0.0-stage → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +674 -0
- package/README.ja.md +90 -0
- package/README.md +89 -2
- package/dist/clean-e451psab.js +262 -0
- package/dist/cli-0drsg55d.js +143 -0
- package/dist/cli-48gejg5n.js +194 -0
- package/dist/cli-4f1vz23g.js +97 -0
- package/dist/cli-4jdam1m1.js +29 -0
- package/dist/cli-77yanq37.js +21 -0
- package/dist/cli-9b4r23jn.js +30 -0
- package/dist/cli-a56b2ar0.js +20 -0
- package/dist/cli-b0tk5wcx.js +86 -0
- package/dist/cli-btqjpf1q.js +35 -0
- package/dist/cli-dbzx9nzg.js +40 -0
- package/dist/cli-h17ydkz8.js +124 -0
- package/dist/cli-n74f2vav.js +54 -0
- package/dist/cli-nrb5hg6g.js +87 -0
- package/dist/cli-pick-aj567n01.js +1506 -0
- package/dist/cli-qsz1egw1.js +108 -0
- package/dist/cli-update-82cttjfc.js +639 -0
- package/dist/cli.js +630 -0
- package/dist/init-xfq1pt2h.js +64 -0
- package/dist/jump-3262fxj4.js +108 -0
- package/dist/ls-hvwtaxv8.js +34 -0
- package/dist/render-ls-str0w03j.js +142 -0
- package/dist/rm-5v1exwwn.js +7 -0
- package/dist/root-q4zr60sk.js +7 -0
- package/package.json +43 -4
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import {
|
|
2
|
+
messageOf
|
|
3
|
+
} from "./cli-n74f2vav.js";
|
|
4
|
+
import {
|
|
5
|
+
withRepoLock
|
|
6
|
+
} from "./cli-h17ydkz8.js";
|
|
7
|
+
import {
|
|
8
|
+
refArgumentRejection,
|
|
9
|
+
loadRepoContext,
|
|
10
|
+
isWorktreeDirty,
|
|
11
|
+
resolveBranchCheckout
|
|
12
|
+
} from "./cli-4f1vz23g.js";
|
|
13
|
+
import {
|
|
14
|
+
EXIT_SAFE_REJECTION,
|
|
15
|
+
ok,
|
|
16
|
+
fail,
|
|
17
|
+
pass,
|
|
18
|
+
reject
|
|
19
|
+
} from "./cli-77yanq37.js";
|
|
20
|
+
|
|
21
|
+
// src/commands/root-holder-swap.ts
|
|
22
|
+
var holderRejection = (branch, holderPath, reason) => fail(EXIT_SAFE_REJECTION, `Branch "${branch}" is already checked out at ${holderPath}, and it ${reason}. Not swapping — cd there instead of using hop root.`);
|
|
23
|
+
var prunableHolderRejection = (branch, holderPath) => fail(EXIT_SAFE_REJECTION, `Branch "${branch}" is held by a stale worktree registration at ${holderPath} (git reports it prunable). Not swapping — if that worktree was moved, re-link it with "git worktree repair <new path>"; otherwise drop the registration first ("hop rm ${branch}" or "git worktree prune").`);
|
|
24
|
+
var multipleHoldersRejection = (branch, holders) => fail(EXIT_SAFE_REJECTION, `Branch "${branch}" is checked out at ${holders.length} worktrees at once (${holders.map((holder) => holder.path).join(", ")}). Not swapping — hop won't guess which one to detach.`);
|
|
25
|
+
var resolveHolderSwap = async ({
|
|
26
|
+
git,
|
|
27
|
+
fresh,
|
|
28
|
+
target,
|
|
29
|
+
switchOptions,
|
|
30
|
+
policy
|
|
31
|
+
}) => {
|
|
32
|
+
if (target === "-") {
|
|
33
|
+
return pass(null);
|
|
34
|
+
}
|
|
35
|
+
const freshHolders = fresh.worktrees.filter((wt) => wt.branch === target && wt.kind !== "root");
|
|
36
|
+
if (freshHolders.length > 1) {
|
|
37
|
+
return reject(multipleHoldersRejection(target, freshHolders));
|
|
38
|
+
}
|
|
39
|
+
const [freshHolder] = freshHolders;
|
|
40
|
+
if (freshHolder === undefined) {
|
|
41
|
+
return pass(null);
|
|
42
|
+
}
|
|
43
|
+
if (policy.expectedPath !== undefined && freshHolder.path !== policy.expectedPath) {
|
|
44
|
+
return reject(holderRejection(target, freshHolder.path, `changed from the picker selection at ${policy.expectedPath}`));
|
|
45
|
+
}
|
|
46
|
+
if (freshHolder.kind === "external" && !policy.allowExternal) {
|
|
47
|
+
return reject(holderRejection(target, freshHolder.path, "is external and requires confirmation before it can be put into detached HEAD"));
|
|
48
|
+
}
|
|
49
|
+
if (switchOptions.createBranch === true) {
|
|
50
|
+
return reject(holderRejection(target, freshHolder.path, "wasn't visible when hop checked existing local branches (e.g. it has no commits yet, or was created after that check)"));
|
|
51
|
+
}
|
|
52
|
+
if (freshHolder.locked) {
|
|
53
|
+
return reject(holderRejection(target, freshHolder.path, `is locked by git${freshHolder.lockReason === null ? "" : ` (${freshHolder.lockReason})`}`));
|
|
54
|
+
}
|
|
55
|
+
if (freshHolder.prunable) {
|
|
56
|
+
return reject(prunableHolderRejection(target, freshHolder.path));
|
|
57
|
+
}
|
|
58
|
+
const holderDirty = await isWorktreeDirty(git, fresh.worktrees, freshHolder.path);
|
|
59
|
+
if (holderDirty) {
|
|
60
|
+
return reject(holderRejection(target, freshHolder.path, "has uncommitted or untracked changes"));
|
|
61
|
+
}
|
|
62
|
+
await git.detachHead(freshHolder.path);
|
|
63
|
+
return pass({ path: freshHolder.path, branch: target });
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
// src/commands/root.ts
|
|
67
|
+
var root2 = async (git, fs, options) => {
|
|
68
|
+
if (options.target !== undefined && options.target !== "-") {
|
|
69
|
+
const refRejection = refArgumentRejection(options.target, options.track);
|
|
70
|
+
if (refRejection !== null) {
|
|
71
|
+
return refRejection;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const context = await loadRepoContext(git, fs, options.cwd);
|
|
75
|
+
if (options.target === undefined) {
|
|
76
|
+
return ok({
|
|
77
|
+
path: context.rootPath,
|
|
78
|
+
data: {
|
|
79
|
+
branch: rootBranchOf(context),
|
|
80
|
+
switched: false,
|
|
81
|
+
detachedHolder: null
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
const dirtyRejection = await dirtyRootRejection(git, context);
|
|
86
|
+
if (dirtyRejection !== null) {
|
|
87
|
+
return dirtyRejection;
|
|
88
|
+
}
|
|
89
|
+
if (options.target === "-") {
|
|
90
|
+
return switchAndReport({
|
|
91
|
+
git,
|
|
92
|
+
fs,
|
|
93
|
+
context,
|
|
94
|
+
target: "-",
|
|
95
|
+
switchOptions: {},
|
|
96
|
+
holderPolicy: { allowExternal: true }
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
const checkout = await resolveBranchCheckout(git, context.rootPath, options.target, options.track);
|
|
100
|
+
if (!checkout.ok) {
|
|
101
|
+
return checkout.rejection;
|
|
102
|
+
}
|
|
103
|
+
return switchAndReport({
|
|
104
|
+
git,
|
|
105
|
+
fs,
|
|
106
|
+
context,
|
|
107
|
+
target: options.target,
|
|
108
|
+
switchOptions: checkout.value,
|
|
109
|
+
holderPolicy: {
|
|
110
|
+
allowExternal: options.allowExternalHolderSwap ?? true,
|
|
111
|
+
...options.expectedHolderPath === undefined ? {} : { expectedPath: options.expectedHolderPath }
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
};
|
|
115
|
+
var rootBranchOf = ({ worktrees }) => worktrees.find((wt) => wt.kind === "root")?.branch ?? null;
|
|
116
|
+
var dirtyRootRejection = async (git, context) => {
|
|
117
|
+
const dirty = await isWorktreeDirty(git, context.worktrees, context.rootPath);
|
|
118
|
+
if (!dirty) {
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
return fail(EXIT_SAFE_REJECTION, "Root clone has uncommitted or untracked changes. Commit, stash, or discard them before switching.");
|
|
122
|
+
};
|
|
123
|
+
var rollBackSwitch = async (git, { rootPath, previousBranch, detachedHolder }) => {
|
|
124
|
+
const warnings = [];
|
|
125
|
+
if (previousBranch !== null) {
|
|
126
|
+
try {
|
|
127
|
+
await git.switchBranch(rootPath, previousBranch, {});
|
|
128
|
+
} catch {
|
|
129
|
+
warnings.push(`Could not restore ${rootPath} to its original branch (${previousBranch})`);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
if (detachedHolder !== null) {
|
|
133
|
+
try {
|
|
134
|
+
await git.switchBranch(detachedHolder.path, detachedHolder.branch, {});
|
|
135
|
+
} catch {
|
|
136
|
+
warnings.push(`Could not restore ${detachedHolder.path} to its original branch (${detachedHolder.branch}); it remains in detached HEAD`);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return warnings;
|
|
140
|
+
};
|
|
141
|
+
var switchAndReport = ({
|
|
142
|
+
git,
|
|
143
|
+
fs,
|
|
144
|
+
context,
|
|
145
|
+
target,
|
|
146
|
+
switchOptions,
|
|
147
|
+
holderPolicy
|
|
148
|
+
}) => withRepoLock(context.commonDir, async () => {
|
|
149
|
+
let detachedHolder = null;
|
|
150
|
+
let previousBranch = null;
|
|
151
|
+
try {
|
|
152
|
+
const fresh = await loadRepoContext(git, fs, context.rootPath);
|
|
153
|
+
previousBranch = rootBranchOf(fresh);
|
|
154
|
+
const stillDirty = await dirtyRootRejection(git, fresh);
|
|
155
|
+
if (stillDirty !== null) {
|
|
156
|
+
return stillDirty;
|
|
157
|
+
}
|
|
158
|
+
const holderSwap = await resolveHolderSwap({
|
|
159
|
+
git,
|
|
160
|
+
fresh,
|
|
161
|
+
target,
|
|
162
|
+
switchOptions,
|
|
163
|
+
policy: holderPolicy
|
|
164
|
+
});
|
|
165
|
+
if (!holderSwap.ok) {
|
|
166
|
+
return holderSwap.rejection;
|
|
167
|
+
}
|
|
168
|
+
detachedHolder = holderSwap.value;
|
|
169
|
+
await git.switchBranch(fresh.rootPath, target, switchOptions);
|
|
170
|
+
let resolvedBranch = target;
|
|
171
|
+
if (target === "-") {
|
|
172
|
+
const afterSwitch = await loadRepoContext(git, fs, fresh.rootPath);
|
|
173
|
+
resolvedBranch = rootBranchOf(afterSwitch);
|
|
174
|
+
}
|
|
175
|
+
return ok({
|
|
176
|
+
path: fresh.rootPath,
|
|
177
|
+
data: {
|
|
178
|
+
branch: resolvedBranch,
|
|
179
|
+
switched: true,
|
|
180
|
+
detachedHolder: detachedHolder?.path ?? null
|
|
181
|
+
},
|
|
182
|
+
warnings: detachedHolder === null ? [] : [`Put ${detachedHolder.path} into detached HEAD`]
|
|
183
|
+
});
|
|
184
|
+
} catch (error) {
|
|
185
|
+
const warnings = await rollBackSwitch(git, {
|
|
186
|
+
rootPath: context.rootPath,
|
|
187
|
+
previousBranch,
|
|
188
|
+
detachedHolder
|
|
189
|
+
});
|
|
190
|
+
return fail(EXIT_SAFE_REJECTION, `Failed to switch root: ${messageOf(error)}`, warnings);
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
export { root2 };
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import {
|
|
2
|
+
parsePorcelain
|
|
3
|
+
} from "./cli-b0tk5wcx.js";
|
|
4
|
+
import {
|
|
5
|
+
EXIT_USAGE_ERROR,
|
|
6
|
+
fail,
|
|
7
|
+
pass,
|
|
8
|
+
reject
|
|
9
|
+
} from "./cli-77yanq37.js";
|
|
10
|
+
import {
|
|
11
|
+
isWithin,
|
|
12
|
+
classifyWorktreePath
|
|
13
|
+
} from "./cli-a56b2ar0.js";
|
|
14
|
+
|
|
15
|
+
// src/infra/repo.ts
|
|
16
|
+
import { basename, dirname, join } from "node:path";
|
|
17
|
+
|
|
18
|
+
// src/domain/branch-name.ts
|
|
19
|
+
var isOptionLikeRef = (name) => name.startsWith("-");
|
|
20
|
+
var optionLikeRefRejection = (label, name) => isOptionLikeRef(name) ? fail(EXIT_USAGE_ERROR, `Refusing ${label} "${name}": names starting with "-" would be parsed as git options.`) : null;
|
|
21
|
+
var refArgumentRejection = (branch, track) => optionLikeRefRejection("branch", branch) ?? (track === undefined ? null : optionLikeRefRejection("--track ref", track));
|
|
22
|
+
|
|
23
|
+
// src/domain/tracking.ts
|
|
24
|
+
var resolveTrackingRef = (branch, remotes) => {
|
|
25
|
+
if (remotes.includes("origin")) {
|
|
26
|
+
return { kind: "track", ref: `origin/${branch}` };
|
|
27
|
+
}
|
|
28
|
+
const [first, second] = remotes;
|
|
29
|
+
if (first === undefined) {
|
|
30
|
+
return { kind: "none" };
|
|
31
|
+
}
|
|
32
|
+
if (second !== undefined) {
|
|
33
|
+
return { kind: "ambiguous" };
|
|
34
|
+
}
|
|
35
|
+
return { kind: "track", ref: `${first}/${branch}` };
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
// src/infra/repo.ts
|
|
39
|
+
var loadRepoContext = async (git, fs, cwd) => {
|
|
40
|
+
const [commonDir, porcelainOut] = await Promise.all([
|
|
41
|
+
git.commonDir(cwd),
|
|
42
|
+
git.listWorktreesPorcelain(cwd)
|
|
43
|
+
]);
|
|
44
|
+
const parsed = parsePorcelain(porcelainOut);
|
|
45
|
+
const [firstEntry] = parsed;
|
|
46
|
+
if (firstEntry === undefined) {
|
|
47
|
+
throw new Error("git worktree list returned no entries");
|
|
48
|
+
}
|
|
49
|
+
const rootPath = await realpathOrRaw(fs, firstEntry.path);
|
|
50
|
+
const managedRoot = join(dirname(rootPath), "_worktree", basename(rootPath));
|
|
51
|
+
const worktrees = await Promise.all(parsed.map(async (entry) => {
|
|
52
|
+
const resolvedPath = await realpathOrRaw(fs, entry.path);
|
|
53
|
+
return {
|
|
54
|
+
...entry,
|
|
55
|
+
path: resolvedPath,
|
|
56
|
+
kind: classifyWorktreePath(resolvedPath, rootPath, managedRoot)
|
|
57
|
+
};
|
|
58
|
+
}));
|
|
59
|
+
return { rootPath, managedRoot, commonDir, worktrees };
|
|
60
|
+
};
|
|
61
|
+
var nestedWorktrees = (worktrees, path) => worktrees.filter((wt) => wt.path !== path && !wt.prunable && isWithin(path, wt.path));
|
|
62
|
+
var isWorktreeDirty = (git, worktrees, path) => git.isDirty(path, nestedWorktrees(worktrees, path).map((wt) => wt.path));
|
|
63
|
+
var worktreeDirtyState = async (git, fs, worktrees, wt) => {
|
|
64
|
+
if (wt.bare || wt.prunable || !await fs.exists(wt.path)) {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
return isWorktreeDirty(git, worktrees, wt.path);
|
|
68
|
+
};
|
|
69
|
+
var resolveBranchCheckout = async (git, rootPath, branch, explicitTrack) => {
|
|
70
|
+
const localBranches = await git.listBranches(rootPath);
|
|
71
|
+
const createBranch = !localBranches.includes(branch);
|
|
72
|
+
let track = explicitTrack;
|
|
73
|
+
if (createBranch && track === undefined) {
|
|
74
|
+
const remotes = await git.remotesWithBranch(rootPath, branch);
|
|
75
|
+
const resolution = resolveTrackingRef(branch, remotes);
|
|
76
|
+
if (resolution.kind === "ambiguous") {
|
|
77
|
+
return reject(fail(EXIT_USAGE_ERROR, `Branch "${branch}" exists on multiple remotes (${remotes.join(", ")}). Use --track to disambiguate.`));
|
|
78
|
+
}
|
|
79
|
+
if (resolution.kind === "track") {
|
|
80
|
+
const rejection = optionLikeRefRejection("--track ref", resolution.ref);
|
|
81
|
+
if (rejection !== null) {
|
|
82
|
+
return reject(rejection);
|
|
83
|
+
}
|
|
84
|
+
track = resolution.ref;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return pass({ createBranch, ...track === undefined ? {} : { track } });
|
|
88
|
+
};
|
|
89
|
+
var realpathOrRaw = async (fs, path) => {
|
|
90
|
+
try {
|
|
91
|
+
return await fs.realpath(path);
|
|
92
|
+
} catch {
|
|
93
|
+
return path;
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
export { isOptionLikeRef, optionLikeRefRejection, refArgumentRejection, loadRepoContext, nestedWorktrees, isWorktreeDirty, worktreeDirtyState, resolveBranchCheckout };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// src/domain/posix-path.ts
|
|
2
|
+
var withoutTrailingSlash = (path) => path.length > 1 && path.endsWith("/") ? path.slice(0, -1) : path;
|
|
3
|
+
var shortenHome = (path, home) => {
|
|
4
|
+
if (home === "" || home === "/") {
|
|
5
|
+
return path;
|
|
6
|
+
}
|
|
7
|
+
if (path === home) {
|
|
8
|
+
return "~";
|
|
9
|
+
}
|
|
10
|
+
return path.startsWith(`${home}/`) ? `~${path.slice(home.length)}` : path;
|
|
11
|
+
};
|
|
12
|
+
var parentDir = (path) => {
|
|
13
|
+
const trimmed = withoutTrailingSlash(path);
|
|
14
|
+
const index = trimmed.lastIndexOf("/");
|
|
15
|
+
if (index === -1) {
|
|
16
|
+
return ".";
|
|
17
|
+
}
|
|
18
|
+
return index === 0 ? "/" : trimmed.slice(0, index);
|
|
19
|
+
};
|
|
20
|
+
var baseName = (path) => {
|
|
21
|
+
const trimmed = withoutTrailingSlash(path);
|
|
22
|
+
return trimmed === "/" ? trimmed : trimmed.slice(trimmed.lastIndexOf("/") + 1);
|
|
23
|
+
};
|
|
24
|
+
var absoluteCandidates = (dirs, name) => [
|
|
25
|
+
...new Set(dirs.map((entry) => entry.trim()).filter((entry) => entry.startsWith("/")).map((dir) => `${dir.endsWith("/") ? dir.slice(0, -1) : dir}/${name}`))
|
|
26
|
+
];
|
|
27
|
+
var splitPathEnv = (pathEnv) => (pathEnv ?? "").split(":");
|
|
28
|
+
|
|
29
|
+
export { shortenHome, parentDir, baseName, absoluteCandidates, splitPathEnv };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// src/domain/result.ts
|
|
2
|
+
var EXIT_SUCCESS = 0;
|
|
3
|
+
var EXIT_GENERAL_ERROR = 1;
|
|
4
|
+
var EXIT_USAGE_ERROR = 2;
|
|
5
|
+
var EXIT_SAFE_REJECTION = 3;
|
|
6
|
+
var EXIT_CANCELLED = 130;
|
|
7
|
+
var ok = (fields = {}) => ({
|
|
8
|
+
ok: true,
|
|
9
|
+
exitCode: EXIT_SUCCESS,
|
|
10
|
+
...fields
|
|
11
|
+
});
|
|
12
|
+
var fail = (exitCode, errorMessage, warnings) => ({
|
|
13
|
+
ok: false,
|
|
14
|
+
exitCode,
|
|
15
|
+
errorMessage,
|
|
16
|
+
...warnings === undefined || warnings.length === 0 ? {} : { warnings }
|
|
17
|
+
});
|
|
18
|
+
var pass = (value) => ({ ok: true, value });
|
|
19
|
+
var reject = (rejection) => ({ ok: false, rejection });
|
|
20
|
+
|
|
21
|
+
export { EXIT_SUCCESS, EXIT_GENERAL_ERROR, EXIT_USAGE_ERROR, EXIT_SAFE_REJECTION, EXIT_CANCELLED, ok, fail, pass, reject };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// src/infra/term.ts
|
|
2
|
+
import { createInterface } from "node:readline";
|
|
3
|
+
var colorEnabled = (stream) => stream.isTTY === true && (process.env["NO_COLOR"] ?? "") === "";
|
|
4
|
+
var createTermPort = () => ({
|
|
5
|
+
isTTY() {
|
|
6
|
+
return process.stderr.isTTY === true && process.stdin.isTTY === true;
|
|
7
|
+
},
|
|
8
|
+
logStderr(message) {
|
|
9
|
+
process.stderr.write(`${message}
|
|
10
|
+
`);
|
|
11
|
+
},
|
|
12
|
+
confirm(message) {
|
|
13
|
+
return new Promise((resolve) => {
|
|
14
|
+
const rl = createInterface({
|
|
15
|
+
input: process.stdin,
|
|
16
|
+
output: process.stderr,
|
|
17
|
+
terminal: false
|
|
18
|
+
});
|
|
19
|
+
rl.question(`${message} [y/N] `, (answer) => {
|
|
20
|
+
resolve(answer.trim().toLowerCase() === "y");
|
|
21
|
+
rl.close();
|
|
22
|
+
});
|
|
23
|
+
rl.once("close", () => {
|
|
24
|
+
resolve(false);
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
export { colorEnabled, createTermPort };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// src/domain/classify.ts
|
|
2
|
+
var isWithin = (parent, child) => {
|
|
3
|
+
if (parent === child) {
|
|
4
|
+
return false;
|
|
5
|
+
}
|
|
6
|
+
const parentWithSlash = parent.endsWith("/") ? parent : `${parent}/`;
|
|
7
|
+
return child.startsWith(parentWithSlash);
|
|
8
|
+
};
|
|
9
|
+
var isWithinOrSelf = (parent, child) => parent === child || isWithin(parent, child);
|
|
10
|
+
var classifyWorktreePath = (path, rootPath, managedRoot) => {
|
|
11
|
+
if (path === rootPath) {
|
|
12
|
+
return "root";
|
|
13
|
+
}
|
|
14
|
+
if (path === managedRoot || isWithin(managedRoot, path)) {
|
|
15
|
+
return "managed";
|
|
16
|
+
}
|
|
17
|
+
return "external";
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export { isWithin, isWithinOrSelf, classifyWorktreePath };
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// src/domain/porcelain.ts
|
|
2
|
+
var REFS_HEADS_PREFIX = "refs/heads/";
|
|
3
|
+
var branchNameFromRef = (ref) => ref.startsWith(REFS_HEADS_PREFIX) ? ref.slice(REFS_HEADS_PREFIX.length) : ref;
|
|
4
|
+
var parseRecord = (lines) => {
|
|
5
|
+
let path = null;
|
|
6
|
+
let head = null;
|
|
7
|
+
let branch = null;
|
|
8
|
+
let detached = false;
|
|
9
|
+
let bare = false;
|
|
10
|
+
let locked = false;
|
|
11
|
+
let lockReason = null;
|
|
12
|
+
let prunable = false;
|
|
13
|
+
let prunableReason = null;
|
|
14
|
+
for (const line of lines.filter((candidate) => candidate.length > 0)) {
|
|
15
|
+
const spaceIndex = line.indexOf(" ");
|
|
16
|
+
const key = spaceIndex === -1 ? line : line.slice(0, spaceIndex);
|
|
17
|
+
const value = spaceIndex === -1 ? "" : line.slice(spaceIndex + 1);
|
|
18
|
+
switch (key) {
|
|
19
|
+
case "worktree": {
|
|
20
|
+
path = value;
|
|
21
|
+
break;
|
|
22
|
+
}
|
|
23
|
+
case "HEAD": {
|
|
24
|
+
head = value;
|
|
25
|
+
break;
|
|
26
|
+
}
|
|
27
|
+
case "branch": {
|
|
28
|
+
branch = branchNameFromRef(value);
|
|
29
|
+
break;
|
|
30
|
+
}
|
|
31
|
+
case "detached": {
|
|
32
|
+
detached = true;
|
|
33
|
+
break;
|
|
34
|
+
}
|
|
35
|
+
case "bare": {
|
|
36
|
+
bare = true;
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
case "locked": {
|
|
40
|
+
locked = true;
|
|
41
|
+
lockReason = value.length > 0 ? value : null;
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
case "prunable": {
|
|
45
|
+
prunable = true;
|
|
46
|
+
prunableReason = value.length > 0 ? value : null;
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
default: {
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
if (path === null) {
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
return {
|
|
58
|
+
path,
|
|
59
|
+
head,
|
|
60
|
+
branch,
|
|
61
|
+
detached,
|
|
62
|
+
bare,
|
|
63
|
+
locked,
|
|
64
|
+
lockReason,
|
|
65
|
+
prunable,
|
|
66
|
+
prunableReason
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
var parsePorcelain = (output) => {
|
|
70
|
+
if (output.length === 0) {
|
|
71
|
+
return [];
|
|
72
|
+
}
|
|
73
|
+
const trimmed = output.endsWith("\x00") ? output.slice(0, -1) : output;
|
|
74
|
+
const records = trimmed.split("\x00\x00");
|
|
75
|
+
const result = [];
|
|
76
|
+
for (const record of records.filter((candidate) => candidate.length > 0)) {
|
|
77
|
+
const lines = record.split("\x00");
|
|
78
|
+
const parsed = parseRecord(lines);
|
|
79
|
+
if (parsed !== null) {
|
|
80
|
+
result.push(parsed);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return result;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
export { REFS_HEADS_PREFIX, parsePorcelain };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import {
|
|
2
|
+
REFS_HEADS_PREFIX
|
|
3
|
+
} from "./cli-b0tk5wcx.js";
|
|
4
|
+
|
|
5
|
+
// src/domain/upstream.ts
|
|
6
|
+
var NONE = { kind: "none" };
|
|
7
|
+
var FIELD_SEPARATOR = "\x00";
|
|
8
|
+
var UPSTREAM_FORMAT = "%(refname)%00%(upstream)%00%(upstream:track,nobracket)";
|
|
9
|
+
var countOf = (track, word) => {
|
|
10
|
+
const match = new RegExp(`${word} (?<count>\\d+)`, "u").exec(track);
|
|
11
|
+
return match?.groups?.count === undefined ? 0 : Number(match.groups.count);
|
|
12
|
+
};
|
|
13
|
+
var parseTrack = (upstream, track) => {
|
|
14
|
+
if (upstream.length === 0) {
|
|
15
|
+
return NONE;
|
|
16
|
+
}
|
|
17
|
+
if (track === "gone") {
|
|
18
|
+
return { kind: "gone" };
|
|
19
|
+
}
|
|
20
|
+
return { kind: "tracking", ahead: countOf(track, "ahead"), behind: countOf(track, "behind") };
|
|
21
|
+
};
|
|
22
|
+
var parseUpstreamStates = (output) => {
|
|
23
|
+
const states = new Map;
|
|
24
|
+
for (const line of output.split(`
|
|
25
|
+
`)) {
|
|
26
|
+
const [refname = "", upstream = "", track = ""] = line.split(FIELD_SEPARATOR);
|
|
27
|
+
if (refname.startsWith(REFS_HEADS_PREFIX)) {
|
|
28
|
+
states.set(refname.slice(REFS_HEADS_PREFIX.length), parseTrack(upstream, track.trim()));
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return states;
|
|
32
|
+
};
|
|
33
|
+
var upstreamStateOf = (states, branch) => states.get(branch) ?? NONE;
|
|
34
|
+
|
|
35
|
+
export { UPSTREAM_FORMAT, parseUpstreamStates, upstreamStateOf };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// src/render.ts
|
|
2
|
+
var PLAIN_JSON_INDENT = 2;
|
|
3
|
+
var render = (command, result, json) => {
|
|
4
|
+
if (json) {
|
|
5
|
+
const envelope = {
|
|
6
|
+
schemaVersion: 1,
|
|
7
|
+
command,
|
|
8
|
+
data: result.ok ? result.data : undefined,
|
|
9
|
+
warnings: result.warnings ?? []
|
|
10
|
+
};
|
|
11
|
+
process.stdout.write(`${JSON.stringify(envelope)}
|
|
12
|
+
`);
|
|
13
|
+
if (!result.ok && result.errorMessage !== undefined) {
|
|
14
|
+
process.stderr.write(`${result.errorMessage}
|
|
15
|
+
`);
|
|
16
|
+
}
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
if (result.path !== undefined) {
|
|
20
|
+
process.stdout.write(`${result.path}
|
|
21
|
+
`);
|
|
22
|
+
} else if (result.ok && result.data !== undefined) {
|
|
23
|
+
process.stdout.write(`${JSON.stringify(result.data, null, PLAIN_JSON_INDENT)}
|
|
24
|
+
`);
|
|
25
|
+
}
|
|
26
|
+
for (const warning of result.warnings ?? []) {
|
|
27
|
+
process.stderr.write(`warning: ${warning}
|
|
28
|
+
`);
|
|
29
|
+
}
|
|
30
|
+
if (!result.ok && result.errorMessage !== undefined) {
|
|
31
|
+
process.stderr.write(`${result.errorMessage}
|
|
32
|
+
`);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
var reportResult = (command, result, json) => {
|
|
36
|
+
render(command, result, json);
|
|
37
|
+
process.exitCode = result.exitCode;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export { render, reportResult };
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import {
|
|
2
|
+
EXIT_SAFE_REJECTION,
|
|
3
|
+
fail
|
|
4
|
+
} from "./cli-77yanq37.js";
|
|
5
|
+
|
|
6
|
+
// src/infra/lock.ts
|
|
7
|
+
import { randomUUID } from "node:crypto";
|
|
8
|
+
import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
9
|
+
import { join } from "node:path";
|
|
10
|
+
|
|
11
|
+
// src/domain/lock-policy.ts
|
|
12
|
+
var canReclaimLock = (input) => {
|
|
13
|
+
if (input.processAlive !== false) {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
return input.nowMs - input.startedAtMs > input.ttlMs;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
// src/infra/lock.ts
|
|
20
|
+
var LOCK_DIR_NAME = "nuthatch-lock";
|
|
21
|
+
var LOCK_INFO_FILE = "info.json";
|
|
22
|
+
var DEFAULT_TTL_MS = 30000;
|
|
23
|
+
var HEARTBEAT_INTERVAL_MS = 5000;
|
|
24
|
+
|
|
25
|
+
class LockHeldError extends Error {
|
|
26
|
+
info;
|
|
27
|
+
constructor(info) {
|
|
28
|
+
super(`Repository is locked by another hop process (pid ${info.pid})`);
|
|
29
|
+
this.name = "LockHeldError";
|
|
30
|
+
this.info = info;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
var isProcessAlive = (pid) => {
|
|
34
|
+
try {
|
|
35
|
+
process.kill(pid, 0);
|
|
36
|
+
return true;
|
|
37
|
+
} catch (error) {
|
|
38
|
+
const { code } = error;
|
|
39
|
+
if (code === "ESRCH") {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
if (code === "EPERM") {
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
return "unknown";
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
var readLockInfo = async (lockDir) => {
|
|
49
|
+
try {
|
|
50
|
+
const raw = await readFile(join(lockDir, LOCK_INFO_FILE), "utf8");
|
|
51
|
+
return JSON.parse(raw);
|
|
52
|
+
} catch {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
var writeLockInfo = async (lockDir, info) => {
|
|
57
|
+
await writeFile(join(lockDir, LOCK_INFO_FILE), JSON.stringify(info), "utf8");
|
|
58
|
+
};
|
|
59
|
+
var acquireRepoLock = async (commonDir, ttlMs = DEFAULT_TTL_MS) => {
|
|
60
|
+
const lockDir = join(commonDir, LOCK_DIR_NAME);
|
|
61
|
+
for (;; ) {
|
|
62
|
+
try {
|
|
63
|
+
await mkdir(lockDir);
|
|
64
|
+
break;
|
|
65
|
+
} catch (error) {
|
|
66
|
+
if (error.code !== "EEXIST") {
|
|
67
|
+
throw error;
|
|
68
|
+
}
|
|
69
|
+
const existing = await readLockInfo(lockDir);
|
|
70
|
+
if (existing === null) {
|
|
71
|
+
throw new LockHeldError({ pid: -1, startedAtMs: 0, token: "unknown" });
|
|
72
|
+
}
|
|
73
|
+
const reclaimable = canReclaimLock({
|
|
74
|
+
processAlive: isProcessAlive(existing.pid),
|
|
75
|
+
startedAtMs: existing.startedAtMs,
|
|
76
|
+
nowMs: Date.now(),
|
|
77
|
+
ttlMs
|
|
78
|
+
});
|
|
79
|
+
if (!reclaimable) {
|
|
80
|
+
throw new LockHeldError(existing);
|
|
81
|
+
}
|
|
82
|
+
await rm(lockDir, { recursive: true, force: true });
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const info = {
|
|
86
|
+
pid: process.pid,
|
|
87
|
+
startedAtMs: Date.now(),
|
|
88
|
+
token: randomUUID()
|
|
89
|
+
};
|
|
90
|
+
await writeLockInfo(lockDir, info);
|
|
91
|
+
const heartbeat = setInterval(() => {
|
|
92
|
+
writeLockInfo(lockDir, { ...info, startedAtMs: Date.now() });
|
|
93
|
+
}, HEARTBEAT_INTERVAL_MS);
|
|
94
|
+
heartbeat.unref();
|
|
95
|
+
return {
|
|
96
|
+
async release() {
|
|
97
|
+
clearInterval(heartbeat);
|
|
98
|
+
await rm(lockDir, { recursive: true, force: true });
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
};
|
|
102
|
+
var acquireUnlessHeld = async (commonDir) => {
|
|
103
|
+
try {
|
|
104
|
+
return await acquireRepoLock(commonDir);
|
|
105
|
+
} catch (error) {
|
|
106
|
+
if (error instanceof LockHeldError) {
|
|
107
|
+
return error;
|
|
108
|
+
}
|
|
109
|
+
throw error;
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
var withRepoLock = async (commonDir, mutate) => {
|
|
113
|
+
const lock = await acquireUnlessHeld(commonDir);
|
|
114
|
+
if (lock instanceof LockHeldError) {
|
|
115
|
+
return fail(EXIT_SAFE_REJECTION, lock.message);
|
|
116
|
+
}
|
|
117
|
+
try {
|
|
118
|
+
return await mutate();
|
|
119
|
+
} finally {
|
|
120
|
+
await lock.release();
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
export { withRepoLock };
|