diff --git a/README.md b/README.md index 21b12306..0fccecc4 100644 --- a/README.md +++ b/README.md @@ -93,20 +93,24 @@ If you encounter this issue while having installed the correct version of node o ## How to use it? -* [`sfdx sgd:source:delta -f [-t ] [-r ] [-i ] [-D ] [-s ] [-o ] [-a ] [-d] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`](#sfdx-sgdsourcedelta--f-string--t-string--r-filepath--i-filepath--d-filepath--s-filepath--o-filepath--a-number--d---json---loglevel-tracedebuginfowarnerrorfataltracedebuginfowarnerrorfatal) +* [`sfdx sgd:source:delta -f [-t ] [-r ] [-i ] [-D ] [-s ] [-W] [-o ] [-a ] [-d] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]`](#sfdx-sgdsourcedelta--f-string--t-string--r-filepath--i-filepath--d-filepath--s-filepath--w--o-filepath--a-number--d---json---loglevel-tracedebuginfowarnerrorfataltracedebuginfowarnerrorfatal) -## `sfdx sgd:source:delta -f [-t ] [-r ] [-i ] [-D ] [-s ] [-o ] [-a ] [-d] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]` +## `sfdx sgd:source:delta -f [-t ] [-r ] [-i ] [-D ] [-s ] [-W] [-o ] [-a ] [-d] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL]` Generate the sfdx content in source format and destructive change from two git commits ``` USAGE - $ sfdx sgd:source:delta -f [-t ] [-r ] [-i ] [-D ] [-s ] [-o - ] [-a ] [-d] [--json] [--loglevel + $ sfdx sgd:source:delta -f [-t ] [-r ] [-i ] [-D ] [-s ] [-W] + [-o ] [-a ] [-d] [--json] [--loglevel trace|debug|info|warn|error|fatal|TRACE|DEBUG|INFO|WARN|ERROR|FATAL] OPTIONS -D, --ignore-destructive=ignore-destructive ignore file to use + + -W, --ignore-whitespace ignore git diff whitespace (space, + tab, eol) changes + -a, --api-version=api-version [default: 52] salesforce API version -d, --generate-delta generate delta files in [--output] diff --git a/__tests__/unit/lib/utils/fileGitDiff.test.js b/__tests__/unit/lib/utils/fileGitDiff.test.js index c8c11534..e8fc96bb 100644 --- a/__tests__/unit/lib/utils/fileGitDiff.test.js +++ b/__tests__/unit/lib/utils/fileGitDiff.test.js @@ -37,7 +37,21 @@ describe(`test if fileGitDiff`, () => { expect(work).toStrictEqual(output) }) - test('can parse git diff contexte line', () => { + test('can apply permissive git diff', () => { + const output = 'diff' + + child_process.spawnSync.mockImplementation(() => ({ + stdout: output, + })) + const work = fileGitDiff(TEST_PATH, { + output: '', + repo: '', + ignoreWhitespace: true, + }) + expect(work).toStrictEqual(output) + }) + + test('can parse git diff context line', () => { const output = 'context line' child_process.spawnSync.mockImplementation(() => ({ diff --git a/__tests__/unit/lib/utils/repoGitDiff.test.js b/__tests__/unit/lib/utils/repoGitDiff.test.js index 79478a8a..5a31bd28 100644 --- a/__tests__/unit/lib/utils/repoGitDiff.test.js +++ b/__tests__/unit/lib/utils/repoGitDiff.test.js @@ -21,6 +21,24 @@ describe(`test if repoGitDiff`, () => { expect(work).toStrictEqual(output) }) + test('can parse git permissively', () => { + const output = [] + child_process.spawnSync.mockImplementation(() => ({ + stdout: '', + })) + const work = repoGitDiff( + { + output: '', + repo: '', + ignore: FORCEIGNORE_MOCK_PATH, + ignoreWhitespace: true, + }, + // eslint-disable-next-line no-undef + globalMetadata + ) + expect(work).toStrictEqual(output) + }) + test('can resolve deletion', () => { const output = [ 'D force-app/main/default/objects/Account/fields/awesome.field-meta.xml', diff --git a/bin/cli b/bin/cli index e36fa66c..14112aec 100644 --- a/bin/cli +++ b/bin/cli @@ -22,6 +22,7 @@ const options = program messages.sourceFlag, CliHelper.SOURCE_DEFAULT_VALUE ) + .option('-W, --ignore-whitespace', messages.ignoreWhitespaceFlag) .option('-i, --ignore [file]', messages.ignoreFlag) .option('-D, --ignore-destructive [file]', messages.ignoreDestructiveFlag) .option( diff --git a/messages/delta.js b/messages/delta.js index 6bb3a8f7..31b2402c 100644 --- a/messages/delta.js +++ b/messages/delta.js @@ -11,4 +11,5 @@ module.exports = { ignoreDestructiveFlag: 'ignore file to use', apiVersionFlag: 'salesforce API version', deltaFlag: 'generate delta files in [--output] folder', + ignoreWhitespaceFlag: 'ignore git diff whitespace (space, tab, eol) changes', } diff --git a/src/commands/sgd/source/delta.ts b/src/commands/sgd/source/delta.ts index f20cd678..bc0c7e4f 100644 --- a/src/commands/sgd/source/delta.ts +++ b/src/commands/sgd/source/delta.ts @@ -45,6 +45,10 @@ export default class SourceDeltaGenerate extends SfdxCommand { description: messages.getMessage('sourceFlag'), default: CliHelper.SOURCE_DEFAULT_VALUE, }), + 'ignore-whitespace': flags.boolean({ + char: 'W', + description: messages.getMessage('ignoreWhitespaceFlag'), + }), output: flags.filepath({ char: 'o', description: messages.getMessage('outputFlag'), @@ -78,6 +82,7 @@ export default class SourceDeltaGenerate extends SfdxCommand { ignoreDestructive: this.flags['ignore-destructive'], apiVersion: this.flags['api-version'], repo: this.flags.repo, + ignoreWhitespace: this.flags['ignore-whitespace'], generateDelta: this.flags['generate-delta'], }) output.warnings = jobResult?.warnings?.map(warning => warning.message) diff --git a/src/utils/fileGitDiff.js b/src/utils/fileGitDiff.js index fd1798e2..2fa6646f 100644 --- a/src/utils/fileGitDiff.js +++ b/src/utils/fileGitDiff.js @@ -6,9 +6,19 @@ const gc = require('./gitConstants') const unitDiffParams = ['--no-pager', 'diff', '--no-prefix', '-U200'] module.exports = (filePath, config) => { + const ignoreWhitespaceParams = config.ignoreWhitespace + ? gc.IGNORE_WHITESPACE_PARAMS + : [] const { stdout: diff } = childProcess.spawnSync( 'git', - [...unitDiffParams, config.from, config.to, '--', filePath], + [ + ...unitDiffParams, + ...ignoreWhitespaceParams, + config.from, + config.to, + '--', + filePath, + ], { cwd: config.repo, encoding: gc.UTF8_ENCODING } ) diff --git a/src/utils/gitConstants.js b/src/utils/gitConstants.js index 3c040a96..ca51870f 100644 --- a/src/utils/gitConstants.js +++ b/src/utils/gitConstants.js @@ -3,5 +3,11 @@ module.exports.ADDITION = 'A' module.exports.DELETION = 'D' module.exports.GIT_DIFF_TYPE_REGEX = /^.\s+/u module.exports.MINUS = '-' +module.exports.IGNORE_WHITESPACE_PARAMS = [ + '--ignore-all-space', + '--ignore-blank-lines', + '--ignore-cr-at-eol', + '--word-diff-regex', +] module.exports.PLUS = '+' module.exports.UTF8_ENCODING = 'utf8' diff --git a/src/utils/repoGitDiff.js b/src/utils/repoGitDiff.js index 3a62f168..9e39619d 100644 --- a/src/utils/repoGitDiff.js +++ b/src/utils/repoGitDiff.js @@ -13,9 +13,18 @@ const lcSensitivity = { } module.exports = (config, metadata) => { + const ignoreWhitespaceParams = config.ignoreWhitespace + ? gc.IGNORE_WHITESPACE_PARAMS + : [] const { stdout: diff } = childProcess.spawnSync( 'git', - [...fullDiffParams, config.from, config.to, config.source], + [ + ...fullDiffParams, + ...ignoreWhitespaceParams, + config.from, + config.to, + config.source, + ], { cwd: config.repo, encoding: gc.UTF8_ENCODING } )