1use std::path::Path;
2
3use colored::Colorize;
4
5use crate::error::{Error, Result};
6use crate::git;
7use crate::side_repo::SideRepo;
8use crate::tracked::TrackedPaths;
9
10pub fn run(path: &Path) -> Result<()> {
16 let work_tree = git::repo_root()?;
17
18 let relative_path = if path.is_absolute() {
20 path.strip_prefix(&work_tree)
21 .map_or_else(|_| path.to_path_buf(), Path::to_path_buf)
22 } else {
23 path.to_path_buf()
24 };
25
26 let full_path = work_tree.join(&relative_path);
28 if !full_path.exists() {
29 return Err(Error::PathNotFound(relative_path));
30 }
31
32 let repo = SideRepo::open()?;
34 repo.ensure_initialized()?;
35
36 let mut tracked = TrackedPaths::load(&repo)?;
38
39 if tracked.contains(&relative_path) {
41 return Err(Error::PathAlreadyTracked(relative_path));
42 }
43
44 tracked.add(&relative_path);
46 tracked.save()?;
47
48 repo.stage(&relative_path)?;
50
51 repo.stage_tracked_file()?;
53
54 println!(
55 "{} {}",
56 "Tracking:".green().bold(),
57 relative_path.display()
58 );
59
60 Ok(())
61}