Skip to content

Commit 22a5156

Browse files
committed
Fix issue when loading a specific file
1 parent 2e15d38 commit 22a5156

File tree

3 files changed

+76
-33
lines changed

3 files changed

+76
-33
lines changed

src/copychat/cli.py

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
from enum import Enum
77

88
from .core import (
9-
is_glob_pattern,
10-
resolve_paths,
119
scan_directory,
1210
DiffMode,
1311
get_file_content,
@@ -160,36 +158,48 @@ def main(
160158
if not paths:
161159
paths = ["."]
162160

163-
# Handle glob patterns in command line arguments
164-
resolved_paths = []
165-
for path in paths:
166-
if is_glob_pattern(path):
167-
# Use resolve_paths for glob patterns
168-
resolved = resolve_paths([path], base_path=source_dir)
169-
resolved_paths.extend(resolved)
170-
else:
171-
# Keep regular paths as-is
172-
resolved_paths.append(
173-
source_dir / path if source_dir != Path(".") else Path(path)
174-
)
175-
176-
# Scan all resolved paths
161+
# Handle paths
177162
all_files = {}
178-
for target in resolved_paths:
179-
if target.is_file():
180-
content = get_file_content(target, diff_mode)
181-
if content is not None:
182-
all_files[target] = content
163+
for path in paths:
164+
target = Path(path)
165+
if target.is_absolute():
166+
# Use absolute paths as-is
167+
if target.is_file():
168+
content = get_file_content(target, diff_mode)
169+
if content is not None:
170+
all_files[target] = content
171+
else:
172+
files = scan_directory(
173+
target,
174+
include=include.split(",") if include else None,
175+
exclude_patterns=exclude,
176+
diff_mode=diff_mode,
177+
max_depth=depth,
178+
)
179+
all_files.update(files)
183180
else:
184-
files = scan_directory(
185-
target,
186-
include=include.split(",") if include else None,
187-
exclude_patterns=exclude,
188-
diff_mode=diff_mode,
189-
max_depth=depth,
190-
)
191-
all_files.update(files)
181+
# For relative paths, try both relative to current dir and source dir
182+
targets = [Path.cwd() / path]
183+
if source_dir != Path("."):
184+
targets.append(source_dir / path)
192185

186+
for target in targets:
187+
if target.exists():
188+
if target.is_file():
189+
content = get_file_content(target, diff_mode)
190+
if content is not None:
191+
all_files[target] = content
192+
break
193+
else:
194+
files = scan_directory(
195+
target,
196+
include=include.split(",") if include else None,
197+
exclude_patterns=exclude,
198+
diff_mode=diff_mode,
199+
max_depth=depth,
200+
)
201+
all_files.update(files)
202+
break
193203
if not all_files:
194204
error_console.print("Found [red]0[/] matching files")
195205
raise typer.Exit(1) # Exit with code 1 to indicate no files found

src/copychat/core.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,15 @@ def resolve_paths(paths: list[str], base_path: Path = Path(".")) -> list[Path]:
4141
continue
4242
resolved.append(match)
4343
except ValueError:
44-
continue
44+
# If path is not relative to base_path, just use it as-is
45+
resolved.append(match)
4546
else:
46-
resolved.append(base_path / path)
47+
# For non-glob paths, use them as-is
48+
path_obj = Path(path)
49+
if path_obj.is_absolute():
50+
resolved.append(path_obj)
51+
else:
52+
resolved.append(base_path / path)
4753
return resolved
4854

4955

tests/test_core.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ def test_resolve_paths(tmp_path):
3838

3939
# Test glob resolution
4040
paths = resolve_paths(["*.py", "src/**/*.py"], base_path=tmp_path)
41-
assert len(paths) == 4
41+
assert len(paths) == 3
4242
assert tmp_path / "test1.py" in paths
4343
assert tmp_path / "test2.py" in paths
4444
assert tmp_path / "src" / "main.py" in paths
4545

4646
# Test mixed glob and regular paths
4747
paths = resolve_paths(["src", "*.py"], base_path=tmp_path)
48-
assert len(paths) == 4 # main.py will be found twice
48+
assert len(paths) == 3
4949
assert tmp_path / "src" in paths
5050

5151

@@ -114,3 +114,30 @@ def test_scan_with_recursive_glob(tmp_path):
114114
) # Changed from tmp_path / "very" / "**/*.py"
115115
assert len(subdir_files) == 1
116116
assert any("test2.py" in str(p) for p in subdir_files)
117+
118+
119+
def test_scan_single_file(tmp_path):
120+
"""Test scanning a single file."""
121+
# Create a test file
122+
test_file = tmp_path / "test.py"
123+
test_file.write_text("print('hello world')")
124+
125+
# Create some other files that shouldn't be included
126+
(tmp_path / "other.py").write_text("print('other')")
127+
(tmp_path / "test.js").write_text("console.log('test')")
128+
129+
# Test scanning just the single file
130+
files = scan_directory(test_file, include=["py"])
131+
132+
# Should only contain our specific file
133+
assert len(files) == 1
134+
assert test_file in files
135+
assert files[test_file] == "print('hello world')"
136+
137+
# Test with non-matching extension filter
138+
files = scan_directory(test_file, include=["js"])
139+
assert len(files) == 0
140+
141+
# Test with non-existent file
142+
files = scan_directory(tmp_path / "nonexistent.py", include=["py"])
143+
assert len(files) == 0

0 commit comments

Comments
 (0)