Skip to content

Commit

Permalink
refactor: generate gitDir only once, using git rev-parse
Browse files Browse the repository at this point in the history
  • Loading branch information
Iiro Jäppinen authored and okonet committed Jun 6, 2019
1 parent 738af13 commit 8921989
Show file tree
Hide file tree
Showing 13 changed files with 141 additions and 141 deletions.
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -33,7 +33,6 @@
"dedent": "^0.7.0",
"del": "^3.0.0",
"execa": "^1.0.0",
"find-parent-dir": "^0.3.0",
"g-status": "^2.0.2",
"is-glob": "^4.0.0",
"is-windows": "^1.0.2",
Expand Down
4 changes: 2 additions & 2 deletions src/generateTasks.js
Expand Up @@ -8,13 +8,13 @@ const resolveGitDir = require('./resolveGitDir')

const debug = require('debug')('lint-staged:gen-tasks')

module.exports = function generateTasks(config, stagedRelFiles) {
module.exports = async function generateTasks(config, stagedRelFiles) {
debug('Generating linter tasks')

const normalizedConfig = getConfig(config) // Ensure we have a normalized config
const { linters, globOptions, ignore } = normalizedConfig

const gitDir = resolveGitDir()
const gitDir = await resolveGitDir()
const cwd = process.cwd()
const stagedFiles = stagedRelFiles.map(file => path.resolve(gitDir, file))

Expand Down
5 changes: 2 additions & 3 deletions src/gitWorkflow.js
Expand Up @@ -5,7 +5,6 @@ const execa = require('execa')
const gStatus = require('g-status')
const del = require('del')
const debug = require('debug')('lint-staged:git')
const resolveGitDir = require('./resolveGitDir')

let workingCopyTree = null
let indexTree = null
Expand All @@ -16,7 +15,7 @@ function getAbsolutePath(dir) {
}

async function execGit(cmd, options) {
const cwd = options && options.cwd ? options.cwd : resolveGitDir()
const { cwd } = options
debug('Running git command', cmd)
try {
const { stdout } = await execa('git', [].concat(cmd), {
Expand Down Expand Up @@ -51,7 +50,7 @@ async function getDiffForTrees(tree1, tree2, options) {
}

async function hasPartiallyStagedFiles(options) {
const cwd = options && options.cwd ? options.cwd : resolveGitDir()
const { cwd } = options
const files = await gStatus({ cwd })
const partiallyStaged = files.filter(
file =>
Expand Down
4 changes: 2 additions & 2 deletions src/makeCmdTasks.js
Expand Up @@ -14,14 +14,14 @@ const debug = require('debug')('lint-staged:make-cmd-tasks')
* @param {number} options.chunkSize
* @param {number} options.subTaskConcurrency
*/
module.exports = function makeCmdTasks(
module.exports = async function makeCmdTasks(
commands,
pathsToLint,
{ chunkSize = Number.MAX_SAFE_INTEGER, subTaskConcurrency = 1 } = {}
) {
debug('Creating listr tasks for commands %o', commands)

const gitDir = resolveGitDir()
const gitDir = await resolveGitDir()
const lintersArray = Array.isArray(commands) ? commands : [commands]

return lintersArray.map(linter => ({
Expand Down
6 changes: 3 additions & 3 deletions src/resolveGitDir.js
@@ -1,7 +1,7 @@
'use strict'

const findParentDir = require('find-parent-dir')
const execa = require('execa')

module.exports = function resolveGitDir() {
return findParentDir.sync(process.cwd(), '.git')
module.exports = async function resolveGitDir() {
return (await execa('git', ['rev-parse', '--show-toplevel'])).stdout
}
152 changes: 76 additions & 76 deletions src/runAll.js
Expand Up @@ -16,100 +16,100 @@ const debug = require('debug')('lint-staged:run')
* @param config {Object}
* @returns {Promise}
*/
module.exports = function runAll(config) {
module.exports = async function runAll(config) {
debug('Running all linter scripts')
// Config validation
if (!config || !has(config, 'concurrent') || !has(config, 'renderer')) {
throw new Error('Invalid config provided to runAll! Use getConfig instead.')
}

const { concurrent, renderer, chunkSize, subTaskConcurrency } = config
const gitDir = resolveGitDir()
const gitDir = await resolveGitDir()
debug('Resolved git directory to be `%s`', gitDir)

sgf.cwd = gitDir
return pify(sgf)('ACM').then(files => {
/* files is an Object{ filename: String, status: String } */
const filenames = files.map(file => file.filename)
debug('Loaded list of staged files in git:\n%O', filenames)

const tasks = generateTasks(config, filenames).map(task => ({
title: `Running tasks for ${task.pattern}`,
task: () =>
new Listr(
makeCmdTasks(task.commands, task.fileList, {
chunkSize,
subTaskConcurrency
}),
{
// In sub-tasks we don't want to run concurrently
// and we want to abort on errors
dateFormat: false,
concurrent: false,
exitOnError: true
}
),
skip: () => {
if (task.fileList.length === 0) {
return `No staged files match ${task.pattern}`
/* files is an Object{ filename: String, status: String } */
const files = await pify(sgf)('ACM')
const filenames = files.map(file => file.filename)
debug('Loaded list of staged files in git:\n%O', filenames)

const tasks = (await generateTasks(config, filenames)).map(task => ({
title: `Running tasks for ${task.pattern}`,
task: async () =>
new Listr(
await makeCmdTasks(task.commands, task.fileList, {
chunkSize,
subTaskConcurrency
}),
{
// In sub-tasks we don't want to run concurrently
// and we want to abort on errors
dateFormat: false,
concurrent: false,
exitOnError: true
}
return false
),
skip: () => {
if (task.fileList.length === 0) {
return `No staged files match ${task.pattern}`
}
}))

const listrBaseOptions = {
dateFormat: false,
renderer
return false
}
}))

// If all of the configured "linters" should be skipped
// avoid executing any lint-staged logic
if (tasks.every(task => task.skip())) {
console.log('No staged files match any of provided globs.')
return 'No tasks to run.'
}
const listrBaseOptions = {
dateFormat: false,
renderer
}

// Do not terminate main Listr process on SIGINT
process.on('SIGINT', () => {})
// If all of the configured "linters" should be skipped
// avoid executing any lint-staged logic
if (tasks.every(task => task.skip())) {
console.log('No staged files match any of provided globs.')
return 'No tasks to run.'
}

return new Listr(
[
{
title: 'Stashing changes...',
skip: async () => {
const hasPSF = await git.hasPartiallyStagedFiles()
if (!hasPSF) {
return 'No partially staged files found...'
}
return false
},
task: ctx => {
ctx.hasStash = true
return git.gitStashSave()
// Do not terminate main Listr process on SIGINT
process.on('SIGINT', () => {})

return new Listr(
[
{
title: 'Stashing changes...',
skip: async () => {
const hasPSF = await git.hasPartiallyStagedFiles({ cwd: gitDir })
if (!hasPSF) {
return 'No partially staged files found...'
}
return false
},
{
title: 'Running linters...',
task: () =>
new Listr(tasks, {
...listrBaseOptions,
concurrent,
exitOnError: !concurrent // Wait for all errors when running concurrently
})
},
{
title: 'Updating stash...',
enabled: ctx => ctx.hasStash,
skip: ctx => ctx.hasErrors && 'Skipping stash update since some tasks exited with errors',
task: () => git.updateStash()
},
{
title: 'Restoring local changes...',
enabled: ctx => ctx.hasStash,
task: () => git.gitStashPop()
task: ctx => {
ctx.hasStash = true
return git.gitStashSave({ cwd: gitDir })
}
],
listrBaseOptions
).run()
})
},
{
title: 'Running linters...',
task: () =>
new Listr(tasks, {
...listrBaseOptions,
concurrent,
exitOnError: !concurrent // Wait for all errors when running concurrently
})
},
{
title: 'Updating stash...',
enabled: ctx => ctx.hasStash,
skip: ctx => ctx.hasErrors && 'Skipping stash update since some tasks exited with errors',
task: () => git.updateStash({ cwd: gitDir })
},
{
title: 'Restoring local changes...',
enabled: ctx => ctx.hasStash,
task: () => git.gitStashPop({ cwd: gitDir })
}
],
listrBaseOptions
).run()
}

0 comments on commit 8921989

Please sign in to comment.