Read-only mirror — the public repositories of the Forgejo instance, refreshed every 10 minutes. The live instance (fg.dieschoenewolke.de) is reachable only over the VPN; clone here with https://git.dieschoenewolke.de/git/<owner>/<repo>.git.
summaryrefslogtreecommitdiff
path: root/src/ref-helper.ts
diff options
context:
space:
mode:
authoreric sciple <ericsciple@users.noreply.github.com>2019-12-03 10:28:59 -0500
committerGitHub <noreply@github.com>2019-12-03 10:28:59 -0500
commite404ba5989baa550f631fb1262539d1375a856b7 (patch)
tree4cc911589e7c9a4c976bc99eb4dcb886d2b2d8a8 /src/ref-helper.ts
parent6a11491f2eea6739d1296d604c5402cffa43a1f1 (diff)
Convert checkout to a regular action (#70)
Diffstat (limited to 'src/ref-helper.ts')
-rw-r--r--src/ref-helper.ts109
1 files changed, 109 insertions, 0 deletions
diff --git a/src/ref-helper.ts b/src/ref-helper.ts
new file mode 100644
index 0000000..ff256af
--- /dev/null
+++ b/src/ref-helper.ts
@@ -0,0 +1,109 @@
+import {IGitCommandManager} from './git-command-manager'
+
+export interface ICheckoutInfo {
+ ref: string
+ startPoint: string
+}
+
+export async function getCheckoutInfo(
+ git: IGitCommandManager,
+ ref: string,
+ commit: string
+): Promise<ICheckoutInfo> {
+ if (!git) {
+ throw new Error('Arg git cannot be empty')
+ }
+
+ if (!ref && !commit) {
+ throw new Error('Args ref and commit cannot both be empty')
+ }
+
+ const result = ({} as unknown) as ICheckoutInfo
+ const upperRef = (ref || '').toUpperCase()
+
+ // SHA only
+ if (!ref) {
+ result.ref = commit
+ }
+ // refs/heads/
+ else if (upperRef.startsWith('REFS/HEADS/')) {
+ const branch = ref.substring('refs/heads/'.length)
+ result.ref = branch
+ result.startPoint = `refs/remotes/origin/${branch}`
+ }
+ // refs/pull/
+ else if (upperRef.startsWith('REFS/PULL/')) {
+ const branch = ref.substring('refs/pull/'.length)
+ result.ref = `refs/remotes/pull/${branch}`
+ }
+ // refs/tags/
+ else if (upperRef.startsWith('REFS/')) {
+ result.ref = ref
+ }
+ // Unqualified ref, check for a matching branch or tag
+ else {
+ if (await git.branchExists(true, `origin/${ref}`)) {
+ result.ref = ref
+ result.startPoint = `refs/remotes/origin/${ref}`
+ } else if (await git.tagExists(`${ref}`)) {
+ result.ref = `refs/tags/${ref}`
+ } else {
+ throw new Error(
+ `A branch or tag with the name '${ref}' could not be found`
+ )
+ }
+ }
+
+ return result
+}
+
+export function getRefSpec(ref: string, commit: string): string[] {
+ if (!ref && !commit) {
+ throw new Error('Args ref and commit cannot both be empty')
+ }
+
+ const upperRef = (ref || '').toUpperCase()
+
+ // SHA
+ if (commit) {
+ // refs/heads
+ if (upperRef.startsWith('REFS/HEADS/')) {
+ const branch = ref.substring('refs/heads/'.length)
+ return [`+${commit}:refs/remotes/origin/${branch}`]
+ }
+ // refs/pull/
+ else if (upperRef.startsWith('REFS/PULL/')) {
+ const branch = ref.substring('refs/pull/'.length)
+ return [`+${commit}:refs/remotes/pull/${branch}`]
+ }
+ // refs/tags/
+ else if (upperRef.startsWith('REFS/TAGS/')) {
+ return [`+${commit}:${ref}`]
+ }
+ // Otherwise no destination ref
+ else {
+ return [commit]
+ }
+ }
+ // Unqualified ref, check for a matching branch or tag
+ else if (!upperRef.startsWith('REFS/')) {
+ return [
+ `+refs/heads/${ref}*:refs/remotes/origin/${ref}*`,
+ `+refs/tags/${ref}*:refs/tags/${ref}*`
+ ]
+ }
+ // refs/heads/
+ else if (upperRef.startsWith('REFS/HEADS/')) {
+ const branch = ref.substring('refs/heads/'.length)
+ return [`+${ref}:refs/remotes/origin/${branch}`]
+ }
+ // refs/pull/
+ else if (upperRef.startsWith('REFS/PULL/')) {
+ const branch = ref.substring('refs/pull/'.length)
+ return [`+${ref}:refs/remotes/pull/${branch}`]
+ }
+ // refs/tags/
+ else {
+ return [`+${ref}:${ref}`]
+ }
+}