use anyhow::Result; use clap::Parser; use std::path::Path; use crate::{ git_fs::{gitignore::expands_and_filter_path, index::Index, normalize_path_in_worktree}, subcommands::hash_object::HashObjectSubcommand, }; use super::Subcommand; #[derive(Parser, Debug)] pub struct AddSubcommand { pub paths: Vec, } impl Subcommand for AddSubcommand { fn run(&self) -> Result { if self.paths.is_empty() { return Ok(String::from("Nothing specified, nothing added")); } let mut index = Index::load()?; for path in self.paths.iter().map(|p| Path::new(p).to_path_buf()) { let expanded = expands_and_filter_path(path)?; for p in expanded { println!("Adding {}", p.display()); let path_str = String::from(p.to_str().unwrap()); let name = String::from(normalize_path_in_worktree(&p)?.to_str().unwrap()); println!("adding {}", name); let hash = HashObjectSubcommand { write: true, path: path_str, } .run_raw()?; index.add_file(name, hash)?; } } index.save()?; Ok(String::from("")) } }