Skip to content

Commit

Permalink
feat: filter deletion for case insensitive file renaming (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
scolladon committed Apr 20, 2021
1 parent 3f4f7bd commit d8c8fbf
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
17 changes: 17 additions & 0 deletions __tests__/unit/lib/utils/repoGitDiff.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,23 @@ describe(`test if repoGitDiff`, () => {
expect(work).toStrictEqual(expected)
})

test('can filter case changed files', () => {
const output = [
'D force-app/main/default/objects/Account/fields/TEST__c.field-meta.xml',
'A force-app/main/default/objects/Account/fields/Test__c.field-meta.xml',
]
child_process.spawnSync.mockImplementation(() => ({
stdout: output.join(os.EOL),
}))
const work = repoGitDiff(
{ output: '', repo: '' },
// eslint-disable-next-line no-undef
globalMetadata
)
const expected = [output[1]]
expect(work).toStrictEqual(expected)
})

test('cannot filter renamed files', () => {
const output = [
'D force-app/main/default/classes/Account.cls',
Expand Down
20 changes: 11 additions & 9 deletions src/utils/repoGitDiff.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ const os = require('os')
const path = require('path')

const fullDiffParams = ['--no-pager', 'diff', '--name-status', '--no-renames']
const lcSensitivity = {
sensitivity: 'accent',
}

module.exports = (config, metadata) => {
const { stdout: diff } = childProcess.spawnSync(
Expand All @@ -25,22 +28,21 @@ const treatResult = (repoDiffResult, metadata, config) => {
(acc, line) => (acc[line.charAt(0)]?.push(line), acc),
{ [gc.ADDITION]: [], [gc.DELETION]: [] }
)
const AfileNames = new Set(
linesPerDiffType[gc.ADDITION].map(
line => path.parse(line.replace(gc.GIT_DIFF_TYPE_REGEX, '')).base
)
const AfileNames = linesPerDiffType[gc.ADDITION].map(
line => path.parse(line.replace(gc.GIT_DIFF_TYPE_REGEX, '')).base
)
const deletedRenamed = new Set(
linesPerDiffType[gc.DELETION].filter(line =>
AfileNames.has(path.parse(line.replace(gc.GIT_DIFF_TYPE_REGEX, '')).base)
const deletedRenamed = linesPerDiffType[gc.DELETION].filter(line => {
const dEl = path.parse(line.replace(gc.GIT_DIFF_TYPE_REGEX, '')).base
return AfileNames.some(
aEl => !aEl.localeCompare(dEl, undefined, lcSensitivity)
)
)
})

return lines
.filter(
line =>
!!line &&
!deletedRenamed.has(line) &&
!deletedRenamed.includes(line) &&
line
.split(path.sep)
.some(part => Object.prototype.hasOwnProperty.call(metadata, part))
Expand Down

0 comments on commit d8c8fbf

Please sign in to comment.