Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add /lock command for converting editable files to read-only #2419

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions aider/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,24 @@ def completions_model(self):
models = litellm.model_cost.keys()
return models

def cmd_lock(self, args=None):
"Move all editable files to read-only status"
if not self.coder:
self.io.tool_error("No active coder")
return

editable_files = self.coder.abs_fnames or []
if not editable_files:
self.io.tool_error("No editable files to lock")
return

read_only_files = self.coder.abs_read_only_fnames or set()
read_only_files.update(editable_files)
self.coder.abs_read_only_fnames = read_only_files
self.coder.abs_fnames = set()

self.io.tool_output(f"Moved {len(editable_files)} files to read-only status")

def cmd_models(self, args):
"Search the list of available models"

Expand Down
48 changes: 48 additions & 0 deletions tests/basic/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -1600,3 +1600,51 @@ def test_cmd_reset(self):

del coder
del commands

def test_cmd_lock(self):
io = InputOutput(pretty=False, fancy_input=False, yes=True)
coder = Coder.create(self.GPT35, None, io)
commands = Commands(io, coder)

# Set up test files
editable_files = {
os.path.join(self.tempdir, "file1.py"),
os.path.join(self.tempdir, "file2.py"),
}
read_only_files = {os.path.join(self.tempdir, "readonly.py")}

coder.abs_fnames = editable_files
coder.abs_read_only_fnames = read_only_files

# Execute lock command
commands.cmd_lock()

# Verify files were moved to read-only
self.assertEqual(coder.abs_fnames, set())
self.assertEqual(coder.abs_read_only_fnames, editable_files | read_only_files)

def test_cmd_lock_no_files(self):
io = InputOutput(pretty=False, fancy_input=False, yes=True)
coder = Coder.create(self.GPT35, None, io)
commands = Commands(io, coder)

# Set up with no editable files
coder.abs_fnames = set()
read_only_files = {os.path.join(self.tempdir, "readonly.py")}
coder.abs_read_only_fnames = read_only_files

with mock.patch.object(io, "tool_error") as mock_error:
commands.cmd_lock()
mock_error.assert_called_once_with("No editable files to lock")

# Verify nothing changed
self.assertEqual(coder.abs_fnames, set())
self.assertEqual(coder.abs_read_only_fnames, read_only_files)

def test_cmd_lock_no_coder(self):
io = InputOutput(pretty=False, fancy_input=False, yes=True)
commands = Commands(io, coder=None)

with mock.patch.object(io, "tool_error") as mock_error:
commands.cmd_lock()
mock_error.assert_called_once_with("No active coder")