Skip to content

Commit

Permalink
Merge pull request #11 from githubocto/diffstat-fix
Browse files Browse the repository at this point in the history
Add diffstat case for deleted file
  • Loading branch information
irealva committed May 21, 2021
2 parents 97f24e2 + f5a825d commit 03db417
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 17 deletions.
21 changes: 16 additions & 5 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,20 +357,31 @@ async function getHeadSize(path) {
}
}
async function diffSize(file) {
const stat = fs_1.statSync(file.path);
core.debug(`Calculating diff for ${JSON.stringify(file)}, with size ${stat.size}b`);
switch (file.flag) {
case 'M':
case 'M': {
const stat = fs_1.statSync(file.path);
core.debug(`Calculating diff for ${JSON.stringify(file)}, with size ${stat.size}b`);
// get old size and compare
const oldSize = await getHeadSize(file.path);
const delta = oldSize === undefined ? stat.size : stat.size - oldSize;
core.debug(` ==> ${file.path} modified: old ${oldSize}, new ${stat.size}, delta ${delta}b `);
return delta;
case 'A':
}
case 'A': {
const stat = fs_1.statSync(file.path);
core.debug(`Calculating diff for ${JSON.stringify(file)}, with size ${stat.size}b`);
core.debug(` ==> ${file.path} added: delta ${stat.size}b`);
return stat.size;
default:
}
case 'D': {
const oldSize = await getHeadSize(file.path);
const delta = oldSize === undefined ? 0 : oldSize;
core.debug(` ==> ${file.path} deleted: delta ${delta}b`);
return delta;
}
default: {
throw new Error(`Encountered an unexpected file status in git: ${file.flag} ${file.path}`);
}
}
}
async function diff(filename) {
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "flat",
"version": "2.0.2",
"version": "2.1.0",
"description": "The GitHub action which powers data fetching for Flat",
"main": "index.js",
"scripts": {
Expand Down
34 changes: 24 additions & 10 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,29 +46,42 @@ async function getHeadSize(path: string): Promise<number | undefined> {
}
}

async function diffSize(file: GitStatus): Promise<number> {
const stat = statSync(file.path)
core.debug(
`Calculating diff for ${JSON.stringify(file)}, with size ${stat.size}b`
)
switch (file.flag) {
case 'M':
async function diffSize(file: GitStatus): Promise<number> {
switch (file.flag) {
case 'M': {
const stat = statSync(file.path)
core.debug(
`Calculating diff for ${JSON.stringify(file)}, with size ${stat.size}b`
)

// get old size and compare
const oldSize = await getHeadSize(file.path)
const delta = oldSize === undefined ? stat.size : stat.size - oldSize
core.debug(
` ==> ${file.path} modified: old ${oldSize}, new ${stat.size}, delta ${delta}b `
)
return delta
}
case 'A': {
const stat = statSync(file.path)
core.debug(
`Calculating diff for ${JSON.stringify(file)}, with size ${stat.size}b`
)

case 'A':
core.debug(` ==> ${file.path} added: delta ${stat.size}b`)
return stat.size

default:
}
case 'D': {
const oldSize = await getHeadSize(file.path)
const delta = oldSize === undefined ? 0 : oldSize
core.debug(` ==> ${file.path} deleted: delta ${delta}b`)
return delta
}
default: {
throw new Error(
`Encountered an unexpected file status in git: ${file.flag} ${file.path}`
)
}
}
}

Expand All @@ -84,3 +97,4 @@ export async function diff(filename: string): Promise<number> {
}
return await diffSize(status)
}

0 comments on commit 03db417

Please sign in to comment.