Skip to main content

binocular/preview/request/git/
history.rs

1use crate::preview::encoding;
2use crate::preview::{create_rich_text_document, PreviewContent};
3use crate::search::sources::git::read_history_blob;
4use ratatui::text::Text;
5
6pub(crate) fn build_history_preview(repo_root: &str, commit: &str, path: &str) -> PreviewContent {
7    match read_history_blob(
8        std::path::Path::new(repo_root),
9        commit,
10        std::path::Path::new(path),
11    ) {
12        Ok(bytes) => PreviewContent::RichText(create_rich_text_document(
13            decode_history_blob(bytes),
14            std::path::Path::new(path),
15        )),
16        Err(err) => PreviewContent::PlainText(Text::from(format!(
17            "Failed to load git history preview: {err}"
18        ))),
19    }
20}
21
22fn decode_history_blob(bytes: Vec<u8>) -> String {
23    if let Some(decoded) = encoding::try_decode_utf16(&bytes) {
24        return decoded;
25    }
26
27    if bytes.contains(&0) {
28        return "Binary historical blobs are not previewable".to_string();
29    }
30
31    String::from_utf8_lossy(&bytes).into_owned()
32}