Skip to content

Commit

Permalink
use --name-status
Browse files Browse the repository at this point in the history
  • Loading branch information
saschanaz committed Nov 25, 2020
1 parent 693bf7d commit 47d084d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 27 deletions.
45 changes: 23 additions & 22 deletions scripts/diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,20 @@ const chalk = require('chalk');
const deepDiff = require('deep-diff');
const {
getMergeBase,
getGitDiffPathList,
getFileContent,
getGitDiffStatuses,
} = require('./lib/git.js');

// Note: This does not detect renamed files
/**
* @param {string} base
* @param {string} head
* @param {string} path
* @param {string} baseCommit
* @param {string} headCommit
* @param {string} basePath
*/
function getBaseAndHeadContents(base, head, path) {
let baseContent;
let headContent;
try {
baseContent = JSON.parse(getFileContent(base, path));
} catch {}
try {
headContent = JSON.parse(getFileContent(head, path));
} catch {}
return { base: baseContent, head: headContent };
function getBaseAndHeadContents(baseCommit, basePath, headCommit, headPath) {
const base = JSON.parse(getFileContent(baseCommit, basePath));
const head = JSON.parse(getFileContent(headCommit, headPath));
return { base, head };
}

/**
Expand Down Expand Up @@ -87,19 +81,26 @@ function mergeAsMap(items) {
* @param {string} base
* @param {string} head
*/
function getDiffs(base, head) {
function getDiffs(base, head = '') {
const namedDescriptions = [];
for (const path of getGitDiffPathList(base, head)) {
if (!path.endsWith('.json') || !path.includes('/')) {
for (const status of getGitDiffStatuses(base, head)) {
if (!status.headPath.endsWith('.json') || !status.headPath.includes('/')) {
continue;
}
const contents = getBaseAndHeadContents(base, head, path);
if (!contents.base || !contents.head) {

// Note that A means Added for git while it means Array for deep-diff
if (status.value === 'A' || status.value === 'D') {
namedDescriptions.push({
name: path.replace(/\//g, '.').slice(0, -5), // trim file extension
description: contents.base ? 'Entirely removed' : 'Newly added',
name: status.basePath.replace(/\//g, '.').slice(0, -5), // trim file extension
description: status.value === 'A' ? 'Newly added' : 'Entirely removed',
});
} else {
const contents = getBaseAndHeadContents(
base,
status.basePath,
head,
status.headPath,
);
namedDescriptions.push(
...deepDiff.diff(contents.base, contents.head).map(describeDiffItem),
);
Expand All @@ -109,7 +110,7 @@ function getDiffs(base, head) {
}

if (require.main === module) {
let [base = 'origin/HEAD', head = '*'] = process.argv.slice(2);
let [base = 'origin/HEAD', head] = process.argv.slice(2);
for (const [key, values] of getDiffs(getMergeBase(base, head), head)) {
console.log(chalk`{bold ${key}}:`);
for (const value of values) {
Expand Down
19 changes: 14 additions & 5 deletions scripts/lib/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const child_process = require('child_process');
* @param {string} x
* @param {string} y
*/
function getMergeBase(x, y) {
function getMergeBase(x, y = "HEAD") {
return child_process
.execSync(`git merge-base ${x} ${y}`, { encoding: 'utf-8' })
.trim();
Expand All @@ -14,12 +14,21 @@ function getMergeBase(x, y) {
* @param {string} base
* @param {string} head
*/
function getGitDiffPathList(base, head) {
function getGitDiffStatuses(base, head) {
function parseFields(fields) {
return {
value: fields[0],
headPath: fields[2] || fields[1],
basePath: fields[1]
};
}

return child_process
.execSync(`git diff --name-only ${base} ${head}`, { encoding: 'utf-8' })
.execSync(`git diff --name-status ${base} ${head}`, { encoding: 'utf-8' })
.trim()
.split('\n')
.map(path => path.trim()); // because the line ending could be \r\n
.map(line => line.split("\t"))
.map(parseFields);
}

/**
Expand All @@ -37,6 +46,6 @@ function getFileContent(commit, path) {

module.exports = {
getMergeBase,
getGitDiffPathList,
getGitDiffStatuses,
getFileContent,
};

0 comments on commit 47d084d

Please sign in to comment.