mintee/src/git.rs

201 lines
5.6 KiB
Rust

/*
* Copyright (c) 2025 Emma Tebibyte <emma@tebibyte.media>
* SPDX-License-Identifier: AGPL-3.0-or-later
*
* This file is part of Mintee.
*
* Mintee is free software: you can redistribute it and/or modify it under the
* terms of the GNU Affero General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* Mintee is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Mintee. If not, see https://www.gnu.org/licenses/.
*
* This file incorporates work covered by the following copyright and
* permission notice:
*
* MIT License
*
* Copyright (c) 2021 Sergey "Shnatsel" Davidoff <shnatsel@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
use std::{
error::Error,
path::PathBuf,
};
use git2::{ Branch, Commit, Repository, Sort, TreeEntry };
pub struct GitCommit {
author: (Option<String>, Option<String>), // name, e-mail
entries: Vec<GitEntry>,
hash: String,
message: Option<String>,
short_hash: Option<String>,
time: i64, // seconds since Unix epoch
}
impl TryFrom<Commit<'_>> for GitCommit {
type Error = Box<dyn Error>;
fn try_from(commit: Commit) -> Result<Self, Self::Error> {
let hash = commit.id().to_string();
let short_hash = commit.as_object().short_id()?.as_str().map(|f| f.to_owned());
let message = commit.message().map(|f| f.to_owned());
let commit_signature = commit.author();
let name = commit_signature.name().map(|f| f.to_owned());
let address = commit_signature.email().map(|f| f.to_owned());
let author = (name, address);
let time = commit.time().seconds();
let entries = commit
.tree()?
.iter()
.map(|c| -> Result<GitEntry, std::string::FromUtf8Error> {
Ok(GitEntry::try_from(c)?)
}).collect::<Result<Vec<_>, _>>()?;
Ok(GitCommit { author, entries, hash, message, short_hash, time })
}
}
pub struct GitEntry {
path: String,
}
impl GitEntry {
fn new(path: String) -> Self {
GitEntry { path }
}
}
impl TryFrom<TreeEntry<'_>> for GitEntry {
type Error = std::string::FromUtf8Error;
fn try_from(entry: TreeEntry) -> Result<Self, Self::Error> {
let path = String::from_utf8(entry.name_bytes().to_vec())?;
Ok(Self { path })
}
}
pub struct GitBranch {
commits: Vec<GitCommit>,
name: String,
}
struct GitBranchWrapper<'a> {
branch: Branch<'a>,
repo: &'a Repository,
}
impl<'a> GitBranchWrapper<'a> {
fn new(branch: Branch<'a>, repo: &'a Repository) -> Self {
Self { branch, repo }
}
}
impl TryFrom<GitBranchWrapper<'_>> for GitBranch {
type Error = Box<dyn Error>;
fn try_from(branch: GitBranchWrapper) -> Result<Self, Self::Error> {
let name = String::from_utf8(branch.branch.name_bytes()?.to_vec())?;
let repo = branch.repo;
let branch_oid = branch.branch.get().target().unwrap();
let mut revwalk = repo.revwalk()?;
revwalk.set_sorting(Sort::TOPOLOGICAL | Sort::TIME)?;
revwalk.push(branch_oid)?;
let commits = revwalk
.into_iter()
.map(|o| -> Result<GitCommit, Box<dyn Error>> {
Ok(GitCommit::try_from(repo.find_commit(o?)?)?)
}).collect::<Result<Vec<_>, _>>()?;
Ok(GitBranch { commits, name })
}
}
pub struct GitRepo {
branches: Vec<GitBranch>,
name: String,
owner: String,
}
impl GitRepo {
fn open(path: PathBuf) -> Result<Self, Box<dyn Error>> {
let repo = Repository::open(path.clone())?;
let branches = repo
.branches(None)?
.map(|b| -> Result<GitBranch, Box<dyn Error>> {
Ok(GitBranch::try_from(GitBranchWrapper::new(b?.0, &repo))?)
}).collect::<Result<Vec<_>, _>>()?;
/*
let mut revwalk = repo.revwalk()?;
revwalk.set_sorting(Sort::TOPOLOGICAL | Sort::TIME)?;
revwalk.push_head()?;
let commits = revwalk
.into_iter()
.map(|o| -> Result<GitCommit, Box<dyn Error>> {
Ok(GitCommit::try_from(repo.find_commit(o?)?)?)
}).collect::<Result<Vec<_>, _>>()?;
*/
let full_path = path.clone().as_path().canonicalize()?;
let relative_path = full_path.strip_prefix("/var/mintee/repos")?;
let owner = String::from_utf8(
relative_path
.parent()
.unwrap()
.to_path_buf()
.as_mut_os_string()
.as_encoded_bytes()
.to_vec()
)?;
let name = String::from_utf8(
relative_path
.file_name()
.unwrap()
.to_owned()
.as_encoded_bytes()
.to_vec()
)?;
Ok(GitRepo { branches, name, owner })
}
fn get_remote_repo() { // for AP repos
todo!();
}
}