2020-09-29 22:32:49 +00:00
|
|
|
import exec from './exec'
|
2020-07-11 15:17:56 +00:00
|
|
|
import * as core from '@actions/core'
|
|
|
|
import {File, ChangeStatus} from './file'
|
2020-05-26 15:16:09 +00:00
|
|
|
|
2020-06-24 19:53:31 +00:00
|
|
|
export const NULL_SHA = '0000000000000000000000000000000000000000'
|
2020-11-22 19:59:32 +00:00
|
|
|
export const HEAD = 'HEAD'
|
2020-06-24 19:53:31 +00:00
|
|
|
|
2020-09-29 22:32:49 +00:00
|
|
|
export async function getChangesInLastCommit(): Promise<File[]> {
|
|
|
|
core.startGroup(`Change detection in last commit`)
|
|
|
|
let output = ''
|
|
|
|
try {
|
|
|
|
output = (await exec('git', ['log', '--format=', '--no-renames', '--name-status', '-z', '-n', '1'])).stdout
|
|
|
|
} finally {
|
|
|
|
fixStdOutNullTermination()
|
|
|
|
core.endGroup()
|
|
|
|
}
|
|
|
|
|
|
|
|
return parseGitDiffOutput(output)
|
|
|
|
}
|
2020-09-01 20:47:38 +00:00
|
|
|
|
2021-03-08 14:09:07 +00:00
|
|
|
export async function getChanges(baseRef: string): Promise<File[]> {
|
|
|
|
if (!(await hasCommit(baseRef))) {
|
2020-09-29 22:32:49 +00:00
|
|
|
// Fetch single commit
|
2021-03-08 14:09:07 +00:00
|
|
|
core.startGroup(`Fetching ${baseRef} from origin`)
|
|
|
|
await exec('git', ['fetch', '--depth=1', '--no-tags', 'origin', baseRef])
|
2020-09-29 22:32:49 +00:00
|
|
|
core.endGroup()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get differences between ref and HEAD
|
2021-03-08 14:09:07 +00:00
|
|
|
core.startGroup(`Change detection ${baseRef}..HEAD`)
|
2020-09-01 20:47:38 +00:00
|
|
|
let output = ''
|
|
|
|
try {
|
|
|
|
// Two dots '..' change detection - directly compares two versions
|
2021-03-08 14:09:07 +00:00
|
|
|
output = (await exec('git', ['diff', '--no-renames', '--name-status', '-z', `${baseRef}..HEAD`])).stdout
|
2020-09-01 20:47:38 +00:00
|
|
|
} finally {
|
|
|
|
fixStdOutNullTermination()
|
|
|
|
core.endGroup()
|
2020-05-26 15:16:09 +00:00
|
|
|
}
|
2020-09-01 20:47:38 +00:00
|
|
|
|
|
|
|
return parseGitDiffOutput(output)
|
2020-05-26 15:16:09 +00:00
|
|
|
}
|
|
|
|
|
2020-11-22 19:59:32 +00:00
|
|
|
export async function getChangesOnHead(): Promise<File[]> {
|
|
|
|
// Get current changes - both staged and unstaged
|
|
|
|
core.startGroup(`Change detection on HEAD`)
|
|
|
|
let output = ''
|
|
|
|
try {
|
|
|
|
output = (await exec('git', ['diff', '--no-renames', '--name-status', '-z', 'HEAD'])).stdout
|
|
|
|
} finally {
|
|
|
|
fixStdOutNullTermination()
|
|
|
|
core.endGroup()
|
|
|
|
}
|
|
|
|
|
|
|
|
return parseGitDiffOutput(output)
|
|
|
|
}
|
|
|
|
|
2021-04-07 20:32:59 +00:00
|
|
|
export async function getChangesSinceMergeBase(base: string, head: string, initialFetchDepth: number): Promise<File[]> {
|
2021-03-25 22:34:50 +00:00
|
|
|
let baseRef: string | undefined
|
2021-04-07 20:32:59 +00:00
|
|
|
let headRef: string | undefined
|
2020-09-01 20:47:38 +00:00
|
|
|
async function hasMergeBase(): Promise<boolean> {
|
2021-04-07 20:32:59 +00:00
|
|
|
if (baseRef === undefined || headRef === undefined) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return (await exec('git', ['merge-base', baseRef, headRef], {ignoreReturnCode: true})).code === 0
|
2020-05-26 15:16:09 +00:00
|
|
|
}
|
|
|
|
|
2021-03-08 14:09:07 +00:00
|
|
|
let noMergeBase = false
|
2021-04-07 20:41:22 +00:00
|
|
|
core.startGroup(`Searching for merge-base ${base}...${head}`)
|
2021-03-08 14:09:07 +00:00
|
|
|
try {
|
2021-03-25 22:34:50 +00:00
|
|
|
baseRef = await getFullRef(base)
|
2021-04-07 20:32:59 +00:00
|
|
|
headRef = await getFullRef(head)
|
2021-03-09 20:44:15 +00:00
|
|
|
if (!(await hasMergeBase())) {
|
2021-04-07 20:32:59 +00:00
|
|
|
await exec('git', ['fetch', '--no-tags', `--depth=${initialFetchDepth}`, 'origin', base, head])
|
|
|
|
if (baseRef === undefined || headRef === undefined) {
|
|
|
|
baseRef = baseRef ?? (await getFullRef(base))
|
|
|
|
headRef = headRef ?? (await getFullRef(head))
|
|
|
|
if (baseRef === undefined || headRef === undefined) {
|
|
|
|
await exec('git', ['fetch', '--tags', '--depth=1', 'origin', base, head], {
|
2021-03-29 15:09:18 +00:00
|
|
|
ignoreReturnCode: true // returns exit code 1 if tags on remote were updated - we can safely ignore it
|
|
|
|
})
|
2021-04-07 20:32:59 +00:00
|
|
|
baseRef = baseRef ?? (await getFullRef(base))
|
|
|
|
headRef = headRef ?? (await getFullRef(head))
|
2021-03-25 22:34:50 +00:00
|
|
|
if (baseRef === undefined) {
|
|
|
|
throw new Error(`Could not determine what is ${base} - fetch works but it's not a branch or tag`)
|
|
|
|
}
|
2021-04-07 20:32:59 +00:00
|
|
|
if (headRef === undefined) {
|
|
|
|
throw new Error(`Could not determine what is ${head} - fetch works but it's not a branch or tag`)
|
|
|
|
}
|
2021-03-25 22:34:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-09 20:44:15 +00:00
|
|
|
let depth = initialFetchDepth
|
|
|
|
let lastCommitCount = await getCommitCount()
|
|
|
|
while (!(await hasMergeBase())) {
|
|
|
|
depth = Math.min(depth * 2, Number.MAX_SAFE_INTEGER)
|
2021-04-07 20:32:59 +00:00
|
|
|
await exec('git', ['fetch', `--deepen=${depth}`, 'origin', base, head])
|
2021-03-09 20:44:15 +00:00
|
|
|
const commitCount = await getCommitCount()
|
|
|
|
if (commitCount === lastCommitCount) {
|
|
|
|
core.info('No more commits were fetched')
|
|
|
|
core.info('Last attempt will be to fetch full history')
|
|
|
|
await exec('git', ['fetch'])
|
|
|
|
if (!(await hasMergeBase())) {
|
|
|
|
noMergeBase = true
|
|
|
|
}
|
|
|
|
break
|
2021-03-08 14:09:07 +00:00
|
|
|
}
|
2021-03-09 20:44:15 +00:00
|
|
|
lastCommitCount = commitCount
|
2021-03-08 14:09:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
core.endGroup()
|
2020-09-01 20:47:38 +00:00
|
|
|
}
|
|
|
|
|
2021-04-11 18:49:05 +00:00
|
|
|
// Three dots '...' change detection - finds merge-base and compares against it
|
2021-04-07 20:32:59 +00:00
|
|
|
let diffArg = `${baseRef}...${headRef}`
|
2021-03-08 14:09:07 +00:00
|
|
|
if (noMergeBase) {
|
2021-03-25 22:34:50 +00:00
|
|
|
core.warning('No merge base found - change detection will use direct <commit>..<commit> comparison')
|
2021-04-07 20:32:59 +00:00
|
|
|
diffArg = `${baseRef}..${headRef}`
|
2020-09-01 20:47:38 +00:00
|
|
|
}
|
2020-07-11 15:17:56 +00:00
|
|
|
|
2021-03-25 22:34:50 +00:00
|
|
|
// Get changes introduced on ref compared to base
|
|
|
|
core.startGroup(`Change detection ${diffArg}`)
|
2020-09-01 20:47:38 +00:00
|
|
|
let output = ''
|
|
|
|
try {
|
2021-03-25 22:34:50 +00:00
|
|
|
output = (await exec('git', ['diff', '--no-renames', '--name-status', '-z', diffArg])).stdout
|
2020-09-01 20:47:38 +00:00
|
|
|
} finally {
|
|
|
|
fixStdOutNullTermination()
|
|
|
|
core.endGroup()
|
|
|
|
}
|
|
|
|
|
|
|
|
return parseGitDiffOutput(output)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function parseGitDiffOutput(output: string): File[] {
|
2020-07-11 15:17:56 +00:00
|
|
|
const tokens = output.split('\u0000').filter(s => s.length > 0)
|
|
|
|
const files: File[] = []
|
|
|
|
for (let i = 0; i + 1 < tokens.length; i += 2) {
|
|
|
|
files.push({
|
|
|
|
status: statusMap[tokens[i]],
|
|
|
|
filename: tokens[i + 1]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return files
|
2020-05-26 15:16:09 +00:00
|
|
|
}
|
2020-06-24 19:53:31 +00:00
|
|
|
|
2020-09-01 20:47:38 +00:00
|
|
|
export async function listAllFilesAsAdded(): Promise<File[]> {
|
|
|
|
core.startGroup('Listing all files tracked by git')
|
|
|
|
let output = ''
|
|
|
|
try {
|
2020-09-29 22:32:49 +00:00
|
|
|
output = (await exec('git', ['ls-files', '-z'])).stdout
|
2020-09-01 20:47:38 +00:00
|
|
|
} finally {
|
|
|
|
fixStdOutNullTermination()
|
|
|
|
core.endGroup()
|
|
|
|
}
|
|
|
|
|
|
|
|
return output
|
|
|
|
.split('\u0000')
|
|
|
|
.filter(s => s.length > 0)
|
|
|
|
.map(path => ({
|
|
|
|
status: ChangeStatus.Added,
|
|
|
|
filename: path
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2020-09-29 22:32:49 +00:00
|
|
|
export async function getCurrentRef(): Promise<string> {
|
|
|
|
core.startGroup(`Determining current ref`)
|
|
|
|
try {
|
|
|
|
const branch = (await exec('git', ['branch', '--show-current'])).stdout.trim()
|
|
|
|
if (branch) {
|
|
|
|
return branch
|
|
|
|
}
|
|
|
|
|
|
|
|
const describe = await exec('git', ['describe', '--tags', '--exact-match'], {ignoreReturnCode: true})
|
|
|
|
if (describe.code === 0) {
|
|
|
|
return describe.stdout.trim()
|
|
|
|
}
|
|
|
|
|
2021-03-08 14:09:07 +00:00
|
|
|
return (await exec('git', ['rev-parse', HEAD])).stdout.trim()
|
2020-09-29 22:32:49 +00:00
|
|
|
} finally {
|
|
|
|
core.endGroup()
|
|
|
|
}
|
2020-06-24 19:53:31 +00:00
|
|
|
}
|
|
|
|
|
2020-09-29 22:32:49 +00:00
|
|
|
export function getShortName(ref: string): string {
|
|
|
|
if (!ref) return ''
|
|
|
|
|
|
|
|
const heads = 'refs/heads/'
|
|
|
|
const tags = 'refs/tags/'
|
|
|
|
|
|
|
|
if (ref.startsWith(heads)) return ref.slice(heads.length)
|
|
|
|
if (ref.startsWith(tags)) return ref.slice(tags.length)
|
|
|
|
|
|
|
|
return ref
|
2020-06-24 19:53:31 +00:00
|
|
|
}
|
|
|
|
|
2020-10-16 10:28:12 +00:00
|
|
|
export function isGitSha(ref: string): boolean {
|
|
|
|
return /^[a-z0-9]{40}$/.test(ref)
|
|
|
|
}
|
|
|
|
|
2020-09-29 22:32:49 +00:00
|
|
|
async function hasCommit(ref: string): Promise<boolean> {
|
|
|
|
core.startGroup(`Checking if commit for ${ref} is locally available`)
|
|
|
|
try {
|
|
|
|
return (await exec('git', ['cat-file', '-e', `${ref}^{commit}`], {ignoreReturnCode: true})).code === 0
|
|
|
|
} finally {
|
|
|
|
core.endGroup()
|
|
|
|
}
|
2020-06-24 19:53:31 +00:00
|
|
|
}
|
|
|
|
|
2021-03-08 14:09:07 +00:00
|
|
|
async function getCommitCount(): Promise<number> {
|
|
|
|
const output = (await exec('git', ['rev-list', '--count', '--all'])).stdout
|
2020-09-01 20:47:38 +00:00
|
|
|
const count = parseInt(output)
|
|
|
|
return isNaN(count) ? 0 : count
|
|
|
|
}
|
|
|
|
|
2021-03-25 22:39:10 +00:00
|
|
|
async function getFullRef(shortName: string): Promise<string | undefined> {
|
|
|
|
if (isGitSha(shortName)) {
|
2021-03-25 22:34:50 +00:00
|
|
|
return shortName
|
|
|
|
}
|
|
|
|
|
2021-03-25 23:05:48 +00:00
|
|
|
const output = (await exec('git', ['show-ref', shortName], {ignoreReturnCode: true})).stdout
|
|
|
|
const refs = output
|
|
|
|
.split(/\r?\n/g)
|
|
|
|
.map(l => l.match(/refs\/.*$/)?.[0] ?? '')
|
|
|
|
.filter(l => l !== '')
|
|
|
|
|
|
|
|
if (refs.length === 0) {
|
|
|
|
return undefined
|
2021-03-25 22:34:50 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 23:05:48 +00:00
|
|
|
const remoteRef = refs.find(ref => ref.startsWith('refs/remotes/origin/'))
|
|
|
|
if (remoteRef) {
|
|
|
|
return remoteRef
|
|
|
|
}
|
2021-03-25 22:34:50 +00:00
|
|
|
|
2021-03-25 23:05:48 +00:00
|
|
|
return refs[0]
|
2021-03-25 22:34:50 +00:00
|
|
|
}
|
|
|
|
|
2020-09-01 20:47:38 +00:00
|
|
|
function fixStdOutNullTermination(): void {
|
|
|
|
// Previous command uses NULL as delimiters and output is printed to stdout.
|
|
|
|
// We have to make sure next thing written to stdout will start on new line.
|
|
|
|
// Otherwise things like ::set-output wouldn't work.
|
|
|
|
core.info('')
|
|
|
|
}
|
|
|
|
|
2020-07-11 15:17:56 +00:00
|
|
|
const statusMap: {[char: string]: ChangeStatus} = {
|
|
|
|
A: ChangeStatus.Added,
|
|
|
|
C: ChangeStatus.Copied,
|
|
|
|
D: ChangeStatus.Deleted,
|
|
|
|
M: ChangeStatus.Modified,
|
|
|
|
R: ChangeStatus.Renamed,
|
|
|
|
U: ChangeStatus.Unmerged
|
|
|
|
}
|