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