Skip to content
This repository was archived by the owner on Sep 22, 2025. It is now read-only.

Commit 6216680

Browse files
fix: 修复异常退出仍返回0的问题 (...) (#34)
* fix: 修复异常退出仍返回0的问题 (...) remove: 移除了测试时遗留的部分代码 fix: 修复了在部分时候不会自动检查更新/获取公告的问题 * fix: 修复提交命令在没有修改时还会要求提交信息的问题
1 parent 1e9ecdf commit 6216680

File tree

3 files changed

+115
-58
lines changed

3 files changed

+115
-58
lines changed

中文git-pack.py

Lines changed: 58 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88
init(autoreset=True)
99
script_path = os.path.dirname(sys.argv[0])
1010
full_path = os.path.join(script_path, "中文git.exe")
11+
exit_code = 0 # 只有不正常退出需要定义
1112

1213
# ---------- 版本定义及更新 ----------
1314
# 定义版本号
1415
VERSION = 'v2.9-pack'
1516

1617
# --- 读取配置文件 ---
1718
def fetch_json():
19+
global exit_code
1820
config_url = "https://duckduckstudio.github.io/yazicbs.github.io/Tools/chinese_git/files/json/config.json"
1921
headers = {
2022
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
@@ -27,9 +29,11 @@ def fetch_json():
2729
return json_data
2830
else:
2931
print(f"{Fore.RED}{Fore.RESET} 无法获取最新默认配置文件\n{Fore.BLUE}[!]{Fore.RESET} 返回状态码: {Fore.YELLOW}{response.status_code}{Fore.RESET}")
32+
exit_code = 1
3033
return None
3134
except Exception as e:
3235
print(f"{Fore.RED}{Fore.RESET} 尝试获取最新默认配置文件失败,错误: {Fore.RED}{e}{Fore.RESET}")
36+
exit_code = 1
3337
return None
3438

3539
def merge_json(old_json, new_json):
@@ -57,6 +61,7 @@ def merge_json(old_json, new_json):
5761
return updated_json
5862

5963
def update_json():
64+
global exit_code
6065
new_json = fetch_json()
6166
if not new_json:
6267
return 1
@@ -74,6 +79,7 @@ def update_json():
7479
return 0
7580
except Exception as e:
7681
print(f"{Fore.RED}{Fore.RESET} 更新配置文件时出错:\n{Fore.RED}{e}{Fore.RESET}")
82+
exit_code = 1
7783
return 1
7884

7985
config_file = os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])), "config.json")
@@ -87,6 +93,7 @@ def update_json():
8793
auto_check_update = True
8894
auto_get_notice = True
8995
print(f"{Fore.RED}{Fore.RESET} 读取配置文件时出错:\n{Fore.RED}{e}{Fore.RESET}\n{Fore.BLUE}[!]{Fore.RESET} 请检查配置文件是否正确,您可以先删除配置文件然后运行任意中文git的命令来重新生成默认配置文件。")
96+
exit_code = 1
9097
else:
9198
# 没有配置文件就默认都要
9299
auto_check_update = True
@@ -120,6 +127,7 @@ def update_json():
120127
print(f"{Fore.GREEN}{Fore.RESET} 默认配置文件生成成功")
121128
except Exception as e:
122129
print(f"{Fore.RED}{Fore.RESET} 默认配置文件生成失败!请{Fore.YELLOW}手动添加{Fore.RESET}配置文件,否则将无法运行一些功能!")
130+
exit_code = 1
123131
print(f"{Fore.BLUE}[!]{Fore.RESET} 如果你觉得这不应该可以提交Issue")
124132
# -------------------
125133

@@ -138,6 +146,7 @@ def always_check():# 每次执行命令都要检查的
138146
pass
139147

140148
def check_for_updates():
149+
global exit_code
141150
# 提取版本号
142151
current_version = VERSION.split('-')[0] # 分离可能的 '-pack' 后缀
143152

@@ -157,10 +166,12 @@ def check_for_updates():
157166
return None
158167
except Exception as e:
159168
print(f"{Fore.RED}{Fore.RESET} 检查更新时出错: {e}")
169+
exit_code = 1
160170
return None
161171

162172
# 自动检查更新并提示用户安装
163173
def auto_update():
174+
global exit_code
164175
new_version = check_for_updates()
165176

166177
if new_version:
@@ -184,13 +195,16 @@ def auto_update():
184195
sys.exit()
185196
except FileNotFoundError:
186197
print(f"{Fore.RED}{Fore.RESET} 未找到更新程序,请前往发行版页手动下载补全!")
198+
exit_code = 1
187199
except Exception as e:
188200
print(f"{Fore.RED}{Fore.RESET} 启动更新程序时出错: {Fore.RED}{e}{Fore.RESET}")
201+
exit_code = 1
189202
else:
190203
print(f"{Fore.BLUE}[!]{Fore.RESET} 已跳过更新。")
191204

192205
# ------- 更新更新程序 -------
193206
def download_update_file(version):
207+
global exit_code
194208
# 根据版本确定下载 URL
195209
download_url = f'https://github.com/DuckDuckStudio/Chinese_git/releases/download/{version}/Pack_Version_Update.exe'
196210
#spare_download_url = f'https://duckduckstudio.github.io/yazicbs.github.io/Tools/chinese_git/Spare-Download/Pack_Version_Update.exe'
@@ -224,7 +238,9 @@ def download_update_file(version):
224238
# return new_filename
225239
# except Exception as e:
226240
# print(f"{Fore.RED}✕{Fore.RESET} 下载更新文件时出错: {e}")
241+
# exit_code = 1
227242
# return None
243+
exit_code = 1
228244
return None
229245

230246
def replace_current_program(new_filename):
@@ -234,13 +250,15 @@ def replace_current_program(new_filename):
234250
print(f"{Fore.GREEN}{Fore.RESET} 更新程序已成功更新。")
235251
except Exception as e:
236252
print(f"{Fore.RED}{Fore.RESET} 替换当前更新程序时出错: {e}")
253+
exit_code = 1
237254

238255
# ---------- 版本...更新 结束 ----------
239256
# ---------- 公告获取 -----------------
240257
notice_url = 'https://duckduckstudio.github.io/yazicbs.github.io/Tools/chinese_git/notice/notice.txt'
241258
previous_notice_file = os.path.join(script_path, 'previous_notice.txt')# 显示过的公告
242259

243260
def get_notice_content(url, manual=False):
261+
global exit_code
244262
try:
245263
response = requests.get(url)
246264
if response.status_code == 200:
@@ -252,13 +270,17 @@ def get_notice_content(url, manual=False):
252270
t = input(f"{Fore.BLUE}?{Fore.RESET} 是否读取本地最新公告({Fore.GREEN}{Fore.RESET}/{Fore.RED}{Fore.RESET}):").lower()
253271
if t in ['是', 'y', 'yes']:
254272
display_notice('本地')
273+
else:
274+
exit_code = 1
255275
return None
256276
except Exception as e:
257277
if manual:
258278
print(f"{Fore.RED}{Fore.RESET} 获取最新公告失败!\n错误信息: {Fore.RED}{e}{Fore.RESET}")
259279
t = input(f"{Fore.BLUE}?{Fore.RESET} 是否读取本地最新公告({Fore.GREEN}{Fore.RESET}/{Fore.RED}{Fore.RESET}):").lower()
260280
if t in ['是', 'y', 'yes']:
261281
display_notice('本地')
282+
else:
283+
exit_code = 1
262284
return None
263285

264286
def save_previous_notice(content):
@@ -272,9 +294,10 @@ def read_previous_notice():
272294
except FileNotFoundError:
273295
return None
274296
except Exception:
275-
return None # 以防出现像 microsoft/winget-pkgs #156224 中的错误
297+
return None
276298

277299
def display_notice(manual=False):
300+
global exit_code
278301
if manual == True:
279302
content = get_notice_content(notice_url, True)
280303
elif manual == False:
@@ -284,6 +307,7 @@ def display_notice(manual=False):
284307
content = read_previous_notice()
285308
if not content:
286309
print(f"{Fore.RED}{Fore.RESET} 没有本地公告")
310+
exit_code = 1
287311
return
288312
else:
289313
previous_notice = read_previous_notice()
@@ -330,8 +354,6 @@ def check_git_stash():
330354
git_stash = subprocess.run(["git", "stash", "show"], capture_output=True, text=True)
331355
output_lines = git_stash.stdout.split('\n')
332356

333-
print(output_lines)
334-
335357
if output_lines != ['']:
336358
staged_changes = True
337359

@@ -340,8 +362,6 @@ def check_git_stash():
340362
git_stash = subprocess.run(["git", "diff", "--name-only"], capture_output=True, text=True)
341363
output_lines = git_stash.stdout.split('\n')
342364

343-
print(output_lines)
344-
345365
if output_lines != ['']:
346366
unstaged_changes = True
347367

@@ -398,8 +418,6 @@ def git_command(command, *args):
398418
try:
399419
if command == "提交":
400420
staged, unstaged = check_git_stash()
401-
print("暂存的更改:", staged)
402-
print("未暂存的更改:", unstaged)
403421
if staged:
404422
print(f"{Fore.BLUE}[!]{Fore.BLUE} 将提交暂存区的内容")
405423
elif unstaged:
@@ -409,17 +427,17 @@ def git_command(command, *args):
409427
print(f"{Fore.GREEN}{Fore.RESET} 已暂存所有更改")
410428
else:
411429
print(f"{Fore.RED}{Fore.RESET} 没有已暂存的更改")
412-
return
430+
exit_code = 1
413431
else:
414432
print(f"{Fore.RED}{Fore.RESET} 没有更改")
415-
return
433+
exit_code = 1
416434

417-
if not args:
435+
if not args and exit_code != 1:
418436
commit_message = input("请输入提交信息: ")
419437
if not commit_message:
420438
# 还不输提交信息?玩我呢
421439
print(f"{Fore.RED}{Fore.RESET} 请提供提交信息")
422-
return
440+
exit_code = 1
423441
result = subprocess.run('git ' + git_command + ' -m "' + commit_message + '"', capture_output=True, text=True)
424442
else:
425443
result = subprocess.run('git ' + git_command + ' ' + ' '.join(args), capture_output=True, text=True)
@@ -428,6 +446,7 @@ def git_command(command, *args):
428446
result = subprocess.run('git ' + git_command + ' --all', capture_output=True, text=True)
429447
elif not args:
430448
print(f"{Fore.RED}{Fore.RESET} 你要暂存什么你没告诉我啊")
449+
exit_code = 1
431450
else:
432451
result = subprocess.run('git ' + git_command + ' ' + ' '.join(args), capture_output=True, text=True)
433452
elif command == "切换分支" or command == "签出到":
@@ -438,6 +457,7 @@ def git_command(command, *args):
438457
result = subprocess.run('git ' + git_command + ' ' + ' '.join(args), capture_output=True, text=True)
439458
else:
440459
print(f"{Fore.RED}{Fore.RESET} 多余的参数")
460+
exit_code = 1
441461
elif command == "新建分支":
442462
if not args:
443463
new_branch = input("请输入新分支名称: ")
@@ -446,19 +466,20 @@ def git_command(command, *args):
446466
result = subprocess.run('git ' + git_command + ' ' + ' '.join(args), capture_output=True, text=True)
447467
else:
448468
print(f"{Fore.RED}{Fore.RESET} 多余的参数")
469+
exit_code = 1
449470
elif command == "删除分支":
450471
if not args:
451472
print(f"{Fore.RED}{Fore.RESET} 删除分支命令需要指定要删除的分支名称")
452-
return
473+
exit_code = 1
453474
elif len(args) > 2:
454475
print(f"{Fore.RED}{Fore.RESET} 多余的参数")
455-
return
476+
exit_code = 1
456477
elif len(args) == 2:
457478
if args[1] == "+确认":
458479
git_command = "branch -d"
459480
else:
460481
print(f"{Fore.RED}{Fore.RESET} 无效的附加参数")
461-
return
482+
exit_code = 1
462483
else:
463484
result = subprocess.run('git ' + git_command + ' ' + ' '.join(args), capture_output=True, text=True)
464485
elif command == "版本":
@@ -469,6 +490,7 @@ def git_command(command, *args):
469490
elif command == "还原":
470491
if not args:
471492
print(f"{Fore.RED}{Fore.RESET} 还原命令需要参数")
493+
exit_code = 1
472494
else:
473495
if args[0] == "最新提交":
474496
result = subprocess.run('git ' + git_command + ' HEAD', capture_output=True, text=True)
@@ -482,7 +504,7 @@ def git_command(command, *args):
482504
result = subprocess.run(['git ', git_command, f'HEAD~{num}'], capture_output=True, text=True)
483505
except ValueError:
484506
print(f"{Fore.RED}{Fore.RESET} 参数错误,请输入倒数第n个提交,n为正整数。")
485-
return
507+
exit_code = 1
486508
else:
487509
result = subprocess.run('git ' + git_command + ' ' + args[0], capture_output=True, text=True)
488510
elif command == "克隆":
@@ -496,22 +518,22 @@ def git_command(command, *args):
496518
file = input("请输入需要检查的文件/文件夹:")
497519
if not file:
498520
print(f"{Fore.RED}{Fore.RESET} 文件/文件夹名不能为空")
499-
return
521+
exit_code = 1
500522
result = subprocess.run('git ' + git_command + ' ' + file, capture_output=True, text=True)
501523
print (result)
502524
else:
503525
result = subprocess.run('git ' + git_command + ' ' + ' '.join(args), capture_output=True, text=True)
504526
elif command == "查看本地分支":
505527
if len(args) > 2:
506528
print(f"{Fore.RED}{Fore.RESET} 多余的参数")
507-
return
529+
exit_code = 1
508530
elif args[0] == "+最后提交":
509531
git_command = "branch -v"
510532
elif (args[0] == "+最后提交" and args[1] == "+与上游分支关系") or (args[0] == "+与上游分支关系" and args[1] == "+最后提交"):
511533
git_command = "branch -vv"
512534
else:
513535
print(f"{Fore.RED}{Fore.RESET} 无效的参数")
514-
return
536+
exit_code = 1
515537
result = subprocess.run('git ' + git_command + ' ' + ' '.join(args), capture_output=True, text=True)
516538
elif command == "合并":
517539
if not args:
@@ -525,11 +547,11 @@ def git_command(command, *args):
525547
new_branch = input("请输入新分支名:")
526548
if old_branch == new_branch:
527549
print(f"{Fore.RED}{Fore.RESET} 新旧分支名称相同")
528-
return
550+
exit_code = 1
529551
result = subprocess.run('git ' + git_command + ' ' + old_branch + ' ' + new_branch, capture_output=True, text=True)
530552
if args < 2:
531553
print(f"{Fore.RED}{Fore.RESET} 缺少参数")
532-
return
554+
exit_code = 1
533555
else:
534556
result = subprocess.run('git ' + git_command + ' ' + ' '.join(args), capture_output=True, text=True)
535557
elif command == "更新":
@@ -543,18 +565,18 @@ def git_command(command, *args):
543565
elif command == "重置":
544566
if not args:
545567
print(f"{Fore.RED}{Fore.RESET} 重置指令需要具体的参数。")
546-
return
568+
exit_code = 1
547569
elif len(args) > 2:
548570
print(f"{Fore.RED}{Fore.RESET} 多余的参数")
549-
return
571+
exit_code = 1
550572
elif len(args) == 2:
551573
if args[1] == "+保留更改":# 默认
552574
git_command = "reset --mixed"
553575
elif args[1] == "+删除更改":
554576
git_command = "reset --hard"
555577
else:
556578
print(f"{Fore.RED}{Fore.RESET} 无效的附加参数")
557-
return
579+
exit_code = 1
558580

559581
if args[0] in ["最新提交", "HEAD"]:
560582
print(f"{Fore.YELLOW}{Fore.RESET} 虽然您这样做不会出错,但这样做有意义吗(思考)")
@@ -569,33 +591,35 @@ def git_command(command, *args):
569591
result = subprocess.run(['git ', git_command, f'HEAD~{num}'], capture_output=True, text=True)
570592
except ValueError:
571593
print(f"{Fore.RED}{Fore.RESET} 参数错误,请输入倒数第n个提交,n为正整数。")
572-
return
594+
exit_code = 1
573595
else:
574596
result = subprocess.run('git ' + git_command + ' ' + ' '.join(args), capture_output=True, text=True)
575597
else:
576598
result = subprocess.run('git ' + git_command + ' ' + ' '.join(args), capture_output=True, text=True)
577599

578-
if result.returncode == 0:
579-
print(result.stdout)
580-
else:
600+
if result.returncode == 0 and exit_code == 0:# 习惯性用 && 了...
601+
print(result)
602+
elif exit_code != 1:# 已设置错误代码的都已输出错误信息
581603
print(f"{Fore.RED}{Fore.RESET} 错误: {result.stderr}")
604+
exit_code = 1
582605

583606
if auto_check_update == "True":
584607
always_check()# 自动检查更新
585608
if auto_get_notice == "True":
586609
display_notice() # 自动公告获取
587610
except Exception as e:
588611
print(f"{Fore.RED}{Fore.RESET} 执行git命令时出错: {e}")
612+
exit_code = 1
589613
if auto_check_update == "True":
590614
always_check()# 自动检查更新
591615
if auto_get_notice == "True":
592-
display_notice() # 自动公告获取
616+
display_notice()# 自动公告获取
593617
else:
594618
print("不支持的命令:", command)
595619
if auto_check_update == "True":
596620
always_check()# 自动检查更新
597621
if auto_get_notice == "True":
598-
display_notice() # 自动公告获取
622+
display_notice()# 自动公告获取
599623

600624
if __name__ == "__main__":
601625
if len(sys.argv) > 1:
@@ -607,4 +631,8 @@ def git_command(command, *args):
607631
if auto_check_update == "True":
608632
always_check()# 自动检查更新
609633
if auto_get_notice == "True":
610-
display_notice() # 自动公告获取
634+
display_notice()# 自动公告获取
635+
exit_code = 1
636+
637+
exit_code = 1# 如果没有 __name__ == "__main__"
638+
sys.exit(exit_code)

中文git.exe

132 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)