🏡 index : ~doyle/rgit.git

author Jordan Doyle <jordan@doyle.la> 2022-07-17 20:46:26.0 +01:00:00
committer Jordan Doyle <jordan@doyle.la> 2022-07-17 20:46:26.0 +01:00:00
commit
471ec711f568869a0b65c01390b8f39f0d82bc84 [patch]
tree
a3728412a818324d40443d86fc9d59141883460f
parent
a41e966d637b74dec806da7193a54996f0760e56
download
471ec711f568869a0b65c01390b8f39f0d82bc84.tar.gz

Run indexer every 5 minutes



Diff

 src/main.rs             |  9 +++++++--
 src/database/indexer.rs | 88 ++++++++++++++++++++++++++++++++++++++++----------------------------------------
 src/methods/repo.rs     |  3 +--
 3 files changed, 46 insertions(+), 54 deletions(-)

diff --git a/src/main.rs b/src/main.rs
index 3fc07d3..c55e9ba 100644
--- a/src/main.rs
+++ a/src/main.rs
@@ -9,8 +9,10 @@
};
use bat::assets::HighlightingAssets;
use std::sync::Arc;
use std::time::Duration;
use syntect::html::ClassStyle;
use tower_layer::layer_fn;
use tracing::info;

use crate::{git::Git, layers::logger::LoggingMiddleware};

@@ -33,9 +35,12 @@
    std::thread::spawn({
        let db = db.clone();

        move || {
        move || loop {
            info!("Running periodic index");
            crate::database::indexer::run_indexer(&db);
            eprintln!("finished indexer");
            info!("Finished periodic index");

            std::thread::sleep(Duration::from_secs(300));
        }
    });

diff --git a/src/database/indexer.rs b/src/database/indexer.rs
index 109f197..5e42a29 100644
--- a/src/database/indexer.rs
+++ a/src/database/indexer.rs
@@ -8,7 +8,40 @@
pub fn run_indexer(db: &sled::Db) {
    let scan_path = Path::new("/Users/jordan/Code/test-git");
    update_repository_metadata(scan_path, &db);
    update_repository_reflog(scan_path, &db);
}

fn update_repository_metadata(scan_path: &Path, db: &sled::Db) {
    let mut discovered = Vec::new();
    discover_repositories(scan_path, &mut discovered);

    for repository in discovered {
        let relative = get_relative_path(scan_path, &repository);

        let id = Repository::open(db, relative)
            .map(|v| v.id)
            .unwrap_or_else(|| RepositoryId::new(db));
        let name = relative.file_name().unwrap().to_string_lossy().to_string();
        let description = Some(
            String::from_utf8_lossy(
                &std::fs::read(repository.join("description")).unwrap_or_default(),
            )
            .to_string(),
        )
        .filter(|v| !v.is_empty());

        Repository {
            id,
            name,
            description,
            owner: None, // TODO read this from config
            last_modified: OffsetDateTime::now_utc(),
        }
        .insert(db, relative);
    }
}

fn update_repository_reflog(scan_path: &Path, db: &sled::Db) {
    for (relative_path, db_repository) in Repository::fetch_all(&db) {
        let git_repository = git2::Repository::open(scan_path.join(relative_path)).unwrap();

@@ -31,7 +64,11 @@

            for rev in revwalk {
                let rev = rev.unwrap();
                let commit = git_repository.find_commit(rev).unwrap();
                let commit = if let Ok(commit) = git_repository.find_commit(rev) {
                    commit
                } else {
                    continue;
                };

                let author = commit.author();
                let committer = commit.committer();
@@ -71,42 +108,10 @@

                db_commit.insert(&commit_tree, i);
            }
        }
    }
}

fn update_repository_metadata(scan_path: &Path, db: &sled::Db) {
    let mut discovered = Vec::new();
    discover_repositories(scan_path, &mut discovered);

    for repository in discovered {
        let relative = get_relative_path(scan_path, &repository);

        let id = Repository::open(db, relative)
            .map(|v| v.id)
            .unwrap_or_else(|| RepositoryId::new(db));
        let name = relative.file_name().unwrap().to_string_lossy().to_string();
        let description = Some(
            String::from_utf8_lossy(
                &std::fs::read(repository.join("description")).unwrap_or_default(),
            )
            .to_string(),
        )
        .filter(|v| !v.is_empty());

        Repository {
            id,
            name,
            description,
            owner: None, // TODO read this from config
            last_modified: OffsetDateTime::now_utc(),
        }
        .insert(db, relative);
    }
}

// util

fn get_relative_path<'a>(relative_to: &Path, full_path: &'a Path) -> &'a Path {
    full_path.strip_prefix(relative_to).unwrap()
}
@@ -125,22 +130,5 @@
            // probably not a bare git repo, lets recurse deeper
            discover_repositories(&dir, discovered_repos);
        }
    }
}

#[cfg(test)]
mod test {
    use crate::database::schema::repository::Repository;
    use time::Instant;

    #[test]
    fn test_discovery() {
        let db = sled::open(std::env::temp_dir().join("sled-test.db")).unwrap();

        let start = Instant::now();
        super::run_indexer(&db);
        let repo = Repository::open(&db, "1p.git");

        panic!("{} - {:#?}", start.elapsed(), repo);
    }
}
diff --git a/src/methods/repo.rs b/src/methods/repo.rs
index 465decb..bd0b00c 100644
--- a/src/methods/repo.rs
+++ a/src/methods/repo.rs
@@ -171,8 +171,7 @@
    let offset = query.offset.unwrap_or(0);

    let reference = format!("refs/heads/{}", query.branch.as_deref().unwrap_or("master"));
    let repository =
        crate::database::schema::repository::Repository::open(&db, &*repo).unwrap();
    let repository = crate::database::schema::repository::Repository::open(&db, &*repo).unwrap();
    let commit_tree = repository.commit_tree(&db, &reference);
    let mut commits = commit_tree.fetch_latest(101, offset);