13
0
Fork 0
mirror of https://github.com/dorny/paths-filter.git synced 2026-06-28 20:30:43 +00:00
This commit is contained in:
Zydney Ambat 2026-06-24 22:26:55 +00:00 committed by GitHub
commit 8316ec3307
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 112 additions and 3 deletions

View file

@ -0,0 +1,95 @@
import * as git from '../src/git'
import * as exec from '@actions/exec'
jest.mock('@actions/exec')
const mockedGetExecOutput = jest.mocked(exec.getExecOutput)
beforeEach(() => {
mockedGetExecOutput.mockReset()
})
describe('resolveRefToSha', () => {
test('resolves a branch name to its commit SHA', async () => {
mockedGetExecOutput.mockResolvedValue({
exitCode: 0,
stdout: '8b399ed1681b9efd6b1e048ca1c5cba47edf3855\n',
stderr: ''
})
const sha = await git.resolveRefToSha('master')
expect(sha).toBe('8b399ed1681b9efd6b1e048ca1c5cba47edf3855')
expect(mockedGetExecOutput).toHaveBeenCalledWith('git', ['rev-parse', 'master'])
})
test('resolves a tag to its commit SHA', async () => {
mockedGetExecOutput.mockResolvedValue({
exitCode: 0,
stdout: '8b399ed1681b9efd6b1e048ca1c5cba47edf3855\n',
stderr: ''
})
const sha = await git.resolveRefToSha('release-18')
expect(sha).toBe('8b399ed1681b9efd6b1e048ca1c5cba47edf3855')
expect(mockedGetExecOutput).toHaveBeenCalledWith('git', ['rev-parse', 'release-18'])
})
test('returns SHA as-is when given a full SHA', async () => {
const fullSha = '8b399ed1681b9efd6b1e048ca1c5cba47edf3855'
mockedGetExecOutput.mockResolvedValue({
exitCode: 0,
stdout: `${fullSha}\n`,
stderr: ''
})
const sha = await git.resolveRefToSha(fullSha)
expect(sha).toBe(fullSha)
})
})
describe('isBaseSameAsHead with SHA comparison', () => {
test('different ref names pointing to same commit are treated as same', async () => {
const sameSha = '8b399ed1681b9efd6b1e048ca1c5cba47edf3855'
mockedGetExecOutput.mockResolvedValue({
exitCode: 0,
stdout: `${sameSha}\n`,
stderr: ''
})
const base: string = 'master'
const head: string = 'release-18'
const baseSha = await git.resolveRefToSha(base)
const headSha = await git.resolveRefToSha(head)
const isBaseSameAsHead = base === head || baseSha === headSha
expect(base === head).toBe(false) // ref names differ
expect(isBaseSameAsHead).toBe(true) // but SHAs match
})
test('different ref names pointing to different commits are not same', async () => {
mockedGetExecOutput
.mockResolvedValueOnce({
exitCode: 0,
stdout: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n',
stderr: ''
})
.mockResolvedValueOnce({
exitCode: 0,
stdout: 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n',
stderr: ''
})
const base: string = 'master'
const head: string = 'feature/xyz'
const baseSha = await git.resolveRefToSha(base)
const headSha = await git.resolveRefToSha(head)
const isBaseSameAsHead = base === head || baseSha === headSha
expect(isBaseSameAsHead).toBe(false)
})
test('same ref names are detected without SHA resolution', async () => {
const base: string = 'master'
const head: string = 'master'
const isBaseSameAsHead = base === head
expect(isBaseSameAsHead).toBe(true)
expect(mockedGetExecOutput).not.toHaveBeenCalled()
})
})

11
dist/index.js vendored
View file

@ -204,7 +204,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
return result;
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.isGitSha = exports.getShortName = exports.getCurrentRef = exports.listAllFilesAsAdded = exports.parseGitDiffOutput = exports.getChangesSinceMergeBase = exports.getChangesOnHead = exports.getChanges = exports.getChangesInLastCommit = exports.HEAD = exports.NULL_SHA = void 0;
exports.resolveRefToSha = exports.isGitSha = exports.getShortName = exports.getCurrentRef = exports.listAllFilesAsAdded = exports.parseGitDiffOutput = exports.getChangesSinceMergeBase = exports.getChangesOnHead = exports.getChanges = exports.getChangesInLastCommit = exports.HEAD = exports.NULL_SHA = void 0;
const exec_1 = __nccwpck_require__(1514);
const core = __importStar(__nccwpck_require__(2186));
const file_1 = __nccwpck_require__(4014);
@ -394,6 +394,11 @@ function isGitSha(ref) {
return /^[a-z0-9]{40}$/.test(ref);
}
exports.isGitSha = isGitSha;
async function resolveRefToSha(ref) {
const output = (await (0, exec_1.getExecOutput)('git', ['rev-parse', ref])).stdout.trim();
return output;
}
exports.resolveRefToSha = resolveRefToSha;
async function hasCommit(ref) {
return (await (0, exec_1.getExecOutput)('git', ['cat-file', '-e', `${ref}^{commit}`], { ignoreReturnCode: true })).exitCode === 0;
}
@ -675,7 +680,9 @@ async function getChangedFilesFromGit(base, head, initialFetchDepth) {
throw new Error("This action requires 'base' input to be configured or 'repository.default_branch' to be set in the event payload");
}
const isBaseSha = git.isGitSha(base);
const isBaseSameAsHead = base === head;
const baseSha = await git.resolveRefToSha(base);
const headSha = await git.resolveRefToSha(head);
const isBaseSameAsHead = base === head || baseSha === headSha;
// If base is commit SHA we will do comparison against the referenced commit
// Or if base references same branch it was pushed to, we will do comparison against the previously pushed commit
if (isBaseSha || isBaseSameAsHead) {

View file

@ -198,6 +198,11 @@ export function isGitSha(ref: string): boolean {
return /^[a-z0-9]{40}$/.test(ref)
}
export async function resolveRefToSha(ref: string): Promise<string> {
const output = (await getExecOutput('git', ['rev-parse', ref])).stdout.trim()
return output
}
async function hasCommit(ref: string): Promise<boolean> {
return (await getExecOutput('git', ['cat-file', '-e', `${ref}^{commit}`], {ignoreReturnCode: true})).exitCode === 0
}

View file

@ -153,7 +153,9 @@ async function getChangedFilesFromGit(base: string, head: string, initialFetchDe
}
const isBaseSha = git.isGitSha(base)
const isBaseSameAsHead = base === head
const baseSha = await git.resolveRefToSha(base)
const headSha = await git.resolveRefToSha(head)
const isBaseSameAsHead = base === head || baseSha === headSha
// If base is commit SHA we will do comparison against the referenced commit
// Or if base references same branch it was pushed to, we will do comparison against the previously pushed commit