diff --git a/lib/utils.js b/lib/utils.js index 722b6fae..df2e395a 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -2,6 +2,7 @@ // SPDX-License-Identifier: MIT const { DateTime } = require('luxon') const { spawn } = require('child_process') +const { intersection } = require('lodash') const dateTimeFormats = [ 'EEE MMM d HH:mm:ss \'GMT\'ZZ yyyy' //in pom properties @@ -31,6 +32,12 @@ function trimAllParents(paths, parents) { return paths.map(path => trimParents(path, parents)) } +function isGitFile(file) { + if (!file) return false + const segments = file.split(/[\\/]/g) + return intersection(segments, ['.git']).length > 0 +} + function extractDate(dateAndTime, formats = dateTimeFormats) { if (!dateAndTime) return dateAndTime let luxonResult = DateTime.fromISO(dateAndTime) @@ -75,4 +82,4 @@ function spawnPromisified(command, args, options) { }) } -module.exports = { normalizePath, normalizePaths, trimParents, trimAllParents, extractDate, spawnPromisified } +module.exports = { normalizePath, normalizePaths, trimParents, trimAllParents, isGitFile, extractDate, spawnPromisified } diff --git a/providers/process/abstractClearlyDefinedProcessor.js b/providers/process/abstractClearlyDefinedProcessor.js index b1c5176e..506d860c 100644 --- a/providers/process/abstractClearlyDefinedProcessor.js +++ b/providers/process/abstractClearlyDefinedProcessor.js @@ -6,7 +6,7 @@ const throat = require('throat') const path = require('path') const { pick, merge } = require('lodash') const du = require('du') -const { trimParents } = require('../../lib/utils') +const { trimParents, isGitFile } = require('../../lib/utils') class AbstractClearlyDefinedProcessor extends AbstractProcessor { get toolVersion() { @@ -75,7 +75,7 @@ class AbstractClearlyDefinedProcessor extends AbstractProcessor { let count = 0 const bytes = await du(location, { filter: file => { - if (path.basename(file) === '.git') return false + if (isGitFile(file)) return false count++ return true } diff --git a/providers/process/abstractProcessor.js b/providers/process/abstractProcessor.js index e371561e..4ccb5d32 100644 --- a/providers/process/abstractProcessor.js +++ b/providers/process/abstractProcessor.js @@ -6,11 +6,11 @@ const EntitySpec = require('../../lib/entitySpec') const fs = require('fs') const path = require('path') const shajs = require('sha.js') -const { clone, flatten, intersection, pick, set } = require('lodash') +const { clone, flatten, pick, set } = require('lodash') const { promisify } = require('util') const readdir = promisify(fs.readdir) const lstat = promisify(fs.lstat) -const { trimAllParents } = require('../../lib/utils') +const { trimAllParents, isGitFile } = require('../../lib/utils') class AbstractProcessor extends BaseHandler { constructor(options) { @@ -150,12 +150,7 @@ class AbstractProcessor extends BaseHandler { */ async filterFiles(location) { const fullList = await this.getFiles(location) - const exclusions = ['.git'] - const filteredList = fullList.filter(file => { - if (!file) return false - const segments = file.split(/[\\/]/g) - return !intersection(segments, exclusions).length - }) + const filteredList = fullList.filter(file => file && !isGitFile(file)) return trimAllParents(filteredList, location).filter(x => x) } diff --git a/test/unit/lib/utilsTests.js b/test/unit/lib/utilsTests.js index 82bee191..b3cf5028 100644 --- a/test/unit/lib/utilsTests.js +++ b/test/unit/lib/utilsTests.js @@ -3,7 +3,7 @@ const chai = require('chai') const chaiAsPromised = require('chai-as-promised') -const { normalizePath, normalizePaths, trimParents, trimAllParents, extractDate, spawnPromisified } = require('../../../lib/utils') +const { normalizePath, normalizePaths, trimParents, trimAllParents, extractDate, spawnPromisified, isGitFile } = require('../../../lib/utils') const { promisify } = require('util') const execFile = promisify(require('child_process').execFile) chai.use(chaiAsPromised) @@ -56,6 +56,22 @@ describe('Utils path functions', () => { }) }) +describe('Util isGitFile', () => { + const entries = new Map([ + [null, false], + ['/', false], + ['/tmp/tempX/package/src', false], + ['.git', true], + ['/tmp/tempX/package/.git', true], + ['/tmp/tempX/package/.git/hooks/pre-merge-commit.sample', true] + ]) + + entries.forEach((expected, file) => { + it(`should return ${expected} for isGitFile given '${file}'`, () => expect(isGitFile(file)).to.eq(expected)) + }) + +}) + describe('Util extractDate', () => { it('handle null', () => { expect(extractDate(null)).to.be.null