window批处理文件(.bat),用来清理git的master分支

@echo off
chcp 65001 > nul
setlocal enabledelayedexpansion

echo 正在检查Git仓库...
git rev-parse --is-inside-work-tree >nul 2>&1
if %errorlevel% neq 0 (
    echo 错误:当前目录不是Git仓库!
    pause
    exit /b 1
)

echo 警告:这将丢弃所有未提交的更改和本地提交!
echo Are you sure to continue? (Y/N)
set /p confirm="Enter Y or N: "
if /i "%confirm%"=="Y" goto proceed
if /i "%confirm%"=="y" goto proceed
echo Operation cancelled.
pause
exit /b 0
:proceed

echo 正在强制同步本地 master 分支到远程状态...
git fetch origin master
if errorlevel 1 (
    echo 错误:git fetch 失败!
    pause
    exit /b 1
)

git checkout master
if errorlevel 1 (
    echo 错误:切换到 master 分支失败!
    pause
    exit /b 1
)

git reset --hard origin/master
if errorlevel 1 (
    echo 错误:重置分支失败!
    pause
    exit /b 1
)

git pull
if errorlevel 1 (
    echo 警告:git pull 执行失败,但同步操作已完成
)

echo 完成!
pause

使用说明:
1、将脚本保存为 reset_git.bat
2、将.bat文件放到git仓库更目录下,双击运行

你提供的是一段 **Linux Bash 脚本**,现在需要将其改为在 Jenkins 中使用 **Windows Shell(即 Windows Batch,`.bat`)** 执行。 但由于以下限制: - Windows 原生命令行(cmd.exe)不支持 `if ! grep`、`$(...)` 等 Bash 特性 - `curl` 和 `ssh` 需要确保已安装并加入 PATH(Git for Windows 提供了这些工具) --- ## ✅ 目标 将你的 Bash 脚本转换为可在 **Jenkins 的 "Execute Windows batch command"** 中运行的 `.bat` 脚本,并实现相同功能: 1. 设置 Git 用户名/邮箱 2. 下载 Gerrit `commit-msg` hook(如果不存在) 3. 添加文件、提交并确保有 `Change-Id` 4. 推送至 Gerrit: `HEAD:refs/for/master` --- ## ✅ 改写后的 Windows Batch 脚本(适用于 Jenkins) ```bat @echo off cd /d C:\home\jenkins\workspace\smb_solution_omada_controller\smb-omadac-v5.10above-win-compile-basic-testing\common_data_management_system :: 设置 Git 用户信息 git config user.name "Jenkins Builder" git config user.email "jenkins@yourcompany.com" :: 定义路径 set GITDIR=.git set HOOKDIR=%GITDIR%\hooks set HOOKFILE=%HOOKDIR%\commit-msg set URL=http://pdgerrit.tp-link.com/tools/hooks/commit-msg :: 创建 hooks 目录(如果不存在) if not exist "%HOOKDIR%" mkdir "%HOOKDIR%" :: 如果 commit-msg 不存在,则下载 if not exist "%HOOKFILE%" ( echo Installing Gerrit commit-msg hook... curl -f -o "%HOOKFILE%" "%URL%" if errorlevel 1 ( echo Failed to download commit-msg hook! exit /b 1 ) echo Successfully installed commit-msg hook. ) :: 添加文件并提交 git add 1.txt git commit -m "Jenkins: test 1.txt to master" :: 强制 amend 一次以确保 Change-Id 被插入(最简单方式,无需判断) echo Ensuring Change-Id is present... git commit --amend --no-edit :: 推送至 Gerrit git push origin HEAD:refs/for/master :: 检查推送是否成功 if errorlevel 1 ( echo ERROR: Push failed! exit /b 1 ) echo Push succeeded. ``` --- ## ✅ 关键说明 | 功能 | 实现方式 | |------|---------| | ✅ 切换目录 | `cd /d C:\...\`(注意路径是 Windows 风格) | | ✅ 设置用户 | `git config user.name/email` | | ✅ 创建 hooks 目录 | `if not exist mkdir` | | ✅ 下载 hook | 使用 `curl -f -o file url`(Git for Windows 自带 curl) | | ✅ 确保 Change-Id | 直接执行 `git commit --amend --no-edit` 最稳妥(钩子会补上) | | ✅ 推送 | `git push origin HEAD:refs/for/master` | > 💡 为什么不用判断是否有 `Change-Id`? > 因为 Windows cmd 很难做 `grep` 类操作。直接 `--amend --no-edit` 是最可靠的做法:它不会改变提交内容,但会触发 `commit-msg` 钩子自动添加 `Change-Id`。 --- ## ✅ 前提条件(必须满足) 1. ✅ Jenkins 构建节点上安装了 **Git for Windows**(推荐 [https://git-scm.com/download/win](https://git-scm.com/download/win)) 2. ✅ `git`, `curl`, `ssh` 在系统 PATH 中可用(Git 安装时选择 “Use Git from Windows Command Prompt” 或 “Git Bash only” 但确保工具可调用) 3. ✅ Jenkins 以能访问 `.ssh/id_rsa` 的用户运行(用于 SSH 推送) 4. ✅ 可访问 `http://pdgerrit.tp-link.com/tools/hooks/commit-msg` --- ## ✅ Jenkins 配置截图建议 在 Jenkins job 中选择: > Build → Execute Windows batch command 然后粘贴上面的 `.bat` 脚本内容。 --- ## ✅ 替代方案:使用 PowerShell(更强大) 如果你允许使用 **PowerShell**(比 Batch 更强),可以这样写: ```powershell $workspace = "C:\home\jenkins\workspace\smb_solution_omada_controller\smb-omadac-v5.10above-win-compile-basic-testing\common_data_management_system" cd $workspace git config user.name "Jenkins Builder" git config user.email "jenkins@yourcompany.com" $gitDir = ".git" $hookDir = "$gitDir\hooks" $hookFile = "$hookDir\commit-msg" $url = "http://pdgerrit.tp-link.com/tools/hooks/commit-msg" if (!(Test-Path $hookDir)) { New-Item -ItemType Directory -Path $hookDir } if (!(Test-Path $hookFile)) { Invoke-WebRequest -Uri $url -OutFile $hookFile chmod +x $hookFile # 如果需要设置权限 } git add 1.txt git commit -m "Jenkins: test 1.txt to master" git commit --amend --no-edit git push origin HEAD:refs/for/master ``` > 在 Jenkins 中选择:**"Execute Windows PowerShell script"** --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值