@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
package/git/index.cjs.js
ADDED
|
@@ -0,0 +1,2863 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var node_child_process = require('node:child_process');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Creates a git commit model.
|
|
7
|
+
*
|
|
8
|
+
* @param options - Commit creation options
|
|
9
|
+
* @returns A new GitCommit object
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* const commit = createGitCommit({
|
|
13
|
+
* hash: 'abc123...',
|
|
14
|
+
* authorName: 'John Doe',
|
|
15
|
+
* authorEmail: 'john@example.com',
|
|
16
|
+
* authorDate: '2026-03-12T10:00:00Z',
|
|
17
|
+
* subject: 'feat: add new feature',
|
|
18
|
+
* })
|
|
19
|
+
*/
|
|
20
|
+
function createGitCommit(options) {
|
|
21
|
+
const body = options.body ?? '';
|
|
22
|
+
const subject = options.subject;
|
|
23
|
+
return {
|
|
24
|
+
hash: options.hash,
|
|
25
|
+
shortHash: getShortHash(options.hash),
|
|
26
|
+
authorName: options.authorName,
|
|
27
|
+
authorEmail: options.authorEmail,
|
|
28
|
+
authorDate: options.authorDate,
|
|
29
|
+
committerName: options.committerName ?? options.authorName,
|
|
30
|
+
committerEmail: options.committerEmail ?? options.authorEmail,
|
|
31
|
+
commitDate: options.commitDate ?? options.authorDate,
|
|
32
|
+
subject,
|
|
33
|
+
body,
|
|
34
|
+
message: body ? `${subject}\n\n${body}` : subject,
|
|
35
|
+
parents: options.parents ?? [],
|
|
36
|
+
refs: options.refs ?? [],
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Gets a short hash (7 characters) from a full commit hash.
|
|
41
|
+
*
|
|
42
|
+
* @param hash - Full commit hash
|
|
43
|
+
* @returns Short hash (7 characters)
|
|
44
|
+
*/
|
|
45
|
+
function getShortHash(hash) {
|
|
46
|
+
return hash.slice(0, 7);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Checks if two commits are the same based on their hash.
|
|
50
|
+
*
|
|
51
|
+
* @param a - First commit
|
|
52
|
+
* @param b - Second commit
|
|
53
|
+
* @returns True if commits have the same hash
|
|
54
|
+
*/
|
|
55
|
+
function isSameCommit(a, b) {
|
|
56
|
+
return a.hash === b.hash;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Checks if a commit is a merge commit (has multiple parents).
|
|
60
|
+
*
|
|
61
|
+
* @param commit - Commit to check
|
|
62
|
+
* @returns True if commit has more than one parent
|
|
63
|
+
*/
|
|
64
|
+
function isMergeCommit(commit) {
|
|
65
|
+
return commit.parents.length > 1;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Checks if a commit is a root commit (has no parents).
|
|
69
|
+
*
|
|
70
|
+
* @param commit - Commit to check
|
|
71
|
+
* @returns True if commit has no parents
|
|
72
|
+
*/
|
|
73
|
+
function isRootCommit(commit) {
|
|
74
|
+
return commit.parents.length === 0;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Extracts the scope from a commit subject if it follows conventional commit format.
|
|
78
|
+
* Uses character-by-character parsing (no regex).
|
|
79
|
+
*
|
|
80
|
+
* @param subject - Commit subject line
|
|
81
|
+
* @returns Scope string or undefined if no scope found
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* extractScope('feat(lib-versioning): add git support') // 'lib-versioning'
|
|
85
|
+
* extractScope('fix: resolve issue') // undefined
|
|
86
|
+
*/
|
|
87
|
+
function extractScope(subject) {
|
|
88
|
+
// Look for pattern: type(scope): or type(scope)!:
|
|
89
|
+
let i = 0;
|
|
90
|
+
// Skip type characters (a-z)
|
|
91
|
+
while (i < subject.length) {
|
|
92
|
+
const code = subject.charCodeAt(i);
|
|
93
|
+
if (code >= 97 && code <= 122) {
|
|
94
|
+
// a-z
|
|
95
|
+
i++;
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
// Must find opening parenthesis
|
|
102
|
+
if (i >= subject.length || subject[i] !== '(') {
|
|
103
|
+
return undefined;
|
|
104
|
+
}
|
|
105
|
+
// Skip '('
|
|
106
|
+
i++;
|
|
107
|
+
const scopeStart = i;
|
|
108
|
+
// Find closing parenthesis
|
|
109
|
+
while (i < subject.length && subject[i] !== ')') {
|
|
110
|
+
i++;
|
|
111
|
+
}
|
|
112
|
+
if (i >= subject.length) {
|
|
113
|
+
return undefined;
|
|
114
|
+
}
|
|
115
|
+
const scope = subject.slice(scopeStart, i);
|
|
116
|
+
return scope || undefined;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Extracts the type from a commit subject if it follows conventional commit format.
|
|
120
|
+
* Uses character-by-character parsing (no regex).
|
|
121
|
+
*
|
|
122
|
+
* @param subject - Commit subject line
|
|
123
|
+
* @returns Type string or undefined if no valid type found
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* extractType('feat(lib-versioning): add git support') // 'feat'
|
|
127
|
+
* extractType('fix: resolve issue') // 'fix'
|
|
128
|
+
* extractType('random message') // undefined
|
|
129
|
+
*/
|
|
130
|
+
function extractType(subject) {
|
|
131
|
+
let i = 0;
|
|
132
|
+
// Collect type characters (a-z)
|
|
133
|
+
while (i < subject.length) {
|
|
134
|
+
const code = subject.charCodeAt(i);
|
|
135
|
+
if (code >= 97 && code <= 122) {
|
|
136
|
+
// a-z
|
|
137
|
+
i++;
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
break;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
if (i === 0) {
|
|
144
|
+
return undefined;
|
|
145
|
+
}
|
|
146
|
+
const type = subject.slice(0, i);
|
|
147
|
+
// Next character must be '(' , '!' or ':'
|
|
148
|
+
if (i >= subject.length) {
|
|
149
|
+
return undefined;
|
|
150
|
+
}
|
|
151
|
+
const next = subject[i];
|
|
152
|
+
if (next === '(' || next === ':' || next === '!') {
|
|
153
|
+
return type;
|
|
154
|
+
}
|
|
155
|
+
return undefined;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Safe copies of Math built-in methods.
|
|
160
|
+
*
|
|
161
|
+
* These references are captured at module initialization time to protect against
|
|
162
|
+
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
163
|
+
*
|
|
164
|
+
* @module @hyperfrontend/immutable-api-utils/built-in-copy/math
|
|
165
|
+
*/
|
|
166
|
+
// Capture references at module initialization time
|
|
167
|
+
const _Math = globalThis.Math;
|
|
168
|
+
// ============================================================================
|
|
169
|
+
// Min/Max
|
|
170
|
+
// ============================================================================
|
|
171
|
+
/**
|
|
172
|
+
* (Safe copy) Returns the larger of zero or more numbers.
|
|
173
|
+
*/
|
|
174
|
+
const max = _Math.max;
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Creates a lightweight git tag.
|
|
178
|
+
*
|
|
179
|
+
* @param options - Tag creation options
|
|
180
|
+
* @returns A new GitTag object
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* const tag = createLightweightTag({
|
|
184
|
+
* name: 'v1.0.0',
|
|
185
|
+
* commitHash: 'abc123...',
|
|
186
|
+
* })
|
|
187
|
+
*/
|
|
188
|
+
function createLightweightTag(options) {
|
|
189
|
+
return {
|
|
190
|
+
name: options.name,
|
|
191
|
+
commitHash: options.commitHash,
|
|
192
|
+
type: 'lightweight',
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Creates an annotated git tag.
|
|
197
|
+
*
|
|
198
|
+
* @param options - Tag creation options
|
|
199
|
+
* @returns A new GitTag object
|
|
200
|
+
*
|
|
201
|
+
* @example
|
|
202
|
+
* const tag = createAnnotatedTag({
|
|
203
|
+
* name: 'v1.0.0',
|
|
204
|
+
* commitHash: 'abc123...',
|
|
205
|
+
* message: 'Release v1.0.0',
|
|
206
|
+
* taggerName: 'John Doe',
|
|
207
|
+
* taggerEmail: 'john@example.com',
|
|
208
|
+
* tagDate: '2026-03-12T10:00:00Z',
|
|
209
|
+
* })
|
|
210
|
+
*/
|
|
211
|
+
function createAnnotatedTag(options) {
|
|
212
|
+
return {
|
|
213
|
+
name: options.name,
|
|
214
|
+
commitHash: options.commitHash,
|
|
215
|
+
type: 'annotated',
|
|
216
|
+
message: options.message,
|
|
217
|
+
taggerName: options.taggerName,
|
|
218
|
+
taggerEmail: options.taggerEmail,
|
|
219
|
+
tagDate: options.tagDate,
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Checks if a tag is annotated.
|
|
224
|
+
*
|
|
225
|
+
* @param tag - Tag to check
|
|
226
|
+
* @returns True if tag is annotated
|
|
227
|
+
*/
|
|
228
|
+
function isAnnotatedTag(tag) {
|
|
229
|
+
return tag.type === 'annotated';
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Checks if a tag is lightweight.
|
|
233
|
+
*
|
|
234
|
+
* @param tag - Tag to check
|
|
235
|
+
* @returns True if tag is lightweight
|
|
236
|
+
*/
|
|
237
|
+
function isLightweightTag(tag) {
|
|
238
|
+
return tag.type === 'lightweight';
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Extracts version from tag name.
|
|
242
|
+
* Handles common formats: v1.2.3, `@scope/package@1.2.3`, package@1.2.3
|
|
243
|
+
* Uses character-by-character parsing (no regex).
|
|
244
|
+
*
|
|
245
|
+
* @param tagName - Tag name to parse
|
|
246
|
+
* @returns The extracted version string or undefined if no version found
|
|
247
|
+
*
|
|
248
|
+
* @example
|
|
249
|
+
* extractVersionFromTag('v1.2.3') // '1.2.3'
|
|
250
|
+
* extractVersionFromTag('@scope/pkg@1.2.3') // '1.2.3'
|
|
251
|
+
* extractVersionFromTag('release-1.2.3') // '1.2.3'
|
|
252
|
+
*/
|
|
253
|
+
function extractVersionFromTag(tagName) {
|
|
254
|
+
// Strategy: Find last occurrence of '@' followed by version, or 'v' followed by version
|
|
255
|
+
// Version starts with digit and contains digits, dots, and possibly prerelease identifiers
|
|
256
|
+
// First, try to find @version pattern (for scoped packages)
|
|
257
|
+
let i = tagName.length - 1;
|
|
258
|
+
// Find last '@'
|
|
259
|
+
while (i >= 0 && tagName[i] !== '@') {
|
|
260
|
+
i--;
|
|
261
|
+
}
|
|
262
|
+
if (i >= 0) {
|
|
263
|
+
// Found '@', check if followed by version
|
|
264
|
+
const afterAt = tagName.slice(i + 1);
|
|
265
|
+
const version = parseVersionPart(afterAt);
|
|
266
|
+
if (version) {
|
|
267
|
+
return version;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
// Try to find 'v' followed by version
|
|
271
|
+
i = 0;
|
|
272
|
+
while (i < tagName.length) {
|
|
273
|
+
if ((tagName[i] === 'v' || tagName[i] === 'V') && i + 1 < tagName.length) {
|
|
274
|
+
const nextCode = tagName.charCodeAt(i + 1);
|
|
275
|
+
if (nextCode >= 48 && nextCode <= 57) {
|
|
276
|
+
// 0-9
|
|
277
|
+
const version = parseVersionPart(tagName.slice(i + 1));
|
|
278
|
+
if (version) {
|
|
279
|
+
return version;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
i++;
|
|
284
|
+
}
|
|
285
|
+
// Try to find digit sequence after common separators (-, _)
|
|
286
|
+
i = 0;
|
|
287
|
+
while (i < tagName.length) {
|
|
288
|
+
const char = tagName[i];
|
|
289
|
+
if (char === '-' || char === '_') {
|
|
290
|
+
const afterSep = tagName.slice(i + 1);
|
|
291
|
+
const code = afterSep.charCodeAt(0);
|
|
292
|
+
if (code >= 48 && code <= 57) {
|
|
293
|
+
// 0-9
|
|
294
|
+
const version = parseVersionPart(afterSep);
|
|
295
|
+
if (version) {
|
|
296
|
+
return version;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
i++;
|
|
301
|
+
}
|
|
302
|
+
// Last resort: if entire string is a version
|
|
303
|
+
return parseVersionPart(tagName);
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Parses a version-like string from the start of input.
|
|
307
|
+
* Uses character-by-character parsing (no regex).
|
|
308
|
+
*
|
|
309
|
+
* @param input - String to parse
|
|
310
|
+
* @returns Version string or undefined
|
|
311
|
+
*/
|
|
312
|
+
function parseVersionPart(input) {
|
|
313
|
+
if (!input)
|
|
314
|
+
return undefined;
|
|
315
|
+
// Must start with digit
|
|
316
|
+
const firstCode = input.charCodeAt(0);
|
|
317
|
+
if (firstCode < 48 || firstCode > 57) {
|
|
318
|
+
return undefined;
|
|
319
|
+
}
|
|
320
|
+
// Collect version characters: digits, dots, hyphens, plus, letters (for prerelease)
|
|
321
|
+
let i = 0;
|
|
322
|
+
let dotCount = 0;
|
|
323
|
+
while (i < input.length) {
|
|
324
|
+
const code = input.charCodeAt(i);
|
|
325
|
+
if (code >= 48 && code <= 57) {
|
|
326
|
+
// 0-9
|
|
327
|
+
i++;
|
|
328
|
+
}
|
|
329
|
+
else if (code === 46) {
|
|
330
|
+
// .
|
|
331
|
+
dotCount++;
|
|
332
|
+
i++;
|
|
333
|
+
}
|
|
334
|
+
else if (code === 45) {
|
|
335
|
+
// -
|
|
336
|
+
i++;
|
|
337
|
+
}
|
|
338
|
+
else if (code === 43) {
|
|
339
|
+
// +
|
|
340
|
+
i++;
|
|
341
|
+
}
|
|
342
|
+
else if ((code >= 97 && code <= 122) || (code >= 65 && code <= 90)) {
|
|
343
|
+
// a-z, A-Z
|
|
344
|
+
i++;
|
|
345
|
+
}
|
|
346
|
+
else {
|
|
347
|
+
break;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
// Must have at least one dot (e.g., 1.0)
|
|
351
|
+
if (dotCount === 0) {
|
|
352
|
+
return undefined;
|
|
353
|
+
}
|
|
354
|
+
const version = input.slice(0, i);
|
|
355
|
+
// Basic validation: must contain at least two parts separated by dots
|
|
356
|
+
if (!version.includes('.')) {
|
|
357
|
+
return undefined;
|
|
358
|
+
}
|
|
359
|
+
return version;
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* Extracts package name from tag name.
|
|
363
|
+
* Handles formats like: `@scope/package@1.2.3`, package@1.2.3, package-v1.2.3
|
|
364
|
+
* Uses character-by-character parsing (no regex).
|
|
365
|
+
*
|
|
366
|
+
* @param tagName - Tag name to parse
|
|
367
|
+
* @returns The extracted package name or undefined if not found
|
|
368
|
+
*
|
|
369
|
+
* @example
|
|
370
|
+
* extractPackageFromTag('@scope/pkg@1.2.3') // '@scope/pkg'
|
|
371
|
+
* extractPackageFromTag('lib-utils@1.2.3') // 'lib-utils'
|
|
372
|
+
* extractPackageFromTag('v1.2.3') // undefined
|
|
373
|
+
*/
|
|
374
|
+
function extractPackageFromTag(tagName) {
|
|
375
|
+
// Find the last '@' that's followed by a version number
|
|
376
|
+
let lastVersionAt = -1;
|
|
377
|
+
let i = tagName.length - 1;
|
|
378
|
+
while (i >= 0) {
|
|
379
|
+
if (tagName[i] === '@') {
|
|
380
|
+
// Check if followed by a digit
|
|
381
|
+
if (i + 1 < tagName.length) {
|
|
382
|
+
const nextCode = tagName.charCodeAt(i + 1);
|
|
383
|
+
if (nextCode >= 48 && nextCode <= 57) {
|
|
384
|
+
// 0-9
|
|
385
|
+
lastVersionAt = i;
|
|
386
|
+
break;
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
i--;
|
|
391
|
+
}
|
|
392
|
+
if (lastVersionAt > 0) {
|
|
393
|
+
return tagName.slice(0, lastVersionAt);
|
|
394
|
+
}
|
|
395
|
+
// Check for -v or _v pattern
|
|
396
|
+
i = tagName.length - 1;
|
|
397
|
+
while (i > 0) {
|
|
398
|
+
if (tagName[i] === 'v' || tagName[i] === 'V') {
|
|
399
|
+
const prev = tagName[i - 1];
|
|
400
|
+
if (prev === '-' || prev === '_') {
|
|
401
|
+
// Check if followed by digit
|
|
402
|
+
if (i + 1 < tagName.length) {
|
|
403
|
+
const nextCode = tagName.charCodeAt(i + 1);
|
|
404
|
+
if (nextCode >= 48 && nextCode <= 57) {
|
|
405
|
+
return tagName.slice(0, i - 1);
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
i--;
|
|
411
|
+
}
|
|
412
|
+
return undefined;
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* Builds a tag name from package name and version.
|
|
416
|
+
*
|
|
417
|
+
* @param packageName - Package name (e.g., '@scope/package' or 'package')
|
|
418
|
+
* @param version - Version string (e.g., '1.2.3')
|
|
419
|
+
* @param format - Tag format template, uses ${package} and ${version} placeholders
|
|
420
|
+
* @returns Formatted tag name
|
|
421
|
+
*
|
|
422
|
+
* @example
|
|
423
|
+
* buildTagName('@scope/pkg', '1.2.3') // '@scope/pkg@1.2.3'
|
|
424
|
+
* buildTagName('utils', '1.0.0', 'v${version}') // 'v1.0.0'
|
|
425
|
+
* buildTagName('pkg', '2.0.0', '${package}-v${version}') // 'pkg-v2.0.0'
|
|
426
|
+
*/
|
|
427
|
+
function buildTagName(packageName, version, format = '${package}@${version}') {
|
|
428
|
+
// Simple character-by-character replacement (no regex)
|
|
429
|
+
let result = '';
|
|
430
|
+
let i = 0;
|
|
431
|
+
while (i < format.length) {
|
|
432
|
+
if (i + 9 < format.length && format.slice(i, i + 10) === '${package}') {
|
|
433
|
+
result += packageName;
|
|
434
|
+
i += 10;
|
|
435
|
+
}
|
|
436
|
+
else if (i + 9 < format.length && format.slice(i, i + 10) === '${version}') {
|
|
437
|
+
result += version;
|
|
438
|
+
i += 10;
|
|
439
|
+
}
|
|
440
|
+
else {
|
|
441
|
+
result += format[i];
|
|
442
|
+
i++;
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
return result;
|
|
446
|
+
}
|
|
447
|
+
/**
|
|
448
|
+
* Compares two tags by version (newest first).
|
|
449
|
+
* Useful for sorting tags.
|
|
450
|
+
*
|
|
451
|
+
* @param a - First tag
|
|
452
|
+
* @param b - Second tag
|
|
453
|
+
* @returns Comparison result (-1, 0, or 1)
|
|
454
|
+
*/
|
|
455
|
+
function compareTagsByVersion(a, b) {
|
|
456
|
+
const versionA = extractVersionFromTag(a.name);
|
|
457
|
+
const versionB = extractVersionFromTag(b.name);
|
|
458
|
+
if (!versionA && !versionB)
|
|
459
|
+
return 0;
|
|
460
|
+
if (!versionA)
|
|
461
|
+
return 1;
|
|
462
|
+
if (!versionB)
|
|
463
|
+
return -1;
|
|
464
|
+
return compareVersionStrings(versionB, versionA); // Descending order
|
|
465
|
+
}
|
|
466
|
+
/**
|
|
467
|
+
* Simple version string comparison.
|
|
468
|
+
* Compares major.minor.patch numerically.
|
|
469
|
+
*
|
|
470
|
+
* @param a - First version
|
|
471
|
+
* @param b - Second version
|
|
472
|
+
* @returns Comparison result (-1, 0, or 1)
|
|
473
|
+
*/
|
|
474
|
+
function compareVersionStrings(a, b) {
|
|
475
|
+
const partsA = a.split('.');
|
|
476
|
+
const partsB = b.split('.');
|
|
477
|
+
const maxLen = max(partsA.length, partsB.length);
|
|
478
|
+
for (let i = 0; i < maxLen; i++) {
|
|
479
|
+
const numA = parseNumericPart(partsA[i]);
|
|
480
|
+
const numB = parseNumericPart(partsB[i]);
|
|
481
|
+
if (numA < numB)
|
|
482
|
+
return -1;
|
|
483
|
+
if (numA > numB)
|
|
484
|
+
return 1;
|
|
485
|
+
}
|
|
486
|
+
return 0;
|
|
487
|
+
}
|
|
488
|
+
/**
|
|
489
|
+
* Parses numeric part of version segment.
|
|
490
|
+
*
|
|
491
|
+
* @param part - Version part to parse
|
|
492
|
+
* @returns Numeric value
|
|
493
|
+
*/
|
|
494
|
+
function parseNumericPart(part) {
|
|
495
|
+
if (!part)
|
|
496
|
+
return 0;
|
|
497
|
+
// Extract leading digits only
|
|
498
|
+
let num = 0;
|
|
499
|
+
for (let i = 0; i < part.length; i++) {
|
|
500
|
+
const code = part.charCodeAt(i);
|
|
501
|
+
if (code >= 48 && code <= 57) {
|
|
502
|
+
num = num * 10 + (code - 48);
|
|
503
|
+
}
|
|
504
|
+
else {
|
|
505
|
+
break;
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
return num;
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
/**
|
|
512
|
+
* Creates a git reference from full name.
|
|
513
|
+
* Parses the reference type from the full name.
|
|
514
|
+
*
|
|
515
|
+
* @param options - Reference creation options
|
|
516
|
+
* @returns A new GitRef object
|
|
517
|
+
*
|
|
518
|
+
* @example
|
|
519
|
+
* const ref = createGitRef({
|
|
520
|
+
* fullName: 'refs/heads/main',
|
|
521
|
+
* commitHash: 'abc123...',
|
|
522
|
+
* })
|
|
523
|
+
*/
|
|
524
|
+
function createGitRef(options) {
|
|
525
|
+
const { type, name, remote } = parseRefName(options.fullName);
|
|
526
|
+
return {
|
|
527
|
+
fullName: options.fullName,
|
|
528
|
+
name,
|
|
529
|
+
type,
|
|
530
|
+
commitHash: options.commitHash,
|
|
531
|
+
remote,
|
|
532
|
+
isHead: options.isHead,
|
|
533
|
+
};
|
|
534
|
+
}
|
|
535
|
+
/**
|
|
536
|
+
* Parses a full reference name into its components.
|
|
537
|
+
* Uses character-by-character parsing (no regex).
|
|
538
|
+
*
|
|
539
|
+
* @param fullName - Full reference name
|
|
540
|
+
* @returns Parsed components
|
|
541
|
+
*
|
|
542
|
+
* @example
|
|
543
|
+
* parseRefName('refs/heads/main') // { type: 'branch', name: 'main' }
|
|
544
|
+
* parseRefName('refs/remotes/origin/main') // { type: 'remote', name: 'main', remote: 'origin' }
|
|
545
|
+
*/
|
|
546
|
+
function parseRefName(fullName) {
|
|
547
|
+
// Handle HEAD specially
|
|
548
|
+
if (fullName === 'HEAD') {
|
|
549
|
+
return { type: 'head', name: 'HEAD' };
|
|
550
|
+
}
|
|
551
|
+
// Split by '/' character
|
|
552
|
+
const parts = splitByChar(fullName, '/');
|
|
553
|
+
// refs/heads/... -> branch
|
|
554
|
+
if (parts.length >= 3 && parts[0] === 'refs' && parts[1] === 'heads') {
|
|
555
|
+
return {
|
|
556
|
+
type: 'branch',
|
|
557
|
+
name: parts.slice(2).join('/'),
|
|
558
|
+
};
|
|
559
|
+
}
|
|
560
|
+
// refs/tags/... -> tag
|
|
561
|
+
if (parts.length >= 3 && parts[0] === 'refs' && parts[1] === 'tags') {
|
|
562
|
+
return {
|
|
563
|
+
type: 'tag',
|
|
564
|
+
name: parts.slice(2).join('/'),
|
|
565
|
+
};
|
|
566
|
+
}
|
|
567
|
+
// refs/remotes/origin/... -> remote
|
|
568
|
+
if (parts.length >= 4 && parts[0] === 'refs' && parts[1] === 'remotes') {
|
|
569
|
+
return {
|
|
570
|
+
type: 'remote',
|
|
571
|
+
name: parts.slice(3).join('/'),
|
|
572
|
+
remote: parts[2],
|
|
573
|
+
};
|
|
574
|
+
}
|
|
575
|
+
// refs/stash -> stash
|
|
576
|
+
if (parts.length >= 2 && parts[0] === 'refs' && parts[1] === 'stash') {
|
|
577
|
+
return {
|
|
578
|
+
type: 'stash',
|
|
579
|
+
name: parts.slice(1).join('/'),
|
|
580
|
+
};
|
|
581
|
+
}
|
|
582
|
+
// Default to branch for unknown patterns
|
|
583
|
+
return {
|
|
584
|
+
type: 'branch',
|
|
585
|
+
name: fullName,
|
|
586
|
+
};
|
|
587
|
+
}
|
|
588
|
+
/**
|
|
589
|
+
* Splits a string by a character.
|
|
590
|
+
* Character-by-character implementation (no regex).
|
|
591
|
+
*
|
|
592
|
+
* @param str - String to split
|
|
593
|
+
* @param char - Character to split by
|
|
594
|
+
* @returns Array of parts
|
|
595
|
+
*/
|
|
596
|
+
function splitByChar(str, char) {
|
|
597
|
+
const parts = [];
|
|
598
|
+
let current = '';
|
|
599
|
+
for (let i = 0; i < str.length; i++) {
|
|
600
|
+
if (str[i] === char) {
|
|
601
|
+
parts.push(current);
|
|
602
|
+
current = '';
|
|
603
|
+
}
|
|
604
|
+
else {
|
|
605
|
+
current += str[i];
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
parts.push(current);
|
|
609
|
+
return parts;
|
|
610
|
+
}
|
|
611
|
+
/**
|
|
612
|
+
* Checks if a reference is a branch.
|
|
613
|
+
*
|
|
614
|
+
* @param ref - Reference to check
|
|
615
|
+
* @returns True if reference is a branch
|
|
616
|
+
*/
|
|
617
|
+
function isBranchRef(ref) {
|
|
618
|
+
return ref.type === 'branch';
|
|
619
|
+
}
|
|
620
|
+
/**
|
|
621
|
+
* Checks if a reference is a tag.
|
|
622
|
+
*
|
|
623
|
+
* @param ref - Reference to check
|
|
624
|
+
* @returns True if reference is a tag
|
|
625
|
+
*/
|
|
626
|
+
function isTagRef(ref) {
|
|
627
|
+
return ref.type === 'tag';
|
|
628
|
+
}
|
|
629
|
+
/**
|
|
630
|
+
* Checks if a reference is a remote tracking branch.
|
|
631
|
+
*
|
|
632
|
+
* @param ref - Reference to check
|
|
633
|
+
* @returns True if reference is a remote
|
|
634
|
+
*/
|
|
635
|
+
function isRemoteRef(ref) {
|
|
636
|
+
return ref.type === 'remote';
|
|
637
|
+
}
|
|
638
|
+
/**
|
|
639
|
+
* Checks if a reference points to the current HEAD.
|
|
640
|
+
*
|
|
641
|
+
* @param ref - Reference to check
|
|
642
|
+
* @returns True if reference is HEAD
|
|
643
|
+
*/
|
|
644
|
+
function isHeadRef(ref) {
|
|
645
|
+
return ref.type === 'head' || ref.isHead === true;
|
|
646
|
+
}
|
|
647
|
+
/**
|
|
648
|
+
* Gets the tracking remote for a reference.
|
|
649
|
+
*
|
|
650
|
+
* @param ref - Reference to check
|
|
651
|
+
* @returns Remote name or undefined
|
|
652
|
+
*/
|
|
653
|
+
function getRemote(ref) {
|
|
654
|
+
return ref.remote;
|
|
655
|
+
}
|
|
656
|
+
/**
|
|
657
|
+
* Builds a full reference name from type and name.
|
|
658
|
+
*
|
|
659
|
+
* @param type - Reference type
|
|
660
|
+
* @param name - Reference name
|
|
661
|
+
* @param remote - Remote name (for remote type)
|
|
662
|
+
* @returns Full reference name
|
|
663
|
+
*
|
|
664
|
+
* @example
|
|
665
|
+
* buildRefName('branch', 'main') // 'refs/heads/main'
|
|
666
|
+
* buildRefName('tag', 'v1.0.0') // 'refs/tags/v1.0.0'
|
|
667
|
+
* buildRefName('remote', 'main', 'origin') // 'refs/remotes/origin/main'
|
|
668
|
+
*/
|
|
669
|
+
function buildRefName(type, name, remote) {
|
|
670
|
+
switch (type) {
|
|
671
|
+
case 'branch':
|
|
672
|
+
return `refs/heads/${name}`;
|
|
673
|
+
case 'tag':
|
|
674
|
+
return `refs/tags/${name}`;
|
|
675
|
+
case 'remote':
|
|
676
|
+
return remote ? `refs/remotes/${remote}/${name}` : `refs/remotes/${name}`;
|
|
677
|
+
case 'head':
|
|
678
|
+
return 'HEAD';
|
|
679
|
+
case 'stash':
|
|
680
|
+
return `refs/stash`;
|
|
681
|
+
default:
|
|
682
|
+
return name;
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
/**
|
|
686
|
+
* Compares two references by name (alphabetically).
|
|
687
|
+
*
|
|
688
|
+
* @param a - First reference
|
|
689
|
+
* @param b - Second reference
|
|
690
|
+
* @returns Comparison result (-1, 0, or 1)
|
|
691
|
+
*/
|
|
692
|
+
function compareRefsByName(a, b) {
|
|
693
|
+
if (a.name < b.name)
|
|
694
|
+
return -1;
|
|
695
|
+
if (a.name > b.name)
|
|
696
|
+
return 1;
|
|
697
|
+
return 0;
|
|
698
|
+
}
|
|
699
|
+
/**
|
|
700
|
+
* Filters references by type.
|
|
701
|
+
*
|
|
702
|
+
* @param refs - References to filter
|
|
703
|
+
* @param type - Type to filter by
|
|
704
|
+
* @returns Filtered references
|
|
705
|
+
*/
|
|
706
|
+
function filterRefsByType(refs, type) {
|
|
707
|
+
const result = [];
|
|
708
|
+
for (const ref of refs) {
|
|
709
|
+
if (ref.type === type) {
|
|
710
|
+
result.push(ref);
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
return result;
|
|
714
|
+
}
|
|
715
|
+
/**
|
|
716
|
+
* Filters references by remote.
|
|
717
|
+
*
|
|
718
|
+
* @param refs - References to filter
|
|
719
|
+
* @param remote - Remote name to filter by
|
|
720
|
+
* @returns Filtered references
|
|
721
|
+
*/
|
|
722
|
+
function filterRefsByRemote(refs, remote) {
|
|
723
|
+
const result = [];
|
|
724
|
+
for (const ref of refs) {
|
|
725
|
+
if (ref.remote === remote) {
|
|
726
|
+
result.push(ref);
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
return result;
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
/**
|
|
733
|
+
* Safe copies of Error built-ins via factory functions.
|
|
734
|
+
*
|
|
735
|
+
* Since constructors cannot be safely captured via Object.assign, this module
|
|
736
|
+
* provides factory functions that use Reflect.construct internally.
|
|
737
|
+
*
|
|
738
|
+
* These references are captured at module initialization time to protect against
|
|
739
|
+
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
740
|
+
*
|
|
741
|
+
* @module @hyperfrontend/immutable-api-utils/built-in-copy/error
|
|
742
|
+
*/
|
|
743
|
+
// Capture references at module initialization time
|
|
744
|
+
const _Error = globalThis.Error;
|
|
745
|
+
const _Reflect$1 = globalThis.Reflect;
|
|
746
|
+
/**
|
|
747
|
+
* (Safe copy) Creates a new Error using the captured Error constructor.
|
|
748
|
+
* Use this instead of `new Error()`.
|
|
749
|
+
*
|
|
750
|
+
* @param message - Optional error message.
|
|
751
|
+
* @param options - Optional error options.
|
|
752
|
+
* @returns A new Error instance.
|
|
753
|
+
*/
|
|
754
|
+
const createError = (message, options) => _Reflect$1.construct(_Error, [message, options]);
|
|
755
|
+
|
|
756
|
+
/**
|
|
757
|
+
* Default log options.
|
|
758
|
+
*/
|
|
759
|
+
const DEFAULT_LOG_OPTIONS = {
|
|
760
|
+
maxCount: 100,
|
|
761
|
+
includeMerges: true,
|
|
762
|
+
timeout: 30000,
|
|
763
|
+
};
|
|
764
|
+
/**
|
|
765
|
+
* Git log format string for structured output.
|
|
766
|
+
* Uses ASCII delimiters that won't appear in commit messages.
|
|
767
|
+
*/
|
|
768
|
+
const LOG_FORMAT = [
|
|
769
|
+
'%H', // full hash
|
|
770
|
+
'%an', // author name
|
|
771
|
+
'%ae', // author email
|
|
772
|
+
'%aI', // author date (ISO 8601)
|
|
773
|
+
'%cn', // committer name
|
|
774
|
+
'%ce', // committer email
|
|
775
|
+
'%cI', // commit date (ISO 8601)
|
|
776
|
+
'%s', // subject
|
|
777
|
+
'%b', // body
|
|
778
|
+
'%P', // parent hashes
|
|
779
|
+
'%D', // refs
|
|
780
|
+
].join('%x00'); // NUL separator
|
|
781
|
+
/**
|
|
782
|
+
* Record separator for commit entries.
|
|
783
|
+
*/
|
|
784
|
+
const RECORD_SEPARATOR = '\x1e'; // ASCII Record Separator
|
|
785
|
+
/**
|
|
786
|
+
* Gets the commit log from a git repository.
|
|
787
|
+
*
|
|
788
|
+
* @param options - Configuration for retrieving the commit log
|
|
789
|
+
* @returns Array of GitCommit objects
|
|
790
|
+
*
|
|
791
|
+
* @example
|
|
792
|
+
* const commits = getCommitLog({ maxCount: 10 })
|
|
793
|
+
* const recentChanges = getCommitLog({ from: 'v1.0.0', to: 'HEAD' })
|
|
794
|
+
*/
|
|
795
|
+
function getCommitLog(options = {}) {
|
|
796
|
+
const opts = { ...DEFAULT_LOG_OPTIONS, ...options };
|
|
797
|
+
const args = ['log', `--format=${RECORD_SEPARATOR}${LOG_FORMAT}`];
|
|
798
|
+
// Add options
|
|
799
|
+
if (opts.maxCount !== undefined && opts.maxCount > 0) {
|
|
800
|
+
args.push(`-n${opts.maxCount}`);
|
|
801
|
+
}
|
|
802
|
+
if (!opts.includeMerges) {
|
|
803
|
+
args.push('--no-merges');
|
|
804
|
+
}
|
|
805
|
+
if (opts.author) {
|
|
806
|
+
const safeAuthor = escapeGitArg(opts.author);
|
|
807
|
+
args.push(`--author=${safeAuthor}`);
|
|
808
|
+
}
|
|
809
|
+
// Add range
|
|
810
|
+
if (opts.from && opts.to) {
|
|
811
|
+
const safeFrom = escapeGitRef(opts.from);
|
|
812
|
+
const safeTo = escapeGitRef(opts.to);
|
|
813
|
+
args.push(`${safeFrom}..${safeTo}`);
|
|
814
|
+
}
|
|
815
|
+
else if (opts.from) {
|
|
816
|
+
const safeFrom = escapeGitRef(opts.from);
|
|
817
|
+
args.push(`${safeFrom}..HEAD`);
|
|
818
|
+
}
|
|
819
|
+
else if (opts.to) {
|
|
820
|
+
const safeTo = escapeGitRef(opts.to);
|
|
821
|
+
args.push(safeTo);
|
|
822
|
+
}
|
|
823
|
+
// Add path filter
|
|
824
|
+
if (opts.path) {
|
|
825
|
+
const safePath = escapeGitPath(opts.path);
|
|
826
|
+
args.push('--', safePath);
|
|
827
|
+
}
|
|
828
|
+
try {
|
|
829
|
+
const output = node_child_process.execSync(`git ${args.join(' ')}`, {
|
|
830
|
+
encoding: 'utf-8',
|
|
831
|
+
cwd: opts.cwd,
|
|
832
|
+
timeout: opts.timeout,
|
|
833
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
834
|
+
maxBuffer: 50 * 1024 * 1024, // 50MB
|
|
835
|
+
});
|
|
836
|
+
return parseCommitLog(output);
|
|
837
|
+
}
|
|
838
|
+
catch (error) {
|
|
839
|
+
// Check if error is due to no commits
|
|
840
|
+
if (error instanceof Error && error.message.includes('does not have any commits')) {
|
|
841
|
+
return [];
|
|
842
|
+
}
|
|
843
|
+
throw error;
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
/**
|
|
847
|
+
* Gets commits between two references.
|
|
848
|
+
*
|
|
849
|
+
* @param from - Starting reference (exclusive)
|
|
850
|
+
* @param to - Ending reference (inclusive, default: HEAD)
|
|
851
|
+
* @param options - Additional options
|
|
852
|
+
* @returns Array of GitCommit objects
|
|
853
|
+
*
|
|
854
|
+
* @example
|
|
855
|
+
* const commits = getCommitsBetween('v1.0.0', 'v1.1.0')
|
|
856
|
+
*/
|
|
857
|
+
function getCommitsBetween(from, to = 'HEAD', options = {}) {
|
|
858
|
+
return getCommitLog({ ...options, from, to });
|
|
859
|
+
}
|
|
860
|
+
/**
|
|
861
|
+
* Gets commits since a specific tag or reference.
|
|
862
|
+
*
|
|
863
|
+
* @param since - Reference to start from (exclusive)
|
|
864
|
+
* @param options - Additional options
|
|
865
|
+
* @returns Array of GitCommit objects
|
|
866
|
+
*
|
|
867
|
+
* @example
|
|
868
|
+
* const commits = getCommitsSince('v1.0.0')
|
|
869
|
+
*/
|
|
870
|
+
function getCommitsSince(since, options = {}) {
|
|
871
|
+
return getCommitLog({ ...options, from: since });
|
|
872
|
+
}
|
|
873
|
+
/**
|
|
874
|
+
* Gets a single commit by its hash.
|
|
875
|
+
*
|
|
876
|
+
* @param hash - Commit hash (full or short)
|
|
877
|
+
* @param options - Additional options
|
|
878
|
+
* @returns GitCommit or null if not found
|
|
879
|
+
*
|
|
880
|
+
* @example
|
|
881
|
+
* const commit = getCommit('abc1234')
|
|
882
|
+
*/
|
|
883
|
+
function getCommit(hash, options = {}) {
|
|
884
|
+
const safeHash = escapeGitRef(hash);
|
|
885
|
+
try {
|
|
886
|
+
const commits = getCommitLog({
|
|
887
|
+
...options,
|
|
888
|
+
to: safeHash,
|
|
889
|
+
maxCount: 1,
|
|
890
|
+
});
|
|
891
|
+
return commits[0] ?? null;
|
|
892
|
+
}
|
|
893
|
+
catch {
|
|
894
|
+
return null;
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
/**
|
|
898
|
+
* Checks if a commit exists in the repository.
|
|
899
|
+
*
|
|
900
|
+
* @param hash - Commit hash to check
|
|
901
|
+
* @param options - Additional options
|
|
902
|
+
* @returns True if commit exists
|
|
903
|
+
*/
|
|
904
|
+
function commitExists(hash, options = {}) {
|
|
905
|
+
const safeHash = escapeGitRef(hash);
|
|
906
|
+
try {
|
|
907
|
+
node_child_process.execSync(`git cat-file -t ${safeHash}`, {
|
|
908
|
+
encoding: 'utf-8',
|
|
909
|
+
cwd: options.cwd,
|
|
910
|
+
timeout: options.timeout ?? 5000,
|
|
911
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
912
|
+
});
|
|
913
|
+
return true;
|
|
914
|
+
}
|
|
915
|
+
catch {
|
|
916
|
+
return false;
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
/**
|
|
920
|
+
* Parses raw git log output into GitCommit objects.
|
|
921
|
+
*
|
|
922
|
+
* @param output - Raw git log output
|
|
923
|
+
* @returns Array of GitCommit objects
|
|
924
|
+
*/
|
|
925
|
+
function parseCommitLog(output) {
|
|
926
|
+
const commits = [];
|
|
927
|
+
if (!output.trim()) {
|
|
928
|
+
return commits;
|
|
929
|
+
}
|
|
930
|
+
// Split by record separator
|
|
931
|
+
const records = splitByDelimiter(output, RECORD_SEPARATOR);
|
|
932
|
+
for (const record of records) {
|
|
933
|
+
const trimmed = record.trim();
|
|
934
|
+
if (!trimmed)
|
|
935
|
+
continue;
|
|
936
|
+
// Split by NUL character
|
|
937
|
+
const fields = splitByDelimiter(trimmed, '\x00');
|
|
938
|
+
if (fields.length < 10)
|
|
939
|
+
continue;
|
|
940
|
+
const [hash, authorName, authorEmail, authorDate, committerName, committerEmail, commitDate, subject, body, parentsStr, refsStr] = fields;
|
|
941
|
+
// Parse parents (space-separated hashes)
|
|
942
|
+
const parents = parentsStr ? splitByDelimiter(parentsStr, ' ').filter((p) => p.trim()) : [];
|
|
943
|
+
// Parse refs (comma-separated, may have prefixes like 'HEAD -> ')
|
|
944
|
+
const refs = parseRefs(refsStr || '');
|
|
945
|
+
commits.push(createGitCommit({
|
|
946
|
+
hash,
|
|
947
|
+
authorName,
|
|
948
|
+
authorEmail,
|
|
949
|
+
authorDate,
|
|
950
|
+
committerName,
|
|
951
|
+
committerEmail,
|
|
952
|
+
commitDate,
|
|
953
|
+
subject,
|
|
954
|
+
body: body || undefined,
|
|
955
|
+
parents,
|
|
956
|
+
refs,
|
|
957
|
+
}));
|
|
958
|
+
}
|
|
959
|
+
return commits;
|
|
960
|
+
}
|
|
961
|
+
/**
|
|
962
|
+
* Parses ref string from git log.
|
|
963
|
+
*
|
|
964
|
+
* @param refsStr - Raw refs string from git log
|
|
965
|
+
* @returns Array of ref names
|
|
966
|
+
*/
|
|
967
|
+
function parseRefs(refsStr) {
|
|
968
|
+
if (!refsStr.trim()) {
|
|
969
|
+
return [];
|
|
970
|
+
}
|
|
971
|
+
const refs = [];
|
|
972
|
+
const parts = splitByDelimiter(refsStr, ',');
|
|
973
|
+
for (const part of parts) {
|
|
974
|
+
let ref = part.trim();
|
|
975
|
+
// Handle 'HEAD -> branch' format
|
|
976
|
+
const arrowIndex = findSubstring(ref, ' -> ');
|
|
977
|
+
if (arrowIndex !== -1) {
|
|
978
|
+
refs.push('HEAD');
|
|
979
|
+
ref = ref.slice(arrowIndex + 4);
|
|
980
|
+
}
|
|
981
|
+
// Handle 'tag: tagname' format
|
|
982
|
+
if (startsWithPrefix$2(ref, 'tag: ')) {
|
|
983
|
+
ref = ref.slice(5);
|
|
984
|
+
}
|
|
985
|
+
if (ref) {
|
|
986
|
+
refs.push(ref);
|
|
987
|
+
}
|
|
988
|
+
}
|
|
989
|
+
return refs;
|
|
990
|
+
}
|
|
991
|
+
/**
|
|
992
|
+
* Splits string by delimiter (no regex).
|
|
993
|
+
*
|
|
994
|
+
* @param str - String to split
|
|
995
|
+
* @param delimiter - Delimiter to split by
|
|
996
|
+
* @returns Array of parts
|
|
997
|
+
*/
|
|
998
|
+
function splitByDelimiter(str, delimiter) {
|
|
999
|
+
const parts = [];
|
|
1000
|
+
let current = '';
|
|
1001
|
+
let i = 0;
|
|
1002
|
+
while (i < str.length) {
|
|
1003
|
+
if (matchesAt(str, i, delimiter)) {
|
|
1004
|
+
parts.push(current);
|
|
1005
|
+
current = '';
|
|
1006
|
+
i += delimiter.length;
|
|
1007
|
+
}
|
|
1008
|
+
else {
|
|
1009
|
+
current += str[i];
|
|
1010
|
+
i++;
|
|
1011
|
+
}
|
|
1012
|
+
}
|
|
1013
|
+
parts.push(current);
|
|
1014
|
+
return parts;
|
|
1015
|
+
}
|
|
1016
|
+
/**
|
|
1017
|
+
* Checks if string matches at position.
|
|
1018
|
+
*
|
|
1019
|
+
* @param str - String to check
|
|
1020
|
+
* @param pos - Position to check at
|
|
1021
|
+
* @param pattern - Pattern to match
|
|
1022
|
+
* @returns True if matches
|
|
1023
|
+
*/
|
|
1024
|
+
function matchesAt(str, pos, pattern) {
|
|
1025
|
+
if (pos + pattern.length > str.length)
|
|
1026
|
+
return false;
|
|
1027
|
+
for (let i = 0; i < pattern.length; i++) {
|
|
1028
|
+
if (str[pos + i] !== pattern[i])
|
|
1029
|
+
return false;
|
|
1030
|
+
}
|
|
1031
|
+
return true;
|
|
1032
|
+
}
|
|
1033
|
+
/**
|
|
1034
|
+
* Finds substring position (no regex).
|
|
1035
|
+
*
|
|
1036
|
+
* @param str - String to search
|
|
1037
|
+
* @param pattern - Pattern to find
|
|
1038
|
+
* @returns Position or -1 if not found
|
|
1039
|
+
*/
|
|
1040
|
+
function findSubstring(str, pattern) {
|
|
1041
|
+
for (let i = 0; i <= str.length - pattern.length; i++) {
|
|
1042
|
+
if (matchesAt(str, i, pattern)) {
|
|
1043
|
+
return i;
|
|
1044
|
+
}
|
|
1045
|
+
}
|
|
1046
|
+
return -1;
|
|
1047
|
+
}
|
|
1048
|
+
/**
|
|
1049
|
+
* Checks if string starts with prefix (no regex).
|
|
1050
|
+
*
|
|
1051
|
+
* @param str - String to check
|
|
1052
|
+
* @param prefix - Prefix to check for
|
|
1053
|
+
* @returns True if starts with prefix
|
|
1054
|
+
*/
|
|
1055
|
+
function startsWithPrefix$2(str, prefix) {
|
|
1056
|
+
return matchesAt(str, 0, prefix);
|
|
1057
|
+
}
|
|
1058
|
+
// ============================================================================
|
|
1059
|
+
// Security helpers - character-by-character validation (no regex)
|
|
1060
|
+
// ============================================================================
|
|
1061
|
+
/**
|
|
1062
|
+
* Maximum allowed git reference length.
|
|
1063
|
+
*/
|
|
1064
|
+
const MAX_REF_LENGTH = 256;
|
|
1065
|
+
/**
|
|
1066
|
+
* Escapes a git reference for safe use in shell commands.
|
|
1067
|
+
*
|
|
1068
|
+
* @param ref - Reference to escape
|
|
1069
|
+
* @returns Safe reference string
|
|
1070
|
+
* @throws {Error} If reference contains invalid characters
|
|
1071
|
+
*/
|
|
1072
|
+
function escapeGitRef(ref) {
|
|
1073
|
+
if (!ref || typeof ref !== 'string') {
|
|
1074
|
+
throw createError('Git reference is required');
|
|
1075
|
+
}
|
|
1076
|
+
if (ref.length > MAX_REF_LENGTH) {
|
|
1077
|
+
throw createError(`Git reference exceeds maximum length of ${MAX_REF_LENGTH}`);
|
|
1078
|
+
}
|
|
1079
|
+
const safe = [];
|
|
1080
|
+
for (let i = 0; i < ref.length; i++) {
|
|
1081
|
+
const code = ref.charCodeAt(i);
|
|
1082
|
+
// Allow: a-z, A-Z, 0-9, /, -, _, ., ~, ^, @, {, }
|
|
1083
|
+
if ((code >= 97 && code <= 122) || // a-z
|
|
1084
|
+
(code >= 65 && code <= 90) || // A-Z
|
|
1085
|
+
(code >= 48 && code <= 57) || // 0-9
|
|
1086
|
+
code === 47 || // /
|
|
1087
|
+
code === 45 || // -
|
|
1088
|
+
code === 95 || // _
|
|
1089
|
+
code === 46 || // .
|
|
1090
|
+
code === 126 || // ~
|
|
1091
|
+
code === 94 || // ^
|
|
1092
|
+
code === 64 || // @
|
|
1093
|
+
code === 123 || // {
|
|
1094
|
+
code === 125 // }
|
|
1095
|
+
) {
|
|
1096
|
+
safe.push(ref[i]);
|
|
1097
|
+
}
|
|
1098
|
+
else {
|
|
1099
|
+
throw createError(`Invalid character in git reference at position ${i}: "${ref[i]}"`);
|
|
1100
|
+
}
|
|
1101
|
+
}
|
|
1102
|
+
return safe.join('');
|
|
1103
|
+
}
|
|
1104
|
+
/**
|
|
1105
|
+
* Maximum allowed git path length.
|
|
1106
|
+
*/
|
|
1107
|
+
const MAX_PATH_LENGTH$1 = 4096;
|
|
1108
|
+
/**
|
|
1109
|
+
* Escapes a file path for safe use in git commands.
|
|
1110
|
+
*
|
|
1111
|
+
* @param path - Path to escape
|
|
1112
|
+
* @returns Safe path string
|
|
1113
|
+
* @throws {Error} If path contains invalid characters
|
|
1114
|
+
*/
|
|
1115
|
+
function escapeGitPath(path) {
|
|
1116
|
+
if (!path || typeof path !== 'string') {
|
|
1117
|
+
throw createError('Path is required');
|
|
1118
|
+
}
|
|
1119
|
+
if (path.length > MAX_PATH_LENGTH$1) {
|
|
1120
|
+
throw createError(`Path exceeds maximum length of ${MAX_PATH_LENGTH$1}`);
|
|
1121
|
+
}
|
|
1122
|
+
const safe = [];
|
|
1123
|
+
for (let i = 0; i < path.length; i++) {
|
|
1124
|
+
const code = path.charCodeAt(i);
|
|
1125
|
+
// Allow: a-z, A-Z, 0-9, /, \, -, _, ., space
|
|
1126
|
+
if ((code >= 97 && code <= 122) || // a-z
|
|
1127
|
+
(code >= 65 && code <= 90) || // A-Z
|
|
1128
|
+
(code >= 48 && code <= 57) || // 0-9
|
|
1129
|
+
code === 47 || // /
|
|
1130
|
+
code === 92 || // \
|
|
1131
|
+
code === 45 || // -
|
|
1132
|
+
code === 95 || // _
|
|
1133
|
+
code === 46 || // .
|
|
1134
|
+
code === 32 // space
|
|
1135
|
+
) {
|
|
1136
|
+
safe.push(path[i]);
|
|
1137
|
+
}
|
|
1138
|
+
else {
|
|
1139
|
+
throw createError(`Invalid character in path at position ${i}: "${path[i]}"`);
|
|
1140
|
+
}
|
|
1141
|
+
}
|
|
1142
|
+
return safe.join('');
|
|
1143
|
+
}
|
|
1144
|
+
/**
|
|
1145
|
+
* Maximum allowed argument length.
|
|
1146
|
+
*/
|
|
1147
|
+
const MAX_ARG_LENGTH = 1000;
|
|
1148
|
+
/**
|
|
1149
|
+
* Escapes a general git argument for safe use in shell commands.
|
|
1150
|
+
*
|
|
1151
|
+
* @param arg - Argument to escape
|
|
1152
|
+
* @returns Safe argument string
|
|
1153
|
+
* @throws {Error} If argument contains invalid characters
|
|
1154
|
+
*/
|
|
1155
|
+
function escapeGitArg(arg) {
|
|
1156
|
+
if (!arg || typeof arg !== 'string') {
|
|
1157
|
+
throw createError('Argument is required');
|
|
1158
|
+
}
|
|
1159
|
+
if (arg.length > MAX_ARG_LENGTH) {
|
|
1160
|
+
throw createError(`Argument exceeds maximum length of ${MAX_ARG_LENGTH}`);
|
|
1161
|
+
}
|
|
1162
|
+
const safe = [];
|
|
1163
|
+
for (let i = 0; i < arg.length; i++) {
|
|
1164
|
+
const code = arg.charCodeAt(i);
|
|
1165
|
+
// Allow: a-z, A-Z, 0-9, space, @, ., -, _, <, >, +
|
|
1166
|
+
if ((code >= 97 && code <= 122) || // a-z
|
|
1167
|
+
(code >= 65 && code <= 90) || // A-Z
|
|
1168
|
+
(code >= 48 && code <= 57) || // 0-9
|
|
1169
|
+
code === 32 || // space
|
|
1170
|
+
code === 64 || // @
|
|
1171
|
+
code === 46 || // .
|
|
1172
|
+
code === 45 || // -
|
|
1173
|
+
code === 95 || // _
|
|
1174
|
+
code === 60 || // <
|
|
1175
|
+
code === 62 || // >
|
|
1176
|
+
code === 43 // +
|
|
1177
|
+
) {
|
|
1178
|
+
safe.push(arg[i]);
|
|
1179
|
+
}
|
|
1180
|
+
else {
|
|
1181
|
+
throw createError(`Invalid character in argument at position ${i}: "${arg[i]}"`);
|
|
1182
|
+
}
|
|
1183
|
+
}
|
|
1184
|
+
return safe.join('');
|
|
1185
|
+
}
|
|
1186
|
+
|
|
1187
|
+
/**
|
|
1188
|
+
* Safe copies of Date built-in via factory function and static methods.
|
|
1189
|
+
*
|
|
1190
|
+
* Since constructors cannot be safely captured via Object.assign, this module
|
|
1191
|
+
* provides a factory function that uses Reflect.construct internally.
|
|
1192
|
+
*
|
|
1193
|
+
* These references are captured at module initialization time to protect against
|
|
1194
|
+
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
1195
|
+
*
|
|
1196
|
+
* @module @hyperfrontend/immutable-api-utils/built-in-copy/date
|
|
1197
|
+
*/
|
|
1198
|
+
// Capture references at module initialization time
|
|
1199
|
+
const _Date = globalThis.Date;
|
|
1200
|
+
const _Reflect = globalThis.Reflect;
|
|
1201
|
+
function createDate(...args) {
|
|
1202
|
+
return _Reflect.construct(_Date, args);
|
|
1203
|
+
}
|
|
1204
|
+
|
|
1205
|
+
/**
|
|
1206
|
+
* Safe copies of Number built-in methods and constants.
|
|
1207
|
+
*
|
|
1208
|
+
* These references are captured at module initialization time to protect against
|
|
1209
|
+
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
1210
|
+
*
|
|
1211
|
+
* @module @hyperfrontend/immutable-api-utils/built-in-copy/number
|
|
1212
|
+
*/
|
|
1213
|
+
// Capture references at module initialization time
|
|
1214
|
+
const _parseInt = globalThis.parseInt;
|
|
1215
|
+
const _isNaN = globalThis.isNaN;
|
|
1216
|
+
// ============================================================================
|
|
1217
|
+
// Parsing
|
|
1218
|
+
// ============================================================================
|
|
1219
|
+
/**
|
|
1220
|
+
* (Safe copy) Parses a string and returns an integer.
|
|
1221
|
+
*/
|
|
1222
|
+
const parseInt = _parseInt;
|
|
1223
|
+
// ============================================================================
|
|
1224
|
+
// Global Type Checking (legacy, less strict)
|
|
1225
|
+
// ============================================================================
|
|
1226
|
+
/**
|
|
1227
|
+
* (Safe copy) Global isNaN function (coerces to number first, less strict than Number.isNaN).
|
|
1228
|
+
*/
|
|
1229
|
+
const globalIsNaN = _isNaN;
|
|
1230
|
+
|
|
1231
|
+
/**
|
|
1232
|
+
* Default tag options.
|
|
1233
|
+
*/
|
|
1234
|
+
const DEFAULT_TAG_OPTIONS = {
|
|
1235
|
+
timeout: 10000,
|
|
1236
|
+
};
|
|
1237
|
+
/**
|
|
1238
|
+
* Gets all tags from the repository.
|
|
1239
|
+
*
|
|
1240
|
+
* @param options - Tag listing options
|
|
1241
|
+
* @returns Array of GitTag objects
|
|
1242
|
+
*
|
|
1243
|
+
* @example
|
|
1244
|
+
* const tags = getTags()
|
|
1245
|
+
* const versionTags = getTags({ pattern: 'v' })
|
|
1246
|
+
*/
|
|
1247
|
+
function getTags(options = {}) {
|
|
1248
|
+
const opts = { ...DEFAULT_TAG_OPTIONS, ...options };
|
|
1249
|
+
const args = ['tag', '-l', '--sort=-creatordate'];
|
|
1250
|
+
if (opts.pattern) {
|
|
1251
|
+
const safePattern = escapeGitTagPattern(opts.pattern);
|
|
1252
|
+
args.push(safePattern + '*');
|
|
1253
|
+
}
|
|
1254
|
+
try {
|
|
1255
|
+
const output = node_child_process.execSync(`git ${args.join(' ')}`, {
|
|
1256
|
+
encoding: 'utf-8',
|
|
1257
|
+
cwd: opts.cwd,
|
|
1258
|
+
timeout: opts.timeout,
|
|
1259
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
1260
|
+
});
|
|
1261
|
+
const tagNames = output
|
|
1262
|
+
.split('\n')
|
|
1263
|
+
.map((line) => line.trim())
|
|
1264
|
+
.filter((line) => line.length > 0);
|
|
1265
|
+
// Limit results if requested
|
|
1266
|
+
const limitedNames = opts.maxCount ? tagNames.slice(0, opts.maxCount) : tagNames;
|
|
1267
|
+
// Get details for each tag
|
|
1268
|
+
const tags = [];
|
|
1269
|
+
for (const name of limitedNames) {
|
|
1270
|
+
const tag = getTagDetails(name, opts);
|
|
1271
|
+
if (tag) {
|
|
1272
|
+
tags.push(tag);
|
|
1273
|
+
}
|
|
1274
|
+
}
|
|
1275
|
+
return tags;
|
|
1276
|
+
}
|
|
1277
|
+
catch {
|
|
1278
|
+
return [];
|
|
1279
|
+
}
|
|
1280
|
+
}
|
|
1281
|
+
/**
|
|
1282
|
+
* Gets detailed information about a specific tag.
|
|
1283
|
+
*
|
|
1284
|
+
* @param name - The tag name to look up
|
|
1285
|
+
* @param options - Configuration for the tag operation
|
|
1286
|
+
* @returns GitTag or null if not found
|
|
1287
|
+
*
|
|
1288
|
+
* @example
|
|
1289
|
+
* const tag = getTag('v1.0.0')
|
|
1290
|
+
*/
|
|
1291
|
+
function getTag(name, options = {}) {
|
|
1292
|
+
return getTagDetails(name, { ...DEFAULT_TAG_OPTIONS, ...options });
|
|
1293
|
+
}
|
|
1294
|
+
/**
|
|
1295
|
+
* Gets tag details including type and commit hash.
|
|
1296
|
+
*
|
|
1297
|
+
* @param name - The tag name to retrieve details for
|
|
1298
|
+
* @param options - Configuration for the tag operation
|
|
1299
|
+
* @returns GitTag or null
|
|
1300
|
+
*/
|
|
1301
|
+
function getTagDetails(name, options) {
|
|
1302
|
+
const safeName = escapeGitRef(name);
|
|
1303
|
+
try {
|
|
1304
|
+
// Get the commit hash the tag points to
|
|
1305
|
+
const commitHash = node_child_process.execSync(`git rev-list -1 ${safeName}`, {
|
|
1306
|
+
encoding: 'utf-8',
|
|
1307
|
+
cwd: options.cwd,
|
|
1308
|
+
timeout: options.timeout,
|
|
1309
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
1310
|
+
}).trim();
|
|
1311
|
+
// Check if it's an annotated tag by trying to get tag message
|
|
1312
|
+
try {
|
|
1313
|
+
const tagInfo = node_child_process.execSync(`git cat-file tag ${safeName}`, {
|
|
1314
|
+
encoding: 'utf-8',
|
|
1315
|
+
cwd: options.cwd,
|
|
1316
|
+
timeout: options.timeout,
|
|
1317
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
1318
|
+
});
|
|
1319
|
+
// Parse annotated tag info
|
|
1320
|
+
const parsed = parseAnnotatedTagInfo(tagInfo);
|
|
1321
|
+
return createAnnotatedTag({
|
|
1322
|
+
name,
|
|
1323
|
+
commitHash,
|
|
1324
|
+
message: parsed.message,
|
|
1325
|
+
taggerName: parsed.taggerName,
|
|
1326
|
+
taggerEmail: parsed.taggerEmail,
|
|
1327
|
+
tagDate: parsed.tagDate,
|
|
1328
|
+
});
|
|
1329
|
+
}
|
|
1330
|
+
catch {
|
|
1331
|
+
// Not an annotated tag, it's lightweight
|
|
1332
|
+
return createLightweightTag({
|
|
1333
|
+
name,
|
|
1334
|
+
commitHash,
|
|
1335
|
+
});
|
|
1336
|
+
}
|
|
1337
|
+
}
|
|
1338
|
+
catch {
|
|
1339
|
+
return null;
|
|
1340
|
+
}
|
|
1341
|
+
}
|
|
1342
|
+
/**
|
|
1343
|
+
* Parses annotated tag info from git cat-file output.
|
|
1344
|
+
*
|
|
1345
|
+
* @param info - Raw tag info
|
|
1346
|
+
* @returns Parsed info
|
|
1347
|
+
*/
|
|
1348
|
+
function parseAnnotatedTagInfo(info) {
|
|
1349
|
+
const lines = info.split('\n');
|
|
1350
|
+
let taggerName = '';
|
|
1351
|
+
let taggerEmail = '';
|
|
1352
|
+
let tagDate = '';
|
|
1353
|
+
let messageStart = -1;
|
|
1354
|
+
for (let i = 0; i < lines.length; i++) {
|
|
1355
|
+
const line = lines[i];
|
|
1356
|
+
if (startsWithPrefix$1(line, 'tagger ')) {
|
|
1357
|
+
const taggerLine = line.slice(7);
|
|
1358
|
+
const parsed = parseTaggerLine(taggerLine);
|
|
1359
|
+
taggerName = parsed.name;
|
|
1360
|
+
taggerEmail = parsed.email;
|
|
1361
|
+
tagDate = parsed.date;
|
|
1362
|
+
}
|
|
1363
|
+
// Empty line marks start of message
|
|
1364
|
+
if (line === '' && messageStart === -1) {
|
|
1365
|
+
messageStart = i + 1;
|
|
1366
|
+
break;
|
|
1367
|
+
}
|
|
1368
|
+
}
|
|
1369
|
+
const message = messageStart >= 0 ? lines.slice(messageStart).join('\n').trim() : '';
|
|
1370
|
+
return {
|
|
1371
|
+
message,
|
|
1372
|
+
taggerName,
|
|
1373
|
+
taggerEmail,
|
|
1374
|
+
tagDate,
|
|
1375
|
+
};
|
|
1376
|
+
}
|
|
1377
|
+
/**
|
|
1378
|
+
* Parses tagger line from annotated tag.
|
|
1379
|
+
* Format: Name <email> timestamp timezone
|
|
1380
|
+
*
|
|
1381
|
+
* @param line - Raw tagger line from git output
|
|
1382
|
+
* @returns Parsed tagger info with name, email, and date
|
|
1383
|
+
*/
|
|
1384
|
+
function parseTaggerLine(line) {
|
|
1385
|
+
let name = '';
|
|
1386
|
+
let email = '';
|
|
1387
|
+
let date = '';
|
|
1388
|
+
// Find email in angle brackets
|
|
1389
|
+
let emailStart = -1;
|
|
1390
|
+
let emailEnd = -1;
|
|
1391
|
+
for (let i = 0; i < line.length; i++) {
|
|
1392
|
+
if (line[i] === '<') {
|
|
1393
|
+
emailStart = i + 1;
|
|
1394
|
+
}
|
|
1395
|
+
else if (line[i] === '>' && emailStart !== -1) {
|
|
1396
|
+
emailEnd = i;
|
|
1397
|
+
break;
|
|
1398
|
+
}
|
|
1399
|
+
}
|
|
1400
|
+
if (emailStart !== -1 && emailEnd !== -1) {
|
|
1401
|
+
name = line.slice(0, emailStart - 1).trim();
|
|
1402
|
+
email = line.slice(emailStart, emailEnd);
|
|
1403
|
+
// Rest is timestamp and timezone
|
|
1404
|
+
const rest = line.slice(emailEnd + 1).trim();
|
|
1405
|
+
const parts = rest.split(' ');
|
|
1406
|
+
if (parts.length >= 1) {
|
|
1407
|
+
// Convert Unix timestamp to ISO 8601
|
|
1408
|
+
const timestamp = parseInt(parts[0], 10);
|
|
1409
|
+
if (!globalIsNaN(timestamp)) {
|
|
1410
|
+
date = createDate(timestamp * 1000).toISOString();
|
|
1411
|
+
}
|
|
1412
|
+
}
|
|
1413
|
+
}
|
|
1414
|
+
return { name, email, date };
|
|
1415
|
+
}
|
|
1416
|
+
/**
|
|
1417
|
+
* Checks if a tag exists.
|
|
1418
|
+
*
|
|
1419
|
+
* @param name - The tag name to verify
|
|
1420
|
+
* @param options - Configuration for the tag operation
|
|
1421
|
+
* @returns True if tag exists
|
|
1422
|
+
*
|
|
1423
|
+
* @example
|
|
1424
|
+
* if (tagExists('v1.0.0')) { ... }
|
|
1425
|
+
*/
|
|
1426
|
+
function tagExists(name, options = {}) {
|
|
1427
|
+
const opts = { ...DEFAULT_TAG_OPTIONS, ...options };
|
|
1428
|
+
const safeName = escapeGitRef(name);
|
|
1429
|
+
try {
|
|
1430
|
+
node_child_process.execSync(`git rev-parse ${safeName}`, {
|
|
1431
|
+
encoding: 'utf-8',
|
|
1432
|
+
cwd: opts.cwd,
|
|
1433
|
+
timeout: opts.timeout,
|
|
1434
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
1435
|
+
});
|
|
1436
|
+
return true;
|
|
1437
|
+
}
|
|
1438
|
+
catch {
|
|
1439
|
+
return false;
|
|
1440
|
+
}
|
|
1441
|
+
}
|
|
1442
|
+
/**
|
|
1443
|
+
* Gets the latest tag (by creation date).
|
|
1444
|
+
*
|
|
1445
|
+
* @param options - Tag options with optional pattern
|
|
1446
|
+
* @returns Latest GitTag or null
|
|
1447
|
+
*
|
|
1448
|
+
* @example
|
|
1449
|
+
* const latest = getLatestTag()
|
|
1450
|
+
* const latestVersion = getLatestTag({ pattern: 'v' })
|
|
1451
|
+
*/
|
|
1452
|
+
function getLatestTag(options = {}) {
|
|
1453
|
+
const tags = getTags({ ...options, maxCount: 1 });
|
|
1454
|
+
return tags[0] ?? null;
|
|
1455
|
+
}
|
|
1456
|
+
/**
|
|
1457
|
+
* Gets tags that match a package name.
|
|
1458
|
+
*
|
|
1459
|
+
* @param packageName - Package name to match
|
|
1460
|
+
* @param options - Tag options
|
|
1461
|
+
* @returns Array of matching tags
|
|
1462
|
+
*
|
|
1463
|
+
* @example
|
|
1464
|
+
* const tags = getTagsForPackage('@scope/pkg')
|
|
1465
|
+
*/
|
|
1466
|
+
function getTagsForPackage(packageName, options = {}) {
|
|
1467
|
+
// Common patterns: @scope/pkg@version, pkg@version, pkg-vversion
|
|
1468
|
+
const allTags = getTags(options);
|
|
1469
|
+
return allTags.filter((tag) => {
|
|
1470
|
+
// Check if tag starts with package name followed by @ or -v
|
|
1471
|
+
const name = tag.name;
|
|
1472
|
+
// Pattern: package@version
|
|
1473
|
+
if (startsWithPrefix$1(name, packageName + '@')) {
|
|
1474
|
+
return true;
|
|
1475
|
+
}
|
|
1476
|
+
// Pattern: package-v
|
|
1477
|
+
if (startsWithPrefix$1(name, packageName + '-v')) {
|
|
1478
|
+
return true;
|
|
1479
|
+
}
|
|
1480
|
+
return false;
|
|
1481
|
+
});
|
|
1482
|
+
}
|
|
1483
|
+
// ============================================================================
|
|
1484
|
+
// Helper functions
|
|
1485
|
+
// ============================================================================
|
|
1486
|
+
/**
|
|
1487
|
+
* Checks if string starts with prefix (no regex).
|
|
1488
|
+
*
|
|
1489
|
+
* @param str - The string to check
|
|
1490
|
+
* @param prefix - The prefix to look for
|
|
1491
|
+
* @returns True if str starts with the given prefix
|
|
1492
|
+
*/
|
|
1493
|
+
function startsWithPrefix$1(str, prefix) {
|
|
1494
|
+
if (prefix.length > str.length)
|
|
1495
|
+
return false;
|
|
1496
|
+
for (let i = 0; i < prefix.length; i++) {
|
|
1497
|
+
if (str[i] !== prefix[i])
|
|
1498
|
+
return false;
|
|
1499
|
+
}
|
|
1500
|
+
return true;
|
|
1501
|
+
}
|
|
1502
|
+
/**
|
|
1503
|
+
* Maximum tag pattern length.
|
|
1504
|
+
*/
|
|
1505
|
+
const MAX_PATTERN_LENGTH = 256;
|
|
1506
|
+
/**
|
|
1507
|
+
* Escapes a tag pattern for safe use in git commands.
|
|
1508
|
+
*
|
|
1509
|
+
* @param pattern - Pattern to escape
|
|
1510
|
+
* @returns Safe pattern string
|
|
1511
|
+
*/
|
|
1512
|
+
function escapeGitTagPattern(pattern) {
|
|
1513
|
+
if (!pattern || typeof pattern !== 'string') {
|
|
1514
|
+
throw createError('Pattern is required');
|
|
1515
|
+
}
|
|
1516
|
+
if (pattern.length > MAX_PATTERN_LENGTH) {
|
|
1517
|
+
throw createError(`Pattern exceeds maximum length of ${MAX_PATTERN_LENGTH}`);
|
|
1518
|
+
}
|
|
1519
|
+
const safe = [];
|
|
1520
|
+
for (let i = 0; i < pattern.length; i++) {
|
|
1521
|
+
const code = pattern.charCodeAt(i);
|
|
1522
|
+
// Allow: a-z, A-Z, 0-9, /, -, _, ., @
|
|
1523
|
+
if ((code >= 97 && code <= 122) || // a-z
|
|
1524
|
+
(code >= 65 && code <= 90) || // A-Z
|
|
1525
|
+
(code >= 48 && code <= 57) || // 0-9
|
|
1526
|
+
code === 47 || // /
|
|
1527
|
+
code === 45 || // -
|
|
1528
|
+
code === 95 || // _
|
|
1529
|
+
code === 46 || // .
|
|
1530
|
+
code === 64 // @
|
|
1531
|
+
) {
|
|
1532
|
+
safe.push(pattern[i]);
|
|
1533
|
+
}
|
|
1534
|
+
else {
|
|
1535
|
+
throw createError(`Invalid character in pattern at position ${i}: "${pattern[i]}"`);
|
|
1536
|
+
}
|
|
1537
|
+
}
|
|
1538
|
+
return safe.join('');
|
|
1539
|
+
}
|
|
1540
|
+
|
|
1541
|
+
/**
|
|
1542
|
+
* Creates a new tag.
|
|
1543
|
+
*
|
|
1544
|
+
* @param name - The name for the new tag
|
|
1545
|
+
* @param options - Configuration including optional message for annotated tags
|
|
1546
|
+
* @returns Created GitTag
|
|
1547
|
+
*
|
|
1548
|
+
* @example
|
|
1549
|
+
* // Create lightweight tag
|
|
1550
|
+
* const tag = createTag('v1.0.0')
|
|
1551
|
+
*
|
|
1552
|
+
* // Create annotated tag
|
|
1553
|
+
* const tag = createTag('v1.0.0', { message: 'Release v1.0.0' })
|
|
1554
|
+
*/
|
|
1555
|
+
function createTag(name, options = {}) {
|
|
1556
|
+
const opts = { ...DEFAULT_TAG_OPTIONS, ...options };
|
|
1557
|
+
const safeName = escapeGitRef(name);
|
|
1558
|
+
const args = ['tag'];
|
|
1559
|
+
if (opts.force) {
|
|
1560
|
+
args.push('-f');
|
|
1561
|
+
}
|
|
1562
|
+
if (opts.message) {
|
|
1563
|
+
// Annotated tag
|
|
1564
|
+
args.push('-a');
|
|
1565
|
+
args.push(safeName);
|
|
1566
|
+
args.push('-m');
|
|
1567
|
+
args.push(`"${escapeGitMessage(opts.message)}"`);
|
|
1568
|
+
}
|
|
1569
|
+
else {
|
|
1570
|
+
// Lightweight tag
|
|
1571
|
+
args.push(safeName);
|
|
1572
|
+
}
|
|
1573
|
+
if (opts.target) {
|
|
1574
|
+
args.push(escapeGitRef(opts.target));
|
|
1575
|
+
}
|
|
1576
|
+
try {
|
|
1577
|
+
node_child_process.execSync(`git ${args.join(' ')}`, {
|
|
1578
|
+
encoding: 'utf-8',
|
|
1579
|
+
cwd: opts.cwd,
|
|
1580
|
+
timeout: opts.timeout,
|
|
1581
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
1582
|
+
});
|
|
1583
|
+
// Get the created tag
|
|
1584
|
+
const tag = getTag(name, opts);
|
|
1585
|
+
if (!tag) {
|
|
1586
|
+
throw createError(`Failed to retrieve created tag: ${name}`);
|
|
1587
|
+
}
|
|
1588
|
+
return tag;
|
|
1589
|
+
}
|
|
1590
|
+
catch (error) {
|
|
1591
|
+
if (error instanceof Error) {
|
|
1592
|
+
throw createError(`Failed to create tag ${name}: ${error.message}`);
|
|
1593
|
+
}
|
|
1594
|
+
throw error;
|
|
1595
|
+
}
|
|
1596
|
+
}
|
|
1597
|
+
/**
|
|
1598
|
+
* Deletes a tag.
|
|
1599
|
+
*
|
|
1600
|
+
* @param name - The tag name to delete
|
|
1601
|
+
* @param options - Configuration for the tag operation
|
|
1602
|
+
* @returns True if deleted
|
|
1603
|
+
*
|
|
1604
|
+
* @example
|
|
1605
|
+
* const deleted = deleteTag('v1.0.0')
|
|
1606
|
+
*/
|
|
1607
|
+
function deleteTag(name, options = {}) {
|
|
1608
|
+
const opts = { ...DEFAULT_TAG_OPTIONS, ...options };
|
|
1609
|
+
const safeName = escapeGitRef(name);
|
|
1610
|
+
try {
|
|
1611
|
+
node_child_process.execSync(`git tag -d ${safeName}`, {
|
|
1612
|
+
encoding: 'utf-8',
|
|
1613
|
+
cwd: opts.cwd,
|
|
1614
|
+
timeout: opts.timeout,
|
|
1615
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
1616
|
+
});
|
|
1617
|
+
return true;
|
|
1618
|
+
}
|
|
1619
|
+
catch {
|
|
1620
|
+
return false;
|
|
1621
|
+
}
|
|
1622
|
+
}
|
|
1623
|
+
/**
|
|
1624
|
+
* Pushes a tag to a remote.
|
|
1625
|
+
*
|
|
1626
|
+
* @param name - The tag to push to the remote
|
|
1627
|
+
* @param remote - Remote name (defaults to 'origin')
|
|
1628
|
+
* @param options - Configuration for the tag operation
|
|
1629
|
+
* @returns True if pushed successfully
|
|
1630
|
+
*
|
|
1631
|
+
* @example
|
|
1632
|
+
* pushTag('v1.0.0')
|
|
1633
|
+
* pushTag('v1.0.0', 'upstream')
|
|
1634
|
+
*/
|
|
1635
|
+
function pushTag(name, remote = 'origin', options = {}) {
|
|
1636
|
+
const opts = { ...DEFAULT_TAG_OPTIONS, ...options };
|
|
1637
|
+
const safeName = escapeGitRef(name);
|
|
1638
|
+
const safeRemote = escapeGitRef(remote);
|
|
1639
|
+
try {
|
|
1640
|
+
node_child_process.execSync(`git push ${safeRemote} ${safeName}`, {
|
|
1641
|
+
encoding: 'utf-8',
|
|
1642
|
+
cwd: opts.cwd,
|
|
1643
|
+
timeout: opts.timeout * 3, // Allow more time for network
|
|
1644
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
1645
|
+
});
|
|
1646
|
+
return true;
|
|
1647
|
+
}
|
|
1648
|
+
catch {
|
|
1649
|
+
return false;
|
|
1650
|
+
}
|
|
1651
|
+
}
|
|
1652
|
+
// ============================================================================
|
|
1653
|
+
// Security helpers
|
|
1654
|
+
// ============================================================================
|
|
1655
|
+
/**
|
|
1656
|
+
* Maximum message length.
|
|
1657
|
+
*/
|
|
1658
|
+
const MAX_MESSAGE_LENGTH = 10000;
|
|
1659
|
+
/**
|
|
1660
|
+
* Escapes a message for safe use in git commands.
|
|
1661
|
+
*
|
|
1662
|
+
* @param message - Message to escape
|
|
1663
|
+
* @returns Safe message string
|
|
1664
|
+
*/
|
|
1665
|
+
function escapeGitMessage(message) {
|
|
1666
|
+
if (!message || typeof message !== 'string') {
|
|
1667
|
+
throw createError('Message is required');
|
|
1668
|
+
}
|
|
1669
|
+
if (message.length > MAX_MESSAGE_LENGTH) {
|
|
1670
|
+
throw createError(`Message exceeds maximum length of ${MAX_MESSAGE_LENGTH}`);
|
|
1671
|
+
}
|
|
1672
|
+
const safe = [];
|
|
1673
|
+
for (let i = 0; i < message.length; i++) {
|
|
1674
|
+
const char = message[i];
|
|
1675
|
+
const code = message.charCodeAt(i);
|
|
1676
|
+
// Escape double quotes and backslashes
|
|
1677
|
+
if (char === '"' || char === '\\') {
|
|
1678
|
+
safe.push('\\');
|
|
1679
|
+
safe.push(char);
|
|
1680
|
+
}
|
|
1681
|
+
// Allow printable ASCII and common whitespace
|
|
1682
|
+
else if ((code >= 32 && code <= 126) || // Printable ASCII
|
|
1683
|
+
code === 10 || // newline
|
|
1684
|
+
code === 13 || // carriage return
|
|
1685
|
+
code === 9 // tab
|
|
1686
|
+
) {
|
|
1687
|
+
safe.push(char);
|
|
1688
|
+
}
|
|
1689
|
+
// Skip other control characters
|
|
1690
|
+
}
|
|
1691
|
+
return safe.join('');
|
|
1692
|
+
}
|
|
1693
|
+
|
|
1694
|
+
/**
|
|
1695
|
+
* Default commit options.
|
|
1696
|
+
*/
|
|
1697
|
+
const DEFAULT_COMMIT_OPTIONS = {
|
|
1698
|
+
timeout: 30000,
|
|
1699
|
+
};
|
|
1700
|
+
/**
|
|
1701
|
+
* Creates a new commit.
|
|
1702
|
+
*
|
|
1703
|
+
* @param message - Commit message (subject line)
|
|
1704
|
+
* @param options - Create options
|
|
1705
|
+
* @returns Created GitCommit
|
|
1706
|
+
*
|
|
1707
|
+
* @example
|
|
1708
|
+
* const commit = createCommit('feat: add new feature')
|
|
1709
|
+
* const commit = createCommit('fix: resolve bug', { body: 'Detailed description' })
|
|
1710
|
+
*/
|
|
1711
|
+
function commit(message, options = {}) {
|
|
1712
|
+
const opts = { ...DEFAULT_COMMIT_OPTIONS, ...options };
|
|
1713
|
+
// noEdit is only valid when amending - reuse existing commit message
|
|
1714
|
+
const isNoEditAmend = opts.amend && opts.noEdit;
|
|
1715
|
+
if (!isNoEditAmend && (!message || typeof message !== 'string')) {
|
|
1716
|
+
throw createError('Commit message is required');
|
|
1717
|
+
}
|
|
1718
|
+
const args = ['commit'];
|
|
1719
|
+
if (isNoEditAmend) {
|
|
1720
|
+
// Amend without changing the message
|
|
1721
|
+
args.push('--amend', '--no-edit');
|
|
1722
|
+
}
|
|
1723
|
+
else {
|
|
1724
|
+
const safeMessage = escapeGitMessage(message);
|
|
1725
|
+
// Build message with optional body
|
|
1726
|
+
let fullMessage = safeMessage;
|
|
1727
|
+
if (opts.body) {
|
|
1728
|
+
const safeBody = escapeGitMessage(opts.body);
|
|
1729
|
+
fullMessage = `${safeMessage}\n\n${safeBody}`;
|
|
1730
|
+
}
|
|
1731
|
+
args.push('-m', `"${fullMessage}"`);
|
|
1732
|
+
if (opts.amend) {
|
|
1733
|
+
args.push('--amend');
|
|
1734
|
+
}
|
|
1735
|
+
}
|
|
1736
|
+
if (opts.allowEmpty) {
|
|
1737
|
+
args.push('--allow-empty');
|
|
1738
|
+
}
|
|
1739
|
+
if (opts.sign) {
|
|
1740
|
+
args.push('-S');
|
|
1741
|
+
}
|
|
1742
|
+
if (opts.noVerify) {
|
|
1743
|
+
args.push('--no-verify');
|
|
1744
|
+
}
|
|
1745
|
+
if (opts.author) {
|
|
1746
|
+
const safeAuthor = escapeAuthor(opts.author);
|
|
1747
|
+
args.push(`--author="${safeAuthor}"`);
|
|
1748
|
+
}
|
|
1749
|
+
// Add specific files if provided
|
|
1750
|
+
if (opts.files && opts.files.length > 0) {
|
|
1751
|
+
args.push('--');
|
|
1752
|
+
for (const file of opts.files) {
|
|
1753
|
+
args.push(escapeFilePath(file));
|
|
1754
|
+
}
|
|
1755
|
+
}
|
|
1756
|
+
try {
|
|
1757
|
+
node_child_process.execSync(`git ${args.join(' ')}`, {
|
|
1758
|
+
encoding: 'utf-8',
|
|
1759
|
+
cwd: opts.cwd,
|
|
1760
|
+
timeout: opts.timeout,
|
|
1761
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
1762
|
+
});
|
|
1763
|
+
// Get the created commit
|
|
1764
|
+
const commit = getCommit('HEAD', opts);
|
|
1765
|
+
if (!commit) {
|
|
1766
|
+
throw createError('Failed to retrieve created commit');
|
|
1767
|
+
}
|
|
1768
|
+
return commit;
|
|
1769
|
+
}
|
|
1770
|
+
catch (error) {
|
|
1771
|
+
if (error instanceof Error) {
|
|
1772
|
+
throw createError(`Failed to create commit: ${error.message}`);
|
|
1773
|
+
}
|
|
1774
|
+
throw error;
|
|
1775
|
+
}
|
|
1776
|
+
}
|
|
1777
|
+
/**
|
|
1778
|
+
* Amends the last commit with new message.
|
|
1779
|
+
*
|
|
1780
|
+
* @param message - The new commit message to use
|
|
1781
|
+
* @param options - Configuration for the commit operation
|
|
1782
|
+
* @returns GitCommit object representing the amended commit
|
|
1783
|
+
*
|
|
1784
|
+
* @example
|
|
1785
|
+
* const commit = amendCommit('feat: improved feature')
|
|
1786
|
+
*/
|
|
1787
|
+
function amendCommit(message, options = {}) {
|
|
1788
|
+
return commit(message, { ...options, amend: true });
|
|
1789
|
+
}
|
|
1790
|
+
/**
|
|
1791
|
+
* Amends the last commit without changing the message.
|
|
1792
|
+
* Useful for adding staged changes to the previous commit.
|
|
1793
|
+
*
|
|
1794
|
+
* @param options - Configuration for the commit operation
|
|
1795
|
+
* @returns GitCommit object representing the amended commit
|
|
1796
|
+
*
|
|
1797
|
+
* @example
|
|
1798
|
+
* stage(['extra-file.ts'])
|
|
1799
|
+
* amendCommitNoEdit() // adds staged files to last commit
|
|
1800
|
+
*/
|
|
1801
|
+
function amendCommitNoEdit(options = {}) {
|
|
1802
|
+
return commit('', { ...options, amend: true, noEdit: true });
|
|
1803
|
+
}
|
|
1804
|
+
/**
|
|
1805
|
+
* Creates an empty commit (useful for CI triggers).
|
|
1806
|
+
*
|
|
1807
|
+
* @param message - Text for the empty commit
|
|
1808
|
+
* @param options - Configuration for the commit operation
|
|
1809
|
+
* @returns GitCommit object representing the new empty commit
|
|
1810
|
+
*
|
|
1811
|
+
* @example
|
|
1812
|
+
* const commit = createEmptyCommit('chore: trigger CI')
|
|
1813
|
+
*/
|
|
1814
|
+
function createEmptyCommit(message, options = {}) {
|
|
1815
|
+
return commit(message, { ...options, allowEmpty: true });
|
|
1816
|
+
}
|
|
1817
|
+
// ============================================================================
|
|
1818
|
+
// Security helpers - character-by-character validation (no regex)
|
|
1819
|
+
// ============================================================================
|
|
1820
|
+
/**
|
|
1821
|
+
* Maximum file path length.
|
|
1822
|
+
*/
|
|
1823
|
+
const MAX_PATH_LENGTH = 4096;
|
|
1824
|
+
/**
|
|
1825
|
+
* Escapes a file path for safe use in git commands.
|
|
1826
|
+
*
|
|
1827
|
+
* @param path - Path to escape
|
|
1828
|
+
* @returns Safe path string
|
|
1829
|
+
*/
|
|
1830
|
+
function escapeFilePath(path) {
|
|
1831
|
+
if (!path || typeof path !== 'string') {
|
|
1832
|
+
throw createError('File path is required');
|
|
1833
|
+
}
|
|
1834
|
+
if (path.length > MAX_PATH_LENGTH) {
|
|
1835
|
+
throw createError(`Path exceeds maximum length of ${MAX_PATH_LENGTH}`);
|
|
1836
|
+
}
|
|
1837
|
+
const safe = [];
|
|
1838
|
+
for (let i = 0; i < path.length; i++) {
|
|
1839
|
+
const code = path.charCodeAt(i);
|
|
1840
|
+
// Allow: a-z, A-Z, 0-9, /, \, -, _, ., space
|
|
1841
|
+
if ((code >= 97 && code <= 122) || // a-z
|
|
1842
|
+
(code >= 65 && code <= 90) || // A-Z
|
|
1843
|
+
(code >= 48 && code <= 57) || // 0-9
|
|
1844
|
+
code === 47 || // /
|
|
1845
|
+
code === 92 || // \
|
|
1846
|
+
code === 45 || // -
|
|
1847
|
+
code === 95 || // _
|
|
1848
|
+
code === 46 || // .
|
|
1849
|
+
code === 32 // space
|
|
1850
|
+
) {
|
|
1851
|
+
safe.push(path[i]);
|
|
1852
|
+
}
|
|
1853
|
+
else {
|
|
1854
|
+
throw createError(`Invalid character in path at position ${i}: "${path[i]}"`);
|
|
1855
|
+
}
|
|
1856
|
+
}
|
|
1857
|
+
return safe.join('');
|
|
1858
|
+
}
|
|
1859
|
+
/**
|
|
1860
|
+
* Maximum author length.
|
|
1861
|
+
*/
|
|
1862
|
+
const MAX_AUTHOR_LENGTH = 500;
|
|
1863
|
+
/**
|
|
1864
|
+
* Escapes an author string for safe use in git commands.
|
|
1865
|
+
* Format: "Name <email>"
|
|
1866
|
+
*
|
|
1867
|
+
* @param author - Author to escape
|
|
1868
|
+
* @returns Safe author string
|
|
1869
|
+
*/
|
|
1870
|
+
function escapeAuthor(author) {
|
|
1871
|
+
if (!author || typeof author !== 'string') {
|
|
1872
|
+
throw createError('Author is required');
|
|
1873
|
+
}
|
|
1874
|
+
if (author.length > MAX_AUTHOR_LENGTH) {
|
|
1875
|
+
throw createError(`Author exceeds maximum length of ${MAX_AUTHOR_LENGTH}`);
|
|
1876
|
+
}
|
|
1877
|
+
const safe = [];
|
|
1878
|
+
for (let i = 0; i < author.length; i++) {
|
|
1879
|
+
const code = author.charCodeAt(i);
|
|
1880
|
+
// Allow: a-z, A-Z, 0-9, space, @, ., -, _, <, >
|
|
1881
|
+
if ((code >= 97 && code <= 122) || // a-z
|
|
1882
|
+
(code >= 65 && code <= 90) || // A-Z
|
|
1883
|
+
(code >= 48 && code <= 57) || // 0-9
|
|
1884
|
+
code === 32 || // space
|
|
1885
|
+
code === 64 || // @
|
|
1886
|
+
code === 46 || // .
|
|
1887
|
+
code === 45 || // -
|
|
1888
|
+
code === 95 || // _
|
|
1889
|
+
code === 60 || // <
|
|
1890
|
+
code === 62 // >
|
|
1891
|
+
) {
|
|
1892
|
+
safe.push(author[i]);
|
|
1893
|
+
}
|
|
1894
|
+
else {
|
|
1895
|
+
throw createError(`Invalid character in author at position ${i}: "${author[i]}"`);
|
|
1896
|
+
}
|
|
1897
|
+
}
|
|
1898
|
+
return safe.join('');
|
|
1899
|
+
}
|
|
1900
|
+
|
|
1901
|
+
/**
|
|
1902
|
+
* Stages files for commit.
|
|
1903
|
+
*
|
|
1904
|
+
* @param files - Array of file paths relative to working directory
|
|
1905
|
+
* @param options - Configuration for the staging operation
|
|
1906
|
+
* @returns True if staging succeeded
|
|
1907
|
+
*
|
|
1908
|
+
* @example
|
|
1909
|
+
* stage(['package.json', 'CHANGELOG.md'])
|
|
1910
|
+
* stage(['.'], { all: true })
|
|
1911
|
+
*/
|
|
1912
|
+
function stage(files, options = {}) {
|
|
1913
|
+
const opts = { ...DEFAULT_COMMIT_OPTIONS, ...options };
|
|
1914
|
+
const args = ['add'];
|
|
1915
|
+
if (opts.all) {
|
|
1916
|
+
args.push('-A');
|
|
1917
|
+
}
|
|
1918
|
+
else if (opts.update) {
|
|
1919
|
+
args.push('-u');
|
|
1920
|
+
}
|
|
1921
|
+
if (opts.force) {
|
|
1922
|
+
args.push('-f');
|
|
1923
|
+
}
|
|
1924
|
+
// Add files
|
|
1925
|
+
for (const file of files) {
|
|
1926
|
+
args.push(escapeFilePath(file));
|
|
1927
|
+
}
|
|
1928
|
+
try {
|
|
1929
|
+
node_child_process.execSync(`git ${args.join(' ')}`, {
|
|
1930
|
+
encoding: 'utf-8',
|
|
1931
|
+
cwd: opts.cwd,
|
|
1932
|
+
timeout: opts.timeout,
|
|
1933
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
1934
|
+
});
|
|
1935
|
+
return true;
|
|
1936
|
+
}
|
|
1937
|
+
catch {
|
|
1938
|
+
return false;
|
|
1939
|
+
}
|
|
1940
|
+
}
|
|
1941
|
+
/**
|
|
1942
|
+
* Unstages files.
|
|
1943
|
+
*
|
|
1944
|
+
* @param files - Array of file paths to remove from staging area
|
|
1945
|
+
* @param options - Configuration for the unstage operation
|
|
1946
|
+
* @returns True if unstaging succeeded
|
|
1947
|
+
*
|
|
1948
|
+
* @example
|
|
1949
|
+
* unstage(['package.json'])
|
|
1950
|
+
*/
|
|
1951
|
+
function unstage(files, options = {}) {
|
|
1952
|
+
const opts = { ...DEFAULT_COMMIT_OPTIONS, ...options };
|
|
1953
|
+
const args = ['reset', 'HEAD', '--'];
|
|
1954
|
+
for (const file of files) {
|
|
1955
|
+
args.push(escapeFilePath(file));
|
|
1956
|
+
}
|
|
1957
|
+
try {
|
|
1958
|
+
node_child_process.execSync(`git ${args.join(' ')}`, {
|
|
1959
|
+
encoding: 'utf-8',
|
|
1960
|
+
cwd: opts.cwd,
|
|
1961
|
+
timeout: opts.timeout,
|
|
1962
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
1963
|
+
});
|
|
1964
|
+
return true;
|
|
1965
|
+
}
|
|
1966
|
+
catch {
|
|
1967
|
+
return false;
|
|
1968
|
+
}
|
|
1969
|
+
}
|
|
1970
|
+
/**
|
|
1971
|
+
* Stages all changes (tracked and untracked).
|
|
1972
|
+
*
|
|
1973
|
+
* @param options - Configuration for the staging operation
|
|
1974
|
+
* @returns True if all changes were successfully added to the index
|
|
1975
|
+
*
|
|
1976
|
+
* @example
|
|
1977
|
+
* stageAll() // stages all tracked and untracked changes
|
|
1978
|
+
*/
|
|
1979
|
+
function stageAll(options = {}) {
|
|
1980
|
+
return stage(['.'], { ...options, all: true });
|
|
1981
|
+
}
|
|
1982
|
+
/**
|
|
1983
|
+
* Checks if there are staged changes.
|
|
1984
|
+
*
|
|
1985
|
+
* @param options - Configuration for the operation
|
|
1986
|
+
* @returns True if there are staged changes ready to commit
|
|
1987
|
+
*
|
|
1988
|
+
* @example
|
|
1989
|
+
* if (hasStagedChanges()) { createCommit('...') }
|
|
1990
|
+
*/
|
|
1991
|
+
function hasStagedChanges(options = {}) {
|
|
1992
|
+
const opts = { ...DEFAULT_COMMIT_OPTIONS, ...options };
|
|
1993
|
+
try {
|
|
1994
|
+
node_child_process.execSync('git diff --cached --quiet', {
|
|
1995
|
+
encoding: 'utf-8',
|
|
1996
|
+
cwd: opts.cwd,
|
|
1997
|
+
timeout: opts.timeout,
|
|
1998
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
1999
|
+
});
|
|
2000
|
+
// Exit code 0 means no changes
|
|
2001
|
+
return false;
|
|
2002
|
+
}
|
|
2003
|
+
catch {
|
|
2004
|
+
// Exit code 1 means there are changes
|
|
2005
|
+
return true;
|
|
2006
|
+
}
|
|
2007
|
+
}
|
|
2008
|
+
/**
|
|
2009
|
+
* Checks if there are unstaged changes (working tree dirty).
|
|
2010
|
+
*
|
|
2011
|
+
* @param options - Configuration for the operation
|
|
2012
|
+
* @returns True if there are unstaged changes in the working tree
|
|
2013
|
+
*
|
|
2014
|
+
* @example
|
|
2015
|
+
* if (hasUnstagedChanges()) { stage(['.']) }
|
|
2016
|
+
*/
|
|
2017
|
+
function hasUnstagedChanges(options = {}) {
|
|
2018
|
+
const opts = { ...DEFAULT_COMMIT_OPTIONS, ...options };
|
|
2019
|
+
try {
|
|
2020
|
+
node_child_process.execSync('git diff --quiet', {
|
|
2021
|
+
encoding: 'utf-8',
|
|
2022
|
+
cwd: opts.cwd,
|
|
2023
|
+
timeout: opts.timeout,
|
|
2024
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
2025
|
+
});
|
|
2026
|
+
// Exit code 0 means no changes
|
|
2027
|
+
return false;
|
|
2028
|
+
}
|
|
2029
|
+
catch {
|
|
2030
|
+
// Exit code 1 means there are changes
|
|
2031
|
+
return true;
|
|
2032
|
+
}
|
|
2033
|
+
}
|
|
2034
|
+
|
|
2035
|
+
/**
|
|
2036
|
+
* Gets the current HEAD commit hash.
|
|
2037
|
+
*
|
|
2038
|
+
* @param options - Git operation configuration
|
|
2039
|
+
* @returns HEAD commit hash or null
|
|
2040
|
+
*
|
|
2041
|
+
* @example
|
|
2042
|
+
* const head = getHead()
|
|
2043
|
+
*/
|
|
2044
|
+
function getHead(options = {}) {
|
|
2045
|
+
const opts = { ...DEFAULT_COMMIT_OPTIONS, ...options };
|
|
2046
|
+
try {
|
|
2047
|
+
return node_child_process.execSync('git rev-parse HEAD', {
|
|
2048
|
+
encoding: 'utf-8',
|
|
2049
|
+
cwd: opts.cwd,
|
|
2050
|
+
timeout: opts.timeout,
|
|
2051
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
2052
|
+
}).trim();
|
|
2053
|
+
}
|
|
2054
|
+
catch {
|
|
2055
|
+
return null;
|
|
2056
|
+
}
|
|
2057
|
+
}
|
|
2058
|
+
/**
|
|
2059
|
+
* Gets the current branch name.
|
|
2060
|
+
*
|
|
2061
|
+
* @param options - Configuration for the operation
|
|
2062
|
+
* @returns Branch name or null if detached
|
|
2063
|
+
*
|
|
2064
|
+
* @example
|
|
2065
|
+
* const branch = getCurrentBranch()
|
|
2066
|
+
*/
|
|
2067
|
+
function getCurrentBranch(options = {}) {
|
|
2068
|
+
const opts = { ...DEFAULT_COMMIT_OPTIONS, ...options };
|
|
2069
|
+
try {
|
|
2070
|
+
const result = node_child_process.execSync('git symbolic-ref --short HEAD', {
|
|
2071
|
+
encoding: 'utf-8',
|
|
2072
|
+
cwd: opts.cwd,
|
|
2073
|
+
timeout: opts.timeout,
|
|
2074
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
2075
|
+
}).trim();
|
|
2076
|
+
return result || null;
|
|
2077
|
+
}
|
|
2078
|
+
catch {
|
|
2079
|
+
// Detached HEAD or not a git repo
|
|
2080
|
+
return null;
|
|
2081
|
+
}
|
|
2082
|
+
}
|
|
2083
|
+
/**
|
|
2084
|
+
* Checks if there are untracked files.
|
|
2085
|
+
*
|
|
2086
|
+
* @param options - Configuration for the operation
|
|
2087
|
+
* @returns True if there are untracked files in the working directory
|
|
2088
|
+
*/
|
|
2089
|
+
function hasUntrackedFiles(options = {}) {
|
|
2090
|
+
const opts = { ...DEFAULT_COMMIT_OPTIONS, ...options };
|
|
2091
|
+
try {
|
|
2092
|
+
const result = node_child_process.execSync('git ls-files --others --exclude-standard', {
|
|
2093
|
+
encoding: 'utf-8',
|
|
2094
|
+
cwd: opts.cwd,
|
|
2095
|
+
timeout: opts.timeout,
|
|
2096
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
2097
|
+
}).trim();
|
|
2098
|
+
return result.length > 0;
|
|
2099
|
+
}
|
|
2100
|
+
catch {
|
|
2101
|
+
return false;
|
|
2102
|
+
}
|
|
2103
|
+
}
|
|
2104
|
+
|
|
2105
|
+
/**
|
|
2106
|
+
* Default status options.
|
|
2107
|
+
*/
|
|
2108
|
+
const DEFAULT_STATUS_OPTIONS = {
|
|
2109
|
+
timeout: 10000,
|
|
2110
|
+
};
|
|
2111
|
+
/**
|
|
2112
|
+
* Gets the full repository status.
|
|
2113
|
+
*
|
|
2114
|
+
* @param options - Configuration for the status query
|
|
2115
|
+
* @returns Comprehensive repository status information
|
|
2116
|
+
*
|
|
2117
|
+
* @example
|
|
2118
|
+
* const status = getStatus()
|
|
2119
|
+
* if (!status.clean) {
|
|
2120
|
+
* console.log('Working tree has changes')
|
|
2121
|
+
* }
|
|
2122
|
+
*/
|
|
2123
|
+
function getStatus(options = {}) {
|
|
2124
|
+
const opts = { ...DEFAULT_STATUS_OPTIONS, ...options };
|
|
2125
|
+
// Get porcelain status with branch info
|
|
2126
|
+
const output = node_child_process.execSync('git status --porcelain=v2 --branch', {
|
|
2127
|
+
encoding: 'utf-8',
|
|
2128
|
+
cwd: opts.cwd,
|
|
2129
|
+
timeout: opts.timeout,
|
|
2130
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
2131
|
+
});
|
|
2132
|
+
return parseStatus(output);
|
|
2133
|
+
}
|
|
2134
|
+
/**
|
|
2135
|
+
* Parses git status porcelain v2 output.
|
|
2136
|
+
*
|
|
2137
|
+
* @param output - Raw status output
|
|
2138
|
+
* @returns Parsed status
|
|
2139
|
+
*/
|
|
2140
|
+
function parseStatus(output) {
|
|
2141
|
+
const lines = output.split('\n');
|
|
2142
|
+
let branch = null;
|
|
2143
|
+
let detached = false;
|
|
2144
|
+
let upstream;
|
|
2145
|
+
let ahead = 0;
|
|
2146
|
+
let behind = 0;
|
|
2147
|
+
const staged = [];
|
|
2148
|
+
const modified = [];
|
|
2149
|
+
const untracked = [];
|
|
2150
|
+
let hasConflicts = false;
|
|
2151
|
+
for (const line of lines) {
|
|
2152
|
+
if (!line)
|
|
2153
|
+
continue;
|
|
2154
|
+
// Branch headers
|
|
2155
|
+
if (startsWithPrefix(line, '# branch.head ')) {
|
|
2156
|
+
const branchName = line.slice(14);
|
|
2157
|
+
if (branchName === '(detached)') {
|
|
2158
|
+
detached = true;
|
|
2159
|
+
}
|
|
2160
|
+
else {
|
|
2161
|
+
branch = branchName;
|
|
2162
|
+
}
|
|
2163
|
+
}
|
|
2164
|
+
else if (startsWithPrefix(line, '# branch.upstream ')) {
|
|
2165
|
+
upstream = line.slice(18);
|
|
2166
|
+
}
|
|
2167
|
+
else if (startsWithPrefix(line, '# branch.ab ')) {
|
|
2168
|
+
const ab = parseAheadBehind(line.slice(12));
|
|
2169
|
+
ahead = ab.ahead;
|
|
2170
|
+
behind = ab.behind;
|
|
2171
|
+
}
|
|
2172
|
+
// Changed entries (ordinary changed)
|
|
2173
|
+
else if (line[0] === '1') {
|
|
2174
|
+
const entry = parseChangedEntry(line);
|
|
2175
|
+
if (entry) {
|
|
2176
|
+
if (entry.indexStatus) {
|
|
2177
|
+
staged.push(entry);
|
|
2178
|
+
}
|
|
2179
|
+
if (entry.workTreeStatus && entry.workTreeStatus !== 'untracked') {
|
|
2180
|
+
modified.push(entry);
|
|
2181
|
+
}
|
|
2182
|
+
}
|
|
2183
|
+
}
|
|
2184
|
+
// Renamed/copied entries
|
|
2185
|
+
else if (line[0] === '2') {
|
|
2186
|
+
const entry = parseRenamedEntry(line);
|
|
2187
|
+
if (entry) {
|
|
2188
|
+
if (entry.indexStatus) {
|
|
2189
|
+
staged.push(entry);
|
|
2190
|
+
}
|
|
2191
|
+
if (entry.workTreeStatus) {
|
|
2192
|
+
modified.push(entry);
|
|
2193
|
+
}
|
|
2194
|
+
}
|
|
2195
|
+
}
|
|
2196
|
+
// Unmerged entries
|
|
2197
|
+
else if (line[0] === 'u') {
|
|
2198
|
+
hasConflicts = true;
|
|
2199
|
+
const entry = parseUnmergedEntry(line);
|
|
2200
|
+
if (entry) {
|
|
2201
|
+
staged.push(entry);
|
|
2202
|
+
}
|
|
2203
|
+
}
|
|
2204
|
+
// Untracked entries
|
|
2205
|
+
else if (line[0] === '?') {
|
|
2206
|
+
const path = line.slice(2);
|
|
2207
|
+
untracked.push(path);
|
|
2208
|
+
}
|
|
2209
|
+
}
|
|
2210
|
+
const clean = staged.length === 0 && modified.length === 0 && untracked.length === 0 && !hasConflicts;
|
|
2211
|
+
return {
|
|
2212
|
+
branch,
|
|
2213
|
+
detached,
|
|
2214
|
+
upstream,
|
|
2215
|
+
ahead,
|
|
2216
|
+
behind,
|
|
2217
|
+
staged,
|
|
2218
|
+
modified,
|
|
2219
|
+
untracked,
|
|
2220
|
+
clean,
|
|
2221
|
+
hasConflicts,
|
|
2222
|
+
};
|
|
2223
|
+
}
|
|
2224
|
+
/**
|
|
2225
|
+
* Parses ahead/behind string.
|
|
2226
|
+
*
|
|
2227
|
+
* @param str - String like "+5 -2"
|
|
2228
|
+
* @returns Parsed values
|
|
2229
|
+
*/
|
|
2230
|
+
function parseAheadBehind(str) {
|
|
2231
|
+
let ahead = 0;
|
|
2232
|
+
let behind = 0;
|
|
2233
|
+
const parts = str.split(' ');
|
|
2234
|
+
for (const part of parts) {
|
|
2235
|
+
if (part[0] === '+') {
|
|
2236
|
+
ahead = parseInt(part.slice(1), 10) || 0;
|
|
2237
|
+
}
|
|
2238
|
+
else if (part[0] === '-') {
|
|
2239
|
+
behind = parseInt(part.slice(1), 10) || 0;
|
|
2240
|
+
}
|
|
2241
|
+
}
|
|
2242
|
+
return { ahead, behind };
|
|
2243
|
+
}
|
|
2244
|
+
/**
|
|
2245
|
+
* Parses a changed entry line.
|
|
2246
|
+
*
|
|
2247
|
+
* @param line - Status line starting with '1'
|
|
2248
|
+
* @returns Parsed entry or null
|
|
2249
|
+
*/
|
|
2250
|
+
function parseChangedEntry(line) {
|
|
2251
|
+
// Format: 1 <XY> <sub> <mH> <mI> <mW> <hH> <hI> <path>
|
|
2252
|
+
const parts = line.split(' ');
|
|
2253
|
+
if (parts.length < 9)
|
|
2254
|
+
return null;
|
|
2255
|
+
const xy = parts[1];
|
|
2256
|
+
const path = parts.slice(8).join(' ');
|
|
2257
|
+
const indexStatus = statusFromChar(xy[0]);
|
|
2258
|
+
const workTreeStatus = statusFromChar(xy[1]);
|
|
2259
|
+
return {
|
|
2260
|
+
path,
|
|
2261
|
+
indexStatus,
|
|
2262
|
+
workTreeStatus,
|
|
2263
|
+
};
|
|
2264
|
+
}
|
|
2265
|
+
/**
|
|
2266
|
+
* Parses a renamed entry line.
|
|
2267
|
+
*
|
|
2268
|
+
* @param line - Status line starting with '2'
|
|
2269
|
+
* @returns Parsed entry or null
|
|
2270
|
+
*/
|
|
2271
|
+
function parseRenamedEntry(line) {
|
|
2272
|
+
// Format: 2 <XY> <sub> <mH> <mI> <mW> <hH> <hI> <X><score> <path><tab><origPath>
|
|
2273
|
+
const parts = line.split(' ');
|
|
2274
|
+
if (parts.length < 10)
|
|
2275
|
+
return null;
|
|
2276
|
+
const xy = parts[1];
|
|
2277
|
+
const pathPart = parts.slice(9).join(' ');
|
|
2278
|
+
// Split by tab
|
|
2279
|
+
const tabIndex = pathPart.indexOf('\t');
|
|
2280
|
+
const path = tabIndex >= 0 ? pathPart.slice(0, tabIndex) : pathPart;
|
|
2281
|
+
const origPath = tabIndex >= 0 ? pathPart.slice(tabIndex + 1) : undefined;
|
|
2282
|
+
const indexStatus = statusFromChar(xy[0]);
|
|
2283
|
+
const workTreeStatus = statusFromChar(xy[1]);
|
|
2284
|
+
return {
|
|
2285
|
+
path,
|
|
2286
|
+
indexStatus,
|
|
2287
|
+
workTreeStatus,
|
|
2288
|
+
origPath,
|
|
2289
|
+
};
|
|
2290
|
+
}
|
|
2291
|
+
/**
|
|
2292
|
+
* Parses an unmerged entry line.
|
|
2293
|
+
*
|
|
2294
|
+
* @param line - Status line starting with 'u'
|
|
2295
|
+
* @returns Parsed entry or null
|
|
2296
|
+
*/
|
|
2297
|
+
function parseUnmergedEntry(line) {
|
|
2298
|
+
// Format: u <XY> <sub> <m1> <m2> <m3> <mW> <h1> <h2> <h3> <path>
|
|
2299
|
+
const parts = line.split(' ');
|
|
2300
|
+
if (parts.length < 11)
|
|
2301
|
+
return null;
|
|
2302
|
+
const path = parts.slice(10).join(' ');
|
|
2303
|
+
return {
|
|
2304
|
+
path,
|
|
2305
|
+
indexStatus: 'unmerged',
|
|
2306
|
+
workTreeStatus: 'unmerged',
|
|
2307
|
+
};
|
|
2308
|
+
}
|
|
2309
|
+
/**
|
|
2310
|
+
* Converts status character to FileStatus.
|
|
2311
|
+
*
|
|
2312
|
+
* @param char - Status character
|
|
2313
|
+
* @returns FileStatus or null
|
|
2314
|
+
*/
|
|
2315
|
+
function statusFromChar(char) {
|
|
2316
|
+
switch (char) {
|
|
2317
|
+
case 'M':
|
|
2318
|
+
return 'modified';
|
|
2319
|
+
case 'T':
|
|
2320
|
+
return 'modified'; // Type change
|
|
2321
|
+
case 'A':
|
|
2322
|
+
return 'added';
|
|
2323
|
+
case 'D':
|
|
2324
|
+
return 'deleted';
|
|
2325
|
+
case 'R':
|
|
2326
|
+
return 'renamed';
|
|
2327
|
+
case 'C':
|
|
2328
|
+
return 'copied';
|
|
2329
|
+
case 'U':
|
|
2330
|
+
return 'unmerged';
|
|
2331
|
+
case '?':
|
|
2332
|
+
return 'untracked';
|
|
2333
|
+
case '!':
|
|
2334
|
+
return 'ignored';
|
|
2335
|
+
case '.':
|
|
2336
|
+
return null;
|
|
2337
|
+
default:
|
|
2338
|
+
return null;
|
|
2339
|
+
}
|
|
2340
|
+
}
|
|
2341
|
+
/**
|
|
2342
|
+
* Checks if string starts with prefix (no regex).
|
|
2343
|
+
*
|
|
2344
|
+
* @param str - The string to check
|
|
2345
|
+
* @param prefix - The prefix to look for
|
|
2346
|
+
* @returns True if str starts with the given prefix
|
|
2347
|
+
*/
|
|
2348
|
+
function startsWithPrefix(str, prefix) {
|
|
2349
|
+
if (prefix.length > str.length)
|
|
2350
|
+
return false;
|
|
2351
|
+
for (let i = 0; i < prefix.length; i++) {
|
|
2352
|
+
if (str[i] !== prefix[i])
|
|
2353
|
+
return false;
|
|
2354
|
+
}
|
|
2355
|
+
return true;
|
|
2356
|
+
}
|
|
2357
|
+
/**
|
|
2358
|
+
* Checks if the working tree is clean (no changes).
|
|
2359
|
+
*
|
|
2360
|
+
* @param options - Configuration for the status check
|
|
2361
|
+
* @returns True if working tree is clean with no uncommitted changes
|
|
2362
|
+
*
|
|
2363
|
+
* @example
|
|
2364
|
+
* if (!isClean()) {
|
|
2365
|
+
* throw new Error('Working tree has changes')
|
|
2366
|
+
* }
|
|
2367
|
+
*/
|
|
2368
|
+
function isClean(options = {}) {
|
|
2369
|
+
const status = getStatus(options);
|
|
2370
|
+
return status.clean;
|
|
2371
|
+
}
|
|
2372
|
+
/**
|
|
2373
|
+
* Checks if the directory is a git repository.
|
|
2374
|
+
*
|
|
2375
|
+
* @param options - Status options
|
|
2376
|
+
* @returns True if in a git repository
|
|
2377
|
+
*
|
|
2378
|
+
* @example
|
|
2379
|
+
* if (!isGitRepository()) {
|
|
2380
|
+
* throw new Error('Not a git repository')
|
|
2381
|
+
* }
|
|
2382
|
+
*/
|
|
2383
|
+
function isGitRepository(options = {}) {
|
|
2384
|
+
const opts = { ...DEFAULT_STATUS_OPTIONS, ...options };
|
|
2385
|
+
try {
|
|
2386
|
+
node_child_process.execSync('git rev-parse --is-inside-work-tree', {
|
|
2387
|
+
encoding: 'utf-8',
|
|
2388
|
+
cwd: opts.cwd,
|
|
2389
|
+
timeout: opts.timeout,
|
|
2390
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
2391
|
+
});
|
|
2392
|
+
return true;
|
|
2393
|
+
}
|
|
2394
|
+
catch {
|
|
2395
|
+
return false;
|
|
2396
|
+
}
|
|
2397
|
+
}
|
|
2398
|
+
/**
|
|
2399
|
+
* Gets the repository root directory.
|
|
2400
|
+
*
|
|
2401
|
+
* @param options - Status options
|
|
2402
|
+
* @returns Root directory path or null
|
|
2403
|
+
*
|
|
2404
|
+
* @example
|
|
2405
|
+
* const root = getRepositoryRoot()
|
|
2406
|
+
*/
|
|
2407
|
+
function getRepositoryRoot(options = {}) {
|
|
2408
|
+
const opts = { ...DEFAULT_STATUS_OPTIONS, ...options };
|
|
2409
|
+
try {
|
|
2410
|
+
return node_child_process.execSync('git rev-parse --show-toplevel', {
|
|
2411
|
+
encoding: 'utf-8',
|
|
2412
|
+
cwd: opts.cwd,
|
|
2413
|
+
timeout: opts.timeout,
|
|
2414
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
2415
|
+
}).trim();
|
|
2416
|
+
}
|
|
2417
|
+
catch {
|
|
2418
|
+
return null;
|
|
2419
|
+
}
|
|
2420
|
+
}
|
|
2421
|
+
/**
|
|
2422
|
+
* Gets the current commit hash (HEAD).
|
|
2423
|
+
*
|
|
2424
|
+
* @param options - Status options
|
|
2425
|
+
* @returns Commit hash or null
|
|
2426
|
+
*/
|
|
2427
|
+
function getHeadHash(options = {}) {
|
|
2428
|
+
const opts = { ...DEFAULT_STATUS_OPTIONS, ...options };
|
|
2429
|
+
try {
|
|
2430
|
+
return node_child_process.execSync('git rev-parse HEAD', {
|
|
2431
|
+
encoding: 'utf-8',
|
|
2432
|
+
cwd: opts.cwd,
|
|
2433
|
+
timeout: opts.timeout,
|
|
2434
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
2435
|
+
}).trim();
|
|
2436
|
+
}
|
|
2437
|
+
catch {
|
|
2438
|
+
return null;
|
|
2439
|
+
}
|
|
2440
|
+
}
|
|
2441
|
+
/**
|
|
2442
|
+
* Gets the short current commit hash.
|
|
2443
|
+
*
|
|
2444
|
+
* @param options - Status options
|
|
2445
|
+
* @returns Short hash or null
|
|
2446
|
+
*/
|
|
2447
|
+
function getHeadShortHash(options = {}) {
|
|
2448
|
+
const opts = { ...DEFAULT_STATUS_OPTIONS, ...options };
|
|
2449
|
+
try {
|
|
2450
|
+
return node_child_process.execSync('git rev-parse --short HEAD', {
|
|
2451
|
+
encoding: 'utf-8',
|
|
2452
|
+
cwd: opts.cwd,
|
|
2453
|
+
timeout: opts.timeout,
|
|
2454
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
2455
|
+
}).trim();
|
|
2456
|
+
}
|
|
2457
|
+
catch {
|
|
2458
|
+
return null;
|
|
2459
|
+
}
|
|
2460
|
+
}
|
|
2461
|
+
/**
|
|
2462
|
+
* Checks if there are merge conflicts.
|
|
2463
|
+
*
|
|
2464
|
+
* @param options - Status options
|
|
2465
|
+
* @returns True if there are conflicts
|
|
2466
|
+
*/
|
|
2467
|
+
function hasConflicts(options = {}) {
|
|
2468
|
+
const status = getStatus(options);
|
|
2469
|
+
return status.hasConflicts;
|
|
2470
|
+
}
|
|
2471
|
+
/**
|
|
2472
|
+
* Gets the number of commits ahead of upstream.
|
|
2473
|
+
*
|
|
2474
|
+
* @param options - Status options
|
|
2475
|
+
* @returns Number of commits ahead
|
|
2476
|
+
*/
|
|
2477
|
+
function getAheadCount(options = {}) {
|
|
2478
|
+
const status = getStatus(options);
|
|
2479
|
+
return status.ahead;
|
|
2480
|
+
}
|
|
2481
|
+
/**
|
|
2482
|
+
* Gets the number of commits behind upstream.
|
|
2483
|
+
*
|
|
2484
|
+
* @param options - Status options
|
|
2485
|
+
* @returns Number of commits behind
|
|
2486
|
+
*/
|
|
2487
|
+
function getBehindCount(options = {}) {
|
|
2488
|
+
const status = getStatus(options);
|
|
2489
|
+
return status.behind;
|
|
2490
|
+
}
|
|
2491
|
+
/**
|
|
2492
|
+
* Checks if the repository needs to be pushed.
|
|
2493
|
+
*
|
|
2494
|
+
* @param options - Status options
|
|
2495
|
+
* @returns True if there are unpushed commits
|
|
2496
|
+
*/
|
|
2497
|
+
function needsPush(options = {}) {
|
|
2498
|
+
return getAheadCount(options) > 0;
|
|
2499
|
+
}
|
|
2500
|
+
/**
|
|
2501
|
+
* Checks if the repository needs to be pulled.
|
|
2502
|
+
*
|
|
2503
|
+
* @param options - Status options
|
|
2504
|
+
* @returns True if there are commits to pull
|
|
2505
|
+
*/
|
|
2506
|
+
function needsPull(options = {}) {
|
|
2507
|
+
return getBehindCount(options) > 0;
|
|
2508
|
+
}
|
|
2509
|
+
/**
|
|
2510
|
+
* Gets list of staged file paths.
|
|
2511
|
+
*
|
|
2512
|
+
* @param options - Status options
|
|
2513
|
+
* @returns Array of staged file paths
|
|
2514
|
+
*/
|
|
2515
|
+
function getStagedFiles(options = {}) {
|
|
2516
|
+
const status = getStatus(options);
|
|
2517
|
+
return status.staged.map((e) => e.path);
|
|
2518
|
+
}
|
|
2519
|
+
/**
|
|
2520
|
+
* Gets list of modified file paths (unstaged).
|
|
2521
|
+
*
|
|
2522
|
+
* @param options - Status options
|
|
2523
|
+
* @returns Array of modified file paths
|
|
2524
|
+
*/
|
|
2525
|
+
function getModifiedFiles(options = {}) {
|
|
2526
|
+
const status = getStatus(options);
|
|
2527
|
+
return status.modified.map((e) => e.path);
|
|
2528
|
+
}
|
|
2529
|
+
/**
|
|
2530
|
+
* Gets list of untracked file paths.
|
|
2531
|
+
*
|
|
2532
|
+
* @param options - Status options
|
|
2533
|
+
* @returns Array of untracked file paths
|
|
2534
|
+
*/
|
|
2535
|
+
function getUntrackedFiles(options = {}) {
|
|
2536
|
+
const status = getStatus(options);
|
|
2537
|
+
return status.untracked;
|
|
2538
|
+
}
|
|
2539
|
+
|
|
2540
|
+
/**
|
|
2541
|
+
* Default git client configuration.
|
|
2542
|
+
*/
|
|
2543
|
+
const DEFAULT_GIT_CLIENT_CONFIG = {
|
|
2544
|
+
cwd: process.cwd(),
|
|
2545
|
+
timeout: 30000,
|
|
2546
|
+
throwOnError: true,
|
|
2547
|
+
};
|
|
2548
|
+
/**
|
|
2549
|
+
* Creates a git client for a specific working directory.
|
|
2550
|
+
*
|
|
2551
|
+
* @param config - Client configuration
|
|
2552
|
+
* @returns GitClient instance
|
|
2553
|
+
*
|
|
2554
|
+
* @example
|
|
2555
|
+
* const git = createGitClient({ cwd: '/path/to/repo' })
|
|
2556
|
+
* const status = git.getStatus()
|
|
2557
|
+
* const commits = git.getCommitsSince('v1.0.0')
|
|
2558
|
+
*/
|
|
2559
|
+
function createGitClient(config = {}) {
|
|
2560
|
+
const cwd = config.cwd ?? process.cwd();
|
|
2561
|
+
const timeout = config.timeout ?? DEFAULT_GIT_CLIENT_CONFIG.timeout;
|
|
2562
|
+
const opts = { cwd, timeout };
|
|
2563
|
+
return {
|
|
2564
|
+
cwd,
|
|
2565
|
+
timeout,
|
|
2566
|
+
// Log operations
|
|
2567
|
+
getCommitLog: (options) => getCommitLog({ ...opts, ...options }),
|
|
2568
|
+
getCommitsBetween: (from, to, options) => getCommitsBetween(from, to, { ...opts, ...options }),
|
|
2569
|
+
getCommitsSince: (since, options) => getCommitsSince(since, { ...opts, ...options }),
|
|
2570
|
+
getCommit: (hash) => getCommit(hash, opts),
|
|
2571
|
+
commitExists: (hash) => commitExists(hash, opts),
|
|
2572
|
+
// Tag operations
|
|
2573
|
+
getTags: (options) => getTags({ ...opts, ...options }),
|
|
2574
|
+
getTag: (name) => getTag(name, opts),
|
|
2575
|
+
createTag: (name, options) => createTag(name, { ...opts, ...options }),
|
|
2576
|
+
deleteTag: (name) => deleteTag(name, opts),
|
|
2577
|
+
tagExists: (name) => tagExists(name, opts),
|
|
2578
|
+
getLatestTag: (options) => getLatestTag({ ...opts, ...options }),
|
|
2579
|
+
getTagsForPackage: (packageName, options) => getTagsForPackage(packageName, { ...opts, ...options }),
|
|
2580
|
+
pushTag: (name, remote) => pushTag(name, remote, opts),
|
|
2581
|
+
// Commit operations
|
|
2582
|
+
createCommit: (message, options) => commit(message, { ...opts, ...options }),
|
|
2583
|
+
stage: (files, options) => stage(files, { ...opts, ...options }),
|
|
2584
|
+
unstage: (files) => unstage(files, opts),
|
|
2585
|
+
stageAll: () => stageAll(opts),
|
|
2586
|
+
amendCommit: (message, options) => amendCommit(message, { ...opts, ...options }),
|
|
2587
|
+
createEmptyCommit: (message, options) => createEmptyCommit(message, { ...opts, ...options }),
|
|
2588
|
+
getHead: () => getHead(opts),
|
|
2589
|
+
getCurrentBranch: () => getCurrentBranch(opts),
|
|
2590
|
+
hasStagedChanges: () => hasStagedChanges(opts),
|
|
2591
|
+
hasUnstagedChanges: () => hasUnstagedChanges(opts),
|
|
2592
|
+
hasUntrackedFiles: () => hasUntrackedFiles(opts),
|
|
2593
|
+
// Status operations
|
|
2594
|
+
getStatus: () => getStatus(opts),
|
|
2595
|
+
isClean: () => isClean(opts),
|
|
2596
|
+
isGitRepository: () => isGitRepository(opts),
|
|
2597
|
+
getRepositoryRoot: () => getRepositoryRoot(opts),
|
|
2598
|
+
getHeadHash: () => getHeadHash(opts),
|
|
2599
|
+
getHeadShortHash: () => getHeadShortHash(opts),
|
|
2600
|
+
hasConflicts: () => hasConflicts(opts),
|
|
2601
|
+
getAheadCount: () => getAheadCount(opts),
|
|
2602
|
+
getBehindCount: () => getBehindCount(opts),
|
|
2603
|
+
needsPush: () => needsPush(opts),
|
|
2604
|
+
needsPull: () => needsPull(opts),
|
|
2605
|
+
getStagedFiles: () => getStagedFiles(opts),
|
|
2606
|
+
getModifiedFiles: () => getModifiedFiles(opts),
|
|
2607
|
+
getUntrackedFiles: () => getUntrackedFiles(opts),
|
|
2608
|
+
// Ref operations
|
|
2609
|
+
getRefs: () => getRefs(opts),
|
|
2610
|
+
getBranches: () => getBranches(opts),
|
|
2611
|
+
getRemoteBranches: (remote) => getRemoteBranches(opts, remote),
|
|
2612
|
+
fetch: (remote, options) => fetch(opts, remote, options),
|
|
2613
|
+
pull: (remote, branch) => pull(opts, remote, branch),
|
|
2614
|
+
push: (remote, branch, options) => push(opts, remote, branch, options),
|
|
2615
|
+
};
|
|
2616
|
+
}
|
|
2617
|
+
// ============================================================================
|
|
2618
|
+
// Additional ref operations used by the client
|
|
2619
|
+
// ============================================================================
|
|
2620
|
+
/**
|
|
2621
|
+
* Gets all refs from the repository.
|
|
2622
|
+
*
|
|
2623
|
+
* @param options - Configuration object containing cwd and timeout
|
|
2624
|
+
* @param options.cwd - Working directory for the git command
|
|
2625
|
+
* @param options.timeout - Command timeout in milliseconds
|
|
2626
|
+
* @returns Array of GitRef objects representing all refs in the repository
|
|
2627
|
+
*/
|
|
2628
|
+
function getRefs(options) {
|
|
2629
|
+
try {
|
|
2630
|
+
const output = node_child_process.execSync('git show-ref', {
|
|
2631
|
+
encoding: 'utf-8',
|
|
2632
|
+
cwd: options.cwd,
|
|
2633
|
+
timeout: options.timeout,
|
|
2634
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
2635
|
+
});
|
|
2636
|
+
const refs = [];
|
|
2637
|
+
const lines = output.split('\n');
|
|
2638
|
+
for (const line of lines) {
|
|
2639
|
+
const trimmed = line.trim();
|
|
2640
|
+
if (!trimmed)
|
|
2641
|
+
continue;
|
|
2642
|
+
// Format: <hash> <refname>
|
|
2643
|
+
const spaceIndex = trimmed.indexOf(' ');
|
|
2644
|
+
if (spaceIndex === -1)
|
|
2645
|
+
continue;
|
|
2646
|
+
const hash = trimmed.slice(0, spaceIndex);
|
|
2647
|
+
const fullName = trimmed.slice(spaceIndex + 1);
|
|
2648
|
+
refs.push(createGitRef({ fullName, commitHash: hash }));
|
|
2649
|
+
}
|
|
2650
|
+
return refs;
|
|
2651
|
+
}
|
|
2652
|
+
catch {
|
|
2653
|
+
return [];
|
|
2654
|
+
}
|
|
2655
|
+
}
|
|
2656
|
+
/**
|
|
2657
|
+
* Gets local branches.
|
|
2658
|
+
*
|
|
2659
|
+
* @param options - Configuration object containing cwd and timeout
|
|
2660
|
+
* @param options.cwd - Working directory for the git command
|
|
2661
|
+
* @param options.timeout - Command timeout in milliseconds
|
|
2662
|
+
* @returns Array of GitRef objects representing local branches
|
|
2663
|
+
*/
|
|
2664
|
+
function getBranches(options) {
|
|
2665
|
+
const refs = getRefs(options);
|
|
2666
|
+
return refs.filter((ref) => ref.type === 'branch');
|
|
2667
|
+
}
|
|
2668
|
+
/**
|
|
2669
|
+
* Gets remote branches.
|
|
2670
|
+
*
|
|
2671
|
+
* @param options - Configuration object containing cwd and timeout
|
|
2672
|
+
* @param options.cwd - Working directory for the git command
|
|
2673
|
+
* @param options.timeout - Command timeout in milliseconds
|
|
2674
|
+
* @param remote - Optional remote name to filter branches by
|
|
2675
|
+
* @returns Array of GitRef objects representing remote branches
|
|
2676
|
+
*/
|
|
2677
|
+
function getRemoteBranches(options, remote) {
|
|
2678
|
+
const refs = getRefs(options);
|
|
2679
|
+
return refs.filter((ref) => {
|
|
2680
|
+
if (ref.type !== 'remote')
|
|
2681
|
+
return false;
|
|
2682
|
+
if (remote && ref.remote !== remote)
|
|
2683
|
+
return false;
|
|
2684
|
+
return true;
|
|
2685
|
+
});
|
|
2686
|
+
}
|
|
2687
|
+
/**
|
|
2688
|
+
* Fetches from remote.
|
|
2689
|
+
*
|
|
2690
|
+
* @param options - Configuration object containing cwd and timeout
|
|
2691
|
+
* @param options.cwd - Working directory for the git command
|
|
2692
|
+
* @param options.timeout - Command timeout in milliseconds
|
|
2693
|
+
* @param remote - Remote name to fetch from (defaults to 'origin')
|
|
2694
|
+
* @param fetchOptions - Additional fetch configuration
|
|
2695
|
+
* @param fetchOptions.prune - Whether to prune deleted remote branches
|
|
2696
|
+
* @param fetchOptions.tags - Whether to fetch tags
|
|
2697
|
+
* @returns True if fetch succeeded
|
|
2698
|
+
*/
|
|
2699
|
+
function fetch(options, remote = 'origin', fetchOptions) {
|
|
2700
|
+
const args = ['fetch', remote];
|
|
2701
|
+
if (fetchOptions?.prune) {
|
|
2702
|
+
args.push('--prune');
|
|
2703
|
+
}
|
|
2704
|
+
if (fetchOptions?.tags) {
|
|
2705
|
+
args.push('--tags');
|
|
2706
|
+
}
|
|
2707
|
+
try {
|
|
2708
|
+
node_child_process.execSync(`git ${args.join(' ')}`, {
|
|
2709
|
+
encoding: 'utf-8',
|
|
2710
|
+
cwd: options.cwd,
|
|
2711
|
+
timeout: options.timeout * 3, // Allow more time for network
|
|
2712
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
2713
|
+
});
|
|
2714
|
+
return true;
|
|
2715
|
+
}
|
|
2716
|
+
catch {
|
|
2717
|
+
return false;
|
|
2718
|
+
}
|
|
2719
|
+
}
|
|
2720
|
+
/**
|
|
2721
|
+
* Pulls from remote.
|
|
2722
|
+
*
|
|
2723
|
+
* @param options - Configuration object containing cwd and timeout
|
|
2724
|
+
* @param options.cwd - Working directory for the git command
|
|
2725
|
+
* @param options.timeout - Command timeout in milliseconds
|
|
2726
|
+
* @param remote - Remote name to pull from (defaults to 'origin')
|
|
2727
|
+
* @param branch - Optional branch name to pull
|
|
2728
|
+
* @returns True if pull succeeded
|
|
2729
|
+
*/
|
|
2730
|
+
function pull(options, remote = 'origin', branch) {
|
|
2731
|
+
const args = ['pull', remote];
|
|
2732
|
+
if (branch) {
|
|
2733
|
+
args.push(branch);
|
|
2734
|
+
}
|
|
2735
|
+
try {
|
|
2736
|
+
node_child_process.execSync(`git ${args.join(' ')}`, {
|
|
2737
|
+
encoding: 'utf-8',
|
|
2738
|
+
cwd: options.cwd,
|
|
2739
|
+
timeout: options.timeout * 3,
|
|
2740
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
2741
|
+
});
|
|
2742
|
+
return true;
|
|
2743
|
+
}
|
|
2744
|
+
catch {
|
|
2745
|
+
return false;
|
|
2746
|
+
}
|
|
2747
|
+
}
|
|
2748
|
+
/**
|
|
2749
|
+
* Pushes to remote.
|
|
2750
|
+
*
|
|
2751
|
+
* @param options - Configuration object containing cwd and timeout
|
|
2752
|
+
* @param options.cwd - Working directory for the git command
|
|
2753
|
+
* @param options.timeout - Command timeout in milliseconds
|
|
2754
|
+
* @param remote - Remote name to push to (defaults to 'origin')
|
|
2755
|
+
* @param branch - Optional branch name to push
|
|
2756
|
+
* @param pushOptions - Additional push configuration
|
|
2757
|
+
* @param pushOptions.force - Whether to force push
|
|
2758
|
+
* @param pushOptions.setUpstream - Whether to set upstream tracking
|
|
2759
|
+
* @returns True if push succeeded
|
|
2760
|
+
*/
|
|
2761
|
+
function push(options, remote = 'origin', branch, pushOptions) {
|
|
2762
|
+
const args = ['push', remote];
|
|
2763
|
+
if (branch) {
|
|
2764
|
+
args.push(branch);
|
|
2765
|
+
}
|
|
2766
|
+
if (pushOptions?.force) {
|
|
2767
|
+
args.push('--force');
|
|
2768
|
+
}
|
|
2769
|
+
if (pushOptions?.setUpstream) {
|
|
2770
|
+
args.push('--set-upstream');
|
|
2771
|
+
}
|
|
2772
|
+
try {
|
|
2773
|
+
node_child_process.execSync(`git ${args.join(' ')}`, {
|
|
2774
|
+
encoding: 'utf-8',
|
|
2775
|
+
cwd: options.cwd,
|
|
2776
|
+
timeout: options.timeout * 3,
|
|
2777
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
2778
|
+
});
|
|
2779
|
+
return true;
|
|
2780
|
+
}
|
|
2781
|
+
catch {
|
|
2782
|
+
return false;
|
|
2783
|
+
}
|
|
2784
|
+
}
|
|
2785
|
+
|
|
2786
|
+
exports.DEFAULT_COMMIT_OPTIONS = DEFAULT_COMMIT_OPTIONS;
|
|
2787
|
+
exports.DEFAULT_GIT_CLIENT_CONFIG = DEFAULT_GIT_CLIENT_CONFIG;
|
|
2788
|
+
exports.DEFAULT_LOG_OPTIONS = DEFAULT_LOG_OPTIONS;
|
|
2789
|
+
exports.DEFAULT_STATUS_OPTIONS = DEFAULT_STATUS_OPTIONS;
|
|
2790
|
+
exports.DEFAULT_TAG_OPTIONS = DEFAULT_TAG_OPTIONS;
|
|
2791
|
+
exports.amendCommit = amendCommit;
|
|
2792
|
+
exports.amendCommitNoEdit = amendCommitNoEdit;
|
|
2793
|
+
exports.buildRefName = buildRefName;
|
|
2794
|
+
exports.buildTagName = buildTagName;
|
|
2795
|
+
exports.commit = commit;
|
|
2796
|
+
exports.commitExists = commitExists;
|
|
2797
|
+
exports.compareRefsByName = compareRefsByName;
|
|
2798
|
+
exports.compareTagsByVersion = compareTagsByVersion;
|
|
2799
|
+
exports.createAnnotatedTag = createAnnotatedTag;
|
|
2800
|
+
exports.createEmptyCommit = createEmptyCommit;
|
|
2801
|
+
exports.createGitClient = createGitClient;
|
|
2802
|
+
exports.createGitCommit = createGitCommit;
|
|
2803
|
+
exports.createGitRef = createGitRef;
|
|
2804
|
+
exports.createLightweightTag = createLightweightTag;
|
|
2805
|
+
exports.createTag = createTag;
|
|
2806
|
+
exports.deleteTag = deleteTag;
|
|
2807
|
+
exports.escapeAuthor = escapeAuthor;
|
|
2808
|
+
exports.escapeFilePath = escapeFilePath;
|
|
2809
|
+
exports.escapeGitArg = escapeGitArg;
|
|
2810
|
+
exports.escapeGitMessage = escapeGitMessage;
|
|
2811
|
+
exports.escapeGitPath = escapeGitPath;
|
|
2812
|
+
exports.escapeGitRef = escapeGitRef;
|
|
2813
|
+
exports.escapeGitTagPattern = escapeGitTagPattern;
|
|
2814
|
+
exports.extractPackageFromTag = extractPackageFromTag;
|
|
2815
|
+
exports.extractScope = extractScope;
|
|
2816
|
+
exports.extractType = extractType;
|
|
2817
|
+
exports.extractVersionFromTag = extractVersionFromTag;
|
|
2818
|
+
exports.filterRefsByRemote = filterRefsByRemote;
|
|
2819
|
+
exports.filterRefsByType = filterRefsByType;
|
|
2820
|
+
exports.getAheadCount = getAheadCount;
|
|
2821
|
+
exports.getBehindCount = getBehindCount;
|
|
2822
|
+
exports.getCommit = getCommit;
|
|
2823
|
+
exports.getCommitLog = getCommitLog;
|
|
2824
|
+
exports.getCommitsBetween = getCommitsBetween;
|
|
2825
|
+
exports.getCommitsSince = getCommitsSince;
|
|
2826
|
+
exports.getCurrentBranch = getCurrentBranch;
|
|
2827
|
+
exports.getHead = getHead;
|
|
2828
|
+
exports.getHeadHash = getHeadHash;
|
|
2829
|
+
exports.getHeadShortHash = getHeadShortHash;
|
|
2830
|
+
exports.getLatestTag = getLatestTag;
|
|
2831
|
+
exports.getModifiedFiles = getModifiedFiles;
|
|
2832
|
+
exports.getRemote = getRemote;
|
|
2833
|
+
exports.getRepositoryRoot = getRepositoryRoot;
|
|
2834
|
+
exports.getShortHash = getShortHash;
|
|
2835
|
+
exports.getStagedFiles = getStagedFiles;
|
|
2836
|
+
exports.getStatus = getStatus;
|
|
2837
|
+
exports.getTag = getTag;
|
|
2838
|
+
exports.getTags = getTags;
|
|
2839
|
+
exports.getTagsForPackage = getTagsForPackage;
|
|
2840
|
+
exports.getUntrackedFiles = getUntrackedFiles;
|
|
2841
|
+
exports.hasConflicts = hasConflicts;
|
|
2842
|
+
exports.hasStagedChanges = hasStagedChanges;
|
|
2843
|
+
exports.hasUnstagedChanges = hasUnstagedChanges;
|
|
2844
|
+
exports.hasUntrackedFiles = hasUntrackedFiles;
|
|
2845
|
+
exports.isAnnotatedTag = isAnnotatedTag;
|
|
2846
|
+
exports.isBranchRef = isBranchRef;
|
|
2847
|
+
exports.isClean = isClean;
|
|
2848
|
+
exports.isGitRepository = isGitRepository;
|
|
2849
|
+
exports.isHeadRef = isHeadRef;
|
|
2850
|
+
exports.isLightweightTag = isLightweightTag;
|
|
2851
|
+
exports.isMergeCommit = isMergeCommit;
|
|
2852
|
+
exports.isRemoteRef = isRemoteRef;
|
|
2853
|
+
exports.isRootCommit = isRootCommit;
|
|
2854
|
+
exports.isSameCommit = isSameCommit;
|
|
2855
|
+
exports.isTagRef = isTagRef;
|
|
2856
|
+
exports.needsPull = needsPull;
|
|
2857
|
+
exports.needsPush = needsPush;
|
|
2858
|
+
exports.pushTag = pushTag;
|
|
2859
|
+
exports.stage = stage;
|
|
2860
|
+
exports.stageAll = stageAll;
|
|
2861
|
+
exports.tagExists = tagExists;
|
|
2862
|
+
exports.unstage = unstage;
|
|
2863
|
+
//# sourceMappingURL=index.cjs.js.map
|