Is there a good library for line and word diffs?
I searched for the same thing and found npm module diff but didn't test it. it have line and words diff functions, so you can call line diff and on those lines that differ run word diff.
I researched it a while back and even preemptively added https://npm.im/diff-lines to package.json to potentially use for git diff. It's based on the https://npm.im/diff module.
@wmhilton also example how to read files from commit would be nice (can be put in FAQ). So you can use diff yourself.
Based on @mojavelinux code
async function readBranchFile({dir, fs, filepath, branch}) {
const originBaseRef = 'refs/remotes/origin/';
const sha = await git.resolveRef({ fs, dir, ref: originBaseRef + branch });
const { object: { tree } } = await git.readObject({ fs, dir, oid: sha });
return (async function loop(tree, path) {
if (!path.length) {
throw new Error(`File ${filepath} not found`);
}
var name = path.shift();
const { object: { entries } } = await git.readObject({ fs, dir, oid: tree });
const packageEntry = entries.find((entry) => entry.path === name);
if (!packageEntry) {
throw new Error(`File ${filepath} not found`);
} else {
if (packageEntry.type == "blob") {
const { object: pkg } = await git.readObject({ fs, dir, oid: packageEntry.oid })
return pkg.toString('utf8');
} else if (packageEntry.type == "tree") {
return loop(packageEntry.oid, path);
}
}
})(tree, filepath.split('/'));
}
Here is simplified version of my diff function using jsdiff
function diff({dir, filepath, branch}) {
var fname = dir + '/' + filepath;
return pfs.readFile(fname).then(function(newFile) {
return Promise.all([newFile, readBranchFile({fs, dir, branch, filepath})]);
}).then(([newFile, oldFile]) => {
const diff = JsDiff.structuredPatch(filepath, filepath, oldFile, newFile);
const = header = 'diff --git a/' + filepath + ' b/' + filepath;
return header + '\n' + diff.hunks.map(hunk => hunk.lines.join('\n')).join('\n');
});
}
FYI. I develop diff lib. https://github.com/bokuweb/wu-diff-js
I think you will need diff for repack if you aim to implement it. If so, it would be cool to have the diff implementation you're going to use exposed as a part of the API as well. There's no problem bringing in a third party diff library of course, but somehow it would feel 'better' to have it as a part of Isomorphic Git. :-)
Most helpful comment
I researched it a while back and even preemptively added https://npm.im/diff-lines to package.json to potentially use for
git diff. It's based on the https://npm.im/diff module.