Commit e5ace154 authored by Auto Backup's avatar Auto Backup

refactor: 优雅重构Git信息获取函数

- 新增 executeGitLog() 统一处理跨平台%转义问题
- 使用 replace('%', '%%') 自动转义Windows bat命令中的%符号
- 简化 getGitCommitInfo() 函数,消除重复代码
- 保持向后兼容性和相同功能
parent e7722a69
...@@ -19,6 +19,26 @@ def runPowerShellWithOutput(script) { ...@@ -19,6 +19,26 @@ def runPowerShellWithOutput(script) {
} }
} }
/**
* 执行 Git log 命令,自动处理跨平台 % 转义问题
* @param format Git 的 --pretty 格式字符串(如 %an, %B, %ae)
* @param commitArg Commit 参数(如 -1 或具体的 commit hash)
* @return 命令输出结果(已 trim)
*/
def executeGitLog(format, commitArg = '-1') {
def command = "git log ${commitArg} --pretty=${format}"
if (isUnix()) {
// Unix 系统直接使用 sh,% 不需要转义
return sh(script: command, returnStdout: true).trim()
} else {
// Windows 系统使用 bat,需要将 % 转义为 %%
// 替换 % 为 %% 但要注意不要替换路径中的 %
def escapedCommand = command.replace('%', '%%')
return bat(script: escapedCommand, returnStdout: true).trim()
}
}
/** /**
* 获取 Git 提交信息(统一函数,修复 PowerShell 中 % 转义问题) * 获取 Git 提交信息(统一函数,修复 PowerShell 中 % 转义问题)
* @param commitHash 可选,指定 commit hash,默认使用最新提交 * @param commitHash 可选,指定 commit hash,默认使用最新提交
...@@ -36,18 +56,10 @@ def getGitCommitInfo(commitHash = '') { ...@@ -36,18 +56,10 @@ def getGitCommitInfo(commitHash = '') {
def commitArg = commitHash ? "${commitHash}" : "-1" def commitArg = commitHash ? "${commitHash}" : "-1"
try { try {
if (isUnix()) { // 使用统一的 executeGitLog 函数获取各项信息
// Unix 系统直接使用 sh result.author = executeGitLog('%an', commitArg)
result.author = sh(script: "git log ${commitArg} --pretty=%an", returnStdout: true).trim() result.message = executeGitLog('%B', commitArg)
result.message = sh(script: "git log ${commitArg} --pretty=%B", returnStdout: true).trim() result.email = executeGitLog('%ae', commitArg)
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()
}
// 提取 Bug ID // 提取 Bug ID
def matcher = (result.message =~ /#[0-9]+/) def matcher = (result.message =~ /#[0-9]+/)
...@@ -64,18 +76,6 @@ def getGitCommitInfo(commitHash = '') { ...@@ -64,18 +76,6 @@ def getGitCommitInfo(commitHash = '') {
return result return result
} }
/**
* 跨平台命令执行函数(统一)
*/
def runCommandWithOutput(command) {
try {
def result = runPowerShellWithOutput(command)
return [success: true, stdout: result, stderr: '']
} catch (Exception e) {
return [success: false, stdout: '', stderr: e.message]
}
}
// properties([ // properties([
// pipelineTriggers([ // pipelineTriggers([
// // 使用 Cron 表达式,实现极其精准的控制 // // 使用 Cron 表达式,实现极其精准的控制
......
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