Small update

This commit is contained in:
2025-10-24 19:56:24 +08:00
parent 86d9378fe5
commit db628efba8
2 changed files with 70 additions and 1 deletions

View File

@@ -4,6 +4,7 @@ use pulldown_cmark::{html, Options, Parser};
use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;
use std::process::Command;
#[derive(Debug, Clone)]
pub struct Post {
@@ -65,6 +66,74 @@ impl PostManager {
}
}
fn get_post_timestamps(&self) -> Result<HashMap<String, DateTime<Utc>>, String> {
let output = Command::new("git")
.arg("whatchanged")
.arg("--pretty=%h - %cd - %s")
.arg("--date=unix")
.arg("--")
.arg("posts")
.current_dir(&self.git_dir)
.output()
.map_err(|e| format!("Failed to execute git whatchanged: {}", e))?;
if !output.status.success() {
return Err(format!(
"Git whatchanged command failed: {}",
String::from_utf8_lossy(&output.stderr)
));
}
let log_output = String::from_utf8_lossy(&output.stdout);
let mut timestamps: HashMap<String, DateTime<Utc>> = HashMap::new();
let mut current_timestamp: Option<DateTime<Utc>> = None;
for line in log_output.lines() {
let line = line.trim();
// Skip empty lines
if line.is_empty() {
continue;
}
// Parse commit header line: "f4fcf0e - 1761305168 - New post"
if !line.starts_with(':') {
if let Some(dash_pos) = line.find(" - ") {
let after_first_dash = &line[dash_pos + 3..];
if let Some(second_dash_pos) = after_first_dash.find(" - ") {
let timestamp_str = after_first_dash[..second_dash_pos].trim();
if let Ok(timestamp) = timestamp_str.parse::<i64>() {
current_timestamp = DateTime::from_timestamp(timestamp, 0);
}
}
}
}
// Parse file change line: ":000000 100644 0000000 6bdad65 A posts/hello-world-2.md"
else if line.starts_with(':') {
if let Some(timestamp) = current_timestamp {
let parts: Vec<&str> = line.split_whitespace().collect();
if parts.len() >= 6 {
let status = parts[4]; // A (add), D (delete), M (modify)
let file_path = parts[5];
// Only process existing files (not deleted ones)
if status != "D" && file_path.starts_with("posts/") {
if let Some(file_name) = file_path.strip_prefix("posts/") {
if let Some(name) = file_name.strip_suffix(".md") {
// Only update if we don't have a timestamp yet (latest commit wins)
timestamps.entry(name.to_string()).or_insert(timestamp);
}
}
}
}
}
}
}
Ok(timestamps)
}
fn refresh_posts(&mut self) -> Result<(), String> {
self.posts.clear();