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 = '') {
// 确定使用哪个 commit
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 {
// 获取作者
def authorResult = runCommandWithOutput(cmdAuthor)
if (authorResult.success) {
result.author = authorResult.stdout
if (isUnix()) {
// Unix 系统直接使用 sh
result.author = sh(script: "git log ${commitArg} --pretty=%an", returnStdout: true).trim()
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 命令
// 使用双 %% 转义(Jenkins bat 命令中的 % 需要双写)
result.author = bat(script: "git log ${commitArg} --pretty=%an", returnStdout: true).trim()
result.message = bat(script: "git log ${commitArg} --pretty=%B", returnStdout: true).trim()
result.email = bat(script: "git log ${commitArg} --pretty=%ae", returnStdout: true).trim()
}
// 获取提交信息
def msgResult = runCommandWithOutput(cmdMessage)
if (msgResult.success) {
result.message = msgResult.stdout
// 提取 Bug ID
def matcher = (result.message =~ /#[0-9]+/)
result.bugId = matcher ? matcher[0] : "未关联Bug"
}
// 获取邮箱
def emailResult = runCommandWithOutput(cmdEmail)
if (emailResult.success) {
result.email = emailResult.stdout
}
} catch (Exception e) {
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