git_worktree_manager/
shared_files.rs1use std::path::Path;
6
7use console::style;
8
9pub fn parse_cwshare(repo_path: &Path) -> Vec<String> {
11 let cwshare_path = repo_path.join(".cwshare");
12 if !cwshare_path.exists() {
13 return Vec::new();
14 }
15
16 match std::fs::read_to_string(&cwshare_path) {
17 Ok(content) => content
18 .lines()
19 .map(|l| l.trim())
20 .filter(|l| !l.is_empty() && !l.starts_with('#'))
21 .map(String::from)
22 .collect(),
23 Err(_) => Vec::new(),
24 }
25}
26
27pub fn share_files(source_repo: &Path, target_worktree: &Path) {
29 let paths = parse_cwshare(source_repo);
30 if paths.is_empty() {
31 return;
32 }
33
34 println!("\n{}", style("Copying shared files:").cyan().bold());
35
36 for rel_path in &paths {
37 let source = source_repo.join(rel_path);
38 let target = target_worktree.join(rel_path);
39
40 if !source.exists() {
41 continue;
42 }
43 if target.exists() {
44 println!(
45 " {} Skipped (already exists): {}",
46 style("-").dim(),
47 rel_path
48 );
49 continue;
50 }
51
52 let result = if source.is_dir() {
53 copy_dir_recursive(&source, &target)
54 } else {
55 if let Some(parent) = target.parent() {
56 let _ = std::fs::create_dir_all(parent);
57 }
58 std::fs::copy(&source, &target).map(|_| ())
59 };
60
61 match result {
62 Ok(()) => println!(" {} Copied: {}", style("*").green(), rel_path),
63 Err(e) => println!(" {} Failed: {}: {}", style("!").yellow(), rel_path, e),
64 }
65 }
66 println!();
67}
68
69fn copy_dir_recursive(src: &Path, dst: &Path) -> std::io::Result<()> {
70 std::fs::create_dir_all(dst)?;
71 for entry in std::fs::read_dir(src)? {
72 let entry = entry?;
73 let ty = entry.file_type()?;
74 let dst_path = dst.join(entry.file_name());
75 if ty.is_dir() {
76 copy_dir_recursive(&entry.path(), &dst_path)?;
77 } else if ty.is_symlink() {
78 let target = std::fs::read_link(entry.path())?;
79 #[cfg(unix)]
80 std::os::unix::fs::symlink(&target, &dst_path)?;
81 #[cfg(windows)]
82 std::os::windows::fs::symlink_file(&target, &dst_path)?;
83 } else {
84 std::fs::copy(entry.path(), &dst_path)?;
85 }
86 }
87 Ok(())
88}