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:
Diffstat (limited to 'src/ref-helper.ts')
-rw-r--r--src/ref-helper.ts66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/ref-helper.ts b/src/ref-helper.ts
index f600179..381fa60 100644
--- a/src/ref-helper.ts
+++ b/src/ref-helper.ts
@@ -3,6 +3,8 @@ import {IGitCommandManager} from './git-command-manager'
import * as core from '@actions/core'
import * as github from '@actions/github'
+export const tagsRefSpec = '+refs/tags/*:refs/tags/*'
+
export interface ICheckoutInfo {
ref: string
startPoint: string
@@ -60,6 +62,16 @@ export async function getCheckoutInfo(
return result
}
+export function getRefSpecForAllHistory(ref: string, commit: string): string[] {
+ const result = ['+refs/heads/*:refs/remotes/origin/*', tagsRefSpec]
+ if (ref && ref.toUpperCase().startsWith('REFS/PULL/')) {
+ const branch = ref.substring('refs/pull/'.length)
+ result.push(`+${commit || ref}:refs/remotes/pull/${branch}`)
+ }
+
+ return result
+}
+
export function getRefSpec(ref: string, commit: string): string[] {
if (!ref && !commit) {
throw new Error('Args ref and commit cannot both be empty')
@@ -111,6 +123,60 @@ export function getRefSpec(ref: string, commit: string): string[] {
}
}
+/**
+ * Tests whether the initial fetch created the ref at the expected commit
+ */
+export async function testRef(
+ git: IGitCommandManager,
+ ref: string,
+ commit: string
+): Promise<boolean> {
+ if (!git) {
+ throw new Error('Arg git cannot be empty')
+ }
+
+ if (!ref && !commit) {
+ throw new Error('Args ref and commit cannot both be empty')
+ }
+
+ // No SHA? Nothing to test
+ if (!commit) {
+ return true
+ }
+ // SHA only?
+ else if (!ref) {
+ return await git.shaExists(commit)
+ }
+
+ const upperRef = ref.toUpperCase()
+
+ // refs/heads/
+ if (upperRef.startsWith('REFS/HEADS/')) {
+ const branch = ref.substring('refs/heads/'.length)
+ return (
+ (await git.branchExists(true, `origin/${branch}`)) &&
+ commit === (await git.revParse(`refs/remotes/origin/${branch}`))
+ )
+ }
+ // refs/pull/
+ else if (upperRef.startsWith('REFS/PULL/')) {
+ // Assume matches because fetched using the commit
+ return true
+ }
+ // refs/tags/
+ else if (upperRef.startsWith('REFS/TAGS/')) {
+ const tagName = ref.substring('refs/tags/'.length)
+ return (
+ (await git.tagExists(tagName)) && commit === (await git.revParse(ref))
+ )
+ }
+ // Unexpected
+ else {
+ core.debug(`Unexpected ref format '${ref}' when testing ref info`)
+ return true
+ }
+}
+
export async function checkCommitInfo(
token: string,
commitInfo: string,