dagger/dagger · error

error fetching branch from origin: %w %s

Error message

error fetching branch from origin: %w
%s

What it means

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.

Source

Thrown at engine/telemetry/labels.go:601

	target = strings.TrimPrefix(target, "refs/")
	src := fmt.Sprintf("refs/%s", target)
	dest := fmt.Sprintf("refs/dagger/%s", target)

	// Only allow shortening history when the repository is *already* shallow.
	// Passing --depth 1 to a full checkout writes .git/shallow and corrupts it,
	// breaking subsequent git history operations (merge-base, describe, ...).
	args := []string{"fetch"}
	if isShallowRepo(workdir) {
		args = append(args, "--depth", "1")
	}
	args = append(args, remote, fmt.Sprintf("+%s:%s", src, dest))

	// Fetch from the origin remote
	cmd := exec.Command("git", args...)
	cmd.Dir = workdir
	out, err := cmd.CombinedOutput()
	if err != nil {
		return nil, fmt.Errorf("error fetching branch from origin: %w\n%s", err, string(out))
	}

	// Get the reference of the fetched branch
	ref, err := repo.Reference(plumbing.ReferenceName(dest), true)
	if err != nil {
		return nil, fmt.Errorf("error getting reference %q: %w", dest, err)
	}

	// Get the commit object of the fetched branch
	branchCommit, err := repo.CommitObject(ref.Hash())
	if err != nil {
		return nil, fmt.Errorf("error getting commit %q: %w", ref.Hash(), err)
	}

	// Cleanup the temp ref
	cmd = exec.Command("git", "update-ref", "-d", dest, ref.Hash().String())
	cmd.Dir = workdir
	out, err = cmd.CombinedOutput()

View on GitHub (pinned to 82ba2681db)

Solutions

  1. Run the same `git fetch` manually in the workdir and read the trailing git output in the error message.
  2. Verify the `origin` remote exists and its URL is correct (`git remote -v`).
  3. Check remote auth (SSH key/token) and network access; for private repos refresh credentials.
  4. Confirm the branch/PR ref still exists on the remote; it may have been deleted.

Example fix

// before
out, err := cmd.CombinedOutput()
if err != nil {
    return nil, fmt.Errorf("error fetching branch from origin: %w\n%s", err, string(out))
}
// after (diagnose first)
if err != nil {
    _ = run("git", "remote", "-v") // confirm origin exists
    return nil, fmt.Errorf("error fetching branch from origin: %w\n%s", err, string(out))
}
Defensive patterns

Strategy: validation

Validate before calling

// check remote before relying on fetch
func hasOrigin(workdir string) error {
    out, err := exec.Command("git", "-C", workdir, "remote", "get-url", "origin").Output()
    if err != nil || len(out) == 0 {
        return errors.New("no origin remote configured")
    }
    return nil
}

Try / catch

ref, err := fetchRef(...)
if err != nil {
    var exitErr *exec.ExitError
    if errors.As(err, &exitErr) {
        // git's stderr is appended after \n in the message; log it for diagnosis
        slog.Warn("git fetch failed", "detail", err.Error())
    }
    return fallbackRef
}

Prevention

When it happens

Trigger: exec.Command("git", args...).CombinedOutput() returns a non-zero exit while fetching the branch/PR ref from origin in the given workdir.

Common situations: 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.

Related errors


AI-assisted analysis of dagger/dagger@82ba2681db (2026-09-05). Data as JSON: /api/errors/5e7bfc78e3eeceb8. Report an issue: GitHub.