partially moves backend repo logic out of render code
This commit is contained in:
@@ -44,30 +44,15 @@
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
use std::{
|
||||
env::current_dir,
|
||||
fs::metadata,
|
||||
path::PathBuf,
|
||||
};
|
||||
use std::path::PathBuf;
|
||||
|
||||
use git2::{ Commit, Repository, Sort };
|
||||
use serde::Serialize;
|
||||
use git2::Repository;
|
||||
use tera::{ Context, Tera };
|
||||
|
||||
trait ToContext {
|
||||
fn to_context(self) -> Result<Context, git2::Error>;
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct Entry {
|
||||
class: String,
|
||||
committer: String,
|
||||
last_commit: String,
|
||||
last_commit_short: String,
|
||||
last_commit_time: i64,
|
||||
path: String,
|
||||
}
|
||||
|
||||
impl ToContext for Repository {
|
||||
fn to_context(self) -> Result<Context, git2::Error> {
|
||||
let repo = self.commondir();
|
||||
@@ -99,62 +84,6 @@ impl ToContext for Repository {
|
||||
}
|
||||
}
|
||||
|
||||
impl Entry {
|
||||
fn new(commit: &Commit, path: String) -> Result<Self, git2::Error> {
|
||||
let commit_id = commit.id();
|
||||
let ft = metadata(&path).unwrap().file_type();
|
||||
let class: String;
|
||||
|
||||
if ft.is_dir() {
|
||||
class = "directory".to_owned();
|
||||
} else {
|
||||
class = "file".to_owned();
|
||||
}
|
||||
|
||||
Ok(Entry {
|
||||
class,
|
||||
committer: commit
|
||||
.committer()
|
||||
.name()
|
||||
.unwrap_or("")
|
||||
.to_owned(),
|
||||
last_commit: commit_id.to_string(),
|
||||
last_commit_short: commit.as_object().short_id()?.as_str().unwrap_or("").to_owned(),
|
||||
last_commit_time: commit.time().seconds(),
|
||||
path,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn get_entries(repo: &Repository) -> Result<Vec<Entry>, git2::Error> {
|
||||
let mut entries = Vec::new();
|
||||
let mut revwalk = repo.revwalk()?;
|
||||
|
||||
revwalk.set_sorting(Sort::TOPOLOGICAL | Sort::TIME)?;
|
||||
revwalk.push_head()?;
|
||||
|
||||
for commit_id in revwalk {
|
||||
let commit_id = commit_id?;
|
||||
let commit = repo.find_commit(commit_id)?;
|
||||
if commit.parent_count() <= 1 {
|
||||
let tree = commit.tree()?;
|
||||
let prev_tree = match commit.parent_count() {
|
||||
1 => Some(commit.parent(0)?.tree()?),
|
||||
0 => None,
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
let diff = repo.diff_tree_to_tree(prev_tree.as_ref(), Some(&tree), None)?;
|
||||
for delta in diff.deltas() {
|
||||
if let Some(file_path) = delta.new_file().path() {
|
||||
entries.push(Entry::new(&commit, file_path.to_str().unwrap_or("Invalid UTF-8").to_owned())?);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(entries)
|
||||
}
|
||||
|
||||
fn render_path(path: PathBuf) -> tera::Result<String> {
|
||||
let tera = Tera::new("./assets/templates/repo/*")?;
|
||||
let repo = Repository::discover(path).unwrap();
|
||||
|
||||
Reference in New Issue
Block a user