{"record":{"id":"83959856611222e8","repo":"alibaba/open-code-review","slug":"preview-failed-w","errorCode":null,"errorMessage":"preview failed: %w","messagePattern":"preview failed: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/opencodereview/delegate_cmd.go","lineNumber":166,"sourceCode":"\t\treturn \"workspace\"\n\t}\n}\n\n// resolver returns the rules Resolver, asserting DetailResolver if available.\nfunc (dc *delegateContext) resolver() rules.Resolver {\n\treturn dc.cc.Resolver\n}\n\nfunc executeDelegatePreview(opts delegateOptions) error {\n\tdc, err := loadDelegateContext(opts)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tctx := context.Background()\n\tpreview, err := dc.preview(ctx)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"preview failed: %w\", err)\n\t}\n\tmergeBase := dc.mergeBase(ctx)\n\tif opts.format == \"json\" {\n\t\treturn writeDelegateJSON(delegatePreviewJSON{\n\t\t\tSchemaVersion:   delegateSchemaVersion,\n\t\t\tMode:            dc.reviewMode(),\n\t\t\tRepository:      dc.cc.RepoDir,\n\t\t\tFrom:            dc.opts.from,\n\t\t\tTo:              dc.opts.to,\n\t\t\tCommit:          dc.opts.commit,\n\t\t\tMergeBase:       mergeBase,\n\t\t\tBackground:      dc.opts.background,\n\t\t\tTotalFiles:      preview.TotalFiles,\n\t\t\tReviewableCount: preview.ReviewableCount,\n\t\t\tExcludedCount:   preview.ExcludedCount,\n\t\t\tTotalInsertions: preview.TotalInsertions,\n\t\t\tTotalDeletions:  preview.TotalDeletions,\n\t\t\tReviewableFiles: previewFiles(preview, true),","sourceCodeStart":148,"sourceCodeEnd":184,"githubUrl":"https://github.com/alibaba/open-code-review/blob/5cf97d0d15cbd41b602513c4be3bfec3cee5bf7f/cmd/opencodereview/delegate_cmd.go#L148-L184","documentation":"executeDelegatePreview calls dc.preview(ctx) to compute the delegate review preview (merge-base, diff scope, etc.) and wraps any failure as \"preview failed: <cause>\". It means the underlying git/preview computation errored; the real cause is preserved by %w.","triggerScenarios":"Running a delegate command in preview mode outside a git repository, with no commits reachable from the merge base, on an unborn branch (no HEAD), or with a corrupted git index.","commonSituations":"Running the command in a freshly `git init`-ed repo with no commits; detached/empty branch; git binary missing or misconfigured; shallow clone lacking the merge base.","solutions":["Read the wrapped cause after `preview failed:` — it names the underlying git error","Run the command inside a git repository with at least one commit on both sides of the merge base","Fetch/unshallow the repo if the merge base commit is missing","Verify `git log`, `git merge-base` work manually in the same directory"],"exampleFix":"// before (empty repo)\n$ ocr delegate --preview\npreview failed: exit status 128 (bad revision 'HEAD')\n// after\n$ git commit --allow-empty -m init && ocr delegate --preview","handlingStrategy":"try-catch","validationCode":"func repoHasCommits(dir string) bool {\n    return runGitCmd(dir, \"rev-parse\", \"--verify\", \"HEAD\") == nil\n}","typeGuard":null,"tryCatchPattern":"preview, err := dc.preview(ctx)\nif err != nil {\n    if errors.Is(err, exec.ErrNotFound) { /* git missing */ }\n    var ee *exec.ExitError\n    if errors.As(err, &ee) { log.Fatalf(\"preview failed: %s\", ee.Stderr) }\n    return fmt.Errorf(\"preview failed: %w\", err)\n}","preventionTips":["Run delegate commands only inside initialized, committed git repos","Ensure the merge base exists (fetch before comparing across remotes)","Avoid shallow clones when diffing against remote branches"],"tags":["git","cli","preview"],"backgroundTag":"git-command-failed","analyzedSha":"5cf97d0d15cbd41b602513c4be3bfec3cee5bf7f","analyzedAt":"2026-09-02T02:08:09.116Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}