dircat 1.0.1

High-performance Rust utility that concatenates and displays directory contents, similar to the C++ DirCat.
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
// src/discovery/entry_processor.rs

use crate::config::path_resolve::ResolvedInput;
use crate::config::DiscoveryConfig;
use crate::core_types::FileInfo;
use crate::errors::Error;
use crate::filtering::{
    check_process_last, is_file_type, is_lockfile, passes_extension_filters, passes_size_filter,
};
#[cfg(feature = "git")]
use crate::git;
use anyhow::Result;
use ignore::DirEntry;
use log::{debug, trace, warn};
use std::path::{Path, PathBuf};
use tracing::instrument;

/// Processes a single directory entry from the walk.
///
/// Performs filtering based on type, size, extensions, gitignore rules (handled by walker),
/// custom ignore patterns (handled by walker), path regex, filename regex, lockfile status,
/// and content type (text/binary).
///
/// Returns `Ok(Some(FileInfo))` if the entry is a file that passes all filters.
/// Returns `Ok(None)` if the entry is filtered out or is not a regular file.
/// Returns `Err(Error)` for critical errors (like permission issues accessing metadata or reading file head).
pub(crate) fn process_direntry(
    entry_result: Result<DirEntry, ignore::Error>,
    config: &DiscoveryConfig,
    resolved: &ResolvedInput,
) -> Result<Option<FileInfo>, Error> {
    // --- 1. Handle Walker Errors ---
    let entry = match entry_result {
        Ok(entry) => entry,
        Err(ignore_error) => {
            warn!("Walker error: {}", ignore_error);
            return Ok(None); // Skip this entry
        }
    };

    let absolute_path = entry.path().to_path_buf();
    trace!("Processing entry: {}", absolute_path.display());

    // --- 2. Calculate Relative Path ---
    let relative_path = if resolved.is_file {
        let path_from_git_url = {
            #[cfg(feature = "git")]
            {
                git::parse_github_folder_url(&resolved.display)
                    .map(|parsed_url| PathBuf::from(parsed_url.subdirectory))
            }
            #[cfg(not(feature = "git"))]
            {
                None
            }
        };

        path_from_git_url.unwrap_or_else(|| {
            // Fallback for local files or if git feature is off
            absolute_path
                .file_name()
                .map(PathBuf::from)
                .unwrap_or_else(|| {
                    warn!(
                        "Could not get filename for file input: {}",
                        absolute_path.display()
                    );
                    absolute_path.clone()
                })
        })
    } else {
        absolute_path
            .strip_prefix(&resolved.path)
            .map(|p| p.to_path_buf())
            .unwrap_or_else(|err| {
                warn!(
                    "Failed to strip prefix '{}' from '{}': {}. Using absolute path.",
                    resolved.path.display(),
                    absolute_path.display(),
                    err
                );
                absolute_path.clone()
            })
    };
    trace!("Calculated relative path: {}", relative_path.display());

    // --- 2a. Check "process last" status early, as it affects other filters ---
    let (is_last, last_order) = check_process_last(&relative_path, config);

    // Manual gitignore override logic is not needed here. When --last or --only is used,
    // the walker is configured with a high-precedence temporary ignore file containing
    // whitelist rules (`!pattern`) for the override patterns. This correctly causes the
    // walker to yield gitignored files that match an override pattern, while still
    // respecting other .gitignore rules.

    // --- 3. Get Metadata ---
    let metadata = match entry.metadata() {
        Ok(md) => md,
        Err(e) => {
            warn!(
                "Skipping entry '{}' due to metadata error: {}",
                absolute_path.display(),
                e
            );
            return Ok(None);
        }
    };

    // --- 4. Filter by File Type ---
    if !is_file_type(&metadata) {
        trace!("Skipping non-file entry: {}", absolute_path.display());
        return Ok(None);
    }
    trace!("Entry is a file: {}", absolute_path.display());

    // --- 5. Filter by Lockfile ---
    if config.skip_lockfiles && is_lockfile(&absolute_path) {
        debug!(
            "Skipping lockfile due to --no-lockfiles flag: {}",
            absolute_path.display()
        );
        return Ok(None);
    }
    trace!("File passed lockfile filter: {}", absolute_path.display());

    // --- 6. Filter by Size ---
    if !passes_size_filter(&metadata, config) {
        debug!(
            "Skipping file due to size constraint: {} (Size: {} bytes)",
            absolute_path.display(),
            metadata.len()
        );
        return Ok(None);
    }
    trace!("File passed size filter: {}", absolute_path.display());

    // --- 7. Filter by Extension ---
    if !passes_extension_filters(&absolute_path, config) {
        debug!(
            "Skipping file due to extension filter: {}",
            absolute_path.display()
        );
        return Ok(None);
    }
    trace!("File passed extension filter: {}", absolute_path.display());

    // --- 8. Filter by Regex (Path and Filename) ---
    if !passes_regex_filters(&absolute_path, &relative_path, config)? {
        debug!(
            "Skipping file due to regex filter: {}",
            absolute_path.display()
        );
        return Ok(None);
    }
    trace!("File passed regex filters: {}", absolute_path.display());

    // --- 10. Construct FileInfo ---
    let file_info = FileInfo {
        absolute_path,
        relative_path,
        size: metadata.len(),
        processed_content: None, // Content is read later in the processing stage
        counts: None,            // Counts are calculated later
        is_process_last: is_last,
        process_last_order: last_order,
        is_binary: false, // Will be determined during the processing stage
    };

    debug!(
        "Entry passed metadata filters: {}",
        file_info.relative_path.display()
    );
    Ok(Some(file_info))
}

/// Checks if a file passes the path and filename regex filters.
#[instrument(level = "debug", skip(config), fields(relative_path = %relative_path.display(), filename = ?path.file_name()))]
fn passes_regex_filters(
    path: &Path,          // Absolute path for filename extraction
    relative_path: &Path, // Relative path for path regex matching
    config: &DiscoveryConfig,
) -> Result<bool, Error> {
    // --- 1. Check Exclude Path Regex First (takes precedence) ---
    if let Some(exclude_path_regex_vec) = &config.exclude_path_regex {
        let relative_path_str = relative_path.to_string_lossy().replace('\\', "/");
        let is_excluded = exclude_path_regex_vec
            .iter()
            .any(|re| re.is_match(&relative_path_str));
        if is_excluded {
            debug!(
                "Path matched an exclude regex, skipping: {}",
                relative_path_str
            );
            return Ok(false);
        }
    }

    // --- 2. Check Include Path Regex ---
    if let Some(include_path_regex_vec) = &config.path_regex {
        let relative_path_str = relative_path.to_string_lossy().replace('\\', "/");
        let matches = include_path_regex_vec
            .iter()
            .any(|re| re.is_match(&relative_path_str));
        debug!(
            "Checking include path regex vector against relative path: regexes={:?}, path={}",
            include_path_regex_vec, relative_path_str,
        );
        if !matches {
            debug!("Path regex vector did not match relative path");
            return Ok(false);
        }
        debug!("Path regex vector matched relative path");
    }

    // --- 3. Check Include Filename Regex ---
    if let Some(filename_regex_vec) = &config.filename_regex {
        if let Some(filename) = path.file_name() {
            let filename_str = filename.to_string_lossy();
            let matches = filename_regex_vec
                .iter()
                .any(|re| re.is_match(&filename_str));
            debug!(
                "Checking filename regex vector: regexes={:?}, filename={}",
                filename_regex_vec, filename_str,
            );
            if !matches {
                debug!("Filename regex vector did not match");
                return Ok(false);
            }
            debug!("Filename regex vector matched");
        } else {
            debug!("Path has no filename component, failing filename regex match");
            return Ok(false);
        }
    }

    Ok(true)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::DiscoveryConfig;
    use regex::Regex;
    use std::collections::HashMap;
    use std::fs::{self, File};
    use std::path::PathBuf;
    use tempfile::tempdir;

    // Helper to create a Config with specific regex filters
    fn create_config_with_regex(
        path_patterns: Option<Vec<&str>>,
        exclude_path_patterns: Option<Vec<&str>>,
        filename_patterns: Option<Vec<&str>>,
    ) -> DiscoveryConfig {
        let mut regex_map: HashMap<String, Option<Vec<Regex>>> = HashMap::new();
        let pattern_map = HashMap::from([
            ("path", path_patterns),
            ("exclude_path", exclude_path_patterns),
            ("filename", filename_patterns),
        ]);

        for (key, patterns_opt) in pattern_map {
            let compiled = patterns_opt.map(|patterns| {
                patterns
                    .iter()
                    .map(|p| Regex::new(p).unwrap())
                    .collect::<Vec<_>>()
            });
            regex_map.insert(key.to_string(), compiled);
        }

        let mut config = DiscoveryConfig::default_for_test();
        config.path_regex = regex_map.remove("path").unwrap();
        config.exclude_path_regex = regex_map.remove("exclude_path").unwrap();
        config.filename_regex = regex_map.remove("filename").unwrap();
        config
    }

    // Helper to create paths for testing
    fn create_paths(base: &Path, relative: &str) -> (PathBuf, PathBuf) {
        (base.join(relative), PathBuf::from(relative))
    }

    #[test]
    fn test_regex_no_filters() -> Result<(), Error> {
        let dir = tempdir().unwrap();
        let (abs_path, rel_path) = create_paths(dir.path(), "test_file.txt");
        File::create(&abs_path).unwrap();
        let config = create_config_with_regex(None, None, None);
        assert!(passes_regex_filters(&abs_path, &rel_path, &config)?);
        Ok(())
    }

    #[test]
    fn test_regex_path_match() -> Result<(), Error> {
        let dir = tempdir().unwrap();
        let (abs_path1, rel_path1) = create_paths(dir.path(), "subdir/match_file.txt");
        let (abs_path2, rel_path2) = create_paths(dir.path(), "no_match_file.txt");
        fs::create_dir_all(abs_path1.parent().unwrap()).unwrap();
        File::create(&abs_path1).unwrap();
        File::create(&abs_path2).unwrap();

        // Regex that should match rel_path1 (starts with "subdir/")
        let config = create_config_with_regex(Some(vec!["^subdir/"]), None, None);

        assert!(passes_regex_filters(&abs_path1, &rel_path1, &config)?);
        assert!(!passes_regex_filters(&abs_path2, &rel_path2, &config)?);
        Ok(())
    }

    #[test]
    fn test_regex_exclude_path_match() -> Result<(), Error> {
        let dir = tempdir().unwrap();
        let (abs_path1, rel_path1) = create_paths(dir.path(), "src/main.rs");
        let (abs_path2, rel_path2) = create_paths(dir.path(), "tests/main.rs");
        fs::create_dir_all(abs_path1.parent().unwrap()).unwrap();
        fs::create_dir_all(abs_path2.parent().unwrap()).unwrap();
        File::create(&abs_path1).unwrap();
        File::create(&abs_path2).unwrap();

        // Exclude anything in the "tests/" directory
        let config = create_config_with_regex(None, Some(vec!["^tests/"]), None);

        assert!(passes_regex_filters(&abs_path1, &rel_path1, &config)?); // src/main.rs should pass
        assert!(!passes_regex_filters(&abs_path2, &rel_path2, &config)?); // tests/main.rs should be excluded
        Ok(())
    }

    #[test]
    fn test_regex_exclude_overrides_include() -> Result<(), Error> {
        let dir = tempdir().unwrap();
        let (abs_path1, rel_path1) = create_paths(dir.path(), "src/main.rs");
        let (abs_path2, rel_path2) = create_paths(dir.path(), "src/lib.rs");
        fs::create_dir_all(abs_path1.parent().unwrap()).unwrap();
        File::create(&abs_path1).unwrap();
        File::create(&abs_path2).unwrap();

        // Include everything in "src/", but exclude "main.rs"
        let config = create_config_with_regex(Some(vec!["^src/"]), Some(vec!["main\\.rs$"]), None);

        assert!(!passes_regex_filters(&abs_path1, &rel_path1, &config)?); // main.rs is excluded
        assert!(passes_regex_filters(&abs_path2, &rel_path2, &config)?); // lib.rs is included
        Ok(())
    }

    #[test]
    fn test_regex_path_match_windows_style_relative() -> Result<(), Error> {
        // Simulate a windows-style relative path string for the regex
        let dir = tempdir().unwrap();
        // Relative path uses backslashes, but gets normalized
        let (abs_path, rel_path) = create_paths(dir.path(), "subdir\\match_file.txt");
        fs::create_dir_all(abs_path.parent().unwrap()).unwrap();
        File::create(&abs_path).unwrap();

        // Regex uses forward slashes, should match normalized relative path
        let config_fwd = create_config_with_regex(Some(vec!["^subdir/match"]), None, None);
        // Regex uses backslashes, should NOT match normalized relative path
        let config_bwd = create_config_with_regex(Some(vec![r"^subdir\\match"]), None, None);

        assert!(passes_regex_filters(&abs_path, &rel_path, &config_fwd)?);
        assert!(!passes_regex_filters(&abs_path, &rel_path, &config_bwd)?);
        Ok(())
    }

    #[test]
    fn test_regex_filename_match() -> Result<(), Error> {
        let dir = tempdir().unwrap();
        let (abs_path1, rel_path1) = create_paths(dir.path(), "match_this.log");
        let (abs_path2, rel_path2) = create_paths(dir.path(), "ignore_this.txt");
        File::create(&abs_path1).unwrap();
        File::create(&abs_path2).unwrap();

        // Regex that should match path1's filename
        let config = create_config_with_regex(None, None, Some(vec![r"^match_.*\.log$"]));

        assert!(passes_regex_filters(&abs_path1, &rel_path1, &config)?);
        assert!(!passes_regex_filters(&abs_path2, &rel_path2, &config)?);
        Ok(())
    }

    #[test]
    fn test_regex_path_and_filename_match() -> Result<(), Error> {
        let dir = tempdir().unwrap();
        let (abs_path1, rel_path1) = create_paths(dir.path(), "target_dir/target_file.rs"); // Match both
        let (abs_path2, rel_path2) = create_paths(dir.path(), "target_dir/other_file.rs"); // Match path, fail filename
        let (abs_path3, rel_path3) = create_paths(dir.path(), "other_dir/target_file.rs"); // Fail path, match filename
        let (abs_path4, rel_path4) = create_paths(dir.path(), "other_dir/another_file.txt"); // Fail both
        fs::create_dir_all(abs_path1.parent().unwrap()).unwrap();
        fs::create_dir_all(abs_path3.parent().unwrap()).unwrap();
        File::create(&abs_path1).unwrap();
        File::create(&abs_path2).unwrap();
        File::create(&abs_path3).unwrap();
        File::create(&abs_path4).unwrap();

        // Regexes that should match path1
        let config = create_config_with_regex(
            Some(vec!["^target_dir/"]), // Matches relative path
            None,
            Some(vec![r"^target_file\.rs$"]), // Matches filename
        );

        assert!(passes_regex_filters(&abs_path1, &rel_path1, &config)?); // Both match
        assert!(!passes_regex_filters(&abs_path2, &rel_path2, &config)?); // Filename fails
        assert!(!passes_regex_filters(&abs_path3, &rel_path3, &config)?); // Path fails
        assert!(!passes_regex_filters(&abs_path4, &rel_path4, &config)?); // Both fail
        Ok(())
    }

    #[test]
    fn test_regex_no_filename() -> Result<(), Error> {
        // Test behavior when the path itself has no filename component (e.g., ".")
        let current_dir_abs = PathBuf::from("."); // Represents current dir
        let current_dir_rel = PathBuf::from(".");

        let config_filename = create_config_with_regex(None, None, Some(vec!["anything"])); // Has filename regex
                                                                                            // Should fail because "." has no filename component
        assert!(!passes_regex_filters(
            &current_dir_abs,
            &current_dir_rel,
            &config_filename
        )?);

        let config_path = create_config_with_regex(Some(vec![r"^\.$"]), None, None); // Match path "."
                                                                                     // Should pass because path regex matches relative path "."
        assert!(passes_regex_filters(
            &current_dir_abs,
            &current_dir_rel,
            &config_path
        )?);

        Ok(())
    }
}