/* * Copyright (c) 2025 Emma Tebibyte * 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 * * 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, Option), // name, e-mail entries: Vec, hash: String, message: Option, short_hash: Option, time: i64, // seconds since Unix epoch } impl TryFrom> for GitCommit { type Error = Box; fn try_from(commit: Commit) -> Result { 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 { Ok(GitEntry::try_from(c)?) }).collect::, _>>()?; 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> for GitEntry { type Error = std::string::FromUtf8Error; fn try_from(entry: TreeEntry) -> Result { let path = String::from_utf8(entry.name_bytes().to_vec())?; Ok(Self { path }) } } pub struct GitBranch { commits: Vec, 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> for GitBranch { type Error = Box; fn try_from(branch: GitBranchWrapper) -> Result { 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> { Ok(GitCommit::try_from(repo.find_commit(o?)?)?) }).collect::, _>>()?; Ok(GitBranch { commits, name }) } } pub struct GitRepo { branches: Vec, name: String, owner: String, } impl GitRepo { fn open(path: PathBuf) -> Result> { let repo = Repository::open(path.clone())?; let branches = repo .branches(None)? .map(|b| -> Result> { Ok(GitBranch::try_from(GitBranchWrapper::new(b?.0, &repo))?) }).collect::, _>>()?; /* let mut revwalk = repo.revwalk()?; revwalk.set_sorting(Sort::TOPOLOGICAL | Sort::TIME)?; revwalk.push_head()?; let commits = revwalk .into_iter() .map(|o| -> Result> { Ok(GitCommit::try_from(repo.find_commit(o?)?)?) }).collect::, _>>()?; */ 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!(); } }