octocode 0.11.0

AI-powered code indexer with semantic search, GraphRAG knowledge graphs, and MCP server for multi-language codebases
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
// Copyright 2025 Muvon Un Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Shared utilities for import resolution across all languages
//!
//! This module provides common file-finding and path resolution utilities
//! that can be used by language-specific import resolvers.

use std::collections::HashMap;
use std::path::{Path, PathBuf};

use crate::utils::path::PathNormalizer;

/// File registry for efficient file lookup by extension and pattern
pub struct FileRegistry {
	/// Files grouped by extension for quick lookup
	files_by_extension: HashMap<String, Vec<String>>,
	/// All files for general searches
	all_files: Vec<String>,
}

impl FileRegistry {
	/// Create a new file registry from a list of file paths
	pub fn new(all_files: &[String]) -> Self {
		let mut files_by_extension = HashMap::new();

		for file_path in all_files {
			if let Some(extension) = Path::new(file_path).extension() {
				if let Some(ext_str) = extension.to_str() {
					files_by_extension
						.entry(ext_str.to_lowercase())
						.or_insert_with(Vec::new)
						.push(file_path.clone());
				}
			}
		}

		Self {
			files_by_extension,
			all_files: all_files.to_vec(),
		}
	}

	/// Get all files with specific extensions
	pub fn get_files_with_extensions(&self, extensions: &[&str]) -> Vec<String> {
		let mut result = Vec::new();
		for ext in extensions {
			if let Some(files) = self.files_by_extension.get(&ext.to_lowercase()) {
				result.extend(files.clone());
			}
		}
		result
	}

	/// Find a file with multiple possible extensions
	pub fn find_file_with_extensions(
		&self,
		base_path: &Path,
		extensions: &[&str],
	) -> Option<String> {
		for ext in extensions {
			let file_path = if ext.is_empty() {
				base_path.to_path_buf()
			} else {
				PathBuf::from(format!("{}.{}", base_path.to_string_lossy(), ext))
			};

			if let Some(found) = self.find_exact_file(&file_path.to_string_lossy()) {
				return Some(found);
			}
		}
		None
	}

	/// Find exact file match with cross-platform path comparison
	pub fn find_exact_file(&self, target_path: &str) -> Option<String> {
		// Use cross-platform path comparison first (most reliable for tests)
		if let Some(found) = PathNormalizer::find_path_in_collection(target_path, &self.all_files) {
			return Some(found.to_string());
		}

		// Try direct canonicalize only as fallback (for real files)
		if let Ok(canonical_target) = std::path::Path::new(target_path).canonicalize() {
			// Handle Windows UNC paths
			let canonical_str = canonical_target.to_string_lossy();
			let normalized_canonical = if canonical_str.starts_with("//?/") {
				if let Some(drive_pos) = canonical_str.find(":/") {
					if let Some(relative_part) = canonical_str.get(drive_pos + 2..) {
						PathNormalizer::normalize_separators(relative_part)
					} else {
						PathNormalizer::normalize_separators(&canonical_str)
					}
				} else {
					PathNormalizer::normalize_separators(&canonical_str)
				}
			} else {
				PathNormalizer::normalize_separators(&canonical_str)
			};

			for file_path in &self.all_files {
				if let Ok(canonical_file) = std::path::Path::new(file_path).canonicalize() {
					let canonical_file_str = canonical_file.to_string_lossy();
					let normalized_file = if canonical_file_str.starts_with("//?/") {
						if let Some(drive_pos) = canonical_file_str.find(":/") {
							if let Some(relative_part) = canonical_file_str.get(drive_pos + 2..) {
								PathNormalizer::normalize_separators(relative_part)
							} else {
								PathNormalizer::normalize_separators(&canonical_file_str)
							}
						} else {
							PathNormalizer::normalize_separators(&canonical_file_str)
						}
					} else {
						PathNormalizer::normalize_separators(&canonical_file_str)
					};

					if normalized_canonical == normalized_file {
						return Some(file_path.clone());
					}
				}
			}
		}

		None
	}

	/// Find files matching a pattern
	pub fn find_files_by_pattern(&self, pattern: &str) -> Vec<String> {
		self.all_files
			.iter()
			.filter(|file| file.contains(pattern))
			.cloned()
			.collect()
	}

	/// Get all files
	pub fn get_all_files(&self) -> &[String] {
		&self.all_files
	}
}

/// Find project root by looking for common project indicators
pub fn find_project_root(source_file: &str) -> Option<String> {
	let source_path = Path::new(source_file);
	let mut current_dir = source_path.parent()?;

	loop {
		// Look for common project root indicators
		let indicators = [
			"Cargo.toml",
			"package.json",
			"setup.py",
			"go.mod",
			"composer.json",
			"pyproject.toml",
			"pom.xml",
			"build.gradle",
			".git",
		];

		for indicator in &indicators {
			let indicator_path = current_dir.join(indicator);
			if indicator_path.exists() {
				return Some(current_dir.to_string_lossy().to_string());
			}
		}

		// Move up one directory
		if let Some(parent) = current_dir.parent() {
			current_dir = parent;
		} else {
			break;
		}
	}

	None
}

/// Normalize a file path for consistent comparison
pub fn normalize_path(path: &str) -> String {
	let path_buf = Path::new(path);

	// Manually resolve .. components to avoid Windows canonicalization issues
	let mut components = Vec::new();
	for component in path_buf.components() {
		match component {
			std::path::Component::ParentDir => {
				// Pop the last component if possible
				if !components.is_empty() {
					components.pop();
				}
			}
			std::path::Component::CurDir => {
				// Skip current directory components
			}
			std::path::Component::Prefix(_) => {
				// Skip Windows drive prefixes for relative path normalization
			}
			std::path::Component::RootDir => {
				// Skip root directory for relative path normalization
			}
			std::path::Component::Normal(name) => {
				components.push(name.to_string_lossy().to_string());
			}
		}
	}

	// If we have components, build relative path with normalized separators
	if !components.is_empty() {
		let normalized: PathBuf = components.into_iter().collect();
		return PathNormalizer::normalize_separators(&normalized.to_string_lossy());
	}

	// Try canonicalization only as fallback and make relative
	if let Ok(canonical) = path_buf.canonicalize() {
		if let Ok(current_dir) = std::env::current_dir() {
			if let Ok(relative) = canonical.strip_prefix(&current_dir) {
				return PathNormalizer::normalize_separators(&relative.to_string_lossy());
			}
		}
		// Handle Windows UNC paths like //?/D:/path/to/file
		let canonical_str = canonical.to_string_lossy();
		if canonical_str.starts_with("//?/") {
			if let Some(drive_pos) = canonical_str.find(":/") {
				if let Some(relative_part) = canonical_str.get(drive_pos + 2..) {
					return PathNormalizer::normalize_separators(relative_part);
				}
			}
		}
		return PathNormalizer::normalize_separators(&canonical_str);
	}

	// Final fallback: just normalize separators
	PathNormalizer::normalize_separators(path)
}

/// Detect language from file path extension
pub fn detect_language_from_path(file_path: &str) -> Option<String> {
	let path = Path::new(file_path);
	let extension = path.extension()?.to_str()?;

	match extension {
		"rs" => Some("rust".to_string()),
		"js" | "mjs" => Some("javascript".to_string()),
		"ts" | "tsx" => Some("typescript".to_string()),
		"py" => Some("python".to_string()),
		"go" => Some("go".to_string()),
		"php" => Some("php".to_string()),
		"cpp" | "cc" | "cxx" | "c++" => Some("cpp".to_string()),
		"c" | "h" => Some("c".to_string()),
		"rb" => Some("ruby".to_string()),
		"sh" | "bash" => Some("bash".to_string()),
		"json" => Some("json".to_string()),
		"css" | "scss" | "sass" => Some("css".to_string()),
		"md" | "markdown" => Some("markdown".to_string()),
		"svelte" => Some("svelte".to_string()),
		_ => None,
	}
}

/// Helper to resolve relative paths from a source directory
pub fn resolve_relative_path(source_file: &str, relative_path: &str) -> Option<PathBuf> {
	let source_path = Path::new(source_file);
	let source_dir = source_path.parent()?;
	let resolved = source_dir.join(relative_path);

	// Normalize the path to resolve ".." components
	// This converts "src/../lib.rs" to "lib.rs"
	let normalized_str = normalize_path(&resolved.to_string_lossy());
	Some(PathBuf::from(normalized_str))
}

/// Helper to find files in a specific directory
pub fn find_files_in_directory(
	directory: &Path,
	registry: &FileRegistry,
	extensions: &[&str],
) -> Vec<String> {
	let dir_str = directory.to_string_lossy();
	registry
		.get_files_with_extensions(extensions)
		.into_iter()
		.filter(|file| file.starts_with(&*dir_str))
		.collect()
}

#[cfg(test)]
mod tests {
	use super::*;

	#[test]
	fn test_file_registry_creation() {
		let files = vec![
			"src/main.rs".to_string(),
			"src/lib.rs".to_string(),
			"package.json".to_string(),
			"index.js".to_string(),
		];

		let registry = FileRegistry::new(&files);
		let rust_files = registry.get_files_with_extensions(&["rs"]);
		assert_eq!(rust_files.len(), 2);
		assert!(rust_files.contains(&"src/main.rs".to_string()));
		assert!(rust_files.contains(&"src/lib.rs".to_string()));
	}

	#[test]
	fn test_find_file_with_extensions() {
		let files = vec!["src/utils.rs".to_string(), "src/utils.js".to_string()];

		let registry = FileRegistry::new(&files);
		let result = registry.find_file_with_extensions(Path::new("src/utils"), &["rs", "js"]);
		assert!(result.is_some());
		let result_path = result.unwrap();
		assert!(result_path.ends_with(".rs") || result_path.ends_with(".js"));
	}

	#[test]
	fn test_detect_language_from_path() {
		assert_eq!(
			detect_language_from_path("main.rs"),
			Some("rust".to_string())
		);
		assert_eq!(
			detect_language_from_path("index.js"),
			Some("javascript".to_string())
		);
		assert_eq!(
			detect_language_from_path("app.py"),
			Some("python".to_string())
		);
		assert_eq!(detect_language_from_path("unknown.xyz"), None);
	}

	#[test]
	fn test_resolve_relative_path() {
		let result = resolve_relative_path("src/main.rs", "../lib.rs");
		assert!(result.is_some());
		assert_eq!(result.unwrap().to_string_lossy(), "lib.rs");
	}

	#[test]
	fn test_cross_platform_path_comparison() {
		let files = vec![
			"src/main.rs".to_string(),
			"src\\utils\\helper.rs".to_string(), // Windows-style path
			"lib/config.rs".to_string(),
		];

		let registry = FileRegistry::new(&files);

		// Test finding Windows-style path with Unix-style query
		let result = registry.find_exact_file("src/utils/helper.rs");
		assert!(result.is_some(), "Should find Windows path with Unix query");

		// Test finding Unix-style path with Windows-style query
		let result = registry.find_exact_file("lib\\config.rs");
		assert!(result.is_some(), "Should find Unix path with Windows query");
	}

	#[test]
	fn test_normalize_path_separators() {
		// Test Windows to Unix normalization
		assert_eq!(
			PathNormalizer::normalize_separators("src\\main.rs"),
			"src/main.rs"
		);
		assert_eq!(
			PathNormalizer::normalize_separators("src\\utils\\helper.rs"),
			"src/utils/helper.rs"
		);

		// Test Unix paths remain unchanged
		assert_eq!(
			PathNormalizer::normalize_separators("src/main.rs"),
			"src/main.rs"
		);
		assert_eq!(
			PathNormalizer::normalize_separators("src/utils/helper.rs"),
			"src/utils/helper.rs"
		);

		// Test mixed separators
		assert_eq!(
			PathNormalizer::normalize_separators("src\\utils/helper.rs"),
			"src/utils/helper.rs"
		);
		assert_eq!(
			PathNormalizer::normalize_separators("src/utils\\helper.rs"),
			"src/utils/helper.rs"
		);

		// Test empty and single character
		assert_eq!(PathNormalizer::normalize_separators(""), "");
		assert_eq!(PathNormalizer::normalize_separators("\\"), "/");
		assert_eq!(PathNormalizer::normalize_separators("/"), "/");
	}
}