From 6b04c3de11c12d12cb75c0ff80728a7f4c2a349a Mon Sep 17 00:00:00 2001 From: "bobo.yang" Date: Fri, 7 Mar 2025 11:38:29 +0800 Subject: [PATCH] fix: Improve git push error handling and feedback - Replace subprocess_check_output with subprocess_run for better error handling - Add specific error messages and formatting for push failures - Implement exit code when push fails to properly indicate failure --- merico/github/commit/commit.py | 18 ++++++++++++++++-- merico/gitlab/commit/commit.py | 26 ++++++++++++++++++++------ 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/merico/github/commit/commit.py b/merico/github/commit/commit.py index 81a77c5..baaa0ef 100644 --- a/merico/github/commit/commit.py +++ b/merico/github/commit/commit.py @@ -389,12 +389,24 @@ def push_changes(): return False print(f"Pushing changes to origin/{current_branch}...", end="\n\n", flush=True) - result = subprocess_check_output(["git", "push", "origin", current_branch]) + result = subprocess_run( + ["git", "push", "origin", current_branch], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True, + ) + + if result.returncode != 0: + print(f"Push failed: {result.stderr}", end="\n\n", flush=True) + return False print("Push completed successfully.", end="\n\n", flush=True) return True except subprocess.CalledProcessError as e: print(f"Push failed: {str(e)}", end="\n\n", file=sys.stderr, flush=True) return False + except Exception as e: + print(f"An unexpected error occurred: {str(e)}", end="\n\n", file=sys.stderr, flush=True) + return False def main(): global language @@ -461,7 +473,9 @@ def main(): else: # 添加推送步骤 if ask_for_push(): - push_changes() + if not push_changes(): + print("Push failed.", flush=True) + sys.exit(-1) print("Commit completed.", flush=True) sys.exit(0) diff --git a/merico/gitlab/commit/commit.py b/merico/gitlab/commit/commit.py index 2827855..493f4b2 100644 --- a/merico/gitlab/commit/commit.py +++ b/merico/gitlab/commit/commit.py @@ -385,15 +385,27 @@ def push_changes(): try: current_branch = get_current_branch() if not current_branch: - print("Could not determine current branch. Push failed.", file=sys.stderr, flush=True) + print("Could not determine current branch. Push failed.", end="\n\n", file=sys.stderr, flush=True) return False - print(f"Pushing changes to origin/{current_branch}...", flush=True) - result = subprocess_check_output(["git", "push", "origin", current_branch]) - print("Push completed successfully.", flush=True) + print(f"Pushing changes to origin/{current_branch}...", end="\n\n", flush=True) + result = subprocess_run( + ["git", "push", "origin", current_branch], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True, + ) + + if result.returncode != 0: + print(f"Push failed: {result.stderr}", end="\n\n", flush=True) + return False + print("Push completed successfully.", end="\n\n", flush=True) return True except subprocess.CalledProcessError as e: - print(f"Push failed: {str(e)}", file=sys.stderr, flush=True) + print(f"Push failed: {str(e)}", end="\n\n", file=sys.stderr, flush=True) + return False + except Exception as e: + print(f"An unexpected error occurred: {str(e)}", end="\n\n", file=sys.stderr, flush=True) return False def main(): @@ -461,7 +473,9 @@ def main(): else: # 添加推送步骤 if ask_for_push(): - push_changes() + if not push_changes(): + print("Push failed.", flush=True) + sys.exit(-1) print("Commit completed.", flush=True) sys.exit(0)