@forwardimpact/libutil 0.1.94 → 0.1.95
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 +21 -201
- package/package.json +1 -1
- package/src/git-client.js +272 -4
- package/src/rules.js +8 -3
package/LICENSE
CHANGED
|
@@ -1,201 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
-
exercising permissions granted by this License.
|
|
25
|
-
|
|
26
|
-
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
-
including but not limited to software source code, documentation
|
|
28
|
-
source, and configuration files.
|
|
29
|
-
|
|
30
|
-
"Object" form shall mean any form resulting from mechanical
|
|
31
|
-
transformation or translation of a Source form, including but
|
|
32
|
-
not limited to compiled object code, generated documentation,
|
|
33
|
-
and conversions to other media types.
|
|
34
|
-
|
|
35
|
-
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
-
Object form, made available under the License, as indicated by a
|
|
37
|
-
copyright notice that is included in or attached to the work
|
|
38
|
-
(an example is provided in the Appendix below).
|
|
39
|
-
|
|
40
|
-
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
-
form, that is based on (or derived from) the Work and for which the
|
|
42
|
-
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
-
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
-
of this License, Derivative Works shall not include works that remain
|
|
45
|
-
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
-
the Work and Derivative Works thereof.
|
|
47
|
-
|
|
48
|
-
"Contribution" shall mean any work of authorship, including
|
|
49
|
-
the original version of the Work and any modifications or additions
|
|
50
|
-
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
-
submitted to the Licensor for inclusion in the Work by the copyright owner
|
|
52
|
-
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
-
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
-
means any form of electronic, verbal, or written communication sent
|
|
55
|
-
to the Licensor or its representatives, including but not limited to
|
|
56
|
-
communication on electronic mailing lists, source code control systems,
|
|
57
|
-
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
-
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
-
excluding communication that is conspicuously marked or otherwise
|
|
60
|
-
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
-
|
|
62
|
-
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
-
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
-
subsequently incorporated within the Work.
|
|
65
|
-
|
|
66
|
-
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
-
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
-
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
-
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
-
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
-
Work and such Derivative Works in Source or Object form.
|
|
72
|
-
|
|
73
|
-
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
-
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
-
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
-
(except as stated in this section) patent license to make, have made,
|
|
77
|
-
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
-
where such license applies only to those patent claims licensable
|
|
79
|
-
by such Contributor that are necessarily infringed by their
|
|
80
|
-
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
-
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
-
institute patent litigation against any entity (including a
|
|
83
|
-
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
-
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
-
or contributory patent infringement, then any patent licenses
|
|
86
|
-
granted to You under this License for that Work shall terminate
|
|
87
|
-
as of the date such litigation is filed.
|
|
88
|
-
|
|
89
|
-
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
-
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
-
modifications, and in Source or Object form, provided that You
|
|
92
|
-
meet the following conditions:
|
|
93
|
-
|
|
94
|
-
(a) You must give any other recipients of the Work or
|
|
95
|
-
Derivative Works a copy of this License; and
|
|
96
|
-
|
|
97
|
-
(b) You must cause any modified files to carry prominent notices
|
|
98
|
-
stating that You changed the files; and
|
|
99
|
-
|
|
100
|
-
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
-
that You distribute, all copyright, patent, trademark, and
|
|
102
|
-
attribution notices from the Source form of the Work,
|
|
103
|
-
excluding those notices that do not pertain to any part of
|
|
104
|
-
the Derivative Works; and
|
|
105
|
-
|
|
106
|
-
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
-
distribution, then any Derivative Works that You distribute must
|
|
108
|
-
include a readable copy of the attribution notices contained
|
|
109
|
-
within such NOTICE file, excluding those notices that do not
|
|
110
|
-
pertain to any part of the Derivative Works, in at least one
|
|
111
|
-
of the following places: within a NOTICE text file distributed
|
|
112
|
-
as part of the Derivative Works; within the Source form or
|
|
113
|
-
documentation, if provided along with the Derivative Works; or,
|
|
114
|
-
within a display generated by the Derivative Works, if and
|
|
115
|
-
wherever such third-party notices normally appear. The contents
|
|
116
|
-
of the NOTICE file are for informational purposes only and
|
|
117
|
-
do not modify the License. You may add Your own attribution
|
|
118
|
-
notices within Derivative Works that You distribute, alongside
|
|
119
|
-
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
-
that such additional attribution notices cannot be construed
|
|
121
|
-
as modifying the License.
|
|
122
|
-
|
|
123
|
-
You may add Your own copyright statement to Your modifications and
|
|
124
|
-
may provide additional or different license terms and conditions
|
|
125
|
-
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
-
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
-
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
-
the conditions stated in this License.
|
|
129
|
-
|
|
130
|
-
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
-
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
-
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
-
this License, without any additional terms or conditions.
|
|
134
|
-
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
-
the terms of any separate license agreement you may have executed
|
|
136
|
-
with Licensor regarding such Contributions.
|
|
137
|
-
|
|
138
|
-
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
-
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
-
except as required for reasonable and customary use in describing the
|
|
141
|
-
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
-
|
|
143
|
-
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
-
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
-
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
-
implied, including, without limitation, any warranties or conditions
|
|
148
|
-
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
-
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
-
appropriateness of using or redistributing the Work and assume any
|
|
151
|
-
risks associated with Your exercise of permissions under this License.
|
|
152
|
-
|
|
153
|
-
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
-
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
-
unless required by applicable law (such as deliberate and grossly
|
|
156
|
-
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
-
liable to You for damages, including any direct, indirect, special,
|
|
158
|
-
incidental, or consequential damages of any character arising as a
|
|
159
|
-
result of this License or out of the use or inability to use the
|
|
160
|
-
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
-
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
-
other commercial damages or losses), even if such Contributor
|
|
163
|
-
has been advised of the possibility of such damages.
|
|
164
|
-
|
|
165
|
-
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
-
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
-
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
-
or other liability obligations and/or rights consistent with this
|
|
169
|
-
License. However, in accepting such obligations, You may act only
|
|
170
|
-
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
-
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
-
defend, and hold each Contributor harmless for any liability
|
|
173
|
-
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
-
of your accepting any such warranty or additional liability.
|
|
175
|
-
|
|
176
|
-
END OF TERMS AND CONDITIONS
|
|
177
|
-
|
|
178
|
-
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
-
|
|
180
|
-
To apply the Apache License to your work, attach the following
|
|
181
|
-
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
-
replaced with your own identifying information. (Don't include
|
|
183
|
-
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
-
comment syntax for the file format. We also recommend that a
|
|
185
|
-
file or class name and description of purpose be included on the
|
|
186
|
-
same "printed page" as the copyright notice for easier
|
|
187
|
-
identification within third-party archives.
|
|
188
|
-
|
|
189
|
-
Copyright 2026 Dick Olsson
|
|
190
|
-
|
|
191
|
-
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
-
you may not use this file except in compliance with the License.
|
|
193
|
-
You may obtain a copy of the License at
|
|
194
|
-
|
|
195
|
-
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
-
|
|
197
|
-
Unless required by applicable law or agreed to in writing, software
|
|
198
|
-
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
-
See the License for the specific language governing permissions and
|
|
201
|
-
limitations under the License.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 Zachary Rice
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/package.json
CHANGED
package/src/git-client.js
CHANGED
|
@@ -17,6 +17,33 @@ export class GitError extends Error {
|
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
+
// Resolve a `git diff` "+++ <target>" header to a working-tree path, or null
|
|
21
|
+
// for a deletion ("+++ /dev/null"). The "b/" prefix is git's destination side.
|
|
22
|
+
function parseDiffTarget(headerLine) {
|
|
23
|
+
const target = headerLine.slice(4).trim();
|
|
24
|
+
return target.startsWith("b/") ? target.slice(2) : null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Reject `:`-prefixed pathspec entries before they reach git. A leading `:`
|
|
29
|
+
* marks git pathspec magic (`:/`, `:(exclude)…`, `:(glob)…`), which the `--`
|
|
30
|
+
* separator does NOT neutralise — `--` ends OPTION parsing, not pathspec magic.
|
|
31
|
+
* A dynamically derived filename starting with `:` could therefore widen a
|
|
32
|
+
* scoped commit beyond the named files, so the path-forwarding methods
|
|
33
|
+
* (`commitPaths`, `status`) reject it at entry rather than passing it to git.
|
|
34
|
+
* @param {string[]} [paths]
|
|
35
|
+
*/
|
|
36
|
+
function assertSafePaths(paths) {
|
|
37
|
+
for (const p of paths ?? []) {
|
|
38
|
+
if (typeof p === "string" && p.startsWith(":")) {
|
|
39
|
+
throw new Error(
|
|
40
|
+
`unsafe pathspec: ':'-prefixed entries are rejected (got '${p}'); ` +
|
|
41
|
+
"':' magic survives the '--' separator and could widen the commit",
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
20
47
|
/**
|
|
21
48
|
* Typed wrapper over the `git` CLI. All shelling-out flows through the
|
|
22
49
|
* injected `runtime.subprocess`, so callers never import `node:child_process`
|
|
@@ -56,8 +83,15 @@ export class GitClient {
|
|
|
56
83
|
return this.#runRaw(args, { cwd });
|
|
57
84
|
}
|
|
58
85
|
|
|
59
|
-
/**
|
|
86
|
+
/**
|
|
87
|
+
* Return `git status --porcelain` output, optionally limited to `paths`.
|
|
88
|
+
* @param {object} [options]
|
|
89
|
+
* @param {string} [options.cwd]
|
|
90
|
+
* @param {string[]} [options.paths] - Pathspecs to scope the status to.
|
|
91
|
+
* `:`-prefixed entries are rejected ({@link assertSafePaths}).
|
|
92
|
+
*/
|
|
60
93
|
async status({ cwd, paths } = {}) {
|
|
94
|
+
assertSafePaths(paths);
|
|
61
95
|
const args = ["status", "--porcelain"];
|
|
62
96
|
if (paths?.length) args.push("--", ...paths);
|
|
63
97
|
return this.#runRaw(args, { cwd });
|
|
@@ -77,12 +111,107 @@ export class GitClient {
|
|
|
77
111
|
return this.#runRaw(["rebase", "--abort"], { cwd, allowFailure: true });
|
|
78
112
|
}
|
|
79
113
|
|
|
80
|
-
/**
|
|
81
|
-
|
|
114
|
+
/**
|
|
115
|
+
* Move HEAD to `ref` without touching the index or working tree
|
|
116
|
+
* (`git reset --soft`). Drops local commits ahead of `ref` while leaving every
|
|
117
|
+
* uncommitted change — including foreign residue from parallel writers — in
|
|
118
|
+
* place, unlike a `--hard` reset.
|
|
119
|
+
*/
|
|
120
|
+
async resetSoft(ref, { cwd } = {}) {
|
|
121
|
+
return this.#runRaw(["reset", "--soft", ref], { cwd });
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Reset only `paths` to their content at `ref` (`git checkout <ref> --
|
|
126
|
+
* <paths>`), leaving the rest of the working tree untouched. With
|
|
127
|
+
* `allowMissing`, a path absent on `ref` (e.g. a founding write of a file the
|
|
128
|
+
* tip does not yet carry) yields a non-zero result instead of throwing.
|
|
129
|
+
* @param {string} ref
|
|
130
|
+
* @param {string[]} paths
|
|
131
|
+
* @param {{cwd?: string, allowMissing?: boolean}} [options]
|
|
132
|
+
*/
|
|
133
|
+
async checkoutPaths(ref, paths, { cwd, allowMissing = false } = {}) {
|
|
134
|
+
return this.#runRaw(["checkout", ref, "--", ...paths], {
|
|
135
|
+
cwd,
|
|
136
|
+
allowFailure: allowMissing,
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Merge `ref` into the current branch resolving conflicts with `-X ours`.
|
|
142
|
+
* With `allowFailure`, a non-zero exit resolves to the raw result instead of
|
|
143
|
+
* throwing, so the caller can abort and refuse rather than strand a
|
|
144
|
+
* mid-merge tree.
|
|
145
|
+
*/
|
|
146
|
+
async mergeOursStrategy({
|
|
147
|
+
cwd,
|
|
148
|
+
ref,
|
|
149
|
+
autostash = false,
|
|
150
|
+
allowFailure = false,
|
|
151
|
+
}) {
|
|
82
152
|
const args = ["merge"];
|
|
83
153
|
if (autostash) args.push("--autostash");
|
|
84
154
|
args.push("-X", "ours", "--no-edit", ref);
|
|
85
|
-
return this.#runRaw(args, { cwd });
|
|
155
|
+
return this.#runRaw(args, { cwd, allowFailure });
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** Abort an in-progress merge, restoring the pre-merge state. */
|
|
159
|
+
async mergeAbort({ cwd } = {}) {
|
|
160
|
+
return this.#runRaw(["merge", "--abort"], { cwd, allowFailure: true });
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Paths with an unmerged index entry (`git status --porcelain` rows whose XY
|
|
165
|
+
* status is a U-family code: `UU`/`AA`/`DD`/`AU`/`UA`/`DU`/`UD`). Decidable
|
|
166
|
+
* from the index alone — no history resolution — so it holds on a shallow
|
|
167
|
+
* clone.
|
|
168
|
+
*/
|
|
169
|
+
async unmergedPaths({ cwd } = {}) {
|
|
170
|
+
const result = await this.#runRaw(["status", "--porcelain"], { cwd });
|
|
171
|
+
const unmerged = /^(DD|AU|UD|UA|DU|AA|UU)/;
|
|
172
|
+
return result.stdout
|
|
173
|
+
.split("\n")
|
|
174
|
+
.filter((line) => unmerged.test(line))
|
|
175
|
+
.map((line) => line.slice(3).trim())
|
|
176
|
+
.filter(Boolean);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Whether the repository is mid-merge: an unmerged index entry exists or a
|
|
181
|
+
* MERGE_HEAD is pinned. Both signals are local to the clone, so this holds at
|
|
182
|
+
* any fetch depth.
|
|
183
|
+
*/
|
|
184
|
+
async isMidMerge({ cwd } = {}) {
|
|
185
|
+
if ((await this.unmergedPaths({ cwd })).length > 0) return true;
|
|
186
|
+
const head = await this.#runRaw(
|
|
187
|
+
["rev-parse", "-q", "--verify", "MERGE_HEAD"],
|
|
188
|
+
{ cwd, allowFailure: true },
|
|
189
|
+
);
|
|
190
|
+
return head.exitCode === 0;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* The content introduced by `range` (e.g. `origin/master..HEAD`), grouped by
|
|
195
|
+
* path. Returns a `Map<path, addedText>` where `addedText` is the added side
|
|
196
|
+
* of the diff with the leading `+` of each hunk line stripped and the
|
|
197
|
+
* `+++`/`---` file headers excluded, so line-anchored scanners match at
|
|
198
|
+
* column 1. Throws {@link GitError} on a non-zero exit (e.g. an unresolvable
|
|
199
|
+
* ref) — callers must treat a throw as refuse-with-reason, never a silent
|
|
200
|
+
* pass.
|
|
201
|
+
*/
|
|
202
|
+
async introducedByFile(range, { cwd } = {}) {
|
|
203
|
+
const result = await this.#runRaw(["diff", "--no-color", range], { cwd });
|
|
204
|
+
const byFile = new Map();
|
|
205
|
+
let current = null;
|
|
206
|
+
for (const line of result.stdout.split("\n")) {
|
|
207
|
+
if (line.startsWith("+++ ")) {
|
|
208
|
+
current = parseDiffTarget(line);
|
|
209
|
+
if (current && !byFile.has(current)) byFile.set(current, []);
|
|
210
|
+
} else if (current && line.startsWith("+")) {
|
|
211
|
+
byFile.get(current).push(line.slice(1));
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return new Map([...byFile].map(([p, lines]) => [p, lines.join("\n")]));
|
|
86
215
|
}
|
|
87
216
|
|
|
88
217
|
/** Stage all changes and commit with `message`. */
|
|
@@ -97,8 +226,14 @@ export class GitClient {
|
|
|
97
226
|
* Stage and commit only `paths`, leaving the rest of the working tree
|
|
98
227
|
* untouched. The commit carries the same pathspec so content staged by
|
|
99
228
|
* other writers is never swept in.
|
|
229
|
+
* @param {string} message
|
|
230
|
+
* @param {string[]} paths - Pathspecs to stage and commit. `:`-prefixed
|
|
231
|
+
* entries are rejected ({@link assertSafePaths}) so a dynamically derived
|
|
232
|
+
* filename can never widen the commit beyond the named files.
|
|
233
|
+
* @param {{cwd?: string, author?: string}} [options]
|
|
100
234
|
*/
|
|
101
235
|
async commitPaths(message, paths, { cwd, author } = {}) {
|
|
236
|
+
assertSafePaths(paths);
|
|
102
237
|
await this.#runRaw(["add", "--", ...paths], { cwd });
|
|
103
238
|
const args = ["commit", "-m", message];
|
|
104
239
|
if (author) args.push("--author", author);
|
|
@@ -114,6 +249,63 @@ export class GitClient {
|
|
|
114
249
|
return this.#runRaw(args, { cwd });
|
|
115
250
|
}
|
|
116
251
|
|
|
252
|
+
/**
|
|
253
|
+
* List commits in `ref` authored by `author`, newest first, as
|
|
254
|
+
* `{sha, when}` where `when` is the commit epoch seconds (`%ct`). Excludes
|
|
255
|
+
* merge commits. Returns `[]` on any git failure (e.g. an unborn ref).
|
|
256
|
+
* @param {string} author - Author match (git `--author=` pattern).
|
|
257
|
+
* @param {{cwd?: string, ref?: string}} [opts]
|
|
258
|
+
* @returns {Promise<Array<{sha: string, when: number}>>}
|
|
259
|
+
*/
|
|
260
|
+
async logByAuthor(author, { cwd, ref = "HEAD" } = {}) {
|
|
261
|
+
const r = await this.#runRaw(
|
|
262
|
+
["log", ref, `--author=${author}`, "--no-merges", "--format=%H %ct"],
|
|
263
|
+
{ cwd, allowFailure: true },
|
|
264
|
+
);
|
|
265
|
+
if (r.exitCode !== 0) return [];
|
|
266
|
+
return r.stdout
|
|
267
|
+
.trim()
|
|
268
|
+
.split("\n")
|
|
269
|
+
.filter(Boolean)
|
|
270
|
+
.map((line) => {
|
|
271
|
+
const [sha, ct] = line.split(" ");
|
|
272
|
+
return { sha, when: Number.parseInt(ct, 10) };
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Unified-diff text (`--unified=0`) of a two-tree `range` (e.g. `"A B"` or
|
|
278
|
+
* `"A..B"`), or `null` on git failure. A two-tree range diff (never a
|
|
279
|
+
* single-commit `git show`) diffs merge commits correctly. An empty string
|
|
280
|
+
* is a legitimate empty diff; `null` is an error — callers distinguish them.
|
|
281
|
+
* @param {string} range - Range spec; split on spaces into git args.
|
|
282
|
+
* @param {{cwd?: string}} [opts]
|
|
283
|
+
* @returns {Promise<string|null>}
|
|
284
|
+
*/
|
|
285
|
+
async diffRange(range, { cwd } = {}) {
|
|
286
|
+
const r = await this.#runRaw(
|
|
287
|
+
["diff", "--no-color", "--unified=0", ...range.split(" ")],
|
|
288
|
+
{ cwd, allowFailure: true },
|
|
289
|
+
);
|
|
290
|
+
return r.exitCode === 0 ? r.stdout : null;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Contents of `filePath` at `ref` (`git show ref:path`), or `null` when the
|
|
295
|
+
* path is absent at that ref.
|
|
296
|
+
* @param {string} ref - The ref to read from (e.g. `"origin/master"`).
|
|
297
|
+
* @param {string} filePath - Repo-relative path.
|
|
298
|
+
* @param {{cwd?: string}} [opts]
|
|
299
|
+
* @returns {Promise<string|null>}
|
|
300
|
+
*/
|
|
301
|
+
async showFile(ref, filePath, { cwd } = {}) {
|
|
302
|
+
const r = await this.#runRaw(["show", `${ref}:${filePath}`], {
|
|
303
|
+
cwd,
|
|
304
|
+
allowFailure: true,
|
|
305
|
+
});
|
|
306
|
+
return r.exitCode === 0 ? r.stdout : null;
|
|
307
|
+
}
|
|
308
|
+
|
|
117
309
|
/** Count commits in `range` (`git rev-list --count`). */
|
|
118
310
|
async revListCount(range, { cwd }) {
|
|
119
311
|
const result = await this.#runRaw(["rev-list", "--count", range], { cwd });
|
|
@@ -145,6 +337,82 @@ export class GitClient {
|
|
|
145
337
|
return result.stdout.trim();
|
|
146
338
|
}
|
|
147
339
|
|
|
340
|
+
/**
|
|
341
|
+
* The short name of the branch HEAD points at, or "" when HEAD is detached.
|
|
342
|
+
* An unborn HEAD on a branch (no commits yet) still returns that branch name —
|
|
343
|
+
* `symbolic-ref` reads the ref HEAD targets, not whether it resolves.
|
|
344
|
+
*/
|
|
345
|
+
async headBranch({ cwd } = {}) {
|
|
346
|
+
const r = await this.#runRaw(["symbolic-ref", "--short", "-q", "HEAD"], {
|
|
347
|
+
cwd,
|
|
348
|
+
allowFailure: true,
|
|
349
|
+
});
|
|
350
|
+
return r.stdout.trim();
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/** Whether `ref` resolves to a commit (`rev-parse --verify`). */
|
|
354
|
+
async refExists(ref, { cwd } = {}) {
|
|
355
|
+
const r = await this.#runRaw(
|
|
356
|
+
["rev-parse", "--verify", "-q", `${ref}^{commit}`],
|
|
357
|
+
{ cwd, allowFailure: true },
|
|
358
|
+
);
|
|
359
|
+
return r.exitCode === 0;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* Whether `a` and `b` share a merge-base within the fetched history.
|
|
364
|
+
* `git merge-base` exits 1 (not an error) when no base exists, so this runs
|
|
365
|
+
* with `allowFailure` and reads the exit code rather than throwing.
|
|
366
|
+
*/
|
|
367
|
+
async mergeBaseExists(a, b, { cwd } = {}) {
|
|
368
|
+
const r = await this.#runRaw(["merge-base", a, b], {
|
|
369
|
+
cwd,
|
|
370
|
+
allowFailure: true,
|
|
371
|
+
});
|
|
372
|
+
return r.exitCode === 0;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* Whether `branch` exists on `remote` (`ls-remote --heads`). Throws a
|
|
377
|
+
* {@link GitError} when the remote cannot be observed, so callers can
|
|
378
|
+
* distinguish a genuinely empty remote from a failed observation.
|
|
379
|
+
*/
|
|
380
|
+
async remoteBranchExists(remote, branch, { cwd } = {}) {
|
|
381
|
+
const r = await this.#runRaw(["ls-remote", "--heads", remote, branch], {
|
|
382
|
+
cwd,
|
|
383
|
+
});
|
|
384
|
+
return r.stdout.trim() !== "";
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* Deepen history to full depth for `branch` from `remote`. Runs with
|
|
389
|
+
* `allowFailure` because `--unshallow` errors on a complete clone; callers
|
|
390
|
+
* gate this behind a shallow-clone check and treat a non-zero exit as
|
|
391
|
+
* "deepening failed".
|
|
392
|
+
*/
|
|
393
|
+
async fetchDeepen(remote, branch, { cwd } = {}) {
|
|
394
|
+
return this.#runRaw(["fetch", "--unshallow", remote, branch], {
|
|
395
|
+
cwd,
|
|
396
|
+
allowFailure: true,
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* List the tags and heads a remote `url` exposes, without cloning. Returns
|
|
402
|
+
* the raw `{ stdout, stderr, exitCode }` so callers can distinguish exit 0
|
|
403
|
+
* (refs listed), exit 128 (auth demand — absent or private), and transport
|
|
404
|
+
* faults. Auth and env are handled by `#runRaw`: a tokenless client transports
|
|
405
|
+
* anonymously, and `GIT_TERMINAL_PROMPT` is whatever the runtime's `proc.env`
|
|
406
|
+
* carries.
|
|
407
|
+
* @param {string} url - The repository URL (e.g. `https://github.com/owner/repo`).
|
|
408
|
+
* @returns {Promise<{stdout: string, stderr: string, exitCode: number}>}
|
|
409
|
+
*/
|
|
410
|
+
async lsRemote(url) {
|
|
411
|
+
return this.#runRaw(["ls-remote", "--tags", "--heads", url], {
|
|
412
|
+
allowFailure: true,
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
|
|
148
416
|
/** Return a new client that threads `token` into the git env. */
|
|
149
417
|
withAuth(token) {
|
|
150
418
|
return new GitClient({ runtime: this.#runtime, token });
|
package/src/rules.js
CHANGED
|
@@ -11,8 +11,10 @@
|
|
|
11
11
|
// an array of finding items. Each item is a plain object whose fields the
|
|
12
12
|
// rule's `message` function reads (e.g., `{ value: 572 }`).
|
|
13
13
|
// - `message(subject, item, ctx)` builds the human-readable message string.
|
|
14
|
-
// - `hint` is an optional
|
|
15
|
-
//
|
|
14
|
+
// - `hint` is an optional action prompt rendered by the text emitter: either a
|
|
15
|
+
// static string or a `(subject, item, ctx) => string` resolved once per
|
|
16
|
+
// finding. A function hint lets a rule emit a fully resolved command from the
|
|
17
|
+
// subject (e.g. a `rotate --agent <prefix>` naming the flagged file's agent).
|
|
16
18
|
//
|
|
17
19
|
// `ctx` is passed unchanged to every rule. Cross-subject state (e.g., a
|
|
18
20
|
// duplicate-detection map) lives on `ctx` and is mutated by the rule during
|
|
@@ -38,7 +40,10 @@ function applyRule(rule, subject, ctx) {
|
|
|
38
40
|
path: subject.path ?? null,
|
|
39
41
|
lineNo: item.lineNo ?? subject.lineNo ?? null,
|
|
40
42
|
message: rule.message(subject, item, ctx),
|
|
41
|
-
hint:
|
|
43
|
+
hint:
|
|
44
|
+
typeof rule.hint === "function"
|
|
45
|
+
? rule.hint(subject, item, ctx)
|
|
46
|
+
: (rule.hint ?? null),
|
|
42
47
|
}));
|
|
43
48
|
}
|
|
44
49
|
|