55import shutil
66from urllib .parse import quote_plus
77
8+ # 平台配置映射
9+ PLATFORM_CONFIGS = {
10+ "gitee" : {
11+ "domain" : "gitee.com" ,
12+ "auth_method" : "token" # 或 "username_password"
13+ },
14+ "gitcode" : {
15+ "domain" : "gitcode.com" ,
16+ "auth_method" : "token" # GitCode必须使用Token认证[6](@ref)
17+ }
18+ }
19+
20+ def clone_repo_skip_lfs (platform , owner , repo , username = None , token = None , target_dir = "temp_repo" ):
21+ """克隆仓库并强制跳过LFS文件下载,支持多平台"""
22+ if platform not in PLATFORM_CONFIGS :
23+ raise ValueError (f"不支持的平台: { platform } ,支持的平台: { list (PLATFORM_CONFIGS .keys ())} " )
24+
25+ config = PLATFORM_CONFIGS [platform ]
26+ domain = config ["domain" ]
827
9- def clone_repo_skip_lfs (gitee_owner , gitee_repo , username = None , token = None , target_dir = "temp_repo" ):
10- """克隆仓库并强制跳过LFS文件下载"""
28+ # 构建认证URL
1129 if username and token :
1230 encoded_username = quote_plus (username )
1331 encoded_token = quote_plus (token )
14- repo_url = f"https://{ encoded_username } :{ encoded_token } @gitee.com/{ gitee_owner } /{ gitee_repo } .git"
32+ repo_url = f"https://{ encoded_username } :{ encoded_token } @{ domain } /{ owner } /{ repo } .git"
33+ elif token and config ["auth_method" ] == "token" :
34+ # GitCode推荐方式:使用用户名+Token[6](@ref)
35+ encoded_token = quote_plus (token )
36+ # 假设username作为GitCode用户名
37+ username = username or "gitcode_user"
38+ repo_url = f"https://{ username } :{ encoded_token } @{ domain } /{ owner } /{ repo } .git"
1539 else :
16- repo_url = f"https://gitee.com/ { gitee_owner } /{ gitee_repo } .git"
40+ repo_url = f"https://{ domain } / { owner } /{ repo } .git"
1741
1842 force_remove (target_dir )
1943
@@ -24,6 +48,8 @@ def clone_repo_skip_lfs(gitee_owner, gitee_repo, username=None, token=None, targ
2448 "GIT_CLONE_PROTECTION_ACTIVE" : "false"
2549 })
2650
51+ print (f"正在克隆 { platform } 仓库: { repo_url .replace (encoded_token , '***' ) if token else repo_url } " )
52+
2753 subprocess .run (
2854 ["git" , "clone" , repo_url , target_dir ],
2955 env = env ,
@@ -34,10 +60,15 @@ def clone_repo_skip_lfs(gitee_owner, gitee_repo, username=None, token=None, targ
3460 )
3561 return target_dir
3662 except subprocess .CalledProcessError as e :
37- error_msg = " 克隆失败: "
63+ error_msg = f" { platform } 克隆失败: "
3864 if username and token :
3965 error_msg += "认证失败或"
4066 error_msg += e .stderr .strip () if e .stderr else '未知错误'
67+
68+ # 平台特定的错误提示
69+ if platform == "gitcode" :
70+ error_msg += "\n GitCode提示: 请确认使用Token认证而非密码[6](@ref)"
71+
4172 force_remove (target_dir )
4273 raise RuntimeError (error_msg )
4374
@@ -123,15 +154,19 @@ def force_remove(path):
123154 os .system (f'rm -rf "{ path } "' if os .name != 'nt' else f'rd /s /q "{ path } "' )
124155
125156
126- def main (owner , repo , output_file = "lfs_mapping.json" , username = None , token = None ):
157+ def main (platform , owner , repo , output_file = "lfs_mapping.json" , username = None , token = None ):
158+ """主函数,支持平台参数"""
127159 try :
128- repo_dir = clone_repo_skip_lfs (owner , repo , username , token )
160+ if platform == "gitcode" and not token :
161+ print ("警告: GitCode强烈建议使用Token认证而非密码[6](@ref)" )
162+
163+ repo_dir = clone_repo_skip_lfs (platform , owner , repo , username , token )
129164 mapping = get_all_branches_lfs_mapping (repo_dir )
130165
131166 with open (output_file , "w" , encoding = "utf-8" ) as f :
132167 json .dump (mapping , f , indent = 2 , ensure_ascii = False )
133168
134- print (f"结果已保存到 { output_file } " )
169+ print (f"{ platform } 平台结果已保存到 { output_file } " )
135170 return True
136171 except Exception as e :
137172 print (f"错误: { str (e )} " , file = sys .stderr )
@@ -140,18 +175,19 @@ def main(owner, repo, output_file="lfs_mapping.json", username=None, token=None)
140175 if 'repo_dir' in locals ():
141176 force_remove (repo_dir )
142177
143-
144178if __name__ == "__main__" :
145- if len (sys .argv ) < 3 :
146- print ("用法: python lfsNameQuery.py <owner> <repo> [output_file] [username] [token]" )
179+ if len (sys .argv ) < 4 :
180+ print ("用法: python lfsNameQuery.py <platform> <owner> <repo> [output_file] [username] [token]" )
181+ print ("平台支持: gitee, gitcode" )
147182 sys .exit (1 )
148183
149184 args = {
150- "owner" : sys .argv [1 ],
151- "repo" : sys .argv [2 ],
152- "output_file" : sys .argv [3 ] if len (sys .argv ) > 3 else "lfs_mapping.json" ,
153- "username" : sys .argv [4 ] if len (sys .argv ) > 4 else None ,
154- "token" : sys .argv [5 ] if len (sys .argv ) > 5 else None
185+ "platform" : sys .argv [1 ], # 新增平台参数
186+ "owner" : sys .argv [2 ],
187+ "repo" : sys .argv [3 ],
188+ "output_file" : sys .argv [4 ] if len (sys .argv ) > 4 else "lfs_mapping.json" ,
189+ "username" : sys .argv [5 ] if len (sys .argv ) > 5 else None ,
190+ "token" : sys .argv [6 ] if len (sys .argv ) > 6 else None
155191 }
156192
157- sys .exit (0 if main ( ** args ) else 1 )
193+ sys .exit (0 if main (** args ) else 1 )
0 commit comments