@hyperfrontend/versioning 0.1.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/ARCHITECTURE.md +593 -0
- package/CHANGELOG.md +35 -0
- package/FUNDING.md +141 -0
- package/LICENSE.md +21 -0
- package/README.md +195 -0
- package/SECURITY.md +82 -0
- package/changelog/compare/diff.d.ts +128 -0
- package/changelog/compare/diff.d.ts.map +1 -0
- package/changelog/compare/index.cjs.js +628 -0
- package/changelog/compare/index.cjs.js.map +1 -0
- package/changelog/compare/index.d.ts +4 -0
- package/changelog/compare/index.d.ts.map +1 -0
- package/changelog/compare/index.esm.js +612 -0
- package/changelog/compare/index.esm.js.map +1 -0
- package/changelog/compare/is-equal.d.ts +114 -0
- package/changelog/compare/is-equal.d.ts.map +1 -0
- package/changelog/index.cjs.js +6448 -0
- package/changelog/index.cjs.js.map +1 -0
- package/changelog/index.d.ts +6 -0
- package/changelog/index.d.ts.map +1 -0
- package/changelog/index.esm.js +6358 -0
- package/changelog/index.esm.js.map +1 -0
- package/changelog/models/changelog.d.ts +86 -0
- package/changelog/models/changelog.d.ts.map +1 -0
- package/changelog/models/commit-ref.d.ts +51 -0
- package/changelog/models/commit-ref.d.ts.map +1 -0
- package/changelog/models/entry.d.ts +84 -0
- package/changelog/models/entry.d.ts.map +1 -0
- package/changelog/models/index.cjs.js +2043 -0
- package/changelog/models/index.cjs.js.map +1 -0
- package/changelog/models/index.d.ts +11 -0
- package/changelog/models/index.d.ts.map +1 -0
- package/changelog/models/index.esm.js +2026 -0
- package/changelog/models/index.esm.js.map +1 -0
- package/changelog/models/schema.d.ts +68 -0
- package/changelog/models/schema.d.ts.map +1 -0
- package/changelog/models/section.d.ts +25 -0
- package/changelog/models/section.d.ts.map +1 -0
- package/changelog/operations/add-entry.d.ts +56 -0
- package/changelog/operations/add-entry.d.ts.map +1 -0
- package/changelog/operations/add-item.d.ts +18 -0
- package/changelog/operations/add-item.d.ts.map +1 -0
- package/changelog/operations/filter-by-predicate.d.ts +81 -0
- package/changelog/operations/filter-by-predicate.d.ts.map +1 -0
- package/changelog/operations/filter-by-range.d.ts +63 -0
- package/changelog/operations/filter-by-range.d.ts.map +1 -0
- package/changelog/operations/filter-entries.d.ts +9 -0
- package/changelog/operations/filter-entries.d.ts.map +1 -0
- package/changelog/operations/index.cjs.js +2455 -0
- package/changelog/operations/index.cjs.js.map +1 -0
- package/changelog/operations/index.d.ts +15 -0
- package/changelog/operations/index.d.ts.map +1 -0
- package/changelog/operations/index.esm.js +2411 -0
- package/changelog/operations/index.esm.js.map +1 -0
- package/changelog/operations/merge.d.ts +88 -0
- package/changelog/operations/merge.d.ts.map +1 -0
- package/changelog/operations/remove-entry.d.ts +45 -0
- package/changelog/operations/remove-entry.d.ts.map +1 -0
- package/changelog/operations/remove-section.d.ts +50 -0
- package/changelog/operations/remove-section.d.ts.map +1 -0
- package/changelog/operations/transform.d.ts +143 -0
- package/changelog/operations/transform.d.ts.map +1 -0
- package/changelog/parse/index.cjs.js +1282 -0
- package/changelog/parse/index.cjs.js.map +1 -0
- package/changelog/parse/index.d.ts +5 -0
- package/changelog/parse/index.d.ts.map +1 -0
- package/changelog/parse/index.esm.js +1275 -0
- package/changelog/parse/index.esm.js.map +1 -0
- package/changelog/parse/line.d.ts +48 -0
- package/changelog/parse/line.d.ts.map +1 -0
- package/changelog/parse/parser.d.ts +16 -0
- package/changelog/parse/parser.d.ts.map +1 -0
- package/changelog/parse/tokenizer.d.ts +49 -0
- package/changelog/parse/tokenizer.d.ts.map +1 -0
- package/changelog/serialize/index.cjs.js +574 -0
- package/changelog/serialize/index.cjs.js.map +1 -0
- package/changelog/serialize/index.d.ts +6 -0
- package/changelog/serialize/index.d.ts.map +1 -0
- package/changelog/serialize/index.esm.js +564 -0
- package/changelog/serialize/index.esm.js.map +1 -0
- package/changelog/serialize/templates.d.ts +81 -0
- package/changelog/serialize/templates.d.ts.map +1 -0
- package/changelog/serialize/to-json.d.ts +57 -0
- package/changelog/serialize/to-json.d.ts.map +1 -0
- package/changelog/serialize/to-string.d.ts +30 -0
- package/changelog/serialize/to-string.d.ts.map +1 -0
- package/commits/index.cjs.js +648 -0
- package/commits/index.cjs.js.map +1 -0
- package/commits/index.d.ts +3 -0
- package/commits/index.d.ts.map +1 -0
- package/commits/index.esm.js +629 -0
- package/commits/index.esm.js.map +1 -0
- package/commits/models/breaking.d.ts +39 -0
- package/commits/models/breaking.d.ts.map +1 -0
- package/commits/models/commit-type.d.ts +32 -0
- package/commits/models/commit-type.d.ts.map +1 -0
- package/commits/models/conventional.d.ts +49 -0
- package/commits/models/conventional.d.ts.map +1 -0
- package/commits/models/index.cjs.js +207 -0
- package/commits/models/index.cjs.js.map +1 -0
- package/commits/models/index.d.ts +7 -0
- package/commits/models/index.d.ts.map +1 -0
- package/commits/models/index.esm.js +193 -0
- package/commits/models/index.esm.js.map +1 -0
- package/commits/parse/body.d.ts +18 -0
- package/commits/parse/body.d.ts.map +1 -0
- package/commits/parse/footer.d.ts +16 -0
- package/commits/parse/footer.d.ts.map +1 -0
- package/commits/parse/header.d.ts +15 -0
- package/commits/parse/header.d.ts.map +1 -0
- package/commits/parse/index.cjs.js +505 -0
- package/commits/parse/index.cjs.js.map +1 -0
- package/commits/parse/index.d.ts +5 -0
- package/commits/parse/index.d.ts.map +1 -0
- package/commits/parse/index.esm.js +499 -0
- package/commits/parse/index.esm.js.map +1 -0
- package/commits/parse/message.d.ts +17 -0
- package/commits/parse/message.d.ts.map +1 -0
- package/commits/utils/replace-char.d.ts +19 -0
- package/commits/utils/replace-char.d.ts.map +1 -0
- package/flow/executor/execute.d.ts +72 -0
- package/flow/executor/execute.d.ts.map +1 -0
- package/flow/executor/index.cjs.js +4402 -0
- package/flow/executor/index.cjs.js.map +1 -0
- package/flow/executor/index.d.ts +3 -0
- package/flow/executor/index.d.ts.map +1 -0
- package/flow/executor/index.esm.js +4398 -0
- package/flow/executor/index.esm.js.map +1 -0
- package/flow/factory.d.ts +58 -0
- package/flow/factory.d.ts.map +1 -0
- package/flow/index.cjs.js +8506 -0
- package/flow/index.cjs.js.map +1 -0
- package/flow/index.d.ts +7 -0
- package/flow/index.d.ts.map +1 -0
- package/flow/index.esm.js +8451 -0
- package/flow/index.esm.js.map +1 -0
- package/flow/models/flow.d.ts +130 -0
- package/flow/models/flow.d.ts.map +1 -0
- package/flow/models/index.cjs.js +285 -0
- package/flow/models/index.cjs.js.map +1 -0
- package/flow/models/index.d.ts +7 -0
- package/flow/models/index.d.ts.map +1 -0
- package/flow/models/index.esm.js +268 -0
- package/flow/models/index.esm.js.map +1 -0
- package/flow/models/step.d.ts +108 -0
- package/flow/models/step.d.ts.map +1 -0
- package/flow/models/types.d.ts +150 -0
- package/flow/models/types.d.ts.map +1 -0
- package/flow/presets/conventional.d.ts +59 -0
- package/flow/presets/conventional.d.ts.map +1 -0
- package/flow/presets/independent.d.ts +61 -0
- package/flow/presets/independent.d.ts.map +1 -0
- package/flow/presets/index.cjs.js +3903 -0
- package/flow/presets/index.cjs.js.map +1 -0
- package/flow/presets/index.d.ts +4 -0
- package/flow/presets/index.d.ts.map +1 -0
- package/flow/presets/index.esm.js +3889 -0
- package/flow/presets/index.esm.js.map +1 -0
- package/flow/presets/synced.d.ts +65 -0
- package/flow/presets/synced.d.ts.map +1 -0
- package/flow/steps/analyze-commits.d.ts +19 -0
- package/flow/steps/analyze-commits.d.ts.map +1 -0
- package/flow/steps/calculate-bump.d.ts +27 -0
- package/flow/steps/calculate-bump.d.ts.map +1 -0
- package/flow/steps/create-commit.d.ts +16 -0
- package/flow/steps/create-commit.d.ts.map +1 -0
- package/flow/steps/create-tag.d.ts +22 -0
- package/flow/steps/create-tag.d.ts.map +1 -0
- package/flow/steps/fetch-registry.d.ts +19 -0
- package/flow/steps/fetch-registry.d.ts.map +1 -0
- package/flow/steps/generate-changelog.d.ts +25 -0
- package/flow/steps/generate-changelog.d.ts.map +1 -0
- package/flow/steps/index.cjs.js +3523 -0
- package/flow/steps/index.cjs.js.map +1 -0
- package/flow/steps/index.d.ts +8 -0
- package/flow/steps/index.d.ts.map +1 -0
- package/flow/steps/index.esm.js +3504 -0
- package/flow/steps/index.esm.js.map +1 -0
- package/flow/steps/update-packages.d.ts +25 -0
- package/flow/steps/update-packages.d.ts.map +1 -0
- package/flow/utils/interpolate.d.ts +11 -0
- package/flow/utils/interpolate.d.ts.map +1 -0
- package/git/factory.d.ts +233 -0
- package/git/factory.d.ts.map +1 -0
- package/git/index.cjs.js +2863 -0
- package/git/index.cjs.js.map +1 -0
- package/git/index.d.ts +5 -0
- package/git/index.d.ts.map +1 -0
- package/git/index.esm.js +2785 -0
- package/git/index.esm.js.map +1 -0
- package/git/models/commit.d.ts +129 -0
- package/git/models/commit.d.ts.map +1 -0
- package/git/models/index.cjs.js +755 -0
- package/git/models/index.cjs.js.map +1 -0
- package/git/models/index.d.ts +7 -0
- package/git/models/index.d.ts.map +1 -0
- package/git/models/index.esm.js +729 -0
- package/git/models/index.esm.js.map +1 -0
- package/git/models/ref.d.ts +120 -0
- package/git/models/ref.d.ts.map +1 -0
- package/git/models/tag.d.ts +141 -0
- package/git/models/tag.d.ts.map +1 -0
- package/git/operations/commit.d.ts +97 -0
- package/git/operations/commit.d.ts.map +1 -0
- package/git/operations/head-info.d.ts +29 -0
- package/git/operations/head-info.d.ts.map +1 -0
- package/git/operations/index.cjs.js +1954 -0
- package/git/operations/index.cjs.js.map +1 -0
- package/git/operations/index.d.ts +14 -0
- package/git/operations/index.d.ts.map +1 -0
- package/git/operations/index.esm.js +1903 -0
- package/git/operations/index.esm.js.map +1 -0
- package/git/operations/log.d.ts +104 -0
- package/git/operations/log.d.ts.map +1 -0
- package/git/operations/manage-tags.d.ts +60 -0
- package/git/operations/manage-tags.d.ts.map +1 -0
- package/git/operations/query-tags.d.ts +88 -0
- package/git/operations/query-tags.d.ts.map +1 -0
- package/git/operations/stage.d.ts +66 -0
- package/git/operations/stage.d.ts.map +1 -0
- package/git/operations/status.d.ts +173 -0
- package/git/operations/status.d.ts.map +1 -0
- package/index.cjs.js +16761 -0
- package/index.cjs.js.map +1 -0
- package/index.d.ts +102 -0
- package/index.d.ts.map +1 -0
- package/index.esm.js +16427 -0
- package/index.esm.js.map +1 -0
- package/package.json +200 -0
- package/registry/factory.d.ts +18 -0
- package/registry/factory.d.ts.map +1 -0
- package/registry/index.cjs.js +543 -0
- package/registry/index.cjs.js.map +1 -0
- package/registry/index.d.ts +5 -0
- package/registry/index.d.ts.map +1 -0
- package/registry/index.esm.js +535 -0
- package/registry/index.esm.js.map +1 -0
- package/registry/models/index.cjs.js +69 -0
- package/registry/models/index.cjs.js.map +1 -0
- package/registry/models/index.d.ts +6 -0
- package/registry/models/index.d.ts.map +1 -0
- package/registry/models/index.esm.js +66 -0
- package/registry/models/index.esm.js.map +1 -0
- package/registry/models/package-info.d.ts +55 -0
- package/registry/models/package-info.d.ts.map +1 -0
- package/registry/models/registry.d.ts +62 -0
- package/registry/models/registry.d.ts.map +1 -0
- package/registry/models/version-info.d.ts +67 -0
- package/registry/models/version-info.d.ts.map +1 -0
- package/registry/npm/cache.d.ts +50 -0
- package/registry/npm/cache.d.ts.map +1 -0
- package/registry/npm/client.d.ts +30 -0
- package/registry/npm/client.d.ts.map +1 -0
- package/registry/npm/index.cjs.js +456 -0
- package/registry/npm/index.cjs.js.map +1 -0
- package/registry/npm/index.d.ts +4 -0
- package/registry/npm/index.d.ts.map +1 -0
- package/registry/npm/index.esm.js +451 -0
- package/registry/npm/index.esm.js.map +1 -0
- package/semver/compare/compare.d.ts +100 -0
- package/semver/compare/compare.d.ts.map +1 -0
- package/semver/compare/index.cjs.js +386 -0
- package/semver/compare/index.cjs.js.map +1 -0
- package/semver/compare/index.d.ts +3 -0
- package/semver/compare/index.d.ts.map +1 -0
- package/semver/compare/index.esm.js +370 -0
- package/semver/compare/index.esm.js.map +1 -0
- package/semver/compare/sort.d.ts +36 -0
- package/semver/compare/sort.d.ts.map +1 -0
- package/semver/format/index.cjs.js +58 -0
- package/semver/format/index.cjs.js.map +1 -0
- package/semver/format/index.d.ts +2 -0
- package/semver/format/index.d.ts.map +1 -0
- package/semver/format/index.esm.js +53 -0
- package/semver/format/index.esm.js.map +1 -0
- package/semver/format/to-string.d.ts +31 -0
- package/semver/format/to-string.d.ts.map +1 -0
- package/semver/increment/bump.d.ts +37 -0
- package/semver/increment/bump.d.ts.map +1 -0
- package/semver/increment/index.cjs.js +223 -0
- package/semver/increment/index.cjs.js.map +1 -0
- package/semver/increment/index.d.ts +2 -0
- package/semver/increment/index.d.ts.map +1 -0
- package/semver/increment/index.esm.js +219 -0
- package/semver/increment/index.esm.js.map +1 -0
- package/semver/index.cjs.js +1499 -0
- package/semver/index.cjs.js.map +1 -0
- package/semver/index.d.ts +6 -0
- package/semver/index.d.ts.map +1 -0
- package/semver/index.esm.js +1458 -0
- package/semver/index.esm.js.map +1 -0
- package/semver/models/index.cjs.js +153 -0
- package/semver/models/index.cjs.js.map +1 -0
- package/semver/models/index.d.ts +5 -0
- package/semver/models/index.d.ts.map +1 -0
- package/semver/models/index.esm.js +139 -0
- package/semver/models/index.esm.js.map +1 -0
- package/semver/models/range.d.ts +83 -0
- package/semver/models/range.d.ts.map +1 -0
- package/semver/models/version.d.ts +78 -0
- package/semver/models/version.d.ts.map +1 -0
- package/semver/parse/index.cjs.js +799 -0
- package/semver/parse/index.cjs.js.map +1 -0
- package/semver/parse/index.d.ts +5 -0
- package/semver/parse/index.d.ts.map +1 -0
- package/semver/parse/index.esm.js +793 -0
- package/semver/parse/index.esm.js.map +1 -0
- package/semver/parse/range.d.ts +38 -0
- package/semver/parse/range.d.ts.map +1 -0
- package/semver/parse/version.d.ts +49 -0
- package/semver/parse/version.d.ts.map +1 -0
- package/workspace/discovery/changelog-path.d.ts +21 -0
- package/workspace/discovery/changelog-path.d.ts.map +1 -0
- package/workspace/discovery/dependencies.d.ts +145 -0
- package/workspace/discovery/dependencies.d.ts.map +1 -0
- package/workspace/discovery/discover-changelogs.d.ts +76 -0
- package/workspace/discovery/discover-changelogs.d.ts.map +1 -0
- package/workspace/discovery/index.cjs.js +2300 -0
- package/workspace/discovery/index.cjs.js.map +1 -0
- package/workspace/discovery/index.d.ts +13 -0
- package/workspace/discovery/index.d.ts.map +1 -0
- package/workspace/discovery/index.esm.js +2283 -0
- package/workspace/discovery/index.esm.js.map +1 -0
- package/workspace/discovery/packages.d.ts +83 -0
- package/workspace/discovery/packages.d.ts.map +1 -0
- package/workspace/index.cjs.js +4445 -0
- package/workspace/index.cjs.js.map +1 -0
- package/workspace/index.d.ts +52 -0
- package/workspace/index.d.ts.map +1 -0
- package/workspace/index.esm.js +4394 -0
- package/workspace/index.esm.js.map +1 -0
- package/workspace/models/index.cjs.js +284 -0
- package/workspace/models/index.cjs.js.map +1 -0
- package/workspace/models/index.d.ts +10 -0
- package/workspace/models/index.d.ts.map +1 -0
- package/workspace/models/index.esm.js +261 -0
- package/workspace/models/index.esm.js.map +1 -0
- package/workspace/models/project.d.ts +118 -0
- package/workspace/models/project.d.ts.map +1 -0
- package/workspace/models/workspace.d.ts +139 -0
- package/workspace/models/workspace.d.ts.map +1 -0
- package/workspace/operations/batch-update.d.ts +99 -0
- package/workspace/operations/batch-update.d.ts.map +1 -0
- package/workspace/operations/cascade-bump.d.ts +125 -0
- package/workspace/operations/cascade-bump.d.ts.map +1 -0
- package/workspace/operations/index.cjs.js +2675 -0
- package/workspace/operations/index.cjs.js.map +1 -0
- package/workspace/operations/index.d.ts +12 -0
- package/workspace/operations/index.d.ts.map +1 -0
- package/workspace/operations/index.esm.js +2663 -0
- package/workspace/operations/index.esm.js.map +1 -0
- package/workspace/operations/validate.d.ts +85 -0
- package/workspace/operations/validate.d.ts.map +1 -0
|
@@ -0,0 +1,1903 @@
|
|
|
1
|
+
import { execSync } from 'node:child_process';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Safe copies of Error built-ins via factory functions.
|
|
5
|
+
*
|
|
6
|
+
* Since constructors cannot be safely captured via Object.assign, this module
|
|
7
|
+
* provides factory functions that use Reflect.construct internally.
|
|
8
|
+
*
|
|
9
|
+
* These references are captured at module initialization time to protect against
|
|
10
|
+
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
11
|
+
*
|
|
12
|
+
* @module @hyperfrontend/immutable-api-utils/built-in-copy/error
|
|
13
|
+
*/
|
|
14
|
+
// Capture references at module initialization time
|
|
15
|
+
const _Error = globalThis.Error;
|
|
16
|
+
const _Reflect$1 = globalThis.Reflect;
|
|
17
|
+
/**
|
|
18
|
+
* (Safe copy) Creates a new Error using the captured Error constructor.
|
|
19
|
+
* Use this instead of `new Error()`.
|
|
20
|
+
*
|
|
21
|
+
* @param message - Optional error message.
|
|
22
|
+
* @param options - Optional error options.
|
|
23
|
+
* @returns A new Error instance.
|
|
24
|
+
*/
|
|
25
|
+
const createError = (message, options) => _Reflect$1.construct(_Error, [message, options]);
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Creates a git commit model.
|
|
29
|
+
*
|
|
30
|
+
* @param options - Commit creation options
|
|
31
|
+
* @returns A new GitCommit object
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* const commit = createGitCommit({
|
|
35
|
+
* hash: 'abc123...',
|
|
36
|
+
* authorName: 'John Doe',
|
|
37
|
+
* authorEmail: 'john@example.com',
|
|
38
|
+
* authorDate: '2026-03-12T10:00:00Z',
|
|
39
|
+
* subject: 'feat: add new feature',
|
|
40
|
+
* })
|
|
41
|
+
*/
|
|
42
|
+
function createGitCommit(options) {
|
|
43
|
+
const body = options.body ?? '';
|
|
44
|
+
const subject = options.subject;
|
|
45
|
+
return {
|
|
46
|
+
hash: options.hash,
|
|
47
|
+
shortHash: getShortHash(options.hash),
|
|
48
|
+
authorName: options.authorName,
|
|
49
|
+
authorEmail: options.authorEmail,
|
|
50
|
+
authorDate: options.authorDate,
|
|
51
|
+
committerName: options.committerName ?? options.authorName,
|
|
52
|
+
committerEmail: options.committerEmail ?? options.authorEmail,
|
|
53
|
+
commitDate: options.commitDate ?? options.authorDate,
|
|
54
|
+
subject,
|
|
55
|
+
body,
|
|
56
|
+
message: body ? `${subject}\n\n${body}` : subject,
|
|
57
|
+
parents: options.parents ?? [],
|
|
58
|
+
refs: options.refs ?? [],
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Gets a short hash (7 characters) from a full commit hash.
|
|
63
|
+
*
|
|
64
|
+
* @param hash - Full commit hash
|
|
65
|
+
* @returns Short hash (7 characters)
|
|
66
|
+
*/
|
|
67
|
+
function getShortHash(hash) {
|
|
68
|
+
return hash.slice(0, 7);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Default log options.
|
|
73
|
+
*/
|
|
74
|
+
const DEFAULT_LOG_OPTIONS = {
|
|
75
|
+
maxCount: 100,
|
|
76
|
+
includeMerges: true,
|
|
77
|
+
timeout: 30000,
|
|
78
|
+
};
|
|
79
|
+
/**
|
|
80
|
+
* Git log format string for structured output.
|
|
81
|
+
* Uses ASCII delimiters that won't appear in commit messages.
|
|
82
|
+
*/
|
|
83
|
+
const LOG_FORMAT = [
|
|
84
|
+
'%H', // full hash
|
|
85
|
+
'%an', // author name
|
|
86
|
+
'%ae', // author email
|
|
87
|
+
'%aI', // author date (ISO 8601)
|
|
88
|
+
'%cn', // committer name
|
|
89
|
+
'%ce', // committer email
|
|
90
|
+
'%cI', // commit date (ISO 8601)
|
|
91
|
+
'%s', // subject
|
|
92
|
+
'%b', // body
|
|
93
|
+
'%P', // parent hashes
|
|
94
|
+
'%D', // refs
|
|
95
|
+
].join('%x00'); // NUL separator
|
|
96
|
+
/**
|
|
97
|
+
* Record separator for commit entries.
|
|
98
|
+
*/
|
|
99
|
+
const RECORD_SEPARATOR = '\x1e'; // ASCII Record Separator
|
|
100
|
+
/**
|
|
101
|
+
* Gets the commit log from a git repository.
|
|
102
|
+
*
|
|
103
|
+
* @param options - Configuration for retrieving the commit log
|
|
104
|
+
* @returns Array of GitCommit objects
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* const commits = getCommitLog({ maxCount: 10 })
|
|
108
|
+
* const recentChanges = getCommitLog({ from: 'v1.0.0', to: 'HEAD' })
|
|
109
|
+
*/
|
|
110
|
+
function getCommitLog(options = {}) {
|
|
111
|
+
const opts = { ...DEFAULT_LOG_OPTIONS, ...options };
|
|
112
|
+
const args = ['log', `--format=${RECORD_SEPARATOR}${LOG_FORMAT}`];
|
|
113
|
+
// Add options
|
|
114
|
+
if (opts.maxCount !== undefined && opts.maxCount > 0) {
|
|
115
|
+
args.push(`-n${opts.maxCount}`);
|
|
116
|
+
}
|
|
117
|
+
if (!opts.includeMerges) {
|
|
118
|
+
args.push('--no-merges');
|
|
119
|
+
}
|
|
120
|
+
if (opts.author) {
|
|
121
|
+
const safeAuthor = escapeGitArg(opts.author);
|
|
122
|
+
args.push(`--author=${safeAuthor}`);
|
|
123
|
+
}
|
|
124
|
+
// Add range
|
|
125
|
+
if (opts.from && opts.to) {
|
|
126
|
+
const safeFrom = escapeGitRef(opts.from);
|
|
127
|
+
const safeTo = escapeGitRef(opts.to);
|
|
128
|
+
args.push(`${safeFrom}..${safeTo}`);
|
|
129
|
+
}
|
|
130
|
+
else if (opts.from) {
|
|
131
|
+
const safeFrom = escapeGitRef(opts.from);
|
|
132
|
+
args.push(`${safeFrom}..HEAD`);
|
|
133
|
+
}
|
|
134
|
+
else if (opts.to) {
|
|
135
|
+
const safeTo = escapeGitRef(opts.to);
|
|
136
|
+
args.push(safeTo);
|
|
137
|
+
}
|
|
138
|
+
// Add path filter
|
|
139
|
+
if (opts.path) {
|
|
140
|
+
const safePath = escapeGitPath(opts.path);
|
|
141
|
+
args.push('--', safePath);
|
|
142
|
+
}
|
|
143
|
+
try {
|
|
144
|
+
const output = execSync(`git ${args.join(' ')}`, {
|
|
145
|
+
encoding: 'utf-8',
|
|
146
|
+
cwd: opts.cwd,
|
|
147
|
+
timeout: opts.timeout,
|
|
148
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
149
|
+
maxBuffer: 50 * 1024 * 1024, // 50MB
|
|
150
|
+
});
|
|
151
|
+
return parseCommitLog(output);
|
|
152
|
+
}
|
|
153
|
+
catch (error) {
|
|
154
|
+
// Check if error is due to no commits
|
|
155
|
+
if (error instanceof Error && error.message.includes('does not have any commits')) {
|
|
156
|
+
return [];
|
|
157
|
+
}
|
|
158
|
+
throw error;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Gets commits between two references.
|
|
163
|
+
*
|
|
164
|
+
* @param from - Starting reference (exclusive)
|
|
165
|
+
* @param to - Ending reference (inclusive, default: HEAD)
|
|
166
|
+
* @param options - Additional options
|
|
167
|
+
* @returns Array of GitCommit objects
|
|
168
|
+
*
|
|
169
|
+
* @example
|
|
170
|
+
* const commits = getCommitsBetween('v1.0.0', 'v1.1.0')
|
|
171
|
+
*/
|
|
172
|
+
function getCommitsBetween(from, to = 'HEAD', options = {}) {
|
|
173
|
+
return getCommitLog({ ...options, from, to });
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Gets commits since a specific tag or reference.
|
|
177
|
+
*
|
|
178
|
+
* @param since - Reference to start from (exclusive)
|
|
179
|
+
* @param options - Additional options
|
|
180
|
+
* @returns Array of GitCommit objects
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* const commits = getCommitsSince('v1.0.0')
|
|
184
|
+
*/
|
|
185
|
+
function getCommitsSince(since, options = {}) {
|
|
186
|
+
return getCommitLog({ ...options, from: since });
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Gets a single commit by its hash.
|
|
190
|
+
*
|
|
191
|
+
* @param hash - Commit hash (full or short)
|
|
192
|
+
* @param options - Additional options
|
|
193
|
+
* @returns GitCommit or null if not found
|
|
194
|
+
*
|
|
195
|
+
* @example
|
|
196
|
+
* const commit = getCommit('abc1234')
|
|
197
|
+
*/
|
|
198
|
+
function getCommit(hash, options = {}) {
|
|
199
|
+
const safeHash = escapeGitRef(hash);
|
|
200
|
+
try {
|
|
201
|
+
const commits = getCommitLog({
|
|
202
|
+
...options,
|
|
203
|
+
to: safeHash,
|
|
204
|
+
maxCount: 1,
|
|
205
|
+
});
|
|
206
|
+
return commits[0] ?? null;
|
|
207
|
+
}
|
|
208
|
+
catch {
|
|
209
|
+
return null;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Checks if a commit exists in the repository.
|
|
214
|
+
*
|
|
215
|
+
* @param hash - Commit hash to check
|
|
216
|
+
* @param options - Additional options
|
|
217
|
+
* @returns True if commit exists
|
|
218
|
+
*/
|
|
219
|
+
function commitExists(hash, options = {}) {
|
|
220
|
+
const safeHash = escapeGitRef(hash);
|
|
221
|
+
try {
|
|
222
|
+
execSync(`git cat-file -t ${safeHash}`, {
|
|
223
|
+
encoding: 'utf-8',
|
|
224
|
+
cwd: options.cwd,
|
|
225
|
+
timeout: options.timeout ?? 5000,
|
|
226
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
227
|
+
});
|
|
228
|
+
return true;
|
|
229
|
+
}
|
|
230
|
+
catch {
|
|
231
|
+
return false;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Parses raw git log output into GitCommit objects.
|
|
236
|
+
*
|
|
237
|
+
* @param output - Raw git log output
|
|
238
|
+
* @returns Array of GitCommit objects
|
|
239
|
+
*/
|
|
240
|
+
function parseCommitLog(output) {
|
|
241
|
+
const commits = [];
|
|
242
|
+
if (!output.trim()) {
|
|
243
|
+
return commits;
|
|
244
|
+
}
|
|
245
|
+
// Split by record separator
|
|
246
|
+
const records = splitByDelimiter(output, RECORD_SEPARATOR);
|
|
247
|
+
for (const record of records) {
|
|
248
|
+
const trimmed = record.trim();
|
|
249
|
+
if (!trimmed)
|
|
250
|
+
continue;
|
|
251
|
+
// Split by NUL character
|
|
252
|
+
const fields = splitByDelimiter(trimmed, '\x00');
|
|
253
|
+
if (fields.length < 10)
|
|
254
|
+
continue;
|
|
255
|
+
const [hash, authorName, authorEmail, authorDate, committerName, committerEmail, commitDate, subject, body, parentsStr, refsStr] = fields;
|
|
256
|
+
// Parse parents (space-separated hashes)
|
|
257
|
+
const parents = parentsStr ? splitByDelimiter(parentsStr, ' ').filter((p) => p.trim()) : [];
|
|
258
|
+
// Parse refs (comma-separated, may have prefixes like 'HEAD -> ')
|
|
259
|
+
const refs = parseRefs(refsStr || '');
|
|
260
|
+
commits.push(createGitCommit({
|
|
261
|
+
hash,
|
|
262
|
+
authorName,
|
|
263
|
+
authorEmail,
|
|
264
|
+
authorDate,
|
|
265
|
+
committerName,
|
|
266
|
+
committerEmail,
|
|
267
|
+
commitDate,
|
|
268
|
+
subject,
|
|
269
|
+
body: body || undefined,
|
|
270
|
+
parents,
|
|
271
|
+
refs,
|
|
272
|
+
}));
|
|
273
|
+
}
|
|
274
|
+
return commits;
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Parses ref string from git log.
|
|
278
|
+
*
|
|
279
|
+
* @param refsStr - Raw refs string from git log
|
|
280
|
+
* @returns Array of ref names
|
|
281
|
+
*/
|
|
282
|
+
function parseRefs(refsStr) {
|
|
283
|
+
if (!refsStr.trim()) {
|
|
284
|
+
return [];
|
|
285
|
+
}
|
|
286
|
+
const refs = [];
|
|
287
|
+
const parts = splitByDelimiter(refsStr, ',');
|
|
288
|
+
for (const part of parts) {
|
|
289
|
+
let ref = part.trim();
|
|
290
|
+
// Handle 'HEAD -> branch' format
|
|
291
|
+
const arrowIndex = findSubstring(ref, ' -> ');
|
|
292
|
+
if (arrowIndex !== -1) {
|
|
293
|
+
refs.push('HEAD');
|
|
294
|
+
ref = ref.slice(arrowIndex + 4);
|
|
295
|
+
}
|
|
296
|
+
// Handle 'tag: tagname' format
|
|
297
|
+
if (startsWithPrefix$2(ref, 'tag: ')) {
|
|
298
|
+
ref = ref.slice(5);
|
|
299
|
+
}
|
|
300
|
+
if (ref) {
|
|
301
|
+
refs.push(ref);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
return refs;
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* Splits string by delimiter (no regex).
|
|
308
|
+
*
|
|
309
|
+
* @param str - String to split
|
|
310
|
+
* @param delimiter - Delimiter to split by
|
|
311
|
+
* @returns Array of parts
|
|
312
|
+
*/
|
|
313
|
+
function splitByDelimiter(str, delimiter) {
|
|
314
|
+
const parts = [];
|
|
315
|
+
let current = '';
|
|
316
|
+
let i = 0;
|
|
317
|
+
while (i < str.length) {
|
|
318
|
+
if (matchesAt(str, i, delimiter)) {
|
|
319
|
+
parts.push(current);
|
|
320
|
+
current = '';
|
|
321
|
+
i += delimiter.length;
|
|
322
|
+
}
|
|
323
|
+
else {
|
|
324
|
+
current += str[i];
|
|
325
|
+
i++;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
parts.push(current);
|
|
329
|
+
return parts;
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* Checks if string matches at position.
|
|
333
|
+
*
|
|
334
|
+
* @param str - String to check
|
|
335
|
+
* @param pos - Position to check at
|
|
336
|
+
* @param pattern - Pattern to match
|
|
337
|
+
* @returns True if matches
|
|
338
|
+
*/
|
|
339
|
+
function matchesAt(str, pos, pattern) {
|
|
340
|
+
if (pos + pattern.length > str.length)
|
|
341
|
+
return false;
|
|
342
|
+
for (let i = 0; i < pattern.length; i++) {
|
|
343
|
+
if (str[pos + i] !== pattern[i])
|
|
344
|
+
return false;
|
|
345
|
+
}
|
|
346
|
+
return true;
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* Finds substring position (no regex).
|
|
350
|
+
*
|
|
351
|
+
* @param str - String to search
|
|
352
|
+
* @param pattern - Pattern to find
|
|
353
|
+
* @returns Position or -1 if not found
|
|
354
|
+
*/
|
|
355
|
+
function findSubstring(str, pattern) {
|
|
356
|
+
for (let i = 0; i <= str.length - pattern.length; i++) {
|
|
357
|
+
if (matchesAt(str, i, pattern)) {
|
|
358
|
+
return i;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
return -1;
|
|
362
|
+
}
|
|
363
|
+
/**
|
|
364
|
+
* Checks if string starts with prefix (no regex).
|
|
365
|
+
*
|
|
366
|
+
* @param str - String to check
|
|
367
|
+
* @param prefix - Prefix to check for
|
|
368
|
+
* @returns True if starts with prefix
|
|
369
|
+
*/
|
|
370
|
+
function startsWithPrefix$2(str, prefix) {
|
|
371
|
+
return matchesAt(str, 0, prefix);
|
|
372
|
+
}
|
|
373
|
+
// ============================================================================
|
|
374
|
+
// Security helpers - character-by-character validation (no regex)
|
|
375
|
+
// ============================================================================
|
|
376
|
+
/**
|
|
377
|
+
* Maximum allowed git reference length.
|
|
378
|
+
*/
|
|
379
|
+
const MAX_REF_LENGTH = 256;
|
|
380
|
+
/**
|
|
381
|
+
* Escapes a git reference for safe use in shell commands.
|
|
382
|
+
*
|
|
383
|
+
* @param ref - Reference to escape
|
|
384
|
+
* @returns Safe reference string
|
|
385
|
+
* @throws {Error} If reference contains invalid characters
|
|
386
|
+
*/
|
|
387
|
+
function escapeGitRef(ref) {
|
|
388
|
+
if (!ref || typeof ref !== 'string') {
|
|
389
|
+
throw createError('Git reference is required');
|
|
390
|
+
}
|
|
391
|
+
if (ref.length > MAX_REF_LENGTH) {
|
|
392
|
+
throw createError(`Git reference exceeds maximum length of ${MAX_REF_LENGTH}`);
|
|
393
|
+
}
|
|
394
|
+
const safe = [];
|
|
395
|
+
for (let i = 0; i < ref.length; i++) {
|
|
396
|
+
const code = ref.charCodeAt(i);
|
|
397
|
+
// Allow: a-z, A-Z, 0-9, /, -, _, ., ~, ^, @, {, }
|
|
398
|
+
if ((code >= 97 && code <= 122) || // a-z
|
|
399
|
+
(code >= 65 && code <= 90) || // A-Z
|
|
400
|
+
(code >= 48 && code <= 57) || // 0-9
|
|
401
|
+
code === 47 || // /
|
|
402
|
+
code === 45 || // -
|
|
403
|
+
code === 95 || // _
|
|
404
|
+
code === 46 || // .
|
|
405
|
+
code === 126 || // ~
|
|
406
|
+
code === 94 || // ^
|
|
407
|
+
code === 64 || // @
|
|
408
|
+
code === 123 || // {
|
|
409
|
+
code === 125 // }
|
|
410
|
+
) {
|
|
411
|
+
safe.push(ref[i]);
|
|
412
|
+
}
|
|
413
|
+
else {
|
|
414
|
+
throw createError(`Invalid character in git reference at position ${i}: "${ref[i]}"`);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
return safe.join('');
|
|
418
|
+
}
|
|
419
|
+
/**
|
|
420
|
+
* Maximum allowed git path length.
|
|
421
|
+
*/
|
|
422
|
+
const MAX_PATH_LENGTH$1 = 4096;
|
|
423
|
+
/**
|
|
424
|
+
* Escapes a file path for safe use in git commands.
|
|
425
|
+
*
|
|
426
|
+
* @param path - Path to escape
|
|
427
|
+
* @returns Safe path string
|
|
428
|
+
* @throws {Error} If path contains invalid characters
|
|
429
|
+
*/
|
|
430
|
+
function escapeGitPath(path) {
|
|
431
|
+
if (!path || typeof path !== 'string') {
|
|
432
|
+
throw createError('Path is required');
|
|
433
|
+
}
|
|
434
|
+
if (path.length > MAX_PATH_LENGTH$1) {
|
|
435
|
+
throw createError(`Path exceeds maximum length of ${MAX_PATH_LENGTH$1}`);
|
|
436
|
+
}
|
|
437
|
+
const safe = [];
|
|
438
|
+
for (let i = 0; i < path.length; i++) {
|
|
439
|
+
const code = path.charCodeAt(i);
|
|
440
|
+
// Allow: a-z, A-Z, 0-9, /, \, -, _, ., space
|
|
441
|
+
if ((code >= 97 && code <= 122) || // a-z
|
|
442
|
+
(code >= 65 && code <= 90) || // A-Z
|
|
443
|
+
(code >= 48 && code <= 57) || // 0-9
|
|
444
|
+
code === 47 || // /
|
|
445
|
+
code === 92 || // \
|
|
446
|
+
code === 45 || // -
|
|
447
|
+
code === 95 || // _
|
|
448
|
+
code === 46 || // .
|
|
449
|
+
code === 32 // space
|
|
450
|
+
) {
|
|
451
|
+
safe.push(path[i]);
|
|
452
|
+
}
|
|
453
|
+
else {
|
|
454
|
+
throw createError(`Invalid character in path at position ${i}: "${path[i]}"`);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
return safe.join('');
|
|
458
|
+
}
|
|
459
|
+
/**
|
|
460
|
+
* Maximum allowed argument length.
|
|
461
|
+
*/
|
|
462
|
+
const MAX_ARG_LENGTH = 1000;
|
|
463
|
+
/**
|
|
464
|
+
* Escapes a general git argument for safe use in shell commands.
|
|
465
|
+
*
|
|
466
|
+
* @param arg - Argument to escape
|
|
467
|
+
* @returns Safe argument string
|
|
468
|
+
* @throws {Error} If argument contains invalid characters
|
|
469
|
+
*/
|
|
470
|
+
function escapeGitArg(arg) {
|
|
471
|
+
if (!arg || typeof arg !== 'string') {
|
|
472
|
+
throw createError('Argument is required');
|
|
473
|
+
}
|
|
474
|
+
if (arg.length > MAX_ARG_LENGTH) {
|
|
475
|
+
throw createError(`Argument exceeds maximum length of ${MAX_ARG_LENGTH}`);
|
|
476
|
+
}
|
|
477
|
+
const safe = [];
|
|
478
|
+
for (let i = 0; i < arg.length; i++) {
|
|
479
|
+
const code = arg.charCodeAt(i);
|
|
480
|
+
// Allow: a-z, A-Z, 0-9, space, @, ., -, _, <, >, +
|
|
481
|
+
if ((code >= 97 && code <= 122) || // a-z
|
|
482
|
+
(code >= 65 && code <= 90) || // A-Z
|
|
483
|
+
(code >= 48 && code <= 57) || // 0-9
|
|
484
|
+
code === 32 || // space
|
|
485
|
+
code === 64 || // @
|
|
486
|
+
code === 46 || // .
|
|
487
|
+
code === 45 || // -
|
|
488
|
+
code === 95 || // _
|
|
489
|
+
code === 60 || // <
|
|
490
|
+
code === 62 || // >
|
|
491
|
+
code === 43 // +
|
|
492
|
+
) {
|
|
493
|
+
safe.push(arg[i]);
|
|
494
|
+
}
|
|
495
|
+
else {
|
|
496
|
+
throw createError(`Invalid character in argument at position ${i}: "${arg[i]}"`);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
return safe.join('');
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
/**
|
|
503
|
+
* Safe copies of Date built-in via factory function and static methods.
|
|
504
|
+
*
|
|
505
|
+
* Since constructors cannot be safely captured via Object.assign, this module
|
|
506
|
+
* provides a factory function that uses Reflect.construct internally.
|
|
507
|
+
*
|
|
508
|
+
* These references are captured at module initialization time to protect against
|
|
509
|
+
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
510
|
+
*
|
|
511
|
+
* @module @hyperfrontend/immutable-api-utils/built-in-copy/date
|
|
512
|
+
*/
|
|
513
|
+
// Capture references at module initialization time
|
|
514
|
+
const _Date = globalThis.Date;
|
|
515
|
+
const _Reflect = globalThis.Reflect;
|
|
516
|
+
function createDate(...args) {
|
|
517
|
+
return _Reflect.construct(_Date, args);
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
/**
|
|
521
|
+
* Safe copies of Number built-in methods and constants.
|
|
522
|
+
*
|
|
523
|
+
* These references are captured at module initialization time to protect against
|
|
524
|
+
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
525
|
+
*
|
|
526
|
+
* @module @hyperfrontend/immutable-api-utils/built-in-copy/number
|
|
527
|
+
*/
|
|
528
|
+
// Capture references at module initialization time
|
|
529
|
+
const _parseInt = globalThis.parseInt;
|
|
530
|
+
const _isNaN = globalThis.isNaN;
|
|
531
|
+
// ============================================================================
|
|
532
|
+
// Parsing
|
|
533
|
+
// ============================================================================
|
|
534
|
+
/**
|
|
535
|
+
* (Safe copy) Parses a string and returns an integer.
|
|
536
|
+
*/
|
|
537
|
+
const parseInt = _parseInt;
|
|
538
|
+
// ============================================================================
|
|
539
|
+
// Global Type Checking (legacy, less strict)
|
|
540
|
+
// ============================================================================
|
|
541
|
+
/**
|
|
542
|
+
* (Safe copy) Global isNaN function (coerces to number first, less strict than Number.isNaN).
|
|
543
|
+
*/
|
|
544
|
+
const globalIsNaN = _isNaN;
|
|
545
|
+
|
|
546
|
+
/**
|
|
547
|
+
* Creates a lightweight git tag.
|
|
548
|
+
*
|
|
549
|
+
* @param options - Tag creation options
|
|
550
|
+
* @returns A new GitTag object
|
|
551
|
+
*
|
|
552
|
+
* @example
|
|
553
|
+
* const tag = createLightweightTag({
|
|
554
|
+
* name: 'v1.0.0',
|
|
555
|
+
* commitHash: 'abc123...',
|
|
556
|
+
* })
|
|
557
|
+
*/
|
|
558
|
+
function createLightweightTag(options) {
|
|
559
|
+
return {
|
|
560
|
+
name: options.name,
|
|
561
|
+
commitHash: options.commitHash,
|
|
562
|
+
type: 'lightweight',
|
|
563
|
+
};
|
|
564
|
+
}
|
|
565
|
+
/**
|
|
566
|
+
* Creates an annotated git tag.
|
|
567
|
+
*
|
|
568
|
+
* @param options - Tag creation options
|
|
569
|
+
* @returns A new GitTag object
|
|
570
|
+
*
|
|
571
|
+
* @example
|
|
572
|
+
* const tag = createAnnotatedTag({
|
|
573
|
+
* name: 'v1.0.0',
|
|
574
|
+
* commitHash: 'abc123...',
|
|
575
|
+
* message: 'Release v1.0.0',
|
|
576
|
+
* taggerName: 'John Doe',
|
|
577
|
+
* taggerEmail: 'john@example.com',
|
|
578
|
+
* tagDate: '2026-03-12T10:00:00Z',
|
|
579
|
+
* })
|
|
580
|
+
*/
|
|
581
|
+
function createAnnotatedTag(options) {
|
|
582
|
+
return {
|
|
583
|
+
name: options.name,
|
|
584
|
+
commitHash: options.commitHash,
|
|
585
|
+
type: 'annotated',
|
|
586
|
+
message: options.message,
|
|
587
|
+
taggerName: options.taggerName,
|
|
588
|
+
taggerEmail: options.taggerEmail,
|
|
589
|
+
tagDate: options.tagDate,
|
|
590
|
+
};
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
/**
|
|
594
|
+
* Default tag options.
|
|
595
|
+
*/
|
|
596
|
+
const DEFAULT_TAG_OPTIONS = {
|
|
597
|
+
timeout: 10000,
|
|
598
|
+
};
|
|
599
|
+
/**
|
|
600
|
+
* Gets all tags from the repository.
|
|
601
|
+
*
|
|
602
|
+
* @param options - Tag listing options
|
|
603
|
+
* @returns Array of GitTag objects
|
|
604
|
+
*
|
|
605
|
+
* @example
|
|
606
|
+
* const tags = getTags()
|
|
607
|
+
* const versionTags = getTags({ pattern: 'v' })
|
|
608
|
+
*/
|
|
609
|
+
function getTags(options = {}) {
|
|
610
|
+
const opts = { ...DEFAULT_TAG_OPTIONS, ...options };
|
|
611
|
+
const args = ['tag', '-l', '--sort=-creatordate'];
|
|
612
|
+
if (opts.pattern) {
|
|
613
|
+
const safePattern = escapeGitTagPattern(opts.pattern);
|
|
614
|
+
args.push(safePattern + '*');
|
|
615
|
+
}
|
|
616
|
+
try {
|
|
617
|
+
const output = execSync(`git ${args.join(' ')}`, {
|
|
618
|
+
encoding: 'utf-8',
|
|
619
|
+
cwd: opts.cwd,
|
|
620
|
+
timeout: opts.timeout,
|
|
621
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
622
|
+
});
|
|
623
|
+
const tagNames = output
|
|
624
|
+
.split('\n')
|
|
625
|
+
.map((line) => line.trim())
|
|
626
|
+
.filter((line) => line.length > 0);
|
|
627
|
+
// Limit results if requested
|
|
628
|
+
const limitedNames = opts.maxCount ? tagNames.slice(0, opts.maxCount) : tagNames;
|
|
629
|
+
// Get details for each tag
|
|
630
|
+
const tags = [];
|
|
631
|
+
for (const name of limitedNames) {
|
|
632
|
+
const tag = getTagDetails(name, opts);
|
|
633
|
+
if (tag) {
|
|
634
|
+
tags.push(tag);
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
return tags;
|
|
638
|
+
}
|
|
639
|
+
catch {
|
|
640
|
+
return [];
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
/**
|
|
644
|
+
* Gets detailed information about a specific tag.
|
|
645
|
+
*
|
|
646
|
+
* @param name - The tag name to look up
|
|
647
|
+
* @param options - Configuration for the tag operation
|
|
648
|
+
* @returns GitTag or null if not found
|
|
649
|
+
*
|
|
650
|
+
* @example
|
|
651
|
+
* const tag = getTag('v1.0.0')
|
|
652
|
+
*/
|
|
653
|
+
function getTag(name, options = {}) {
|
|
654
|
+
return getTagDetails(name, { ...DEFAULT_TAG_OPTIONS, ...options });
|
|
655
|
+
}
|
|
656
|
+
/**
|
|
657
|
+
* Gets tag details including type and commit hash.
|
|
658
|
+
*
|
|
659
|
+
* @param name - The tag name to retrieve details for
|
|
660
|
+
* @param options - Configuration for the tag operation
|
|
661
|
+
* @returns GitTag or null
|
|
662
|
+
*/
|
|
663
|
+
function getTagDetails(name, options) {
|
|
664
|
+
const safeName = escapeGitRef(name);
|
|
665
|
+
try {
|
|
666
|
+
// Get the commit hash the tag points to
|
|
667
|
+
const commitHash = execSync(`git rev-list -1 ${safeName}`, {
|
|
668
|
+
encoding: 'utf-8',
|
|
669
|
+
cwd: options.cwd,
|
|
670
|
+
timeout: options.timeout,
|
|
671
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
672
|
+
}).trim();
|
|
673
|
+
// Check if it's an annotated tag by trying to get tag message
|
|
674
|
+
try {
|
|
675
|
+
const tagInfo = execSync(`git cat-file tag ${safeName}`, {
|
|
676
|
+
encoding: 'utf-8',
|
|
677
|
+
cwd: options.cwd,
|
|
678
|
+
timeout: options.timeout,
|
|
679
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
680
|
+
});
|
|
681
|
+
// Parse annotated tag info
|
|
682
|
+
const parsed = parseAnnotatedTagInfo(tagInfo);
|
|
683
|
+
return createAnnotatedTag({
|
|
684
|
+
name,
|
|
685
|
+
commitHash,
|
|
686
|
+
message: parsed.message,
|
|
687
|
+
taggerName: parsed.taggerName,
|
|
688
|
+
taggerEmail: parsed.taggerEmail,
|
|
689
|
+
tagDate: parsed.tagDate,
|
|
690
|
+
});
|
|
691
|
+
}
|
|
692
|
+
catch {
|
|
693
|
+
// Not an annotated tag, it's lightweight
|
|
694
|
+
return createLightweightTag({
|
|
695
|
+
name,
|
|
696
|
+
commitHash,
|
|
697
|
+
});
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
catch {
|
|
701
|
+
return null;
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
/**
|
|
705
|
+
* Parses annotated tag info from git cat-file output.
|
|
706
|
+
*
|
|
707
|
+
* @param info - Raw tag info
|
|
708
|
+
* @returns Parsed info
|
|
709
|
+
*/
|
|
710
|
+
function parseAnnotatedTagInfo(info) {
|
|
711
|
+
const lines = info.split('\n');
|
|
712
|
+
let taggerName = '';
|
|
713
|
+
let taggerEmail = '';
|
|
714
|
+
let tagDate = '';
|
|
715
|
+
let messageStart = -1;
|
|
716
|
+
for (let i = 0; i < lines.length; i++) {
|
|
717
|
+
const line = lines[i];
|
|
718
|
+
if (startsWithPrefix$1(line, 'tagger ')) {
|
|
719
|
+
const taggerLine = line.slice(7);
|
|
720
|
+
const parsed = parseTaggerLine(taggerLine);
|
|
721
|
+
taggerName = parsed.name;
|
|
722
|
+
taggerEmail = parsed.email;
|
|
723
|
+
tagDate = parsed.date;
|
|
724
|
+
}
|
|
725
|
+
// Empty line marks start of message
|
|
726
|
+
if (line === '' && messageStart === -1) {
|
|
727
|
+
messageStart = i + 1;
|
|
728
|
+
break;
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
const message = messageStart >= 0 ? lines.slice(messageStart).join('\n').trim() : '';
|
|
732
|
+
return {
|
|
733
|
+
message,
|
|
734
|
+
taggerName,
|
|
735
|
+
taggerEmail,
|
|
736
|
+
tagDate,
|
|
737
|
+
};
|
|
738
|
+
}
|
|
739
|
+
/**
|
|
740
|
+
* Parses tagger line from annotated tag.
|
|
741
|
+
* Format: Name <email> timestamp timezone
|
|
742
|
+
*
|
|
743
|
+
* @param line - Raw tagger line from git output
|
|
744
|
+
* @returns Parsed tagger info with name, email, and date
|
|
745
|
+
*/
|
|
746
|
+
function parseTaggerLine(line) {
|
|
747
|
+
let name = '';
|
|
748
|
+
let email = '';
|
|
749
|
+
let date = '';
|
|
750
|
+
// Find email in angle brackets
|
|
751
|
+
let emailStart = -1;
|
|
752
|
+
let emailEnd = -1;
|
|
753
|
+
for (let i = 0; i < line.length; i++) {
|
|
754
|
+
if (line[i] === '<') {
|
|
755
|
+
emailStart = i + 1;
|
|
756
|
+
}
|
|
757
|
+
else if (line[i] === '>' && emailStart !== -1) {
|
|
758
|
+
emailEnd = i;
|
|
759
|
+
break;
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
if (emailStart !== -1 && emailEnd !== -1) {
|
|
763
|
+
name = line.slice(0, emailStart - 1).trim();
|
|
764
|
+
email = line.slice(emailStart, emailEnd);
|
|
765
|
+
// Rest is timestamp and timezone
|
|
766
|
+
const rest = line.slice(emailEnd + 1).trim();
|
|
767
|
+
const parts = rest.split(' ');
|
|
768
|
+
if (parts.length >= 1) {
|
|
769
|
+
// Convert Unix timestamp to ISO 8601
|
|
770
|
+
const timestamp = parseInt(parts[0], 10);
|
|
771
|
+
if (!globalIsNaN(timestamp)) {
|
|
772
|
+
date = createDate(timestamp * 1000).toISOString();
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
return { name, email, date };
|
|
777
|
+
}
|
|
778
|
+
/**
|
|
779
|
+
* Checks if a tag exists.
|
|
780
|
+
*
|
|
781
|
+
* @param name - The tag name to verify
|
|
782
|
+
* @param options - Configuration for the tag operation
|
|
783
|
+
* @returns True if tag exists
|
|
784
|
+
*
|
|
785
|
+
* @example
|
|
786
|
+
* if (tagExists('v1.0.0')) { ... }
|
|
787
|
+
*/
|
|
788
|
+
function tagExists(name, options = {}) {
|
|
789
|
+
const opts = { ...DEFAULT_TAG_OPTIONS, ...options };
|
|
790
|
+
const safeName = escapeGitRef(name);
|
|
791
|
+
try {
|
|
792
|
+
execSync(`git rev-parse ${safeName}`, {
|
|
793
|
+
encoding: 'utf-8',
|
|
794
|
+
cwd: opts.cwd,
|
|
795
|
+
timeout: opts.timeout,
|
|
796
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
797
|
+
});
|
|
798
|
+
return true;
|
|
799
|
+
}
|
|
800
|
+
catch {
|
|
801
|
+
return false;
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
/**
|
|
805
|
+
* Gets the latest tag (by creation date).
|
|
806
|
+
*
|
|
807
|
+
* @param options - Tag options with optional pattern
|
|
808
|
+
* @returns Latest GitTag or null
|
|
809
|
+
*
|
|
810
|
+
* @example
|
|
811
|
+
* const latest = getLatestTag()
|
|
812
|
+
* const latestVersion = getLatestTag({ pattern: 'v' })
|
|
813
|
+
*/
|
|
814
|
+
function getLatestTag(options = {}) {
|
|
815
|
+
const tags = getTags({ ...options, maxCount: 1 });
|
|
816
|
+
return tags[0] ?? null;
|
|
817
|
+
}
|
|
818
|
+
/**
|
|
819
|
+
* Gets tags that match a package name.
|
|
820
|
+
*
|
|
821
|
+
* @param packageName - Package name to match
|
|
822
|
+
* @param options - Tag options
|
|
823
|
+
* @returns Array of matching tags
|
|
824
|
+
*
|
|
825
|
+
* @example
|
|
826
|
+
* const tags = getTagsForPackage('@scope/pkg')
|
|
827
|
+
*/
|
|
828
|
+
function getTagsForPackage(packageName, options = {}) {
|
|
829
|
+
// Common patterns: @scope/pkg@version, pkg@version, pkg-vversion
|
|
830
|
+
const allTags = getTags(options);
|
|
831
|
+
return allTags.filter((tag) => {
|
|
832
|
+
// Check if tag starts with package name followed by @ or -v
|
|
833
|
+
const name = tag.name;
|
|
834
|
+
// Pattern: package@version
|
|
835
|
+
if (startsWithPrefix$1(name, packageName + '@')) {
|
|
836
|
+
return true;
|
|
837
|
+
}
|
|
838
|
+
// Pattern: package-v
|
|
839
|
+
if (startsWithPrefix$1(name, packageName + '-v')) {
|
|
840
|
+
return true;
|
|
841
|
+
}
|
|
842
|
+
return false;
|
|
843
|
+
});
|
|
844
|
+
}
|
|
845
|
+
// ============================================================================
|
|
846
|
+
// Helper functions
|
|
847
|
+
// ============================================================================
|
|
848
|
+
/**
|
|
849
|
+
* Checks if string starts with prefix (no regex).
|
|
850
|
+
*
|
|
851
|
+
* @param str - The string to check
|
|
852
|
+
* @param prefix - The prefix to look for
|
|
853
|
+
* @returns True if str starts with the given prefix
|
|
854
|
+
*/
|
|
855
|
+
function startsWithPrefix$1(str, prefix) {
|
|
856
|
+
if (prefix.length > str.length)
|
|
857
|
+
return false;
|
|
858
|
+
for (let i = 0; i < prefix.length; i++) {
|
|
859
|
+
if (str[i] !== prefix[i])
|
|
860
|
+
return false;
|
|
861
|
+
}
|
|
862
|
+
return true;
|
|
863
|
+
}
|
|
864
|
+
/**
|
|
865
|
+
* Maximum tag pattern length.
|
|
866
|
+
*/
|
|
867
|
+
const MAX_PATTERN_LENGTH = 256;
|
|
868
|
+
/**
|
|
869
|
+
* Escapes a tag pattern for safe use in git commands.
|
|
870
|
+
*
|
|
871
|
+
* @param pattern - Pattern to escape
|
|
872
|
+
* @returns Safe pattern string
|
|
873
|
+
*/
|
|
874
|
+
function escapeGitTagPattern(pattern) {
|
|
875
|
+
if (!pattern || typeof pattern !== 'string') {
|
|
876
|
+
throw createError('Pattern is required');
|
|
877
|
+
}
|
|
878
|
+
if (pattern.length > MAX_PATTERN_LENGTH) {
|
|
879
|
+
throw createError(`Pattern exceeds maximum length of ${MAX_PATTERN_LENGTH}`);
|
|
880
|
+
}
|
|
881
|
+
const safe = [];
|
|
882
|
+
for (let i = 0; i < pattern.length; i++) {
|
|
883
|
+
const code = pattern.charCodeAt(i);
|
|
884
|
+
// Allow: a-z, A-Z, 0-9, /, -, _, ., @
|
|
885
|
+
if ((code >= 97 && code <= 122) || // a-z
|
|
886
|
+
(code >= 65 && code <= 90) || // A-Z
|
|
887
|
+
(code >= 48 && code <= 57) || // 0-9
|
|
888
|
+
code === 47 || // /
|
|
889
|
+
code === 45 || // -
|
|
890
|
+
code === 95 || // _
|
|
891
|
+
code === 46 || // .
|
|
892
|
+
code === 64 // @
|
|
893
|
+
) {
|
|
894
|
+
safe.push(pattern[i]);
|
|
895
|
+
}
|
|
896
|
+
else {
|
|
897
|
+
throw createError(`Invalid character in pattern at position ${i}: "${pattern[i]}"`);
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
return safe.join('');
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
/**
|
|
904
|
+
* Creates a new tag.
|
|
905
|
+
*
|
|
906
|
+
* @param name - The name for the new tag
|
|
907
|
+
* @param options - Configuration including optional message for annotated tags
|
|
908
|
+
* @returns Created GitTag
|
|
909
|
+
*
|
|
910
|
+
* @example
|
|
911
|
+
* // Create lightweight tag
|
|
912
|
+
* const tag = createTag('v1.0.0')
|
|
913
|
+
*
|
|
914
|
+
* // Create annotated tag
|
|
915
|
+
* const tag = createTag('v1.0.0', { message: 'Release v1.0.0' })
|
|
916
|
+
*/
|
|
917
|
+
function createTag(name, options = {}) {
|
|
918
|
+
const opts = { ...DEFAULT_TAG_OPTIONS, ...options };
|
|
919
|
+
const safeName = escapeGitRef(name);
|
|
920
|
+
const args = ['tag'];
|
|
921
|
+
if (opts.force) {
|
|
922
|
+
args.push('-f');
|
|
923
|
+
}
|
|
924
|
+
if (opts.message) {
|
|
925
|
+
// Annotated tag
|
|
926
|
+
args.push('-a');
|
|
927
|
+
args.push(safeName);
|
|
928
|
+
args.push('-m');
|
|
929
|
+
args.push(`"${escapeGitMessage(opts.message)}"`);
|
|
930
|
+
}
|
|
931
|
+
else {
|
|
932
|
+
// Lightweight tag
|
|
933
|
+
args.push(safeName);
|
|
934
|
+
}
|
|
935
|
+
if (opts.target) {
|
|
936
|
+
args.push(escapeGitRef(opts.target));
|
|
937
|
+
}
|
|
938
|
+
try {
|
|
939
|
+
execSync(`git ${args.join(' ')}`, {
|
|
940
|
+
encoding: 'utf-8',
|
|
941
|
+
cwd: opts.cwd,
|
|
942
|
+
timeout: opts.timeout,
|
|
943
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
944
|
+
});
|
|
945
|
+
// Get the created tag
|
|
946
|
+
const tag = getTag(name, opts);
|
|
947
|
+
if (!tag) {
|
|
948
|
+
throw createError(`Failed to retrieve created tag: ${name}`);
|
|
949
|
+
}
|
|
950
|
+
return tag;
|
|
951
|
+
}
|
|
952
|
+
catch (error) {
|
|
953
|
+
if (error instanceof Error) {
|
|
954
|
+
throw createError(`Failed to create tag ${name}: ${error.message}`);
|
|
955
|
+
}
|
|
956
|
+
throw error;
|
|
957
|
+
}
|
|
958
|
+
}
|
|
959
|
+
/**
|
|
960
|
+
* Deletes a tag.
|
|
961
|
+
*
|
|
962
|
+
* @param name - The tag name to delete
|
|
963
|
+
* @param options - Configuration for the tag operation
|
|
964
|
+
* @returns True if deleted
|
|
965
|
+
*
|
|
966
|
+
* @example
|
|
967
|
+
* const deleted = deleteTag('v1.0.0')
|
|
968
|
+
*/
|
|
969
|
+
function deleteTag(name, options = {}) {
|
|
970
|
+
const opts = { ...DEFAULT_TAG_OPTIONS, ...options };
|
|
971
|
+
const safeName = escapeGitRef(name);
|
|
972
|
+
try {
|
|
973
|
+
execSync(`git tag -d ${safeName}`, {
|
|
974
|
+
encoding: 'utf-8',
|
|
975
|
+
cwd: opts.cwd,
|
|
976
|
+
timeout: opts.timeout,
|
|
977
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
978
|
+
});
|
|
979
|
+
return true;
|
|
980
|
+
}
|
|
981
|
+
catch {
|
|
982
|
+
return false;
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
/**
|
|
986
|
+
* Pushes a tag to a remote.
|
|
987
|
+
*
|
|
988
|
+
* @param name - The tag to push to the remote
|
|
989
|
+
* @param remote - Remote name (defaults to 'origin')
|
|
990
|
+
* @param options - Configuration for the tag operation
|
|
991
|
+
* @returns True if pushed successfully
|
|
992
|
+
*
|
|
993
|
+
* @example
|
|
994
|
+
* pushTag('v1.0.0')
|
|
995
|
+
* pushTag('v1.0.0', 'upstream')
|
|
996
|
+
*/
|
|
997
|
+
function pushTag(name, remote = 'origin', options = {}) {
|
|
998
|
+
const opts = { ...DEFAULT_TAG_OPTIONS, ...options };
|
|
999
|
+
const safeName = escapeGitRef(name);
|
|
1000
|
+
const safeRemote = escapeGitRef(remote);
|
|
1001
|
+
try {
|
|
1002
|
+
execSync(`git push ${safeRemote} ${safeName}`, {
|
|
1003
|
+
encoding: 'utf-8',
|
|
1004
|
+
cwd: opts.cwd,
|
|
1005
|
+
timeout: opts.timeout * 3, // Allow more time for network
|
|
1006
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
1007
|
+
});
|
|
1008
|
+
return true;
|
|
1009
|
+
}
|
|
1010
|
+
catch {
|
|
1011
|
+
return false;
|
|
1012
|
+
}
|
|
1013
|
+
}
|
|
1014
|
+
// ============================================================================
|
|
1015
|
+
// Security helpers
|
|
1016
|
+
// ============================================================================
|
|
1017
|
+
/**
|
|
1018
|
+
* Maximum message length.
|
|
1019
|
+
*/
|
|
1020
|
+
const MAX_MESSAGE_LENGTH = 10000;
|
|
1021
|
+
/**
|
|
1022
|
+
* Escapes a message for safe use in git commands.
|
|
1023
|
+
*
|
|
1024
|
+
* @param message - Message to escape
|
|
1025
|
+
* @returns Safe message string
|
|
1026
|
+
*/
|
|
1027
|
+
function escapeGitMessage(message) {
|
|
1028
|
+
if (!message || typeof message !== 'string') {
|
|
1029
|
+
throw createError('Message is required');
|
|
1030
|
+
}
|
|
1031
|
+
if (message.length > MAX_MESSAGE_LENGTH) {
|
|
1032
|
+
throw createError(`Message exceeds maximum length of ${MAX_MESSAGE_LENGTH}`);
|
|
1033
|
+
}
|
|
1034
|
+
const safe = [];
|
|
1035
|
+
for (let i = 0; i < message.length; i++) {
|
|
1036
|
+
const char = message[i];
|
|
1037
|
+
const code = message.charCodeAt(i);
|
|
1038
|
+
// Escape double quotes and backslashes
|
|
1039
|
+
if (char === '"' || char === '\\') {
|
|
1040
|
+
safe.push('\\');
|
|
1041
|
+
safe.push(char);
|
|
1042
|
+
}
|
|
1043
|
+
// Allow printable ASCII and common whitespace
|
|
1044
|
+
else if ((code >= 32 && code <= 126) || // Printable ASCII
|
|
1045
|
+
code === 10 || // newline
|
|
1046
|
+
code === 13 || // carriage return
|
|
1047
|
+
code === 9 // tab
|
|
1048
|
+
) {
|
|
1049
|
+
safe.push(char);
|
|
1050
|
+
}
|
|
1051
|
+
// Skip other control characters
|
|
1052
|
+
}
|
|
1053
|
+
return safe.join('');
|
|
1054
|
+
}
|
|
1055
|
+
|
|
1056
|
+
/**
|
|
1057
|
+
* Default commit options.
|
|
1058
|
+
*/
|
|
1059
|
+
const DEFAULT_COMMIT_OPTIONS = {
|
|
1060
|
+
timeout: 30000,
|
|
1061
|
+
};
|
|
1062
|
+
/**
|
|
1063
|
+
* Creates a new commit.
|
|
1064
|
+
*
|
|
1065
|
+
* @param message - Commit message (subject line)
|
|
1066
|
+
* @param options - Create options
|
|
1067
|
+
* @returns Created GitCommit
|
|
1068
|
+
*
|
|
1069
|
+
* @example
|
|
1070
|
+
* const commit = createCommit('feat: add new feature')
|
|
1071
|
+
* const commit = createCommit('fix: resolve bug', { body: 'Detailed description' })
|
|
1072
|
+
*/
|
|
1073
|
+
function commit(message, options = {}) {
|
|
1074
|
+
const opts = { ...DEFAULT_COMMIT_OPTIONS, ...options };
|
|
1075
|
+
// noEdit is only valid when amending - reuse existing commit message
|
|
1076
|
+
const isNoEditAmend = opts.amend && opts.noEdit;
|
|
1077
|
+
if (!isNoEditAmend && (!message || typeof message !== 'string')) {
|
|
1078
|
+
throw createError('Commit message is required');
|
|
1079
|
+
}
|
|
1080
|
+
const args = ['commit'];
|
|
1081
|
+
if (isNoEditAmend) {
|
|
1082
|
+
// Amend without changing the message
|
|
1083
|
+
args.push('--amend', '--no-edit');
|
|
1084
|
+
}
|
|
1085
|
+
else {
|
|
1086
|
+
const safeMessage = escapeGitMessage(message);
|
|
1087
|
+
// Build message with optional body
|
|
1088
|
+
let fullMessage = safeMessage;
|
|
1089
|
+
if (opts.body) {
|
|
1090
|
+
const safeBody = escapeGitMessage(opts.body);
|
|
1091
|
+
fullMessage = `${safeMessage}\n\n${safeBody}`;
|
|
1092
|
+
}
|
|
1093
|
+
args.push('-m', `"${fullMessage}"`);
|
|
1094
|
+
if (opts.amend) {
|
|
1095
|
+
args.push('--amend');
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
if (opts.allowEmpty) {
|
|
1099
|
+
args.push('--allow-empty');
|
|
1100
|
+
}
|
|
1101
|
+
if (opts.sign) {
|
|
1102
|
+
args.push('-S');
|
|
1103
|
+
}
|
|
1104
|
+
if (opts.noVerify) {
|
|
1105
|
+
args.push('--no-verify');
|
|
1106
|
+
}
|
|
1107
|
+
if (opts.author) {
|
|
1108
|
+
const safeAuthor = escapeAuthor(opts.author);
|
|
1109
|
+
args.push(`--author="${safeAuthor}"`);
|
|
1110
|
+
}
|
|
1111
|
+
// Add specific files if provided
|
|
1112
|
+
if (opts.files && opts.files.length > 0) {
|
|
1113
|
+
args.push('--');
|
|
1114
|
+
for (const file of opts.files) {
|
|
1115
|
+
args.push(escapeFilePath(file));
|
|
1116
|
+
}
|
|
1117
|
+
}
|
|
1118
|
+
try {
|
|
1119
|
+
execSync(`git ${args.join(' ')}`, {
|
|
1120
|
+
encoding: 'utf-8',
|
|
1121
|
+
cwd: opts.cwd,
|
|
1122
|
+
timeout: opts.timeout,
|
|
1123
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
1124
|
+
});
|
|
1125
|
+
// Get the created commit
|
|
1126
|
+
const commit = getCommit('HEAD', opts);
|
|
1127
|
+
if (!commit) {
|
|
1128
|
+
throw createError('Failed to retrieve created commit');
|
|
1129
|
+
}
|
|
1130
|
+
return commit;
|
|
1131
|
+
}
|
|
1132
|
+
catch (error) {
|
|
1133
|
+
if (error instanceof Error) {
|
|
1134
|
+
throw createError(`Failed to create commit: ${error.message}`);
|
|
1135
|
+
}
|
|
1136
|
+
throw error;
|
|
1137
|
+
}
|
|
1138
|
+
}
|
|
1139
|
+
/**
|
|
1140
|
+
* Amends the last commit with new message.
|
|
1141
|
+
*
|
|
1142
|
+
* @param message - The new commit message to use
|
|
1143
|
+
* @param options - Configuration for the commit operation
|
|
1144
|
+
* @returns GitCommit object representing the amended commit
|
|
1145
|
+
*
|
|
1146
|
+
* @example
|
|
1147
|
+
* const commit = amendCommit('feat: improved feature')
|
|
1148
|
+
*/
|
|
1149
|
+
function amendCommit(message, options = {}) {
|
|
1150
|
+
return commit(message, { ...options, amend: true });
|
|
1151
|
+
}
|
|
1152
|
+
/**
|
|
1153
|
+
* Amends the last commit without changing the message.
|
|
1154
|
+
* Useful for adding staged changes to the previous commit.
|
|
1155
|
+
*
|
|
1156
|
+
* @param options - Configuration for the commit operation
|
|
1157
|
+
* @returns GitCommit object representing the amended commit
|
|
1158
|
+
*
|
|
1159
|
+
* @example
|
|
1160
|
+
* stage(['extra-file.ts'])
|
|
1161
|
+
* amendCommitNoEdit() // adds staged files to last commit
|
|
1162
|
+
*/
|
|
1163
|
+
function amendCommitNoEdit(options = {}) {
|
|
1164
|
+
return commit('', { ...options, amend: true, noEdit: true });
|
|
1165
|
+
}
|
|
1166
|
+
/**
|
|
1167
|
+
* Creates an empty commit (useful for CI triggers).
|
|
1168
|
+
*
|
|
1169
|
+
* @param message - Text for the empty commit
|
|
1170
|
+
* @param options - Configuration for the commit operation
|
|
1171
|
+
* @returns GitCommit object representing the new empty commit
|
|
1172
|
+
*
|
|
1173
|
+
* @example
|
|
1174
|
+
* const commit = createEmptyCommit('chore: trigger CI')
|
|
1175
|
+
*/
|
|
1176
|
+
function createEmptyCommit(message, options = {}) {
|
|
1177
|
+
return commit(message, { ...options, allowEmpty: true });
|
|
1178
|
+
}
|
|
1179
|
+
// ============================================================================
|
|
1180
|
+
// Security helpers - character-by-character validation (no regex)
|
|
1181
|
+
// ============================================================================
|
|
1182
|
+
/**
|
|
1183
|
+
* Maximum file path length.
|
|
1184
|
+
*/
|
|
1185
|
+
const MAX_PATH_LENGTH = 4096;
|
|
1186
|
+
/**
|
|
1187
|
+
* Escapes a file path for safe use in git commands.
|
|
1188
|
+
*
|
|
1189
|
+
* @param path - Path to escape
|
|
1190
|
+
* @returns Safe path string
|
|
1191
|
+
*/
|
|
1192
|
+
function escapeFilePath(path) {
|
|
1193
|
+
if (!path || typeof path !== 'string') {
|
|
1194
|
+
throw createError('File path is required');
|
|
1195
|
+
}
|
|
1196
|
+
if (path.length > MAX_PATH_LENGTH) {
|
|
1197
|
+
throw createError(`Path exceeds maximum length of ${MAX_PATH_LENGTH}`);
|
|
1198
|
+
}
|
|
1199
|
+
const safe = [];
|
|
1200
|
+
for (let i = 0; i < path.length; i++) {
|
|
1201
|
+
const code = path.charCodeAt(i);
|
|
1202
|
+
// Allow: a-z, A-Z, 0-9, /, \, -, _, ., space
|
|
1203
|
+
if ((code >= 97 && code <= 122) || // a-z
|
|
1204
|
+
(code >= 65 && code <= 90) || // A-Z
|
|
1205
|
+
(code >= 48 && code <= 57) || // 0-9
|
|
1206
|
+
code === 47 || // /
|
|
1207
|
+
code === 92 || // \
|
|
1208
|
+
code === 45 || // -
|
|
1209
|
+
code === 95 || // _
|
|
1210
|
+
code === 46 || // .
|
|
1211
|
+
code === 32 // space
|
|
1212
|
+
) {
|
|
1213
|
+
safe.push(path[i]);
|
|
1214
|
+
}
|
|
1215
|
+
else {
|
|
1216
|
+
throw createError(`Invalid character in path at position ${i}: "${path[i]}"`);
|
|
1217
|
+
}
|
|
1218
|
+
}
|
|
1219
|
+
return safe.join('');
|
|
1220
|
+
}
|
|
1221
|
+
/**
|
|
1222
|
+
* Maximum author length.
|
|
1223
|
+
*/
|
|
1224
|
+
const MAX_AUTHOR_LENGTH = 500;
|
|
1225
|
+
/**
|
|
1226
|
+
* Escapes an author string for safe use in git commands.
|
|
1227
|
+
* Format: "Name <email>"
|
|
1228
|
+
*
|
|
1229
|
+
* @param author - Author to escape
|
|
1230
|
+
* @returns Safe author string
|
|
1231
|
+
*/
|
|
1232
|
+
function escapeAuthor(author) {
|
|
1233
|
+
if (!author || typeof author !== 'string') {
|
|
1234
|
+
throw createError('Author is required');
|
|
1235
|
+
}
|
|
1236
|
+
if (author.length > MAX_AUTHOR_LENGTH) {
|
|
1237
|
+
throw createError(`Author exceeds maximum length of ${MAX_AUTHOR_LENGTH}`);
|
|
1238
|
+
}
|
|
1239
|
+
const safe = [];
|
|
1240
|
+
for (let i = 0; i < author.length; i++) {
|
|
1241
|
+
const code = author.charCodeAt(i);
|
|
1242
|
+
// Allow: a-z, A-Z, 0-9, space, @, ., -, _, <, >
|
|
1243
|
+
if ((code >= 97 && code <= 122) || // a-z
|
|
1244
|
+
(code >= 65 && code <= 90) || // A-Z
|
|
1245
|
+
(code >= 48 && code <= 57) || // 0-9
|
|
1246
|
+
code === 32 || // space
|
|
1247
|
+
code === 64 || // @
|
|
1248
|
+
code === 46 || // .
|
|
1249
|
+
code === 45 || // -
|
|
1250
|
+
code === 95 || // _
|
|
1251
|
+
code === 60 || // <
|
|
1252
|
+
code === 62 // >
|
|
1253
|
+
) {
|
|
1254
|
+
safe.push(author[i]);
|
|
1255
|
+
}
|
|
1256
|
+
else {
|
|
1257
|
+
throw createError(`Invalid character in author at position ${i}: "${author[i]}"`);
|
|
1258
|
+
}
|
|
1259
|
+
}
|
|
1260
|
+
return safe.join('');
|
|
1261
|
+
}
|
|
1262
|
+
|
|
1263
|
+
/**
|
|
1264
|
+
* Stages files for commit.
|
|
1265
|
+
*
|
|
1266
|
+
* @param files - Array of file paths relative to working directory
|
|
1267
|
+
* @param options - Configuration for the staging operation
|
|
1268
|
+
* @returns True if staging succeeded
|
|
1269
|
+
*
|
|
1270
|
+
* @example
|
|
1271
|
+
* stage(['package.json', 'CHANGELOG.md'])
|
|
1272
|
+
* stage(['.'], { all: true })
|
|
1273
|
+
*/
|
|
1274
|
+
function stage(files, options = {}) {
|
|
1275
|
+
const opts = { ...DEFAULT_COMMIT_OPTIONS, ...options };
|
|
1276
|
+
const args = ['add'];
|
|
1277
|
+
if (opts.all) {
|
|
1278
|
+
args.push('-A');
|
|
1279
|
+
}
|
|
1280
|
+
else if (opts.update) {
|
|
1281
|
+
args.push('-u');
|
|
1282
|
+
}
|
|
1283
|
+
if (opts.force) {
|
|
1284
|
+
args.push('-f');
|
|
1285
|
+
}
|
|
1286
|
+
// Add files
|
|
1287
|
+
for (const file of files) {
|
|
1288
|
+
args.push(escapeFilePath(file));
|
|
1289
|
+
}
|
|
1290
|
+
try {
|
|
1291
|
+
execSync(`git ${args.join(' ')}`, {
|
|
1292
|
+
encoding: 'utf-8',
|
|
1293
|
+
cwd: opts.cwd,
|
|
1294
|
+
timeout: opts.timeout,
|
|
1295
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
1296
|
+
});
|
|
1297
|
+
return true;
|
|
1298
|
+
}
|
|
1299
|
+
catch {
|
|
1300
|
+
return false;
|
|
1301
|
+
}
|
|
1302
|
+
}
|
|
1303
|
+
/**
|
|
1304
|
+
* Unstages files.
|
|
1305
|
+
*
|
|
1306
|
+
* @param files - Array of file paths to remove from staging area
|
|
1307
|
+
* @param options - Configuration for the unstage operation
|
|
1308
|
+
* @returns True if unstaging succeeded
|
|
1309
|
+
*
|
|
1310
|
+
* @example
|
|
1311
|
+
* unstage(['package.json'])
|
|
1312
|
+
*/
|
|
1313
|
+
function unstage(files, options = {}) {
|
|
1314
|
+
const opts = { ...DEFAULT_COMMIT_OPTIONS, ...options };
|
|
1315
|
+
const args = ['reset', 'HEAD', '--'];
|
|
1316
|
+
for (const file of files) {
|
|
1317
|
+
args.push(escapeFilePath(file));
|
|
1318
|
+
}
|
|
1319
|
+
try {
|
|
1320
|
+
execSync(`git ${args.join(' ')}`, {
|
|
1321
|
+
encoding: 'utf-8',
|
|
1322
|
+
cwd: opts.cwd,
|
|
1323
|
+
timeout: opts.timeout,
|
|
1324
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
1325
|
+
});
|
|
1326
|
+
return true;
|
|
1327
|
+
}
|
|
1328
|
+
catch {
|
|
1329
|
+
return false;
|
|
1330
|
+
}
|
|
1331
|
+
}
|
|
1332
|
+
/**
|
|
1333
|
+
* Stages all changes (tracked and untracked).
|
|
1334
|
+
*
|
|
1335
|
+
* @param options - Configuration for the staging operation
|
|
1336
|
+
* @returns True if all changes were successfully added to the index
|
|
1337
|
+
*
|
|
1338
|
+
* @example
|
|
1339
|
+
* stageAll() // stages all tracked and untracked changes
|
|
1340
|
+
*/
|
|
1341
|
+
function stageAll(options = {}) {
|
|
1342
|
+
return stage(['.'], { ...options, all: true });
|
|
1343
|
+
}
|
|
1344
|
+
/**
|
|
1345
|
+
* Checks if there are staged changes.
|
|
1346
|
+
*
|
|
1347
|
+
* @param options - Configuration for the operation
|
|
1348
|
+
* @returns True if there are staged changes ready to commit
|
|
1349
|
+
*
|
|
1350
|
+
* @example
|
|
1351
|
+
* if (hasStagedChanges()) { createCommit('...') }
|
|
1352
|
+
*/
|
|
1353
|
+
function hasStagedChanges(options = {}) {
|
|
1354
|
+
const opts = { ...DEFAULT_COMMIT_OPTIONS, ...options };
|
|
1355
|
+
try {
|
|
1356
|
+
execSync('git diff --cached --quiet', {
|
|
1357
|
+
encoding: 'utf-8',
|
|
1358
|
+
cwd: opts.cwd,
|
|
1359
|
+
timeout: opts.timeout,
|
|
1360
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
1361
|
+
});
|
|
1362
|
+
// Exit code 0 means no changes
|
|
1363
|
+
return false;
|
|
1364
|
+
}
|
|
1365
|
+
catch {
|
|
1366
|
+
// Exit code 1 means there are changes
|
|
1367
|
+
return true;
|
|
1368
|
+
}
|
|
1369
|
+
}
|
|
1370
|
+
/**
|
|
1371
|
+
* Checks if there are unstaged changes (working tree dirty).
|
|
1372
|
+
*
|
|
1373
|
+
* @param options - Configuration for the operation
|
|
1374
|
+
* @returns True if there are unstaged changes in the working tree
|
|
1375
|
+
*
|
|
1376
|
+
* @example
|
|
1377
|
+
* if (hasUnstagedChanges()) { stage(['.']) }
|
|
1378
|
+
*/
|
|
1379
|
+
function hasUnstagedChanges(options = {}) {
|
|
1380
|
+
const opts = { ...DEFAULT_COMMIT_OPTIONS, ...options };
|
|
1381
|
+
try {
|
|
1382
|
+
execSync('git diff --quiet', {
|
|
1383
|
+
encoding: 'utf-8',
|
|
1384
|
+
cwd: opts.cwd,
|
|
1385
|
+
timeout: opts.timeout,
|
|
1386
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
1387
|
+
});
|
|
1388
|
+
// Exit code 0 means no changes
|
|
1389
|
+
return false;
|
|
1390
|
+
}
|
|
1391
|
+
catch {
|
|
1392
|
+
// Exit code 1 means there are changes
|
|
1393
|
+
return true;
|
|
1394
|
+
}
|
|
1395
|
+
}
|
|
1396
|
+
|
|
1397
|
+
/**
|
|
1398
|
+
* Gets the current HEAD commit hash.
|
|
1399
|
+
*
|
|
1400
|
+
* @param options - Git operation configuration
|
|
1401
|
+
* @returns HEAD commit hash or null
|
|
1402
|
+
*
|
|
1403
|
+
* @example
|
|
1404
|
+
* const head = getHead()
|
|
1405
|
+
*/
|
|
1406
|
+
function getHead(options = {}) {
|
|
1407
|
+
const opts = { ...DEFAULT_COMMIT_OPTIONS, ...options };
|
|
1408
|
+
try {
|
|
1409
|
+
return execSync('git rev-parse HEAD', {
|
|
1410
|
+
encoding: 'utf-8',
|
|
1411
|
+
cwd: opts.cwd,
|
|
1412
|
+
timeout: opts.timeout,
|
|
1413
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
1414
|
+
}).trim();
|
|
1415
|
+
}
|
|
1416
|
+
catch {
|
|
1417
|
+
return null;
|
|
1418
|
+
}
|
|
1419
|
+
}
|
|
1420
|
+
/**
|
|
1421
|
+
* Gets the current branch name.
|
|
1422
|
+
*
|
|
1423
|
+
* @param options - Configuration for the operation
|
|
1424
|
+
* @returns Branch name or null if detached
|
|
1425
|
+
*
|
|
1426
|
+
* @example
|
|
1427
|
+
* const branch = getCurrentBranch()
|
|
1428
|
+
*/
|
|
1429
|
+
function getCurrentBranch(options = {}) {
|
|
1430
|
+
const opts = { ...DEFAULT_COMMIT_OPTIONS, ...options };
|
|
1431
|
+
try {
|
|
1432
|
+
const result = execSync('git symbolic-ref --short HEAD', {
|
|
1433
|
+
encoding: 'utf-8',
|
|
1434
|
+
cwd: opts.cwd,
|
|
1435
|
+
timeout: opts.timeout,
|
|
1436
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
1437
|
+
}).trim();
|
|
1438
|
+
return result || null;
|
|
1439
|
+
}
|
|
1440
|
+
catch {
|
|
1441
|
+
// Detached HEAD or not a git repo
|
|
1442
|
+
return null;
|
|
1443
|
+
}
|
|
1444
|
+
}
|
|
1445
|
+
/**
|
|
1446
|
+
* Checks if there are untracked files.
|
|
1447
|
+
*
|
|
1448
|
+
* @param options - Configuration for the operation
|
|
1449
|
+
* @returns True if there are untracked files in the working directory
|
|
1450
|
+
*/
|
|
1451
|
+
function hasUntrackedFiles(options = {}) {
|
|
1452
|
+
const opts = { ...DEFAULT_COMMIT_OPTIONS, ...options };
|
|
1453
|
+
try {
|
|
1454
|
+
const result = execSync('git ls-files --others --exclude-standard', {
|
|
1455
|
+
encoding: 'utf-8',
|
|
1456
|
+
cwd: opts.cwd,
|
|
1457
|
+
timeout: opts.timeout,
|
|
1458
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
1459
|
+
}).trim();
|
|
1460
|
+
return result.length > 0;
|
|
1461
|
+
}
|
|
1462
|
+
catch {
|
|
1463
|
+
return false;
|
|
1464
|
+
}
|
|
1465
|
+
}
|
|
1466
|
+
|
|
1467
|
+
/**
|
|
1468
|
+
* Default status options.
|
|
1469
|
+
*/
|
|
1470
|
+
const DEFAULT_STATUS_OPTIONS = {
|
|
1471
|
+
timeout: 10000,
|
|
1472
|
+
};
|
|
1473
|
+
/**
|
|
1474
|
+
* Gets the full repository status.
|
|
1475
|
+
*
|
|
1476
|
+
* @param options - Configuration for the status query
|
|
1477
|
+
* @returns Comprehensive repository status information
|
|
1478
|
+
*
|
|
1479
|
+
* @example
|
|
1480
|
+
* const status = getStatus()
|
|
1481
|
+
* if (!status.clean) {
|
|
1482
|
+
* console.log('Working tree has changes')
|
|
1483
|
+
* }
|
|
1484
|
+
*/
|
|
1485
|
+
function getStatus(options = {}) {
|
|
1486
|
+
const opts = { ...DEFAULT_STATUS_OPTIONS, ...options };
|
|
1487
|
+
// Get porcelain status with branch info
|
|
1488
|
+
const output = execSync('git status --porcelain=v2 --branch', {
|
|
1489
|
+
encoding: 'utf-8',
|
|
1490
|
+
cwd: opts.cwd,
|
|
1491
|
+
timeout: opts.timeout,
|
|
1492
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
1493
|
+
});
|
|
1494
|
+
return parseStatus(output);
|
|
1495
|
+
}
|
|
1496
|
+
/**
|
|
1497
|
+
* Parses git status porcelain v2 output.
|
|
1498
|
+
*
|
|
1499
|
+
* @param output - Raw status output
|
|
1500
|
+
* @returns Parsed status
|
|
1501
|
+
*/
|
|
1502
|
+
function parseStatus(output) {
|
|
1503
|
+
const lines = output.split('\n');
|
|
1504
|
+
let branch = null;
|
|
1505
|
+
let detached = false;
|
|
1506
|
+
let upstream;
|
|
1507
|
+
let ahead = 0;
|
|
1508
|
+
let behind = 0;
|
|
1509
|
+
const staged = [];
|
|
1510
|
+
const modified = [];
|
|
1511
|
+
const untracked = [];
|
|
1512
|
+
let hasConflicts = false;
|
|
1513
|
+
for (const line of lines) {
|
|
1514
|
+
if (!line)
|
|
1515
|
+
continue;
|
|
1516
|
+
// Branch headers
|
|
1517
|
+
if (startsWithPrefix(line, '# branch.head ')) {
|
|
1518
|
+
const branchName = line.slice(14);
|
|
1519
|
+
if (branchName === '(detached)') {
|
|
1520
|
+
detached = true;
|
|
1521
|
+
}
|
|
1522
|
+
else {
|
|
1523
|
+
branch = branchName;
|
|
1524
|
+
}
|
|
1525
|
+
}
|
|
1526
|
+
else if (startsWithPrefix(line, '# branch.upstream ')) {
|
|
1527
|
+
upstream = line.slice(18);
|
|
1528
|
+
}
|
|
1529
|
+
else if (startsWithPrefix(line, '# branch.ab ')) {
|
|
1530
|
+
const ab = parseAheadBehind(line.slice(12));
|
|
1531
|
+
ahead = ab.ahead;
|
|
1532
|
+
behind = ab.behind;
|
|
1533
|
+
}
|
|
1534
|
+
// Changed entries (ordinary changed)
|
|
1535
|
+
else if (line[0] === '1') {
|
|
1536
|
+
const entry = parseChangedEntry(line);
|
|
1537
|
+
if (entry) {
|
|
1538
|
+
if (entry.indexStatus) {
|
|
1539
|
+
staged.push(entry);
|
|
1540
|
+
}
|
|
1541
|
+
if (entry.workTreeStatus && entry.workTreeStatus !== 'untracked') {
|
|
1542
|
+
modified.push(entry);
|
|
1543
|
+
}
|
|
1544
|
+
}
|
|
1545
|
+
}
|
|
1546
|
+
// Renamed/copied entries
|
|
1547
|
+
else if (line[0] === '2') {
|
|
1548
|
+
const entry = parseRenamedEntry(line);
|
|
1549
|
+
if (entry) {
|
|
1550
|
+
if (entry.indexStatus) {
|
|
1551
|
+
staged.push(entry);
|
|
1552
|
+
}
|
|
1553
|
+
if (entry.workTreeStatus) {
|
|
1554
|
+
modified.push(entry);
|
|
1555
|
+
}
|
|
1556
|
+
}
|
|
1557
|
+
}
|
|
1558
|
+
// Unmerged entries
|
|
1559
|
+
else if (line[0] === 'u') {
|
|
1560
|
+
hasConflicts = true;
|
|
1561
|
+
const entry = parseUnmergedEntry(line);
|
|
1562
|
+
if (entry) {
|
|
1563
|
+
staged.push(entry);
|
|
1564
|
+
}
|
|
1565
|
+
}
|
|
1566
|
+
// Untracked entries
|
|
1567
|
+
else if (line[0] === '?') {
|
|
1568
|
+
const path = line.slice(2);
|
|
1569
|
+
untracked.push(path);
|
|
1570
|
+
}
|
|
1571
|
+
}
|
|
1572
|
+
const clean = staged.length === 0 && modified.length === 0 && untracked.length === 0 && !hasConflicts;
|
|
1573
|
+
return {
|
|
1574
|
+
branch,
|
|
1575
|
+
detached,
|
|
1576
|
+
upstream,
|
|
1577
|
+
ahead,
|
|
1578
|
+
behind,
|
|
1579
|
+
staged,
|
|
1580
|
+
modified,
|
|
1581
|
+
untracked,
|
|
1582
|
+
clean,
|
|
1583
|
+
hasConflicts,
|
|
1584
|
+
};
|
|
1585
|
+
}
|
|
1586
|
+
/**
|
|
1587
|
+
* Parses ahead/behind string.
|
|
1588
|
+
*
|
|
1589
|
+
* @param str - String like "+5 -2"
|
|
1590
|
+
* @returns Parsed values
|
|
1591
|
+
*/
|
|
1592
|
+
function parseAheadBehind(str) {
|
|
1593
|
+
let ahead = 0;
|
|
1594
|
+
let behind = 0;
|
|
1595
|
+
const parts = str.split(' ');
|
|
1596
|
+
for (const part of parts) {
|
|
1597
|
+
if (part[0] === '+') {
|
|
1598
|
+
ahead = parseInt(part.slice(1), 10) || 0;
|
|
1599
|
+
}
|
|
1600
|
+
else if (part[0] === '-') {
|
|
1601
|
+
behind = parseInt(part.slice(1), 10) || 0;
|
|
1602
|
+
}
|
|
1603
|
+
}
|
|
1604
|
+
return { ahead, behind };
|
|
1605
|
+
}
|
|
1606
|
+
/**
|
|
1607
|
+
* Parses a changed entry line.
|
|
1608
|
+
*
|
|
1609
|
+
* @param line - Status line starting with '1'
|
|
1610
|
+
* @returns Parsed entry or null
|
|
1611
|
+
*/
|
|
1612
|
+
function parseChangedEntry(line) {
|
|
1613
|
+
// Format: 1 <XY> <sub> <mH> <mI> <mW> <hH> <hI> <path>
|
|
1614
|
+
const parts = line.split(' ');
|
|
1615
|
+
if (parts.length < 9)
|
|
1616
|
+
return null;
|
|
1617
|
+
const xy = parts[1];
|
|
1618
|
+
const path = parts.slice(8).join(' ');
|
|
1619
|
+
const indexStatus = statusFromChar(xy[0]);
|
|
1620
|
+
const workTreeStatus = statusFromChar(xy[1]);
|
|
1621
|
+
return {
|
|
1622
|
+
path,
|
|
1623
|
+
indexStatus,
|
|
1624
|
+
workTreeStatus,
|
|
1625
|
+
};
|
|
1626
|
+
}
|
|
1627
|
+
/**
|
|
1628
|
+
* Parses a renamed entry line.
|
|
1629
|
+
*
|
|
1630
|
+
* @param line - Status line starting with '2'
|
|
1631
|
+
* @returns Parsed entry or null
|
|
1632
|
+
*/
|
|
1633
|
+
function parseRenamedEntry(line) {
|
|
1634
|
+
// Format: 2 <XY> <sub> <mH> <mI> <mW> <hH> <hI> <X><score> <path><tab><origPath>
|
|
1635
|
+
const parts = line.split(' ');
|
|
1636
|
+
if (parts.length < 10)
|
|
1637
|
+
return null;
|
|
1638
|
+
const xy = parts[1];
|
|
1639
|
+
const pathPart = parts.slice(9).join(' ');
|
|
1640
|
+
// Split by tab
|
|
1641
|
+
const tabIndex = pathPart.indexOf('\t');
|
|
1642
|
+
const path = tabIndex >= 0 ? pathPart.slice(0, tabIndex) : pathPart;
|
|
1643
|
+
const origPath = tabIndex >= 0 ? pathPart.slice(tabIndex + 1) : undefined;
|
|
1644
|
+
const indexStatus = statusFromChar(xy[0]);
|
|
1645
|
+
const workTreeStatus = statusFromChar(xy[1]);
|
|
1646
|
+
return {
|
|
1647
|
+
path,
|
|
1648
|
+
indexStatus,
|
|
1649
|
+
workTreeStatus,
|
|
1650
|
+
origPath,
|
|
1651
|
+
};
|
|
1652
|
+
}
|
|
1653
|
+
/**
|
|
1654
|
+
* Parses an unmerged entry line.
|
|
1655
|
+
*
|
|
1656
|
+
* @param line - Status line starting with 'u'
|
|
1657
|
+
* @returns Parsed entry or null
|
|
1658
|
+
*/
|
|
1659
|
+
function parseUnmergedEntry(line) {
|
|
1660
|
+
// Format: u <XY> <sub> <m1> <m2> <m3> <mW> <h1> <h2> <h3> <path>
|
|
1661
|
+
const parts = line.split(' ');
|
|
1662
|
+
if (parts.length < 11)
|
|
1663
|
+
return null;
|
|
1664
|
+
const path = parts.slice(10).join(' ');
|
|
1665
|
+
return {
|
|
1666
|
+
path,
|
|
1667
|
+
indexStatus: 'unmerged',
|
|
1668
|
+
workTreeStatus: 'unmerged',
|
|
1669
|
+
};
|
|
1670
|
+
}
|
|
1671
|
+
/**
|
|
1672
|
+
* Converts status character to FileStatus.
|
|
1673
|
+
*
|
|
1674
|
+
* @param char - Status character
|
|
1675
|
+
* @returns FileStatus or null
|
|
1676
|
+
*/
|
|
1677
|
+
function statusFromChar(char) {
|
|
1678
|
+
switch (char) {
|
|
1679
|
+
case 'M':
|
|
1680
|
+
return 'modified';
|
|
1681
|
+
case 'T':
|
|
1682
|
+
return 'modified'; // Type change
|
|
1683
|
+
case 'A':
|
|
1684
|
+
return 'added';
|
|
1685
|
+
case 'D':
|
|
1686
|
+
return 'deleted';
|
|
1687
|
+
case 'R':
|
|
1688
|
+
return 'renamed';
|
|
1689
|
+
case 'C':
|
|
1690
|
+
return 'copied';
|
|
1691
|
+
case 'U':
|
|
1692
|
+
return 'unmerged';
|
|
1693
|
+
case '?':
|
|
1694
|
+
return 'untracked';
|
|
1695
|
+
case '!':
|
|
1696
|
+
return 'ignored';
|
|
1697
|
+
case '.':
|
|
1698
|
+
return null;
|
|
1699
|
+
default:
|
|
1700
|
+
return null;
|
|
1701
|
+
}
|
|
1702
|
+
}
|
|
1703
|
+
/**
|
|
1704
|
+
* Checks if string starts with prefix (no regex).
|
|
1705
|
+
*
|
|
1706
|
+
* @param str - The string to check
|
|
1707
|
+
* @param prefix - The prefix to look for
|
|
1708
|
+
* @returns True if str starts with the given prefix
|
|
1709
|
+
*/
|
|
1710
|
+
function startsWithPrefix(str, prefix) {
|
|
1711
|
+
if (prefix.length > str.length)
|
|
1712
|
+
return false;
|
|
1713
|
+
for (let i = 0; i < prefix.length; i++) {
|
|
1714
|
+
if (str[i] !== prefix[i])
|
|
1715
|
+
return false;
|
|
1716
|
+
}
|
|
1717
|
+
return true;
|
|
1718
|
+
}
|
|
1719
|
+
/**
|
|
1720
|
+
* Checks if the working tree is clean (no changes).
|
|
1721
|
+
*
|
|
1722
|
+
* @param options - Configuration for the status check
|
|
1723
|
+
* @returns True if working tree is clean with no uncommitted changes
|
|
1724
|
+
*
|
|
1725
|
+
* @example
|
|
1726
|
+
* if (!isClean()) {
|
|
1727
|
+
* throw new Error('Working tree has changes')
|
|
1728
|
+
* }
|
|
1729
|
+
*/
|
|
1730
|
+
function isClean(options = {}) {
|
|
1731
|
+
const status = getStatus(options);
|
|
1732
|
+
return status.clean;
|
|
1733
|
+
}
|
|
1734
|
+
/**
|
|
1735
|
+
* Checks if the directory is a git repository.
|
|
1736
|
+
*
|
|
1737
|
+
* @param options - Status options
|
|
1738
|
+
* @returns True if in a git repository
|
|
1739
|
+
*
|
|
1740
|
+
* @example
|
|
1741
|
+
* if (!isGitRepository()) {
|
|
1742
|
+
* throw new Error('Not a git repository')
|
|
1743
|
+
* }
|
|
1744
|
+
*/
|
|
1745
|
+
function isGitRepository(options = {}) {
|
|
1746
|
+
const opts = { ...DEFAULT_STATUS_OPTIONS, ...options };
|
|
1747
|
+
try {
|
|
1748
|
+
execSync('git rev-parse --is-inside-work-tree', {
|
|
1749
|
+
encoding: 'utf-8',
|
|
1750
|
+
cwd: opts.cwd,
|
|
1751
|
+
timeout: opts.timeout,
|
|
1752
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
1753
|
+
});
|
|
1754
|
+
return true;
|
|
1755
|
+
}
|
|
1756
|
+
catch {
|
|
1757
|
+
return false;
|
|
1758
|
+
}
|
|
1759
|
+
}
|
|
1760
|
+
/**
|
|
1761
|
+
* Gets the repository root directory.
|
|
1762
|
+
*
|
|
1763
|
+
* @param options - Status options
|
|
1764
|
+
* @returns Root directory path or null
|
|
1765
|
+
*
|
|
1766
|
+
* @example
|
|
1767
|
+
* const root = getRepositoryRoot()
|
|
1768
|
+
*/
|
|
1769
|
+
function getRepositoryRoot(options = {}) {
|
|
1770
|
+
const opts = { ...DEFAULT_STATUS_OPTIONS, ...options };
|
|
1771
|
+
try {
|
|
1772
|
+
return execSync('git rev-parse --show-toplevel', {
|
|
1773
|
+
encoding: 'utf-8',
|
|
1774
|
+
cwd: opts.cwd,
|
|
1775
|
+
timeout: opts.timeout,
|
|
1776
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
1777
|
+
}).trim();
|
|
1778
|
+
}
|
|
1779
|
+
catch {
|
|
1780
|
+
return null;
|
|
1781
|
+
}
|
|
1782
|
+
}
|
|
1783
|
+
/**
|
|
1784
|
+
* Gets the current commit hash (HEAD).
|
|
1785
|
+
*
|
|
1786
|
+
* @param options - Status options
|
|
1787
|
+
* @returns Commit hash or null
|
|
1788
|
+
*/
|
|
1789
|
+
function getHeadHash(options = {}) {
|
|
1790
|
+
const opts = { ...DEFAULT_STATUS_OPTIONS, ...options };
|
|
1791
|
+
try {
|
|
1792
|
+
return execSync('git rev-parse HEAD', {
|
|
1793
|
+
encoding: 'utf-8',
|
|
1794
|
+
cwd: opts.cwd,
|
|
1795
|
+
timeout: opts.timeout,
|
|
1796
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
1797
|
+
}).trim();
|
|
1798
|
+
}
|
|
1799
|
+
catch {
|
|
1800
|
+
return null;
|
|
1801
|
+
}
|
|
1802
|
+
}
|
|
1803
|
+
/**
|
|
1804
|
+
* Gets the short current commit hash.
|
|
1805
|
+
*
|
|
1806
|
+
* @param options - Status options
|
|
1807
|
+
* @returns Short hash or null
|
|
1808
|
+
*/
|
|
1809
|
+
function getHeadShortHash(options = {}) {
|
|
1810
|
+
const opts = { ...DEFAULT_STATUS_OPTIONS, ...options };
|
|
1811
|
+
try {
|
|
1812
|
+
return execSync('git rev-parse --short HEAD', {
|
|
1813
|
+
encoding: 'utf-8',
|
|
1814
|
+
cwd: opts.cwd,
|
|
1815
|
+
timeout: opts.timeout,
|
|
1816
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
1817
|
+
}).trim();
|
|
1818
|
+
}
|
|
1819
|
+
catch {
|
|
1820
|
+
return null;
|
|
1821
|
+
}
|
|
1822
|
+
}
|
|
1823
|
+
/**
|
|
1824
|
+
* Checks if there are merge conflicts.
|
|
1825
|
+
*
|
|
1826
|
+
* @param options - Status options
|
|
1827
|
+
* @returns True if there are conflicts
|
|
1828
|
+
*/
|
|
1829
|
+
function hasConflicts(options = {}) {
|
|
1830
|
+
const status = getStatus(options);
|
|
1831
|
+
return status.hasConflicts;
|
|
1832
|
+
}
|
|
1833
|
+
/**
|
|
1834
|
+
* Gets the number of commits ahead of upstream.
|
|
1835
|
+
*
|
|
1836
|
+
* @param options - Status options
|
|
1837
|
+
* @returns Number of commits ahead
|
|
1838
|
+
*/
|
|
1839
|
+
function getAheadCount(options = {}) {
|
|
1840
|
+
const status = getStatus(options);
|
|
1841
|
+
return status.ahead;
|
|
1842
|
+
}
|
|
1843
|
+
/**
|
|
1844
|
+
* Gets the number of commits behind upstream.
|
|
1845
|
+
*
|
|
1846
|
+
* @param options - Status options
|
|
1847
|
+
* @returns Number of commits behind
|
|
1848
|
+
*/
|
|
1849
|
+
function getBehindCount(options = {}) {
|
|
1850
|
+
const status = getStatus(options);
|
|
1851
|
+
return status.behind;
|
|
1852
|
+
}
|
|
1853
|
+
/**
|
|
1854
|
+
* Checks if the repository needs to be pushed.
|
|
1855
|
+
*
|
|
1856
|
+
* @param options - Status options
|
|
1857
|
+
* @returns True if there are unpushed commits
|
|
1858
|
+
*/
|
|
1859
|
+
function needsPush(options = {}) {
|
|
1860
|
+
return getAheadCount(options) > 0;
|
|
1861
|
+
}
|
|
1862
|
+
/**
|
|
1863
|
+
* Checks if the repository needs to be pulled.
|
|
1864
|
+
*
|
|
1865
|
+
* @param options - Status options
|
|
1866
|
+
* @returns True if there are commits to pull
|
|
1867
|
+
*/
|
|
1868
|
+
function needsPull(options = {}) {
|
|
1869
|
+
return getBehindCount(options) > 0;
|
|
1870
|
+
}
|
|
1871
|
+
/**
|
|
1872
|
+
* Gets list of staged file paths.
|
|
1873
|
+
*
|
|
1874
|
+
* @param options - Status options
|
|
1875
|
+
* @returns Array of staged file paths
|
|
1876
|
+
*/
|
|
1877
|
+
function getStagedFiles(options = {}) {
|
|
1878
|
+
const status = getStatus(options);
|
|
1879
|
+
return status.staged.map((e) => e.path);
|
|
1880
|
+
}
|
|
1881
|
+
/**
|
|
1882
|
+
* Gets list of modified file paths (unstaged).
|
|
1883
|
+
*
|
|
1884
|
+
* @param options - Status options
|
|
1885
|
+
* @returns Array of modified file paths
|
|
1886
|
+
*/
|
|
1887
|
+
function getModifiedFiles(options = {}) {
|
|
1888
|
+
const status = getStatus(options);
|
|
1889
|
+
return status.modified.map((e) => e.path);
|
|
1890
|
+
}
|
|
1891
|
+
/**
|
|
1892
|
+
* Gets list of untracked file paths.
|
|
1893
|
+
*
|
|
1894
|
+
* @param options - Status options
|
|
1895
|
+
* @returns Array of untracked file paths
|
|
1896
|
+
*/
|
|
1897
|
+
function getUntrackedFiles(options = {}) {
|
|
1898
|
+
const status = getStatus(options);
|
|
1899
|
+
return status.untracked;
|
|
1900
|
+
}
|
|
1901
|
+
|
|
1902
|
+
export { DEFAULT_COMMIT_OPTIONS, DEFAULT_LOG_OPTIONS, DEFAULT_STATUS_OPTIONS, DEFAULT_TAG_OPTIONS, amendCommit, amendCommitNoEdit, commit, commitExists, createEmptyCommit, createTag, deleteTag, escapeAuthor, escapeFilePath, escapeGitArg, escapeGitMessage, escapeGitPath, escapeGitRef, escapeGitTagPattern, getAheadCount, getBehindCount, getCommit, getCommitLog, getCommitsBetween, getCommitsSince, getCurrentBranch, getHead, getHeadHash, getHeadShortHash, getLatestTag, getModifiedFiles, getRepositoryRoot, getStagedFiles, getStatus, getTag, getTags, getTagsForPackage, getUntrackedFiles, hasConflicts, hasStagedChanges, hasUnstagedChanges, hasUntrackedFiles, isClean, isGitRepository, needsPull, needsPush, pushTag, stage, stageAll, tagExists, unstage };
|
|
1903
|
+
//# sourceMappingURL=index.esm.js.map
|