fix: sync repo default branch

This commit is contained in:
engigu
2026-02-07 10:30:47 +08:00
parent 7851afd13a
commit 2bd614500d
2 changed files with 19 additions and 10 deletions
+18 -9
View File
@@ -152,10 +152,13 @@ def sync_git(args):
repo_url = repo_url.replace("https://", f"https://{args.auth_token}@")
dest = args.target_path
branch = args.branch or "main"
branch = args.branch
# 如果指定了 path 且是单文件模式,使用 raw URL 下载
if args.path and args.single_file:
# 单文件下载必须指定分支,如果未指定则默认为 main
if not args.branch:
args.branch = "main"
sync_git_file(args, repo_url, env)
return
@@ -199,28 +202,34 @@ def sync_git(args):
# 稀疏 clone(如果指定了 path)
if args.path:
run([
cmd = [
"git", "clone",
"--depth", "1",
"--filter=blob:none",
"--no-checkout",
"-b", branch,
repo_url,
dest
], env=env)
]
if branch:
cmd.extend(["-b", branch])
run(cmd, env=env)
run(["git", "sparse-checkout", "init", "--cone"], cwd=dest, env=env)
run(["git", "sparse-checkout", "set", args.path], cwd=dest, env=env)
run(["git", "checkout"], cwd=dest, env=env)
else:
# 普通 clone
run([
cmd = [
"git", "clone",
"--depth", "1",
"-b", branch,
repo_url,
dest
], env=env)
]
if branch:
cmd.extend(["-b", branch])
run(cmd, env=env)
print("同步完成")
@@ -286,8 +295,8 @@ def main():
help="源地址(Git仓库URL或文件URL")
parser.add_argument("--target-path", required=True,
help="目标路径")
parser.add_argument("--branch", default="main",
help="Git 分支名(仅 git 类型有效)")
parser.add_argument("--branch",
help="Git 分支名(仅 git 类型有效,不填则使用默认分支")
parser.add_argument("--path",
help="仅拉取指定文件或目录(仅 git 类型有效)")
parser.add_argument("--single-file", action="store_true",