Skip to content
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
9 changes: 8 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,11 @@ Each test case below corresponds to a buggy-to-fixed repository pair.
- https://github.com/nvbn/thefuck/issues/807
- thefuck_5
- Related issues:
- https://github.com/nvbn/thefuck/issues/723
- https://github.com/nvbn/thefuck/issues/723
- thefuck_7
- Related issues:
- Not available
- thefuck_9
- Related issues:
- https://github.com/nvbn/thefuck/pull/559
- https://github.com/nvbn/thefuck/issues/558
2 changes: 0 additions & 2 deletions thefuck_9/tests/rules/test_git_push.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ def test_match(stderr):
def test_get_new_command(stderr):
assert get_new_command(Command('git push', stderr=stderr))\
== "git push --set-upstream origin master"
assert get_new_command(Command('git push -u', stderr=stderr))\
== "git push --set-upstream origin master"
assert get_new_command(Command('git push -u origin', stderr=stderr))\
== "git push --set-upstream origin master"
assert get_new_command(Command('git push --set-upstream origin', stderr=stderr))\
Expand Down
6 changes: 1 addition & 5 deletions thefuck_9/thefuck/rules/git_push.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@ def get_new_command(command):
pass
if upstream_option_index is not -1:
command.script_parts.pop(upstream_option_index)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 Warning 🐛 Bug

Unguarded second pop on command.script_parts can raise IndexError in git_push.py

Issue Explanation
  • get_new_command removes '--set-upstream' or '-u' and their argument by popping elements twice from command.script_parts.
  • The second pop is unguarded and assumes an argument exists after the option.
  • Previously, this second pop was inside a try/except block which handled the IndexError for cases like git push -u without an argument.
  • The try/except block was removed, leaving the second pop vulnerable to raising uncaught IndexError if the argument is missing.
  • No length or boundary check is present before the pops.

Reply if you have any questions or let me know if I missed something.

Don't forget to react with a 👍 or 👎 to the comments made by Blar to help us improve.

try:
command.script_parts.pop(upstream_option_index)
except IndexError:
# This happens for `git push -u`
pass
Comment on lines -27 to -31
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Error 🐛 Bug

Removal of try-except block in get_new_command causes crash for 'git push -u' commands without a target argument.

Issue Explanation
  • Previous code included a try-except block to catch IndexError when popping the argument following '-u' option.
  • This guarded against crashing for commands like git push -u that lack a remote/branch argument.
  • The try-except block was removed, meaning the code now directly pops twice without guard.
  • Without an argument after '-u', popping the second element raises an IndexError, causing a crash.
  • No evidence of alternative guard conditions or test coverage for this edge case was found.
  • This causes logical inconsistency and potential runtime crashes for users with such input.

Reply if you have any questions or let me know if I missed something.

Don't forget to react with a 👍 or 👎 to the comments made by Blar to help us improve.

command.script_parts.pop(upstream_option_index)
Comment on lines 25 to +27
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Critical: Removal of error handling introduces potential IndexError.

The code removes both the upstream option and its argument by calling pop() twice at the same index. However, if the upstream option is the last element (e.g., git push -u), the second pop() will raise an IndexError since there's no argument to remove.

The AI summary indicates that previous error handling for this case was removed, which could cause runtime exceptions.

Consider restoring the try-except block or adding a length check:

 if upstream_option_index is not -1:
     command.script_parts.pop(upstream_option_index)
-    command.script_parts.pop(upstream_option_index)
+    if upstream_option_index < len(command.script_parts):
+        command.script_parts.pop(upstream_option_index)

Alternatively, restore the original try-except handling:

 if upstream_option_index is not -1:
     command.script_parts.pop(upstream_option_index)
-    command.script_parts.pop(upstream_option_index)
+    try:
+        command.script_parts.pop(upstream_option_index)
+    except IndexError:
+        pass
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if upstream_option_index is not -1:
command.script_parts.pop(upstream_option_index)
try:
command.script_parts.pop(upstream_option_index)
except IndexError:
# This happens for `git push -u`
pass
command.script_parts.pop(upstream_option_index)
if upstream_option_index is not -1:
command.script_parts.pop(upstream_option_index)
if upstream_option_index < len(command.script_parts):
command.script_parts.pop(upstream_option_index)
🤖 Prompt for AI Agents
In thefuck_9/thefuck/rules/git_push.py around lines 25 to 27, the code removes
the upstream option and its argument by popping twice at the same index without
checking if the argument exists, which can cause an IndexError if the option is
last. To fix this, restore the original try-except block around the pop calls to
safely handle cases where the argument is missing, or add a condition to check
that the list has enough elements before popping the second time.


push_upstream = command.stderr.split('\n')[-3].strip().partition('git ')[2]
return replace_argument(" ".join(command.script_parts), 'push', push_upstream)