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

Commit

Permalink
Merge pull request #265 from froucher/feature/263-git-diff-all-at-once
Browse files Browse the repository at this point in the history
fix: get git diff all at once
  • Loading branch information
amtrack authored Sep 25, 2020
2 parents 90ef3d1 + 0a132d9 commit f316ca5
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 57 deletions.
39 changes: 25 additions & 14 deletions lib/cli/changeset.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ var path = require('path');
var fs = require('fs-extra');
var vinylFs = require('vinyl-fs');
var mergeStream = require('merge-stream');
var miss = require('mississippi');
var Readable = require('stream').Readable;

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

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

mergeStream(stdin, metadataContainer.getStream())
.pipe(MetadataContainer.completeMetadataStream())
.pipe(MetadataContainer.outputStream({
apiVersion: apiVersion
}))
.pipe(vinylFs.dest(deploymentPath))
.on('end', function() {
return callback(null, "exported metadata container to " + path.relative(proc.cwd, deploymentPath));
});
miss.finished(proc.stdin, function(err) {
if (err) return console.trace('stream had an error or closed early', err);

var th = new Readable({ objectMode: true }); // new stream with git diff changes
th.push(diff);
th.push(null); // indicates end-of-file basically - the end of the stream

mergeStream(
th.pipe(MetadataContainer.diffStream()),
metadataContainer.getStream())
.pipe(MetadataContainer.completeMetadataStream())
.pipe(MetadataContainer.outputStream({
apiVersion: apiVersion
}))
.pipe(vinylFs.dest(deploymentPath))
.on('end', function() {
return callback(null, "exported metadata container to " + path.relative(proc.cwd, deploymentPath));
});
});

};
77 changes: 34 additions & 43 deletions lib/diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ var MetadataContainer = require('./metadata-container');
var Git = require('./git');
var Utils = require('./utils');
var path = require('path');
var miss = require('mississippi');

var Diff = module.exports = function(data) {
this.data = data;
Expand Down Expand Up @@ -56,52 +55,44 @@ Diff.prototype.getMetadataContainers = function(opts) {
};
};

Diff.stream = function(opts) {
Diff.parse = function(diff, opts) {
var git = new Git(process.cwd());
var unpackagedPath = path.join(process.cwd(), 'src');
opts = opts || {};

return miss.through.obj(function(diff, enc, cb) {
var files = parseDiff(diff.toString());

if (files.length <= 0) return cb();

var containerFrom = new MetadataContainer();
var containerTo = new MetadataContainer();
files.forEach(function(file) {
if (Utils.isValidFilename(file.from) || Utils.isValidFilename(file.to) || Utils.isValidMetaFilename(file.from) || Utils.isValidMetaFilename(file.to)) {
var fileFrom = MetadataFileFactory.createInstance({
path: file.from === '/dev/null' ? file.from : path.relative(unpackagedPath, file.from),
contents: Buffer.from(""),
ignoreWhitespace: opts.ignoreWhitespace,
fileFrom: true
});
var fileTo = MetadataFileFactory.createInstance({
path: file.to === '/dev/null' ? file.to : path.relative(unpackagedPath, file.to),
contents: Buffer.from(""),
ignoreWhitespace: opts.ignoreWhitespace,
fileTo: true
});
if (file.index && file.index.length) {
// retrieve file using git
var parts = file.index[0].split('..');
if (!file.new) {
fileFrom.contents = git.show(parts[0]);
}
if (!file.deleted) {
fileTo.contents = git.show(parts[1]);
}
var files = parseDiff(diff.toString());
var containerFrom = new MetadataContainer();
var containerTo = new MetadataContainer();
files.forEach(function(file) {
if (Utils.isValidFilename(file.from) || Utils.isValidFilename(file.to) || Utils.isValidMetaFilename(file.from) || Utils.isValidMetaFilename(file.to)) {
var fileFrom = MetadataFileFactory.createInstance({
path: file.from === '/dev/null' ? file.from : path.relative(unpackagedPath, file.from),
contents: Buffer.from(""),
ignoreWhitespace: opts.ignoreWhitespace,
fileFrom: true
});
var fileTo = MetadataFileFactory.createInstance({
path: file.to === '/dev/null' ? file.to : path.relative(unpackagedPath, file.to),
contents: Buffer.from(""),
ignoreWhitespace: opts.ignoreWhitespace,
fileTo: true
});
if (file.index && file.index.length) {
// retrieve file using git
var parts = file.index[0].split('..');
if (!file.new) {
fileFrom.contents = git.show(parts[0]);
}
if (!file.deleted) {
fileTo.contents = git.show(parts[1]);
}
containerFrom.add(fileFrom, []);
containerTo.add(fileTo, []);

}
});

cb(null, {
source: containerFrom,
target: containerTo
});

containerFrom.add(fileFrom, []);
containerTo.add(fileTo, []);
}
});

return {
source: containerFrom,
target: containerTo
};
}

0 comments on commit f316ca5

Please sign in to comment.