mirror of
https://seed.radicle.dev/rad:z6cFWeWpnZNHh9rUW8phgA3b5yGt.git
synced 2026-10-24 05:40:38 +08:00
remove source/ layer and file_system/ layer.
- Merge `person.rs` into `object/commit.rs`. - Move `source/commit.rs` into `object/`. - Merge `file_system/directory.rs` into `file_system.rs`. - Rename `file_system.rs` to `fs.rs`. Signed-off-by: Han Xu <keepsimple@gmail.com>
This commit is contained in:
@@ -25,7 +25,7 @@
|
||||
//! the directories in a tree-like structure.
|
||||
|
||||
use radicle_surf::{
|
||||
file_system::{directory, Directory},
|
||||
fs::{self, Directory},
|
||||
git::Repository,
|
||||
};
|
||||
use std::{env, time::Instant};
|
||||
@@ -53,8 +53,8 @@ fn print_directory(d: &Directory, repo: &Repository, indent_level: usize) {
|
||||
println!("{}{}/", &indent, d.name());
|
||||
for entry in d.entries(repo).unwrap() {
|
||||
match entry {
|
||||
directory::Entry::File(f) => println!(" {}{}", &indent, f.name()),
|
||||
directory::Entry::Directory(d) => print_directory(&d, repo, indent_level + 1),
|
||||
fs::Entry::File(f) => println!(" {}{}", &indent, f.name()),
|
||||
fs::Entry::Directory(d) => print_directory(&d, repo, indent_level + 1),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,114 +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/>.
|
||||
|
||||
//! A model of a non-empty directory data structure that can be searched,
|
||||
//! queried, and rendered. The concept is to represent VCS directory, but is not
|
||||
//! necessarily tied to one.
|
||||
//!
|
||||
//! # Examples
|
||||
//!
|
||||
//! ```
|
||||
//! use nonempty::NonEmpty;
|
||||
//! use radicle_surf::file_system as fs;
|
||||
//!
|
||||
//! // This used for unsafe set up of the directory, but should not be used in production code.
|
||||
//! use radicle_surf::file_system::unsound;
|
||||
//!
|
||||
//! let mut directory = fs::Directory::root();
|
||||
//!
|
||||
//! // Set up root files
|
||||
//! let readme = fs::File::new(b"Radicle Surfing");
|
||||
//! let cargo = fs::File::new(b"[package]\nname = \"radicle-surf\"");
|
||||
//! let root_files = NonEmpty::from((
|
||||
//! (unsound::label::new("README.md"), readme),
|
||||
//! vec![(unsound::label::new("Cargo.toml"), cargo)],
|
||||
//! ));
|
||||
//!
|
||||
//! // Set up src files
|
||||
//! let lib = fs::File::new(b"pub mod diff;\npub mod file_system;\n pub mod vcs;");
|
||||
//! let file_system_mod = fs::File::new(b"pub mod directory;\npub mod error;\nmod path;");
|
||||
//!
|
||||
//! directory.insert_files(&[], root_files);
|
||||
//! directory.insert_file(unsound::path::new("src/lib.rs"), lib.clone());
|
||||
//! directory.insert_file(unsound::path::new("src/file_system/mod.rs"), file_system_mod);
|
||||
//!
|
||||
//! // With a directory in place we can begin to operate on it
|
||||
//! // The first we will do is list what contents are at the root.
|
||||
//! let root_contents = directory.list_directory();
|
||||
//!
|
||||
//! // Checking that we have the correct contents
|
||||
//! assert_eq!(
|
||||
//! root_contents,
|
||||
//! vec![
|
||||
//! fs::SystemType::file(unsound::label::new("Cargo.toml")),
|
||||
//! fs::SystemType::file(unsound::label::new("README.md")),
|
||||
//! fs::SystemType::directory(unsound::label::new("src")),
|
||||
//! ]
|
||||
//! );
|
||||
//!
|
||||
//! // We can then go down one level to explore sub-directories
|
||||
//! // Note here that we can use `Path::new`, since there's guranteed to be a `Label`,
|
||||
//! // although we cheated and created the label unsafely.
|
||||
//! let src = directory.find_directory(fs::Path::new(unsound::label::new("src")));
|
||||
//!
|
||||
//! // Ensure that we found the src directory
|
||||
//! assert!(src.is_some());
|
||||
//! let src = src.unwrap();
|
||||
//!
|
||||
//! let src_contents = src.list_directory();
|
||||
//!
|
||||
//! // Checking we have the correct contents of 'src'
|
||||
//! assert_eq!(
|
||||
//! src_contents,
|
||||
//! vec![
|
||||
//! fs::SystemType::directory(unsound::label::new("file_system")),
|
||||
//! fs::SystemType::file(unsound::label::new("lib.rs")),
|
||||
//! ]
|
||||
//! );
|
||||
//!
|
||||
//! // We can dive down to 'file_system' either from the root or src, they should be the same.
|
||||
//! assert_eq!(
|
||||
//! src.find_directory(unsound::path::new("file_system")),
|
||||
//! directory.find_directory(unsound::path::new("src/file_system")),
|
||||
//! );
|
||||
//!
|
||||
//! // We can also find files
|
||||
//! assert_eq!(
|
||||
//! src.find_file(unsound::path::new("lib.rs")),
|
||||
//! Some(lib)
|
||||
//! );
|
||||
//!
|
||||
//! // From anywhere
|
||||
//! assert_eq!(
|
||||
//! directory.find_file(unsound::path::new("src/file_system/mod.rs")),
|
||||
//! src.find_file(unsound::path::new("file_system/mod.rs")),
|
||||
//! );
|
||||
//!
|
||||
//! // And we can also check the size of directories and files
|
||||
//! assert_eq!(
|
||||
//! directory.find_file(unsound::path::new("src/file_system/mod.rs")).map(|f| f.size()),
|
||||
//! Some(43),
|
||||
//! );
|
||||
//!
|
||||
//! assert_eq!(
|
||||
//! directory.size(),
|
||||
//! 137,
|
||||
//! );
|
||||
//! ```
|
||||
|
||||
pub mod directory;
|
||||
pub use directory::{Directory, Entries, Entry, File, FileContent};
|
||||
@@ -28,10 +28,7 @@ use thiserror::Error;
|
||||
|
||||
use crate::{
|
||||
diff::{self, *},
|
||||
file_system::{
|
||||
directory::{self, File, FileContent},
|
||||
Directory,
|
||||
},
|
||||
fs::{self, Directory, File, FileContent},
|
||||
git::{
|
||||
commit,
|
||||
glob,
|
||||
@@ -47,7 +44,7 @@ use crate::{
|
||||
Tag,
|
||||
ToCommit,
|
||||
},
|
||||
source::{self, commit::Header, Blob, Tree, TreeEntry},
|
||||
object::{commit::Header, Blob, Tree, TreeEntry},
|
||||
};
|
||||
|
||||
pub mod iter;
|
||||
@@ -70,9 +67,9 @@ pub enum Error {
|
||||
Diff(#[from] diff::git::error::Diff),
|
||||
/// A wrapper around the generic [`git2::Error`].
|
||||
#[error(transparent)]
|
||||
Directory(#[from] directory::error::Directory),
|
||||
Directory(#[from] fs::error::Directory),
|
||||
#[error(transparent)]
|
||||
File(#[from] directory::error::File),
|
||||
File(#[from] fs::error::File),
|
||||
#[error(transparent)]
|
||||
Git(#[from] git2::Error),
|
||||
#[error(transparent)]
|
||||
@@ -301,7 +298,7 @@ impl Repository {
|
||||
let last_commit = self
|
||||
.last_commit(path, commit)?
|
||||
.ok_or_else(|| Error::PathNotFound(path.as_ref().to_path_buf()))?;
|
||||
let header = source::commit::Header::from(last_commit);
|
||||
let header = Header::from(last_commit);
|
||||
Ok(Tree::new(dir.id(), entries, header))
|
||||
}
|
||||
|
||||
@@ -314,7 +311,7 @@ impl Repository {
|
||||
let last_commit = self
|
||||
.last_commit(path, commit)?
|
||||
.ok_or_else(|| Error::PathNotFound(path.as_ref().to_path_buf()))?;
|
||||
let header = source::commit::Header::from(last_commit);
|
||||
let header = Header::from(last_commit);
|
||||
|
||||
let content = file.content(self)?;
|
||||
Ok(Blob::new(file.id(), content.as_bytes(), header))
|
||||
|
||||
@@ -29,6 +29,6 @@ pub extern crate git_ref_format;
|
||||
extern crate radicle_git_ext as git_ext;
|
||||
|
||||
pub mod diff;
|
||||
pub mod file_system;
|
||||
pub mod fs;
|
||||
pub mod git;
|
||||
pub mod source;
|
||||
pub mod object;
|
||||
|
||||
@@ -26,16 +26,18 @@ pub use blob::{Blob, BlobContent};
|
||||
pub mod tree;
|
||||
pub use tree::{Tree, TreeEntry};
|
||||
|
||||
use crate::{file_system::directory, git};
|
||||
pub mod commit;
|
||||
|
||||
use crate::{fs, git};
|
||||
|
||||
/// An error reported by object types.
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum Error {
|
||||
#[error(transparent)]
|
||||
Directory(#[from] directory::error::Directory),
|
||||
Directory(#[from] fs::error::Directory),
|
||||
|
||||
#[error(transparent)]
|
||||
File(#[from] directory::error::File),
|
||||
File(#[from] fs::error::File),
|
||||
|
||||
/// An error occurred during a git operation.
|
||||
#[error(transparent)]
|
||||
@@ -27,7 +27,7 @@ use serde::{
|
||||
Serialize,
|
||||
};
|
||||
|
||||
use crate::source::commit;
|
||||
use crate::object::commit;
|
||||
|
||||
/// Represents a git blob object.
|
||||
pub struct Blob {
|
||||
@@ -29,7 +29,6 @@ use serde::{
|
||||
use crate::{
|
||||
diff,
|
||||
git::{self, glob, Glob, Repository},
|
||||
source::person::Person,
|
||||
};
|
||||
|
||||
use radicle_git_ext::Oid;
|
||||
@@ -194,3 +193,14 @@ pub enum Error {
|
||||
#[error("the path '{0}' was not found")]
|
||||
PathNotFound(PathBuf),
|
||||
}
|
||||
|
||||
/// Representation of a person (e.g. committer, author, signer) from a
|
||||
/// repository. Usually extracted from a signature.
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Person {
|
||||
/// Name part of the commit signature.
|
||||
pub name: String,
|
||||
/// Email part of the commit signature.
|
||||
pub email: String,
|
||||
}
|
||||
@@ -27,7 +27,7 @@ use serde::{
|
||||
Serialize,
|
||||
};
|
||||
|
||||
use crate::{file_system::directory, source::commit};
|
||||
use crate::{fs, object::commit};
|
||||
|
||||
/// Represents a tree object as in git. It is essentially the content of
|
||||
/// one directory. Note that multiple directories can have the same content,
|
||||
@@ -176,11 +176,11 @@ impl PartialEq for TreeEntry {
|
||||
|
||||
impl Eq for TreeEntry {}
|
||||
|
||||
impl From<directory::Entry> for Entry {
|
||||
fn from(entry: directory::Entry) -> Self {
|
||||
impl From<fs::Entry> for Entry {
|
||||
fn from(entry: fs::Entry) -> Self {
|
||||
match entry {
|
||||
directory::Entry::File(f) => Entry::Blob(f.id()),
|
||||
directory::Entry::Directory(d) => Entry::Tree(d.id()),
|
||||
fs::Entry::File(f) => Entry::Blob(f.id()),
|
||||
fs::Entry::Directory(d) => Entry::Tree(d.id()),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
//! The `source` module provides a layer on top of the [`crate::git`]
|
||||
//! functionality.
|
||||
//!
|
||||
//! It provides data types of [`Blob`]s, [`Tree`]s, and [`Commit`]s
|
||||
//! (see [git objects][git-objects]). These types are analgous to
|
||||
//! [`crate::file_system::File`], [`crate::file_system::Directory`], and
|
||||
//! [`crate::git::Commit`]. However, they provide extra metadata and
|
||||
//! can be serialized to serve to other applications. For example,
|
||||
//! they could be used in an HTTP server for viewing a Git repository.
|
||||
//!
|
||||
//! [git-objects]: https://git-scm.com/book/en/v2/Git-Internals-Git-Objects
|
||||
|
||||
pub mod object;
|
||||
pub use object::{blob, tree, Blob, BlobContent, Tree, TreeEntry};
|
||||
|
||||
pub mod commit;
|
||||
pub use commit::{commit, commits, Commit};
|
||||
|
||||
mod person;
|
||||
pub use person::Person;
|
||||
@@ -1,29 +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 a person in a repo.
|
||||
|
||||
/// Representation of a person (e.g. committer, author, signer) from a
|
||||
/// repository. Usually extracted from a signature.
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Person {
|
||||
/// Name part of the commit signature.
|
||||
pub name: String,
|
||||
/// Email part of the commit signature.
|
||||
pub email: String,
|
||||
}
|
||||
@@ -6,7 +6,7 @@
|
||||
mod directory {
|
||||
use git_ref_format::refname;
|
||||
use radicle_surf::{
|
||||
file_system::{directory, Entry},
|
||||
fs::{self, Entry},
|
||||
git::{Branch, Repository},
|
||||
};
|
||||
use std::path::Path;
|
||||
@@ -21,21 +21,21 @@ mod directory {
|
||||
// find_entry for a file.
|
||||
let path = Path::new("src/memory.rs");
|
||||
let entry = root.find_entry(&path, &repo).unwrap();
|
||||
assert!(matches!(entry, Some(directory::Entry::File(_))));
|
||||
assert!(matches!(entry, Some(fs::Entry::File(_))));
|
||||
|
||||
// find_entry for a directory.
|
||||
let path = Path::new("this/is/a/really/deeply/nested/directory/tree");
|
||||
let entry = root.find_entry(&path, &repo).unwrap();
|
||||
assert!(matches!(entry, Some(directory::Entry::Directory(_))));
|
||||
assert!(matches!(entry, Some(fs::Entry::Directory(_))));
|
||||
|
||||
// find_entry for a non-leaf directory and its relative path.
|
||||
let path = Path::new("text");
|
||||
let entry = root.find_entry(&path, &repo).unwrap();
|
||||
assert!(matches!(entry, Some(directory::Entry::Directory(_))));
|
||||
if let Some(directory::Entry::Directory(sub_dir)) = entry {
|
||||
assert!(matches!(entry, Some(fs::Entry::Directory(_))));
|
||||
if let Some(fs::Entry::Directory(sub_dir)) = entry {
|
||||
let inner_path = Path::new("garden.txt");
|
||||
let inner_entry = sub_dir.find_entry(&inner_path, &repo).unwrap();
|
||||
assert!(matches!(inner_entry, Some(directory::Entry::File(_))));
|
||||
assert!(matches!(inner_entry, Some(fs::Entry::File(_))));
|
||||
}
|
||||
|
||||
// find_entry for non-existing file
|
||||
@@ -108,8 +108,8 @@ mod directory {
|
||||
|
||||
let path = Path::new("src");
|
||||
let entry = root.find_entry(&path, &repo).unwrap();
|
||||
assert!(matches!(entry, Some(directory::Entry::Directory(_))));
|
||||
if let Some(directory::Entry::Directory(d)) = entry {
|
||||
assert!(matches!(entry, Some(fs::Entry::Directory(_))));
|
||||
if let Some(fs::Entry::Directory(d)) = entry {
|
||||
assert_eq!(16297, d.size(&repo).unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ use std::path::Path;
|
||||
|
||||
use git_ref_format::refname;
|
||||
use radicle_surf::{
|
||||
file_system::{directory, Directory},
|
||||
fs::{self, Directory},
|
||||
git::{Branch, Repository},
|
||||
};
|
||||
|
||||
@@ -22,14 +22,14 @@ fn iterate_root_dir_recursive() {
|
||||
/// recursively.
|
||||
/// Returns the number of items visited (i.e. printed)
|
||||
fn println_dir(dir: &Directory, repo: &Repository) -> i32 {
|
||||
dir.traverse::<directory::error::Directory, _, _>(
|
||||
dir.traverse::<fs::error::Directory, _, _>(
|
||||
repo,
|
||||
(0, 0),
|
||||
&mut |(count, indent_level), entry| {
|
||||
println!("> {}{}", " ".repeat(indent_level * 4), entry.name());
|
||||
match entry {
|
||||
directory::Entry::File(_) => Ok((count + 1, indent_level)),
|
||||
directory::Entry::Directory(_) => Ok((count + 1, indent_level + 1)),
|
||||
fs::Entry::File(_) => Ok((count + 1, indent_level)),
|
||||
fs::Entry::Directory(_) => Ok((count + 1, indent_level + 1)),
|
||||
}
|
||||
},
|
||||
)
|
||||
@@ -49,7 +49,7 @@ fn browse_repo_lazily() {
|
||||
assert_eq!(count, 36);
|
||||
|
||||
fn traverse(dir: &Directory, repo: &Repository) -> i32 {
|
||||
dir.traverse::<directory::error::Directory, _, _>(repo, 0, &mut |count, _| Ok(count + 1))
|
||||
dir.traverse::<fs::error::Directory, _, _>(repo, 0, &mut |count, _| Ok(count + 1))
|
||||
.unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user