{"record":{"id":"798206b1bf815a68","repo":"babalae/better-genshin-impact","slug":"branchname","errorCode":null,"errorMessage":"远程仓库中未找到 {branchName} 分支","messagePattern":"远程仓库中未找到 (.+?) 分支","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"BetterGenshinImpact/Core/Script/ScriptRepoUpdater.cs","lineNumber":1289,"sourceCode":"            var fetchOptions = new FetchOptions\n            {\n                TagFetchMode = TagFetchMode.None,\n                ProxyOptions = { ProxyType = ProxyType.None },\n                Depth = 1, // 浅拉取，只获取最新的提交\n                CredentialsProvider = CreateCredentialsHandler(), // 添加凭据处理器\n                OnTransferProgress = progress =>\n                {\n                    onCheckoutProgress?.Invoke($\"拉取对象 {progress.ReceivedObjects}/{progress.TotalObjects}\", progress.ReceivedObjects, progress.TotalObjects);\n                    return true;\n                }\n            };\n            string refSpec = $\"+refs/heads/{branchName}:refs/remotes/origin/{branchName}\";\n            Commands.Fetch(repo, remote.Name, new[] { refSpec }, fetchOptions, \"初始化拉取\");\n\n            // 获取远程分支\n            var remoteBranch = repo.Branches[$\"origin/{branchName}\"];\n            if (remoteBranch == null)\n                throw new Exception($\"远程仓库中未找到 {branchName} 分支\");\n\n            // 创建本地分支\n            var localBranch = repo.CreateBranch(branchName, remoteBranch.Tip);\n            repo.Branches.Update(localBranch, b => b.TrackedBranch = remoteBranch.CanonicalName);\n\n            // 手动检出HEAD到新分支\n            repo.Refs.UpdateTarget(repo.Refs.Head, localBranch.CanonicalName);\n\n            // 手动检出 repo.json 文件\n            CheckoutRepoJson(repo, remoteBranch.Tip);\n        }\n        finally\n        {\n            repo?.Dispose();\n        }\n    }\n\n    /// <summary>","sourceCodeStart":1271,"sourceCodeEnd":1307,"githubUrl":"https://github.com/babalae/better-genshin-impact/blob/a7cb36712dcb409be610257d877fcea3597e9d6b/BetterGenshinImpact/Core/Script/ScriptRepoUpdater.cs#L1271-L1307","documentation":"Thrown during initial repository clone/fetch (CloneRepository flow) when Commands.Fetch with a refspec for the requested branch completes but repo.Branches[\"origin/{branchName}\"] returns null. LibGit2Sharp created the remote-tracking ref under a different name, the fetch silently failed to populate the branch, or the branch name contains unexpected characters.","triggerScenarios":"Commands.Fetch(repo, remote.Name, [\"+refs/heads/{branchName}:refs/remotes/origin/{branchName}\"], fetchOptions, ...) is called, then repo.Branches[$\"origin/{branchName}\"] is accessed. The indexer returns null when the remote-tracking branch was not created — typically because the upstream branch does not exist, the fetch was a shallow clone (Depth=1) that pruned it, or a transient fetch error was swallowed.","commonSituations":"Shallow fetch (Depth=1 set on line 1275) combined with a server that doesn't honor the refspec; the requested branch was deleted between a clone attempt and this call; branch name casing mismatch on case-sensitive servers; Gitea/GitHub auth token lacks read scope for that branch.","solutions":["Remove Depth=1 from FetchOptions (line 1275) if the server mishandles shallow fetch with specific refspecs, or verify the branch exists first via ListReferences.","Before fetching, verify the branch exists remotely: if (repo.Network.ListReferences(repoUrl).All(r => r.CanonicalName != $\"refs/heads/{branchName}\")) throw a clearer error.","Check repo.Branches using a case-insensitive lookup (repo.Branches.FirstOrDefault(b => string.Equals(b.FriendlyName, $\"origin/{branchName}\", StringComparison.OrdinalIgnoreCase))).","Inspect the actual refspecs created after Fetch via repo.Network.Remotes[remote.Name].FetchRefSpec to confirm the refspec matched server-side refs."],"exampleFix":"// before\nstring refSpec = $\"+refs/heads/{branchName}:refs/remotes/origin/{branchName}\";\nCommands.Fetch(repo, remote.Name, new[] { refSpec }, fetchOptions, \"初始化拉取\");\nvar remoteBranch = repo.Branches[$\"origin/{branchName}\"];\nif (remoteBranch == null)\n    throw new Exception($\"远程仓库中未找到 {branchName} 分支\");\n\n// after (verify existence first, clearer message)\nvar refs = repo.Network.ListReferences(repoUrl, CreateCredentialsHandler());\nif (refs.All(r => r.CanonicalName != $\"refs/heads/{branchName}\"))\n    throw new Exception($\"远程仓库 {repoUrl} 不存在分支 {branchName}，请检查仓库地址与分支名\");\nCommands.Fetch(repo, remote.Name, new[] { refSpec }, fetchOptions, \"初始化拉取\");\nvar remoteBranch = repo.Branches[$\"origin/{branchName}\"];\nif (remoteBranch == null)\n    throw new Exception($\"拉取完成但未生成 origin/{branchName} 跟踪分支，可能是浅克隆或服务端不支持该 refspec\");","handlingStrategy":"validation","validationCode":"// Verify remote branch exists before fetch\nvar remoteRefs = repo.Network.ListReferences(repoUrl, CreateCredentialsHandler());\nbool exists = remoteRefs.Any(r => r.CanonicalName == $\"refs/heads/{branchName}\");\nif (!exists)\n    throw new InvalidOperationException(\n        $\"远程仓库 {repoUrl} 不存在分支 {branchName}\");","typeGuard":null,"tryCatchPattern":"try { CloneRepository(repoUrl, repoPath, branchName, onCheckoutProgress); }\ncatch (Exception ex) when (ex.Message.Contains(\"未找到\") || ex.Message.Contains(\"分支\"))\n{\n    _logger.LogError(ex, \"克隆失败：分支 {Branch} 不存在于 {Url}\", branchName, repoUrl);\n    // surface to UI, do not retry blindly\n    throw;\n}","preventionTips":["Avoid Depth=1 shallow fetch when the server's refspec handling is unknown.","Confirm branch existence via ListReferences before Commands.Fetch.","Log the refspecs actually applied after Fetch for post-mortem."],"tags":["git","libgit2sharp","clone","shallow-fetch","branch-config"],"backgroundTag":null,"analyzedSha":"a7cb36712dcb409be610257d877fcea3597e9d6b","analyzedAt":"2026-08-13T16:44:57.548Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}