From 64487cfe291be82657958535385fd80017c8b86d Mon Sep 17 00:00:00 2001 From: chenhaoran Date: Wed, 20 Nov 2024 15:19:31 +0800 Subject: [PATCH 1/2] fix:handling the issue of 'git remote prune origin' failing due to ref lock --- packaging/delete_ref_lock.py | 70 ++++++++++++++++++++++++------------ 1 file changed, 47 insertions(+), 23 deletions(-) diff --git a/packaging/delete_ref_lock.py b/packaging/delete_ref_lock.py index cf0e4cdd058a..42803e43fd25 100644 --- a/packaging/delete_ref_lock.py +++ b/packaging/delete_ref_lock.py @@ -6,54 +6,78 @@ def git_fetch(): result = subprocess.run(['git', 'fetch'], capture_output=True, text=True) return result -# 解析分支名称 +def git_prune(): + # git remote prune origin + print("git remote prune origin") + result = subprocess.run(['git', 'remote', 'prune', 'origin'], capture_output=True, text=True) + return result + def parse_branch_name_type1(error_output): + # error: cannot lock ref 'refs/remotes/origin/fix/3.0/TD-32817': is at 7af5 but expected eaba # 使用正则表达式匹配 'is at' 前的分支名称 match = re.search(r"error: cannot lock ref '(refs/remotes/origin/[^']+)': is at", error_output) if match: return match.group(1) return None -# 解析第二种错误中的分支名称 def parse_branch_name_type2(error_output): - # 使用正则表达式匹配 'exists' 前的第一个引号内的分支名称 + # 使用正则表达式匹配 'exists; cannot create' 前的第一个引号内的分支名称 match = re.search(r"'(refs/remotes/origin/[^']+)' exists;", error_output) if match: return match.group(1) return None +# parse branch name from error output of git remote prune origin +def parse_branch_name_type3(error_output): + # 使用正则表达式匹配 'Unable to' 前的第一个引号内的分支名称 + # git error: could not delete references: cannot lock ref 'refs/remotes/origin/test/3.0/TS-4893': Unable to create 'D:/workspace/main/TDinternal/community/.git/refs/remotes/origin/test/3.0/TS-4893.lock': File exists + match = re.search(r"references: cannot lock ref '(refs/remotes/origin/[^']+)': Unable to", error_output) + if match: + return match.group(1) + return None + + # 执行 git update-ref -d 命令 def git_update_ref(branch_name): if branch_name: subprocess.run(['git', 'update-ref', '-d', f'{branch_name}'], check=True) # 解析错误类型并执行相应的修复操作 +# parse error type and execute corresponding repair operation def handle_error(error_output): - # 错误类型1:本地引用的提交ID与远程不一致 - if "is at" in error_output and "but expected" in error_output: - branch_name = parse_branch_name_type1(error_output) - if branch_name: - print(f"Detected error type 1, attempting to delete ref for branch: {branch_name}") - git_update_ref(branch_name) - else: - print("Error parsing branch name for type 1.") - # 错误类型2:尝试创建新的远程引用时,本地已经存在同名的引用 - elif "exists; cannot create" in error_output: - branch_name = parse_branch_name_type2(error_output) - if branch_name: - print(f"Detected error type 2, attempting to delete ref for branch: {branch_name}") - git_update_ref(branch_name) - else: - print("Error parsing branch name for type 2.") + error_types = [ + ("is at", "but expected", parse_branch_name_type1, "type 1"), + ("exists; cannot create", None, parse_branch_name_type2, "type 2"), + ("Unable to create", "File exists", parse_branch_name_type3, "type 3") + ] + + for error_type in error_types: + if error_type[0] in error_output and (error_type[1] is None or error_type[1] in error_output): + branch_name = error_type[2](error_output) + if branch_name: + print(f"Detected error {error_type[3]}, attempting to delete ref for branch: {branch_name}") + git_update_ref(branch_name) + else: + print(f"Error parsing branch name for {error_type[3]}.") + break # 主函数 def main(): - fetch_result = git_fetch() - if fetch_result.returncode != 0: # 如果 git fetch 命令失败 - error_output = fetch_result.stderr + # fetch_result = git_fetch() + # if fetch_result.returncode != 0: # 如果 git fetch 命令失败 + # error_output = fetch_result.stderr + # handle_error(error_output) + # else: + # print("Git fetch successful.") + + prune_result = git_prune() + print(prune_result.returncode) + if prune_result.returncode != 0: # 如果 git fetch 命令失败 + error_output = prune_result.stderr + print(error_output) handle_error(error_output) else: - print("Git fetch successful.") + print("Git prune successful.") if __name__ == "__main__": main() \ No newline at end of file From 0cfe979082862f21fc936303dfe290f859fc16e3 Mon Sep 17 00:00:00 2001 From: chenhaoran Date: Wed, 20 Nov 2024 15:23:41 +0800 Subject: [PATCH 2/2] fix:handling the issue of 'git remote prune origin' failing due to ref lock --- packaging/delete_ref_lock.py | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/packaging/delete_ref_lock.py b/packaging/delete_ref_lock.py index 42803e43fd25..7200246a8a92 100644 --- a/packaging/delete_ref_lock.py +++ b/packaging/delete_ref_lock.py @@ -1,7 +1,6 @@ import subprocess import re -# 执行 git fetch 命令并捕获输出 def git_fetch(): result = subprocess.run(['git', 'fetch'], capture_output=True, text=True) return result @@ -14,14 +13,14 @@ def git_prune(): def parse_branch_name_type1(error_output): # error: cannot lock ref 'refs/remotes/origin/fix/3.0/TD-32817': is at 7af5 but expected eaba - # 使用正则表达式匹配 'is at' 前的分支名称 + # match the branch name before ‘is at’ with a regular expression match = re.search(r"error: cannot lock ref '(refs/remotes/origin/[^']+)': is at", error_output) if match: return match.group(1) return None def parse_branch_name_type2(error_output): - # 使用正则表达式匹配 'exists; cannot create' 前的第一个引号内的分支名称 + # match the branch name before ‘exists; cannot create’ with a regular expression match = re.search(r"'(refs/remotes/origin/[^']+)' exists;", error_output) if match: return match.group(1) @@ -29,7 +28,7 @@ def parse_branch_name_type2(error_output): # parse branch name from error output of git remote prune origin def parse_branch_name_type3(error_output): - # 使用正则表达式匹配 'Unable to' 前的第一个引号内的分支名称 + # match the branch name before the first single quote before 'Unable to' with a regular expression # git error: could not delete references: cannot lock ref 'refs/remotes/origin/test/3.0/TS-4893': Unable to create 'D:/workspace/main/TDinternal/community/.git/refs/remotes/origin/test/3.0/TS-4893.lock': File exists match = re.search(r"references: cannot lock ref '(refs/remotes/origin/[^']+)': Unable to", error_output) if match: @@ -37,12 +36,11 @@ def parse_branch_name_type3(error_output): return None -# 执行 git update-ref -d 命令 +# execute git update-ref -d to delete the ref def git_update_ref(branch_name): if branch_name: subprocess.run(['git', 'update-ref', '-d', f'{branch_name}'], check=True) -# 解析错误类型并执行相应的修复操作 # parse error type and execute corresponding repair operation def handle_error(error_output): error_types = [ @@ -61,18 +59,17 @@ def handle_error(error_output): print(f"Error parsing branch name for {error_type[3]}.") break -# 主函数 def main(): - # fetch_result = git_fetch() - # if fetch_result.returncode != 0: # 如果 git fetch 命令失败 - # error_output = fetch_result.stderr - # handle_error(error_output) - # else: - # print("Git fetch successful.") + fetch_result = git_fetch() + if fetch_result.returncode != 0: + error_output = fetch_result.stderr + handle_error(error_output) + else: + print("Git fetch successful.") prune_result = git_prune() print(prune_result.returncode) - if prune_result.returncode != 0: # 如果 git fetch 命令失败 + if prune_result.returncode != 0: error_output = prune_result.stderr print(error_output) handle_error(error_output)