2020-05-26 15:16:09 +00:00
|
|
|
import {exec} from '@actions/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'
|
|
|
|
export const FETCH_HEAD = 'FETCH_HEAD'
|
|
|
|
|
|
|
|
export async function fetchCommit(ref: string): Promise<void> {
|
|
|
|
const exitCode = await exec('git', ['fetch', '--depth=1', '--no-tags', 'origin', ref])
|
2020-05-26 15:16:09 +00:00
|
|
|
if (exitCode !== 0) {
|
2020-06-24 19:53:31 +00:00
|
|
|
throw new Error(`Fetching ${ref} failed`)
|
2020-05-26 15:16:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-11 15:17:56 +00:00
|
|
|
export async function getChangedFiles(ref: string, cmd = exec): Promise<File[]> {
|
2020-05-26 15:16:09 +00:00
|
|
|
let output = ''
|
2020-07-11 15:17:56 +00:00
|
|
|
const exitCode = await cmd('git', ['diff-index', '--name-status', '-z', ref], {
|
2020-05-26 15:16:09 +00:00
|
|
|
listeners: {
|
|
|
|
stdout: (data: Buffer) => (output += data.toString())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if (exitCode !== 0) {
|
2020-06-15 19:49:10 +00:00
|
|
|
throw new Error(`Couldn't determine changed files`)
|
2020-05-26 15:16:09 +00:00
|
|
|
}
|
|
|
|
|
2020-07-11 15:17:56 +00:00
|
|
|
// 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('')
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
export function isTagRef(ref: string): boolean {
|
|
|
|
return ref.startsWith('refs/tags/')
|
|
|
|
}
|
|
|
|
|
|
|
|
export function trimRefs(ref: string): string {
|
|
|
|
return trimStart(ref, 'refs/')
|
|
|
|
}
|
|
|
|
|
|
|
|
export function trimRefsHeads(ref: string): string {
|
|
|
|
const trimRef = trimStart(ref, 'refs/')
|
|
|
|
return trimStart(trimRef, 'heads/')
|
|
|
|
}
|
|
|
|
|
|
|
|
function trimStart(ref: string, start: string): string {
|
|
|
|
return ref.startsWith(start) ? ref.substr(start.length) : ref
|
|
|
|
}
|
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
|
|
|
|
}
|