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}@") repo_url = repo_url.replace("https://", f"https://{args.auth_token}@")
dest = args.target_path dest = args.target_path
branch = args.branch or "main" branch = args.branch
# 如果指定了 path 且是单文件模式,使用 raw URL 下载 # 如果指定了 path 且是单文件模式,使用 raw URL 下载
if args.path and args.single_file: if args.path and args.single_file:
# 单文件下载必须指定分支,如果未指定则默认为 main
if not args.branch:
args.branch = "main"
sync_git_file(args, repo_url, env) sync_git_file(args, repo_url, env)
return return
@@ -199,28 +202,34 @@ def sync_git(args):
# 稀疏 clone(如果指定了 path) # 稀疏 clone(如果指定了 path)
if args.path: if args.path:
run([ cmd = [
"git", "clone", "git", "clone",
"--depth", "1", "--depth", "1",
"--filter=blob:none", "--filter=blob:none",
"--no-checkout", "--no-checkout",
"-b", branch,
repo_url, repo_url,
dest 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", "init", "--cone"], cwd=dest, env=env)
run(["git", "sparse-checkout", "set", args.path], cwd=dest, env=env) run(["git", "sparse-checkout", "set", args.path], cwd=dest, env=env)
run(["git", "checkout"], cwd=dest, env=env) run(["git", "checkout"], cwd=dest, env=env)
else: else:
# 普通 clone # 普通 clone
run([ cmd = [
"git", "clone", "git", "clone",
"--depth", "1", "--depth", "1",
"-b", branch,
repo_url, repo_url,
dest dest
], env=env) ]
if branch:
cmd.extend(["-b", branch])
run(cmd, env=env)
print("同步完成") print("同步完成")
@@ -286,8 +295,8 @@ def main():
help="源地址(Git仓库URL或文件URL") help="源地址(Git仓库URL或文件URL")
parser.add_argument("--target-path", required=True, parser.add_argument("--target-path", required=True,
help="目标路径") help="目标路径")
parser.add_argument("--branch", default="main", parser.add_argument("--branch",
help="Git 分支名(仅 git 类型有效)") help="Git 分支名(仅 git 类型有效,不填则使用默认分支")
parser.add_argument("--path", parser.add_argument("--path",
help="仅拉取指定文件或目录(仅 git 类型有效)") help="仅拉取指定文件或目录(仅 git 类型有效)")
parser.add_argument("--single-file", action="store_true", parser.add_argument("--single-file", action="store_true",
+1 -1
View File
@@ -172,7 +172,7 @@ async function save() {
</div> </div>
<div v-if="repoConfig.source_type === 'git'" class="grid grid-cols-1 sm:grid-cols-4 items-center gap-2 sm:gap-3"> <div v-if="repoConfig.source_type === 'git'" class="grid grid-cols-1 sm:grid-cols-4 items-center gap-2 sm:gap-3">
<Label class="sm:text-right text-sm">分支</Label> <Label class="sm:text-right text-sm">分支</Label>
<Input v-model="repoConfig.branch" placeholder="main (可选)" class="sm:col-span-3 h-8 text-sm" autocomplete="off" /> <Input v-model="repoConfig.branch" placeholder="默认分支 (可选)" class="sm:col-span-3 h-8 text-sm" autocomplete="off" />
</div> </div>
<div v-if="repoConfig.source_type === 'git'" class="grid grid-cols-1 sm:grid-cols-4 items-center gap-2 sm:gap-3"> <div v-if="repoConfig.source_type === 'git'" class="grid grid-cols-1 sm:grid-cols-4 items-center gap-2 sm:gap-3">
<Label class="sm:text-right text-sm">稀疏路径</Label> <Label class="sm:text-right text-sm">稀疏路径</Label>