yek 0.25.5

A tool to serialize a repository into chunks of text files
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
use crate::{
    models::{InputConfig, OutputConfig, ProcessedFile, ProcessingConfig},
    pipeline::ProcessingContext,
};
use anyhow::{anyhow, Result};
use content_inspector::{inspect, ContentType};
use ignore::gitignore::GitignoreBuilder;
use path_slash::PathBufExt;
use rayon::prelude::*;
use std::{
    collections::HashMap,
    path::Path,
    sync::{Arc, Mutex},
    time::Instant,
};
use tracing::debug;

/// Thread-safe file processor that fixes race conditions
pub struct ParallelFileProcessor {
    context: Arc<ProcessingContext>,
    file_counter: Arc<Mutex<HashMap<i32, usize>>>,
}

impl ParallelFileProcessor {
    pub fn new(context: ProcessingContext) -> Self {
        Self {
            context: Arc::new(context),
            file_counter: Arc::new(Mutex::new(HashMap::new())),
        }
    }

    /// Process files in parallel with proper synchronization
    pub fn process_files_parallel(&self, base_path: &Path) -> Result<Vec<ProcessedFile>> {
        let start_time = Instant::now();
        let mut all_processed_files = Vec::new();

        // Expand globs into a list of paths
        let expanded_paths = self.expand_globs(base_path)?;

        // Determine the base directory for relative path calculation
        let base_dir = self.determine_base_dir(base_path);

        // Process each expanded path
        for path in expanded_paths {
            if self.context.file_system.is_file(&path) {
                let files = self.process_single_file(&path, &base_dir)?;
                all_processed_files.extend(files);
            } else if self.context.file_system.is_directory(&path) {
                let files = self.process_directory(&path, &base_dir)?;
                all_processed_files.extend(files);
            }
        }

        // Sort final results by priority (asc) then file_index (asc)
        all_processed_files.par_sort_by(|a, b| {
            a.priority
                .cmp(&b.priority)
                .then_with(|| a.file_index.cmp(&b.file_index))
        });

        if self.context.processing_config.debug {
            debug!(
                "Processed {} files in parallel for base_path: {} in {:?}",
                all_processed_files.len(),
                base_path.display(),
                start_time.elapsed()
            );
        }

        Ok(all_processed_files)
    }

    /// Expand glob patterns into concrete paths
    fn expand_globs(&self, base_path: &Path) -> Result<Vec<std::path::PathBuf>> {
        let mut expanded_paths = Vec::new();
        let path_str = base_path.to_string_lossy();

        for entry in glob::glob(&path_str)? {
            match entry {
                Ok(path) => {
                    // Resolve symlinks to prevent issues
                    let resolved_path = if self.context.file_system.is_symlink(&path) {
                        self.context
                            .file_system
                            .resolve_symlink(&path)
                            .unwrap_or(path)
                    } else {
                        path
                    };
                    expanded_paths.push(resolved_path);
                }
                Err(e) => debug!("Glob entry error: {:?}", e),
            }
        }

        Ok(expanded_paths)
    }

    /// Determine the base directory for relative path calculations
    fn determine_base_dir(&self, base_path: &Path) -> std::path::PathBuf {
        if let Some(shared_base_dir) = self.calculate_shared_base_dir() {
            return shared_base_dir;
        }

        let path_str = base_path.to_string_lossy();

        if Self::is_glob_pattern(&path_str) {
            // For glob patterns, use current directory to ensure unique paths across different sources
            std::env::current_dir().unwrap_or_else(|_| Path::new(".").to_path_buf())
        } else if base_path.is_file() {
            // For single files, use the parent directory
            base_path.parent().unwrap_or(Path::new(".")).to_path_buf()
        } else {
            // For directories, use the directory itself
            base_path.to_path_buf()
        }
    }

    fn calculate_shared_base_dir(&self) -> Option<std::path::PathBuf> {
        let input_paths = &self.context.input_config.input_paths;
        if input_paths.len() <= 1 {
            return None;
        }

        let current_dir = std::env::current_dir().unwrap_or_else(|_| Path::new(".").to_path_buf());
        input_paths
            .iter()
            .map(|input_path| Self::resolve_input_base(input_path, &current_dir))
            .reduce(|base, path| Self::find_common_base(&base, &path))
    }

    fn resolve_input_base(input_path: &str, current_dir: &Path) -> std::path::PathBuf {
        let path = Path::new(input_path);

        if Self::is_glob_pattern(input_path) {
            let wildcard_index = input_path
                .char_indices()
                .find(|(_, c)| matches!(c, '*' | '?' | '['))
                .map(|(i, _)| i)
                .unwrap_or(input_path.len());

            let prefix = &input_path[..wildcard_index];
            let prefix_path = Path::new(prefix);
            let base_dir = prefix_path.parent().unwrap_or(Path::new(""));

            if base_dir.as_os_str().is_empty() {
                current_dir.to_path_buf()
            } else if base_dir.is_absolute() {
                base_dir.to_path_buf()
            } else {
                current_dir.join(base_dir)
            }
        } else if path.is_absolute() {
            path.to_path_buf()
        } else {
            current_dir.join(path)
        }
    }

    fn is_glob_pattern(path: &str) -> bool {
        path.contains('*') || path.contains('?') || path.contains('[')
    }

    fn find_common_base(path1: &Path, path2: &Path) -> std::path::PathBuf {
        let mut common_base = std::path::PathBuf::new();

        for (component1, component2) in path1.components().zip(path2.components()) {
            if component1 != component2 {
                break;
            }
            common_base.push(component1.as_os_str());
        }

        if common_base.as_os_str().is_empty() {
            Path::new(".").to_path_buf()
        } else {
            common_base
        }
    }

    /// Process a single file
    fn process_single_file(&self, file_path: &Path, base_dir: &Path) -> Result<Vec<ProcessedFile>> {
        let rel_path = self.normalize_path(file_path, base_dir);

        // Check if file should be ignored
        if self.should_ignore_file(file_path, &rel_path) {
            debug!("Skipping ignored file: {rel_path}");
            return Ok(Vec::new());
        }

        // Read and process file content
        match self.context.file_system.read_file(file_path) {
            Ok(content) => {
                if inspect(&content) == ContentType::BINARY {
                    debug!("Skipping binary file: {rel_path}");
                    Ok(Vec::new())
                } else {
                    let processed_file = self.create_processed_file(&rel_path, &content)?;
                    Ok(vec![processed_file])
                }
            }
            Err(e) => {
                debug!("Failed to read {rel_path}: {e}");
                // Skip files that can't be read instead of failing
                Ok(Vec::new())
            }
        }
    }

    /// Process all files in a directory
    fn process_directory(&self, dir_path: &Path, base_dir: &Path) -> Result<Vec<ProcessedFile>> {
        let mut processed_files = Vec::new();

        // Build gitignore patterns
        let gitignore = self.build_gitignore(dir_path)?;

        // Use parallel processing for directory contents
        let files_to_process: Vec<_> =
            self.collect_files_to_process(dir_path, base_dir, &gitignore)?;

        // Process files in parallel with proper synchronization
        let results: Vec<Result<ProcessedFile>> = files_to_process
            .par_iter()
            .map(|(path, rel_path)| self.process_file_with_priority(path, rel_path, base_dir))
            .collect();

        // Filter out errors (e.g., binary files) and collect successful results
        processed_files.extend(results.into_iter().filter_map(|r| r.ok()));

        Ok(processed_files)
    }

    /// Collect all files that need to be processed from a directory
    fn collect_files_to_process(
        &self,
        dir_path: &Path,
        base_dir: &Path,
        gitignore: &Arc<ignore::gitignore::Gitignore>,
    ) -> Result<Vec<(std::path::PathBuf, String)>> {
        let mut files_to_process = Vec::new();

        // Use ignore's walker for efficient directory traversal
        let mut walk_builder = ignore::WalkBuilder::new(dir_path);
        walk_builder
            .follow_links(false)
            .standard_filters(true)
            .require_git(false);

        let gitignore = Arc::clone(gitignore);

        // Use sequential walking instead of parallel to avoid closure issues
        for result in walk_builder.build() {
            let entry = match result {
                Ok(e) => e,
                Err(_) => continue,
            };

            // Only process files
            if !entry.file_type().is_some_and(|ft| ft.is_file()) {
                continue;
            }

            let path = entry.path().to_path_buf();
            let rel_path = self.normalize_path(&path, base_dir);

            // Check gitignore
            if gitignore.matched(&path, false).is_ignore() {
                debug!("Skipping ignored file: {rel_path}");
                continue;
            }

            // Send to processing
            files_to_process.push((path, rel_path));
        }

        Ok(files_to_process)
    }

    /// Process a single file with priority calculation and thread-safe index assignment
    fn process_file_with_priority(
        &self,
        file_path: &Path,
        rel_path: &str,
        _base_dir: &Path,
    ) -> Result<ProcessedFile> {
        // Read file content
        let content = self.context.file_system.read_file(file_path)?;

        if inspect(&content) == ContentType::BINARY {
            return Err(anyhow!("Binary file: {}", rel_path));
        }

        // Calculate priority with category
        let (priority, category) = self.calculate_priority_with_category(rel_path);

        // Get thread-safe file index
        let file_index = self.get_next_file_index(priority);

        Ok(ProcessedFile::new_with_category(
            rel_path.to_string(),
            String::from_utf8_lossy(&content).to_string(),
            priority,
            file_index,
            category,
        ))
    }

    /// Calculate priority for a file (legacy method for backward compatibility)
    #[allow(dead_code)]
    fn calculate_priority(&self, rel_path: &str) -> i32 {
        let mut priority = 0;

        // Apply priority rules
        for rule in &self.context.processing_config.priority_rules {
            if let Ok(regex) = regex::Regex::new(&rule.pattern) {
                if regex.is_match(rel_path) {
                    priority += rule.score;
                }
            }
        }

        // Apply git boost if available
        if let Some(commit_time) = self.context.repository_info.commit_times.get(rel_path) {
            let max_boost = self.context.input_config.git_boost_max.unwrap_or(100);
            priority += self.calculate_git_boost(
                *commit_time,
                &self.context.repository_info.commit_times,
                max_boost,
            );
        }

        priority
    }

    /// Calculate priority for a file including category-based offset
    fn calculate_priority_with_category(
        &self,
        rel_path: &str,
    ) -> (i32, crate::category::FileCategory) {
        use crate::priority::get_file_priority_with_category;

        // Get base priority from rules and category
        let (mut priority, category) = get_file_priority_with_category(
            rel_path,
            &self.context.processing_config.priority_rules,
            &self.context.processing_config.category_weights,
        );

        // Apply git boost if available
        if let Some(commit_time) = self.context.repository_info.commit_times.get(rel_path) {
            let max_boost = self.context.input_config.git_boost_max.unwrap_or(100);
            priority += self.calculate_git_boost(
                *commit_time,
                &self.context.repository_info.commit_times,
                max_boost,
            );
        }

        (priority, category)
    }

    /// Calculate git boost for a file
    fn calculate_git_boost(
        &self,
        file_time: u64,
        all_times: &HashMap<String, u64>,
        max_boost: i32,
    ) -> i32 {
        if all_times.is_empty() {
            return 0;
        }

        let times: Vec<&u64> = all_times.values().collect();
        let min_time = times.iter().min().map_or(file_time, |&&t| t);
        let max_time = times.iter().max().map_or(file_time, |&&t| t);

        if max_time == min_time {
            return 0; // All files have same timestamp
        }

        let normalized = (file_time - min_time) as f64 / (max_time - min_time) as f64;
        (normalized * max_boost as f64).round() as i32
    }

    /// Get next file index for a priority level in a thread-safe manner
    fn get_next_file_index(&self, priority: i32) -> usize {
        let mut counter = self.file_counter.lock().unwrap();
        let next_index = counter.entry(priority).or_insert(0);
        let index = *next_index;
        *next_index += 1;
        index
    }

    /// Check if a file should be ignored
    fn should_ignore_file(&self, file_path: &Path, _rel_path: &str) -> bool {
        // Check ignore patterns
        let path_str = file_path.to_string_lossy();
        let ignored_by_pattern = self
            .context
            .input_config
            .ignore_patterns
            .iter()
            .any(|pattern| pattern.matches(&path_str));

        // Check binary extensions
        let is_binary = file_path
            .extension()
            .and_then(|ext| ext.to_str())
            .map(|ext| self.context.input_config.binary_extensions.contains(ext))
            .unwrap_or(false);

        ignored_by_pattern || is_binary
    }

    /// Build gitignore for a directory
    fn build_gitignore(&self, dir_path: &Path) -> Result<Arc<ignore::gitignore::Gitignore>> {
        let mut gitignore_builder = GitignoreBuilder::new(dir_path);

        // Add custom patterns
        for pattern in &self.context.input_config.ignore_patterns {
            gitignore_builder.add_line(None, &pattern.to_string())?;
        }

        // Add .gitignore file if it exists
        let gitignore_file = dir_path.join(".gitignore");
        if self.context.file_system.path_exists(&gitignore_file) {
            gitignore_builder.add(&gitignore_file);
        }

        Ok(Arc::new(gitignore_builder.build()?))
    }

    /// Create a processed file with proper metadata
    fn create_processed_file(&self, rel_path: &str, content: &[u8]) -> Result<ProcessedFile> {
        let (priority, category) = self.calculate_priority_with_category(rel_path);
        let file_index = self.get_next_file_index(priority);

        Ok(ProcessedFile::new_with_category(
            rel_path.to_string(),
            String::from_utf8_lossy(content).to_string(),
            priority,
            file_index,
            category,
        ))
    }

    /// Normalize path to relative, slash-normalized form
    fn normalize_path(&self, path: &Path, base: &Path) -> String {
        path.strip_prefix(base)
            .unwrap_or(path)
            .to_path_buf()
            .to_slash()
            .unwrap_or_default()
            .to_string()
    }
}

/// Create a relative, slash-normalized path
pub fn normalize_path(path: &Path, base: &Path) -> String {
    path.strip_prefix(base)
        .unwrap_or(path)
        .to_path_buf()
        .to_slash()
        .unwrap_or_default()
        .to_string()
}

/// Legacy function for backward compatibility - delegates to new implementation
pub fn process_files_parallel(
    base_path: &Path,
    config: &crate::config::YekConfig,
    _boost_map: &HashMap<String, i32>,
) -> Result<Vec<ProcessedFile>> {
    // This is a temporary bridge - in the final implementation,
    // this would be replaced with the new pipeline-based approach
    let processor = ParallelFileProcessor::new(ProcessingContext::new(
        InputConfig {
            input_paths: config.input_paths.clone(),
            ignore_patterns: config
                .ignore_patterns
                .iter()
                .map(|s| glob::Pattern::new(s).unwrap())
                .collect(),
            binary_extensions: config.binary_extensions.iter().cloned().collect(),
            max_git_depth: config.max_git_depth,
            git_boost_max: config.git_boost_max,
        },
        OutputConfig::default(), // TODO: Convert from YekConfig
        ProcessingConfig {
            priority_rules: config.priority_rules.clone(),
            category_weights: config.category_weights.clone().unwrap_or_default(),
            debug: config.debug,
            parallel: true,
            max_threads: None,
            memory_limit_mb: None,
            batch_size: 1000,
        },
        crate::models::RepositoryInfo::new(base_path.to_path_buf(), false), // TODO: Proper repo info
        Arc::new(crate::repository::RealFileSystem),
    ));

    processor.process_files_parallel(base_path)
}