| 1 |
use std::{ |
| 2 |
ffi::OsStr, |
| 3 |
path::{Path, PathBuf}, |
| 4 |
}; |
| 5 |
|
| 6 |
use anyhow::Context; |
| 7 |
use flate2::{Compression, write::GzEncoder}; |
| 8 |
use gix::objs::tree::EntryKind; |
| 9 |
use tar::Builder; |
| 10 |
|
| 11 |
use crate::{ |
| 12 |
error::Error, |
| 13 |
git::{ |
| 14 |
refs::Ref, |
| 15 |
tree::{self, Blob, PathView}, |
| 16 |
}, |
| 17 |
}; |
| 18 |
|
| 19 |
#[derive(Clone)] |
| 20 |
pub struct Repository { |
| 21 |
pub name: String, |
| 22 |
pub description: String, |
| 23 |
pub owner: Option<String>, |
| 24 |
pub path: PathBuf, |
| 25 |
pub gix: gix::ThreadSafeRepository, |
| 26 |
} |
| 27 |
|
| 28 |
impl Repository { |
| 29 |
pub fn open(path: &Path) -> Result<Self, Error> { |
| 30 |
let name = path |
| 31 |
.file_name() |
| 32 |
.and_then(OsStr::to_str) |
| 33 |
.ok_or(Error::Failed)?; |
| 34 |
|
| 35 |
let raw_desc = std::fs::read(path.join("description")).unwrap_or_default(); |
| 36 |
let description = match String::from_utf8(raw_desc) { |
| 37 |
Ok(s) if !s.is_empty() => s, |
| 38 |
Ok(_) => "There is no description!".to_string(), |
| 39 |
Err(_) => "Failed to get the description!".to_string(), |
| 40 |
}; |
| 41 |
|
| 42 |
let gix = gix::ThreadSafeRepository::open(path).map_err(|_| Error::Failed)?; |
| 43 |
let owner = gix |
| 44 |
.to_thread_local() |
| 45 |
.config_snapshot() |
| 46 |
.string("gitweb.owner") |
| 47 |
.map(|v| v.to_string()); |
| 48 |
|
| 49 |
Ok(Self { |
| 50 |
name: name.to_string(), |
| 51 |
description, |
| 52 |
owner, |
| 53 |
path: path.to_path_buf(), |
| 54 |
gix, |
| 55 |
}) |
| 56 |
} |
| 57 |
|
| 58 |
pub fn gix(&self) -> gix::Repository { |
| 59 |
self.gix.to_thread_local() |
| 60 |
} |
| 61 |
|
| 62 |
pub fn readme(&self, current_ref: &Ref) -> Option<String> { |
| 63 |
let gix_repo = self.gix(); |
| 64 |
let tree = Self::root_tree(&gix_repo, current_ref, None) |
| 65 |
.or_else(|_| { |
| 66 |
gix_repo |
| 67 |
.head() |
| 68 |
.ok() |
| 69 |
.and_then(|mut h| h.peel_to_commit().ok()) |
| 70 |
.and_then(|c| c.tree().ok()) |
| 71 |
.context("no head tree") |
| 72 |
}) |
| 73 |
.ok()?; |
| 74 |
|
| 75 |
for entry in tree.iter().flatten() { |
| 76 |
if entry |
| 77 |
.filename() |
| 78 |
.to_string() |
| 79 |
.to_ascii_lowercase() |
| 80 |
.starts_with("readme") |
| 81 |
{ |
| 82 |
let blob = entry.object().ok()?.into_blob(); |
| 83 |
return Some(String::from_utf8_lossy(&blob.data).into_owned()); |
| 84 |
} |
| 85 |
} |
| 86 |
None |
| 87 |
} |
| 88 |
|
| 89 |
pub fn archive( |
| 90 |
&self, |
| 91 |
current_ref: &Ref, |
| 92 |
commit: &Option<String>, |
| 93 |
) -> Result<(Vec<u8>, String), anyhow::Error> { |
| 94 |
let gix_repo = self.gix(); |
| 95 |
let tree = Self::root_tree(&gix_repo, current_ref, commit.as_deref())?; |
| 96 |
let filename = match commit { |
| 97 |
Some(c) => format!("{c}.tar.gz"), |
| 98 |
None => format!("{}.tar.gz", current_ref.name.replace('/', "-")), |
| 99 |
}; |
| 100 |
|
| 101 |
let encoder = GzEncoder::new(Vec::new(), Compression::default()); |
| 102 |
let mut builder = Builder::new(encoder); |
| 103 |
|
| 104 |
Self::add_tree_to_archive(&tree, "", &mut builder)?; |
| 105 |
|
| 106 |
let bytes = builder.into_inner()?.finish()?; |
| 107 |
Ok((bytes, filename)) |
| 108 |
} |
| 109 |
|
| 110 |
fn add_tree_to_archive( |
| 111 |
tree: &gix::Tree<'_>, |
| 112 |
prefix: &str, |
| 113 |
builder: &mut Builder<GzEncoder<Vec<u8>>>, |
| 114 |
) -> anyhow::Result<()> { |
| 115 |
for entry in tree.iter() { |
| 116 |
let entry = entry?; |
| 117 |
let mode = entry.mode(); |
| 118 |
let name = entry.filename().to_string(); |
| 119 |
let path = if prefix.is_empty() { |
| 120 |
name |
| 121 |
} else { |
| 122 |
format!("{prefix}/{name}") |
| 123 |
}; |
| 124 |
|
| 125 |
if mode.is_tree() { |
| 126 |
let subtree = entry.object()?.into_tree(); |
| 127 |
Self::add_tree_to_archive(&subtree, &path, builder)?; |
| 128 |
} else if mode.is_blob() || mode.is_executable() { |
| 129 |
let blob = entry.object()?.into_blob(); |
| 130 |
let data = &blob.data; |
| 131 |
|
| 132 |
let mut header = tar::Header::new_gnu(); |
| 133 |
header.set_size(data.len() as u64); |
| 134 |
header.set_mode(if mode.is_executable() { 0o755 } else { 0o644 }); |
| 135 |
header.set_cksum(); |
| 136 |
|
| 137 |
builder.append_data(&mut header, &path, &data[..])?; |
| 138 |
} |
| 139 |
} |
| 140 |
Ok(()) |
| 141 |
} |
| 142 |
|
| 143 |
pub fn resolve_path( |
| 144 |
&self, |
| 145 |
current_ref: &Ref, |
| 146 |
commit_id: Option<&str>, |
| 147 |
path: &str, |
| 148 |
) -> anyhow::Result<PathView> { |
| 149 |
let gix_repo = self.gix(); |
| 150 |
let root_tree = Self::root_tree(&gix_repo, current_ref, commit_id)?; |
| 151 |
let path = path.trim_matches('/'); |
| 152 |
|
| 153 |
if path.is_empty() { |
| 154 |
return Ok(PathView::Tree(tree::read_entries(&root_tree, "")?)); |
| 155 |
} |
| 156 |
|
| 157 |
let entry = Self::lookup_entry(&root_tree, path)?; |
| 158 |
let mode = entry.mode(); |
| 159 |
|
| 160 |
match mode.kind() { |
| 161 |
EntryKind::Tree => { |
| 162 |
let subtree = entry.object()?.into_tree(); |
| 163 |
Ok(PathView::Tree(tree::read_entries(&subtree, path)?)) |
| 164 |
} |
| 165 |
EntryKind::Blob | EntryKind::BlobExecutable => { |
| 166 |
let blob = entry.object()?.into_blob(); |
| 167 |
let lines = std::str::from_utf8(&blob.data) |
| 168 |
.ok() |
| 169 |
.map(|s| crate::syntax_highlight::highlight_lines(s, path)); |
| 170 |
|
| 171 |
Ok(PathView::Blob(Blob { |
| 172 |
id: entry.oid().to_string(), |
| 173 |
mode: mode.value() as u32, |
| 174 |
size: blob.data.len() as u64, |
| 175 |
lines, |
| 176 |
})) |
| 177 |
} |
| 178 |
EntryKind::Link => anyhow::bail!("symlinks are not yet supported for direct viewing"), |
| 179 |
EntryKind::Commit => { |
| 180 |
anyhow::bail!("path resolves to a submodule, not a viewable file/dir") |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
pub fn read_raw_blob( |
| 186 |
&self, |
| 187 |
current_ref: &Ref, |
| 188 |
commit_id: Option<&str>, |
| 189 |
path: &str, |
| 190 |
) -> anyhow::Result<Option<Vec<u8>>> { |
| 191 |
let path = path.trim_matches('/'); |
| 192 |
if path.is_empty() { |
| 193 |
return Ok(None); |
| 194 |
} |
| 195 |
|
| 196 |
let gix_repo = self.gix(); |
| 197 |
let root_tree = Self::root_tree(&gix_repo, current_ref, commit_id)?; |
| 198 |
let entry = Self::lookup_entry(&root_tree, path)?; |
| 199 |
|
| 200 |
match entry.mode().kind() { |
| 201 |
EntryKind::Tree => Ok(None), |
| 202 |
EntryKind::Blob | EntryKind::BlobExecutable => { |
| 203 |
let blob = entry.object()?.into_blob(); |
| 204 |
Ok(Some(blob.detach().data)) |
| 205 |
} |
| 206 |
EntryKind::Link => anyhow::bail!("symlinks are not yet supported for direct viewing"), |
| 207 |
EntryKind::Commit => { |
| 208 |
anyhow::bail!("path resolves to a submodule, not a viewable file/dir") |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
fn root_tree<'a>( |
| 214 |
gix_repo: &'a gix::Repository, |
| 215 |
current_ref: &Ref, |
| 216 |
commit_id: Option<&str>, |
| 217 |
) -> anyhow::Result<gix::Tree<'a>> { |
| 218 |
if let Some(id) = commit_id { |
| 219 |
let oid = id.parse::<gix::ObjectId>()?; |
| 220 |
return Ok(gix_repo.find_commit(oid)?.tree()?); |
| 221 |
} |
| 222 |
gix_repo |
| 223 |
.find_reference(¤t_ref.name)? |
| 224 |
.peel_to_tree() |
| 225 |
.context("failed to get root tree") |
| 226 |
} |
| 227 |
|
| 228 |
fn lookup_entry<'a>( |
| 229 |
root_tree: &gix::Tree<'a>, |
| 230 |
path: &str, |
| 231 |
) -> anyhow::Result<gix::object::tree::Entry<'a>> { |
| 232 |
root_tree |
| 233 |
.lookup_entry_by_path(path) |
| 234 |
.with_context(|| format!("failed lookup for path: {path}"))? |
| 235 |
.with_context(|| format!("path not found: {path}")) |
| 236 |
} |
| 237 |
} |