Skip to content
This repository was archived by the owner on Oct 23, 2022. It is now read-only.

Commit f316ca5

Browse files
authored
Merge pull request #265 from froucher/feature/263-git-diff-all-at-once
fix: get git diff all at once
2 parents 90ef3d1 + 0a132d9 commit f316ca5

File tree

2 files changed

+59
-57
lines changed

2 files changed

+59
-57
lines changed

lib/cli/changeset.js

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ var path = require('path');
1212
var fs = require('fs-extra');
1313
var vinylFs = require('vinyl-fs');
1414
var mergeStream = require('merge-stream');
15+
var miss = require('mississippi');
16+
var Readable = require('stream').Readable;
1517

1618
var doc = "Usage:\n" +
1719
" force-dev-tool changeset create <name> [<metadataFileOrComponentNames>...] [options]\n" +
@@ -71,20 +73,29 @@ SubCommand.prototype.process = function(proc, callback) {
7173
}
7274
});
7375

74-
var stdin = proc.stdin
75-
.pipe(Diff.stream({
76-
ignoreWhitespace: !!self.opts['--ignore-whitespace']
77-
}))
78-
.pipe(MetadataContainer.diffStream())
76+
var diff = {}; // first get the stdin stream with all git diff changes
77+
proc.stdin.pipe(miss.concat(function(result) {
78+
diff = Diff.parse(result, { ignoreWhitespace: !!self.opts['--ignore-whitespace'] });
79+
}));
7980

80-
mergeStream(stdin, metadataContainer.getStream())
81-
.pipe(MetadataContainer.completeMetadataStream())
82-
.pipe(MetadataContainer.outputStream({
83-
apiVersion: apiVersion
84-
}))
85-
.pipe(vinylFs.dest(deploymentPath))
86-
.on('end', function() {
87-
return callback(null, "exported metadata container to " + path.relative(proc.cwd, deploymentPath));
88-
});
81+
miss.finished(proc.stdin, function(err) {
82+
if (err) return console.trace('stream had an error or closed early', err);
83+
84+
var th = new Readable({ objectMode: true }); // new stream with git diff changes
85+
th.push(diff);
86+
th.push(null); // indicates end-of-file basically - the end of the stream
87+
88+
mergeStream(
89+
th.pipe(MetadataContainer.diffStream()),
90+
metadataContainer.getStream())
91+
.pipe(MetadataContainer.completeMetadataStream())
92+
.pipe(MetadataContainer.outputStream({
93+
apiVersion: apiVersion
94+
}))
95+
.pipe(vinylFs.dest(deploymentPath))
96+
.on('end', function() {
97+
return callback(null, "exported metadata container to " + path.relative(proc.cwd, deploymentPath));
98+
});
99+
});
89100

90101
};

lib/diff.js

Lines changed: 34 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ var MetadataContainer = require('./metadata-container');
66
var Git = require('./git');
77
var Utils = require('./utils');
88
var path = require('path');
9-
var miss = require('mississippi');
109

1110
var Diff = module.exports = function(data) {
1211
this.data = data;
@@ -56,52 +55,44 @@ Diff.prototype.getMetadataContainers = function(opts) {
5655
};
5756
};
5857

59-
Diff.stream = function(opts) {
58+
Diff.parse = function(diff, opts) {
6059
var git = new Git(process.cwd());
6160
var unpackagedPath = path.join(process.cwd(), 'src');
6261
opts = opts || {};
63-
64-
return miss.through.obj(function(diff, enc, cb) {
65-
var files = parseDiff(diff.toString());
66-
67-
if (files.length <= 0) return cb();
68-
69-
var containerFrom = new MetadataContainer();
70-
var containerTo = new MetadataContainer();
71-
files.forEach(function(file) {
72-
if (Utils.isValidFilename(file.from) || Utils.isValidFilename(file.to) || Utils.isValidMetaFilename(file.from) || Utils.isValidMetaFilename(file.to)) {
73-
var fileFrom = MetadataFileFactory.createInstance({
74-
path: file.from === '/dev/null' ? file.from : path.relative(unpackagedPath, file.from),
75-
contents: Buffer.from(""),
76-
ignoreWhitespace: opts.ignoreWhitespace,
77-
fileFrom: true
78-
});
79-
var fileTo = MetadataFileFactory.createInstance({
80-
path: file.to === '/dev/null' ? file.to : path.relative(unpackagedPath, file.to),
81-
contents: Buffer.from(""),
82-
ignoreWhitespace: opts.ignoreWhitespace,
83-
fileTo: true
84-
});
85-
if (file.index && file.index.length) {
86-
// retrieve file using git
87-
var parts = file.index[0].split('..');
88-
if (!file.new) {
89-
fileFrom.contents = git.show(parts[0]);
90-
}
91-
if (!file.deleted) {
92-
fileTo.contents = git.show(parts[1]);
93-
}
62+
var files = parseDiff(diff.toString());
63+
var containerFrom = new MetadataContainer();
64+
var containerTo = new MetadataContainer();
65+
files.forEach(function(file) {
66+
if (Utils.isValidFilename(file.from) || Utils.isValidFilename(file.to) || Utils.isValidMetaFilename(file.from) || Utils.isValidMetaFilename(file.to)) {
67+
var fileFrom = MetadataFileFactory.createInstance({
68+
path: file.from === '/dev/null' ? file.from : path.relative(unpackagedPath, file.from),
69+
contents: Buffer.from(""),
70+
ignoreWhitespace: opts.ignoreWhitespace,
71+
fileFrom: true
72+
});
73+
var fileTo = MetadataFileFactory.createInstance({
74+
path: file.to === '/dev/null' ? file.to : path.relative(unpackagedPath, file.to),
75+
contents: Buffer.from(""),
76+
ignoreWhitespace: opts.ignoreWhitespace,
77+
fileTo: true
78+
});
79+
if (file.index && file.index.length) {
80+
// retrieve file using git
81+
var parts = file.index[0].split('..');
82+
if (!file.new) {
83+
fileFrom.contents = git.show(parts[0]);
84+
}
85+
if (!file.deleted) {
86+
fileTo.contents = git.show(parts[1]);
9487
}
95-
containerFrom.add(fileFrom, []);
96-
containerTo.add(fileTo, []);
97-
9888
}
99-
});
100-
101-
cb(null, {
102-
source: containerFrom,
103-
target: containerTo
104-
});
105-
89+
containerFrom.add(fileFrom, []);
90+
containerTo.add(fileTo, []);
91+
}
10692
});
93+
94+
return {
95+
source: containerFrom,
96+
target: containerTo
97+
};
10798
}

0 commit comments

Comments
 (0)