From 3ba4aca2688d835ec58d5ab2a46dc4c462047555 Mon Sep 17 00:00:00 2001 From: "Paul Gauthier (aider)" Date: Tue, 26 Nov 2024 15:01:50 -0800 Subject: [PATCH] refactor: improve error handling for .gitignore file operations --- aider/main.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/aider/main.py b/aider/main.py index 6391ee619..29c6a9efb 100644 --- a/aider/main.py +++ b/aider/main.py @@ -157,13 +157,17 @@ def check_gitignore(git_root, io, ask=True): gitignore_file = Path(git_root) / ".gitignore" if gitignore_file.exists(): - content = io.read_text(gitignore_file) - if content is None: + try: + content = io.read_text(gitignore_file) + if content is None: + return + existing_lines = content.splitlines() + for pat in patterns: + if pat not in existing_lines: + patterns_to_add.append(pat) + except OSError as e: + io.tool_error(f"Error when trying to read {gitignore_file}: {e}") return - existing_lines = content.splitlines() - for pat in patterns: - if pat not in existing_lines: - patterns_to_add.append(pat) else: content = "" patterns_to_add = patterns @@ -181,8 +185,8 @@ def check_gitignore(git_root, io, ask=True): try: io.write_text(gitignore_file, content) io.tool_output(f"Added {', '.join(patterns_to_add)} to .gitignore") - except PermissionError: - io.tool_error(f"Permission denied when trying to write to {gitignore_file}") + except OSError as e: + io.tool_error(f"Error when trying to write to {gitignore_file}: {e}") io.tool_output( "Try running with appropriate permissions or manually add these patterns to .gitignore:" )