{"record":{"id":"5e7bfc78e3eeceb8","repo":"dagger/dagger","slug":"error-fetching-branch-from-origin-w-s","errorCode":null,"errorMessage":"error fetching branch from origin: %w\n%s","messagePattern":"error fetching branch from origin: %w\n(.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/telemetry/labels.go","lineNumber":601,"sourceCode":"\ttarget = strings.TrimPrefix(target, \"refs/\")\n\tsrc := fmt.Sprintf(\"refs/%s\", target)\n\tdest := fmt.Sprintf(\"refs/dagger/%s\", target)\n\n\t// Only allow shortening history when the repository is *already* shallow.\n\t// Passing --depth 1 to a full checkout writes .git/shallow and corrupts it,\n\t// breaking subsequent git history operations (merge-base, describe, ...).\n\targs := []string{\"fetch\"}\n\tif isShallowRepo(workdir) {\n\t\targs = append(args, \"--depth\", \"1\")\n\t}\n\targs = append(args, remote, fmt.Sprintf(\"+%s:%s\", src, dest))\n\n\t// Fetch from the origin remote\n\tcmd := exec.Command(\"git\", args...)\n\tcmd.Dir = workdir\n\tout, err := cmd.CombinedOutput()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"error fetching branch from origin: %w\\n%s\", err, string(out))\n\t}\n\n\t// Get the reference of the fetched branch\n\tref, err := repo.Reference(plumbing.ReferenceName(dest), true)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"error getting reference %q: %w\", dest, err)\n\t}\n\n\t// Get the commit object of the fetched branch\n\tbranchCommit, err := repo.CommitObject(ref.Hash())\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"error getting commit %q: %w\", ref.Hash(), err)\n\t}\n\n\t// Cleanup the temp ref\n\tcmd = exec.Command(\"git\", \"update-ref\", \"-d\", dest, ref.Hash().String())\n\tcmd.Dir = workdir\n\tout, err = cmd.CombinedOutput()","sourceCodeStart":583,"sourceCodeEnd":619,"githubUrl":"https://github.com/dagger/dagger/blob/82ba2681dbe30d3547a1dc50ea495900ab5b6047/engine/telemetry/labels.go#L583-L619","documentation":"fetchRef runs `git fetch` against the origin remote to resolve a PR head branch, capturing combined stdout/stderr. If the git command exits non-zero, the error (plus git's output) is wrapped as \"error fetching branch from origin: %w\\n%s\". This is used by fetchPRHead to resolve telemetry labels for PRs.","triggerScenarios":"exec.Command(\"git\", args...).CombinedOutput() returns a non-zero exit while fetching the branch/PR ref from origin in the given workdir.","commonSituations":"Repository has no `origin` remote or an incorrect remote URL; branch was force-deleted on the remote; no network/auth access to the remote (private repo, expired token); shallow clone missing fetch refs.","solutions":["Run the same `git fetch` manually in the workdir and read the trailing git output in the error message.","Verify the `origin` remote exists and its URL is correct (`git remote -v`).","Check remote auth (SSH key/token) and network access; for private repos refresh credentials.","Confirm the branch/PR ref still exists on the remote; it may have been deleted."],"exampleFix":"// before\nout, err := cmd.CombinedOutput()\nif err != nil {\n    return nil, fmt.Errorf(\"error fetching branch from origin: %w\\n%s\", err, string(out))\n}\n// after (diagnose first)\nif err != nil {\n    _ = run(\"git\", \"remote\", \"-v\") // confirm origin exists\n    return nil, fmt.Errorf(\"error fetching branch from origin: %w\\n%s\", err, string(out))\n}","handlingStrategy":"validation","validationCode":"// check remote before relying on fetch\nfunc hasOrigin(workdir string) error {\n    out, err := exec.Command(\"git\", \"-C\", workdir, \"remote\", \"get-url\", \"origin\").Output()\n    if err != nil || len(out) == 0 {\n        return errors.New(\"no origin remote configured\")\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":"ref, err := fetchRef(...)\nif err != nil {\n    var exitErr *exec.ExitError\n    if errors.As(err, &exitErr) {\n        // git's stderr is appended after \\n in the message; log it for diagnosis\n        slog.Warn(\"git fetch failed\", \"detail\", err.Error())\n    }\n    return fallbackRef\n}","preventionTips":["Ensure `origin` remote exists with a reachable URL.","Keep remote credentials valid (SSH keys/tokens) in CI.","Don't delete branches on the remote while PR heads are still resolved from them.","Avoid shallow clones when PR-head resolution is needed."],"tags":["git","network","subprocess"],"backgroundTag":"git-fetch-failed","analyzedSha":"82ba2681dbe30d3547a1dc50ea495900ab5b6047","analyzedAt":"2026-09-05T07:21:37.930Z","contentChangedAt":"2026-09-05T07:21:37.930Z","schemaVersion":2},"datasetVersion":"2026-09-12T12:17:11.808Z"}