Skip to content

Commit 11fcf89

Browse files
authored
Merge pull request CocaColf#1 from CocaColf/fix/new_file_diff_result_empty
fix:(diff): fix get function changes incorrect when the file added or deleted
2 parents 90056b3 + 6f6a936 commit 11fcf89

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

src/utils/function_change/diff.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,19 @@ import { DiffFunctionInfo, FunctionInfo } from '../../type';
99

1010
// get the change of function before and after file change
1111
export function getFunctionDiffInfo (filePath: string, commitSha?: string) {
12-
const functionInfoNow = getFunctionBlock(filePath);
13-
1412
let functionInfoBefore;
13+
let functionInfoNow;
14+
15+
if (!fs.existsSync(filePath)) { // file deleted
16+
functionInfoNow = {};
17+
} else {
18+
functionInfoNow = getFunctionBlock(filePath);
19+
}
20+
1521
const blobId = getFileCtxBeforeChange(filePath, commitSha);
1622
if (!blobId) {
17-
functionInfoBefore = functionInfoNow;
23+
// treat it as a new file
24+
functionInfoBefore = {};
1825
} else {
1926
const beforeCtx = execaCommandSync(`git cat-file blob ${blobId}`).stdout;
2027
const tempFile = `./temp_${path.posix.basename(filePath)}`;

src/utils/function_change/file_change.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { execaCommandSync } from 'execa';
22
import { FileChange } from '../../type';
33

4-
const COMMAND = 'git diff --name-status';
4+
const COMMANDS = ['git diff --name-status', 'git diff --name-status --staged'];
55

66
const formatList = (str: string, type: string) => {
77
const arr = str.split('\n').filter(item => {
@@ -22,11 +22,18 @@ export function getFileChange () {
2222

2323
const typeList = ['M', 'D', 'A'];
2424

25-
const changeInfo = execaCommandSync(COMMAND).stdout;
25+
for (const command of COMMANDS) {
26+
const changeInfo = execaCommandSync(command).stdout;
2627

27-
typeList.forEach(type => {
28-
result[type] = formatList(changeInfo, type)
29-
})
28+
typeList.forEach(type => {
29+
const formatResult = formatList(changeInfo, type);
30+
if (result[type]) {
31+
result[type].push(...formatResult);
32+
} else {
33+
result[type] = formatResult;
34+
}
35+
});
36+
}
3037

3138
return result;
3239
}

0 commit comments

Comments
 (0)