mirror of
https://github.com/dorny/paths-filter.git
synced 2025-06-08 00:59:04 +00:00
Extend filter syntax with optional specification of file status: add, modified, deleted (#22)
* Add support for specification of change type (add,modified,delete) * Use NULL as separator in git-diff command output * Improve PR test workflow * Fix the workflow file
This commit is contained in:
parent
caef9bef1f
commit
1ff702da35
11 changed files with 397 additions and 90 deletions
33
src/git.ts
33
src/git.ts
|
@ -1,4 +1,6 @@
|
|||
import {exec} from '@actions/exec'
|
||||
import * as core from '@actions/core'
|
||||
import {File, ChangeStatus} from './file'
|
||||
|
||||
export const NULL_SHA = '0000000000000000000000000000000000000000'
|
||||
export const FETCH_HEAD = 'FETCH_HEAD'
|
||||
|
@ -10,9 +12,9 @@ export async function fetchCommit(ref: string): Promise<void> {
|
|||
}
|
||||
}
|
||||
|
||||
export async function getChangedFiles(ref: string): Promise<string[]> {
|
||||
export async function getChangedFiles(ref: string, cmd = exec): Promise<File[]> {
|
||||
let output = ''
|
||||
const exitCode = await exec('git', ['diff-index', '--name-only', ref], {
|
||||
const exitCode = await cmd('git', ['diff-index', '--name-status', '-z', ref], {
|
||||
listeners: {
|
||||
stdout: (data: Buffer) => (output += data.toString())
|
||||
}
|
||||
|
@ -22,10 +24,20 @@ export async function getChangedFiles(ref: string): Promise<string[]> {
|
|||
throw new Error(`Couldn't determine changed files`)
|
||||
}
|
||||
|
||||
return output
|
||||
.split('\n')
|
||||
.map(s => s.trim())
|
||||
.filter(s => s.length > 0)
|
||||
// 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
|
||||
}
|
||||
|
||||
export function isTagRef(ref: string): boolean {
|
||||
|
@ -44,3 +56,12 @@ export function trimRefsHeads(ref: string): string {
|
|||
function trimStart(ref: string, start: string): string {
|
||||
return ref.startsWith(start) ? ref.substr(start.length) : ref
|
||||
}
|
||||
|
||||
const statusMap: {[char: string]: ChangeStatus} = {
|
||||
A: ChangeStatus.Added,
|
||||
C: ChangeStatus.Copied,
|
||||
D: ChangeStatus.Deleted,
|
||||
M: ChangeStatus.Modified,
|
||||
R: ChangeStatus.Renamed,
|
||||
U: ChangeStatus.Unmerged
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue