summaryrefslogtreecommitdiff
path: root/src/git.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/git.rs')
-rw-r--r--src/git.rs40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/git.rs b/src/git.rs
index c07615d..d92b5a0 100644
--- a/src/git.rs
+++ b/src/git.rs
@@ -109,6 +109,33 @@ pub struct RepoMetadata {
pub description: String,
}
+pub struct VerboseRepoMetadata {
+ pub path: PathBuf,
+ pub description: String,
+ pub size: u64,
+}
+
+fn get_size(path: impl AsRef<Path>) -> Result<u64, ShackleError> {
+ let path_metadata = path.as_ref().symlink_metadata()?;
+
+ if path_metadata.is_dir() {
+ let mut size_in_bytes = path_metadata.len();
+ for entry in path.as_ref().read_dir()? {
+ let entry = entry?;
+ let entry_metadata = entry.metadata()?;
+
+ if entry_metadata.is_dir() {
+ size_in_bytes += get_size(entry.path())?;
+ } else {
+ size_in_bytes += entry_metadata.len();
+ }
+ }
+ Ok(size_in_bytes)
+ } else {
+ Ok(path_metadata.len())
+ }
+}
+
pub fn list() -> Result<Vec<RepoMetadata>, ShackleError> {
fn add_from_dir(
collection_dir: &Path,
@@ -163,6 +190,19 @@ pub fn list() -> Result<Vec<RepoMetadata>, ShackleError> {
Ok(results)
}
+pub fn list_verbose() -> Result<Vec<VerboseRepoMetadata>, ShackleError> {
+ list()?
+ .into_iter()
+ .map(|meta| {
+ get_size(&meta.path).map(|size| VerboseRepoMetadata {
+ path: meta.path,
+ description: meta.description,
+ size,
+ })
+ })
+ .collect()
+}
+
pub fn set_description(directory: &Path, description: &str) -> Result<(), ShackleError> {
if !is_valid_git_repo_path(directory)? {
return Err(ShackleError::InvalidDirectory);