gitbutlerapp/gitbutler · error

Refusing to read ' ' from commit as it's not relative to…

Error message

Refusing to read '{relative_path:?}' from commit {commit_id:?} as it's not relative to the worktree

What it means

read_commit_file only accepts paths relative to the worktree root. If relative_path is absolute (or otherwise not relative), it refuses to read the file from the commit's tree, blocking path traversal outside the repository.

Solutions

  1. Pass a path relative to the worktree root (strip the workdir prefix before calling).
  2. Normalize the input: path.strip_prefix(workdir) or Path::new(p).strip_prefix(base) and use the remainder.
  3. Reject/handle absolute paths at the API boundary with a clear client-side error.

Example fix

// before
read_file_from_commit(project, commit_id, "/repo/src/main.rs")
// after
let rel = Path::new(abs).strip_prefix(workdir)?;
read_file_from_commit(project, commit_id, rel)
Defensive patterns

Strategy: validation

Validate before calling

fn ensure_relative(p: &Path) -> Result<&Path, String> {
    if p.is_relative() && !p.starts_with("/") { Ok(p) } else { Err(format!("path must be worktree-relative: {p:?}")) }
}

Type guard

fn is_worktree_relative(p: &Path) -> bool { p.is_relative() }

Try / catch

match result {
    Err(e) if e.to_string().contains("not relative to the worktree") => {
        let rel = Path::new(abs).strip_prefix(workdir)?;
        retry_with(rel)
    }
    other => other,
}

Prevention

When it happens

Trigger: Calling read_file_from_commit or read_worktree_file with an absolute path (e.g. "/etc/passwd", "C:\\repo\\file") or a path with a root/prefix component instead of a repo-relative path like "src/main.rs".

Common situations: Frontend sends an absolute path taken from an OS file dialog; path built by joining an absolute base; Windows drive-letter paths passed unchanged.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of gitbutlerapp/gitbutler@58e5313667 (2026-09-18). Data as JSON: /api/errors/619b5d3776b1bfd5. Report an issue: GitHub.

Appendix: source

Thrown at crates/gitbutler-repo/src/commands.rs:342

            .filter(|entry| entry.0 > 0)
            .sorted_by(|a, b| b.0.cmp(&a.0))
            .take(limit)
            .map(|(_, path)| path)
            .collect();

        Ok(scored_files)
    }
}

/// Read `relative_path` from the tree of `commit_id` in `repo`, see
/// [`RepoCommands::read_file_from_commit()`].
fn read_commit_file(
    repo: &gix::Repository,
    commit_id: gix::ObjectId,
    relative_path: &Path,
) -> Result<FileInfo> {
    if !relative_path.is_relative() {
        bail!(
            "Refusing to read '{relative_path:?}' from commit {commit_id:?} as it's not relative to the worktree"
        );
    }

    let tree = repo.find_commit(commit_id)?.tree()?;

    Ok(match tree.lookup_entry_by_path(relative_path)? {
        Some(entry) => {
            let blob = repo.find_blob(entry.id())?;
            FileInfo::from_content(relative_path, &blob.data)
        }
        None => FileInfo::deleted(),
    })
}

/// Read `path` from the checkout of `repo`, see [`RepoCommands::read_file_from_workspace()`].
/// `path` can be relative or absolute, and is validated to be inside the worktree.
/// A linked worktree opened from disk reads the same way as the main worktree.

View on GitHub (pinned to 58e5313667)