mirror of
https://forge.pointfixe.fr/hubert/gitust.git
synced 2026-02-04 18:47:29 +01:00
refactorisation in files
This commit is contained in:
28
src/gitutils/gitcommit.rs
Normal file
28
src/gitutils/gitcommit.rs
Normal file
@@ -0,0 +1,28 @@
|
||||
use std::fmt::{Display, Formatter};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum GitRef{
|
||||
Commit(String),
|
||||
Branch(String),
|
||||
Head,
|
||||
}
|
||||
|
||||
impl GitRef {
|
||||
pub fn new_commit(s : String) -> GitRef {
|
||||
GitRef::Commit(s)
|
||||
}
|
||||
|
||||
pub fn new_branch(s : String) -> GitRef {
|
||||
GitRef::Branch(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for GitRef {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{}", match &self {
|
||||
GitRef::Commit(s) => {s.as_str()}
|
||||
GitRef::Branch(s) => {s.as_str()}
|
||||
GitRef::Head => {"HEAD"}
|
||||
})
|
||||
}
|
||||
}
|
||||
14
src/gitutils/gitdir.rs
Normal file
14
src/gitutils/gitdir.rs
Normal file
@@ -0,0 +1,14 @@
|
||||
use std::fmt::{Display, Formatter};
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct GitDir<'a>{
|
||||
pub fullname: PathBuf,
|
||||
pub tree : git2::Tree<'a>,
|
||||
}
|
||||
|
||||
impl Display for GitDir<'_> {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{}", self.fullname.display())
|
||||
}
|
||||
}
|
||||
21
src/gitutils/gitfile.rs
Normal file
21
src/gitutils/gitfile.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
use std::fmt::{Display, Formatter};
|
||||
use std::path::{PathBuf};
|
||||
use std::ffi::OsStr;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct GitFile(pub PathBuf);
|
||||
|
||||
impl GitFile {
|
||||
pub(crate) fn new(fullname : PathBuf) -> GitFile {
|
||||
GitFile(fullname)
|
||||
}
|
||||
pub fn name(&self) -> Option<&OsStr> {
|
||||
self.0.file_name()
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for GitFile {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{}", self.0.display())
|
||||
}
|
||||
}
|
||||
81
src/gitutils/gitrepo.rs
Normal file
81
src/gitutils/gitrepo.rs
Normal file
@@ -0,0 +1,81 @@
|
||||
use std::ops::Add;
|
||||
|
||||
use git2::{ObjectType, Oid, Repository, BranchType};
|
||||
|
||||
use crate::git::GitBrowseEntry;
|
||||
use crate::gitutils::gitcommit::GitRef;
|
||||
use crate::gitutils::gitdir::GitDir;
|
||||
use crate::gitutils::gitfile::GitFile;
|
||||
use std::path::{PathBuf, Path};
|
||||
use std::str::FromStr;
|
||||
|
||||
pub struct GitRepo {
|
||||
git2 : Repository,
|
||||
}
|
||||
|
||||
impl GitRepo {
|
||||
|
||||
pub fn new(path : &str) -> Result<GitRepo, git2::Error> {
|
||||
Repository::open(path).map(|git2| GitRepo{git2})
|
||||
}
|
||||
|
||||
pub fn get_tree<'a, 'b, 'c> (&'a self, commit : &'b GitRef, path : &'c Path) -> Result<GitDir<'a>, git2::Error> {
|
||||
let commit = match commit {
|
||||
GitRef::Commit(commit) => {
|
||||
// println!("commit : {}", commit.as_str());
|
||||
let oid = Oid::from_str(commit.as_str())?;
|
||||
// println!("{}", oid);
|
||||
self.git2.find_commit(oid)?
|
||||
}
|
||||
GitRef::Branch(branch) => {
|
||||
let branch = self.git2.find_branch(branch.as_str(), BranchType::Local)?;
|
||||
branch.get().peel_to_commit()?
|
||||
}
|
||||
GitRef::Head => {
|
||||
// println!("coucou1");
|
||||
let head = self.git2.head()?;
|
||||
// println!("coucou");
|
||||
head.peel_to_commit()?
|
||||
}
|
||||
};
|
||||
let root_tree = commit.tree()?;
|
||||
let tree = if path.as_os_str().len() == 0 {
|
||||
root_tree
|
||||
} else {
|
||||
let oid1 = root_tree.get_path(path)?.id();
|
||||
// println!("{}", oid1);
|
||||
self.git2.find_tree(oid1)?
|
||||
};
|
||||
let fullname = path.to_path_buf();
|
||||
Ok(GitDir { fullname, tree })
|
||||
}
|
||||
|
||||
pub async fn browse<'a>(&'a self, dir: &GitDir<'a>) -> Result<Vec<GitBrowseEntry<'a>>, git2::Error> {
|
||||
let mut res = Vec::new();
|
||||
for entry in dir.tree.iter() {
|
||||
match entry.kind() {
|
||||
None => {Err(git2::Error::from_str("each tree entry must be well defined"))?;}
|
||||
Some(kind) => match kind {
|
||||
ObjectType::Any => {Err(git2::Error::from_str("tree entry cannot be of kind Any"))?;}
|
||||
ObjectType::Commit => {Err(git2::Error::from_str("tree entry cannot be of kind Commit"))?;}
|
||||
ObjectType::Tree => {
|
||||
let name = entry.name().ok_or(git2::Error::from_str("entry must have valid utf8 name"))?;
|
||||
let mut fullname = dir.fullname.clone();
|
||||
fullname.push(name);
|
||||
let subtree = self.git2.find_tree(entry.id())?;
|
||||
res.push(GitBrowseEntry::EGitDir(GitDir {fullname, tree : subtree}));
|
||||
}
|
||||
ObjectType::Blob => {
|
||||
let name = entry.name().ok_or(git2::Error::from_str("entry must have valid utf8 name"))?;
|
||||
let mut fullname = dir.fullname.clone();
|
||||
fullname.push(name);
|
||||
res.push(GitBrowseEntry::EGitFile(GitFile::new(fullname)));
|
||||
}
|
||||
ObjectType::Tag => {Err(git2::Error::from_str("tree entry cannot be of kind tag"))?;}
|
||||
}
|
||||
};
|
||||
}
|
||||
return Ok(res);
|
||||
}
|
||||
|
||||
}
|
||||
4
src/gitutils/mod.rs
Normal file
4
src/gitutils/mod.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
pub mod gitdir;
|
||||
pub mod gitrepo;
|
||||
pub mod gitcommit;
|
||||
pub mod gitfile;
|
||||
Reference in New Issue
Block a user