gwm 0.3.4

Git Worktree Manager - A CLI tool for managing Git worktrees with an interactive TUI
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
//! File copying utilities for worktree creation.
//!
//! Provides functionality to copy gitignored files (like `.env`) from the source
//! worktree to newly created worktrees.
//!
//! # Overview
//!
//! When a new worktree is created, certain files that are gitignored should be
//! copied from the main worktree to ensure the development environment works
//! correctly. Common examples include:
//!
//! - `.env` files with environment variables
//! - `.env.local` files with local overrides
//!
//! # Configuration
//!
//! The copy behavior is configured in `config.toml`:
//!
//! ```toml
//! [copy_ignored_files]
//! enabled = true
//! patterns = [".env", ".env.*", ".env.local", ".env.*.local"]
//! exclude_patterns = [".env.example", ".env.sample"]
//! ```
//!
//! # Pattern Matching
//!
//! - `patterns`: Glob patterns for files to include. **If empty, all gitignored
//!   files in the worktree will be considered as copy candidates** (determined
//!   by `git check-ignore`).
//! - `exclude_patterns`: Glob patterns for files to exclude (takes precedence)
//!
//! # Differences from TypeScript version
//!
//! In the TypeScript version, if both `patterns` and `exclude_patterns` are empty,
//! no files are copied. In this Rust version, if `patterns` is empty, all gitignored
//! files are copied (minus any `exclude_patterns`).

use std::collections::HashSet;
use std::fs;
use std::io::Write as _;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};

use glob::Pattern;
use walkdir::WalkDir;

use crate::config::CopyIgnoredFilesConfig;
use crate::error::Result;

/// ディレクトリを再帰的にコピーする
///
/// # Arguments
///
/// * `source` - コピー元ディレクトリのパス
/// * `target` - コピー先ディレクトリのパス
///
/// # Returns
///
/// コピーされたファイル数
fn copy_dir_recursive(source: &Path, target: &Path) -> std::io::Result<u64> {
    fs::create_dir_all(target)?;
    let mut count = 0;
    for entry in fs::read_dir(source)? {
        let entry = entry?;
        let source_path = entry.path();
        let target_path = target.join(entry.file_name());

        // シンボリックリンクは明示的にスキップ(循環参照防止)
        if source_path.is_symlink() {
            continue;
        }

        if source_path.is_dir() {
            count += copy_dir_recursive(&source_path, &target_path)?;
        } else if source_path.is_file() {
            fs::copy(&source_path, &target_path)?;
            count += 1;
        }
    }
    Ok(count)
}

/// ワークツリー内の gitignore されたファイルを取得する
///
/// # Arguments
///
/// * `worktree` - ワークツリーのパス
///
/// # Returns
///
/// gitignore されたファイルのパスの集合
///
/// # Errors
///
/// - ワークツリーが git リポジトリでない場合
/// - `git check-ignore` コマンドの実行に失敗した場合
/// - ディレクトリの走査中にエラーが発生した場合(警告のみ出力し継続)
fn get_gitignored_files(worktree: &Path) -> Result<HashSet<PathBuf>> {
    let mut ignored_files = HashSet::new();

    // ワークツリー内の全ファイルを再帰的に列挙
    let all_paths: Vec<PathBuf> = WalkDir::new(worktree)
        .into_iter()
        .filter_map(|entry_result| match entry_result {
            Ok(entry) => Some(entry),
            Err(err) => {
                eprintln!("Warning: Error walking directory: {}", err);
                None
            }
        })
        .filter(|e| e.path() != worktree) // ルート自体はスキップ
        .filter(|e| !e.path().to_string_lossy().contains("/.git/")) // .git ディレクトリはスキップ
        .filter(|e| e.file_type().is_file() || e.file_type().is_dir())
        .map(|e| e.path().to_path_buf())
        .collect();

    if all_paths.is_empty() {
        return Ok(ignored_files);
    }

    // git check-ignore --stdin -z を使って gitignore されたファイルを判定
    let mut child = Command::new("git")
        .args([
            "-C",
            &worktree.to_string_lossy(),
            "check-ignore",
            "--stdin",
            "-z",
        ])
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()?;

    // 全パスを stdin に書き込む(NUL区切り)
    if let Some(mut stdin) = child.stdin.take() {
        for path in &all_paths {
            if let Ok(relative) = path.strip_prefix(worktree) {
                if let Err(e) = stdin.write_all(relative.to_string_lossy().as_bytes()) {
                    eprintln!("Warning: Failed to write path to git check-ignore: {}", e);
                    continue;
                }
                if let Err(e) = stdin.write_all(b"\0") {
                    eprintln!(
                        "Warning: Failed to write null byte to git check-ignore: {}",
                        e
                    );
                    continue; // プロトコル破損を防ぐためスキップ
                }
            }
        }
    }

    let output = child.wait_with_output()?;

    // git check-ignore の終了コード:
    // - 0: 少なくとも1つのパスが無視された
    // - 1: どのパスも無視されなかった(正常)
    // - 128: not a git repository(空の結果を返す)
    // - その他: エラー
    if !output.status.success() {
        let exit_code = output.status.code();
        // exit code 1 は「無視されたファイルなし」を意味するので正常
        if exit_code != Some(1) {
            // exit code 128 は「not a git repository」を意味する
            // この場合は gitignore されたファイルがないため、空の結果を返す
            if exit_code == Some(128) {
                return Ok(ignored_files);
            }

            let stderr = String::from_utf8_lossy(&output.stderr);
            if !stderr.is_empty() {
                eprintln!("Warning: git check-ignore stderr: {}", stderr.trim());
            }
            // 致命的なエラーの場合はエラーを返す
            if exit_code.is_some() && exit_code != Some(0) && exit_code != Some(1) {
                return Err(crate::error::GwmError::git_command(format!(
                    "git check-ignore failed with exit code {:?}: {}",
                    exit_code,
                    stderr.trim()
                )));
            }
        }
    }

    // 出力をパース(NUL区切り)
    let stdout = String::from_utf8_lossy(&output.stdout);
    for ignored_path in stdout.split('\0') {
        if !ignored_path.is_empty() {
            ignored_files.insert(worktree.join(ignored_path));
        }
    }

    Ok(ignored_files)
}

/// Result of copying gitignored files.
#[derive(Debug, Default)]
pub struct CopyResult {
    /// Files that were successfully copied.
    pub copied: Vec<String>,
    /// Files that were skipped due to exclude patterns.
    pub skipped: Vec<String>,
    /// Files that already existed in target (not overwritten).
    pub existing: Vec<String>,
    /// Files that failed to copy (with error messages).
    pub failed: Vec<(String, String)>,
}

impl CopyResult {
    /// Returns true if any files were copied.
    pub fn has_copied(&self) -> bool {
        !self.copied.is_empty()
    }

    /// Returns true if any files failed to copy.
    pub fn has_failed(&self) -> bool {
        !self.failed.is_empty()
    }

    /// Returns a summary message for display.
    pub fn summary(&self) -> String {
        let mut parts = Vec::new();

        if !self.copied.is_empty() {
            parts.push(format!(
                "Copied {} file(s): {}",
                self.copied.len(),
                self.copied.join(", ")
            ));
        }

        if !self.failed.is_empty() {
            let failed_names: Vec<_> = self.failed.iter().map(|(name, _)| name.as_str()).collect();
            parts.push(format!(
                "Failed {} file(s): {}",
                self.failed.len(),
                failed_names.join(", ")
            ));
        }

        if parts.is_empty() {
            "No files copied".to_string()
        } else {
            parts.join("; ")
        }
    }
}

/// Copy gitignored files from source worktree to target worktree.
///
/// # Arguments
///
/// * `source_worktree` - Path to the source worktree (usually the main worktree)
/// * `target_worktree` - Path to the newly created worktree
/// * `config` - Configuration for which files to copy
///
/// # Returns
///
/// A `CopyResult` containing information about copied and skipped files.
///
/// # Example
///
/// ```ignore
/// use gwm::utils::copy::copy_ignored_files;
/// use gwm::config::CopyIgnoredFilesConfig;
///
/// let config = CopyIgnoredFilesConfig::default();
/// let result = copy_ignored_files(
///     Path::new("/path/to/main"),
///     Path::new("/path/to/new-worktree"),
///     &config,
/// )?;
///
/// for file in &result.copied {
///     println!("Copied: {}", file);
/// }
/// ```
pub fn copy_ignored_files(
    source_worktree: &Path,
    target_worktree: &Path,
    config: &CopyIgnoredFilesConfig,
) -> Result<CopyResult> {
    // If copying is disabled, return empty result
    if !config.enabled {
        return Ok(CopyResult::default());
    }

    let mut result = CopyResult::default();

    // Compile exclude patterns for filtering
    let exclude_patterns: Vec<Pattern> = config
        .exclude_patterns
        .iter()
        .filter_map(|p| match Pattern::new(p) {
            Ok(pattern) => Some(pattern),
            Err(e) => {
                eprintln!("Warning: Invalid exclude pattern '{}': {}", p, e);
                None
            }
        })
        .collect();

    // Use glob to find matching files/directories
    let mut matched_paths: HashSet<PathBuf> = HashSet::new();

    if config.patterns.is_empty() {
        // patterns が空の場合: gitignore されたファイルのみを対象
        matched_paths = get_gitignored_files(source_worktree)?;
    } else {
        // patterns が指定されている場合: glob でマッチするものを対象
        for pattern in &config.patterns {
            let full_pattern = source_worktree.join(pattern);
            let pattern_str = full_pattern.to_string_lossy();

            match glob::glob(&pattern_str) {
                Ok(paths) => {
                    for entry in paths.flatten() {
                        matched_paths.insert(entry);
                    }
                }
                Err(e) => {
                    eprintln!("Warning: Invalid glob pattern '{}': {}", pattern, e);
                }
            }
        }
    }

    // Process matched paths
    for source_path in matched_paths {
        // Get relative path from source_worktree
        let relative_path = match source_path.strip_prefix(source_worktree) {
            Ok(p) => p,
            Err(e) => {
                eprintln!(
                    "Warning: Failed to get relative path for '{}': {}",
                    source_path.display(),
                    e
                );
                continue;
            }
        };
        let relative_str = relative_path.to_string_lossy().to_string();

        // Check if matches any exclude pattern
        let matches_exclude = exclude_patterns.iter().any(|p| p.matches(&relative_str));
        if matches_exclude {
            result.skipped.push(relative_str);
            continue;
        }

        let target_path = target_worktree.join(relative_path);

        // Check if target already exists
        if target_path.exists() {
            result.existing.push(relative_str);
            continue;
        }

        // Ensure parent directory exists
        if let Some(parent) = target_path.parent() {
            if !parent.exists() {
                if let Err(e) = fs::create_dir_all(parent) {
                    result.failed.push((relative_str, e.to_string()));
                    continue;
                }
            }
        }

        if source_path.is_dir() {
            // ディレクトリの場合は再帰的にコピー
            match copy_dir_recursive(&source_path, &target_path) {
                Ok(count) => {
                    result
                        .copied
                        .push(format!("{}/ ({} files)", relative_str, count));
                }
                Err(e) => {
                    result.failed.push((relative_str, e.to_string()));
                }
            }
        } else if source_path.is_file() {
            // ファイルの場合は通常コピー
            match fs::copy(&source_path, &target_path) {
                Ok(_) => result.copied.push(relative_str),
                Err(e) => result.failed.push((relative_str, e.to_string())),
            }
        }
        // シンボリックリンク等はスキップ
    }

    Ok(result)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs::File;
    use std::io::Write;
    use std::process::Command;
    use tempfile::TempDir;

    fn create_test_config() -> CopyIgnoredFilesConfig {
        CopyIgnoredFilesConfig {
            enabled: true,
            patterns: vec![".env".to_string(), ".env.*".to_string()],
            exclude_patterns: vec![".env.example".to_string(), ".env.sample".to_string()],
        }
    }

    /// テスト用のgitリポジトリを初期化する
    fn init_git_repo(path: &Path) {
        Command::new("git")
            .args(["init"])
            .current_dir(path)
            .output()
            .expect("Failed to init git repo");

        // 最小限のgit設定
        Command::new("git")
            .args(["config", "user.email", "test@example.com"])
            .current_dir(path)
            .output()
            .ok();
        Command::new("git")
            .args(["config", "user.name", "Test User"])
            .current_dir(path)
            .output()
            .ok();
    }

    /// .gitignore ファイルを作成する
    fn create_gitignore(path: &Path, patterns: &[&str]) {
        let gitignore_path = path.join(".gitignore");
        let mut file = File::create(gitignore_path).expect("Failed to create .gitignore");
        for pattern in patterns {
            writeln!(file, "{}", pattern).expect("Failed to write .gitignore");
        }
    }

    #[test]
    fn test_copy_env_file() {
        let source = TempDir::new().unwrap();
        let target = TempDir::new().unwrap();

        // Create .env file in source
        let env_path = source.path().join(".env");
        let mut file = File::create(env_path).unwrap();
        writeln!(file, "API_KEY=secret").unwrap();

        let config = create_test_config();
        let result = copy_ignored_files(source.path(), target.path(), &config).unwrap();

        assert_eq!(result.copied, vec![".env"]);
        assert!(target.path().join(".env").exists());

        // Verify content was copied correctly
        let content = fs::read_to_string(target.path().join(".env")).unwrap();
        assert!(content.contains("API_KEY=secret"));
    }

    #[test]
    fn test_skip_excluded_files() {
        let source = TempDir::new().unwrap();
        let target = TempDir::new().unwrap();

        // Create .env.example in source (should be excluded)
        File::create(source.path().join(".env.example")).unwrap();

        let config = create_test_config();
        let result = copy_ignored_files(source.path(), target.path(), &config).unwrap();

        assert!(result.copied.is_empty());
        assert_eq!(result.skipped, vec![".env.example"]);
        assert!(!target.path().join(".env.example").exists());
    }

    #[test]
    fn test_skip_existing_files() {
        let source = TempDir::new().unwrap();
        let target = TempDir::new().unwrap();

        // Create .env in both source and target
        File::create(source.path().join(".env")).unwrap();
        File::create(target.path().join(".env")).unwrap();

        let config = create_test_config();
        let result = copy_ignored_files(source.path(), target.path(), &config).unwrap();

        assert!(result.copied.is_empty());
        assert_eq!(result.existing, vec![".env"]);
    }

    #[test]
    fn test_disabled_config() {
        let source = TempDir::new().unwrap();
        let target = TempDir::new().unwrap();

        File::create(source.path().join(".env")).unwrap();

        let config = CopyIgnoredFilesConfig {
            enabled: false,
            patterns: vec![".env".to_string()],
            exclude_patterns: vec![],
        };

        let result = copy_ignored_files(source.path(), target.path(), &config).unwrap();

        assert!(result.copied.is_empty());
        assert!(!target.path().join(".env").exists());
    }

    #[test]
    fn test_multiple_env_files() {
        let source = TempDir::new().unwrap();
        let target = TempDir::new().unwrap();

        // Create multiple .env files
        File::create(source.path().join(".env")).unwrap();
        File::create(source.path().join(".env.local")).unwrap();
        File::create(source.path().join(".env.development")).unwrap();
        File::create(source.path().join(".env.example")).unwrap(); // excluded

        let config = create_test_config();
        let result = copy_ignored_files(source.path(), target.path(), &config).unwrap();

        assert_eq!(result.copied.len(), 3);
        assert!(result.copied.contains(&".env".to_string()));
        assert!(result.copied.contains(&".env.local".to_string()));
        assert!(result.copied.contains(&".env.development".to_string()));
        assert_eq!(result.skipped, vec![".env.example"]);
    }

    #[test]
    fn test_copy_result_summary() {
        let mut result = CopyResult::default();
        assert_eq!(result.summary(), "No files copied");

        result.copied = vec![".env".to_string(), ".env.local".to_string()];
        assert_eq!(result.summary(), "Copied 2 file(s): .env, .env.local");
    }

    /// exclude_patternsのみ設定時(patternsが空)のテスト
    /// patternsが空の場合は gitignore されたファイルを対象とし、exclude_patternsで除外する
    #[test]
    fn test_exclude_patterns_only() {
        let source = TempDir::new().unwrap();
        let target = TempDir::new().unwrap();

        // git リポジトリを初期化
        init_git_repo(source.path());

        // .gitignore を作成(.env*, config.json を無視)
        create_gitignore(source.path(), &[".env*", "config.json"]);

        // Create multiple files in source
        File::create(source.path().join(".env")).unwrap();
        File::create(source.path().join(".env.local")).unwrap();
        File::create(source.path().join(".env.example")).unwrap();
        File::create(source.path().join("config.json")).unwrap();
        File::create(source.path().join("tracked.txt")).unwrap(); // git管理対象

        // patterns is empty → gitignored ファイルが対象
        // exclude_patterns で .env.example を除外
        let config = CopyIgnoredFilesConfig {
            enabled: true,
            patterns: vec![], // empty → gitignored ファイルが対象
            exclude_patterns: vec![".env.example".to_string()],
        };

        let result = copy_ignored_files(source.path(), target.path(), &config).unwrap();

        // gitignored ファイルのうち、exclude で除外されていないものがコピーされる
        assert!(result.copied.contains(&".env".to_string()));
        assert!(result.copied.contains(&".env.local".to_string()));
        assert!(result.copied.contains(&"config.json".to_string()));
        // .env.example は exclude で除外
        assert!(result.skipped.contains(&".env.example".to_string()));
        // tracked.txt は gitignored ではないのでコピーされない
        assert!(!result.copied.contains(&"tracked.txt".to_string()));
    }

    /// patternsとexclude_patternsの両方が空の場合は gitignored ファイル全部をコピー
    #[test]
    fn test_empty_patterns_and_exclude_patterns() {
        let source = TempDir::new().unwrap();
        let target = TempDir::new().unwrap();

        // git リポジトリを初期化
        init_git_repo(source.path());

        // .gitignore を作成(.env のみ無視)
        create_gitignore(source.path(), &[".env"]);

        // Create files in source
        File::create(source.path().join(".env")).unwrap();
        File::create(source.path().join("config.json")).unwrap(); // git管理対象

        // Both patterns and exclude_patterns are empty → gitignored ファイル全部がコピー対象
        let config = CopyIgnoredFilesConfig {
            enabled: true,
            patterns: vec![],
            exclude_patterns: vec![],
        };

        let result = copy_ignored_files(source.path(), target.path(), &config).unwrap();

        // gitignored ファイル (.env) のみコピーされる
        assert!(result.copied.contains(&".env".to_string()));
        // config.json は gitignored ではないのでコピーされない
        assert!(!result.copied.contains(&"config.json".to_string()));
    }

    /// ディレクトリを再帰的にコピーするテスト
    #[test]
    fn test_copy_directory() {
        let source = TempDir::new().unwrap();
        let target = TempDir::new().unwrap();

        // ディレクトリ構造を作成
        let docs_dir = source.path().join(".local_docs");
        fs::create_dir(&docs_dir).unwrap();
        let mut file = File::create(docs_dir.join("note.md")).unwrap();
        writeln!(file, "# Test Note").unwrap();

        // サブディレクトリも作成
        let sub_dir = docs_dir.join("sub");
        fs::create_dir(&sub_dir).unwrap();
        File::create(sub_dir.join("nested.txt")).unwrap();

        let config = CopyIgnoredFilesConfig {
            enabled: true,
            patterns: vec![".local_docs".to_string()],
            exclude_patterns: vec![],
        };

        let result = copy_ignored_files(source.path(), target.path(), &config).unwrap();

        // ディレクトリがコピーされたことを確認
        assert_eq!(result.copied.len(), 1);
        assert!(result.copied[0].starts_with(".local_docs/"));
        assert!(result.copied[0].contains("2 files")); // note.md と nested.txt

        // ファイルが存在することを確認
        assert!(target.path().join(".local_docs").exists());
        assert!(target.path().join(".local_docs").is_dir());
        assert!(target.path().join(".local_docs/note.md").exists());
        assert!(target.path().join(".local_docs/sub/nested.txt").exists());

        // 内容が正しくコピーされたことを確認
        let content = fs::read_to_string(target.path().join(".local_docs/note.md")).unwrap();
        assert!(content.contains("# Test Note"));
    }

    /// 既存ディレクトリはスキップされるテスト
    #[test]
    fn test_skip_existing_directory() {
        let source = TempDir::new().unwrap();
        let target = TempDir::new().unwrap();

        // ソースにディレクトリを作成
        let docs_dir = source.path().join(".local_docs");
        fs::create_dir(&docs_dir).unwrap();
        File::create(docs_dir.join("note.md")).unwrap();

        // ターゲットに同名ディレクトリを作成
        fs::create_dir(target.path().join(".local_docs")).unwrap();

        let config = CopyIgnoredFilesConfig {
            enabled: true,
            patterns: vec![".local_docs".to_string()],
            exclude_patterns: vec![],
        };

        let result = copy_ignored_files(source.path(), target.path(), &config).unwrap();

        // 既存としてスキップされる
        assert!(result.copied.is_empty());
        assert_eq!(result.existing, vec![".local_docs"]);
    }

    /// ファイルとディレクトリの混在テスト
    #[test]
    fn test_copy_files_and_directories() {
        let source = TempDir::new().unwrap();
        let target = TempDir::new().unwrap();

        // ファイルを作成
        File::create(source.path().join(".env")).unwrap();

        // ディレクトリを作成
        let docs_dir = source.path().join(".local_docs");
        fs::create_dir(&docs_dir).unwrap();
        File::create(docs_dir.join("note.md")).unwrap();

        let config = CopyIgnoredFilesConfig {
            enabled: true,
            patterns: vec![".env".to_string(), ".local_docs".to_string()],
            exclude_patterns: vec![],
        };

        let result = copy_ignored_files(source.path(), target.path(), &config).unwrap();

        // ファイルとディレクトリの両方がコピーされる
        assert_eq!(result.copied.len(), 2);
        assert!(result.copied.contains(&".env".to_string()));
        assert!(result.copied.iter().any(|s| s.starts_with(".local_docs/")));

        // ファイルが存在することを確認
        assert!(target.path().join(".env").exists());
        assert!(target.path().join(".local_docs/note.md").exists());
    }

    /// サブディレクトリ内のファイル/ディレクトリをパスパターンでコピーするテスト
    #[test]
    fn test_copy_subdirectory_path_pattern() {
        let source = TempDir::new().unwrap();
        let target = TempDir::new().unwrap();

        // サブディレクトリ構造を作成 (gwm-rust/.003_local_temp_docs のようなケース)
        let subdir = source.path().join("subproject");
        fs::create_dir(&subdir).unwrap();
        let docs_dir = subdir.join(".local_docs");
        fs::create_dir(&docs_dir).unwrap();
        let mut file = File::create(docs_dir.join("note.md")).unwrap();
        writeln!(file, "# Nested Note").unwrap();

        // パスを含むパターンでコピー
        let config = CopyIgnoredFilesConfig {
            enabled: true,
            patterns: vec!["subproject/.local_docs".to_string()],
            exclude_patterns: vec![],
        };

        let result = copy_ignored_files(source.path(), target.path(), &config).unwrap();

        // サブディレクトリ内のディレクトリがコピーされる
        assert_eq!(result.copied.len(), 1);
        assert!(result.copied[0].contains(".local_docs"));

        // ファイルが存在することを確認
        assert!(target.path().join("subproject/.local_docs").exists());
        assert!(target
            .path()
            .join("subproject/.local_docs/note.md")
            .exists());

        // 内容が正しくコピーされたことを確認
        let content =
            fs::read_to_string(target.path().join("subproject/.local_docs/note.md")).unwrap();
        assert!(content.contains("# Nested Note"));
    }

    /// glob パターン (**) を使ったテスト
    #[test]
    fn test_copy_with_glob_pattern() {
        let source = TempDir::new().unwrap();
        let target = TempDir::new().unwrap();

        // 複数階層のディレクトリ構造を作成
        let dir1 = source.path().join("dir1");
        fs::create_dir(&dir1).unwrap();
        File::create(dir1.join(".env")).unwrap();

        let dir2 = source.path().join("dir2");
        fs::create_dir(&dir2).unwrap();
        File::create(dir2.join(".env")).unwrap();

        // ルートにも .env を作成
        File::create(source.path().join(".env")).unwrap();

        // **/.env パターンでコピー (ルートの .env は含まれない)
        let config = CopyIgnoredFilesConfig {
            enabled: true,
            patterns: vec!["**/.env".to_string()],
            exclude_patterns: vec![],
        };

        let result = copy_ignored_files(source.path(), target.path(), &config).unwrap();

        // サブディレクトリの .env がコピーされる
        assert!(result.copied.len() >= 2);
        assert!(target.path().join("dir1/.env").exists());
        assert!(target.path().join("dir2/.env").exists());
    }

    /// patterns が空の場合、サブディレクトリ内の gitignored ファイルもコピーされる
    #[test]
    fn test_patterns_empty_copies_nested_gitignored_files() {
        let source = TempDir::new().unwrap();
        let target = TempDir::new().unwrap();

        // git リポジトリを初期化
        init_git_repo(source.path());

        // サブディレクトリを作成
        let subdir = source.path().join("config");
        fs::create_dir(&subdir).unwrap();

        // .gitignore を作成(.env* を無視)
        create_gitignore(source.path(), &[".env*"]);

        // ルートと サブディレクトリに .env を作成
        File::create(source.path().join(".env")).unwrap();
        File::create(subdir.join(".env.local")).unwrap();

        let config = CopyIgnoredFilesConfig {
            enabled: true,
            patterns: vec![], // empty → gitignored ファイルが対象
            exclude_patterns: vec![],
        };

        let result = copy_ignored_files(source.path(), target.path(), &config).unwrap();

        // ルートとサブディレクトリの両方の gitignored ファイルがコピーされる
        assert!(result.copied.contains(&".env".to_string()));
        assert!(result.copied.contains(&"config/.env.local".to_string()));
    }

    /// 非gitリポジトリの場合、patterns が空なら何もコピーしない
    #[test]
    fn test_patterns_empty_in_non_git_repo() {
        let source = TempDir::new().unwrap();
        let target = TempDir::new().unwrap();

        // git リポジトリを初期化しない(非gitリポジトリ)

        // ファイルを作成
        File::create(source.path().join(".env")).unwrap();
        File::create(source.path().join("config.json")).unwrap();

        let config = CopyIgnoredFilesConfig {
            enabled: true,
            patterns: vec![], // empty → gitignored ファイルが対象だが、git repo ではない
            exclude_patterns: vec![],
        };

        let result = copy_ignored_files(source.path(), target.path(), &config).unwrap();

        // 非gitリポジトリなので gitignored ファイルはない → 何もコピーされない
        assert!(result.copied.is_empty());
    }
}