it compiles

This commit is contained in:
Vladan Popovic 2023-05-18 01:53:40 +02:00
parent b1dd07aed1
commit 4dc14db293
12 changed files with 115 additions and 13 deletions

View File

@ -5,7 +5,9 @@ authors = ["Vladan Popovic <vladanovic@gmail.com>"]
edition = "2018"
[dependencies]
axum = "0.6.18"
chrono = { version = "*" }
tokio = { version = "1.28.1", features = ["tokio-macros"] }
[dependencies.git2]
version = "*"

View File

@ -1,4 +1,4 @@
use crate::errors::SocialError;
use crate::git::error::SocialError;
use crate::git::repo::SocialRepo;
use crate::git::{SocialBlame, SocialBlameHunk};
use git2::{Blame, BlameHunk, BlameOptions};

View File

@ -1,4 +1,4 @@
use crate::errors::SocialError;
use crate::git::error::SocialError;
use crate::git::repo::SocialRepo;
use crate::git::SocialDiff;
use git2::{Diff, DiffOptions};

View File

@ -1,4 +1,4 @@
use crate::errors::SocialError;
use crate::git::error::SocialError;
use crate::git::repo::SocialRepo;
use crate::git::SocialCommit;
@ -63,13 +63,13 @@ impl Log for SocialRepo {
.take(1)
.next()
.map(|r| {
r.map_err(|_| SocialError {
message: "Invalid commit reference!".to_string(),
r.map_err(|err| SocialError {
message: format!("Invalid commit! {}", err)
})
.map(|c| c.into())
})
.ok_or(SocialError {
message: "Repo doesn't contain any commits!".to_owned(),
message: format!("Repo doesn't contain any commits!")
})?
}
}

View File

@ -1,5 +1,6 @@
pub(crate) mod blame;
pub(crate) mod diff;
pub(crate) mod error;
pub(crate) mod log;
pub(crate) mod references;
pub(crate) mod repo;

67
src/git/reference.rs Normal file
View File

@ -0,0 +1,67 @@
use crate::git::log::into_social_commit;
use crate::git::repository::SocialRepo;
use crate::git::Error;
use git2::{Branch, BranchType, Commit};
use shared::{SocialBranch, SocialCommit, SocialTag};
fn into_social_branch(b: Branch<'_>) -> SocialBranch {
SocialBranch {
name: b.name().unwrap_or(Some("")).unwrap_or("").to_owned(),
head: SocialCommit::default(),
}
}
fn into_social_tag(name: Option<&str>) -> SocialTag {
SocialTag {
name: name.unwrap_or("").to_owned(),
head: SocialCommit::default(),
annotation: None,
}
}
pub trait Refs {
fn branches(&self) -> Result<Vec<SocialBranch>, Error>;
fn tags(&self) -> Result<Vec<SocialTag>, Error>;
}
impl Refs for SocialRepo {
fn branches(&self) -> Result<Vec<SocialBranch>, Error> {
let all_branches = self.repo.branches(Some(BranchType::Local))?;
let mut resulting_brances: Vec<SocialBranch> = vec![];
for r in all_branches {
r.map(|(branch, _)| {
let mut sb = into_social_branch(branch);
self.repo
.revparse_single(&sb.name)
.map(|obj| {
obj.peel_to_commit()
.map(|c: Commit| {
sb.head = into_social_commit(c);
resulting_brances.push(sb);
})
.unwrap_or(());
})
.unwrap_or(());
})
.unwrap_or(());
}
Ok(resulting_brances)
}
fn tags(&self) -> Result<Vec<SocialTag>, Error> {
let tagnames = self.repo.tag_names(None)?;
let mut result: Vec<SocialTag> = vec![];
for t in tagnames.into_iter() {
let mut st = into_social_tag(t);
let _ = self.repo.revparse_single(&st.name).map(|obj| {
obj.peel_to_commit().map(|c: Commit| {
st.head = into_social_commit(c);
result.push(st);
})
});
}
Ok(result)
}
}

View File

@ -1,4 +1,4 @@
use crate::errors::SocialError;
use crate::git::error::SocialError;
use crate::git::repo::SocialRepo;
use crate::git::{SocialBranch, SocialCommit, SocialTag};
use git2::{Branch, BranchType, Commit};

View File

@ -1,4 +1,4 @@
use crate::errors::SocialError;
use crate::git::error::SocialError;
use git2::{Error as GitError, Repository, Tree};
use std::{fs::read_to_string, path::Path};
@ -22,6 +22,7 @@ impl<'repo> SocialRepo {
.and_then(|obj| obj.peel_to_tree())
}
#[allow(dead_code)]
pub fn get_description(&self) -> String {
// The description is in a file named "description",
// in the .git directory, or the root directory if bare.

34
src/git/repository.rs Normal file
View File

@ -0,0 +1,34 @@
use crate::errors::SocialError;
use git2::{Error as GitError, Repository, Tree};
use std::{fs::read_to_string, path::Path};
pub struct SocialRepo {
pub repo: Repository,
}
unsafe impl Send for SocialRepo {}
unsafe impl Sync for SocialRepo {}
impl SocialRepo {
pub fn new(path: &str) -> Result<Self, SocialError> {
Repository::open(path)
.map(|repo| Self { repo })
.map_err(|_| {
let err = "fatal: not a git repository";
SocialError::from(err)
})
}
pub fn get_tree(&self, rev: &str) -> Result<Tree, GitError> {
self.repo
.revparse_single(rev)
.and_then(|obj| obj.peel_to_tree())
}
pub fn get_description(&self) -> String {
// The description is in a file named "description",
// in the .git directory, or the root directory if bare.
let git_path = self.repo.path().join(Path::new("description"));
read_to_string(git_path).unwrap_or("".to_string())
}
}

View File

@ -1,4 +1,4 @@
use crate::errors::SocialError;
use crate::git::error::SocialError;
use crate::git::repo::SocialRepo;
use crate::git::{SocialFileContent, SocialTreeEntry};
use git2::{Blob, Error as GitError, Object, ObjectType, TreeEntry};
@ -65,9 +65,7 @@ impl LsTree for SocialRepo {
})
.and_then(|blob: &Blob| {
String::from_utf8(blob.content().to_vec()).map_err(|_| {
GitError::from_str(
format!("Cannot read file {}", path.display()).as_ref(),
)
GitError::from_str(format!("Cannot read file {}", path.display()).as_ref())
})
})
.map(|content| SocialFileContent {

View File

@ -1,4 +1,3 @@
mod errors;
mod git;
use git::repo::SocialRepo;