use anyhow::Result;
use endringer_core::types::{CommitId, StashEntry};
use gix::Repository;
pub(crate) fn stash_entries(repo: &Repository) -> Result<Vec<StashEntry>> {
let reflog_path = repo.git_dir().join("logs").join("refs").join("stash");
let content = match std::fs::read_to_string(&reflog_path) {
Ok(c) => c,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(vec![]),
Err(e) => return Err(e.into()),
};
let raw_lines: Vec<&str> = content.lines().collect();
let entries: Vec<StashEntry> = raw_lines
.iter()
.rev()
.enumerate()
.filter_map(|(index, line)| parse_reflog_line(line, index))
.collect();
Ok(entries)
}
fn parse_reflog_line(line: &str, index: usize) -> Option<StashEntry> {
let tab_pos = line.find('\t')?;
let message = line[tab_pos + 1..].trim().to_owned();
let new_oid_hex = line.split_whitespace().nth(1)?;
let commit_id = CommitId::from_hex(new_oid_hex).ok()?;
Some(StashEntry { index, commit_id, message })
}