From 6f6a9360dff95472e78fbb03a77d7706a67782b1 Mon Sep 17 00:00:00 2001 From: CocaColf Date: Mon, 18 Apr 2022 11:42:04 +0800 Subject: [PATCH] fix:(diff): fix get function changes incorrect when the file added or deleted --- src/utils/function_change/diff.ts | 13 ++++++++++--- src/utils/function_change/file_change.ts | 17 ++++++++++++----- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/utils/function_change/diff.ts b/src/utils/function_change/diff.ts index e4bf92d..1f84f2c 100644 --- a/src/utils/function_change/diff.ts +++ b/src/utils/function_change/diff.ts @@ -9,12 +9,19 @@ import { DiffFunctionInfo, FunctionInfo } from '../../type'; // get the change of function before and after file change export function getFunctionDiffInfo (filePath: string, commitSha?: string) { - const functionInfoNow = getFunctionBlock(filePath); - let functionInfoBefore; + let functionInfoNow; + + if (!fs.existsSync(filePath)) { // file deleted + functionInfoNow = {}; + } else { + functionInfoNow = getFunctionBlock(filePath); + } + const blobId = getFileCtxBeforeChange(filePath, commitSha); if (!blobId) { - functionInfoBefore = functionInfoNow; + // treat it as a new file + functionInfoBefore = {}; } else { const beforeCtx = execaCommandSync(`git cat-file blob ${blobId}`).stdout; const tempFile = `./temp_${path.posix.basename(filePath)}`; diff --git a/src/utils/function_change/file_change.ts b/src/utils/function_change/file_change.ts index c415f97..57f321c 100644 --- a/src/utils/function_change/file_change.ts +++ b/src/utils/function_change/file_change.ts @@ -1,7 +1,7 @@ import { execaCommandSync } from 'execa'; import { FileChange } from '../../type'; -const COMMAND = 'git diff --name-status'; +const COMMANDS = ['git diff --name-status', 'git diff --name-status --staged']; const formatList = (str: string, type: string) => { const arr = str.split('\n').filter(item => { @@ -22,11 +22,18 @@ export function getFileChange () { const typeList = ['M', 'D', 'A']; - const changeInfo = execaCommandSync(COMMAND).stdout; + for (const command of COMMANDS) { + const changeInfo = execaCommandSync(command).stdout; - typeList.forEach(type => { - result[type] = formatList(changeInfo, type) - }) + typeList.forEach(type => { + const formatResult = formatList(changeInfo, type); + if (result[type]) { + result[type].push(...formatResult); + } else { + result[type] = formatResult; + } + }); + } return result; } \ No newline at end of file pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy