11import os
22import subprocess
3+ import webbrowser
4+ import urllib .parse
35
46def clear_screen ():
57 """清空屏幕"""
@@ -29,19 +31,22 @@ def main_menu():
2931 print (" [9] 添加远程仓库" )
3032 print (" [7] 删除本地分支" )
3133 print (" [8] 删除远程分支" )
34+ print (" [10] 创建 Pull Request" )
3235 print ("\n [0] 退出" )
3336 print ("\n " )
3437
3538 while True :
36- choice = input (" 请选择 (0-9 ): " )
37- if choice in ('0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' ):
39+ choice = input (" 请选择 (0-10 ): " )
40+ if choice in ('0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , '10' ):
3841 return choice
3942 else :
4043 print ("\n **错误**: 无效的选择,请重新选择." )
4144 input ("按任意键继续..." )
4245
4346def create_branch ():
44- """创建并切换到新分支"""
47+ """创建并切换到新分支
48+ 命令: git checkout -b <branch_name>
49+ """
4550 clear_screen ()
4651 print ("=====================================================" )
4752 print (" [1] 创建并切换到新分支" )
@@ -68,7 +73,10 @@ def create_branch():
6873 input ("按任意键继续..." )
6974
7075def add_changes ():
71- """添加修改"""
76+ """添加修改
77+ 命令: git add . (添加所有文件)
78+ git add <file_name> (添加指定文件)
79+ """
7280 clear_screen ()
7381 print ("=====================================================" )
7482 print (" [2] 添加修改" )
@@ -102,7 +110,9 @@ def add_changes():
102110 input ("按任意键继续..." )
103111
104112def commit_changes ():
105- """提交修改"""
113+ """提交修改
114+ 命令: git commit --file temp_commit_message.txt
115+ """
106116 clear_screen ()
107117 print ("=====================================================" )
108118 print (" [3] 提交修改" )
@@ -134,7 +144,9 @@ def commit_changes():
134144 input ("按任意键继续..." )
135145
136146def push_branch ():
137- """推送分支到 GitHub"""
147+ """推送分支到 GitHub
148+ 命令: git push <remote_name> <branch_name>
149+ """
138150 clear_screen ()
139151 print ("=====================================================" )
140152 print (" [4] 推送分支到 GitHub" )
@@ -151,11 +163,9 @@ def push_branch():
151163 add_remote () #调用新增远程仓库的函数
152164 remote_name = "origin" #添加完成后,将 remote_name 设置为 origin
153165
154- push_branch_name = input (" 请输入要推送的分支名称: " )
166+ push_branch_name = input (" 请输入要推送的分支名称 (默认为 master, 直接回车使用默认值) : " ) #修改了提示语
155167 if not push_branch_name :
156- print ("\n **错误**: 分支名称不能为空!" )
157- input ("按任意键继续..." )
158- return
168+ push_branch_name = "master" # 如果用户没有输入,则使用 "master" 作为默认值
159169
160170 print ("\n 正在拉取远程分支" , push_branch_name , "的最新更改..." )
161171 return_code , stdout , stderr = run_git_command (["git" , "pull" , remote_name , push_branch_name ])
@@ -184,7 +194,11 @@ def push_branch():
184194 input ("按任意键继续..." )
185195
186196def pull_upstream ():
187- """同步 Fork (从 Upstream 更新)"""
197+ """同步 Fork (从 Upstream 更新)
198+ 命令: git checkout master
199+ git pull upstream master
200+ git push origin master
201+ """
188202 clear_screen ()
189203 print ("=====================================================" )
190204 print (" [5] 同步 Fork (从 Upstream 更新)" )
@@ -228,7 +242,9 @@ def pull_upstream():
228242 input ("按任意键继续..." )
229243
230244def setup_upstream ():
231- """设置 Upstream 仓库地址"""
245+ """设置 Upstream 仓库地址
246+ 命令: git remote add upstream https://github.com/Anduin2017/HowToCook.git
247+ """
232248 clear_screen ()
233249 print ("=====================================================" )
234250 print (" [6] 设置 Upstream 仓库地址" )
@@ -251,7 +267,9 @@ def setup_upstream():
251267 input ("按任意键继续..." )
252268
253269def add_remote ():
254- """添加远程仓库"""
270+ """添加远程仓库
271+ 命令: git remote add <remote_name> <remote_url>
272+ """
255273 clear_screen ()
256274 print ("=====================================================" )
257275 print (" [9] 添加远程仓库" )
@@ -283,7 +301,9 @@ def add_remote():
283301 input ("按任意键继续..." )
284302
285303def delete_local_branch ():
286- """删除本地分支"""
304+ """删除本地分支
305+ 命令: git branch -d <local_branch_name>
306+ """
287307 clear_screen ()
288308 print ("=====================================================" )
289309 print (" [7] 删除本地分支" )
@@ -331,7 +351,9 @@ def delete_local_branch():
331351 input ("按任意键继续..." )
332352
333353def delete_remote_branch ():
334- """删除远程分支"""
354+ """删除远程分支
355+ 命令: git push origin --delete <remote_branch_name>
356+ """
335357 clear_screen ()
336358 print ("=====================================================" )
337359 print (" [8] 删除远程分支" )
@@ -360,6 +382,69 @@ def delete_remote_branch():
360382 print ("\n 已成功删除远程分支" , remote_branch , "。" )
361383 input ("按任意键继续..." )
362384
385+ def create_pull_request ():
386+ """创建 Pull Request (实际上是生成 URL 并提示用户手动创建)
387+ 命令:无 (需要用户手动操作)
388+ """
389+ clear_screen ()
390+ print ("=====================================================" )
391+ print (" [10] 创建 Pull Request" )
392+ print ("=====================================================" )
393+ print ("\n " )
394+
395+ # 询问用户 Fork 的仓库名
396+ fork_username = input ("请输入你的 GitHub 用户名(你的 Fork 仓库的所有者): " )
397+ if not fork_username :
398+ print ("\n **错误**: GitHub 用户名不能为空!" )
399+ input ("按任意键继续..." )
400+ return
401+
402+ # 询问用户要创建 PR 的分支名称
403+ branch_name = input ("请输入要创建 Pull Request 的分支名称: " )
404+ if not branch_name :
405+ print ("\n **错误**: 分支名称不能为空!" )
406+ input ("按任意键继续..." )
407+ return
408+
409+ # 构建 URL
410+ base_repo = "Anduin2017/HowToCook"
411+ head_repo = f"{ fork_username } :{ branch_name } " # 构建 head repo 名称
412+
413+ # urlencode 分支名称和 PR 标题,以处理特殊字符
414+ encoded_head = urllib .parse .quote (head_repo )
415+ encoded_title = urllib .parse .quote ("简明扼要地描述你的修改" )
416+
417+ pr_body = """## 注意
418+
419+ Pull Request 提交后,预计 1 分钟内将会得到自动化格式检查的结果。只有通过自动化检查的 Pull Request 才会被人工审核。
420+
421+ - [ ] 请确保此 Pull Request 能够通过自动化代码检查。
422+
423+ ## 签署
424+
425+ 必须签署下面的对话框才能开始审核。
426+
427+ ### HowToCook 仓库采用了 [The Unlicense](https://unlicense.org/) 协议
428+
429+ 菜谱在签入前,必须确保其可以**直接声明进入 "公共领域"(public domain)**。这意味着一旦合并后,任何人都**可以**自由复制,修改,发布,使用,编译,出售或以菜谱的形式或菜的形式分发,**无论**是出于**商业目的**还是**非商目的**,以及**任何**手段。
430+
431+ - [ ] 我确保了我的内容不涉及版权内容,并且授权它 The Unlicense 协议。
432+ """
433+
434+ pr_url = f"https://github.com/{ base_repo } /compare/master...{ encoded_head } ?title={ encoded_title } &body={ urllib .parse .quote (pr_body )} "
435+
436+ print ("\n 请复制以下 URL 到你的浏览器中,手动创建 Pull Request:" )
437+ print (pr_url )
438+
439+ # 尝试使用 web browser 打开 URL, 如果失败,至少URL已经显示给用户了。
440+ try :
441+ webbrowser .open (pr_url )
442+ except webbrowser .Error :
443+ print ("无法自动打开浏览器,请手动复制 URL。" )
444+
445+ input ("\n 按任意键继续..." )
446+
447+
363448if __name__ == "__main__" :
364449 while True :
365450 choice = main_menu ()
@@ -385,4 +470,6 @@ def delete_remote_branch():
385470 elif choice == '7' :
386471 delete_local_branch ()
387472 elif choice == '8' :
388- delete_remote_branch ()
473+ delete_remote_branch ()
474+ elif choice == '10' :
475+ create_pull_request () # 新增 PR 功能
0 commit comments