Skip to content

Commit a497e82

Browse files
committed
refactor(modules): improve file existence check in commit_generator
- Enhanced the `_get_changed_files` method to filter out non-existent files by verifying their presence in the filesystem, preventing potential errors during commit operations. - Added warning logs for skipped files that do not physically exist, aiding in debugging and transparency. - Clarified inline comments to improve code readability and maintainability.
1 parent 16da695 commit a497e82

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

avcmt/modules/commit_generator.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,27 @@ def _run_git_command(self, command: list[str]) -> str:
8181
return result.stdout.strip()
8282
except subprocess.CalledProcessError as e:
8383
error_message = (
84-
f"Git command '{' '.join(command)}' failed: {e.stderr.strip()}"
84+
f"Git command '{' '.join(e.cmd)}' failed: {e.stderr.strip()}"
8585
)
8686
self.logger.error(error_message)
8787
raise CommitError(error_message) from e
8888

8989
def _get_changed_files(self) -> list[str]:
90+
# Original logic to get files from Git
9091
output = self._run_git_command(
9192
["git", "ls-files", "--others", "--modified", "--exclude-standard"]
9293
)
93-
return [line.strip() for line in output.split("\n") if line.strip()]
94+
detected_files = [line.strip() for line in output.split("\n") if line.strip()]
95+
96+
# Filter out files that do not physically exist
97+
existing_files = []
98+
for file_path_str in detected_files:
99+
file_path = Path(file_path_str)
100+
if file_path.is_file(): # Ensure it is a file and exists
101+
existing_files.append(file_path_str)
102+
else:
103+
self.logger.warning(f"⏩ Skipping non-existent file: {file_path_str}")
104+
return existing_files
94105

95106
@staticmethod
96107
def _group_files_by_directory(files: list[str]) -> dict[str, list[str]]:
@@ -135,6 +146,7 @@ def _push_changes(self):
135146
self._run_git_command(["git", "push"])
136147
self.logger.info("✔️ All changes pushed successfully.")
137148

149+
# --- Suggested Section (With Minor Improvements) ---
138150
self.logger.info("\n")
139151
self.logger.info("💡 NEXT STEP: Synchronize with CI/CD Results")
140152
self.logger.info(
@@ -175,7 +187,7 @@ def _process_single_group(
175187
self.logger.info(f"--- Processing group: {group_name} ---")
176188

177189
if self.dry_run:
178-
self._stage_changes(files)
190+
self._stage_changes(files) # This is line 178 that triggers the error
179191
diff = self._get_diff_for_files(files)
180192

181193
if not diff.strip():

0 commit comments

Comments
 (0)