-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSvnProvider.ts
140 lines (124 loc) · 3.85 KB
/
SvnProvider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import * as vscode from "vscode";
import { ISCMProvider } from "./SCMProvider";
import { promisify } from "util";
import * as childProcess from "child_process";
import { DiffSimplifier } from "../utils/DiffSimplifier";
import { LocalizationManager } from "../utils/LocalizationManager";
const exec = promisify(childProcess.exec);
export class SvnProvider implements ISCMProvider {
type = "svn" as const;
private api: any;
private workspaceRoot: string;
private repositories: any;
constructor(private readonly svnExtension: any) {
this.api = svnExtension;
const workspaceRoot = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath;
if (!workspaceRoot) {
throw new Error(
LocalizationManager.getInstance().getMessage("workspace.not.found")
);
}
this.workspaceRoot = workspaceRoot;
}
async isAvailable(): Promise<boolean> {
try {
console.log("typeof", this.svnExtension, this.svnExtension.name);
if (
!this.svnExtension ||
typeof this.svnExtension.getAPI !== "function"
) {
return false;
}
const api = this.svnExtension.getAPI();
const repositories = api.repositories;
if (repositories.length > 0) {
this.api = api;
this.repositories = repositories;
return true;
}
return false;
} catch (error) {
console.error("Failed to check SVN availability:", error);
return false;
}
}
async getDiff(files?: string[]): Promise<string | undefined> {
try {
let command: string;
if (files && files.length > 0) {
// 对特定文件执行 diff
const filesPaths = files.map((file) => `"${file}"`).join(" ");
if (filesPaths.length === 0) {
command = "svn diff";
} else {
command = `svn diff ${filesPaths}`;
}
} else {
// 对所有暂存文件执行 diff
command = "svn diff";
}
const { stdout, stderr } = await exec(command, {
cwd: this.workspaceRoot,
});
if (stderr) {
throw new Error(stderr);
}
if (!stdout.trim()) {
throw new Error(
LocalizationManager.getInstance().getMessage("diff.noChanges")
);
}
// 获取配置
const config = vscode.workspace.getConfiguration("dish-ai-commit");
const enableSimplification = config.get<boolean>(
"enableDiffSimplification"
);
// 根据配置决定是否显示警告和简化diff
if (enableSimplification) {
vscode.window.showWarningMessage(
LocalizationManager.getInstance().getMessage(
"diff.simplification.warning"
)
);
return DiffSimplifier.simplify(stdout);
}
// 如果未启用简化,直接返回原始diff
return stdout;
} catch (error) {
if (error instanceof Error) {
vscode.window.showErrorMessage(
LocalizationManager.getInstance().format(
"git.diff.failed",
error.message
)
);
}
throw error;
}
}
async commit(message: string, files?: string[]): Promise<void> {
const repository = this.api?.repositories?.[0];
if (!repository) {
throw new Error(
LocalizationManager.getInstance().getMessage("git.repository.not.found")
);
}
try {
await repository.commitFiles(files || [], message);
} catch (error) {
console.error("Failed to commit to SVN:", error);
throw new Error(
LocalizationManager.getInstance().format("svn.commit.failed", error)
);
}
}
async setCommitInput(message: string): Promise<void> {
const repository = this.api?.repositories?.[0];
if (!repository) {
throw new Error(
LocalizationManager.getInstance().getMessage("git.repository.not.found")
);
}
repository.inputBox.value = message;
}
}