Skip to content

Commit 5d7e3de

Browse files
feat(svn): Add cross-platform SVN output encoding conversion function
- Add convertSvnOutput function to handle the encoding problem of SVN command output, and provides GBK/GB2312 support for Windows Chinese environment. - Solve the garbled problem that may occur in Chinese paths or contents under Windows systems.
1 parent 9497c21 commit 5d7e3de

File tree

1 file changed

+41
-3
lines changed

1 file changed

+41
-3
lines changed

src/utils/svn.ts

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,44 @@ import { promises as fs } from "fs"
44
import { exec } from "child_process"
55
import { promisify } from "util"
66
import { truncateOutput } from "../integrations/misc/extract-text"
7+
import * as os from "os"
8+
9+
/**
10+
* Convert SVN command output buffer to string with cross-platform encoding support
11+
* @param output Buffer or string output from SVN command
12+
* @returns Properly decoded string
13+
*/
14+
function convertSvnOutput(output: Buffer | string): string {
15+
if (typeof output === "string") {
16+
return output
17+
}
18+
19+
// Try UTF-8 first (works for Linux/macOS and modern Windows)
20+
try {
21+
const utf8Result = output.toString("utf8")
22+
// Check if the result contains replacement characters (indicates encoding issues)
23+
if (!utf8Result.includes("\uFFFD")) {
24+
return utf8Result
25+
}
26+
} catch (error) {
27+
// UTF-8 decoding failed
28+
}
29+
30+
// On Windows, try common Chinese encodings if UTF-8 failed
31+
if (os.platform() === "win32") {
32+
try {
33+
// Try GBK/GB2312 encoding (common on Chinese Windows systems)
34+
// Note: Node.js doesn't support GBK directly, so we'll use latin1 as fallback
35+
// and let the system handle the encoding
36+
return output.toString("latin1")
37+
} catch (error) {
38+
// Fallback to latin1 if all else fails
39+
}
40+
}
41+
42+
// Final fallback: use UTF-8 even if it has replacement characters
43+
return output.toString("utf8")
44+
}
745

846
const execAsync = promisify(exec)
947
const SVN_OUTPUT_LINE_LIMIT = 500
@@ -827,14 +865,14 @@ export async function getSvnCommitInfoForMentions(revision: string, cwd: string)
827865
}
828866
}
829867

830-
// Get diff information (same as getSvnCommitInfo function)
868+
// Get diff information with cross-platform encoding compatibility
831869
let diff = ""
832870
try {
833871
console.log("[DEBUG] Getting diff for revision:", cleanRevision)
834-
const { stdout: diffOutput } = await execAsync(`svn diff -c ${cleanRevision}`, {
872+
const { stdout: rawDiffOutput } = await execAsync(`svn diff -c ${cleanRevision}`, {
835873
cwd,
836-
encoding: "utf8",
837874
})
875+
const diffOutput = convertSvnOutput(rawDiffOutput)
838876
diff = truncateOutput(diffOutput, SVN_OUTPUT_LINE_LIMIT)
839877
console.log("[DEBUG] Diff length:", diff.length)
840878
} catch (error) {

0 commit comments

Comments
 (0)