Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: split tasks into chunks to support shells with limited max argu…
…ment length
- Loading branch information
Showing
6 changed files
with
134 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
'use strict' | ||
|
||
const normalize = require('normalize-path') | ||
const path = require('path') | ||
|
||
/** | ||
* Chunk array into sub-arrays | ||
* @param {Array} arr | ||
* @param {Number} chunkCount | ||
* @retuns {Array<Array>} | ||
*/ | ||
const chunkArray = (arr, chunkCount) => { | ||
if (chunkCount === 1) return [arr] | ||
const chunked = [] | ||
let position = 0 | ||
for (let i = 0; i < chunkCount; i++) { | ||
const chunkLength = Math.ceil((arr.length - position) / (chunkCount - i)) | ||
chunked.push([]) | ||
chunked[i] = arr.slice(position, chunkLength + position) | ||
position += chunkLength | ||
} | ||
return chunked | ||
} | ||
|
||
/** | ||
* https://serverfault.com/questions/69430/what-is-the-maximum-length-of-a-command-line-in-mac-os-x | ||
* https://support.microsoft.com/en-us/help/830473/command-prompt-cmd-exe-command-line-string-limitation | ||
* https://unix.stackexchange.com/a/120652 | ||
*/ | ||
const MAX_ARG_LENGTH = | ||
(process.platform === 'darwin' && 262144) || (process.platform === 'win32' && 8191) || 131072 | ||
|
||
module.exports = function chunkFiles({ | ||
files, | ||
gitDir, | ||
maxArgLength = MAX_ARG_LENGTH / 2, | ||
relative = false | ||
}) { | ||
const normalizedFiles = files.map(file => normalize(relative ? file : path.resolve(gitDir, file))) | ||
const fileListLength = normalizedFiles.join(' ').length | ||
const chunkCount = Math.min(Math.ceil(fileListLength / maxArgLength), files.length) | ||
return chunkArray(files, chunkCount) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import chunkFiles from '../lib/chunkFiles' | ||
|
||
describe('chunkFiles', () => { | ||
const files = ['example.js', 'foo.js', 'bar.js', 'foo/bar.js'] | ||
const gitDir = '/opt/git/example.git' | ||
|
||
it('should default to sane value', () => { | ||
const chunkedFiles = chunkFiles({ files: ['foo.js'], gitDir, relative: true }) | ||
expect(chunkedFiles).toEqual([['foo.js']]) | ||
}) | ||
|
||
it('should not chunk short argument string', () => { | ||
const chunkedFiles = chunkFiles({ files, gitDir, maxArgLength: 1000 }) | ||
expect(chunkedFiles).toEqual([files]) | ||
}) | ||
|
||
it('should chunk too long argument string', () => { | ||
const chunkedFiles = chunkFiles({ files, gitDir, maxArgLength: 20 }) | ||
expect(chunkedFiles).toEqual(files.map(file => [file])) | ||
}) | ||
|
||
it('should take into account relative setting', () => { | ||
const chunkedFiles = chunkFiles({ files, gitDir, maxArgLength: 20, relative: true }) | ||
expect(chunkedFiles).toEqual([ | ||
[files[0], files[1]], | ||
[files[2], files[3]] | ||
]) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters