@procedures/git 0.51.0 → 0.52.1
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/CHANGELOG.md +25 -0
- package/dist/.tsbuildinfo +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/procedures/git/files.d.ts +61 -0
- package/dist/procedures/git/files.d.ts.map +1 -0
- package/dist/procedures/git/files.js +229 -0
- package/dist/procedures/git/files.js.map +1 -0
- package/package.json +4 -4
- package/src/index.ts +1 -0
- package/src/procedures/git/files.ts +236 -0
package/dist/index.d.ts
CHANGED
|
@@ -17,5 +17,6 @@ export * from "./procedures/git.js";
|
|
|
17
17
|
export { git, GitError, type GitContext, type GitRunOptions } from "./procedures/git/run.js";
|
|
18
18
|
export { parsePorcelainV1, classifyStatus, type GitPorcelainEntry } from "./procedures/git/porcelain.js";
|
|
19
19
|
export { parseNumstat } from "./procedures/git/diff.js";
|
|
20
|
+
export { gitDirsOf, onRemote, readHead, readRefs, readWorktrees, type GitDirs, type GitHeadInfo } from "./procedures/git/files.js";
|
|
20
21
|
export * from "./module.js";
|
|
21
22
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,cAAc,qBAAqB,CAAC;AACpC,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAE,KAAK,UAAU,EAAE,KAAK,aAAa,EAAE,MAAM,yBAAyB,CAAC;AAC7F,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE,KAAK,iBAAiB,EAAE,MAAM,+BAA+B,CAAC;AACzG,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AACxD,cAAc,aAAa,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,cAAc,qBAAqB,CAAC;AACpC,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAE,KAAK,UAAU,EAAE,KAAK,aAAa,EAAE,MAAM,yBAAyB,CAAC;AAC7F,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE,KAAK,iBAAiB,EAAE,MAAM,+BAA+B,CAAC;AACzG,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AACxD,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAE,aAAa,EAAE,KAAK,OAAO,EAAE,KAAK,WAAW,EAAE,MAAM,2BAA2B,CAAC;AACnI,cAAc,aAAa,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -17,5 +17,6 @@ export * from "./procedures/git.js";
|
|
|
17
17
|
export { git, GitError } from "./procedures/git/run.js";
|
|
18
18
|
export { parsePorcelainV1, classifyStatus } from "./procedures/git/porcelain.js";
|
|
19
19
|
export { parseNumstat } from "./procedures/git/diff.js";
|
|
20
|
+
export { gitDirsOf, onRemote, readHead, readRefs, readWorktrees } from "./procedures/git/files.js";
|
|
20
21
|
export * from "./module.js";
|
|
21
22
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,cAAc,qBAAqB,CAAC;AACpC,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAuC,MAAM,yBAAyB,CAAC;AAC7F,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAA0B,MAAM,+BAA+B,CAAC;AACzG,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AACxD,cAAc,aAAa,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,cAAc,qBAAqB,CAAC;AACpC,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAuC,MAAM,yBAAyB,CAAC;AAC7F,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAA0B,MAAM,+BAA+B,CAAC;AACzG,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AACxD,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAE,aAAa,EAAkC,MAAM,2BAA2B,CAAC;AACnI,cAAc,aAAa,CAAC"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The state of a repository from its files, with no git process (roadmap CLI-123).
|
|
3
|
+
*
|
|
4
|
+
* A list of many working trees with `git` starts one process or more for each tree. These
|
|
5
|
+
* functions read the same state from the files of the git folder: the `HEAD` files, the folders of
|
|
6
|
+
* `worktrees`, the loose refs and `packed-refs`. They read no objects. A repository with the
|
|
7
|
+
* `reftable` format of refs gets an error.
|
|
8
|
+
*/
|
|
9
|
+
import type { GitWorktreeInfo } from "../../types.js";
|
|
10
|
+
/** The head of a working tree: its branch, or a detached commit. */
|
|
11
|
+
export interface GitHeadInfo {
|
|
12
|
+
/** The commit of the head. A branch with no commit (a new repository) has none. */
|
|
13
|
+
head?: string | undefined;
|
|
14
|
+
/** The branch of the head, with no `refs/heads/` prefix. A detached head has none. */
|
|
15
|
+
branch?: string | undefined;
|
|
16
|
+
detached: boolean;
|
|
17
|
+
}
|
|
18
|
+
/** The folders of git of a working tree: its own git folder, and the common folder of its repository. */
|
|
19
|
+
export interface GitDirs {
|
|
20
|
+
/** The git folder of the tree: `.git` of the main tree, or the folder in `worktrees` of a linked tree. */
|
|
21
|
+
gitDir: string;
|
|
22
|
+
/** The git folder with the refs, the objects and the folder `worktrees`. */
|
|
23
|
+
commonDir: string;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* This function gives the git folders of a working tree, of a git folder, or of a bare repository.
|
|
27
|
+
* A linked tree has a file `.git` with the line `gitdir: <path>`. It throws for a folder that is
|
|
28
|
+
* not in git.
|
|
29
|
+
*/
|
|
30
|
+
export declare function gitDirsOf(tree: string): Promise<GitDirs>;
|
|
31
|
+
/**
|
|
32
|
+
* This function reads the refs of a repository from its files: the loose refs under `refs/` and the
|
|
33
|
+
* lines of `packed-refs`. A loose ref takes the place of its packed line, as in git. A symbolic ref
|
|
34
|
+
* gives the hash of its target. A symbolic ref to a missing ref is not in the result. The map goes
|
|
35
|
+
* from each full name (`refs/heads/main`, `refs/remotes/origin/main`) to its hash. The repository
|
|
36
|
+
* is any working tree of it, or its git folder.
|
|
37
|
+
*/
|
|
38
|
+
export declare function readRefs(repo: string): Promise<Map<string, string>>;
|
|
39
|
+
/**
|
|
40
|
+
* This function reads the head of a working tree from its files: the branch and its commit, or a
|
|
41
|
+
* detached commit. A linked tree reads its HEAD through its file `.git`. A branch with no commit
|
|
42
|
+
* gives the branch with no `head`.
|
|
43
|
+
*/
|
|
44
|
+
export declare function readHead(tree: string): Promise<GitHeadInfo>;
|
|
45
|
+
/**
|
|
46
|
+
* This function reads the working trees of a repository from its files, as `git worktree list`
|
|
47
|
+
* gives them. The main tree is first. Then each linked tree follows, by the name of its folder in
|
|
48
|
+
* `worktrees`. The repository is any working tree of it, or its git folder.
|
|
49
|
+
*
|
|
50
|
+
* A linked tree whose folder is gone is `prunable`. A tree with a file `locked` is `locked`. The
|
|
51
|
+
* function reads the refs one time for all trees. A common folder with the name `.git` gives a main
|
|
52
|
+
* tree. Another name gives a bare main entry.
|
|
53
|
+
*/
|
|
54
|
+
export declare function readWorktrees(main: string): Promise<GitWorktreeInfo[]>;
|
|
55
|
+
/**
|
|
56
|
+
* This function tells if a remote-tracking ref (`refs/remotes/*`) of the repository points at
|
|
57
|
+
* the commit `sha` exactly. It gives true, else "unknown": the commit can be in the history of a
|
|
58
|
+
* remote ref, and this function reads no objects. The `sha` is a full hash.
|
|
59
|
+
*/
|
|
60
|
+
export declare function onRemote(repo: string, sha: string): Promise<true | "unknown">;
|
|
61
|
+
//# sourceMappingURL=files.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"files.d.ts","sourceRoot":"","sources":["../../../src/procedures/git/files.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAIH,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAEtD,oEAAoE;AACpE,MAAM,WAAW,WAAW;IAC1B,mFAAmF;IACnF,IAAI,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC1B,sFAAsF;IACtF,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC5B,QAAQ,EAAE,OAAO,CAAC;CACnB;AAED,yGAAyG;AACzG,MAAM,WAAW,OAAO;IACtB,0GAA0G;IAC1G,MAAM,EAAE,MAAM,CAAC;IACf,4EAA4E;IAC5E,SAAS,EAAE,MAAM,CAAC;CACnB;AA4BD;;;;GAIG;AACH,wBAAsB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAmB9D;AAqDD;;;;;;GAMG;AACH,wBAAsB,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAGzE;AAiCD;;;;GAIG;AACH,wBAAsB,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC,CAGjE;AAED;;;;;;;;GAQG;AACH,wBAAsB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,EAAE,CAAC,CA4B5E;AAED;;;;GAIG;AACH,wBAAsB,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,GAAG,SAAS,CAAC,CAKnF"}
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The state of a repository from its files, with no git process (roadmap CLI-123).
|
|
3
|
+
*
|
|
4
|
+
* A list of many working trees with `git` starts one process or more for each tree. These
|
|
5
|
+
* functions read the same state from the files of the git folder: the `HEAD` files, the folders of
|
|
6
|
+
* `worktrees`, the loose refs and `packed-refs`. They read no objects. A repository with the
|
|
7
|
+
* `reftable` format of refs gets an error.
|
|
8
|
+
*/
|
|
9
|
+
import { readdir, readFile, stat } from "node:fs/promises";
|
|
10
|
+
import { basename, dirname, join, resolve } from "node:path";
|
|
11
|
+
/** A full hash of a commit: 40 hex digits (SHA-1) or 64 (SHA-256). */
|
|
12
|
+
const HASH = /^(?:[0-9a-f]{40}|[0-9a-f]{64})$/;
|
|
13
|
+
/** The most steps of a symbolic ref to its commit. Git stops at 5 too. */
|
|
14
|
+
const MAX_SYMREF_DEPTH = 5;
|
|
15
|
+
/** This function gives the text of a file, or undefined when the file does not exist. */
|
|
16
|
+
async function textOf(path) {
|
|
17
|
+
try {
|
|
18
|
+
return await readFile(path, "utf8");
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
if (error.code === "ENOENT" || error.code === "ENOTDIR")
|
|
22
|
+
return undefined;
|
|
23
|
+
throw error;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/** True when a path exists. */
|
|
27
|
+
async function exists(path) {
|
|
28
|
+
try {
|
|
29
|
+
await stat(path);
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* This function gives the git folders of a working tree, of a git folder, or of a bare repository.
|
|
38
|
+
* A linked tree has a file `.git` with the line `gitdir: <path>`. It throws for a folder that is
|
|
39
|
+
* not in git.
|
|
40
|
+
*/
|
|
41
|
+
export async function gitDirsOf(tree) {
|
|
42
|
+
const dotGit = join(tree, ".git");
|
|
43
|
+
let gitDir;
|
|
44
|
+
const link = await textOf(dotGit).catch((error) => {
|
|
45
|
+
// A folder .git: the main tree
|
|
46
|
+
if (error.code === "EISDIR")
|
|
47
|
+
return null;
|
|
48
|
+
throw error;
|
|
49
|
+
});
|
|
50
|
+
if (link === null)
|
|
51
|
+
gitDir = resolve(dotGit);
|
|
52
|
+
else if (link !== undefined) {
|
|
53
|
+
const match = /^gitdir: (.+)$/m.exec(link);
|
|
54
|
+
if (match === null)
|
|
55
|
+
throw new Error(`The file ${dotGit} has no line gitdir`);
|
|
56
|
+
gitDir = resolve(tree, match[1].trim());
|
|
57
|
+
}
|
|
58
|
+
else if (await exists(join(tree, "HEAD"))) {
|
|
59
|
+
// A git folder or a bare repository
|
|
60
|
+
gitDir = resolve(tree);
|
|
61
|
+
}
|
|
62
|
+
else
|
|
63
|
+
throw new Error(`The folder ${tree} is not a git working tree`);
|
|
64
|
+
const common = await textOf(join(gitDir, "commondir"));
|
|
65
|
+
return { gitDir, commonDir: common === undefined ? gitDir : resolve(gitDir, common.trim()) };
|
|
66
|
+
}
|
|
67
|
+
/** This function throws for a repository whose refs are in the reftable format: these functions do not read it. */
|
|
68
|
+
async function assertFilesRefs(commonDir) {
|
|
69
|
+
if (await exists(join(commonDir, "reftable")))
|
|
70
|
+
throw new Error(`The repository ${commonDir} keeps its refs in the reftable format: read them with git`);
|
|
71
|
+
}
|
|
72
|
+
/** This function reads `packed-refs`: the name and the hash of each ref. A peeled line (`^`) and a comment are not refs. */
|
|
73
|
+
function parsePackedRefs(text) {
|
|
74
|
+
const refs = new Map();
|
|
75
|
+
for (const line of text.split(/\r?\n/)) {
|
|
76
|
+
if (line === "" || line.startsWith("#") || line.startsWith("^"))
|
|
77
|
+
continue;
|
|
78
|
+
const space = line.indexOf(" ");
|
|
79
|
+
if (space < 0)
|
|
80
|
+
continue;
|
|
81
|
+
const hash = line.slice(0, space).toLowerCase();
|
|
82
|
+
if (HASH.test(hash))
|
|
83
|
+
refs.set(line.slice(space + 1).trim(), hash);
|
|
84
|
+
}
|
|
85
|
+
return refs;
|
|
86
|
+
}
|
|
87
|
+
/** This function adds the loose refs of a folder of refs to `out`: a hash, or `ref: <name>` for a symbolic ref. */
|
|
88
|
+
async function readLoose(commonDir, folder, out) {
|
|
89
|
+
let entries;
|
|
90
|
+
try {
|
|
91
|
+
entries = await readdir(join(commonDir, folder), { withFileTypes: true });
|
|
92
|
+
}
|
|
93
|
+
catch (error) {
|
|
94
|
+
if (error.code === "ENOENT")
|
|
95
|
+
return;
|
|
96
|
+
throw error;
|
|
97
|
+
}
|
|
98
|
+
await Promise.all(entries.map(async (entry) => {
|
|
99
|
+
const name = `${folder}/${entry.name}`;
|
|
100
|
+
if (entry.isDirectory())
|
|
101
|
+
return readLoose(commonDir, name, out);
|
|
102
|
+
// A lock file is a write of git that did not end
|
|
103
|
+
if (!entry.isFile() || entry.name.endsWith(".lock"))
|
|
104
|
+
return;
|
|
105
|
+
const text = (await textOf(join(commonDir, name)))?.trim();
|
|
106
|
+
if (text !== undefined && text !== "")
|
|
107
|
+
out.set(name, text);
|
|
108
|
+
}));
|
|
109
|
+
}
|
|
110
|
+
/** This function gives the hash of a value of a ref: a hash, or a symbolic ref that it follows in `refs`. */
|
|
111
|
+
function hashOf(value, refs) {
|
|
112
|
+
let current = value;
|
|
113
|
+
for (let depth = 0; current !== undefined && depth <= MAX_SYMREF_DEPTH; depth++) {
|
|
114
|
+
const lower = current.toLowerCase();
|
|
115
|
+
if (HASH.test(lower))
|
|
116
|
+
return lower;
|
|
117
|
+
if (!current.startsWith("ref: "))
|
|
118
|
+
return undefined;
|
|
119
|
+
current = refs.get(current.slice(5).trim());
|
|
120
|
+
}
|
|
121
|
+
return undefined;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* This function reads the refs of a repository from its files: the loose refs under `refs/` and the
|
|
125
|
+
* lines of `packed-refs`. A loose ref takes the place of its packed line, as in git. A symbolic ref
|
|
126
|
+
* gives the hash of its target. A symbolic ref to a missing ref is not in the result. The map goes
|
|
127
|
+
* from each full name (`refs/heads/main`, `refs/remotes/origin/main`) to its hash. The repository
|
|
128
|
+
* is any working tree of it, or its git folder.
|
|
129
|
+
*/
|
|
130
|
+
export async function readRefs(repo) {
|
|
131
|
+
const { commonDir } = await gitDirsOf(repo);
|
|
132
|
+
return refsOf(commonDir);
|
|
133
|
+
}
|
|
134
|
+
/** This function reads the refs of a common git folder. */
|
|
135
|
+
async function refsOf(commonDir) {
|
|
136
|
+
await assertFilesRefs(commonDir);
|
|
137
|
+
const packed = await textOf(join(commonDir, "packed-refs"));
|
|
138
|
+
const values = new Map(packed === undefined ? [] : parsePackedRefs(packed));
|
|
139
|
+
const loose = new Map();
|
|
140
|
+
await readLoose(commonDir, "refs", loose);
|
|
141
|
+
for (const [name, value] of loose)
|
|
142
|
+
values.set(name, value);
|
|
143
|
+
const refs = new Map();
|
|
144
|
+
for (const name of [...values.keys()].sort()) {
|
|
145
|
+
const hash = hashOf(values.get(name), values);
|
|
146
|
+
if (hash !== undefined)
|
|
147
|
+
refs.set(name, hash);
|
|
148
|
+
}
|
|
149
|
+
return refs;
|
|
150
|
+
}
|
|
151
|
+
/** This function gives the head of the HEAD file of a git folder, with the hashes of `refs`. */
|
|
152
|
+
async function headOf(gitDir, refs) {
|
|
153
|
+
const text = (await textOf(join(gitDir, "HEAD")))?.trim();
|
|
154
|
+
if (text === undefined)
|
|
155
|
+
throw new Error(`The git folder ${gitDir} has no HEAD file`);
|
|
156
|
+
if (text.startsWith("ref: ")) {
|
|
157
|
+
const ref = text.slice(5).trim();
|
|
158
|
+
const head = (await refs()).get(ref);
|
|
159
|
+
const branch = ref.startsWith("refs/heads/") ? ref.slice("refs/heads/".length) : ref;
|
|
160
|
+
return { ...(head !== undefined && { head }), branch, detached: false };
|
|
161
|
+
}
|
|
162
|
+
const head = text.toLowerCase();
|
|
163
|
+
if (!HASH.test(head))
|
|
164
|
+
throw new Error(`The HEAD file of ${gitDir} holds no hash and no ref`);
|
|
165
|
+
return { head, detached: true };
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* This function reads the head of a working tree from its files: the branch and its commit, or a
|
|
169
|
+
* detached commit. A linked tree reads its HEAD through its file `.git`. A branch with no commit
|
|
170
|
+
* gives the branch with no `head`.
|
|
171
|
+
*/
|
|
172
|
+
export async function readHead(tree) {
|
|
173
|
+
const { gitDir, commonDir } = await gitDirsOf(tree);
|
|
174
|
+
return headOf(gitDir, () => refsOf(commonDir));
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* This function reads the working trees of a repository from its files, as `git worktree list`
|
|
178
|
+
* gives them. The main tree is first. Then each linked tree follows, by the name of its folder in
|
|
179
|
+
* `worktrees`. The repository is any working tree of it, or its git folder.
|
|
180
|
+
*
|
|
181
|
+
* A linked tree whose folder is gone is `prunable`. A tree with a file `locked` is `locked`. The
|
|
182
|
+
* function reads the refs one time for all trees. A common folder with the name `.git` gives a main
|
|
183
|
+
* tree. Another name gives a bare main entry.
|
|
184
|
+
*/
|
|
185
|
+
export async function readWorktrees(main) {
|
|
186
|
+
const { commonDir } = await gitDirsOf(main);
|
|
187
|
+
let cached;
|
|
188
|
+
const refs = () => (cached ??= refsOf(commonDir));
|
|
189
|
+
const bare = basename(commonDir) !== ".git";
|
|
190
|
+
const mainTree = bare
|
|
191
|
+
? { path: commonDir, detached: false, bare: true, locked: false, prunable: false }
|
|
192
|
+
: { path: dirname(commonDir), ...(await headOf(commonDir, refs)), bare: false, locked: false, prunable: false };
|
|
193
|
+
let names;
|
|
194
|
+
try {
|
|
195
|
+
names = (await readdir(join(commonDir, "worktrees"), { withFileTypes: true })).filter((entry) => entry.isDirectory()).map((entry) => entry.name);
|
|
196
|
+
}
|
|
197
|
+
catch (error) {
|
|
198
|
+
if (error.code !== "ENOENT")
|
|
199
|
+
throw error;
|
|
200
|
+
names = [];
|
|
201
|
+
}
|
|
202
|
+
const linked = await Promise.all(names.sort().map(async (name) => {
|
|
203
|
+
const dir = join(commonDir, "worktrees", name);
|
|
204
|
+
const gitdir = (await textOf(join(dir, "gitdir")))?.trim();
|
|
205
|
+
// A folder of worktrees with no gitdir file is not a linked tree
|
|
206
|
+
if (gitdir === undefined || gitdir === "")
|
|
207
|
+
return undefined;
|
|
208
|
+
const dotGit = resolve(dir, gitdir);
|
|
209
|
+
const path = dirname(dotGit);
|
|
210
|
+
const [head, locked, present] = await Promise.all([headOf(dir, refs), exists(join(dir, "locked")), exists(dotGit)]);
|
|
211
|
+
return { path, ...head, bare: false, locked, prunable: !present };
|
|
212
|
+
}));
|
|
213
|
+
return [mainTree, ...linked.filter((tree) => tree !== undefined)];
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* This function tells if a remote-tracking ref (`refs/remotes/*`) of the repository points at
|
|
217
|
+
* the commit `sha` exactly. It gives true, else "unknown": the commit can be in the history of a
|
|
218
|
+
* remote ref, and this function reads no objects. The `sha` is a full hash.
|
|
219
|
+
*/
|
|
220
|
+
export async function onRemote(repo, sha) {
|
|
221
|
+
const hash = sha.trim().toLowerCase();
|
|
222
|
+
if (!HASH.test(hash))
|
|
223
|
+
return "unknown";
|
|
224
|
+
for (const [name, value] of await readRefs(repo))
|
|
225
|
+
if (name.startsWith("refs/remotes/") && value === hash)
|
|
226
|
+
return true;
|
|
227
|
+
return "unknown";
|
|
228
|
+
}
|
|
229
|
+
//# sourceMappingURL=files.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"files.js","sourceRoot":"","sources":["../../../src/procedures/git/files.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAC3D,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAoB7D,sEAAsE;AACtE,MAAM,IAAI,GAAG,iCAAiC,CAAC;AAE/C,0EAA0E;AAC1E,MA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AAO,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,GAAG,EAAE,IAAI,CAAC,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;QACpH,OAAO,EAAE,IAAI,EAAE,GAAG,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,OAAO,EAAE,CAAC;IACpE,CAAC,CAAC,CACH,CAAC;IACF,OAAO,CAAC,QAAQ,EAAE,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,EAA2B,EAAE,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC;AAC7F,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,QAAQ,CAAC,IAAY,EAAE,GAAW;IACtD,MAAM,IAAI,GAAG,GAAG,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;IACtC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC;QAAE,OAAO,SAAS,CAAC;IACvC,KAAK,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,IAAI,MAAM,QAAQ,CAAC,IAAI,CAAC;QAAE,IAAI,IAAI,CAAC,UAAU,CAAC,eAAe,CAAC,IAAI,KAAK,KAAK,IAAI;YAAE,OAAO,IAAI,CAAC;IACtH,OAAO,SAAS,CAAC;AACnB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package",
|
|
3
3
|
"name": "@procedures/git",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.52.1",
|
|
5
5
|
"description": "Git as 45 typed procedures: status, diff, log, show, blame, commit, push, pull, branch, tag, merge, rebase, worktree, stash and more, run with argv, async and abortable.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"git",
|
|
@@ -60,9 +60,9 @@
|
|
|
60
60
|
"procedures": "./dist/register.js"
|
|
61
61
|
},
|
|
62
62
|
"dependencies": {
|
|
63
|
-
"@procedures/client": "0.
|
|
64
|
-
"@procedures/fs": "0.
|
|
65
|
-
"@procedures/shell": "0.
|
|
63
|
+
"@procedures/client": "0.52.1",
|
|
64
|
+
"@procedures/fs": "0.52.1",
|
|
65
|
+
"@procedures/shell": "0.52.1",
|
|
66
66
|
"zod": "^4.6.5"
|
|
67
67
|
},
|
|
68
68
|
"devDependencies": {
|
package/src/index.ts
CHANGED
|
@@ -18,4 +18,5 @@ export * from "./procedures/git.js";
|
|
|
18
18
|
export { git, GitError, type GitContext, type GitRunOptions } from "./procedures/git/run.js";
|
|
19
19
|
export { parsePorcelainV1, classifyStatus, type GitPorcelainEntry } from "./procedures/git/porcelain.js";
|
|
20
20
|
export { parseNumstat } from "./procedures/git/diff.js";
|
|
21
|
+
export { gitDirsOf, onRemote, readHead, readRefs, readWorktrees, type GitDirs, type GitHeadInfo } from "./procedures/git/files.js";
|
|
21
22
|
export * from "./module.js";
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The state of a repository from its files, with no git process (roadmap CLI-123).
|
|
3
|
+
*
|
|
4
|
+
* A list of many working trees with `git` starts one process or more for each tree. These
|
|
5
|
+
* functions read the same state from the files of the git folder: the `HEAD` files, the folders of
|
|
6
|
+
* `worktrees`, the loose refs and `packed-refs`. They read no objects. A repository with the
|
|
7
|
+
* `reftable` format of refs gets an error.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { readdir, readFile, stat } from "node:fs/promises";
|
|
11
|
+
import { basename, dirname, join, resolve } from "node:path";
|
|
12
|
+
import type { GitWorktreeInfo } from "../../types.js";
|
|
13
|
+
|
|
14
|
+
/** The head of a working tree: its branch, or a detached commit. */
|
|
15
|
+
export interface GitHeadInfo {
|
|
16
|
+
/** The commit of the head. A branch with no commit (a new repository) has none. */
|
|
17
|
+
head?: string | undefined;
|
|
18
|
+
/** The branch of the head, with no `refs/heads/` prefix. A detached head has none. */
|
|
19
|
+
branch?: string | undefined;
|
|
20
|
+
detached: boolean;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** The folders of git of a working tree: its own git folder, and the common folder of its repository. */
|
|
24
|
+
export interface GitDirs {
|
|
25
|
+
/** The git folder of the tree: `.git` of the main tree, or the folder in `worktrees` of a linked tree. */
|
|
26
|
+
gitDir: string;
|
|
27
|
+
/** The git folder with the refs, the objects and the folder `worktrees`. */
|
|
28
|
+
commonDir: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** A full hash of a commit: 40 hex digits (SHA-1) or 64 (SHA-256). */
|
|
32
|
+
const HASH = /^(?:[0-9a-f]{40}|[0-9a-f]{64})$/;
|
|
33
|
+
|
|
34
|
+
/** The most steps of a symbolic ref to its commit. Git stops at 5 too. */
|
|
35
|
+
const MAX_SYMREF_DEPTH = 5;
|
|
36
|
+
|
|
37
|
+
/** This function gives the text of a file, or undefined when the file does not exist. */
|
|
38
|
+
async function textOf(path: string): Promise<string | undefined> {
|
|
39
|
+
try {
|
|
40
|
+
return await readFile(path, "utf8");
|
|
41
|
+
} catch (error) {
|
|
42
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT" || (error as NodeJS.ErrnoException).code === "ENOTDIR") return undefined;
|
|
43
|
+
throw error;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** True when a path exists. */
|
|
48
|
+
async function exists(path: string): Promise<boolean> {
|
|
49
|
+
try {
|
|
50
|
+
await stat(path);
|
|
51
|
+
return true;
|
|
52
|
+
} catch {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* This function gives the git folders of a working tree, of a git folder, or of a bare repository.
|
|
59
|
+
* A linked tree has a file `.git` with the line `gitdir: <path>`. It throws for a folder that is
|
|
60
|
+
* not in git.
|
|
61
|
+
*/
|
|
62
|
+
export async function gitDirsOf(tree: string): Promise<GitDirs> {
|
|
63
|
+
const dotGit = join(tree, ".git");
|
|
64
|
+
let gitDir: string;
|
|
65
|
+
const link = await textOf(dotGit).catch((error: unknown) => {
|
|
66
|
+
// A folder .git: the main tree
|
|
67
|
+
if ((error as NodeJS.ErrnoException).code === "EISDIR") return null;
|
|
68
|
+
throw error;
|
|
69
|
+
});
|
|
70
|
+
if (link === null) gitDir = resolve(dotGit);
|
|
71
|
+
else if (link !== undefined) {
|
|
72
|
+
const match = /^gitdir: (.+)$/m.exec(link);
|
|
73
|
+
if (match === null) throw new Error(`The file ${dotGit} has no line gitdir`);
|
|
74
|
+
gitDir = resolve(tree, match[1]!.trim());
|
|
75
|
+
} else if (await exists(join(tree, "HEAD"))) {
|
|
76
|
+
// A git folder or a bare repository
|
|
77
|
+
gitDir = resolve(tree);
|
|
78
|
+
} else throw new Error(`The folder ${tree} is not a git working tree`);
|
|
79
|
+
const common = await textOf(join(gitDir, "commondir"));
|
|
80
|
+
return { gitDir, commonDir: common === undefined ? gitDir : resolve(gitDir, common.trim()) };
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** This function throws for a repository whose refs are in the reftable format: these functions do not read it. */
|
|
84
|
+
async function assertFilesRefs(commonDir: string): Promise<void> {
|
|
85
|
+
if (await exists(join(commonDir, "reftable"))) throw new Error(`The repository ${commonDir} keeps its refs in the reftable format: read them with git`);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** This function reads `packed-refs`: the name and the hash of each ref. A peeled line (`^`) and a comment are not refs. */
|
|
89
|
+
function parsePackedRefs(text: string): Map<string, string> {
|
|
90
|
+
const refs = new Map<string, string>();
|
|
91
|
+
for (const line of text.split(/\r?\n/)) {
|
|
92
|
+
if (line === "" || line.startsWith("#") || line.startsWith("^")) continue;
|
|
93
|
+
const space = line.indexOf(" ");
|
|
94
|
+
if (space < 0) continue;
|
|
95
|
+
const hash = line.slice(0, space).toLowerCase();
|
|
96
|
+
if (HASH.test(hash)) refs.set(line.slice(space + 1).trim(), hash);
|
|
97
|
+
}
|
|
98
|
+
return refs;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** This function adds the loose refs of a folder of refs to `out`: a hash, or `ref: <name>` for a symbolic ref. */
|
|
102
|
+
async function readLoose(commonDir: string, folder: string, out: Map<string, string>): Promise<void> {
|
|
103
|
+
let entries;
|
|
104
|
+
try {
|
|
105
|
+
entries = await readdir(join(commonDir, folder), { withFileTypes: true });
|
|
106
|
+
} catch (error) {
|
|
107
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT") return;
|
|
108
|
+
throw error;
|
|
109
|
+
}
|
|
110
|
+
await Promise.all(
|
|
111
|
+
entries.map(async (entry) => {
|
|
112
|
+
const name = `${folder}/${entry.name}`;
|
|
113
|
+
if (entry.isDirectory()) return readLoose(commonDir, name, out);
|
|
114
|
+
// A lock file is a write of git that did not end
|
|
115
|
+
if (!entry.isFile() || entry.name.endsWith(".lock")) return;
|
|
116
|
+
const text = (await textOf(join(commonDir, name)))?.trim();
|
|
117
|
+
if (text !== undefined && text !== "") out.set(name, text);
|
|
118
|
+
}),
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** This function gives the hash of a value of a ref: a hash, or a symbolic ref that it follows in `refs`. */
|
|
123
|
+
function hashOf(value: string, refs: ReadonlyMap<string, string>): string | undefined {
|
|
124
|
+
let current: string | undefined = value;
|
|
125
|
+
for (let depth = 0; current !== undefined && depth <= MAX_SYMREF_DEPTH; depth++) {
|
|
126
|
+
const lower = current.toLowerCase();
|
|
127
|
+
if (HASH.test(lower)) return lower;
|
|
128
|
+
if (!current.startsWith("ref: ")) return undefined;
|
|
129
|
+
current = refs.get(current.slice(5).trim());
|
|
130
|
+
}
|
|
131
|
+
return undefined;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* This function reads the refs of a repository from its files: the loose refs under `refs/` and the
|
|
136
|
+
* lines of `packed-refs`. A loose ref takes the place of its packed line, as in git. A symbolic ref
|
|
137
|
+
* gives the hash of its target. A symbolic ref to a missing ref is not in the result. The map goes
|
|
138
|
+
* from each full name (`refs/heads/main`, `refs/remotes/origin/main`) to its hash. The repository
|
|
139
|
+
* is any working tree of it, or its git folder.
|
|
140
|
+
*/
|
|
141
|
+
export async function readRefs(repo: string): Promise<Map<string, string>> {
|
|
142
|
+
const { commonDir } = await gitDirsOf(repo);
|
|
143
|
+
return refsOf(commonDir);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** This function reads the refs of a common git folder. */
|
|
147
|
+
async function refsOf(commonDir: string): Promise<Map<string, string>> {
|
|
148
|
+
await assertFilesRefs(commonDir);
|
|
149
|
+
const packed = await textOf(join(commonDir, "packed-refs"));
|
|
150
|
+
const values = new Map<string, string>(packed === undefined ? [] : parsePackedRefs(packed));
|
|
151
|
+
const loose = new Map<string, string>();
|
|
152
|
+
await readLoose(commonDir, "refs", loose);
|
|
153
|
+
for (const [name, value] of loose) values.set(name, value);
|
|
154
|
+
const refs = new Map<string, string>();
|
|
155
|
+
for (const name of [...values.keys()].sort()) {
|
|
156
|
+
const hash = hashOf(values.get(name)!, values);
|
|
157
|
+
if (hash !== undefined) refs.set(name, hash);
|
|
158
|
+
}
|
|
159
|
+
return refs;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** This function gives the head of the HEAD file of a git folder, with the hashes of `refs`. */
|
|
163
|
+
async function headOf(gitDir: string, refs: () => Promise<ReadonlyMap<string, string>>): Promise<GitHeadInfo> {
|
|
164
|
+
const text = (await textOf(join(gitDir, "HEAD")))?.trim();
|
|
165
|
+
if (text === undefined) throw new Error(`The git folder ${gitDir} has no HEAD file`);
|
|
166
|
+
if (text.startsWith("ref: ")) {
|
|
167
|
+
const ref = text.slice(5).trim();
|
|
168
|
+
const head = (await refs()).get(ref);
|
|
169
|
+
const branch = ref.startsWith("refs/heads/") ? ref.slice("refs/heads/".length) : ref;
|
|
170
|
+
return { ...(head !== undefined && { head }), branch, detached: false };
|
|
171
|
+
}
|
|
172
|
+
const head = text.toLowerCase();
|
|
173
|
+
if (!HASH.test(head)) throw new Error(`The HEAD file of ${gitDir} holds no hash and no ref`);
|
|
174
|
+
return { head, detached: true };
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* This function reads the head of a working tree from its files: the branch and its commit, or a
|
|
179
|
+
* detached commit. A linked tree reads its HEAD through its file `.git`. A branch with no commit
|
|
180
|
+
* gives the branch with no `head`.
|
|
181
|
+
*/
|
|
182
|
+
export async function readHead(tree: string): Promise<GitHeadInfo> {
|
|
183
|
+
const { gitDir, commonDir } = await gitDirsOf(tree);
|
|
184
|
+
return headOf(gitDir, () => refsOf(commonDir));
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* This function reads the working trees of a repository from its files, as `git worktree list`
|
|
189
|
+
* gives them. The main tree is first. Then each linked tree follows, by the name of its folder in
|
|
190
|
+
* `worktrees`. The repository is any working tree of it, or its git folder.
|
|
191
|
+
*
|
|
192
|
+
* A linked tree whose folder is gone is `prunable`. A tree with a file `locked` is `locked`. The
|
|
193
|
+
* function reads the refs one time for all trees. A common folder with the name `.git` gives a main
|
|
194
|
+
* tree. Another name gives a bare main entry.
|
|
195
|
+
*/
|
|
196
|
+
export async function readWorktrees(main: string): Promise<GitWorktreeInfo[]> {
|
|
197
|
+
const { commonDir } = await gitDirsOf(main);
|
|
198
|
+
let cached: Promise<Map<string, string>> | undefined;
|
|
199
|
+
const refs = () => (cached ??= refsOf(commonDir));
|
|
200
|
+
const bare = basename(commonDir) !== ".git";
|
|
201
|
+
const mainTree: GitWorktreeInfo = bare
|
|
202
|
+
? { path: commonDir, detached: false, bare: true, locked: false, prunable: false }
|
|
203
|
+
: { path: dirname(commonDir), ...(await headOf(commonDir, refs)), bare: false, locked: false, prunable: false };
|
|
204
|
+
let names: string[];
|
|
205
|
+
try {
|
|
206
|
+
names = (await readdir(join(commonDir, "worktrees"), { withFileTypes: true })).filter((entry) => entry.isDirectory()).map((entry) => entry.name);
|
|
207
|
+
} catch (error) {
|
|
208
|
+
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
|
|
209
|
+
names = [];
|
|
210
|
+
}
|
|
211
|
+
const linked = await Promise.all(
|
|
212
|
+
names.sort().map(async (name): Promise<GitWorktreeInfo | undefined> => {
|
|
213
|
+
const dir = join(commonDir, "worktrees", name);
|
|
214
|
+
const gitdir = (await textOf(join(dir, "gitdir")))?.trim();
|
|
215
|
+
// A folder of worktrees with no gitdir file is not a linked tree
|
|
216
|
+
if (gitdir === undefined || gitdir === "") return undefined;
|
|
217
|
+
const dotGit = resolve(dir, gitdir);
|
|
218
|
+
const path = dirname(dotGit);
|
|
219
|
+
const [head, locked, present] = await Promise.all([headOf(dir, refs), exists(join(dir, "locked")), exists(dotGit)]);
|
|
220
|
+
return { path, ...head, bare: false, locked, prunable: !present };
|
|
221
|
+
}),
|
|
222
|
+
);
|
|
223
|
+
return [mainTree, ...linked.filter((tree): tree is GitWorktreeInfo => tree !== undefined)];
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* This function tells if a remote-tracking ref (`refs/remotes/*`) of the repository points at
|
|
228
|
+
* the commit `sha` exactly. It gives true, else "unknown": the commit can be in the history of a
|
|
229
|
+
* remote ref, and this function reads no objects. The `sha` is a full hash.
|
|
230
|
+
*/
|
|
231
|
+
export async function onRemote(repo: string, sha: string): Promise<true | "unknown"> {
|
|
232
|
+
const hash = sha.trim().toLowerCase();
|
|
233
|
+
if (!HASH.test(hash)) return "unknown";
|
|
234
|
+
for (const [name, value] of await readRefs(repo)) if (name.startsWith("refs/remotes/") && value === hash) return true;
|
|
235
|
+
return "unknown";
|
|
236
|
+
}
|