Skip to content
This repository was archived by the owner on Nov 24, 2023. It is now read-only.

Commit 06243ad

Browse files
Merge pull request #159 from technote-space/feature/#157
feat: add relative option (#157)
2 parents 28dcf08 + 4572b36 commit 06243ad

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

__tests__/utils/command.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,31 @@ describe('getGitDiff', () => {
564564
'git diff \'get-diff-action/master..get-diff-action/pull/55/merge\' --shortstat -w -- \'__tests__/main.test.ts\'',
565565
]);
566566
});
567+
568+
it('should get git diff (relative)', async() => {
569+
process.env.GITHUB_WORKSPACE = '/home/runner/work/my-repo-name/my-repo-name';
570+
process.env.INPUT_GITHUB_TOKEN = 'test token';
571+
process.env.INPUT_RELATIVE = 'src/test';
572+
const mockExec = spyOnSpawn();
573+
setChildProcessParams({
574+
stdout: (command: string): string => {
575+
if (command.startsWith('git diff')) {
576+
return 'test.txt';
577+
}
578+
return '';
579+
},
580+
});
581+
582+
expect(await getGitDiff(logger, prContext)).toEqual([
583+
{file: 'test.txt', ...emptyDiff},
584+
]);
585+
execCalledWith(mockExec, [
586+
'git remote add get-diff-action \'https://octocat:test token@github.com/hello/world.git\' || :',
587+
'git fetch --no-tags --no-recurse-submodules \'--depth=10000\' get-diff-action \'refs/pull/55/merge:refs/remotes/get-diff-action/pull/55/merge\' \'refs/heads/master:refs/remotes/get-diff-action/master\' || :',
588+
'git diff \'get-diff-action/master...get-diff-action/pull/55/merge\' \'--diff-filter=AMRC\' --name-only \'--relative=src/test\' || :',
589+
'git diff \'get-diff-action/master...get-diff-action/pull/55/merge\' --shortstat -w -- \'test.txt\'',
590+
]);
591+
});
567592
});
568593

569594
describe('getFileDiff', () => {

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ inputs:
1717
BASE:
1818
description: base
1919
required: false
20+
RELATIVE:
21+
description: relative filter
22+
required: false
2023
DIFF_FILTER:
2124
description: Diff filter.
2225
default: 'AMRC'

src/utils/command.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const command = new Command(new Logger());
1212
const getRawInput = (name: string): string => process.env[`INPUT_${name.replace(/ /g, '_').toUpperCase()}`] || '';
1313
const getDot = (): string => getInput('DOT', {required: true});
1414
const getFilter = (): string => getInput('DIFF_FILTER', {required: true});
15+
const getRelativePath = (): string => getInput('RELATIVE');
1516
const getOutputFormatType = (): string => getRawInput('FORMAT');
1617
const escapeWhenJsonFormat = (): boolean => Utils.getBoolValue(getRawInput('ESCAPE_JSON'));
1718
const getSeparator = (): string => getRawInput('SEPARATOR');
@@ -89,13 +90,17 @@ export const getGitDiff = async(logger: Logger, context: Context): Promise<Array
8990
const patterns = getPatterns();
9091
const options = getMatchOptions();
9192
const filter = getFilter();
93+
const relative = getRelativePath();
9294

9395
return (await Utils.split((await command.execAsync({
9496
command: 'git diff',
9597
args: [
9698
`${getCompareRef(diffInfo.base)}${dot}${getCompareRef(diffInfo.head)}`,
9799
`--diff-filter=${filter}`,
98100
'--name-only',
101+
...(relative ? [
102+
`--relative=${relative}`,
103+
] : []),
99104
],
100105
cwd: Utils.getWorkspace(),
101106
suppressError: true,

0 commit comments

Comments
 (0)