Skip to content

Commit

Permalink
🎨 style(git): 优化代码格式和错误处理逻辑
Browse files Browse the repository at this point in the history
- 改进了 GitRepository 接口的方法参数格式
- 优化了 getFileStatus 方法的代码结构和可读性
- 调整了错误信息本地化键名为更明确的 "git.diff.failed"
- 优化了错误处理和条件判断的代码格式
  • Loading branch information
littleCareless committed Dec 11, 2024
1 parent 2bd795a commit b413151
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions src/scm/GitProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ interface GitRepository {
inputBox: {
value: string;
};
commit(message: string, options: { all: boolean; files?: string[] }): Promise<void>;
commit(
message: string,
options: { all: boolean; files?: string[] }
): Promise<void>;
}

export class GitProvider implements ISCMProvider {
Expand Down Expand Up @@ -44,17 +47,29 @@ export class GitProvider implements ISCMProvider {
// 优化 getFileStatus 方法
private async getFileStatus(file: string): Promise<string> {
try {
const { stdout: status } = await exec(`git status --porcelain "${file}"`, {
cwd: this.workspaceRoot,
});

if (!status) return "Unknown";

if (status.startsWith("??")) return "New File";
if (status.startsWith(" D") || status.startsWith("D ")) return "Deleted File";
const { stdout: status } = await exec(
`git status --porcelain "${file}"`,
{
cwd: this.workspaceRoot,
}
);

if (!status) {
return "Unknown";
}

if (status.startsWith("??")) {
return "New File";
}
if (status.startsWith(" D") || status.startsWith("D ")) {
return "Deleted File";
}
return "Modified File";
} catch (error) {
console.error("Failed to get file status:", error instanceof Error ? error.message : error);
console.error(
"Failed to get file status:",
error instanceof Error ? error.message : error
);
return "Unknown";
}
}
Expand Down Expand Up @@ -128,7 +143,7 @@ export class GitProvider implements ISCMProvider {
);
}
throw new Error(
LocalizationManager.getInstance().getMessage("diff.failed")
LocalizationManager.getInstance().getMessage("git.diff.failed")
);
}
}
Expand Down

0 comments on commit b413151

Please sign in to comment.