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

feat: add relative option (#157) #159

Merged
merged 1 commit into from
Jul 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions __tests__/utils/command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,31 @@ describe('getGitDiff', () => {
'git diff \'get-diff-action/master..get-diff-action/pull/55/merge\' --shortstat -w -- \'__tests__/main.test.ts\'',
]);
});

it('should get git diff (relative)', async() => {
process.env.GITHUB_WORKSPACE = '/home/runner/work/my-repo-name/my-repo-name';
process.env.INPUT_GITHUB_TOKEN = 'test token';
process.env.INPUT_RELATIVE = 'src/test';
const mockExec = spyOnSpawn();
setChildProcessParams({
stdout: (command: string): string => {
if (command.startsWith('git diff')) {
return 'test.txt';
}
return '';
},
});

expect(await getGitDiff(logger, prContext)).toEqual([
{file: 'test.txt', ...emptyDiff},
]);
execCalledWith(mockExec, [
'git remote add get-diff-action \'https://octocat:test [email protected]/hello/world.git\' || :',
'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\' || :',
'git diff \'get-diff-action/master...get-diff-action/pull/55/merge\' \'--diff-filter=AMRC\' --name-only \'--relative=src/test\' || :',
'git diff \'get-diff-action/master...get-diff-action/pull/55/merge\' --shortstat -w -- \'test.txt\'',
]);
});
});

describe('getFileDiff', () => {
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ inputs:
BASE:
description: base
required: false
RELATIVE:
description: relative filter
required: false
DIFF_FILTER:
description: Diff filter.
default: 'AMRC'
Expand Down
5 changes: 5 additions & 0 deletions src/utils/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const command = new Command(new Logger());
const getRawInput = (name: string): string => process.env[`INPUT_${name.replace(/ /g, '_').toUpperCase()}`] || '';
const getDot = (): string => getInput('DOT', {required: true});
const getFilter = (): string => getInput('DIFF_FILTER', {required: true});
const getRelativePath = (): string => getInput('RELATIVE');
const getOutputFormatType = (): string => getRawInput('FORMAT');
const escapeWhenJsonFormat = (): boolean => Utils.getBoolValue(getRawInput('ESCAPE_JSON'));
const getSeparator = (): string => getRawInput('SEPARATOR');
Expand Down Expand Up @@ -89,13 +90,17 @@ export const getGitDiff = async(logger: Logger, context: Context): Promise<Array
const patterns = getPatterns();
const options = getMatchOptions();
const filter = getFilter();
const relative = getRelativePath();

return (await Utils.split((await command.execAsync({
command: 'git diff',
args: [
`${getCompareRef(diffInfo.base)}${dot}${getCompareRef(diffInfo.head)}`,
`--diff-filter=${filter}`,
'--name-only',
...(relative ? [
`--relative=${relative}`,
] : []),
],
cwd: Utils.getWorkspace(),
suppressError: true,
Expand Down