git-changed 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 +21 -0
- package/README.md +328 -0
- package/base.d.ts +17 -0
- package/base.js +49 -0
- package/changed.d.ts +49 -0
- package/changed.js +269 -0
- package/cli.d.ts +14 -0
- package/cli.js +200 -0
- package/esm/base.d.ts +17 -0
- package/esm/base.js +45 -0
- package/esm/changed.d.ts +49 -0
- package/esm/changed.js +262 -0
- package/esm/cli.d.ts +14 -0
- package/esm/cli.js +196 -0
- package/esm/git.d.ts +22 -0
- package/esm/git.js +55 -0
- package/esm/index.d.ts +5 -0
- package/esm/index.js +4 -0
- package/esm/match.d.ts +10 -0
- package/esm/match.js +90 -0
- package/esm/types.d.ts +69 -0
- package/esm/types.js +1 -0
- package/git.d.ts +22 -0
- package/git.js +64 -0
- package/index.d.ts +5 -0
- package/index.js +20 -0
- package/match.d.ts +10 -0
- package/match.js +95 -0
- package/package.json +47 -0
- package/types.d.ts +69 -0
- package/types.js +2 -0
package/changed.js
ADDED
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GitChanged = void 0;
|
|
4
|
+
exports.changedFiles = changedFiles;
|
|
5
|
+
exports.changedPaths = changedPaths;
|
|
6
|
+
exports.toAbsolute = toAbsolute;
|
|
7
|
+
const fs_1 = require("fs");
|
|
8
|
+
const path_1 = require("path");
|
|
9
|
+
const base_1 = require("./base");
|
|
10
|
+
const git_1 = require("./git");
|
|
11
|
+
const match_1 = require("./match");
|
|
12
|
+
/** Map a `git diff --name-status` letter to a status. */
|
|
13
|
+
function diffStatus(code) {
|
|
14
|
+
switch (code[0]) {
|
|
15
|
+
case 'A':
|
|
16
|
+
return 'added';
|
|
17
|
+
case 'M':
|
|
18
|
+
case 'T':
|
|
19
|
+
return 'modified';
|
|
20
|
+
case 'D':
|
|
21
|
+
return 'deleted';
|
|
22
|
+
case 'R':
|
|
23
|
+
case 'C':
|
|
24
|
+
return 'renamed';
|
|
25
|
+
default:
|
|
26
|
+
return 'unknown';
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/** Map the two-letter `git status --porcelain` code to a status. */
|
|
30
|
+
function porcelainStatus(code) {
|
|
31
|
+
if (code === '??')
|
|
32
|
+
return 'untracked';
|
|
33
|
+
// Either column can carry the interesting letter: `M ` is staged, ` M` is
|
|
34
|
+
// unstaged, `MM` is both. Prefer the first non-space.
|
|
35
|
+
const letter = code.trim()[0] ?? '';
|
|
36
|
+
switch (letter) {
|
|
37
|
+
case 'A':
|
|
38
|
+
return 'added';
|
|
39
|
+
case 'D':
|
|
40
|
+
return 'deleted';
|
|
41
|
+
case 'R':
|
|
42
|
+
case 'C':
|
|
43
|
+
return 'renamed';
|
|
44
|
+
case 'M':
|
|
45
|
+
case 'T':
|
|
46
|
+
return 'modified';
|
|
47
|
+
default:
|
|
48
|
+
return 'unknown';
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/** Strip git's quoting of paths with unusual characters. */
|
|
52
|
+
function unquote(p) {
|
|
53
|
+
if (!p.startsWith('"'))
|
|
54
|
+
return p;
|
|
55
|
+
const inner = p.slice(1, -1);
|
|
56
|
+
try {
|
|
57
|
+
return JSON.parse(`"${inner}"`);
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
return inner;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Merge a path into the accumulator. A path can show up twice — committed
|
|
65
|
+
* against the base *and* modified again in the working tree — so keep the union
|
|
66
|
+
* of the flags and let the later, more specific status win.
|
|
67
|
+
*/
|
|
68
|
+
function record(map, path, next) {
|
|
69
|
+
const prev = map.get(path);
|
|
70
|
+
if (!prev) {
|
|
71
|
+
map.set(path, next);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
map.set(path, {
|
|
75
|
+
// A path deleted in the working tree is deleted, whatever the base said.
|
|
76
|
+
status: next.status === 'deleted' ? 'deleted' : prev.status,
|
|
77
|
+
from: prev.from ?? next.from,
|
|
78
|
+
committed: prev.committed || next.committed,
|
|
79
|
+
worktree: prev.worktree || next.worktree
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
/** Committed changes between the merge base (or the base ref) and `HEAD`. */
|
|
83
|
+
function collectCommitted(map, cwd, from) {
|
|
84
|
+
// -M detects renames so the destination is reported instead of an
|
|
85
|
+
// add/delete pair.
|
|
86
|
+
const out = (0, git_1.tryGit)(['diff', '--name-status', '-M', from, 'HEAD'], cwd) ?? '';
|
|
87
|
+
for (const line of out.split('\n')) {
|
|
88
|
+
if (!line.trim())
|
|
89
|
+
continue;
|
|
90
|
+
const parts = line.split('\t');
|
|
91
|
+
const code = parts[0];
|
|
92
|
+
const status = diffStatus(code);
|
|
93
|
+
if (status === 'renamed' && parts.length >= 3) {
|
|
94
|
+
// R100<TAB>old<TAB>new — the destination is what exists now.
|
|
95
|
+
record(map, unquote(parts[2]), {
|
|
96
|
+
status,
|
|
97
|
+
from: unquote(parts[1]),
|
|
98
|
+
committed: true,
|
|
99
|
+
worktree: false
|
|
100
|
+
});
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
if (parts.length < 2)
|
|
104
|
+
continue;
|
|
105
|
+
record(map, unquote(parts[1]), { status, committed: true, worktree: false });
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
/** Uncommitted and untracked changes in the working tree. */
|
|
109
|
+
function collectWorktree(map, cwd, untracked) {
|
|
110
|
+
// -uall lists files inside a new directory individually. Without it git
|
|
111
|
+
// reports the directory as a single entry and every file under it is invisible
|
|
112
|
+
// to the caller — the bug this utility exists to stop re-implementing.
|
|
113
|
+
const args = ['status', '--porcelain', untracked ? '-uall' : '-uno'];
|
|
114
|
+
const out = (0, git_1.tryGit)(args, cwd) ?? '';
|
|
115
|
+
for (const raw of out.split('\n')) {
|
|
116
|
+
const line = raw.replace(/\s+$/, '');
|
|
117
|
+
if (!line)
|
|
118
|
+
continue;
|
|
119
|
+
const code = line.slice(0, 2);
|
|
120
|
+
let path = line.slice(3);
|
|
121
|
+
let from;
|
|
122
|
+
const arrow = path.indexOf(' -> ');
|
|
123
|
+
if (arrow !== -1) {
|
|
124
|
+
from = unquote(path.slice(0, arrow));
|
|
125
|
+
path = path.slice(arrow + 4);
|
|
126
|
+
}
|
|
127
|
+
path = unquote(path);
|
|
128
|
+
if (!path)
|
|
129
|
+
continue;
|
|
130
|
+
record(map, path, {
|
|
131
|
+
status: porcelainStatus(code),
|
|
132
|
+
from,
|
|
133
|
+
committed: false,
|
|
134
|
+
worktree: true
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Files changed relative to a base ref, unioned with working-tree changes.
|
|
140
|
+
*
|
|
141
|
+
* ```ts
|
|
142
|
+
* changedFiles({ ext: '.sql', exclude: ['**\/generated/**'] });
|
|
143
|
+
* // → { files: [...], paths: [...], base: 'origin/main', source: 'merge-base' }
|
|
144
|
+
* ```
|
|
145
|
+
*
|
|
146
|
+
* Resolution order for the base is `options.base` → `$GITHUB_BASE_REF` → the
|
|
147
|
+
* repository default branch; pass `base: false` to skip it entirely. When no
|
|
148
|
+
* base is available the result falls back to the working tree with
|
|
149
|
+
* `source: 'worktree'` rather than throwing, so a gate still checks what the
|
|
150
|
+
* author is editing on a shallow or detached checkout.
|
|
151
|
+
*/
|
|
152
|
+
function changedFiles(options = {}) {
|
|
153
|
+
const cwd = (0, path_1.resolve)(options.cwd ?? process.cwd());
|
|
154
|
+
if (!(0, git_1.isRepo)(cwd)) {
|
|
155
|
+
throw new git_1.GitChangedError(`Not a git repository: ${cwd}`);
|
|
156
|
+
}
|
|
157
|
+
const root = (0, git_1.repoRoot)(cwd) ?? cwd;
|
|
158
|
+
const base = options.base === false ? undefined : (0, base_1.resolveBase)(options.base, cwd);
|
|
159
|
+
let mergeBase;
|
|
160
|
+
let source = 'worktree';
|
|
161
|
+
if (base) {
|
|
162
|
+
mergeBase = (0, git_1.tryGit)(['merge-base', 'HEAD', base], cwd)?.trim() || undefined;
|
|
163
|
+
source = mergeBase ? 'merge-base' : 'base';
|
|
164
|
+
}
|
|
165
|
+
const map = new Map();
|
|
166
|
+
// Diff from the merge base, not the branch tip: changes the base picked up
|
|
167
|
+
// since this branch forked are not this branch's changes.
|
|
168
|
+
if (base)
|
|
169
|
+
collectCommitted(map, cwd, mergeBase ?? base);
|
|
170
|
+
if (options.worktree !== false) {
|
|
171
|
+
collectWorktree(map, cwd, options.untracked !== false);
|
|
172
|
+
}
|
|
173
|
+
const exts = (0, match_1.normalizeExts)(options.ext);
|
|
174
|
+
const included = (0, match_1.makeMatcher)(options.include ?? [], cwd);
|
|
175
|
+
const hasInclude = (options.include ?? []).filter((p) => p && p.trim()).length > 0;
|
|
176
|
+
const excluded = (0, match_1.makeMatcher)(options.exclude ?? [], cwd);
|
|
177
|
+
const within = options.within ?? [];
|
|
178
|
+
const existingOnly = options.existingOnly !== false;
|
|
179
|
+
const files = [];
|
|
180
|
+
for (const [gitPath, entry] of map) {
|
|
181
|
+
// git reports paths from the repository root; everything else here is
|
|
182
|
+
// relative to `cwd`, which may be a subdirectory.
|
|
183
|
+
const abs = (0, path_1.resolve)(root, gitPath);
|
|
184
|
+
const rel = (0, path_1.relative)(cwd, abs).split(path_1.sep).join('/');
|
|
185
|
+
const exists = (0, fs_1.existsSync)(abs);
|
|
186
|
+
if (existingOnly && !exists)
|
|
187
|
+
continue;
|
|
188
|
+
if (exts.length > 0 && !exts.some((e) => abs.toLowerCase().endsWith(e)))
|
|
189
|
+
continue;
|
|
190
|
+
if (!(0, match_1.withinAny)(abs, within, cwd))
|
|
191
|
+
continue;
|
|
192
|
+
if (hasInclude && !included(abs))
|
|
193
|
+
continue;
|
|
194
|
+
if (excluded(abs))
|
|
195
|
+
continue;
|
|
196
|
+
files.push({
|
|
197
|
+
path: abs,
|
|
198
|
+
relative: rel,
|
|
199
|
+
status: entry.status,
|
|
200
|
+
from: entry.from,
|
|
201
|
+
committed: entry.committed,
|
|
202
|
+
worktree: entry.worktree,
|
|
203
|
+
exists
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
files.sort((a, b) => (a.path < b.path ? -1 : a.path > b.path ? 1 : 0));
|
|
207
|
+
return {
|
|
208
|
+
files,
|
|
209
|
+
paths: files.map((f) => f.path),
|
|
210
|
+
base,
|
|
211
|
+
mergeBase,
|
|
212
|
+
source,
|
|
213
|
+
repoRoot: root
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
/** Just the absolute paths — the common case. */
|
|
217
|
+
function changedPaths(options = {}) {
|
|
218
|
+
return changedFiles(options).paths;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Bound to one repository, so a tool that asks several questions resolves the
|
|
222
|
+
* repository root and the base once.
|
|
223
|
+
*
|
|
224
|
+
* ```ts
|
|
225
|
+
* const changed = new GitChanged({ cwd, exclude: ['dist/'] });
|
|
226
|
+
* changed.paths({ ext: '.sql' });
|
|
227
|
+
* changed.paths({ ext: ['.ts', '.tsx'] });
|
|
228
|
+
* ```
|
|
229
|
+
*
|
|
230
|
+
* Options given to the constructor are defaults; options passed to a method
|
|
231
|
+
* override them key by key. Array options replace rather than merge — an
|
|
232
|
+
* `exclude` on a call means "these instead", which is easier to reason about
|
|
233
|
+
* than a growing union.
|
|
234
|
+
*/
|
|
235
|
+
class GitChanged {
|
|
236
|
+
defaults;
|
|
237
|
+
cwd;
|
|
238
|
+
constructor(options = {}) {
|
|
239
|
+
this.cwd = (0, path_1.resolve)(options.cwd ?? process.cwd());
|
|
240
|
+
this.defaults = { ...options, cwd: this.cwd };
|
|
241
|
+
}
|
|
242
|
+
/** Whether `cwd` is inside a git work tree. */
|
|
243
|
+
isRepo() {
|
|
244
|
+
return (0, git_1.isRepo)(this.cwd);
|
|
245
|
+
}
|
|
246
|
+
/** Absolute path to the repository root. */
|
|
247
|
+
root() {
|
|
248
|
+
return (0, git_1.repoRoot)(this.cwd);
|
|
249
|
+
}
|
|
250
|
+
/** The base ref this instance would diff against. */
|
|
251
|
+
base() {
|
|
252
|
+
const base = this.defaults.base;
|
|
253
|
+
return base === false ? undefined : (0, base_1.resolveBase)(base, this.cwd);
|
|
254
|
+
}
|
|
255
|
+
files(options = {}) {
|
|
256
|
+
return this.result(options).files;
|
|
257
|
+
}
|
|
258
|
+
paths(options = {}) {
|
|
259
|
+
return this.result(options).paths;
|
|
260
|
+
}
|
|
261
|
+
result(options = {}) {
|
|
262
|
+
return changedFiles({ ...this.defaults, ...options, cwd: this.cwd });
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
exports.GitChanged = GitChanged;
|
|
266
|
+
/** Resolve a path against `cwd`, for callers holding relative paths. */
|
|
267
|
+
function toAbsolute(file, cwd = process.cwd()) {
|
|
268
|
+
return (0, path_1.isAbsolute)(file) ? file : (0, path_1.resolve)(cwd, file);
|
|
269
|
+
}
|
package/cli.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import type { ChangedOptions } from './types';
|
|
3
|
+
interface Parsed {
|
|
4
|
+
options: ChangedOptions;
|
|
5
|
+
status: boolean;
|
|
6
|
+
absolute: boolean;
|
|
7
|
+
json: boolean;
|
|
8
|
+
nul: boolean;
|
|
9
|
+
help: boolean;
|
|
10
|
+
version: boolean;
|
|
11
|
+
}
|
|
12
|
+
export declare function parseArgs(argv: string[]): Parsed;
|
|
13
|
+
export declare function run(argv?: string[]): number;
|
|
14
|
+
export {};
|
package/cli.js
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.parseArgs = parseArgs;
|
|
5
|
+
exports.run = run;
|
|
6
|
+
const changed_1 = require("./changed");
|
|
7
|
+
const git_1 = require("./git");
|
|
8
|
+
const USAGE = `git-changed — list files changed against the merge base
|
|
9
|
+
|
|
10
|
+
Usage:
|
|
11
|
+
git-changed [options]
|
|
12
|
+
|
|
13
|
+
Options:
|
|
14
|
+
--base <ref> Diff against <ref> (default: $GITHUB_BASE_REF, else the
|
|
15
|
+
repository default branch)
|
|
16
|
+
--no-base Working-tree changes only
|
|
17
|
+
--ext <exts> Keep only these extensions (repeatable, comma-separated)
|
|
18
|
+
--include <globs> Keep only paths matching these globs (repeatable)
|
|
19
|
+
--exclude <globs> Drop paths matching these globs (repeatable)
|
|
20
|
+
--within <dirs> Restrict to these directories (repeatable)
|
|
21
|
+
--no-worktree Committed changes only
|
|
22
|
+
--no-untracked Skip untracked files
|
|
23
|
+
--deleted Include paths that no longer exist
|
|
24
|
+
--status Prefix each path with its status
|
|
25
|
+
--absolute Print absolute paths (default: relative to cwd)
|
|
26
|
+
--json Print the full result as JSON
|
|
27
|
+
-0, --null NUL-separate output, for \`xargs -0\`
|
|
28
|
+
--cwd <dir> Run as if in <dir>
|
|
29
|
+
-h, --help Show this help
|
|
30
|
+
-v, --version Show the version
|
|
31
|
+
|
|
32
|
+
Exit code is 0 whether or not anything changed; an empty list is an answer, not
|
|
33
|
+
an error. Use --json (or test for empty output) to branch on it.
|
|
34
|
+
|
|
35
|
+
Examples:
|
|
36
|
+
git-changed --ext .sql --exclude 'dist/' '**/generated/**'
|
|
37
|
+
git-changed --ext .ts --null | xargs -0 -r prettier --check
|
|
38
|
+
git-changed --base origin/develop --status`;
|
|
39
|
+
function packageVersion() {
|
|
40
|
+
// The CLI sits in dist/ in the repo but at the package root once published,
|
|
41
|
+
// so the manifest is one directory up in one layout and alongside in the other.
|
|
42
|
+
for (const candidate of ['../package.json', './package.json']) {
|
|
43
|
+
try {
|
|
44
|
+
return require(candidate).version;
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
// Wrong layout; try the next candidate.
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return 'unknown';
|
|
51
|
+
}
|
|
52
|
+
/** Split repeatable, comma-separated list values: `--ext .ts,.tsx --ext .mts`. */
|
|
53
|
+
function pushList(target, value) {
|
|
54
|
+
for (const part of value.split(',')) {
|
|
55
|
+
const trimmed = part.trim();
|
|
56
|
+
if (trimmed)
|
|
57
|
+
target.push(trimmed);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
function parseArgs(argv) {
|
|
61
|
+
const ext = [];
|
|
62
|
+
const include = [];
|
|
63
|
+
const exclude = [];
|
|
64
|
+
const within = [];
|
|
65
|
+
const options = {};
|
|
66
|
+
const parsed = {
|
|
67
|
+
options,
|
|
68
|
+
status: false,
|
|
69
|
+
absolute: false,
|
|
70
|
+
json: false,
|
|
71
|
+
nul: false,
|
|
72
|
+
help: false,
|
|
73
|
+
version: false
|
|
74
|
+
};
|
|
75
|
+
for (let i = 0; i < argv.length; i++) {
|
|
76
|
+
const arg = argv[i];
|
|
77
|
+
// Accept both `--ext .sql` and `--ext=.sql`.
|
|
78
|
+
const eq = arg.indexOf('=');
|
|
79
|
+
const flag = arg.startsWith('--') && eq !== -1 ? arg.slice(0, eq) : arg;
|
|
80
|
+
const inline = arg.startsWith('--') && eq !== -1 ? arg.slice(eq + 1) : undefined;
|
|
81
|
+
const next = () => {
|
|
82
|
+
const value = inline ?? argv[++i];
|
|
83
|
+
if (value === undefined) {
|
|
84
|
+
throw new git_1.GitChangedError(`${flag} requires a value`);
|
|
85
|
+
}
|
|
86
|
+
return value;
|
|
87
|
+
};
|
|
88
|
+
switch (flag) {
|
|
89
|
+
case '-h':
|
|
90
|
+
case '--help':
|
|
91
|
+
parsed.help = true;
|
|
92
|
+
break;
|
|
93
|
+
case '-v':
|
|
94
|
+
case '--version':
|
|
95
|
+
parsed.version = true;
|
|
96
|
+
break;
|
|
97
|
+
case '--base':
|
|
98
|
+
options.base = next();
|
|
99
|
+
break;
|
|
100
|
+
case '--no-base':
|
|
101
|
+
options.base = false;
|
|
102
|
+
break;
|
|
103
|
+
case '--ext':
|
|
104
|
+
pushList(ext, next());
|
|
105
|
+
break;
|
|
106
|
+
case '--include':
|
|
107
|
+
pushList(include, next());
|
|
108
|
+
break;
|
|
109
|
+
case '--exclude':
|
|
110
|
+
pushList(exclude, next());
|
|
111
|
+
break;
|
|
112
|
+
case '--within':
|
|
113
|
+
pushList(within, next());
|
|
114
|
+
break;
|
|
115
|
+
case '--cwd':
|
|
116
|
+
options.cwd = next();
|
|
117
|
+
break;
|
|
118
|
+
case '--no-worktree':
|
|
119
|
+
options.worktree = false;
|
|
120
|
+
break;
|
|
121
|
+
case '--no-untracked':
|
|
122
|
+
options.untracked = false;
|
|
123
|
+
break;
|
|
124
|
+
case '--deleted':
|
|
125
|
+
options.existingOnly = false;
|
|
126
|
+
break;
|
|
127
|
+
case '--status':
|
|
128
|
+
parsed.status = true;
|
|
129
|
+
break;
|
|
130
|
+
case '--absolute':
|
|
131
|
+
parsed.absolute = true;
|
|
132
|
+
break;
|
|
133
|
+
case '--json':
|
|
134
|
+
parsed.json = true;
|
|
135
|
+
break;
|
|
136
|
+
case '-0':
|
|
137
|
+
case '--null':
|
|
138
|
+
parsed.nul = true;
|
|
139
|
+
break;
|
|
140
|
+
default:
|
|
141
|
+
throw new git_1.GitChangedError(`Unknown option: ${arg}`);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
if (ext.length)
|
|
145
|
+
options.ext = ext;
|
|
146
|
+
if (include.length)
|
|
147
|
+
options.include = include;
|
|
148
|
+
if (exclude.length)
|
|
149
|
+
options.exclude = exclude;
|
|
150
|
+
if (within.length)
|
|
151
|
+
options.within = within;
|
|
152
|
+
return parsed;
|
|
153
|
+
}
|
|
154
|
+
function run(argv = process.argv.slice(2)) {
|
|
155
|
+
let parsed;
|
|
156
|
+
try {
|
|
157
|
+
parsed = parseArgs(argv);
|
|
158
|
+
}
|
|
159
|
+
catch (err) {
|
|
160
|
+
console.error(err instanceof Error ? err.message : String(err));
|
|
161
|
+
console.error(`\n${USAGE}`);
|
|
162
|
+
return 2;
|
|
163
|
+
}
|
|
164
|
+
if (parsed.help) {
|
|
165
|
+
console.log(USAGE);
|
|
166
|
+
return 0;
|
|
167
|
+
}
|
|
168
|
+
if (parsed.version) {
|
|
169
|
+
console.log(packageVersion());
|
|
170
|
+
return 0;
|
|
171
|
+
}
|
|
172
|
+
let result;
|
|
173
|
+
try {
|
|
174
|
+
result = (0, changed_1.changedFiles)(parsed.options);
|
|
175
|
+
}
|
|
176
|
+
catch (err) {
|
|
177
|
+
console.error(err instanceof Error ? err.message : String(err));
|
|
178
|
+
return 1;
|
|
179
|
+
}
|
|
180
|
+
if (parsed.json) {
|
|
181
|
+
console.log(JSON.stringify(result, null, 2));
|
|
182
|
+
return 0;
|
|
183
|
+
}
|
|
184
|
+
const lines = result.files.map((f) => {
|
|
185
|
+
const path = parsed.absolute ? f.path : f.relative;
|
|
186
|
+
return parsed.status ? `${f.status}\t${path}` : path;
|
|
187
|
+
});
|
|
188
|
+
if (parsed.nul) {
|
|
189
|
+
// No trailing newline: xargs -0 splits on NUL, and a stray newline would
|
|
190
|
+
// become part of the last filename.
|
|
191
|
+
process.stdout.write(lines.map((l) => `${l}\0`).join(''));
|
|
192
|
+
}
|
|
193
|
+
else if (lines.length) {
|
|
194
|
+
process.stdout.write(`${lines.join('\n')}\n`);
|
|
195
|
+
}
|
|
196
|
+
return 0;
|
|
197
|
+
}
|
|
198
|
+
if (require.main === module) {
|
|
199
|
+
process.exit(run());
|
|
200
|
+
}
|
package/esm/base.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** The repository's default branch (`origin/main`, `origin/master`, …). */
|
|
2
|
+
export declare function defaultBranch(cwd: string): string | undefined;
|
|
3
|
+
/**
|
|
4
|
+
* Resolve the ref to diff against, in precedence order:
|
|
5
|
+
*
|
|
6
|
+
* 1. an explicit base (`--base`, or `base` in code) — always wins;
|
|
7
|
+
* 2. `$GITHUB_BASE_REF` — the PR's target branch, set by GitHub Actions on
|
|
8
|
+
* `pull_request` events, as `origin/<branch>` since only the remote ref is
|
|
9
|
+
* fetched;
|
|
10
|
+
* 3. the repository's default branch.
|
|
11
|
+
*
|
|
12
|
+
* `undefined` means "no base is available" — a detached checkout, a fresh repo
|
|
13
|
+
* with no remote, or a shallow clone whose base ref was never fetched. Callers
|
|
14
|
+
* fall back to the working tree rather than failing: a gate that cannot see the
|
|
15
|
+
* base should still catch what the author is editing right now.
|
|
16
|
+
*/
|
|
17
|
+
export declare function resolveBase(base?: string, cwd?: string): string | undefined;
|
package/esm/base.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { tryGit } from './git';
|
|
2
|
+
/**
|
|
3
|
+
* Candidate default branches, in the order git itself would find them. Tried
|
|
4
|
+
* only when the remote HEAD symref is missing — which is the norm in CI, where
|
|
5
|
+
* `actions/checkout` never sets it.
|
|
6
|
+
*/
|
|
7
|
+
const FALLBACK_BASES = ['origin/main', 'origin/master', 'main', 'master'];
|
|
8
|
+
/** The repository's default branch (`origin/main`, `origin/master`, …). */
|
|
9
|
+
export function defaultBranch(cwd) {
|
|
10
|
+
const head = tryGit(['symbolic-ref', 'refs/remotes/origin/HEAD'], cwd)?.trim();
|
|
11
|
+
if (head) {
|
|
12
|
+
// refs/remotes/origin/HEAD → refs/remotes/origin/main → origin/main
|
|
13
|
+
const short = head.replace(/^refs\/remotes\//, '');
|
|
14
|
+
if (short)
|
|
15
|
+
return short;
|
|
16
|
+
}
|
|
17
|
+
return FALLBACK_BASES.find((ref) => tryGit(['rev-parse', '--verify', '--quiet', ref], cwd) !== undefined);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Resolve the ref to diff against, in precedence order:
|
|
21
|
+
*
|
|
22
|
+
* 1. an explicit base (`--base`, or `base` in code) — always wins;
|
|
23
|
+
* 2. `$GITHUB_BASE_REF` — the PR's target branch, set by GitHub Actions on
|
|
24
|
+
* `pull_request` events, as `origin/<branch>` since only the remote ref is
|
|
25
|
+
* fetched;
|
|
26
|
+
* 3. the repository's default branch.
|
|
27
|
+
*
|
|
28
|
+
* `undefined` means "no base is available" — a detached checkout, a fresh repo
|
|
29
|
+
* with no remote, or a shallow clone whose base ref was never fetched. Callers
|
|
30
|
+
* fall back to the working tree rather than failing: a gate that cannot see the
|
|
31
|
+
* base should still catch what the author is editing right now.
|
|
32
|
+
*/
|
|
33
|
+
export function resolveBase(base, cwd = process.cwd()) {
|
|
34
|
+
if (base && base.trim())
|
|
35
|
+
return base.trim();
|
|
36
|
+
const prBase = process.env.GITHUB_BASE_REF;
|
|
37
|
+
if (prBase && prBase.trim()) {
|
|
38
|
+
const ref = `origin/${prBase.trim()}`;
|
|
39
|
+
if (tryGit(['rev-parse', '--verify', '--quiet', ref], cwd) !== undefined)
|
|
40
|
+
return ref;
|
|
41
|
+
// The remote ref is missing (a single-branch or shallow fetch). Fall through
|
|
42
|
+
// rather than handing back a ref that every later git call will reject.
|
|
43
|
+
}
|
|
44
|
+
return defaultBranch(cwd);
|
|
45
|
+
}
|
package/esm/changed.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { ChangedFile, ChangedOptions, ChangedResult } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Files changed relative to a base ref, unioned with working-tree changes.
|
|
4
|
+
*
|
|
5
|
+
* ```ts
|
|
6
|
+
* changedFiles({ ext: '.sql', exclude: ['**\/generated/**'] });
|
|
7
|
+
* // → { files: [...], paths: [...], base: 'origin/main', source: 'merge-base' }
|
|
8
|
+
* ```
|
|
9
|
+
*
|
|
10
|
+
* Resolution order for the base is `options.base` → `$GITHUB_BASE_REF` → the
|
|
11
|
+
* repository default branch; pass `base: false` to skip it entirely. When no
|
|
12
|
+
* base is available the result falls back to the working tree with
|
|
13
|
+
* `source: 'worktree'` rather than throwing, so a gate still checks what the
|
|
14
|
+
* author is editing on a shallow or detached checkout.
|
|
15
|
+
*/
|
|
16
|
+
export declare function changedFiles(options?: ChangedOptions): ChangedResult;
|
|
17
|
+
/** Just the absolute paths — the common case. */
|
|
18
|
+
export declare function changedPaths(options?: ChangedOptions): string[];
|
|
19
|
+
/**
|
|
20
|
+
* Bound to one repository, so a tool that asks several questions resolves the
|
|
21
|
+
* repository root and the base once.
|
|
22
|
+
*
|
|
23
|
+
* ```ts
|
|
24
|
+
* const changed = new GitChanged({ cwd, exclude: ['dist/'] });
|
|
25
|
+
* changed.paths({ ext: '.sql' });
|
|
26
|
+
* changed.paths({ ext: ['.ts', '.tsx'] });
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* Options given to the constructor are defaults; options passed to a method
|
|
30
|
+
* override them key by key. Array options replace rather than merge — an
|
|
31
|
+
* `exclude` on a call means "these instead", which is easier to reason about
|
|
32
|
+
* than a growing union.
|
|
33
|
+
*/
|
|
34
|
+
export declare class GitChanged {
|
|
35
|
+
private readonly defaults;
|
|
36
|
+
readonly cwd: string;
|
|
37
|
+
constructor(options?: ChangedOptions);
|
|
38
|
+
/** Whether `cwd` is inside a git work tree. */
|
|
39
|
+
isRepo(): boolean;
|
|
40
|
+
/** Absolute path to the repository root. */
|
|
41
|
+
root(): string | undefined;
|
|
42
|
+
/** The base ref this instance would diff against. */
|
|
43
|
+
base(): string | undefined;
|
|
44
|
+
files(options?: ChangedOptions): ChangedFile[];
|
|
45
|
+
paths(options?: ChangedOptions): string[];
|
|
46
|
+
result(options?: ChangedOptions): ChangedResult;
|
|
47
|
+
}
|
|
48
|
+
/** Resolve a path against `cwd`, for callers holding relative paths. */
|
|
49
|
+
export declare function toAbsolute(file: string, cwd?: string): string;
|