Commit e7722a69 authored by Auto Backup's avatar Auto Backup

fix: Windows上git命令改用bat直接执行,避免PowerShell%转义问题

- Windows上使用bat执行git命令,而非通过pwsh/powershell
- Jenkins bat中%需要双写%%,但git --pretty=%an在bat中本身就是正确的
- 保持Unix系统使用sh执行
parent 3bf3863c
...@@ -34,35 +34,24 @@ def getGitCommitInfo(commitHash = '') { ...@@ -34,35 +34,24 @@ def getGitCommitInfo(commitHash = '') {
// 确定使用哪个 commit // 确定使用哪个 commit
def commitArg = commitHash ? "${commitHash}" : "-1" def commitArg = commitHash ? "${commitHash}" : "-1"
def gitCmdPrefix = "git log ${commitArg} --pretty="
// Windows PowerShell 中 % 是特殊字符,需要用单引号避免变量展开
// 使用 sh -c 在 Windows 上也能工作
def cmdAuthor = isUnix() ? "${gitCmdPrefix}%an" : "sh -c '${gitCmdPrefix}%an'"
def cmdMessage = isUnix() ? "${gitCmdPrefix}%B" : "sh -c '${gitCmdPrefix}%B'"
def cmdEmail = isUnix() ? "${gitCmdPrefix}%ae" : "sh -c '${gitCmdPrefix}%ae'"
try { try {
// 获取作者 if (isUnix()) {
def authorResult = runCommandWithOutput(cmdAuthor) // Unix 系统直接使用 sh
if (authorResult.success) { result.author = sh(script: "git log ${commitArg} --pretty=%an", returnStdout: true).trim()
result.author = authorResult.stdout result.message = sh(script: "git log ${commitArg} --pretty=%B", returnStdout: true).trim()
} result.email = sh(script: "git log ${commitArg} --pretty=%ae", returnStdout: true).trim()
} else {
// 获取提交信息 // Windows 系统:PowerShell 会把 % 当作变量,必须使用 bat 执行 git 命令
def msgResult = runCommandWithOutput(cmdMessage) // 使用双 %% 转义(Jenkins bat 命令中的 % 需要双写)
if (msgResult.success) { result.author = bat(script: "git log ${commitArg} --pretty=%an", returnStdout: true).trim()
result.message = msgResult.stdout result.message = bat(script: "git log ${commitArg} --pretty=%B", returnStdout: true).trim()
// 提取 Bug ID result.email = bat(script: "git log ${commitArg} --pretty=%ae", returnStdout: true).trim()
def matcher = (result.message =~ /#[0-9]+/)
result.bugId = matcher ? matcher[0] : "未关联Bug"
} }
// 获取邮箱 // 提取 Bug ID
def emailResult = runCommandWithOutput(cmdEmail) def matcher = (result.message =~ /#[0-9]+/)
if (emailResult.success) { result.bugId = matcher ? matcher[0] : "未关联Bug"
result.email = emailResult.stdout
}
} catch (Exception e) { } catch (Exception e) {
echo "获取 Git 提交信息失败: ${e.message}" echo "获取 Git 提交信息失败: ${e.message}"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment