Skip to content

fix:(diff): fix get function changes incorrect when the file added or deleted #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/utils/function_change/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)}`;
Expand Down
17 changes: 12 additions & 5 deletions src/utils/function_change/file_change.ts
Original file line number Diff line number Diff line change
@@ -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 => {
Expand All @@ -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;
}
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