From b9e5fc82eb77ef04d6dd6fd562b4ba87e1634804 Mon Sep 17 00:00:00 2001 From: Tony <424635328@qq.com> Date: Sat, 19 Apr 2025 21:55:03 +0800 Subject: [PATCH 1/7] . --- s.bat | 211 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 211 insertions(+) create mode 100644 s.bat diff --git a/s.bat b/s.bat new file mode 100644 index 0000000000..e0e1086258 --- /dev/null +++ b/s.bat @@ -0,0 +1,211 @@ +@echo off +chcp 65001 > nul + +title HowToCook 项目贡献助手 + +:mainMenu +echo. +echo ============================================ +echo HowToCook 项目贡献助手 +echo ============================================ +echo. +echo 请选择操作: +echo 1. 创建/切换分支 +echo 2. 添加修改 +echo 3. 提交修改 +echo 4. 推送分支 +echo 5. 同步 Fork +echo 6. 设置 upstream +echo 7. 删除本地分支 +echo 8. 删除远程分支 +echo 0. 退出 +echo. +set /p "choice=请选择 (0-8): " + +REM 使用更简洁的 IF 语句结构 +if "%choice%"=="0" goto exit +if "%choice%"=="1" goto createBranch +if "%choice%"=="2" goto addChanges +if "%choice%"=="3" goto commitChanges +if "%choice%"=="4" goto pushBranch +if "%choice%"=="5" goto pullUpstream +if "%choice%"=="6" goto setupUpstream +if "%choice%"=="7" goto deleteLocalBranch +if "%choice%"=="8" goto deleteRemoteBranch + +echo. +echo 无效的选择,请重新选择. +pause +goto mainMenu + +:createBranch +echo. +echo -------------------------------------------- +echo 创建并切换到新分支 +echo -------------------------------------------- +echo. +set /p "branchName=请输入新分支名称: " +git checkout -b "%branchName%" +if %errorlevel% neq 0 ( + echo 创建分支失败,错误代码: %errorlevel% + echo 请检查分支名称是否合法或已存在。 可以尝试使用git branch查看已存在的分支 + pause + goto mainMenu +) +echo 已创建并切换到分支 "%branchName%" +pause +goto mainMenu + +:addChanges +echo. +echo -------------------------------------------- +echo 添加修改 +echo -------------------------------------------- +echo. +set /p "addType=输入 a 添加所有文件, 或输入文件名添加特定文件: " +if /i "%addType%"=="a" ( + git add . + if %errorlevel% neq 0 ( + echo 添加所有文件失败,错误代码: %errorlevel% + echo 请检查是否在git仓库目录下运行 或者是否有文件未保存 + pause + goto mainMenu + ) + echo 已添加所有文件到暂存区。 +) else ( + git add "%addType%" + if %errorlevel% neq 0 ( + echo 添加文件 "%addType%" 失败,错误代码: %errorlevel% + echo 请检查文件名 "%addType%" 是否正确, 可以使用git status查看 + pause + goto mainMenu + ) + echo 已添加文件 "%addType%" 到暂存区。 +) +pause +goto mainMenu + +:commitChanges +echo. +echo -------------------------------------------- +echo 提交修改 +echo -------------------------------------------- +echo. +set /p "commitMessage=请输入提交信息: " +git commit -m "%commitMessage%" +if %errorlevel% neq 0 ( + echo 提交失败,错误代码: %errorlevel% + echo 请检查暂存区是否为空(git status)或者是否有未解决的冲突(git status)。 + pause + goto mainMenu +) +echo 提交成功,提交信息: "%commitMessage%" +pause +goto mainMenu + +:pushBranch +echo. +echo -------------------------------------------- +echo 推送分支到 GitHub +echo -------------------------------------------- +echo. +set /p "pushBranch=请输入要推送的分支名称: " +git push origin "%pushBranch%" +if %errorlevel% neq 0 ( + echo 推送分支 "%pushBranch%" 失败,错误代码: %errorlevel% + echo 请检查分支名称是否正确, 远程仓库配置是否正确, 或者权限是否足够。 可以尝试git remote -v 和 git branch -vv + pause + goto mainMenu +) +echo 分支 "%pushBranch%" 已成功推送到 origin。 +pause +goto mainMenu + +:pullUpstream +echo. +echo -------------------------------------------- +echo 同步 Fork +echo -------------------------------------------- +echo. +git checkout master +if %errorlevel% neq 0 ( + echo 切换到 master 分支失败,错误代码: %errorlevel% + echo 请检查本地是否存在名为master的分支,或者是否有未提交的修改。 + pause + goto mainMenu +) +git pull upstream master +if %errorlevel% neq 0 ( + echo 从 upstream 拉取 master 分支失败,错误代码: %errorlevel% + echo 请检查upstream是否配置正确, 可以尝试git remote -v查看 + pause + goto mainMenu +) +git push origin master +if %errorlevel% neq 0 ( + echo 推送 master 分支到 origin 失败,错误代码: %errorlevel% + echo 请检查远程仓库配置是否正确, 远程分支是否存在。 可以尝试git remote -v 和 git branch -vv + pause + goto mainMenu +) +echo 已成功从 upstream 同步并推送到 origin/master。 +pause +goto mainMenu + +:setupUpstream +echo. +echo -------------------------------------------- +echo 设置 upstream +echo -------------------------------------------- +echo. +git remote add upstream https://github.com/Anduin2017/HowToCook.git +if %errorlevel% neq 0 ( + echo 设置 upstream 失败,错误代码: %errorlevel% + echo 请检查是否已经设置过upstream或者upstream地址是否正确。 可以尝试git remote -v查看 + pause + goto mainMenu +) +echo 已成功设置 upstream 为 https://github.com/Anduin2017/HowToCook.git。 +pause +goto mainMenu + +:deleteLocalBranch +echo. +echo -------------------------------------------- +echo 删除本地分支 +echo -------------------------------------------- +echo. +set /p "localBranch=请输入要删除的本地分支名称: " +git branch -d "%localBranch%" +if %errorlevel% neq 0 ( + echo 删除本地分支 "%localBranch%" 失败,错误代码: %errorlevel% + echo 请确保分支存在且已合并。如果未合并,请使用 -D 强制删除。 可以尝试git branch查看本地分支 + pause + goto mainMenu +) +echo 已成功删除本地分支 "%localBranch%"。 +pause +goto mainMenu + +:deleteRemoteBranch +echo. +echo -------------------------------------------- +echo 删除远程分支 +echo -------------------------------------------- +echo. +set /p "remoteBranch=请输入要删除的远程分支名称: " +git push origin --delete "%remoteBranch%" +if %errorlevel% neq 0 ( + echo 删除远程分支 "%remoteBranch%" 失败,错误代码: %errorlevel% + echo 请检查分支名称是否正确或者权限是否足够。 可以尝试git remote -v 和 git branch -vv + pause + goto mainMenu +) +echo 已成功删除远程分支 "%remoteBranch%"。 +pause +goto mainMenu + +:exit +echo. +echo 感谢使用! +exit \ No newline at end of file From 2fe246a27f6dcc69542768a6d15032cf5b49994a Mon Sep 17 00:00:00 2001 From: Tony <424635328@qq.com> Date: Sat, 19 Apr 2025 22:02:51 +0800 Subject: [PATCH 2/7] . --- s.bat | 287 ++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 210 insertions(+), 77 deletions(-) diff --git a/s.bat b/s.bat index e0e1086258..6d07e908da 100644 --- a/s.bat +++ b/s.bat @@ -3,24 +3,28 @@ chcp 65001 > nul title HowToCook 项目贡献助手 + :mainMenu +cls REM 清屏 +echo. +echo ====================================================== +echo [ HowToCook 项目贡献助手 ] +echo ====================================================== +echo. +echo 请选择操作: echo. -echo ============================================ -echo HowToCook 项目贡献助手 -echo ============================================ +echo [1] 创建/切换分支 +echo [2] 添加修改 +echo [3] 提交修改 +echo [4] 推送分支 +echo [5] 同步 Fork (从 Upstream 更新) +echo [6] 设置 Upstream 仓库地址 +echo [7] 删除本地分支 +echo [8] 删除远程分支 echo. -echo 请选择操作: -echo 1. 创建/切换分支 -echo 2. 添加修改 -echo 3. 提交修改 -echo 4. 推送分支 -echo 5. 同步 Fork -echo 6. 设置 upstream -echo 7. 删除本地分支 -echo 8. 删除远程分支 -echo 0. 退出 +echo [0] 退出 echo. -set /p "choice=请选择 (0-8): " +set /p "choice= 请选择 (0-8): " REM 使用更简洁的 IF 语句结构 if "%choice%"=="0" goto exit @@ -34,178 +38,307 @@ if "%choice%"=="7" goto deleteLocalBranch if "%choice%"=="8" goto deleteRemoteBranch echo. -echo 无效的选择,请重新选择. +echo **错误**: 无效的选择,请重新选择. pause goto mainMenu :createBranch +cls echo. -echo -------------------------------------------- -echo 创建并切换到新分支 -echo -------------------------------------------- +echo ====================================================== +echo [1] 创建并切换到新分支 +echo ====================================================== echo. -set /p "branchName=请输入新分支名称: " +set /p "branchName= 请输入新分支名称: " + +REM 分支名称校验(简单示例,更严格的校验可参考正则表达式) +if "%branchName%"=="" ( + echo. + echo **错误**: 分支名称不能为空! + pause + goto mainMenu +) +echo. +echo 正在创建并切换到分支 "%branchName%"... git checkout -b "%branchName%" if %errorlevel% neq 0 ( - echo 创建分支失败,错误代码: %errorlevel% - echo 请检查分支名称是否合法或已存在。 可以尝试使用git branch查看已存在的分支 + echo. + echo **错误**: 创建分支失败,错误代码: %errorlevel% + echo 请检查分支名称是否合法或已存在。 可以尝试使用git branch查看已存在的分支 pause goto mainMenu ) -echo 已创建并切换到分支 "%branchName%" +echo. +echo 已成功创建并切换到分支 "%branchName%" pause goto mainMenu :addChanges +cls echo. -echo -------------------------------------------- -echo 添加修改 -echo -------------------------------------------- +echo ====================================================== +echo [2] 添加修改 +echo ====================================================== echo. -set /p "addType=输入 a 添加所有文件, 或输入文件名添加特定文件: " +echo [a] 添加所有文件 +echo [文件名] 添加指定文件 (例如: README.md) +echo. +set /p "addType= 请输入选项 (a 或 文件名): " if /i "%addType%"=="a" ( + echo. + echo 正在添加所有文件... git add . if %errorlevel% neq 0 ( - echo 添加所有文件失败,错误代码: %errorlevel% - echo 请检查是否在git仓库目录下运行 或者是否有文件未保存 + echo. + echo **错误**: 添加所有文件失败,错误代码: %errorlevel% + echo 请检查是否在git仓库目录下运行 或者是否有文件未保存 pause goto mainMenu ) - echo 已添加所有文件到暂存区。 + echo. + echo 已添加所有文件到暂存区。 ) else ( + echo. + echo 正在添加文件 "%addType%"... git add "%addType%" if %errorlevel% neq 0 ( - echo 添加文件 "%addType%" 失败,错误代码: %errorlevel% - echo 请检查文件名 "%addType%" 是否正确, 可以使用git status查看 + echo. + echo **错误**: 添加文件 "%addType%" 失败,错误代码: %errorlevel% + echo 请检查文件名 "%addType%" 是否正确, 可以使用git status查看 pause goto mainMenu ) - echo 已添加文件 "%addType%" 到暂存区。 + echo. + echo 已添加文件 "%addType%" 到暂存区。 ) pause goto mainMenu :commitChanges +cls echo. -echo -------------------------------------------- -echo 提交修改 -echo -------------------------------------------- +echo ====================================================== +echo [3] 提交修改 +echo ====================================================== echo. -set /p "commitMessage=请输入提交信息: " +set /p "commitMessage= 请输入提交信息: " +if "%commitMessage%"=="" ( + echo. + echo **错误**: 提交信息不能为空! + pause + goto mainMenu +) +echo. +echo 正在提交修改... git commit -m "%commitMessage%" if %errorlevel% neq 0 ( - echo 提交失败,错误代码: %errorlevel% - echo 请检查暂存区是否为空(git status)或者是否有未解决的冲突(git status)。 + echo. + echo **错误**: 提交失败,错误代码: %errorlevel% + echo 请检查暂存区是否为空(git status)或者是否有未解决的冲突(git status)。 pause goto mainMenu ) -echo 提交成功,提交信息: "%commitMessage%" +echo. +echo 提交成功,提交信息: "%commitMessage%" pause goto mainMenu :pushBranch +cls +echo. +echo ====================================================== +echo [4] 推送分支到 GitHub +echo ====================================================== echo. -echo -------------------------------------------- -echo 推送分支到 GitHub -echo -------------------------------------------- +set /p "pushBranch= 请输入要推送的分支名称: " +if "%pushBranch%"=="" ( + echo. + echo **错误**: 分支名称不能为空! + pause + goto mainMenu +) echo. -set /p "pushBranch=请输入要推送的分支名称: " +echo 正在推送分支 "%pushBranch%"... git push origin "%pushBranch%" if %errorlevel% neq 0 ( - echo 推送分支 "%pushBranch%" 失败,错误代码: %errorlevel% - echo 请检查分支名称是否正确, 远程仓库配置是否正确, 或者权限是否足够。 可以尝试git remote -v 和 git branch -vv + echo. + echo **错误**: 推送分支 "%pushBranch%" 失败,错误代码: %errorlevel% + echo 请检查分支名称是否正确, 远程仓库配置是否正确, 或者权限是否足够。 可以尝试git remote -v 和 git branch -vv + echo. + echo 常见错误: + echo. + echo (1) 没有推送权限:确认你是否是该仓库的贡献者,并拥有push权限 + echo (2) 分支名称错误:确认分支名称书写正确, 大小写是否正确。 + echo (3) 远程仓库不存在: 确认远程仓库地址配置正确,用 git remote -v 查看 pause goto mainMenu ) -echo 分支 "%pushBranch%" 已成功推送到 origin。 +echo. +echo 分支 "%pushBranch%" 已成功推送到 origin。 pause goto mainMenu :pullUpstream +cls echo. -echo -------------------------------------------- -echo 同步 Fork -echo -------------------------------------------- +echo ====================================================== +echo [5] 同步 Fork (从 Upstream 更新) +echo ====================================================== echo. +echo 正在切换到 master 分支... git checkout master if %errorlevel% neq 0 ( - echo 切换到 master 分支失败,错误代码: %errorlevel% - echo 请检查本地是否存在名为master的分支,或者是否有未提交的修改。 + echo. + echo **错误**: 切换到 master 分支失败,错误代码: %errorlevel% + echo 请检查本地是否存在名为master的分支,或者是否有未提交的修改。 pause goto mainMenu ) +echo. +echo 正在从 upstream 拉取 master 分支... git pull upstream master if %errorlevel% neq 0 ( - echo 从 upstream 拉取 master 分支失败,错误代码: %errorlevel% - echo 请检查upstream是否配置正确, 可以尝试git remote -v查看 + echo. + echo **错误**: 从 upstream 拉取 master 分支失败,错误代码: %errorlevel% + echo 请检查upstream是否配置正确, 可以尝试git remote -v查看 + echo. + echo 常见错误: + echo. + echo (1) upstream 未配置:确认 upstream 已经正确设置,使用 git remote -v 查看 + echo (2) 网络问题:确认网络连接正常 pause goto mainMenu ) +echo. +echo 正在推送到 origin/master... git push origin master if %errorlevel% neq 0 ( - echo 推送 master 分支到 origin 失败,错误代码: %errorlevel% - echo 请检查远程仓库配置是否正确, 远程分支是否存在。 可以尝试git remote -v 和 git branch -vv + echo. + echo **错误**: 推送 master 分支到 origin 失败,错误代码: %errorlevel% + echo 请检查远程仓库配置是否正确, 远程分支是否存在。 可以尝试git remote -v 和 git branch -vv + echo. + echo 常见错误: + echo. + echo (1) 没有推送权限:确认你是否是该仓库的贡献者,并拥有push权限 + echo (2) 远程仓库不存在: 确认远程仓库地址配置正确,用 git remote -v 查看 pause goto mainMenu ) -echo 已成功从 upstream 同步并推送到 origin/master。 +echo. +echo 已成功从 upstream 同步并推送到 origin/master。 pause goto mainMenu :setupUpstream +cls echo. -echo -------------------------------------------- -echo 设置 upstream -echo -------------------------------------------- +echo ====================================================== +echo [6] 设置 Upstream 仓库地址 +echo ====================================================== echo. +echo 正在设置 upstream... git remote add upstream https://github.com/Anduin2017/HowToCook.git if %errorlevel% neq 0 ( - echo 设置 upstream 失败,错误代码: %errorlevel% - echo 请检查是否已经设置过upstream或者upstream地址是否正确。 可以尝试git remote -v查看 + echo. + echo **错误**: 设置 upstream 失败,错误代码: %errorlevel% + echo 请检查是否已经设置过upstream或者upstream地址是否正确。 可以尝试git remote -v查看 + echo. + echo 常见错误: + echo. + echo (1) upstream 已经存在: 确认是否重复设置,使用 git remote -v 查看 + echo (2) 网络问题:确认网络连接正常 pause goto mainMenu ) -echo 已成功设置 upstream 为 https://github.com/Anduin2017/HowToCook.git。 +echo. +echo 已成功设置 upstream 为 https://github.com/Anduin2017/HowToCook.git。 pause goto mainMenu :deleteLocalBranch +cls echo. -echo -------------------------------------------- -echo 删除本地分支 -echo -------------------------------------------- +echo ====================================================== +echo [7] 删除本地分支 +echo ====================================================== +echo. +set /p "localBranch= 请输入要删除的本地分支名称: " +if "%localBranch%"=="" ( + echo. + echo **错误**: 分支名称不能为空! + pause + goto mainMenu +) + +REM 检查本地分支是否存在 +for /f "tokens=*" %%a in ('git branch ^| findstr "%localBranch%"') do set "branchExists=true" + +if not defined branchExists ( + echo. + echo **错误**: 本地分支 "%localBranch%" 不存在! + pause + goto mainMenu +) + echo. -set /p "localBranch=请输入要删除的本地分支名称: " +echo 正在删除本地分支 "%localBranch%"... git branch -d "%localBranch%" if %errorlevel% neq 0 ( - echo 删除本地分支 "%localBranch%" 失败,错误代码: %errorlevel% - echo 请确保分支存在且已合并。如果未合并,请使用 -D 强制删除。 可以尝试git branch查看本地分支 + echo. + echo **错误**: 删除本地分支 "%localBranch%" 失败,错误代码: %errorlevel% + echo 请确保分支存在且已合并。如果未合并,请使用 -D 强制删除。 可以尝试git branch查看本地分支 + echo. + echo 常见错误: + echo. + echo (1) 分支未合并: 如果确定要删除,可以使用 git branch -D "%localBranch%" (请谨慎使用) + echo (2) 权限问题: 确认对本地仓库有操作权限 pause goto mainMenu ) -echo 已成功删除本地分支 "%localBranch%"。 +echo. +echo 已成功删除本地分支 "%localBranch%"。 pause goto mainMenu :deleteRemoteBranch +cls +echo. +echo ====================================================== +echo [8] 删除远程分支 +echo ====================================================== echo. -echo -------------------------------------------- -echo 删除远程分支 -echo -------------------------------------------- +set /p "remoteBranch= 请输入要删除的远程分支名称: " +if "%remoteBranch%"=="" ( + echo. + echo **错误**: 分支名称不能为空! + pause + goto mainMenu +) + +REM 检查远程分支是否存在 (简化,实际更复杂,需要fetch后再检查) echo. -set /p "remoteBranch=请输入要删除的远程分支名称: " +echo 正在删除远程分支 "%remoteBranch%"... git push origin --delete "%remoteBranch%" if %errorlevel% neq 0 ( - echo 删除远程分支 "%remoteBranch%" 失败,错误代码: %errorlevel% - echo 请检查分支名称是否正确或者权限是否足够。 可以尝试git remote -v 和 git branch -vv + echo. + echo **错误**: 删除远程分支 "%remoteBranch%" 失败,错误代码: %errorlevel% + echo 请检查分支名称是否正确或者权限是否足够。 可以尝试git remote -v 和 git branch -vv + echo. + echo 常见错误: + echo. + echo (1) 没有删除权限:确认你是否是该仓库的贡献者,并拥有删除远程分支的权限 + echo (2) 远程分支不存在:确认远程分支名称是否正确 + echo (3) 网络问题:确认网络连接正常 pause goto mainMenu ) -echo 已成功删除远程分支 "%remoteBranch%"。 +echo. +echo 已成功删除远程分支 "%remoteBranch%"。 pause goto mainMenu :exit +cls echo. -echo 感谢使用! +echo 感谢使用! exit \ No newline at end of file From c4ac85717e4c1ad366a7fb615ae462749b90e4d3 Mon Sep 17 00:00:00 2001 From: Tony <424635328@qq.com> Date: Sat, 19 Apr 2025 22:11:42 +0800 Subject: [PATCH 3/7] "04.19" --- s.bat | 72 +++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 60 insertions(+), 12 deletions(-) diff --git a/s.bat b/s.bat index 6d07e908da..e733c62647 100644 --- a/s.bat +++ b/s.bat @@ -3,7 +3,6 @@ chcp 65001 > nul title HowToCook 项目贡献助手 - :mainMenu cls REM 清屏 echo. @@ -19,12 +18,13 @@ echo [3] 提交修改 echo [4] 推送分支 echo [5] 同步 Fork (从 Upstream 更新) echo [6] 设置 Upstream 仓库地址 +echo [9] 添加远程仓库 echo [7] 删除本地分支 echo [8] 删除远程分支 echo. echo [0] 退出 echo. -set /p "choice= 请选择 (0-8): " +set /p "choice= 请选择 (0-9): " REM 使用更简洁的 IF 语句结构 if "%choice%"=="0" goto exit @@ -34,6 +34,7 @@ if "%choice%"=="3" goto commitChanges if "%choice%"=="4" goto pushBranch if "%choice%"=="5" goto pullUpstream if "%choice%"=="6" goto setupUpstream +if "%choice%"=="9" goto addRemote if "%choice%"=="7" goto deleteLocalBranch if "%choice%"=="8" goto deleteRemoteBranch @@ -122,24 +123,24 @@ echo [3] 提交修改 echo ====================================================== echo. set /p "commitMessage= 请输入提交信息: " -if "%commitMessage%"=="" ( - echo. - echo **错误**: 提交信息不能为空! - pause - goto mainMenu -) + +REM 创建一个临时文件来存储提交信息 +echo "%commitMessage%" > temp_commit_message.txt + echo. echo 正在提交修改... -git commit -m "%commitMessage%" +git commit --file=temp_commit_message.txt if %errorlevel% neq 0 ( echo. echo **错误**: 提交失败,错误代码: %errorlevel% echo 请检查暂存区是否为空(git status)或者是否有未解决的冲突(git status)。 + del temp_commit_message.txt REM 删除临时文件 pause goto mainMenu ) echo. echo 提交成功,提交信息: "%commitMessage%" +del temp_commit_message.txt REM 删除临时文件 pause goto mainMenu @@ -150,6 +151,15 @@ echo ====================================================== echo [4] 推送分支到 GitHub echo ====================================================== echo. + +REM 询问用户要推送到的远程仓库名称 +echo 如果您的 fork 仓库 (git@github.com:424635328/HowToCook.git 或 https://github.com/424635328/HowToCook) 已设置为 origin,请直接回车。 +echo 如果未设置,请输入您的 fork 仓库的远程仓库名称,或输入 "add" 添加一个新的远程仓库。 +set /p "remoteName= 请输入要推送到的远程仓库名称 (默认为 origin): " +if "%remoteName%"=="" set "remoteName=origin" + +if /i "%remoteName%"=="add" goto addRemote + set /p "pushBranch= 请输入要推送的分支名称: " if "%pushBranch%"=="" ( echo. @@ -158,8 +168,8 @@ if "%pushBranch%"=="" ( goto mainMenu ) echo. -echo 正在推送分支 "%pushBranch%"... -git push origin "%pushBranch%" +echo 正在推送分支 "%pushBranch%" 到 "%remoteName%"... +git push "%remoteName%" "%pushBranch%" if %errorlevel% neq 0 ( echo. echo **错误**: 推送分支 "%pushBranch%" 失败,错误代码: %errorlevel% @@ -174,7 +184,7 @@ if %errorlevel% neq 0 ( goto mainMenu ) echo. -echo 分支 "%pushBranch%" 已成功推送到 origin。 +echo 分支 "%pushBranch%" 已成功推送到 "%remoteName%"。 pause goto mainMenu @@ -255,6 +265,44 @@ echo 已成功设置 upstream 为 https://github.com/Anduin2017/HowToCook.git pause goto mainMenu +:addRemote +cls +echo. +echo ====================================================== +echo [添加远程仓库] +echo ====================================================== +echo. +set /p "newRemoteName= 请输入新的远程仓库名称: " +if "%newRemoteName%"=="" ( + echo. + echo **错误**: 远程仓库名称不能为空! + pause + goto mainMenu +) + +set /p "newRemoteURL= 请输入新的远程仓库地址 (例如: git@github.com:424635328/HowToCook.git 或 https://github.com/424635328/HowToCook): " +if "%newRemoteURL%"=="" ( + echo. + echo **错误**: 远程仓库地址不能为空! + pause + goto mainMenu +) + +echo. +echo 正在添加远程仓库 "%newRemoteName%",地址为 "%newRemoteURL%" ... +git remote add "%newRemoteName%" "%newRemoteURL%" +if %errorlevel% neq 0 ( + echo. + echo **错误**: 添加远程仓库失败,错误代码: %errorlevel% + echo 请检查远程仓库名称是否已存在, 或者远程仓库地址是否正确。 可以尝试git remote -v查看 + pause + goto mainMenu +) +echo. +echo 已成功添加远程仓库 "%newRemoteName%",地址为 "%newRemoteURL%"。 +pause +goto pushBranch REM 返回到推送分支操作 + :deleteLocalBranch cls echo. From 87ab0cca3ee5bd7f226acd92c9b4b63927ea075b Mon Sep 17 00:00:00 2001 From: Tony <424635328@qq.com> Date: Sat, 19 Apr 2025 22:17:37 +0800 Subject: [PATCH 4/7] . --- s.bat | 392 ---------------------------------------------------------- s.py | 388 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 388 insertions(+), 392 deletions(-) delete mode 100644 s.bat create mode 100644 s.py diff --git a/s.bat b/s.bat deleted file mode 100644 index e733c62647..0000000000 --- a/s.bat +++ /dev/null @@ -1,392 +0,0 @@ -@echo off -chcp 65001 > nul - -title HowToCook 项目贡献助手 - -:mainMenu -cls REM 清屏 -echo. -echo ====================================================== -echo [ HowToCook 项目贡献助手 ] -echo ====================================================== -echo. -echo 请选择操作: -echo. -echo [1] 创建/切换分支 -echo [2] 添加修改 -echo [3] 提交修改 -echo [4] 推送分支 -echo [5] 同步 Fork (从 Upstream 更新) -echo [6] 设置 Upstream 仓库地址 -echo [9] 添加远程仓库 -echo [7] 删除本地分支 -echo [8] 删除远程分支 -echo. -echo [0] 退出 -echo. -set /p "choice= 请选择 (0-9): " - -REM 使用更简洁的 IF 语句结构 -if "%choice%"=="0" goto exit -if "%choice%"=="1" goto createBranch -if "%choice%"=="2" goto addChanges -if "%choice%"=="3" goto commitChanges -if "%choice%"=="4" goto pushBranch -if "%choice%"=="5" goto pullUpstream -if "%choice%"=="6" goto setupUpstream -if "%choice%"=="9" goto addRemote -if "%choice%"=="7" goto deleteLocalBranch -if "%choice%"=="8" goto deleteRemoteBranch - -echo. -echo **错误**: 无效的选择,请重新选择. -pause -goto mainMenu - -:createBranch -cls -echo. -echo ====================================================== -echo [1] 创建并切换到新分支 -echo ====================================================== -echo. -set /p "branchName= 请输入新分支名称: " - -REM 分支名称校验(简单示例,更严格的校验可参考正则表达式) -if "%branchName%"=="" ( - echo. - echo **错误**: 分支名称不能为空! - pause - goto mainMenu -) -echo. -echo 正在创建并切换到分支 "%branchName%"... -git checkout -b "%branchName%" -if %errorlevel% neq 0 ( - echo. - echo **错误**: 创建分支失败,错误代码: %errorlevel% - echo 请检查分支名称是否合法或已存在。 可以尝试使用git branch查看已存在的分支 - pause - goto mainMenu -) -echo. -echo 已成功创建并切换到分支 "%branchName%" -pause -goto mainMenu - -:addChanges -cls -echo. -echo ====================================================== -echo [2] 添加修改 -echo ====================================================== -echo. -echo [a] 添加所有文件 -echo [文件名] 添加指定文件 (例如: README.md) -echo. -set /p "addType= 请输入选项 (a 或 文件名): " -if /i "%addType%"=="a" ( - echo. - echo 正在添加所有文件... - git add . - if %errorlevel% neq 0 ( - echo. - echo **错误**: 添加所有文件失败,错误代码: %errorlevel% - echo 请检查是否在git仓库目录下运行 或者是否有文件未保存 - pause - goto mainMenu - ) - echo. - echo 已添加所有文件到暂存区。 -) else ( - echo. - echo 正在添加文件 "%addType%"... - git add "%addType%" - if %errorlevel% neq 0 ( - echo. - echo **错误**: 添加文件 "%addType%" 失败,错误代码: %errorlevel% - echo 请检查文件名 "%addType%" 是否正确, 可以使用git status查看 - pause - goto mainMenu - ) - echo. - echo 已添加文件 "%addType%" 到暂存区。 -) -pause -goto mainMenu - -:commitChanges -cls -echo. -echo ====================================================== -echo [3] 提交修改 -echo ====================================================== -echo. -set /p "commitMessage= 请输入提交信息: " - -REM 创建一个临时文件来存储提交信息 -echo "%commitMessage%" > temp_commit_message.txt - -echo. -echo 正在提交修改... -git commit --file=temp_commit_message.txt -if %errorlevel% neq 0 ( - echo. - echo **错误**: 提交失败,错误代码: %errorlevel% - echo 请检查暂存区是否为空(git status)或者是否有未解决的冲突(git status)。 - del temp_commit_message.txt REM 删除临时文件 - pause - goto mainMenu -) -echo. -echo 提交成功,提交信息: "%commitMessage%" -del temp_commit_message.txt REM 删除临时文件 -pause -goto mainMenu - -:pushBranch -cls -echo. -echo ====================================================== -echo [4] 推送分支到 GitHub -echo ====================================================== -echo. - -REM 询问用户要推送到的远程仓库名称 -echo 如果您的 fork 仓库 (git@github.com:424635328/HowToCook.git 或 https://github.com/424635328/HowToCook) 已设置为 origin,请直接回车。 -echo 如果未设置,请输入您的 fork 仓库的远程仓库名称,或输入 "add" 添加一个新的远程仓库。 -set /p "remoteName= 请输入要推送到的远程仓库名称 (默认为 origin): " -if "%remoteName%"=="" set "remoteName=origin" - -if /i "%remoteName%"=="add" goto addRemote - -set /p "pushBranch= 请输入要推送的分支名称: " -if "%pushBranch%"=="" ( - echo. - echo **错误**: 分支名称不能为空! - pause - goto mainMenu -) -echo. -echo 正在推送分支 "%pushBranch%" 到 "%remoteName%"... -git push "%remoteName%" "%pushBranch%" -if %errorlevel% neq 0 ( - echo. - echo **错误**: 推送分支 "%pushBranch%" 失败,错误代码: %errorlevel% - echo 请检查分支名称是否正确, 远程仓库配置是否正确, 或者权限是否足够。 可以尝试git remote -v 和 git branch -vv - echo. - echo 常见错误: - echo. - echo (1) 没有推送权限:确认你是否是该仓库的贡献者,并拥有push权限 - echo (2) 分支名称错误:确认分支名称书写正确, 大小写是否正确。 - echo (3) 远程仓库不存在: 确认远程仓库地址配置正确,用 git remote -v 查看 - pause - goto mainMenu -) -echo. -echo 分支 "%pushBranch%" 已成功推送到 "%remoteName%"。 -pause -goto mainMenu - -:pullUpstream -cls -echo. -echo ====================================================== -echo [5] 同步 Fork (从 Upstream 更新) -echo ====================================================== -echo. -echo 正在切换到 master 分支... -git checkout master -if %errorlevel% neq 0 ( - echo. - echo **错误**: 切换到 master 分支失败,错误代码: %errorlevel% - echo 请检查本地是否存在名为master的分支,或者是否有未提交的修改。 - pause - goto mainMenu -) -echo. -echo 正在从 upstream 拉取 master 分支... -git pull upstream master -if %errorlevel% neq 0 ( - echo. - echo **错误**: 从 upstream 拉取 master 分支失败,错误代码: %errorlevel% - echo 请检查upstream是否配置正确, 可以尝试git remote -v查看 - echo. - echo 常见错误: - echo. - echo (1) upstream 未配置:确认 upstream 已经正确设置,使用 git remote -v 查看 - echo (2) 网络问题:确认网络连接正常 - pause - goto mainMenu -) -echo. -echo 正在推送到 origin/master... -git push origin master -if %errorlevel% neq 0 ( - echo. - echo **错误**: 推送 master 分支到 origin 失败,错误代码: %errorlevel% - echo 请检查远程仓库配置是否正确, 远程分支是否存在。 可以尝试git remote -v 和 git branch -vv - echo. - echo 常见错误: - echo. - echo (1) 没有推送权限:确认你是否是该仓库的贡献者,并拥有push权限 - echo (2) 远程仓库不存在: 确认远程仓库地址配置正确,用 git remote -v 查看 - pause - goto mainMenu -) -echo. -echo 已成功从 upstream 同步并推送到 origin/master。 -pause -goto mainMenu - -:setupUpstream -cls -echo. -echo ====================================================== -echo [6] 设置 Upstream 仓库地址 -echo ====================================================== -echo. -echo 正在设置 upstream... -git remote add upstream https://github.com/Anduin2017/HowToCook.git -if %errorlevel% neq 0 ( - echo. - echo **错误**: 设置 upstream 失败,错误代码: %errorlevel% - echo 请检查是否已经设置过upstream或者upstream地址是否正确。 可以尝试git remote -v查看 - echo. - echo 常见错误: - echo. - echo (1) upstream 已经存在: 确认是否重复设置,使用 git remote -v 查看 - echo (2) 网络问题:确认网络连接正常 - pause - goto mainMenu -) -echo. -echo 已成功设置 upstream 为 https://github.com/Anduin2017/HowToCook.git。 -pause -goto mainMenu - -:addRemote -cls -echo. -echo ====================================================== -echo [添加远程仓库] -echo ====================================================== -echo. -set /p "newRemoteName= 请输入新的远程仓库名称: " -if "%newRemoteName%"=="" ( - echo. - echo **错误**: 远程仓库名称不能为空! - pause - goto mainMenu -) - -set /p "newRemoteURL= 请输入新的远程仓库地址 (例如: git@github.com:424635328/HowToCook.git 或 https://github.com/424635328/HowToCook): " -if "%newRemoteURL%"=="" ( - echo. - echo **错误**: 远程仓库地址不能为空! - pause - goto mainMenu -) - -echo. -echo 正在添加远程仓库 "%newRemoteName%",地址为 "%newRemoteURL%" ... -git remote add "%newRemoteName%" "%newRemoteURL%" -if %errorlevel% neq 0 ( - echo. - echo **错误**: 添加远程仓库失败,错误代码: %errorlevel% - echo 请检查远程仓库名称是否已存在, 或者远程仓库地址是否正确。 可以尝试git remote -v查看 - pause - goto mainMenu -) -echo. -echo 已成功添加远程仓库 "%newRemoteName%",地址为 "%newRemoteURL%"。 -pause -goto pushBranch REM 返回到推送分支操作 - -:deleteLocalBranch -cls -echo. -echo ====================================================== -echo [7] 删除本地分支 -echo ====================================================== -echo. -set /p "localBranch= 请输入要删除的本地分支名称: " -if "%localBranch%"=="" ( - echo. - echo **错误**: 分支名称不能为空! - pause - goto mainMenu -) - -REM 检查本地分支是否存在 -for /f "tokens=*" %%a in ('git branch ^| findstr "%localBranch%"') do set "branchExists=true" - -if not defined branchExists ( - echo. - echo **错误**: 本地分支 "%localBranch%" 不存在! - pause - goto mainMenu -) - -echo. -echo 正在删除本地分支 "%localBranch%"... -git branch -d "%localBranch%" -if %errorlevel% neq 0 ( - echo. - echo **错误**: 删除本地分支 "%localBranch%" 失败,错误代码: %errorlevel% - echo 请确保分支存在且已合并。如果未合并,请使用 -D 强制删除。 可以尝试git branch查看本地分支 - echo. - echo 常见错误: - echo. - echo (1) 分支未合并: 如果确定要删除,可以使用 git branch -D "%localBranch%" (请谨慎使用) - echo (2) 权限问题: 确认对本地仓库有操作权限 - pause - goto mainMenu -) -echo. -echo 已成功删除本地分支 "%localBranch%"。 -pause -goto mainMenu - -:deleteRemoteBranch -cls -echo. -echo ====================================================== -echo [8] 删除远程分支 -echo ====================================================== -echo. -set /p "remoteBranch= 请输入要删除的远程分支名称: " -if "%remoteBranch%"=="" ( - echo. - echo **错误**: 分支名称不能为空! - pause - goto mainMenu -) - -REM 检查远程分支是否存在 (简化,实际更复杂,需要fetch后再检查) -echo. -echo 正在删除远程分支 "%remoteBranch%"... -git push origin --delete "%remoteBranch%" -if %errorlevel% neq 0 ( - echo. - echo **错误**: 删除远程分支 "%remoteBranch%" 失败,错误代码: %errorlevel% - echo 请检查分支名称是否正确或者权限是否足够。 可以尝试git remote -v 和 git branch -vv - echo. - echo 常见错误: - echo. - echo (1) 没有删除权限:确认你是否是该仓库的贡献者,并拥有删除远程分支的权限 - echo (2) 远程分支不存在:确认远程分支名称是否正确 - echo (3) 网络问题:确认网络连接正常 - pause - goto mainMenu -) -echo. -echo 已成功删除远程分支 "%remoteBranch%"。 -pause -goto mainMenu - -:exit -cls -echo. -echo 感谢使用! -exit \ No newline at end of file diff --git a/s.py b/s.py new file mode 100644 index 0000000000..2262780332 --- /dev/null +++ b/s.py @@ -0,0 +1,388 @@ +import os +import subprocess + +def clear_screen(): + """清空屏幕""" + os.system('cls' if os.name == 'nt' else 'clear') + +def run_git_command(command_list): + """运行 Git 命令并返回状态码和输出""" + try: + process = subprocess.run(command_list, capture_output=True, text=True, check=True) + return process.returncode, process.stdout, process.stderr + except subprocess.CalledProcessError as e: + return e.returncode, e.stdout, e.stderr + +def main_menu(): + """显示主菜单并获取用户选择""" + clear_screen() + print("=====================================================") + print(" [ HowToCook 项目贡献助手 ]") + print("=====================================================") + print("\n 请选择操作:") + print("\n [1] 创建/切换分支") + print(" [2] 添加修改") + print(" [3] 提交修改") + print(" [4] 推送分支") + print(" [5] 同步 Fork (从 Upstream 更新)") + print(" [6] 设置 Upstream 仓库地址") + print(" [9] 添加远程仓库") + print(" [7] 删除本地分支") + print(" [8] 删除远程分支") + print("\n [0] 退出") + print("\n") + + while True: + choice = input(" 请选择 (0-9): ") + if choice in ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9'): + return choice + else: + print("\n **错误**: 无效的选择,请重新选择.") + input("按任意键继续...") + +def create_branch(): + """创建并切换到新分支""" + clear_screen() + print("=====================================================") + print(" [1] 创建并切换到新分支") + print("=====================================================") + print("\n") + + branch_name = input(" 请输入新分支名称: ") + if not branch_name: + print("\n **错误**: 分支名称不能为空!") + input("按任意键继续...") + return + + print("\n 正在创建并切换到分支", branch_name, "...") + return_code, stdout, stderr = run_git_command(["git", "checkout", "-b", branch_name]) + + if return_code != 0: + print("\n **错误**: 创建分支失败,错误代码:", return_code) + print(" 请检查分支名称是否合法或已存在。 可以尝试使用git branch查看已存在的分支") + print(stderr) + input("按任意键继续...") + return + + print("\n 已成功创建并切换到分支", branch_name) + input("按任意键继续...") + +def add_changes(): + """添加修改""" + clear_screen() + print("=====================================================") + print(" [2] 添加修改") + print("=====================================================") + print("\n [a] 添加所有文件") + print(" [文件名] 添加指定文件 (例如: README.md)") + print("\n") + + add_type = input(" 请输入选项 (a 或 文件名): ") + if add_type.lower() == 'a': + print("\n 正在添加所有文件...") + return_code, stdout, stderr = run_git_command(["git", "add", "."]) + if return_code != 0: + print("\n **错误**: 添加所有文件失败,错误代码:", return_code) + print(" 请检查是否在git仓库目录下运行 或者是否有文件未保存") + print(stderr) + input("按任意键继续...") + return + print("\n 已添加所有文件到暂存区。") + else: + print("\n 正在添加文件", add_type, "...") + return_code, stdout, stderr = run_git_command(["git", "add", add_type]) + if return_code != 0: + print("\n **错误**: 添加文件", add_type, "失败,错误代码:", return_code) + print(" 请检查文件名", add_type, " 是否正确, 可以使用git status查看") + print(stderr) + input("按任意键继续...") + return + print("\n 已添加文件", add_type, "到暂存区。") + + input("按任意键继续...") + +def commit_changes(): + """提交修改""" + clear_screen() + print("=====================================================") + print(" [3] 提交修改") + print("=====================================================") + print("\n") + + commit_message = input(" 请输入提交信息: ") + try: + with open("temp_commit_message.txt", "w", encoding="utf-8") as f: + f.write(commit_message) + except Exception as e: + print(f"\n**错误**: 创建临时文件失败: {e}") + input("按任意键继续...") + return + + print("\n 正在提交修改...") + return_code, stdout, stderr = run_git_command(["git", "commit", "--file", "temp_commit_message.txt"]) + + if return_code != 0: + print("\n **错误**: 提交失败,错误代码:", return_code) + print(" 请检查暂存区是否为空(git status)或者是否有未解决的冲突(git status)。") + print(stderr) + os.remove("temp_commit_message.txt") + input("按任意键继续...") + return + + print("\n 提交成功,提交信息:", commit_message) + os.remove("temp_commit_message.txt") + input("按任意键继续...") + +def push_branch(): + """推送分支到 GitHub""" + clear_screen() + print("=====================================================") + print(" [4] 推送分支到 GitHub") + print("=====================================================") + print("\n") + + print(" 如果您的 fork 仓库 (git@github.com:424635328/HowToCook.git 或 https://github.com/424635328/HowToCook) 已设置为 origin,请直接回车。") + print(" 如果未设置,请输入您的 fork 仓库的远程仓库名称,或输入 'add' 添加一个新的远程仓库。") + remote_name = input(" 请输入要推送到的远程仓库名称 (默认为 origin): ") + if not remote_name: + remote_name = "origin" + + if remote_name.lower() == "add": + add_remote() #调用新增远程仓库的函数 + remote_name = "origin" #添加完成后,将 remote_name 设置为 origin + + push_branch_name = input(" 请输入要推送的分支名称: ") + if not push_branch_name: + print("\n **错误**: 分支名称不能为空!") + input("按任意键继续...") + return + + print("\n 正在拉取远程分支", push_branch_name, "的最新更改...") + return_code, stdout, stderr = run_git_command(["git", "pull", remote_name, push_branch_name]) + if return_code != 0: + print("\n **错误**: 拉取远程更改失败,错误代码:", return_code) + print(" 请检查您的网络连接, 远程仓库地址是否正确,以及是否存在冲突。请手动解决冲突后重试。") + print(stderr) + input("按任意键继续...") + return + + print("\n 正在推送分支", push_branch_name, "到", remote_name, "...") + return_code, stdout, stderr = run_git_command(["git", "push", remote_name, push_branch_name]) + if return_code != 0: + print("\n **错误**: 推送分支", push_branch_name, "失败,错误代码:", return_code) + print(" 请检查分支名称是否正确, 远程仓库配置是否正确, 或者权限是否足够。 可以尝试git remote -v 和 git branch -vv") + print("\n 常见错误:") + print("\n (1) 没有推送权限:确认你是否是该仓库的贡献者,并拥有push权限") + print(" (2) 分支名称错误:确认分支名称书写正确, 大小写是否正确。") + print(" (3) 远程仓库不存在: 确认远程仓库地址配置正确,用 git remote -v 查看") + print(" (4) 您的本地分支落后于远程分支,请先拉取远程更改。") + print(stderr) + input("按任意键继续...") + return + + print("\n 分支", push_branch_name, "已成功推送到", remote_name, "。") + input("按任意键继续...") + +def pull_upstream(): + """同步 Fork (从 Upstream 更新)""" + clear_screen() + print("=====================================================") + print(" [5] 同步 Fork (从 Upstream 更新)") + print("=====================================================") + print("\n") + + print(" 正在切换到 master 分支...") + return_code, stdout, stderr = run_git_command(["git", "checkout", "master"]) + if return_code != 0: + print("\n **错误**: 切换到 master 分支失败,错误代码:", return_code) + print(" 请检查本地是否存在名为master的分支,或者是否有未提交的修改。") + print(stderr) + input("按任意键继续...") + return + + print("\n 正在从 upstream 拉取 master 分支...") + return_code, stdout, stderr = run_git_command(["git", "pull", "upstream", "master"]) + if return_code != 0: + print("\n **错误**: 从 upstream 拉取 master 分支失败,错误代码:", return_code) + print(" 请检查upstream是否配置正确, 可以尝试git remote -v查看") + print("\n 常见错误:") + print("\n (1) upstream 未配置:确认 upstream 已经正确设置,使用 git remote -v 查看") + print(" (2) 网络问题:确认网络连接正常") + print(stderr) + input("按任意键继续...") + return + + print("\n 正在推送到 origin/master...") + return_code, stdout, stderr = run_git_command(["git", "push", "origin", "master"]) + if return_code != 0: + print("\n **错误**: 推送 master 分支到 origin 失败,错误代码:", return_code) + print(" 请检查远程仓库配置是否正确, 远程分支是否存在。 可以尝试git remote -v 和 git branch -vv") + print("\n 常见错误:") + print("\n (1) 没有推送权限:确认你是否是该仓库的贡献者,并拥有push权限") + print(" (2) 远程仓库不存在: 确认远程仓库地址配置正确,用 git remote -v 查看") + print(stderr) + input("按任意键继续...") + return + + print("\n 已成功从 upstream 同步并推送到 origin/master。") + input("按任意键继续...") + +def setup_upstream(): + """设置 Upstream 仓库地址""" + clear_screen() + print("=====================================================") + print(" [6] 设置 Upstream 仓库地址") + print("=====================================================") + print("\n") + + print(" 正在设置 upstream...") + return_code, stdout, stderr = run_git_command(["git", "remote", "add", "upstream", "https://github.com/Anduin2017/HowToCook.git"]) + if return_code != 0: + print("\n **错误**: 设置 upstream 失败,错误代码:", return_code) + print(" 请检查是否已经设置过upstream或者upstream地址是否正确。 可以尝试git remote -v查看") + print("\n 常见错误:") + print("\n (1) upstream 已经存在: 确认是否重复设置,使用 git remote -v 查看") + print(" (2) 网络问题:确认网络连接正常") + print(stderr) + input("按任意键继续...") + return + + print("\n 已成功设置 upstream 为 https://github.com/Anduin2017/HowToCook.git。") + input("按任意键继续...") + +def add_remote(): + """添加远程仓库""" + clear_screen() + print("=====================================================") + print(" [9] 添加远程仓库") + print("=====================================================") + print("\n") + + new_remote_name = input(" 请输入新的远程仓库名称: ") + if not new_remote_name: + print("\n **错误**: 远程仓库名称不能为空!") + input("按任意键继续...") + return + + new_remote_url = input(" 请输入新的远程仓库地址 (例如: git@github.com:424635328/HowToCook.git 或 https://github.com/424635328/HowToCook): ") + if not new_remote_url: + print("\n **错误**: 远程仓库地址不能为空!") + input("按任意键继续...") + return + + print("\n 正在添加远程仓库", new_remote_name, ",地址为", new_remote_url, "...") + return_code, stdout, stderr = run_git_command(["git", "remote", "add", new_remote_name, new_remote_url]) + if return_code != 0: + print("\n **错误**: 添加远程仓库失败,错误代码:", return_code) + print(" 请检查远程仓库名称是否已存在, 或者远程仓库地址是否正确。 可以尝试git remote -v查看") + print(stderr) + input("按任意键继续...") + return + + print("\n 已成功添加远程仓库", new_remote_name, ",地址为", new_remote_url, "。") + input("按任意键继续...") + +def delete_local_branch(): + """删除本地分支""" + clear_screen() + print("=====================================================") + print(" [7] 删除本地分支") + print("=====================================================") + print("\n") + + local_branch = input(" 请输入要删除的本地分支名称: ") + if not local_branch: + print("\n **错误**: 分支名称不能为空!") + input("按任意键继续...") + return + + # 检查本地分支是否存在 + return_code, stdout, stderr = run_git_command(["git", "branch"]) + if return_code != 0: + print("\n **错误**: 获取本地分支列表失败!") + print(stderr) + input("按任意键继续...") + return + + branch_exists = False + for line in stdout.splitlines(): + if local_branch in line: + branch_exists = True + break + + if not branch_exists: + print("\n **错误**: 本地分支", local_branch, "不存在!") + input("按任意键继续...") + return + + print("\n 正在删除本地分支", local_branch, "...") + return_code, stdout, stderr = run_git_command(["git", "branch", "-d", local_branch]) + if return_code != 0: + print("\n **错误**: 删除本地分支", local_branch, "失败,错误代码:", return_code) + print(" 请确保分支存在且已合并。如果未合并,请使用 -D 强制删除。 可以尝试git branch查看本地分支") + print("\n 常见错误:") + print("\n (1) 分支未合并: 如果确定要删除,可以使用 git branch -D", local_branch, "(请谨慎使用)") + print(" (2) 权限问题: 确认对本地仓库有操作权限") + print(stderr) + input("按任意键继续...") + return + + print("\n 已成功删除本地分支", local_branch, "。") + input("按任意键继续...") + +def delete_remote_branch(): + """删除远程分支""" + clear_screen() + print("=====================================================") + print(" [8] 删除远程分支") + print("=====================================================") + print("\n") + + remote_branch = input(" 请输入要删除的远程分支名称: ") + if not remote_branch: + print("\n **错误**: 分支名称不能为空!") + input("按任意键继续...") + return + + print("\n 正在删除远程分支", remote_branch, "...") + return_code, stdout, stderr = run_git_command(["git", "push", "origin", "--delete", remote_branch]) + if return_code != 0: + print("\n **错误**: 删除远程分支", remote_branch, "失败,错误代码:", return_code) + print(" 请检查分支名称是否正确或者权限是否足够。 可以尝试git remote -v 和 git branch -vv") + print("\n 常见错误:") + print("\n (1) 没有删除权限:确认你是否是该仓库的贡献者,并拥有删除远程分支的权限") + print(" (2) 远程分支不存在:确认远程分支名称是否正确") + print(" (3) 网络问题:确认网络连接正常") + print(stderr) + input("按任意键继续...") + return + + print("\n 已成功删除远程分支", remote_branch, "。") + input("按任意键继续...") + +if __name__ == "__main__": + while True: + choice = main_menu() + + if choice == '0': + clear_screen() + print("\n 感谢使用!") + break + elif choice == '1': + create_branch() + elif choice == '2': + add_changes() + elif choice == '3': + commit_changes() + elif choice == '4': + push_branch() + elif choice == '5': + pull_upstream() + elif choice == '6': + setup_upstream() + elif choice == '9': + add_remote() + elif choice == '7': + delete_local_branch() + elif choice == '8': + delete_remote_branch() \ No newline at end of file From 7364a35229ec9d019261c0e47aab19477bff7779 Mon Sep 17 00:00:00 2001 From: Tony <424635328@qq.com> Date: Sat, 19 Apr 2025 22:19:26 +0800 Subject: [PATCH 5/7] . --- s.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/s.py b/s.py index 2262780332..be6a171cad 100644 --- a/s.py +++ b/s.py @@ -8,7 +8,7 @@ def clear_screen(): def run_git_command(command_list): """运行 Git 命令并返回状态码和输出""" try: - process = subprocess.run(command_list, capture_output=True, text=True, check=True) + process = subprocess.run(command_list, capture_output=True, text=True, check=True, encoding='utf-8') # 指定编码为 utf-8 return process.returncode, process.stdout, process.stderr except subprocess.CalledProcessError as e: return e.returncode, e.stdout, e.stderr From fe80b5969e994b2e4e95a11aa4f898ad51338e24 Mon Sep 17 00:00:00 2001 From: Tony <424635328@qq.com> Date: Sat, 19 Apr 2025 22:31:42 +0800 Subject: [PATCH 6/7] . --- s.py => htc_helper.py | 119 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 103 insertions(+), 16 deletions(-) rename s.py => htc_helper.py (80%) diff --git a/s.py b/htc_helper.py similarity index 80% rename from s.py rename to htc_helper.py index be6a171cad..91f52dfcf9 100644 --- a/s.py +++ b/htc_helper.py @@ -1,5 +1,7 @@ import os import subprocess +import webbrowser +import urllib.parse def clear_screen(): """清空屏幕""" @@ -29,19 +31,22 @@ def main_menu(): print(" [9] 添加远程仓库") print(" [7] 删除本地分支") print(" [8] 删除远程分支") + print(" [10] 创建 Pull Request") print("\n [0] 退出") print("\n") while True: - choice = input(" 请选择 (0-9): ") - if choice in ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9'): + choice = input(" 请选择 (0-10): ") + if choice in ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10'): return choice else: print("\n **错误**: 无效的选择,请重新选择.") input("按任意键继续...") def create_branch(): - """创建并切换到新分支""" + """创建并切换到新分支 + 命令: git checkout -b + """ clear_screen() print("=====================================================") print(" [1] 创建并切换到新分支") @@ -68,7 +73,10 @@ def create_branch(): input("按任意键继续...") def add_changes(): - """添加修改""" + """添加修改 + 命令: git add . (添加所有文件) + git add (添加指定文件) + """ clear_screen() print("=====================================================") print(" [2] 添加修改") @@ -102,7 +110,9 @@ def add_changes(): input("按任意键继续...") def commit_changes(): - """提交修改""" + """提交修改 + 命令: git commit --file temp_commit_message.txt + """ clear_screen() print("=====================================================") print(" [3] 提交修改") @@ -134,7 +144,9 @@ def commit_changes(): input("按任意键继续...") def push_branch(): - """推送分支到 GitHub""" + """推送分支到 GitHub + 命令: git push + """ clear_screen() print("=====================================================") print(" [4] 推送分支到 GitHub") @@ -151,11 +163,9 @@ def push_branch(): add_remote() #调用新增远程仓库的函数 remote_name = "origin" #添加完成后,将 remote_name 设置为 origin - push_branch_name = input(" 请输入要推送的分支名称: ") + push_branch_name = input(" 请输入要推送的分支名称 (默认为 master, 直接回车使用默认值): ") #修改了提示语 if not push_branch_name: - print("\n **错误**: 分支名称不能为空!") - input("按任意键继续...") - return + push_branch_name = "master" # 如果用户没有输入,则使用 "master" 作为默认值 print("\n 正在拉取远程分支", push_branch_name, "的最新更改...") return_code, stdout, stderr = run_git_command(["git", "pull", remote_name, push_branch_name]) @@ -184,7 +194,11 @@ def push_branch(): input("按任意键继续...") def pull_upstream(): - """同步 Fork (从 Upstream 更新)""" + """同步 Fork (从 Upstream 更新) + 命令: git checkout master + git pull upstream master + git push origin master + """ clear_screen() print("=====================================================") print(" [5] 同步 Fork (从 Upstream 更新)") @@ -228,7 +242,9 @@ def pull_upstream(): input("按任意键继续...") def setup_upstream(): - """设置 Upstream 仓库地址""" + """设置 Upstream 仓库地址 + 命令: git remote add upstream https://github.com/Anduin2017/HowToCook.git + """ clear_screen() print("=====================================================") print(" [6] 设置 Upstream 仓库地址") @@ -251,7 +267,9 @@ def setup_upstream(): input("按任意键继续...") def add_remote(): - """添加远程仓库""" + """添加远程仓库 + 命令: git remote add + """ clear_screen() print("=====================================================") print(" [9] 添加远程仓库") @@ -283,7 +301,9 @@ def add_remote(): input("按任意键继续...") def delete_local_branch(): - """删除本地分支""" + """删除本地分支 + 命令: git branch -d + """ clear_screen() print("=====================================================") print(" [7] 删除本地分支") @@ -331,7 +351,9 @@ def delete_local_branch(): input("按任意键继续...") def delete_remote_branch(): - """删除远程分支""" + """删除远程分支 + 命令: git push origin --delete + """ clear_screen() print("=====================================================") print(" [8] 删除远程分支") @@ -360,6 +382,69 @@ def delete_remote_branch(): print("\n 已成功删除远程分支", remote_branch, "。") input("按任意键继续...") +def create_pull_request(): + """创建 Pull Request (实际上是生成 URL 并提示用户手动创建) + 命令:无 (需要用户手动操作) + """ + clear_screen() + print("=====================================================") + print(" [10] 创建 Pull Request") + print("=====================================================") + print("\n") + + # 询问用户 Fork 的仓库名 + fork_username = input("请输入你的 GitHub 用户名(你的 Fork 仓库的所有者): ") + if not fork_username: + print("\n **错误**: GitHub 用户名不能为空!") + input("按任意键继续...") + return + + # 询问用户要创建 PR 的分支名称 + branch_name = input("请输入要创建 Pull Request 的分支名称: ") + if not branch_name: + print("\n **错误**: 分支名称不能为空!") + input("按任意键继续...") + return + + # 构建 URL + base_repo = "Anduin2017/HowToCook" + head_repo = f"{fork_username}:{branch_name}" # 构建 head repo 名称 + + # urlencode 分支名称和 PR 标题,以处理特殊字符 + encoded_head = urllib.parse.quote(head_repo) + encoded_title = urllib.parse.quote("简明扼要地描述你的修改") + + pr_body = """## 注意 + +Pull Request 提交后,预计 1 分钟内将会得到自动化格式检查的结果。只有通过自动化检查的 Pull Request 才会被人工审核。 + +- [ ] 请确保此 Pull Request 能够通过自动化代码检查。 + +## 签署 + +必须签署下面的对话框才能开始审核。 + +### HowToCook 仓库采用了 [The Unlicense](https://unlicense.org/) 协议 + +菜谱在签入前,必须确保其可以**直接声明进入 "公共领域"(public domain)**。这意味着一旦合并后,任何人都**可以**自由复制,修改,发布,使用,编译,出售或以菜谱的形式或菜的形式分发,**无论**是出于**商业目的**还是**非商目的**,以及**任何**手段。 + +- [ ] 我确保了我的内容不涉及版权内容,并且授权它 The Unlicense 协议。 +""" + + pr_url = f"https://github.com/{base_repo}/compare/master...{encoded_head}?title={encoded_title}&body={urllib.parse.quote(pr_body)}" + + print("\n 请复制以下 URL 到你的浏览器中,手动创建 Pull Request:") + print(pr_url) + + # 尝试使用 web browser 打开 URL, 如果失败,至少URL已经显示给用户了。 + try: + webbrowser.open(pr_url) + except webbrowser.Error: + print("无法自动打开浏览器,请手动复制 URL。") + + input("\n按任意键继续...") + + if __name__ == "__main__": while True: choice = main_menu() @@ -385,4 +470,6 @@ def delete_remote_branch(): elif choice == '7': delete_local_branch() elif choice == '8': - delete_remote_branch() \ No newline at end of file + delete_remote_branch() + elif choice == '10': + create_pull_request() # 新增 PR 功能 \ No newline at end of file From 6af11f123070ce795e87308f4d33fde1dd54b01b Mon Sep 17 00:00:00 2001 From: Tony <424635328@qq.com> Date: Sat, 19 Apr 2025 22:43:06 +0800 Subject: [PATCH 7/7] . --- htc_helper.py | 64 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 61 insertions(+), 3 deletions(-) diff --git a/htc_helper.py b/htc_helper.py index 91f52dfcf9..6d41bd6392 100644 --- a/htc_helper.py +++ b/htc_helper.py @@ -32,12 +32,13 @@ def main_menu(): print(" [7] 删除本地分支") print(" [8] 删除远程分支") print(" [10] 创建 Pull Request") + print(" [11] 清理 Commits (谨慎使用!)") print("\n [0] 退出") print("\n") while True: - choice = input(" 请选择 (0-10): ") - if choice in ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10'): + choice = input(" 请选择 (0-11): ") + if choice in ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11'): return choice else: print("\n **错误**: 无效的选择,请重新选择.") @@ -444,6 +445,61 @@ def create_pull_request(): input("\n按任意键继续...") +def clean_commits(): + """清理 Commits (使用 git reset --hard) + **危险操作**: 将会永久丢弃未推送的 commits! 使用前请务必备份! + 命令:git reset --hard HEAD~ + """ + clear_screen() + print("=====================================================") + print(" [11] 清理 Commits (极其危险!)") + print("=====================================================") + print("\n") + + print(" **警告:此操作会永久丢弃你本地分支上未推送的 commits! 使用前请务必备份你的代码!**") + print(" 此操作会将 HEAD 重置到指定 commit,并丢弃之后的 commits。") + print(" 如果你想保留最近的 n 个 commits,将会丢弃后面的 commits。") + + num_commits = input("\n 要保留最近多少个 commits? (输入数字并回车,输入0则清空所有commit): ") + if not num_commits: + print("\n **错误**: 必须输入要保留的 commits 数量! 操作已取消。") + input("\n按任意键继续...") + return + + try: + num_commits = int(num_commits) + if num_commits < 0: + print("\n **错误**: commit 数量必须是非负整数! 操作已取消。") + input("\n按任意键继续...") + return + except ValueError: + print("\n **错误**: 输入的不是有效的整数! 操作已取消。") + input("\n按任意键继续...") + return + + # 再次发出警告 + print("\n **再次警告: 你确定要丢弃最近的 {} 个 commit 之后的所有 commits 吗?此操作不可撤销!**".format(num_commits)) + confirmation = input(" 输入 'yes' 继续,输入其他任何内容取消操作: ") + if confirmation.lower() != "yes": + print("\n操作已取消。") + input("\n按任意键继续...") + return + + reset_command = f"git reset --hard HEAD~{num_commits}" + + print(f"\n 执行命令: {reset_command}") # 显示将要执行的命令 + + return_code, stdout, stderr = run_git_command(["git", "reset", "--hard", f"HEAD~{num_commits}"]) + + if return_code != 0: + print("\n **错误**: reset 失败,请检查您的操作。") + print(stderr) + else: + print("\n 成功重置到 HEAD~{}!".format(num_commits)) + print(" **注意: 你的本地更改可能已经被丢弃,请检查你的工作目录。**") + + input("\n按任意键继续...") + if __name__ == "__main__": while True: @@ -472,4 +528,6 @@ def create_pull_request(): elif choice == '8': delete_remote_branch() elif choice == '10': - create_pull_request() # 新增 PR 功能 \ No newline at end of file + create_pull_request() # 新增 PR 功能 + elif choice == '11': + clean_commits() \ No newline at end of file