From de23e9c3ee96ad725debe0fa64042d413ed10eea Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Mon, 18 Jul 2022 00:19:13 +0100 Subject: [PATCH] Don't reindex if there hasn't been any new commits --- src/git.rs | 4 ++-- src/database/indexer.rs | 28 ++++++++++++++++++++++------ src/database/schema/commit.rs | 14 ++++++++++++++ 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/git.rs b/src/git.rs index dd41468..51b95ee 100644 --- a/src/git.rs +++ a/src/git.rs @@ -530,7 +530,7 @@ _ => (None, false), }; - let line = std::str::from_utf8(diff_line.content()).unwrap(); + let line = String::from_utf8_lossy(diff_line.content()); let extension = if should_highlight_as_source { let path = delta.new_file().path().unwrap(); @@ -547,7 +547,7 @@ let mut html_generator = ClassedHTMLGenerator::new_with_class_style(syntax, &syntax_set, ClassStyle::Spaced); html_generator - .parse_html_for_line_which_includes_newline(line) + .parse_html_for_line_which_includes_newline(&line) .unwrap(); if let Some(class) = class { write!(diff_output, r#""#).unwrap(); diff --git a/src/database/indexer.rs b/src/database/indexer.rs index f4c76dd..fa7417b 100644 --- a/src/database/indexer.rs +++ a/src/database/indexer.rs @@ -1,7 +1,7 @@ use git2::Sort; use std::path::{Path, PathBuf}; use time::OffsetDateTime; -use tracing::info; +use tracing::{info, info_span}; use crate::database::schema::{ commit::Commit, @@ -44,25 +44,31 @@ let git_repository = git2::Repository::open(scan_path.join(&relative_path)).unwrap(); for reference in git_repository.references().unwrap() { - let reference = if let Some(reference) = reference.as_ref().ok().and_then(|v| v.name()) - { - reference - } else { - continue; - }; + let reference = reference.unwrap(); - if !reference.starts_with("refs/heads/") { + let reference_name = String::from_utf8_lossy(reference.name_bytes()); + if !reference_name.starts_with("refs/heads/") { continue; } + + let span = info_span!("index_update", reference = reference_name.as_ref(), repository = relative_path); + let _entered = span.enter(); - info!("Updating indexes for {} on {}", reference, relative_path); + info!("Refreshing indexes"); - let commit_tree = db_repository.get().commit_tree(db, reference); + let commit_tree = db_repository.get().commit_tree(db, &reference_name); + + if let (Some(latest_indexed), Ok(latest_commit)) = (commit_tree.fetch_latest_one(), reference.peel_to_commit()) { + if latest_commit.id().as_bytes() == &*latest_indexed.get().hash { + info!("No commits since last index"); + continue; + } + } // TODO: only scan revs from the last time we looked let mut revwalk = git_repository.revwalk().unwrap(); revwalk.set_sorting(Sort::REVERSE).unwrap(); - revwalk.push_ref(reference).unwrap(); + revwalk.push_ref(&reference_name).unwrap(); let mut i = 0; for rev in revwalk { diff --git a/src/database/schema/commit.rs b/src/database/schema/commit.rs index 47627d9..b76f41e 100644 --- a/src/database/schema/commit.rs +++ a/src/database/schema/commit.rs @@ -120,6 +120,20 @@ Self(tree) } + pub fn fetch_latest_one(&self) -> Option { + self.last() + .unwrap() + .map(|(_, value)| { + // internally value is an Arc so it should already be stablederef but because + // of reasons unbeknownst to me, sled has its own Arc implementation so we need + // to box the value as well to get a stablederef... + let value = Box::new(value); + + Yoke::try_attach_to_cart(value, |data: &IVec| bincode::deserialize(&data)) + .unwrap() + }) + } + pub async fn fetch_latest(&self, amount: usize, offset: usize) -> Vec { let latest_key = if let Some((latest_key, _)) = self.last().unwrap() { let mut latest_key_bytes = [0; std::mem::size_of::()]; -- rgit 0.1.3