@reelout/core 0.1.1 → 0.5.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.
Files changed (138) hide show
  1. package/LICENSE +21 -0
  2. package/dist/analyzer.js +33 -0
  3. package/dist/analyzer.js.map +1 -0
  4. package/dist/builder.js +64 -0
  5. package/dist/builder.js.map +1 -0
  6. package/dist/changelog.js +63 -0
  7. package/dist/changelog.js.map +1 -0
  8. package/dist/formatError-BXNdyRfu.js +23 -0
  9. package/dist/formatError-BXNdyRfu.js.map +1 -0
  10. package/dist/index.js +225 -1
  11. package/dist/index.js.map +1 -0
  12. package/dist/types-la_KkjCS.js +1 -0
  13. package/dist/types.js +2 -0
  14. package/dist/workflow-CroJ6v4g.js +2736 -0
  15. package/dist/workflow-CroJ6v4g.js.map +1 -0
  16. package/package.json +35 -6
  17. package/src/analyzer.ts +59 -0
  18. package/src/builder.ts +116 -0
  19. package/src/cache/TTLCache.ts +100 -0
  20. package/src/cache/index.ts +4 -0
  21. package/src/changelog.ts +107 -0
  22. package/src/config/const.ts +104 -0
  23. package/src/config/createPlugin.ts +41 -0
  24. package/src/config/findReleaseRule.ts +26 -0
  25. package/src/config/index.ts +15 -0
  26. package/src/config/isSemVer.ts +31 -0
  27. package/src/config/isSemVerGitTag.ts +25 -0
  28. package/src/config/normalizeCommitFilter.ts +37 -0
  29. package/src/config/normalizeReleaseRule.ts +66 -0
  30. package/src/config/normalizeWorkspace.ts +34 -0
  31. package/src/config/resolvePlugin.ts +57 -0
  32. package/src/config/resolveReleaseChannel.ts +47 -0
  33. package/src/config/resolveVersionQualifier.ts +53 -0
  34. package/src/config/sanitizePrereleaseLabel.ts +18 -0
  35. package/src/git/branch/getCurrentGitBranch.ts +18 -0
  36. package/src/git/branch/index.ts +4 -0
  37. package/src/git/commit/createGitCommit.ts +29 -0
  38. package/src/git/commit/extractMentions.ts +34 -0
  39. package/src/git/commit/getCurrentGitCommit.ts +19 -0
  40. package/src/git/commit/getParsedGitCommit.ts +49 -0
  41. package/src/git/commit/index.ts +9 -0
  42. package/src/git/commit/parseGitCommit.ts +114 -0
  43. package/src/git/commit/parseGitPrettyFormat.ts +76 -0
  44. package/src/git/history/countGitHistory.ts +28 -0
  45. package/src/git/history/expandGitHistory.ts +112 -0
  46. package/src/git/history/index.ts +9 -0
  47. package/src/git/history/mergeGitHistory.ts +76 -0
  48. package/src/git/history/restoreFullGitHistory.ts +19 -0
  49. package/src/git/history/showGitHistory.ts +80 -0
  50. package/src/git/history/showParsedGitHistory.ts +28 -0
  51. package/src/git/index.ts +9 -0
  52. package/src/git/repository/ensureGitUserConfig.ts +50 -0
  53. package/src/git/repository/getGitConfigValue.ts +27 -0
  54. package/src/git/repository/getGitRemote.ts +20 -0
  55. package/src/git/repository/index.ts +10 -0
  56. package/src/git/repository/isGitShallowRepository.ts +21 -0
  57. package/src/git/repository/pushGitChanges.ts +26 -0
  58. package/src/git/repository/resolveGitRepository.ts +40 -0
  59. package/src/git/repository/withShallowLock.ts +62 -0
  60. package/src/git/revision/detectGitRelationship.ts +43 -0
  61. package/src/git/revision/index.ts +7 -0
  62. package/src/git/revision/isGitAncestor.ts +33 -0
  63. package/src/git/revision/readGitFile.ts +29 -0
  64. package/src/git/revision/resolveGitRevision.ts +23 -0
  65. package/src/git/tag/createGitTag.ts +25 -0
  66. package/src/git/tag/deleteGitTag.ts +17 -0
  67. package/src/git/tag/ensureGitTag.ts +63 -0
  68. package/src/git/tag/generateGitTagName.ts +43 -0
  69. package/src/git/tag/getCurrentGitTag.ts +23 -0
  70. package/src/git/tag/getGitTagMessage.ts +35 -0
  71. package/src/git/tag/getLatestGitTag.ts +22 -0
  72. package/src/git/tag/index.ts +13 -0
  73. package/src/git/tag/isGitTagConnected.ts +33 -0
  74. package/src/git/tag/isGitTagPointed.ts +38 -0
  75. package/src/git/tag/isGitTagPresent.ts +17 -0
  76. package/src/index.ts +9 -2
  77. package/src/plugin/HookManager.ts +128 -0
  78. package/src/plugin/index.ts +4 -0
  79. package/src/types.ts +259 -0
  80. package/src/util/buildTrailerPattern.ts +20 -0
  81. package/src/util/compact.ts +14 -0
  82. package/src/util/execCommand.ts +196 -0
  83. package/src/util/execGit.ts +27 -0
  84. package/src/util/formatError.ts +21 -0
  85. package/src/util/formatFile.ts +41 -0
  86. package/src/util/index.ts +19 -0
  87. package/src/util/interpolate.ts +141 -0
  88. package/src/util/isCI.ts +18 -0
  89. package/src/util/isClass.ts +34 -0
  90. package/src/util/isDefined.ts +12 -0
  91. package/src/util/isMatch.ts +43 -0
  92. package/src/util/isQuietEnvironment.ts +35 -0
  93. package/src/util/isTruthyString.ts +19 -0
  94. package/src/util/readJson.ts +47 -0
  95. package/src/util/unique.ts +26 -0
  96. package/src/util/writeJson.ts +28 -0
  97. package/src/workflow/analysis/deriveChangeReleases.ts +76 -0
  98. package/src/workflow/analysis/deriveTransitionReleases.ts +134 -0
  99. package/src/workflow/analysis/index.ts +10 -0
  100. package/src/workflow/analysis/isCommitForPackage.ts +23 -0
  101. package/src/workflow/analysis/promotePrereleaseVersions.ts +51 -0
  102. package/src/workflow/analysis/propagateDependencyUpdates.ts +71 -0
  103. package/src/workflow/analysis/sliceUnreleased.ts +36 -0
  104. package/src/workflow/analysis/streamPackageCommits.ts +34 -0
  105. package/src/workflow/changelog/buildChangelog.ts +79 -0
  106. package/src/workflow/changelog/buildReleaseNotes.ts +50 -0
  107. package/src/workflow/changelog/conventional-changelog-conventionalcommits.d.ts +7 -0
  108. package/src/workflow/changelog/extractLatestVersion.ts +22 -0
  109. package/src/workflow/changelog/extractPlainText.ts +38 -0
  110. package/src/workflow/changelog/extractPreviousVersion.ts +77 -0
  111. package/src/workflow/changelog/extractSemVer.ts +33 -0
  112. package/src/workflow/changelog/index.ts +12 -0
  113. package/src/workflow/changelog/iterateChangelogVersions.ts +70 -0
  114. package/src/workflow/changelog/prependChangelog.ts +59 -0
  115. package/src/workflow/changelog/writeChangelog.ts +27 -0
  116. package/src/workflow/committing/generateReleaseCommitMessage.ts +121 -0
  117. package/src/workflow/committing/index.ts +5 -0
  118. package/src/workflow/committing/isReleaseCommit.ts +56 -0
  119. package/src/workflow/discovery/buildDependencyGraph.ts +43 -0
  120. package/src/workflow/discovery/discoverPackageTag.ts +98 -0
  121. package/src/workflow/discovery/index.ts +9 -0
  122. package/src/workflow/discovery/resolvePackageStableTag.ts +88 -0
  123. package/src/workflow/discovery/resolvePackageTag.ts +45 -0
  124. package/src/workflow/discovery/sortPackages.ts +75 -0
  125. package/src/workflow/discovery/verifyPackageTag.ts +49 -0
  126. package/src/workflow/executeReleaseWorkflow.ts +130 -0
  127. package/src/workflow/index.ts +10 -0
  128. package/src/workflow/report/generateReleaseReport.ts +54 -0
  129. package/src/workflow/report/index.ts +5 -0
  130. package/src/workflow/report/printReleaseReport.ts +97 -0
  131. package/src/workflow/versioning/bumpVersion.ts +76 -0
  132. package/src/workflow/versioning/deriveReleaseType.ts +33 -0
  133. package/src/workflow/versioning/determineReleaseType.ts +35 -0
  134. package/src/workflow/versioning/extractVersionQualifier.ts +29 -0
  135. package/src/workflow/versioning/index.ts +10 -0
  136. package/src/workflow/versioning/isSameReleaseCycle.ts +36 -0
  137. package/src/workflow/versioning/isStableVersion.ts +31 -0
  138. package/src/workflow/versioning/pickPriorityReleaseType.ts +27 -0
@@ -0,0 +1,112 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // Copyright (c) 2026 Fai
3
+
4
+ import { execGit } from '#util'
5
+ import { isGitShallowRepository } from '../repository/isGitShallowRepository'
6
+ import { withShallowLock } from '../repository/withShallowLock'
7
+ import { isGitAncestor } from '../revision/isGitAncestor'
8
+ import { resolveGitRevision } from '../revision/resolveGitRevision'
9
+ import { countGitHistory } from './countGitHistory'
10
+
11
+ export interface ExpandGitHistoryOptions {
12
+ /**
13
+ * Number of commits to deepen the history by when paging
14
+ * @default 50
15
+ */
16
+ pageSize?: number | undefined
17
+
18
+ /**
19
+ * Local reference used to measure current shallow depth (for example HEAD)
20
+ * When provided the function accounts for existing depth before fetching more
21
+ */
22
+ from?: string | null | undefined
23
+
24
+ /**
25
+ * Target boundary reference where history deepening should stop
26
+ * If provided the function will first attempt a single fetch to reach this commit
27
+ */
28
+ until?: string | null | undefined
29
+ }
30
+
31
+ /**
32
+ * Deepen a shallow clone so additional commits become available.
33
+ *
34
+ * Attempts a single `--shallow-since` fetch to reach `until` (if provided),
35
+ * otherwise deepens by `pageSize`, accounting for existing depth when `from`
36
+ * is supplied. Operation is serialized to avoid concurrent deepens.
37
+ *
38
+ * @param cwd Repository working directory
39
+ * @param options Options: { pageSize?, from?, until? }
40
+ * @returns true if history was deepened or already sufficient, otherwise false
41
+ */
42
+ export async function expandGitHistory(
43
+ cwd: string,
44
+ options: ExpandGitHistoryOptions = {}
45
+ ): Promise<boolean> {
46
+ if (!(await isGitShallowRepository(cwd))) {
47
+ return false
48
+ }
49
+
50
+ return withShallowLock(cwd, async () => {
51
+ const { until } = options
52
+ let { from } = options
53
+ let { pageSize = 50 } = options
54
+
55
+ // If an `until` boundary is provided, try a single fetch using the target commit's date
56
+ if (until) {
57
+ if (await isGitAncestor(cwd, until, 'HEAD')) {
58
+ return true
59
+ }
60
+
61
+ const commitDate = (
62
+ await execGit(cwd, ['show', '-s', '--format=%cI', `${until}^{commit}`])
63
+ ).trim()
64
+
65
+ try {
66
+ await execGit(cwd, [
67
+ '-c',
68
+ 'fetch.negotiationAlgorithm=skipping',
69
+ 'fetch',
70
+ `--shallow-since=${commitDate}`,
71
+ '--update-shallow',
72
+ '--filter=blob:none',
73
+ ])
74
+
75
+ return true
76
+ } catch {}
77
+ }
78
+
79
+ // Incremental deepening: account for existing local depth when `from` is set
80
+ if (from) {
81
+ from = await resolveGitRevision(cwd, from)
82
+
83
+ const depth = await countGitHistory(cwd, from)
84
+
85
+ if (depth > pageSize) {
86
+ return true
87
+ }
88
+
89
+ // Reduce requested deepen amount by existing depth (exclude the `from` commit)
90
+ pageSize -= depth - 1
91
+ }
92
+
93
+ // Perform a deepen fetch; if `from` is not an ancestor of HEAD, fetch that ref
94
+ try {
95
+ await execGit(cwd, [
96
+ '-c',
97
+ 'fetch.negotiationAlgorithm=skipping',
98
+ 'fetch',
99
+ `--deepen=${pageSize}`,
100
+ '--update-shallow',
101
+ '--filter=blob:none',
102
+ ...(from && !(await isGitAncestor(cwd, from, 'HEAD'))
103
+ ? ['origin', from, 'HEAD']
104
+ : []),
105
+ ])
106
+
107
+ return true
108
+ } catch {
109
+ return false
110
+ }
111
+ })
112
+ }
@@ -0,0 +1,9 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // Copyright (c) 2026 Fai
3
+
4
+ export * from './countGitHistory'
5
+ export * from './expandGitHistory'
6
+ export * from './mergeGitHistory'
7
+ export * from './restoreFullGitHistory'
8
+ export * from './showGitHistory'
9
+ export * from './showParsedGitHistory'
@@ -0,0 +1,76 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // Copyright (c) 2026 Fai
3
+
4
+ import type { GitCommit } from '#types'
5
+ import {
6
+ detectGitRelationship,
7
+ GIT_REL_FLAGS,
8
+ } from '../revision/detectGitRelationship'
9
+
10
+ /**
11
+ * Merge two newest-first commit lists into a single deduplicated newest-first list.
12
+ *
13
+ * Compares commits by hash, uses git relationship (ancestor/descendant) when needed,
14
+ * and falls back to committerDate for unrelated commits.
15
+ *
16
+ * @param cwd Repository working directory used for relationship checks
17
+ * @param a Commits from the first source (newest-first)
18
+ * @param b Commits from the second source (newest-first)
19
+ * @returns Merged commits in newest-first order with duplicates removed
20
+ */
21
+ export async function mergeGitHistory(
22
+ cwd: string,
23
+ a: readonly GitCommit[],
24
+ b: readonly GitCommit[]
25
+ ): Promise<GitCommit[]> {
26
+ const merged: GitCommit[] = []
27
+ const a2 = [...a]
28
+ const b2 = [...b]
29
+ let i = a2.shift()
30
+ let j = b2.shift()
31
+
32
+ while (i && j) {
33
+ const rel =
34
+ i.hash === j.hash
35
+ ? GIT_REL_FLAGS.IS_EQUAL
36
+ : await detectGitRelationship(cwd, i.hash, j.hash)
37
+
38
+ switch (rel) {
39
+ case GIT_REL_FLAGS.IS_EQUAL:
40
+ merged.push(i)
41
+ i = a2.shift()
42
+ j = b2.shift()
43
+ break
44
+
45
+ case GIT_REL_FLAGS.IS_ANCESTOR:
46
+ merged.push(j)
47
+ j = b2.shift()
48
+ break
49
+
50
+ case GIT_REL_FLAGS.IS_DESCENDANT:
51
+ merged.push(i)
52
+ i = a2.shift()
53
+ break
54
+
55
+ case GIT_REL_FLAGS.NONE:
56
+ if (i.committerDate < j.committerDate) {
57
+ merged.push(j)
58
+ j = b2.shift()
59
+ } else {
60
+ merged.push(i)
61
+ i = a2.shift()
62
+ }
63
+ break
64
+ }
65
+ }
66
+
67
+ if (i) {
68
+ merged.push(i)
69
+ }
70
+
71
+ if (j) {
72
+ merged.push(j)
73
+ }
74
+
75
+ return merged.concat(a2).concat(b2)
76
+ }
@@ -0,0 +1,19 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // Copyright (c) 2026 Fai
3
+
4
+ import { execGit } from '#util'
5
+
6
+ /**
7
+ * Convert a shallow clone into a full clone by fetching additional history.
8
+ *
9
+ * Runs `git fetch --unshallow`. Returns `true` when the operation succeeds
10
+ * and `false` on error.
11
+ *
12
+ * @param cwd Repository working directory
13
+ * @returns `true` when the operation succeeded, otherwise `false`
14
+ */
15
+ export function restoreFullGitHistory(cwd: string): Promise<boolean> {
16
+ return execGit(cwd, ['fetch', '--unshallow'])
17
+ .then(() => true)
18
+ .catch(() => false)
19
+ }
@@ -0,0 +1,80 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // Copyright (c) 2026 Fai
3
+
4
+ import { execGit } from '#util'
5
+ import { resolveGitRevision } from '../revision/resolveGitRevision'
6
+ import {
7
+ expandGitHistory,
8
+ type ExpandGitHistoryOptions,
9
+ } from './expandGitHistory'
10
+
11
+ export interface ShowGitHistoryOptions extends ExpandGitHistoryOptions {
12
+ filePath?: string | readonly string[] | undefined
13
+ }
14
+
15
+ /**
16
+ * Yield commit SHAs from repository history in paginated order.
17
+ *
18
+ * Ensures commits are available via `expandGitHistory`, resolves `until` to a
19
+ * commit id, and pages from `from` toward `until`, yielding each commit SHA.
20
+ * If a git command fails the generator ends silently.
21
+ *
22
+ * @param cwd Repository working directory
23
+ * @param options Supports `from`, `until`, `pageSize`, and `filePath`
24
+ * @yields Commit SHA strings in paginated order
25
+ */
26
+ export async function* showGitHistory(
27
+ cwd: string,
28
+ options: ShowGitHistoryOptions = {}
29
+ ): AsyncGenerator<string> {
30
+ try {
31
+ const { filePath, pageSize = 50 } = options
32
+ let from = options.from || 'HEAD'
33
+ let until = options.until || undefined
34
+
35
+ if (until) {
36
+ until = await resolveGitRevision(cwd, until)
37
+ }
38
+
39
+ while (true) {
40
+ await expandGitHistory(cwd, { from, until, pageSize })
41
+
42
+ const args = ['rev-list', `--max-count=${pageSize}`, from]
43
+
44
+ if (until) {
45
+ args.push(`^${until}`)
46
+ }
47
+
48
+ if (filePath) {
49
+ args.push(
50
+ '--',
51
+ ...(Array.isArray(filePath) ? filePath : [filePath]).filter(Boolean)
52
+ )
53
+ }
54
+
55
+ const stdout = await execGit(cwd, args)
56
+
57
+ const list = stdout.trim().split('\n').filter(Boolean)
58
+
59
+ for (const item of list.slice(0, -1)) {
60
+ yield item
61
+ }
62
+
63
+ const tail = list.at(-1)
64
+
65
+ if (!tail) {
66
+ return
67
+ }
68
+
69
+ if (tail === from) {
70
+ yield tail
71
+
72
+ from = `${tail}^`
73
+
74
+ from = await resolveGitRevision(cwd, from)
75
+ } else {
76
+ from = tail
77
+ }
78
+ }
79
+ } catch {}
80
+ }
@@ -0,0 +1,28 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // Copyright (c) 2026 Fai
3
+
4
+ import type { GitCommit } from '#types'
5
+ import { getParsedGitCommit } from '../commit/getParsedGitCommit'
6
+ import { showGitHistory, type ShowGitHistoryOptions } from './showGitHistory'
7
+
8
+ /**
9
+ * Yield parsed commits sequentially from repository history.
10
+ *
11
+ * Fetches commits using `showGitHistory` and parses each commit with
12
+ * `getParsedGitCommit`. Yields successfully parsed `GitCommit` objects on demand
13
+ * and skips unparsable commits.
14
+ *
15
+ * @param cwd Repository working directory path
16
+ * @param options Filter and pagination options passed to `showGitHistory`
17
+ * @yields Parsed `GitCommit` objects in historical order
18
+ */
19
+ export async function* showParsedGitHistory(
20
+ cwd: string,
21
+ options?: ShowGitHistoryOptions
22
+ ): AsyncGenerator<GitCommit> {
23
+ for await (const commit of showGitHistory(cwd, options)) {
24
+ try {
25
+ yield await getParsedGitCommit(cwd, commit)
26
+ } catch {}
27
+ }
28
+ }
@@ -0,0 +1,9 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // Copyright (c) 2026 Fai
3
+
4
+ export * from './branch'
5
+ export * from './commit'
6
+ export * from './history'
7
+ export * from './repository'
8
+ export * from './revision'
9
+ export * from './tag'
@@ -0,0 +1,50 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // Copyright (c) 2026 Fai
3
+
4
+ import type { GitAuthor } from '#types'
5
+ import { execGit } from '#util'
6
+ import { getGitConfigValue } from './getGitConfigValue'
7
+
8
+ /**
9
+ * Ensure Git user configuration is set and return a cleanup handle.
10
+ *
11
+ * Checks the existing `user.name` and `user.email` configuration for the given
12
+ * working directory. Updates the configuration if either value is missing and
13
+ * returns an `AsyncDisposable` resource that restores the original state upon
14
+ * disposal.
15
+ *
16
+ * @param cwd Working directory path in which to check and set Git configuration
17
+ * @param user Git author details containing name and email
18
+ * @returns Async disposable cleanup object if updated, or undefined if already set
19
+ */
20
+ export async function ensureGitUserConfig(
21
+ cwd: string,
22
+ user: GitAuthor
23
+ ): Promise<AsyncDisposable | undefined> {
24
+ const initialName = await getGitConfigValue(cwd, 'user.name')
25
+ const initialEmail = await getGitConfigValue(cwd, 'user.email')
26
+
27
+ if (initialName && initialEmail) {
28
+ return
29
+ }
30
+
31
+ // https://github.com/actions/checkout?tab=readme-ov-file#push-a-commit-using-the-built-in-token
32
+ await execGit(cwd, ['config', 'user.name', user.name])
33
+ await execGit(cwd, ['config', 'user.email', user.email])
34
+
35
+ return {
36
+ async [Symbol.asyncDispose]() {
37
+ if (initialName) {
38
+ await execGit(cwd, ['config', 'user.name', initialName])
39
+ } else {
40
+ await execGit(cwd, ['config', '--unset', 'user.name'])
41
+ }
42
+
43
+ if (initialEmail) {
44
+ await execGit(cwd, ['config', 'user.email', initialEmail])
45
+ } else {
46
+ await execGit(cwd, ['config', '--unset', 'user.email'])
47
+ }
48
+ },
49
+ }
50
+ }
@@ -0,0 +1,27 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // Copyright (c) 2026 Fai
3
+
4
+ import { execGit } from '#util'
5
+
6
+ /**
7
+ * Get a local git configuration value for `key` in the repository at `cwd`.
8
+ *
9
+ * Returns the config value when present; otherwise `undefined`.
10
+ *
11
+ * @param cwd Repository working directory
12
+ * @param key Git config key to read
13
+ * @returns The config value or `undefined` if not set
14
+ */
15
+ export async function getGitConfigValue(
16
+ cwd: string,
17
+ key: string
18
+ ): Promise<string | undefined> {
19
+ try {
20
+ const stdout = await execGit(cwd, ['config', '--local', '--get', key])
21
+ const value = stdout.trim()
22
+
23
+ return value ? value : undefined
24
+ } catch {
25
+ return undefined
26
+ }
27
+ }
@@ -0,0 +1,20 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // Copyright (c) 2026 Fai
3
+
4
+ import { execGit } from '#util'
5
+
6
+ /**
7
+ * Get the URL for the named remote (defaults to `origin`).
8
+ *
9
+ * @param cwd Repository working directory
10
+ * @param remoteName Remote name (defaults to `'origin'`)
11
+ * @returns Remote URL string or `null` when unavailable
12
+ */
13
+ export function getGitRemote(
14
+ cwd: string,
15
+ remoteName = 'origin'
16
+ ): Promise<string | null> {
17
+ return execGit(cwd, ['remote', 'get-url', remoteName])
18
+ .then((stdout) => stdout.trim())
19
+ .catch(() => null)
20
+ }
@@ -0,0 +1,10 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // Copyright (c) 2026 Fai
3
+
4
+ export * from './ensureGitUserConfig'
5
+ export * from './getGitConfigValue'
6
+ export * from './getGitRemote'
7
+ export * from './isGitShallowRepository'
8
+ export * from './pushGitChanges'
9
+ export * from './resolveGitRepository'
10
+ export * from './withShallowLock'
@@ -0,0 +1,21 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // Copyright (c) 2026 Fai
3
+
4
+ import { execGit } from '#util'
5
+
6
+ /**
7
+ * Determine whether the repository at `cwd` is a shallow clone.
8
+ *
9
+ * Runs `git rev-parse --is-shallow-repository` to detect shallow clones.
10
+ * Useful before operations that require full history.
11
+ *
12
+ * @param cwd Repository working directory
13
+ * @returns `true` if shallow, `false` if full clone, or `null` on error
14
+ */
15
+ export async function isGitShallowRepository(cwd: string): Promise<boolean> {
16
+ const stdout = (
17
+ await execGit(cwd, ['rev-parse', '--is-shallow-repository'])
18
+ ).trim()
19
+
20
+ return stdout === 'true'
21
+ }
@@ -0,0 +1,26 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // Copyright (c) 2026 Fai
3
+
4
+ import { compact, execGit } from '#util'
5
+
6
+ /**
7
+ * Push a branch and optional tags to the `origin` remote using an atomic push.
8
+ *
9
+ * @param cwd Repository working directory
10
+ * @param branch Branch name to push
11
+ * @param additionalTags Optional list of tag names to push alongside the branch
12
+ * @returns Promise that resolves when the push completes
13
+ */
14
+ export async function pushGitChanges(
15
+ cwd: string,
16
+ branch: string | null,
17
+ ...additionalTags: readonly string[]
18
+ ): Promise<void> {
19
+ await execGit(cwd, [
20
+ 'push',
21
+ 'origin',
22
+ ...compact([branch, ...additionalTags]),
23
+ '--atomic',
24
+ '--no-verify',
25
+ ])
26
+ }
@@ -0,0 +1,40 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // Copyright (c) 2026 Fai
3
+
4
+ import type { GitRepository } from '#types'
5
+ import GitHost from 'hosted-git-info'
6
+ import { getGitRemote } from './getGitRemote'
7
+
8
+ /**
9
+ * Resolve Git repository host, owner, and project metadata from the `origin` remote URL.
10
+ *
11
+ * @param cwd Repository working directory
12
+ * @returns Promise that resolves to a `GitRepository` object, or `null` when unavailable
13
+ */
14
+ export async function resolveGitRepository(
15
+ cwd: string
16
+ ): Promise<GitRepository | null> {
17
+ const origin = await getGitRemote(cwd)
18
+
19
+ if (!origin) {
20
+ return null
21
+ }
22
+
23
+ const info = GitHost.fromUrl(origin)
24
+
25
+ if (
26
+ info &&
27
+ typeof info.type === 'string' &&
28
+ typeof info.user === 'string' &&
29
+ typeof info.project === 'string'
30
+ ) {
31
+ return {
32
+ host: info.type,
33
+ owner: info.user,
34
+ repo: info.project,
35
+ url: info.browse(),
36
+ }
37
+ }
38
+
39
+ return null
40
+ }
@@ -0,0 +1,62 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // Copyright (c) 2026 Fai
3
+
4
+ import { AsyncLocalStorage } from 'node:async_hooks'
5
+ import { isGitShallowRepository } from './isGitShallowRepository'
6
+
7
+ const lockQueue = new Map<string, Promise<void>>()
8
+
9
+ const context = new AsyncLocalStorage<Set<string>>()
10
+
11
+ /**
12
+ * Serialize access to a shallow Git repository while running a callback.
13
+ *
14
+ * If `cwd` is a shallow clone, callers from different async contexts are
15
+ * executed one at a time; nested calls within the same async context run
16
+ * immediately without waiting. If the repository becomes non-shallow before
17
+ * a queued turn runs, downstream waiters are released early.
18
+ *
19
+ * @param cwd Repository working directory path string
20
+ * @param callback Function to execute while the lock is held
21
+ * @returns Promise resolving to the result of `callback`
22
+ */
23
+ export async function withShallowLock<T>(
24
+ cwd: string,
25
+ callback: () => T | Promise<T>
26
+ ): Promise<T> {
27
+ if (!(await isGitShallowRepository(cwd))) {
28
+ return callback()
29
+ }
30
+
31
+ const active = context.getStore()
32
+
33
+ if (active?.has(cwd)) {
34
+ return callback()
35
+ }
36
+
37
+ const { promise, resolve } = Promise.withResolvers<void>()
38
+ const previous = lockQueue.get(cwd) ?? Promise.resolve()
39
+ const queued = previous.then(() => promise)
40
+
41
+ lockQueue.set(cwd, queued)
42
+
43
+ // Wait until previous tasks complete.
44
+ await previous
45
+
46
+ // If an earlier task made the repo non-shallow, release downstream waiters now.
47
+ if (!(await isGitShallowRepository(cwd))) {
48
+ resolve()
49
+ }
50
+
51
+ try {
52
+ return await context.run(new Set(active).add(cwd), callback)
53
+ } finally {
54
+ // Ensure downstream waiters are unblocked even if callback throws.
55
+ resolve()
56
+
57
+ // Remove the queue entry only if no newer task has replaced it.
58
+ if (lockQueue.get(cwd) === queued) {
59
+ lockQueue.delete(cwd)
60
+ }
61
+ }
62
+ }
@@ -0,0 +1,43 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // Copyright (c) 2026 Fai
3
+
4
+ import { isGitAncestor } from './isGitAncestor'
5
+
6
+ export const GIT_REL_FLAGS = {
7
+ NONE: 0,
8
+ IS_ANCESTOR: 1,
9
+ IS_DESCENDANT: 2,
10
+ IS_EQUAL: 3,
11
+ } as const
12
+
13
+ export type GitRelFlags = (typeof GIT_REL_FLAGS)[keyof typeof GIT_REL_FLAGS]
14
+
15
+ /**
16
+ * Determine the relationship between two git refs.
17
+ *
18
+ * Runs `git merge-base --is-ancestor` in both directions and returns a
19
+ * numeric bitmask using `GIT_REL_FLAGS`. A successful check (exit code 0)
20
+ * indicates an ancestor relationship; non-zero exit is treated as "not ancestor".
21
+ *
22
+ * @param cwd Repository working directory
23
+ * @param refA First ref (candidate ancestor)
24
+ * @param refB Second ref (candidate descendant)
25
+ * @returns Bitmask of type `GitRelFlags`
26
+ */
27
+ export async function detectGitRelationship(
28
+ cwd: string,
29
+ refA: string,
30
+ refB: string
31
+ ): Promise<GitRelFlags> {
32
+ let flags = GIT_REL_FLAGS.NONE
33
+
34
+ const [aIsAncestor, bIsAncestor] = await Promise.all([
35
+ isGitAncestor(cwd, refA, refB),
36
+ isGitAncestor(cwd, refB, refA),
37
+ ])
38
+
39
+ if (aIsAncestor) flags |= GIT_REL_FLAGS.IS_ANCESTOR
40
+ if (bIsAncestor) flags |= GIT_REL_FLAGS.IS_DESCENDANT
41
+
42
+ return flags as GitRelFlags
43
+ }
@@ -0,0 +1,7 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // Copyright (c) 2026 Fai
3
+
4
+ export * from './detectGitRelationship'
5
+ export * from './isGitAncestor'
6
+ export * from './readGitFile'
7
+ export * from './resolveGitRevision'
@@ -0,0 +1,33 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // Copyright (c) 2026 Fai
3
+
4
+ import { execGit } from '#util'
5
+
6
+ /**
7
+ * Determine whether `ancestor` is an ancestor of `descendant`.
8
+ *
9
+ * Runs `git merge-base --is-ancestor <ancestor> <descendant>` in the repository at `cwd`.
10
+ * Resolves to `true` if `ancestor` is an ancestor, `false` if not.
11
+ * Rethrows errors for invalid refs or other failures.
12
+ *
13
+ * @param cwd Repository working directory
14
+ * @param ancestor Candidate ancestor ref
15
+ * @param descendant Candidate descendant ref
16
+ * @returns Resolves to `true` if `ancestor` is an ancestor of `descendant`, `false` otherwise
17
+ * @throws If a ref is invalid or the Git command fails
18
+ */
19
+ export function isGitAncestor(
20
+ cwd: string,
21
+ ancestor: string,
22
+ descendant: string
23
+ ): Promise<boolean> {
24
+ return execGit(cwd, ['merge-base', '--is-ancestor', ancestor, descendant])
25
+ .then(() => true)
26
+ .catch((err) => {
27
+ if ((err as { code?: number })?.code === 1) {
28
+ return false
29
+ }
30
+
31
+ throw err
32
+ })
33
+ }