From 1a5a66a9574e2a8b857bd62545a6f2a6da5811aa Mon Sep 17 00:00:00 2001 From: Andrii Miroshnychenko Date: Thu, 16 Jun 2022 19:38:29 +0200 Subject: [PATCH] fix: use new `--diff` and `--diff-filter` options when checking task modifications --- lib/getDiffCommand.js | 18 +++++++++++ lib/getStagedFiles.js | 19 ++---------- lib/gitWorkflow.js | 8 +++-- lib/runAll.js | 9 +++++- test/unit/getDiffCommand.spec.js | 51 ++++++++++++++++++++++++++++++++ 5 files changed, 84 insertions(+), 21 deletions(-) create mode 100644 lib/getDiffCommand.js create mode 100644 test/unit/getDiffCommand.spec.js diff --git a/lib/getDiffCommand.js b/lib/getDiffCommand.js new file mode 100644 index 000000000..531b6ff3b --- /dev/null +++ b/lib/getDiffCommand.js @@ -0,0 +1,18 @@ +export function getDiffCommand(diff, diffFilter) { + /** + * Docs for --diff-filter option: + * @see https://git-scm.com/docs/git-diff#Documentation/git-diff.txt---diff-filterACDMRTUXB82308203 + */ + const diffFilterArg = diffFilter !== undefined ? diffFilter.trim() : 'ACMR' + + /** Use `--diff branch1...branch2` or `--diff="branch1 branch2", or fall back to default staged files */ + const diffArgs = diff !== undefined ? diff.trim().split(' ') : ['--staged'] + + /** + * Docs for -z option: + * @see https://git-scm.com/docs/git-diff#Documentation/git-diff.txt--z + */ + const diffCommand = ['diff', '--name-only', '-z', `--diff-filter=${diffFilterArg}`, ...diffArgs] + + return diffCommand +} diff --git a/lib/getStagedFiles.js b/lib/getStagedFiles.js index b301d4225..8cbb83bb9 100644 --- a/lib/getStagedFiles.js +++ b/lib/getStagedFiles.js @@ -3,27 +3,12 @@ import path from 'node:path' import normalize from 'normalize-path' import { execGit } from './execGit.js' +import { getDiffCommand } from './getDiffCommand.js' import { parseGitZOutput } from './parseGitZOutput.js' export const getStagedFiles = async ({ cwd = process.cwd(), diff, diffFilter } = {}) => { try { - /** - * Docs for --diff-filter option: - * @see https://git-scm.com/docs/git-diff#Documentation/git-diff.txt---diff-filterACDMRTUXB82308203 - */ - const diffFilterArg = diffFilter !== undefined ? diffFilter.trim() : 'ACMR' - - /** Use `--diff branch1...branch2` or `--diff="branch1 branch2", or fall back to default staged files */ - const diffArgs = diff !== undefined ? diff.trim().split(' ') : ['--staged'] - - /** - * Docs for -z option: - * @see https://git-scm.com/docs/git-diff#Documentation/git-diff.txt--z - */ - const lines = await execGit( - ['diff', '--name-only', '-z', `--diff-filter=${diffFilterArg}`, ...diffArgs], - { cwd } - ) + const lines = await execGit(getDiffCommand(diff, diffFilter), { cwd }) if (!lines) return [] return parseGitZOutput(lines).map((file) => normalize(path.resolve(cwd, file))) diff --git a/lib/gitWorkflow.js b/lib/gitWorkflow.js index 8ae95e7e1..9813aa4dd 100644 --- a/lib/gitWorkflow.js +++ b/lib/gitWorkflow.js @@ -4,6 +4,7 @@ import debug from 'debug' import { execGit } from './execGit.js' import { readFile, unlink, writeFile } from './file.js' +import { getDiffCommand } from './getDiffCommand.js' import { GitError, RestoreOriginalStateError, @@ -65,12 +66,13 @@ const handleError = (error, ctx, symbol) => { } export class GitWorkflow { - constructor({ allowEmpty, gitConfigDir, gitDir, matchedFileChunks }) { + constructor({ allowEmpty, gitConfigDir, gitDir, matchedFileChunks, diff, diffFilter }) { this.execGit = (args, options = {}) => execGit(args, { ...options, cwd: gitDir }) this.deletedFiles = [] this.gitConfigDir = gitConfigDir this.gitDir = gitDir - this.unstagedDiff = null + this.diff = diff + this.diffFilter = diffFilter this.allowEmpty = allowEmpty this.matchedFileChunks = matchedFileChunks @@ -262,7 +264,7 @@ export class GitWorkflow { debugLog('Done adding task modifications to index!') - const stagedFilesAfterAdd = await this.execGit(['diff', '--name-only', '--cached']) + const stagedFilesAfterAdd = await this.execGit(getDiffCommand(this.diff, this.diffFilter)) if (!stagedFilesAfterAdd && !this.allowEmpty) { // Tasks reverted all staged changes and the commit would be empty // Throw error to stop commit unless `--allow-empty` was used diff --git a/lib/runAll.js b/lib/runAll.js index 4edd82909..f0ff20342 100644 --- a/lib/runAll.js +++ b/lib/runAll.js @@ -262,7 +262,14 @@ export const runAll = async ( relative: false, }) - const git = new GitWorkflow({ allowEmpty, gitConfigDir, gitDir, matchedFileChunks }) + const git = new GitWorkflow({ + allowEmpty, + gitConfigDir, + gitDir, + matchedFileChunks, + diff, + diffFilter, + }) const runner = new Listr( [ diff --git a/test/unit/getDiffCommand.spec.js b/test/unit/getDiffCommand.spec.js new file mode 100644 index 000000000..470962398 --- /dev/null +++ b/test/unit/getDiffCommand.spec.js @@ -0,0 +1,51 @@ +import { getDiffCommand } from '../../lib/getDiffCommand.js' + +describe('chunkFiles', () => { + const customDiffString = 'origin/main..custom-branch' + const customDiffSpaceSeparatedString = 'origin/main custom-branch' + const customDiffFilter = 'a' + + it('should default to sane value', () => { + const diff = getDiffCommand() + expect(diff).toEqual(['diff', '--name-only', '-z', `--diff-filter=ACMR`, '--staged']) + }) + + it('should work only with diff set as string', () => { + const diff = getDiffCommand(customDiffString) + expect(diff).toEqual([ + 'diff', + '--name-only', + '-z', + `--diff-filter=ACMR`, + 'origin/main..custom-branch', + ]) + }) + + it('should work only with diff set as space separated string', () => { + const diff = getDiffCommand(customDiffSpaceSeparatedString) + expect(diff).toEqual([ + 'diff', + '--name-only', + '-z', + `--diff-filter=ACMR`, + 'origin/main', + 'custom-branch', + ]) + }) + + it('should work only with diffFilter set', () => { + const diff = getDiffCommand(undefined, customDiffFilter) + expect(diff).toEqual(['diff', '--name-only', '-z', `--diff-filter=a`, '--staged']) + }) + + it('should work with both diff and diffFilter set', () => { + const diff = getDiffCommand(customDiffString, customDiffFilter) + expect(diff).toEqual([ + 'diff', + '--name-only', + '-z', + `--diff-filter=a`, + 'origin/main..custom-branch', + ]) + }) +})