Skip to content

Commit 1229abc

Browse files
author
Irene Alvarado
committed
Add diffstat case for deleted file
Fixes bug when using removeFile in postprocessing script
1 parent 6473bc1 commit 1229abc

3 files changed

Lines changed: 43 additions & 17 deletions

File tree

dist/index.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -357,20 +357,31 @@ async function getHeadSize(path) {
357357
}
358358
}
359359
async function diffSize(file) {
360-
const stat = fs_1.statSync(file.path);
361-
core.debug(`Calculating diff for ${JSON.stringify(file)}, with size ${stat.size}b`);
362360
switch (file.flag) {
363-
case 'M':
361+
case 'M': {
362+
const stat = fs_1.statSync(file.path);
363+
core.debug(`Calculating diff for ${JSON.stringify(file)}, with size ${stat.size}b`);
364364
// get old size and compare
365365
const oldSize = await getHeadSize(file.path);
366366
const delta = oldSize === undefined ? stat.size : stat.size - oldSize;
367367
core.debug(` ==> ${file.path} modified: old ${oldSize}, new ${stat.size}, delta ${delta}b `);
368368
return delta;
369-
case 'A':
369+
}
370+
case 'A': {
371+
const stat = fs_1.statSync(file.path);
372+
core.debug(`Calculating diff for ${JSON.stringify(file)}, with size ${stat.size}b`);
370373
core.debug(` ==> ${file.path} added: delta ${stat.size}b`);
371374
return stat.size;
372-
default:
375+
}
376+
case 'D': {
377+
const oldSize = await getHeadSize(file.path);
378+
const delta = oldSize === undefined ? 0 : oldSize;
379+
core.debug(` ==> ${file.path} deleted: delta ${delta}b`);
380+
return delta;
381+
}
382+
default: {
373383
throw new Error(`Encountered an unexpected file status in git: ${file.flag} ${file.path}`);
384+
}
374385
}
375386
}
376387
async function diff(filename) {

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/git.ts

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { exec, ExecOptions } from '@actions/exec'
2-
import { statSync } from 'fs'
2+
import { statSync, existsSync } from 'fs'
33
import path from 'path'
44
import * as core from '@actions/core'
5+
import { CONNREFUSED } from 'node:dns'
56

67
export type GitStatus = {
78
flag: string
@@ -46,29 +47,42 @@ async function getHeadSize(path: string): Promise<number | undefined> {
4647
}
4748
}
4849

49-
async function diffSize(file: GitStatus): Promise<number> {
50-
const stat = statSync(file.path)
51-
core.debug(
52-
`Calculating diff for ${JSON.stringify(file)}, with size ${stat.size}b`
53-
)
54-
switch (file.flag) {
55-
case 'M':
50+
async function diffSize(file: GitStatus): Promise<number> {
51+
switch (file.flag) {
52+
case 'M': {
53+
const stat = statSync(file.path)
54+
core.debug(
55+
`Calculating diff for ${JSON.stringify(file)}, with size ${stat.size}b`
56+
)
57+
5658
// get old size and compare
5759
const oldSize = await getHeadSize(file.path)
5860
const delta = oldSize === undefined ? stat.size : stat.size - oldSize
5961
core.debug(
6062
` ==> ${file.path} modified: old ${oldSize}, new ${stat.size}, delta ${delta}b `
6163
)
6264
return delta
65+
}
66+
case 'A': {
67+
const stat = statSync(file.path)
68+
core.debug(
69+
`Calculating diff for ${JSON.stringify(file)}, with size ${stat.size}b`
70+
)
6371

64-
case 'A':
6572
core.debug(` ==> ${file.path} added: delta ${stat.size}b`)
6673
return stat.size
67-
68-
default:
74+
}
75+
case 'D': {
76+
const oldSize = await getHeadSize(file.path)
77+
const delta = oldSize === undefined ? 0 : oldSize
78+
core.debug(` ==> ${file.path} deleted: delta ${delta}b`)
79+
return delta
80+
}
81+
default: {
6982
throw new Error(
7083
`Encountered an unexpected file status in git: ${file.flag} ${file.path}`
7184
)
85+
}
7286
}
7387
}
7488

@@ -84,3 +98,4 @@ export async function diff(filename: string): Promise<number> {
8498
}
8599
return await diffSize(status)
86100
}
101+

0 commit comments

Comments
 (0)