mirror of
https://seed.radicle.dev/rad:z6cFWeWpnZNHh9rUW8phgA3b5yGt.git
synced 2026-11-16 08:10:41 +08:00
radicle-surf: refactor source objects
Refactor the source data types to reuse existing logic and update to the latest protocol. The `Blob` and `Tree` data types are updated to be backed by `File` and `Directory` respectively. This allows the construction of them to be much simpler and reuse any existing logic on those types. This also lead to the removal of `ObjectType` and `Info`. The `Revision` type was removed in favour of using the `Revision` trait. The `Revisions` type was renamed to `View`, while also renaming the `revision` module to `view`. A `View` is a simple encoding of what a peer's "view" would look like, i.e. what references do they have. This also required the introduction of getting free-form categories in the reference db, i.e. `refs/<category>`, and so a `Categories` iterator was also introduced. Signed-off-by: Fintan Halpenny <fintan.halpenny@gmail.com> X-Clacks-Overhead: GNU Terry Pratchett
This commit is contained in:
@@ -29,7 +29,7 @@ use serde::{
|
||||
use crate::{
|
||||
diff,
|
||||
git::{self, glob, Glob, Repository},
|
||||
source::{person::Person, revision::Revision},
|
||||
source::person::Person,
|
||||
};
|
||||
|
||||
use radicle_git_ext::Oid;
|
||||
@@ -227,18 +227,13 @@ pub fn header(repo: &Repository, sha1: Oid) -> Result<Header, Error> {
|
||||
///
|
||||
/// Will return [`Error`] if the project doesn't exist or the surf interaction
|
||||
/// fails.
|
||||
pub fn commits(repo: &Repository, maybe_revision: Option<Revision>) -> Result<Commits, Error> {
|
||||
let rev = match maybe_revision {
|
||||
Some(revision) => revision,
|
||||
None => Revision::Sha {
|
||||
sha: repo.head_oid()?,
|
||||
},
|
||||
};
|
||||
|
||||
let stats = repo.get_commit_stats(&rev)?;
|
||||
let commits: Result<Vec<git::Commit>, git::Error> = repo.history(&rev)?.collect();
|
||||
let headers = commits?.iter().map(Header::from).collect();
|
||||
|
||||
pub fn commits<R>(repo: &Repository, revision: &R) -> Result<Commits, Error>
|
||||
where
|
||||
R: git::Revision,
|
||||
{
|
||||
let stats = repo.get_commit_stats(revision)?;
|
||||
let commits = repo.history(revision)?.collect::<Result<Vec<_>, _>>()?;
|
||||
let headers = commits.into_iter().map(Header::from).collect();
|
||||
Ok(Commits { headers, stats })
|
||||
}
|
||||
|
||||
|
||||
@@ -20,68 +20,13 @@
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
use serde::{
|
||||
ser::{SerializeStruct as _, Serializer},
|
||||
Serialize,
|
||||
};
|
||||
|
||||
pub mod blob;
|
||||
pub use blob::{Blob, BlobContent};
|
||||
|
||||
pub mod tree;
|
||||
pub use tree::{tree, Tree, TreeEntry};
|
||||
pub use tree::{Tree, TreeEntry};
|
||||
|
||||
use crate::{file_system::directory, git, source::commit};
|
||||
|
||||
/// Git object types.
|
||||
///
|
||||
/// `shafiul.github.io/gitbook/1_the_git_object_model.html`
|
||||
#[derive(Debug, Eq, Ord, PartialOrd, PartialEq)]
|
||||
pub enum ObjectType {
|
||||
/// References a list of other trees and blobs.
|
||||
Tree,
|
||||
/// Used to store file data.
|
||||
Blob,
|
||||
}
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
impl Serialize for ObjectType {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
match self {
|
||||
Self::Blob => serializer.serialize_unit_variant("ObjectType", 0, "BLOB"),
|
||||
Self::Tree => serializer.serialize_unit_variant("ObjectType", 1, "TREE"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Set of extra information we carry for blob and tree objects returned from
|
||||
/// the API.
|
||||
pub struct Info {
|
||||
/// Name part of an object.
|
||||
pub name: String,
|
||||
/// The type of the object.
|
||||
pub object_type: ObjectType,
|
||||
/// The last commmit that touched this object.
|
||||
pub last_commit: Option<commit::Header>,
|
||||
}
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
impl Serialize for Info {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
let mut state = serializer.serialize_struct("Info", 3)?;
|
||||
state.serialize_field("name", &self.name)?;
|
||||
state.serialize_field("objectType", &self.object_type)?;
|
||||
state.serialize_field("lastCommit", &self.last_commit)?;
|
||||
state.end()
|
||||
}
|
||||
}
|
||||
use crate::{file_system::directory, git};
|
||||
|
||||
/// An error reported by object types.
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
@@ -89,6 +34,9 @@ pub enum Error {
|
||||
#[error(transparent)]
|
||||
Directory(#[from] directory::error::Directory),
|
||||
|
||||
#[error(transparent)]
|
||||
File(#[from] directory::error::File),
|
||||
|
||||
/// An error occurred during a git operation.
|
||||
#[error(transparent)]
|
||||
Git(#[from] git::Error),
|
||||
|
||||
@@ -18,10 +18,7 @@
|
||||
//! Represents git object type 'blob', i.e. actual file contents.
|
||||
//! See git [doc](https://git-scm.com/book/en/v2/Git-Internals-Git-Objects) for more details.
|
||||
|
||||
use std::{
|
||||
path::{Path, PathBuf},
|
||||
str,
|
||||
};
|
||||
use std::{path::Path, str};
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
use serde::{
|
||||
@@ -30,28 +27,63 @@ use serde::{
|
||||
};
|
||||
|
||||
use crate::{
|
||||
git::Repository,
|
||||
source::{
|
||||
commit,
|
||||
object::{Error, Info, ObjectType},
|
||||
revision::Revision,
|
||||
},
|
||||
file_system::{File, FileContent},
|
||||
git::{self, Repository},
|
||||
source::{commit, object::Error},
|
||||
};
|
||||
|
||||
#[cfg(feature = "syntax")]
|
||||
use crate::syntax;
|
||||
|
||||
/// File data abstraction.
|
||||
pub struct Blob {
|
||||
/// Actual content of the file, if the content is ASCII.
|
||||
pub file: File,
|
||||
pub content: BlobContent,
|
||||
/// Extra info for the file.
|
||||
pub info: Info,
|
||||
/// Absolute path to the object from the root of the repo.
|
||||
pub path: PathBuf,
|
||||
pub commit: Option<commit::Header>,
|
||||
}
|
||||
|
||||
impl Blob {
|
||||
/// Returns the [`Blob`] for a file at `revision` under `path`.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Will return [`Error`] if the project doesn't exist or a surf interaction
|
||||
/// fails.
|
||||
pub fn new<P, R>(repo: &Repository, revision: &R, path: &P) -> Result<Blob, Error>
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
R: git::Revision,
|
||||
{
|
||||
Self::make_blob(repo, revision, path, |c| BlobContent::from(c))
|
||||
}
|
||||
|
||||
fn make_blob<P, R>(
|
||||
repo: &Repository,
|
||||
revision: &R,
|
||||
path: &P,
|
||||
content: impl FnOnce(FileContent) -> BlobContent,
|
||||
) -> Result<Blob, Error>
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
R: git::Revision,
|
||||
{
|
||||
let path = path.as_ref();
|
||||
let root = repo.root_dir(revision)?;
|
||||
|
||||
let file = root
|
||||
.find_file(&path, repo)?
|
||||
.ok_or_else(|| Error::PathNotFound(path.to_path_buf()))?;
|
||||
|
||||
let last_commit = repo
|
||||
.last_commit(path, revision)?
|
||||
.map(|c| commit::Header::from(&c));
|
||||
|
||||
let content = content(file.content(repo)?);
|
||||
|
||||
Ok(Blob {
|
||||
file,
|
||||
content,
|
||||
commit: last_commit,
|
||||
})
|
||||
}
|
||||
|
||||
/// Indicates if the content of the [`Blob`] is binary.
|
||||
#[must_use]
|
||||
pub fn is_binary(&self) -> bool {
|
||||
@@ -71,12 +103,14 @@ impl Serialize for Blob {
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
let mut state = serializer.serialize_struct("Blob", 5)?;
|
||||
const FIELDS: usize = 6;
|
||||
let mut state = serializer.serialize_struct("Blob", FIELDS)?;
|
||||
state.serialize_field("binary", &self.is_binary())?;
|
||||
state.serialize_field("html", &self.is_html())?;
|
||||
state.serialize_field("content", &self.content)?;
|
||||
state.serialize_field("info", &self.info)?;
|
||||
state.serialize_field("path", &self.path)?;
|
||||
state.serialize_field("lastCommit", &self.commit)?;
|
||||
state.serialize_field("name", &self.file.name())?;
|
||||
state.serialize_field("path", &self.file.location())?;
|
||||
state.end()
|
||||
}
|
||||
}
|
||||
@@ -88,9 +122,9 @@ pub enum BlobContent {
|
||||
Plain(String),
|
||||
/// Content is syntax-highlighted HTML.
|
||||
///
|
||||
/// Note that is necessary to enable the `syntax` feature flag for this
|
||||
/// variant to be constructed. Use `highlighting::blob`, instead of
|
||||
/// [`blob`] to get highlighted content.
|
||||
/// Note that it is necessary to enable the `syntax` feature flag
|
||||
/// for this variant to be constructed. Use `Blob::highlighted`,
|
||||
/// instead of `Blob::new` to get highlighted content.
|
||||
Html(String),
|
||||
/// Content is binary and needs special treatment.
|
||||
Binary(Vec<u8>),
|
||||
@@ -112,98 +146,54 @@ impl Serialize for BlobContent {
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the [`Blob`] for a file at `revision` under `path`.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Will return [`Error`] if the project doesn't exist or a surf interaction
|
||||
/// fails.
|
||||
pub fn blob<P>(repo: &Repository, maybe_revision: Option<Revision>, path: &P) -> Result<Blob, Error>
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
{
|
||||
make_blob(repo, maybe_revision, path, content)
|
||||
}
|
||||
|
||||
fn make_blob<P, C>(
|
||||
repo: &Repository,
|
||||
maybe_revision: Option<Revision>,
|
||||
path: &P,
|
||||
content: C,
|
||||
) -> Result<Blob, Error>
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
C: FnOnce(&[u8]) -> BlobContent,
|
||||
{
|
||||
let path = path.as_ref();
|
||||
let revision = maybe_revision.unwrap();
|
||||
let root = repo.root_dir(&revision)?;
|
||||
|
||||
let file = root
|
||||
.find_file(&path, repo)?
|
||||
.ok_or_else(|| Error::PathNotFound(path.to_path_buf()))?;
|
||||
|
||||
let last_commit = repo
|
||||
.last_commit(path, &revision)?
|
||||
.map(|c| commit::Header::from(&c));
|
||||
// TODO: fuck this
|
||||
let name = path
|
||||
.file_name()
|
||||
.unwrap()
|
||||
.to_os_string()
|
||||
.into_string()
|
||||
.ok()
|
||||
.unwrap();
|
||||
|
||||
let file_content = repo.file_content(file)?;
|
||||
let content = content(file_content.as_bytes());
|
||||
|
||||
Ok(Blob {
|
||||
content,
|
||||
info: Info {
|
||||
name,
|
||||
object_type: ObjectType::Blob,
|
||||
last_commit,
|
||||
},
|
||||
path: path.to_path_buf(),
|
||||
})
|
||||
}
|
||||
|
||||
/// Return a [`BlobContent`] given a byte slice.
|
||||
fn content(content: &[u8]) -> BlobContent {
|
||||
match str::from_utf8(content) {
|
||||
Ok(utf8) => BlobContent::Plain(utf8.to_owned()),
|
||||
Err(_) => BlobContent::Binary(content.to_owned()),
|
||||
impl From<FileContent<'_>> for BlobContent {
|
||||
fn from(content: FileContent) -> Self {
|
||||
let content = content.as_bytes();
|
||||
match str::from_utf8(content) {
|
||||
Ok(utf8) => BlobContent::Plain(utf8.to_owned()),
|
||||
Err(_) => BlobContent::Binary(content.to_owned()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "syntax")]
|
||||
pub mod highlighting {
|
||||
use crate::source::syntax;
|
||||
|
||||
use super::*;
|
||||
|
||||
/// Returns the [`Blob`] for a file at `revision` under `path`.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Will return [`Error`] if the project doesn't exist or a surf interaction
|
||||
/// fails.
|
||||
pub fn blob<P>(
|
||||
browser: &mut Browser,
|
||||
maybe_revision: Option<Revision<P>>,
|
||||
path: &str,
|
||||
theme: Option<&str>,
|
||||
) -> Result<Blob, Error>
|
||||
where
|
||||
P: ToString,
|
||||
{
|
||||
make_blob(browser, maybe_revision, path, |contents| {
|
||||
content(path, contents, theme)
|
||||
})
|
||||
impl Blob {
|
||||
/// Returns the [`Blob`] for a file at `revision` under `path`.
|
||||
///
|
||||
/// The content of the [`Blob`] will be highlighted, if possible.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Will return [`Error`] if the project doesn't exist or a surf
|
||||
/// interaction fails.
|
||||
pub fn highlighted<P, R>(
|
||||
browser: &Repository,
|
||||
revision: &R,
|
||||
path: &P,
|
||||
theme: Option<&str>,
|
||||
) -> Result<Blob, Error>
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
R: git::Revision,
|
||||
{
|
||||
Self::make_blob(browser, revision, path, |contents| {
|
||||
content(path, &contents, theme)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Return a [`BlobContent`] given a file path, content and theme. Attempts
|
||||
/// to perform syntax highlighting when the theme is `Some`.
|
||||
fn content(path: &str, content: &[u8], theme_name: Option<&str>) -> BlobContent {
|
||||
fn content<P>(path: P, content: &FileContent, theme_name: Option<&str>) -> BlobContent
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
{
|
||||
let content = content.as_bytes();
|
||||
let content = match str::from_utf8(content) {
|
||||
Ok(content) => content,
|
||||
Err(_) => return BlobContent::Binary(content.to_owned()),
|
||||
|
||||
@@ -18,9 +18,8 @@
|
||||
//! Represents git object type 'tree', i.e. like directory entries in Unix.
|
||||
//! See git [doc](https://git-scm.com/book/en/v2/Git-Internals-Git-Objects) for more details.
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::path::Path;
|
||||
|
||||
use git_ref_format::refname;
|
||||
#[cfg(feature = "serialize")]
|
||||
use serde::{
|
||||
ser::{SerializeStruct as _, Serializer},
|
||||
@@ -28,46 +27,95 @@ use serde::{
|
||||
};
|
||||
|
||||
use crate::{
|
||||
file_system::directory,
|
||||
git::Repository,
|
||||
source::{
|
||||
commit,
|
||||
object::{Error, Info, ObjectType},
|
||||
revision::Revision,
|
||||
},
|
||||
file_system::{directory, Directory},
|
||||
git::{self, Repository},
|
||||
source::{commit, object::Error},
|
||||
};
|
||||
|
||||
/// Result of a directory listing, carries other trees and blobs.
|
||||
pub struct Tree {
|
||||
/// Absolute path to the tree object from the repo root.
|
||||
pub path: PathBuf,
|
||||
pub directory: Directory,
|
||||
pub commit: Option<commit::Header>,
|
||||
/// Entries listed in that tree result.
|
||||
pub entries: Vec<TreeEntry>,
|
||||
/// Extra info for the tree object.
|
||||
pub info: Info,
|
||||
}
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
impl Tree {
|
||||
/// Retrieve the [`Tree`] for the given `revision` and directory `prefix`.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Will return [`Error`] if any of the surf interactions fail.
|
||||
pub fn new<P, R>(repo: &Repository, revision: &R, prefix: Option<&P>) -> Result<Tree, Error>
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
R: git::Revision,
|
||||
{
|
||||
let prefix = prefix.map(|p| p.as_ref());
|
||||
|
||||
let prefix_dir = match prefix {
|
||||
None => repo.root_dir(revision)?,
|
||||
Some(path) => repo
|
||||
.root_dir(revision)?
|
||||
.find_directory(path, repo)?
|
||||
.ok_or_else(|| Error::PathNotFound(path.to_path_buf()))?,
|
||||
};
|
||||
|
||||
let mut entries = prefix_dir
|
||||
.entries(repo)?
|
||||
.entries()
|
||||
.cloned()
|
||||
.map(TreeEntry::from)
|
||||
.collect::<Vec<_>>();
|
||||
entries.sort();
|
||||
|
||||
let last_commit = if prefix.is_none() {
|
||||
let history = repo.history(revision)?;
|
||||
Some(commit::Header::from(history.head()))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
Ok(Tree {
|
||||
entries,
|
||||
directory: prefix_dir,
|
||||
commit: last_commit,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
impl Serialize for Tree {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
let mut state = serializer.serialize_struct("Tree", 3)?;
|
||||
state.serialize_field("path", &self.path)?;
|
||||
const FIELDS: usize = 4;
|
||||
let mut state = serializer.serialize_struct("Tree", FIELDS)?;
|
||||
state.serialize_field("entries", &self.entries)?;
|
||||
state.serialize_field("info", &self.info)?;
|
||||
state.serialize_field("lastCommit", &self.commit)?;
|
||||
state.serialize_field("name", &self.directory.name())?;
|
||||
state.serialize_field("path", &self.directory.location())?;
|
||||
state.end()
|
||||
}
|
||||
}
|
||||
|
||||
// TODO(xla): Ensure correct by construction.
|
||||
/// Entry in a Tree result.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct TreeEntry {
|
||||
/// Extra info for the entry.
|
||||
pub info: Info,
|
||||
/// Absolute path to the object from the root of the repo.
|
||||
pub path: PathBuf,
|
||||
pub entry: directory::Entry,
|
||||
}
|
||||
|
||||
impl From<directory::Entry> for TreeEntry {
|
||||
fn from(entry: directory::Entry) -> Self {
|
||||
Self { entry }
|
||||
}
|
||||
}
|
||||
|
||||
impl From<TreeEntry> for directory::Entry {
|
||||
fn from(TreeEntry { entry }: TreeEntry) -> Self {
|
||||
entry
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
@@ -76,97 +124,18 @@ impl Serialize for TreeEntry {
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
let mut state = serializer.serialize_struct("Tree", 2)?;
|
||||
state.serialize_field("path", &self.path)?;
|
||||
state.serialize_field("info", &self.info)?;
|
||||
const FIELDS: usize = 4;
|
||||
let mut state = serializer.serialize_struct("TreeEntry", FIELDS)?;
|
||||
state.serialize_field("path", &self.entry.location())?;
|
||||
state.serialize_field("name", &self.entry.name())?;
|
||||
state.serialize_field("lastCommit", &None::<commit::Header>)?;
|
||||
state.serialize_field(
|
||||
"kind",
|
||||
match self.entry {
|
||||
directory::Entry::File(_) => "blob",
|
||||
directory::Entry::Directory(_) => "directory",
|
||||
},
|
||||
)?;
|
||||
state.end()
|
||||
}
|
||||
}
|
||||
|
||||
/// Retrieve the [`Tree`] for the given `revision` and directory `prefix`.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Will return [`Error`] if any of the surf interactions fail.
|
||||
pub fn tree<P>(
|
||||
repo: &Repository,
|
||||
maybe_revision: Option<Revision>,
|
||||
maybe_prefix: Option<&P>,
|
||||
) -> Result<Tree, Error>
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
{
|
||||
let maybe_prefix = maybe_prefix.map(|p| p.as_ref());
|
||||
let rev = match maybe_revision {
|
||||
Some(r) => r,
|
||||
None => Revision::Branch {
|
||||
name: refname!("main"),
|
||||
remote: None,
|
||||
},
|
||||
};
|
||||
|
||||
let prefix_dir = match maybe_prefix {
|
||||
None => repo.root_dir(&rev)?,
|
||||
Some(path) => repo
|
||||
.root_dir(&rev)?
|
||||
.find_directory(path, repo)?
|
||||
.ok_or_else(|| Error::PathNotFound(path.to_path_buf()))?,
|
||||
};
|
||||
|
||||
let mut entries = prefix_dir
|
||||
.entries(repo)?
|
||||
.entries()
|
||||
.fold(Vec::new(), |mut entries, entry| {
|
||||
let entry_path = match maybe_prefix {
|
||||
Some(path) => {
|
||||
let mut path = path.to_path_buf();
|
||||
path.push(entry.name());
|
||||
path
|
||||
},
|
||||
None => PathBuf::new(),
|
||||
};
|
||||
|
||||
let info = Info {
|
||||
name: entry.name().clone(),
|
||||
object_type: match entry {
|
||||
directory::Entry::Directory(_) => ObjectType::Tree,
|
||||
directory::Entry::File { .. } => ObjectType::Blob,
|
||||
},
|
||||
last_commit: None,
|
||||
};
|
||||
|
||||
entries.push(TreeEntry {
|
||||
info,
|
||||
path: entry_path,
|
||||
});
|
||||
entries
|
||||
});
|
||||
|
||||
// We want to ensure that in the response Tree entries come first. `Ord` being
|
||||
// derived on the enum ensures Variant declaration order.
|
||||
//
|
||||
// https://doc.rust-lang.org/std/cmp/trait.Ord.html#derivable
|
||||
entries.sort_by(|a, b| a.info.object_type.cmp(&b.info.object_type));
|
||||
|
||||
let last_commit = if maybe_prefix.is_none() {
|
||||
let history = repo.history(&rev)?;
|
||||
Some(commit::Header::from(history.head()))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let name = match maybe_prefix {
|
||||
None => "".into(),
|
||||
Some(path) => path.file_name().unwrap().to_str().unwrap().to_string(),
|
||||
};
|
||||
let info = Info {
|
||||
name,
|
||||
object_type: ObjectType::Tree,
|
||||
last_commit,
|
||||
};
|
||||
|
||||
Ok(Tree {
|
||||
path: maybe_prefix.map_or(PathBuf::new(), |path| path.to_path_buf()),
|
||||
entries,
|
||||
info,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,189 +0,0 @@
|
||||
// This file is part of radicle-surf
|
||||
// <https://github.com/radicle-dev/radicle-surf>
|
||||
//
|
||||
// Copyright (C) 2019-2020 The Radicle Team <dev@radicle.xyz>
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License version 3 or
|
||||
// later as published by the Free Software Foundation.
|
||||
//
|
||||
// This program 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 General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
//! Represents revisions
|
||||
|
||||
use std::collections::BTreeSet;
|
||||
|
||||
use git_ref_format::{lit, refspec, Qualified, RefString};
|
||||
|
||||
#[cfg(feature = "serialize")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use radicle_git_ext::Oid;
|
||||
|
||||
use crate::git::{self, Error, Glob, Repository};
|
||||
|
||||
/// Types of a peer.
|
||||
pub enum Category<P, U> {
|
||||
/// Local peer.
|
||||
Local {
|
||||
/// Peer Id
|
||||
peer_id: P,
|
||||
/// User name
|
||||
user: U,
|
||||
},
|
||||
/// Remote peer.
|
||||
Remote {
|
||||
/// Peer Id
|
||||
peer_id: P,
|
||||
/// User name
|
||||
user: U,
|
||||
},
|
||||
}
|
||||
|
||||
/// A revision selector for a `Browser`.
|
||||
#[cfg_attr(
|
||||
feature = "serialize",
|
||||
derive(Deserialize, Serialize),
|
||||
serde(rename_all = "camelCase", tag = "type")
|
||||
)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Revision {
|
||||
/// Select a tag under the name provided.
|
||||
#[cfg_attr(feature = "serialize", serde(rename_all = "camelCase"))]
|
||||
Tag {
|
||||
/// Name of the tag.
|
||||
name: RefString,
|
||||
},
|
||||
/// Select a branch under the name provided.
|
||||
#[cfg_attr(feature = "serialize", serde(rename_all = "camelCase"))]
|
||||
Branch {
|
||||
/// Name of the branch.
|
||||
name: RefString,
|
||||
/// The remote peer, if specified.
|
||||
remote: Option<RefString>,
|
||||
},
|
||||
/// Select a SHA1 under the name provided.
|
||||
#[cfg_attr(feature = "serialize", serde(rename_all = "camelCase"))]
|
||||
Sha {
|
||||
/// The SHA1 value.
|
||||
sha: Oid,
|
||||
},
|
||||
}
|
||||
|
||||
impl git::Revision for &Revision {
|
||||
type Error = git2::Error;
|
||||
|
||||
fn object_id(&self, repo: &Repository) -> Result<Oid, Self::Error> {
|
||||
match self {
|
||||
Revision::Tag { name } => match name.qualified() {
|
||||
None => Qualified::from(lit::refs_tags(name)).object_id(repo),
|
||||
Some(name) => name.object_id(repo),
|
||||
},
|
||||
Revision::Branch { name, remote } => match remote {
|
||||
Some(remote) => {
|
||||
Qualified::from(lit::refs_remotes(remote.join(name))).object_id(repo)
|
||||
},
|
||||
None => git::Branch::local(name).refname().object_id(repo),
|
||||
},
|
||||
Revision::Sha { sha } => Ok(*sha),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Bundled response to retrieve both branches and tags for
|
||||
/// a user's repo.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct Revisions<P, U> {
|
||||
/// The peer peer_id for the user.
|
||||
pub peer_id: P,
|
||||
/// The user who owns these revisions.
|
||||
pub user: U,
|
||||
/// List of branch reference names.
|
||||
pub branches: BTreeSet<RefString>,
|
||||
/// List of tag reference names.
|
||||
pub tags: BTreeSet<RefString>,
|
||||
}
|
||||
|
||||
/// Provide the [`Revisions`] for the given `peer_id`, looking for the
|
||||
/// remote branches.
|
||||
///
|
||||
/// If there are no branches then this returns `None`.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// * If we cannot get the branches from the `Browser`
|
||||
pub fn remote<U>(
|
||||
repo: &Repository,
|
||||
peer_id: RefString,
|
||||
user: U,
|
||||
) -> Result<Revisions<RefString, U>, Error> {
|
||||
let pattern = peer_id.to_pattern(refspec::pattern!("*"));
|
||||
let branches = repo
|
||||
.branch_names(Glob::remotes(pattern))?
|
||||
.map(|name| name.map(RefString::from))
|
||||
.collect::<Result<_, _>>()?;
|
||||
Ok(Revisions {
|
||||
peer_id,
|
||||
user,
|
||||
branches,
|
||||
// TODO(rudolfs): implement remote peer tags once we decide how
|
||||
// https://radicle.community/t/git-tags/214
|
||||
tags: BTreeSet::new(),
|
||||
})
|
||||
}
|
||||
|
||||
/// Provide the [`Revisions`] for the given `peer_id`, looking for the
|
||||
/// local branches.
|
||||
///
|
||||
/// If there are no branches then this returns `None`.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// * If we cannot get the branches from the `Browser`
|
||||
pub fn local<U>(
|
||||
repo: &Repository,
|
||||
peer_id: RefString,
|
||||
user: U,
|
||||
) -> Result<Revisions<RefString, U>, Error> {
|
||||
let branches = repo
|
||||
.branch_names(Glob::all_heads())?
|
||||
.map(|name| name.map(RefString::from))
|
||||
.collect::<Result<_, _>>()?;
|
||||
let tags = repo
|
||||
.tag_names()?
|
||||
.map(|name| name.map(RefString::from))
|
||||
.collect::<Result<_, _>>()?;
|
||||
Ok(Revisions {
|
||||
peer_id,
|
||||
user,
|
||||
branches,
|
||||
tags,
|
||||
})
|
||||
}
|
||||
|
||||
/// Provide the [`Revisions`] of a peer.
|
||||
///
|
||||
/// If the peer is [`Category::Local`], meaning that is the current person doing
|
||||
/// the browsing and no remote is set for the reference.
|
||||
///
|
||||
/// Othewise, the peer is [`Category::Remote`], meaning that we are looking into
|
||||
/// a remote part of a reference.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// * If we cannot get the branches from the `Browser`
|
||||
pub fn revisions<U>(
|
||||
repo: &Repository,
|
||||
peer: Category<RefString, U>,
|
||||
) -> Result<Revisions<RefString, U>, Error> {
|
||||
match peer {
|
||||
Category::Local { peer_id, user } => local(repo, peer_id, user),
|
||||
Category::Remote { peer_id, user } => remote(repo, peer_id, user),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
// This file is part of radicle-surf
|
||||
// <https://github.com/radicle-dev/radicle-surf>
|
||||
//
|
||||
// Copyright (C) 2019-2020 The Radicle Team <dev@radicle.xyz>
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License version 3 or
|
||||
// later as published by the Free Software Foundation.
|
||||
//
|
||||
// This program 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 General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use std::path::Path;
|
||||
|
||||
use syntect::{
|
||||
easy::HighlightLines,
|
||||
highlighting::ThemeSet,
|
||||
parsing::SyntaxSet,
|
||||
util::LinesWithEndings,
|
||||
};
|
||||
|
||||
lazy_static::lazy_static! {
|
||||
// The syntax set is slow to load (~30ms), so we make sure to only load it once.
|
||||
// It _will_ affect the latency of the first request that uses syntax highlighting,
|
||||
// but this is acceptable for now.
|
||||
pub static ref SYNTAX_SET: SyntaxSet = {
|
||||
let default_set = SyntaxSet::load_defaults_newlines();
|
||||
let mut builder = default_set.into_builder();
|
||||
|
||||
if cfg!(debug_assertions) {
|
||||
// In development assets are relative to the proxy source.
|
||||
// Don't crash if we aren't able to load additional syntaxes for some reason.
|
||||
builder.add_from_folder("./assets", true).ok();
|
||||
} else {
|
||||
// In production assets are relative to the proxy executable.
|
||||
let exe_path = std::env::current_exe().expect("Can't get current exe path");
|
||||
let root_path = exe_path
|
||||
.parent()
|
||||
.expect("Could not get parent path of current executable");
|
||||
let mut tmp = root_path.to_path_buf();
|
||||
tmp.push("assets");
|
||||
let asset_path = tmp.to_str().expect("Couldn't convert pathbuf to str");
|
||||
|
||||
// Don't crash if we aren't able to load additional syntaxes for some reason.
|
||||
match builder.add_from_folder(asset_path, true) {
|
||||
Ok(_) => (),
|
||||
Err(err) => log::warn!("Syntax builder error : {}", err),
|
||||
};
|
||||
}
|
||||
builder.build()
|
||||
};
|
||||
}
|
||||
|
||||
/// Return highlighted text given a file path, the original text, and
|
||||
/// a theme.
|
||||
pub fn highlight<P>(path: P, content: &str, theme_name: &str) -> Option<String>
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
{
|
||||
let syntax = path
|
||||
.as_ref()
|
||||
.extension()
|
||||
.and_then(std::ffi::OsStr::to_str)
|
||||
.and_then(|ext| SYNTAX_SET.find_syntax_by_extension(ext));
|
||||
|
||||
let ts = ThemeSet::load_defaults();
|
||||
let theme = ts.themes.get(theme_name);
|
||||
|
||||
match (syntax, theme) {
|
||||
(Some(syntax), Some(theme)) => {
|
||||
let mut highlighter = HighlightLines::new(syntax, theme);
|
||||
let mut html = String::with_capacity(content.len());
|
||||
|
||||
for line in LinesWithEndings::from(content) {
|
||||
let regions = highlighter.highlight_line(line, &SYNTAX_SET).ok()?;
|
||||
syntect::html::append_highlighted_html_for_styled_line(
|
||||
®ions[..],
|
||||
syntect::html::IncludeBackground::No,
|
||||
&mut html,
|
||||
)
|
||||
.ok();
|
||||
}
|
||||
Some(html)
|
||||
},
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
// This file is part of radicle-surf
|
||||
// <https://github.com/radicle-dev/radicle-surf>
|
||||
//
|
||||
// Copyright (C) 2019-2020 The Radicle Team <dev@radicle.xyz>
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License version 3 or
|
||||
// later as published by the Free Software Foundation.
|
||||
//
|
||||
// This program 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 General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use std::collections::{BTreeMap, BTreeSet};
|
||||
|
||||
use git_ref_format::{RefStr, RefString};
|
||||
|
||||
use crate::git::{Error, Glob, Repository};
|
||||
|
||||
/// A `View` represents a logical view of peer's repository in the
|
||||
/// [Heartwood protocol][heartwood].
|
||||
///
|
||||
/// The `Id` parameter allows the specification of what the identifier
|
||||
/// for a peer is in Heartwood.
|
||||
///
|
||||
/// The `P` parameter allows the specification of what a person looks
|
||||
/// like in Heartwood, whether that be a DID document, website, etc.
|
||||
///
|
||||
/// [heartwood]: https://github.com/radicle-dev/heartwood
|
||||
pub struct View<Id, P> {
|
||||
/// Identifier for the peer that this `View` is associated to.
|
||||
pub identifier: Id,
|
||||
/// Personal information for the peer that this `View` is associated to.
|
||||
pub person: Option<P>,
|
||||
/// All `refs/heads` and `refs/remotes` reference names.
|
||||
pub branches: BTreeSet<RefString>,
|
||||
/// All `refs/tags` reference names.
|
||||
pub tags: BTreeSet<RefString>,
|
||||
/// Any `refs/<category>` reference names, where each key in the map is a
|
||||
/// `<category>` and the set is the references under that category.
|
||||
pub references: BTreeMap<RefString, BTreeSet<RefString>>,
|
||||
}
|
||||
|
||||
/// Construct a [`View`] for the `identifier` and `person`.
|
||||
///
|
||||
/// `identifier` is assumed to act as the namespace in the Heartwood storage.
|
||||
///
|
||||
/// `categories` is the set of git non-standard reference categories,
|
||||
/// i.e. `refs/heads`, `refs/remotes`, `refs/tags`, and `refs/notes`.
|
||||
pub fn view<Id, R, P>(
|
||||
repo: &Repository,
|
||||
identifier: Id,
|
||||
person: Option<P>,
|
||||
categories: impl IntoIterator<Item = RefString>,
|
||||
) -> Result<View<Id, P>, Error>
|
||||
where
|
||||
Id: Clone + Into<R>,
|
||||
R: AsRef<RefStr>,
|
||||
{
|
||||
let namespace = identifier.clone().into();
|
||||
repo.with_namespace(&namespace.as_ref().to_ref_string(), move || {
|
||||
let branches = repo
|
||||
.branch_names(Glob::all_heads())?
|
||||
.map(|name| name.map(RefString::from))
|
||||
.collect::<Result<_, _>>()?;
|
||||
let tags = repo
|
||||
.tag_names(&Glob::all_tags())?
|
||||
.map(|name| name.map(RefString::from))
|
||||
.collect::<Result<_, _>>()?;
|
||||
let categories = categories.into_iter().fold(Glob::default(), |globs, cat| {
|
||||
globs.and(Glob::all_category(cat))
|
||||
});
|
||||
let references =
|
||||
repo.categories(&categories)?
|
||||
.try_fold(BTreeMap::new(), |mut map, r| {
|
||||
let (cat, name) = r?;
|
||||
map.entry(cat)
|
||||
.and_modify(|names: &mut BTreeSet<RefString>| {
|
||||
names.insert(name.clone());
|
||||
})
|
||||
.or_insert_with(|| BTreeSet::from_iter(Some(name)));
|
||||
Ok::<_, Error>(map)
|
||||
})?;
|
||||
Ok(View {
|
||||
identifier,
|
||||
person,
|
||||
branches,
|
||||
tags,
|
||||
references,
|
||||
})
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user