From be378c00572b57aa026c3f20b9d89653becf173d Mon Sep 17 00:00:00 2001 From: "abuzarmahmood (aider)" Date: Tue, 6 May 2025 07:34:01 +0000 Subject: [PATCH 1/7] feat: add merge_master function with error handling and branch update logic --- src/branch_handler.py | 84 +++++++++++++++++++++++++++++++++++++++++++ src/git_utils.py | 7 ++++ 2 files changed, 91 insertions(+) diff --git a/src/branch_handler.py b/src/branch_handler.py index 96e827e..5559fed 100644 --- a/src/branch_handler.py +++ b/src/branch_handler.py @@ -179,6 +179,90 @@ def back_to_master_branch(repo_path: str) -> None: print(f"Checked out {main_branch} branch") +def merge_master(repo_path: str, issue=None) -> bool: + """ + Merge the main/master branch into the current branch with error handling. + + Args: + repo_path: Path to local git repository + issue: The GitHub issue to log errors to (optional) + + Returns: + True if merge was successful, False otherwise + """ + from src.git_utils import write_issue_response, add_signature_to_comment + import traceback + + repo = git.Repo(repo_path) + current_branch = repo.active_branch.name + + # Determine main branch (main or master) + if 'main' in repo.heads: + main_branch = 'main' + elif 'master' in repo.heads: + main_branch = 'master' + else: + error_msg = "Neither 'main' nor 'master' branch found in repository" + print(error_msg) + if issue: + try: + from response_agent import llm_config + error_msg_with_signature = add_signature_to_comment( + error_msg, llm_config['model']) + except (ImportError, KeyError): + error_msg_with_signature = error_msg + \ + "\n\n---\n*This response was automatically generated by blech_bot*" + write_issue_response(issue, error_msg_with_signature) + return False + + # Don't try to merge if we're already on the main branch + if current_branch == main_branch: + print(f"Already on {main_branch} branch, no merge needed") + return True + + try: + # Make sure main branch is up to date + repo.git.checkout(main_branch) + repo.git.pull('origin', main_branch) + + # Go back to feature branch + repo.git.checkout(current_branch) + + # Merge main into current branch + repo.git.merge(main_branch) + print(f"Successfully merged {main_branch} into {current_branch}") + return True + except Exception as e: + error_msg = f"Error merging {main_branch} into {current_branch}: {str(e)}\n\n```\n{traceback.format_exc()}\n```" + print(error_msg) + + # Try to abort the merge if it's in progress + try: + repo.git.merge('--abort') + print("Merge aborted") + except: + print("Could not abort merge, it may not have been in progress") + + # Log error to issue if provided + if issue: + try: + from response_agent import llm_config + error_msg_with_signature = add_signature_to_comment( + error_msg, llm_config['model']) + except (ImportError, KeyError): + error_msg_with_signature = error_msg + \ + "\n\n---\n*This response was automatically generated by blech_bot*" + write_issue_response(issue, error_msg_with_signature) + + # Make sure we're back on the feature branch + try: + repo.git.checkout(current_branch) + except: + print(f"Could not checkout {current_branch} after failed merge") + + return False + + def push_changes(repo_path: str, branch_name: Optional[str] = None, force: bool = False) -> None: """ Push local changes to remote repository diff --git a/src/git_utils.py b/src/git_utils.py index b4edb82..c01eaf1 100644 --- a/src/git_utils.py +++ b/src/git_utils.py @@ -233,6 +233,7 @@ def update_repository(repo_path: str) -> None: git_repo = git.Repo(repo_path) origin = git_repo.remotes.origin origin.pull() + print("Repository updated with the latest changes from remote") def get_pr_branch(pr: PullRequest) -> str: @@ -265,6 +266,8 @@ def get_development_branch(issue: Issue, repo_path: str, create: bool = False) - ValueError: If gh CLI is not installed RuntimeError: If multiple branches exist for the issue """ + # Update repository before any branch operations + update_repository(repo_path) try: # Check for existing branches related to this issue related_branches = get_issue_related_branches(repo_path, issue) @@ -470,8 +473,12 @@ def push_changes_with_authentication( Args: repo_path: Path to the local git repository + out_thread: The issue or PR to log errors to branch_name: Name of the branch to push (default: current branch) """ + # Try to merge master/main branch first + from src.branch_handler import merge_master + merge_master(repo_path, out_thread) load_dotenv() token = os.getenv('GITHUB_TOKEN') From 6264ac9aa021ef07139f2c09ec6fed33f41ef191 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 6 May 2025 07:34:11 +0000 Subject: [PATCH 2/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/branch_handler.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/branch_handler.py b/src/branch_handler.py index 5559fed..e5f4aae 100644 --- a/src/branch_handler.py +++ b/src/branch_handler.py @@ -192,10 +192,10 @@ def merge_master(repo_path: str, issue=None) -> bool: """ from src.git_utils import write_issue_response, add_signature_to_comment import traceback - + repo = git.Repo(repo_path) current_branch = repo.active_branch.name - + # Determine main branch (main or master) if 'main' in repo.heads: main_branch = 'main' @@ -214,20 +214,20 @@ def merge_master(repo_path: str, issue=None) -> bool: "\n\n---\n*This response was automatically generated by blech_bot*" write_issue_response(issue, error_msg_with_signature) return False - + # Don't try to merge if we're already on the main branch if current_branch == main_branch: print(f"Already on {main_branch} branch, no merge needed") return True - + try: # Make sure main branch is up to date repo.git.checkout(main_branch) repo.git.pull('origin', main_branch) - + # Go back to feature branch repo.git.checkout(current_branch) - + # Merge main into current branch repo.git.merge(main_branch) print(f"Successfully merged {main_branch} into {current_branch}") @@ -235,14 +235,14 @@ def merge_master(repo_path: str, issue=None) -> bool: except Exception as e: error_msg = f"Error merging {main_branch} into {current_branch}: {str(e)}\n\n```\n{traceback.format_exc()}\n```" print(error_msg) - + # Try to abort the merge if it's in progress try: repo.git.merge('--abort') print("Merge aborted") except: print("Could not abort merge, it may not have been in progress") - + # Log error to issue if provided if issue: try: @@ -253,13 +253,13 @@ def merge_master(repo_path: str, issue=None) -> bool: error_msg_with_signature = error_msg + \ "\n\n---\n*This response was automatically generated by blech_bot*" write_issue_response(issue, error_msg_with_signature) - + # Make sure we're back on the feature branch try: repo.git.checkout(current_branch) except: print(f"Could not checkout {current_branch} after failed merge") - + return False From 0105b96468d0128605d4a3387bb6aa805763fbed Mon Sep 17 00:00:00 2001 From: "abuzarmahmood (aider)" Date: Tue, 6 May 2025 07:41:42 +0000 Subject: [PATCH 3/7] feat: Implement robust branch merging and update functionality --- src/branch_handler.py | 87 +++++++++++++++++++++++++++++++++++++++++++ src/git_utils.py | 4 ++ 2 files changed, 91 insertions(+) diff --git a/src/branch_handler.py b/src/branch_handler.py index e5f4aae..7df664d 100644 --- a/src/branch_handler.py +++ b/src/branch_handler.py @@ -141,6 +141,9 @@ def checkout_branch(repo_path: str, branch_name: str, create: bool = False) -> N # Force align branch with remote repo.git.reset('--hard', f'origin/{branch_name}') print(f"Branch {branch_name} aligned with remote") + # Force align branch with remote + repo.git.reset('--hard', f'origin/{branch_name}') + print(f"Branch {branch_name} aligned with remote") def delete_branch(repo_path: str, branch_name: str, force: bool = False) -> None: @@ -157,6 +160,90 @@ def delete_branch(repo_path: str, branch_name: str, force: bool = False) -> None repo.delete_head(branch_name, force=force) +def merge_master(repo_path: str, issue=None) -> bool: + """ + Merge the main/master branch into the current branch with error handling. + + Args: + repo_path: Path to local git repository + issue: The GitHub issue to log errors to (optional) + + Returns: + True if merge was successful, False otherwise + """ + from src.git_utils import write_issue_response, add_signature_to_comment + import traceback + + repo = git.Repo(repo_path) + current_branch = repo.active_branch.name + + # Determine main branch (main or master) + if 'main' in repo.heads: + main_branch = 'main' + elif 'master' in repo.heads: + main_branch = 'master' + else: + error_msg = "Neither 'main' nor 'master' branch found in repository" + print(error_msg) + if issue: + try: + from response_agent import llm_config + error_msg_with_signature = add_signature_to_comment( + error_msg, llm_config['model']) + except (ImportError, KeyError): + error_msg_with_signature = error_msg + \ + "\n\n---\n*This response was automatically generated by blech_bot*" + write_issue_response(issue, error_msg_with_signature) + return False + + # Don't try to merge if we're already on the main branch + if current_branch == main_branch: + print(f"Already on {main_branch} branch, no merge needed") + return True + + try: + # Make sure main branch is up to date + repo.git.checkout(main_branch) + repo.git.pull('origin', main_branch) + + # Go back to feature branch + repo.git.checkout(current_branch) + + # Merge main into current branch + repo.git.merge(main_branch) + print(f"Successfully merged {main_branch} into {current_branch}") + return True + except Exception as e: + error_msg = f"Error merging {main_branch} into {current_branch}: {str(e)}\n\n```\n{traceback.format_exc()}\n```" + print(error_msg) + + # Try to abort the merge if it's in progress + try: + repo.git.merge('--abort') + print("Merge aborted") + except: + print("Could not abort merge, it may not have been in progress") + + # Log error to issue if provided + if issue: + try: + from response_agent import llm_config + error_msg_with_signature = add_signature_to_comment( + error_msg, llm_config['model']) + except (ImportError, KeyError): + error_msg_with_signature = error_msg + \ + "\n\n---\n*This response was automatically generated by blech_bot*" + write_issue_response(issue, error_msg_with_signature) + + # Make sure we're back on the feature branch + try: + repo.git.checkout(current_branch) + except: + print(f"Could not checkout {current_branch} after failed merge") + + return False + + def back_to_master_branch(repo_path: str) -> None: """ Switch back to master/main branch, detecting which one exists diff --git a/src/git_utils.py b/src/git_utils.py index c01eaf1..4cb40b8 100644 --- a/src/git_utils.py +++ b/src/git_utils.py @@ -234,6 +234,7 @@ def update_repository(repo_path: str) -> None: origin = git_repo.remotes.origin origin.pull() print("Repository updated with the latest changes from remote") + print("Repository updated with the latest changes from remote") def get_pr_branch(pr: PullRequest) -> str: @@ -479,6 +480,9 @@ def push_changes_with_authentication( # Try to merge master/main branch first from src.branch_handler import merge_master merge_master(repo_path, out_thread) + # Try to merge master/main branch first + from src.branch_handler import merge_master + merge_master(repo_path, out_thread) load_dotenv() token = os.getenv('GITHUB_TOKEN') From ee31ec6af3ada9857c3ec78ebbf804db87394adc Mon Sep 17 00:00:00 2001 From: "abuzarmahmood (aider)" Date: Tue, 6 May 2025 07:57:05 +0000 Subject: [PATCH 4/7] refactor: Remove duplicate code and redundant merge calls in git_utils.py --- src/git_utils.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/git_utils.py b/src/git_utils.py index 4cb40b8..c01eaf1 100644 --- a/src/git_utils.py +++ b/src/git_utils.py @@ -234,7 +234,6 @@ def update_repository(repo_path: str) -> None: origin = git_repo.remotes.origin origin.pull() print("Repository updated with the latest changes from remote") - print("Repository updated with the latest changes from remote") def get_pr_branch(pr: PullRequest) -> str: @@ -480,9 +479,6 @@ def push_changes_with_authentication( # Try to merge master/main branch first from src.branch_handler import merge_master merge_master(repo_path, out_thread) - # Try to merge master/main branch first - from src.branch_handler import merge_master - merge_master(repo_path, out_thread) load_dotenv() token = os.getenv('GITHUB_TOKEN') From 970ec91786743c47c887d3e61043800c895b9b5e Mon Sep 17 00:00:00 2001 From: "abuzarmahmood (aider)" Date: Tue, 6 May 2025 07:57:21 +0000 Subject: [PATCH 5/7] refactor: Remove duplicate merge_master function --- src/branch_handler.py | 82 ------------------------------------------- 1 file changed, 82 deletions(-) diff --git a/src/branch_handler.py b/src/branch_handler.py index 7df664d..7c234d0 100644 --- a/src/branch_handler.py +++ b/src/branch_handler.py @@ -160,88 +160,6 @@ def delete_branch(repo_path: str, branch_name: str, force: bool = False) -> None repo.delete_head(branch_name, force=force) -def merge_master(repo_path: str, issue=None) -> bool: - """ - Merge the main/master branch into the current branch with error handling. - - Args: - repo_path: Path to local git repository - issue: The GitHub issue to log errors to (optional) - - Returns: - True if merge was successful, False otherwise - """ - from src.git_utils import write_issue_response, add_signature_to_comment - import traceback - - repo = git.Repo(repo_path) - current_branch = repo.active_branch.name - - # Determine main branch (main or master) - if 'main' in repo.heads: - main_branch = 'main' - elif 'master' in repo.heads: - main_branch = 'master' - else: - error_msg = "Neither 'main' nor 'master' branch found in repository" - print(error_msg) - if issue: - try: - from response_agent import llm_config - error_msg_with_signature = add_signature_to_comment( - error_msg, llm_config['model']) - except (ImportError, KeyError): - error_msg_with_signature = error_msg + \ - "\n\n---\n*This response was automatically generated by blech_bot*" - write_issue_response(issue, error_msg_with_signature) - return False - - # Don't try to merge if we're already on the main branch - if current_branch == main_branch: - print(f"Already on {main_branch} branch, no merge needed") - return True - - try: - # Make sure main branch is up to date - repo.git.checkout(main_branch) - repo.git.pull('origin', main_branch) - - # Go back to feature branch - repo.git.checkout(current_branch) - - # Merge main into current branch - repo.git.merge(main_branch) - print(f"Successfully merged {main_branch} into {current_branch}") - return True - except Exception as e: - error_msg = f"Error merging {main_branch} into {current_branch}: {str(e)}\n\n```\n{traceback.format_exc()}\n```" - print(error_msg) - - # Try to abort the merge if it's in progress - try: - repo.git.merge('--abort') - print("Merge aborted") - except: - print("Could not abort merge, it may not have been in progress") - - # Log error to issue if provided - if issue: - try: - from response_agent import llm_config - error_msg_with_signature = add_signature_to_comment( - error_msg, llm_config['model']) - except (ImportError, KeyError): - error_msg_with_signature = error_msg + \ - "\n\n---\n*This response was automatically generated by blech_bot*" - write_issue_response(issue, error_msg_with_signature) - - # Make sure we're back on the feature branch - try: - repo.git.checkout(current_branch) - except: - print(f"Could not checkout {current_branch} after failed merge") - - return False def back_to_master_branch(repo_path: str) -> None: From fb8ca899fef06183806cd078c5be32f9b918aab5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 6 May 2025 07:57:49 +0000 Subject: [PATCH 6/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/branch_handler.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/branch_handler.py b/src/branch_handler.py index 7c234d0..8633d87 100644 --- a/src/branch_handler.py +++ b/src/branch_handler.py @@ -160,8 +160,6 @@ def delete_branch(repo_path: str, branch_name: str, force: bool = False) -> None repo.delete_head(branch_name, force=force) - - def back_to_master_branch(repo_path: str) -> None: """ Switch back to master/main branch, detecting which one exists From 9e683b55ae656b0705a3c285ac70c174350da03e Mon Sep 17 00:00:00 2001 From: Abuzar Mahmood Date: Tue, 6 May 2025 04:12:21 -0400 Subject: [PATCH 7/7] Update branch_handler.py --- src/branch_handler.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/branch_handler.py b/src/branch_handler.py index 8633d87..e5f4aae 100644 --- a/src/branch_handler.py +++ b/src/branch_handler.py @@ -141,9 +141,6 @@ def checkout_branch(repo_path: str, branch_name: str, create: bool = False) -> N # Force align branch with remote repo.git.reset('--hard', f'origin/{branch_name}') print(f"Branch {branch_name} aligned with remote") - # Force align branch with remote - repo.git.reset('--hard', f'origin/{branch_name}') - print(f"Branch {branch_name} aligned with remote") def delete_branch(repo_path: str, branch_name: str, force: bool = False) -> None: