Переглянути джерело

Start git integration (see issue #4)

Alexandre Leblanc 3 роки тому
батько
коміт
c3bd95ce65
5 змінених файлів з 126 додано та 22 видалено
  1. 87 0
      Cargo.lock
  2. 5 4
      Cargo.toml
  3. 2 5
      src/main.rs
  4. 30 11
      src/repository/git_repository.rs
  5. 2 2
      src/repository/mod.rs

+ 87 - 0
Cargo.lock

@@ -520,6 +520,21 @@ dependencies = [
  "wasi 0.11.0+wasi-snapshot-preview1",
 ]
 
+[[package]]
+name = "git2"
+version = "0.14.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d0155506aab710a86160ddb504a480d2964d7ab5b9e62419be69e0032bc5931c"
+dependencies = [
+ "bitflags",
+ "libc",
+ "libgit2-sys",
+ "log",
+ "openssl-probe",
+ "openssl-sys",
+ "url",
+]
+
 [[package]]
 name = "h2"
 version = "0.3.13"
@@ -631,6 +646,46 @@ version = "0.2.126"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836"
 
+[[package]]
+name = "libgit2-sys"
+version = "0.13.4+1.4.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d0fa6563431ede25f5cc7f6d803c6afbc1c5d3ad3d4925d12c882bf2b526f5d1"
+dependencies = [
+ "cc",
+ "libc",
+ "libssh2-sys",
+ "libz-sys",
+ "openssl-sys",
+ "pkg-config",
+]
+
+[[package]]
+name = "libssh2-sys"
+version = "0.2.23"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b094a36eb4b8b8c8a7b4b8ae43b2944502be3e59cd87687595cf6b0a71b3f4ca"
+dependencies = [
+ "cc",
+ "libc",
+ "libz-sys",
+ "openssl-sys",
+ "pkg-config",
+ "vcpkg",
+]
+
+[[package]]
+name = "libz-sys"
+version = "1.1.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9702761c3935f8cc2f101793272e202c72b99da8f4224a19ddcf1279a6450bbf"
+dependencies = [
+ "cc",
+ "libc",
+ "pkg-config",
+ "vcpkg",
+]
+
 [[package]]
 name = "local-channel"
 version = "0.1.3"
@@ -761,6 +816,25 @@ version = "1.12.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "7709cef83f0c1f58f666e746a08b21e0085f7440fa6a29cc194d68aac97a4225"
 
+[[package]]
+name = "openssl-probe"
+version = "0.1.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf"
+
+[[package]]
+name = "openssl-sys"
+version = "0.9.75"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e5f9bd0c2710541a3cda73d6f9ac4f1b240de4ae261065d309dbe73d9dceb42f"
+dependencies = [
+ "autocfg",
+ "cc",
+ "libc",
+ "pkg-config",
+ "vcpkg",
+]
+
 [[package]]
 name = "parking_lot"
 version = "0.12.1"
@@ -808,6 +882,12 @@ version = "0.1.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
 
+[[package]]
+name = "pkg-config"
+version = "0.3.25"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1df8c4ec4b0627e53bdf214615ad287367e482558cf84b109250b37464dc03ae"
+
 [[package]]
 name = "ppv-lite86"
 version = "0.2.16"
@@ -1169,6 +1249,12 @@ dependencies = [
  "percent-encoding",
 ]
 
+[[package]]
+name = "vcpkg"
+version = "0.2.15"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
+
 [[package]]
 name = "vcs-blog"
 version = "0.1.0"
@@ -1177,6 +1263,7 @@ dependencies = [
  "actix-web",
  "chrono",
  "fern",
+ "git2",
  "log",
  "pulldown-cmark",
  "walkdir",

+ 5 - 4
Cargo.toml

@@ -8,8 +8,9 @@ description = "Blog engine running on version control system to store articles"
 [dependencies]
 actix-web = "4"
 actix-files = "0.6.1"
-log = "0.4"
-fern = "0.6"
 chrono = "0.4"
-walkdir = "2.3.2"
-pulldown-cmark = "0.9"
+fern = "0.6"
+git2 = "0.14.4"
+log = "0.4"
+pulldown-cmark = "0.9"
+walkdir = "2.3.2"

+ 2 - 5
src/main.rs

@@ -1,8 +1,8 @@
 extern crate chrono;
 
 mod config;
-mod theme;
 mod repository;
+mod theme;
 
 use actix_web::middleware::Logger;
 use actix_web::{get, web, App, HttpResponse, HttpServer, Responder};
@@ -10,11 +10,8 @@ use fern;
 use std::{env, io};
 
 use crate::config::Configuration;
+use crate::repository::{ArticlesRepository, FsRepository};
 use crate::theme::Theme;
-use crate::repository::{
-    ArticlesRepository,
-    FsRepository
-};
 
 #[get("")]
 async fn show_articles_list() -> impl Responder {

+ 30 - 11
src/repository/git_repository.rs

@@ -1,33 +1,52 @@
-use git2::Repository;
+use git2;
 
 use crate::ArticlesRepository;
 
-pub struct GitRepository<'a> {
-    repo: &'a git2::Repository
+pub struct GitRepository {
+    repo: git2::Repository,
 }
 
-impl<'a> GitRepository<'a> {
-    fn new(repo: &'a git2::Repository) -> Self {
-        GitRepository {
-            repo
-        }
+impl GitRepository {
+    fn from_path(path: &str) -> Result<Self, git2::Error> {
+        let repo = git2::Repository::open(path)?;
+
+        Ok(GitRepository { repo })
     }
 }
 
 //Modification times: https://github.com/Shnatsel/rustsec/blob/f043c75731df311292a7e4f4dbb05c6728d055f5/rustsec/src/repository/git/modification_time.rs#L18
-impl<'a> ArticlesRepository for GitRepository<'a> {
+impl ArticlesRepository for GitRepository {
     fn get_directory_listing(&self, path: &str) -> Vec<String> {
-        vec![]
+        match self.repo.index() {
+            Ok(index) => index
+                .iter()
+                .map(|entry| String::from_utf8(entry.path).unwrap())
+                .filter(|entry| entry.ends_with(".md"))
+                .collect(),
+            Err(_) => Vec::new(),
+        }
     }
 }
 
 #[cfg(test)]
 mod tests_git_repository {
     use super::*;
+    use git2::IntoCString;
 
     #[test]
-    fn when_path_is_empty_should_return_nothing() {}
+    fn when_path_is_empty_should_return_nothing() {
+        let repo = GitRepository::from_path(".").expect("Should have stuff.");
+
+        for path in repo.get_directory_listing(".") {
+            println!("{path}");
+        }
+    }
 
+    // TODO: Change directory to something where there is no folders.
     #[test]
     fn when_path_is_valid_should_return_proper_listing() {}
+
+    // Blahblahblah
+    #[test]
+    fn when_fetching_file_informations_return_git_info() {}
 }

+ 2 - 2
src/repository/mod.rs

@@ -1,7 +1,7 @@
 pub mod articles_repository;
-pub mod git_repository;
 pub mod fs_repository;
+pub mod git_repository;
 
 pub use articles_repository::ArticlesRepository;
+pub use fs_repository::FsRepository;
 pub use git_repository::GitRepository;
-pub use fs_repository::FsRepository;