|
1 | 1 | import fse from 'fs-extra';
|
2 | 2 | import path from 'path';
|
3 | 3 | import { Error } from '../constants';
|
| 4 | +import { spawnShell, winSpawn } from '../utils/spawnHelper'; |
4 | 5 | import exec from '../utils/execHelper';
|
5 | 6 |
|
6 |
| -const clean = (to, from, source) => { |
7 |
| - const ticks = process.platform === 'win32' ? '"' : '\''; |
8 |
| - const workdir = source.repo().workdir(); |
9 |
| - const filePath = path.join(workdir, source.path()); |
10 |
| - const command = `git lfs clean ${ticks}${source.path()}${ticks}`; |
11 |
| - |
12 |
| - return fse.readFile(filePath) |
13 |
| - .then(buf => exec(command, buf, { cwd: workdir })) |
14 |
| - .then(({ stdout }) => { |
15 |
| - const sha = new Buffer(stdout); |
16 |
| - return to.set(sha, sha.length).then(() => Error.CODE.OK); |
17 |
| - }); |
| 7 | +const IS_WINDOWS = process.platform === 'win32'; |
| 8 | +const ticks = IS_WINDOWS ? '"' : '\''; |
| 9 | + |
| 10 | +const parseSize = (ptr) => { |
| 11 | + const idx = ptr.indexOf('size ') + 5; |
| 12 | + return Number(ptr.substring(idx).trim()); |
18 | 13 | };
|
19 | 14 |
|
20 |
| -const smudge = (to, from, source) => { |
21 |
| - const workdir = source.repo().workdir(); |
| 15 | +export default (crednetialsCallback) => { |
| 16 | + const clean = (to, from, source) => { |
| 17 | + const workdir = source.repo().workdir(); |
| 18 | + const filePath = path.join(workdir, source.path()); |
| 19 | + const command = `git lfs clean ${ticks}${source.path()}${ticks}`; |
22 | 20 |
|
23 |
| - return exec('git lfs smudge', from.ptr(), { cwd: workdir }) |
24 |
| - .then(({ stdout }) => { |
25 |
| - const sha = new Buffer(stdout); |
26 |
| - return to.set(sha, sha.length).then(() => Error.CODE.OK); |
27 |
| - }); |
28 |
| -}; |
| 21 | + return fse.readFile(filePath) |
| 22 | + .then(buf => exec(command, buf, { cwd: workdir })) |
| 23 | + .then(({ stdout }) => { |
| 24 | + const sha = new Buffer(stdout); |
| 25 | + return to.set(sha, sha.length).then(() => Error.CODE.OK); |
| 26 | + }); |
| 27 | + }; |
| 28 | + |
| 29 | + const smudge = (to, from, source) => { |
| 30 | + const workdir = source.repo().workdir(); |
| 31 | + const parts = source.path().split('/'); |
| 32 | + const filepath = parts[parts.length - 1]; |
| 33 | + const ptr = from.ptr(); |
| 34 | + const size = parseSize(ptr); |
| 35 | + const echo = IS_WINDOWS ? `echo|set /p="${ptr}"` : `echo -ne "${ptr}"`; |
| 36 | + |
| 37 | + const promise = IS_WINDOWS |
| 38 | + ? winSpawn(`git lfs smudge ${ticks}${filepath}${ticks}`, ptr, { cwd: workdir }) |
| 39 | + : spawnShell( |
| 40 | + `${echo} | git lfs smudge ${ticks}${filepath}${ticks}`, |
| 41 | + { cwd: workdir }, |
| 42 | + size, |
| 43 | + crednetialsCallback |
| 44 | + ); |
| 45 | + |
| 46 | + return promise |
| 47 | + .then(({ stdout }) => to.set(stdout, stdout.length) |
| 48 | + .then(() => Error.CODE.OK)); |
| 49 | + }; |
29 | 50 |
|
30 |
| -let previousFilterPromise = Promise.resolve(); |
| 51 | + let previousFilterPromise = Promise.resolve(); |
31 | 52 |
|
32 |
| -export const apply = (to, from, source) => { |
33 |
| - const mode = source.mode(); |
| 53 | + return (to, from, source) => { |
| 54 | + const mode = source.mode(); |
34 | 55 |
|
35 |
| - const runNextFilter = () => Promise.resolve() |
36 |
| - .then(() => { |
37 |
| - if (mode === 1) { |
38 |
| - return clean(to, from, source); |
39 |
| - } |
40 |
| - return smudge(to, from, source); |
41 |
| - }) |
42 |
| - .then( |
43 |
| - () => Error.CODE.OK, |
44 |
| - () => Error.CODE.PASSTHROUGH |
45 |
| - ); |
| 56 | + const runNextFilter = () => Promise.resolve() |
| 57 | + .then(() => { |
| 58 | + if (mode === 1) { |
| 59 | + return clean(to, from, source); |
| 60 | + } |
| 61 | + return smudge(to, from, source); |
| 62 | + }) |
| 63 | + .then( |
| 64 | + () => Error.CODE.OK, |
| 65 | + () => Error.CODE.PASSTHROUGH |
| 66 | + ); |
46 | 67 |
|
47 |
| - previousFilterPromise = previousFilterPromise |
48 |
| - .then(runNextFilter, runNextFilter); |
| 68 | + previousFilterPromise = previousFilterPromise |
| 69 | + .then(runNextFilter, runNextFilter); |
49 | 70 |
|
50 |
| - return previousFilterPromise; |
| 71 | + return previousFilterPromise; |
| 72 | + }; |
51 | 73 | };
|
0 commit comments