Files
rgit/src/subcommands/add.rs
T
2026-03-11 17:49:08 +01:00

46 lines
1.3 KiB
Rust

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<String>,
}
impl Subcommand for AddSubcommand {
fn run(&self) -> Result<String> {
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(""))
}
}